Skip to content

Commit

Permalink
Merge pull request #9 from c98tristan:feat/update-trie-to-stacktrie
Browse files Browse the repository at this point in the history
  • Loading branch information
trinhdn97 committed Aug 1, 2023
1 parent f47664c commit 475b70a
Show file tree
Hide file tree
Showing 28 changed files with 269 additions and 491 deletions.
3 changes: 2 additions & 1 deletion consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/tomochain/tomochain/params"
"github.com/tomochain/tomochain/rlp"
"github.com/tomochain/tomochain/rpc"
"github.com/tomochain/tomochain/trie"
)

const (
Expand Down Expand Up @@ -575,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), 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
3 changes: 2 additions & 1 deletion consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/tomochain/tomochain/core/state"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/params"
"github.com/tomochain/tomochain/trie"
)

// Ethash proof-of-work protocol constants.
Expand Down Expand Up @@ -519,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), nil
return types.NewBlock(header, txs, uncles, receipts, new(trie.StackTrie)), nil
}

// Some weird constants to avoid constant memory allocs for them.
Expand Down
3 changes: 2 additions & 1 deletion consensus/posv/posv.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/tomochain/tomochain/rpc"
"github.com/tomochain/tomochain/tomox/tradingstate"
"github.com/tomochain/tomochain/tomoxlending/lendingstate"
"github.com/tomochain/tomochain/trie"
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
)

Expand Down Expand Up @@ -985,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), 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
3 changes: 2 additions & 1 deletion core/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import (
"os"
"testing"

"github.com/tomochain/tomochain/core/rawdb"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/common/math"
"github.com/tomochain/tomochain/consensus/ethash"
"github.com/tomochain/tomochain/core/rawdb"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/core/vm"
"github.com/tomochain/tomochain/crypto"
Expand Down
6 changes: 4 additions & 2 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package core

import (
"fmt"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/consensus"
"github.com/tomochain/tomochain/consensus/posv"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/tomochain/tomochain/params"
"github.com/tomochain/tomochain/tomox/tradingstate"
"github.com/tomochain/tomochain/tomoxlending/lendingstate"
"github.com/tomochain/tomochain/trie"
)

// BlockValidator is responsible for validating block headers, uncles and
Expand Down Expand Up @@ -71,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()); 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 @@ -93,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)
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
5 changes: 3 additions & 2 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/tomochain/tomochain/core/vm"
"github.com/tomochain/tomochain/crypto"
"github.com/tomochain/tomochain/params"
"github.com/tomochain/tomochain/trie"
)

// Test fork of length N starting from block i
Expand Down Expand Up @@ -617,12 +618,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()) != types.DeriveSha(ablock.Transactions()) {
} 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 := rawdb.GetBlockReceipts(fastDb, hash, rawdb.GetBlockNumber(fastDb, hash), fast.Config()), rawdb.GetBlockReceipts(archiveDb, hash, rawdb.GetBlockNumber(archiveDb, hash), fast.Config()); types.DeriveSha(freceipts) != types.DeriveSha(areceipts) {
if freceipts, areceipts := rawdb.GetBlockReceipts(fastDb, hash, rawdb.GetBlockNumber(fastDb, hash), fast.Config()), rawdb.GetBlockReceipts(archiveDb, hash, rawdb.GetBlockNumber(archiveDb, hash), fast.Config()); types.DeriveSha(freceipts, trie.NewStackTrie(nil)) != types.DeriveSha(areceipts, trie.NewStackTrie(nil)) {
t.Errorf("block #%d [%x]: receipts mismatch: have %v, want %v", num, hash, freceipts, areceipts)
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/tomochain/tomochain/log"
"github.com/tomochain/tomochain/params"
"github.com/tomochain/tomochain/rlp"
"github.com/tomochain/tomochain/trie"
)

//go:generate gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go
Expand Down Expand Up @@ -258,7 +259,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)
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
Loading

0 comments on commit 475b70a

Please sign in to comment.