@@ -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
@@ -83,7 +88,10 @@ func (a *AccountTrie) GetAccount(addr common.Address) (*types.StateAccount, erro
8388 return account , err
8489}
8590
86- // GetStorage implements state.Trie.
91+ // GetStorage returns the value associated with a storage key for a given account address.
92+ // - If the storage slot has been updated, the new value is returned.
93+ // - If the storage slot has been deleted, (nil, nil) is returned.
94+ // - If the storage slot does not exist, (nil, nil) is returned.
8795func (a * AccountTrie ) GetStorage (addr common.Address , key []byte ) ([]byte , error ) {
8896 // If the account has been deleted, we should return nil
8997 accountKey := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
@@ -119,7 +127,8 @@ func (a *AccountTrie) GetStorage(addr common.Address, key []byte) ([]byte, error
119127 return decoded , err
120128}
121129
122- // UpdateAccount implements state.Trie.
130+ // UpdateAccount replaces or creates the state account associated with an address.
131+ // This new value will be returned for subsequent `GetAccount` calls.
123132func (a * AccountTrie ) UpdateAccount (addr common.Address , account * types.StateAccount ) error {
124133 // Queue the keys and values for later commit
125134 key := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
@@ -134,7 +143,8 @@ func (a *AccountTrie) UpdateAccount(addr common.Address, account *types.StateAcc
134143 return nil
135144}
136145
137- // UpdateStorage implements state.Trie.
146+ // UpdateStorage replaces or creates the value associated with a storage key for a given account address.
147+ // This new value will be returned for subsequent `GetStorage` calls.
138148func (a * AccountTrie ) UpdateStorage (addr common.Address , key []byte , value []byte ) error {
139149 var combinedKey [2 * common .HashLength ]byte
140150 accountKey := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
@@ -155,7 +165,7 @@ func (a *AccountTrie) UpdateStorage(addr common.Address, key []byte, value []byt
155165 return nil
156166}
157167
158- // DeleteAccount implements state.Trie .
168+ // DeleteAccount removes the state account associated with an address .
159169func (a * AccountTrie ) DeleteAccount (addr common.Address ) error {
160170 key := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
161171 // Queue the key for deletion
@@ -166,7 +176,7 @@ func (a *AccountTrie) DeleteAccount(addr common.Address) error {
166176 return nil
167177}
168178
169- // DeleteStorage implements state.Trie .
179+ // DeleteStorage removes the value associated with a storage key for a given account address .
170180func (a * AccountTrie ) DeleteStorage (addr common.Address , key []byte ) error {
171181 var combinedKey [2 * common .HashLength ]byte
172182 accountKey := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
@@ -182,7 +192,9 @@ func (a *AccountTrie) DeleteStorage(addr common.Address, key []byte) error {
182192 return nil
183193}
184194
185- // Hash implements state.Trie.
195+ // Hash returns the current hash of the state trie.
196+ // This will create a proposal and drop it, so it is not efficient to call for each transaction.
197+ // If there are no changes since the last call, the cached root is returned.
186198func (a * AccountTrie ) Hash () common.Hash {
187199 hash , err := a .hash ()
188200 if err != nil {
@@ -205,16 +217,18 @@ func (a *AccountTrie) hash() (common.Hash, error) {
205217 return a .root , nil
206218}
207219
208- // Commit implements state.Trie.
209- func (a * AccountTrie ) Commit (_ bool ) (common.Hash , * trienode.NodeSet , error ) {
220+ // Commit returns the new root hash of the trie and a NodeSet containing all modified accounts and storage slots.
221+ // The format of the NodeSet is different than in go-ethereum's trie implementation due to Firewood's design.
222+ // This boolean is ignored, as it is a relic of the StateTrie implementation.
223+ func (a * AccountTrie ) Commit (bool ) (common.Hash , * trienode.NodeSet , error ) {
210224 // Get the hash of the trie.
211225 hash , err := a .hash ()
212226 if err != nil {
213227 return common.Hash {}, nil , err
214228 }
215229
216230 // Create the NodeSet. This will be sent to `triedb.Update` later.
217- nodeset := trienode .NewNodeSet (a . parentRoot )
231+ nodeset := trienode .NewNodeSet (common. Hash {} )
218232 for i , key := range a .updateKeys {
219233 nodeset .AddNode (key , & trienode.Node {
220234 Blob : a .updateValues [i ],
@@ -231,17 +245,20 @@ func (*AccountTrie) UpdateContractCode(_ common.Address, _ common.Hash, _ []byte
231245}
232246
233247// GetKey implements state.Trie.
234- func (* AccountTrie ) GetKey (_ []byte ) []byte {
235- return nil // Not implemented, as this is only used in APIs
248+ // This should not be used, since any user should not be accessing by raw key.
249+ func (* AccountTrie ) GetKey ([]byte ) []byte {
250+ return nil
236251}
237252
238253// NodeIterator implements state.Trie.
239- func (* AccountTrie ) NodeIterator (_ []byte ) (trie.NodeIterator , error ) {
254+ // Firewood does not support iterating over internal nodes.
255+ func (* AccountTrie ) NodeIterator ([]byte ) (trie.NodeIterator , error ) {
240256 return nil , errors .New ("NodeIterator not implemented for Firewood" )
241257}
242258
243259// Prove implements state.Trie.
244- func (* AccountTrie ) Prove (_ []byte , _ ethdb.KeyValueWriter ) error {
260+ // Firewood does not yet support providing key proofs.
261+ func (* AccountTrie ) Prove ([]byte , ethdb.KeyValueWriter ) error {
245262 return errors .New ("Prove not implemented for Firewood" )
246263}
247264
0 commit comments