Skip to content

Commit

Permalink
Chore: Change parameter of NewBlock function from Trie to Stacktrie
Browse files Browse the repository at this point in the history
  • Loading branch information
c98tristan committed Jul 25, 2023
1 parent 44d2c54 commit 72ca227
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ func (c *Clique) Finalize(chain consensus.ChainReader, header *types.Header, sta
header.UncleHash = types.CalcUncleHash(nil)

// Assemble and return the final block for sealing
return types.NewBlock(header, txs, nil, receipts, new(trie.Trie)), nil
return types.NewBlock(header, txs, nil, receipts, new(trie.StackTrie)), nil
}

// Authorize injects a private key into the consensus engine to mint new blocks
Expand Down
2 changes: 1 addition & 1 deletion consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func (ethash *Ethash) Finalize(chain consensus.ChainReader, header *types.Header
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))

// Header seems complete, assemble into a block and return
return types.NewBlock(header, txs, uncles, receipts, new(trie.Trie)), nil
return types.NewBlock(header, txs, uncles, receipts, new(trie.StackTrie)), nil
}

// Some weird constants to avoid constant memory allocs for them.
Expand Down
2 changes: 1 addition & 1 deletion consensus/posv/posv.go
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ func (c *Posv) Finalize(chain consensus.ChainReader, header *types.Header, state
header.UncleHash = types.CalcUncleHash(nil)

// Assemble and return the final block for sealing
return types.NewBlock(header, txs, nil, receipts, new(trie.Trie)), nil
return types.NewBlock(header, txs, nil, receipts, new(trie.StackTrie)), nil
}

// Authorize injects a private key into the consensus engine to mint new blocks
Expand Down
4 changes: 2 additions & 2 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
if hash := types.CalcUncleHash(block.Uncles()); hash != header.UncleHash {
return fmt.Errorf("uncle root hash mismatch: have %x, want %x", hash, header.UncleHash)
}
if hash := types.DeriveSha(block.Transactions(), new(trie.Trie)); hash != header.TxHash {
if hash := types.DeriveSha(block.Transactions(), new(trie.StackTrie)); hash != header.TxHash {
return fmt.Errorf("transaction root hash mismatch: have %x, want %x", hash, header.TxHash)
}
return nil
Expand All @@ -95,7 +95,7 @@ func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *stat
return fmt.Errorf("invalid bloom (remote: %x local: %x)", header.Bloom, rbloom)
}
// Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, R1]]))
receiptSha := types.DeriveSha(receipts, new(trie.Trie))
receiptSha := types.DeriveSha(receipts, new(trie.StackTrie))
if receiptSha != header.ReceiptHash {
return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha)
}
Expand Down
4 changes: 2 additions & 2 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,12 +619,12 @@ func TestFastVsFullChains(t *testing.T) {
}
if fblock, ablock := fast.GetBlockByHash(hash), archive.GetBlockByHash(hash); fblock.Hash() != ablock.Hash() {
t.Errorf("block #%d [%x]: block mismatch: have %v, want %v", num, hash, fblock, ablock)
} else if types.DeriveSha(fblock.Transactions(), new(trie.Trie)) != types.DeriveSha(ablock.Transactions(), new(trie.Trie)) {
} else if types.DeriveSha(fblock.Transactions(), new(trie.StackTrie)) != types.DeriveSha(ablock.Transactions(), new(trie.StackTrie)) {
t.Errorf("block #%d [%x]: transactions mismatch: have %v, want %v", num, hash, fblock.Transactions(), ablock.Transactions())
} else if types.CalcUncleHash(fblock.Uncles()) != types.CalcUncleHash(ablock.Uncles()) {
t.Errorf("block #%d [%x]: uncles mismatch: have %v, want %v", num, hash, fblock.Uncles(), ablock.Uncles())
}
if freceipts, areceipts := GetBlockReceipts(fastDb, hash, GetBlockNumber(fastDb, hash)), GetBlockReceipts(archiveDb, hash, GetBlockNumber(archiveDb, hash)); types.DeriveSha(freceipts, new(trie.Trie)) != types.DeriveSha(areceipts, new(trie.Trie)) {
if freceipts, areceipts := GetBlockReceipts(fastDb, hash, GetBlockNumber(fastDb, hash)), GetBlockReceipts(archiveDb, hash, GetBlockNumber(archiveDb, hash)); types.DeriveSha(freceipts, new(trie.StackTrie)) != types.DeriveSha(areceipts, new(trie.StackTrie)) {
t.Errorf("block #%d [%x]: receipts mismatch: have %v, want %v", num, hash, freceipts, areceipts)
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/database_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestBodyStorage(t *testing.T) {
}
if entry := GetBody(db, hash, 0); entry == nil {
t.Fatalf("Stored body not found")
} else if types.DeriveSha(types.Transactions(entry.Transactions), new(trie.Trie)) != types.DeriveSha(types.Transactions(body.Transactions), new(trie.Trie)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
} else if types.DeriveSha(types.Transactions(entry.Transactions), new(trie.StackTrie)) != types.DeriveSha(types.Transactions(body.Transactions), new(trie.StackTrie)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body)
}
if entry := GetBodyRLP(db, hash, 0); entry == nil {
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestBlockStorage(t *testing.T) {
}
if entry := GetBody(db, block.Hash(), block.NumberU64()); entry == nil {
t.Fatalf("Stored body not found")
} else if types.DeriveSha(types.Transactions(entry.Transactions), new(trie.Trie)) != types.DeriveSha(block.Transactions(), new(trie.Trie)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(block.Uncles()) {
} else if types.DeriveSha(types.Transactions(entry.Transactions), new(trie.StackTrie)) != types.DeriveSha(block.Transactions(), new(trie.StackTrie)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(block.Uncles()) {
t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, block.Body())
}
// Delete the block and verify the execution
Expand Down Expand Up @@ -297,7 +297,7 @@ func TestLookupStorage(t *testing.T) {
tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
txs := []*types.Transaction{tx1, tx2, tx3}

block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil, new(trie.Trie))
block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil, new(trie.StackTrie))

// Check that no transactions entries are in a pristine database
for i, tx := range txs {
Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
statedb.Commit(false)
statedb.Database().TrieDB().Commit(root, true)

return types.NewBlock(head, nil, nil, nil, new(trie.Trie))
return types.NewBlock(head, nil, nil, nil, new(trie.StackTrie))
}

// Commit writes the block and state of a genesis specification to the database.
Expand Down
2 changes: 1 addition & 1 deletion core/tx_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (bc *testBlockChain) Config() *params.ChainConfig {
func (bc *testBlockChain) CurrentBlock() *types.Block {
return types.NewBlock(&types.Header{
GasLimit: bc.gasLimit,
}, nil, nil, nil, new(trie.Trie))
}, nil, nil, nil, new(trie.StackTrie))
}

func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {
Expand Down
5 changes: 2 additions & 3 deletions core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
package types

import (
"bytes"
"hash"
"math/big"
"testing"

"bytes"
"reflect"
"testing"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/rlp"
Expand Down
4 changes: 2 additions & 2 deletions eth/downloader/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ func (q *queue) DeliverBodies(id string, txLists [][]*types.Transaction, uncleLi
defer q.lock.Unlock()

reconstruct := func(header *types.Header, index int, result *fetchResult) error {
if types.DeriveSha(types.Transactions(txLists[index]), new(trie.Trie)) != header.TxHash || types.CalcUncleHash(uncleLists[index]) != header.UncleHash {
if types.DeriveSha(types.Transactions(txLists[index]), new(trie.StackTrie)) != header.TxHash || types.CalcUncleHash(uncleLists[index]) != header.UncleHash {
return errInvalidBody
}
result.Transactions = txLists[index]
Expand All @@ -786,7 +786,7 @@ func (q *queue) DeliverReceipts(id string, receiptList [][]*types.Receipt) (int,
defer q.lock.Unlock()

reconstruct := func(header *types.Header, index int, result *fetchResult) error {
if types.DeriveSha(types.Receipts(receiptList[index]), new(trie.Trie)) != header.ReceiptHash {
if types.DeriveSha(types.Receipts(receiptList[index]), new(trie.StackTrie)) != header.ReceiptHash {
return errInvalidReceipt
}
result.Receipts = receiptList[index]
Expand Down
2 changes: 1 addition & 1 deletion eth/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func (f *Fetcher) loop() {

for hash, announce := range f.completing {
if f.queued[hash] == nil {
txnHash := types.DeriveSha(types.Transactions(task.transactions[i]), new(trie.Trie))
txnHash := types.DeriveSha(types.Transactions(task.transactions[i]), new(trie.StackTrie))
uncleHash := types.CalcUncleHash(task.uncles[i])

if txnHash == announce.header.TxHash && uncleHash == announce.header.UncleHash && announce.origin == task.peer {
Expand Down
2 changes: 1 addition & 1 deletion eth/fetcher/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
testAddress = crypto.PubkeyToAddress(testKey.PublicKey)
genesis = core.GenesisBlockForTesting(testdb, testAddress, big.NewInt(1000000000))
unknownBlock = types.NewBlock(&types.Header{GasLimit: params.GenesisGasLimit}, nil, nil, nil, new(trie.Trie))
unknownBlock = types.NewBlock(&types.Header{GasLimit: params.GenesisGasLimit}, nil, nil, nil, new(trie.StackTrie))
)

// makeChain creates a chain of n blocks starting at and including parent.
Expand Down
4 changes: 2 additions & 2 deletions les/odr_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (r *BlockRequest) Validate(db ethdb.Database, msg *Msg) error {
if header == nil {
return errHeaderUnavailable
}
if header.TxHash != types.DeriveSha(types.Transactions(body.Transactions), new(trie.Trie)) {
if header.TxHash != types.DeriveSha(types.Transactions(body.Transactions), new(trie.StackTrie)) {
return errTxHashMismatch
}
if header.UncleHash != types.CalcUncleHash(body.Uncles) {
Expand Down Expand Up @@ -170,7 +170,7 @@ func (r *ReceiptsRequest) Validate(db ethdb.Database, msg *Msg) error {
if header == nil {
return errHeaderUnavailable
}
if header.ReceiptHash != types.DeriveSha(receipt, new(trie.Trie)) {
if header.ReceiptHash != types.DeriveSha(receipt, new(trie.StackTrie)) {
return errReceiptHashMismatch
}
// Validations passed, store and return
Expand Down
2 changes: 1 addition & 1 deletion trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ func (db *Database) commit(hash common.Hash, batch ethdb.Batch, uncacher *cleane
if err != nil {
return err
}
fmt.Println("commit", node)

if err := batch.Put(hash[:], node.rlp()); err != nil {
return err
}
Expand Down

0 comments on commit 72ca227

Please sign in to comment.