aboutsummaryrefslogtreecommitdiff
path: root/src/index
diff options
context:
space:
mode:
authorfurszy <matiasfurszyfer@protonmail.com>2024-02-23 16:31:13 -0300
committerfurszy <matiasfurszyfer@protonmail.com>2024-03-12 11:55:04 -0300
commitf1469eb45469672046c5793b44863f606736c853 (patch)
tree1f706a6f01fefc753415fc9735d3ee7bc09b4e18 /src/index
parenta6756ecdb2f1ac960433412807aa377d1ee80d05 (diff)
index: cache last block filter header
Avoid disk read operations on every new processed block.
Diffstat (limited to 'src/index')
-rw-r--r--src/index/blockfilterindex.cpp22
-rw-r--r--src/index/blockfilterindex.h3
2 files changed, 19 insertions, 6 deletions
diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp
index 204e5d7e18..41bdca9df5 100644
--- a/src/index/blockfilterindex.cpp
+++ b/src/index/blockfilterindex.cpp
@@ -128,6 +128,16 @@ bool BlockFilterIndex::CustomInit(const std::optional<interfaces::BlockKey>& blo
m_next_filter_pos.nFile = 0;
m_next_filter_pos.nPos = 0;
}
+
+ if (block) {
+ auto op_last_header = ReadFilterHeader(block->height, block->hash);
+ if (!op_last_header) {
+ LogError("Cannot read last block filter header; index may be corrupted\n");
+ return false;
+ }
+ m_last_header = *op_last_header;
+ }
+
return true;
}
@@ -241,7 +251,6 @@ std::optional<uint256> BlockFilterIndex::ReadFilterHeader(int height, const uint
bool BlockFilterIndex::CustomAppend(const interfaces::BlockInfo& block)
{
CBlockUndo block_undo;
- uint256 prev_header;
if (block.height > 0) {
// pindex variable gives indexing code access to node internals. It
@@ -250,15 +259,14 @@ bool BlockFilterIndex::CustomAppend(const interfaces::BlockInfo& block)
if (!m_chainstate->m_blockman.UndoReadFromDisk(block_undo, *pindex)) {
return false;
}
-
- auto op_prev_header = ReadFilterHeader(block.height - 1, *Assert(block.prev_hash));
- if (!op_prev_header) return false;
- prev_header = *op_prev_header;
}
BlockFilter filter(m_filter_type, *Assert(block.data), block_undo);
- return Write(filter, block.height, filter.ComputeHeader(prev_header));
+ const uint256& header = filter.ComputeHeader(m_last_header);
+ bool res = Write(filter, block.height, header);
+ if (res) m_last_header = header; // update last header
+ return res;
}
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
batch.Write(DB_FILTER_POS, m_next_filter_pos);
if (!m_db->WriteBatch(batch)) return false;
+ // Update cached header
+ m_last_header = *Assert(ReadFilterHeader(new_tip.height, new_tip.hash));
return true;
}
diff --git a/src/index/blockfilterindex.h b/src/index/blockfilterindex.h
index 01ba1025c8..cdb9563fb8 100644
--- a/src/index/blockfilterindex.h
+++ b/src/index/blockfilterindex.h
@@ -42,6 +42,9 @@ private:
/** cache of block hash to filter header, to avoid disk access when responding to getcfcheckpt. */
std::unordered_map<uint256, uint256, FilterHeaderHasher> m_headers_cache GUARDED_BY(m_cs_headers_cache);
+ // Last computed header to avoid disk reads on every new block.
+ uint256 m_last_header{};
+
bool AllowPrune() const override { return true; }
bool Write(const BlockFilter& filter, uint32_t block_height, const uint256& filter_header);