From f92264d3421dea603e5875aca7d3cc9277953955 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Thu, 6 Apr 2023 22:49:14 -0700 Subject: [PATCH] further work on state trie wrapper --- .../wrappers/backendwrapper/backendwrapper.go | 24 +++++------ .../wrappers/backendwrapper/triewrapper.go | 41 +++++++++++++++---- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/plugins/wrappers/backendwrapper/backendwrapper.go b/plugins/wrappers/backendwrapper/backendwrapper.go index 4e532b211..1a0d051a8 100644 --- a/plugins/wrappers/backendwrapper/backendwrapper.go +++ b/plugins/wrappers/backendwrapper/backendwrapper.go @@ -11,13 +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/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/trie" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" @@ -464,16 +464,16 @@ 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) 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 -} +// 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)) +// stTr, _ := trie.NewStateTrie(trie.StorageTrieID(common.Hash(stateRoot), crypto.Keccak256Hash(account[:]), act.Root), b.b.ChainDb().triedb) +// return stTr, nil +// } diff --git a/plugins/wrappers/backendwrapper/triewrapper.go b/plugins/wrappers/backendwrapper/triewrapper.go index 11cfd0d8d..da3b6ec0a 100644 --- a/plugins/wrappers/backendwrapper/triewrapper.go +++ b/plugins/wrappers/backendwrapper/triewrapper.go @@ -1,6 +1,8 @@ package backendwrapper import ( + "math/big" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/core/state" @@ -26,12 +28,13 @@ 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) +func (t WrappedTrie) TryGetAccount(address core.Address) (*WrappedStateAccount, error) { + act, err := t.t.TryGetAccount(common.Address(address)) if err != nil { return nil, err } return NewWrappedStateAccount(act), nil + // return act, nil } 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) { //EmptyRootHash - return core.Hash(core.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")), nil + return core.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"), nil } func (t WrappedTrie) NodeIterator(startKey []byte) core.NodeIterator { @@ -75,6 +78,21 @@ func NewWrappedStateAccount(s *types.StateAccount) *WrappedStateAccount { 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 // Balance *big.Int // Root Hash // merkle root of the storage trie @@ -82,11 +100,11 @@ func NewWrappedStateAccount(s *types.StateAccount) *WrappedStateAccount { // } type WrappedNodeIterator struct { - n trie.NodeInterator + n trie.NodeIterator } func (n WrappedNodeIterator) Next(b bool) bool { - return n.n.Next() + return n.n.Next(b) } func (n WrappedNodeIterator) Error() error { @@ -125,7 +143,16 @@ func (n WrappedNodeIterator) LeafProof() [][]byte { return n.n.LeafProof() } -func (n WrappedNodeIterator) AddResolver(trie.NodeResolver) { - return n.n.AddResolver() +func (n WrappedNodeIterator) AddResolver(c core.NodeResolver) { + // nr := NewWrappedNodeResolver(trie.NodeResolver) + return n.n.AddResolver(c) +} + +type WrappedNodeResolver struct { + r core.NodeResolver +} + +func NewWrappedNodeResolver(r trie.NodeResolver) WrappedNodeResolver { + return WrappedNodeResolver{r} }