Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Non-breaking changes] Chore: Update derive SHA for stacktrie #375

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8253211
implements RLP encoding/decoding processing for structs
trinhdn2 Jun 16, 2023
fa67be8
Update RLP lib
trinhdn2 Jun 16, 2023
9b5dadb
Implement RLP encoder code generation tool
trinhdn2 Jun 16, 2023
ddf35f8
Update unit tests and benchmark
trinhdn2 Jun 16, 2023
17e4876
Include RLPgen tool to CI
trinhdn2 Jun 20, 2023
aa7c4b4
Add benchmarks for types RLP encoding/decoding
trinhdn2 Jun 21, 2023
ac89cd3
Generate RLP encoder for some structs
trinhdn2 Jun 21, 2023
060f4ce
Convert status of receipts from uint to uint64
trinhdn2 Jun 21, 2023
8fdaf68
Unify multiple keccak interface into one inside crypto package
trinhdn2 Jul 16, 2023
c7be6cc
Merge branch 'refactor/keccak-state' into ft/stacktrie
trinhdn2 Jul 16, 2023
340bf2b
Add encode method to Node interface
trinhdn2 Jul 16, 2023
33149c9
Implement stacktrie
trinhdn2 Jul 16, 2023
2e9abc4
Add encode method for rawShortNode and rawFullNode
trinhdn2 Jul 16, 2023
008274f
Fix import cycle
trinhdn2 Jul 16, 2023
f88dba4
Fix stacktrie unit tests
trinhdn2 Jul 16, 2023
327c90d
Remove rlpLog RLP encoder
trinhdn2 Jul 17, 2023
4d22f8d
Fix unit tests
trinhdn2 Jul 18, 2023
024181b
chore: remove intPool
c98tristan Jul 17, 2023
c384646
Feat: Update derive SHA for stacktrie
c98tristan Jul 19, 2023
b8421e5
Chore: Add benchmark for Stacktrie
c98tristan Jul 19, 2023
6099609
Chore: Fix Update function missing return type
c98tristan Jul 19, 2023
ae6ca82
Chore: Change GetRlp to EncodeIndex in LendingTransaction and OrderTr…
c98tristan Jul 24, 2023
515a614
Update DeriveSha with new Hasher and DerivableList interface
trinhdn2 Jul 19, 2023
44d2c54
Chore: Add intPool in interpreter.go
c98tristan Jul 24, 2023
72ca227
Chore: Change parameter of NewBlock function from Trie to Stacktrie
c98tristan Jul 25, 2023
530cbb1
Chore: Sorting imported library
c98tristan Jul 25, 2023
260f47e
Chore: Sorting imported library
c98tristan Jul 25, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"context"
"errors"
"fmt"
"github.com/tomochain/tomochain/consensus"
"github.com/tomochain/tomochain/core/rawdb"
"math/big"
"sync"
"time"
Expand All @@ -30,9 +28,11 @@ import (
"github.com/tomochain/tomochain/accounts/abi/bind"
"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/common/math"
"github.com/tomochain/tomochain/consensus"
"github.com/tomochain/tomochain/consensus/ethash"
"github.com/tomochain/tomochain/core"
"github.com/tomochain/tomochain/core/bloombits"
"github.com/tomochain/tomochain/core/rawdb"
"github.com/tomochain/tomochain/core/state"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/core/vm"
Expand Down Expand Up @@ -202,7 +202,7 @@ func (b *SimulatedBackend) CallContract(ctx context.Context, call tomochain.Call
return rval, err
}

//FIXME: please use copyState for this function
// FIXME: please use copyState for this function
// CallContractWithState executes a contract call at the given state.
func (b *SimulatedBackend) CallContractWithState(call tomochain.CallMsg, chain consensus.ChainContext, statedb *state.StateDB) ([]byte, error) {
// Ensure message is initialized properly.
Expand Down Expand Up @@ -285,7 +285,6 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call tomochain.CallM

snapshot := b.pendingState.Snapshot()
_, _, failed, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
fmt.Println("EstimateGas",err,failed)
b.pendingState.RevertToSnapshot(snapshot)

if err != nil || failed {
Expand Down
13 changes: 7 additions & 6 deletions build/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

//go:build none
// +build none

/*
Expand All @@ -23,14 +24,13 @@ Usage: go run build/ci.go <command> <command flags/arguments>

Available commands are:

install [ -arch architecture ] [ -cc compiler ] [ packages... ] -- builds packages and executables
test [ -coverage ] [ packages... ] -- runs the tests
lint -- runs certain pre-selected linters
importkeys -- imports signing keys from env
xgo [ -alltools ] [ options ] -- cross builds according to options
install [ -arch architecture ] [ -cc compiler ] [ packages... ] -- builds packages and executables
test [ -coverage ] [ packages... ] -- runs the tests
lint -- runs certain pre-selected linters
importkeys -- imports signing keys from env
xgo [ -alltools ] [ options ] -- cross builds according to options

For all commands, -n prevents execution of external programs (dry run mode).

*/
package main

Expand Down Expand Up @@ -62,6 +62,7 @@ var (
executablePath("rlpdump"),
executablePath("swarm"),
executablePath("wnode"),
executablePath("rlp/rlpgen"),
}
)

Expand Down
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
11 changes: 6 additions & 5 deletions consensus/posv/posv.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/tomochain/tomochain/tomox/tradingstate"
"github.com/tomochain/tomochain/tomoxlending/lendingstate"
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
"io/ioutil"
"math/big"
"math/rand"
Expand All @@ -50,6 +47,10 @@ import (
"github.com/tomochain/tomochain/params"
"github.com/tomochain/tomochain/rlp"
"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"
)

const (
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 Expand Up @@ -1146,7 +1147,7 @@ func (c *Posv) CacheData(header *types.Header, txs []*types.Transaction, receipt
signTxs := []*types.Transaction{}
for _, tx := range txs {
if tx.IsSigningTransaction() {
var b uint
var b uint64
for _, r := range receipts {
if r.TxHash == tx.Hash() {
if len(r.PostState) > 0 {
Expand Down
13 changes: 11 additions & 2 deletions core/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package core

import (
"crypto/ecdsa"
"github.com/tomochain/tomochain/core/rawdb"
"io/ioutil"
"math/big"
"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"
Expand Down Expand Up @@ -238,10 +239,16 @@ func makeChainForBench(db ethdb.Database, full bool, count uint64) {
WriteHeader(db, header)
WriteCanonicalHash(db, hash, n)
WriteTd(db, hash, n, big.NewInt(int64(n+1)))
if n == 0 {
WriteChainConfig(db, hash, params.AllEthashProtocolChanges)
}
WriteHeadHeaderHash(db, hash)

if full || n == 0 {
block := types.NewBlockWithHeader(header)
WriteBody(db, hash, n, block.Body())
WriteBlockReceipts(db, hash, n, nil)
WriteHeadBlockHash(db, hash)
}
}
}
Expand Down Expand Up @@ -275,6 +282,8 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
}
makeChainForBench(db, full, count)
db.Close()
cacheConfig := defaultCacheConfig
cacheConfig.Disabled = true

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -284,7 +293,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
if err != nil {
b.Fatalf("error opening database at %v: %v", dir, err)
}
chain, err := NewBlockChain(db, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{})
chain, err := NewBlockChain(db, cacheConfig, params.TestChainConfig, ethash.NewFaker(), vm.Config{})
if err != nil {
b.Fatalf("error creating chain: %v", err)
}
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
8 changes: 8 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ type CacheConfig struct {
TrieNodeLimit int // Memory limit (MB) at which to flush the current in-memory trie to disk
TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk
}

// defaultCacheConfig are the default caching values if none are specified by the
// user (also used during testing).
var defaultCacheConfig = &CacheConfig{
TrieNodeLimit: 256,
TrieTimeLimit: 5 * time.Minute,
}

type ResultProcessBlock struct {
logs []*types.Log
receipts []*types.Receipt
Expand Down
7 changes: 4 additions & 3 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package core

import (
"fmt"
"github.com/tomochain/tomochain/core/rawdb"
"math/big"
"math/rand"
"sync"
Expand All @@ -27,11 +26,13 @@ import (

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/consensus/ethash"
"github.com/tomochain/tomochain/core/rawdb"
"github.com/tomochain/tomochain/core/state"
"github.com/tomochain/tomochain/core/types"
"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 := GetBlockReceipts(fastDb, hash, GetBlockNumber(fastDb, hash)), GetBlockReceipts(archiveDb, hash, GetBlockNumber(archiveDb, hash)); types.DeriveSha(freceipts) != types.DeriveSha(areceipts) {
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
2 changes: 1 addition & 1 deletion core/database_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/tomochain/tomochain/core/rawdb"
"math/big"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/core/rawdb"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/ethdb"
"github.com/tomochain/tomochain/log"
Expand Down
9 changes: 5 additions & 4 deletions core/database_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ package core

import (
"bytes"
"github.com/tomochain/tomochain/core/rawdb"
"math/big"
"testing"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/core/rawdb"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/crypto/sha3"
"github.com/tomochain/tomochain/rlp"
"github.com/tomochain/tomochain/trie"
)

// Tests block header storage and retrieval operations.
Expand Down Expand Up @@ -83,7 +84,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)) != types.DeriveSha(types.Transactions(body.Transactions)) || 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 @@ -139,7 +140,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)) != types.DeriveSha(block.Transactions()) || 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 @@ -295,7 +296,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)
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
13 changes: 7 additions & 6 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/tomochain/tomochain/core/rawdb"
"math/big"
"strings"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/common/hexutil"
"github.com/tomochain/tomochain/common/math"
"github.com/tomochain/tomochain/core/rawdb"
"github.com/tomochain/tomochain/core/state"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/ethdb"
"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 @@ -140,10 +141,10 @@ func (e *GenesisMismatchError) Error() string {
// SetupGenesisBlock writes or updates the genesis block in db.
// The block that will be used is:
//
// genesis == nil genesis != nil
// +------------------------------------------
// db has no genesis | main-net default | genesis
// db has genesis | from DB | genesis (if compatible)
// genesis == nil genesis != nil
// +------------------------------------------
// db has no genesis | main-net default | genesis
// db has genesis | from DB | genesis (if compatible)
//
// The stored chain configuration will be updated if it is compatible (i.e. does not
// specify a fork block below the local head block). In case of a conflict, the
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