-
Notifications
You must be signed in to change notification settings - Fork 36.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
index: Verify the block filter hash when reading the filter from disk. #24832
Changes from all commits
b0a53d5
299023c
aee9a81
e734228
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,6 +5,7 @@ | |||||
#include <map> | ||||||
|
||||||
#include <dbwrapper.h> | ||||||
#include <hash.h> | ||||||
#include <index/blockfilterindex.h> | ||||||
#include <node/blockstorage.h> | ||||||
#include <util/system.h> | ||||||
|
@@ -143,18 +144,22 @@ bool BlockFilterIndex::CommitInternal(CDBBatch& batch) | |||||
return BaseIndex::CommitInternal(batch); | ||||||
} | ||||||
|
||||||
bool BlockFilterIndex::ReadFilterFromDisk(const FlatFilePos& pos, BlockFilter& filter) const | ||||||
bool BlockFilterIndex::ReadFilterFromDisk(const FlatFilePos& pos, const uint256& hash, BlockFilter& filter) const | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Since this function also has a
Suggested change
|
||||||
{ | ||||||
CAutoFile filein(m_filter_fileseq->Open(pos, true), SER_DISK, CLIENT_VERSION); | ||||||
if (filein.IsNull()) { | ||||||
return false; | ||||||
} | ||||||
|
||||||
// Check that the hash of the encoded_filter matches the one stored in the db. | ||||||
uint256 block_hash; | ||||||
std::vector<uint8_t> encoded_filter; | ||||||
try { | ||||||
filein >> block_hash >> encoded_filter; | ||||||
filter = BlockFilter(GetFilterType(), block_hash, std::move(encoded_filter)); | ||||||
uint256 result; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
CHash256().Write(encoded_filter).Finalize(result); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think {} list initialization is generally preferred? Here, and in some other places in the PR.
Suggested change
|
||||||
if (result != hash) return error("Checksum mismatch in filter decode."); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be helpful to include |
||||||
filter = BlockFilter(GetFilterType(), block_hash, std::move(encoded_filter), /*skip_decode_check=*/true); | ||||||
} | ||||||
Comment on lines
+154
to
163
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you consider moving the hash checking into a separate function for readability and maintainability? |
||||||
catch (const std::exception& e) { | ||||||
return error("%s: Failed to deserialize block filter from disk: %s", __func__, e.what()); | ||||||
|
@@ -381,7 +386,7 @@ bool BlockFilterIndex::LookupFilter(const CBlockIndex* block_index, BlockFilter& | |||||
return false; | ||||||
} | ||||||
|
||||||
return ReadFilterFromDisk(entry.pos, filter_out); | ||||||
return ReadFilterFromDisk(entry.pos, entry.hash, filter_out); | ||||||
} | ||||||
|
||||||
bool BlockFilterIndex::LookupFilterHeader(const CBlockIndex* block_index, uint256& header_out) | ||||||
|
@@ -425,7 +430,7 @@ bool BlockFilterIndex::LookupFilterRange(int start_height, const CBlockIndex* st | |||||
filters_out.resize(entries.size()); | ||||||
auto filter_pos_it = filters_out.begin(); | ||||||
for (const auto& entry : entries) { | ||||||
if (!ReadFilterFromDisk(entry.pos, *filter_pos_it)) { | ||||||
if (!ReadFilterFromDisk(entry.pos, entry.hash, *filter_pos_it)) { | ||||||
return false; | ||||||
} | ||||||
++filter_pos_it; | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could name it GCSFilterGetHash instead of GCSBlockFilterGetHash for consistency with the other benchmarks.