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(contracts-rfq): save destination chain ID for requested bridges [SLT-423] #3356

Merged
merged 4 commits into from
Nov 4, 2024

Conversation

ChiTimesChi
Copy link
Collaborator

@ChiTimesChi ChiTimesChi commented Oct 31, 2024

Description
Swaps out proof block number for destination chain ID in the BridgeTxDetails struct that tracks relevant fields for a bridge request on the origin chain.

Summary by CodeRabbit

  • New Features

    • Enhanced timestamp handling with increased precision for bridge transactions.
    • Introduction of new parameters and structures for improved cross-chain transaction management.
  • Bug Fixes

    • Improved validation of transaction statuses and proofs in testing functions.
  • Documentation

    • Updated comments to reflect changes in timestamp management and transaction structures.

@ChiTimesChi ChiTimesChi requested a review from parodime October 31, 2024 21:23
Copy link
Contributor

coderabbitai bot commented Oct 31, 2024

Walkthrough

The changes in this pull request involve updates to the FastBridgeV2 contract and its related interface IFastBridgeV2. Key modifications include changing the type of the proofBlockTimestamp variable from uint40 to uint56, enhancing timestamp handling. The BridgeTxDetails struct has been updated to remove certain fields and add destChainId. New structs and functions have been introduced to improve transaction management and testing clarity. Overall, these changes refine the contract's functionality and enhance the structure of the associated tests.

Changes

File Path Change Summary
packages/contracts-rfq/contracts/FastBridgeV2.sol - Updated proofBlockTimestamp from uint40 to uint56.
- Modified _timeSince function parameter type to uint56.
- Updated bridge function to set destChainId.
- Added checks in relay function for zapNative.
- Updated comments regarding timestamps.
packages/contracts-rfq/contracts/interfaces/IFastBridgeV2.sol - Updated BridgeTxDetails struct: removed proofBlockTimestamp and proofBlockNumber, added destChainId, changed proofBlockTimestamp to uint56.
- Added BridgeParamsV2 struct with new fields.
- Updated BridgeTransactionV2 struct to include originChainId and destChainId.
packages/contracts-rfq/test/FastBridgeV2.Src.t.sol - Added functions: checkStatusAndProofAfterBridge, checkStatusAndProofAfterProve, checkStatusAndProofAfterClaim, checkStatusAndProofAfterDispute, checkStatusAndProofAfterRefund.
- Updated existing tests to use new checking functions for status assertions.
- Added expectedProofTS parameter in checkStatusAndProofAfterClaim.

Possibly related PRs

Suggested labels

size/m, needs-go-generate-services/rfq

Suggested reviewers

  • aureliusbtc
  • parodime
  • trajan0x

🐇 In the land of code, a change we see,
Timestamps now hold more, a grand decree.
With structs anew, and tests refined,
The bridge grows stronger, in every line.
Let transactions flow, with clarity bright,
In the world of smart contracts, all feels right! 🌟


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.

Copy link

Deploying sanguine-fe with  Cloudflare Pages  Cloudflare Pages

Latest commit: c417fce
Status: ✅  Deploy successful!
Preview URL: https://b579967c.sanguine-fe.pages.dev
Branch Preview URL: https://feat-fbv2-save-dest-chain-id.sanguine-fe.pages.dev

View logs

Copy link

Changes to gas cost

Generated at commit: da4136aa288893e9b07a1f5104ab024f0b8cc9b6, compared to commit: e939e4e50d047cc433fc1c3ddfffa53da7897a11

🧾 Summary (50% most significant diffs)

Contract Method Avg (+/-) %
FastBridgeV2 bridgeProofs
bridgeRelayDetails
canClaim
dispute
prove(bytes,bytes32)
prove(bytes32,bytes32,address)
+6 ❌
-6 ✅
+6 ❌
+118 ❌
-43 ✅
-43 ✅
+0.90%
-0.88%
+0.20%
+0.38%
-0.12%
-0.13%

Full diff report 👇
Contract Deployment Cost (+/-) Method Min (+/-) % Avg (+/-) % Median (+/-) % Max (+/-) % # Calls (+/-)
FastBridgeV2 3,422,835 (+8,804) bridge
bridgeProofs
bridgeRelayDetails
canClaim
claim(bytes)
claim(bytes,address)
dispute
prove(bytes,bytes32)
prove(bytes32,bytes32,address)
relay(bytes)
relay(bytes,address)
69,046 (+32)
674 (+6)
672 (-6)
3,061 (+6)
39,966 (+6)
42,939 (+6)
31,122 (+118)
34,512 (-43)
32,018 (-43)
61,340 (-18)
61,747 (-18)
+0.05%
+0.90%
-0.88%
+0.20%
+0.02%
+0.01%
+0.38%
-0.12%
-0.13%
-0.03%
-0.03%
80,424 (+32)
674 (+6)
672 (-6)
3,061 (+6)
46,877 (+6)
48,600 (+6)
31,132 (+118)
34,825 (-43)
32,026 (-43)
67,794 (-18)
68,201 (-18)
+0.04%
+0.90%
-0.88%
+0.20%
+0.01%
+0.01%
+0.38%
-0.12%
-0.13%
-0.03%
-0.03%
81,125 (+32)
674 (+6)
672 (-6)
3,061 (+6)
47,461 (+6)
49,184 (+6)
31,134 (+118)
34,536 (-43)
32,030 (-43)
67,717 (-18)
68,124 (-18)
+0.04%
+0.90%
-0.88%
+0.20%
+0.01%
+0.01%
+0.38%
-0.12%
-0.13%
-0.03%
-0.03%
93,228 (+32)
674 (+6)
672 (-6)
3,061 (+6)
54,920 (+6)
55,393 (+6)
31,134 (+118)
35,476 (-43)
32,030 (-43)
76,910 (-18)
77,317 (-18)
+0.03%
+0.90%
-0.88%
+0.20%
+0.01%
+0.01%
+0.38%
-0.12%
-0.13%
-0.02%
-0.02%
260 (0)
12 (0)
12 (0)
12 (0)
6 (0)
6 (0)
6 (0)
68 (0)
68 (0)
6 (0)
6 (0)

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

🧹 Outside diff range and nitpick comments (7)
packages/contracts-rfq/contracts/FastBridgeV2.sol (1)

428-435: LGTM: Well-documented timestamp handling with gas optimization.

The _timeSince function is well implemented with:

  • Clear documentation of the rollover case
  • Safe use of unchecked block for gas optimization
  • Proper type casting for timestamp comparison

Consider adding an example in the documentation to illustrate the rollover case:

 /// @dev proof.timestamp stores casted uint56(block.timestamp) block timestamps for gas optimization
 ///      _timeSince(proof) can accomodate rollover case when block.timestamp > type(uint56).max but
-///      proof.timestamp < type(uint56).max via unchecked statement
+///      proof.timestamp < type(uint56).max via unchecked statement
+///      Example: block.timestamp = 2^56 + 100, proof.timestamp = 50 => delta = 50
packages/contracts-rfq/test/FastBridgeV2.Src.t.sol (1)

97-108: LGTM! Consider adding documentation for the helper functions.

The new helper functions effectively encapsulate common assertion patterns for different bridge states. They follow a consistent structure and help improve test maintainability.

Consider adding NatSpec documentation to describe the expected state for each helper function:

+/// @notice Verifies that a bridge transaction is in the REQUESTED state with no proof details
 function checkStatusAndProofAfterBridge(bytes32 txId) public view {

Also applies to: 285-296, 473-484, 702-705, 807-818

packages/contracts-rfq/contracts/interfaces/IFastBridgeV2.sol (5)

Line range hint 36-36: Use uint256 instead of int256 for quoteExclusivitySeconds

Since quoteExclusivitySeconds represents a duration and should not be negative, using an unsigned integer prevents negative values which could cause unexpected behavior.

Apply this diff to fix the type:

-    int256 quoteExclusivitySeconds;
+    uint256 quoteExclusivitySeconds;

Line range hint 28-28: Clarify the destToken address for native tokens

The address 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE is commonly used to represent native tokens. Ensure that this convention is consistently applied and that the code correctly handles scenarios involving native tokens and zapNative.


Line range hint 66-66: Consider overloading the bridge function for better readability

Passing both BridgeParams and BridgeParamsV2 structs may lead to confusion. Overloading the bridge function or creating a single comprehensive struct could improve code clarity and usability.

Example of function overloading:

-    function bridge(BridgeParams memory params, BridgeParamsV2 memory paramsV2) external payable;
+    function bridge(BridgeParams memory params) external payable;
+    function bridge(BridgeParamsV2 memory paramsV2) external payable;

Alternatively, combine the structs:

+    struct BridgeParamsCombined {
+        // Fields from BridgeParams
+        // Fields from BridgeParamsV2
+    }
+    function bridge(BridgeParamsCombined memory params) external payable;

Line range hint 61-61: Add an event for bridge function to emit transaction details

Emitting an event in the bridge function can enhance transparency and facilitate tracking of bridge transactions.

Example of adding an event:

event BridgeInitiated(bytes32 indexed transactionId, address sender, uint32 destChainId, uint256 amount);

And emit it in the bridge function implementation.


Line range hint 93-95: Ensure proper documentation for getBridgeTransactionV2

The getBridgeTransactionV2 function lacks detailed documentation on its behavior and return values. Adding comprehensive comments will improve maintainability and ease of use.

Consider expanding the documentation:

 /// @notice Decodes bridge request into a bridge transaction V2 struct used by FastBridgeV2
+/// @dev Parses the encoded request and returns a structured `BridgeTransactionV2`
 /// @param request The bridge request to decode
+/// @return bridgeTx The decoded bridge transaction details as `BridgeTransactionV2`
 function getBridgeTransactionV2(bytes memory request) external view returns (BridgeTransactionV2 memory);
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 47194e6 and c417fce.

📒 Files selected for processing (3)
  • packages/contracts-rfq/contracts/FastBridgeV2.sol (5 hunks)
  • packages/contracts-rfq/contracts/interfaces/IFastBridgeV2.sol (1 hunks)
  • packages/contracts-rfq/test/FastBridgeV2.Src.t.sol (36 hunks)
🧰 Additional context used
📓 Learnings (3)
packages/contracts-rfq/contracts/FastBridgeV2.sol (1)
Learnt from: ChiTimesChi
PR: synapsecns/sanguine#3284
File: packages/contracts-rfq/contracts/FastBridgeV2.sol:100-118
Timestamp: 2024-10-14T14:45:57.214Z
Learning: When reviewing code in `packages/contracts-rfq/contracts/FastBridgeV2.sol` (Solidity), focus on changes introduced in the PR and avoid commenting on existing functions like `refund` if they are not modified by the PR, as such comments are considered out of scope.
packages/contracts-rfq/contracts/interfaces/IFastBridgeV2.sol (1)
Learnt from: ChiTimesChi
PR: synapsecns/sanguine#3284
File: packages/contracts-rfq/contracts/FastBridgeV2.sol:100-118
Timestamp: 2024-10-14T14:45:57.214Z
Learning: When reviewing code in `packages/contracts-rfq/contracts/FastBridgeV2.sol` (Solidity), focus on changes introduced in the PR and avoid commenting on existing functions like `refund` if they are not modified by the PR, as such comments are considered out of scope.
packages/contracts-rfq/test/FastBridgeV2.Src.t.sol (3)
Learnt from: ChiTimesChi
PR: synapsecns/sanguine#3284
File: packages/contracts-rfq/test/FastBridgeV2.Src.t.sol:919-993
Timestamp: 2024-10-14T14:48:01.520Z
Learning: In Solidity test files for FastBridgeV2 (e.g., `packages/contracts-rfq/test/FastBridgeV2.Src.t.sol`), code duplication in test functions is acceptable to maintain readability and maintainability, even if it contradicts DRY principles.
Learnt from: ChiTimesChi
PR: synapsecns/sanguine#3284
File: packages/contracts-rfq/test/FastBridgeV2.t.sol:53-56
Timestamp: 2024-10-14T13:24:45.293Z
Learning: Modifying test utility contracts in the `test` directory is unnecessary and would only pollute the code.
Learnt from: ChiTimesChi
PR: synapsecns/sanguine#3284
File: packages/contracts-rfq/contracts/FastBridgeV2.sol:100-118
Timestamp: 2024-10-14T14:45:57.214Z
Learning: When reviewing code in `packages/contracts-rfq/contracts/FastBridgeV2.sol` (Solidity), focus on changes introduced in the PR and avoid commenting on existing functions like `refund` if they are not modified by the PR, as such comments are considered out of scope.
🔇 Additional comments (4)
packages/contracts-rfq/contracts/FastBridgeV2.sol (2)

92-92: LGTM: Improved timestamp handling with uint56.

The change from uint40 to uint56 for proofBlockTimestamp provides a larger buffer against timestamp overflow, extending support from year 2104 to year 4147.

Also applies to: 317-317, 331-331


223-225: LGTM: Efficient storage of destination chain ID.

The implementation efficiently stores the destination chain ID alongside the status update, providing crucial tracking information for cross-chain transactions.

packages/contracts-rfq/test/FastBridgeV2.Src.t.sol (1)

123-123: LGTM! Helper functions are well integrated.

The integration of the new helper functions across test cases improves code organization and makes the test assertions more consistent and maintainable.

Also applies to: 134-134, 156-156, 172-172, 191-191, 502-502, 515-515, 528-528, 540-540, 553-553, 574-574, 588-588, 602-602, 615-615, 628-628, 713-713, 725-725, 737-737, 750-750, 826-826, 839-839, 852-852, 865-865, 878-878, 892-892, 906-906, 920-920

packages/contracts-rfq/contracts/interfaces/IFastBridgeV2.sol (1)

Line range hint 42-42: Double-check the exclusivity logic in BridgeTransactionV2

Ensure that the exclusivity mechanism using exclusivityRelayer and exclusivityEndTime is correctly implemented and that edge cases are handled, such as when exclusivity expires.

Run the following script to search for usages and implementations of exclusivity fields:

✅ Verification successful

Let me gather more information about the exclusivity validation logic.


Let me check the actual validation logic in FastBridgeV2.sol.


Let me check one final piece - the validation of exclusivityEndTime.


Based on the comprehensive analysis of the exclusivity mechanism in FastBridgeV2, I can now provide the final response.

Exclusivity mechanism is properly implemented with comprehensive safeguards

The exclusivity logic in BridgeTransactionV2 is correctly implemented with multiple layers of protection:

  • The exclusivityEndTime is validated to be within range [0 .. params.deadline]
  • The relay validation ensures only the designated exclusivityRelayer can process the transaction until exclusivityEndTime
  • After exclusivityEndTime, any relayer can process the transaction
  • Extensive test coverage verifies both positive and negative cases, including edge cases like:
    • Last second of exclusivity period
    • Expired exclusivity period
    • Unauthorized relayer attempts during exclusivity
    • Zero exclusivity time handling
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify usage of `exclusivityRelayer` and `exclusivityEndTime`

# Test: Search for all references to exclusivity fields in the codebase.
# Expect: Proper handling of exclusivity logic.
rg 'exclusivity(Relayer|EndTime)'

Length of output: 6272


Script:

#!/bin/bash
# Search for the validation logic in FastBridgeV2.sol
rg -A 10 '_validateBridgeParams' packages/contracts-rfq/contracts/FastBridgeV2.sol

# Search for tests related to exclusivity validation
rg -A 10 'test.*exclusivity' packages/contracts-rfq/test/

Length of output: 19777


Script:

#!/bin/bash
# Search for the relay validation logic in FastBridgeV2.sol
rg -A 10 'if \(exclRelayer' packages/contracts-rfq/contracts/FastBridgeV2.sol

# Search for the validation function implementation
rg -B 5 -A 15 'function _validateBridgeParams' packages/contracts-rfq/contracts/FastBridgeV2.sol

Length of output: 1476


Script:

#!/bin/bash
# Search for exclusivity validation in FastBridgeV2.sol
rg -B 2 -A 5 'exclusivityEndTime.*params\.deadline' packages/contracts-rfq/contracts/FastBridgeV2.sol

# Search for any custom errors related to exclusivity
rg 'error.*Exclusivity' packages/contracts-rfq/contracts/FastBridgeV2.sol

Length of output: 972

Copy link

codecov bot commented Oct 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.29025%. Comparing base (47194e6) to head (c417fce).
Report is 13 commits behind head on master.

Additional details and impacted files
@@                 Coverage Diff                 @@
##              master       #3356         +/-   ##
===================================================
+ Coverage   90.44834%   92.29025%   +1.84190%     
===================================================
  Files             54          60          +6     
  Lines           1026        1323        +297     
  Branches          82          82                 
===================================================
+ Hits             928        1221        +293     
- Misses            95          99          +4     
  Partials           3           3                 
Flag Coverage Δ
packages 90.44834% <ø> (ø)
solidity 98.65320% <100.00000%> (?)

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
Collaborator

@parodime parodime left a comment

Choose a reason for hiding this comment

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

LGTM! Good simplification

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.

2 participants