Skip to content

Commit fe01a2f

Browse files
authored
all: use unified emptyRootHash and emptyCodeHash (#26718)
The EmptyRootHash and EmptyCodeHash are defined everywhere in the codebase, this PR replaces all of them with unified one defined in core/types package, and also defines constants for TxRoot, WithdrawalsRoot and UncleRoot
1 parent 2f20fd3 commit fe01a2f

36 files changed

+202
-209
lines changed

cmd/devp2p/internal/ethtest/snap.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"math/rand"
2424

2525
"github.com/ethereum/go-ethereum/common"
26+
"github.com/ethereum/go-ethereum/core/types"
2627
"github.com/ethereum/go-ethereum/crypto"
2728
"github.com/ethereum/go-ethereum/eth/protocols/snap"
2829
"github.com/ethereum/go-ethereum/internal/utesting"
@@ -210,13 +211,6 @@ type byteCodesTest struct {
210211
expHashes int
211212
}
212213

213-
var (
214-
// emptyRoot is the known root hash of an empty trie.
215-
emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
216-
// emptyCode is the known hash of the empty EVM bytecode.
217-
emptyCode = common.HexToHash("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")
218-
)
219-
220214
// TestSnapGetByteCodes various forms of GetByteCodes requests.
221215
func (s *Suite) TestSnapGetByteCodes(t *utesting.T) {
222216
// The halfchain import should yield these bytecodes
@@ -263,15 +257,15 @@ func (s *Suite) TestSnapGetByteCodes(t *utesting.T) {
263257
},
264258
// Empties
265259
{
266-
nBytes: 10000, hashes: []common.Hash{emptyRoot},
260+
nBytes: 10000, hashes: []common.Hash{types.EmptyRootHash},
267261
expHashes: 0,
268262
},
269263
{
270-
nBytes: 10000, hashes: []common.Hash{emptyCode},
264+
nBytes: 10000, hashes: []common.Hash{types.EmptyCodeHash},
271265
expHashes: 1,
272266
},
273267
{
274-
nBytes: 10000, hashes: []common.Hash{emptyCode, emptyCode, emptyCode},
268+
nBytes: 10000, hashes: []common.Hash{types.EmptyCodeHash, types.EmptyCodeHash, types.EmptyCodeHash},
275269
expHashes: 3,
276270
},
277271
// The existing bytecodes
@@ -363,7 +357,7 @@ func (s *Suite) TestSnapTrieNodes(t *utesting.T) {
363357
for i := 1; i <= 65; i++ {
364358
accPaths = append(accPaths, pathTo(i))
365359
}
366-
empty := emptyCode
360+
empty := types.EmptyCodeHash
367361
for i, tc := range []trieNodesTest{
368362
{
369363
root: s.chain.RootAt(999),

cmd/evm/internal/t8ntool/block.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ func (i *bbInput) ToBlock() *types.Block {
120120
UncleHash: types.EmptyUncleHash,
121121
Coinbase: common.Address{},
122122
Root: i.Header.Root,
123-
TxHash: types.EmptyRootHash,
124-
ReceiptHash: types.EmptyRootHash,
123+
TxHash: types.EmptyTxsHash,
124+
ReceiptHash: types.EmptyReceiptsHash,
125125
Bloom: i.Header.Bloom,
126126
Difficulty: common.Big0,
127127
Number: i.Header.Number,

cmd/geth/snapshot.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ import (
3838
cli "github.com/urfave/cli/v2"
3939
)
4040

41-
var (
42-
// emptyRoot is the known root hash of an empty trie.
43-
emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
44-
45-
// emptyCode is the known hash of the empty EVM bytecode.
46-
emptyCode = crypto.Keccak256(nil)
47-
)
48-
4941
var (
5042
snapshotCommand = &cli.Command{
5143
Name: "snapshot",
@@ -308,7 +300,7 @@ func traverseState(ctx *cli.Context) error {
308300
log.Error("Invalid account encountered during traversal", "err", err)
309301
return err
310302
}
311-
if acc.Root != emptyRoot {
303+
if acc.Root != types.EmptyRootHash {
312304
id := trie.StorageTrieID(root, common.BytesToHash(accIter.Key), acc.Root)
313305
storageTrie, err := trie.NewStateTrie(id, triedb)
314306
if err != nil {
@@ -324,7 +316,7 @@ func traverseState(ctx *cli.Context) error {
324316
return storageIter.Err
325317
}
326318
}
327-
if !bytes.Equal(acc.CodeHash, emptyCode) {
319+
if !bytes.Equal(acc.CodeHash, types.EmptyCodeHash.Bytes()) {
328320
if !rawdb.HasCode(chaindb, common.BytesToHash(acc.CodeHash)) {
329321
log.Error("Code is missing", "hash", common.BytesToHash(acc.CodeHash))
330322
return errors.New("missing code")
@@ -423,7 +415,7 @@ func traverseRawState(ctx *cli.Context) error {
423415
log.Error("Invalid account encountered during traversal", "err", err)
424416
return errors.New("invalid account")
425417
}
426-
if acc.Root != emptyRoot {
418+
if acc.Root != types.EmptyRootHash {
427419
id := trie.StorageTrieID(root, common.BytesToHash(accIter.LeafKey()), acc.Root)
428420
storageTrie, err := trie.NewStateTrie(id, triedb)
429421
if err != nil {
@@ -461,7 +453,7 @@ func traverseRawState(ctx *cli.Context) error {
461453
return storageIter.Error()
462454
}
463455
}
464-
if !bytes.Equal(acc.CodeHash, emptyCode) {
456+
if !bytes.Equal(acc.CodeHash, types.EmptyCodeHash.Bytes()) {
465457
if !rawdb.HasCode(chaindb, common.BytesToHash(acc.CodeHash)) {
466458
log.Error("Code is missing", "account", common.BytesToHash(accIter.LeafKey()))
467459
return errors.New("missing code")
@@ -536,7 +528,7 @@ func dumpState(ctx *cli.Context) error {
536528
CodeHash: account.CodeHash,
537529
SecureKey: accIt.Hash().Bytes(),
538530
}
539-
if !conf.SkipCode && !bytes.Equal(account.CodeHash, emptyCode) {
531+
if !conf.SkipCode && !bytes.Equal(account.CodeHash, types.EmptyCodeHash.Bytes()) {
540532
da.Code = rawdb.ReadCode(db, common.BytesToHash(account.CodeHash))
541533
}
542534
if !conf.SkipStorage {

consensus/ethash/algorithm_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,11 +709,11 @@ func TestConcurrentDiskCacheGeneration(t *testing.T) {
709709
block := types.NewBlockWithHeader(&types.Header{
710710
Number: big.NewInt(3311058),
711711
ParentHash: common.HexToHash("0xd783efa4d392943503f28438ad5830b2d5964696ffc285f338585e9fe0a37a05"),
712-
UncleHash: common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"),
712+
UncleHash: types.EmptyUncleHash,
713713
Coinbase: common.HexToAddress("0xc0ea08a2d404d3172d2add29a45be56da40e2949"),
714714
Root: common.HexToHash("0x77d14e10470b5850332524f8cd6f69ad21f070ce92dca33ab2858300242ef2f1"),
715-
TxHash: common.HexToHash("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"),
716-
ReceiptHash: common.HexToHash("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"),
715+
TxHash: types.EmptyTxsHash,
716+
ReceiptHash: types.EmptyReceiptsHash,
717717
Difficulty: big.NewInt(167925187834220),
718718
GasLimit: 4015682,
719719
GasUsed: 0,

core/bench_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ func makeChainForBench(db ethdb.Database, full bool, count uint64) {
252252
ParentHash: hash,
253253
Difficulty: big.NewInt(1),
254254
UncleHash: types.EmptyUncleHash,
255-
TxHash: types.EmptyRootHash,
256-
ReceiptHash: types.EmptyRootHash,
255+
TxHash: types.EmptyTxsHash,
256+
ReceiptHash: types.EmptyReceiptsHash,
257257
}
258258
hash = header.Hash()
259259

core/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ func (g *Genesis) ToBlock() *types.Block {
467467
}
468468
var withdrawals []*types.Withdrawal
469469
if g.Config != nil && g.Config.IsShanghai(g.Timestamp) {
470-
head.WithdrawalsHash = &types.EmptyRootHash
470+
head.WithdrawalsHash = &types.EmptyWithdrawalsHash
471471
withdrawals = make([]*types.Withdrawal, 0)
472472
}
473473
return types.NewBlock(head, nil, nil, nil, trie.NewStackTrie(nil)).WithWithdrawals(withdrawals)

core/rawdb/accessors_chain_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ func TestBlockStorage(t *testing.T) {
113113
block := types.NewBlockWithHeader(&types.Header{
114114
Extra: []byte("test block"),
115115
UncleHash: types.EmptyUncleHash,
116-
TxHash: types.EmptyRootHash,
117-
ReceiptHash: types.EmptyRootHash,
116+
TxHash: types.EmptyTxsHash,
117+
ReceiptHash: types.EmptyReceiptsHash,
118118
})
119119
if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
120120
t.Fatalf("Non existent block returned: %v", entry)
@@ -161,8 +161,8 @@ func TestPartialBlockStorage(t *testing.T) {
161161
block := types.NewBlockWithHeader(&types.Header{
162162
Extra: []byte("test block"),
163163
UncleHash: types.EmptyUncleHash,
164-
TxHash: types.EmptyRootHash,
165-
ReceiptHash: types.EmptyRootHash,
164+
TxHash: types.EmptyTxsHash,
165+
ReceiptHash: types.EmptyReceiptsHash,
166166
})
167167
// Store a header and check that it's not recognized as a block
168168
WriteHeader(db, block.Header())
@@ -198,8 +198,8 @@ func TestBadBlockStorage(t *testing.T) {
198198
Number: big.NewInt(1),
199199
Extra: []byte("bad block"),
200200
UncleHash: types.EmptyUncleHash,
201-
TxHash: types.EmptyRootHash,
202-
ReceiptHash: types.EmptyRootHash,
201+
TxHash: types.EmptyTxsHash,
202+
ReceiptHash: types.EmptyReceiptsHash,
203203
})
204204
if entry := ReadBadBlock(db, block.Hash()); entry != nil {
205205
t.Fatalf("Non existent block returned: %v", entry)
@@ -216,8 +216,8 @@ func TestBadBlockStorage(t *testing.T) {
216216
Number: big.NewInt(2),
217217
Extra: []byte("bad block two"),
218218
UncleHash: types.EmptyUncleHash,
219-
TxHash: types.EmptyRootHash,
220-
ReceiptHash: types.EmptyRootHash,
219+
TxHash: types.EmptyTxsHash,
220+
ReceiptHash: types.EmptyReceiptsHash,
221221
})
222222
WriteBadBlock(db, blockTwo)
223223

@@ -235,8 +235,8 @@ func TestBadBlockStorage(t *testing.T) {
235235
Number: big.NewInt(int64(n)),
236236
Extra: []byte("bad block"),
237237
UncleHash: types.EmptyUncleHash,
238-
TxHash: types.EmptyRootHash,
239-
ReceiptHash: types.EmptyRootHash,
238+
TxHash: types.EmptyTxsHash,
239+
ReceiptHash: types.EmptyReceiptsHash,
240240
})
241241
WriteBadBlock(db, block)
242242
}
@@ -446,8 +446,8 @@ func TestAncientStorage(t *testing.T) {
446446
Number: big.NewInt(0),
447447
Extra: []byte("test block"),
448448
UncleHash: types.EmptyUncleHash,
449-
TxHash: types.EmptyRootHash,
450-
ReceiptHash: types.EmptyRootHash,
449+
TxHash: types.EmptyTxsHash,
450+
ReceiptHash: types.EmptyReceiptsHash,
451451
})
452452
// Ensure nothing non-existent will be read
453453
hash, number := block.Hash(), block.NumberU64()
@@ -889,8 +889,8 @@ func TestHeadersRLPStorage(t *testing.T) {
889889
Number: big.NewInt(int64(i)),
890890
Extra: []byte("test block"),
891891
UncleHash: types.EmptyUncleHash,
892-
TxHash: types.EmptyRootHash,
893-
ReceiptHash: types.EmptyRootHash,
892+
TxHash: types.EmptyTxsHash,
893+
ReceiptHash: types.EmptyReceiptsHash,
894894
ParentHash: pHash,
895895
})
896896
chain = append(chain, block)

core/state/iterator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (it *NodeIterator) step() error {
117117
if !it.dataIt.Next(true) {
118118
it.dataIt = nil
119119
}
120-
if !bytes.Equal(account.CodeHash, emptyCodeHash) {
120+
if !bytes.Equal(account.CodeHash, types.EmptyCodeHash.Bytes()) {
121121
it.codeHash = common.BytesToHash(account.CodeHash)
122122
addrHash := common.BytesToHash(it.stateIt.LeafKey())
123123
it.code, err = it.state.db.ContractCode(addrHash, common.BytesToHash(account.CodeHash))

core/state/pruner/pruner.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/ethereum/go-ethereum/core/rawdb"
3232
"github.com/ethereum/go-ethereum/core/state/snapshot"
3333
"github.com/ethereum/go-ethereum/core/types"
34-
"github.com/ethereum/go-ethereum/crypto"
3534
"github.com/ethereum/go-ethereum/ethdb"
3635
"github.com/ethereum/go-ethereum/log"
3736
"github.com/ethereum/go-ethereum/rlp"
@@ -55,14 +54,6 @@ const (
5554
rangeCompactionThreshold = 100000
5655
)
5756

58-
var (
59-
// emptyRoot is the known root hash of an empty trie.
60-
emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
61-
62-
// emptyCode is the known hash of the empty EVM bytecode.
63-
emptyCode = crypto.Keccak256(nil)
64-
)
65-
6657
// Config includes all the configurations for pruning.
6758
type Config struct {
6859
Datadir string // The directory of the state database
@@ -446,7 +437,7 @@ func extractGenesis(db ethdb.Database, stateBloom *stateBloom) error {
446437
if err := rlp.DecodeBytes(accIter.LeafBlob(), &acc); err != nil {
447438
return err
448439
}
449-
if acc.Root != emptyRoot {
440+
if acc.Root != types.EmptyRootHash {
450441
id := trie.StorageTrieID(genesis.Root(), common.BytesToHash(accIter.LeafKey()), acc.Root)
451442
storageTrie, err := trie.NewStateTrie(id, trie.NewDatabase(db))
452443
if err != nil {
@@ -463,7 +454,7 @@ func extractGenesis(db ethdb.Database, stateBloom *stateBloom) error {
463454
return storageIter.Error()
464455
}
465456
}
466-
if !bytes.Equal(acc.CodeHash, emptyCode) {
457+
if !bytes.Equal(acc.CodeHash, types.EmptyCodeHash.Bytes()) {
467458
stateBloom.Put(acc.CodeHash, nil)
468459
}
469460
}

core/state/snapshot/account.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"math/big"
2222

2323
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/core/types"
2425
"github.com/ethereum/go-ethereum/rlp"
2526
)
2627

@@ -41,10 +42,10 @@ func SlimAccount(nonce uint64, balance *big.Int, root common.Hash, codehash []by
4142
Nonce: nonce,
4243
Balance: balance,
4344
}
44-
if root != emptyRoot {
45+
if root != types.EmptyRootHash {
4546
slim.Root = root[:]
4647
}
47-
if !bytes.Equal(codehash, emptyCode[:]) {
48+
if !bytes.Equal(codehash, types.EmptyCodeHash[:]) {
4849
slim.CodeHash = codehash
4950
}
5051
return slim
@@ -68,10 +69,10 @@ func FullAccount(data []byte) (Account, error) {
6869
return Account{}, err
6970
}
7071
if len(account.Root) == 0 {
71-
account.Root = emptyRoot[:]
72+
account.Root = types.EmptyRootHash[:]
7273
}
7374
if len(account.CodeHash) == 0 {
74-
account.CodeHash = emptyCode[:]
75+
account.CodeHash = types.EmptyCodeHash[:]
7576
}
7677
return account, nil
7778
}

0 commit comments

Comments
 (0)