Compare commits

...

1 Commits

Author SHA1 Message Date
Rick975
d33ca4f108 initial version 2024-08-07 10:23:51 +08:00
7 changed files with 234 additions and 3 deletions

@ -20,6 +20,7 @@ package core
import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/triedb/versadb"
"io"
"math/big"
"runtime"
@ -196,6 +197,11 @@ func (c *CacheConfig) triedbConfig() *triedb.Config {
JournalFile: c.JournalFile,
}
}
if c.StateScheme == rawdb.VersaScheme {
config.VersaDB = &versadb.Config{
CleanCacheSize: c.TrieCleanLimit * 1024 * 1024,
}
}
return config
}

@ -46,6 +46,8 @@ const HashScheme = "hash"
// on extra state diffs to survive deep reorg.
const PathScheme = "path"
const VersaScheme = "versa"
// hasher is used to compute the sha256 hash of the provided data.
type hasher struct{ sha crypto.KeccakState }

@ -195,6 +195,10 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
if db.triedb.IsVerkle() {
return trie.NewVerkleTrie(root, db.triedb, utils.NewPointCache(commitmentCacheItems))
}
// TODO, trie handler instead of tree pointer
if db.triedb.IsVersionedState() {
return trie.NewVersionTrie(root, db.triedb)
}
tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb)
if err != nil {
return nil, err
@ -214,6 +218,10 @@ func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Addre
if db.triedb.IsVerkle() {
return self, nil
}
// TODO
if db.triedb.IsVersionedState() {
return trie.NewVersionTrie(root, db.triedb)
}
tr, err := trie.NewStateTrie(trie.StorageTrieID(stateRoot, crypto.Keccak256Hash(address.Bytes()), root), db.triedb)
if err != nil {
return nil, err

@ -23,6 +23,7 @@ type ID struct {
StateRoot common.Hash // The root of the corresponding state(block.root)
Owner common.Hash // The contract address hash which the trie belongs to
Root common.Hash // The root hash of trie
Version uint64 // The version of a Versa tree
}
// StateTrieID constructs an identifier for state trie with the provided state root.

119
trie/version_trie.go Normal file

@ -0,0 +1,119 @@
package trie
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/triedb/database"
)
// VersionTrie is a wrapper around version state that implements the trie.Trie
// interface so that version trees can be reused verbatim.
type VersionTrie struct {
db database.Database
reader *trieReader
//tree TreeHandler
}
// NewVersionTrie constructs a version state tree based on the specified root hash.
func NewVersionTrie(root common.Hash, db database.Database) (*VersionTrie, error) {
reader, err := newTrieReader(root, common.Hash{}, db)
if err != nil {
return nil, err
}
// TODO
// Open a tree
// tree, err := OpenTree(state StateHandler, version int64, owner common.Hash, root common.Hash)
if err != nil {
return nil, err
}
return &VersionTrie{
db: db,
reader: reader,
// tree: tree,
}, nil
}
// GetKey returns the sha3 preimage of a hashed key that was previously used
// to store a value.
func (t *VersionTrie) GetKey(key []byte) []byte {
return key
}
// GetAccount implements state.Trie, retrieving the account with the specified
// account address. If the specified account is not in the tree, nil will
// be returned. If the tree is corrupted, an error will be returned.
func (t *VersionTrie) GetAccount(address common.Address) (*types.StateAccount, error) {
// TODO
}
// GetStorage returns the value for key stored in the trie. The value bytes
// must not be modified by the caller. If a node was not found in the database,
// a trie.MissingNodeError is returned.
func (t *VersionTrie) GetStorage(addr common.Address, key []byte) ([]byte, error) {
// TODO
}
// UpdateAccount abstracts an account write to the trie. It encodes the
// provided account object with associated algorithm and then updates it
// in the trie with provided address.
func (t *VersionTrie) UpdateAccount(address common.Address, account *types.StateAccount) error {
// TODO
}
// UpdateStorage associates key with value in the trie. If value has length zero,
// any existing value is deleted from the trie. The value bytes must not be modified
// by the caller while they are stored in the trie. If a node was not found in the
// database, a trie.MissingNodeError is returned.
func (t *VersionTrie) UpdateStorage(addr common.Address, key, value []byte) error {
// TODO
}
// DeleteAccount abstracts an account deletion from the trie.
func (t *VersionTrie) DeleteAccount(address common.Address) error {
// TODO
}
// DeleteStorage removes any existing value for key from the trie. If a node
// was not found in the database, a trie.MissingNodeError is returned.
func (t *VersionTrie) DeleteStorage(addr common.Address, key []byte) error {
// TODO
}
// UpdateContractCode abstracts code write to the trie. It is expected
// to be moved to the stateWriter interface when the latter is ready.
func (t *VersionTrie) UpdateContractCode(address common.Address, codeHash common.Hash, code []byte) error {
// TODO
}
// Hash returns the root hash of the trie. It does not write to the database and
// can be used even if the trie doesn't have one.
func (t *VersionTrie) Hash() common.Hash {
// TODO
}
// Commit collects all dirty nodes in the trie and replace them with the
// corresponding node hash.
func (t *VersionTrie) Commit(collectLeaf bool) (common.Hash, error) {
// TODO
}
// NodeIterator returns an iterator that returns nodes of the trie. Iteration
// starts at the key after the given start key. And error will be returned
// if fails to create node iterator.
func (t *VersionTrie) NodeIterator(startKey []byte) (trie.NodeIterator, error) {
// TODO
}
// Prove constructs a Merkle proof for key. The result contains all encoded nodes
// on the path to the value at key. The value itself is also included in the last
// node and can be retrieved by verifying the proof.
//
// If the trie does not contain a value for key, the returned proof contains all
// nodes of the longest existing prefix of the key (at least the root), ending
// with the node that proves the absence of the key.
func (t *VersionTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error {
// TODO
}

@ -18,6 +18,7 @@ package triedb
import (
"errors"
"github.com/ethereum/go-ethereum/triedb/versadb"
"strings"
"github.com/ethereum/go-ethereum/common"
@ -38,8 +39,10 @@ type Config struct {
Cache int
NoTries bool
IsVerkle bool // Flag whether the db is holding a verkle tree
IsVersa bool
HashDB *hashdb.Config // Configs for hash-based scheme
PathDB *pathdb.Config // Configs for experimental path-based scheme
VersaDB *versadb.Config // TODO, Richard
}
// HashDefaults represents a config for using hash-based scheme with
@ -148,6 +151,11 @@ func NewDatabase(diskdb ethdb.Database, config *Config) *Database {
config.PathDB = pathdb.Defaults
}
db.backend = pathdb.New(triediskdb, config.PathDB)
} else if strings.Compare(dbScheme, rawdb.VersaScheme) == 0 {
if config.VersaDB == nil {
config.VersaDB = versadb.Defaults
}
db.backend = versadb.New(triediskdb, config.VersaDB)
} else {
var resolver hashdb.ChildResolver
if config.IsVerkle {
@ -409,3 +417,8 @@ func (db *Database) GetAllRooHash() [][]string {
func (db *Database) IsVerkle() bool {
return db.config.IsVerkle
}
// IsVersionedState returns the indicator if the database is holding a versioned state.
func (db *Database) IsVersionedState() bool {
return db.config.IsVersa
}

@ -0,0 +1,82 @@
package versadb
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/trie/trienode"
"github.com/ethereum/go-ethereum/trie/triestate"
"sync"
)
const (
// maxDiffLayers is the maximum diff layers allowed in the layer tree.
maxDiffLayers = 128
)
// Config contains the settings for database.
type Config struct {
CleanCacheSize int // Maximum memory allowance (in bytes) for caching clean nodes
}
// Defaults is the default setting for database if it's not specified. ,
var Defaults = &Config{
CleanCacheSize: 0,
}
type Database struct {
// readOnly is the flag whether the mutation is allowed to be applied.
// It will be set automatically when the database is journaled during
// the shutdown to reject all following unexpected mutations.
readOnly bool // Flag if database is opened in read only mode
config *Config // Configuration for database
lock sync.RWMutex // Lock to prevent mutations from happening at the same time
}
// New initializes the version state database.
func New(diskdb ethdb.Database, config *Config) *Database {
if config == nil {
config = Defaults
}
// TODO
db := VersaDB.NewVersaDB(nil)
return db
}
// Scheme returns the identifier of used storage scheme.
func (db *Database) Scheme() string {
return rawdb.VersaScheme
}
// Initialized returns an indicator if the state data is already initialized
// according to the state scheme.
func (db *Database) Initialized(genesisRoot common.Hash) bool {
// TODO
}
// Size returns the current storage size of the memory cache in front of the
// persistent database layer.
func (db *Database) Size() (common.StorageSize, common.StorageSize, common.StorageSize) {
// TODO
}
// Update performs a state transition by committing dirty nodes contained
// in the given set in order to update state from the specified parent to
// the specified root.
//
// The passed in maps(nodes, states) will be retained to avoid copying
// everything. Therefore, these maps must not be changed afterwards.
func (db *Database) Update(root common.Hash, parent common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set) error {
// TODO
}
// Commit writes all relevant trie nodes belonging to the specified state
// to disk. Report specifies whether logs will be displayed in info level.
func (db *Database) Commit(root common.Hash, report bool) error {
// TODO
}
// Close closes the trie database backend and releases all held resources.
func (db *Database) Close() error {
// TODO
}