Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overflow in state sync eta #1195

Merged
merged 5 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
Loading