Skip to content

Commit a6756ec

Browse files
committed
index: blockfilter, decouple header lookup into its own function
1 parent 331f044 commit a6756ec

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/index/blockfilterindex.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
225241
bool 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);

src/index/blockfilterindex.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class BlockFilterIndex final : public BaseIndex
4646

4747
bool Write(const BlockFilter& filter, uint32_t block_height, const uint256& filter_header);
4848

49+
std::optional<uint256> ReadFilterHeader(int height, const uint256& expected_block_hash);
50+
4951
protected:
5052
bool CustomInit(const std::optional<interfaces::BlockKey>& block) override;
5153

0 commit comments

Comments
 (0)