@@ -128,6 +128,16 @@ bool BlockFilterIndex::CustomInit(const std::optional<interfaces::BlockKey>& blo
128
128
m_next_filter_pos.nFile = 0 ;
129
129
m_next_filter_pos.nPos = 0 ;
130
130
}
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
+
131
141
return true ;
132
142
}
133
143
@@ -241,7 +251,6 @@ std::optional<uint256> BlockFilterIndex::ReadFilterHeader(int height, const uint
241
251
bool BlockFilterIndex::CustomAppend (const interfaces::BlockInfo& block)
242
252
{
243
253
CBlockUndo block_undo;
244
- uint256 prev_header;
245
254
246
255
if (block.height > 0 ) {
247
256
// pindex variable gives indexing code access to node internals. It
@@ -250,15 +259,14 @@ bool BlockFilterIndex::CustomAppend(const interfaces::BlockInfo& block)
250
259
if (!m_chainstate->m_blockman .UndoReadFromDisk (block_undo, *pindex)) {
251
260
return false ;
252
261
}
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;
257
262
}
258
263
259
264
BlockFilter filter (m_filter_type, *Assert (block.data ), block_undo);
260
265
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;
262
270
}
263
271
264
272
bool 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
326
334
batch.Write (DB_FILTER_POS, m_next_filter_pos);
327
335
if (!m_db->WriteBatch (batch)) return false ;
328
336
337
+ // Update cached header
338
+ m_last_header = *Assert (ReadFilterHeader (new_tip.height , new_tip.hash ));
329
339
return true ;
330
340
}
331
341
0 commit comments