Skip to content

Commit

Permalink
Don't write cache on shutdown (#317)
Browse files Browse the repository at this point in the history
* don't save on shutdown

* remove test
  • Loading branch information
patrick-ogrady authored Oct 25, 2022
1 parent eb9ac63 commit 641ea69
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 75 deletions.
57 changes: 0 additions & 57 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"fmt"
"math/big"
"testing"
"time"

"github.com/VictoriaMetrics/fastcache"
"github.com/ava-labs/subnet-evm/consensus/dummy"
"github.com/ava-labs/subnet-evm/core/rawdb"
"github.com/ava-labs/subnet-evm/core/state"
Expand All @@ -18,10 +16,8 @@ import (
"github.com/ava-labs/subnet-evm/core/vm"
"github.com/ava-labs/subnet-evm/ethdb"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/trie"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
)

var (
Expand Down Expand Up @@ -644,56 +640,3 @@ func TestCanonicalHashMarker(t *testing.T) {
}
}
}

func TestCleanCacheJournal(t *testing.T) {
chainDB := rawdb.NewMemoryDatabase()
gspec := &Genesis{
Config: &params.ChainConfig{HomesteadBlock: new(big.Int)},
Alloc: GenesisAlloc{
common.Address{1}: {Balance: big.NewInt(1)}, // non-zero alloc needed to cause trie writes.
},
}
genesisBlock := gspec.MustCommit(chainDB)
require.NotNil(t, genesisBlock)
journal := t.TempDir()

blockchain, err := createBlockChain(
chainDB,
&CacheConfig{
TrieCleanLimit: 64, // Smallest possible non-zero size (for a faster test).
TrieDirtyLimit: 256,
TrieDirtyCommitTarget: 20,
Pruning: true, // Enable pruning
CommitInterval: 4096,
SnapshotLimit: 256,
AcceptorQueueLimit: 64,
TrieCleanRejournal: 1 * time.Minute, // Must be non-zero to enable journaling.
TrieCleanJournal: journal,
},
gspec.Config,
common.Hash{})
require.NoError(t, err)
blockchain.Stop() // this causes the cache to be written.

// Load cache and verify it is not empty.
cache, err := fastcache.LoadFromFile(journal)
require.NoError(t, err)
stats := fastcache.Stats{}
cache.UpdateStats(&stats)
require.NotZero(t, stats.EntriesCount)

// The cache should contain the full state trie (since it is small enough).
trieDB := trie.NewDatabase(chainDB)
tr, err := trie.New(common.Hash{}, genesisBlock.Root(), trieDB)
require.NoError(t, err)
it := tr.NodeIterator(nil)
nodeCount := 0
for it.Next(true) {
if it.Leaf() {
continue // leaf nodes do not have a hash
}
require.True(t, cache.Has(it.Hash().Bytes()))
nodeCount++
}
require.NotZero(t, nodeCount)
}
18 changes: 0 additions & 18 deletions trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"fmt"
"io"
"reflect"
"runtime"
"sync"
"time"

Expand Down Expand Up @@ -921,16 +920,6 @@ func (db *Database) saveCache(dir string, threads int) error {
return nil
}

// SaveCache atomically saves fast cache data to the given dir using half
// available CPU cores.
func (db *Database) SaveCache(dir string) error {
concurrency := runtime.GOMAXPROCS(0) / 2
if concurrency == 0 {
concurrency = 1
}
return db.saveCache(dir, concurrency)
}

// SaveCachePeriodically atomically saves fast cache data to the given dir with
// the specified interval. All dump operation will only use a single CPU core.
func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, stopCh <-chan struct{}) {
Expand All @@ -942,13 +931,6 @@ func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, st
case <-ticker.C:
db.saveCache(dir, 1)
case <-stopCh:
// Write the latest contents of the cache to disk after receiving a stop request.
// Note: this is different than geth, which requires an explicit
// call to save the cache on shutdown.
err := db.SaveCache(dir)
if err != nil {
log.Warn("Failed to save cache after stop requested", "err", err)
}
return
}
}
Expand Down

0 comments on commit 641ea69

Please sign in to comment.