Skip to content

Commit cbe48af

Browse files
rjl493456442shekhirin
authored andcommitted
all: new empty trie with types.EmptyRootHash instead of null (ethereum#27230)
1 parent c0703ab commit cbe48af

25 files changed

+121
-113
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
293293

294294
func MakePreState(db ethdb.Database, accounts core.GenesisAlloc) *state.StateDB {
295295
sdb := state.NewDatabaseWithConfig(db, &trie.Config{Preimages: true})
296-
statedb, _ := state.New(common.Hash{}, sdb, nil)
296+
statedb, _ := state.New(types.EmptyRootHash, sdb, nil)
297297
for addr, a := range accounts {
298298
statedb.SetCode(addr, a.Code)
299299
statedb.SetNonce(addr, a.Nonce)

cmd/evm/runner.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/ethereum/go-ethereum/core"
3535
"github.com/ethereum/go-ethereum/core/rawdb"
3636
"github.com/ethereum/go-ethereum/core/state"
37+
"github.com/ethereum/go-ethereum/core/types"
3738
"github.com/ethereum/go-ethereum/core/vm"
3839
"github.com/ethereum/go-ethereum/core/vm/runtime"
3940
"github.com/ethereum/go-ethereum/eth/tracers/logger"
@@ -146,7 +147,7 @@ func runCmd(ctx *cli.Context) error {
146147
chainConfig = gen.Config
147148
} else {
148149
sdb := state.NewDatabaseWithConfig(rawdb.NewMemoryDatabase(), &trie.Config{Preimages: preimages})
149-
statedb, _ = state.New(common.Hash{}, sdb, nil)
150+
statedb, _ = state.New(types.EmptyRootHash, sdb, nil)
150151
genesisConfig = new(core.Genesis)
151152
}
152153
if ctx.String(SenderFlag.Name) != "" {

core/genesis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (ga *GenesisAlloc) deriveHash() (common.Hash, error) {
120120
// Create an ephemeral in-memory database for computing hash,
121121
// all the derived states will be discarded to not pollute disk.
122122
db := state.NewDatabase(rawdb.NewMemoryDatabase())
123-
statedb, err := state.New(common.Hash{}, db, nil)
123+
statedb, err := state.New(types.EmptyRootHash, db, nil)
124124
if err != nil {
125125
return common.Hash{}, err
126126
}
@@ -139,7 +139,7 @@ func (ga *GenesisAlloc) deriveHash() (common.Hash, error) {
139139
// all the generated states will be persisted into the given database.
140140
// Also, the genesis state specification will be flushed as well.
141141
func (ga *GenesisAlloc) flush(db ethdb.Database, triedb *trie.Database, blockhash common.Hash) error {
142-
statedb, err := state.New(common.Hash{}, state.NewDatabaseWithNodeDB(db, triedb), nil)
142+
statedb, err := state.New(types.EmptyRootHash, state.NewDatabaseWithNodeDB(db, triedb), nil)
143143
if err != nil {
144144
return err
145145
}

core/state/snapshot/generate_test.go

Lines changed: 41 additions & 41 deletions
Large diffs are not rendered by default.

core/state/state_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/ethereum/go-ethereum/common"
2525
"github.com/ethereum/go-ethereum/core/rawdb"
26+
"github.com/ethereum/go-ethereum/core/types"
2627
"github.com/ethereum/go-ethereum/crypto"
2728
"github.com/ethereum/go-ethereum/ethdb"
2829
"github.com/ethereum/go-ethereum/trie"
@@ -35,13 +36,13 @@ type stateTest struct {
3536

3637
func newStateTest() *stateTest {
3738
db := rawdb.NewMemoryDatabase()
38-
sdb, _ := New(common.Hash{}, NewDatabase(db), nil)
39+
sdb, _ := New(types.EmptyRootHash, NewDatabase(db), nil)
3940
return &stateTest{db: db, state: sdb}
4041
}
4142

4243
func TestDump(t *testing.T) {
4344
db := rawdb.NewMemoryDatabase()
44-
sdb, _ := New(common.Hash{}, NewDatabaseWithConfig(db, &trie.Config{Preimages: true}), nil)
45+
sdb, _ := New(types.EmptyRootHash, NewDatabaseWithConfig(db, &trie.Config{Preimages: true}), nil)
4546
s := &stateTest{db: db, state: sdb}
4647

4748
// generate a few entries
@@ -150,7 +151,7 @@ func TestSnapshotEmpty(t *testing.T) {
150151
}
151152

152153
func TestSnapshot2(t *testing.T) {
153-
state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
154+
state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
154155

155156
stateobjaddr0 := common.BytesToAddress([]byte("so0"))
156157
stateobjaddr1 := common.BytesToAddress([]byte("so1"))

core/state/statedb_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
func TestUpdateLeaks(t *testing.T) {
4040
// Create an empty state database
4141
db := rawdb.NewMemoryDatabase()
42-
state, _ := New(common.Hash{}, NewDatabase(db), nil)
42+
state, _ := New(types.EmptyRootHash, NewDatabase(db), nil)
4343

4444
// Update it with some accounts
4545
for i := byte(0); i < 255; i++ {
@@ -73,8 +73,8 @@ func TestIntermediateLeaks(t *testing.T) {
7373
// Create two state databases, one transitioning to the final state, the other final from the beginning
7474
transDb := rawdb.NewMemoryDatabase()
7575
finalDb := rawdb.NewMemoryDatabase()
76-
transState, _ := New(common.Hash{}, NewDatabase(transDb), nil)
77-
finalState, _ := New(common.Hash{}, NewDatabase(finalDb), nil)
76+
transState, _ := New(types.EmptyRootHash, NewDatabase(transDb), nil)
77+
finalState, _ := New(types.EmptyRootHash, NewDatabase(finalDb), nil)
7878

7979
modify := func(state *StateDB, addr common.Address, i, tweak byte) {
8080
state.SetBalance(addr, big.NewInt(int64(11*i)+int64(tweak)))
@@ -149,7 +149,7 @@ func TestIntermediateLeaks(t *testing.T) {
149149
// https://github.com/ethereum/go-ethereum/pull/15549.
150150
func TestCopy(t *testing.T) {
151151
// Create a random state test to copy and modify "independently"
152-
orig, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
152+
orig, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
153153

154154
for i := byte(0); i < 255; i++ {
155155
obj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
@@ -409,7 +409,7 @@ func (test *snapshotTest) String() string {
409409
func (test *snapshotTest) run() bool {
410410
// Run all actions and create snapshots.
411411
var (
412-
state, _ = New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
412+
state, _ = New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
413413
snapshotRevs = make([]int, len(test.snapshots))
414414
sindex = 0
415415
)
@@ -423,7 +423,7 @@ func (test *snapshotTest) run() bool {
423423
// Revert all snapshots in reverse order. Each revert must yield a state
424424
// that is equivalent to fresh state with all actions up the snapshot applied.
425425
for sindex--; sindex >= 0; sindex-- {
426-
checkstate, _ := New(common.Hash{}, state.Database(), nil)
426+
checkstate, _ := New(types.EmptyRootHash, state.Database(), nil)
427427
for _, action := range test.actions[:test.snapshots[sindex]] {
428428
action.fn(action, checkstate)
429429
}
@@ -501,7 +501,7 @@ func TestTouchDelete(t *testing.T) {
501501
// TestCopyOfCopy tests that modified objects are carried over to the copy, and the copy of the copy.
502502
// See https://github.com/ethereum/go-ethereum/pull/15225#issuecomment-380191512
503503
func TestCopyOfCopy(t *testing.T) {
504-
state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
504+
state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
505505
addr := common.HexToAddress("aaaa")
506506
state.SetBalance(addr, big.NewInt(42))
507507

@@ -518,7 +518,7 @@ func TestCopyOfCopy(t *testing.T) {
518518
//
519519
// See https://github.com/ethereum/go-ethereum/issues/20106.
520520
func TestCopyCommitCopy(t *testing.T) {
521-
state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
521+
state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
522522

523523
// Create an account and check if the retrieved balance is correct
524524
addr := common.HexToAddress("0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe")
@@ -590,7 +590,7 @@ func TestCopyCommitCopy(t *testing.T) {
590590
//
591591
// See https://github.com/ethereum/go-ethereum/issues/20106.
592592
func TestCopyCopyCommitCopy(t *testing.T) {
593-
state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
593+
state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
594594

595595
// Create an account and check if the retrieved balance is correct
596596
addr := common.HexToAddress("0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe")
@@ -680,7 +680,7 @@ func TestCopyCopyCommitCopy(t *testing.T) {
680680
// first, but the journal wiped the entire state object on create-revert.
681681
func TestDeleteCreateRevert(t *testing.T) {
682682
// Create an initial state with a single contract
683-
state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
683+
state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
684684

685685
addr := common.BytesToAddress([]byte("so"))
686686
state.SetBalance(addr, big.NewInt(1))
@@ -713,7 +713,7 @@ func TestMissingTrieNodes(t *testing.T) {
713713
memDb := rawdb.NewMemoryDatabase()
714714
db := NewDatabase(memDb)
715715
var root common.Hash
716-
state, _ := New(common.Hash{}, db, nil)
716+
state, _ := New(types.EmptyRootHash, db, nil)
717717
addr := common.BytesToAddress([]byte("so"))
718718
{
719719
state.SetBalance(addr, big.NewInt(1))
@@ -762,7 +762,7 @@ func TestStateDBAccessList(t *testing.T) {
762762

763763
memDb := rawdb.NewMemoryDatabase()
764764
db := NewDatabase(memDb)
765-
state, _ := New(common.Hash{}, db, nil)
765+
state, _ := New(types.EmptyRootHash, db, nil)
766766
state.accessList = newAccessList()
767767

768768
verifyAddrs := func(astrings ...string) {
@@ -932,7 +932,7 @@ func TestFlushOrderDataLoss(t *testing.T) {
932932
var (
933933
memdb = rawdb.NewMemoryDatabase()
934934
statedb = NewDatabase(memdb)
935-
state, _ = New(common.Hash{}, statedb, nil)
935+
state, _ = New(types.EmptyRootHash, statedb, nil)
936936
)
937937
for a := byte(0); a < 10; a++ {
938938
state.CreateAccount(common.Address{a})
@@ -968,7 +968,7 @@ func TestFlushOrderDataLoss(t *testing.T) {
968968
func TestStateDBTransientStorage(t *testing.T) {
969969
memDb := rawdb.NewMemoryDatabase()
970970
db := NewDatabase(memDb)
971-
state, _ := New(common.Hash{}, db, nil)
971+
state, _ := New(types.EmptyRootHash, db, nil)
972972

973973
key := common.Hash{0x01}
974974
value := common.Hash{0x02}

core/state/sync_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func makeTestState() (ethdb.Database, Database, common.Hash, []*testAccount) {
4343
// Create an empty state
4444
db := rawdb.NewMemoryDatabase()
4545
sdb := NewDatabase(db)
46-
state, _ := New(common.Hash{}, sdb, nil)
46+
state, _ := New(types.EmptyRootHash, sdb, nil)
4747

4848
// Fill it with some arbitrary data
4949
var accounts []*testAccount

core/state/trie_prefetcher_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ import (
2323

2424
"github.com/ethereum/go-ethereum/common"
2525
"github.com/ethereum/go-ethereum/core/rawdb"
26+
"github.com/ethereum/go-ethereum/core/types"
2627
)
2728

2829
func filledStateDB() *StateDB {
29-
state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
30+
state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
3031

3132
// Create an account and check if the retrieved balance is correct
3233
addr := common.HexToAddress("0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe")

core/txpool/txpool2_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestTransactionFutureAttack(t *testing.T) {
7878
t.Parallel()
7979

8080
// Create the pool to test the limit enforcement with
81-
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
81+
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
8282
blockchain := newTestBlockChain(1000000, statedb, new(event.Feed))
8383
config := testTxPoolConfig
8484
config.GlobalQueue = 100
@@ -114,7 +114,7 @@ func TestTransactionFutureAttack(t *testing.T) {
114114
func TestTransactionFuture1559(t *testing.T) {
115115
t.Parallel()
116116
// Create the pool to test the pricing enforcement with
117-
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
117+
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
118118
blockchain := newTestBlockChain(1000000, statedb, new(event.Feed))
119119
pool := NewTxPool(testTxPoolConfig, eip1559Config, blockchain)
120120
defer pool.Stop()
@@ -146,7 +146,7 @@ func TestTransactionFuture1559(t *testing.T) {
146146
func TestTransactionZAttack(t *testing.T) {
147147
t.Parallel()
148148
// Create the pool to test the pricing enforcement with
149-
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
149+
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
150150
blockchain := newTestBlockChain(1000000, statedb, new(event.Feed))
151151
pool := NewTxPool(testTxPoolConfig, eip1559Config, blockchain)
152152
defer pool.Stop()
@@ -213,7 +213,7 @@ func TestTransactionZAttack(t *testing.T) {
213213

214214
func BenchmarkFutureAttack(b *testing.B) {
215215
// Create the pool to test the limit enforcement with
216-
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
216+
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
217217
blockchain := newTestBlockChain(1000000, statedb, new(event.Feed))
218218
config := testTxPoolConfig
219219
config.GlobalQueue = 100

0 commit comments

Comments
 (0)