| 
 | 1 | +// Copyright 2025 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 filtermaps  | 
 | 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 | +)  | 
 | 24 | + | 
 | 25 | +// blockchain represents the underlying blockchain of ChainView.  | 
 | 26 | +type blockchain interface {  | 
 | 27 | +	GetHeader(hash common.Hash, number uint64) *types.Header  | 
 | 28 | +	GetCanonicalHash(number uint64) common.Hash  | 
 | 29 | +	GetReceiptsByHash(hash common.Hash) types.Receipts  | 
 | 30 | +}  | 
 | 31 | + | 
 | 32 | +// ChainView represents an immutable view of a chain with a block id and a set  | 
 | 33 | +// of receipts associated to each block number and a block hash associated with  | 
 | 34 | +// all block numbers except the head block. This is because in the future  | 
 | 35 | +// ChainView might represent a view where the head block is currently being  | 
 | 36 | +// created. Block id is a unique identifier that can also be calculated for the  | 
 | 37 | +// head block.  | 
 | 38 | +// Note that the view's head does not have to be the current canonical head  | 
 | 39 | +// of the underlying blockchain, it should only possess the block headers  | 
 | 40 | +// and receipts up until the expected chain view head.  | 
 | 41 | +type ChainView struct {  | 
 | 42 | +	chain      blockchain  | 
 | 43 | +	headNumber uint64  | 
 | 44 | +	hashes     []common.Hash // block hashes starting backwards from headNumber until first canonical hash  | 
 | 45 | +}  | 
 | 46 | + | 
 | 47 | +// NewChainView creates a new ChainView.  | 
 | 48 | +func NewChainView(chain blockchain, number uint64, hash common.Hash) *ChainView {  | 
 | 49 | +	cv := &ChainView{  | 
 | 50 | +		chain:      chain,  | 
 | 51 | +		headNumber: number,  | 
 | 52 | +		hashes:     []common.Hash{hash},  | 
 | 53 | +	}  | 
 | 54 | +	cv.extendNonCanonical()  | 
 | 55 | +	return cv  | 
 | 56 | +}  | 
 | 57 | + | 
 | 58 | +// getBlockHash returns the block hash belonging to the given block number.  | 
 | 59 | +// Note that the hash of the head block is not returned because ChainView might  | 
 | 60 | +// represent a view where the head block is currently being created.  | 
 | 61 | +func (cv *ChainView) getBlockHash(number uint64) common.Hash {  | 
 | 62 | +	if number >= cv.headNumber {  | 
 | 63 | +		panic("invalid block number")  | 
 | 64 | +	}  | 
 | 65 | +	return cv.blockHash(number)  | 
 | 66 | +}  | 
 | 67 | + | 
 | 68 | +// getBlockId returns the unique block id belonging to the given block number.  | 
 | 69 | +// Note that it is currently equal to the block hash. In the future it might  | 
 | 70 | +// be a different id for future blocks if the log index root becomes part of  | 
 | 71 | +// consensus and therefore rendering the index with the new head will happen  | 
 | 72 | +// before the hash of that new head is available.  | 
 | 73 | +func (cv *ChainView) getBlockId(number uint64) common.Hash {  | 
 | 74 | +	if number > cv.headNumber {  | 
 | 75 | +		panic("invalid block number")  | 
 | 76 | +	}  | 
 | 77 | +	return cv.blockHash(number)  | 
 | 78 | +}  | 
 | 79 | + | 
 | 80 | +// getReceipts returns the set of receipts belonging to the block at the given  | 
 | 81 | +// block number.  | 
 | 82 | +func (cv *ChainView) getReceipts(number uint64) types.Receipts {  | 
 | 83 | +	if number > cv.headNumber {  | 
 | 84 | +		panic("invalid block number")  | 
 | 85 | +	}  | 
 | 86 | +	return cv.chain.GetReceiptsByHash(cv.blockHash(number))  | 
 | 87 | +}  | 
 | 88 | + | 
 | 89 | +// limitedView returns a new chain view that is a truncated version of the parent view.  | 
 | 90 | +func (cv *ChainView) limitedView(newHead uint64) *ChainView {  | 
 | 91 | +	if newHead >= cv.headNumber {  | 
 | 92 | +		return cv  | 
 | 93 | +	}  | 
 | 94 | +	return NewChainView(cv.chain, newHead, cv.blockHash(newHead))  | 
 | 95 | +}  | 
 | 96 | + | 
 | 97 | +// equalViews returns true if the two chain views are equivalent.  | 
 | 98 | +func equalViews(cv1, cv2 *ChainView) bool {  | 
 | 99 | +	if cv1 == nil || cv2 == nil {  | 
 | 100 | +		return false  | 
 | 101 | +	}  | 
 | 102 | +	return cv1.headNumber == cv2.headNumber && cv1.getBlockId(cv1.headNumber) == cv2.getBlockId(cv2.headNumber)  | 
 | 103 | +}  | 
 | 104 | + | 
 | 105 | +// matchViews returns true if the two chain views are equivalent up until the  | 
 | 106 | +// specified block number. If the specified number is higher than one of the  | 
 | 107 | +// heads then false is returned.  | 
 | 108 | +func matchViews(cv1, cv2 *ChainView, number uint64) bool {  | 
 | 109 | +	if cv1 == nil || cv2 == nil {  | 
 | 110 | +		return false  | 
 | 111 | +	}  | 
 | 112 | +	if cv1.headNumber < number || cv2.headNumber < number {  | 
 | 113 | +		return false  | 
 | 114 | +	}  | 
 | 115 | +	if number == cv1.headNumber || number == cv2.headNumber {  | 
 | 116 | +		return cv1.getBlockId(number) == cv2.getBlockId(number)  | 
 | 117 | +	}  | 
 | 118 | +	return cv1.getBlockHash(number) == cv2.getBlockHash(number)  | 
 | 119 | +}  | 
 | 120 | + | 
 | 121 | +// extendNonCanonical checks whether the previously known reverse list of head  | 
 | 122 | +// hashes still ends with one that is canonical on the underlying blockchain.  | 
 | 123 | +// If necessary then it traverses further back on the header chain and adds  | 
 | 124 | +// more hashes to the list.  | 
 | 125 | +func (cv *ChainView) extendNonCanonical() bool {  | 
 | 126 | +	for {  | 
 | 127 | +		hash, number := cv.hashes[len(cv.hashes)-1], cv.headNumber-uint64(len(cv.hashes)-1)  | 
 | 128 | +		if cv.chain.GetCanonicalHash(number) == hash {  | 
 | 129 | +			return true  | 
 | 130 | +		}  | 
 | 131 | +		if number == 0 {  | 
 | 132 | +			log.Error("Unknown genesis block hash found")  | 
 | 133 | +			return false  | 
 | 134 | +		}  | 
 | 135 | +		header := cv.chain.GetHeader(hash, number)  | 
 | 136 | +		if header == nil {  | 
 | 137 | +			log.Error("Header not found", "number", number, "hash", hash)  | 
 | 138 | +			return false  | 
 | 139 | +		}  | 
 | 140 | +		cv.hashes = append(cv.hashes, header.ParentHash)  | 
 | 141 | +	}  | 
 | 142 | +}  | 
 | 143 | + | 
 | 144 | +// blockHash returns the given block hash without doing the head number check.  | 
 | 145 | +func (cv *ChainView) blockHash(number uint64) common.Hash {  | 
 | 146 | +	if number+uint64(len(cv.hashes)) <= cv.headNumber {  | 
 | 147 | +		hash := cv.chain.GetCanonicalHash(number)  | 
 | 148 | +		if !cv.extendNonCanonical() {  | 
 | 149 | +			return common.Hash{}  | 
 | 150 | +		}  | 
 | 151 | +		if number+uint64(len(cv.hashes)) <= cv.headNumber {  | 
 | 152 | +			return hash  | 
 | 153 | +		}  | 
 | 154 | +	}  | 
 | 155 | +	return cv.hashes[cv.headNumber-number]  | 
 | 156 | +}  | 
0 commit comments