small fixes

This commit is contained in:
Felipe Andrade 2023-05-27 10:18:34 -07:00
parent 45061a1c8c
commit 04058779a9
2 changed files with 11 additions and 8 deletions

@ -212,9 +212,6 @@ func NewConsensusPoller(bg *BackendGroup, opts ...ConsensusOpt) *ConsensusPoller
ctx, cancelFunc := context.WithCancel(context.Background()) ctx, cancelFunc := context.WithCancel(context.Background())
state := make(map[*Backend]*backendState, len(bg.Backends)) state := make(map[*Backend]*backendState, len(bg.Backends))
for _, be := range bg.Backends {
state[be] = &backendState{}
}
cp := &ConsensusPoller{ cp := &ConsensusPoller{
cancelFunc: cancelFunc, cancelFunc: cancelFunc,
@ -239,6 +236,7 @@ func NewConsensusPoller(bg *BackendGroup, opts ...ConsensusOpt) *ConsensusPoller
cp.asyncHandler = NewPollerAsyncHandler(ctx, cp) cp.asyncHandler = NewPollerAsyncHandler(ctx, cp)
} }
cp.Reset()
cp.asyncHandler.Init() cp.asyncHandler.Init()
return cp return cp
@ -291,11 +289,11 @@ func (cp *ConsensusPoller) UpdateBackend(ctx context.Context, be *Backend) {
log.Warn("error updating backend - finalized block", "name", be.Name, "err", err) log.Warn("error updating backend - finalized block", "name", be.Name, "err", err)
} }
// just for readability
oldFinalized := bs.finalizedBlockNumber oldFinalized := bs.finalizedBlockNumber
oldSafe := bs.safeBlockNumber oldSafe := bs.safeBlockNumber
updateDelay := time.Since(bs.lastUpdate) RecordConsensusBackendUpdateDelay(be, bs.lastUpdate)
RecordConsensusBackendUpdateDelay(be, updateDelay)
changed := cp.setBackendState(be, peerCount, inSync, changed := cp.setBackendState(be, peerCount, inSync,
latestBlockNumber, latestBlockHash, latestBlockNumber, latestBlockHash,
@ -314,7 +312,7 @@ func (cp *ConsensusPoller) UpdateBackend(ctx context.Context, be *Backend) {
"latestBlockHash", latestBlockHash, "latestBlockHash", latestBlockHash,
"finalizedBlockNumber", finalizedBlockNumber, "finalizedBlockNumber", finalizedBlockNumber,
"safeBlockNumber", safeBlockNumber, "safeBlockNumber", safeBlockNumber,
"updateDelay", updateDelay) "lastUpdate", bs.lastUpdate)
} }
// sanity check for latest, safe and finalized block tags // sanity check for latest, safe and finalized block tags
@ -658,7 +656,7 @@ func (cp *ConsensusPoller) getConsensusCandidates() map[*Backend]*backendState {
lagging := make([]*Backend, 0, len(candidates)) lagging := make([]*Backend, 0, len(candidates))
for be, bs := range candidates { for be, bs := range candidates {
// check if backend is lagging behind the highest block // check if backend is lagging behind the highest block
if bs.latestBlockNumber < highestLatestBlock && uint64(highestLatestBlock-bs.latestBlockNumber) > cp.maxBlockLag { if uint64(highestLatestBlock-bs.latestBlockNumber) > cp.maxBlockLag {
lagging = append(lagging, be) lagging = append(lagging, be)
} }
} }

@ -482,7 +482,12 @@ func RecordConsensusBackendInSync(b *Backend, inSync bool) {
consensusInSyncBackend.WithLabelValues(b.Name).Set(boolToFloat64(inSync)) consensusInSyncBackend.WithLabelValues(b.Name).Set(boolToFloat64(inSync))
} }
func RecordConsensusBackendUpdateDelay(b *Backend, delay time.Duration) { func RecordConsensusBackendUpdateDelay(b *Backend, lastUpdate time.Time) {
// avoid recording the delay for the first update
if lastUpdate.IsZero() {
return
}
delay := time.Since(lastUpdate)
consensusUpdateDelayBackend.WithLabelValues(b.Name).Set(float64(delay.Milliseconds())) consensusUpdateDelayBackend.WithLabelValues(b.Name).Set(float64(delay.Milliseconds()))
} }