@@ -128,6 +128,16 @@ bool BlockFilterIndex::CustomInit(const std::optional<interfaces::BlockKey>& blo
128128 m_next_filter_pos.nFile = 0 ;
129129 m_next_filter_pos.nPos = 0 ;
130130 }
131+
132+ if (block) {
133+ auto op_last_header = ReadFilterHeader (block->height , block->hash );
134+ if (!op_last_header) {
135+ LogError (" Cannot read last block filter header; index may be corrupted\n " );
136+ return false ;
137+ }
138+ m_last_header = *op_last_header;
139+ }
140+
131141 return true ;
132142}
133143
@@ -241,7 +251,6 @@ std::optional<uint256> BlockFilterIndex::ReadFilterHeader(int height, const uint
241251bool BlockFilterIndex::CustomAppend (const interfaces::BlockInfo& block)
242252{
243253 CBlockUndo block_undo;
244- uint256 prev_header;
245254
246255 if (block.height > 0 ) {
247256 // pindex variable gives indexing code access to node internals. It
@@ -250,15 +259,14 @@ bool BlockFilterIndex::CustomAppend(const interfaces::BlockInfo& block)
250259 if (!m_chainstate->m_blockman .UndoReadFromDisk (block_undo, *pindex)) {
251260 return false ;
252261 }
253-
254- auto op_prev_header = ReadFilterHeader (block.height - 1 , *Assert (block.prev_hash ));
255- if (!op_prev_header) return false ;
256- prev_header = *op_prev_header;
257262 }
258263
259264 BlockFilter filter (m_filter_type, *Assert (block.data ), block_undo);
260265
261- return Write (filter, block.height , filter.ComputeHeader (prev_header));
266+ const uint256& header = filter.ComputeHeader (m_last_header);
267+ bool res = Write (filter, block.height , header);
268+ if (res) m_last_header = header; // update last header
269+ return res;
262270}
263271
264272bool BlockFilterIndex::Write (const BlockFilter& filter, uint32_t block_height, const uint256& filter_header)
@@ -326,6 +334,8 @@ bool BlockFilterIndex::CustomRewind(const interfaces::BlockKey& current_tip, con
326334 batch.Write (DB_FILTER_POS, m_next_filter_pos);
327335 if (!m_db->WriteBatch (batch)) return false ;
328336
337+ // Update cached header
338+ m_last_header = *Assert (ReadFilterHeader (new_tip.height , new_tip.hash ));
329339 return true ;
330340}
331341
0 commit comments