-
-
Notifications
You must be signed in to change notification settings - Fork 354
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
Make initial block index loading faster #1420
Conversation
WalkthroughThe update introduces a flexible mechanism for checking the Proof of Work (PoW) of blockchain blocks during client startup. It adds an option to either perform a comprehensive PoW check for all blocks or limit the verification to the most recent ones, depending on a specified flag. This enhancement aims to improve startup efficiency without compromising the integrity of the blockchain. Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
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.
//! By default don't check block index PoW on client startup | ||
static const bool DEFAULT_FULL_BLOCKINDEX_CHECK = false; | ||
//! If not doing full check of block index, check only N of the latest blocks | ||
static const int DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK = 10000; |
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.
The introduction of DEFAULT_FULL_BLOCKINDEX_CHECK
and DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK
constants is a significant change aimed at optimizing the initial block index loading process. However, there are a few considerations and recommendations:
- Documentation: It's crucial to document these constants, explaining their purpose, usage, and the rationale behind the default values. This will aid in maintainability and understanding for future developers.
- Configuration Flexibility: While the PR introduces a mechanism to control the depth of the block index check via a flag, consider exposing
DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK
as a configurable parameter (e.g., via a configuration file or command-line argument). This would provide operators with greater flexibility in tuning the system based on their specific needs or constraints. - Validation: Ensure that there is validation in place for the
DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK
value, especially if it becomes user-configurable. For example, negative values should be disallowed, and there might be a reasonable upper limit to consider.
+ // Documentation for DEFAULT_FULL_BLOCKINDEX_CHECK
+ // Documentation for DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK
+ // Consider making DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK configurable
+ // Add validation for DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
//! By default don't check block index PoW on client startup | |
static const bool DEFAULT_FULL_BLOCKINDEX_CHECK = false; | |
//! If not doing full check of block index, check only N of the latest blocks | |
static const int DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK = 10000; | |
//! By default don't check block index PoW on client startup | |
// Documentation for DEFAULT_FULL_BLOCKINDEX_CHECK | |
static const bool DEFAULT_FULL_BLOCKINDEX_CHECK = false; | |
//! If not doing full check of block index, check only N of the latest blocks | |
// Documentation for DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK | |
// Consider making DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK configurable | |
// Add validation for DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK | |
static const int DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK = 10000; |
|
||
pindexNew->activeDisablingSporks = diskindex.activeDisablingSporks; | ||
|
||
if (!CheckProofOfWork(pindexNew->GetBlockPoWHash(), pindexNew->nBits, consensusParams)) | ||
return error("LoadBlockIndex(): CheckProofOfWork failed: %s", pindexNew->ToString()); | ||
if (fCheckPoWForAllBlocks) { | ||
if (!CheckProofOfWork(pindexNew->GetBlockPoWHash(), pindexNew->nBits, consensusParams)) | ||
return error("LoadBlockIndex(): CheckProofOfWork failed: %s", pindexNew->ToString()); | ||
} | ||
else { | ||
if (pindexNew->nHeight >= firstInLastNBlocksHeight) { | ||
lastNBlocks.insert(std::pair<int, CBlockIndex*>(pindexNew->nHeight, pindexNew)); | ||
if (lastNBlocks.size() > DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK) { | ||
// pop the first element from the map | ||
auto firstElement = lastNBlocks.begin(); | ||
auto elementToPop = firstElement++; | ||
lastNBlocks.erase(elementToPop); | ||
firstInLastNBlocksHeight = firstElement->first; | ||
} | ||
} | ||
} | ||
|
||
pcursor->Next(); | ||
} else { |
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.
📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [359-452]
The modifications to CBlockTreeDB::LoadBlockIndexGuts
introduce a conditional check for Proof of Work (PoW) based on the -fullblockindexcheck
flag and the number of most recent blocks. While this optimization aims to improve the initial block index loading speed, there are several areas that require attention:
- Correctness: The logic for selectively checking PoW for the most recent blocks appears sound. However, ensure thorough testing, especially around edge cases such as blockchain reorganizations that might affect the set of the most recent blocks.
- Performance: This change could significantly impact startup times. It would be beneficial to quantify this impact through benchmarking under various scenarios (e.g., with and without the flag enabled, with different values for the number of blocks to check).
- Maintainability: The added complexity to
LoadBlockIndexGuts
increases the cognitive load for future maintainers. Consider refactoring to improve readability and maintainability. For example, extracting the logic for managinglastNBlocks
into separate functions could make the code more modular and easier to understand. - Error Handling: Ensure that any failures in PoW checks lead to clear and actionable error messages, aiding in troubleshooting potential issues during startup.
+ // Refactor to improve maintainability
+ // Benchmark to quantify performance impact
+ // Ensure thorough testing, especially around edge cases
+ // Improve error handling and messaging
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.
Looks good to me
PR intention
Speed up loading block index from disk.
Code changes brief
Code eliminates the check of PoW for all the block headers except last 10000.
Summary by CodeRabbit