Merge pull request #76 from openrelayxyz/feature/expose-state-trie

Feature/expose state trie
This commit is contained in:
Philip Morlier 2023-05-05 14:29:36 -07:00 committed by GitHub
commit 9badeb2b93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 135 additions and 7 deletions

2
go.mod
View File

@ -50,7 +50,7 @@ require (
github.com/mattn/go-isatty v0.0.16 github.com/mattn/go-isatty v0.0.16
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
github.com/olekukonko/tablewriter v0.0.5 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/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7
github.com/rs/cors v1.7.0 github.com/rs/cors v1.7.0
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible

8
go.sum
View File

@ -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/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 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao=
github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA= 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 h1:DZfsyhDK1hnSS5lH8l+JggqzEleHteTYfutAiVlSUM8=
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw= 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= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
@ -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.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 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= 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 v1.1.0 h1:pK02TCFT32G/o/GNgAF+i3CihWfa/G71i0/vB9PHG3k=
github.com/openrelayxyz/plugeth-utils v0.0.24/go.mod h1:W1Hwwhv04MCuNCcI6a62/kL6eWbH7OO+KvpBEyQTgIs= github.com/openrelayxyz/plugeth-utils v1.1.0/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/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= 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/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=

View File

@ -11,10 +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/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/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
@ -460,3 +462,32 @@ func (b *Backend) ChainConfig() *params.ChainConfig {
} }
return b.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
}

View File

@ -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) )
}
}