From 37dd9086ec491900311fc39837f4a62ef5fd3a4a Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 2 Mar 2017 14:03:33 +0100 Subject: core: refactor genesis handling This commit solves several issues concerning the genesis block: * Genesis/ChainConfig loading was handled by cmd/geth code. This left library users in the cold. They could specify a JSON-encoded string and overwrite the config, but didn't get any of the additional checks performed by geth. * Decoding and writing of genesis JSON was conflated in WriteGenesisBlock. This made it a lot harder to embed the genesis block into the forthcoming config file loader. This commit changes things so there is a single Genesis type that represents genesis blocks. All uses of Write*Genesis* are changed to use the new type instead. * If the chain config supplied by the user was incompatible with the current chain (i.e. the chain had already advanced beyond a scheduled fork), it got overwritten. This is not an issue in practice because previous forks have always had the highest total difficulty. It might matter in the future though. The new code reverts the local chain to the point of the fork when upgrading configuration. The change to genesis block data removes compression library dependencies from package core. --- eth/filters/filter_system_test.go | 11 +++++------ eth/filters/filter_test.go | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-) (limited to 'eth/filters') diff --git a/eth/filters/filter_system_test.go b/eth/filters/filter_system_test.go index d9c245a85..822580b56 100644 --- a/eth/filters/filter_system_test.go +++ b/eth/filters/filter_system_test.go @@ -72,12 +72,11 @@ func TestBlockSubscription(t *testing.T) { t.Parallel() var ( - mux = new(event.TypeMux) - db, _ = ethdb.NewMemDatabase() - backend = &testBackend{mux, db} - api = NewPublicFilterAPI(backend, false) - - genesis = core.WriteGenesisBlockForTesting(db) + mux = new(event.TypeMux) + db, _ = ethdb.NewMemDatabase() + backend = &testBackend{mux, db} + api = NewPublicFilterAPI(backend, false) + genesis = new(core.Genesis).MustCommit(db) chain, _ = core.GenerateChain(params.TestChainConfig, genesis, db, 10, func(i int, gen *core.BlockGen) {}) chainEvents = []core.ChainEvent{} ) diff --git a/eth/filters/filter_test.go b/eth/filters/filter_test.go index c2dc2b842..cd5e7cafd 100644 --- a/eth/filters/filter_test.go +++ b/eth/filters/filter_test.go @@ -60,7 +60,7 @@ func BenchmarkMipmaps(b *testing.B) { ) defer db.Close() - genesis := core.WriteGenesisBlockForTesting(db, core.GenesisAccount{Address: addr1, Balance: big.NewInt(1000000)}) + genesis := core.GenesisBlockForTesting(db, addr1, big.NewInt(1000000)) chain, receipts := core.GenerateChain(params.TestChainConfig, genesis, db, 100010, func(i int, gen *core.BlockGen) { var receipts types.Receipts switch i { @@ -112,7 +112,7 @@ func BenchmarkMipmaps(b *testing.B) { for i := 0; i < b.N; i++ { logs, _ := filter.Find(context.Background()) if len(logs) != 4 { - b.Fatal("expected 4 log, got", len(logs)) + b.Fatal("expected 4 logs, got", len(logs)) } } } @@ -138,7 +138,7 @@ func TestFilters(t *testing.T) { ) defer db.Close() - genesis := core.WriteGenesisBlockForTesting(db, core.GenesisAccount{Address: addr, Balance: big.NewInt(1000000)}) + genesis := core.GenesisBlockForTesting(db, addr, big.NewInt(1000000)) chain, receipts := core.GenerateChain(params.TestChainConfig, genesis, db, 1000, func(i int, gen *core.BlockGen) { var receipts types.Receipts switch i { -- cgit v1.2.3