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. --- light/lightchain.go | 6 +--- light/lightchain_test.go | 81 ++++++++++++++++-------------------------------- light/odr_test.go | 5 +-- light/txpool_test.go | 5 +-- 4 files changed, 33 insertions(+), 64 deletions(-) (limited to 'light') diff --git a/light/lightchain.go b/light/lightchain.go index 82b7a5866..98fb024f0 100644 --- a/light/lightchain.go +++ b/light/lightchain.go @@ -96,11 +96,7 @@ func NewLightChain(odr OdrBackend, config *params.ChainConfig, pow pow.PoW, mux bc.genesisBlock, _ = bc.GetBlockByNumber(NoOdr, 0) if bc.genesisBlock == nil { - bc.genesisBlock, err = core.WriteDefaultGenesisBlock(odr.Database()) - if err != nil { - return nil, err - } - log.Warn("Wrote default ethereum genesis block") + return nil, core.ErrNoGenesis } if bc.genesisBlock.Hash() == params.MainNetGenesisHash { diff --git a/light/lightchain_test.go b/light/lightchain_test.go index 7460fd1a3..e9236f1fd 100644 --- a/light/lightchain_test.go +++ b/light/lightchain_test.go @@ -20,7 +20,6 @@ import ( "context" "fmt" "math/big" - "runtime" "testing" "github.com/ethereum/go-ethereum/common" @@ -30,7 +29,6 @@ import ( "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/pow" - "github.com/hashicorp/golang-lru" ) // So we can deterministically seed different blockchains @@ -59,14 +57,10 @@ func testChainConfig() *params.ChainConfig { // chain. Depending on the full flag, if creates either a full block chain or a // header only chain. func newCanonical(n int) (ethdb.Database, *LightChain, error) { - // Create te new chain database db, _ := ethdb.NewMemDatabase() - evmux := &event.TypeMux{} - - // Initialize a fresh chain with only a genesis block - genesis, _ := core.WriteTestNetGenesisBlock(db) - - blockchain, _ := NewLightChain(&dummyOdr{db: db}, testChainConfig(), pow.FakePow{}, evmux) + gspec := core.Genesis{Config: testChainConfig()} + genesis := gspec.MustCommit(db) + blockchain, _ := NewLightChain(&dummyOdr{db: db}, gspec.Config, pow.FakePow{}, new(event.TypeMux)) // Create and inject the requested chain if n == 0 { return db, blockchain, nil @@ -77,21 +71,20 @@ func newCanonical(n int) (ethdb.Database, *LightChain, error) { return db, blockchain, err } -func init() { - runtime.GOMAXPROCS(runtime.NumCPU()) -} - -func theLightChain(db ethdb.Database, t *testing.T) *LightChain { - var eventMux event.TypeMux - core.WriteTestNetGenesisBlock(db) - LightChain, err := NewLightChain(&dummyOdr{db: db}, testChainConfig(), pow.NewTestEthash(), &eventMux) +// newTestLightChain creates a LightChain that doesn't validate anything. +func newTestLightChain() *LightChain { + db, _ := ethdb.NewMemDatabase() + gspec := &core.Genesis{ + Difficulty: big.NewInt(1), + Config: testChainConfig(), + } + gspec.MustCommit(db) + lc, err := NewLightChain(&dummyOdr{db: db}, gspec.Config, pow.NewTestEthash(), new(event.TypeMux)) if err != nil { - t.Error("failed creating LightChain:", err) - t.FailNow() - return nil + panic(err) } - - return LightChain + lc.SetValidator(bproc{}) + return lc } // Test fork of length N starting from block i @@ -302,20 +295,6 @@ func (odr *dummyOdr) Retrieve(ctx context.Context, req OdrRequest) error { return nil } -func chm(genesis *types.Block, db ethdb.Database) *LightChain { - odr := &dummyOdr{db: db} - var eventMux event.TypeMux - bc := &LightChain{odr: odr, chainDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: pow.FakePow{}} - bc.hc, _ = core.NewHeaderChain(db, testChainConfig(), bc.Validator, bc.getProcInterrupt) - bc.bodyCache, _ = lru.New(100) - bc.bodyRLPCache, _ = lru.New(100) - bc.blockCache, _ = lru.New(100) - bc.SetValidator(bproc{}) - bc.ResetWithGenesisBlock(genesis) - - return bc -} - // Tests that reorganizing a long difficult chain after a short easy one // overwrites the canonical numbers and links in the database. func TestReorgLongHeaders(t *testing.T) { @@ -329,14 +308,11 @@ func TestReorgShortHeaders(t *testing.T) { } func testReorg(t *testing.T, first, second []int, td int64) { - // Create a pristine block chain - db, _ := ethdb.NewMemDatabase() - genesis, _ := core.WriteTestNetGenesisBlock(db) - bc := chm(genesis, db) + bc := newTestLightChain() // Insert an easy and a difficult chain afterwards - bc.InsertHeaderChain(makeHeaderChainWithDiff(genesis, first, 11), 1) - bc.InsertHeaderChain(makeHeaderChainWithDiff(genesis, second, 22), 1) + bc.InsertHeaderChain(makeHeaderChainWithDiff(bc.genesisBlock, first, 11), 1) + bc.InsertHeaderChain(makeHeaderChainWithDiff(bc.genesisBlock, second, 22), 1) // Check that the chain is valid number and link wise prev := bc.CurrentHeader() for header := bc.GetHeaderByNumber(bc.CurrentHeader().Number.Uint64() - 1); header.Number.Uint64() != 0; prev, header = header, bc.GetHeaderByNumber(header.Number.Uint64()-1) { @@ -345,7 +321,7 @@ func testReorg(t *testing.T, first, second []int, td int64) { } } // Make sure the chain total difficulty is the correct one - want := new(big.Int).Add(genesis.Difficulty(), big.NewInt(td)) + want := new(big.Int).Add(bc.genesisBlock.Difficulty(), big.NewInt(td)) if have := bc.GetTdByHash(bc.CurrentHeader().Hash()); have.Cmp(want) != 0 { t.Errorf("total difficulty mismatch: have %v, want %v", have, want) } @@ -353,14 +329,11 @@ func testReorg(t *testing.T, first, second []int, td int64) { // Tests that the insertion functions detect banned hashes. func TestBadHeaderHashes(t *testing.T) { - // Create a pristine block chain - db, _ := ethdb.NewMemDatabase() - genesis, _ := core.WriteTestNetGenesisBlock(db) - bc := chm(genesis, db) + bc := newTestLightChain() // Create a chain, ban a hash and try to import var err error - headers := makeHeaderChainWithDiff(genesis, []int{1, 2, 4}, 10) + headers := makeHeaderChainWithDiff(bc.genesisBlock, []int{1, 2, 4}, 10) core.BadHashes[headers[2].Hash()] = true _, err = bc.InsertHeaderChain(headers, 1) if !core.IsBadHashError(err) { @@ -371,13 +344,10 @@ func TestBadHeaderHashes(t *testing.T) { // Tests that bad hashes are detected on boot, and the chan rolled back to a // good state prior to the bad hash. func TestReorgBadHeaderHashes(t *testing.T) { - // Create a pristine block chain - db, _ := ethdb.NewMemDatabase() - genesis, _ := core.WriteTestNetGenesisBlock(db) - bc := chm(genesis, db) + bc := newTestLightChain() // Create a chain, import and ban aferwards - headers := makeHeaderChainWithDiff(genesis, []int{1, 2, 3, 4}, 10) + headers := makeHeaderChainWithDiff(bc.genesisBlock, []int{1, 2, 3, 4}, 10) if _, err := bc.InsertHeaderChain(headers, 1); err != nil { t.Fatalf("failed to import headers: %v", err) @@ -387,8 +357,9 @@ func TestReorgBadHeaderHashes(t *testing.T) { } core.BadHashes[headers[3].Hash()] = true defer func() { delete(core.BadHashes, headers[3].Hash()) }() - // Create a new chain manager and check it rolled back the state - ncm, err := NewLightChain(&dummyOdr{db: db}, testChainConfig(), pow.FakePow{}, new(event.TypeMux)) + + // Create a new LightChain and check that it rolled back the state. + ncm, err := NewLightChain(&dummyOdr{db: bc.chainDb}, testChainConfig(), pow.FakePow{}, new(event.TypeMux)) if err != nil { t.Fatalf("failed to create new chain manager: %v", err) } diff --git a/light/odr_test.go b/light/odr_test.go index ba82ec04f..37d999994 100644 --- a/light/odr_test.go +++ b/light/odr_test.go @@ -251,9 +251,10 @@ func testChainOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) { pow = new(pow.FakePow) sdb, _ = ethdb.NewMemDatabase() ldb, _ = ethdb.NewMemDatabase() - genesis = core.WriteGenesisBlockForTesting(sdb, core.GenesisAccount{Address: testBankAddress, Balance: testBankFunds}) + gspec = core.Genesis{Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}} + genesis = gspec.MustCommit(sdb) ) - core.WriteGenesisBlockForTesting(ldb, core.GenesisAccount{Address: testBankAddress, Balance: testBankFunds}) + gspec.MustCommit(ldb) // Assemble the test environment blockchain, _ := core.NewBlockChain(sdb, testChainConfig(), pow, evmux, vm.Config{}) chainConfig := ¶ms.ChainConfig{HomesteadBlock: new(big.Int)} diff --git a/light/txpool_test.go b/light/txpool_test.go index e93955511..2b5fa7116 100644 --- a/light/txpool_test.go +++ b/light/txpool_test.go @@ -86,9 +86,10 @@ func TestTxPool(t *testing.T) { pow = new(pow.FakePow) sdb, _ = ethdb.NewMemDatabase() ldb, _ = ethdb.NewMemDatabase() - genesis = core.WriteGenesisBlockForTesting(sdb, core.GenesisAccount{Address: testBankAddress, Balance: testBankFunds}) + gspec = core.Genesis{Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}} + genesis = gspec.MustCommit(sdb) ) - core.WriteGenesisBlockForTesting(ldb, core.GenesisAccount{Address: testBankAddress, Balance: testBankFunds}) + gspec.MustCommit(ldb) // Assemble the test environment blockchain, _ := core.NewBlockChain(sdb, testChainConfig(), pow, evmux, vm.Config{}) chainConfig := ¶ms.ChainConfig{HomesteadBlock: new(big.Int)} -- cgit v1.2.3