@@ -55,7 +55,9 @@ func NewChainView(chain blockchain, number uint64, hash common.Hash) *ChainView
5555 headNumber : number ,
5656 hashes : []common.Hash {hash },
5757 }
58- cv .extendNonCanonical ()
58+ if ! cv .extendNonCanonical () {
59+ return nil
60+ }
5961 return cv
6062}
6163
@@ -129,7 +131,11 @@ func (cv *ChainView) SharedRange(cv2 *ChainView) common.Range[uint64] {
129131 return common.Range [uint64 ]{}
130132 }
131133 var sharedLen uint64
132- for n := min (cv .headNumber + 1 - uint64 (len (cv .hashes )), cv2 .headNumber + 1 - uint64 (len (cv2 .hashes ))); n <= cv .headNumber && n <= cv2 .headNumber && cv .blockHash (n ) == cv2 .blockHash (n ); n ++ {
134+ for n := min (cv .headNumber + 1 - uint64 (len (cv .hashes )), cv2 .headNumber + 1 - uint64 (len (cv2 .hashes ))); n <= cv .headNumber && n <= cv2 .headNumber ; n ++ {
135+ h1 , h2 := cv .blockHash (n ), cv2 .blockHash (n )
136+ if h1 != h2 || h1 == (common.Hash {}) {
137+ break
138+ }
133139 sharedLen = n + 1
134140 }
135141 return common .NewRange (0 , sharedLen )
@@ -153,10 +159,13 @@ func matchViews(cv1, cv2 *ChainView, number uint64) bool {
153159 if cv1 .headNumber < number || cv2 .headNumber < number {
154160 return false
155161 }
162+ var h1 , h2 common.Hash
156163 if number == cv1 .headNumber || number == cv2 .headNumber {
157- return cv1 .BlockId (number ) == cv2 .BlockId (number )
164+ h1 , h2 = cv1 .BlockId (number ), cv2 .BlockId (number )
165+ } else {
166+ h1 , h2 = cv1 .BlockHash (number ), cv2 .BlockHash (number )
158167 }
159- return cv1 . BlockHash ( number ) == cv2 . BlockHash ( number )
168+ return h1 == h2 && h1 != common. Hash {}
160169}
161170
162171// extendNonCanonical checks whether the previously known reverse list of head
@@ -175,7 +184,10 @@ func (cv *ChainView) extendNonCanonical() bool {
175184 }
176185 header := cv .chain .GetHeader (hash , number )
177186 if header == nil {
178- log .Error ("Header not found" , "number" , number , "hash" , hash )
187+ // Header not found - this can happen after debug_setHead operations
188+ // where blocks have been deleted. Return false to indicate the chain view
189+ // is no longer valid rather than logging repeated errors.
190+ log .Debug ("Header not found during chain view extension" , "number" , number , "hash" , hash )
179191 return false
180192 }
181193 cv .hashes = append (cv .hashes , header .ParentHash )
0 commit comments