Skip to content

Commit

Permalink
test: units
Browse files Browse the repository at this point in the history
  • Loading branch information
0xShaito committed Jul 30, 2024
1 parent 8192c4b commit 49a4120
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 18 deletions.
2 changes: 1 addition & 1 deletion solidity/contracts/extensions/AccountingExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ contract AccountingExtension is IAccountingExtension {
address _sender
) external onlyAllowedModule(_requestId) onlyParticipant(_requestId, _bonder) {
bool _moduleApproved = _approvals[_bonder].contains(msg.sender);
bool _senderApproved = _bonder == _sender || _approvals[_bonder].contains(_sender);
bool _senderApproved = _approvals[_bonder].contains(_sender);

if (!(_moduleApproved && _senderApproved)) {
revert AccountingExtension_InsufficientAllowance();
Expand Down
23 changes: 16 additions & 7 deletions solidity/contracts/modules/response/BondedResponseModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,22 @@ contract BondedResponseModule is Module, IBondedResponseModule {
if (_status == IOracle.DisputeStatus.Lost) revert BondedResponseModule_AlreadyResponded();
}

_params.accountingExtension.bond({
_bonder: _response.proposer,
_requestId: _response.requestId,
_token: _params.bondToken,
_amount: _params.bondSize,
_sender: _sender
});
if (_sender != _response.proposer) {
_params.accountingExtension.bond({
_bonder: _response.proposer,
_requestId: _response.requestId,
_token: _params.bondToken,
_amount: _params.bondSize,
_sender: _sender
});
} else {
_params.accountingExtension.bond({
_bonder: _response.proposer,
_requestId: _response.requestId,
_token: _params.bondToken,
_amount: _params.bondSize
});
}

emit ResponseProposed(_response.requestId, _response, block.number);
}
Expand Down
66 changes: 63 additions & 3 deletions solidity/test/unit/extensions/AccountingExtension.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,19 @@ contract AccountingExtension_Unit_Bond is BaseTest {
uint256 _amount,
uint256 _initialBalance,
address _bonder,
address _sender
address _sender,
address _module
) public {
_amount = bound(_amount, 0, _initialBalance);

vm.prank(_bonder);
extension.approveModule(_module);

vm.prank(_bonder);
extension.approveModule(_sender);

// Mock and expect the module calling validation
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.allowedModule, (_requestId, _sender)), abi.encode(true));
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.allowedModule, (_requestId, _module)), abi.encode(true));

// Mock and expect the module checking for participant
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.isParticipant, (_requestId, _bonder)), abi.encode(true));
Expand All @@ -190,7 +194,7 @@ contract AccountingExtension_Unit_Bond is BaseTest {
_expectEmit(address(extension));
emit Bonded(_requestId, _bonder, token, _amount);

vm.prank(_sender);
vm.prank(_module);
extension.bond({_bonder: _bonder, _requestId: _requestId, _token: token, _amount: _amount});

// Check: is the balanceOf decreased?
Expand Down Expand Up @@ -293,6 +297,62 @@ contract AccountingExtension_Unit_Bond is BaseTest {
vm.prank(_sender);
extension.bond({_bonder: _bonder, _requestId: _requestId, _token: token, _amount: _amount, _sender: _sender});
}

/**
* @notice Test bonding reverting if only the sender is approved but not the module
*/
function test_withCaller_revertIfInsufficientAllowance_onlySender(
bytes32 _requestId,
uint256 _amount,
address _bonder,
address _sender,
address _module
) public {
vm.assume(_sender != _module);

vm.prank(_bonder);
extension.approveModule(_sender);

// mock the module calling validation
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.allowedModule, (_requestId, _module)), abi.encode(true));

// Mock and expect the module checking for a participant
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.isParticipant, (_requestId, _bonder)), abi.encode(true));

// Check: does it revert if the caller is not approved?
vm.expectRevert(IAccountingExtension.AccountingExtension_InsufficientAllowance.selector);

vm.prank(_module);
extension.bond({_bonder: _bonder, _requestId: _requestId, _token: token, _amount: _amount, _sender: _sender});
}

/**
* @notice Test bonding reverting if only the module is approved but not the sender
*/
function test_withCaller_revertIfInsufficientAllowance_onlyModule(
bytes32 _requestId,
uint256 _amount,
address _bonder,
address _sender,
address _module
) public {
vm.assume(_sender != _module);

vm.prank(_bonder);
extension.approveModule(_module);

// mock the module calling validation
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.allowedModule, (_requestId, _module)), abi.encode(true));

// Mock and expect the module checking for a participant
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.isParticipant, (_requestId, _bonder)), abi.encode(true));

// Check: does it revert if the caller is not approved?
vm.expectRevert(IAccountingExtension.AccountingExtension_InsufficientAllowance.selector);

vm.prank(_module);
extension.bond({_bonder: _bonder, _requestId: _requestId, _token: token, _amount: _amount, _sender: _sender});
}
}

contract AccountingExtension_Unit_Pay is BaseTest {
Expand Down
49 changes: 42 additions & 7 deletions solidity/test/unit/modules/response/BondedResponseModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,12 @@ contract BondedResponseModule_Unit_Propose is BaseTest {
// Mock and expect IAccountingExtension.bond to be called
_mockAndExpect(
address(accounting),
abi.encodeWithSignature(
'bond(address,bytes32,address,uint256,address)', _proposer, _requestId, _token, _bondSize, _sender
),
abi.encodeWithSignature('bond(address,bytes32,address,uint256)', _proposer, _requestId, _token, _bondSize),
abi.encode()
);

vm.prank(address(oracle));
bondedResponseModule.propose(mockRequest, mockResponse, _sender);
bondedResponseModule.propose(mockRequest, mockResponse, _proposer);
}

function test_emitsEvent(
Expand All @@ -163,16 +161,53 @@ contract BondedResponseModule_Unit_Propose is BaseTest {
// Mock and expect IOracle.getResponseIds to be called
_mockAndExpect(
address(accounting),
abi.encodeWithSignature(
'bond(address,bytes32,address,uint256,address)', _proposer, _requestId, _token, _bondSize, _sender
),
abi.encodeWithSignature('bond(address,bytes32,address,uint256)', _proposer, _requestId, _token, _bondSize),
abi.encode()
);

// Check: is the event emitted?
vm.expectEmit(true, true, true, true, address(bondedResponseModule));
emit ResponseProposed({_requestId: _requestId, _response: mockResponse, _blockNumber: block.number});

vm.prank(address(oracle));
bondedResponseModule.propose(mockRequest, mockResponse, _proposer);
}

/**
* @notice Test that the propose function works correctly and bonds the proposer's funds when the sender is different than the proposer
*/
function test_propose_another_sender(
IERC20 _token,
uint256 _bondSize,
uint256 _deadline,
uint256 _disputeWindow,
address _sender,
address _proposer
) public assumeFuzzable(_sender) assumeFuzzable(_proposer) {
vm.assume(_sender != _proposer);
_deadline = bound(_deadline, block.timestamp + 1, type(uint248).max);
_disputeWindow = bound(_disputeWindow, 61, 365 days);
_bondSize = bound(_bondSize, 0, type(uint248).max);

// Set the response module parameters
mockRequest.responseModuleData = abi.encode(accounting, _token, _bondSize, _deadline, _disputeWindow);

bytes32 _requestId = _getId(mockRequest);
mockResponse.requestId = _requestId;
mockResponse.proposer = _proposer;

// Mock and expect IOracle.getResponseIds to be called
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.getResponseIds, _requestId), abi.encode(new bytes32[](0)));

// Mock and expect IAccountingExtension.bond to be called
_mockAndExpect(
address(accounting),
abi.encodeWithSignature(
'bond(address,bytes32,address,uint256,address)', _proposer, _requestId, _token, _bondSize, _sender
),
abi.encode()
);

vm.prank(address(oracle));
bondedResponseModule.propose(mockRequest, mockResponse, _sender);
}
Expand Down

0 comments on commit 49a4120

Please sign in to comment.