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..81d0e7f8e9 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,18 @@ 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 { + err = d.setFastSyncStatus(FastSyncFail) } else { - if err := d.setFastSyncStatus(FastSyncDel); err != nil { - return err - } + err = d.setFastSyncStatus(FastSyncDel) } } + if mode == FullSync { + err = d.setFastSyncStatus(FastSyncDel) + } + d.queue.Close() d.Cancel() return err