Skip to content

Commit 348decc

Browse files
authored
update comments and file names (#1744)
1 parent 18f1318 commit 348decc

File tree

5 files changed

+69
-49
lines changed

5 files changed

+69
-49
lines changed

core/blockchain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ const (
175175
// clean cache's underlying fastcache.
176176
trieCleanCacheStatsNamespace = "hashdb/memcache/clean/fastcache"
177177

178-
firewoodFileName = "firewood_state"
178+
firewoodFileName = "firewood.db"
179179
)
180180

181181
// cacheableFeeConfig encapsulates fee configuration itself and the block number that it has changed at,

core/extstate/firewood_database.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ func (db *firewoodAccessorDb) OpenTrie(root common.Hash) (state.Trie, error) {
2929
}
3030

3131
// OpenStorageTrie opens a wrapped version of the account trie.
32-
func (*firewoodAccessorDb) OpenStorageTrie(_ common.Hash, _ common.Address, accountRoot common.Hash, self state.Trie) (state.Trie, error) {
32+
//
33+
//nolint:revive // removing names loses context.
34+
func (*firewoodAccessorDb) OpenStorageTrie(stateRoot common.Hash, addr common.Address, accountRoot common.Hash, self state.Trie) (state.Trie, error) {
3335
accountTrie, ok := self.(*firewood.AccountTrie)
3436
if !ok {
3537
return nil, fmt.Errorf("Invalid account trie type: %T", self)
3638
}
37-
return firewood.NewStorageTrie(accountTrie, accountRoot)
39+
return firewood.NewStorageTrie(accountTrie)
3840
}
3941

4042
// CopyTrie returns a deep copy of the given trie.

triedb/firewood/account_trie.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
2729
type 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.
5358
func (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.
8795
func (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.
123132
func (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.
138148
func (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.
159169
func (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.
170180
func (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.
186198
func (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

triedb/firewood/database.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ var Defaults = Config{
7676
ReadCacheStrategy: ffi.CacheAllReads,
7777
}
7878

79-
func (c Config) BackendConstructor(_ ethdb.Database) triedb.DBOverride {
80-
return New(&c)
79+
func (c Config) BackendConstructor(ethdb.Database) triedb.DBOverride {
80+
db, err := New(c)
81+
if err != nil {
82+
log.Crit("firewood: error creating database", "error", err)
83+
}
84+
return db
8185
}
8286

8387
type Database struct {
@@ -94,14 +98,9 @@ type Database struct {
9498

9599
// New creates a new Firewood database with the given disk database and configuration.
96100
// Any error during creation will cause the program to exit.
97-
func New(config *Config) *Database {
98-
if config == nil {
99-
log.Crit("firewood: config must be provided")
100-
}
101-
102-
err := validatePath(config.FilePath)
103-
if err != nil {
104-
log.Crit("firewood: error validating config", "error", err)
101+
func New(config Config) (*Database, error) {
102+
if err := validatePath(config.FilePath); err != nil {
103+
return nil, err
105104
}
106105

107106
fw, err := ffi.New(config.FilePath, &ffi.Config{
@@ -111,12 +110,12 @@ func New(config *Config) *Database {
111110
ReadCacheStrategy: config.ReadCacheStrategy,
112111
})
113112
if err != nil {
114-
log.Crit("firewood: error creating firewood database", "error", err)
113+
return nil, err
115114
}
116115

117116
currentRoot, err := fw.Root()
118117
if err != nil {
119-
log.Crit("firewood: error getting current root", "error", err)
118+
return nil, err
120119
}
121120

122121
return &Database{
@@ -125,7 +124,7 @@ func New(config *Config) *Database {
125124
proposalTree: &ProposalContext{
126125
Root: common.Hash(currentRoot),
127126
},
128-
}
127+
}, nil
129128
}
130129

131130
func validatePath(path string) error {
@@ -135,17 +134,19 @@ func validatePath(path string) error {
135134

136135
// Check that the directory exists
137136
dir := filepath.Dir(path)
138-
_, err := os.Stat(dir)
139-
if err == nil {
140-
return nil // Directory exists
141-
}
142-
if !os.IsNotExist(err) {
137+
switch info, err := os.Stat(dir); {
138+
case os.IsNotExist(err):
139+
log.Info("Database directory not found, creating", "path", dir)
140+
if err := os.MkdirAll(dir, 0o755); err != nil {
141+
return fmt.Errorf("error creating database directory: %w", err)
142+
}
143+
return nil
144+
case err != nil:
143145
return fmt.Errorf("error checking database directory: %w", err)
146+
case !info.IsDir():
147+
return fmt.Errorf("database directory path is not a directory: %s", dir)
144148
}
145-
log.Info("Database directory not found, creating", "path", dir)
146-
if err := os.MkdirAll(dir, 0o755); err != nil {
147-
return fmt.Errorf("error creating database directory: %w", err)
148-
}
149+
149150
return nil
150151
}
151152

@@ -161,7 +162,7 @@ func (*Database) Scheme() string {
161162
}
162163

163164
// Initialized checks whether a non-empty genesis block has been written.
164-
func (db *Database) Initialized(_ common.Hash) bool {
165+
func (db *Database) Initialized(common.Hash) bool {
165166
rootBytes, err := db.fwDisk.Root()
166167
if err != nil {
167168
log.Error("firewood: error getting current root", "error", err)

triedb/firewood/storage_trie.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ import (
1010

1111
type StorageTrie struct {
1212
*AccountTrie
13-
storageRoot common.Hash
1413
}
1514

1615
// `NewStorageTrie` returns a wrapper around an `AccountTrie` since Firewood
1716
// does not require a separate storage trie. All changes are managed by the account trie.
18-
func NewStorageTrie(accountTrie *AccountTrie, storageRoot common.Hash) (*StorageTrie, error) {
17+
func NewStorageTrie(accountTrie *AccountTrie) (*StorageTrie, error) {
1918
return &StorageTrie{
2019
AccountTrie: accountTrie,
21-
storageRoot: storageRoot,
2220
}, nil
2321
}
2422

2523
// Actual commit is handled by the account trie.
26-
// Return the old storage root as if there was no change - we don't want to use the
27-
// actual account trie hash and nodeset here.
28-
func (s *StorageTrie) Commit(bool) (common.Hash, *trienode.NodeSet, error) {
29-
return s.storageRoot, nil, nil
24+
// Return the old storage root as if there was no change since Firewood
25+
// will manage the hash calculations without it.
26+
// All changes are managed by the account trie.
27+
func (*StorageTrie) Commit(bool) (common.Hash, *trienode.NodeSet, error) {
28+
return common.Hash{}, nil, nil
3029
}
3130

3231
// Firewood doesn't require tracking storage roots inside of an account.
33-
func (s *StorageTrie) Hash() common.Hash {
34-
return s.storageRoot // only used in statedb to populate a `StateAccount`
32+
// They will be updated in place when hashing of the proposal takes place.
33+
func (*StorageTrie) Hash() common.Hash {
34+
return common.Hash{}
3535
}
3636

3737
// Copy should never be called on a storage trie, as it is just a wrapper around the account trie.

0 commit comments

Comments
 (0)