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] Refactor trie interfaces #378

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
8253211
implements RLP encoding/decoding processing for structs
trinhdn97 Jun 16, 2023
fa67be8
Update RLP lib
trinhdn97 Jun 16, 2023
9b5dadb
Implement RLP encoder code generation tool
trinhdn97 Jun 16, 2023
ddf35f8
Update unit tests and benchmark
trinhdn97 Jun 16, 2023
17e4876
Include RLPgen tool to CI
trinhdn97 Jun 20, 2023
aa7c4b4
Add benchmarks for types RLP encoding/decoding
trinhdn97 Jun 21, 2023
ac89cd3
Generate RLP encoder for some structs
trinhdn97 Jun 21, 2023
060f4ce
Convert status of receipts from uint to uint64
trinhdn97 Jun 21, 2023
8fdaf68
Unify multiple keccak interface into one inside crypto package
trinhdn97 Jul 16, 2023
c7be6cc
Merge branch 'refactor/keccak-state' into ft/stacktrie
trinhdn97 Jul 16, 2023
340bf2b
Add encode method to Node interface
trinhdn97 Jul 16, 2023
33149c9
Implement stacktrie
trinhdn97 Jul 16, 2023
2e9abc4
Add encode method for rawShortNode and rawFullNode
trinhdn97 Jul 16, 2023
008274f
Fix import cycle
trinhdn97 Jul 16, 2023
f88dba4
Fix stacktrie unit tests
trinhdn97 Jul 16, 2023
327c90d
Remove rlpLog RLP encoder
trinhdn97 Jul 17, 2023
4d22f8d
Fix unit tests
trinhdn97 Jul 18, 2023
c37affb
Move statedb.Account struct to types.StateAccount
trinhdn97 Jul 20, 2023
e0776fb
Move database_util.go to rawdb package
trinhdn97 Jul 20, 2023
5ce901b
Move db keys to schema.go
trinhdn97 Jul 20, 2023
36373e7
Refine db keys
trinhdn97 Jul 20, 2023
6392afd
Rename
trinhdn97 Jul 20, 2023
cf62b98
Split into sub accessor files
trinhdn97 Jul 20, 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
trinhdn97 Jul 19, 2023
44d2c54
Chore: Add intPool in interpreter.go
c98tristan Jul 24, 2023
5fae822
Merge branch 'refactor/state-account' into refactor/rawdb
trinhdn97 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
a6098a1
Merge remote-tracking branch 'c98tristan/feat/update-trie-to-stacktri…
trinhdn97 Jul 25, 2023
b1cb0fc
Resolve conflicts after merged
trinhdn97 Jul 25, 2023
5e8bde3
Refactor some rawdb methods
trinhdn97 Jul 31, 2023
cb81493
[WIP] Implement new trie interface and separate preimageStore
trinhdn97 Jul 31, 2023
a409636
Refactor NewDatabaseWithConfig
trinhdn97 Jul 31, 2023
4e70b3e
Include configs when init trie databases
trinhdn97 Jul 31, 2023
1b9882d
Nitpick
trinhdn97 Jul 31, 2023
943f500
Fix UpdateStorage in trie
trinhdn97 Aug 3, 2023
be70268
Fix trie unit tests
trinhdn97 Aug 3, 2023
bf41851
Fix GetCommittedState and GetState
trinhdn97 Aug 3, 2023
12d08d0
Fix state test
trinhdn97 Aug 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions accounts/keystore/keystore_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (w *keystoreWallet) SignHash(account accounts.Account, hash []byte) ([]byte
if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
return nil, accounts.ErrUnknownAccount
}
// Account seems valid, request the keystore to sign
// StateAccount seems valid, request the keystore to sign
return w.keystore.SignHash(account, hash)
}

Expand All @@ -106,7 +106,7 @@ func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction,
if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
return nil, accounts.ErrUnknownAccount
}
// Account seems valid, request the keystore to sign
// StateAccount seems valid, request the keystore to sign
return w.keystore.SignTx(account, tx, chainID)
}

Expand All @@ -120,7 +120,7 @@ func (w *keystoreWallet) SignHashWithPassphrase(account accounts.Account, passph
if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
return nil, accounts.ErrUnknownAccount
}
// Account seems valid, request the keystore to sign
// StateAccount seems valid, request the keystore to sign
return w.keystore.SignHashWithPassphrase(account, passphrase, hash)
}

Expand All @@ -134,6 +134,6 @@ func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphra
if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
return nil, accounts.ErrUnknownAccount
}
// Account seems valid, request the keystore to sign
// StateAccount seems valid, request the keystore to sign
return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
}
2 changes: 1 addition & 1 deletion accounts/usbwallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (w *wallet) selfDerive() {
// Termination requested
continue
case reqc = <-w.deriveReq:
// Account discovery requested
// StateAccount discovery requested
}
// Derivation needs a chain and device access, skip if either unavailable
w.stateLock.RLock()
Expand Down
2 changes: 1 addition & 1 deletion cmd/faucet/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ type faucet struct {
index []byte // Index page to serve up on the web

keystore *keystore.KeyStore // Keystore containing the single signer
account accounts.Account // Account funding user faucet requests
account accounts.Account // StateAccount funding user faucet requests
nonce uint64 // Current pending nonce of the faucet
price *big.Int // Current gas price to issue funds with

Expand Down
4 changes: 2 additions & 2 deletions cmd/gc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/tomochain/tomochain/cmd/utils"
"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/core/rawdb"
"github.com/tomochain/tomochain/core/state"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/eth"
"github.com/tomochain/tomochain/ethdb"
"github.com/tomochain/tomochain/ethdb/leveldb"
Expand Down Expand Up @@ -81,7 +81,7 @@ func main() {
if running {
for _, address := range cleanAddress {
enc := trieRoot.trie.Get(address.Bytes())
var data state.Account
var data types.StateAccount
rlp.DecodeBytes(enc, &data)
fmt.Println(time.Now().Format(time.RFC3339), "Start clean state address ", address.Hex(), " at block ", trieRoot.number)
signerRoot, err := resolveHash(data.Root[:], db)
Expand Down
2 changes: 1 addition & 1 deletion consensus/clique/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
type Vote struct {
Signer common.Address `json:"signer"` // Authorized signer that cast this vote
Block uint64 `json:"block"` // Block number the vote was cast in (expire old votes)
Address common.Address `json:"address"` // Account being voted on to change its authorization
Address common.Address `json:"address"` // StateAccount being voted on to change its authorization
Authorize bool `json:"authorize"` // Whether to authorize or deauthorize the voted account
}

Expand Down
8 changes: 4 additions & 4 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,9 @@ 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"
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
)

const (
Expand Down Expand Up @@ -181,7 +181,7 @@ var (

// SignerFn is a signer callback function to request a hash to be signed by a
// backing account.
//type SignerFn func(accounts.Account, []byte) ([]byte, error)
//type SignerFn func(accounts.StateAccount, []byte) ([]byte, error)

// sigHash returns the hash which is used as input for the proof-of-stake-voting
// signing. It is the hash of the entire header apart from the 65 byte signature
Expand Down
2 changes: 1 addition & 1 deletion consensus/posv/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
//type Vote struct {
// Signer common.Address `json:"signer"` // Authorized signer that cast this vote
// Block uint64 `json:"block"` // Block number the vote was cast in (expire old votes)
// Address common.Address `json:"address"` // Account being voted on to change its authorization
// Address common.Address `json:"address"` // StateAccount being voted on to change its authorization
// Authorize bool `json:"authorize"` // Whether to authorize or deauthorize the voted account
//}

Expand Down
3 changes: 2 additions & 1 deletion core/state/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/rlp"
"github.com/tomochain/tomochain/trie"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ func (self *StateDB) RawDump() Dump {
it := trie.NewIterator(self.trie.NodeIterator(nil))
for it.Next() {
addr := self.trie.GetKey(it.Key)
var data Account
var data types.StateAccount
if err := rlp.DecodeBytes(it.Value, &data); err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion core/state/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/rlp"
"github.com/tomochain/tomochain/trie"
)
Expand Down Expand Up @@ -104,7 +105,7 @@ func (it *NodeIterator) step() error {
return nil
}
// Otherwise we've reached an account node, initiate data iteration
var account Account
var account types.StateAccount
if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil {
return err
}
Expand Down
18 changes: 5 additions & 13 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math/big"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/crypto"
"github.com/tomochain/tomochain/rlp"
)
Expand Down Expand Up @@ -58,12 +59,12 @@ func (self Storage) Copy() Storage {
//
// The usage pattern is as follows:
// First you need to obtain a state object.
// Account values can be accessed and modified through the object.
// StateAccount values can be accessed and modified through the object.
// Finally, call CommitTrie to write the modified storage trie into a database.
type stateObject struct {
address common.Address
addrHash common.Hash // hash of ethereum address of the account
data Account
data types.StateAccount
db *StateDB

// DB error.
Expand Down Expand Up @@ -95,17 +96,8 @@ func (s *stateObject) empty() bool {
return s.data.Nonce == 0 && s.data.Balance.Sign() == 0 && bytes.Equal(s.data.CodeHash, emptyCodeHash)
}

// Account is the Ethereum consensus representation of accounts.
// These objects are stored in the main account trie.
type Account struct {
Nonce uint64
Balance *big.Int
Root common.Hash // merkle root of the storage trie
CodeHash []byte
}

// newObject creates a state object.
func newObject(db *StateDB, address common.Address, data Account, onDirty func(addr common.Address)) *stateObject {
func newObject(db *StateDB, address common.Address, data types.StateAccount, onDirty func(addr common.Address)) *stateObject {
if data.Balance == nil {
data.Balance = new(big.Int)
}
Expand Down Expand Up @@ -397,7 +389,7 @@ func (self *stateObject) Nonce() uint64 {
}

// Never called, but must be present to allow stateObject to be used
// as a vm.Account interface that also satisfies the vm.ContractRef
// as a vm.StateAccount interface that also satisfies the vm.ContractRef
// interface. Interfaces are awesome.
func (self *stateObject) Value() *big.Int {
panic("Value on stateObject should never be called")
Expand Down
10 changes: 5 additions & 5 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObje
self.setError(err)
return nil
}
var data Account
var data types.StateAccount
if err := rlp.DecodeBytes(enc, &data); err != nil {
log.Error("Failed to decode state object", "addr", addr, "err", err)
return nil
Expand Down Expand Up @@ -432,7 +432,7 @@ func (self *StateDB) MarkStateObjectDirty(addr common.Address) {
// the given address, it is overwritten and returned as the second return value.
func (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) {
prev = self.getStateObject(addr)
newobj = newObject(self, addr, Account{}, self.MarkStateObjectDirty)
newobj = newObject(self, addr, types.StateAccount{}, self.MarkStateObjectDirty)
newobj.setNonce(0) // sets the object to dirty
if prev == nil {
self.journal = append(self.journal, createObjectChange{account: &addr})
Expand All @@ -449,8 +449,8 @@ func (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObjec
// CreateAccount is called during the EVM CREATE operation. The situation might arise that
// a contract does the following:
//
// 1. sends funds to sha(account ++ (nonce + 1))
// 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
// 1. sends funds to sha(account ++ (nonce + 1))
// 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
//
// Carrying over the balance ensures that Ether doesn't disappear.
func (self *StateDB) CreateAccount(addr common.Address) {
Expand Down Expand Up @@ -636,7 +636,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error)
}
// Write trie changes.
root, err = s.trie.Commit(func(leaf []byte, parent common.Hash) error {
var account Account
var account types.StateAccount
if err := rlp.DecodeBytes(leaf, &account); err != nil {
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion core/state/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/ethdb"
"github.com/tomochain/tomochain/rlp"
"github.com/tomochain/tomochain/trie"
Expand All @@ -29,7 +30,7 @@ import (
func NewStateSync(root common.Hash, database ethdb.KeyValueReader, bloom *trie.SyncBloom) *trie.Sync {
var syncer *trie.Sync
callback := func(leaf []byte, parent common.Hash) error {
var obj Account
var obj types.StateAccount
if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil {
return err
}
Expand Down
5 changes: 0 additions & 5 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ import (
"github.com/tomochain/tomochain/rlp"
)

var (
EmptyRootHash = DeriveSha(Transactions{})
EmptyUncleHash = CalcUncleHash(nil)
)

// A BlockNonce is a 64-bit hash which proves (combined with the
// mix-hash) that a sufficient amount of computation has been carried
// out on a block.
Expand Down
39 changes: 39 additions & 0 deletions core/types/hashes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// 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/>.

package types

import (
"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/crypto"
)

var (
// EmptyRootHash is the known root hash of an empty trie.
EmptyRootHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")

// EmptyUncleHash is the known hash of the empty uncle set.
EmptyUncleHash = rlpHash([]*Header(nil)) // 1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347

// EmptyCodeHash is the known hash of the empty EVM bytecode.
EmptyCodeHash = crypto.Keccak256Hash(nil) // c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470

// EmptyTxsHash is the known hash of the empty transaction set.
EmptyTxsHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")

// EmptyReceiptsHash is the known hash of the empty receipt set.
EmptyReceiptsHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
)
Loading