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

test(contracts-rfq): gas benchmark for the arbitrary call [SLT-233] #3273

Merged
merged 6 commits into from
Oct 11, 2024

Conversation

ChiTimesChi
Copy link
Collaborator

@ChiTimesChi ChiTimesChi commented Oct 11, 2024

Description

  • Adds gas benchmark tests for bridge/prove/claim/relay/refund with an arbitrary call.
  • Fixes the "maximum" value for bridge gas benchmark by setting the initial user nonce to 1.

Summary by CodeRabbit

  • New Features

    • Introduced new contracts for benchmarking gas usage during arbitrary calls.
    • Added functionality to manipulate sender's nonce for enhanced testing.
  • Bug Fixes

    • Updated nonce assignments to ensure they start from 1 for various transaction types.
  • Refactor

    • Renamed several contracts for clarity and consistency.
    • Refactored test functions to improve structure and readability.
  • Tests

    • Added new test functions to assess the behavior of arbitrary calls and protocol fees.

Copy link
Contributor

coderabbitai bot commented Oct 11, 2024

Walkthrough

This pull request introduces several changes across multiple Solidity test files related to the FastBridgeV2 contract. Key updates include the addition of new test contracts for benchmarking gas overhead associated with arbitrary calls, renaming existing contracts for consistency, and modifications to functions that manage transaction parameters and nonce values. The changes enhance the testing framework to better simulate real-world scenarios and ensure proper functionality of the FastBridgeV2 contract.

Changes

File Path Change Summary
packages/contracts-rfq/test/FastBridgeV2.GasBench.Dst.ArbitraryCall.t.sol Added contract FastBridgeV2GasBenchmarkDstArbitraryCallTest with overridden setUp and createFixturesV2 methods.
packages/contracts-rfq/test/FastBridgeV2.GasBench.Dst.Excl.t.sol Renamed contracts and updated createFixturesV2 to call super.createFixturesV2().
packages/contracts-rfq/test/FastBridgeV2.GasBench.Dst.t.sol Renamed contract from FastBridgeV2DstGasBenchmarkTest to FastBridgeV2GasBenchmarkDstTest.
packages/contracts-rfq/test/FastBridgeV2.GasBench.Encoding.t.sol Renamed contract to FastBridgeV2GasBenchmarkEncodingTest and added test_getBridgeTransactionV2_withArbitraryCall method.
packages/contracts-rfq/test/FastBridgeV2.GasBench.Src.ArbitraryCall.t.sol Added contract FastBridgeV2GasBenchmarkSrcArbitraryCallTest with overridden createFixturesV2 method.
packages/contracts-rfq/test/FastBridgeV2.GasBench.Src.PFees.t.sol Updated configureFastBridge to set protocol fee rate to 1% and adjusted createFixtures.
packages/contracts-rfq/test/FastBridgeV2.GasBench.Src.t.sol Modified nonce assignments and refactored test functions for bridging tokens and ETH.
packages/contracts-rfq/test/FastBridgeV2.t.sol Added cheatSenderNonce function to manipulate sender's nonce.

Possibly related PRs

Suggested labels

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

Suggested reviewers

  • traian0x
  • aureliusbtc

🐇 In the realm of code, we leap and bound,
With tests and functions, new paths are found.
Gas benchmarks soaring, like rabbits in flight,
In the world of Solidity, we code through the night! 🌙✨


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

codecov bot commented Oct 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 32.29410%. Comparing base (2b05e51) to head (c4393fc).
Report is 12 commits behind head on master.

Additional details and impacted files
@@                 Coverage Diff                 @@
##              master       #3273         +/-   ##
===================================================
+ Coverage   30.68788%   32.29410%   +1.60622%     
===================================================
  Files            447         433         -14     
  Lines          29787       28739       -1048     
  Branches          82         155         +73     
===================================================
+ Hits            9141        9281        +140     
+ Misses         19797       18612       -1185     
+ Partials         849         846          -3     
Flag Coverage Δ
packages 90.43902% <ø> (ø)
promexporter ?
solidity 93.82716% <ø> (?)

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

Deploying sanguine-fe with  Cloudflare Pages  Cloudflare Pages

Latest commit: c4393fc
Status: ✅  Deploy successful!
Preview URL: https://03d6c40a.sanguine-fe.pages.dev
Branch Preview URL: https://test-fbv2-arbitrary-call-gas.sanguine-fe.pages.dev

View logs

Copy link

Changes to gas cost

Generated at commit: 53d53a2ccac0268cf6560489d3c3abb40c2454b7, compared to commit: 6dd94ca5c4e9c8ec1263940d7ffff78096d90b2b

🧾 Summary (50% most significant diffs)

Contract Method Avg (+/-) %
FastBridgeV2 getBridgeTransactionV2
relay(bytes)
relay(bytes,address)
+147 ❌
+1,653 ❌
+1,653 ❌
+3.49%
+2.34%
+2.33%

Full diff report 👇
Contract Deployment Cost (+/-) Method Min (+/-) % Avg (+/-) % Median (+/-) % Max (+/-) % # Calls (+/-)
FastBridgeV2 2,870,712 (0) claim(bytes)
claim(bytes,address)
getBridgeTransactionV2
grantRole
prove(bytes,bytes32)
prove(bytes32,bytes32,address)
refund
relay(bytes)
relay(bytes,address)
43,909 (0)
46,762 (0)
4,214 (0)
101,405 (0)
35,595 (+12)
32,104 (-12)
47,669 (0)
64,685 (0)
65,102 (0)
0.00%
0.00%
0.00%
0.00%
+0.03%
-0.04%
0.00%
0.00%
0.00%
50,936 (-527)
52,539 (-527)
4,361 (+147)
114,536 (-135)
35,908 (+298)
32,139 (0)
51,742 (+347)
72,263 (+1,653)
72,680 (+1,653)
-1.02%
-0.99%
+3.49%
-0.12%
+0.84%
0.00%
+0.68%
+2.34%
+2.33%
51,472 (0)
53,075 (0)
4,361 (+147)
118,505 (0)
35,619 (+6)
32,140 (0)
51,903 (+499)
72,174 (+1,564)
72,591 (+1,564)
0.00%
0.00%
+3.49%
0.00%
+0.02%
0.00%
+0.97%
+2.21%
+2.20%
59,000 (0)
59,353 (0)
4,508 (+294)
118,505 (0)
36,571 (+844)
32,140 (0)
56,137 (+1,033)
82,727 (+6,192)
83,144 (+6,192)
0.00%
0.00%
+6.98%
0.00%
+2.36%
0.00%
+1.87%
+8.09%
+8.05%
6 (+2)
6 (+2)
2 (+1)
263 (+80)
63 (+21)
63 (+21)
12 (+4)
6 (+4)
6 (+4)

@ChiTimesChi ChiTimesChi changed the title test(contracts-rfq): gas benchmark for the arbitrary call test(contracts-rfq): gas benchmark for the arbitrary call [SLT-233] Oct 11, 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

🧹 Outside diff range and nitpick comments (11)
packages/contracts-rfq/test/FastBridgeV2.GasBench.Dst.Excl.t.sol (1)

16-18: LGTM! Consider adding documentation for new function calls.

The changes to createFixturesV2 improve the test setup by ensuring the parent class's implementation is called and adding specific exclusivity configurations for tokens and ETH. This aligns well with the contract's purpose of testing exclusivity.

Consider adding brief comments explaining the purpose of setTokenTestExclusivityParams and setEthTestExclusivityParams for improved readability. For example:

// Set exclusivity parameters for token tests
setTokenTestExclusivityParams(relayerA, EXCLUSIVITY_PERIOD);
// Set exclusivity parameters for ETH tests
setEthTestExclusivityParams(relayerA, EXCLUSIVITY_PERIOD);
packages/contracts-rfq/test/FastBridgeV2.GasBench.Src.ArbitraryCall.t.sol (3)

6-7: LGTM: Appropriate contract declaration and linter configuration.

The contract declaration and inheritance are correct. The solhint-disable comment is appropriate for a test file.

Consider moving the solhint-disable comment to the top of the file for better visibility and to apply to the entire file.


8-17: LGTM: Well-implemented override with comprehensive setup for arbitrary call testing.

The createFixturesV2() function correctly overrides the parent implementation and extends it to include arbitrary call parameters. This aligns well with the PR objective of implementing gas benchmarks for arbitrary calls.

Consider adding a brief comment explaining the purpose of the mockCallParams and why it's being set for various transactions. This would improve code readability and maintainability.


1-18: Overall assessment: Well-implemented test contract for gas benchmarking of arbitrary calls.

This new test contract effectively extends the existing gas benchmark tests to include arbitrary calls, aligning perfectly with the PR objectives. The code is well-structured, follows good practices, and achieves its intended purpose of enhancing the testing framework for FastBridgeV2.

To further improve this test suite:

  1. Consider adding specific test cases that utilize these arbitrary call parameters to ensure they're being correctly applied in the actual benchmark tests.
  2. If not already present in the parent class, consider adding assertions or checks to verify that the gas costs for arbitrary calls are within expected ranges.
packages/contracts-rfq/test/FastBridgeV2.GasBench.Encoding.t.sol (1)

18-21: New test function enhances coverage for arbitrary calls.

The addition of test_getBridgeTransactionV2_withArbitraryCall() improves test coverage by including a scenario with arbitrary call data. This aligns well with the PR objectives of implementing gas benchmarks for various bridge operations, including arbitrary calls.

Consider adding a comment explaining the purpose of this test function and the significance of the arbitrary call parameters used.

 function test_getBridgeTransactionV2_withArbitraryCall() public {
+    // Test getBridgeTransactionV2 with arbitrary call data to simulate real-world scenarios
     setTokenTestCallParams({callParams: abi.encode(userA, keccak256("Random ID"))});
     test_getBridgeTransactionV2();
 }
packages/contracts-rfq/test/FastBridgeV2.GasBench.Dst.ArbitraryCall.t.sol (2)

15-20: LGTM: Well-implemented setUp function with clear setup steps.

The setUp function is well-structured and correctly overrides the parent function. It properly initializes the test environment by creating a new RecipientMock contract and labeling it for improved readability.

Consider adding a brief comment explaining why userB is always used as the recipient of tokens in the inherited tests. This would provide more context for future developers working on this code.


22-27: LGTM: Well-implemented createFixturesV2 function with comprehensive setup.

The createFixturesV2 function is well-structured and correctly overrides the parent function. It properly sets up mock call parameters for both token and ETH tests, simulating real-world scenarios.

Consider extracting the creation of mock call parameters into a separate private function for improved readability and potential reuse. For example:

function createFixturesV2() public virtual override {
    super.createFixturesV2();
    bytes memory mockCallParams = createMockCallParams();
    setTokenTestCallParams(mockCallParams);
    setEthTestCallParams(mockCallParams);
}

function createMockCallParams() private view returns (bytes memory) {
    return abi.encode(userA, keccak256("Random ID"));
}

This refactoring would make the createFixturesV2 function more concise and easier to understand at a glance.

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

30-34: LGTM! Consider adding more context to the comment.

The changes to the nonce values are well-structured and the comment provides useful context. Good job on maintaining code clarity.

To further improve clarity, consider expanding the comment to briefly explain why starting from 1 is important. For example:

- // See FastBridgeV2GasBenchmarkSrcTest.initExistingTxs for why these start from 1, not 0
+ // Start nonces from 1 to align with FastBridgeV2GasBenchmarkSrcTest.initExistingTxs
+ // This ensures consistent behavior with real-world scenarios where nonce 0 is typically reserved

This provides more immediate context without requiring the reader to navigate to another file.

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

235-237: LGTM! Consider adding a brief comment for clarity.

The cheatSenderNonce function is a valuable addition to the testing framework, allowing precise control over sender nonces. This aligns well with the PR objective of addressing the "maximum" value issue for bridge gas benchmarks.

Consider adding a brief comment explaining the purpose of this function:

+    /// @dev Allows direct manipulation of sender nonces for testing purposes
     function cheatSenderNonce(address sender, uint256 nonce) public {
         stdstore.target(address(fastBridge)).sig("senderNonces(address)").with_key(sender).checked_write(nonce);
     }
packages/contracts-rfq/test/FastBridgeV2.GasBench.Src.t.sol (2)

103-107: Consider changing visibility of checkAfterBridgeToken() to internal

Since checkAfterBridgeToken() is only called within the contract, changing its visibility to internal restricts access appropriately and aligns with Solidity best practices.

Apply this diff to change the visibility:

-function checkAfterBridgeToken() public view {
+function checkAfterBridgeToken() internal view {

186-190: Modify visibility of checkAfterBridgeEth() to internal

Similar to checkAfterBridgeToken(), changing the visibility of checkAfterBridgeEth() to internal is advisable since it's only used internally within the contract.

Apply this diff to change the visibility:

-function checkAfterBridgeEth() public view {
+function checkAfterBridgeEth() internal view {
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 34f5ff5 and c4393fc.

📒 Files selected for processing (8)
  • packages/contracts-rfq/test/FastBridgeV2.GasBench.Dst.ArbitraryCall.t.sol (1 hunks)
  • packages/contracts-rfq/test/FastBridgeV2.GasBench.Dst.Excl.t.sol (2 hunks)
  • packages/contracts-rfq/test/FastBridgeV2.GasBench.Dst.t.sol (1 hunks)
  • packages/contracts-rfq/test/FastBridgeV2.GasBench.Encoding.t.sol (2 hunks)
  • packages/contracts-rfq/test/FastBridgeV2.GasBench.Src.ArbitraryCall.t.sol (1 hunks)
  • packages/contracts-rfq/test/FastBridgeV2.GasBench.Src.PFees.t.sol (1 hunks)
  • packages/contracts-rfq/test/FastBridgeV2.GasBench.Src.t.sol (4 hunks)
  • packages/contracts-rfq/test/FastBridgeV2.t.sol (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/contracts-rfq/test/FastBridgeV2.GasBench.Dst.t.sol
🧰 Additional context used
🔇 Additional comments (15)
packages/contracts-rfq/test/FastBridgeV2.GasBench.Dst.Excl.t.sol (2)

7-7: LGTM! Contract name updated for consistency.

The contract name change to FastBridgeV2GasBenchmarkDstExclusivityTest is consistent with the import statement update and follows a clear naming convention. This improvement enhances code readability and maintains consistency within the testing framework.


4-4: LGTM! Verify the import change across the codebase.

The updated import statement reflects a more descriptive and consistent naming convention for the base test contract. This change aligns well with the PR objectives of enhancing the testing framework.

To ensure consistency, let's verify this change across the codebase:

✅ Verification successful

Import verification successful!

All occurrences of the old contract name have been removed, and the new contract name is consistently used across the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the updated import statement and contract name usage.

# Test 1: Check for any remaining occurrences of the old contract name
echo "Checking for old contract name usage:"
rg "FastBridgeV2DstGasBenchmarkTest" --type solidity

# Test 2: Verify the usage of the new contract name
echo "Verifying new contract name usage:"
rg "FastBridgeV2GasBenchmarkDstTest" --type solidity

Length of output: 335


Script:

#!/bin/bash
# Description: Verify the updated import statement and contract name usage by searching Solidity files.

# Test 1: Check for any remaining occurrences of the old contract name
echo "Checking for old contract name usage:"
rg "FastBridgeV2DstGasBenchmarkTest" -g "*.sol"

# Test 2: Verify the usage of the new contract name
echo "Verifying new contract name usage:"
rg "FastBridgeV2GasBenchmarkDstTest" -g "*.sol"

Length of output: 1013

packages/contracts-rfq/test/FastBridgeV2.GasBench.Src.ArbitraryCall.t.sol (2)

1-2: LGTM: Appropriate license and Solidity version.

The file correctly starts with the MIT license identifier and uses an up-to-date Solidity version (^0.8.20).


4-4: LGTM: Correct import statement.

The import statement correctly imports the parent contract FastBridgeV2GasBenchmarkSrcTest from the relative path.

packages/contracts-rfq/test/FastBridgeV2.GasBench.Encoding.t.sol (2)

7-7: Improved contract naming for better clarity.

The contract name change from FastBridgeV2GasBenchmarkSrcProtocolFeesTest to FastBridgeV2GasBenchmarkEncodingTest better reflects the purpose of the contract, focusing on encoding benchmarks. This improvement enhances code readability and maintainability.


Line range hint 8-16: Existing test functions provide a solid baseline.

The unchanged functions test_getBridgeTransaction() and test_getBridgeTransactionV2() continue to provide essential baseline tests for the getBridgeTransaction and getBridgeTransactionV2 methods. Their preservation maintains consistency in the testing suite, while the newly added function complements them by covering the specific case of arbitrary calls.

packages/contracts-rfq/test/FastBridgeV2.GasBench.Dst.ArbitraryCall.t.sol (1)

1-14: LGTM: Well-structured contract declaration with clear purpose.

The contract declaration and inheritance are well-defined. The comment provides valuable context about the purpose of using a mock recipient for arbitrary call testing, which is helpful for understanding the test setup.

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

41-48: Nonce initialization is clear and well-documented

Setting the nonces explicitly and documenting the reasoning enhances readability and ensures the correct sequencing of transactions for the gas benchmarks.


71-77: Efficient initialization of existing transactions

Using cheatSenderNonce(userA, 1) to set the initial nonce avoids inflated gas costs due to storage writes from zero. The subsequent bridge transactions correctly simulate prior activity.


103-107: Good practice by abstracting assertions into helper function checkAfterBridgeToken()

Extracting repeated assertions into checkAfterBridgeToken() improves code maintainability and reduces duplication in test cases.


109-112: Refactored test_bridge_token() enhances test clarity

By calling the helper function checkAfterBridgeToken(), the test function becomes more concise and focused on the action rather than the assertions.


117-117: Ensure consistency in using helper functions across tests

In test_bridge_token_withExclusivity(), correctly using checkAfterBridgeToken() maintains consistency and reduces code duplication.


186-190: Effective use of checkAfterBridgeEth() for assertions

Creating checkAfterBridgeEth() to handle repeated assertions enhances code reuse and readability in ETH bridge tests.


192-195: Simplified test_bridge_eth() improves test readability

Refactoring to use checkAfterBridgeEth() streamlines the test function and emphasizes the primary action being tested.


200-200: Consistency in test assertions with helper functions

Using checkAfterBridgeEth() in test_bridge_eth_withExclusivity() maintains consistent testing patterns across different scenarios.

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.

1 participant