@@ -259,15 +259,14 @@ type BlockChain struct {
259
259
validator Validator // Block and state validator interface
260
260
prefetcher Prefetcher
261
261
processor Processor // Block transaction processor interface
262
- forker * ForkChoice
263
262
vmConfig vm.Config
264
263
logger * tracing.Hooks
265
264
}
266
265
267
266
// NewBlockChain returns a fully initialised block chain using information
268
267
// available in the database. It initialises the default Ethereum Validator
269
268
// 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 ) {
271
270
if cacheConfig == nil {
272
271
cacheConfig = defaultCacheConfig
273
272
}
@@ -312,7 +311,6 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
312
311
return nil , err
313
312
}
314
313
bc .flushInterval .Store (int64 (cacheConfig .TrieTimeLimit ))
315
- bc .forker = NewForkChoice (bc , shouldPreserve )
316
314
bc .stateCache = state .NewDatabaseWithNodeDB (bc .db , bc .triedb )
317
315
bc .validator = NewBlockValidator (chainConfig , bc )
318
316
bc .prefetcher = newStatePrefetcher (chainConfig , bc .hc )
@@ -1243,13 +1241,6 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
1243
1241
1244
1242
// Rewind may have occurred, skip in that case.
1245
1243
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
- }
1253
1244
rawdb .WriteHeadFastBlockHash (bc .db , head .Hash ())
1254
1245
bc .currentSnapBlock .Store (head .Header ())
1255
1246
headFastBlockGauge .Update (int64 (head .NumberU64 ()))
@@ -1548,42 +1539,30 @@ func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types
1548
1539
return NonStatTy , err
1549
1540
}
1550
1541
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
1561
1547
}
1562
- status = CanonStatTy
1563
- } else {
1564
- status = SideStatTy
1565
1548
}
1549
+
1566
1550
// 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 )
1585
1556
}
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
1587
1566
}
1588
1567
1589
1568
// 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)
1634
1613
if bc .insertStopped () {
1635
1614
return 0 , nil
1636
1615
}
1637
-
1638
1616
// Start a parallel signature recovery (signer will fluke on fork transition, minimal perf loss)
1639
1617
SenderCacher .RecoverFromBlocks (types .MakeSigner (bc .chainConfig , chain [0 ].Number (), chain [0 ].Time ()), chain )
1640
1618
@@ -1667,24 +1645,10 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
1667
1645
// 2. The block is stored as a sidechain, and is lying about it's stateroot, and passes a stateroot
1668
1646
// from the canonical chain, which has not been verified.
1669
1647
// Skip all known blocks that are behind us.
1670
- var (
1671
- reorg bool
1672
- current = bc .CurrentBlock ()
1673
- )
1648
+ current := bc .CurrentBlock ()
1674
1649
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
1688
1652
}
1689
1653
log .Debug ("Ignoring already known block" , "number" , block .Number (), "hash" , block .Hash ())
1690
1654
stats .ignored ++
@@ -2006,9 +1970,8 @@ func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, s
2006
1970
// insertSideChain is only used pre-merge.
2007
1971
func (bc * BlockChain ) insertSideChain (block * types.Block , it * insertIterator ) (int , error ) {
2008
1972
var (
2009
- externTd * big.Int
2010
- lastBlock = block
2011
- current = bc .CurrentBlock ()
1973
+ externTd * big.Int
1974
+ current = bc .CurrentBlock ()
2012
1975
)
2013
1976
// The first sidechain block error is already verified to be ErrPrunedAncestor.
2014
1977
// 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
2059
2022
"txs" , len (block .Transactions ()), "gas" , block .GasUsed (), "uncles" , len (block .Uncles ()),
2060
2023
"root" , block .Root ())
2061
2024
}
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
2078
2025
}
2079
2026
// Gather all the sidechain hashes (full blocks may be memory heavy)
2080
2027
var (
@@ -2541,7 +2488,7 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header) (int, error) {
2541
2488
return 0 , errChainStopped
2542
2489
}
2543
2490
defer bc .chainmu .Unlock ()
2544
- _ , err := bc .hc .InsertHeaderChain (chain , start , bc . forker )
2491
+ _ , err := bc .hc .InsertHeaderChain (chain , start )
2545
2492
return 0 , err
2546
2493
}
2547
2494
0 commit comments