github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/trie/trie_reader.go (about) 1 // Copyright 2022 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package trie 18 19 import ( 20 "github.com/ethereum/go-ethereum/common" 21 "github.com/ethereum/go-ethereum/core/types" 22 "github.com/ethereum/go-ethereum/log" 23 "github.com/ethereum/go-ethereum/trie/triestate" 24 "github.com/ethereum/go-ethereum/triedb/database" 25 ) 26 27 // trieReader is a wrapper of the underlying node reader. It's not safe 28 // for concurrent usage. 29 type trieReader struct { 30 owner common.Hash 31 reader database.Reader 32 banned map[string]struct{} // Marker to prevent node from being accessed, for tests 33 } 34 35 // newTrieReader initializes the trie reader with the given node reader. 36 func newTrieReader(stateRoot, owner common.Hash, db database.Database) (*trieReader, error) { 37 if stateRoot == (common.Hash{}) || stateRoot == types.EmptyRootHash { 38 if stateRoot == (common.Hash{}) { 39 log.Error("Zero state root hash!") 40 } 41 return &trieReader{owner: owner}, nil 42 } 43 reader, err := db.Reader(stateRoot) 44 if err != nil { 45 return nil, &MissingNodeError{Owner: owner, NodeHash: stateRoot, err: err} 46 } 47 return &trieReader{owner: owner, reader: reader}, nil 48 } 49 50 // newEmptyReader initializes the pure in-memory reader. All read operations 51 // should be forbidden and returns the MissingNodeError. 52 func newEmptyReader() *trieReader { 53 return &trieReader{} 54 } 55 56 // node retrieves the rlp-encoded trie node with the provided trie node 57 // information. An MissingNodeError will be returned in case the node is 58 // not found or any error is encountered. 59 func (r *trieReader) node(path []byte, hash common.Hash) ([]byte, error) { 60 // Perform the logics in tests for preventing trie node access. 61 if r.banned != nil { 62 if _, ok := r.banned[string(path)]; ok { 63 return nil, &MissingNodeError{Owner: r.owner, NodeHash: hash, Path: path} 64 } 65 } 66 if r.reader == nil { 67 return nil, &MissingNodeError{Owner: r.owner, NodeHash: hash, Path: path} 68 } 69 blob, err := r.reader.Node(r.owner, path, hash) 70 if err != nil || len(blob) == 0 { 71 return nil, &MissingNodeError{Owner: r.owner, NodeHash: hash, Path: path, err: err} 72 } 73 return blob, nil 74 } 75 76 // MerkleLoader implements triestate.TrieLoader for constructing tries. 77 type MerkleLoader struct { 78 db database.Database 79 } 80 81 // NewMerkleLoader creates the merkle trie loader. 82 func NewMerkleLoader(db database.Database) *MerkleLoader { 83 return &MerkleLoader{db: db} 84 } 85 86 // OpenTrie opens the main account trie. 87 func (l *MerkleLoader) OpenTrie(root common.Hash) (triestate.Trie, error) { 88 return New(TrieID(root), l.db) 89 } 90 91 // OpenStorageTrie opens the storage trie of an account. 92 func (l *MerkleLoader) OpenStorageTrie(stateRoot common.Hash, addrHash, root common.Hash) (triestate.Trie, error) { 93 return New(StorageTrieID(stateRoot, addrHash, root), l.db) 94 }