@@ -24,6 +24,8 @@ import (
2424// 2. The `Hash` method actually creates the proposal, since Firewood cannot calculate
2525// the hash of the trie without committing it. It is immediately dropped, and this
2626// can likely be optimized.
27+ //
28+ // Note this is not concurrent safe.
2729type AccountTrie struct {
2830 fw * Database
2931 parentRoot common.Hash
@@ -49,7 +51,10 @@ func NewAccountTrie(root common.Hash, db *Database) (*AccountTrie, error) {
4951 }, nil
5052}
5153
52- // GetAccount implements state.Trie.
54+ // GetAccount returns the state account associated with an address.
55+ // - If the account has been updated, the new value is returned.
56+ // - If the account has been deleted, (nil, nil) is returned.
57+ // - If the account does not exist, (nil, nil) is returned.
5358func (a * AccountTrie ) GetAccount (addr common.Address ) (* types.StateAccount , error ) {
5459 key := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
5560
@@ -82,7 +87,10 @@ func (a *AccountTrie) GetAccount(addr common.Address) (*types.StateAccount, erro
8287 return account , err
8388}
8489
85- // GetStorage implements state.Trie.
90+ // GetStorage returns the value associated with a storage key for a given account address.
91+ // - If the storage slot has been updated, the new value is returned.
92+ // - If the storage slot has been deleted, (nil, nil) is returned.
93+ // - If the storage slot does not exist, (nil, nil) is returned.
8694func (a * AccountTrie ) GetStorage (addr common.Address , key []byte ) ([]byte , error ) {
8795 // If the account has been deleted, we should return nil
8896 accountKey := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
@@ -117,7 +125,8 @@ func (a *AccountTrie) GetStorage(addr common.Address, key []byte) ([]byte, error
117125 return decoded , err
118126}
119127
120- // UpdateAccount implements state.Trie.
128+ // UpdateAccount replaces or creates the state account associated with an address.
129+ // This new value will be returned for subsequent `GetAccount` calls.
121130func (a * AccountTrie ) UpdateAccount (addr common.Address , account * types.StateAccount ) error {
122131 // Queue the keys and values for later commit
123132 key := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
@@ -132,7 +141,8 @@ func (a *AccountTrie) UpdateAccount(addr common.Address, account *types.StateAcc
132141 return nil
133142}
134143
135- // UpdateStorage implements state.Trie.
144+ // UpdateStorage replaces or creates the value associated with a storage key for a given account address.
145+ // This new value will be returned for subsequent `GetStorage` calls.
136146func (a * AccountTrie ) UpdateStorage (addr common.Address , key []byte , value []byte ) error {
137147 var combinedKey [2 * common .HashLength ]byte
138148 accountKey := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
@@ -153,7 +163,7 @@ func (a *AccountTrie) UpdateStorage(addr common.Address, key []byte, value []byt
153163 return nil
154164}
155165
156- // DeleteAccount implements state.Trie .
166+ // DeleteAccount removes the state account associated with an address .
157167func (a * AccountTrie ) DeleteAccount (addr common.Address ) error {
158168 key := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
159169 // Queue the key for deletion
@@ -164,7 +174,7 @@ func (a *AccountTrie) DeleteAccount(addr common.Address) error {
164174 return nil
165175}
166176
167- // DeleteStorage implements state.Trie .
177+ // DeleteStorage removes the value associated with a storage key for a given account address .
168178func (a * AccountTrie ) DeleteStorage (addr common.Address , key []byte ) error {
169179 var combinedKey [2 * common .HashLength ]byte
170180 accountKey := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
@@ -180,7 +190,9 @@ func (a *AccountTrie) DeleteStorage(addr common.Address, key []byte) error {
180190 return nil
181191}
182192
183- // Hash implements state.Trie.
193+ // Hash returns the current hash of the state trie.
194+ // This will create a proposal and drop it, so it is not efficient to call for each transaction.
195+ // If there are no changes since the last call, the cached root is returned.
184196func (a * AccountTrie ) Hash () common.Hash {
185197 hash , err := a .hash ()
186198 if err != nil {
@@ -203,7 +215,9 @@ func (a *AccountTrie) hash() (common.Hash, error) {
203215 return a .root , nil
204216}
205217
206- // Commit implements state.Trie.
218+ // Commit returns the new root hash of the trie and a NodeSet containing all modified accounts and storage slots.
219+ // The format of the NodeSet is different than in go-ethereum's trie implementation due to Firewood's design.
220+ // This boolean is ignored, as it is a relic of the StateTrie implementation.
207221func (a * AccountTrie ) Commit (bool ) (common.Hash , * trienode.NodeSet , error ) {
208222 // Get the hash of the trie.
209223 hash , err := a .hash ()
@@ -212,7 +226,7 @@ func (a *AccountTrie) Commit(bool) (common.Hash, *trienode.NodeSet, error) {
212226 }
213227
214228 // Create the NodeSet. This will be sent to `triedb.Update` later.
215- nodeset := trienode .NewNodeSet (a . parentRoot )
229+ nodeset := trienode .NewNodeSet (common. Hash {} )
216230 for i , key := range a .updateKeys {
217231 nodeset .AddNode (key , & trienode.Node {
218232 Blob : a .updateValues [i ],
@@ -229,16 +243,19 @@ func (*AccountTrie) UpdateContractCode(common.Address, common.Hash, []byte) erro
229243}
230244
231245// GetKey implements state.Trie.
246+ // This should not be used, since any user should not be accessing by raw key.
232247func (* AccountTrie ) GetKey ([]byte ) []byte {
233- return nil // Not implemented, as this is only used in APIs
248+ return nil
234249}
235250
236251// NodeIterator implements state.Trie.
252+ // Firewood does not support iterating over internal nodes.
237253func (* AccountTrie ) NodeIterator ([]byte ) (trie.NodeIterator , error ) {
238254 return nil , errors .New ("NodeIterator not implemented for Firewood" )
239255}
240256
241257// Prove implements state.Trie.
258+ // Firewood does not yet support providing key proofs.
242259func (* AccountTrie ) Prove ([]byte , ethdb.KeyValueWriter ) error {
243260 return errors .New ("Prove not implemented for Firewood" )
244261}
0 commit comments