Skip to content

Commit 3958b59

Browse files
author
Darioush Jalali
authored
Fix overflow in state sync eta (#1195)
* Fix overflow in state sync eta * add UT * use max * use timer.EstimateETA
1 parent caa42de commit 3958b59

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

sync/statesync/trie_sync_stats.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

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

134-
triesTime := now.Sub(t.triesStartTime) * time.Duration(t.triesRemaining) / time.Duration(t.triesSynced)
135+
triesTime := timer.EstimateETA(t.triesStartTime, uint64(t.triesSynced), uint64(t.triesSynced+t.triesRemaining))
136+
eta := max(leafsTime, triesTime)
135137
log.Info(
136138
"state sync: syncing storage tries",
137139
"triesRemaining", t.triesRemaining,
138-
"ETA", roundETA(leafsTime+triesTime), // TODO: should we use max instead of sum?
140+
"ETA", roundETA(eta),
139141
)
142+
return eta
140143
}
141144

142145
func (t *trieSyncStats) setTriesRemaining(triesRemaining int) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// (c) 2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package statesync
5+
6+
import (
7+
"testing"
8+
"time"
9+
10+
"github.com/ava-labs/subnet-evm/metrics"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestETAShouldNotOverflow(t *testing.T) {
15+
require := require.New(t)
16+
now := time.Now()
17+
start := now.Add(-6 * time.Hour)
18+
19+
stats := &trieSyncStats{
20+
triesStartTime: start,
21+
triesSynced: 100_000,
22+
triesRemaining: 450_000,
23+
leafsRateGauge: metrics.NilGauge{},
24+
}
25+
require.Positive(stats.updateETA(time.Minute, now))
26+
}

0 commit comments

Comments
 (0)