Skip to content

Commit

Permalink
Improving metrics (#94)
Browse files Browse the repository at this point in the history
* passing events via reference instead of keeping copung them. Adding the precessed time in the event to keep track of when they have being processed

* fixed prints and comments

* check for nil
  • Loading branch information
raffaele-oplabs authored Oct 14, 2024
1 parent 88bb56c commit dd04bbd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 40 deletions.
14 changes: 9 additions & 5 deletions op-monitorism/faultproof_withdrawals/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,14 @@ func (m *Monitor) Run(ctx context.Context) {

// ConsumeEvents processes a slice of enriched withdrawal events and updates their states.
// 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).
func (m *Monitor) ConsumeEvents(enrichedWithdrawalEvents map[common.Hash]validator.EnrichedProvenWithdrawalEvent) error {
func (m *Monitor) ConsumeEvents(enrichedWithdrawalEvents map[common.Hash]*validator.EnrichedProvenWithdrawalEvent) error {
for _, enrichedWithdrawalEvent := range enrichedWithdrawalEvents {
m.log.Info("processing withdrawal event", "event", &enrichedWithdrawalEvent)
err := m.withdrawalValidator.UpdateEnrichedWithdrawalEvent(&enrichedWithdrawalEvent)
if enrichedWithdrawalEvent == nil {
m.log.Error("WITHDRAWAL: enrichedWithdrawalEvent is nil in ConsumeEvents")
panic("WITHDRAWAL: enrichedWithdrawalEvent is nil in ConsumeEvents")
}
m.log.Info("processing withdrawal event", "event", enrichedWithdrawalEvent)
err := m.withdrawalValidator.UpdateEnrichedWithdrawalEvent(enrichedWithdrawalEvent)
//upgrade state to the latest L2 height after the event is processed
m.state.latestL2Height = m.withdrawalValidator.GetLatestL2Height()
if err != nil {
Expand All @@ -303,11 +307,11 @@ func (m *Monitor) ConsumeEvents(enrichedWithdrawalEvents map[common.Hash]validat

// ConsumeEvent processes a single enriched withdrawal event.
// It logs the event details and checks for any forgery detection.
func (m *Monitor) ConsumeEvent(enrichedWithdrawalEvent validator.EnrichedProvenWithdrawalEvent) error {
func (m *Monitor) ConsumeEvent(enrichedWithdrawalEvent *validator.EnrichedProvenWithdrawalEvent) error {
if enrichedWithdrawalEvent.DisputeGame.DisputeGameData.L2ChainID.Cmp(m.l2ChainID) != 0 {
m.log.Error("l2ChainID mismatch", "expected", fmt.Sprintf("%d", m.l2ChainID), "got", fmt.Sprintf("%d", enrichedWithdrawalEvent.DisputeGame.DisputeGameData.L2ChainID))
}
valid, err := m.withdrawalValidator.IsWithdrawalEventValid(&enrichedWithdrawalEvent)
valid, err := m.withdrawalValidator.IsWithdrawalEventValid(enrichedWithdrawalEvent)
if err != nil {
m.log.Error("failed to check if forgery detected", "error", err)
return err
Expand Down
20 changes: 10 additions & 10 deletions op-monitorism/faultproof_withdrawals/monitor_live_sepolia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func TestConsumeEventValid_DEFENDER_WINS_Sepolia(t *testing.T) {
},
}

eventsMap := map[common.Hash]validator.EnrichedProvenWithdrawalEvent{
validEvent.Event.WithdrawalHash: validEvent,
eventsMap := map[common.Hash]*validator.EnrichedProvenWithdrawalEvent{
validEvent.Event.WithdrawalHash: &validEvent,
}
err := test_monitor.ConsumeEvents(eventsMap)
require.NoError(t, err)
Expand Down Expand Up @@ -179,8 +179,8 @@ func TestConsumeEventValid_CHALLENGER_WINS_Sepolia(t *testing.T) {
},
}

eventsMap := map[common.Hash]validator.EnrichedProvenWithdrawalEvent{
event.Event.WithdrawalHash: event,
eventsMap := map[common.Hash]*validator.EnrichedProvenWithdrawalEvent{
event.Event.WithdrawalHash: &event,
}
err := test_monitor.ConsumeEvents(eventsMap)
require.NoError(t, err)
Expand Down Expand Up @@ -230,8 +230,8 @@ func TestConsumeEventValid_BlacklistedSepolia(t *testing.T) {
},
}

eventsMap := map[common.Hash]validator.EnrichedProvenWithdrawalEvent{
event.Event.WithdrawalHash: event,
eventsMap := map[common.Hash]*validator.EnrichedProvenWithdrawalEvent{
event.Event.WithdrawalHash: &event,
}
err := test_monitor.ConsumeEvents(eventsMap)
require.NoError(t, err)
Expand Down Expand Up @@ -280,8 +280,8 @@ func TestConsumeEventForgery1Sepolia(t *testing.T) {
},
}

eventsMap := map[common.Hash]validator.EnrichedProvenWithdrawalEvent{
validEvent.Event.WithdrawalHash: validEvent,
eventsMap := map[common.Hash]*validator.EnrichedProvenWithdrawalEvent{
validEvent.Event.WithdrawalHash: &validEvent,
}
err := test_monitor.ConsumeEvents(eventsMap)
require.NoError(t, err)
Expand Down Expand Up @@ -330,8 +330,8 @@ func TestConsumeEventForgery2Sepolia(t *testing.T) {
},
}

eventsMap := map[common.Hash]validator.EnrichedProvenWithdrawalEvent{
event.Event.WithdrawalHash: event,
eventsMap := map[common.Hash]*validator.EnrichedProvenWithdrawalEvent{
event.Event.WithdrawalHash: &event,
}
err := test_monitor.ConsumeEvents(eventsMap)
require.NoError(t, err)
Expand Down
51 changes: 29 additions & 22 deletions op-monitorism/faultproof_withdrawals/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package faultproof_withdrawals
import (
"fmt"
"math"
"time"

"github.com/ethereum-optimism/monitorism/op-monitorism/faultproof_withdrawals/validator"
"github.com/ethereum-optimism/optimism/op-service/metrics"
Expand Down Expand Up @@ -32,12 +33,12 @@ type State struct {
// possible attacks detected

// Forgeries detected on games that are already resolved
potentialAttackOnDefenderWinsGames map[common.Hash]validator.EnrichedProvenWithdrawalEvent
potentialAttackOnDefenderWinsGames map[common.Hash]*validator.EnrichedProvenWithdrawalEvent
numberOfPotentialAttacksOnDefenderWinsGames uint64

// Forgeries detected on games that are still in progress
// Faultproof system should make them invalid
potentialAttackOnInProgressGames map[common.Hash]validator.EnrichedProvenWithdrawalEvent
potentialAttackOnInProgressGames map[common.Hash]*validator.EnrichedProvenWithdrawalEvent
numberOfPotentialAttackOnInProgressGames uint64

// Suspicious events
Expand All @@ -54,7 +55,7 @@ func NewState(logger log.Logger, nextL1Height uint64, latestL1Height uint64, lat
}

ret := State{
potentialAttackOnDefenderWinsGames: make(map[common.Hash]validator.EnrichedProvenWithdrawalEvent),
potentialAttackOnDefenderWinsGames: make(map[common.Hash]*validator.EnrichedProvenWithdrawalEvent),
numberOfPotentialAttacksOnDefenderWinsGames: 0,
suspiciousEventsOnChallengerWinsGames: func() *lru.Cache {
cache, err := lru.New(suspiciousEventsOnChallengerWinsGamesCacheSize)
Expand All @@ -66,7 +67,7 @@ func NewState(logger log.Logger, nextL1Height uint64, latestL1Height uint64, lat
}(),
numberOfSuspiciousEventsOnChallengerWinsGames: 0,

potentialAttackOnInProgressGames: make(map[common.Hash]validator.EnrichedProvenWithdrawalEvent),
potentialAttackOnInProgressGames: make(map[common.Hash]*validator.EnrichedProvenWithdrawalEvent),
numberOfPotentialAttackOnInProgressGames: 0,

eventsProcessed: 0,
Expand Down Expand Up @@ -106,56 +107,61 @@ func (s *State) LogState() {
)
}

func (s *State) IncrementWithdrawalsValidated(enrichedWithdrawalEvent validator.EnrichedProvenWithdrawalEvent) {
s.logger.Info("STATE WITHDRAWAL: valid", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", &enrichedWithdrawalEvent)
func (s *State) IncrementWithdrawalsValidated(enrichedWithdrawalEvent *validator.EnrichedProvenWithdrawalEvent) {
s.logger.Info("STATE WITHDRAWAL: valid", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", enrichedWithdrawalEvent)
s.withdrawalsProcessed++
enrichedWithdrawalEvent.ProcessedTimeStamp = float64(time.Now().Unix())
}

func (s *State) IncrementPotentialAttackOnDefenderWinsGames(enrichedWithdrawalEvent validator.EnrichedProvenWithdrawalEvent) {
func (s *State) IncrementPotentialAttackOnDefenderWinsGames(enrichedWithdrawalEvent *validator.EnrichedProvenWithdrawalEvent) {
key := enrichedWithdrawalEvent.Event.Raw.TxHash

s.logger.Error("STATE WITHDRAWAL: is NOT valid, forgery detected", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", &enrichedWithdrawalEvent)
s.logger.Error("STATE WITHDRAWAL: is NOT valid, forgery detected", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", enrichedWithdrawalEvent)
s.potentialAttackOnDefenderWinsGames[key] = enrichedWithdrawalEvent
s.numberOfPotentialAttacksOnDefenderWinsGames++

if _, ok := s.potentialAttackOnInProgressGames[key]; ok {
s.logger.Error("STATE WITHDRAWAL: added to potential attacks. Removing from inProgress", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", &enrichedWithdrawalEvent)
s.logger.Error("STATE WITHDRAWAL: added to potential attacks. Removing from inProgress", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", enrichedWithdrawalEvent)
delete(s.potentialAttackOnInProgressGames, key)
s.numberOfPotentialAttackOnInProgressGames--
}

s.withdrawalsProcessed++
enrichedWithdrawalEvent.ProcessedTimeStamp = float64(time.Now().Unix())

}

func (s *State) IncrementPotentialAttackOnInProgressGames(enrichedWithdrawalEvent validator.EnrichedProvenWithdrawalEvent) {
func (s *State) IncrementPotentialAttackOnInProgressGames(enrichedWithdrawalEvent *validator.EnrichedProvenWithdrawalEvent) {
key := enrichedWithdrawalEvent.Event.Raw.TxHash
// check if key already exists
if _, ok := s.potentialAttackOnInProgressGames[key]; ok {
s.logger.Error("STATE WITHDRAWAL:is NOT valid, game is still in progress", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", &enrichedWithdrawalEvent)
s.logger.Error("STATE WITHDRAWAL:is NOT valid, game is still in progress", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", enrichedWithdrawalEvent)
} else {
s.logger.Error("STATE WITHDRAWAL:is NOT valid, game is still in progress. New game found In Progress", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", &enrichedWithdrawalEvent)
s.logger.Error("STATE WITHDRAWAL:is NOT valid, game is still in progress. New game found In Progress", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", enrichedWithdrawalEvent)
s.numberOfPotentialAttackOnInProgressGames++
enrichedWithdrawalEvent.ProcessedTimeStamp = float64(time.Now().Unix())

}

// eventually update the map with the new enrichedWithdrawalEvent
s.potentialAttackOnInProgressGames[key] = enrichedWithdrawalEvent
}

func (s *State) IncrementSuspiciousEventsOnChallengerWinsGames(enrichedWithdrawalEvent validator.EnrichedProvenWithdrawalEvent) {
func (s *State) IncrementSuspiciousEventsOnChallengerWinsGames(enrichedWithdrawalEvent *validator.EnrichedProvenWithdrawalEvent) {
key := enrichedWithdrawalEvent.Event.Raw.TxHash

s.logger.Error("STATE WITHDRAWAL:is NOT valid, but the game is correctly resolved", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", &enrichedWithdrawalEvent)
s.logger.Error("STATE WITHDRAWAL:is NOT valid, but the game is correctly resolved", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", enrichedWithdrawalEvent)
s.suspiciousEventsOnChallengerWinsGames.Add(key, enrichedWithdrawalEvent)
s.numberOfSuspiciousEventsOnChallengerWinsGames++

if _, ok := s.potentialAttackOnInProgressGames[key]; ok {
s.logger.Error("STATE WITHDRAWAL: added to suspicious attacks. Removing from inProgress", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", &enrichedWithdrawalEvent)
s.logger.Error("STATE WITHDRAWAL: added to suspicious attacks. Removing from inProgress", "TxHash", fmt.Sprintf("%v", enrichedWithdrawalEvent.Event.Raw.TxHash), "enrichedWithdrawalEvent", enrichedWithdrawalEvent)
delete(s.potentialAttackOnInProgressGames, key)
s.numberOfPotentialAttackOnInProgressGames--
}

s.withdrawalsProcessed++

enrichedWithdrawalEvent.ProcessedTimeStamp = float64(time.Now().Unix())
}

func (s *State) GetPercentages() (uint64, uint64) {
Expand Down Expand Up @@ -380,6 +386,9 @@ func (m *Metrics) UpdateMetricsFromState(state *State) {
}
m.previousNodeConnectionFailures = state.nodeConnectionFailures

// Clear the previous values
m.PotentialAttackOnDefenderWinsGamesGaugeVec.Reset()

// Update metrics for forgeries withdrawals events
for _, event := range state.potentialAttackOnDefenderWinsGames {
withdrawalHash := common.BytesToHash(event.Event.WithdrawalHash[:]).Hex()
Expand All @@ -395,7 +404,7 @@ func (m *Metrics) UpdateMetricsFromState(state *State) {
fmt.Sprintf("%v", event.Enriched),
fmt.Sprintf("%v", event.Event.Raw.BlockNumber),
event.Event.Raw.TxHash.String(),
).Set(1) // Set a value for existence
).Set(event.ProcessedTimeStamp) // Set the timestamp of when the event was processed
}

// Clear the previous values
Expand All @@ -416,7 +425,7 @@ func (m *Metrics) UpdateMetricsFromState(state *State) {
fmt.Sprintf("%v", event.Enriched),
fmt.Sprintf("%v", event.Event.Raw.BlockNumber),
event.Event.Raw.TxHash.String(),
).Set(1) // Set a value for existence
).Set(event.ProcessedTimeStamp) // Set the timestamp of when the event was processed
}

// Clear the previous values
Expand All @@ -425,8 +434,7 @@ func (m *Metrics) UpdateMetricsFromState(state *State) {
for _, key := range state.suspiciousEventsOnChallengerWinsGames.Keys() {
enrichedEvent, ok := state.suspiciousEventsOnChallengerWinsGames.Get(key)
if ok {
event := enrichedEvent.(validator.EnrichedProvenWithdrawalEvent)

event := enrichedEvent.(*validator.EnrichedProvenWithdrawalEvent)
withdrawalHash := common.BytesToHash(event.Event.WithdrawalHash[:]).Hex()
proofSubmitter := event.Event.ProofSubmitter.String()
status := event.DisputeGame.DisputeGameData.Status.String()
Expand All @@ -440,8 +448,7 @@ func (m *Metrics) UpdateMetricsFromState(state *State) {
fmt.Sprintf("%v", event.Enriched),
fmt.Sprintf("%v", event.Event.Raw.BlockNumber),
event.Event.Raw.TxHash.String(),
).Set(1) // Set a value for existence
).Set(event.ProcessedTimeStamp) // Set the timestamp of when the event was processed
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type EnrichedProvenWithdrawalEvent struct {
Blacklisted bool // Indicates if the game is blacklisted.
WithdrawalHashPresentOnL2 bool // Indicates if the withdrawal hash is present on L2.
Enriched bool // Indicates if the event is enriched.
ProcessedTimeStamp float64 // Unix TimeStamp seconds when the event was processed.
}

// ProvenWithdrawalValidator validates proven withdrawal events.
Expand Down Expand Up @@ -205,13 +206,13 @@ func (wv *ProvenWithdrawalValidator) GetEnrichedWithdrawalsEvents(start uint64,

// GetEnrichedWithdrawalsEvents retrieves enriched withdrawal events within the specified block range.
// It returns a slice of EnrichedProvenWithdrawalEvent along with any error encountered.
func (wv *ProvenWithdrawalValidator) GetEnrichedWithdrawalsEventsMap(start uint64, end *uint64) (map[common.Hash]EnrichedProvenWithdrawalEvent, error) {
func (wv *ProvenWithdrawalValidator) GetEnrichedWithdrawalsEventsMap(start uint64, end *uint64) (map[common.Hash]*EnrichedProvenWithdrawalEvent, error) {
iterator, err := wv.optimismPortal2Helper.GetProvenWithdrawalsExtension1EventsIterator(start, end)
if err != nil {
return nil, fmt.Errorf("failed to get proven withdrawals extension1 iterator error:%w", err)
}

enrichedProvenWithdrawalEvents := make(map[common.Hash]EnrichedProvenWithdrawalEvent)
enrichedProvenWithdrawalEvents := make(map[common.Hash]*EnrichedProvenWithdrawalEvent)

for iterator.Next() {
event := iterator.Event
Expand All @@ -229,7 +230,7 @@ func (wv *ProvenWithdrawalValidator) GetEnrichedWithdrawalsEventsMap(start uint6
}

key := enrichedWithdrawalEvent.Event.Raw.TxHash
enrichedProvenWithdrawalEvents[key] = *enrichedWithdrawalEvent
enrichedProvenWithdrawalEvents[key] = enrichedWithdrawalEvent
}

return enrichedProvenWithdrawalEvents, nil
Expand Down

0 comments on commit dd04bbd

Please sign in to comment.