diff options
author | Felix Lange <fjl@twurst.com> | 2017-03-02 21:03:33 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2017-03-23 22:58:43 +0800 |
commit | 37dd9086ec491900311fc39837f4a62ef5fd3a4a (patch) | |
tree | 0d2c7bb99ad1cd2ffa6d014e44288452a537aba5 /core/genesis_test.go | |
parent | 67c47459f283842bbf0063d9a1dc078251d6f433 (diff) | |
download | dexon-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/genesis_test.go')
-rw-r--r-- | core/genesis_test.go | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/core/genesis_test.go b/core/genesis_test.go new file mode 100644 index 000000000..b73dd776f --- /dev/null +++ b/core/genesis_test.go @@ -0,0 +1,161 @@ +// Copyright 2017 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. + +package core + +import ( + "math/big" + "reflect" + "testing" + + "github.com/davecgh/go-spew/spew" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/pow" +) + +func TestDefaultGenesisBlock(t *testing.T) { + block, _ := DefaultGenesisBlock().ToBlock() + if block.Hash() != params.MainNetGenesisHash { + t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainNetGenesisHash) + } + block, _ = DefaultTestnetGenesisBlock().ToBlock() + if block.Hash() != params.TestNetGenesisHash { + t.Errorf("wrong testnet genesis hash, got %v, want %v", block.Hash(), params.TestNetGenesisHash) + } +} + +func TestSetupGenesis(t *testing.T) { + var ( + customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50") + customg = Genesis{ + Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3)}, + Alloc: GenesisAlloc{ + {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, + }, + } + oldcustomg = customg + ) + oldcustomg.Config = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(2)} + tests := []struct { + name string + fn func(ethdb.Database) (*params.ChainConfig, common.Hash, error) + wantConfig *params.ChainConfig + wantHash common.Hash + wantErr error + }{ + { + name: "genesis without ChainConfig", + fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { + return SetupGenesisBlock(db, new(Genesis)) + }, + wantErr: errGenesisNoConfig, + wantConfig: params.AllProtocolChanges, + }, + { + name: "no block in DB, genesis == nil", + fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { + return SetupGenesisBlock(db, nil) + }, + wantHash: params.MainNetGenesisHash, + wantConfig: params.MainnetChainConfig, + }, + { + name: "mainnet block in DB, genesis == nil", + fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { + DefaultGenesisBlock().MustCommit(db) + return SetupGenesisBlock(db, nil) + }, + wantHash: params.MainNetGenesisHash, + wantConfig: params.MainnetChainConfig, + }, + { + name: "custom block in DB, genesis == nil", + fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { + customg.MustCommit(db) + return SetupGenesisBlock(db, nil) + }, + wantHash: customghash, + wantConfig: customg.Config, + }, + { + name: "custom block in DB, genesis == testnet", + fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { + customg.MustCommit(db) + return SetupGenesisBlock(db, DefaultTestnetGenesisBlock()) + }, + wantErr: &GenesisMismatchError{Stored: customghash, New: params.TestNetGenesisHash}, + wantHash: params.TestNetGenesisHash, + wantConfig: params.TestnetChainConfig, + }, + { + name: "compatible config in DB", + fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { + oldcustomg.MustCommit(db) + return SetupGenesisBlock(db, &customg) + }, + wantHash: customghash, + wantConfig: customg.Config, + }, + { + name: "incompatible config in DB", + fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { + // Commit the 'old' genesis block with Homestead transition at #2. + // Advance to block #4, past the homestead transition block of customg. + genesis := oldcustomg.MustCommit(db) + bc, _ := NewBlockChain(db, oldcustomg.Config, pow.FakePow{}, new(event.TypeMux), vm.Config{}) + bc.SetValidator(bproc{}) + bc.InsertChain(makeBlockChainWithDiff(genesis, []int{2, 3, 4, 5}, 0)) + bc.CurrentBlock() + // This should return a compatibility error. + return SetupGenesisBlock(db, &customg) + }, + wantHash: customghash, + wantConfig: customg.Config, + wantErr: ¶ms.ConfigCompatError{ + What: "Homestead fork block", + StoredConfig: big.NewInt(2), + NewConfig: big.NewInt(3), + RewindTo: 1, + }, + }, + } + + for _, test := range tests { + db, _ := ethdb.NewMemDatabase() + config, hash, err := test.fn(db) + // Check the return values. + if !reflect.DeepEqual(err, test.wantErr) { + spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true} + t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr)) + } + if !reflect.DeepEqual(config, test.wantConfig) { + t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig) + } + if hash != test.wantHash { + t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex()) + } else if err == nil { + // Check database content. + stored := GetBlock(db, test.wantHash, 0) + if stored.Hash() != test.wantHash { + t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash) + } + } + } +} |