Skip to content

Commit 83e4c49

Browse files
authored
trie : use trie.NewStackTrie instead of new(trie.Trie) (#22246)
The PR makes use of the stacktrie, which is is more lenient on resource consumption, than the regular trie, in cases where we only need it for DeriveSha
1 parent ef84da8 commit 83e4c49

File tree

12 files changed

+15
-15
lines changed

12 files changed

+15
-15
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
229229
}
230230
execRs := &ExecutionResult{
231231
StateRoot: root,
232-
TxRoot: types.DeriveSha(includedTxs, new(trie.Trie)),
233-
ReceiptRoot: types.DeriveSha(receipts, new(trie.Trie)),
232+
TxRoot: types.DeriveSha(includedTxs, trie.NewStackTrie(nil)),
233+
ReceiptRoot: types.DeriveSha(receipts, trie.NewStackTrie(nil)),
234234
Bloom: types.CreateBloom(receipts),
235235
LogsHash: rlpHash(statedb.Logs()),
236236
Receipts: receipts,

consensus/clique/clique.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
565565
c.Finalize(chain, header, state, txs, uncles)
566566

567567
// Assemble and return the final block for sealing
568-
return types.NewBlock(header, txs, nil, receipts, new(trie.Trie)), nil
568+
return types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil)), nil
569569
}
570570

571571
// Authorize injects a private key into the consensus engine to mint new blocks

consensus/ethash/consensus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ func (ethash *Ethash) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
586586
ethash.Finalize(chain, header, state, txs, uncles)
587587

588588
// Header seems complete, assemble into a block and return
589-
return types.NewBlock(header, txs, uncles, receipts, new(trie.Trie)), nil
589+
return types.NewBlock(header, txs, uncles, receipts, trie.NewStackTrie(nil)), nil
590590
}
591591

592592
// SealHash returns the hash of a block prior to it being sealed.

core/blockchain_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,12 @@ func TestFastVsFullChains(t *testing.T) {
683683
}
684684
if fblock, arblock, anblock := fast.GetBlockByHash(hash), archive.GetBlockByHash(hash), ancient.GetBlockByHash(hash); fblock.Hash() != arblock.Hash() || anblock.Hash() != arblock.Hash() {
685685
t.Errorf("block #%d [%x]: block mismatch: fastdb %v, ancientdb %v, archivedb %v", num, hash, fblock, anblock, arblock)
686-
} else if types.DeriveSha(fblock.Transactions(), new(trie.Trie)) != types.DeriveSha(arblock.Transactions(), new(trie.Trie)) || types.DeriveSha(anblock.Transactions(), new(trie.Trie)) != types.DeriveSha(arblock.Transactions(), new(trie.Trie)) {
686+
} else if types.DeriveSha(fblock.Transactions(), trie.NewStackTrie(nil)) != types.DeriveSha(arblock.Transactions(), trie.NewStackTrie(nil)) || types.DeriveSha(anblock.Transactions(), trie.NewStackTrie(nil)) != types.DeriveSha(arblock.Transactions(), trie.NewStackTrie(nil)) {
687687
t.Errorf("block #%d [%x]: transactions mismatch: fastdb %v, ancientdb %v, archivedb %v", num, hash, fblock.Transactions(), anblock.Transactions(), arblock.Transactions())
688688
} else if types.CalcUncleHash(fblock.Uncles()) != types.CalcUncleHash(arblock.Uncles()) || types.CalcUncleHash(anblock.Uncles()) != types.CalcUncleHash(arblock.Uncles()) {
689689
t.Errorf("block #%d [%x]: uncles mismatch: fastdb %v, ancientdb %v, archivedb %v", num, hash, fblock.Uncles(), anblock, arblock.Uncles())
690690
}
691-
if freceipts, anreceipts, areceipts := rawdb.ReadReceipts(fastDb, hash, *rawdb.ReadHeaderNumber(fastDb, hash), fast.Config()), rawdb.ReadReceipts(ancientDb, hash, *rawdb.ReadHeaderNumber(ancientDb, hash), fast.Config()), rawdb.ReadReceipts(archiveDb, hash, *rawdb.ReadHeaderNumber(archiveDb, hash), fast.Config()); types.DeriveSha(freceipts, new(trie.Trie)) != types.DeriveSha(areceipts, new(trie.Trie)) {
691+
if freceipts, anreceipts, areceipts := rawdb.ReadReceipts(fastDb, hash, *rawdb.ReadHeaderNumber(fastDb, hash), fast.Config()), rawdb.ReadReceipts(ancientDb, hash, *rawdb.ReadHeaderNumber(ancientDb, hash), fast.Config()), rawdb.ReadReceipts(archiveDb, hash, *rawdb.ReadHeaderNumber(archiveDb, hash), fast.Config()); types.DeriveSha(freceipts, trie.NewStackTrie(nil)) != types.DeriveSha(areceipts, trie.NewStackTrie(nil)) {
692692
t.Errorf("block #%d [%x]: receipts mismatch: fastdb %v, ancientdb %v, archivedb %v", num, hash, freceipts, anreceipts, areceipts)
693693
}
694694
}

core/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
288288
statedb.Commit(false)
289289
statedb.Database().TrieDB().Commit(root, true, nil)
290290

291-
return types.NewBlock(head, nil, nil, nil, new(trie.Trie))
291+
return types.NewBlock(head, nil, nil, nil, trie.NewStackTrie(nil))
292292
}
293293

294294
// Commit writes the block and state of a genesis specification to the database.

core/state_processor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,5 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr
148148
}
149149
header.Root = common.BytesToHash(hasher.Sum(nil))
150150
// Assemble and return the final block for sealing
151-
return types.NewBlock(header, txs, nil, receipts, new(trie.Trie))
151+
return types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil))
152152
}

core/tx_pool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type testBlockChain struct {
5555
func (bc *testBlockChain) CurrentBlock() *types.Block {
5656
return types.NewBlock(&types.Header{
5757
GasLimit: bc.gasLimit,
58-
}, nil, nil, nil, new(trie.Trie))
58+
}, nil, nil, nil, trie.NewStackTrie(nil))
5959
}
6060

6161
func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {

eth/fetcher/block_fetcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ func (f *BlockFetcher) loop() {
620620
continue
621621
}
622622
if txnHash == (common.Hash{}) {
623-
txnHash = types.DeriveSha(types.Transactions(task.transactions[i]), new(trie.Trie))
623+
txnHash = types.DeriveSha(types.Transactions(task.transactions[i]), trie.NewStackTrie(nil))
624624
}
625625
if txnHash != announce.header.TxHash {
626626
continue

eth/fetcher/block_fetcher_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var (
3939
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
4040
testAddress = crypto.PubkeyToAddress(testKey.PublicKey)
4141
genesis = core.GenesisBlockForTesting(testdb, testAddress, big.NewInt(1000000000))
42-
unknownBlock = types.NewBlock(&types.Header{GasLimit: params.GenesisGasLimit}, nil, nil, nil, new(trie.Trie))
42+
unknownBlock = types.NewBlock(&types.Header{GasLimit: params.GenesisGasLimit}, nil, nil, nil, trie.NewStackTrie(nil))
4343
)
4444

4545
// makeChain creates a chain of n blocks starting at and including parent.

les/odr_requests.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (r *BlockRequest) Validate(db ethdb.Database, msg *Msg) error {
116116
if r.Header == nil {
117117
return errHeaderUnavailable
118118
}
119-
if r.Header.TxHash != types.DeriveSha(types.Transactions(body.Transactions), new(trie.Trie)) {
119+
if r.Header.TxHash != types.DeriveSha(types.Transactions(body.Transactions), trie.NewStackTrie(nil)) {
120120
return errTxHashMismatch
121121
}
122122
if r.Header.UncleHash != types.CalcUncleHash(body.Uncles) {
@@ -174,7 +174,7 @@ func (r *ReceiptsRequest) Validate(db ethdb.Database, msg *Msg) error {
174174
if r.Header == nil {
175175
return errHeaderUnavailable
176176
}
177-
if r.Header.ReceiptHash != types.DeriveSha(receipt, new(trie.Trie)) {
177+
if r.Header.ReceiptHash != types.DeriveSha(receipt, trie.NewStackTrie(nil)) {
178178
return errReceiptHashMismatch
179179
}
180180
// Validations passed, store and return

0 commit comments

Comments
 (0)