From 15e5c08b85c2dcd76fbe30c3919bc5940f957e7a Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Wed, 26 Mar 2025 14:25:48 +0800 Subject: [PATCH] core, eth/downloader: fix chain sync eta --- core/blockchain_test.go | 7 +++++++ eth/downloader/downloader.go | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/core/blockchain_test.go b/core/blockchain_test.go index a44f6fd3c1..8f5a64e206 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -4339,6 +4339,13 @@ func testInsertChainWithCutoff(t *testing.T, cutoff uint64, ancientLimit uint64) if header.Hash() != hash { t.Errorf("block #%d: header mismatch: want: %v, got: %v", num, hash, header.Hash()) } + tail, err := db.Tail() + if err != nil { + t.Fatalf("Failed to get chain tail, %v", err) + } + if tail != cutoffBlock.NumberU64() { + t.Fatalf("Unexpected chain tail, want: %d, got: %d", cutoffBlock.NumberU64(), tail) + } // Block bodies and receipts before the cutoff should be non-existent if num < cutoffBlock.NumberU64() { body := chain.GetBody(hash) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index a3f26886d4..34c72be393 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -1133,10 +1133,20 @@ func (d *Downloader) reportSnapSyncProgress(force bool) { header = d.blockchain.CurrentHeader() block = d.blockchain.CurrentSnapBlock() ) - syncedBlocks := block.Number.Uint64() - d.syncStartBlock - if syncedBlocks == 0 { + // Prevent reporting if nothing has been synchronized yet + if block.Number.Uint64() <= d.syncStartBlock { return } + // Prevent reporting noise if the actual chain synchronization (headers + // and bodies) hasn't started yet. Inserting the ancient header chain is + // fast enough and would introduce significant bias if included in the count. + if d.chainCutoffNumber != 0 && block.Number.Uint64() < d.chainCutoffNumber { + return + } + fetchedBlocks := block.Number.Uint64() - d.syncStartBlock + if d.chainCutoffNumber != 0 { + fetchedBlocks = block.Number.Uint64() - d.chainCutoffNumber + } // Retrieve the current chain head and calculate the ETA latest, _, _, err := d.skeleton.Bounds() if err != nil { @@ -1151,9 +1161,7 @@ func (d *Downloader) reportSnapSyncProgress(force bool) { } var ( left = latest.Number.Uint64() - block.Number.Uint64() - - // TODO(rjl493456442) fix the ETA in the pruning mode. - eta = time.Since(d.syncStartTime) / time.Duration(syncedBlocks) * time.Duration(left) + eta = time.Since(d.syncStartTime) / time.Duration(fetchedBlocks) * time.Duration(left) progress = fmt.Sprintf("%.2f%%", float64(block.Number.Uint64())*100/float64(latest.Number.Uint64())) headers = fmt.Sprintf("%v@%v", log.FormatLogfmtUint64(header.Number.Uint64()), common.StorageSize(headerBytes).TerminalString())