Skip to content

Commit ca64a12

Browse files
karalabefjl
authored andcommitted
eth/downloader: save and load trie sync progress (#16224)
1 parent 12f4d28 commit ca64a12

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

core/database_util.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var (
4747
headHeaderKey = []byte("LastHeader")
4848
headBlockKey = []byte("LastBlock")
4949
headFastKey = []byte("LastFast")
50+
trieSyncKey = []byte("TrieSync")
5051

5152
// Data item prefixes (use single byte to avoid mixing data types, avoid `i`).
5253
headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
@@ -146,6 +147,16 @@ func GetHeadFastBlockHash(db DatabaseReader) common.Hash {
146147
return common.BytesToHash(data)
147148
}
148149

150+
// GetTrieSyncProgress retrieves the number of tries nodes fast synced to allow
151+
// reportinc correct numbers across restarts.
152+
func GetTrieSyncProgress(db DatabaseReader) uint64 {
153+
data, _ := db.Get(trieSyncKey)
154+
if len(data) == 0 {
155+
return 0
156+
}
157+
return new(big.Int).SetBytes(data).Uint64()
158+
}
159+
149160
// GetHeaderRLP retrieves a block header in its raw RLP database encoding, or nil
150161
// if the header's not found.
151162
func GetHeaderRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue {
@@ -374,6 +385,15 @@ func WriteHeadFastBlockHash(db ethdb.Putter, hash common.Hash) error {
374385
return nil
375386
}
376387

388+
// WriteTrieSyncProgress stores the fast sync trie process counter to support
389+
// retrieving it across restarts.
390+
func WriteTrieSyncProgress(db ethdb.Putter, count uint64) error {
391+
if err := db.Put(trieSyncKey, new(big.Int).SetUint64(count).Bytes()); err != nil {
392+
log.Crit("Failed to store fast sync trie progress", "err", err)
393+
}
394+
return nil
395+
}
396+
377397
// WriteHeader serializes a block header into the database.
378398
func WriteHeader(db ethdb.Putter, header *types.Header) error {
379399
data, err := rlp.EncodeToBytes(header)

eth/downloader/downloader.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
ethereum "github.com/ethereum/go-ethereum"
2929
"github.com/ethereum/go-ethereum/common"
30+
"github.com/ethereum/go-ethereum/core"
3031
"github.com/ethereum/go-ethereum/core/types"
3132
"github.com/ethereum/go-ethereum/ethdb"
3233
"github.com/ethereum/go-ethereum/event"
@@ -221,7 +222,10 @@ func New(mode SyncMode, stateDb ethdb.Database, mux *event.TypeMux, chain BlockC
221222
quitCh: make(chan struct{}),
222223
stateCh: make(chan dataPack),
223224
stateSyncStart: make(chan *stateSync),
224-
trackStateReq: make(chan *stateReq),
225+
syncStatsState: stateSyncStats{
226+
processed: core.GetTrieSyncProgress(stateDb),
227+
},
228+
trackStateReq: make(chan *stateReq),
225229
}
226230
go dl.qosTuner()
227231
go dl.stateFetcher()

eth/downloader/statesync.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"time"
2424

2525
"github.com/ethereum/go-ethereum/common"
26+
"github.com/ethereum/go-ethereum/core"
2627
"github.com/ethereum/go-ethereum/core/state"
2728
"github.com/ethereum/go-ethereum/crypto/sha3"
2829
"github.com/ethereum/go-ethereum/ethdb"
@@ -466,4 +467,7 @@ func (s *stateSync) updateStats(written, duplicate, unexpected int, duration tim
466467
if written > 0 || duplicate > 0 || unexpected > 0 {
467468
log.Info("Imported new state entries", "count", written, "elapsed", common.PrettyDuration(duration), "processed", s.d.syncStatsState.processed, "pending", s.d.syncStatsState.pending, "retry", len(s.tasks), "duplicate", s.d.syncStatsState.duplicate, "unexpected", s.d.syncStatsState.unexpected)
468469
}
470+
if written > 0 {
471+
core.WriteTrieSyncProgress(s.d.stateDB, s.d.syncStatsState.processed)
472+
}
469473
}

0 commit comments

Comments
 (0)