Skip to content

Commit

Permalink
Merge branch 'v1.4.11_b0b67be' into merge_v1.4.11_b0b67be
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Nov 5, 2024
2 parents 8e3f646 + b0b67be commit 49e6150
Show file tree
Hide file tree
Showing 35 changed files with 225 additions and 160 deletions.
3 changes: 1 addition & 2 deletions cmd/utils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ func ImportHistory(chain *core.BlockChain, db ethdb.Database, dir string, networ
start = time.Now()
reported = time.Now()
imported = 0
forker = core.NewForkChoice(chain, nil)
h = sha256.New()
buf = bytes.NewBuffer(nil)
)
Expand Down Expand Up @@ -305,7 +304,7 @@ func ImportHistory(chain *core.BlockChain, db ethdb.Database, dir string, networ
if err != nil {
return fmt.Errorf("error reading receipts %d: %w", it.Number(), err)
}
if status, err := chain.HeaderChain().InsertHeaderChain([]*types.Header{block.Header()}, start, forker); err != nil {
if status, err := chain.HeaderChain().InsertHeaderChain([]*types.Header{block.Header()}, start); err != nil {
return fmt.Errorf("error inserting header %d: %w", it.Number(), err)
} else if status != core.CanonStatTy {
return fmt.Errorf("error inserting header %d, not canon: %v", it.Number(), status)
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2756,7 +2756,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
}
}
// Disable transaction indexing/unindexing by default.
chain, err := core.NewBlockChain(chainDb, cache, gspec, nil, engine, vmcfg, nil, nil)
chain, err := core.NewBlockChain(chainDb, cache, gspec, nil, engine, vmcfg, nil)
if err != nil {
Fatalf("Can't create BlockChain: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/utils/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestHistoryImportAndExport(t *testing.T) {
})

// Initialize BlockChain.
chain, err := core.NewBlockChain(db, nil, genesis, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
chain, err := core.NewBlockChain(db, nil, genesis, nil, ethash.NewFaker(), vm.Config{}, nil)
if err != nil {
t.Fatalf("unable to initialize chain: %v", err)
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestHistoryImportAndExport(t *testing.T) {
})

genesis.MustCommit(db2, triedb.NewDatabase(db, triedb.HashDefaults))
imported, err := core.NewBlockChain(db2, nil, genesis, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
imported, err := core.NewBlockChain(db2, nil, genesis, nil, ethash.NewFaker(), vm.Config{}, nil)
if err != nil {
t.Fatalf("unable to initialize chain: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions consensus/clique/clique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestReimportMirroredState(t *testing.T) {
copy(genspec.ExtraData[extraVanity:], addr[:])

// Generate a batch of blocks, each properly signed
chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, genspec, nil, engine, vm.Config{}, nil, nil)
chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, genspec, nil, engine, vm.Config{}, nil)
defer chain.Stop()

_, blocks, _ := core.GenerateChainWithGenesis(genspec, engine, 3, func(i int, block *core.BlockGen) {
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestReimportMirroredState(t *testing.T) {
}
// Insert the first two blocks and make sure the chain is valid
db = rawdb.NewMemoryDatabase()
chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil, nil)
chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil)
defer chain.Stop()

if _, err := chain.InsertChain(blocks[:2]); err != nil {
Expand All @@ -106,7 +106,7 @@ func TestReimportMirroredState(t *testing.T) {
// Simulate a crash by creating a new chain on top of the database, without
// flushing the dirty states out. Insert the last block, triggering a sidechain
// reimport.
chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil, nil)
chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil)
defer chain.Stop()

if _, err := chain.InsertChain(blocks[2:]); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion consensus/clique/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func (tt *cliqueTest) run(t *testing.T) {
batches[len(batches)-1] = append(batches[len(batches)-1], block)
}
// Pass all the headers through clique and ensure tallying succeeds
chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, genesis, nil, engine, vm.Config{}, nil, nil)
chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, genesis, nil, engine, vm.Config{}, nil)
if err != nil {
t.Fatalf("failed to create test chain: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {

// Time the insertion of the new chain.
// State and blocks are stored in the same DB.
chainman, _ := NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
chainman, _ := NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil)
defer chainman.Stop()
b.ReportAllocs()
b.ResetTimer()
Expand Down Expand Up @@ -312,7 +312,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
if err != nil {
b.Fatalf("error opening database at %v: %v", dir, err)
}
chain, err := NewBlockChain(db, &cacheConfig, genesis, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
chain, err := NewBlockChain(db, &cacheConfig, genesis, nil, ethash.NewFaker(), vm.Config{}, nil)
if err != nil {
b.Fatalf("error creating chain: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/block_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func testHeaderVerification(t *testing.T, scheme string) {
headers[i] = block.Header()
}
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
chain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
chain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil)
defer chain.Stop()

for i := 0; i < len(blocks); i++ {
Expand Down Expand Up @@ -160,7 +160,7 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
postHeaders[i] = block.Header()
}
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
chain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{}, nil, nil)
chain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{}, nil)
defer chain.Stop()

// Verify the blocks before the merging
Expand Down
67 changes: 51 additions & 16 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ type BlockChain struct {
prefetcher Prefetcher
validator Validator // Block and state validator interface
processor Processor // Block transaction processor interface
forker *ForkChoice
vmConfig vm.Config

// monitor
Expand All @@ -323,11 +322,17 @@ type BlockChain struct {
}

// NewBlockChain returns a fully initialised block chain using information
<<<<<<< HEAD
// available in the database. It initialises the default Ethereum Validator and
// Processor.
func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis, overrides *ChainOverrides, engine consensus.Engine,
vmConfig vm.Config, shouldPreserve func(block *types.Header) bool, txLookupLimit *uint64,
options ...BlockChainOption) (*BlockChain, error) {
=======
// available in the database. It initialises the default Ethereum Validator
// and Processor.
func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis, overrides *ChainOverrides, engine consensus.Engine, vmConfig vm.Config, txLookupLimit *uint64) (*BlockChain, error) {
>>>>>>> v1.4.11_b0b67be
if cacheConfig == nil {
cacheConfig = defaultCacheConfig
}
Expand Down Expand Up @@ -392,7 +397,6 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
return nil, err
}
bc.flushInterval.Store(int64(cacheConfig.TrieTimeLimit))
bc.forker = NewForkChoice(bc, shouldPreserve)
bc.stateCache = state.NewDatabaseWithNodeDB(bc.db, bc.triedb)
bc.validator = NewBlockValidator(chainConfig, bc)
bc.prefetcher = NewStatePrefetcher(chainConfig, bc.hc)
Expand Down Expand Up @@ -1517,6 +1521,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [

// Rewind may have occurred, skip in that case.
if bc.CurrentHeader().Number.Cmp(head.Number()) >= 0 {
<<<<<<< HEAD
reorg, err := bc.forker.ReorgNeededWithFastFinality(bc.CurrentSnapBlock(), head.Header())
if err != nil {
log.Warn("Reorg failed", "err", err)
Expand All @@ -1525,6 +1530,9 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
return false
}
rawdb.WriteHeadFastBlockHash(bc.db.BlockStore(), head.Hash())
=======
rawdb.WriteHeadFastBlockHash(bc.db, head.Hash())
>>>>>>> v1.4.11_b0b67be
bc.currentSnapBlock.Store(head.Header())
headFastBlockGauge.Update(int64(head.NumberU64()))
return true
Expand Down Expand Up @@ -1916,6 +1924,7 @@ func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types
if err != nil {
return NonStatTy, err
}
<<<<<<< HEAD
if reorg {
bc.highestVerifiedBlock.Store(types.CopyHeader(block.Header()))
bc.highestVerifiedBlockFeed.Send(HighestVerifiedBlockEvent{Header: block.Header()})
Expand All @@ -1930,15 +1939,25 @@ func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types
if err := bc.reorg(currentBlock, block); err != nil {
return NonStatTy, err
}
=======
currentBlock := bc.CurrentBlock()

// Reorganise the chain if the parent is not the head block
if block.ParentHash() != currentBlock.Hash() {
if err := bc.reorg(currentBlock, block); err != nil {
return NonStatTy, err
>>>>>>> v1.4.11_b0b67be
}
status = CanonStatTy
} else {
status = SideStatTy
}

// Set new head.
if status == CanonStatTy {
bc.writeHeadBlock(block)
bc.writeHeadBlock(block)

bc.chainFeed.Send(ChainEvent{Block: block, Hash: block.Hash(), Logs: logs})
if len(logs) > 0 {
bc.logsFeed.Send(logs)
}
<<<<<<< HEAD
bc.futureBlocks.Remove(block.Hash())

if status == CanonStatTy {
Expand All @@ -1965,8 +1984,17 @@ func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types
}
} else {
bc.chainSideFeed.Send(ChainSideEvent{Block: block})
=======
// In theory, we should fire a ChainHeadEvent when we inject
// a canonical block, but sometimes we can insert a batch of
// canonical blocks. Avoid firing too many ChainHeadEvents,
// we will fire an accumulated ChainHeadEvent and disable fire
// event here.
if emitHeadEvent {
bc.chainHeadFeed.Send(ChainHeadEvent{Block: block})
>>>>>>> v1.4.11_b0b67be
}
return status, nil
return CanonStatTy, nil
}

// addFutureBlock checks if the block is within the max allowed window to get
Expand Down Expand Up @@ -2036,10 +2064,13 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
if bc.insertStopped() {
return 0, nil
}
<<<<<<< HEAD

if len(chain) > 0 {
blockRecvTimeDiffGauge.Update(time.Now().Unix() - int64(chain[0].Time()))
}
=======
>>>>>>> v1.4.11_b0b67be
// Start a parallel signature recovery (signer will fluke on fork transition, minimal perf loss)
signer := types.MakeSigner(bc.chainConfig, chain[0].Number(), chain[0].Time())
go SenderCacher.RecoverFromBlocks(signer, chain)
Expand Down Expand Up @@ -2086,11 +2117,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
// 2. The block is stored as a sidechain, and is lying about it's stateroot, and passes a stateroot
// from the canonical chain, which has not been verified.
// Skip all known blocks that are behind us.
var (
reorg bool
current = bc.CurrentBlock()
)
current := bc.CurrentBlock()
for block != nil && bc.skipBlock(err, it) {
<<<<<<< HEAD
reorg, err = bc.forker.ReorgNeededWithFastFinality(current, block.Header())
if err != nil {
return it.index, err
Expand All @@ -2104,6 +2133,10 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
if block.NumberU64() > current.Number.Uint64() || bc.GetCanonicalHash(block.NumberU64()) != block.Hash() {
break
}
=======
if block.NumberU64() > current.Number.Uint64() || bc.GetCanonicalHash(block.NumberU64()) != block.Hash() {
break
>>>>>>> v1.4.11_b0b67be
}
log.Debug("Ignoring already known block", "number", block.Number(), "hash", block.Hash())
stats.ignored++
Expand Down Expand Up @@ -2465,9 +2498,8 @@ func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, s
// insertSideChain is only used pre-merge.
func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (int, error) {
var (
externTd *big.Int
lastBlock = block
current = bc.CurrentBlock()
externTd *big.Int
current = bc.CurrentBlock()
)
// The first sidechain block error is already verified to be ErrPrunedAncestor.
// Since we don't import them here, we expect ErrUnknownAncestor for the remaining
Expand Down Expand Up @@ -2518,6 +2550,7 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (i
"txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()),
"root", block.Root())
}
<<<<<<< HEAD
lastBlock = block
}
// At this point, we've written all sidechain blocks to database. Loop ended
Expand All @@ -2534,6 +2567,8 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (i
localTd := bc.GetTd(current.Hash(), current.Number.Uint64())
log.Info("Sidechain written to disk", "start", it.first().NumberU64(), "end", it.previous().Number, "sidetd", externTd, "localtd", localTd)
return it.index, err
=======
>>>>>>> v1.4.11_b0b67be
}
// Gather all the sidechain hashes (full blocks may be memory heavy)
var (
Expand Down Expand Up @@ -3130,7 +3165,7 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header) (int, error) {
return 0, errChainStopped
}
defer bc.chainmu.Unlock()
_, err := bc.hc.InsertHeaderChain(chain, start, bc.forker)
_, err := bc.hc.InsertHeaderChain(chain, start)
return 0, err
}

Expand Down
5 changes: 0 additions & 5 deletions core/blockchain_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,6 @@ func (it *insertIterator) current() *types.Header {
return it.chain[it.index].Header()
}

// first returns the first block in it.
func (it *insertIterator) first() *types.Block {
return it.chain[0]
}

// remaining returns the number of remaining blocks.
func (it *insertIterator) remaining() int {
return len(it.chain) - it.index
Expand Down
10 changes: 7 additions & 3 deletions core/blockchain_repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,7 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s
config.SnapshotLimit = 256
config.SnapshotWait = true
}
<<<<<<< HEAD
config.TriesInMemory = 128

if err = db.SetupFreezerEnv(&ethdb.FreezerEnv{
Expand All @@ -1805,6 +1806,9 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s
t.Fatalf("Failed to create chain: %v", err)
}
chain, err := NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil, nil)
=======
chain, err := NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil)
>>>>>>> v1.4.11_b0b67be
if err != nil {
t.Fatalf("Failed to create chain: %v", err)
}
Expand Down Expand Up @@ -1870,7 +1874,7 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s
}
defer db.Close()

newChain, err := NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil, nil)
newChain, err := NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil)
if err != nil {
t.Fatalf("Failed to recreate chain: %v", err)
}
Expand Down Expand Up @@ -1942,7 +1946,7 @@ func testIssue23496(t *testing.T, scheme string) {
}
engine = ethash.NewFullFaker()
)
chain, err := NewBlockChain(db, DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{}, nil, nil)
chain, err := NewBlockChain(db, DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{}, nil)
if err != nil {
t.Fatalf("Failed to create chain: %v", err)
}
Expand Down Expand Up @@ -1992,7 +1996,7 @@ func testIssue23496(t *testing.T, scheme string) {
}
defer db.Close()

chain, err = NewBlockChain(db, DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{}, nil, nil)
chain, err = NewBlockChain(db, DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{}, nil)
if err != nil {
t.Fatalf("Failed to recreate chain: %v", err)
}
Expand Down
4 changes: 4 additions & 0 deletions core/blockchain_sethead_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1999,6 +1999,7 @@ func testSetHeadWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme
config.SnapshotLimit = 256
config.SnapshotWait = true
}
<<<<<<< HEAD
config.TriesInMemory = 128

if err = db.SetupFreezerEnv(&ethdb.FreezerEnv{
Expand All @@ -2008,6 +2009,9 @@ func testSetHeadWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme
t.Fatalf("Failed to create chain: %v", err)
}
chain, err := NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil, nil)
=======
chain, err := NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil)
>>>>>>> v1.4.11_b0b67be
if err != nil {
t.Fatalf("Failed to create chain: %v", err)
}
Expand Down
Loading

0 comments on commit 49e6150

Please sign in to comment.