State Trie exposed to plugins

The StateTrie interface is exposed via triewrapper.go and then implemented via two methods appended to the backend object.
This commit is contained in:
philip-morlier 2023-04-07 10:39:10 -07:00
parent f92264d342
commit 5470afdeb8
2 changed files with 46 additions and 88 deletions

View File

@ -11,13 +11,12 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
gcore "github.com/ethereum/go-ethereum/core"
// "github.com/ethereum/go-ethereum/core/state"
// "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/log"
// "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
@ -464,16 +463,29 @@ func (b *Backend) ChainConfig() *params.ChainConfig {
return b.chainConfig
}
// func (b *Backend) GetTrie(h core.Hash) state.Trie {
// return NewWrappedTrie(trie.New(trie.TrieID(common.Hash(h)), trie.NewDatabase(b.b.ChainDb())))
// }
func (b *Backend) GetTrie(h core.Hash) (core.Trie, error) {
tr, err := trie.NewStateTrie(trie.TrieID(common.Hash(h)), trie.NewDatabase(b.b.ChainDb()))
if err != nil {
return nil, err
}
return NewWrappedTrie(tr), nil
}
// func (b *Backend) GetAccountTrie(stateRoot core.Hash, account core.Address) (state.Trie, error) {
// tr := b.GetTrie(common.Hash(stateRoot))
// act := tr.TryGetAccount(common.Address(account))
// stTr, _ := trie.NewStateTrie(trie.StorageTrieID(common.Hash(stateRoot), crypto.Keccak256Hash(account[:]), act.Root), b.b.ChainDb().triedb)
// return stTr, nil
// }
func (b *Backend) GetAccountTrie(stateRoot core.Hash, account core.Address) (core.Trie, error) {
tr, err := b.GetTrie(stateRoot)
if err != nil {
return nil, err
}
act, err := tr.TryGetAccount(account)
if err != nil {
return nil, err
}
acTr, err := trie.NewStateTrie(trie.StorageTrieID(common.Hash(stateRoot), crypto.Keccak256Hash(account[:]), common.Hash(act.Root)), trie.NewDatabase(b.b.ChainDb()))
if err != nil {
return nil, err
}
return NewWrappedTrie(acTr), nil
}

View File

@ -1,13 +1,9 @@
package backendwrapper
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/core/types"
"github.com/openrelayxyz/plugeth-utils/core"
)
@ -16,89 +12,43 @@ type WrappedTrie struct {
t state.Trie
}
func NewWrappedTrie(t state.Trie) WrappedTrie {
return WrappedTrie{t}
func NewWrappedTrie(t state.Trie) core.Trie {
return &WrappedTrie{t}
}
func (t WrappedTrie) GetKey(b []byte) []byte {
func (t *WrappedTrie) GetKey(b []byte) []byte {
return t.t.GetKey(b)
}
func (t WrappedTrie) TryGet(key []byte) ([]byte, error) {
func (t *WrappedTrie) TryGet(key []byte) ([]byte, error) {
return t.t.TryGet(key)
}
func (t WrappedTrie) TryGetAccount(address core.Address) (*WrappedStateAccount, error) {
func (t *WrappedTrie) TryGetAccount(address core.Address) (*core.StateAccount, error) {
act, err := t.t.TryGetAccount(common.Address(address))
if err != nil {
return nil, err
}
return NewWrappedStateAccount(act), nil
// return act, nil
return &core.StateAccount{
Nonce: act.Nonce,
Balance: act.Balance,
Root: core.Hash(act.Root),
CodeHash: act.CodeHash,
}, nil
}
func (t WrappedTrie) TryUpdate(key, value []byte) error {
return nil
}
func (t WrappedTrie) TryUpdateAccount(address core.Address, account *core.StateAccount) error {
return nil
}
func (t WrappedTrie) TryDelete(key []byte) error {
return nil
}
func (t WrappedTrie) TryDeleteAccount(address common.Address) error {
return nil
}
func (t WrappedTrie) Hash() core.Hash {
func (t *WrappedTrie) Hash() core.Hash {
return core.Hash(t.t.Hash())
}
func (t WrappedTrie) Commit(collectLeaf bool) (core.Hash, *trie.NodeSet) {
//EmptyRootHash
return core.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"), nil
func (t *WrappedTrie) NodeIterator(startKey []byte) core.NodeIterator {
itr := t.t.NodeIterator(startKey)
return &WrappedNodeIterator{itr}
}
func (t WrappedTrie) NodeIterator(startKey []byte) core.NodeIterator {
return t.t.NodeIterator(startKey)
}
func (t WrappedTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error {
func (t *WrappedTrie) Prove(key []byte, fromLevel uint, proofDb core.KeyValueWriter) error {
return nil
}
type WrappedStateAccount struct {
s *types.StateAccount
}
func NewWrappedStateAccount(s *types.StateAccount) *WrappedStateAccount {
return &WrappedStateAccount{s}
}
func (w *WrappedStateAccount) Nonce() uint64 {
return w.s.Nonce
}
func (w *WrappedStateAccount) Balance() *big.Int {
return w.s.Balance
}
func (w *WrappedStateAccount) Root() core.Hash {
return core.Hash(w.s.Root)
}
func (w *WrappedStateAccount) CodeHash() []byte {
return w.s.CodeHash
}
// Nonce uint64
// Balance *big.Int
// Root Hash // merkle root of the storage trie
// CodeHash []byte
// }
type WrappedNodeIterator struct {
n trie.NodeIterator
}
@ -144,15 +94,11 @@ func (n WrappedNodeIterator) LeafProof() [][]byte {
}
func (n WrappedNodeIterator) AddResolver(c core.NodeResolver) {
// nr := NewWrappedNodeResolver(trie.NodeResolver)
return n.n.AddResolver(c)
}
type WrappedNodeResolver struct {
r core.NodeResolver
}
func NewWrappedNodeResolver(r trie.NodeResolver) WrappedNodeResolver {
return WrappedNodeResolver{r}
n.n.AddResolver(WrappedNodeResolver(c))
}
func WrappedNodeResolver(fn core.NodeResolver) trie.NodeResolver {
return func(owner common.Hash, path []byte, hash common.Hash) []byte {
return fn(core.Hash(owner), path, core.Hash(hash) )
}
}