Skip to content

Commit

Permalink
chore: add influence for versadb
Browse files Browse the repository at this point in the history
  • Loading branch information
joeylichang committed Aug 7, 2024
1 parent 83a9b13 commit ba71e76
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
}
// Re-create statedb instance with new root upon the updated database
// for accessing latest states.
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, err = state.New(root, statedb.Database(), nil)
if err != nil {
return nil, nil, nil, NewError(ErrorEVM, fmt.Errorf("could not reopen state: %v", err))
Expand All @@ -358,7 +359,9 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
}

func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB {
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
sdb := state.NewDatabaseWithConfig(db, &triedb.Config{Preimages: true})
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, _ := state.New(types.EmptyRootHash, sdb, nil)
for addr, a := range accounts {
statedb.SetCode(addr, a.Code)
Expand All @@ -372,6 +375,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB
statedb.Finalise(false)
statedb.AccountsIntermediateRoot()
root, _, _ := statedb.Commit(0, nil)
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, _ = state.New(root, sdb, nil)
return statedb
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,14 @@ func runCmd(ctx *cli.Context) error {
}

db := rawdb.NewMemoryDatabase()
// TODO:: ignore cmd, if use versa if should be nil, if triedb is not used later in this func
triedb := triedb.NewDatabase(db, &triedb.Config{
Preimages: preimages,
HashDB: hashdb.Defaults,
})
defer triedb.Close()
genesis := genesisConfig.MustCommit(db, triedb)
// TODO:: ignore cmd, internally compatible with versa is sufficient.
sdb := state.NewDatabaseWithNodeDB(db, triedb)
statedb, _ = state.New(genesis.Root(), sdb, nil)
chainConfig = genesisConfig.Config
Expand Down
1 change: 1 addition & 0 deletions cmd/evm/staterunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func runStateTest(fname string, cfg vm.Config, jsonOut, dump bool) error {
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%#x\"}\n", root)
}
if dump { // Dump any state to aid debugging
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
cpy, _ := state.New(root, tstate.StateDB.Database(), nil)
dump := cpy.RawDump(nil)
result.State = &dump
Expand Down
3 changes: 3 additions & 0 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, eth
}
} else {
// Use latest
// TODO:: if versa, scheme = VersaScheme
if scheme == rawdb.PathScheme {
triedb := triedb.NewDatabase(db, &triedb.Config{PathDB: utils.PathDBConfigAddJournalFilePath(stack, pathdb.ReadOnly)})
defer triedb.Close()
Expand Down Expand Up @@ -833,6 +834,7 @@ func dump(ctx *cli.Context) error {
triedb := utils.MakeTrieDatabase(ctx, stack, db, true, true, false) // always enable preimage lookup
defer triedb.Close()

// TODO:: state.NewDatabase internally compatible with versa is sufficient.
state, err := state.New(root, state.NewDatabaseWithNodeDB(db, triedb), nil)
if err != nil {
return err
Expand All @@ -850,6 +852,7 @@ func dumpAllRootHashInPath(ctx *cli.Context) error {
defer stack.Close()
db := utils.MakeChainDatabase(ctx, stack, true, false)
defer db.Close()
// TODO:: ignore cmd
triedb := triedb.NewDatabase(db, &triedb.Config{PathDB: utils.PathDBConfigAddJournalFilePath(stack, pathdb.ReadOnly)})
defer triedb.Close()

Expand Down
2 changes: 2 additions & 0 deletions cmd/geth/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ func inspectTrie(ctx *cli.Context) error {
config = triedb.HashDefaults
}

// TODO:: ignore cmd
triedb := triedb.NewDatabase(db, config)
theTrie, err := trie.New(trie.TrieID(trieRootHash), triedb)
if err != nil {
Expand Down Expand Up @@ -1268,6 +1269,7 @@ func hbss2pbss(ctx *cli.Context) error {
}
if lastStateID == 0 || force {
config := triedb.HashDefaults
// TODO:: ignore cmd
triedb := triedb.NewDatabase(db, config)
triedb.Cap(0)
log.Info("hbss2pbss triedb", "scheme", triedb.Scheme())
Expand Down
1 change: 1 addition & 0 deletions cmd/geth/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func accessDb(ctx *cli.Context, stack *node.Node) (ethdb.Database, error) {
} else if dbScheme == rawdb.HashScheme {
config = triedb.HashDefaults
}
// TODO:: ignore snapshot
snaptree, err := snapshot.New(snapconfig, chaindb, triedb.NewDatabase(chaindb, config), headBlock.Root(), TriesInMemory, false)
if err != nil {
log.Error("snaptree error", "err", err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2589,6 +2589,7 @@ func MakeTrieDatabase(ctx *cli.Context, stack *node.Node, disk ethdb.Database, p
// ignore the parameter silently. TODO(rjl493456442)
// please config it if read mode is implemented.
config.HashDB = hashdb.Defaults
// TODO::skip MakeTrieDatabase
return triedb.NewDatabase(disk, config)
}
if readOnly {
Expand All @@ -2597,6 +2598,7 @@ func MakeTrieDatabase(ctx *cli.Context, stack *node.Node, disk ethdb.Database, p
config.PathDB = pathdb.Defaults
}
config.PathDB.JournalFilePath = fmt.Sprintf("%s/%s", stack.ResolvePath("chaindata"), eth.JournalFileName)
// TODO::skip MakeTrieDatabase
return triedb.NewDatabase(disk, config)
}

Expand Down
3 changes: 3 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
diffLayerChanCache, _ := exlru.New(diffLayerCacheLimit)

// Open trie database with provided config
// TODO:: if versa , have its owen init genesis logic
triedb := triedb.NewDatabase(db, cacheConfig.triedbConfig())

// Setup the genesis block, commit the provided genesis specification
Expand Down Expand Up @@ -384,6 +385,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
}
bc.flushInterval.Store(int64(cacheConfig.TrieTimeLimit))
bc.forker = NewForkChoice(bc, shouldPreserve)
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
bc.stateCache = state.NewDatabaseWithNodeDB(bc.db, bc.triedb)
bc.validator = NewBlockValidator(chainConfig, bc, engine)
bc.prefetcher = NewStatePrefetcher(chainConfig, bc, engine)
Expand Down Expand Up @@ -2232,6 +2234,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
}

// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, err := state.NewWithSharedPool(parent.Root, bc.stateCache, bc.snaps)
if err != nil {
return it.index, err
Expand Down
1 change: 1 addition & 0 deletions core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ func (bc *BlockChain) State() (*state.StateDB, error) {

// StateAt returns a new mutable state based on a particular point in time.
func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
stateDb, err := state.New(root, bc.stateCache, bc.snaps)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,12 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
}

// Forcibly use hash-based state scheme for retaining all nodes in disk.
// TODO:: ignore, only use to UT
triedb := triedb.NewDatabase(db, triedb.HashDefaults)
defer triedb.Close()

for i := 0; i < n; i++ {
// @TODO:: ignore, it used to UT, state.NewDatabase internally compatible with versa is sufficient.
statedb, err := state.New(parent.Root(), state.NewDatabaseWithNodeDB(db, triedb), nil)
if err != nil {
panic(err)
Expand Down
3 changes: 3 additions & 0 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ 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.
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
db := state.NewDatabaseWithConfig(rawdb.NewMemoryDatabase(), config)
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, err := state.New(types.EmptyRootHash, db, nil)
if err != nil {
return common.Hash{}, err
Expand Down Expand Up @@ -154,6 +156,7 @@ func flushAlloc(ga *types.GenesisAlloc, db ethdb.Database, triedb *triedb.Databa
if triedbConfig != nil {
triedbConfig.NoTries = false
}
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, err := state.New(types.EmptyRootHash, state.NewDatabaseWithNodeDB(db, triedb), nil)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions core/state/pruner/pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func pruneAll(maindb ethdb.Database, g *core.Genesis) error {
}
log.Info("Database compaction finished", "elapsed", common.PrettyDuration(time.Since(cstart)))
}
// TODO:: ignore, versa has its own prunner
statedb, _ := state.New(common.Hash{}, state.NewDatabase(maindb), nil)
for addr, account := range g.Alloc {
statedb.AddBalance(addr, uint256.MustFromBig(account.Balance))
Expand Down
2 changes: 2 additions & 0 deletions core/state/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ type Tree struct {
// state trie.
// - otherwise, the entire snapshot is considered invalid and will be recreated on
// a background thread.
//
// TODO:: if use versa, set SnapshotLimit == 0, will forbidden to use
func New(config Config, diskdb ethdb.KeyValueStore, triedb *triedb.Database, root common.Hash, cap int, withoutTrie bool) (*Tree, error) {
snap := &Tree{
config: config,
Expand Down
2 changes: 2 additions & 0 deletions core/vm/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
setDefaults(cfg)

if cfg.State == nil {
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
cfg.State, _ = state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
}
var (
Expand Down Expand Up @@ -149,6 +150,7 @@ func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) {
setDefaults(cfg)

if cfg.State == nil {
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
cfg.State, _ = state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
}
var (
Expand Down
8 changes: 7 additions & 1 deletion eth/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec u
// the internal junks created by tracing will be persisted into the disk.
// TODO(rjl493456442), clean cache is disabled to prevent memory leak,
// please re-enable it for better performance.
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
database = state.NewDatabaseWithConfig(eth.chainDb, triedb.HashDefaults)
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
if statedb, err = state.New(block.Root(), database, nil); err == nil {
log.Info("Found disk backend for state trie", "root", block.Root(), "number", block.Number())
return statedb, noopReleaser, nil
Expand All @@ -91,13 +93,16 @@ func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec u
// the internal junks created by tracing will be persisted into the disk.
// TODO(rjl493456442), clean cache is disabled to prevent memory leak,
// please re-enable it for better performance.
// TODO:: if use versa, tdb == nil
tdb = triedb.NewDatabase(eth.chainDb, triedb.HashDefaults)
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
database = state.NewDatabaseWithNodeDB(eth.chainDb, tdb)

// 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
// chain tracing from the genesis).
if !readOnly {
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, err = state.New(current.Root(), database, nil)
if err == nil {
return statedb, noopReleaser, nil
Expand All @@ -116,7 +121,7 @@ func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec u
return nil, nil, fmt.Errorf("missing block %v %d", current.ParentHash(), current.NumberU64()-1)
}
current = parent

// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, err = state.New(current.Root(), database, nil)
if err == nil {
break
Expand Down Expand Up @@ -164,6 +169,7 @@ func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec u
return nil, nil, fmt.Errorf("stateAtBlock commit failed, number %d root %v: %w",
current.NumberU64(), current.Root().Hex(), err)
}
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, err = state.New(root, database, nil) // nolint:staticcheck
if err != nil {
return nil, nil, fmt.Errorf("state reset after block %d failed: %v", current.NumberU64(), err)
Expand Down
1 change: 1 addition & 0 deletions ethdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ type BlockStore interface {
HasSeparateBlockStore() bool
}

// @TODO:: add VersaDB() method
// Database contains all the methods required by the high level database to not
// only access the key-value data store but also the chain freezer.
type Database interface {
Expand Down

0 comments on commit ba71e76

Please sign in to comment.