Skip to content

Commit

Permalink
feat: add btt tests for BCoWPool commit (#162)
Browse files Browse the repository at this point in the history
* test: btt tests for bcowpool.commit

* chore: remove preexisting unit tests replaced by ones in this pr

* test: reorganize commit tree

* fix: make bulloak happy
  • Loading branch information
0xteddybear authored Jul 22, 2024
1 parent 49aba91 commit 76ea1ef
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
29 changes: 0 additions & 29 deletions test/unit/BCoWPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,6 @@ contract BCoWPool_Unit_Constructor is BaseCoWPoolTest {
}
}

contract BCoWPool_Unit_Commit is BaseCoWPoolTest {
function test_Revert_NonSolutionSettler(address sender, bytes32 orderHash) public {
vm.assume(sender != cowSolutionSettler);
vm.prank(sender);
vm.expectRevert(IBCoWPool.CommitOutsideOfSettlement.selector);
bCoWPool.commit(orderHash);
}

function test_Revert_CommitmentAlreadySet(bytes32 _existingCommitment, bytes32 _newCommitment) public {
vm.assume(_existingCommitment != bytes32(0));
bCoWPool.call__setLock(_existingCommitment);
vm.prank(cowSolutionSettler);
vm.expectRevert(IBPool.BPool_Reentrancy.selector);
bCoWPool.commit(_newCommitment);
}

function test_Call_SetLock(bytes32 orderHash) public {
bCoWPool.expectCall__setLock(orderHash);
vm.prank(cowSolutionSettler);
bCoWPool.commit(orderHash);
}

function test_Set_ReentrancyLock(bytes32 orderHash) public {
vm.prank(cowSolutionSettler);
bCoWPool.commit(orderHash);
assertEq(bCoWPool.call__getLock(), orderHash);
}
}

contract BCoWPool_Unit_IsValidSignature is BaseCoWPoolTest {
function setUp() public virtual override {
super.setUp();
Expand Down
35 changes: 31 additions & 4 deletions test/unit/BCoWPool/BCoWPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import {IERC20} from '@cowprotocol/interfaces/IERC20.sol';
import {BCoWPoolBase} from './BCoWPoolBase.sol';

import {IBCoWFactory} from 'interfaces/IBCoWFactory.sol';

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

contract BCoWPool_afterFinalize is BCoWPoolBase {
contract BCoWPool is BCoWPoolBase {
bytes32 public commitmentValue = bytes32(uint256(0xf00ba5));
uint256 public tokenWeight = 1e18;

function setUp() public virtual override {
super.setUp();

bCoWPool.set__tokens(tokens);
bCoWPool.set__records(tokens[0], IBPool.Record({bound: true, index: 0, denorm: tokenWeight}));
bCoWPool.set__records(tokens[1], IBPool.Record({bound: true, index: 1, denorm: tokenWeight}));
Expand All @@ -23,7 +27,7 @@ contract BCoWPool_afterFinalize is BCoWPoolBase {
vm.mockCall(tokens[1], abi.encodeCall(IERC20.approve, (vaultRelayer, type(uint256).max)), abi.encode(true));
}

function test_WhenCalled() external {
function test__afterFinalizeWhenCalled() external {
// it calls approve on every bound token
vm.expectCall(tokens[0], abi.encodeCall(IERC20.approve, (vaultRelayer, type(uint256).max)));
vm.expectCall(tokens[1], abi.encodeCall(IERC20.approve, (vaultRelayer, type(uint256).max)));
Expand All @@ -32,16 +36,39 @@ contract BCoWPool_afterFinalize is BCoWPoolBase {
bCoWPool.call__afterFinalize();
}

function test_WhenFactorysLogBCoWPoolDoesNotRevert() external {
function test__afterFinalizeWhenFactorysLogBCoWPoolDoesNotRevert() external {
// it returns
bCoWPool.call__afterFinalize();
}

function test_WhenFactorysLogBCoWPoolReverts(bytes memory revertData) external {
function test__afterFinalizeWhenFactorysLogBCoWPoolReverts(bytes memory revertData) external {
vm.mockCallRevert(address(this), abi.encodeCall(IBCoWFactory.logBCoWPool, ()), revertData);
// it emits a COWAMMPoolCreated event
vm.expectEmit(address(bCoWPool));
emit IBCoWFactory.COWAMMPoolCreated(address(bCoWPool));
bCoWPool.call__afterFinalize();
}

function test_CommitRevertWhen_ReentrancyLockIsSet(bytes32 lockValue) external {
vm.assume(lockValue != _MUTEX_FREE);
bCoWPool.call__setLock(lockValue);
// it should revert
vm.expectRevert(IBPool.BPool_Reentrancy.selector);
bCoWPool.commit(commitmentValue);
}

function test_CommitRevertWhen_SenderIsNotSolutionSettler(address caller) external {
vm.assume(caller != cowSolutionSettler);
vm.prank(caller);
// it should revert
vm.expectRevert(abi.encodeWithSelector(IBCoWPool.CommitOutsideOfSettlement.selector));
bCoWPool.commit(commitmentValue);
}

function test_CommitWhenPreconditionsAreMet(bytes32 commitmentValue_) external {
vm.prank(cowSolutionSettler);
bCoWPool.commit(commitmentValue_);
// it should set the transient reentrancy lock
assertEq(bCoWPool.call__getLock(), commitmentValue_);
}
}
8 changes: 8 additions & 0 deletions test/unit/BCoWPool/BCoWPool.tree
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ BCoWPool::_afterFinalize
│ └── it returns
└── when factorys logBCoWPool reverts
└── it emits a COWAMMPoolCreated event

BCoWPool::Commit
├── when reentrancy lock is set
│ └──it should revert
├── when sender is not solution settler
│ └──it should revert
└── when preconditions are met
└── it should set the transient reentrancy lock

0 comments on commit 76ea1ef

Please sign in to comment.