Skip to content

Commit 409a8e0

Browse files
committed
all: introduce state reader interface
1 parent f59d013 commit 409a8e0

33 files changed

+589
-355
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
372372
}
373373
// Re-create statedb instance with new root upon the updated database
374374
// for accessing latest states.
375-
statedb, err = state.New(root, statedb.Database(), nil)
375+
statedb, err = state.New(root, statedb.Database())
376376
if err != nil {
377377
return nil, nil, nil, NewError(ErrorEVM, fmt.Errorf("could not reopen state: %v", err))
378378
}
@@ -381,8 +381,9 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
381381
}
382382

383383
func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB {
384-
sdb := state.NewDatabaseWithConfig(db, &triedb.Config{Preimages: true})
385-
statedb, _ := state.New(types.EmptyRootHash, sdb, nil)
384+
tdb := triedb.NewDatabase(db, &triedb.Config{Preimages: true})
385+
sdb := state.NewDatabase(db, tdb, nil)
386+
statedb, _ := state.New(types.EmptyRootHash, sdb)
386387
for addr, a := range accounts {
387388
statedb.SetCode(addr, a.Code)
388389
statedb.SetNonce(addr, a.Nonce)
@@ -393,7 +394,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB
393394
}
394395
// Commit and re-open to start with a clean state.
395396
root, _ := statedb.Commit(0, false)
396-
statedb, _ = state.New(root, sdb, nil)
397+
statedb, _ = state.New(root, sdb)
397398
return statedb
398399
}
399400

cmd/evm/runner.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ func runCmd(ctx *cli.Context) error {
155155
})
156156
defer triedb.Close()
157157
genesis := genesisConfig.MustCommit(db, triedb)
158-
sdb := state.NewDatabaseWithNodeDB(db, triedb)
159-
statedb, _ = state.New(genesis.Root(), sdb, nil)
158+
sdb := state.NewDatabase(db, triedb, nil)
159+
statedb, _ = state.New(genesis.Root(), sdb)
160160
chainConfig = genesisConfig.Config
161161

162162
if ctx.String(SenderFlag.Name) != "" {
@@ -277,7 +277,7 @@ func runCmd(ctx *cli.Context) error {
277277
fmt.Printf("Failed to commit changes %v\n", err)
278278
return err
279279
}
280-
dumpdb, err := state.New(root, sdb, nil)
280+
dumpdb, err := state.New(root, sdb)
281281
if err != nil {
282282
fmt.Printf("Failed to open statedb %v\n", err)
283283
return err

cmd/evm/staterunner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func runStateTest(fname string, cfg vm.Config, dump bool) error {
107107
result.Root = &root
108108
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%#x\"}\n", root)
109109
if dump { // Dump any state to aid debugging
110-
cpy, _ := state.New(root, tstate.StateDB.Database(), nil)
110+
cpy, _ := state.New(root, tstate.StateDB.Database())
111111
dump := cpy.RawDump(nil)
112112
result.State = &dump
113113
}

cmd/geth/chaincmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ func dump(ctx *cli.Context) error {
586586
triedb := utils.MakeTrieDatabase(ctx, db, true, true, false) // always enable preimage lookup
587587
defer triedb.Close()
588588

589-
state, err := state.New(root, state.NewDatabaseWithNodeDB(db, triedb), nil)
589+
state, err := state.New(root, state.NewDatabase(db, triedb, nil))
590590
if err != nil {
591591
return err
592592
}

core/blockchain.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ type BlockChain struct {
217217
lastWrite uint64 // Last block when the state was flushed
218218
flushInterval atomic.Int64 // Time interval (processing time) after which to flush a state
219219
triedb *triedb.Database // The database handler for maintaining trie nodes.
220-
stateCache state.Database // State database to reuse between imports (contains state cache)
220+
stateDb *state.CachingDB // State database to reuse between imports (contains state cache)
221221
txIndexer *txIndexer // Transaction indexer, might be nil if not enabled
222222

223223
hc *HeaderChain
@@ -310,7 +310,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
310310
}
311311
bc.flushInterval.Store(int64(cacheConfig.TrieTimeLimit))
312312
bc.forker = NewForkChoice(bc, shouldPreserve)
313-
bc.stateCache = state.NewDatabaseWithNodeDB(bc.db, bc.triedb)
313+
bc.stateDb = state.NewDatabase(bc.db, bc.triedb, nil)
314314
bc.validator = NewBlockValidator(chainConfig, bc)
315315
bc.prefetcher = newStatePrefetcher(chainConfig, bc.hc)
316316
bc.processor = NewStateProcessor(chainConfig, bc.hc)
@@ -448,7 +448,13 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
448448
AsyncBuild: !bc.cacheConfig.SnapshotWait,
449449
}
450450
bc.snaps, _ = snapshot.New(snapconfig, bc.db, bc.triedb, head.Root)
451+
452+
// Register the snapshot into statedb. It's an ugly hack as state snapshot
453+
// is constructed after stateDb. TODO(rjl493456442) improve the construction
454+
// logic.
455+
bc.stateDb.SetSnapshot(bc.snaps)
451456
}
457+
452458
// Rewind the chain in case of an incompatible config upgrade.
453459
if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
454460
log.Warn("Rewinding chain to upgrade configuration", "err", compat)
@@ -1800,7 +1806,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
18001806
if parent == nil {
18011807
parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
18021808
}
1803-
statedb, err := state.New(parent.Root, bc.stateCache, bc.snaps)
1809+
statedb, err := state.New(parent.Root, bc.stateDb)
18041810
if err != nil {
18051811
return it.index, err
18061812
}
@@ -1826,7 +1832,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
18261832
var followupInterrupt atomic.Bool
18271833
if !bc.cacheConfig.TrieCleanNoPrefetch {
18281834
if followup, err := it.peek(); followup != nil && err == nil {
1829-
throwaway, _ := state.New(parent.Root, bc.stateCache, bc.snaps)
1835+
throwaway, _ := state.New(parent.Root, bc.stateDb)
18301836

18311837
go func(start time.Time, followup *types.Block, throwaway *state.StateDB) {
18321838
// Disable tracing for prefetcher executions.

core/blockchain_reader.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func (bc *BlockChain) GetTd(hash common.Hash, number uint64) *big.Int {
308308

309309
// HasState checks if state trie is fully present in the database or not.
310310
func (bc *BlockChain) HasState(hash common.Hash) bool {
311-
_, err := bc.stateCache.OpenTrie(hash)
311+
_, err := bc.stateDb.OpenTrie(hash)
312312
return err == nil
313313
}
314314

@@ -341,12 +341,9 @@ func (bc *BlockChain) stateRecoverable(root common.Hash) bool {
341341
// If the code doesn't exist in the in-memory cache, check the storage with
342342
// new code scheme.
343343
func (bc *BlockChain) ContractCodeWithPrefix(hash common.Hash) ([]byte, error) {
344-
type codeReader interface {
345-
ContractCodeWithPrefix(address common.Address, codeHash common.Hash) ([]byte, error)
346-
}
347344
// TODO(rjl493456442) The associated account address is also required
348345
// in Verkle scheme. Fix it once snap-sync is supported for Verkle.
349-
return bc.stateCache.(codeReader).ContractCodeWithPrefix(common.Address{}, hash)
346+
return bc.stateDb.ContractCodeWithPrefix(common.Address{}, hash)
350347
}
351348

352349
// State returns a new mutable state based on the current HEAD block.
@@ -356,7 +353,7 @@ func (bc *BlockChain) State() (*state.StateDB, error) {
356353

357354
// StateAt returns a new mutable state based on a particular point in time.
358355
func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
359-
return state.New(root, bc.stateCache, bc.snaps)
356+
return state.New(root, bc.stateDb)
360357
}
361358

362359
// Config retrieves the chain's fork configuration.
@@ -382,7 +379,7 @@ func (bc *BlockChain) Processor() Processor {
382379

383380
// StateCache returns the caching database underpinning the blockchain instance.
384381
func (bc *BlockChain) StateCache() state.Database {
385-
return bc.stateCache
382+
return bc.stateDb
386383
}
387384

388385
// GasLimit returns the gas limit of the current HEAD block.

core/blockchain_sethead_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2040,7 +2040,7 @@ func testSetHeadWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme
20402040
dbconfig.HashDB = hashdb.Defaults
20412041
}
20422042
chain.triedb = triedb.NewDatabase(chain.db, dbconfig)
2043-
chain.stateCache = state.NewDatabaseWithNodeDB(chain.db, chain.triedb)
2043+
chain.stateDb = state.NewDatabase(chain.db, chain.triedb, chain.snaps)
20442044

20452045
// Force run a freeze cycle
20462046
type freezer interface {

core/blockchain_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
159159
}
160160
return err
161161
}
162-
statedb, err := state.New(blockchain.GetBlockByHash(block.ParentHash()).Root(), blockchain.stateCache, nil)
162+
statedb, err := state.New(blockchain.GetBlockByHash(block.ParentHash()).Root(), blockchain.stateDb)
163163
if err != nil {
164164
return err
165165
}

core/chain_makers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
368368
defer triedb.Close()
369369

370370
for i := 0; i < n; i++ {
371-
statedb, err := state.New(parent.Root(), state.NewDatabaseWithNodeDB(db, triedb), nil)
371+
statedb, err := state.New(parent.Root(), state.NewDatabase(db, triedb, nil))
372372
if err != nil {
373373
panic(err)
374374
}
@@ -475,7 +475,7 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
475475
}
476476

477477
for i := 0; i < n; i++ {
478-
statedb, err := state.New(parent.Root(), state.NewDatabaseWithNodeDB(db, trdb), nil)
478+
statedb, err := state.New(parent.Root(), state.NewDatabase(db, trdb, nil))
479479
if err != nil {
480480
panic(err)
481481
}

core/genesis.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ func hashAlloc(ga *types.GenesisAlloc, isVerkle bool) (common.Hash, error) {
127127
}
128128
// Create an ephemeral in-memory database for computing hash,
129129
// all the derived states will be discarded to not pollute disk.
130-
db := state.NewDatabaseWithConfig(rawdb.NewMemoryDatabase(), config)
131-
statedb, err := state.New(types.EmptyRootHash, db, nil)
130+
db := rawdb.NewMemoryDatabase()
131+
statedb, err := state.New(types.EmptyRootHash, state.NewDatabase(db, triedb.NewDatabase(db, config), nil))
132132
if err != nil {
133133
return common.Hash{}, err
134134
}
@@ -149,7 +149,7 @@ func hashAlloc(ga *types.GenesisAlloc, isVerkle bool) (common.Hash, error) {
149149
// states will be persisted into the given database. Also, the genesis state
150150
// specification will be flushed as well.
151151
func flushAlloc(ga *types.GenesisAlloc, db ethdb.Database, triedb *triedb.Database, blockhash common.Hash) error {
152-
statedb, err := state.New(types.EmptyRootHash, state.NewDatabaseWithNodeDB(db, triedb), nil)
152+
statedb, err := state.New(types.EmptyRootHash, state.NewDatabase(db, triedb, nil))
153153
if err != nil {
154154
return err
155155
}

0 commit comments

Comments
 (0)