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. --- les/backend.go | 22 ++++++++++++---------- les/helper_test.go | 25 ++++++++++++++----------- les/server.go | 2 +- 3 files changed, 27 insertions(+), 22 deletions(-) (limited to 'les') diff --git a/les/backend.go b/les/backend.go index 3cab75f33..d656cf41f 100644 --- a/les/backend.go +++ b/les/backend.go @@ -18,7 +18,6 @@ package les import ( - "errors" "fmt" "time" @@ -74,15 +73,19 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) { if err != nil { return nil, err } - if err := eth.SetupGenesisBlock(&chainDb, config); err != nil { - return nil, err + chainConfig, genesisHash, genesisErr := core.SetupGenesisBlock(chainDb, config.Genesis) + if _, isCompat := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !isCompat { + return nil, genesisErr } + log.Info("Initialised chain configuration", "config", chainConfig) + odr := NewLesOdr(chainDb) relay := NewLesTxRelay() eth := &LightEthereum{ odr: odr, relay: relay, chainDb: chainDb, + chainConfig: chainConfig, eventMux: ctx.EventMux, accountManager: ctx.AccountManager, pow: eth.CreatePoW(ctx, config), @@ -91,17 +94,16 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) { solcPath: config.SolcPath, } - if config.ChainConfig == nil { - return nil, errors.New("missing chain config") - } - eth.chainConfig = config.ChainConfig eth.blockchain, err = light.NewLightChain(odr, eth.chainConfig, eth.pow, eth.eventMux) if err != nil { - if err == core.ErrNoGenesis { - return nil, fmt.Errorf(`Genesis block not found. Please supply a genesis block with the "--genesis /path/to/file" argument`) - } return nil, err } + // Rewind the chain in case of an incompatible config upgrade. + if compat, ok := genesisErr.(*params.ConfigCompatError); ok { + log.Warn("Rewinding chain to upgrade configuration", "err", compat) + eth.blockchain.SetHead(compat.RewindTo) + core.WriteChainConfig(chainDb, genesisHash, chainConfig) + } eth.txPool = light.NewTxPool(eth.chainConfig, eth.eventMux, eth.blockchain, eth.relay) if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.LightMode, config.NetworkId, eth.eventMux, eth.pow, eth.blockchain, nil, chainDb, odr, relay); err != nil { diff --git a/les/helper_test.go b/les/helper_test.go index 3e8ce57b6..f37bec80e 100644 --- a/les/helper_test.go +++ b/les/helper_test.go @@ -134,28 +134,31 @@ func testRCL() RequestCostList { // channels for different events. func newTestProtocolManager(lightSync bool, blocks int, generator func(int, *core.BlockGen)) (*ProtocolManager, ethdb.Database, *LesOdr, error) { var ( - evmux = new(event.TypeMux) - pow = new(pow.FakePow) - db, _ = ethdb.NewMemDatabase() - genesis = core.WriteGenesisBlockForTesting(db, core.GenesisAccount{Address: testBankAddress, Balance: testBankFunds}) - chainConfig = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(0)} // homestead set to 0 because of chain maker - odr *LesOdr - chain BlockChain + evmux = new(event.TypeMux) + pow = new(pow.FakePow) + db, _ = ethdb.NewMemDatabase() + gspec = core.Genesis{ + Config: params.TestChainConfig, + Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}, + } + genesis = gspec.MustCommit(db) + odr *LesOdr + chain BlockChain ) if lightSync { odr = NewLesOdr(db) - chain, _ = light.NewLightChain(odr, chainConfig, pow, evmux) + chain, _ = light.NewLightChain(odr, gspec.Config, pow, evmux) } else { - blockchain, _ := core.NewBlockChain(db, chainConfig, pow, evmux, vm.Config{}) - gchain, _ := core.GenerateChain(chainConfig, genesis, db, blocks, generator) + blockchain, _ := core.NewBlockChain(db, gspec.Config, pow, evmux, vm.Config{}) + gchain, _ := core.GenerateChain(gspec.Config, genesis, db, blocks, generator) if _, err := blockchain.InsertChain(gchain); err != nil { panic(err) } chain = blockchain } - pm, err := NewProtocolManager(chainConfig, lightSync, NetworkId, evmux, pow, chain, nil, db, odr, nil) + pm, err := NewProtocolManager(gspec.Config, lightSync, NetworkId, evmux, pow, chain, nil, db, odr, nil) if err != nil { return nil, nil, nil, err } diff --git a/les/server.go b/les/server.go index 04bfa0292..5957add36 100644 --- a/les/server.go +++ b/les/server.go @@ -45,7 +45,7 @@ type LesServer struct { } func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) { - pm, err := NewProtocolManager(config.ChainConfig, false, config.NetworkId, eth.EventMux(), eth.Pow(), eth.BlockChain(), eth.TxPool(), eth.ChainDb(), nil, nil) + pm, err := NewProtocolManager(eth.BlockChain().Config(), false, config.NetworkId, eth.EventMux(), eth.Pow(), eth.BlockChain(), eth.TxPool(), eth.ChainDb(), nil, nil) if err != nil { return nil, err } -- cgit