Skip to content

Commit 8b74e76

Browse files
committed
add archive support
1 parent cb9f133 commit 8b74e76

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

libevm/triedb/firewood/firewood.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ import (
4242
)
4343

4444
const (
45-
Scheme = "firewood"
46-
dbFileName = "firewood.db"
47-
logFileName = "firewood.log"
45+
Scheme = "firewood"
46+
dbFileName = "firewood.db"
47+
logFileName = "firewood.log"
48+
rootStoreDir = "root_store"
4849
)
4950

5051
var (
@@ -104,11 +105,12 @@ type proposalMeta struct {
104105

105106
// Config provides necessary parameters for creating a Firewood database.
106107
type Config struct {
107-
DatabasePath string
108+
DatabasePath string // directory where the database files will be stored
108109
CacheSizeBytes uint
109110
FreeListCacheEntries uint
110-
MaxRevisions uint
111+
RevisionsInMemory uint // must be >= 2
111112
CacheStrategy ffi.CacheStrategy
113+
Archive bool // whether to write keep all historical revisions on disk
112114
}
113115

114116
// DefaultConfig returns a default Config with the given directory.
@@ -122,7 +124,7 @@ func DefaultConfig(dir string) Config {
122124
DatabasePath: dir,
123125
CacheSizeBytes: 1024 * 1024, // 1MB
124126
FreeListCacheEntries: 40_000,
125-
MaxRevisions: 100,
127+
RevisionsInMemory: 100,
126128
CacheStrategy: ffi.CacheAllReads,
127129
}
128130
}
@@ -154,11 +156,16 @@ func New(config Config, disk ethdb.Database) (*Database, error) {
154156
}
155157

156158
dbPath := filepath.Join(config.DatabasePath, dbFileName)
159+
var rootStorePath string
160+
if config.Archive {
161+
rootStorePath = filepath.Join(config.DatabasePath, rootStoreDir)
162+
}
157163
fw, err := ffi.New(dbPath, &ffi.Config{
158164
NodeCacheEntries: config.CacheSizeBytes / 256, // TODO(alarso16): 256 bytes per node may not be accurate
159165
FreeListCacheEntries: config.FreeListCacheEntries,
160-
Revisions: config.MaxRevisions,
166+
Revisions: config.RevisionsInMemory,
161167
ReadCacheStrategy: config.CacheStrategy,
168+
RootStoreDir: rootStorePath,
162169
})
163170
if err != nil {
164171
return nil, err
@@ -582,19 +589,19 @@ func (db *Database) Reader(root common.Hash) (database.Reader, error) {
582589
if _, err := db.Firewood.GetFromRoot(ffi.Hash(root), []byte{}); err != nil {
583590
return nil, fmt.Errorf("firewood: unable to retrieve from root %s: %w", root.Hex(), err)
584591
}
585-
return &reader{db: db, root: root}, nil
592+
return &reader{db: db, root: ffi.Hash(root)}, nil
586593
}
587594

588595
// reader is a state reader of Database which implements the Reader interface.
589596
type reader struct {
590597
db *Database
591-
root common.Hash // The root of the state this reader is reading.
598+
root ffi.Hash // The root of the state this reader is reading.
592599
}
593600

594601
// Node retrieves the trie node with the given node hash. No error will be
595602
// returned if the node is not found.
596603
func (reader *reader) Node(_ common.Hash, path []byte, _ common.Hash) ([]byte, error) {
597604
// This function relies on Firewood's internal locking to ensure concurrent reads are safe.
598605
// This is safe even if a proposal is being committed concurrently.
599-
return reader.db.Firewood.GetFromRoot(ffi.Hash(reader.root), path)
606+
return reader.db.Firewood.GetFromRoot(reader.root, path)
600607
}

0 commit comments

Comments
 (0)