@@ -41,12 +41,13 @@ type L2 struct {
4141
4242 builderAddr ethcommon.Address
4343
44- builderPolicyAddr ethcommon.Address
45- builderPolicySignature [4 ]byte
46- flashtestationsRegistryAddr ethcommon.Address
47- flashtestationsRegistrySignature [4 ]byte
48- flashtestationsRegistryEventSignature ethcommon.Hash
49- registeredTx chan ethcommon.Hash
44+ builderPolicyAddr ethcommon.Address
45+ builderPolicySignature [4 ]byte
46+ builderPolicyAddWorkloadIdSignature [4 ]byte
47+ builderPolicyAddWorkloadIdEventSignature ethcommon.Hash
48+ flashtestationsRegistryAddr ethcommon.Address
49+ flashtestationsRegistrySignature [4 ]byte
50+ flashtestationsRegistryEventSignature ethcommon.Hash
5051
5152 flashblockNumberAddr ethcommon.Address
5253 flashblockNumberSignature [4 ]byte
@@ -134,6 +135,16 @@ func newL2(cfg *config.L2) (*L2, error) {
134135 copy (l2 .builderPolicySignature [:], h [:4 ])
135136 }
136137
138+ if cfg .MonitorBuilderPolicyAddWorkloadIdSignature != "" {
139+ h := crypto .Keccak256Hash ([]byte (cfg .MonitorBuilderPolicyAddWorkloadIdSignature ))
140+ copy (l2 .builderPolicyAddWorkloadIdSignature [:], h [:4 ])
141+ }
142+
143+ if cfg .MonitorBuilderPolicyAddWorkloadIdEventSignature != "" {
144+ h := crypto .Keccak256Hash ([]byte (cfg .MonitorBuilderPolicyAddWorkloadIdEventSignature ))
145+ copy (l2 .builderPolicyAddWorkloadIdEventSignature [:], h [:])
146+ }
147+
137148 if cfg .MonitorFlashtestationRegistryContract != "" {
138149 addr , err := ethcommon .ParseHexOrString (cfg .MonitorFlashtestationRegistryContract )
139150 if err != nil {
@@ -319,16 +330,6 @@ func (l2 *L2) run(ctx context.Context) {
319330 }
320331 }()
321332 }
322-
323- go func () {
324- for {
325- select {
326- case txHash := <- l2 .registeredTx :
327- // Process the registration transaction
328- l2 .handleRegistrationTx (ctx , txHash )
329- }
330- }
331- }()
332333}
333334
334335func (l2 * L2 ) persist () error {
@@ -511,18 +512,26 @@ func (l2 *L2) processBlock(ctx context.Context, blockNumber uint64) error {
511512 builderTxCount ++
512513 }
513514
514- if l2 .cfg .MonitorBuilderPolicyContract != "" && l2 .isBuilderPolicyTx (tx ) {
515+ if l2 .cfg .MonitorBuilderPolicyContract != "" && l2 .isBuilderPolicyBlockProofTx (tx ) {
515516 flashtestationsTxCount ++
516517 builderTxCount ++
517518 }
518519
520+ if l2 .cfg .MonitorBuilderPolicyContract != "" && l2 .isBuilderPolicyAddWorkloadIdTx (tx ) {
521+ go func () {
522+ l2 .handleAddWorkloadIdTx (ctx , tx .Hash ())
523+ }()
524+ }
525+
519526 if l2 .cfg .MonitorFlashblockNumberContract != "" && l2 .isFlashblockNumberTx (ctx , block , tx ) {
520527 flashblockNumberTxCount ++
521528 builderTxCount ++
522529 }
523530
524531 if l2 .cfg .MonitorFlashtestationRegistryContract != "" && l2 .isFlashtestationsRegisterTx (tx ) {
525- l2 .registeredTx <- tx .Hash ()
532+ go func () {
533+ l2 .handleRegistrationTx (ctx , tx .Hash ())
534+ }()
526535 }
527536
528537 if l2 .monitorKey != nil {
@@ -766,7 +775,7 @@ func (l2 *L2) isBuilderTx(
766775 return from .Cmp (l2 .builderAddr ) == 0
767776}
768777
769- func (l2 * L2 ) isBuilderPolicyTx (
778+ func (l2 * L2 ) isBuilderPolicyBlockProofTx (
770779 tx * ethtypes.Transaction ,
771780) bool {
772781 if tx == nil || tx .Rejected () {
@@ -784,6 +793,24 @@ func (l2 *L2) isBuilderPolicyTx(
784793 return slices .Compare (tx .Data ()[:4 ], l2 .builderPolicySignature [:]) == 0
785794}
786795
796+ func (l2 * L2 ) isBuilderPolicyAddWorkloadIdTx (
797+ tx * ethtypes.Transaction ,
798+ ) bool {
799+ if tx == nil || tx .Rejected () {
800+ return false
801+ }
802+
803+ if tx .To () == nil || tx .To ().Cmp (l2 .builderPolicyAddr ) != 0 {
804+ return false
805+ }
806+
807+ if len (tx .Data ()) < len (l2 .builderPolicyAddWorkloadIdSignature ) {
808+ return false
809+ }
810+
811+ return slices .Compare (tx .Data ()[:4 ], l2 .builderPolicyAddWorkloadIdSignature [:]) == 0
812+ }
813+
787814func (l2 * L2 ) isFlashtestationsRegisterTx (
788815 tx * ethtypes.Transaction ,
789816) bool {
@@ -1144,6 +1171,42 @@ func (l2 *L2) handleRegistrationTx(ctx context.Context, txHash ethcommon.Hash) {
11441171 return
11451172}
11461173
1174+ func (l2 * L2 ) handleAddWorkloadIdTx (ctx context.Context , txHash ethcommon.Hash ) {
1175+ l := logutils .LoggerFromContext (ctx )
1176+
1177+ receipt , err := l2 .rpc .TransactionReceipt (ctx , txHash )
1178+ if err != nil {
1179+ l .Warn ("Failed to get add workload id transaction receipt" ,
1180+ zap .Error (err ),
1181+ zap .String ("tx" , txHash .Hex ()),
1182+ )
1183+ }
1184+
1185+ if receipt .Status == ethtypes .ReceiptStatusFailed {
1186+ l .Warn ("Add workload id transaction did not succeed" ,
1187+ zap .String ("tx" , txHash .Hex ()),
1188+ )
1189+ return
1190+ }
1191+
1192+ for _ , log := range receipt .Logs {
1193+ if len (log .Topics ) > 1 && log .Topics [0 ] == l2 .builderPolicyAddWorkloadIdEventSignature {
1194+ workloadId := ethcommon .BytesToAddress (log .Topics [1 ].Bytes ())
1195+ l .Info ("Workload added to policy" ,
1196+ zap .String ("workloadId" , hex .EncodeToString (workloadId [:])),
1197+ )
1198+ metrics .WorkloadAddedToPolicyCount .Record (ctx , 1 , otelapi .WithAttributes (
1199+ attribute.KeyValue {Key : "kind" , Value : attribute .StringValue ("l2" )},
1200+ attribute.KeyValue {Key : "network_id" , Value : attribute .Int64Value (l2 .chainID .Int64 ())},
1201+ attribute.KeyValue {Key : "workload_id" , Value : attribute .StringValue (hex .EncodeToString (workloadId [:]))},
1202+ ))
1203+ return
1204+ }
1205+ }
1206+
1207+ return
1208+ }
1209+
11471210// Extract TEE address and raw quote from TEEServiceRegistered event
11481211func (l2 * L2 ) getTEEAddressAndQuoteFromTx (ctx context.Context , txHash ethcommon.Hash ) (ethcommon.Address , []byte , error ) {
11491212 receipt , err := l2 .rpc .TransactionReceipt (ctx , txHash )
0 commit comments