mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
all: get rid of disk for state.Database construction
This commit is contained in:
parent
6d4ac503f9
commit
21a7f43f99
19 changed files with 46 additions and 45 deletions
|
|
@ -413,7 +413,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
|
|||
|
||||
func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB {
|
||||
tdb := triedb.NewDatabase(db, &triedb.Config{Preimages: true})
|
||||
sdb := state.NewDatabase(db, tdb, nil)
|
||||
sdb := state.NewDatabase(tdb, nil)
|
||||
statedb, _ := state.New(types.EmptyRootHash, sdb)
|
||||
for addr, a := range accounts {
|
||||
statedb.SetCode(addr, a.Code)
|
||||
|
|
|
|||
|
|
@ -155,7 +155,7 @@ func runCmd(ctx *cli.Context) error {
|
|||
})
|
||||
defer triedb.Close()
|
||||
genesis := genesisConfig.MustCommit(db, triedb)
|
||||
sdb := state.NewDatabase(db, triedb, nil)
|
||||
sdb := state.NewDatabase(triedb, nil)
|
||||
statedb, _ = state.New(genesis.Root(), sdb)
|
||||
chainConfig = genesisConfig.Config
|
||||
|
||||
|
|
|
|||
|
|
@ -584,7 +584,7 @@ func dump(ctx *cli.Context) error {
|
|||
triedb := utils.MakeTrieDatabase(ctx, db, true, true, false) // always enable preimage lookup
|
||||
defer triedb.Close()
|
||||
|
||||
state, err := state.New(root, state.NewDatabase(db, triedb, nil))
|
||||
state, err := state.New(root, state.NewDatabase(triedb, nil))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -308,7 +308,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
|
|||
return nil, err
|
||||
}
|
||||
bc.flushInterval.Store(int64(cacheConfig.TrieTimeLimit))
|
||||
bc.stateDb = state.NewDatabase(bc.db, bc.triedb, nil)
|
||||
bc.stateDb = state.NewDatabase(bc.triedb, nil)
|
||||
bc.validator = NewBlockValidator(chainConfig, bc)
|
||||
bc.prefetcher = newStatePrefetcher(chainConfig, bc.hc)
|
||||
bc.processor = NewStateProcessor(chainConfig, bc.hc)
|
||||
|
|
@ -448,7 +448,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
|
|||
bc.snaps, _ = snapshot.New(snapconfig, bc.db, bc.triedb, head.Root)
|
||||
|
||||
// Re-initialize the state database with snapshot
|
||||
bc.stateDb = state.NewDatabase(bc.db, bc.triedb, bc.snaps)
|
||||
bc.stateDb = state.NewDatabase(bc.triedb, bc.snaps)
|
||||
}
|
||||
|
||||
// Rewind the chain in case of an incompatible config upgrade.
|
||||
|
|
|
|||
|
|
@ -2040,7 +2040,7 @@ func testSetHeadWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme
|
|||
dbconfig.HashDB = hashdb.Defaults
|
||||
}
|
||||
chain.triedb = triedb.NewDatabase(chain.db, dbconfig)
|
||||
chain.stateDb = state.NewDatabase(chain.db, chain.triedb, chain.snaps)
|
||||
chain.stateDb = state.NewDatabase(chain.triedb, chain.snaps)
|
||||
|
||||
// Force run a freeze cycle
|
||||
type freezer interface {
|
||||
|
|
|
|||
|
|
@ -379,7 +379,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
|
|||
defer triedb.Close()
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
statedb, err := state.New(parent.Root(), state.NewDatabase(db, triedb, nil))
|
||||
statedb, err := state.New(parent.Root(), state.NewDatabase(triedb, nil))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
@ -485,7 +485,7 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
|
|||
}
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
statedb, err := state.New(parent.Root(), state.NewDatabase(db, trdb, nil))
|
||||
statedb, err := state.New(parent.Root(), state.NewDatabase(trdb, nil))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -128,7 +128,7 @@ func hashAlloc(ga *types.GenesisAlloc, isVerkle bool) (common.Hash, error) {
|
|||
// Create an ephemeral in-memory database for computing hash,
|
||||
// all the derived states will be discarded to not pollute disk.
|
||||
db := rawdb.NewMemoryDatabase()
|
||||
statedb, err := state.New(types.EmptyRootHash, state.NewDatabase(db, triedb.NewDatabase(db, config), nil))
|
||||
statedb, err := state.New(types.EmptyRootHash, state.NewDatabase(triedb.NewDatabase(db, config), nil))
|
||||
if err != nil {
|
||||
return common.Hash{}, err
|
||||
}
|
||||
|
|
@ -147,8 +147,8 @@ func hashAlloc(ga *types.GenesisAlloc, isVerkle bool) (common.Hash, error) {
|
|||
|
||||
// flushAlloc is very similar with hash, but the main difference is all the
|
||||
// generated states will be persisted into the given database.
|
||||
func flushAlloc(ga *types.GenesisAlloc, db ethdb.Database, triedb *triedb.Database) (common.Hash, error) {
|
||||
statedb, err := state.New(types.EmptyRootHash, state.NewDatabase(db, triedb, nil))
|
||||
func flushAlloc(ga *types.GenesisAlloc, triedb *triedb.Database) (common.Hash, error) {
|
||||
statedb, err := state.New(types.EmptyRootHash, state.NewDatabase(triedb, nil))
|
||||
if err != nil {
|
||||
return common.Hash{}, err
|
||||
}
|
||||
|
|
@ -497,7 +497,7 @@ func (g *Genesis) Commit(db ethdb.Database, triedb *triedb.Database) (*types.Blo
|
|||
return nil, errors.New("can't start clique chain without signers")
|
||||
}
|
||||
// flush the data to disk and compute the state root
|
||||
root, err := flushAlloc(&g.Alloc, db, triedb)
|
||||
root, err := flushAlloc(&g.Alloc, triedb)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -164,9 +164,9 @@ type CachingDB struct {
|
|||
}
|
||||
|
||||
// NewDatabase creates a state database with the provided data sources.
|
||||
func NewDatabase(disk ethdb.Database, triedb *triedb.Database, snap *snapshot.Tree) *CachingDB {
|
||||
func NewDatabase(triedb *triedb.Database, snap *snapshot.Tree) *CachingDB {
|
||||
return &CachingDB{
|
||||
disk: disk,
|
||||
disk: triedb.Disk(),
|
||||
triedb: triedb,
|
||||
snap: snap,
|
||||
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
|
||||
|
|
@ -178,7 +178,7 @@ func NewDatabase(disk ethdb.Database, triedb *triedb.Database, snap *snapshot.Tr
|
|||
// NewDatabaseForTesting is similar to NewDatabase, but it sets up the different
|
||||
// data sources using the same provided database with default config for testing.
|
||||
func NewDatabaseForTesting(db ethdb.Database) *CachingDB {
|
||||
return NewDatabase(db, triedb.NewDatabase(db, nil), nil)
|
||||
return NewDatabase(triedb.NewDatabase(db, nil), nil)
|
||||
}
|
||||
|
||||
// Reader returns a state reader associated with the specified state root.
|
||||
|
|
@ -293,11 +293,6 @@ func (db *CachingDB) Snapshot() *snapshot.Tree {
|
|||
return db.snap
|
||||
}
|
||||
|
||||
// SetSnapshot sets the provided state snapshot.
|
||||
func (db *CachingDB) SetSnapshot(snap *snapshot.Tree) {
|
||||
db.snap = snap
|
||||
}
|
||||
|
||||
// mustCopyTrie returns a deep-copied trie.
|
||||
func mustCopyTrie(t Trie) Trie {
|
||||
switch t := t.(type) {
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ func newStateEnv() *stateEnv {
|
|||
func TestDump(t *testing.T) {
|
||||
db := rawdb.NewMemoryDatabase()
|
||||
triedb := triedb.NewDatabase(db, &triedb.Config{Preimages: true})
|
||||
tdb := NewDatabase(db, triedb, nil)
|
||||
tdb := NewDatabase(triedb, nil)
|
||||
sdb, _ := New(types.EmptyRootHash, tdb)
|
||||
s := &stateEnv{db: db, state: sdb}
|
||||
|
||||
|
|
@ -103,7 +103,7 @@ func TestDump(t *testing.T) {
|
|||
func TestIterativeDump(t *testing.T) {
|
||||
db := rawdb.NewMemoryDatabase()
|
||||
triedb := triedb.NewDatabase(db, &triedb.Config{Preimages: true})
|
||||
tdb := NewDatabase(db, triedb, nil)
|
||||
tdb := NewDatabase(triedb, nil)
|
||||
sdb, _ := New(types.EmptyRootHash, tdb)
|
||||
s := &stateEnv{db: db, state: sdb}
|
||||
|
||||
|
|
|
|||
|
|
@ -216,7 +216,7 @@ func (test *stateTest) run() bool {
|
|||
if i != 0 {
|
||||
root = roots[len(roots)-1]
|
||||
}
|
||||
state, err := New(root, NewDatabase(disk, tdb, snaps))
|
||||
state, err := New(root, NewDatabase(tdb, snaps))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ func TestUpdateLeaks(t *testing.T) {
|
|||
var (
|
||||
db = rawdb.NewMemoryDatabase()
|
||||
tdb = triedb.NewDatabase(db, nil)
|
||||
sdb = NewDatabase(db, tdb, nil)
|
||||
sdb = NewDatabase(tdb, nil)
|
||||
)
|
||||
state, _ := New(types.EmptyRootHash, sdb)
|
||||
|
||||
|
|
@ -90,8 +90,8 @@ func TestIntermediateLeaks(t *testing.T) {
|
|||
finalDb := rawdb.NewMemoryDatabase()
|
||||
transNdb := triedb.NewDatabase(transDb, nil)
|
||||
finalNdb := triedb.NewDatabase(finalDb, nil)
|
||||
transState, _ := New(types.EmptyRootHash, NewDatabase(transDb, transNdb, nil))
|
||||
finalState, _ := New(types.EmptyRootHash, NewDatabase(finalDb, finalNdb, nil))
|
||||
transState, _ := New(types.EmptyRootHash, NewDatabase(transNdb, nil))
|
||||
finalState, _ := New(types.EmptyRootHash, NewDatabase(finalNdb, nil))
|
||||
|
||||
modify := func(state *StateDB, addr common.Address, i, tweak byte) {
|
||||
state.SetBalance(addr, uint256.NewInt(uint64(11*i)+uint64(tweak)), tracing.BalanceChangeUnspecified)
|
||||
|
|
@ -989,7 +989,7 @@ func testMissingTrieNodes(t *testing.T, scheme string) {
|
|||
CleanCacheSize: 0,
|
||||
}}) // disable caching
|
||||
}
|
||||
db := NewDatabase(memDb, tdb, nil)
|
||||
db := NewDatabase(tdb, nil)
|
||||
|
||||
var root common.Hash
|
||||
state, _ := New(types.EmptyRootHash, db)
|
||||
|
|
@ -1211,7 +1211,7 @@ func TestFlushOrderDataLoss(t *testing.T) {
|
|||
var (
|
||||
memdb = rawdb.NewMemoryDatabase()
|
||||
triedb = triedb.NewDatabase(memdb, triedb.HashDefaults)
|
||||
statedb = NewDatabase(memdb, triedb, nil)
|
||||
statedb = NewDatabase(triedb, nil)
|
||||
state, _ = New(types.EmptyRootHash, statedb)
|
||||
)
|
||||
for a := byte(0); a < 10; a++ {
|
||||
|
|
@ -1284,7 +1284,7 @@ func TestDeleteStorage(t *testing.T) {
|
|||
disk = rawdb.NewMemoryDatabase()
|
||||
tdb = triedb.NewDatabase(disk, nil)
|
||||
snaps, _ = snapshot.New(snapshot.Config{CacheSize: 10}, disk, tdb, types.EmptyRootHash)
|
||||
db = NewDatabase(disk, tdb, snaps)
|
||||
db = NewDatabase(tdb, snaps)
|
||||
state, _ = New(types.EmptyRootHash, db)
|
||||
addr = common.HexToAddress("0x1")
|
||||
)
|
||||
|
|
@ -1299,8 +1299,8 @@ func TestDeleteStorage(t *testing.T) {
|
|||
root, _ := state.Commit(0, true)
|
||||
|
||||
// Init phase done, create two states, one with snap and one without
|
||||
fastState, _ := New(root, NewDatabase(disk, tdb, snaps))
|
||||
slowState, _ := New(root, NewDatabase(disk, tdb, nil))
|
||||
fastState, _ := New(root, NewDatabase(tdb, snaps))
|
||||
slowState, _ := New(root, NewDatabase(tdb, nil))
|
||||
|
||||
obj := fastState.getOrNewStateObject(addr)
|
||||
storageRoot := obj.data.Root
|
||||
|
|
@ -1338,7 +1338,7 @@ func TestStorageDirtiness(t *testing.T) {
|
|||
var (
|
||||
disk = rawdb.NewMemoryDatabase()
|
||||
tdb = triedb.NewDatabase(disk, nil)
|
||||
db = NewDatabase(disk, tdb, nil)
|
||||
db = NewDatabase(tdb, nil)
|
||||
state, _ = New(types.EmptyRootHash, db)
|
||||
addr = common.HexToAddress("0x1")
|
||||
checkDirty = func(key common.Hash, value common.Hash, dirty bool) {
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ func makeTestState(scheme string) (ethdb.Database, Database, *triedb.Database, c
|
|||
}
|
||||
db := rawdb.NewMemoryDatabase()
|
||||
nodeDb := triedb.NewDatabase(db, config)
|
||||
sdb := NewDatabase(db, nodeDb, nil)
|
||||
sdb := NewDatabase(nodeDb, nil)
|
||||
state, _ := New(types.EmptyRootHash, sdb)
|
||||
|
||||
// Fill it with some arbitrary data
|
||||
|
|
@ -94,7 +94,7 @@ func checkStateAccounts(t *testing.T, db ethdb.Database, scheme string, root com
|
|||
config.PathDB = pathdb.Defaults
|
||||
}
|
||||
// Check root availability and state contents
|
||||
state, err := New(root, NewDatabase(db, triedb.NewDatabase(db, &config), nil))
|
||||
state, err := New(root, NewDatabase(triedb.NewDatabase(db, &config), nil))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create state trie at %x: %v", root, err)
|
||||
}
|
||||
|
|
@ -120,7 +120,7 @@ func checkStateConsistency(db ethdb.Database, scheme string, root common.Hash) e
|
|||
if scheme == rawdb.PathScheme {
|
||||
config.PathDB = pathdb.Defaults
|
||||
}
|
||||
state, err := New(root, NewDatabase(db, triedb.NewDatabase(db, config), nil))
|
||||
state, err := New(root, NewDatabase(triedb.NewDatabase(db, config), nil))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ func TestUseAfterTerminate(t *testing.T) {
|
|||
func TestVerklePrefetcher(t *testing.T) {
|
||||
disk := rawdb.NewMemoryDatabase()
|
||||
db := triedb.NewDatabase(disk, triedb.VerkleDefaults)
|
||||
sdb := NewDatabase(disk, db, nil)
|
||||
sdb := NewDatabase(db, nil)
|
||||
|
||||
state, err := New(types.EmptyRootHash, sdb)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -610,7 +610,7 @@ func TestProcessParentBlockHash(t *testing.T) {
|
|||
cacheConfig := DefaultCacheConfigWithScheme(rawdb.PathScheme)
|
||||
cacheConfig.SnapshotLimit = 0
|
||||
triedb := triedb.NewDatabase(db, cacheConfig.triedbConfig(true))
|
||||
statedb, _ := state.New(types.EmptyVerkleHash, state.NewDatabase(db, triedb, nil))
|
||||
statedb, _ := state.New(types.EmptyVerkleHash, state.NewDatabase(triedb, nil))
|
||||
test(statedb)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,8 +42,7 @@ import (
|
|||
func ExecuteStateless(config *params.ChainConfig, witness *stateless.Witness) (common.Hash, common.Hash, error) {
|
||||
// Create and populate the state database to serve as the stateless backend
|
||||
memdb := witness.MakeHashDB()
|
||||
|
||||
db, err := state.New(witness.Root(), state.NewDatabase(memdb, triedb.NewDatabase(memdb, triedb.HashDefaults), nil))
|
||||
db, err := state.New(witness.Root(), state.NewDatabase(triedb.NewDatabase(memdb, triedb.HashDefaults), nil))
|
||||
if err != nil {
|
||||
return common.Hash{}, common.Hash{}, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ func TestAccountRange(t *testing.T) {
|
|||
|
||||
var (
|
||||
mdb = rawdb.NewMemoryDatabase()
|
||||
statedb = state.NewDatabase(mdb, triedb.NewDatabase(mdb, &triedb.Config{Preimages: true}), nil)
|
||||
statedb = state.NewDatabase(triedb.NewDatabase(mdb, &triedb.Config{Preimages: true}), nil)
|
||||
sdb, _ = state.New(types.EmptyRootHash, statedb)
|
||||
addrs = [AccountRangeMaxResults * 2]common.Address{}
|
||||
m = map[common.Address]bool{}
|
||||
|
|
@ -164,7 +164,7 @@ func TestStorageRangeAt(t *testing.T) {
|
|||
var (
|
||||
mdb = rawdb.NewMemoryDatabase()
|
||||
tdb = triedb.NewDatabase(mdb, &triedb.Config{Preimages: true})
|
||||
db = state.NewDatabase(mdb, tdb, nil)
|
||||
db = state.NewDatabase(tdb, nil)
|
||||
sdb, _ = state.New(types.EmptyRootHash, db)
|
||||
addr = common.Address{0x01}
|
||||
keys = []common.Hash{ // hashes of Keys of storage
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec u
|
|||
// TODO(rjl493456442), clean cache is disabled to prevent memory leak,
|
||||
// please re-enable it for better performance.
|
||||
tdb := triedb.NewDatabase(eth.chainDb, triedb.HashDefaults)
|
||||
database = state.NewDatabase(eth.chainDb, tdb, nil)
|
||||
database = state.NewDatabase(tdb, nil)
|
||||
if statedb, err = state.New(block.Root(), database); err == nil {
|
||||
log.Info("Found disk backend for state trie", "root", block.Root(), "number", block.Number())
|
||||
return statedb, noopReleaser, nil
|
||||
|
|
@ -87,7 +87,7 @@ func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec u
|
|||
// TODO(rjl493456442), clean cache is disabled to prevent memory leak,
|
||||
// please re-enable it for better performance.
|
||||
tdb = triedb.NewDatabase(eth.chainDb, triedb.HashDefaults)
|
||||
database = state.NewDatabase(eth.chainDb, tdb, nil)
|
||||
database = state.NewDatabase(tdb, nil)
|
||||
|
||||
// If we didn't check the live database, do check state over ephemeral database,
|
||||
// otherwise we would rewind past a persisted block (specific corner case is
|
||||
|
|
|
|||
|
|
@ -462,7 +462,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc, snapshotter bo
|
|||
tconf.PathDB = pathdb.Defaults
|
||||
}
|
||||
triedb := triedb.NewDatabase(db, tconf)
|
||||
sdb := state.NewDatabase(db, triedb, nil)
|
||||
sdb := state.NewDatabase(triedb, nil)
|
||||
statedb, _ := state.New(types.EmptyRootHash, sdb)
|
||||
for addr, a := range accounts {
|
||||
statedb.SetCode(addr, a.Code)
|
||||
|
|
@ -486,7 +486,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc, snapshotter bo
|
|||
}
|
||||
snaps, _ = snapshot.New(snapconfig, db, triedb, root)
|
||||
}
|
||||
sdb = state.NewDatabase(db, triedb, snaps)
|
||||
sdb = state.NewDatabase(triedb, snaps)
|
||||
statedb, _ = state.New(root, sdb)
|
||||
return StateTestState{statedb, triedb, snaps}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,6 +92,7 @@ type backend interface {
|
|||
// types of node backend as an entrypoint. It's responsible for all interactions
|
||||
// relevant with trie nodes and node preimages.
|
||||
type Database struct {
|
||||
disk ethdb.Database
|
||||
config *Config // Configuration for trie database
|
||||
preimages *preimageStore // The store for caching preimages
|
||||
backend backend // The backend for managing trie nodes
|
||||
|
|
@ -109,6 +110,7 @@ func NewDatabase(diskdb ethdb.Database, config *Config) *Database {
|
|||
preimages = newPreimageStore(diskdb)
|
||||
}
|
||||
db := &Database{
|
||||
disk: diskdb,
|
||||
config: config,
|
||||
preimages: preimages,
|
||||
}
|
||||
|
|
@ -327,3 +329,8 @@ func (db *Database) SetBufferSize(size int) error {
|
|||
func (db *Database) IsVerkle() bool {
|
||||
return db.config.IsVerkle
|
||||
}
|
||||
|
||||
// Disk returns the underlying disk.
|
||||
func (db *Database) Disk() ethdb.Database {
|
||||
return db.disk
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue