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..fbf4128 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,24 +74,24 @@ func (g *ELGroup) Endpoint(name string) *ELEndpoint { return g.endpoints[name] } -func (g *ELGroup) RegisterBlockAndGetLatency(block *big.Int, ts time.Time) time.Duration { - g.mx.RLock() - defer g.mx.RUnlock() +func (g *ELGroup) RegisterBlockAndGetLatency(block *big.Int, blockHash common.Hash, ts time.Time) time.Duration { + 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 { - delete(g.blockTimes, g.blocks.InsertAndPop(blockStr)) - g.highestBlock = big.NewInt(0).Set(block) - g.highestBlockStr = blockStr + 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]