Skip to content
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

blockchain: Optimize old block ver upgrade checks. #2912

Merged

Conversation

davecgh
Copy link
Member

@davecgh davecgh commented Mar 28, 2022

Currently the code that rejects old block versions once the majority of the network has upgraded is performed using a loop that iterates backwards from latest enforced block version while checking for a majority upgrade at each version. This is inefficient for blocks early in the chain since it means multiple versions need to be checked when it isn't really necessary.

This optimizes the relevant code by taking into account that the relevant enforcement semantics reduce to the following:

  • Block versions greater than or equal to the latest enforced block version can never be rejected for being old
  • Block versions must be rejected when the majority of the network has upgraded to ANY version greater than that version

The following timing information obtained from profiling shows the overall reduction achieved with the optimized code for mainnet:

 existing: Total time to check old versions for 645603 headers: 10.61118s
optimized: Total time to check old versions for 645603 headers:  3.52555s

@davecgh davecgh added this to the 1.8.0 milestone Mar 28, 2022
@davecgh davecgh force-pushed the blockchain_optimize_old_block_ver_checks branch from f213eed to 41e412c Compare March 28, 2022 20:50
Copy link
Member

@rstaudt2 rstaudt2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement. Logic looks correct and ran a sync on mainnet with --assumevalid=0 without issue.

Currently the code that rejects old block versions once the majority of
the network has upgraded is performed using a loop that iterates
backwards from latest enforced block version while checking for a
majority upgrade at each version.  This is inefficient for blocks early
in the chain since it means multiple versions need to be checked when it
isn't really necessary.

This optimizes the relevant code by taking into account that the
relevant enforcement semantics reduce to the following:

- Block versions greater than or equal to the latest enforced block
  version can never be rejected for being old
- Block versions must be rejected when the majority of the network has
  upgraded to ANY version greater than that version

The following timing information obtained from profiling shows the
overall reduction achieved with the optimized code for mainnet:

 existing: Total time to check old versions for 645603 headers: 10.61118s
optimized: Total time to check old versions for 645603 headers:  3.52555s
@davecgh davecgh force-pushed the blockchain_optimize_old_block_ver_checks branch from 41e412c to 5a3f082 Compare April 6, 2022 17:49
@davecgh davecgh merged commit 5a3f082 into decred:master Apr 6, 2022
@davecgh davecgh deleted the blockchain_optimize_old_block_ver_checks branch April 6, 2022 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants