Roy Crihfield
761d60acdf
The Geth `core/state` and `trie` packages underwent a big refactor between `v1.11.6` and `1.13.14`. This code, which was adapted from those, needed corresponding updates. To do this I applied the diff patches from Geth directly where possible and in some places had to clone new parts of the Geth code and adapt them. In order to make this process as straightforward as possible in the future, I've attempted to minimize the number of changes vs. Geth and added some documentation in the `trie_by_cid` package. Reviewed-on: #5
95 lines
3.4 KiB
Go
95 lines
3.4 KiB
Go
// Copyright 2022 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package trie
|
|
|
|
import (
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/trie/triestate"
|
|
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/triedb/database"
|
|
)
|
|
|
|
// trieReader is a wrapper of the underlying node reader. It's not safe
|
|
// for concurrent usage.
|
|
type trieReader struct {
|
|
owner common.Hash
|
|
reader database.Reader
|
|
banned map[string]struct{} // Marker to prevent node from being accessed, for tests
|
|
}
|
|
|
|
// newTrieReader initializes the trie reader with the given node reader.
|
|
func newTrieReader(stateRoot, owner common.Hash, db database.Database) (*trieReader, error) {
|
|
if stateRoot == types.EmptyRootHash {
|
|
return &trieReader{owner: owner}, nil
|
|
}
|
|
// Originally, passing a zero state root also caused the reader to not be set
|
|
// We allow it for storage tries for simplicity (ipld-eth-statedb change)
|
|
|
|
reader, err := db.Reader(stateRoot)
|
|
if err != nil {
|
|
return nil, &MissingNodeError{Owner: owner, NodeHash: stateRoot, err: err}
|
|
}
|
|
return &trieReader{owner: owner, reader: reader}, nil
|
|
}
|
|
|
|
// newEmptyReader initializes the pure in-memory reader. All read operations
|
|
// should be forbidden and returns the MissingNodeError.
|
|
func newEmptyReader() *trieReader {
|
|
return &trieReader{}
|
|
}
|
|
|
|
// node retrieves the rlp-encoded trie node with the provided trie node
|
|
// information. An MissingNodeError will be returned in case the node is
|
|
// not found or any error is encountered.
|
|
func (r *trieReader) node(path []byte, hash common.Hash) ([]byte, error) {
|
|
// Perform the logics in tests for preventing trie node access.
|
|
if r.banned != nil {
|
|
if _, ok := r.banned[string(path)]; ok {
|
|
return nil, &MissingNodeError{Owner: r.owner, NodeHash: hash, Path: path}
|
|
}
|
|
}
|
|
if r.reader == nil {
|
|
return nil, &MissingNodeError{Owner: r.owner, NodeHash: hash, Path: path}
|
|
}
|
|
blob, err := r.reader.Node(r.owner, path, hash)
|
|
if err != nil || len(blob) == 0 {
|
|
return nil, &MissingNodeError{Owner: r.owner, NodeHash: hash, Path: path, err: err}
|
|
}
|
|
return blob, nil
|
|
}
|
|
|
|
// MerkleLoader implements triestate.TrieLoader for constructing tries.
|
|
type MerkleLoader struct {
|
|
db database.Database
|
|
}
|
|
|
|
// NewMerkleLoader creates the merkle trie loader.
|
|
func NewMerkleLoader(db database.Database) *MerkleLoader {
|
|
return &MerkleLoader{db: db}
|
|
}
|
|
|
|
// OpenTrie opens the main account trie.
|
|
func (l *MerkleLoader) OpenTrie(root common.Hash) (triestate.Trie, error) {
|
|
return New(TrieID(root), l.db)
|
|
}
|
|
|
|
// OpenStorageTrie opens the storage trie of an account.
|
|
func (l *MerkleLoader) OpenStorageTrie(stateRoot common.Hash, addrHash, root common.Hash) (triestate.Trie, error) {
|
|
return New(StorageTrieID(stateRoot, addrHash, root), l.db)
|
|
}
|