@@ -259,15 +259,14 @@ type BlockChain struct {
259259 validator Validator // Block and state validator interface
260260 prefetcher Prefetcher
261261 processor Processor // Block transaction processor interface
262- forker * ForkChoice
263262 vmConfig vm.Config
264263 logger * tracing.Hooks
265264}
266265
267266// NewBlockChain returns a fully initialised block chain using information
268267// available in the database. It initialises the default Ethereum Validator
269268// and Processor.
270- func NewBlockChain (db ethdb.Database , cacheConfig * CacheConfig , genesis * Genesis , overrides * ChainOverrides , engine consensus.Engine , vmConfig vm.Config , shouldPreserve func ( header * types. Header ) bool , txLookupLimit * uint64 ) (* BlockChain , error ) {
269+ func NewBlockChain (db ethdb.Database , cacheConfig * CacheConfig , genesis * Genesis , overrides * ChainOverrides , engine consensus.Engine , vmConfig vm.Config , txLookupLimit * uint64 ) (* BlockChain , error ) {
271270 if cacheConfig == nil {
272271 cacheConfig = defaultCacheConfig
273272 }
@@ -312,7 +311,6 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
312311 return nil , err
313312 }
314313 bc .flushInterval .Store (int64 (cacheConfig .TrieTimeLimit ))
315- bc .forker = NewForkChoice (bc , shouldPreserve )
316314 bc .stateCache = state .NewDatabaseWithNodeDB (bc .db , bc .triedb )
317315 bc .validator = NewBlockValidator (chainConfig , bc )
318316 bc .prefetcher = newStatePrefetcher (chainConfig , bc .hc )
@@ -1243,13 +1241,6 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
12431241
12441242 // Rewind may have occurred, skip in that case.
12451243 if bc .CurrentHeader ().Number .Cmp (head .Number ()) >= 0 {
1246- reorg , err := bc .forker .ReorgNeeded (bc .CurrentSnapBlock (), head .Header ())
1247- if err != nil {
1248- log .Warn ("Reorg failed" , "err" , err )
1249- return false
1250- } else if ! reorg {
1251- return false
1252- }
12531244 rawdb .WriteHeadFastBlockHash (bc .db , head .Hash ())
12541245 bc .currentSnapBlock .Store (head .Header ())
12551246 headFastBlockGauge .Update (int64 (head .NumberU64 ()))
@@ -1548,42 +1539,30 @@ func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types
15481539 return NonStatTy , err
15491540 }
15501541 currentBlock := bc .CurrentBlock ()
1551- reorg , err := bc .forker .ReorgNeeded (currentBlock , block .Header ())
1552- if err != nil {
1553- return NonStatTy , err
1554- }
1555- if reorg {
1556- // Reorganise the chain if the parent is not the head block
1557- if block .ParentHash () != currentBlock .Hash () {
1558- if err := bc .reorg (currentBlock , block ); err != nil {
1559- return NonStatTy , err
1560- }
1542+
1543+ // Reorganise the chain if the parent is not the head block
1544+ if block .ParentHash () != currentBlock .Hash () {
1545+ if err := bc .reorg (currentBlock , block ); err != nil {
1546+ return NonStatTy , err
15611547 }
1562- status = CanonStatTy
1563- } else {
1564- status = SideStatTy
15651548 }
1549+
15661550 // Set new head.
1567- if status == CanonStatTy {
1568- bc .writeHeadBlock (block )
1569- }
1570- if status == CanonStatTy {
1571- bc .chainFeed .Send (ChainEvent {Block : block , Hash : block .Hash (), Logs : logs })
1572- if len (logs ) > 0 {
1573- bc .logsFeed .Send (logs )
1574- }
1575- // In theory, we should fire a ChainHeadEvent when we inject
1576- // a canonical block, but sometimes we can insert a batch of
1577- // canonical blocks. Avoid firing too many ChainHeadEvents,
1578- // we will fire an accumulated ChainHeadEvent and disable fire
1579- // event here.
1580- if emitHeadEvent {
1581- bc .chainHeadFeed .Send (ChainHeadEvent {Block : block })
1582- }
1583- } else {
1584- bc .chainSideFeed .Send (ChainSideEvent {Block : block })
1551+ bc .writeHeadBlock (block )
1552+
1553+ bc .chainFeed .Send (ChainEvent {Block : block , Hash : block .Hash (), Logs : logs })
1554+ if len (logs ) > 0 {
1555+ bc .logsFeed .Send (logs )
15851556 }
1586- return status , nil
1557+ // In theory, we should fire a ChainHeadEvent when we inject
1558+ // a canonical block, but sometimes we can insert a batch of
1559+ // canonical blocks. Avoid firing too many ChainHeadEvents,
1560+ // we will fire an accumulated ChainHeadEvent and disable fire
1561+ // event here.
1562+ if emitHeadEvent {
1563+ bc .chainHeadFeed .Send (ChainHeadEvent {Block : block })
1564+ }
1565+ return CanonStatTy , nil
15871566}
15881567
15891568// InsertChain attempts to insert the given batch of blocks in to the canonical
@@ -1634,7 +1613,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
16341613 if bc .insertStopped () {
16351614 return 0 , nil
16361615 }
1637-
16381616 // Start a parallel signature recovery (signer will fluke on fork transition, minimal perf loss)
16391617 SenderCacher .RecoverFromBlocks (types .MakeSigner (bc .chainConfig , chain [0 ].Number (), chain [0 ].Time ()), chain )
16401618
@@ -1667,24 +1645,10 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
16671645 // 2. The block is stored as a sidechain, and is lying about it's stateroot, and passes a stateroot
16681646 // from the canonical chain, which has not been verified.
16691647 // Skip all known blocks that are behind us.
1670- var (
1671- reorg bool
1672- current = bc .CurrentBlock ()
1673- )
1648+ current := bc .CurrentBlock ()
16741649 for block != nil && bc .skipBlock (err , it ) {
1675- reorg , err = bc .forker .ReorgNeeded (current , block .Header ())
1676- if err != nil {
1677- return it .index , err
1678- }
1679- if reorg {
1680- // Switch to import mode if the forker says the reorg is necessary
1681- // and also the block is not on the canonical chain.
1682- // In eth2 the forker always returns true for reorg decision (blindly trusting
1683- // the external consensus engine), but in order to prevent the unnecessary
1684- // reorgs when importing known blocks, the special case is handled here.
1685- if block .NumberU64 () > current .Number .Uint64 () || bc .GetCanonicalHash (block .NumberU64 ()) != block .Hash () {
1686- break
1687- }
1650+ if block .NumberU64 () > current .Number .Uint64 () || bc .GetCanonicalHash (block .NumberU64 ()) != block .Hash () {
1651+ break
16881652 }
16891653 log .Debug ("Ignoring already known block" , "number" , block .Number (), "hash" , block .Hash ())
16901654 stats .ignored ++
@@ -2006,9 +1970,8 @@ func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, s
20061970// insertSideChain is only used pre-merge.
20071971func (bc * BlockChain ) insertSideChain (block * types.Block , it * insertIterator ) (int , error ) {
20081972 var (
2009- externTd * big.Int
2010- lastBlock = block
2011- current = bc .CurrentBlock ()
1973+ externTd * big.Int
1974+ current = bc .CurrentBlock ()
20121975 )
20131976 // The first sidechain block error is already verified to be ErrPrunedAncestor.
20141977 // Since we don't import them here, we expect ErrUnknownAncestor for the remaining
@@ -2059,22 +2022,6 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (i
20592022 "txs" , len (block .Transactions ()), "gas" , block .GasUsed (), "uncles" , len (block .Uncles ()),
20602023 "root" , block .Root ())
20612024 }
2062- lastBlock = block
2063- }
2064- // At this point, we've written all sidechain blocks to database. Loop ended
2065- // either on some other error or all were processed. If there was some other
2066- // error, we can ignore the rest of those blocks.
2067- //
2068- // If the externTd was larger than our local TD, we now need to reimport the previous
2069- // blocks to regenerate the required state
2070- reorg , err := bc .forker .ReorgNeeded (current , lastBlock .Header ())
2071- if err != nil {
2072- return it .index , err
2073- }
2074- if ! reorg {
2075- localTd := bc .GetTd (current .Hash (), current .Number .Uint64 ())
2076- log .Info ("Sidechain written to disk" , "start" , it .first ().NumberU64 (), "end" , it .previous ().Number , "sidetd" , externTd , "localtd" , localTd )
2077- return it .index , err
20782025 }
20792026 // Gather all the sidechain hashes (full blocks may be memory heavy)
20802027 var (
@@ -2541,7 +2488,7 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header) (int, error) {
25412488 return 0 , errChainStopped
25422489 }
25432490 defer bc .chainmu .Unlock ()
2544- _ , err := bc .hc .InsertHeaderChain (chain , start , bc . forker )
2491+ _ , err := bc .hc .InsertHeaderChain (chain , start )
25452492 return 0 , err
25462493}
25472494
0 commit comments