Skip to content

Commit 0832972

Browse files
sync: coreth PR #1151: update firewood and fix test flakes (#1714)
Co-authored-by: Austin Larson <78000745+alarso16@users.noreply.github.com>
1 parent b99bd81 commit 0832972

File tree

3 files changed

+52
-39
lines changed

3 files changed

+52
-39
lines changed

core/extstate/database_test.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func newFuzzState(t *testing.T) *fuzzState {
8282
})
8383

8484
firewoodMemdb := rawdb.NewMemoryDatabase()
85-
fwCfg := firewood.Defaults
85+
fwCfg := firewood.Defaults // copy the defaults
8686
fwCfg.FilePath = filepath.Join(t.TempDir(), "firewood") // Use a temporary directory for the Firewood
8787
firewoodState := NewDatabaseWithConfig(
8888
firewoodMemdb,
@@ -309,15 +309,6 @@ func (fs *fuzzState) deleteStorage(accountIndex int, storageIndexInput uint64) {
309309
}
310310

311311
func FuzzTree(f *testing.F) {
312-
for randSeed := range int64(1000) {
313-
rand := rand.New(rand.NewSource(randSeed))
314-
steps := make([]byte, 32)
315-
_, err := rand.Read(steps)
316-
if err != nil {
317-
f.Fatal(err)
318-
}
319-
f.Add(randSeed, steps)
320-
}
321312
f.Fuzz(func(t *testing.T, randSeed int64, byteSteps []byte) {
322313
fuzzState := newFuzzState(t)
323314
rand := rand.New(rand.NewSource(randSeed))

plugin/evm/vm_test.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/ava-labs/avalanchego/snow"
2626
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
2727
"github.com/ava-labs/avalanchego/snow/engine/enginetest"
28+
"github.com/ava-labs/avalanchego/snow/snowtest"
2829
"github.com/ava-labs/avalanchego/upgrade"
2930
"github.com/ava-labs/avalanchego/upgrade/upgradetest"
3031
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
@@ -440,9 +441,28 @@ func testBuildEthTxBlock(t *testing.T, scheme string) {
440441
t.Fatalf("Found unexpected blkID for parent of blk2")
441442
}
442443

443-
restartedTVM, err := restartVM(tvm, tvm.config)
444-
require.NoError(t, err)
445-
restartedVM := restartedTVM.vm
444+
// Close the vm and all databases
445+
if err := tvm.vm.Shutdown(context.Background()); err != nil {
446+
t.Fatal(err)
447+
}
448+
449+
restartedVM := &VM{}
450+
newCTX := snowtest.Context(t, snowtest.CChainID)
451+
newCTX.NetworkUpgrades = upgradetest.GetConfig(fork)
452+
newCTX.ChainDataDir = tvm.vm.ctx.ChainDataDir
453+
conf := getConfig(scheme, "")
454+
if err := restartedVM.Initialize(
455+
context.Background(),
456+
newCTX,
457+
tvm.db,
458+
[]byte(toGenesisJSON(paramstest.ForkToChainConfig[fork])),
459+
[]byte(""),
460+
[]byte(conf),
461+
[]*commonEng.Fx{},
462+
nil,
463+
); err != nil {
464+
t.Fatal(err)
465+
}
446466

447467
// State root should not have been committed and discarded on restart
448468
if ethBlk1Root := ethBlk1.Root(); restartedVM.blockChain.HasState(ethBlk1Root) {
@@ -454,6 +474,11 @@ func testBuildEthTxBlock(t *testing.T, scheme string) {
454474
if ethBlk2Root := ethBlk2.Root(); !restartedVM.blockChain.HasState(ethBlk2Root) {
455475
t.Fatalf("Expected blk2 state root to not be pruned after shutdown (last accepted tip should be committed)")
456476
}
477+
478+
// Shutdown the newest VM
479+
if err := restartedVM.Shutdown(context.Background()); err != nil {
480+
t.Fatal(err)
481+
}
457482
}
458483

459484
// Regression test to ensure that after accepting block A

triedb/firewood/database.go

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ type Config struct {
6868
ReadCacheStrategy ffi.CacheStrategy
6969
}
7070

71-
// Note that `FilePath` is not specificied, and must always be set by the user.
72-
var Defaults = &Config{
71+
// Note that `FilePath` is not specified, and must always be set by the user.
72+
var Defaults = Config{
7373
CleanCacheSize: 1024 * 1024, // 1MB
7474
FreeListCacheEntries: 40_000,
7575
Revisions: 100,
@@ -96,15 +96,20 @@ type Database struct {
9696
// Any error during creation will cause the program to exit.
9797
func New(config *Config) *Database {
9898
if config == nil {
99-
config = Defaults
99+
log.Crit("firewood: config must be provided")
100100
}
101101

102-
fwConfig, err := validatePath(config)
102+
err := validatePath(config.FilePath)
103103
if err != nil {
104104
log.Crit("firewood: error validating config", "error", err)
105105
}
106106

107-
fw, err := ffi.New(config.FilePath, fwConfig)
107+
fw, err := ffi.New(config.FilePath, &ffi.Config{
108+
NodeCacheEntries: uint(config.CleanCacheSize) / 256, // TODO: estimate 256 bytes per node
109+
FreeListCacheEntries: config.FreeListCacheEntries,
110+
Revisions: config.Revisions,
111+
ReadCacheStrategy: config.ReadCacheStrategy,
112+
})
108113
if err != nil {
109114
log.Crit("firewood: error creating firewood database", "error", err)
110115
}
@@ -123,33 +128,25 @@ func New(config *Config) *Database {
123128
}
124129
}
125130

126-
func validatePath(trieConfig *Config) (*ffi.Config, error) {
127-
if trieConfig.FilePath == "" {
128-
return nil, errors.New("firewood database file path must be set")
131+
func validatePath(path string) error {
132+
if path == "" {
133+
return errors.New("firewood database file path must be set")
129134
}
130135

131136
// Check that the directory exists
132-
dir := filepath.Dir(trieConfig.FilePath)
137+
dir := filepath.Dir(path)
133138
_, err := os.Stat(dir)
134-
if err != nil {
135-
if !os.IsNotExist(err) {
136-
return nil, fmt.Errorf("error checking database directory: %w", err)
137-
}
138-
log.Info("Database directory not found, creating", "path", dir)
139-
if err := os.MkdirAll(dir, 0o755); err != nil {
140-
return nil, fmt.Errorf("error creating database directory: %w", err)
141-
}
139+
if err == nil {
140+
return nil // Directory exists
142141
}
143-
144-
// Create the Firewood config from the provided config.
145-
config := &ffi.Config{
146-
NodeCacheEntries: uint(trieConfig.CleanCacheSize) / 256, // TODO: estimate 256 bytes per node
147-
FreeListCacheEntries: trieConfig.FreeListCacheEntries,
148-
Revisions: trieConfig.Revisions,
149-
ReadCacheStrategy: trieConfig.ReadCacheStrategy,
142+
if !os.IsNotExist(err) {
143+
return fmt.Errorf("error checking database directory: %w", err)
150144
}
151-
152-
return config, nil
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+
return nil
153150
}
154151

155152
// Scheme returns the scheme of the database.

0 commit comments

Comments
 (0)