@@ -222,6 +222,22 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
222222 return data_size;
223223}
224224
225+ std::optional<uint256> BlockFilterIndex::ReadFilterHeader (int height, const uint256& expected_block_hash)
226+ {
227+ std::pair<uint256, DBVal> read_out;
228+ if (!m_db->Read (DBHeightKey (height), read_out)) {
229+ return std::nullopt ;
230+ }
231+
232+ if (read_out.first != expected_block_hash) {
233+ LogError (" %s: previous block header belongs to unexpected block %s; expected %s\n " ,
234+ __func__, read_out.first .ToString (), expected_block_hash.ToString ());
235+ return std::nullopt ;
236+ }
237+
238+ return read_out.second .header ;
239+ }
240+
225241bool BlockFilterIndex::CustomAppend (const interfaces::BlockInfo& block)
226242{
227243 CBlockUndo block_undo;
@@ -235,19 +251,9 @@ bool BlockFilterIndex::CustomAppend(const interfaces::BlockInfo& block)
235251 return false ;
236252 }
237253
238- std::pair<uint256, DBVal> read_out;
239- if (!m_db->Read (DBHeightKey (block.height - 1 ), read_out)) {
240- return false ;
241- }
242-
243- uint256 expected_block_hash = *Assert (block.prev_hash );
244- if (read_out.first != expected_block_hash) {
245- LogError (" %s: previous block header belongs to unexpected block %s; expected %s\n " ,
246- __func__, read_out.first .ToString (), expected_block_hash.ToString ());
247- return false ;
248- }
249-
250- prev_header = read_out.second .header ;
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;
251257 }
252258
253259 BlockFilter filter (m_filter_type, *Assert (block.data ), block_undo);
0 commit comments