44 "context"
55 "crypto/ecdsa"
66 "encoding/binary"
7+ "encoding/hex"
78 "encoding/json"
89 "errors"
910 "fmt"
@@ -23,6 +24,7 @@ import (
2324
2425 "go.uber.org/zap"
2526
27+ "github.com/ethereum/go-ethereum/accounts/abi"
2628 ethcommon "github.com/ethereum/go-ethereum/common"
2729 ethtypes "github.com/ethereum/go-ethereum/core/types"
2830 "github.com/ethereum/go-ethereum/crypto"
@@ -1109,7 +1111,7 @@ func (l2 *L2) checkAndResetProbeTxNonce(ctx context.Context) {
11091111func (l2 * L2 ) handleRegistrationTx (ctx context.Context , txHash ethcommon.Hash ) {
11101112 l := logutils .LoggerFromContext (ctx )
11111113
1112- teeAddress , err := l2 .getTEEAddressFromTx (ctx , txHash )
1114+ teeAddress , rawQuote , err := l2 .getTEEAddressAndQuoteFromTx (ctx , txHash )
11131115 if err != nil {
11141116 l .Warn ("Failed to get register flashtestations transaction receipt" ,
11151117 zap .Error (err ),
@@ -1118,32 +1120,74 @@ func (l2 *L2) handleRegistrationTx(ctx context.Context, txHash ethcommon.Hash) {
11181120 return
11191121 }
11201122
1123+ workloadId , err := ComputeWorkloadID (rawQuote )
1124+ if err != nil {
1125+ l .Warn ("Failed to compute workload id" ,
1126+ zap .Error (err ),
1127+ zap .String ("tx" , txHash .Hex ()),
1128+ )
1129+ return
1130+ }
1131+
11211132 metrics .RegisteredFlashtestationsCount .Record (ctx , 1 , otelapi .WithAttributes (
11221133 attribute.KeyValue {Key : "kind" , Value : attribute .StringValue ("l2" )},
11231134 attribute.KeyValue {Key : "network_id" , Value : attribute .Int64Value (l2 .chainID .Int64 ())},
11241135 attribute.KeyValue {Key : "tee_address" , Value : attribute .StringValue (teeAddress .Hex ())},
1136+ attribute.KeyValue {Key : "workload_id" , Value : attribute .StringValue (hex .EncodeToString (workloadId [:]))},
11251137 ))
11261138
1139+ l .Info ("TEE service registered" ,
1140+ zap .String ("teeAddress" , teeAddress .Hex ()),
1141+ zap .String ("workloadId" , hex .EncodeToString (workloadId [:])),
1142+ )
1143+
11271144 return
11281145}
11291146
1130- // Extract TEE address from TEEServiceRegistered event
1131- func (l2 * L2 ) getTEEAddressFromTx (ctx context.Context , txHash ethcommon.Hash ) (ethcommon.Address , error ) {
1147+ // Extract TEE address and raw quote from TEEServiceRegistered event
1148+ func (l2 * L2 ) getTEEAddressAndQuoteFromTx (ctx context.Context , txHash ethcommon.Hash ) (ethcommon.Address , [] byte , error ) {
11321149 receipt , err := l2 .rpc .TransactionReceipt (ctx , txHash )
11331150 if err != nil {
1134- return ethcommon.Address {}, err
1151+ return ethcommon.Address {}, nil , err
11351152 }
11361153
11371154 if receipt .Status == ethtypes .ReceiptStatusFailed {
1138- return ethcommon.Address {}, fmt .Errorf ("Register tee transaction did not succeeed %s" , txHash .Hex ())
1155+ return ethcommon.Address {}, nil , fmt .Errorf ("Register tee transaction did not succeeed %s" , txHash .Hex ())
1156+ }
1157+
1158+ // Define the event arguments for decoding (non-indexed parameters only)
1159+ // event TEEServiceRegistered(address indexed teeAddress, bytes rawQuote, bool alreadyExists);
1160+ bytesType , _ := abi .NewType ("bytes" , "" , nil )
1161+ boolType , _ := abi .NewType ("bool" , "" , nil )
1162+
1163+ eventABI := abi.Arguments {
1164+ {Type : bytesType }, // rawQuote
1165+ {Type : boolType }, // alreadyExists
11391166 }
11401167
11411168 for _ , log := range receipt .Logs {
11421169 if len (log .Topics ) > 1 && log .Topics [0 ] == l2 .flashtestationsRegistryEventSignature {
11431170 // TEE address is in Topics[1] (indexed parameter)
1144- return ethcommon .BytesToAddress (log .Topics [1 ].Bytes ()), nil
1171+ teeAddress := ethcommon .BytesToAddress (log .Topics [1 ].Bytes ())
1172+
1173+ // Decode the data field to get rawQuote and alreadyExists
1174+ decoded , err := eventABI .Unpack (log .Data )
1175+ if err != nil {
1176+ return ethcommon.Address {}, nil , fmt .Errorf ("failed to decode event data: %w" , err )
1177+ }
1178+
1179+ if len (decoded ) < 2 {
1180+ return ethcommon.Address {}, nil , fmt .Errorf ("unexpected decoded data length: %d" , len (decoded ))
1181+ }
1182+
1183+ rawQuote , ok := decoded [0 ].([]byte )
1184+ if ! ok {
1185+ return ethcommon.Address {}, nil , fmt .Errorf ("failed to type assert rawQuote" )
1186+ }
1187+
1188+ return teeAddress , rawQuote , nil
11451189 }
11461190 }
11471191
1148- return ethcommon.Address {}, fmt .Errorf ("TEEServiceRegistered event not found in tx %s" , txHash .Hex ())
1192+ return ethcommon.Address {}, nil , fmt .Errorf ("TEEServiceRegistered event not found in tx %s" , txHash .Hex ())
11491193}
0 commit comments