Skip to content

Conversation

tolbrino
Copy link
Contributor

@tolbrino tolbrino commented Mar 18, 2025

Due to a race-condition the node could lose track of the on-chain status of a to-be-redeemed ticket. Therefore, now it checks on restart if any ticket is marked as BeingRedeemed and matches the next expected ticket index of its channel. If so, its being reset to Untouched.

Also:

  • Before the unrealized value could span multiple channel epochs which could lead to a drift of the unrealized value and the ticket aggregation logic. The cache is now invalidated at appropriate points.

Refs #6926

@tolbrino tolbrino requested a review from NumberFour8 March 18, 2025 10:53
@tolbrino tolbrino self-assigned this Mar 18, 2025
@tolbrino tolbrino added this to the 2.2.3 milestone Mar 18, 2025
Copy link
Contributor

coderabbitai bot commented Mar 18, 2025

📝 Walkthrough

Walkthrough

This pull request performs a minor version update in the Cargo manifest and modifies the caching mechanism in the SQL database module. The cache key for unrealized_value is changed from a single Hash to a tuple (Hash, U256) to differentiate values by epoch. Several related functions in the ticket manager and ticket operations files have been updated to construct and use the new tuple key. Additional import statements have been added for clarity and correctness in the caching module.

Changes

Files Change Summary
db/sql/Cargo.toml Package version updated from "0.12.0" to "0.12.1".
db/sql/src/cache.rs Added import statements; updated the type of unrealized_value from Cache<Hash, Balance> to Cache<(Hash, U256), Balance>.
db/sql/src/ticket_manager.rs, db/sql/src/tickets.rs Modified cache operations to use a tuple key (channel, epoch) (including inserting, retrieving, and invalidating entries); introduced variable channel_epoch.
db/sql/src/channels.rs Added logic to invalidate unrealized values based on channel ID and epoch during channel updates and insertions.
db/api/src/tickets.rs Added new method fix_channels_next_ticket_state to the HoprDbTicketOperations trait.
chain/actions/src/action_queue.rs Reorganized import statements without functional changes.
chain/indexer/src/handlers.rs Added new import for BlockWithLogs and Log types.
hopr/hopr-lib/src/lib.rs Introduced error handling for ticket state synchronization after chain updates.
logic/strategy/src/aggregating.rs, logic/strategy/src/auto_redeeming.rs Reorganized import statements without functional changes.

Sequence Diagram(s)

sequenceDiagram
    participant TM as TicketManager
    participant C as Cache (HoprDbCaches)
    participant TO as TicketOperations

    TM->>C: Insert ticket (compute channel_epoch, then call insert((channel, epoch), updated_value))
    C-->>TM: Confirm updated cache entry
    TM->>C: Retrieve unrealized value using key (channel, epoch)
    C-->>TM: Return cached unrealized value
    TO->>C: Invalidate cache entry using key (channel, epoch)
    C-->>TO: Confirm invalidation
Loading

Possibly related PRs

  • Log the channel_id when aggregation fails #6534: The changes in the main PR, which involve updating the unrealized_value field type in the HoprDbCaches struct, are related to the modifications in the retrieved PR that also involve the HoprDbTicketOperations trait, specifically in how ticket management and cache operations are handled with respect to the unrealized_value.
  • Handle rejected tickets without altering unredeemed metrics #6558: The changes in the main PR are related to the modifications in the retrieved PR that also focus on the handling of the unrealized_value cache, specifically in the context of ticket state management.

Suggested labels

crate:core-network, status:in-progress

Suggested reviewers

  • NumberFour8
  • Teebor-Choka
✨ 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 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.

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 github-actions bot added the dependencies Pull requests that update a dependency file label Mar 18, 2025
@tolbrino tolbrino marked this pull request as ready for review March 18, 2025 10:55
Copy link
Contributor

@NumberFour8 NumberFour8 left a comment

Choose a reason for hiding this comment

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

While technically this is correct, there's no reason why unrealized values should be kept for previous channel epochs.

Maybe a better fix would be to correctly invalidate the cached value once a channel is closed/opened ?

@Teebor-Choka
Copy link
Contributor

@NumberFour8 's solution is better, but this one would work as well.

@tolbrino tolbrino changed the title cache: Tie unrealized value of channel to epoch db: Fix channel/ticket state mismatch on startup Mar 19, 2025
@tolbrino tolbrino enabled auto-merge (squash) March 19, 2025 12:53
@github-actions github-actions bot added testing Related to tests crate:hoprd labels Mar 20, 2025
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: 2

🧹 Nitpick comments (2)
tests/test_websocket_api.py (1)

214-215: Enhanced timeout and logging for deprecated test.

Significantly increasing the timeout from 5 to 1000 seconds and adding message logging will help in debugging the websocket connection issues mentioned in the xfail marker. Be aware that such a long timeout could potentially cause test runs to hang if messages aren't received.

Consider adding a more descriptive log prefix like "DEPRECATED TEST:" to make these log entries more identifiable in the logs.

tests/network-simulation/hoprd.cfg (1)

1-34: Document the purpose of this configuration file.

This is a new configuration file for network simulation testing. Consider adding a comment at the top of the file explaining its purpose, especially its relation to the ticket state mismatch fix that this PR addresses.

+# Configuration file for network simulation tests
+# Used to test the fix for channel/ticket state mismatch on startup
+# See PR #6930 for details
 hopr:
   db:
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between efd8d78 and 8c5a96d.

📒 Files selected for processing (3)
  • hoprd/hoprd/src/main.rs (1 hunks)
  • tests/network-simulation/hoprd.cfg (1 hunks)
  • tests/test_websocket_api.py (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: hoprd / docker
  • GitHub Check: hopli / docker
  • GitHub Check: tests-smart-contracts
  • GitHub Check: Docs / Rust docs
  • GitHub Check: tests-unit
  • GitHub Check: tests-smoke-websocket
  • GitHub Check: tests-unit-nightly
  • GitHub Check: Linter
🔇 Additional comments (8)
hoprd/hoprd/src/main.rs (1)

358-359: Enhanced logging for inactive websocket state.

This trace log provides better visibility into the application's websocket state when there are no active connections, complementing the existing error log that fires when websocket notifications fail. This is a good debugging improvement that helps understand messaging flow.

tests/test_websocket_api.py (3)

9-9: Appropriate import for new logging functionality.

Adding the logging import supports the new debug message functionality added below.


183-183: Minor type clarification.

Changing from integer to floating-point representation is functionally equivalent but makes the type more explicit.


190-190: Good use of xfail marker for known issues.

Adding an xfail marker with a clear explanation properly documents that this test is expected to fail due to known websocket connection issues. This prevents the test from causing confusing failures in CI pipelines.

tests/network-simulation/hoprd.cfg (4)

24-24: Add private key configuration or document that it's intentionally empty.

The private_key field is empty. If this is intentional for security or testing purposes, consider adding a comment. Otherwise, ensure this field is properly configured for the testing environment.


9-9: Clarify if empty strategies array is intentional.

The strategies array is empty. Is this intentional? If specific strategies are needed for the testing environment, they should be added here.


13-14: Verify that these settings support the ticket state mismatch fix.

The PR aims to fix a channel/ticket state mismatch, particularly related to unrealized balance across channel epochs. Ensure that the network: local and check_unrealized_balance: true settings properly support testing this fix.


28-28:

❓ Verification inconclusive

Verify YAML syntax compatibility.

The use of !IPv4 tag for IP address specification may not be compatible with all YAML parsers. Ensure that the parser used in your application supports this custom tag, or consider using a standard string representation.


🏁 Script executed:

#!/bin/bash
# Check if the YAML parser used in the application supports the !IPv4 tag

# First let's find where this config is being loaded
echo "Searching for files that load hoprd.cfg..."
rg -l "hoprd\.cfg" --type rust

Length of output: 127


Action Required: Manually Verify YAML Parser Custom Tag Support

The configuration file at tests/network-simulation/hoprd.cfg (line 28) uses the custom YAML tag !IPv4 for the IP address. Our initial search for references to hoprd.cfg in the Rust codebase did not yield any hits, so we couldn’t automatically determine whether the YAML parser in use supports this tag.

  • Verification Needed: Please manually confirm that the YAML parser (likely invoked via libraries like serde_yaml or similar) correctly recognizes and processes the !IPv4 tag without causing errors.
  • Alternative Suggestion: If parser support for custom YAML tags is not implemented or verified, consider changing the IP address specification to a standard string format (e.g., "0.0.0.0") to ensure compatibility.

@tolbrino tolbrino merged commit 7f05efa into release/singapore Mar 20, 2025
26 of 29 checks passed
@tolbrino tolbrino deleted the tb/202503-tickets-cache branch March 20, 2025 12:26
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