Skip to content

Commit

Permalink
Fix overflow in state sync eta (#1195)
Browse files Browse the repository at this point in the history
* Fix overflow in state sync eta

* add UT

* use max

* use timer.EstimateETA
  • Loading branch information
darioush authored May 30, 2024
1 parent caa42de commit 3958b59
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
11 changes: 7 additions & 4 deletions sync/statesync/trie_sync_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

utils_math "github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/subnet-evm/metrics"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -114,7 +115,7 @@ func (t *trieSyncStats) trieDone(root common.Hash) {
// updateETA calculates and logs and ETA based on the number of leafs
// currently in progress and the number of tries remaining.
// assumes lock is held.
func (t *trieSyncStats) updateETA(sinceUpdate time.Duration, now time.Time) {
func (t *trieSyncStats) updateETA(sinceUpdate time.Duration, now time.Time) time.Duration {
leafsRate := float64(t.leafsSinceUpdate) / sinceUpdate.Seconds()
if t.leafsRate == nil {
t.leafsRate = utils_math.NewAverager(leafsRate, leafRateHalfLife, now)
Expand All @@ -128,15 +129,17 @@ func (t *trieSyncStats) updateETA(sinceUpdate time.Duration, now time.Time) {
// provide a separate ETA for the account trie syncing step since we
// don't know the total number of storage tries yet.
log.Info("state sync: syncing account trie", "ETA", roundETA(leafsTime))
return
return leafsTime
}

triesTime := now.Sub(t.triesStartTime) * time.Duration(t.triesRemaining) / time.Duration(t.triesSynced)
triesTime := timer.EstimateETA(t.triesStartTime, uint64(t.triesSynced), uint64(t.triesSynced+t.triesRemaining))
eta := max(leafsTime, triesTime)
log.Info(
"state sync: syncing storage tries",
"triesRemaining", t.triesRemaining,
"ETA", roundETA(leafsTime+triesTime), // TODO: should we use max instead of sum?
"ETA", roundETA(eta),
)
return eta
}

func (t *trieSyncStats) setTriesRemaining(triesRemaining int) {
Expand Down
26 changes: 26 additions & 0 deletions sync/statesync/trie_sync_stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// (c) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package statesync

import (
"testing"
"time"

"github.com/ava-labs/subnet-evm/metrics"
"github.com/stretchr/testify/require"
)

func TestETAShouldNotOverflow(t *testing.T) {
require := require.New(t)
now := time.Now()
start := now.Add(-6 * time.Hour)

stats := &trieSyncStats{
triesStartTime: start,
triesSynced: 100_000,
triesRemaining: 450_000,
leafsRateGauge: metrics.NilGauge{},
}
require.Positive(stats.updateETA(time.Minute, now))
}

0 comments on commit 3958b59

Please sign in to comment.