Skip to content

Commit

Permalink
stateDB
Browse files Browse the repository at this point in the history
  • Loading branch information
Lawliet-Chan committed May 6, 2024
1 parent 64fff53 commit cf463c0
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions evm/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (
"path/filepath"
)

type State struct {
cfg *Config
stateDB *state.StateDB
trieDB *triedb.Database
snaps *snapshot.Tree
logger *tracing.Hooks
type EthState struct {
cfg *Config
stateCache state.Database
trieDB *triedb.Database
snaps *snapshot.Tree
logger *tracing.Hooks
}

func NewState(cfg *Config) (*State, error) {
func NewState(cfg *Config, currentStateRoot common.Hash) (*EthState, error) {
vmConfig := vm.Config{
EnablePreimageRecording: cfg.EnablePreimageRecording,
}
Expand Down Expand Up @@ -65,23 +65,43 @@ func NewState(cfg *Config) (*State, error) {
trieDB := triedb.NewDatabase(db, trieConfig(cacheCfg, false))
stateCache := state.NewDatabaseWithNodeDB(db, trieDB)

snaps, err := snapshot.New(snapCfg, db, trieDB, types.EmptyRootHash /* current block hash */)
snaps, err := snapshot.New(snapCfg, db, trieDB, currentStateRoot)
if err != nil {
return nil, err
}

statedb, err := state.New(types.EmptyRootHash, stateCache, snaps)
return &EthState{
cfg: cfg,
stateCache: stateCache,
trieDB: trieDB,
snaps: snaps,
logger: vmConfig.Tracer,
}, nil
}

func (s *EthState) GenesisStateDB() (*state.StateDB, error) {
return state.New(types.EmptyRootHash, s.stateCache, nil)
}

func (s *EthState) NewStateDB(parentStateRoot common.Hash) (statedb *state.StateDB, err error) {
statedb, err = state.New(parentStateRoot, s.stateCache, s.snaps)
if err != nil {
return nil, err
return
}
statedb.SetLogger(s.logger)
// Enable prefetching to pull in trie node paths while processing transactions
statedb.StartPrefetcher("chain")
return
}

return &State{
cfg: cfg,
stateDB: statedb,
trieDB: trieDB,
snaps: snaps,
logger: vmConfig.Tracer,
}, nil
func (s *EthState) Commit(blockNum uint64, stateDB *state.StateDB) (common.Hash, error) {
stateDB.StopPrefetcher()
stateRoot, err := stateDB.Commit(blockNum, true)
if err != nil {
return common.Hash{}, err
}
err = s.trieDB.Commit(stateRoot, true)
return stateRoot, err
}

func trieConfig(c *core.CacheConfig, isVerkle bool) *triedb.Config {
Expand Down Expand Up @@ -130,12 +150,3 @@ func snapsConfig(cfg *Config) snapshot.Config {
AsyncBuild: !cfg.SnapshotWait,
}
}

func (s *State) Commit(blockNum uint64) (common.Hash, error) {
stateRoot, err := s.stateDB.Commit(blockNum, true)
if err != nil {
return common.Hash{}, err
}
err = s.trieDB.Commit(stateRoot, true)
return stateRoot, err
}

0 comments on commit cf463c0

Please sign in to comment.