From 96db9b3bac8cda54197d58ff53e82de42f4df3f8 Mon Sep 17 00:00:00 2001 From: niuxiaojie81 <85773309@qq.com> Date: Tue, 20 Jun 2023 17:53:21 +0800 Subject: [PATCH 1/2] fix issues-2016 --- eth/backend.go | 9 ++++----- eth/downloader/downloader.go | 26 ++++++++++++++++---------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index 31f92952ba..1f80f141ed 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -141,7 +141,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { height := rawdb.ReadHeaderNumber(chainDb, rawdb.ReadHeadHeaderHash(chainDb)) log.Debug("read header number from chain db", "height", height) if height != nil && *height > 0 { - //when last fast syncing fail,we will clean chaindb,wal,snapshotdb + //when last fast syncing fail,we will clean chaindb,wal,snapshotdb status, err := snapshotBaseDB.GetBaseDB([]byte(downloader.KeyFastSyncStatus)) // systemError @@ -154,11 +154,10 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { //if find sync status,this means last syncing not finish,should clean all db to reinit //if not find sync status,no need init chain if err == nil { - // Just commit the new block if there is no stored genesis block. stored := rawdb.ReadCanonicalHash(chainDb, 0) - log.Info("last fast sync is fail,init db", "status", common.BytesToUint32(status), "prichain", config.Genesis == nil) + log.Info("last fast sync is fail,init db", "status", common.BytesToUint32(status), "prichain", config.Genesis == nil) chainDb.Close() if err := snapshotBaseDB.Close(); err != nil { return nil, err @@ -192,7 +191,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { return nil, err } - //only private net need InitGenesisAndSetEconomicConfig + //only private net need InitGenesisAndSetEconomicConfig if stored != params.MainnetGenesisHash && config.Genesis == nil { // private net config.Genesis = new(core.Genesis) @@ -200,7 +199,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { return nil, err } } - log.Info("last fast sync is fail,init db finish") + log.Info("last fast sync is fail,init db finish") } else { // Just commit the new block if there is no stored genesis block. stored := rawdb.ReadCanonicalHash(chainDb, 0) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index ce27d79062..d0cf5d3c8f 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -602,8 +602,8 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, bn *big.I } if mode == FastSync { if err := d.snapshotDB.SetEmpty(); err != nil { - p.log.Error("set snapshotDB empty fail") - return errors.New("set snapshotDB empty fail:" + err.Error()) + p.log.Error("set snapshotDB empty fail") + return errors.New("set snapshotDB empty fail:" + err.Error()) } if err := d.snapshotDB.SetCurrent(pivoth.Hash(), *pivoth.Number, *pivoth.Number); err != nil { p.log.Error("set snapshotdb current fail", "err", err) @@ -742,11 +742,11 @@ func (d *Downloader) fetchPPOSInfo(p *peerConnection) (latest *types.Header, piv // if the sync is complete,will del the key func (d *Downloader) setFastSyncStatus(status uint16) error { key := []byte(KeyFastSyncStatus) - log.Debug("set fast sync status", "status", status) + log.Debug("set fast sync status", "status", status) switch status { case FastSyncDel: if err := d.snapshotDB.DelBaseDB(key); err != nil { - log.Error("del fast sync status from snapshotdb fail", "err", err) + log.Error("del fast sync status from snapshotdb fail", "err", err) return err } case FastSyncBegin, FastSyncFail: @@ -755,7 +755,7 @@ func (d *Downloader) setFastSyncStatus(status uint16) error { common.Uint16ToBytes(status), } if err := d.snapshotDB.WriteBaseDB([][2][]byte{syncStatus}); err != nil { - log.Error("save fast sync status to snapshotdb fail", "err", err) + log.Error("save fast sync status to snapshotdb fail", "err", err) return err } default: @@ -852,17 +852,23 @@ func (d *Downloader) spawnSync(fetchers []func() error) error { } } mode := d.getMode() + current := d.blockchain.CurrentBlock().NumberU64() if mode == FastSync { - if failed { - if err := d.setFastSyncStatus(FastSyncFail); err != nil { - return err + if failed && current <= 0 { + if error := d.setFastSyncStatus(FastSyncFail); error != nil { + err = error } } else { - if err := d.setFastSyncStatus(FastSyncDel); err != nil { - return err + if error := d.setFastSyncStatus(FastSyncDel); error != nil { + err = error } } } + if mode == FullSync { + if error := d.setFastSyncStatus(FastSyncDel); error != nil { + err = error + } + } d.queue.Close() d.Cancel() return err From 9c5ad6aa634407ebab05c077cbd271233b172abb Mon Sep 17 00:00:00 2001 From: niuxiaojie81 <85773309@qq.com> Date: Tue, 27 Jun 2023 10:21:41 +0800 Subject: [PATCH 2/2] fix issues-2016 --- eth/downloader/downloader.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index d0cf5d3c8f..81d0e7f8e9 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -854,21 +854,16 @@ func (d *Downloader) spawnSync(fetchers []func() error) error { mode := d.getMode() current := d.blockchain.CurrentBlock().NumberU64() if mode == FastSync { - if failed && current <= 0 { - if error := d.setFastSyncStatus(FastSyncFail); error != nil { - err = error - } + if failed && current == 0 { + err = d.setFastSyncStatus(FastSyncFail) } else { - if error := d.setFastSyncStatus(FastSyncDel); error != nil { - err = error - } + err = d.setFastSyncStatus(FastSyncDel) } } if mode == FullSync { - if error := d.setFastSyncStatus(FastSyncDel); error != nil { - err = error - } + err = d.setFastSyncStatus(FastSyncDel) } + d.queue.Close() d.Cancel() return err