Skip to content

Commit

Permalink
Merge pull request #76 from openrelayxyz/feature/expose-state-trie
Browse files Browse the repository at this point in the history
Feature/expose state trie
  • Loading branch information
philip-morlier authored May 5, 2023
2 parents 923254a + 829b8c4 commit 9badeb2
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ require (
github.com/mattn/go-isatty v0.0.16
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
github.com/olekukonko/tablewriter v0.0.5
github.com/openrelayxyz/plugeth-utils v0.0.25
github.com/openrelayxyz/plugeth-utils v1.1.0
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7
github.com/rs/cors v1.7.0
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,6 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao=
github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA=
github.com/holiman/uint256 v1.2.0 h1:gpSYcPLWGv4sG43I2mVLiDZCNDh/EpGjSk8tmtxitHM=
github.com/holiman/uint256 v1.2.0/go.mod h1:y4ga/t+u+Xwd7CpDgZESaRcWy0I7XMlTMA25ApIH5Jw=
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c h1:DZfsyhDK1hnSS5lH8l+JggqzEleHteTYfutAiVlSUM8=
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down Expand Up @@ -351,10 +349,8 @@ github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9k
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/openrelayxyz/plugeth-utils v0.0.24 h1:Q2BR3SlwHovNFLbEtGrFFqgOCnL+ONyrwTC9wKKAej4=
github.com/openrelayxyz/plugeth-utils v0.0.24/go.mod h1:W1Hwwhv04MCuNCcI6a62/kL6eWbH7OO+KvpBEyQTgIs=
github.com/openrelayxyz/plugeth-utils v0.0.25 h1:UG9XNBA+V3ZVSXRpBIBXpa7fQIPd5MczrnBQTMpK1GY=
github.com/openrelayxyz/plugeth-utils v0.0.25/go.mod h1:W1Hwwhv04MCuNCcI6a62/kL6eWbH7OO+KvpBEyQTgIs=
github.com/openrelayxyz/plugeth-utils v1.1.0 h1:pK02TCFT32G/o/GNgAF+i3CihWfa/G71i0/vB9PHG3k=
github.com/openrelayxyz/plugeth-utils v1.1.0/go.mod h1:W1Hwwhv04MCuNCcI6a62/kL6eWbH7OO+KvpBEyQTgIs=
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down
31 changes: 31 additions & 0 deletions plugins/wrappers/backendwrapper/backendwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +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/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"
Expand Down Expand Up @@ -460,3 +462,32 @@ func (b *Backend) ChainConfig() *params.ChainConfig {
}
return b.chainConfig
}

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) (core.Trie, error) {
tr, err := b.GetTrie(stateRoot)
if err != nil {
return nil, err
}
act, err := tr.GetAccount(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
}





101 changes: 101 additions & 0 deletions plugins/wrappers/backendwrapper/triewrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package backendwrapper

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/trie"

"github.com/openrelayxyz/plugeth-utils/core"
)

type WrappedTrie struct {
t state.Trie
}

func NewWrappedTrie(t state.Trie) core.Trie {
return &WrappedTrie{t}
}

func (t *WrappedTrie) GetKey(b []byte) []byte {
return t.t.GetKey(b)
}

func (t *WrappedTrie) GetAccount(address core.Address) (*core.StateAccount, error) {
act, err := t.t.GetAccount(common.Address(address))
if err != nil {
return nil, err
}
return &core.StateAccount{
Nonce: act.Nonce,
Balance: act.Balance,
Root: core.Hash(act.Root),
CodeHash: act.CodeHash,
}, nil
}

func (t *WrappedTrie) Hash() core.Hash {
return core.Hash(t.t.Hash())
}

func (t *WrappedTrie) NodeIterator(startKey []byte) core.NodeIterator {
itr := t.t.NodeIterator(startKey)
return &WrappedNodeIterator{itr}
}

func (t *WrappedTrie) Prove(key []byte, fromLevel uint, proofDb core.KeyValueWriter) error {
return nil
}

type WrappedNodeIterator struct {
n trie.NodeIterator
}

func (n WrappedNodeIterator) Next(b bool) bool {
return n.n.Next(b)
}

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(c core.NodeResolver) {
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) )
}
}

0 comments on commit 9badeb2

Please sign in to comment.