@@ -20,6 +20,7 @@ import (
2020 "errors"
2121 "fmt"
2222
23+ "github.com/crate-crypto/go-ipa/banderwagon"
2324 "github.com/ethereum/go-ethereum/common"
2425 "github.com/ethereum/go-ethereum/common/lru"
2526 "github.com/ethereum/go-ethereum/core/rawdb"
@@ -28,6 +29,7 @@ import (
2829 "github.com/ethereum/go-ethereum/ethdb"
2930 "github.com/ethereum/go-ethereum/trie"
3031 "github.com/ethereum/go-ethereum/trie/trienode"
32+ "github.com/ethereum/go-ethereum/trie/utils"
3133)
3234
3335const (
@@ -36,6 +38,12 @@ const (
3638
3739 // Cache size granted for caching clean code.
3840 codeCacheSize = 64 * 1024 * 1024
41+
42+ // commitmentSize is the size of commitment stored in cache.
43+ commitmentSize = banderwagon .UncompressedSize
44+
45+ // Cache item granted for caching commitment results.
46+ commitmentCacheItems = 64 * 1024 * 1024 / (commitmentSize + common .AddressLength )
3947)
4048
4149// Database wraps access to tries and contract code.
@@ -44,7 +52,7 @@ type Database interface {
4452 OpenTrie (root common.Hash ) (Trie , error )
4553
4654 // OpenStorageTrie opens the storage trie of an account.
47- OpenStorageTrie (stateRoot common.Hash , address common.Address , root common.Hash ) (Trie , error )
55+ OpenStorageTrie (stateRoot common.Hash , address common.Address , root common.Hash , trie Trie ) (Trie , error )
4856
4957 // CopyTrie returns an independent copy of the given trie.
5058 CopyTrie (Trie ) Trie
@@ -70,11 +78,6 @@ type Trie interface {
7078 // TODO(fjl): remove this when StateTrie is removed
7179 GetKey ([]byte ) []byte
7280
73- // GetStorage returns the value for key stored in the trie. The value bytes
74- // must not be modified by the caller. If a node was not found in the database,
75- // a trie.MissingNodeError is returned.
76- GetStorage (addr common.Address , key []byte ) ([]byte , error )
77-
7881 // GetAccount abstracts an account read from the trie. It retrieves the
7982 // account blob from the trie with provided account address and decodes it
8083 // with associated decoding algorithm. If the specified account is not in
@@ -83,27 +86,32 @@ type Trie interface {
8386 // be returned.
8487 GetAccount (address common.Address ) (* types.StateAccount , error )
8588
86- // UpdateStorage associates key with value in the trie. If value has length zero,
87- // any existing value is deleted from the trie. The value bytes must not be modified
88- // by the caller while they are stored in the trie. If a node was not found in the
89- // database, a trie.MissingNodeError is returned.
90- UpdateStorage (addr common.Address , key , value []byte ) error
89+ // GetStorage returns the value for key stored in the trie. The value bytes
90+ // must not be modified by the caller. If a node was not found in the database,
91+ // a trie.MissingNodeError is returned.
92+ GetStorage (addr common.Address , key []byte ) ([]byte , error )
9193
9294 // UpdateAccount abstracts an account write to the trie. It encodes the
9395 // provided account object with associated algorithm and then updates it
9496 // in the trie with provided address.
9597 UpdateAccount (address common.Address , account * types.StateAccount ) error
9698
97- // UpdateContractCode abstracts code write to the trie. It is expected
98- // to be moved to the stateWriter interface when the latter is ready.
99- UpdateContractCode (address common.Address , codeHash common.Hash , code []byte ) error
99+ // UpdateStorage associates key with value in the trie. If value has length zero,
100+ // any existing value is deleted from the trie. The value bytes must not be modified
101+ // by the caller while they are stored in the trie. If a node was not found in the
102+ // database, a trie.MissingNodeError is returned.
103+ UpdateStorage (addr common.Address , key , value []byte ) error
104+
105+ // DeleteAccount abstracts an account deletion from the trie.
106+ DeleteAccount (address common.Address ) error
100107
101108 // DeleteStorage removes any existing value for key from the trie. If a node
102109 // was not found in the database, a trie.MissingNodeError is returned.
103110 DeleteStorage (addr common.Address , key []byte ) error
104111
105- // DeleteAccount abstracts an account deletion from the trie.
106- DeleteAccount (address common.Address ) error
112+ // UpdateContractCode abstracts code write to the trie. It is expected
113+ // to be moved to the stateWriter interface when the latter is ready.
114+ UpdateContractCode (address common.Address , codeHash common.Hash , code []byte ) error
107115
108116 // Hash returns the root hash of the trie. It does not write to the database and
109117 // can be used even if the trie doesn't have one.
@@ -170,6 +178,9 @@ type cachingDB struct {
170178
171179// OpenTrie opens the main account trie at a specific root hash.
172180func (db * cachingDB ) OpenTrie (root common.Hash ) (Trie , error ) {
181+ if db .triedb .IsVerkle () {
182+ return trie .NewVerkleTrie (root , db .triedb , utils .NewPointCache (commitmentCacheItems ))
183+ }
173184 tr , err := trie .NewStateTrie (trie .StateTrieID (root ), db .triedb )
174185 if err != nil {
175186 return nil , err
@@ -178,7 +189,13 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
178189}
179190
180191// OpenStorageTrie opens the storage trie of an account.
181- func (db * cachingDB ) OpenStorageTrie (stateRoot common.Hash , address common.Address , root common.Hash ) (Trie , error ) {
192+ func (db * cachingDB ) OpenStorageTrie (stateRoot common.Hash , address common.Address , root common.Hash , self Trie ) (Trie , error ) {
193+ // In the verkle case, there is only one tree. But the two-tree structure
194+ // is hardcoded in the codebase. So we need to return the same trie in this
195+ // case.
196+ if db .triedb .IsVerkle () {
197+ return self , nil
198+ }
182199 tr , err := trie .NewStateTrie (trie .StorageTrieID (stateRoot , crypto .Keccak256Hash (address .Bytes ()), root ), db .triedb )
183200 if err != nil {
184201 return nil , err
0 commit comments