Skip to content

Commit f1469eb

Browse files
committed
index: cache last block filter header
Avoid disk read operations on every new processed block.
1 parent a6756ec commit f1469eb

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/index/blockfilterindex.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
241251
bool 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

264272
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
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

src/index/blockfilterindex.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class BlockFilterIndex final : public BaseIndex
4242
/** cache of block hash to filter header, to avoid disk access when responding to getcfcheckpt. */
4343
std::unordered_map<uint256, uint256, FilterHeaderHasher> m_headers_cache GUARDED_BY(m_cs_headers_cache);
4444

45+
// Last computed header to avoid disk reads on every new block.
46+
uint256 m_last_header{};
47+
4548
bool AllowPrune() const override { return true; }
4649

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

0 commit comments

Comments
 (0)