diff --git a/eth/handler.go b/eth/handler.go index 6489a21a5c8d13aba65211fb59a0efee9f04bcb4..bb61e5de05ef2d3d7617cb99e327e329f53a4058 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -30,7 +30,6 @@ import ( "github.com/ledgerwatch/turbo-geth/consensus" "github.com/ledgerwatch/turbo-geth/core" "github.com/ledgerwatch/turbo-geth/core/forkid" - "github.com/ledgerwatch/turbo-geth/core/rawdb" "github.com/ledgerwatch/turbo-geth/core/types" "github.com/ledgerwatch/turbo-geth/crypto" "github.com/ledgerwatch/turbo-geth/eth/downloader" @@ -102,9 +101,10 @@ type ProtocolManager struct { // Test fields or hooks broadcastTxAnnouncesOnly bool // Testing field, disable transaction propagation - mode downloader.SyncMode // Sync mode passed from the command line - datadir string - hdd bool + mode downloader.SyncMode // Sync mode passed from the command line + datadir string + hdd bool + currentHeight uint64 // Atomic variable to contain chain height } // NewProtocolManager returns a new Ethereum sub protocol manager. The Ethereum sub protocol manages peers capable @@ -198,9 +198,7 @@ func initPm(manager *ProtocolManager, engine consensus.Engine, chainConfig *para return engine.VerifyHeader(blockchain, header, true) } heighter := func() uint64 { - headHash := rawdb.ReadHeadHeaderHash(chaindb) - headNumber := rawdb.ReadHeaderNumber(chaindb, headHash) - return *headNumber + return atomic.LoadUint64(&manager.currentHeight) } inserter := func(blocks types.Blocks) (int, error) { atomic.StoreUint32(&manager.acceptTxs, 1) // Mark initial sync done on any fetcher import diff --git a/eth/sync.go b/eth/sync.go index 3c7fcf3696ecca8a1de369daec521fa9f8019802..adfc485f547aba543216be8514d47d87db53c82f 100644 --- a/eth/sync.go +++ b/eth/sync.go @@ -165,10 +165,10 @@ type chainSyncer struct { // chainSyncOp is a scheduled sync operation. type chainSyncOp struct { - mode downloader.SyncMode - peer *peer + mode downloader.SyncMode + peer *peer number uint64 - head common.Hash + head common.Hash } // newChainSyncer creates a chainSyncer. @@ -277,9 +277,7 @@ func peerToSyncOp(mode downloader.SyncMode, p *peer) *chainSyncOp { } func (cs *chainSyncer) modeAndLocalHead() (downloader.SyncMode, uint64) { - headHash := rawdb.ReadHeadHeaderHash(cs.pm.chaindb) - headNumber := rawdb.ReadHeaderNumber(cs.pm.chaindb, headHash) - return cs.pm.mode, *headNumber + return cs.pm.mode, atomic.LoadUint64(&cs.pm.currentHeight) } // startSync launches doSync in a new goroutine. @@ -327,6 +325,7 @@ func (pm *ProtocolManager) doSync(op *chainSyncOp) error { // enable accepting transactions from the network. headHash := rawdb.ReadHeadHeaderHash(pm.chaindb) headNumber := rawdb.ReadHeaderNumber(pm.chaindb, headHash) + atomic.StoreUint64(&pm.currentHeight, *headNumber) // this will be read by the block fetcher when required head := rawdb.ReadBlock(pm.chaindb, headHash, *headNumber) if *headNumber >= pm.checkpointNumber && head != nil { // Checkpoint passed, sanity check the timestamp to have a fallback mechanism