-
Notifications
You must be signed in to change notification settings - Fork 0
tests: add solver delegate coverage #3
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
Open
igorroncevic
wants to merge
12
commits into
feat/setup
Choose a base branch
from
test/add-tests
base: feat/setup
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cf9c6c6
tests: wip
igorroncevic 0f099e3
better tests
igorroncevic 3f02a52
unit tests
igorroncevic 4d3ca32
refactor
igorroncevic c0e4aa2
integration tests
igorroncevic 7e00d57
fork tests
igorroncevic 6dd4735
check snapshots in CI
igorroncevic 6014b9e
add other networks
igorroncevic d5f4220
more precise snapshot
igorroncevic 7377c12
test: address PR review comments
igorroncevic d0db7b4
add rpc urls to env
igorroncevic 626efad
fix tests
igorroncevic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "7702 submission - reverts when - unauthorized error when revert": "460", | ||
| "7702 submission - success - forwards large settlement payload": "96271", | ||
| "7702 submission - success - forwards small settlement payload": "95120", | ||
| "7702 submission - success - settlement sees solver eoa": "121332" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "abi encoded envelope - success - does not reach intended target": "3143", | ||
| "empty calldata - success - receives eth from any caller": "47", | ||
| "packed calldata - reverts when - target empty data when revert": "3753", | ||
| "packed calldata - reverts when - target revert data when revert": "3828", | ||
| "packed calldata - reverts when - unauthorized error when revert": "460", | ||
| "packed calldata - success - calls target with empty payload": "25406", | ||
| "packed calldata approved caller 0 - success - first target call gas only": "47667", | ||
| "packed calldata approved caller 1 - success - repeat target call gas only": "16786", | ||
| "packed calldata approved caller 2 - success - repeat target call gas only": "16820", | ||
| "packed calldata approved caller 3 - success - repeat target call gas only": "16854", | ||
| "packed calldata approved caller 4 - success - repeat target call gas only": "16889", | ||
| "packed calldata no code target - success - returns empty data": "3131", | ||
| "packed calldata return data - success - bubbles exact return data": "3767", | ||
| "packed calldata zero msg value - success - forwards calldata": "26227" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // SPDX-License-Identifier: MIT OR Apache-2.0 | ||
| pragma solidity ^0.8.34; | ||
|
|
||
| import {Test} from "forge-std/Test.sol"; | ||
|
|
||
| import {Solver7702Delegate} from "src/Solver7702Delegate.sol"; | ||
|
|
||
| struct ApprovedCallers { | ||
| address first; | ||
| address second; | ||
| address third; | ||
| address fourth; | ||
| address fifth; | ||
| } | ||
|
|
||
| abstract contract BaseTest is Test { | ||
| uint256 internal constant SOLVER_PRIVATE_KEY = uint256(keccak256("SOLVER_PRIVATE_KEY")); | ||
| uint256 internal constant MSG_VALUE = 1 ether; | ||
|
|
||
| Solver7702Delegate internal delegateContract; | ||
|
|
||
| address internal solver; | ||
| ApprovedCallers internal approvedCallers; | ||
|
|
||
| function setUp() public virtual { | ||
| solver = vm.addr(SOLVER_PRIVATE_KEY); | ||
| approvedCallers = ApprovedCallers({ | ||
| first: makeAddr("APPROVED_CALLER_0"), | ||
| second: makeAddr("APPROVED_CALLER_1"), | ||
| third: makeAddr("APPROVED_CALLER_2"), | ||
| fourth: makeAddr("APPROVED_CALLER_3"), | ||
| fifth: makeAddr("APPROVED_CALLER_4") | ||
| }); | ||
|
|
||
| delegateContract = new Solver7702Delegate( | ||
| [ | ||
| approvedCallers.first, | ||
| approvedCallers.second, | ||
| approvedCallers.third, | ||
| approvedCallers.fourth, | ||
| approvedCallers.fifth | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| function _packedCalldata(address target, bytes memory payload) internal pure returns (bytes memory) { | ||
| return abi.encodePacked(bytes20(target), payload); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // SPDX-License-Identifier: MIT OR Apache-2.0 | ||
| // solhint-disable avoid-low-level-calls, gas-small-strings | ||
| pragma solidity ^0.8.34; | ||
|
|
||
| import {BaseTest} from "test/BaseTest.t.sol"; | ||
| import {MockSettlement} from "test/mocks/MockSettlement.sol"; | ||
|
|
||
| contract Solver7702DelegateForkTest is BaseTest { | ||
| uint256 internal constant SETTLEMENT_AMOUNT = 12.34 ether; | ||
| bytes32 internal constant FORK_ORDER_UID = keccak256("fork order"); | ||
|
|
||
| function test_fork_7702Submission_success_settlementSeesSolverEoa() public { | ||
| // ~~~~~~~~~~ Setup ~~~~~~~~~~ | ||
| string memory mainnetRpcUrl = vm.envOr("MAINNET_RPC_URL", string("")); | ||
| if (bytes(mainnetRpcUrl).length == 0) { | ||
| vm.skip(true, "MAINNET_RPC_URL not set"); | ||
| } | ||
|
|
||
| vm.createSelectFork(mainnetRpcUrl); | ||
| MockSettlement settlement = new MockSettlement(); | ||
| bytes memory payload = abi.encodeCall(MockSettlement.settle, (FORK_ORDER_UID, SETTLEMENT_AMOUNT)); | ||
| vm.signAndAttachDelegation(address(delegateContract), SOLVER_PRIVATE_KEY); | ||
|
|
||
| // ~~~~~~~~~~ Call ~~~~~~~~~~ | ||
| vm.prank(approvedCallers.first); | ||
| (bool success, bytes memory returnData) = solver.call(_packedCalldata(address(settlement), payload)); | ||
| vm.snapshotGasLastCall("7702 submission - success - settlement sees solver eoa on fork"); | ||
|
|
||
| // ~~~~~~~~~~ Assertions ~~~~~~~~~~ | ||
| assertTrue(success); | ||
| assertEq(abi.decode(returnData, (bytes32)), FORK_ORDER_UID); | ||
| assertEq(settlement.lastSender(), solver); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // SPDX-License-Identifier: MIT OR Apache-2.0 | ||
| // solhint-disable avoid-low-level-calls, gas-small-strings | ||
| pragma solidity ^0.8.34; | ||
|
|
||
| import {Solver7702Delegate} from "src/Solver7702Delegate.sol"; | ||
| import {BaseTest} from "test/BaseTest.t.sol"; | ||
| import {MockSettlement} from "test/mocks/MockSettlement.sol"; | ||
|
|
||
| contract Solver7702DelegateIntegrationTest is BaseTest { | ||
| uint256 internal constant SETTLEMENT_AMOUNT = 12.34 ether; | ||
| uint256 internal constant SMALL_SETTLEMENT_WORDS = 1; | ||
| uint256 internal constant LARGE_SETTLEMENT_WORDS = 64; | ||
| bytes32 internal constant ORDER_UID = keccak256("order"); | ||
|
|
||
| MockSettlement internal settlement; | ||
| address internal unauthorizedCaller; | ||
|
|
||
| function setUp() public override { | ||
| super.setUp(); | ||
|
|
||
| settlement = new MockSettlement(); | ||
| unauthorizedCaller = makeAddr("UNAUTHORIZED_CALLER"); | ||
| } | ||
|
|
||
| function test_integration_7702Submission_success_settlementSeesSolverEoa() public { | ||
| // ~~~~~~~~~~ Setup ~~~~~~~~~~ | ||
| bytes memory payload = abi.encodeCall(MockSettlement.settle, (ORDER_UID, SETTLEMENT_AMOUNT)); | ||
| vm.deal(approvedCallers.first, MSG_VALUE); | ||
| vm.signAndAttachDelegation(address(delegateContract), SOLVER_PRIVATE_KEY); | ||
|
|
||
| // ~~~~~~~~~~ Call ~~~~~~~~~~ | ||
| vm.prank(approvedCallers.first); | ||
| (bool success, bytes memory returnData) = | ||
| solver.call{value: MSG_VALUE}(_packedCalldata(address(settlement), payload)); | ||
| vm.snapshotGasLastCall("7702 submission - success - settlement sees solver eoa"); | ||
|
|
||
| // ~~~~~~~~~~ Assertions ~~~~~~~~~~ | ||
| assertTrue(success); | ||
| assertEq(abi.decode(returnData, (bytes32)), ORDER_UID); | ||
| assertEq(settlement.lastSender(), solver); | ||
| assertEq(settlement.lastValue(), MSG_VALUE); | ||
| assertEq(settlement.lastOrderUid(), ORDER_UID); | ||
| assertEq(settlement.lastAmount(), SETTLEMENT_AMOUNT); | ||
| assertEq(address(settlement).balance, MSG_VALUE); | ||
| } | ||
|
|
||
| function test_integration_7702Submission_success_forwardsSmallSettlementPayload() public { | ||
| // ~~~~~~~~~~ Setup ~~~~~~~~~~ | ||
| bytes memory settlementPayload = _settlementPayload(SMALL_SETTLEMENT_WORDS); | ||
| bytes memory payload = abi.encodeCall(MockSettlement.settlePayload, (settlementPayload)); | ||
| vm.signAndAttachDelegation(address(delegateContract), SOLVER_PRIVATE_KEY); | ||
|
|
||
| // ~~~~~~~~~~ Call ~~~~~~~~~~ | ||
| vm.prank(approvedCallers.first); | ||
| (bool success, bytes memory returnData) = solver.call(_packedCalldata(address(settlement), payload)); | ||
| vm.snapshotGasLastCall("7702 submission - success - forwards small settlement payload"); | ||
|
|
||
| // ~~~~~~~~~~ Assertions ~~~~~~~~~~ | ||
| assertTrue(success); | ||
| assertEq(abi.decode(returnData, (bytes32)), keccak256(settlementPayload)); | ||
| assertEq(settlement.lastSender(), solver); | ||
| assertEq(settlement.lastPayloadHash(), keccak256(settlementPayload)); | ||
| assertEq(settlement.lastPayloadLength(), settlementPayload.length); | ||
| } | ||
|
|
||
| function test_integration_7702Submission_success_forwardsLargeSettlementPayload() public { | ||
| // ~~~~~~~~~~ Setup ~~~~~~~~~~ | ||
| bytes memory settlementPayload = _settlementPayload(LARGE_SETTLEMENT_WORDS); | ||
| bytes memory payload = abi.encodeCall(MockSettlement.settlePayload, (settlementPayload)); | ||
| vm.signAndAttachDelegation(address(delegateContract), SOLVER_PRIVATE_KEY); | ||
|
|
||
| // ~~~~~~~~~~ Call ~~~~~~~~~~ | ||
| vm.prank(approvedCallers.first); | ||
| (bool success, bytes memory returnData) = solver.call(_packedCalldata(address(settlement), payload)); | ||
| vm.snapshotGasLastCall("7702 submission - success - forwards large settlement payload"); | ||
|
|
||
| // ~~~~~~~~~~ Assertions ~~~~~~~~~~ | ||
| assertTrue(success); | ||
| assertEq(abi.decode(returnData, (bytes32)), keccak256(settlementPayload)); | ||
| assertEq(settlement.lastSender(), solver); | ||
| assertEq(settlement.lastPayloadHash(), keccak256(settlementPayload)); | ||
| assertEq(settlement.lastPayloadLength(), settlementPayload.length); | ||
| } | ||
|
|
||
| function test_integration_7702Submission_revertsWhen_callerUnauthorized_UnauthorizedErrorWhenRevert() public { | ||
| // ~~~~~~~~~~ Setup ~~~~~~~~~~ | ||
| bytes memory payload = abi.encodeCall(MockSettlement.settle, (ORDER_UID, SETTLEMENT_AMOUNT)); | ||
| vm.signAndAttachDelegation(address(delegateContract), SOLVER_PRIVATE_KEY); | ||
|
|
||
| // ~~~~~~~~~~ Call ~~~~~~~~~~ | ||
| vm.prank(unauthorizedCaller); | ||
| (bool success, bytes memory returnData) = solver.call(_packedCalldata(address(settlement), payload)); | ||
| vm.snapshotGasLastCall("7702 submission - reverts when - unauthorized error when revert"); | ||
|
|
||
| // ~~~~~~~~~~ Assertions ~~~~~~~~~~ | ||
| assertFalse(success); | ||
| assertEq(returnData, abi.encodeWithSelector(Solver7702Delegate.Unauthorized.selector, unauthorizedCaller)); | ||
| } | ||
|
|
||
| function _settlementPayload(uint256 words) internal pure returns (bytes memory payload) { | ||
| payload = new bytes(words * 32); | ||
| for (uint256 i; i < words; ++i) { | ||
| bytes32 word = keccak256(abi.encode(i)); | ||
| assembly { | ||
| mstore(add(add(payload, 0x20), mul(i, 0x20)), word) | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.