Skip to content

Commit 85f2beb

Browse files
authored
refactor(all): move genesis initialization to blockchain ethereum#25523 (#2018)
1 parent 35d5bbd commit 85f2beb

21 files changed

+178
-96
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func NewXDCSimulatedBackend(alloc types.GenesisAlloc, gasLimit uint64, chainConf
143143
TrieTimeLimit: 5 * time.Minute,
144144
Preimages: true,
145145
}
146-
blockchain, _ := core.NewBlockChain(database, cacheConfig, genesis.Config, consensus, vm.Config{})
146+
blockchain, _ := core.NewBlockChain(database, cacheConfig, &genesis, consensus, vm.Config{})
147147

148148
backend := &SimulatedBackend{
149149
database: database,
@@ -171,7 +171,7 @@ func NewSimulatedBackend(alloc types.GenesisAlloc, gasLimit uint64) *SimulatedBa
171171
database := rawdb.NewMemoryDatabase()
172172
genesis := core.Genesis{Config: params.AllEthashProtocolChanges, GasLimit: gasLimit, Alloc: alloc}
173173
genesis.MustCommit(database)
174-
blockchain, _ := core.NewBlockChain(database, nil, genesis.Config, ethash.NewFaker(), vm.Config{})
174+
blockchain, _ := core.NewBlockChain(database, nil, &genesis, ethash.NewFaker(), vm.Config{})
175175

176176
backend := &SimulatedBackend{
177177
database: database,

cmd/XDC/dao_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ func testDAOForkBlockNewChain(t *testing.T, test int, genesis string, expectBloc
128128
}
129129
config, err := rawdb.ReadChainConfig(db, genesisHash)
130130
if err != nil {
131-
t.Errorf("test %d: failed to retrieve chain config: %v", test, err)
132-
return // we want to return here, the other checks can't make it past this point (nil panic).
131+
t.Errorf("test %d: failed to read chain config: %v", test, err)
132+
return
133+
}
134+
if config == nil {
135+
t.Errorf("test %d: failed to retrieve chain config", test)
136+
return
133137
}
134138
// Validate the DAO hard-fork block number against the expected value
135139
if config.DAOForkBlock == nil {

cmd/utils/flags.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,11 +1762,12 @@ func MakeGenesis(ctx *cli.Context) *core.Genesis {
17621762
}
17631763

17641764
// MakeChain creates a chain manager from set command line flags.
1765-
func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (chain *core.BlockChain, chainDb ethdb.Database) {
1766-
var err error
1767-
chainDb = MakeChainDatabase(ctx, stack, readonly)
1768-
1769-
config, _, err := core.SetupGenesisBlock(chainDb, MakeGenesis(ctx))
1765+
func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockChain, ethdb.Database) {
1766+
var (
1767+
gspec = MakeGenesis(ctx)
1768+
chainDb = MakeChainDatabase(ctx, stack, readonly)
1769+
)
1770+
config, _, err := core.LoadChainConfig(chainDb, gspec)
17701771
if err != nil {
17711772
Fatalf("%v", err)
17721773
}
@@ -1809,7 +1810,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (chain *core.B
18091810
}
18101811
}
18111812
// Disable transaction indexing/unindexing by default.
1812-
chain, err = core.NewBlockChain(chainDb, cache, config, engine, vmcfg)
1813+
chain, err := core.NewBlockChain(chainDb, cache, gspec, engine, vmcfg)
18131814
if err != nil {
18141815
Fatalf("Can't create BlockChain: %v", err)
18151816
}

core/bench_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
170170

171171
// Time the insertion of the new chain.
172172
// State and blocks are stored in the same DB.
173-
chainman, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
173+
chainman, _ := NewBlockChain(db, nil, gspec, ethash.NewFaker(), vm.Config{})
174174
defer chainman.Stop()
175175
b.ReportAllocs()
176176
b.ResetTimer()
@@ -236,6 +236,11 @@ func makeChainForBench(db ethdb.Database, full bool, count uint64) {
236236
rawdb.WriteCanonicalHash(db, hash, n)
237237
rawdb.WriteTd(db, hash, n, big.NewInt(int64(n+1)))
238238

239+
if n == 0 {
240+
rawdb.WriteChainConfig(db, hash, params.TestChainConfig)
241+
}
242+
rawdb.WriteHeadHeaderHash(db, hash)
243+
239244
if full || n == 0 {
240245
block := types.NewBlockWithHeader(header)
241246
rawdb.WriteBody(db, hash, n, block.Body())
@@ -273,7 +278,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
273278
if err != nil {
274279
b.Fatalf("error opening database at %v: %v", dir, err)
275280
}
276-
chain, err := NewBlockChain(db, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{})
281+
chain, err := NewBlockChain(db, nil, nil, ethash.NewFaker(), vm.Config{})
277282
if err != nil {
278283
b.Fatalf("error creating chain: %v", err)
279284
}

core/block_validator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestHeaderVerification(t *testing.T) {
4141
headers[i] = block.Header()
4242
}
4343
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
44-
chain, err := NewBlockChain(testdb, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
44+
chain, err := NewBlockChain(testdb, nil, gspec, ethash.NewFaker(), vm.Config{})
4545
defer chain.Stop()
4646
if err != nil {
4747
t.Fatal(err)

core/blockchain.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io"
2424
"math/big"
25+
"strings"
2526
"sync"
2627
"sync/atomic"
2728
"time"
@@ -230,7 +231,7 @@ type BlockChain struct {
230231
// NewBlockChain returns a fully initialised block chain using information
231232
// available in the database. It initialises the default Ethereum Validator and
232233
// Processor.
233-
func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config) (*BlockChain, error) {
234+
func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis, engine consensus.Engine, vmConfig vm.Config) (*BlockChain, error) {
234235
if cacheConfig == nil {
235236
cacheConfig = &CacheConfig{
236237
TrieCleanLimit: 256,
@@ -239,6 +240,19 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
239240
}
240241
}
241242

243+
// Setup the genesis block, commit the provided genesis specification
244+
// to database if the genesis block is not present yet, or load the
245+
// stored one from database.
246+
chainConfig, genesisHash, genesisErr := SetupGenesisBlock(db, genesis)
247+
if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok {
248+
return nil, genesisErr
249+
}
250+
log.Info(strings.Repeat("-", 153))
251+
for line := range strings.SplitSeq(chainConfig.Description(), "\n") {
252+
log.Info(line)
253+
}
254+
log.Info(strings.Repeat("-", 153))
255+
242256
// Open trie database with provided config
243257
triedb := trie.NewDatabaseWithConfig(db, &trie.Config{
244258
Cache: cacheConfig.TrieCleanLimit,
@@ -334,15 +348,22 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
334348
}
335349
}
336350

351+
// Rewind the chain in case of an incompatible config upgrade.
352+
if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
353+
log.Warn("Rewinding chain to upgrade configuration", "err", compat)
354+
bc.SetHead(compat.RewindTo)
355+
rawdb.WriteChainConfig(db, genesisHash, chainConfig)
356+
}
357+
337358
// Start future block processor.
338359
bc.wg.Go(bc.futureBlocksLoop)
339360

340361
return bc, nil
341362
}
342363

343364
// NewBlockChainEx extend old blockchain, add order state db
344-
func NewBlockChainEx(db ethdb.Database, XDCxDb ethdb.XDCxDatabase, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config) (*BlockChain, error) {
345-
blockchain, err := NewBlockChain(db, cacheConfig, chainConfig, engine, vmConfig)
365+
func NewBlockChainEx(db ethdb.Database, XDCxDb ethdb.XDCxDatabase, cacheConfig *CacheConfig, genesis *Genesis, engine consensus.Engine, vmConfig vm.Config) (*BlockChain, error) {
366+
blockchain, err := NewBlockChain(db, cacheConfig, genesis, engine, vmConfig)
346367
if err != nil {
347368
return nil, err
348369
}

core/blockchain_test.go

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ import (
4444
// are also returned in case more test blocks are needed later.
4545
func newCanonical(engine consensus.Engine, n int, full bool) (ethdb.Database, *BlockChain, error) {
4646
var (
47+
db = rawdb.NewMemoryDatabase()
4748
gspec = &Genesis{
4849
BaseFee: big.NewInt(params.InitialBaseFee),
4950
Config: params.AllEthashProtocolChanges,
5051
}
52+
genesis = gspec.MustCommit(db)
5153
)
52-
db := rawdb.NewMemoryDatabase()
53-
genesis := gspec.MustCommit(db)
5454

5555
// Initialize a fresh chain with only a genesis block
56-
blockchain, _ := NewBlockChain(db, nil, params.AllEthashProtocolChanges, engine, vm.Config{})
56+
blockchain, _ := NewBlockChain(db, nil, gspec, engine, vm.Config{})
5757

5858
// Create and inject the requested chain
5959
if n == 0 {
@@ -517,7 +517,11 @@ func testReorgBadHashes(t *testing.T, full bool) {
517517
blockchain.Stop()
518518

519519
// Create a new BlockChain and check that it rolled back the state.
520-
ncm, err := NewBlockChain(blockchain.db, nil, blockchain.chainConfig, ethash.NewFaker(), vm.Config{})
520+
gspec := &Genesis{
521+
BaseFee: big.NewInt(params.InitialBaseFee),
522+
Config: params.AllEthashProtocolChanges,
523+
}
524+
ncm, err := NewBlockChain(blockchain.db, nil, gspec, ethash.NewFaker(), vm.Config{})
521525
if err != nil {
522526
t.Fatalf("failed to create new chain manager: %v", err)
523527
}
@@ -620,7 +624,7 @@ func TestFastVsFullChains(t *testing.T) {
620624
// Import the chain as an archive node for the comparison baseline
621625
archiveDb := rawdb.NewMemoryDatabase()
622626
gspec.MustCommit(archiveDb)
623-
archive, _ := NewBlockChain(archiveDb, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
627+
archive, _ := NewBlockChain(archiveDb, nil, gspec, ethash.NewFaker(), vm.Config{})
624628
defer archive.Stop()
625629

626630
if n, err := archive.InsertChain(blocks); err != nil {
@@ -629,7 +633,7 @@ func TestFastVsFullChains(t *testing.T) {
629633
// Fast import the chain as a non-archive node to test
630634
fastDb := rawdb.NewMemoryDatabase()
631635
gspec.MustCommit(fastDb)
632-
fast, _ := NewBlockChain(fastDb, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
636+
fast, _ := NewBlockChain(fastDb, nil, gspec, ethash.NewFaker(), vm.Config{})
633637
defer fast.Stop()
634638

635639
headers := make([]*types.Header, len(blocks))
@@ -711,7 +715,7 @@ func TestLightVsFastVsFullChainHeads(t *testing.T) {
711715
archiveDb := rawdb.NewMemoryDatabase()
712716
gspec.MustCommit(archiveDb)
713717

714-
archive, _ := NewBlockChain(archiveDb, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
718+
archive, _ := NewBlockChain(archiveDb, nil, gspec, ethash.NewFaker(), vm.Config{})
715719
if n, err := archive.InsertChain(blocks); err != nil {
716720
t.Fatalf("failed to process block %d: %v", n, err)
717721
}
@@ -724,7 +728,7 @@ func TestLightVsFastVsFullChainHeads(t *testing.T) {
724728
// Import the chain as a non-archive node and ensure all pointers are updated
725729
fastDb := rawdb.NewMemoryDatabase()
726730
gspec.MustCommit(fastDb)
727-
fast, _ := NewBlockChain(fastDb, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
731+
fast, _ := NewBlockChain(fastDb, nil, gspec, ethash.NewFaker(), vm.Config{})
728732
defer fast.Stop()
729733

730734
headers := make([]*types.Header, len(blocks))
@@ -745,7 +749,7 @@ func TestLightVsFastVsFullChainHeads(t *testing.T) {
745749
lightDb := rawdb.NewMemoryDatabase()
746750
gspec.MustCommit(lightDb)
747751

748-
light, _ := NewBlockChain(lightDb, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
752+
light, _ := NewBlockChain(lightDb, nil, gspec, ethash.NewFaker(), vm.Config{})
749753
if n, err := light.InsertHeaderChain(headers, 1); err != nil {
750754
t.Fatalf("failed to insert header %d: %v", n, err)
751755
}
@@ -814,7 +818,7 @@ func TestChainTxReorgs(t *testing.T) {
814818
}
815819
})
816820
// Import the chain. This runs all block validation rules.
817-
blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
821+
blockchain, _ := NewBlockChain(db, nil, gspec, ethash.NewFaker(), vm.Config{})
818822
if i, err := blockchain.InsertChain(chain); err != nil {
819823
t.Fatalf("failed to insert original chain[%d]: %v", i, err)
820824
}
@@ -887,7 +891,7 @@ func TestLogReorgs(t *testing.T) {
887891
signer = types.LatestSigner(gspec.Config)
888892
)
889893

890-
blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
894+
blockchain, _ := NewBlockChain(db, nil, gspec, ethash.NewFaker(), vm.Config{})
891895
defer blockchain.Stop()
892896

893897
rmLogsCh := make(chan RemovedLogsEvent)
@@ -1066,7 +1070,7 @@ func TestEIP155Transition(t *testing.T) {
10661070
genesis = gspec.MustCommit(db)
10671071
)
10681072

1069-
blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
1073+
blockchain, _ := NewBlockChain(db, nil, gspec, ethash.NewFaker(), vm.Config{})
10701074
defer blockchain.Stop()
10711075

10721076
blocks, _ := GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 4, func(i int, block *BlockGen) {
@@ -1173,7 +1177,7 @@ func TestEIP161AccountRemoval(t *testing.T) {
11731177
}
11741178
genesis = gspec.MustCommit(db)
11751179
)
1176-
blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
1180+
blockchain, _ := NewBlockChain(db, nil, gspec, ethash.NewFaker(), vm.Config{})
11771181
defer blockchain.Stop()
11781182

11791183
blocks, _ := GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 3, func(i int, block *BlockGen) {
@@ -1252,7 +1256,7 @@ func TestBlockchainHeaderchainReorgConsistency(t *testing.T) {
12521256
diskdb := rawdb.NewMemoryDatabase()
12531257
gspec.MustCommit(diskdb)
12541258

1255-
chain, err := NewBlockChain(diskdb, nil, gspec.Config, engine, vm.Config{})
1259+
chain, err := NewBlockChain(diskdb, nil, gspec, engine, vm.Config{})
12561260
if err != nil {
12571261
t.Fatalf("failed to create tester chain: %v", err)
12581262
}
@@ -1300,7 +1304,7 @@ func TestTrieForkGC(t *testing.T) {
13001304
diskdb := rawdb.NewMemoryDatabase()
13011305
gspec.MustCommit(diskdb)
13021306

1303-
chain, err := NewBlockChain(diskdb, nil, gspec.Config, engine, vm.Config{})
1307+
chain, err := NewBlockChain(diskdb, nil, gspec, engine, vm.Config{})
13041308
if err != nil {
13051309
t.Fatalf("failed to create tester chain: %v", err)
13061310
}
@@ -1327,12 +1331,12 @@ func TestTrieForkGC(t *testing.T) {
13271331
func TestLargeReorgTrieGC(t *testing.T) {
13281332
// Generate the original common chain segment and the two competing forks
13291333
engine := ethash.NewFaker()
1334+
1335+
db := rawdb.NewMemoryDatabase()
13301336
gspec := &Genesis{
13311337
BaseFee: big.NewInt(params.InitialBaseFee),
13321338
Config: params.TestChainConfig,
13331339
}
1334-
1335-
db := rawdb.NewMemoryDatabase()
13361340
genesis := gspec.MustCommit(db)
13371341

13381342
shared, _ := GenerateChain(gspec.Config, genesis, engine, db, 64, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) })
@@ -1343,7 +1347,7 @@ func TestLargeReorgTrieGC(t *testing.T) {
13431347
diskdb := rawdb.NewMemoryDatabase()
13441348
gspec.MustCommit(diskdb)
13451349

1346-
chain, err := NewBlockChain(diskdb, nil, gspec.Config, engine, vm.Config{})
1350+
chain, err := NewBlockChain(diskdb, nil, gspec, engine, vm.Config{})
13471351
if err != nil {
13481352
t.Fatalf("failed to create tester chain: %v", err)
13491353
}
@@ -1425,7 +1429,7 @@ func benchmarkLargeNumberOfValueToNonexisting(b *testing.B, numTxs, numBlocks in
14251429
diskdb := rawdb.NewMemoryDatabase()
14261430
gspec.MustCommit(diskdb)
14271431

1428-
chain, err := NewBlockChain(diskdb, nil, gspec.Config, engine, vm.Config{})
1432+
chain, err := NewBlockChain(diskdb, nil, gspec, engine, vm.Config{})
14291433
if err != nil {
14301434
b.Fatalf("failed to create tester chain: %v", err)
14311435
}
@@ -1652,7 +1656,7 @@ func TestEIP2718Transition(t *testing.T) {
16521656
diskdb := rawdb.NewMemoryDatabase()
16531657
gspec.MustCommit(diskdb)
16541658

1655-
chain, err := NewBlockChain(diskdb, nil, gspec.Config, engine, vm.Config{})
1659+
chain, err := NewBlockChain(diskdb, nil, gspec, engine, vm.Config{})
16561660
if err != nil {
16571661
t.Fatalf("failed to create tester chain: %v", err)
16581662
}
@@ -1747,7 +1751,7 @@ func TestTransientStorageReset(t *testing.T) {
17471751
gspec.MustCommit(diskdb)
17481752

17491753
// Initialize the blockchain with 1153 enabled.
1750-
chain, err := NewBlockChain(diskdb, nil, gspec.Config, engine, vmConfig)
1754+
chain, err := NewBlockChain(diskdb, nil, gspec, engine, vmConfig)
17511755
if err != nil {
17521756
t.Fatalf("failed to create tester chain: %v", err)
17531757
}
@@ -1842,7 +1846,7 @@ func TestEIP3651(t *testing.T) {
18421846
diskdb := rawdb.NewMemoryDatabase()
18431847
gspec.MustCommit(diskdb)
18441848

1845-
chain, err := NewBlockChain(diskdb, nil, gspec.Config, engine, vm.Config{})
1849+
chain, err := NewBlockChain(diskdb, nil, gspec, engine, vm.Config{})
18461850
if err != nil {
18471851
t.Fatalf("failed to create tester chain: %v", err)
18481852
}
@@ -1945,7 +1949,7 @@ func TestDeleteCreateRevert(t *testing.T) {
19451949
diskdb := rawdb.NewMemoryDatabase()
19461950
gspec.MustCommit(diskdb)
19471951

1948-
chain, err := NewBlockChain(diskdb, nil, gspec.Config, engine, vm.Config{})
1952+
chain, err := NewBlockChain(diskdb, nil, gspec, engine, vm.Config{})
19491953
if err != nil {
19501954
t.Fatalf("failed to create tester chain: %v", err)
19511955
}

core/chain_makers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func ExampleGenerateChain() {
7878
})
7979

8080
// Import the chain. This runs all block validation rules.
81-
blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
81+
blockchain, _ := NewBlockChain(db, nil, gspec, ethash.NewFaker(), vm.Config{})
8282
defer blockchain.Stop()
8383

8484
if i, err := blockchain.InsertChain(chain); err != nil {

0 commit comments

Comments
 (0)