From 344cbb25cee20d1ec5bfda9a8bc1f22fe27b8743 Mon Sep 17 00:00:00 2001 From: jeyldii <45122314+jeyldii@users.noreply.github.com> Date: Wed, 16 Jul 2025 17:21:25 +0400 Subject: [PATCH 1/2] Handle reorgs --- server/handlers.go | 3 ++- state/el_group.go | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/server/handlers.go b/server/handlers.go index 7959770..f506bc1 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -33,12 +33,13 @@ func (s *Server) handleEventEthNewHeader( block := header.Number blockStr := block.String() + blockHash := header.Hash() g := s.state.ExecutionGroup(gname) e := g.Endpoint(ename) e.RegisterBlock(block, ts) - latency := g.RegisterBlockAndGetLatency(block, ts) + latency := g.RegisterBlockAndGetLatency(block, blockHash, ts) latency_s := latency.Seconds() switch latency { diff --git a/state/el_group.go b/state/el_group.go index 9125889..9be8818 100644 --- a/state/el_group.go +++ b/state/el_group.go @@ -6,6 +6,7 @@ import ( "sync" "time" + "github.com/ethereum/go-ethereum/common" "github.com/flashbots/node-monitor/utils" ) @@ -25,8 +26,9 @@ type ELGroup struct { blocks *utils.SortedStringQueue blockTimes map[string]time.Time - highestBlock *big.Int - highestBlockStr string + highestBlock *big.Int + highestBlockHash common.Hash + highestBlockStr string mx sync.RWMutex } @@ -72,7 +74,7 @@ func (g *ELGroup) Endpoint(name string) *ELEndpoint { return g.endpoints[name] } -func (g *ELGroup) RegisterBlockAndGetLatency(block *big.Int, ts time.Time) time.Duration { +func (g *ELGroup) RegisterBlockAndGetLatency(block *big.Int, blockHash common.Hash, ts time.Time) time.Duration { g.mx.RLock() defer g.mx.RUnlock() @@ -82,10 +84,16 @@ func (g *ELGroup) RegisterBlockAndGetLatency(block *big.Int, ts time.Time) time. if blockStr > g.highestBlockStr { g.mx.RUnlock() g.mx.Lock() - if blockStr > g.highestBlockStr { + if blockStr == g.highestBlockStr { + if blockHash != g.highestBlockHash { + g.blockTimes[blockStr] = ts + g.highestBlockHash = blockHash + } + } else if blockStr > g.highestBlockStr { delete(g.blockTimes, g.blocks.InsertAndPop(blockStr)) g.highestBlock = big.NewInt(0).Set(block) g.highestBlockStr = blockStr + g.highestBlockHash = blockHash g.blockTimes[blockStr] = ts } g.mx.Unlock() From 26ef540394cb7db9da91a6ae480be3fd60f6d5bd Mon Sep 17 00:00:00 2001 From: jeyldii <45122314+jeyldii@users.noreply.github.com> Date: Wed, 16 Jul 2025 20:21:44 +0400 Subject: [PATCH 2/2] rework a bit handle new block --- state/el_group.go | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/state/el_group.go b/state/el_group.go index 9be8818..fbf4128 100644 --- a/state/el_group.go +++ b/state/el_group.go @@ -75,29 +75,23 @@ func (g *ELGroup) Endpoint(name string) *ELEndpoint { } func (g *ELGroup) RegisterBlockAndGetLatency(block *big.Int, blockHash common.Hash, ts time.Time) time.Duration { - g.mx.RLock() - defer g.mx.RUnlock() + g.mx.Lock() + defer g.mx.Unlock() blockStr := utils.Bigint2string(block) - // update the highest block (if needed) - if blockStr > g.highestBlockStr { - g.mx.RUnlock() - g.mx.Lock() - if blockStr == g.highestBlockStr { - if blockHash != g.highestBlockHash { - g.blockTimes[blockStr] = ts - g.highestBlockHash = blockHash - } - } else if blockStr > g.highestBlockStr { - delete(g.blockTimes, g.blocks.InsertAndPop(blockStr)) - g.highestBlock = big.NewInt(0).Set(block) - g.highestBlockStr = blockStr - g.highestBlockHash = blockHash + if blockStr == g.highestBlockStr { + if blockHash != g.highestBlockHash { g.blockTimes[blockStr] = ts + g.highestBlockHash = blockHash } - g.mx.Unlock() - g.mx.RLock() + // update the highest block (if needed) + } else if blockStr > g.highestBlockStr { + delete(g.blockTimes, g.blocks.InsertAndPop(blockStr)) + g.highestBlock = big.NewInt(0).Set(block) + g.highestBlockStr = blockStr + g.highestBlockHash = blockHash + g.blockTimes[blockStr] = ts } prevTS, exists := g.blockTimes[blockStr]