From 58bdd555c74b82f570191d1794b767ad8782d36f Mon Sep 17 00:00:00 2001 From: Monkey Date: Sun, 11 May 2025 19:28:31 +0800 Subject: [PATCH 1/5] Using full sync for genesis block --- eth/downloader/downloader.go | 6 ++++++ eth/handler.go | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 09837a3045..dc4fb425a6 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -485,6 +485,12 @@ func (d *Downloader) syncToHead() (err error) { if mode == ethconfig.SnapSync { if height <= uint64(fsMinFullBlocks) { origin = 0 + // For genesis block, switch to full sync + if height == 0 { + mode = ethconfig.FullSync + d.mode.Store(uint32(mode)) + log.Info("Switching to full sync for genesis block") + } } else { pivotNumber := pivot.Number.Uint64() if pivotNumber <= origin { diff --git a/eth/handler.go b/eth/handler.go index aaea00e037..a4636e52a9 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -177,7 +177,11 @@ func newHandler(config *handlerConfig) (*handler, error) { } } else { head := h.chain.CurrentBlock() - if head.Number.Uint64() > 0 && h.chain.HasState(head.Root) { + if head.Number.Uint64() == 0 { + // For genesis block, always use full sync + h.snapSync.Store(false) + log.Info("Using full sync for genesis block") + } else if head.Number.Uint64() > 0 && h.chain.HasState(head.Root) { // Print warning log if database is not empty to run snap sync. log.Warn("Switch sync mode from snap sync to full sync", "reason", "snap sync complete") } else { From d7d59ebb935fd82a5aba912faa3aff9b20193fef Mon Sep 17 00:00:00 2001 From: Monkey Date: Thu, 15 May 2025 01:45:01 +0800 Subject: [PATCH 2/5] Revert "Using full sync for genesis block" This reverts commit a1b02725b536716c5b4f5a6559260cc067772d7a. --- eth/downloader/downloader.go | 6 ------ eth/handler.go | 6 +----- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index dc4fb425a6..09837a3045 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -485,12 +485,6 @@ func (d *Downloader) syncToHead() (err error) { if mode == ethconfig.SnapSync { if height <= uint64(fsMinFullBlocks) { origin = 0 - // For genesis block, switch to full sync - if height == 0 { - mode = ethconfig.FullSync - d.mode.Store(uint32(mode)) - log.Info("Switching to full sync for genesis block") - } } else { pivotNumber := pivot.Number.Uint64() if pivotNumber <= origin { diff --git a/eth/handler.go b/eth/handler.go index a4636e52a9..aaea00e037 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -177,11 +177,7 @@ func newHandler(config *handlerConfig) (*handler, error) { } } else { head := h.chain.CurrentBlock() - if head.Number.Uint64() == 0 { - // For genesis block, always use full sync - h.snapSync.Store(false) - log.Info("Using full sync for genesis block") - } else if head.Number.Uint64() > 0 && h.chain.HasState(head.Root) { + if head.Number.Uint64() > 0 && h.chain.HasState(head.Root) { // Print warning log if database is not empty to run snap sync. log.Warn("Switch sync mode from snap sync to full sync", "reason", "snap sync complete") } else { From 8b3386c7133bb13ed4267f2d0e300eb36c92d0a6 Mon Sep 17 00:00:00 2001 From: Monkey Date: Thu, 15 May 2025 01:49:43 +0800 Subject: [PATCH 3/5] recorrect the fixing --- eth/handler.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/eth/handler.go b/eth/handler.go index aaea00e037..902878238a 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -177,7 +177,11 @@ func newHandler(config *handlerConfig) (*handler, error) { } } else { head := h.chain.CurrentBlock() - if head.Number.Uint64() > 0 && h.chain.HasState(head.Root) { + if head.Number.Uint64() == 0 && head.Time > uint64(time.Now().Unix()) { + // Print warning log if the genesis block is in the future. + h.snapSync.Store(false) + log.Warn("Switch sync mode from snap sync to full sync", "reason", "in the future genesis block") + } else if head.Number.Uint64() > 0 && h.chain.HasState(head.Root) { // Print warning log if database is not empty to run snap sync. log.Warn("Switch sync mode from snap sync to full sync", "reason", "snap sync complete") } else { From 9645d9b1d687372a03f8390b26ebd3dfdebd241a Mon Sep 17 00:00:00 2001 From: Monkey Date: Mon, 19 May 2025 20:04:13 +0800 Subject: [PATCH 4/5] Revert " recorrect the fixing" This reverts commit 093f63c608a667e9503d302c3518ff2f3f14d3b1. --- eth/handler.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/eth/handler.go b/eth/handler.go index 902878238a..aaea00e037 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -177,11 +177,7 @@ func newHandler(config *handlerConfig) (*handler, error) { } } else { head := h.chain.CurrentBlock() - if head.Number.Uint64() == 0 && head.Time > uint64(time.Now().Unix()) { - // Print warning log if the genesis block is in the future. - h.snapSync.Store(false) - log.Warn("Switch sync mode from snap sync to full sync", "reason", "in the future genesis block") - } else if head.Number.Uint64() > 0 && h.chain.HasState(head.Root) { + if head.Number.Uint64() > 0 && h.chain.HasState(head.Root) { // Print warning log if database is not empty to run snap sync. log.Warn("Switch sync mode from snap sync to full sync", "reason", "snap sync complete") } else { From 389006997643788b7a9122fbc250b8d9bab5a292 Mon Sep 17 00:00:00 2001 From: Monkey Date: Mon, 19 May 2025 20:06:32 +0800 Subject: [PATCH 5/5] revert & fix --- eth/backend.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/eth/backend.go b/eth/backend.go index 7616ec9d31..698e300633 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -175,7 +175,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { // Here we determine genesis hash and active ChainConfig. // We need these to figure out the consensus parameters and to set up history pruning. - chainConfig, _, err := core.LoadChainConfig(chainDb, config.Genesis) + chainConfig, ghash, err := core.LoadChainConfig(chainDb, config.Genesis) if err != nil { return nil, err } @@ -189,6 +189,21 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { networkID = chainConfig.ChainID.Uint64() } + // Switch to full sync if we're in snap sync and genesis block is in the future. + if config.SyncMode == ethconfig.SnapSync { + genesisTime := uint64(0) + now := uint64(time.Now().Unix()) + if config.Genesis != nil { + genesisTime = config.Genesis.Timestamp + } else if genesis := rawdb.ReadBlock(chainDb, ghash, 0); genesis != nil { + genesisTime = genesis.Time() + } + if genesisTime > uint64(now) { + log.Warn("Genesis block is in the future, switching from snap sync to full sync") + config.SyncMode = ethconfig.FullSync + } + } + // Assemble the Ethereum object. eth := &Ethereum{ config: config,