Skip to content

feat!: vm calc height#7082

Merged
SWvheerden merged 4 commits intotari-project:developmentfrom
SWvheerden:sw_fix_vm_calc
May 22, 2025
Merged

feat!: vm calc height#7082
SWvheerden merged 4 commits intotari-project:developmentfrom
SWvheerden:sw_fix_vm_calc

Conversation

@SWvheerden
Copy link
Copy Markdown
Collaborator

@SWvheerden SWvheerden commented May 22, 2025

Description

Changes the vm key header hash height to be more reorg resilient and keep the same height as monero,

Summary by CodeRabbit

  • Refactor
    • Improved the calculation method for selecting the reference block height used in proof-of-work and difficulty validation, enhancing consistency by introducing a new helper function with updated alignment logic.
  • Bug Fixes
    • Enhanced block header retrieval accuracy during difficulty checks to ensure more reliable validation outcomes.

@SWvheerden SWvheerden requested a review from a team as a code owner May 22, 2025 11:49
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented May 22, 2025

Walkthrough

The changes update the calculation used to determine the block height for fetching a reference block header, replacing the previous inline modulo 2000 logic with a centralized function tari_rx_vm_key_height that computes the height based on modulo 2048 minus 64. This adjustment is applied consistently across block template creation, RandomXT difficulty checks, and header validation logic.

Changes

File(s) Change Summary
applications/minotari_node/src/grpc/base_node_grpc_server.rs Replaced inline modulo 2000 block header height calculations with calls to tari_rx_vm_key_height.
base_layer/core/src/base_node/comms_interface/inbound_handlers.rs Updated difficulty check to use tari_rx_vm_key_height instead of manual modulo 2000 calculation.
base_layer/core/src/validation/difficulty_calculator.rs Added tari_rx_vm_key_height function; refactored VM key height calculation to use modulo 2048 - 64; added tests.
base_layer/core/src/validation/header/header_full_validator.rs Replaced direct modulo 2000 height calculation with tari_rx_vm_key_height for VM key header fetch.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Node
    participant ChainDB

    Client->>Node: Request new block/template or validate block
    Node->>ChainDB: Fetch header at tari_rx_vm_key_height(height)
    ChainDB-->>Node: Return reference header
    Node-->>Client: Respond with new block/template or validation result
Loading

Poem

A hop, a skip, a leap anew,
From 2000 to 2048 we flew!
Subtracting sixty-four,
We check the chain once more—
Bunnies love their numbers true,
Hopping heights for hashes blue!
🐇✨

✨ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

@github-actions
Copy link
Copy Markdown

github-actions bot commented May 22, 2025

Test Results (CI)

    2 files     60 suites   34m 47s ⏱️
1 093 tests 1 089 ✅ 0 💤 4 ❌
2 181 runs  2 173 ✅ 0 💤 8 ❌

For more details on these failures, see this check.

Results for commit 92bf305.

♻️ This comment has been updated with latest results.

Copy link
Copy Markdown
Contributor

@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: 0

🧹 Nitpick comments (1)
base_layer/core/src/validation/difficulty_calculator.rs (1)

74-80: Consider adding documentation for the function implementation

The function logic looks sound and correctly implements the height calculation with reorg protection. However, the bitwise operation might not be immediately clear to all developers.

+/// Returns the block height to use for RandomX VM key selection.
+/// 
+/// For heights <= 2112, returns 0 for backward compatibility.
+/// For heights > 2112, returns height aligned to the nearest lower multiple of 2048,
+/// with a 65-block reorg safety offset to protect against chain reorganizations.
+/// 
+/// The bitwise operation `& !(TARI_RX_VM_KEY_BLOCK_SWAP - 1)` efficiently rounds down
+/// to the nearest multiple of 2048 by clearing the lower 11 bits.
 pub fn tari_rx_vm_key_height(height: u64) -> u64 {
     if height <= TARI_RX_VM_KEY_BLOCK_SWAP + TARI_RX_VM_KEY_REORG_SAFETY_NUMBER {
         0
     } else {
         (height - TARI_RX_VM_KEY_REORG_SAFETY_NUMBER - 1) & !(TARI_RX_VM_KEY_BLOCK_SWAP - 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 990061d and 92bf305.

📒 Files selected for processing (1)
  • base_layer/core/src/validation/difficulty_calculator.rs (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: test (testnet, esmeralda)
  • GitHub Check: test (mainnet, stagenet)
  • GitHub Check: test (nextnet, nextnet)
  • GitHub Check: ci
  • GitHub Check: cargo check with stable
🔇 Additional comments (4)
base_layer/core/src/validation/difficulty_calculator.rs (4)

31-32: Well-defined constants improve readability

These constants nicely parameterize the VM key calculation, making the code more maintainable and self-documenting. The values align with the PR objective of making the height calculation more resilient to blockchain reorganizations.


59-59: Good refactoring to use centralized function

Replacing the inline calculation with a call to the dedicated function centralizes the logic and ensures consistency across the codebase, which is a good practice.


82-124: Comprehensive test suite

Excellent test coverage with various heights, including boundary cases around multiples of 2048 and the reorg safety threshold. The tests clearly verify the expected behavior of the function.


74-80:

Details

✅ Verification successful

Verify consensus impact across the codebase

The change from height - (height % 2000) to this new algorithm could have consensus implications. Let's verify that all references to the old calculation have been updated consistently.


🏁 Script executed:

#!/bin/bash
# Search for any remaining instances of the old calculation pattern
echo "Searching for remaining instances of the old calculation pattern..."
rg "height -\s*\(height %\s*2000\)" --type rust

# Search for all places using the new function
echo "Searching for all places using the new function..."
rg "tari_rx_vm_key_height" --type rust

Length of output: 2669


Consensus Calculation Fully Updated

All instances of the old formula height - (height % 2000) have been replaced with calls to tari_rx_vm_key_height. A repository-wide search confirms:

• No remaining matches for the old pattern
• All references now invoke tari_rx_vm_key_height in:

  • difficulty_calculator.rs
  • header_full_validator.rs
  • inbound_handlers.rs
  • base_node_grpc_server.rs

No further action required.

@SWvheerden SWvheerden merged commit c7bec97 into tari-project:development May 22, 2025
12 of 16 checks passed
@SWvheerden SWvheerden deleted the sw_fix_vm_calc branch May 22, 2025 13:58
@coderabbitai coderabbitai bot mentioned this pull request May 23, 2025
@coderabbitai coderabbitai bot mentioned this pull request Sep 25, 2025
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.

2 participants