github.com/ethereum/go-ethereum@v1.16.1/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/triedb/database" 23 ) 24 25 // trieReader is a wrapper of the underlying node reader. It's not safe 26 // for concurrent usage. 27 type trieReader struct { 28 owner common.Hash 29 reader database.NodeReader 30 banned map[string]struct{} // Marker to prevent node from being accessed, for tests 31 } 32 33 // newTrieReader initializes the trie reader with the given node reader. 34 func newTrieReader(stateRoot, owner common.Hash, db database.NodeDatabase) (*trieReader, error) { 35 if stateRoot == (common.Hash{}) || stateRoot == types.EmptyRootHash { 36 return &trieReader{owner: owner}, nil 37 } 38 reader, err := db.NodeReader(stateRoot) 39 if err != nil { 40 return nil, &MissingNodeError{Owner: owner, NodeHash: stateRoot, err: err} 41 } 42 return &trieReader{owner: owner, reader: reader}, nil 43 } 44 45 // newEmptyReader initializes the pure in-memory reader. All read operations 46 // should be forbidden and returns the MissingNodeError. 47 func newEmptyReader() *trieReader { 48 return &trieReader{} 49 } 50 51 // node retrieves the rlp-encoded trie node with the provided trie node 52 // information. An MissingNodeError will be returned in case the node is 53 // not found or any error is encountered. 54 // 55 // Don't modify the returned byte slice since it's not deep-copied and 56 // still be referenced by database. 57 func (r *trieReader) node(path []byte, hash common.Hash) ([]byte, error) { 58 // Perform the logics in tests for preventing trie node access. 59 if r.banned != nil { 60 if _, ok := r.banned[string(path)]; ok { 61 return nil, &MissingNodeError{Owner: r.owner, NodeHash: hash, Path: path} 62 } 63 } 64 if r.reader == nil { 65 return nil, &MissingNodeError{Owner: r.owner, NodeHash: hash, Path: path} 66 } 67 blob, err := r.reader.Node(r.owner, path, hash) 68 if err != nil || len(blob) == 0 { 69 return nil, &MissingNodeError{Owner: r.owner, NodeHash: hash, Path: path, err: err} 70 } 71 return blob, nil 72 }