diff options
Diffstat (limited to 'les')
-rw-r--r-- | les/backend.go | 22 | ||||
-rw-r--r-- | les/helper_test.go | 25 | ||||
-rw-r--r-- | les/server.go | 2 |
3 files changed, 27 insertions, 22 deletions
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 } |