Skip to content

Commit 9ccf67c

Browse files
committed
monitor added workload id
1 parent 00061b9 commit 9ccf67c

File tree

5 files changed

+124
-19
lines changed

5 files changed

+124
-19
lines changed

cmd/serve.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,24 @@ func CommandServe(cfg *config.Config) *cli.Command {
134134
Value: "permitVerifyBlockBuilderProof(uint8,bytes32,uint256,bytes)",
135135
},
136136

137+
&cli.StringFlag{ // --l2-monitor-builder-policy-add-workload-id-signature
138+
Category: strings.ToUpper(categoryL2),
139+
Destination: &cfg.L2.MonitorBuilderPolicyAddWorkloadIdSignature,
140+
EnvVars: []string{envPrefix + strings.ToUpper(categoryL2) + "_MONITOR_BUILDER_POLICY_ADD_WORKLOAD_ID_SIGNATURE"},
141+
Name: categoryL2 + "-monitor-builder-policy-add-workload-id-signature",
142+
Usage: "l2 builder policy function `signature` to add workload id",
143+
Value: "addWorkloadToPolicy(bytes32,string,string[])",
144+
},
145+
146+
&cli.StringFlag{ // --l2-monitor-builder-policy-add-workload-id-event-signature
147+
Category: strings.ToUpper(categoryL2),
148+
Destination: &cfg.L2.MonitorBuilderPolicyAddWorkloadIdEventSignature,
149+
EnvVars: []string{envPrefix + strings.ToUpper(categoryL2) + "_MONITOR_BUILDER_POLICY_ADD_WORKLOAD_ID_EVENT_SIGNATURE"},
150+
Name: categoryL2 + "-monitor-builder-policy-add-workload-id-event-signature",
151+
Usage: "l2 builder policy event `signature` to add workload id",
152+
Value: "WorkloadAddedToPolicy(bytes32)",
153+
},
154+
137155
&cli.StringFlag{ // --l2-monitor-flashtestations-registry-contract
138156
Category: strings.ToUpper(categoryL2),
139157
Destination: &cfg.L2.MonitorFlashtestationRegistryContract,
@@ -151,6 +169,15 @@ func CommandServe(cfg *config.Config) *cli.Command {
151169
Value: "permitRegisterTEEService(bytes,bytes,uint256,uint256,bytes)",
152170
},
153171

172+
&cli.StringFlag{ // --l2-monitor-flashtestations-registry-event-signature
173+
Category: strings.ToUpper(categoryL2),
174+
Destination: &cfg.L2.MonitorFlashtestationRegistryEventSignature,
175+
EnvVars: []string{envPrefix + strings.ToUpper(categoryL2) + "_MONITOR_FLASHTESTATIONS_REGISTRY_EVENT_SIGNATURE"},
176+
Name: categoryL2 + "-monitor-flashtestations-registry-event-signature",
177+
Usage: "l2 builder flashtestations registry contract event `signature` to monitor",
178+
Value: "TEEServiceRegistered(address,bytes,bool)",
179+
},
180+
154181
&cli.StringFlag{ // --l2-monitor-flashblock-number-contract
155182
Category: strings.ToUpper(categoryL2),
156183
Destination: &cfg.L2.MonitorFlashblockNumberContract,

config/l2.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type L2 struct {
2525
MonitorBuilderAddress string `yaml:"monitor_builder_address"`
2626
MonitorBuilderPolicyContract string `yaml:"monitor_builder_policy_contract"`
2727
MonitorBuilderPolicyContractFunctionSignature string `yaml:"monitor_builder_policy_contract_function_signature"`
28+
MonitorBuilderPolicyAddWorkloadIdSignature string `yaml:"monitor_builder_policy_add_workload_id_signature"`
29+
MonitorBuilderPolicyAddWorkloadIdEventSignature string `yaml:"monitor_builder_policy_add_workload_id_event_signature"`
2830
MonitorFlashblockNumberContract string `yaml:"monitor_builder_flashblock_number_contract"`
2931
MonitorFlashblockNumberContractFunctionSignature string `yaml:"monitor_builder_flashblock_number_contract_function_signature"`
3032
MonitorFlashtestationRegistryContract string `yaml:"monitor_flashtestation_registry_contract"`

metrics/exports.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
FlashtestationsLandedCount otelapi.Int64Gauge
2222
FlashtestationsMissedCount otelapi.Int64Gauge
2323
RegisteredFlashtestationsCount otelapi.Int64Gauge
24+
WorkloadAddedToPolicyCount otelapi.Int64Gauge
2425

2526
ReorgsCount otelapi.Int64Counter
2627
ReorgDepth otelapi.Int64Gauge
@@ -59,6 +60,7 @@ var (
5960
setupFlashtestationsLandedCount,
6061
setupFlashtestationsMissedCount,
6162
setupRegisteredFlashtestationsCount,
63+
setupWorkloadAddedToPolicyCount,
6264

6365
setupReorgsCount,
6466
setupReorgDepth,

metrics/metrics.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ func setupRegisteredFlashtestationsCount(ctx context.Context, _ *config.ProbeTx)
166166
return nil
167167
}
168168

169+
func setupWorkloadAddedToPolicyCount(ctx context.Context, _ *config.ProbeTx) error {
170+
m, err := meter.Int64Gauge("workload_added_to_policy_count",
171+
otelapi.WithDescription("workload added to policy count"),
172+
)
173+
if err != nil {
174+
return err
175+
}
176+
WorkloadAddedToPolicyCount = m
177+
return nil
178+
}
179+
169180
func setupBlocksSeenCount(ctx context.Context, _ *config.ProbeTx) error {
170181
m, err := meter.Int64Gauge("blocks_seen_count",
171182
otelapi.WithDescription("blocks seen by the monitor"),

server/l2.go

Lines changed: 82 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

334335
func (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+
787814
func (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
11481211
func (l2 *L2) getTEEAddressAndQuoteFromTx(ctx context.Context, txHash ethcommon.Hash) (ethcommon.Address, []byte, error) {
11491212
receipt, err := l2.rpc.TransactionReceipt(ctx, txHash)

0 commit comments

Comments
 (0)