From aee14ebee966d6d32b80f42da360cddeb7b969eb Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Thu, 6 Apr 2023 18:09:53 -0700 Subject: [PATCH] Initial commit of work to expose state trie --- .../wrappers/backendwrapper/backendwrapper.go | 19 +++ .../wrappers/backendwrapper/triewrapper.go | 131 ++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 plugins/wrappers/backendwrapper/triewrapper.go diff --git a/plugins/wrappers/backendwrapper/backendwrapper.go b/plugins/wrappers/backendwrapper/backendwrapper.go index 0027ac223..4e532b211 100644 --- a/plugins/wrappers/backendwrapper/backendwrapper.go +++ b/plugins/wrappers/backendwrapper/backendwrapper.go @@ -11,10 +11,13 @@ 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/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/rlp" "github.com/ethereum/go-ethereum/rpc" @@ -460,3 +463,19 @@ 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) GetAccountTrie(stateRoot core.Hash, account core.Address) (state.Trie, error) { + tr := b.GetTrie(common.Hash(stateRoot)) + act := tr.TryGetAccount(common.Address(account)) + tr, _ := trie.NewStateTrie(trie.StorageTrieID(common.Hash(stateRoot), crypto.Keccak256Hash(account[:]), act.Root), b.b.ChainDb().triedb) + return tr, nil +} + + + + + diff --git a/plugins/wrappers/backendwrapper/triewrapper.go b/plugins/wrappers/backendwrapper/triewrapper.go new file mode 100644 index 000000000..11cfd0d8d --- /dev/null +++ b/plugins/wrappers/backendwrapper/triewrapper.go @@ -0,0 +1,131 @@ +package backendwrapper + +import ( + "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" +) + +type WrappedTrie struct { + t state.Trie +} + +func NewWrappedTrie(t state.Trie) WrappedTrie { + return WrappedTrie{t} +} + +func (t WrappedTrie) GetKey(b []byte) []byte { + return t.t.GetKey(b) +} + +func (t WrappedTrie) TryGet(key []byte) ([]byte, error) { + return t.t.TryGet(key) +} + +func (t WrappedTrie) TryGetAccount(address common.Address) (*core.StateAccount, error) { + act, err := t.t.TryGetAccount(address) + if err != nil { + return nil, err + } + return NewWrappedStateAccount(act), 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 { + return core.Hash(t.t.Hash()) +} + +func (t WrappedTrie) Commit(collectLeaf bool) (core.Hash, *trie.NodeSet) { + //EmptyRootHash + return core.Hash(core.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")), nil +} + +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 { + return nil +} + +type WrappedStateAccount struct { + s *types.StateAccount +} + +func NewWrappedStateAccount(s *types.StateAccount) *WrappedStateAccount { + return &WrappedStateAccount{s} +} + +// Nonce uint64 +// Balance *big.Int +// Root Hash // merkle root of the storage trie +// CodeHash []byte +// } + +type WrappedNodeIterator struct { + n trie.NodeInterator +} + +func (n WrappedNodeIterator) Next(b bool) bool { + return n.n.Next() +} + +func (n WrappedNodeIterator) Error() error { + return n.n.Error() +} + +func (n WrappedNodeIterator) Hash() core.Hash { + return core.Hash(n.n.Hash()) +} + +func (n WrappedNodeIterator) Parent() core.Hash { + return core.Hash(n.n.Parent()) +} + +func (n WrappedNodeIterator) Path() []byte { + return n.n.Path() +} + +func (n WrappedNodeIterator) NodeBlob() []byte { + return n.n.NodeBlob() +} + +func (n WrappedNodeIterator) Leaf() bool { + return n.n.Leaf() +} + +func (n WrappedNodeIterator) LeafKey() []byte { + return n.n.LeafKey() +} + +func (n WrappedNodeIterator) LeafBlob() []byte { + return n.n.LeafBlob() +} + +func (n WrappedNodeIterator) LeafProof() [][]byte { + return n.n.LeafProof() +} + +func (n WrappedNodeIterator) AddResolver(trie.NodeResolver) { + return n.n.AddResolver() +} +