2017-03-02 13:03:33 +00:00
|
|
|
// 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 (
|
2022-11-28 13:31:28 +00:00
|
|
|
"encoding/json"
|
2017-03-02 13:03:33 +00:00
|
|
|
"math/big"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-04-04 22:16:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2018-05-07 11:35:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2017-03-02 13:03:33 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2022-11-28 13:31:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie/triedb/pathdb"
|
2017-03-02 13:03:33 +00:00
|
|
|
)
|
|
|
|
|
2021-09-21 08:46:08 +00:00
|
|
|
func TestInvalidCliqueConfig(t *testing.T) {
|
|
|
|
block := DefaultGoerliGenesisBlock()
|
|
|
|
block.ExtraData = []byte{}
|
2022-11-28 13:31:28 +00:00
|
|
|
db := rawdb.NewMemoryDatabase()
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
if _, err := block.Commit(db, trie.NewDatabase(db, nil)); err == nil {
|
2021-09-21 08:46:08 +00:00
|
|
|
t.Fatal("Expected error on invalid clique config")
|
|
|
|
}
|
2017-03-02 13:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupGenesis(t *testing.T) {
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
testSetupGenesis(t, rawdb.HashScheme)
|
|
|
|
testSetupGenesis(t, rawdb.PathScheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSetupGenesis(t *testing.T, scheme string) {
|
2017-03-02 13:03:33 +00:00
|
|
|
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)}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
|
2017-03-02 13:03:33 +00:00
|
|
|
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) {
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
return SetupGenesisBlock(db, trie.NewDatabase(db, newDbConfig(scheme)), new(Genesis))
|
2017-03-02 13:03:33 +00:00
|
|
|
},
|
|
|
|
wantErr: errGenesisNoConfig,
|
2017-10-24 10:40:42 +00:00
|
|
|
wantConfig: params.AllEthashProtocolChanges,
|
2017-03-02 13:03:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no block in DB, genesis == nil",
|
|
|
|
fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
return SetupGenesisBlock(db, trie.NewDatabase(db, newDbConfig(scheme)), nil)
|
2017-03-02 13:03:33 +00:00
|
|
|
},
|
2017-07-04 09:28:58 +00:00
|
|
|
wantHash: params.MainnetGenesisHash,
|
2017-03-02 13:03:33 +00:00
|
|
|
wantConfig: params.MainnetChainConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mainnet block in DB, genesis == nil",
|
|
|
|
fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
DefaultGenesisBlock().MustCommit(db, trie.NewDatabase(db, newDbConfig(scheme)))
|
|
|
|
return SetupGenesisBlock(db, trie.NewDatabase(db, newDbConfig(scheme)), nil)
|
2017-03-02 13:03:33 +00:00
|
|
|
},
|
2017-07-04 09:28:58 +00:00
|
|
|
wantHash: params.MainnetGenesisHash,
|
2017-03-02 13:03:33 +00:00
|
|
|
wantConfig: params.MainnetChainConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "custom block in DB, genesis == nil",
|
|
|
|
fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
tdb := trie.NewDatabase(db, newDbConfig(scheme))
|
|
|
|
customg.Commit(db, tdb)
|
|
|
|
return SetupGenesisBlock(db, tdb, nil)
|
2017-03-02 13:03:33 +00:00
|
|
|
},
|
|
|
|
wantHash: customghash,
|
|
|
|
wantConfig: customg.Config,
|
|
|
|
},
|
|
|
|
{
|
2023-02-09 08:03:00 +00:00
|
|
|
name: "custom block in DB, genesis == goerli",
|
2017-03-02 13:03:33 +00:00
|
|
|
fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
tdb := trie.NewDatabase(db, newDbConfig(scheme))
|
|
|
|
customg.Commit(db, tdb)
|
|
|
|
return SetupGenesisBlock(db, tdb, DefaultGoerliGenesisBlock())
|
2017-03-02 13:03:33 +00:00
|
|
|
},
|
2023-02-09 08:03:00 +00:00
|
|
|
wantErr: &GenesisMismatchError{Stored: customghash, New: params.GoerliGenesisHash},
|
|
|
|
wantHash: params.GoerliGenesisHash,
|
|
|
|
wantConfig: params.GoerliChainConfig,
|
2017-03-02 13:03:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "compatible config in DB",
|
|
|
|
fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
tdb := trie.NewDatabase(db, newDbConfig(scheme))
|
|
|
|
oldcustomg.Commit(db, tdb)
|
|
|
|
return SetupGenesisBlock(db, tdb, &customg)
|
2017-03-02 13:03:33 +00:00
|
|
|
},
|
|
|
|
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.
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
tdb := trie.NewDatabase(db, newDbConfig(scheme))
|
|
|
|
oldcustomg.Commit(db, tdb)
|
2018-02-23 12:02:33 +00:00
|
|
|
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
bc, _ := NewBlockChain(db, DefaultCacheConfigWithScheme(scheme), &oldcustomg, nil, ethash.NewFullFaker(), vm.Config{}, nil, nil)
|
2017-08-07 12:47:25 +00:00
|
|
|
defer bc.Stop()
|
2018-02-23 12:02:33 +00:00
|
|
|
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
_, blocks, _ := GenerateChainWithGenesis(&oldcustomg, ethash.NewFaker(), 4, nil)
|
2018-02-23 12:02:33 +00:00
|
|
|
bc.InsertChain(blocks)
|
2022-11-28 13:31:28 +00:00
|
|
|
|
2017-03-02 13:03:33 +00:00
|
|
|
// This should return a compatibility error.
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
return SetupGenesisBlock(db, tdb, &customg)
|
2017-03-02 13:03:33 +00:00
|
|
|
},
|
|
|
|
wantHash: customghash,
|
|
|
|
wantConfig: customg.Config,
|
|
|
|
wantErr: ¶ms.ConfigCompatError{
|
2022-12-15 07:40:33 +00:00
|
|
|
What: "Homestead fork block",
|
|
|
|
StoredBlock: big.NewInt(2),
|
|
|
|
NewBlock: big.NewInt(3),
|
|
|
|
RewindToBlock: 1,
|
2017-03-02 13:03:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2018-09-24 12:57:49 +00:00
|
|
|
db := rawdb.NewMemoryDatabase()
|
2017-03-02 13:03:33 +00:00
|
|
|
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.
|
2018-05-07 11:35:06 +00:00
|
|
|
stored := rawdb.ReadBlock(db, test.wantHash, 0)
|
2017-03-02 13:03:33 +00:00
|
|
|
if stored.Hash() != test.wantHash {
|
|
|
|
t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-15 22:32:16 +00:00
|
|
|
|
2021-11-08 11:06:01 +00:00
|
|
|
// TestGenesisHashes checks the congruity of default genesis data to
|
|
|
|
// corresponding hardcoded genesis hash values.
|
2021-04-15 22:32:16 +00:00
|
|
|
func TestGenesisHashes(t *testing.T) {
|
2021-11-08 11:06:01 +00:00
|
|
|
for i, c := range []struct {
|
2021-04-15 22:32:16 +00:00
|
|
|
genesis *Genesis
|
2021-11-08 11:06:01 +00:00
|
|
|
want common.Hash
|
2021-04-15 22:32:16 +00:00
|
|
|
}{
|
2021-11-08 11:06:01 +00:00
|
|
|
{DefaultGenesisBlock(), params.MainnetGenesisHash},
|
|
|
|
{DefaultGoerliGenesisBlock(), params.GoerliGenesisHash},
|
|
|
|
{DefaultSepoliaGenesisBlock(), params.SepoliaGenesisHash},
|
|
|
|
} {
|
|
|
|
// Test via MustCommit
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
db := rawdb.NewMemoryDatabase()
|
|
|
|
if have := c.genesis.MustCommit(db, trie.NewDatabase(db, trie.HashDefaults)).Hash(); have != c.want {
|
2021-11-08 11:06:01 +00:00
|
|
|
t.Errorf("case: %d a), want: %s, got: %s", i, c.want.Hex(), have.Hex())
|
|
|
|
}
|
|
|
|
// Test via ToBlock
|
2022-08-09 09:44:39 +00:00
|
|
|
if have := c.genesis.ToBlock().Hash(); have != c.want {
|
2021-11-08 11:06:01 +00:00
|
|
|
t.Errorf("case: %d a), want: %s, got: %s", i, c.want.Hex(), have.Hex())
|
2021-04-15 22:32:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 06:44:43 +00:00
|
|
|
|
|
|
|
func TestGenesis_Commit(t *testing.T) {
|
|
|
|
genesis := &Genesis{
|
|
|
|
BaseFee: big.NewInt(params.InitialBaseFee),
|
|
|
|
Config: params.TestChainConfig,
|
|
|
|
// difficulty is nil
|
|
|
|
}
|
|
|
|
|
|
|
|
db := rawdb.NewMemoryDatabase()
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
genesisBlock := genesis.MustCommit(db, trie.NewDatabase(db, trie.HashDefaults))
|
2022-11-28 13:31:28 +00:00
|
|
|
|
2021-10-26 06:44:43 +00:00
|
|
|
if genesis.Difficulty != nil {
|
|
|
|
t.Fatalf("assumption wrong")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This value should have been set as default in the ToBlock method.
|
|
|
|
if genesisBlock.Difficulty().Cmp(params.GenesisDifficulty) != 0 {
|
|
|
|
t.Errorf("assumption wrong: want: %d, got: %v", params.GenesisDifficulty, genesisBlock.Difficulty())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expect the stored total difficulty to be the difficulty of the genesis block.
|
|
|
|
stored := rawdb.ReadTd(db, genesisBlock.Hash(), genesisBlock.NumberU64())
|
|
|
|
|
|
|
|
if stored.Cmp(genesisBlock.Difficulty()) != 0 {
|
|
|
|
t.Errorf("inequal difficulty; stored: %v, genesisBlock: %v", stored, genesisBlock.Difficulty())
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 09:53:22 +00:00
|
|
|
|
|
|
|
func TestReadWriteGenesisAlloc(t *testing.T) {
|
|
|
|
var (
|
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
|
alloc = &GenesisAlloc{
|
|
|
|
{1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
|
|
|
|
{2}: {Balance: big.NewInt(2), Storage: map[common.Hash]common.Hash{{2}: {2}}},
|
|
|
|
}
|
2022-08-09 09:44:39 +00:00
|
|
|
hash, _ = alloc.deriveHash()
|
2022-03-22 09:53:22 +00:00
|
|
|
)
|
2022-11-28 13:31:28 +00:00
|
|
|
blob, _ := json.Marshal(alloc)
|
|
|
|
rawdb.WriteGenesisStateSpec(db, hash, blob)
|
2022-03-22 09:53:22 +00:00
|
|
|
|
|
|
|
var reload GenesisAlloc
|
2022-08-09 09:44:39 +00:00
|
|
|
err := reload.UnmarshalJSON(rawdb.ReadGenesisStateSpec(db, hash))
|
2022-03-22 09:53:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to load genesis state %v", err)
|
|
|
|
}
|
|
|
|
if len(reload) != len(*alloc) {
|
|
|
|
t.Fatal("Unexpected genesis allocation")
|
|
|
|
}
|
|
|
|
for addr, account := range reload {
|
|
|
|
want, ok := (*alloc)[addr]
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Account is not found")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(want, account) {
|
|
|
|
t.Fatal("Unexpected account")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
|
|
|
|
func newDbConfig(scheme string) *trie.Config {
|
|
|
|
if scheme == rawdb.HashScheme {
|
|
|
|
return trie.HashDefaults
|
|
|
|
}
|
|
|
|
return &trie.Config{PathDB: pathdb.Defaults}
|
|
|
|
}
|