Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rfq-relayer): insert record even for transactions with invalid tokens #3473

Closed
wants to merge 3 commits into from

Conversation

dwasse
Copy link
Collaborator

@dwasse dwasse commented Dec 20, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new field, Reason, to capture the reason for unprocessed transactions in quote requests.
    • Enhanced error handling to include status updates and reasons for specific errors during transaction processing.
  • Bug Fixes

    • Improved traceability of issues by ensuring all relevant information is captured and stored in the database.
  • Chores

    • Adjusted logic flow for storing quote requests to enhance robustness and concurrency handling.

Copy link
Contributor

coderabbitai bot commented Dec 20, 2024

Walkthrough

This pull request introduces a new Reason field to the RequestForQuote and QuoteRequest structs across multiple files in the RFQ (Request for Quote) relayer service. The changes enhance error tracking and reporting by allowing the storage of reasons why a transaction might not be processed. The modifications are primarily focused on adding the new field to structs in model.go, db.go, and handlers.go, with corresponding updates to initialization and conversion methods.

Changes

File Change Summary
services/rfq/relayer/reldb/base/model.go Added Reason string to RequestForQuote struct; updated FromQuoteRequest and ToQuoteRequest methods to handle the new field
services/rfq/relayer/reldb/db.go Added Reason string to QuoteRequest struct
services/rfq/relayer/service/handlers.go Enhanced handleBridgeRequestedLog method to set Reason when errors occur; added error handling for token validity and decimals

Possibly related PRs

Suggested labels

size/m

Suggested reviewers

  • aureliusbtc
  • trajan0x

Poem

🐰 A rabbit's tale of code so bright,
Where Reason joins the data's might
Tracking errors with gentle care
Ensuring quotes are handled fair
A whisker's breadth of insight clear! 🔍

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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 go Pull requests that update Go code size/xs labels Dec 20, 2024
Copy link

cloudflare-workers-and-pages bot commented Dec 20, 2024

Deploying sanguine-fe with  Cloudflare Pages  Cloudflare Pages

Latest commit: a741015
Status: ✅  Deploy successful!
Preview URL: https://97b33e6d.sanguine-fe.pages.dev
Branch Preview URL: https://feat-relayer-reason.sanguine-fe.pages.dev

View logs

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 (1)
services/rfq/relayer/service/handlers.go (1)

94-102: Validate dbReq fields more thoroughly before storage.

The newly introduced dbReq is well structured; however, consider validating important fields such as Sender and Transaction to ensure that the subsequent logic can rely on properly formed data. This can help avoid storing partially invalid records in the database.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 40cd70b and e212fc4.

📒 Files selected for processing (3)
  • services/rfq/relayer/reldb/base/model.go (3 hunks)
  • services/rfq/relayer/reldb/db.go (1 hunks)
  • services/rfq/relayer/service/handlers.go (1 hunks)
🧰 Additional context used
🪛 golangci-lint (1.62.2)
services/rfq/relayer/service/handlers.go

109-109: S1039: unnecessary use of fmt.Sprintf

(gosimple)


118-118: printf: fmt.Sprintf does not support error-wrapping directive %w

(govet)


123-123: printf: non-constant format string in call to fmt.Errorf

(govet)

🪛 GitHub Check: Lint (services/rfq)
services/rfq/relayer/service/handlers.go

[failure] 109-109:
S1039: unnecessary use of fmt.Sprintf (gosimple)


[failure] 118-118:
printf: fmt.Sprintf does not support error-wrapping directive %w (govet)


[failure] 123-123:
printf: non-constant format string in call to fmt.Errorf (govet)

🔇 Additional comments (6)
services/rfq/relayer/service/handlers.go (2)

125-126: Good practice: usage of token decimals is consistent with prior checks.

These lines correctly assign the validated decimals after checking for errors, preventing invalid or nil pointer dereferences. This approach is safe and clear.


130-130: Check for potential database connection errors.

The fmt.Errorf usage is correct for error wrapping. Just ensure that any underlying database errors are properly handled/restored or retried elsewhere if needed, to avoid silent data loss.

services/rfq/relayer/reldb/db.go (1)

91-91: New Reason field improves traceability.

Introducing the Reason field is beneficial for error tracking. Ensure that any updates to it remain consistent with the domain logic and that user-facing messages do not inadvertently expose sensitive information.

services/rfq/relayer/reldb/base/model.go (3)

96-97: Protect sensitive or verbose reason strings.

Adding the Reason field to the database struct is valuable for diagnosing unprocessed transactions. Confirm that the reason content does not include PII, keys, or large data that could bloat the database.


142-142: Propagation of Reason in FromQuoteRequest.

Forwarding request.Reason ensures consistent error reporting across the system. Double-check that the reason remains accurate and does not get overwritten downstream.


231-231: Reason field consistency in ToQuoteRequest.

By setting r.Reason here, the round-trip conversion ensures the reason is preserved when going from the database model back to a QuoteRequest. This is consistent with the rest of the changes and maintains clarity.

Comment on lines 108 to 113
dbReq.Status = reldb.WillNotProcess
dbReq.Reason = fmt.Sprintf("invalid token (no contract code at address)")
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not store db request: %w", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Remove unnecessary use of fmt.Sprintf.

At line 109: fmt.Sprintf("invalid token (no contract code at address)") can be replaced with a direct string assignment for clarity and to address the static analysis suggestion about the unnecessary use of fmt.Sprintf.

- dbReq.Reason = fmt.Sprintf("invalid token (no contract code at address)")
+ dbReq.Reason = "invalid token (no contract code at address)"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
dbReq.Status = reldb.WillNotProcess
dbReq.Reason = fmt.Sprintf("invalid token (no contract code at address)")
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not store db request: %w", err)
}
dbReq.Status = reldb.WillNotProcess
dbReq.Reason = "invalid token (no contract code at address)"
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not store db request: %w", err)
}
🧰 Tools
🪛 golangci-lint (1.62.2)

109-109: S1039: unnecessary use of fmt.Sprintf

(gosimple)

🪛 GitHub Check: Lint (services/rfq)

[failure] 109-109:
S1039: unnecessary use of fmt.Sprintf (gosimple)

Comment on lines 117 to 123
dbReq.Status = reldb.WillNotProcess
dbReq.Reason = fmt.Sprintf("could not get decimals: %w", err)
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not store db request: %w", err)
}
return fmt.Errorf(dbReq.Reason)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Use error-wrapping directives appropriately.

• At line 118: “fmt.Sprintf("could not get decimals: %w", err)” doesn’t support “%w” in fmt.Sprintf. Use fmt.Errorf for correct wrapping instead.
• At line 123: Return an error directly using errors.New or similar if you want the stored reason to be used as-is. This avoids potential format string misinterpretation.

- dbReq.Reason = fmt.Sprintf("could not get decimals: %w", err)
+ dbReq.Reason = fmt.Sprintf("could not get decimals: %s", err)

...

- return fmt.Errorf(dbReq.Reason)
+ return errors.New(dbReq.Reason)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
dbReq.Status = reldb.WillNotProcess
dbReq.Reason = fmt.Sprintf("could not get decimals: %w", err)
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not store db request: %w", err)
}
return fmt.Errorf(dbReq.Reason)
dbReq.Status = reldb.WillNotProcess
dbReq.Reason = fmt.Sprintf("could not get decimals: %s", err)
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not store db request: %w", err)
}
return errors.New(dbReq.Reason)
🧰 Tools
🪛 golangci-lint (1.62.2)

118-118: printf: fmt.Sprintf does not support error-wrapping directive %w

(govet)


123-123: printf: non-constant format string in call to fmt.Errorf

(govet)

🪛 GitHub Check: Lint (services/rfq)

[failure] 118-118:
printf: fmt.Sprintf does not support error-wrapping directive %w (govet)


[failure] 123-123:
printf: non-constant format string in call to fmt.Errorf (govet)

Copy link

codecov bot commented Dec 20, 2024

Codecov Report

Attention: Patch coverage is 7.40741% with 25 lines in your changes missing coverage. Please review.

Project coverage is 31.52087%. Comparing base (40cd70b) to head (a741015).
Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
services/rfq/relayer/service/handlers.go 0.00000% 25 Missing ⚠️
Additional details and impacted files
@@                 Coverage Diff                 @@
##              master       #3473         +/-   ##
===================================================
- Coverage   34.04255%   31.52087%   -2.52168%     
===================================================
  Files            388         489        +101     
  Lines          24581       33803       +9222     
===================================================
+ Hits            8368       10655       +2287     
- Misses         15472       22134       +6662     
- Partials         741        1014        +273     
Flag Coverage Δ
opbot 0.18282% <ø> (ø)
promexporter 6.81642% <ø> (ø)
rfq 24.79939% <7.40741%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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: 1

🧹 Nitpick comments (2)
services/rfq/relayer/service/handlers.go (2)

94-102: Consider using field names in struct initialization for better readability.

While the current initialization works, using explicit field names would make the code more maintainable and less prone to errors when new fields are added.

 dbReq := reldb.QuoteRequest{
-    BlockNumber:   req.Raw.BlockNumber,
-    RawRequest:    req.Request,
-    TransactionID: req.TransactionId,
-    Sender:        req.Sender,
-    Transaction:   bridgeTx,
-    Status:        reldb.Seen,
-    OriginTxHash:  req.Raw.TxHash,
+    BlockNumber:   req.Raw.BlockNumber,
+    RawRequest:    req.Request,
+    TransactionID: req.TransactionId,
+    Sender:        req.Sender,
+    Transaction:   bridgeTx,
+    Status:        reldb.Seen,
+    OriginTxHash:  req.Raw.TxHash,
+    Reason:        "",  // explicitly initialize new field
 }

125-126: Document pointer dereferencing assumption.

Add a comment to clarify that the nil check for decimals was performed in the previous error handling block. This helps prevent accidental removal of the nil check during future refactoring.

+// Safe to dereference as nil check was performed in previous error handling block
 dbReq.OriginTokenDecimals = *originDecimals
 dbReq.DestTokenDecimals = *destDecimals
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e212fc4 and a741015.

📒 Files selected for processing (1)
  • services/rfq/relayer/service/handlers.go (1 hunks)
🔇 Additional comments (1)
services/rfq/relayer/service/handlers.go (1)

108-113: Remove unnecessary use of fmt.Sprintf.

The string is static and doesn't require formatting.

 dbReq.Status = reldb.WillNotProcess
-dbReq.Reason = "invalid token (no contract code at address)"
+dbReq.Reason = "invalid token (no contract code at address)"

Comment on lines +117 to +122
dbReq.Status = reldb.WillNotProcess
dbReq.Reason = fmt.Sprintf("could not get decimals: %s", err.Error())
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not store db request: %w", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve error handling consistency.

The error handling can be improved in two ways:

  1. Avoid duplicating the error message between Reason and returned error
  2. Use consistent error wrapping patterns
 dbReq.Status = reldb.WillNotProcess
-dbReq.Reason = fmt.Sprintf("could not get decimals: %s", err.Error())
+dbReq.Reason = fmt.Sprintf("could not get decimals: %v", err)
 err = r.db.StoreQuoteRequest(ctx, dbReq)
 if err != nil {
     return fmt.Errorf("could not store db request: %w", err)
 }
-return fmt.Errorf("could not get decimals: %w", err)
+return fmt.Errorf("%s: %w", dbReq.Reason, err)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
dbReq.Status = reldb.WillNotProcess
dbReq.Reason = fmt.Sprintf("could not get decimals: %s", err.Error())
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not store db request: %w", err)
}
dbReq.Status = reldb.WillNotProcess
dbReq.Reason = fmt.Sprintf("could not get decimals: %v", err)
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not store db request: %w", err)
}
return fmt.Errorf("%s: %w", dbReq.Reason, err)

Copy link

github-actions bot commented Jan 4, 2025

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Jan 4, 2025
@github-actions github-actions bot closed this Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant