mirror of
https://github.com/luxfi/evm.git
synced 2026-09-24 00:27:44 +00:00
Don't write cache on shutdown (#317)
* don't save on shutdown * remove test
This commit is contained in:
@@ -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"
|
||||
@@ -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 (
|
||||
@@ -644,56 +640,3 @@ func TestCanonicalHashMarker(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanCacheJournal(t *testing.T) {
|
||||
chainDB := rawdb.NewMemoryDatabase()
|
||||
gspec := &Genesis{
|
||||
Config: ¶ms.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)
|
||||
}
|
||||
|
||||
@@ -31,7 +31,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -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{}) {
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user