Skip to content

Conversation

NumberFour8
Copy link
Contributor

@NumberFour8 NumberFour8 commented Aug 2, 2024

In certain situations, the database can contain an aggregated ticket and also a ticket that is inside this aggregation ticket.
E.g. the DB contains a ticket with Index 1 and Offset 4, but also a ticket with Index 3, Offset 1.

This can happen when packets are arriving out of order, and only e.g. a single ticket is needed to trigger an aggregation.

Example:

  • aggregation threshold is set to 3
  • there are 2 unredeemed tickets (index 0, offset 1, index 1 offset 1)
  • ticket with index 4, offset 1 arrives, completing the batch and the aggregation is triggered
  • ticket with index 2 offset 1 and index 3 offset 1 arrive
  • aggregated ticket with index 0 offset 5 arrives from the aggregator

This results in tickets: (2,1), (3,1) and (0,5) being in the DB, which have overlapping index intervals. This will subsequently result in aggregation failure, when another aggregation attempt is made.

This PR fixes the issue by removing the single tickets that overlap with an aggregated ticket (marking them as neglected).
However, this comes at certain loss. This cannot be prevented unless more sophisticated buffering algorithm is introduced before the aggregation takes place.
An ultimate solution is to get rid of aggregation completely, which is planned for future releases.

Closes #6419

@NumberFour8 NumberFour8 requested a review from a team August 2, 2024 16:09
@NumberFour8 NumberFour8 self-assigned this Aug 2, 2024
Copy link
Contributor

coderabbitai bot commented Aug 2, 2024

Walkthrough

Walkthrough

The recent changes enhance ticket management functionality by modifying how redeemable tickets are accessed and processed. Key adjustments include transitioning from single-index to multi-index handling within the TicketSelector, ensuring safer handling of the selector variable through cloning, and improving asynchronous operations across multiple files. These enhancements collectively improve the robustness and flexibility of ticket aggregation and redemption processes.

Changes

Files Change Summary
chain/actions/src/redeem.rs Updated the retrieval method for redeemable tickets by cloning the selector variable to prevent ownership issues.
db/api/src/ticket_manager.rs Enhanced asynchronous handling by cloning the selector variable before use in database queries, ensuring stability in concurrent environments.
db/api/src/tickets.rs Modified TicketSelector structure to support multiple indices with a vector, updated related methods for aggregation handling, and improved test cases for duplicates.

Assessment against linked issues

Objective Addressed Explanation
Tickets aggregation should happen based on aggregation_threshold (#[6419])
Prevent lockup of unredeemed tickets during aggregation (#[6419])

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

@NumberFour8 NumberFour8 marked this pull request as ready for review August 2, 2024 16:10
@NumberFour8 NumberFour8 added this to the 2.1.4 milestone Aug 2, 2024
@github-actions github-actions bot added dependencies Pull requests that update a dependency file crate:hopr-db-api crate:chain-actions labels Aug 2, 2024
@NumberFour8 NumberFour8 changed the title Remove tickets that are within an aggregated prior to sending aggregation request Remove tickets that are within an aggregated ticket prior to sending aggregation request Aug 2, 2024
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

Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 78d7be8 and a940d6b.

Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
Files selected for processing (3)
  • chain/actions/src/redeem.rs (1 hunks)
  • db/api/src/ticket_manager.rs (2 hunks)
  • db/api/src/tickets.rs (31 hunks)
Additional comments not posted (11)
db/api/src/ticket_manager.rs (1)

Line range hint 150-159:
LGTM! Cloning the selector variable ensures thread safety and prevents unintended side effects.

The change improves the control flow by allowing the asynchronous operation to operate on a clone of the selector, which is crucial in concurrent environments.

chain/actions/src/redeem.rs (1)

137-137: LGTM! Cloning the selector variable ensures ownership and borrowing safety.

The change guarantees that the original selector remains intact and can be used elsewhere without risk of modification or invalidation.

db/api/src/tickets.rs (9)

16-17: New imports are necessary.

The new imports from sea_orm and sea_query are required for the changes in the TicketSelector structure and methods.


65-69: Update to Display implementation is approved.

The Display implementation has been updated to correctly format the index field when it contains multiple values.


92-92: Update to is_unique method is approved.

The is_unique method now checks if the length of the index vector is one, ensuring correct identification of a single ticket selection.


95-98: Update to with_index method is approved.

The with_index method has been modified to push the index into the self.index vector, allowing for the selection of multiple tickets.


126-126: Update to From<&AcknowledgedTicket> implementation is approved.

The From<&AcknowledgedTicket> implementation now initializes index as vec![value.ticket.index], ensuring correct initialization.


157-158: Update to IntoCondition implementation is approved.

The IntoCondition implementation has been updated to correctly handle multiple indices using the is_in method.


1412-1417: Update to generate_random_ack_ticket function is approved.

The generate_random_ack_ticket function now includes an index_offset parameter, enhancing its flexibility.


2452-2503: New test case is approved.

The new test case validates the behavior of ticket aggregation when duplicates are present, ensuring the integrity of the ticketing system.


40-51: Change to TicketSelector structure is approved.

The index field in the TicketSelector struct has been changed from Option<u64> to Vec<u64>, allowing for the selection of multiple tickets.

However, ensure that all usages of the TicketSelector structure in the codebase are updated to reflect this change.

Verification successful

Ensure all usages of TicketSelector are updated.

The TicketSelector structure has been updated, but we need to verify that all instances in the codebase correctly reflect the change from Option<u64> to Vec<u64>. This includes proper initialization and usage of the index field.


All usages of TicketSelector have been updated.

The index field in the TicketSelector struct has been updated from Option<u64> to Vec<u64>, and all relevant instances in the codebase reflect this change.

  • protocol.rs
  • ticket_manager.rs
  • tickets.rs
  • redeem.rs
  • handlers.rs
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all usages of the `TicketSelector` structure in the codebase.

# Test: Search for the `TicketSelector` structure. Expect: Only occurrences of the new structure.
rg --type rust -A 5 $'TicketSelector'

Length of output: 18920


Script:

#!/bin/bash
# Description: Verify the initialization and usage of the `index` field in `TicketSelector`.

# Search for the initialization of `TicketSelector` and check the usage of the `index` field.
rg --type rust 'TicketSelector::new\(' -A 5
rg --type rust 'TicketSelector::from\(' -A 5
rg --type rust '\.with_index\(' -A 5

Length of output: 11341

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate:chain-actions crate:hopr-db-api dependencies Pull requests that update a dependency file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants