Skip to content

Comments

fix: peer order#6894

Merged
SWvheerden merged 2 commits intotari-project:developmentfrom
SWvheerden:sw_fix_peer_order
Mar 31, 2025
Merged

fix: peer order#6894
SWvheerden merged 2 commits intotari-project:developmentfrom
SWvheerden:sw_fix_peer_order

Conversation

@SWvheerden
Copy link
Collaborator

@SWvheerden SWvheerden commented Mar 27, 2025

Description

Fixes sync peer order to ensure it picks the best peer

Summary by CodeRabbit

  • New Features

    • Enhanced peer creation to allow specifying accumulated difficulty, with a reliable default when no value is provided.
  • Refactor

    • Improved the sorting logic to ensure peers are ordered accurately based on their accumulated difficulty.
  • Tests

    • Added validation to confirm that peers are sorted correctly by their accumulated difficulty values.

@SWvheerden SWvheerden requested a review from a team as a code owner March 27, 2025 13:54
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 27, 2025

Walkthrough

This change updates the comparison logic of the SyncPeer struct by revising the Ord implementation to use the peer’s accumulated difficulty correctly. The generate_peer function now accepts an additional optional parameter for accumulated difficulty, defaulting to 1 if not provided. A new test case it_sorts_by_pow confirms that peers sort correctly based on their updated accumulated difficulty values.

Changes

File(s) Change Summary
base_layer/core/.../sync_peer.rs & Test module Modified the Ord implementation in SyncPeer to compare peers using the other peer's accumulated difficulty. Updated generate_peer to accept an optional accumulated_difficulty parameter (defaulting to 1) and added a test case it_sorts_by_pow for sorting verification.

Sequence Diagram(s)

sequenceDiagram
    participant Test
    participant PeerGenerator
    participant SyncPeer

    Test->>PeerGenerator: generate_peer(latency, accumulated_difficulty)
    PeerGenerator->>SyncPeer: Create new SyncPeer with given parameters
    Note over SyncPeer: accumulated_difficulty is set (default 1 if None)
    Test->>SyncPeer: Invoke comparison (cmp) between peers
    SyncPeer-->>Test: Return comparison result based on accumulated difficulty
Loading

Possibly related PRs

  • fix: sync peer ordering #6802: Alters the sorting logic for sync_peers to prioritize claimed difficulty, which is closely related to the changes in the Ord implementation.

Suggested reviewers

  • stringhandler

Poem

I’m a rabbit hopping through the code,
Sorting peers on a digital road.
Accumulated diff now leads the way,
New tests ensure the order’s okay.
In every jump, a line of cheer—
CodeRabbit’s rhythm, bright and clear!
🐰✨

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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
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/base_node/sync/sync_peer.rs (1)

193-223: Added test to verify sorting by difficulty

The new test case effectively verifies that peers are correctly sorted by their accumulated difficulty in descending order (highest first). This is an important addition that directly validates the fix made to the Ord implementation.

There's a small inaccuracy in the comment on line 207. It states "because difficulty is identical, it should sort by latency" but this test specifically uses different difficulty values to test sorting by difficulty, not latency.

- // Sort the list; because difficulty is identical, it should sort by latency
+ // Sort the list; it should sort by difficulty in descending order
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a7cdcd7 and e5ac660.

📒 Files selected for processing (1)
  • base_layer/core/src/base_node/sync/sync_peer.rs (4 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
base_layer/core/src/base_node/sync/sync_peer.rs (2)
base_layer/core/src/base_node/state_machine_service/states/header_sync.rs (1)
  • new (50-71)
base_layer/core/src/base_node/proto/chain_metadata.rs (1)
  • from (72-84)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: ci
  • GitHub Check: cargo check with stable
  • GitHub Check: Cucumber tests / Base Layer
🔇 Additional comments (2)
base_layer/core/src/base_node/sync/sync_peer.rs (2)

109-113: Corrected peer sorting logic to prioritize higher difficulty

The change fixes the Ord implementation to properly sort peers by their accumulated difficulty in descending order. This ensures that peers with higher accumulated difficulty (which typically have more up-to-date chains) are preferred during synchronization.

Previously, the comparison was incorrectly causing peers with lower difficulty to be prioritized. This change aligns with the PR objective of fixing peer order during synchronization.


150-163: Enhanced test flexibility by adding difficulty parameter

Good enhancement to the generate_peer function by adding an optional accumulated_difficulty parameter with a default value. This allows test cases to create peers with specific difficulty values, which is essential for thorough testing of sorting behavior.

Copy link
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/base_node/sync/sync_peer.rs (1)

193-222: Added comprehensive test for difficulty-based peer sorting.

The new test it_sorts_by_pow effectively verifies that peers are sorted correctly based on their accumulated difficulty in descending order. This test ensures that the changes to the Ord implementation work as intended.

Consider using a more descriptive name for the test that clearly indicates it tests the descending order of peers by difficulty, such as it_sorts_by_pow_in_descending_order.

-        fn it_sorts_by_pow() {
+        fn it_sorts_by_pow_in_descending_order() {
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e5ac660 and 1e6782f.

📒 Files selected for processing (1)
  • base_layer/core/src/base_node/sync/sync_peer.rs (4 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
base_layer/core/src/base_node/sync/sync_peer.rs (2)
base_layer/core/src/base_node/state_machine_service/states/header_sync.rs (1)
  • new (50-71)
base_layer/core/src/base_node/proto/chain_metadata.rs (1)
  • from (72-84)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: test (nextnet, nextnet)
  • GitHub Check: ci
🔇 Additional comments (4)
base_layer/core/src/base_node/sync/sync_peer.rs (4)

109-113: Fixed peer comparison logic to correctly prioritize peers with higher accumulated difficulty.

The comparison logic has been revised to correctly compare peers by accumulated difficulty in descending order. This fix ensures that peers with higher accumulated difficulty (which likely have a more up-to-date blockchain) are prioritized during synchronization, which is the expected behavior for blockchain nodes.


150-158: LGTM: Added optional accumulated difficulty parameter to generate_peer function.

The generate_peer function has been enhanced to accept an optional accumulated difficulty parameter, defaulting to 1 if not provided. This improvement allows for creating test peers with specific difficulty values, which is useful for testing the sorting behavior.


161-161: LGTM: Updated ChainMetadata creation to use the provided accumulated_difficulty.

The ChainMetadata object creation now correctly incorporates the peer's accumulated difficulty, which ensures that test peers accurately reflect the intended difficulty values for testing purposes.


173-179: LGTM: Updated generate_peer calls with the new parameter.

All calls to generate_peer have been properly updated to include the new None parameter for accumulated difficulty, ensuring compatibility with the updated function signature.

@github-actions
Copy link

Test Results (CI)

    3 files    129 suites   42m 19s ⏱️
1 353 tests 1 353 ✅ 0 💤 0 ❌
4 057 runs  4 057 ✅ 0 💤 0 ❌

Results for commit 1e6782f.

@github-actions
Copy link

Test Results (Integration tests)

36 tests   36 ✅  15m 26s ⏱️
11 suites   0 💤
 2 files     0 ❌

Results for commit 1e6782f.

Copy link
Contributor

@hansieodendaal hansieodendaal left a comment

Choose a reason for hiding this comment

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

utACK

Copy link
Contributor

@hansieodendaal hansieodendaal left a comment

Choose a reason for hiding this comment

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

utACK

@SWvheerden SWvheerden merged commit f7fe98e into tari-project:development Mar 31, 2025
18 checks passed
@SWvheerden SWvheerden deleted the sw_fix_peer_order branch March 31, 2025 09:31
sdbondi added a commit to sdbondi/tari that referenced this pull request Apr 2, 2025
* development: (412 commits)
  chore: new release v1.13.1-pre.0 (tari-project#6898)
  fix: excess sig order in the tx tab (tari-project#6897)
  chore: ensure thread safety (tari-project#6896)
  fix: peer order (tari-project#6894)
  fix: use plain string for grpc address (tari-project#6881)
  fix: startup arg (tari-project#6889)
  feat: add num connections to network state (tari-project#6884)
  refactor: reduce logging to make it less noisy (tari-project#6882)
  feat: check coinbase count (tari-project#6880)
  chore(deps): bump dorny/test-reporter from 1 to 2 (tari-project#6883)
  chore: update readme (tari-project#6878)
  fix:  libtor cli option (tari-project#6877)
  chore: new version v1.13.0-pre.0 (tari-project#6875)
  chore: reset network (tari-project#6874)
  fix:  prune mode validation (tari-project#6873)
  perf: remove duplicate metadata signature verification (tari-project#6866)
  feat: remove static moneroD response (tari-project#6867)
  chore(ci): updates to pull version from workspace (tari-project#6868)
  chore(ci): binaries build continue-on-error when release (tari-project#6865)
  chore: handle seed words env var (tari-project#6855)
  ...
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