Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"fmt"
"io"
"math/big"
"path/filepath"
"runtime"
"strings"
"sync"
Expand All @@ -48,7 +49,6 @@ import (
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/plugin/evm/customrawdb"
"github.com/ava-labs/coreth/plugin/evm/customtypes"
"github.com/ava-labs/coreth/triedb/firewood"
"github.com/ava-labs/coreth/triedb/hashdb"
"github.com/ava-labs/coreth/triedb/pathdb"
"github.com/ava-labs/libevm/common"
Expand All @@ -61,6 +61,7 @@ import (
"github.com/ava-labs/libevm/ethdb"
"github.com/ava-labs/libevm/event"
"github.com/ava-labs/libevm/libevm/stateconf"
"github.com/ava-labs/libevm/libevm/triedb/firewood"
"github.com/ava-labs/libevm/log"
"github.com/ava-labs/libevm/metrics"
"github.com/ava-labs/libevm/triedb"
Expand Down Expand Up @@ -176,6 +177,9 @@ const (
// trieCleanCacheStatsNamespace is the namespace to surface stats from the trie
// clean cache's underlying fastcache.
trieCleanCacheStatsNamespace = "hashdb/memcache/clean/fastcache"

// firewoodPath is the default path for firewood database.
firewoodPath = "firewood"
)

// CacheConfig contains the configuration values for the trie database
Expand Down Expand Up @@ -223,19 +227,19 @@ func (c *CacheConfig) triedbConfig() *triedb.Config {
DirtyCacheSize: c.TrieDirtyLimit * 1024 * 1024,
}.BackendConstructor
}
if c.StateScheme == customrawdb.FirewoodScheme {
if c.StateScheme == firewood.Scheme {
// ChainDataDir may not be set during some tests, where this path won't be called.
if c.ChainDataDir == "" {
log.Crit("Chain data directory must be specified for Firewood")
}

config.DBOverride = firewood.Config{
ChainDataDir: c.ChainDataDir,
CleanCacheSize: c.TrieCleanLimit * 1024 * 1024,
FreeListCacheEntries: firewood.Defaults.FreeListCacheEntries,
Revisions: uint(c.StateHistory), // must be at least 2
ReadCacheStrategy: ffi.CacheAllReads,
ArchiveMode: !c.Pruning,
DatabasePath: filepath.Join(c.ChainDataDir, firewoodPath),
CacheSizeBytes: uint(c.TrieCleanLimit * 1024 * 1024),
FreeListCacheEntries: 40_000, // same as Firewood default
RevisionsInMemory: uint(c.StateHistory), // must be at least 2
CacheStrategy: ffi.CacheAllReads,
Archive: !c.Pruning,
}.BackendConstructor
}
return config
Expand Down Expand Up @@ -263,7 +267,7 @@ func DefaultCacheConfigWithScheme(scheme string) *CacheConfig {
config := *DefaultCacheConfig
config.StateScheme = scheme
// TODO: remove this once if Firewood supports snapshots
if config.StateScheme == customrawdb.FirewoodScheme {
if config.StateScheme == firewood.Scheme {
config.SnapshotLimit = 0 // no snapshot allowed for firewood
}
return &config
Expand Down Expand Up @@ -1789,7 +1793,7 @@ func (bc *BlockChain) commitWithSnap(

// Because Firewood relies on tracking block hashes in a tree, we need to notify the
// database that this block is empty.
if bc.CacheConfig().StateScheme == customrawdb.FirewoodScheme && root == parentRoot {
if bc.triedb.Scheme() == firewood.Scheme && root == parentRoot {
if err := bc.triedb.Update(root, parentRoot, current.NumberU64(), nil, nil, triedbOpt); err != nil {
return common.Hash{}, fmt.Errorf("failed to update trie for block %s: %w", current.Hash(), err)
}
Expand Down Expand Up @@ -1956,7 +1960,7 @@ func (bc *BlockChain) reprocessState(current *types.Block, reexec uint64) error
log.Info("Historical state regenerated", "block", current.NumberU64(), "elapsed", time.Since(start), "nodes", nodes, "preimages", imgs)

// Firewood requires processing each root individually.
if bc.CacheConfig().StateScheme == customrawdb.FirewoodScheme {
if triedb.Scheme() == firewood.Scheme {
for _, root := range roots {
if err := triedb.Commit(root, true); err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions core/blockchain_repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ import (

"github.com/ava-labs/coreth/consensus/dummy"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/plugin/evm/customrawdb"
"github.com/ava-labs/coreth/plugin/evm/upgrade/ap3"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/rawdb"
"github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/crypto"
"github.com/ava-labs/libevm/libevm/triedb/firewood"
ethparams "github.com/ava-labs/libevm/params"
"github.com/ava-labs/libevm/triedb"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -508,7 +508,7 @@ func testLongReorgedDeepRepair(t *testing.T, snapshots bool) {
}

func testRepair(t *testing.T, tt *rewindTest, snapshots bool) {
for _, scheme := range []string{rawdb.HashScheme, rawdb.PathScheme, customrawdb.FirewoodScheme} {
for _, scheme := range []string{rawdb.HashScheme, rawdb.PathScheme, firewood.Scheme} {
t.Run(scheme, func(t *testing.T) {
testRepairWithScheme(t, tt, snapshots, scheme)
})
Expand All @@ -520,7 +520,7 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s
//log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
// fmt.Println(tt.dump(true))

if scheme == customrawdb.FirewoodScheme && snapshots {
if scheme == firewood.Scheme && snapshots {
t.Skip("Firewood scheme does not support snapshots")
}

Expand Down
5 changes: 3 additions & 2 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/ava-labs/libevm/crypto"
"github.com/ava-labs/libevm/eth/tracers/logger"
"github.com/ava-labs/libevm/ethdb"
"github.com/ava-labs/libevm/libevm/triedb/firewood"
ethparams "github.com/ava-labs/libevm/params"
)

Expand Down Expand Up @@ -73,7 +74,7 @@ var (
}

// Firewood should only be included for snapshot disabled tests.
schemes = []string{rawdb.HashScheme, customrawdb.FirewoodScheme}
schemes = []string{rawdb.HashScheme, firewood.Scheme}
)

func newGwei(n int64) *big.Int {
Expand Down Expand Up @@ -728,7 +729,7 @@ func testUngracefulAsyncShutdown(t *testing.T, scheme string, snapshotEnabled bo
// TestCanonicalHashMarker tests all the canonical hash markers are updated/deleted
// correctly in case reorg is called.
func TestCanonicalHashMarker(t *testing.T) {
for _, scheme := range []string{rawdb.HashScheme, rawdb.PathScheme, customrawdb.FirewoodScheme} {
for _, scheme := range []string{rawdb.HashScheme, rawdb.PathScheme, firewood.Scheme} {
t.Run(scheme, func(t *testing.T) {
testCanonicalHashMarker(t, scheme)
})
Expand Down
3 changes: 1 addition & 2 deletions core/extstate/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ package extstate
import (
"github.com/ava-labs/libevm/core/state"
"github.com/ava-labs/libevm/ethdb"
"github.com/ava-labs/libevm/libevm/triedb/firewood"
"github.com/ava-labs/libevm/triedb"

"github.com/ava-labs/coreth/triedb/firewood"
)

func NewDatabaseWithConfig(db ethdb.Database, config *triedb.Config) state.Database {
Expand Down
5 changes: 2 additions & 3 deletions core/extstate/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
"github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/crypto"
"github.com/ava-labs/libevm/libevm/stateconf"
"github.com/ava-labs/libevm/libevm/triedb/firewood"
"github.com/ava-labs/libevm/trie/trienode"
"github.com/ava-labs/libevm/triedb"
"github.com/holiman/uint256"
"github.com/stretchr/testify/require"

"github.com/ava-labs/coreth/triedb/firewood"
"github.com/ava-labs/coreth/triedb/hashdb"
)

Expand Down Expand Up @@ -81,8 +81,7 @@ func newFuzzState(t *testing.T) *fuzzState {
})

firewoodMemdb := rawdb.NewMemoryDatabase()
fwCfg := firewood.Defaults // copy the defaults
fwCfg.ChainDataDir = t.TempDir() // Use a temporary directory for the Firewood
fwCfg := firewood.DefaultConfig(t.TempDir()) // Use a temporary directory for Firewood
firewoodState := NewDatabaseWithConfig(
firewoodMemdb,
&triedb.Config{
Expand Down
3 changes: 1 addition & 2 deletions core/extstate/firewood_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (

"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/state"

"github.com/ava-labs/coreth/triedb/firewood"
"github.com/ava-labs/libevm/libevm/triedb/firewood"
)

var (
Expand Down
8 changes: 7 additions & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/ethdb"
"github.com/ava-labs/libevm/libevm/stateconf"
"github.com/ava-labs/libevm/libevm/triedb/firewood"
"github.com/ava-labs/libevm/log"
ethparams "github.com/ava-labs/libevm/params"
"github.com/ava-labs/libevm/trie"
Expand Down Expand Up @@ -338,8 +339,13 @@ func (g *Genesis) toBlock(db ethdb.Database, triedb *triedb.Database) *types.Blo
if _, err := statedb.Commit(0, false, stateconf.WithTrieDBUpdateOpts(triedbOpt)); err != nil {
panic(fmt.Sprintf("unable to commit genesis block to statedb: %v", err))
}
if root == types.EmptyRootHash && triedb.Scheme() == firewood.Scheme {
if err := triedb.Update(root, root, 0, nil, nil, triedbOpt); err != nil {
panic(fmt.Sprintf("unable to update genesis block in triedb: %v", err))
}
}
// Commit newly generated states into disk if it's not empty.
if root != types.EmptyRootHash {
if root != types.EmptyRootHash || triedb.Scheme() == firewood.Scheme {
if err := triedb.Commit(root, true); err != nil {
panic(fmt.Sprintf("unable to commit genesis block: %v", err))
}
Expand Down
10 changes: 4 additions & 6 deletions core/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,16 @@ import (
"github.com/ava-labs/coreth/consensus/dummy"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/params/extras"
"github.com/ava-labs/coreth/plugin/evm/customrawdb"
"github.com/ava-labs/coreth/plugin/evm/upgrade/ap3"
"github.com/ava-labs/coreth/precompile/contracts/warp"
"github.com/ava-labs/coreth/triedb/firewood"
"github.com/ava-labs/coreth/triedb/pathdb"
"github.com/ava-labs/coreth/utils"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/rawdb"
"github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/ethdb"
"github.com/ava-labs/libevm/libevm/triedb/firewood"
ethparams "github.com/ava-labs/libevm/params"
"github.com/ava-labs/libevm/trie"
"github.com/ava-labs/libevm/triedb"
Expand All @@ -69,7 +68,7 @@ func TestGenesisBlockForTesting(t *testing.T) {
}

func TestSetupGenesis(t *testing.T) {
for _, scheme := range []string{rawdb.HashScheme, rawdb.PathScheme, customrawdb.FirewoodScheme} {
for _, scheme := range []string{rawdb.HashScheme, rawdb.PathScheme, firewood.Scheme} {
t.Run(scheme, func(t *testing.T) {
testSetupGenesis(t, scheme)
})
Expand Down Expand Up @@ -303,10 +302,9 @@ func newDbConfig(t *testing.T, scheme string) *triedb.Config {
return triedb.HashDefaults
case rawdb.PathScheme:
return &triedb.Config{DBOverride: pathdb.Defaults.BackendConstructor}
case customrawdb.FirewoodScheme:
fwCfg := firewood.Defaults
case firewood.Scheme:
// Create a unique temporary directory for each test
fwCfg.ChainDataDir = t.TempDir()
fwCfg := firewood.DefaultConfig(t.TempDir())
return &triedb.Config{DBOverride: fwCfg.BackendConstructor}
default:
t.Fatalf("unknown scheme %s", scheme)
Expand Down
4 changes: 2 additions & 2 deletions core/state_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ package core
import (
"fmt"

"github.com/ava-labs/coreth/plugin/evm/customrawdb"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/ethdb"
"github.com/ava-labs/libevm/libevm/triedb/firewood"
)

// flushWindow is the distance to the [commitInterval] when we start
Expand All @@ -60,7 +60,7 @@ type TrieDB interface {

func NewTrieWriter(db TrieDB, config *CacheConfig) TrieWriter {
// Firewood should only be used in pruning mode, but we shouldn't explicitly manage this.
if config.Pruning && config.StateScheme != customrawdb.FirewoodScheme {
if config.Pruning && config.StateScheme != firewood.Scheme {
cm := &cappedMemoryTrieWriter{
TrieDB: db,
memoryCap: common.StorageSize(config.TrieDirtyLimit) * 1024 * 1024,
Expand Down
4 changes: 2 additions & 2 deletions eth/api_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ import (
"time"

"github.com/ava-labs/coreth/internal/ethapi"
"github.com/ava-labs/coreth/plugin/evm/customrawdb"
"github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/common/hexutil"
"github.com/ava-labs/libevm/core/rawdb"
"github.com/ava-labs/libevm/core/state"
"github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/crypto"
"github.com/ava-labs/libevm/libevm/triedb/firewood"
"github.com/ava-labs/libevm/log"
"github.com/ava-labs/libevm/rlp"
"github.com/ava-labs/libevm/trie"
Expand Down Expand Up @@ -386,5 +386,5 @@ func (api *DebugAPI) GetAccessibleState(from, to rpc.BlockNumber) (uint64, error
}

func (api *DebugAPI) isFirewood() bool {
return api.eth.blockchain.CacheConfig().StateScheme == customrawdb.FirewoodScheme
return api.eth.blockchain.CacheConfig().StateScheme == firewood.Scheme
}
7 changes: 1 addition & 6 deletions eth/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/core/extstate"
"github.com/ava-labs/coreth/eth/tracers"
"github.com/ava-labs/coreth/plugin/evm/customrawdb"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/rawdb"
"github.com/ava-labs/libevm/core/state"
Expand Down Expand Up @@ -226,12 +225,8 @@ func (eth *Ethereum) pathState(block *types.Block) (*state.StateDB, func(), erro
// provided, it would be preferable to start from a fresh state, if we have it
// on disk.
func (eth *Ethereum) stateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (statedb *state.StateDB, release tracers.StateReleaseFunc, err error) {
// Check if we're using firewood backend by typecasting
// firewoodDB.Scheme() == rawdb.HashScheme, so typecasting is necessary
isFirewood := eth.blockchain.CacheConfig().StateScheme == customrawdb.FirewoodScheme

// Use `hashState` if the state can be recomputed from the live database.
if eth.blockchain.TrieDB().Scheme() == rawdb.HashScheme && !isFirewood {
if eth.blockchain.TrieDB().Scheme() == rawdb.HashScheme {
return eth.hashState(ctx, block, reexec, base, readOnly, preferDisk)
}
return eth.pathState(block)
Expand Down
6 changes: 3 additions & 3 deletions eth/tracers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/internal/ethapi"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/plugin/evm/customrawdb"
"github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/common/hexutil"
Expand All @@ -54,6 +53,7 @@ import (
"github.com/ava-labs/libevm/crypto"
"github.com/ava-labs/libevm/eth/tracers/logger"
"github.com/ava-labs/libevm/ethdb"
"github.com/ava-labs/libevm/libevm/triedb/firewood"
ethparams "github.com/ava-labs/libevm/params"
"golang.org/x/exp/slices"
)
Expand All @@ -62,7 +62,7 @@ var (
errStateNotFound = errors.New("state not found")
errBlockNotFound = errors.New("block not found")

schemes = []string{rawdb.HashScheme, customrawdb.FirewoodScheme}
schemes = []string{rawdb.HashScheme, firewood.Scheme}
)

type testBackend struct {
Expand Down Expand Up @@ -101,7 +101,7 @@ func newTestBackend(t *testing.T, n int, gspec *core.Genesis, scheme string, gen
StateHistory: 100, // Sufficient history for testing
ChainDataDir: t.TempDir(),
}
if scheme == customrawdb.FirewoodScheme {
if scheme == firewood.Scheme {
cacheConfig.SnapshotLimit = 0 // Firewood does not support snapshots
}

Expand Down
4 changes: 2 additions & 2 deletions eth/tracers/tracers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (

"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/plugin/evm/customrawdb"
"github.com/ava-labs/coreth/plugin/evm/customtypes"
"github.com/ava-labs/coreth/tests"
"github.com/ava-labs/libevm/common"
Expand All @@ -43,6 +42,7 @@ import (
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/crypto"
"github.com/ava-labs/libevm/eth/tracers/logger"
"github.com/ava-labs/libevm/libevm/triedb/firewood"
)

func TestMain(m *testing.M) {
Expand All @@ -52,7 +52,7 @@ func TestMain(m *testing.M) {
}

func BenchmarkPrestateTracer(b *testing.B) {
for _, scheme := range []string{rawdb.HashScheme, customrawdb.FirewoodScheme} {
for _, scheme := range []string{rawdb.HashScheme, firewood.Scheme} {
b.Run(scheme, func(b *testing.B) {
benchmarkTransactionTrace(b, scheme)
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/VictoriaMetrics/fastcache v1.12.1
github.com/ava-labs/avalanchego v1.14.1-0.20251120155522-df4a8e531761
github.com/ava-labs/firewood-go-ethhash/ffi v0.0.15
github.com/ava-labs/libevm v1.13.15-0.20251016142715-1bccf4f2ddb2
github.com/ava-labs/libevm v1.13.15-0.20251125171320-8b74e7640434
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/deckarep/golang-set/v2 v2.1.0
github.com/go-cmd/cmd v1.4.3
Expand Down
Loading
Loading