-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate GPv2Signing recoverOrderFromTrade tests to Foundry (#204)
## Description See title. This also introduces a small library to make it easier to create a test that uses fuzzed orders. Fuzzed tests are used here because the exact bytes used in the tests are important, and fuzzing allows us to check if changing some bytes affects the test output. Previously, the sample order in the test was built by filling each byte with a nonzero value. I also merged the tests "should round-trip encode order data" and "should compute order unique identifier" because it was just one line more in the first test, and now that we do fuzzing it's more efficient to do both in the same test rather than running independent fuzzes on both. Here the signing and encoding libraries proved to be very nice to use! ## Test Plan CI. ## Related Issues #120 --------- Co-authored-by: mfw78 <[email protected]>
- Loading branch information
Showing
4 changed files
with
111 additions
and
109 deletions.
There are no files selected for viewing
This file contains 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 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 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,73 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
pragma solidity ^0.8; | ||
|
||
import {Vm} from "forge-std/Test.sol"; | ||
|
||
import {EIP1271Verifier, GPv2EIP1271, GPv2Order, GPv2Signing} from "src/contracts/mixins/GPv2Signing.sol"; | ||
|
||
import {Helper} from "./Helper.sol"; | ||
import {Order} from "test/libraries/Order.sol"; | ||
import {Sign} from "test/libraries/Sign.sol"; | ||
import {SettlementEncoder} from "test/libraries/encoders/SettlementEncoder.sol"; | ||
|
||
contract RecoverOrderFromTrade is Helper { | ||
using SettlementEncoder for SettlementEncoder.State; | ||
using Sign for EIP1271Verifier; | ||
|
||
Vm.Wallet private trader; | ||
|
||
constructor() { | ||
trader = vm.createWallet("GPv2Signing.RecoverOrderFromTrade: trader"); | ||
} | ||
|
||
function test_should_round_trip_encode_order_data_and_unique_identifier( | ||
Order.Fuzzed memory params, | ||
uint256 executedAmount | ||
) public { | ||
GPv2Order.Data memory order = Order.fuzz(params); | ||
|
||
SettlementEncoder.State storage encoder = SettlementEncoder.makeSettlementEncoder(); | ||
encoder.signEncodeTrade(vm, trader, order, domainSeparator, GPv2Signing.Scheme.Eip712, executedAmount); | ||
|
||
GPv2Signing.RecoveredOrder memory recovered = | ||
executor.recoverOrderFromTradeTest(encoder.tokens(), encoder.trades[0]); | ||
assertEq(abi.encode(recovered.data), abi.encode(order)); | ||
assertEq(recovered.uid, Order.computeOrderUid(order, domainSeparator, trader.addr)); | ||
} | ||
|
||
function test_should_recover_the_order_for_all_signing_schemes(Order.Fuzzed memory params) public { | ||
GPv2Order.Data memory order = Order.fuzz(params); | ||
|
||
address traderPreSign = makeAddr("trader pre-sign"); | ||
EIP1271Verifier traderEip1271 = EIP1271Verifier(makeAddr("eip1271 verifier")); | ||
Vm.Wallet memory traderEip712 = vm.createWallet("trader eip712"); | ||
Vm.Wallet memory traderEthsign = vm.createWallet("trader ethsign"); | ||
|
||
bytes memory uidPreSign = Order.computeOrderUid(order, domainSeparator, traderPreSign); | ||
vm.prank(traderPreSign); | ||
executor.setPreSignature(uidPreSign, true); | ||
|
||
vm.mockCallRevert(address(traderEip1271), hex"", "unexpected call to mock contract"); | ||
vm.mockCall( | ||
address(traderEip1271), | ||
abi.encodePacked(EIP1271Verifier.isValidSignature.selector), | ||
abi.encode(GPv2EIP1271.MAGICVALUE) | ||
); | ||
|
||
SettlementEncoder.State storage encoder = SettlementEncoder.makeSettlementEncoder(); | ||
encoder.encodeTrade(order, Sign.preSign(traderPreSign), 0); | ||
encoder.encodeTrade(order, Sign.sign(traderEip1271, hex""), 0); | ||
encoder.signEncodeTrade(vm, traderEip712, order, domainSeparator, GPv2Signing.Scheme.Eip712, 0); | ||
encoder.signEncodeTrade(vm, traderEthsign, order, domainSeparator, GPv2Signing.Scheme.EthSign, 0); | ||
|
||
GPv2Signing.RecoveredOrder memory recovered; | ||
recovered = executor.recoverOrderFromTradeTest(encoder.tokens(), encoder.trades[0]); | ||
assertEq(recovered.owner, traderPreSign); | ||
recovered = executor.recoverOrderFromTradeTest(encoder.tokens(), encoder.trades[1]); | ||
assertEq(recovered.owner, address(traderEip1271)); | ||
recovered = executor.recoverOrderFromTradeTest(encoder.tokens(), encoder.trades[2]); | ||
assertEq(recovered.owner, traderEip712.addr); | ||
recovered = executor.recoverOrderFromTradeTest(encoder.tokens(), encoder.trades[3]); | ||
assertEq(recovered.owner, traderEthsign.addr); | ||
} | ||
} |
This file contains 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