Skip to content

Commit

Permalink
core, light, trie: polish code (ethereum#303)
Browse files Browse the repository at this point in the history
* core, light, trie: polish code

* core, trie: address comments

* trie/utils: add benchmark

* trie/utils: fix lint

* core/state: add description

* trie/utils: add function description

* use new InsertValuesAtStem function name

---------

Co-authored-by: Guillaume Ballet <[email protected]>
  • Loading branch information
rjl493456442 and gballet committed Nov 1, 2023
1 parent b6d7684 commit 8998d59
Show file tree
Hide file tree
Showing 9 changed files with 599 additions and 548 deletions.
58 changes: 23 additions & 35 deletions core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"

"github.com/crate-crypto/go-ipa/banderwagon"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/core/rawdb"
Expand All @@ -29,7 +30,6 @@ import (
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/trienode"
"github.com/ethereum/go-ethereum/trie/utils"
"github.com/gballet/go-verkle"
)

const (
Expand All @@ -38,6 +38,12 @@ const (

// Cache size granted for caching clean code.
codeCacheSize = 64 * 1024 * 1024

// commitmentSize is the size of commitment stored in cache.
commitmentSize = banderwagon.UncompressedSize

// Cache item granted for caching commitment results.
commitmentCacheItems = 64 * 1024 * 1024 / (commitmentSize + common.AddressLength)
)

// Database wraps access to tries and contract code.
Expand Down Expand Up @@ -72,11 +78,6 @@ type Trie interface {
// TODO(fjl): remove this when StateTrie is removed
GetKey([]byte) []byte

// GetStorage returns the value for key stored in the trie. The value bytes
// must not be modified by the caller. If a node was not found in the database,
// a trie.MissingNodeError is returned.
GetStorage(addr common.Address, key []byte) ([]byte, error)

// GetAccount abstracts an account read from the trie. It retrieves the
// account blob from the trie with provided account address and decodes it
// with associated decoding algorithm. If the specified account is not in
Expand All @@ -85,27 +86,32 @@ type Trie interface {
// be returned.
GetAccount(address common.Address) (*types.StateAccount, error)

// UpdateStorage associates key with value in the trie. If value has length zero,
// any existing value is deleted from the trie. The value bytes must not be modified
// by the caller while they are stored in the trie. If a node was not found in the
// database, a trie.MissingNodeError is returned.
UpdateStorage(addr common.Address, key, value []byte) error
// GetStorage returns the value for key stored in the trie. The value bytes
// must not be modified by the caller. If a node was not found in the database,
// a trie.MissingNodeError is returned.
GetStorage(addr common.Address, key []byte) ([]byte, error)

// UpdateAccount abstracts an account write to the trie. It encodes the
// provided account object with associated algorithm and then updates it
// in the trie with provided address.
UpdateAccount(address common.Address, account *types.StateAccount) error

// UpdateContractCode abstracts code write to the trie. It is expected
// to be moved to the stateWriter interface when the latter is ready.
UpdateContractCode(address common.Address, codeHash common.Hash, code []byte) error
// UpdateStorage associates key with value in the trie. If value has length zero,
// any existing value is deleted from the trie. The value bytes must not be modified
// by the caller while they are stored in the trie. If a node was not found in the
// database, a trie.MissingNodeError is returned.
UpdateStorage(addr common.Address, key, value []byte) error

// DeleteAccount abstracts an account deletion from the trie.
DeleteAccount(address common.Address) error

// DeleteStorage removes any existing value for key from the trie. If a node
// was not found in the database, a trie.MissingNodeError is returned.
DeleteStorage(addr common.Address, key []byte) error

// DeleteAccount abstracts an account deletion from the trie.
DeleteAccount(address common.Address) error
// UpdateContractCode abstracts code write to the trie. It is expected
// to be moved to the stateWriter interface when the latter is ready.
UpdateContractCode(address common.Address, codeHash common.Hash, code []byte) error

// Hash returns the root hash of the trie. It does not write to the database and
// can be used even if the trie doesn't have one.
Expand Down Expand Up @@ -173,25 +179,7 @@ type cachingDB struct {
// OpenTrie opens the main account trie at a specific root hash.
func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
if db.triedb.IsVerkle() {
reader, err := db.triedb.Reader(root)
if err != nil {
return nil, fmt.Errorf("failed to get node reader in OpenTrie: %w", err)
}

var verkleroot verkle.VerkleNode
if root != (common.Hash{}) && root != types.EmptyRootHash {
verklerootbytes, err := reader.Node(common.Hash{}, nil, common.Hash{})
if err != nil {
return nil, fmt.Errorf("failed to get serialized root node in OpenTrie: %w", err)
}
verkleroot, err = verkle.ParseNode(verklerootbytes, 0)
if err != nil {
return nil, fmt.Errorf("failed to deserialize root node in OpenTrie: %w", err)
}
} else {
verkleroot = verkle.New()
}
return trie.NewVerkleTrie(root, verkleroot, db.triedb, utils.NewPointCache(), true)
return trie.NewVerkleTrie(root, db.triedb, utils.NewPointCache(commitmentCacheItems))
}
tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion core/state/trie_prefetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ func (sf *subfetcher) loop() {
}
sf.trie = trie
} else {
// the trie argument can be nil as verkle doesn't support prefetching
// The trie argument can be nil as verkle doesn't support prefetching
// yet. TODO FIX IT(rjl493456442), otherwise code will panic here.
trie, err := sf.db.OpenStorageTrie(sf.state, sf.addr, sf.root, nil)
if err != nil {
log.Warn("Trie prefetcher failed opening trie", "root", sf.root, "err", err)
Expand Down
5 changes: 4 additions & 1 deletion core/types/hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

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

// EmptyUncleHash is the known hash of the empty uncle set.
Expand All @@ -40,6 +40,9 @@ var (

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

// EmptyVerkleHash is the known hash of an empty verkle trie.
EmptyVerkleHash = common.Hash{}
)

// TrieRootHash returns the hash itself if it's non-empty or the predefined
Expand Down
2 changes: 1 addition & 1 deletion light/odr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error {
t state.Trie
)
if len(req.Id.AccountAddress) > 0 {
t, err = odr.serverState.OpenStorageTrie(req.Id.StateRoot, common.BytesToAddress(req.Id.AccountAddress), req.Id.Root)
t, err = odr.serverState.OpenStorageTrie(req.Id.StateRoot, common.BytesToAddress(req.Id.AccountAddress), req.Id.Root, nil)
} else {
t, err = odr.serverState.OpenTrie(req.Id.Root)
}
Expand Down
Loading

0 comments on commit 8998d59

Please sign in to comment.