@@ -24,6 +24,8 @@ import (
24
24
// 2. The `Hash` method actually creates the proposal, since Firewood cannot calculate
25
25
// the hash of the trie without committing it. It is immediately dropped, and this
26
26
// can likely be optimized.
27
+ //
28
+ // Note this is not concurrent safe.
27
29
type AccountTrie struct {
28
30
fw * Database
29
31
parentRoot common.Hash
@@ -49,7 +51,10 @@ func NewAccountTrie(root common.Hash, db *Database) (*AccountTrie, error) {
49
51
}, nil
50
52
}
51
53
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.
53
58
func (a * AccountTrie ) GetAccount (addr common.Address ) (* types.StateAccount , error ) {
54
59
key := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
55
60
@@ -82,7 +87,10 @@ func (a *AccountTrie) GetAccount(addr common.Address) (*types.StateAccount, erro
82
87
return account , err
83
88
}
84
89
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.
86
94
func (a * AccountTrie ) GetStorage (addr common.Address , key []byte ) ([]byte , error ) {
87
95
// If the account has been deleted, we should return nil
88
96
accountKey := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
@@ -117,7 +125,8 @@ func (a *AccountTrie) GetStorage(addr common.Address, key []byte) ([]byte, error
117
125
return decoded , err
118
126
}
119
127
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.
121
130
func (a * AccountTrie ) UpdateAccount (addr common.Address , account * types.StateAccount ) error {
122
131
// Queue the keys and values for later commit
123
132
key := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
@@ -132,7 +141,8 @@ func (a *AccountTrie) UpdateAccount(addr common.Address, account *types.StateAcc
132
141
return nil
133
142
}
134
143
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.
136
146
func (a * AccountTrie ) UpdateStorage (addr common.Address , key []byte , value []byte ) error {
137
147
var combinedKey [2 * common .HashLength ]byte
138
148
accountKey := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
@@ -153,7 +163,7 @@ func (a *AccountTrie) UpdateStorage(addr common.Address, key []byte, value []byt
153
163
return nil
154
164
}
155
165
156
- // DeleteAccount implements state.Trie .
166
+ // DeleteAccount removes the state account associated with an address .
157
167
func (a * AccountTrie ) DeleteAccount (addr common.Address ) error {
158
168
key := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
159
169
// Queue the key for deletion
@@ -164,7 +174,7 @@ func (a *AccountTrie) DeleteAccount(addr common.Address) error {
164
174
return nil
165
175
}
166
176
167
- // DeleteStorage implements state.Trie .
177
+ // DeleteStorage removes the value associated with a storage key for a given account address .
168
178
func (a * AccountTrie ) DeleteStorage (addr common.Address , key []byte ) error {
169
179
var combinedKey [2 * common .HashLength ]byte
170
180
accountKey := crypto .Keccak256Hash (addr .Bytes ()).Bytes ()
@@ -180,7 +190,9 @@ func (a *AccountTrie) DeleteStorage(addr common.Address, key []byte) error {
180
190
return nil
181
191
}
182
192
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.
184
196
func (a * AccountTrie ) Hash () common.Hash {
185
197
hash , err := a .hash ()
186
198
if err != nil {
@@ -203,7 +215,9 @@ func (a *AccountTrie) hash() (common.Hash, error) {
203
215
return a .root , nil
204
216
}
205
217
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.
207
221
func (a * AccountTrie ) Commit (bool ) (common.Hash , * trienode.NodeSet , error ) {
208
222
// Get the hash of the trie.
209
223
hash , err := a .hash ()
@@ -212,7 +226,7 @@ func (a *AccountTrie) Commit(bool) (common.Hash, *trienode.NodeSet, error) {
212
226
}
213
227
214
228
// Create the NodeSet. This will be sent to `triedb.Update` later.
215
- nodeset := trienode .NewNodeSet (a . parentRoot )
229
+ nodeset := trienode .NewNodeSet (common. Hash {} )
216
230
for i , key := range a .updateKeys {
217
231
nodeset .AddNode (key , & trienode.Node {
218
232
Blob : a .updateValues [i ],
@@ -229,16 +243,19 @@ func (*AccountTrie) UpdateContractCode(common.Address, common.Hash, []byte) erro
229
243
}
230
244
231
245
// GetKey implements state.Trie.
246
+ // This should not be used, since any user should not be accessing by raw key.
232
247
func (* AccountTrie ) GetKey ([]byte ) []byte {
233
- return nil // Not implemented, as this is only used in APIs
248
+ return nil
234
249
}
235
250
236
251
// NodeIterator implements state.Trie.
252
+ // Firewood does not support iterating over internal nodes.
237
253
func (* AccountTrie ) NodeIterator ([]byte ) (trie.NodeIterator , error ) {
238
254
return nil , errors .New ("NodeIterator not implemented for Firewood" )
239
255
}
240
256
241
257
// Prove implements state.Trie.
258
+ // Firewood does not yet support providing key proofs.
242
259
func (* AccountTrie ) Prove ([]byte , ethdb.KeyValueWriter ) error {
243
260
return errors .New ("Prove not implemented for Firewood" )
244
261
}
0 commit comments