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"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
gcore "github.com/ethereum/go-ethereum/core" 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/core/types"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/log" "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/rlp"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
@ -464,16 +463,29 @@ func (b *Backend) ChainConfig() *params.ChainConfig {
return b.chainConfig return b.chainConfig
} }
// func (b *Backend) GetTrie(h core.Hash) state.Trie { func (b *Backend) GetTrie(h core.Hash) (core.Trie, error) {
// return NewWrappedTrie(trie.New(trie.TrieID(common.Hash(h)), trie.NewDatabase(b.b.ChainDb()))) 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) { func (b *Backend) GetAccountTrie(stateRoot core.Hash, account core.Address) (core.Trie, error) {
// tr := b.GetTrie(common.Hash(stateRoot)) tr, err := b.GetTrie(stateRoot)
// act := tr.TryGetAccount(common.Address(account)) if err != nil {
// stTr, _ := trie.NewStateTrie(trie.StorageTrieID(common.Hash(stateRoot), crypto.Keccak256Hash(account[:]), act.Root), b.b.ChainDb().triedb) return nil, err
// return stTr, nil }
// } 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 package backendwrapper
import ( import (
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/core/types"
"github.com/openrelayxyz/plugeth-utils/core" "github.com/openrelayxyz/plugeth-utils/core"
) )
@ -16,89 +12,43 @@ type WrappedTrie struct {
t state.Trie t state.Trie
} }
func NewWrappedTrie(t state.Trie) WrappedTrie { func NewWrappedTrie(t state.Trie) core.Trie {
return WrappedTrie{t} return &WrappedTrie{t}
} }
func (t WrappedTrie) GetKey(b []byte) []byte { func (t *WrappedTrie) GetKey(b []byte) []byte {
return t.t.GetKey(b) 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) 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)) act, err := t.t.TryGetAccount(common.Address(address))
if err != nil { if err != nil {
return nil, err return nil, err
} }
return NewWrappedStateAccount(act), nil return &core.StateAccount{
// return act, nil Nonce: act.Nonce,
Balance: act.Balance,
Root: core.Hash(act.Root),
CodeHash: act.CodeHash,
}, nil
} }
func (t WrappedTrie) TryUpdate(key, value []byte) error { func (t *WrappedTrie) Hash() core.Hash {
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 {
return core.Hash(t.t.Hash()) return core.Hash(t.t.Hash())
} }
func (t WrappedTrie) Commit(collectLeaf bool) (core.Hash, *trie.NodeSet) { func (t *WrappedTrie) NodeIterator(startKey []byte) core.NodeIterator {
//EmptyRootHash itr := t.t.NodeIterator(startKey)
return core.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"), nil return &WrappedNodeIterator{itr}
} }
func (t WrappedTrie) NodeIterator(startKey []byte) core.NodeIterator { func (t *WrappedTrie) Prove(key []byte, fromLevel uint, proofDb core.KeyValueWriter) error {
return t.t.NodeIterator(startKey)
}
func (t WrappedTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error {
return nil 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 { type WrappedNodeIterator struct {
n trie.NodeIterator n trie.NodeIterator
} }
@ -144,15 +94,11 @@ func (n WrappedNodeIterator) LeafProof() [][]byte {
} }
func (n WrappedNodeIterator) AddResolver(c core.NodeResolver) { func (n WrappedNodeIterator) AddResolver(c core.NodeResolver) {
// nr := NewWrappedNodeResolver(trie.NodeResolver) n.n.AddResolver(WrappedNodeResolver(c))
return n.n.AddResolver(c)
}
type WrappedNodeResolver struct {
r core.NodeResolver
}
func NewWrappedNodeResolver(r trie.NodeResolver) WrappedNodeResolver {
return WrappedNodeResolver{r}
} }
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) )
}
}