Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core, light, trie: polish code
Browse files Browse the repository at this point in the history
rjl493456442 committed Oct 30, 2023
1 parent 412075b commit 037a03d
Showing 9 changed files with 638 additions and 704 deletions.
59 changes: 23 additions & 36 deletions core/state/database.go
Original file line number Diff line number Diff line change
@@ -28,8 +28,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"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"
"github.com/ethereum/go-ethereum/trie/verkle"
)

const (
@@ -38,6 +37,12 @@ const (

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

// commitmentSize is the size of commitment stored in cache.
commitmentSize = 3 * 32

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

// Database wraps access to tries and contract code.
@@ -72,11 +77,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
@@ -85,27 +85,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.
@@ -173,25 +178,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, verkle.NewCache(commitmentCacheItems))
}
tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb)
if err != nil {
5 changes: 4 additions & 1 deletion core/types/hashes.go
Original file line number Diff line number Diff line change
@@ -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.
@@ -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
2 changes: 1 addition & 1 deletion light/odr_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading

0 comments on commit 037a03d

Please sign in to comment.