chore: add influence for versadb

This commit is contained in:
joeycli 2024-08-07 17:52:55 +08:00
parent 83a9b13771
commit ba71e76c55
16 changed files with 37 additions and 1 deletions

@ -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 // Re-create statedb instance with new root upon the updated database
// for accessing latest states. // for accessing latest states.
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, err = state.New(root, statedb.Database(), nil) statedb, err = state.New(root, statedb.Database(), nil)
if err != nil { if err != nil {
return nil, nil, nil, NewError(ErrorEVM, fmt.Errorf("could not reopen state: %v", err)) return nil, nil, nil, NewError(ErrorEVM, fmt.Errorf("could not reopen state: %v", err))
@ -358,7 +359,9 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
} }
func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB { 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}) sdb := state.NewDatabaseWithConfig(db, &triedb.Config{Preimages: true})
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, _ := state.New(types.EmptyRootHash, sdb, nil) statedb, _ := state.New(types.EmptyRootHash, sdb, nil)
for addr, a := range accounts { for addr, a := range accounts {
statedb.SetCode(addr, a.Code) statedb.SetCode(addr, a.Code)
@ -372,6 +375,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB
statedb.Finalise(false) statedb.Finalise(false)
statedb.AccountsIntermediateRoot() statedb.AccountsIntermediateRoot()
root, _, _ := statedb.Commit(0, nil) root, _, _ := statedb.Commit(0, nil)
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, _ = state.New(root, sdb, nil) statedb, _ = state.New(root, sdb, nil)
return statedb return statedb
} }

@ -148,12 +148,14 @@ func runCmd(ctx *cli.Context) error {
} }
db := rawdb.NewMemoryDatabase() 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{ triedb := triedb.NewDatabase(db, &triedb.Config{
Preimages: preimages, Preimages: preimages,
HashDB: hashdb.Defaults, HashDB: hashdb.Defaults,
}) })
defer triedb.Close() defer triedb.Close()
genesis := genesisConfig.MustCommit(db, triedb) genesis := genesisConfig.MustCommit(db, triedb)
// TODO:: ignore cmd, internally compatible with versa is sufficient.
sdb := state.NewDatabaseWithNodeDB(db, triedb) sdb := state.NewDatabaseWithNodeDB(db, triedb)
statedb, _ = state.New(genesis.Root(), sdb, nil) statedb, _ = state.New(genesis.Root(), sdb, nil)
chainConfig = genesisConfig.Config chainConfig = genesisConfig.Config

@ -109,6 +109,7 @@ func runStateTest(fname string, cfg vm.Config, jsonOut, dump bool) error {
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%#x\"}\n", root) fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%#x\"}\n", root)
} }
if dump { // Dump any state to aid debugging 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) cpy, _ := state.New(root, tstate.StateDB.Database(), nil)
dump := cpy.RawDump(nil) dump := cpy.RawDump(nil)
result.State = &dump result.State = &dump

@ -778,6 +778,7 @@ func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, eth
} }
} else { } else {
// Use latest // Use latest
// TODO:: if versa, scheme = VersaScheme
if scheme == rawdb.PathScheme { if scheme == rawdb.PathScheme {
triedb := triedb.NewDatabase(db, &triedb.Config{PathDB: utils.PathDBConfigAddJournalFilePath(stack, pathdb.ReadOnly)}) triedb := triedb.NewDatabase(db, &triedb.Config{PathDB: utils.PathDBConfigAddJournalFilePath(stack, pathdb.ReadOnly)})
defer triedb.Close() defer triedb.Close()
@ -833,6 +834,7 @@ func dump(ctx *cli.Context) error {
triedb := utils.MakeTrieDatabase(ctx, stack, db, true, true, false) // always enable preimage lookup triedb := utils.MakeTrieDatabase(ctx, stack, db, true, true, false) // always enable preimage lookup
defer triedb.Close() defer triedb.Close()
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
state, err := state.New(root, state.NewDatabaseWithNodeDB(db, triedb), nil) state, err := state.New(root, state.NewDatabaseWithNodeDB(db, triedb), nil)
if err != nil { if err != nil {
return err return err
@ -850,6 +852,7 @@ func dumpAllRootHashInPath(ctx *cli.Context) error {
defer stack.Close() defer stack.Close()
db := utils.MakeChainDatabase(ctx, stack, true, false) db := utils.MakeChainDatabase(ctx, stack, true, false)
defer db.Close() defer db.Close()
// TODO:: ignore cmd
triedb := triedb.NewDatabase(db, &triedb.Config{PathDB: utils.PathDBConfigAddJournalFilePath(stack, pathdb.ReadOnly)}) triedb := triedb.NewDatabase(db, &triedb.Config{PathDB: utils.PathDBConfigAddJournalFilePath(stack, pathdb.ReadOnly)})
defer triedb.Close() defer triedb.Close()

@ -457,6 +457,7 @@ func inspectTrie(ctx *cli.Context) error {
config = triedb.HashDefaults config = triedb.HashDefaults
} }
// TODO:: ignore cmd
triedb := triedb.NewDatabase(db, config) triedb := triedb.NewDatabase(db, config)
theTrie, err := trie.New(trie.TrieID(trieRootHash), triedb) theTrie, err := trie.New(trie.TrieID(trieRootHash), triedb)
if err != nil { if err != nil {
@ -1268,6 +1269,7 @@ func hbss2pbss(ctx *cli.Context) error {
} }
if lastStateID == 0 || force { if lastStateID == 0 || force {
config := triedb.HashDefaults config := triedb.HashDefaults
// TODO:: ignore cmd
triedb := triedb.NewDatabase(db, config) triedb := triedb.NewDatabase(db, config)
triedb.Cap(0) triedb.Cap(0)
log.Info("hbss2pbss triedb", "scheme", triedb.Scheme()) log.Info("hbss2pbss triedb", "scheme", triedb.Scheme())

@ -256,6 +256,7 @@ func accessDb(ctx *cli.Context, stack *node.Node) (ethdb.Database, error) {
} else if dbScheme == rawdb.HashScheme { } else if dbScheme == rawdb.HashScheme {
config = triedb.HashDefaults config = triedb.HashDefaults
} }
// TODO:: ignore snapshot
snaptree, err := snapshot.New(snapconfig, chaindb, triedb.NewDatabase(chaindb, config), headBlock.Root(), TriesInMemory, false) snaptree, err := snapshot.New(snapconfig, chaindb, triedb.NewDatabase(chaindb, config), headBlock.Root(), TriesInMemory, false)
if err != nil { if err != nil {
log.Error("snaptree error", "err", err) log.Error("snaptree error", "err", err)

@ -2589,6 +2589,7 @@ func MakeTrieDatabase(ctx *cli.Context, stack *node.Node, disk ethdb.Database, p
// ignore the parameter silently. TODO(rjl493456442) // ignore the parameter silently. TODO(rjl493456442)
// please config it if read mode is implemented. // please config it if read mode is implemented.
config.HashDB = hashdb.Defaults config.HashDB = hashdb.Defaults
// TODO::skip MakeTrieDatabase
return triedb.NewDatabase(disk, config) return triedb.NewDatabase(disk, config)
} }
if readOnly { if readOnly {
@ -2597,6 +2598,7 @@ func MakeTrieDatabase(ctx *cli.Context, stack *node.Node, disk ethdb.Database, p
config.PathDB = pathdb.Defaults config.PathDB = pathdb.Defaults
} }
config.PathDB.JournalFilePath = fmt.Sprintf("%s/%s", stack.ResolvePath("chaindata"), eth.JournalFileName) config.PathDB.JournalFilePath = fmt.Sprintf("%s/%s", stack.ResolvePath("chaindata"), eth.JournalFileName)
// TODO::skip MakeTrieDatabase
return triedb.NewDatabase(disk, config) return triedb.NewDatabase(disk, config)
} }

@ -336,6 +336,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
diffLayerChanCache, _ := exlru.New(diffLayerCacheLimit) diffLayerChanCache, _ := exlru.New(diffLayerCacheLimit)
// Open trie database with provided config // Open trie database with provided config
// TODO:: if versa , have its owen init genesis logic
triedb := triedb.NewDatabase(db, cacheConfig.triedbConfig()) triedb := triedb.NewDatabase(db, cacheConfig.triedbConfig())
// Setup the genesis block, commit the provided genesis specification // Setup the genesis block, commit the provided genesis specification
@ -384,6 +385,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
} }
bc.flushInterval.Store(int64(cacheConfig.TrieTimeLimit)) bc.flushInterval.Store(int64(cacheConfig.TrieTimeLimit))
bc.forker = NewForkChoice(bc, shouldPreserve) bc.forker = NewForkChoice(bc, shouldPreserve)
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
bc.stateCache = state.NewDatabaseWithNodeDB(bc.db, bc.triedb) bc.stateCache = state.NewDatabaseWithNodeDB(bc.db, bc.triedb)
bc.validator = NewBlockValidator(chainConfig, bc, engine) bc.validator = NewBlockValidator(chainConfig, bc, engine)
bc.prefetcher = NewStatePrefetcher(chainConfig, bc, engine) bc.prefetcher = NewStatePrefetcher(chainConfig, bc, engine)
@ -2232,6 +2234,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1) 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) statedb, err := state.NewWithSharedPool(parent.Root, bc.stateCache, bc.snaps)
if err != nil { if err != nil {
return it.index, err return it.index, err

@ -396,6 +396,7 @@ func (bc *BlockChain) State() (*state.StateDB, error) {
// StateAt returns a new mutable state based on a particular point in time. // StateAt returns a new mutable state based on a particular point in time.
func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) { 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) stateDb, err := state.New(root, bc.stateCache, bc.snaps)
if err != nil { if err != nil {
return nil, err return nil, err

@ -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. // Forcibly use hash-based state scheme for retaining all nodes in disk.
// TODO:: ignore, only use to UT
triedb := triedb.NewDatabase(db, triedb.HashDefaults) triedb := triedb.NewDatabase(db, triedb.HashDefaults)
defer triedb.Close() defer triedb.Close()
for i := 0; i < n; i++ { 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) statedb, err := state.New(parent.Root(), state.NewDatabaseWithNodeDB(db, triedb), nil)
if err != nil { if err != nil {
panic(err) panic(err)

@ -126,7 +126,9 @@ func hashAlloc(ga *types.GenesisAlloc, isVerkle bool) (common.Hash, error) {
} }
// Create an ephemeral in-memory database for computing hash, // Create an ephemeral in-memory database for computing hash,
// all the derived states will be discarded to not pollute disk. // 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) db := state.NewDatabaseWithConfig(rawdb.NewMemoryDatabase(), config)
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, err := state.New(types.EmptyRootHash, db, nil) statedb, err := state.New(types.EmptyRootHash, db, nil)
if err != nil { if err != nil {
return common.Hash{}, err return common.Hash{}, err
@ -154,6 +156,7 @@ func flushAlloc(ga *types.GenesisAlloc, db ethdb.Database, triedb *triedb.Databa
if triedbConfig != nil { if triedbConfig != nil {
triedbConfig.NoTries = false triedbConfig.NoTries = false
} }
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, err := state.New(types.EmptyRootHash, state.NewDatabaseWithNodeDB(db, triedb), nil) statedb, err := state.New(types.EmptyRootHash, state.NewDatabaseWithNodeDB(db, triedb), nil)
if err != nil { if err != nil {
return err return err

@ -232,6 +232,7 @@ func pruneAll(maindb ethdb.Database, g *core.Genesis) error {
} }
log.Info("Database compaction finished", "elapsed", common.PrettyDuration(time.Since(cstart))) 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) statedb, _ := state.New(common.Hash{}, state.NewDatabase(maindb), nil)
for addr, account := range g.Alloc { for addr, account := range g.Alloc {
statedb.AddBalance(addr, uint256.MustFromBig(account.Balance)) statedb.AddBalance(addr, uint256.MustFromBig(account.Balance))

@ -205,6 +205,8 @@ type Tree struct {
// state trie. // state trie.
// - otherwise, the entire snapshot is considered invalid and will be recreated on // - otherwise, the entire snapshot is considered invalid and will be recreated on
// a background thread. // 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) { func New(config Config, diskdb ethdb.KeyValueStore, triedb *triedb.Database, root common.Hash, cap int, withoutTrie bool) (*Tree, error) {
snap := &Tree{ snap := &Tree{
config: config, config: config,

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

@ -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. // the internal junks created by tracing will be persisted into the disk.
// TODO(rjl493456442), clean cache is disabled to prevent memory leak, // TODO(rjl493456442), clean cache is disabled to prevent memory leak,
// please re-enable it for better performance. // please re-enable it for better performance.
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
database = state.NewDatabaseWithConfig(eth.chainDb, triedb.HashDefaults) 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 { 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()) log.Info("Found disk backend for state trie", "root", block.Root(), "number", block.Number())
return statedb, noopReleaser, nil return statedb, noopReleaser, nil
@ -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. // the internal junks created by tracing will be persisted into the disk.
// TODO(rjl493456442), clean cache is disabled to prevent memory leak, // TODO(rjl493456442), clean cache is disabled to prevent memory leak,
// please re-enable it for better performance. // please re-enable it for better performance.
// TODO:: if use versa, tdb == nil
tdb = triedb.NewDatabase(eth.chainDb, triedb.HashDefaults) tdb = triedb.NewDatabase(eth.chainDb, triedb.HashDefaults)
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
database = state.NewDatabaseWithNodeDB(eth.chainDb, tdb) database = state.NewDatabaseWithNodeDB(eth.chainDb, tdb)
// If we didn't check the live database, do check state over ephemeral database, // 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 // otherwise we would rewind past a persisted block (specific corner case is
// chain tracing from the genesis). // chain tracing from the genesis).
if !readOnly { if !readOnly {
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, err = state.New(current.Root(), database, nil) statedb, err = state.New(current.Root(), database, nil)
if err == nil { if err == nil {
return statedb, noopReleaser, nil return statedb, noopReleaser, nil
@ -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) return nil, nil, fmt.Errorf("missing block %v %d", current.ParentHash(), current.NumberU64()-1)
} }
current = parent current = parent
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, err = state.New(current.Root(), database, nil) statedb, err = state.New(current.Root(), database, nil)
if err == nil { if err == nil {
break break
@ -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", return nil, nil, fmt.Errorf("stateAtBlock commit failed, number %d root %v: %w",
current.NumberU64(), current.Root().Hex(), err) current.NumberU64(), current.Root().Hex(), err)
} }
// TODO:: state.NewDatabase internally compatible with versa is sufficient.
statedb, err = state.New(root, database, nil) // nolint:staticcheck statedb, err = state.New(root, database, nil) // nolint:staticcheck
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("state reset after block %d failed: %v", current.NumberU64(), err) return nil, nil, fmt.Errorf("state reset after block %d failed: %v", current.NumberU64(), err)

@ -245,6 +245,7 @@ type BlockStore interface {
HasSeparateBlockStore() bool HasSeparateBlockStore() bool
} }
// @TODO:: add VersaDB() method
// Database contains all the methods required by the high level database to not // 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. // only access the key-value data store but also the chain freezer.
type Database interface { type Database interface {