From 020ec9f8972b21369416e85990e1451cdefb7af6 Mon Sep 17 00:00:00 2001 From: Sergey Mishin Date: Mon, 22 Jun 2026 16:56:50 +0200 Subject: [PATCH] Fix block time calculation Previously, the function would unconditionally update the last seen block height value and time, which is not correct if the height scrape interval is lower than block time. E.g. if `watchers.height-interval` is set to 600ms, and the real block time is 1.5s, then the block duration will be always shown as 600ms. Updating the block timestamp only when the new block height is different seems to be the correct approach. --- internal/pkg/daemon/daemon.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/pkg/daemon/daemon.go b/internal/pkg/daemon/daemon.go index 773533f..65d6f3d 100644 --- a/internal/pkg/daemon/daemon.go +++ b/internal/pkg/daemon/daemon.go @@ -529,17 +529,17 @@ func (d *Daemon) performUpgrade( } func (d *Daemon) updateHeightAndBlockSpeed(newHeight int64) { - // calculate block speed based on the last few observed blocks + if d.currHeight == newHeight { + return + } + lastBlockHeight := d.currHeight lastHeightTime := d.currHeightTime d.currHeight = newHeight d.currHeightTime = time.Now() - // this may happen when polling - if d.currHeight != lastBlockHeight { - n := newHeight % int64(cap(d.observedBlockSpeeds)) - d.observedBlockSpeeds[n] = time.Millisecond * time.Duration(d.currHeightTime.Sub(lastHeightTime).Milliseconds()/(d.currHeight-lastBlockHeight)) - } + n := newHeight % int64(cap(d.observedBlockSpeeds)) + d.observedBlockSpeeds[n] = time.Millisecond * time.Duration(d.currHeightTime.Sub(lastHeightTime).Milliseconds()/(d.currHeight-lastBlockHeight)) sum, cnt := 0.0, 0 for _, blockSpeed := range d.observedBlockSpeeds {