Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/state: state reader abstraction #29761

Merged
merged 8 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
all: get rid of disk for state.Database construction
  • Loading branch information
rjl493456442 committed Sep 4, 2024
commit 21a7f43f99d127d66ca84a1737dce548ca21938b
2 changes: 1 addition & 1 deletion cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_sethead_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 3 additions & 8 deletions core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions core/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down Expand Up @@ -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}

Expand Down
2 changes: 1 addition & 1 deletion core/state/statedb_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
18 changes: 9 additions & 9 deletions core/state/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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++ {
Expand Down Expand Up @@ -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")
)
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions core/state/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion core/state/trie_prefetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down
3 changes: 1 addition & 2 deletions core/stateless.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions eth/api_debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions eth/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/state_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}
}
Expand Down
7 changes: 7 additions & 0 deletions triedb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -109,6 +110,7 @@ func NewDatabase(diskdb ethdb.Database, config *Config) *Database {
preimages = newPreimageStore(diskdb)
}
db := &Database{
disk: diskdb,
config: config,
preimages: preimages,
}
Expand Down Expand Up @@ -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.
rjl493456442 marked this conversation as resolved.
Show resolved Hide resolved
func (db *Database) Disk() ethdb.Database {
return db.disk
}