further work on state trie wrapper

This commit is contained in:
philip-morlier 2023-04-06 22:49:14 -07:00
parent aee14ebee9
commit f92264d342
2 changed files with 46 additions and 19 deletions

View File

@ -11,13 +11,13 @@ 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/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 +464,16 @@ 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) state.Trie {
return NewWrappedTrie(trie.New(trie.TrieID(common.Hash(h)), trie.NewDatabase(b.b.ChainDb()))) // 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) { // func (b *Backend) GetAccountTrie(stateRoot core.Hash, account core.Address) (state.Trie, error) {
tr := b.GetTrie(common.Hash(stateRoot)) // tr := b.GetTrie(common.Hash(stateRoot))
act := tr.TryGetAccount(common.Address(account)) // act := tr.TryGetAccount(common.Address(account))
tr, _ := trie.NewStateTrie(trie.StorageTrieID(common.Hash(stateRoot), crypto.Keccak256Hash(account[:]), act.Root), b.b.ChainDb().triedb) // stTr, _ := trie.NewStateTrie(trie.StorageTrieID(common.Hash(stateRoot), crypto.Keccak256Hash(account[:]), act.Root), b.b.ChainDb().triedb)
return tr, nil // return stTr, nil
} // }

View File

@ -1,6 +1,8 @@
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/ethdb"
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
@ -26,12 +28,13 @@ func (t WrappedTrie) TryGet(key []byte) ([]byte, error) {
return t.t.TryGet(key) return t.t.TryGet(key)
} }
func (t WrappedTrie) TryGetAccount(address common.Address) (*core.StateAccount, error) { func (t WrappedTrie) TryGetAccount(address core.Address) (*WrappedStateAccount, error) {
act, err := t.t.TryGetAccount(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 NewWrappedStateAccount(act), nil
// return act, nil
} }
func (t WrappedTrie) TryUpdate(key, value []byte) error { func (t WrappedTrie) TryUpdate(key, value []byte) error {
@ -56,7 +59,7 @@ func (t WrappedTrie) Hash() core.Hash {
func (t WrappedTrie) Commit(collectLeaf bool) (core.Hash, *trie.NodeSet) { func (t WrappedTrie) Commit(collectLeaf bool) (core.Hash, *trie.NodeSet) {
//EmptyRootHash //EmptyRootHash
return core.Hash(core.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")), nil return core.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"), nil
} }
func (t WrappedTrie) NodeIterator(startKey []byte) core.NodeIterator { func (t WrappedTrie) NodeIterator(startKey []byte) core.NodeIterator {
@ -75,6 +78,21 @@ func NewWrappedStateAccount(s *types.StateAccount) *WrappedStateAccount {
return &WrappedStateAccount{s} 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 // Nonce uint64
// Balance *big.Int // Balance *big.Int
// Root Hash // merkle root of the storage trie // Root Hash // merkle root of the storage trie
@ -82,11 +100,11 @@ func NewWrappedStateAccount(s *types.StateAccount) *WrappedStateAccount {
// } // }
type WrappedNodeIterator struct { type WrappedNodeIterator struct {
n trie.NodeInterator n trie.NodeIterator
} }
func (n WrappedNodeIterator) Next(b bool) bool { func (n WrappedNodeIterator) Next(b bool) bool {
return n.n.Next() return n.n.Next(b)
} }
func (n WrappedNodeIterator) Error() error { func (n WrappedNodeIterator) Error() error {
@ -125,7 +143,16 @@ func (n WrappedNodeIterator) LeafProof() [][]byte {
return n.n.LeafProof() return n.n.LeafProof()
} }
func (n WrappedNodeIterator) AddResolver(trie.NodeResolver) { func (n WrappedNodeIterator) AddResolver(c core.NodeResolver) {
return n.n.AddResolver() // nr := NewWrappedNodeResolver(trie.NodeResolver)
return n.n.AddResolver(c)
}
type WrappedNodeResolver struct {
r core.NodeResolver
}
func NewWrappedNodeResolver(r trie.NodeResolver) WrappedNodeResolver {
return WrappedNodeResolver{r}
} }