Skip to content

Conversation

Teebor-Choka
Copy link
Contributor

@Teebor-Choka Teebor-Choka commented Nov 4, 2024

Reintroduce backwards compatible and POST compliant behavior on alias entry:

  1. if an alias exists -> fail with 409
  2. if the peer id is already assigned to an alias -> fail with 409
  3. otherwise introduce the alias

Notes

Refixes #6206

@Teebor-Choka Teebor-Choka added the bug Something isn't working label Nov 4, 2024
@Teebor-Choka Teebor-Choka added this to the 2.2.0-rc.1 milestone Nov 4, 2024
@Teebor-Choka Teebor-Choka requested review from jeandemeusy, mjadach-iv and a team November 4, 2024 10:36
@Teebor-Choka Teebor-Choka self-assigned this Nov 4, 2024
@Teebor-Choka Teebor-Choka marked this pull request as ready for review November 4, 2024 10:36
Copy link
Contributor

coderabbitai bot commented Nov 4, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request modifies the alias management system within the HoprdDb and API layers. Key changes include the removal of transaction-based logic in the set_alias method, which now directly handles alias insertion with conflict resolution. A new error variant for alias conflicts is introduced in the DbError enum. Additionally, the API error responses for alias conflicts are enhanced for clarity, and new tests are added to ensure that multiple aliases cannot be assigned to a single peer ID.

Changes

File Path Change Summary
hoprd/db/api/src/aliases.rs Modified set_alias to remove transaction logic, using direct insert with conflict handling. Updated set_aliases for batch insertions.
hoprd/db/api/src/errors.rs Added AliasOrPeerIdAlreadyExists variant to DbError enum for alias conflict handling.
hoprd/rest-api/src/alias.rs Updated error handling in set_alias for clarity on alias conflicts; added handling for new error variant.
hoprd/rest-api/src/lib.rs Added AliasOrPeerIdAliasAlreadyExists variant to ApiErrorStatus enum for specific error reporting.
tests/test_integration.py Introduced test_hoprd_node_should_not_be_able_to_set_multiple_aliases_to_a_single_peerid test; modified existing tests for parameterization.

Assessment against linked issues

Objective Addressed Explanation
Do not allow duplicate peer ID aliasing (#6206)
Return 409 when duplicate alias or PeerID is attempted (#6206)

Possibly related PRs

Suggested labels

dependencies, crate:hopr-db-api, status:in-progress

Suggested reviewers

  • NumberFour8

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>, 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 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 testing Related to tests crate:hoprd-api labels Nov 4, 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: 5

🧹 Outside diff range comments (1)
hoprd/rest-api/src/lib.rs (1)

Line range hint 449-453: Map error variants to appropriate HTTP status codes.

Currently, all API errors return a 500 Internal Server Error status code. For better HTTP semantics, consider mapping error variants to appropriate status codes. The new AliasOrPeerIdAliasAlreadyExists error should return 409 Conflict as specified in the PR objectives.

Here's a suggested implementation:

 impl IntoResponse for ApiError {
     fn into_response(self) -> Response {
-        (StatusCode::INTERNAL_SERVER_ERROR, self).into_response()
+        let status = match self.status.as_str() {
+            "ALIAS_OR_PEER_ID_ALIAS_ALREADY_EXISTS" => StatusCode::CONFLICT,
+            "INVALID_INPUT" => StatusCode::BAD_REQUEST,
+            "UNAUTHORIZED" => StatusCode::UNAUTHORIZED,
+            "NOT_FOUND" => StatusCode::NOT_FOUND,
+            _ => StatusCode::INTERNAL_SERVER_ERROR,
+        };
+        (status, self).into_response()
     }
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between 09244cc and a62ea5e.

📒 Files selected for processing (5)
  • hoprd/db/api/src/aliases.rs (2 hunks)
  • hoprd/db/api/src/errors.rs (1 hunks)
  • hoprd/rest-api/src/alias.rs (2 hunks)
  • hoprd/rest-api/src/lib.rs (1 hunks)
  • tests/test_integration.py (1 hunks)
🧰 Additional context used
🪛 Ruff
tests/test_integration.py

88-88: Line too long (124 > 120)

(E501)

🔇 Additional comments (3)
hoprd/db/api/src/errors.rs (1)

9-10: LGTM! The new error variant is well-placed and supports the uniqueness constraints.

The AliasOrPeerIdAlreadyExists variant with its static error message is appropriately placed in the database layer and aligns well with the PR's objective of preventing duplicate aliases.

hoprd/rest-api/src/alias.rs (1)

87-87: LGTM: Error description accurately reflects both conflict cases.

The updated error description properly documents both conflict scenarios: existing alias and already aliased peer_id, aligning with the bi-directional mapping requirements.

Let's verify the error description matches the implementation:

✅ Verification successful

Let me gather more context about the error handling implementation to ensure full verification.


Let me try a different pattern to get the set_alias implementation.


Let me try one final search pattern to locate the implementation.


Based on the evidence gathered from the shell scripts, I can now provide the final verification:

✓ Error description matches implementation

The error description accurately reflects the implementation:

  • The error variant AliasOrPeerIdAliasAlreadyExists in the codebase confirms handling of both cases
  • The error is properly mapped to HTTP 409 (Conflict) status code
  • The error propagation chain from DB layer (DbError::AliasOrPeerIdAlreadyExists) to API response is consistent
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for corresponding error enum variant and handler
rg -A 2 "AliasOrPeerIdAlias|AliasOrPeerId.*Exists" --type rust

Length of output: 988


Script:

#!/bin/bash
# Check the error enum definition and its mapping
rg -A 5 "enum ApiErrorStatus" --type rust

# Check the actual error handling in the set_alias function
ast-grep --pattern 'async fn set_alias($$$) {
  $$$
}'

Length of output: 575


Script:

#!/bin/bash
# Search for the set_alias function implementation with context
rg -A 20 "pub async fn set_alias" --type rust

Length of output: 47


Script:

#!/bin/bash
# Search for the handler function with more context and different pattern
rg -A 20 "fn set_alias.*->.*Response" --type rust

# Also check the API route definition
rg "route.*alias" --type rust

Length of output: 529

hoprd/db/api/src/aliases.rs (1)

95-109: Consider adding tests for edge cases.

While the current test coverage is comprehensive, consider adding tests for these scenarios:

  1. Very long aliases (to verify any length constraints)
  2. Special characters in aliases (to verify character set handling)
  3. Concurrent alias setting (to verify race condition handling)

Let's check if there are any length or character set constraints in the schema:

@Teebor-Choka Teebor-Choka force-pushed the kauki/hoprd/alias-to-an-existing-peerid-or-alias-should-fail branch from a62ea5e to dfb3987 Compare November 4, 2024 11:31
@Teebor-Choka Teebor-Choka force-pushed the kauki/hoprd/alias-to-an-existing-peerid-or-alias-should-fail branch from dfb3987 to ffb42cd Compare November 4, 2024 11:35
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: 5

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between a62ea5e and ffb42cd.

📒 Files selected for processing (5)
  • hoprd/db/api/src/aliases.rs (3 hunks)
  • hoprd/db/api/src/errors.rs (1 hunks)
  • hoprd/rest-api/src/alias.rs (2 hunks)
  • hoprd/rest-api/src/lib.rs (1 hunks)
  • tests/test_integration.py (1 hunks)
🔇 Additional comments (5)
hoprd/db/api/src/errors.rs (2)

9-10: LGTM! The error variant aligns with PR objectives.

The new error variant correctly handles the case where an alias or peer ID already exists, supporting the PR's goal of preventing duplicate entries and ensuring unique mappings.


9-10: 🧹 Nitpick (assertive)

Consider enhancing error details for debugging.

The current implementation from the previous review comment is still valid and would improve debugging capabilities.

hoprd/rest-api/src/alias.rs (1)

87-87: LGTM: Documentation accurately reflects error conditions.

The updated error description properly covers both conflict scenarios: existing alias and already aliased peer_id. This aligns well with the PR objectives for preventing duplicate entries.

tests/test_integration.py (2)

100-103: LGTM! Well-structured test for automatic self-aliasing.

The test is concise, focused, and effectively verifies that each node automatically contains an alias "me" that maps to its own peer ID.


108-108: LGTM! Improved test name.

The renamed function better describes its purpose of testing alias removal functionality.

@Teebor-Choka Teebor-Choka added this pull request to the merge queue Nov 4, 2024
Merged via the queue into master with commit fce8d10 Nov 4, 2024
26 checks passed
@Teebor-Choka Teebor-Choka deleted the kauki/hoprd/alias-to-an-existing-peerid-or-alias-should-fail branch November 4, 2024 12:12
@coderabbitai coderabbitai bot mentioned this pull request Nov 12, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working crate:hoprd-api testing Related to tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants