aboutsummaryrefslogtreecommitdiffstats
path: root/eth/backend.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2017-03-02 21:03:33 +0800
committerFelix Lange <fjl@twurst.com>2017-03-23 22:58:43 +0800
commit37dd9086ec491900311fc39837f4a62ef5fd3a4a (patch)
tree0d2c7bb99ad1cd2ffa6d014e44288452a537aba5 /eth/backend.go
parent67c47459f283842bbf0063d9a1dc078251d6f433 (diff)
downloadgo-tangerine-37dd9086ec491900311fc39837f4a62ef5fd3a4a.tar
go-tangerine-37dd9086ec491900311fc39837f4a62ef5fd3a4a.tar.gz
go-tangerine-37dd9086ec491900311fc39837f4a62ef5fd3a4a.tar.bz2
go-tangerine-37dd9086ec491900311fc39837f4a62ef5fd3a4a.tar.lz
go-tangerine-37dd9086ec491900311fc39837f4a62ef5fd3a4a.tar.xz
go-tangerine-37dd9086ec491900311fc39837f4a62ef5fd3a4a.tar.zst
go-tangerine-37dd9086ec491900311fc39837f4a62ef5fd3a4a.zip
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.
Diffstat (limited to 'eth/backend.go')
-rw-r--r--eth/backend.go87
1 files changed, 25 insertions, 62 deletions
diff --git a/eth/backend.go b/eth/backend.go
index ef951a6c2..b8df98976 100644
--- a/eth/backend.go
+++ b/eth/backend.go
@@ -18,11 +18,9 @@
package eth
import (
- "errors"
"fmt"
"math/big"
"regexp"
- "strings"
"sync"
"time"
@@ -60,15 +58,17 @@ var (
)
type Config struct {
- ChainConfig *params.ChainConfig // chain configuration
+ // The genesis block, which is inserted if the database is empty.
+ // If nil, the Ethereum main net block is used.
+ Genesis *core.Genesis
- NetworkId int // Network ID to use for selecting peers to connect to
- Genesis string // Genesis JSON to seed the chain database with
- FastSync bool // Enables the state download based fast synchronisation algorithm
- LightMode bool // Running in light client mode
- LightServ int // Maximum percentage of time allowed for serving LES requests
- LightPeers int // Maximum number of LES client peers
- MaxPeers int // Maximum number of global peers
+ NetworkId int // Network ID to use for selecting peers to connect to
+
+ FastSync bool // Enables the state download based fast synchronisation algorithm
+ LightMode bool // Running in light client mode
+ LightServ int // Maximum percentage of time allowed for serving LES requests
+ LightPeers int // Maximum number of LES client peers
+ MaxPeers int // Maximum number of global peers
SkipBcVersionCheck bool // e.g. blockchain export
DatabaseCache int
@@ -100,9 +100,6 @@ type Config struct {
GpobaseCorrectionFactor int
EnablePreimageRecording bool
-
- TestGenesisBlock *types.Block // Genesis block to seed the chain database with (testing only!)
- TestGenesisState ethdb.Database // Genesis state to seed the database with (testing only!)
}
type LesServer interface {
@@ -155,11 +152,15 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
return nil, err
}
stopDbUpgrade := upgradeSequentialKeys(chainDb)
- if err := SetupGenesisBlock(&chainDb, config); err != nil {
- return nil, err
+ chainConfig, genesisHash, genesisErr := core.SetupGenesisBlock(chainDb, config.Genesis)
+ if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok {
+ return nil, genesisErr
}
+ log.Info("Initialised chain configuration", "config", chainConfig)
+
eth := &Ethereum{
chainDb: chainDb,
+ chainConfig: chainConfig,
eventMux: ctx.EventMux,
accountManager: ctx.AccountManager,
pow: CreatePoW(ctx, config),
@@ -184,33 +185,18 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
core.WriteBlockChainVersion(chainDb, core.BlockChainVersion)
}
- // load the genesis block or write a new one if no genesis
- // block is prenent in the database.
- genesis := core.GetBlock(chainDb, core.GetCanonicalHash(chainDb, 0), 0)
- if genesis == nil {
- genesis, err = core.WriteDefaultGenesisBlock(chainDb)
- if err != nil {
- return nil, err
- }
- log.Warn("Wrote default Ethereum genesis block")
- }
-
- if config.ChainConfig == nil {
- return nil, errors.New("missing chain config")
- }
- core.WriteChainConfig(chainDb, genesis.Hash(), config.ChainConfig)
-
- eth.chainConfig = config.ChainConfig
-
- log.Info("Initialised chain configuration", "config", eth.chainConfig)
-
- eth.blockchain, err = core.NewBlockChain(chainDb, eth.chainConfig, eth.pow, eth.EventMux(), vm.Config{EnablePreimageRecording: config.EnablePreimageRecording})
+ vmConfig := vm.Config{EnablePreimageRecording: config.EnablePreimageRecording}
+ eth.blockchain, err = core.NewBlockChain(chainDb, eth.chainConfig, eth.pow, eth.eventMux, vmConfig)
if err != nil {
- if err == core.ErrNoGenesis {
- return nil, fmt.Errorf(`No chain found. Please initialise a new chain using the "init" subcommand.`)
- }
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)
+ }
+
newPool := core.NewTxPool(eth.chainConfig, eth.EventMux(), eth.blockchain.State, eth.blockchain.GasLimit)
eth.txPool = newPool
@@ -255,29 +241,6 @@ func CreateDB(ctx *node.ServiceContext, config *Config, name string) (ethdb.Data
return db, err
}
-// SetupGenesisBlock initializes the genesis block for an Ethereum service
-func SetupGenesisBlock(chainDb *ethdb.Database, config *Config) error {
- // Load up any custom genesis block if requested
- if len(config.Genesis) > 0 {
- block, err := core.WriteGenesisBlock(*chainDb, strings.NewReader(config.Genesis))
- if err != nil {
- return err
- }
- log.Info("Successfully wrote custom genesis block", "hash", block.Hash())
- }
- // Load up a test setup if directly injected
- if config.TestGenesisState != nil {
- *chainDb = config.TestGenesisState
- }
- if config.TestGenesisBlock != nil {
- core.WriteTd(*chainDb, config.TestGenesisBlock.Hash(), config.TestGenesisBlock.NumberU64(), config.TestGenesisBlock.Difficulty())
- core.WriteBlock(*chainDb, config.TestGenesisBlock)
- core.WriteCanonicalHash(*chainDb, config.TestGenesisBlock.Hash(), config.TestGenesisBlock.NumberU64())
- core.WriteHeadBlockHash(*chainDb, config.TestGenesisBlock.Hash())
- }
- return nil
-}
-
// CreatePoW creates the required type of PoW instance for an Ethereum service
func CreatePoW(ctx *node.ServiceContext, config *Config) pow.PoW {
switch {