Skip to content

Conversation

@knst
Copy link
Collaborator

@knst knst commented May 4, 2025

Issue being fixed or feature implemented

Validation of chainlocks requires to read previous block from disk for every new block.

What was done?

Added CL from CbTx of tip of the chain.

How Has This Been Tested?

develop:
image

2025-05-04T18:36:29Z [bench]         - CheckCbTxBestChainlock: 3.34ms [18.68s]
2025-05-04T18:36:29Z [bench]   - Connect total: 54.40ms [94.48s (16.14ms/blk)]

PR:
image

2025-05-04T18:53:18Z [bench]         - CheckCbTxBestChainlock: 3.15ms [16.41s]
2025-05-04T18:53:18Z [bench]   - Connect total: 52.89ms [90.75s (15.51ms/blk)]

perf looks a bit confusing (like cpu spent for CheckCbTxBestChainlock is close to zero which contradicts to logs time (16seconds vs 18seconds). It happened due to missing data for bls / gmp - somehow they are not attached to the caller, but shown as a root nodes without proper stacktrace). Detailed break-down is:

2025-05-04T19:50:37Z [bench]         - CheckCbTxBestChainlock::payload: 0.22ms [1.34s]
2025-05-04T19:50:37Z [bench]         - CheckCbTxBestChainlock::get-cb-cl: 0.52ms [2.50s]
2025-05-04T19:50:37Z [bench]         - CheckCbTxBestChainlock::validate: 2.63ms [14.94s] <--- missing in perf
2025-05-04T19:50:37Z [bench]         - CheckCbTxBestChainlock: 3.38ms [18.87s]

Breaking Changes

N/A

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation
  • I have assigned this pull request to a milestone

@coderabbitai
Copy link

coderabbitai bot commented May 4, 2025

Walkthrough

The changes modify the CheckCbTxBestChainlock function by adding a static mutex-protected cache to store the previous block index and its associated coinbase chainlock data, enabling reuse of this data to avoid repeated expensive lookups. In GetNonNullCoinbaseChainlock, the version activation check was updated from DIP0003 to deployment V20 to reflect that chainlock data in coinbase transactions only exists from V20 onwards. Additionally, explicit coinbase transaction version checks were removed for simplification. Separate changes add assertions on activation heights in regression test parameters and introduce test activation height arguments for V20 in functional tests, without altering public interfaces.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 78e73f8 and 18044c9.

📒 Files selected for processing (4)
  • src/chainparams.cpp (1 hunks)
  • src/evo/cbtx.cpp (4 hunks)
  • test/functional/feature_assumevalid.py (1 hunks)
  • test/functional/feature_csv_activation.py (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • test/functional/feature_csv_activation.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/evo/cbtx.cpp
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: x86_64-pc-linux-gnu_nowallet / Build depends
  • GitHub Check: x86_64-apple-darwin / Build depends
  • GitHub Check: x86_64-w64-mingw32 / Build depends
  • GitHub Check: x86_64-pc-linux-gnu_multiprocess / Build depends
  • GitHub Check: x86_64-pc-linux-gnu / Build depends
  • GitHub Check: arm-linux-gnueabihf / Build depends
🔇 Additional comments (2)
src/chainparams.cpp (1)

924-924: Ensures V20 features are properly sequenced with DIP0003 dependencies.

This assertion ensures that the V20 protocol version (which contains the chainlock caching mechanism mentioned in the PR objectives) can't be activated before the required DIP0003 features. This is critical for maintaining the proper dependency chain in consensus parameters.

test/functional/feature_assumevalid.py (1)

65-65: Properly configures V20 activation height for test environment.

Adding the -testactivationheight=v20@9000 parameter ensures that the V20 protocol (containing the chainlock caching optimization) activates at block height 9000, which is consistent with the DIP0003 activation height. This is aligned with the assertion added in chainparams.cpp, maintaining proper protocol version sequencing in the test environment.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fc96190 and f3a9f4e.

📒 Files selected for processing (1)
  • src/evo/cbtx.cpp (3 hunks)
🧰 Additional context used
🪛 GitHub Actions: Clang Diff Format Check
src/evo/cbtx.cpp

[error] 338-345: Clang format differences found. Code formatting does not match the expected style. Please run clang-format to fix formatting issues.

⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: win64-build / Build source
  • GitHub Check: linux64_ubsan-build / Build source
  • GitHub Check: linux64-build / Build source
  • GitHub Check: linux64_sqlite-build / Build source
  • GitHub Check: linux64_fuzz-build / Build source
  • GitHub Check: arm-linux-build / Build source
  • GitHub Check: linux64_nowallet-build / Build source
🔇 Additional comments (3)
src/evo/cbtx.cpp (3)

340-356: Good performance optimization with the caching mechanism

The implemented caching strategy for chainlock data is an effective way to avoid redundant disk reads through GetNonNullCoinbaseChainlock when processing consecutive blocks. The mutex ensures thread safety when accessing the cached data.

The code properly checks if the previous block index matches the cached one before reusing the cached chainlock, which prevents stale data issues.


384-389: LGTM: Proper cache update after validation

The code correctly updates the cache with the current block's chainlock data after validation, which ensures the cache remains valid for subsequent blocks.


461-464: Effective optimization for pre-V20 blocks

This is a good optimization that avoids unnecessary disk reads for blocks before V20 activation, as those blocks don't have chainlock data in the coinbase transaction. The check is placed before the existing DIP0003 check, which is correct since V20 comes after DIP0003 in the deployment timeline.

@knst knst marked this pull request as draft May 5, 2025 08:59
@knst knst force-pushed the perf-cl-cbtx-cache branch 2 times, most recently from 7ebc7af to 78e73f8 Compare May 5, 2025 14:09
@knst knst marked this pull request as ready for review May 6, 2025 08:14
@knst knst added this to the 23 milestone May 6, 2025
@knst knst requested review from PastaPastaPasta and UdjinM6 May 6, 2025 08:15
src/evo/cbtx.cpp Outdated
Comment on lines 378 to 382
{
LOCK(cached_mutex);
cached_chainlock = std::make_pair(cbTx.bestCLSignature, cbTx.bestCLHeightDiff);
cached_pindex = pindex;
}
Copy link
Member

Choose a reason for hiding this comment

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

this extra scope is redundant

src/evo/cbtx.cpp Outdated
Comment on lines 389 to 393
{
LOCK(cached_mutex);
cached_chainlock = std::make_pair(cbTx.bestCLSignature, cbTx.bestCLHeightDiff);
cached_pindex = pindex;
}
Copy link
Member

Choose a reason for hiding this comment

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

same

src/evo/cbtx.cpp Outdated
Comment on lines 351 to 350
{
LOCK(cached_mutex);
if (cached_pindex == pindex->pprev) {
use_cached = true;
prevBlockCoinbaseChainlock = cached_chainlock;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{
LOCK(cached_mutex);
if (cached_pindex == pindex->pprev) {
use_cached = true;
prevBlockCoinbaseChainlock = cached_chainlock;
}
}
if (LOCK(cached_mutex); cached_pindex == pindex->pprev) {
use_cached = true;
prevBlockCoinbaseChainlock = cached_chainlock;
}

Comment on lines 349 to 350
std::optional<std::pair<CBLSSignature, uint32_t>> prevBlockCoinbaseChainlock{std::nullopt};
bool use_cached = false;
{
LOCK(cached_mutex);
if (cached_pindex == pindex->pprev) {
use_cached = true;
prevBlockCoinbaseChainlock = cached_chainlock;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

or something like that

Suggested change
std::optional<std::pair<CBLSSignature, uint32_t>> prevBlockCoinbaseChainlock{std::nullopt};
bool use_cached = false;
{
LOCK(cached_mutex);
if (cached_pindex == pindex->pprev) {
use_cached = true;
prevBlockCoinbaseChainlock = cached_chainlock;
}
}
auto [prevBlockCoinbaseChainlock, use_cached] = [&]() {
LOCK(cached_mutex);
if (cached_pindex == pindex->pprev) {
return std::make_pair(cached_chainlock, true);
}
return std::make_pair(std::nullopt, false);
}();```

src/evo/cbtx.cpp Outdated
Comment on lines 341 to 345
{
LOCK(cached_mutex);
cached_chainlock = std::make_pair(cbTx.bestCLSignature, cbTx.bestCLHeightDiff);
cached_pindex = pindex;
}
Copy link
Member

Choose a reason for hiding this comment

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

redundant scope

src/evo/cbtx.cpp Outdated
Comment on lines 354 to 355
use_cached = true;
prevBlockCoinbaseChainlock = cached_chainlock;
Copy link
Member

Choose a reason for hiding this comment

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

isn't this bool the same as prevBlockCoinbaseChainlock.has_value()?

@knst knst force-pushed the perf-cl-cbtx-cache branch from 78e73f8 to 18044c9 Compare May 7, 2025 08:08
@knst knst requested review from PastaPastaPasta and UdjinM6 May 7, 2025 19:23
Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

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

utACK 18044c9

UpdateLLMQTestParametersFromArgs(args, Consensus::LLMQType::LLMQ_TEST_INSTANTSEND);
UpdateLLMQTestParametersFromArgs(args, Consensus::LLMQType::LLMQ_TEST_PLATFORM);
UpdateLLMQInstantSendDIP0024FromArgs(args);
assert(consensus.V20Height >= consensus.DIP0003Height);
Copy link
Member

Choose a reason for hiding this comment

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

why?

Copy link
Member

Choose a reason for hiding this comment

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

@knst knst requested a review from PastaPastaPasta May 8, 2025 14:12
Copy link
Member

@PastaPastaPasta PastaPastaPasta left a comment

Choose a reason for hiding this comment

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

utACK 18044c9

@PastaPastaPasta PastaPastaPasta merged commit 29e2d43 into dashpay:develop May 8, 2025
111 of 116 checks passed
knst pushed a commit to knst/dash that referenced this pull request Sep 15, 2025
…nstead running heavy ReadBlockFromDisk

18044c9 perf: use tiny cache for best chainlock in CbTx instead running heavy ReadBlockFromDisk (Konstantin Akimov)
66c9592 refactor: use more strict scope v20 instead dip0003 (Konstantin Akimov)

Pull request description:

  ## Issue being fixed or feature implemented
  Validation of chainlocks requires to read previous block from disk for every new block.

  ## What was done?
  Added CL from CbTx of tip of the chain.

  ## How Has This Been Tested?
  develop:
  <img width="754" alt="image" src="https://github.com/user-attachments/assets/7e1a5ab4-9634-4fc7-910e-9124d2d742d0" />
  ```
  2025-05-04T18:36:29Z [bench]         - CheckCbTxBestChainlock: 3.34ms [18.68s]
  2025-05-04T18:36:29Z [bench]   - Connect total: 54.40ms [94.48s (16.14ms/blk)]
  ```

  PR:
  <img width="754" alt="image" src="https://github.com/user-attachments/assets/462df0f1-358d-46dd-8a08-f3a0324bb282" />
  ```
  2025-05-04T18:53:18Z [bench]         - CheckCbTxBestChainlock: 3.15ms [16.41s]
  2025-05-04T18:53:18Z [bench]   - Connect total: 52.89ms [90.75s (15.51ms/blk)]
  ```

  `perf` looks a bit confusing (like cpu spent for CheckCbTxBestChainlock is close to zero which contradicts to logs time (16seconds vs 18seconds). It happened due to missing data for bls / gmp - somehow they are not attached to the caller, but shown as a root nodes without proper stacktrace). Detailed break-down is:
  ```
  2025-05-04T19:50:37Z [bench]         - CheckCbTxBestChainlock::payload: 0.22ms [1.34s]
  2025-05-04T19:50:37Z [bench]         - CheckCbTxBestChainlock::get-cb-cl: 0.52ms [2.50s]
  2025-05-04T19:50:37Z [bench]         - CheckCbTxBestChainlock::validate: 2.63ms [14.94s] <--- missing in perf
  2025-05-04T19:50:37Z [bench]         - CheckCbTxBestChainlock: 3.38ms [18.87s]
  ```

  ## Breaking Changes
  N/A

  ## Checklist:
  - [x] I have performed a self-review of my own code
  - [ ] I have commented my code, particularly in hard-to-understand areas
  - [ ] I have added or updated relevant unit/integration/functional/e2e tests
  - [ ] I have made corresponding changes to the documentation
  - [x] I have assigned this pull request to a milestone

ACKs for top commit:
  UdjinM6:
    utACK 18044c9
  PastaPastaPasta:
    utACK 18044c9

Tree-SHA512: 21508505e4b3cec9cba7f3835e2b73a81461d7b626f97efb15d442767fa0862f6ba92ed1101e00b04dbbddab1b7c56da796b15c89d80c95ce9b0246bbbbe02f2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants