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: add btt for isValidSignature #161

Merged
merged 21 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 0 additions & 70 deletions test/unit/BCoWPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -106,73 +106,3 @@ contract BCoWPool_Unit_Commit is BaseCoWPoolTest {
assertEq(bCoWPool.call__getLock(), orderHash);
}
}

contract BCoWPool_Unit_IsValidSignature is BaseCoWPoolTest {
function setUp() public virtual override {
super.setUp();
for (uint256 i = 0; i < TOKENS_AMOUNT; i++) {
vm.mockCall(tokens[i], abi.encodePacked(IERC20.approve.selector), abi.encode(true));
}
vm.mockCall(address(bCoWPool.FACTORY()), abi.encodeWithSelector(IBCoWFactory.logBCoWPool.selector), abi.encode());
bCoWPool.finalize();
}

modifier happyPath(GPv2Order.Data memory _order) {
// sets the order appData to the one defined at deployment (setUp)
_order.appData = appData;

// stores the order hash in the transient storage slot
bytes32 _orderHash = GPv2Order.hash(_order, domainSeparator);
bCoWPool.call__setLock(_orderHash);
_;
}

function test_Revert_OrderWithWrongAppdata(GPv2Order.Data memory _order, bytes32 _appData) public {
vm.assume(_appData != appData);
_order.appData = _appData;
bytes32 _orderHash = GPv2Order.hash(_order, domainSeparator);
vm.expectRevert(IBCoWPool.AppDataDoesNotMatch.selector);
bCoWPool.isValidSignature(_orderHash, abi.encode(_order));
}

function test_Revert_OrderSignedWithWrongDomainSeparator(
GPv2Order.Data memory _order,
bytes32 _differentDomainSeparator
) public happyPath(_order) {
vm.assume(_differentDomainSeparator != domainSeparator);
bytes32 _orderHash = GPv2Order.hash(_order, _differentDomainSeparator);
vm.expectRevert(IBCoWPool.OrderDoesNotMatchMessageHash.selector);
bCoWPool.isValidSignature(_orderHash, abi.encode(_order));
}

function test_Revert_OrderWithUnrelatedSignature(
GPv2Order.Data memory _order,
bytes32 _orderHash
) public happyPath(_order) {
vm.expectRevert(IBCoWPool.OrderDoesNotMatchMessageHash.selector);
bCoWPool.isValidSignature(_orderHash, abi.encode(_order));
}

function test_Revert_OrderHashDifferentFromCommitment(
GPv2Order.Data memory _order,
bytes32 _differentCommitment
) public happyPath(_order) {
bCoWPool.call__setLock(_differentCommitment);
bytes32 _orderHash = GPv2Order.hash(_order, domainSeparator);
vm.expectRevert(IBCoWPool.OrderDoesNotMatchCommitmentHash.selector);
bCoWPool.isValidSignature(_orderHash, abi.encode(_order));
}

function test_Call_Verify(GPv2Order.Data memory _order) public happyPath(_order) {
bytes32 _orderHash = GPv2Order.hash(_order, domainSeparator);
bCoWPool.mock_call_verify(_order);
bCoWPool.expectCall_verify(_order);
bCoWPool.isValidSignature(_orderHash, abi.encode(_order));
}

function test_Return_MagicValue(GPv2Order.Data memory _order) public happyPath(_order) {
bytes32 _orderHash = GPv2Order.hash(_order, domainSeparator);
bCoWPool.mock_call_verify(_order);
assertEq(bCoWPool.isValidSignature(_orderHash, abi.encode(_order)), IERC1271.isValidSignature.selector);
}
}
56 changes: 56 additions & 0 deletions test/unit/BCoWPool/BCoWPool_IsValidSignature.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {IERC20} from '@cowprotocol/interfaces/IERC20.sol';

import {GPv2Order} from '@cowprotocol/libraries/GPv2Order.sol';
import {IERC1271} from '@openzeppelin/contracts/interfaces/IERC1271.sol';

import {BCoWPoolBase} from './BCoWPoolBase.sol';
import {IBCoWPool} from 'interfaces/IBCoWPool.sol';

contract BCoWPoolIsValidSignature is BCoWPoolBase {
GPv2Order.Data validOrder;
bytes32 validHash;

function setUp() public virtual override {
super.setUp();
// only set up the values that are checked in this method
validOrder.appData = appData;
validHash = GPv2Order.hash(validOrder, domainSeparator);

bCoWPool.mock_call_verify(validOrder);
}

function test_RevertWhen_OrdersAppdataIsDifferent(bytes32 appData_) external {
vm.assume(appData != appData_);
validOrder.appData = appData_;
// it should revert
vm.expectRevert(IBCoWPool.AppDataDoesNotMatch.selector);
bCoWPool.isValidSignature(validHash, abi.encode(validOrder));
}

function test_RevertWhen_OrderHashDoesNotMatchHashedOrder(bytes32 orderHash) external {
vm.assume(orderHash != validHash);
// it should revert
vm.expectRevert(IBCoWPool.OrderDoesNotMatchMessageHash.selector);
bCoWPool.isValidSignature(orderHash, abi.encode(validOrder));
}

function test_RevertWhen_HashedOrderDoesNotMatchCommitment(bytes32 commitment) external {
vm.assume(validHash != commitment);
bCoWPool.call__setLock(commitment);
// it should revert
vm.expectRevert(IBCoWPool.OrderDoesNotMatchCommitmentHash.selector);
bCoWPool.isValidSignature(validHash, abi.encode(validOrder));
}

function test_WhenPreconditionsAreMet() external {
// can't do it in setUp because transient storage is wiped in between
bCoWPool.call__setLock(validHash);
// it calls verify
bCoWPool.expectCall_verify(validOrder);
// it returns EIP-1271 magic value
assertEq(bCoWPool.isValidSignature(validHash, abi.encode(validOrder)), IERC1271.isValidSignature.selector);
}
}
10 changes: 10 additions & 0 deletions test/unit/BCoWPool/BCoWPool_IsValidSignature.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
BCoWPool::IsValidSignature
├── when orders appdata is different
Copy link
Member

Choose a reason for hiding this comment

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

than allowed? 👀

│ └── it should revert
├── when orderHash does not match hashed order
│ └── it should revert
├── when hashed order does not match commitment
│ └── it should revert
└── when preconditions are met
├── it calls verify
└── it returns EIP-1271 magic value
Loading