Skip to content

Commit

Permalink
test: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gas1cent committed Nov 11, 2023
1 parent 6ce0907 commit da99cdd
Showing 1 changed file with 76 additions and 71 deletions.
147 changes: 76 additions & 71 deletions solidity/test/unit/modules/resolution/ArbitratorModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ contract BaseTest is Test, Helpers {
IOracle public oracle;
// A mock arbitrator
IArbitrator public arbitrator;
// Create a new dummy dispute
IOracle.Dispute public mockDispute;
// Create a new dummy response
IOracle.Response public mockResponse;
address internal _proposer = makeAddr('proposer');
bytes32 public mockId = bytes32('69');

// Events
event ResolutionStarted(bytes32 indexed _requestId, bytes32 indexed _disputeId);
event DisputeResolved(bytes32 indexed _requestId, bytes32 indexed _disputeId, IOracle.DisputeStatus _status);

Expand All @@ -56,15 +51,6 @@ contract BaseTest is Test, Helpers {
vm.etch(address(arbitrator), hex'069420');

arbitratorModule = new ForTest_ArbitratorModule(oracle);

mockDispute = IOracle.Dispute({
disputer: makeAddr('disputer'),
proposer: makeAddr('proposer'),
responseId: bytes32('69'),
requestId: bytes32('69')
});

mockResponse = IOracle.Response({proposer: _proposer, requestId: mockId, response: bytes('')});
}
}

Expand All @@ -81,7 +67,7 @@ contract ArbitratorModule_Unit_ModuleData is BaseTest {

function test_decodeRequestData(address _arbitrator) public {
// Mock data
bytes memory _requestData = abi.encode(address(_arbitrator));
bytes memory _requestData = abi.encode(_arbitrator);

// Test: decode the given request data
IArbitratorModule.RequestParameters memory _requestParameters = arbitratorModule.decodeRequestData(_requestData);
Expand Down Expand Up @@ -109,156 +95,175 @@ contract ArbitratorModule_Unit_StartResolution is BaseTest {
/**
* @notice Test that the escalate function works as expected
*/
function test_startResolution(bytes32 _disputeId, IOracle.Request calldata _request) public {
// Mock and expect the dummy dispute
mockDispute.requestId = _getId(_request);
function test_startResolution(address _arbitrator) public assumeFuzzable(_arbitrator) {
mockRequest.resolutionModuleData = abi.encode(_arbitrator);
bytes32 _requestId = _getId(mockRequest);

// Store the requestData
bytes memory _requestData = abi.encode(address(arbitrator));
mockResponse.requestId = _requestId;
mockDispute.requestId = _requestId;
bytes32 _disputeId = _getId(mockDispute);

// Mock and expect the callback to the arbitrator
_mockAndExpect(address(arbitrator), abi.encodeCall(arbitrator.resolve, (_disputeId)), abi.encode(bytes('')));
_mockAndExpect(_arbitrator, abi.encodeCall(arbitrator.resolve, (_disputeId)), abi.encode(bytes('')));

vm.prank(address(oracle));
arbitratorModule.startResolution(_disputeId, _request, mockResponse, mockDispute);
arbitratorModule.startResolution(_disputeId, mockRequest, mockResponse, mockDispute);

// Check: is status now Escalated?
assertEq(uint256(arbitratorModule.getStatus(_disputeId)), uint256(IArbitratorModule.ArbitrationStatus.Active));
}

function test_emitsEvent(bytes32 _disputeId, IOracle.Request calldata _request) public {
// Mock and expect the dummy dispute
mockDispute.requestId = _getId(_request);

// Store the requestData
bytes memory _requestData = abi.encode(address(arbitrator));
function test_emitsEvent(address _arbitrator) public assumeFuzzable(_arbitrator) {
mockRequest.resolutionModuleData = abi.encode(_arbitrator);
bytes32 _requestId = _getId(mockRequest);
mockResponse.requestId = _requestId;
mockDispute.requestId = _requestId;
bytes32 _disputeId = _getId(mockDispute);

// Mock and expect the callback to the arbitrator
_mockAndExpect(address(arbitrator), abi.encodeCall(arbitrator.resolve, (_disputeId)), abi.encode(bytes('')));
_mockAndExpect(_arbitrator, abi.encodeCall(arbitrator.resolve, (_disputeId)), abi.encode(bytes('')));

// Check: is the event emitted?
vm.expectEmit(true, true, true, true, address(arbitratorModule));
emit ResolutionStarted(mockDispute.requestId, _disputeId);

vm.prank(address(oracle));
arbitratorModule.startResolution(_disputeId, _request, mockResponse, mockDispute);
arbitratorModule.startResolution(_disputeId, mockRequest, mockResponse, mockDispute);
}

function test_revertInvalidCaller(address _caller, bytes32 _disputeId, IOracle.Request calldata _request) public {
function test_revertInvalidCaller(address _caller) public {
vm.assume(_caller != address(oracle));

// Check: does it revert if the caller is not the Oracle?
vm.expectRevert(abi.encodeWithSelector(IModule.Module_OnlyOracle.selector));

vm.prank(_caller);
arbitratorModule.startResolution(_disputeId, _request, mockResponse, mockDispute);
arbitratorModule.startResolution(_getId(mockDispute), mockRequest, mockResponse, mockDispute);
}

function test_revertIfEmptyArbitrator(bytes32 _disputeId, IOracle.Request calldata _request) public {
// Mock and expect the dummy dispute
mockDispute.requestId = _getId(_request);

// Store the requestData
bytes memory _requestData = abi.encode(address(0));
function test_revertIfEmptyArbitrator() public {
mockRequest.resolutionModuleData = abi.encode(address(0));
bytes32 _requestId = _getId(mockRequest);
mockResponse.requestId = _requestId;
mockDispute.requestId = _requestId;

// Check: revert?
vm.expectRevert(abi.encodeWithSelector(IArbitratorModule.ArbitratorModule_InvalidArbitrator.selector));

// Test: escalate the dispute
vm.prank(address(oracle));
arbitratorModule.startResolution(_disputeId, _request, mockResponse, mockDispute);
arbitratorModule.startResolution(_getId(mockDispute), mockRequest, mockResponse, mockDispute);
}
}

contract ArbitratorModule_Unit_ResolveDispute is BaseTest {
/**
* @notice Test that the resolve function works as expected
*/
function test_resolveDispute(bytes32 _disputeId, uint256 _status, IOracle.Request calldata _request) public {
vm.assume(_status <= uint256(IOracle.DisputeStatus.Lost));
vm.assume(_status > uint256(IOracle.DisputeStatus.Escalated));
function test_resolveDispute(uint256 _status, address _arbitrator) public assumeFuzzable(_arbitrator) {
_status = bound(_status, uint256(IOracle.DisputeStatus.Escalated) + 1, uint256(IOracle.DisputeStatus.Lost));
IOracle.DisputeStatus _arbitratorStatus = IOracle.DisputeStatus(_status);

mockRequest.resolutionModuleData = abi.encode(_arbitrator);
mockDispute.requestId = _getId(mockRequest);
bytes32 _disputeId = _getId(mockDispute);

// Mock and expect IOracle.disputeStatus to be called
_mockAndExpect(
address(oracle), abi.encodeCall(oracle.disputeStatus, (_disputeId)), abi.encode(IOracle.DisputeStatus.Escalated)
);

// Mock and expect getAnswer to be called on the arbitrator
_mockAndExpect(
address(arbitrator), abi.encodeCall(arbitrator.getAnswer, (_disputeId)), abi.encode(_arbitratorStatus)
address(_arbitrator), abi.encodeCall(arbitrator.getAnswer, (_disputeId)), abi.encode(_arbitratorStatus)
);

// Mock and expect IOracle.updateDisputeStatus to be called
_mockAndExpect(
address(oracle),
abi.encodeCall(oracle.updateDisputeStatus, (_request, mockResponse, mockDispute, _arbitratorStatus)),
abi.encodeCall(oracle.updateDisputeStatus, (mockRequest, mockResponse, mockDispute, _arbitratorStatus)),
abi.encode()
);

vm.prank(address(oracle));
arbitratorModule.resolveDispute(_disputeId, _request, mockResponse, mockDispute);
arbitratorModule.resolveDispute(_disputeId, mockRequest, mockResponse, mockDispute);

// Check: is status now Resolved?
assertEq(uint256(arbitratorModule.getStatus(_disputeId)), uint256(IArbitratorModule.ArbitrationStatus.Resolved));
}

function test_revertsIfInvalidResolveStatus(
bytes32 _disputeId,
uint256 _status,
IOracle.Request calldata _request
) public {
function test_revertsIfInvalidResolveStatus(uint256 _status, address _arbitrator) public assumeFuzzable(_arbitrator) {
vm.assume(_status <= uint256(IOracle.DisputeStatus.Escalated));
IOracle.DisputeStatus _arbitratorStatus = IOracle.DisputeStatus(_status);

mockRequest.resolutionModuleData = abi.encode(_arbitrator);
mockDispute.requestId = _getId(mockRequest);
bytes32 _disputeId = _getId(mockDispute);

// Mock and expect IOracle.disputeStatus to be called
_mockAndExpect(
address(oracle), abi.encodeCall(oracle.disputeStatus, (_disputeId)), abi.encode(IOracle.DisputeStatus.Escalated)
);

// Mock and expect getAnswer to be called on the arbitrator
_mockAndExpect(
address(arbitrator), abi.encodeCall(arbitrator.getAnswer, (_disputeId)), abi.encode(_arbitratorStatus)
address(_arbitrator), abi.encodeCall(arbitrator.getAnswer, (_disputeId)), abi.encode(_arbitratorStatus)
);

// Check: does it revert if the resolution status is invalid?
vm.expectRevert(abi.encodeWithSelector(IArbitratorModule.ArbitratorModule_InvalidResolutionStatus.selector));

vm.prank(address(oracle));
arbitratorModule.resolveDispute(_disputeId, _request, mockResponse, mockDispute);
arbitratorModule.resolveDispute(_disputeId, mockRequest, mockResponse, mockDispute);
}

function test_emitsEvent(bytes32 _disputeId, uint256 _status, IOracle.Request calldata _request) public {
function test_emitsEvent(uint256 _status, address _arbitrator) public assumeFuzzable(_arbitrator) {
vm.assume(_status <= uint256(IOracle.DisputeStatus.Lost));
vm.assume(_status > uint256(IOracle.DisputeStatus.Escalated));
IOracle.DisputeStatus _arbitratorStatus = IOracle.DisputeStatus(_status);

// Mock and expect getAnswer to be called on the arbitrator
mockRequest.resolutionModuleData = abi.encode(_arbitrator);
mockDispute.requestId = _getId(mockRequest);
bytes32 _disputeId = _getId(mockDispute);

// Mock and expect IOracle.disputeStatus to be called
_mockAndExpect(
address(arbitrator), abi.encodeCall(arbitrator.getAnswer, (_disputeId)), abi.encode(_arbitratorStatus)
address(oracle), abi.encodeCall(oracle.disputeStatus, (_disputeId)), abi.encode(IOracle.DisputeStatus.Escalated)
);

// Mock and expect getAnswer to be called on the arbitrator
_mockAndExpect(_arbitrator, abi.encodeCall(arbitrator.getAnswer, (_disputeId)), abi.encode(_arbitratorStatus));

// Mock and expect IOracle.updateDisputeStatus to be called
_mockAndExpect(
address(oracle),
abi.encodeCall(oracle.updateDisputeStatus, (_request, mockResponse, mockDispute, _arbitratorStatus)),
abi.encodeCall(oracle.updateDisputeStatus, (mockRequest, mockResponse, mockDispute, _arbitratorStatus)),
abi.encode()
);

// Check: is the event emitted?
vm.expectEmit(true, true, true, true, address(arbitratorModule));
emit DisputeResolved(_getId(_request), _disputeId, _arbitratorStatus);
emit DisputeResolved(_getId(mockRequest), _disputeId, _arbitratorStatus);

vm.prank(address(oracle));
arbitratorModule.resolveDispute(_disputeId, _request, mockResponse, mockDispute);
arbitratorModule.resolveDispute(_disputeId, mockRequest, mockResponse, mockDispute);
}

/**
* @notice resolve dispute reverts if the dispute status isn't Active
*/
function test_revertIfInvalidDispute(IOracle.Request calldata _request, bytes32 _disputeId) public {
function test_revertIfInvalidDispute(IOracle.Request calldata _request) public {
// Test the 3 different invalid status (None, Won, Lost)
for (uint256 _status; _status < uint256(type(IOracle.DisputeStatus).max); _status++) {
if (IOracle.DisputeStatus(_status) == IOracle.DisputeStatus.Escalated) continue;
// Create a new dummy dispute
IOracle.Dispute memory _dispute = IOracle.Dispute({
disputer: makeAddr('disputer'),
proposer: makeAddr('proposer'),
responseId: _getId(mockResponse),
requestId: _getId(_request)
});

// Check: does it revert if the dispute id is invalid?
mockDispute.requestId = _getId(_request);
bytes32 _disputeId = _getId(mockDispute);

// Mock and expect IOracle.disputeStatus to be called
_mockAndExpect(
address(oracle), abi.encodeCall(oracle.disputeStatus, (_disputeId)), abi.encode(IOracle.DisputeStatus(_status))
);

vm.expectRevert(abi.encodeWithSelector(IArbitratorModule.ArbitratorModule_InvalidDisputeId.selector));

vm.prank(address(oracle));
Expand All @@ -269,13 +274,13 @@ contract ArbitratorModule_Unit_ResolveDispute is BaseTest {
/**
* @notice Test that the resolve function reverts if the caller isn't the arbitrator
*/
function test_revertIfWrongSender(bytes32 _disputeId, address _caller, IOracle.Request calldata _request) public {
function test_revertIfWrongSender(address _caller) public {
vm.assume(_caller != address(oracle));

// Check: does it revert if not called by the Oracle?
vm.expectRevert(IModule.Module_OnlyOracle.selector);

vm.prank(_caller);
arbitratorModule.resolveDispute(_disputeId, _request, mockResponse, mockDispute);
arbitratorModule.resolveDispute(_getId(mockDispute), mockRequest, mockResponse, mockDispute);
}
}

0 comments on commit da99cdd

Please sign in to comment.