Skip to content

Commit 88bb56c

Browse files
85 faultproof withdrawals does not report connection errors metrics correctly (#90)
* fixed bug, of not setting correct number of nodeconnection errors on metrics * fixed bugs and added features. Still missing proper metrics * fixing metrics * fixing metrics * fixes * completing changes * Update op-monitorism/faultproof_withdrawals/validator/optimism_portal2_helper.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fixing review issues * fixing review issues * fixing review issues * fixing review issues * fixing review issues * fixing review issues * Update op-monitorism/faultproof_withdrawals/monitor_live_sepolia_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update op-monitorism/faultproof_withdrawals/monitor_live_sepolia_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fixing review issues * Update op-monitorism/faultproof_withdrawals/monitor_live_mainnet_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fixing review issues * fixing review issues * Apply suggestions from code review Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fixing review issues * fixing review issues * fixing tests * fixing libraries * fixing minor issues * Update state.go Adding extra comment description * fixing comment * fxing one piece of code * cleaning up and fixing documentation --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent b2fe04b commit 88bb56c

15 files changed

+709
-426
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ This component is designed to work exclusively with chains that are already util
131131
This is a new version of the deprecated [chain-mon faultproof-wd-mon](https://github.com/ethereum-optimism/optimism/tree/chain-mon/v1.2.1/packages/chain-mon/src/faultproof-wd-mon).
132132
For detailed information on how the component works and the algorithms used, please refer to the component README.
133133

134-
| `op-monitorism/faultproof-withdrawals` | [README](https://github.com/ethereum-optimism/monitorism/blob/main/op-monitorism/faultproof-withdrawals/README.md) |
134+
| `op-monitorism/faultproof-withdrawals` | [README](https://github.com/ethereum-optimism/monitorism/blob/main/op-monitorism/faultproof_withdrawals/README.md) |
135135
| ----------------------- | --------------------------------------------------------------------------------------------------- |
136136

137137

op-monitorism/README.md

Lines changed: 0 additions & 146 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FAULTPROOF_WITHDRAWAL_MON_L1_GETH_URL="<geth-url>"
2+
FAULTPROOF_WITHDRAWAL_MON_L2_OP_NODE_URL="<op-geth-node>"
3+
FAULTPROOF_WITHDRAWAL_MON_L2_OP_GETH_URL="<op-geth-url>"
4+
FAULTPROOF_WITHDRAWAL_MON_OPTIMISM_PORTAL="0xbEb5Fc579115071764c7423A4f12eDde41f106Ed" # This is the address of the Optimism portal contract, this should be for the chain you are monitoring
5+
FAULTPROOF_WITHDRAWAL_MON_START_BLOCK_HEIGHT=20872390 # This is the block height from which the monitoring will start, decide at which block height you want to start monitoring
6+
FAULTPROOF_WITHDRAWAL_MON_EVENT_BLOCK_RANGE=1000 # This is the range of blocks to be monitored
7+
MONITORISM_LOOP_INTERVAL_MSEC=100 # This is the interval in milliseconds for the monitoring loop
8+
MONITORISM_METRICS_PORT=7300 # This is the port on which the metrics server will run
9+
MONITORISM_METRICS_ENABLED=true # This is the flag to enable/disable the metrics server

op-monitorism/faultproof_withdrawals/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ or
2222
cd ../
2323
go run ./cmd/monitorism faultproof_withdrawals --metrics.enabled --metrics.port 7300
2424
```
25-
## Available Metrics and Meaning
26-
2725

2826
# Cli options
2927

op-monitorism/faultproof_withdrawals/monitor.go

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/ethereum-optimism/monitorism/op-monitorism/faultproof_withdrawals/validator"
1010
"github.com/ethereum-optimism/optimism/op-service/metrics"
1111

12+
"github.com/ethereum/go-ethereum/common"
1213
"github.com/ethereum/go-ethereum/ethclient"
1314
"github.com/ethereum/go-ethereum/log"
1415
)
@@ -123,14 +124,14 @@ func NewMonitor(ctx context.Context, log log.Logger, m metrics.Factory, cfg CLIC
123124
startingL1BlockHeight = uint64(cfg.StartingL1BlockHeight)
124125
}
125126

126-
state, err := NewState(log, startingL1BlockHeight, latestL1Height)
127+
state, err := NewState(log, startingL1BlockHeight, latestL1Height, ret.withdrawalValidator.GetLatestL2Height())
127128
if err != nil {
128129
return nil, fmt.Errorf("failed to create state: %w", err)
129130
}
130131
ret.state = *state
131132

132133
// log state and metrics
133-
ret.state.LogState(ret.log)
134+
ret.state.LogState()
134135
ret.metrics.UpdateMetricsFromState(&ret.state)
135136

136137
return ret, nil
@@ -228,6 +229,10 @@ func (m *Monitor) GetMaxBlock() (uint64, error) {
228229
// Run executes the main monitoring loop.
229230
// It retrieves new events, processes them, and updates the state accordingly.
230231
func (m *Monitor) Run(ctx context.Context) {
232+
// Defer the update function
233+
defer m.metrics.UpdateMetricsFromState(&m.state)
234+
defer m.state.LogState()
235+
231236
start := m.state.nextL1Height
232237

233238
stop, err := m.GetMaxBlock()
@@ -238,18 +243,16 @@ func (m *Monitor) Run(ctx context.Context) {
238243
}
239244

240245
// review previous invalidProposalWithdrawalsEvents
241-
invalidProposalWithdrawalsEvents, err := m.ConsumeEvents(m.state.invalidProposalWithdrawalsEvents)
246+
err = m.ConsumeEvents(m.state.potentialAttackOnInProgressGames)
242247
if err != nil {
248+
m.state.nodeConnectionFailures++
243249
m.log.Error("failed to consume events", "error", err)
244250
return
245251
}
246252

247-
// update state
248-
m.state.invalidProposalWithdrawalsEvents = *invalidProposalWithdrawalsEvents
249-
250253
// get new events
251-
252-
newEvents, err := m.withdrawalValidator.GetEnrichedWithdrawalsEvents(start, &stop)
254+
m.log.Info("getting enriched withdrawal events", "start", fmt.Sprintf("%d", start), "stop", fmt.Sprintf("%d", stop))
255+
newEvents, err := m.withdrawalValidator.GetEnrichedWithdrawalsEventsMap(start, &stop)
253256
if err != nil {
254257
if start >= stop {
255258
m.log.Info("no new events to process", "start", start, "stop", stop)
@@ -262,99 +265,76 @@ func (m *Monitor) Run(ctx context.Context) {
262265
}
263266
return
264267
}
265-
newInvalidProposalWithdrawalsEvents, err := m.ConsumeEvents(newEvents)
268+
269+
err = m.ConsumeEvents(newEvents)
266270
if err != nil {
271+
m.state.nodeConnectionFailures++
267272
m.log.Error("failed to consume events", "error", err)
268273
return
269274
}
270275

271-
// update state
272-
if len(*newInvalidProposalWithdrawalsEvents) > 0 && newInvalidProposalWithdrawalsEvents != nil {
273-
m.state.invalidProposalWithdrawalsEvents = append(m.state.invalidProposalWithdrawalsEvents, *newInvalidProposalWithdrawalsEvents...)
274-
}
275-
276276
// update state
277277
m.state.nextL1Height = stop
278278

279-
// log state and metrics
280-
m.state.LogState(m.log)
281-
m.metrics.UpdateMetricsFromState(&m.state)
282279
}
283280

284281
// ConsumeEvents processes a slice of enriched withdrawal events and updates their states.
285282
// It returns any events detected during the consumption that requires to be re-analysed again at a later stage (when the event referenced DisputeGame completes).
286-
func (m *Monitor) ConsumeEvents(enrichedWithdrawalEvent []validator.EnrichedProvenWithdrawalEvent) (*[]validator.EnrichedProvenWithdrawalEvent, error) {
287-
var newForgeriesGameInProgressEvent []validator.EnrichedProvenWithdrawalEvent = make([]validator.EnrichedProvenWithdrawalEvent, 0)
288-
for _, enrichedWithdrawalEvent := range enrichedWithdrawalEvent {
283+
func (m *Monitor) ConsumeEvents(enrichedWithdrawalEvents map[common.Hash]validator.EnrichedProvenWithdrawalEvent) error {
284+
for _, enrichedWithdrawalEvent := range enrichedWithdrawalEvents {
285+
m.log.Info("processing withdrawal event", "event", &enrichedWithdrawalEvent)
289286
err := m.withdrawalValidator.UpdateEnrichedWithdrawalEvent(&enrichedWithdrawalEvent)
287+
//upgrade state to the latest L2 height after the event is processed
288+
m.state.latestL2Height = m.withdrawalValidator.GetLatestL2Height()
290289
if err != nil {
291-
m.state.nodeConnectionFailures++
292290
m.log.Error("failed to update enriched withdrawal event", "error", err)
293-
return nil, err
291+
return err
294292
}
295293

296-
consumedEvent, err := m.ConsumeEvent(enrichedWithdrawalEvent)
294+
err = m.ConsumeEvent(enrichedWithdrawalEvent)
297295
if err != nil {
298296
m.log.Error("failed to consume event", "error", err)
299-
return nil, err
300-
} else if !consumedEvent {
301-
newForgeriesGameInProgressEvent = append(newForgeriesGameInProgressEvent, enrichedWithdrawalEvent)
297+
return err
302298
}
303299
}
304300

305-
return &newForgeriesGameInProgressEvent, nil
301+
return nil
306302
}
307303

308304
// ConsumeEvent processes a single enriched withdrawal event.
309305
// It logs the event details and checks for any forgery detection.
310-
func (m *Monitor) ConsumeEvent(enrichedWithdrawalEvent validator.EnrichedProvenWithdrawalEvent) (bool, error) {
311-
m.log.Info("processing withdrawal event", "event", enrichedWithdrawalEvent.Event)
306+
func (m *Monitor) ConsumeEvent(enrichedWithdrawalEvent validator.EnrichedProvenWithdrawalEvent) error {
312307
if enrichedWithdrawalEvent.DisputeGame.DisputeGameData.L2ChainID.Cmp(m.l2ChainID) != 0 {
313308
m.log.Error("l2ChainID mismatch", "expected", fmt.Sprintf("%d", m.l2ChainID), "got", fmt.Sprintf("%d", enrichedWithdrawalEvent.DisputeGame.DisputeGameData.L2ChainID))
314309
}
315310
valid, err := m.withdrawalValidator.IsWithdrawalEventValid(&enrichedWithdrawalEvent)
316311
if err != nil {
317-
m.state.nodeConnectionFailures++
318312
m.log.Error("failed to check if forgery detected", "error", err)
319-
return false, err
313+
return err
320314
}
321-
eventConsumed := false
322315

323316
if !valid {
324-
m.state.numberOfInvalidWithdrawals++
325317
if !enrichedWithdrawalEvent.Blacklisted {
326318
if enrichedWithdrawalEvent.DisputeGame.DisputeGameData.Status == validator.CHALLENGER_WINS {
327-
m.log.Warn("WITHDRAWAL: is NOT valid, but the game is correctly resolved", "enrichedWithdrawalEvent", enrichedWithdrawalEvent)
328-
m.state.withdrawalsValidated++
329-
eventConsumed = true
319+
m.state.IncrementSuspiciousEventsOnChallengerWinsGames(enrichedWithdrawalEvent)
330320
} else if enrichedWithdrawalEvent.DisputeGame.DisputeGameData.Status == validator.DEFENDER_WINS {
331-
m.log.Error("WITHDRAWAL: is NOT valid, forgery detected", "enrichedWithdrawalEvent", enrichedWithdrawalEvent)
332-
m.state.numberOfDetectedForgery++
333-
// add to forgeries
334-
m.state.forgeriesWithdrawalsEvents = append(m.state.forgeriesWithdrawalsEvents, enrichedWithdrawalEvent)
335-
eventConsumed = true
321+
m.state.IncrementPotentialAttackOnDefenderWinsGames(enrichedWithdrawalEvent)
336322
} else if enrichedWithdrawalEvent.DisputeGame.DisputeGameData.Status == validator.IN_PROGRESS {
337-
m.log.Warn("WITHDRAWAL: is NOT valid, game is still in progress.", "enrichedWithdrawalEvent", enrichedWithdrawalEvent)
323+
m.state.IncrementPotentialAttackOnInProgressGames(enrichedWithdrawalEvent)
338324
// add to events to be re-processed
339-
eventConsumed = false
340325
} else {
341326
m.log.Error("WITHDRAWAL: is NOT valid, game status is unknown. UNKNOWN STATE SHOULD NEVER HAPPEN", "enrichedWithdrawalEvent", enrichedWithdrawalEvent)
342-
eventConsumed = false
343327
}
344328

345329
} else {
346-
m.log.Warn("WITHDRAWAL: is NOT valid, but game is blacklisted", "enrichedWithdrawalEvent", enrichedWithdrawalEvent)
347-
m.state.withdrawalsValidated++
348-
eventConsumed = true
330+
m.state.IncrementSuspiciousEventsOnChallengerWinsGames(enrichedWithdrawalEvent)
349331
}
350332
} else {
351-
m.log.Info("WITHDRAWAL: is valid", "enrichedWithdrawalEvent", enrichedWithdrawalEvent)
352-
m.state.withdrawalsValidated++
353-
eventConsumed = true
333+
m.state.IncrementWithdrawalsValidated(enrichedWithdrawalEvent)
354334
}
355-
m.state.processedProvenWithdrawalsExtension1Events++
335+
m.state.eventsProcessed++
356336
m.metrics.UpdateMetricsFromState(&m.state)
357-
return eventConsumed, nil
337+
return nil
358338
}
359339

360340
// Close gracefully shuts down the Monitor by closing the Geth clients.

0 commit comments

Comments
 (0)