2024-08-09 12:29:47 +03:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
versa "github.com/bnb-chain/versioned-state-database"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/lru"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
|
|
|
"github.com/ethereum/go-ethereum/triedb"
|
|
|
|
)
|
|
|
|
|
|
|
|
type cachingVersaDB struct {
|
|
|
|
triedb *triedb.Database
|
|
|
|
versionDB versa.Database
|
|
|
|
codeDB ethdb.KeyValueStore
|
|
|
|
codeSizeCache *lru.Cache[common.Hash, int]
|
|
|
|
codeCache *lru.SizeConstrainedCache[common.Hash, []byte]
|
|
|
|
|
|
|
|
accTree *VersaTree
|
|
|
|
state versa.StateHandler
|
|
|
|
root common.Hash
|
|
|
|
mode versa.StateMode
|
|
|
|
hasState atomic.Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewVersaDatabase should be call by NewDatabaseWithNodeDB
|
|
|
|
// TODO:: NewDatabaseWithNodeDB should add mode param.
|
|
|
|
func NewVersaDatabase(db ethdb.Database, triedb *triedb.Database, mode versa.StateMode) Database {
|
|
|
|
return &cachingVersaDB{
|
|
|
|
triedb: triedb,
|
|
|
|
versionDB: triedb.VersaDB(),
|
|
|
|
codeDB: db,
|
|
|
|
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
|
|
|
|
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
|
2024-08-12 05:35:13 +03:00
|
|
|
mode: mode,
|
2024-08-13 12:32:01 +03:00
|
|
|
state: versa.ErrStateHandler,
|
2024-08-09 12:29:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 12:30:21 +03:00
|
|
|
func (cv *cachingVersaDB) Copy() Database {
|
|
|
|
cp := &cachingVersaDB{}
|
|
|
|
cp.codeCache = cv.codeCache
|
|
|
|
cp.codeSizeCache = cv.codeSizeCache
|
|
|
|
cp.triedb = cv.triedb
|
|
|
|
cp.versionDB = cv.versionDB
|
|
|
|
cp.codeDB = cv.codeDB
|
|
|
|
cp.mode = versa.S_RW // it is important
|
|
|
|
|
|
|
|
// TODO:: maybe add lock for cv.root
|
|
|
|
if cv.hasState.Load() {
|
|
|
|
_, err := cp.OpenTrie(cv.root)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to open trie in copy caching versa db", "error", err)
|
|
|
|
return cp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cp
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyTrie is used with Copy()
|
|
|
|
func (cv *cachingVersaDB) CopyTrie(tr Trie) Trie {
|
|
|
|
vtr, ok := tr.(*VersaTree)
|
|
|
|
if !ok {
|
|
|
|
panic("caching versa db copy non versa tree")
|
|
|
|
}
|
|
|
|
if vtr.accountTree {
|
|
|
|
if cv.accTree != nil {
|
2024-08-14 11:32:58 +03:00
|
|
|
if cv.accTree.root.Cmp(vtr.root) != 0 {
|
|
|
|
panic("copy acc trie mismatch")
|
|
|
|
}
|
2024-08-12 12:30:21 +03:00
|
|
|
return cv.accTree
|
|
|
|
}
|
|
|
|
tree, err := cv.OpenTrie(vtr.root)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to open trie in CopyTrie", "error", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return tree
|
|
|
|
} else {
|
|
|
|
tree, err := cv.OpenStorageTrie(vtr.stateRoot, vtr.address, vtr.root, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to open storage trie in CopyTrie", "error", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return tree
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-12 12:58:22 +03:00
|
|
|
func (cv *cachingVersaDB) HasState(root common.Hash) bool {
|
|
|
|
return cv.versionDB.HasState(root)
|
|
|
|
}
|
|
|
|
|
2024-08-09 12:29:47 +03:00
|
|
|
func (cv *cachingVersaDB) OpenTrie(root common.Hash) (Trie, error) {
|
|
|
|
if cv.hasState.Load() {
|
|
|
|
//TODO:: will change to log.Error after stabilization
|
|
|
|
panic("account tree has open")
|
|
|
|
}
|
|
|
|
|
2024-08-12 10:26:45 +03:00
|
|
|
// TODO:: if root tree, versa db should ignore check version, temp use -1
|
|
|
|
state, err := cv.versionDB.OpenState(-1, root, cv.mode)
|
2024-08-09 12:29:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-08-12 12:30:21 +03:00
|
|
|
handler, err := cv.versionDB.OpenTree(state, -1, common.Hash{}, root)
|
2024-08-09 12:29:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tree := &VersaTree{
|
|
|
|
db: cv.versionDB,
|
|
|
|
handler: handler,
|
|
|
|
accountTree: true,
|
|
|
|
root: root,
|
2024-08-15 05:28:48 +03:00
|
|
|
mode: cv.mode,
|
2024-08-09 12:29:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cv.state = state
|
|
|
|
cv.hasState.Store(true) // if set, can't change
|
|
|
|
cv.accTree = tree
|
|
|
|
cv.root = root
|
|
|
|
|
|
|
|
return tree, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cv *cachingVersaDB) OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash, _ Trie) (Trie, error) {
|
|
|
|
if !cv.hasState.Load() {
|
|
|
|
//TODO:: will change to log.Error after stabilization
|
|
|
|
panic("open account tree, before open storage tree")
|
|
|
|
}
|
2024-08-12 14:40:53 +03:00
|
|
|
if cv.root.Cmp(stateRoot) != 0 {
|
|
|
|
panic(fmt.Sprintf("account root mismatch, on open storage tree, actual: %s, expect: %s", root.String(), cv.root.String()))
|
2024-08-09 12:29:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
version, account, err := cv.accTree.getAccountWithVersion(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-12 14:40:53 +03:00
|
|
|
if account.Root.Cmp(root) != 0 {
|
2024-08-09 12:29:47 +03:00
|
|
|
return nil, fmt.Errorf("state root mismatch")
|
|
|
|
}
|
|
|
|
|
2024-08-12 14:40:53 +03:00
|
|
|
handler, err := cv.versionDB.OpenTree(cv.state, version, crypto.Keccak256Hash(address.Bytes()), root)
|
2024-08-09 12:29:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tree := &VersaTree{
|
|
|
|
db: cv.versionDB,
|
|
|
|
handler: handler,
|
|
|
|
version: version,
|
|
|
|
root: root,
|
|
|
|
stateRoot: stateRoot,
|
|
|
|
address: address,
|
2024-08-15 05:28:48 +03:00
|
|
|
mode: cv.mode,
|
2024-08-09 12:29:47 +03:00
|
|
|
}
|
|
|
|
return tree, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush unique to versa
|
|
|
|
func (cv *cachingVersaDB) Flush() error {
|
|
|
|
return cv.versionDB.Flush(cv.state)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release unique to versa
|
|
|
|
func (cv *cachingVersaDB) Release() error {
|
2024-08-13 13:21:32 +03:00
|
|
|
//log.Info("close state", "state info", cv.versionDB.ParseStateHandler(cv.state))
|
2024-08-09 12:29:47 +03:00
|
|
|
if err := cv.versionDB.CloseState(cv.state); err != nil {
|
2024-08-12 05:35:13 +03:00
|
|
|
return err
|
2024-08-09 12:29:47 +03:00
|
|
|
}
|
2024-08-12 05:35:13 +03:00
|
|
|
cv.hasState.Store(false)
|
|
|
|
cv.accTree = nil
|
|
|
|
cv.state = versa.ErrStateHandler
|
|
|
|
cv.root = common.Hash{}
|
2024-08-09 12:29:47 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-12 05:35:13 +03:00
|
|
|
func (cv *cachingVersaDB) Reset() {
|
|
|
|
if cv.state != versa.ErrStateHandler {
|
2024-08-13 15:20:39 +03:00
|
|
|
panic(fmt.Sprintf("close state reset, state info %s", cv.versionDB.ParseStateHandler(cv.state)))
|
2024-08-12 05:35:13 +03:00
|
|
|
}
|
2024-08-09 12:29:47 +03:00
|
|
|
cv.hasState.Store(false)
|
|
|
|
cv.accTree = nil
|
2024-08-12 05:35:13 +03:00
|
|
|
cv.state = versa.ErrStateHandler
|
|
|
|
cv.root = common.Hash{}
|
|
|
|
}
|
|
|
|
|
2024-08-14 04:32:57 +03:00
|
|
|
func (cv *cachingVersaDB) HasTreeExpired(tr Trie) bool {
|
2024-08-13 12:06:23 +03:00
|
|
|
vtr, ok := tr.(*VersaTree)
|
|
|
|
if !ok {
|
|
|
|
panic("trie type mismatch")
|
|
|
|
}
|
2024-08-14 04:32:57 +03:00
|
|
|
return cv.versionDB.HasTreeExpired(vtr.handler)
|
2024-08-13 12:06:23 +03:00
|
|
|
}
|
|
|
|
|
2024-08-12 05:35:13 +03:00
|
|
|
func (cv *cachingVersaDB) Scheme() string {
|
|
|
|
return cv.triedb.Scheme()
|
2024-08-09 12:29:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cv *cachingVersaDB) ContractCode(addr common.Address, codeHash common.Hash) ([]byte, error) {
|
|
|
|
code, _ := cv.codeCache.Get(codeHash)
|
|
|
|
if len(code) > 0 {
|
|
|
|
return code, nil
|
|
|
|
}
|
|
|
|
code = rawdb.ReadCodeWithPrefix(cv.codeDB, codeHash)
|
|
|
|
if len(code) > 0 {
|
|
|
|
cv.codeCache.Add(codeHash, code)
|
|
|
|
cv.codeSizeCache.Add(codeHash, len(code))
|
|
|
|
return code, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cv *cachingVersaDB) ContractCodeSize(addr common.Address, codeHash common.Hash) (int, error) {
|
|
|
|
if cached, ok := cv.codeSizeCache.Get(codeHash); ok {
|
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
code, err := cv.ContractCode(addr, codeHash)
|
|
|
|
return len(code), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cv *cachingVersaDB) ContractCodeWithPrefix(address common.Address, codeHash common.Hash) ([]byte, error) {
|
|
|
|
code, _ := cv.codeCache.Get(codeHash)
|
|
|
|
if len(code) > 0 {
|
|
|
|
return code, nil
|
|
|
|
}
|
|
|
|
code = rawdb.ReadCodeWithPrefix(cv.codeDB, codeHash)
|
|
|
|
if len(code) > 0 {
|
|
|
|
cv.codeCache.Add(codeHash, code)
|
|
|
|
cv.codeSizeCache.Add(codeHash, len(code))
|
|
|
|
return code, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cv *cachingVersaDB) DiskDB() ethdb.KeyValueStore {
|
|
|
|
return cv.codeDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cv *cachingVersaDB) TrieDB() *triedb.Database {
|
|
|
|
return cv.triedb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cv *cachingVersaDB) NoTries() bool {
|
|
|
|
// TODO:: not support fastnode
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type VersaTree struct {
|
|
|
|
db versa.Database
|
|
|
|
handler versa.TreeHandler
|
|
|
|
version int64
|
|
|
|
accountTree bool
|
|
|
|
|
|
|
|
// TODO:: debugging, used for logging
|
|
|
|
stateRoot common.Hash
|
2024-08-12 14:40:53 +03:00
|
|
|
root common.Hash
|
2024-08-09 12:29:47 +03:00
|
|
|
address common.Address
|
2024-08-15 05:28:48 +03:00
|
|
|
mode versa.StateMode
|
2024-08-09 12:29:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vt *VersaTree) GetKey(key []byte) []byte {
|
|
|
|
_, val, err := vt.db.Get(vt.handler, key)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to get key from version db")
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vt *VersaTree) GetAccount(address common.Address) (*types.StateAccount, error) {
|
2024-08-15 05:28:48 +03:00
|
|
|
ver, res, err := vt.getAccountWithVersion(address)
|
|
|
|
if err == nil && res != nil {
|
|
|
|
log.Info("get account", "mode", vt.mode, "addr", address.String(), "nonce", res.Nonce, "balance", res.Balance, "root", res.Root.String(), "code", common.Bytes2Hex(res.CodeHash), "version", ver)
|
|
|
|
}
|
2024-08-09 12:29:47 +03:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vt *VersaTree) getAccountWithVersion(address common.Address) (int64, *types.StateAccount, error) {
|
|
|
|
vt.CheckAccountTree()
|
|
|
|
ver, res, err := vt.db.Get(vt.handler, address.Bytes())
|
|
|
|
if res == nil || err != nil {
|
|
|
|
return ver, nil, err
|
|
|
|
}
|
|
|
|
ret := new(types.StateAccount)
|
|
|
|
err = rlp.DecodeBytes(res, ret)
|
|
|
|
return ver, ret, err
|
|
|
|
}
|
|
|
|
|
2024-08-15 05:28:48 +03:00
|
|
|
func (vt *VersaTree) GetStorage(address common.Address, key []byte) ([]byte, error) {
|
|
|
|
if vt.address.Cmp(address) != 0 {
|
|
|
|
panic(fmt.Sprintf("address mismatch in get storage, expect: %s, actul: %s", vt.address.String(), address.String()))
|
|
|
|
}
|
2024-08-09 12:29:47 +03:00
|
|
|
vt.CheckStorageTree()
|
2024-08-13 15:35:17 +03:00
|
|
|
_, enc, err := vt.db.Get(vt.handler, key)
|
|
|
|
if err != nil || len(enc) == 0 {
|
2024-08-09 12:29:47 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-13 15:35:17 +03:00
|
|
|
_, content, _, err := rlp.Split(enc)
|
2024-08-15 05:28:48 +03:00
|
|
|
log.Info("get storage", "mode", vt.mode, "handler", vt.handler, "owner", address.String(), "key", common.Bytes2Hex(key), "val", common.Bytes2Hex(content), "stateRoot", vt.stateRoot.String(), "root", vt.root.String(), "version", vt.version)
|
2024-08-13 15:35:17 +03:00
|
|
|
return content, err
|
2024-08-09 12:29:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vt *VersaTree) UpdateAccount(address common.Address, account *types.StateAccount) error {
|
|
|
|
vt.CheckAccountTree()
|
|
|
|
data, err := rlp.EncodeToBytes(account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-15 05:28:48 +03:00
|
|
|
log.Info("update account", "mode", vt.mode, "addr", address.String(), "nonce", account.Nonce, "balance", account.Balance, "root", account.Root.String(), "code", common.Bytes2Hex(account.CodeHash))
|
2024-08-09 12:29:47 +03:00
|
|
|
return vt.db.Put(vt.handler, address.Bytes(), data)
|
|
|
|
}
|
|
|
|
|
2024-08-15 05:28:48 +03:00
|
|
|
func (vt *VersaTree) UpdateStorage(address common.Address, key, value []byte) error {
|
|
|
|
if vt.address.Cmp(address) != 0 {
|
|
|
|
panic(fmt.Sprintf("address mismatch in get storage, expect: %s, actul: %s", vt.address.String(), address.String()))
|
|
|
|
}
|
2024-08-09 12:29:47 +03:00
|
|
|
vt.CheckStorageTree()
|
2024-08-13 05:57:42 +03:00
|
|
|
v, _ := rlp.EncodeToBytes(value)
|
2024-08-15 05:28:48 +03:00
|
|
|
log.Info("update storage", "mode", vt.mode, "handler", vt.handler, "owner", address.String(), "key", common.Bytes2Hex(key), "val", common.Bytes2Hex(value), "stateRoot", vt.stateRoot.String(), "root", vt.root.String())
|
2024-08-13 05:57:42 +03:00
|
|
|
return vt.db.Put(vt.handler, key, v)
|
2024-08-09 12:29:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vt *VersaTree) DeleteAccount(address common.Address) error {
|
|
|
|
vt.CheckAccountTree()
|
|
|
|
return vt.db.Delete(vt.handler, address.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vt *VersaTree) DeleteStorage(_ common.Address, key []byte) error {
|
|
|
|
vt.CheckStorageTree()
|
|
|
|
return vt.db.Delete(vt.handler, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vt *VersaTree) UpdateContractCode(address common.Address, codeHash common.Hash, code []byte) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vt *VersaTree) Hash() common.Hash {
|
|
|
|
hash, err := vt.db.CalcRootHash(vt.handler)
|
|
|
|
if err != nil {
|
2024-08-13 05:57:42 +03:00
|
|
|
// TODO:: debug code, will be change to log error
|
2024-08-13 10:30:45 +03:00
|
|
|
log.Crit("calc tree root hash", "tree handler info", vt.db.ParseTreeHandler(vt.handler), "error", err.Error())
|
2024-08-09 12:29:47 +03:00
|
|
|
}
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vt *VersaTree) Commit(_ bool) (common.Hash, *trienode.NodeSet, error) {
|
|
|
|
hash, err := vt.db.Commit(vt.handler)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to commit versa tree", "error", err)
|
|
|
|
}
|
2024-08-12 05:35:13 +03:00
|
|
|
return hash, nil, err
|
2024-08-09 12:29:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vt *VersaTree) NodeIterator(startKey []byte) (trie.NodeIterator, error) {
|
|
|
|
panic("versa tree not support iterate")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vt *VersaTree) Prove(key []byte, proofDb ethdb.KeyValueWriter) error {
|
|
|
|
panic("versa tree not support prove")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-14 04:32:57 +03:00
|
|
|
// TODO:: debug code, will be deleted after stabilization
|
2024-08-09 12:29:47 +03:00
|
|
|
func (vt *VersaTree) CheckAccountTree() {
|
|
|
|
if !vt.accountTree {
|
|
|
|
panic("sub tree can't operate account")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 04:32:57 +03:00
|
|
|
// TODO:: debug code, will be deleted after stabilization
|
2024-08-09 12:29:47 +03:00
|
|
|
func (vt *VersaTree) CheckStorageTree() {
|
|
|
|
if vt.accountTree {
|
|
|
|
panic("root tree can't operate storage")
|
|
|
|
}
|
|
|
|
}
|