2023-04-07 01:09:53 +00:00
|
|
|
package backendwrapper
|
|
|
|
|
|
|
|
import (
|
2024-01-24 19:13:39 +00:00
|
|
|
"math/big"
|
|
|
|
|
2023-04-07 01:09:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2023-09-13 19:57:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2023-04-07 01:09:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
|
|
|
|
"github.com/openrelayxyz/plugeth-utils/core"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WrappedTrie struct {
|
|
|
|
t state.Trie
|
|
|
|
}
|
|
|
|
|
2023-04-07 17:39:10 +00:00
|
|
|
func NewWrappedTrie(t state.Trie) core.Trie {
|
|
|
|
return &WrappedTrie{t}
|
2023-04-07 01:09:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 17:39:10 +00:00
|
|
|
func (t *WrappedTrie) GetKey(b []byte) []byte {
|
2023-04-07 01:09:53 +00:00
|
|
|
return t.t.GetKey(b)
|
|
|
|
}
|
|
|
|
|
2023-05-05 21:14:15 +00:00
|
|
|
func (t *WrappedTrie) GetAccount(address core.Address) (*core.StateAccount, error) {
|
|
|
|
act, err := t.t.GetAccount(common.Address(address))
|
2023-04-07 01:09:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-07 17:39:10 +00:00
|
|
|
return &core.StateAccount{
|
|
|
|
Nonce: act.Nonce,
|
2024-01-24 19:13:39 +00:00
|
|
|
Balance: new(big.Int).SetBytes(act.Balance.Bytes()),
|
2023-04-07 17:39:10 +00:00
|
|
|
Root: core.Hash(act.Root),
|
|
|
|
CodeHash: act.CodeHash,
|
|
|
|
}, nil
|
2023-04-07 01:09:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 17:39:10 +00:00
|
|
|
func (t *WrappedTrie) Hash() core.Hash {
|
2023-04-07 01:09:53 +00:00
|
|
|
return core.Hash(t.t.Hash())
|
|
|
|
}
|
|
|
|
|
2023-04-07 17:39:10 +00:00
|
|
|
func (t *WrappedTrie) NodeIterator(startKey []byte) core.NodeIterator {
|
2023-09-13 19:57:48 +00:00
|
|
|
itr, err := t.t.NodeIterator(startKey)
|
2023-12-06 22:04:56 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error returned from geth side NodeIterator", "err", err)
|
|
|
|
}
|
2023-04-07 17:39:10 +00:00
|
|
|
return &WrappedNodeIterator{itr}
|
2023-04-07 01:09:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 17:39:10 +00:00
|
|
|
func (t *WrappedTrie) Prove(key []byte, fromLevel uint, proofDb core.KeyValueWriter) error {
|
2023-04-07 01:09:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type WrappedNodeIterator struct {
|
2023-04-07 05:49:14 +00:00
|
|
|
n trie.NodeIterator
|
2023-04-07 01:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n WrappedNodeIterator) Next(b bool) bool {
|
2023-04-07 05:49:14 +00:00
|
|
|
return n.n.Next(b)
|
2023-04-07 01:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-04-07 05:49:14 +00:00
|
|
|
func (n WrappedNodeIterator) AddResolver(c core.NodeResolver) {
|
2023-04-07 17:39:10 +00:00
|
|
|
n.n.AddResolver(WrappedNodeResolver(c))
|
2023-04-07 01:09:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 17:39:10 +00:00
|
|
|
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) )
|
|
|
|
}
|
|
|
|
}
|