aboutsummaryrefslogtreecommitdiffstats
path: root/core/chain_makers.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 /core/chain_makers.go
parent67c47459f283842bbf0063d9a1dc078251d6f433 (diff)
downloaddexon-37dd9086ec491900311fc39837f4a62ef5fd3a4a.tar
dexon-37dd9086ec491900311fc39837f4a62ef5fd3a4a.tar.gz
dexon-37dd9086ec491900311fc39837f4a62ef5fd3a4a.tar.bz2
dexon-37dd9086ec491900311fc39837f4a62ef5fd3a4a.tar.lz
dexon-37dd9086ec491900311fc39837f4a62ef5fd3a4a.tar.xz
dexon-37dd9086ec491900311fc39837f4a62ef5fd3a4a.tar.zst
dexon-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 'core/chain_makers.go')
-rw-r--r--core/chain_makers.go34
1 files changed, 9 insertions, 25 deletions
diff --git a/core/chain_makers.go b/core/chain_makers.go
index f63496fdc..5bf1ece25 100644
--- a/core/chain_makers.go
+++ b/core/chain_makers.go
@@ -30,19 +30,6 @@ import (
"github.com/ethereum/go-ethereum/pow"
)
-/*
- * TODO: move this to another package.
- */
-
-// MakeChainConfig returns a new ChainConfig with the ethereum default chain settings.
-func MakeChainConfig() *params.ChainConfig {
- return &params.ChainConfig{
- HomesteadBlock: big.NewInt(0),
- DAOForkBlock: nil,
- DAOForkSupport: true,
- }
-}
-
// So we can deterministically seed different blockchains
var (
canonicalSeed = 1
@@ -154,7 +141,7 @@ func (b *BlockGen) OffsetTime(seconds int64) {
if b.header.Time.Cmp(b.parent.Header().Time) <= 0 {
panic("block time out of range")
}
- b.header.Difficulty = CalcDifficulty(MakeChainConfig(), b.header.Time.Uint64(), b.parent.Time().Uint64(), b.parent.Number(), b.parent.Difficulty())
+ b.header.Difficulty = CalcDifficulty(b.config, b.header.Time.Uint64(), b.parent.Time().Uint64(), b.parent.Number(), b.parent.Difficulty())
}
// GenerateChain creates a chain of n blocks. The first block's
@@ -170,14 +157,13 @@ func (b *BlockGen) OffsetTime(seconds int64) {
// values. Inserting them into BlockChain requires use of FakePow or
// a similar non-validating proof of work implementation.
func GenerateChain(config *params.ChainConfig, parent *types.Block, db ethdb.Database, n int, gen func(int, *BlockGen)) ([]*types.Block, []types.Receipts) {
+ if config == nil {
+ config = params.TestChainConfig
+ }
blocks, receipts := make(types.Blocks, n), make([]types.Receipts, n)
genblock := func(i int, h *types.Header, statedb *state.StateDB) (*types.Block, types.Receipts) {
b := &BlockGen{parent: parent, i: i, chain: blocks, header: h, statedb: statedb, config: config}
-
// Mutate the state and block according to any hard-fork specs
- if config == nil {
- config = MakeChainConfig()
- }
if daoBlock := config.DAOForkBlock; daoBlock != nil {
limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange)
if h.Number.Cmp(daoBlock) >= 0 && h.Number.Cmp(limit) < 0 {
@@ -226,7 +212,7 @@ func makeHeader(config *params.ChainConfig, parent *types.Block, state *state.St
Root: state.IntermediateRoot(config.IsEIP158(parent.Number())),
ParentHash: parent.Hash(),
Coinbase: parent.Coinbase(),
- Difficulty: CalcDifficulty(MakeChainConfig(), time.Uint64(), new(big.Int).Sub(time, big.NewInt(10)).Uint64(), parent.Number(), parent.Difficulty()),
+ Difficulty: CalcDifficulty(config, time.Uint64(), new(big.Int).Sub(time, big.NewInt(10)).Uint64(), parent.Number(), parent.Difficulty()),
GasLimit: CalcGasLimit(parent),
GasUsed: new(big.Int),
Number: new(big.Int).Add(parent.Number(), common.Big1),
@@ -238,14 +224,12 @@ func makeHeader(config *params.ChainConfig, parent *types.Block, state *state.St
// chain. Depending on the full flag, if creates either a full block chain or a
// header only chain.
func newCanonical(n int, full bool) (ethdb.Database, *BlockChain, error) {
- // Create the new chain database
- db, _ := ethdb.NewMemDatabase()
- evmux := &event.TypeMux{}
-
// Initialize a fresh chain with only a genesis block
- genesis, _ := WriteTestNetGenesisBlock(db)
+ gspec := new(Genesis)
+ db, _ := ethdb.NewMemDatabase()
+ genesis := gspec.MustCommit(db)
- blockchain, _ := NewBlockChain(db, MakeChainConfig(), pow.FakePow{}, evmux, vm.Config{})
+ blockchain, _ := NewBlockChain(db, params.AllProtocolChanges, pow.FakePow{}, new(event.TypeMux), vm.Config{})
// Create and inject the requested chain
if n == 0 {
return db, blockchain, nil