Skip to content

Commit

Permalink
fix: comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ashitakah committed Aug 6, 2024
1 parent 97b844e commit eaec168
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 15 deletions.
14 changes: 7 additions & 7 deletions solidity/contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ contract Oracle is IOracle {
(_requestId, _responseId) = ValidatorLib._validateRequestAndResponse(_request, _response);

if (requestCreatedAt[_requestId] == 0) {
revert Oracle_InvalidRequestBody();
revert Oracle_InvalidRequest();
}

// The caller must be the proposer, unless the response is coming from a dispute module
Expand Down Expand Up @@ -144,7 +144,7 @@ contract Oracle is IOracle {
(_responseId, _disputeId) = ValidatorLib._validateResponseAndDispute(_request, _response, _dispute);

if (responseCreatedAt[_responseId] == 0) {
revert Oracle_InvalidResponseBody();
revert Oracle_InvalidResponse();
}

if (_dispute.proposer != _response.proposer) {
Expand Down Expand Up @@ -178,7 +178,7 @@ contract Oracle is IOracle {
(, bytes32 _disputeId) = ValidatorLib._validateResponseAndDispute(_request, _response, _dispute);

if (disputeCreatedAt[_disputeId] == 0) {
revert Oracle_InvalidDisputeBody();
revert Oracle_InvalidDispute();
}

if (disputeOf[_dispute.responseId] != _disputeId) {
Expand Down Expand Up @@ -208,7 +208,7 @@ contract Oracle is IOracle {
(, bytes32 _disputeId) = ValidatorLib._validateResponseAndDispute(_request, _response, _dispute);

if (disputeCreatedAt[_disputeId] == 0) {
revert Oracle_InvalidDisputeBody();
revert Oracle_InvalidDispute();
}

if (disputeOf[_dispute.responseId] != _disputeId) {
Expand Down Expand Up @@ -240,7 +240,7 @@ contract Oracle is IOracle {
(, bytes32 _disputeId) = ValidatorLib._validateResponseAndDispute(_request, _response, _dispute);

if (disputeCreatedAt[_disputeId] == 0) {
revert Oracle_InvalidDisputeBody();
revert Oracle_InvalidDispute();
}

if (disputeOf[_dispute.responseId] != _disputeId) {
Expand Down Expand Up @@ -354,7 +354,7 @@ contract Oracle is IOracle {
_requestId = ValidatorLib._getId(_request);

if (requestCreatedAt[_requestId] == 0) {
revert Oracle_InvalidRequestBody();
revert Oracle_InvalidRequest();
}

bytes32[] memory _responses = getResponseIds(_requestId);
Expand Down Expand Up @@ -393,7 +393,7 @@ contract Oracle is IOracle {
_responseId = ValidatorLib._validateResponse(_request, _response);

if (responseCreatedAt[_responseId] == 0) {
revert Oracle_InvalidResponseBody();
revert Oracle_InvalidResponse();
}

_requestId = _response.requestId;
Expand Down
15 changes: 15 additions & 0 deletions solidity/interfaces/IOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,21 @@ interface IOracle {
*/
error Oracle_InvalidDisputeBody();

/**
* @notice Thrown when the request provided does not exist
*/
error Oracle_InvalidRequest();

/**
* @notice Thrown when the response provided does not exist
*/
error Oracle_InvalidResponse();

/**
* @notice Thrown when the dispute provided does not exist
*/
error Oracle_InvalidDispute();

/*///////////////////////////////////////////////////////////////
ENUMS
//////////////////////////////////////////////////////////////*/
Expand Down
16 changes: 16 additions & 0 deletions solidity/test/mocks/contracts/MockRequestModule.sol
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import {Module} from '../../../contracts/Module.sol';
import {IOracle} from '../../../interfaces/IOracle.sol';
import {IMockRequestModule} from '../interfaces/IMockRequestModule.sol';

contract MockRequestModule is Module, IMockRequestModule {
constructor(IOracle _oracle) Module(_oracle) {}

function decodeRequestData(bytes calldata _data) public pure returns (RequestParameters memory _requestData) {
_requestData = abi.decode(_data, (RequestParameters));
}

function createRequest(bytes32 _requestId, bytes calldata _data, address _requester) external onlyOracle {}
function moduleName() external view returns (string memory _moduleName) {}
}
16 changes: 8 additions & 8 deletions solidity/test/unit/Oracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ contract Oracle_Unit_ProposeResponse is BaseTest {

function test_proposeResponse_revertsIfInvalidRequest() public {
// Check: revert?
vm.expectRevert(IOracle.Oracle_InvalidRequestBody.selector);
vm.expectRevert(IOracle.Oracle_InvalidRequest.selector);

// Test: try to propose a response with an invalid request
vm.prank(proposer);
Expand Down Expand Up @@ -584,11 +584,11 @@ contract Oracle_Unit_DisputeResponse is BaseTest {
/**
* @notice Reverts if the response doesn't exist
*/
function test_disputeResponse_revertIfInvalidReponse() public {
function test_disputeResponse_revertIfInvalidResponse() public {
oracle.mock_setResponseCreatedAt(_getId(mockResponse), 0);

// Check: revert?
vm.expectRevert(IOracle.Oracle_InvalidResponseBody.selector);
vm.expectRevert(IOracle.Oracle_InvalidResponse.selector);

// Test: try to dispute the response
vm.prank(disputer);
Expand Down Expand Up @@ -716,7 +716,7 @@ contract Oracle_Unit_UpdateDisputeStatus is BaseTest {
oracle.mock_setDisputeCreatedAt(_disputeId, 0);

// Check: revert?
vm.expectRevert(abi.encodeWithSelector(IOracle.Oracle_InvalidDisputeBody.selector));
vm.expectRevert(abi.encodeWithSelector(IOracle.Oracle_InvalidDispute.selector));

// Test: try to update the status
vm.prank(address(resolutionModule));
Expand Down Expand Up @@ -773,7 +773,7 @@ contract Oracle_Unit_ResolveDispute is BaseTest {
oracle.mock_setDisputeCreatedAt(_getId(mockDispute), 0);

// Check: revert?
vm.expectRevert(abi.encodeWithSelector(IOracle.Oracle_InvalidDisputeBody.selector));
vm.expectRevert(abi.encodeWithSelector(IOracle.Oracle_InvalidDispute.selector));

// Test: try to resolve the dispute
oracle.resolveDispute(mockRequest, mockResponse, mockDispute);
Expand Down Expand Up @@ -940,7 +940,7 @@ contract Oracle_Unit_Finalize is BaseTest {
*/
function test_finalize_revertsIfInvalidRequest() public {
oracle.mock_setRequestCreatedAt(_getId(mockRequest), 0);
vm.expectRevert(IOracle.Oracle_InvalidResponseBody.selector);
vm.expectRevert(IOracle.Oracle_InvalidResponse.selector);

vm.prank(requester);
oracle.finalize(mockRequest, mockResponse);
Expand All @@ -955,7 +955,7 @@ contract Oracle_Unit_Finalize is BaseTest {
oracle.mock_setResponseCreatedAt(_getId(mockResponse), 0);

// Check: revert?
vm.expectRevert(IOracle.Oracle_InvalidResponseBody.selector);
vm.expectRevert(IOracle.Oracle_InvalidResponse.selector);

// Test: finalize the request
vm.prank(requester);
Expand Down Expand Up @@ -1246,7 +1246,7 @@ contract Oracle_Unit_EscalateDispute is BaseTest {
oracle.mock_setRequestCreatedAt(_getId(mockRequest), uint128(block.number));
oracle.mock_setResponseCreatedAt(_getId(mockResponse), uint128(block.number));
oracle.mock_setDisputeCreatedAt(_disputeId, 0);
vm.expectRevert(abi.encodeWithSelector(IOracle.Oracle_InvalidDisputeBody.selector));
vm.expectRevert(abi.encodeWithSelector(IOracle.Oracle_InvalidDispute.selector));

oracle.escalateDispute(mockRequest, mockResponse, mockDispute);
}
Expand Down
108 changes: 108 additions & 0 deletions solidity/test/unit/Validator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {IResponseModule} from '../../interfaces/modules/response/IResponseModule

import {IValidator, Validator} from '../../contracts/Validator.sol';

import {ValidatorLib} from '../../lib/ValidatorLib.sol';

import {Helpers} from '../utils/Helpers.sol';

/**
Expand All @@ -22,6 +24,18 @@ import {Helpers} from '../utils/Helpers.sol';
contract MockValidator is Validator {
constructor(IOracle _oracle) Validator(_oracle) {}

function getId(IOracle.Request calldata _request) external pure returns (bytes32 _requestId) {
return _getId(_request);
}

function getId(IOracle.Response calldata _response) external pure returns (bytes32 _responseId) {
return _getId(_response);
}

function getId(IOracle.Dispute calldata _dispute) external pure returns (bytes32 _disputeId) {
return _getId(_dispute);
}

function validateRequestAndResponse(
IOracle.Request calldata _request,
IOracle.Response calldata _response
Expand Down Expand Up @@ -91,7 +105,37 @@ contract BaseTest is Test, Helpers {
}
}

contract ValidatorGetIds is BaseTest {
function test_getId_request() public {
bytes32 _requestId = validator.getId(mockRequest);
assertEq(_requestId, keccak256(abi.encode(mockRequest)));
}

function test_getId_response() public {
bytes32 _responseId = validator.getId(mockResponse);
assertEq(_responseId, keccak256(abi.encode(mockResponse)));
}

function test_getId_dispute() public {
bytes32 _disputeId = validator.getId(mockDispute);
assertEq(_disputeId, keccak256(abi.encode(mockDispute)));
}
}

contract ValidatorValidateRequestAndResponse is BaseTest {
function test_validateRequestAndResponse() public {
(bytes32 _requestId, bytes32 _responseId) = validator.validateRequestAndResponse(mockRequest, mockResponse);
assertEq(_requestId, keccak256(abi.encode(mockRequest)));
assertEq(_responseId, keccak256(abi.encode(mockResponse)));
}

function test_validateRequestAndResponse_InvalidResponseBody() public {
IOracle.Response memory response = mockResponse;

Check warning on line 133 in solidity/test/unit/Validator.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

'response' should start with _
response.requestId = bytes32('invalid');
vm.expectRevert(ValidatorLib.ValidatorLib_InvalidResponseBody.selector);
validator.validateResponse(mockRequest, response);
}

function test_validateRequestAndResponse_InvalidResponse() public {
vm.mockCall(
address(oracle), abi.encodeWithSelector(IOracle.responseCreatedAt.selector, _getId(mockResponse)), abi.encode(0)
Expand All @@ -102,6 +146,18 @@ contract ValidatorValidateRequestAndResponse is BaseTest {
}

contract ValidatorValidateResponse is BaseTest {
function test_validateResponse() public {
bytes32 _responseId = validator.validateResponse(mockRequest, mockResponse);
assertEq(_responseId, keccak256(abi.encode(mockResponse)));
}

function test_validateResponse_InvalidResponseBody() public {
IOracle.Response memory response = mockResponse;

Check warning on line 155 in solidity/test/unit/Validator.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

'response' should start with _
response.requestId = bytes32('invalid');
vm.expectRevert(ValidatorLib.ValidatorLib_InvalidResponseBody.selector);
validator.validateResponse(mockRequest, response);
}

function test_validateResponse_InvalidResponse() public {
vm.mockCall(
address(oracle), abi.encodeWithSelector(IOracle.responseCreatedAt.selector, _getId(mockResponse)), abi.encode(0)
Expand All @@ -112,6 +168,18 @@ contract ValidatorValidateResponse is BaseTest {
}

contract ValidatorValidateDisputeRequest is BaseTest {
function test_validateDispute() public {
bytes32 _disputeId = validator.validateDispute(mockRequest, mockDispute);
assertEq(_disputeId, keccak256(abi.encode(mockDispute)));
}

function test_validateDispute_InvalidDisputeBody() public {
IOracle.Dispute memory dispute = mockDispute;

Check warning on line 177 in solidity/test/unit/Validator.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

'dispute' should start with _
dispute.requestId = bytes32('invalid');
vm.expectRevert(ValidatorLib.ValidatorLib_InvalidDisputeBody.selector);
validator.validateDispute(mockRequest, dispute);
}

function test_validateDispute_InvalidDispute() public {
vm.mockCall(
address(oracle), abi.encodeWithSelector(IOracle.disputeCreatedAt.selector, _getId(mockDispute)), abi.encode(0)
Expand All @@ -122,6 +190,18 @@ contract ValidatorValidateDisputeRequest is BaseTest {
}

contract ValidatorValidateDisputeResponse is BaseTest {
function test_validateDispute() public {
bytes32 _disputeId = validator.validateDispute(mockResponse, mockDispute);
assertEq(_disputeId, keccak256(abi.encode(mockDispute)));
}

function test_validateDispute_InvalidDisputeBody() public {
IOracle.Dispute memory dispute = mockDispute;

Check warning on line 199 in solidity/test/unit/Validator.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

'dispute' should start with _
dispute.responseId = bytes32('invalid');
vm.expectRevert(ValidatorLib.ValidatorLib_InvalidDisputeBody.selector);
validator.validateDispute(mockResponse, dispute);
}

function test_validateDispute_InvalidDispute() public {
vm.mockCall(
address(oracle), abi.encodeWithSelector(IOracle.disputeCreatedAt.selector, _getId(mockDispute)), abi.encode(0)
Expand All @@ -132,6 +212,34 @@ contract ValidatorValidateDisputeResponse is BaseTest {
}

contract Validator_ValidateResponseAndDispute is BaseTest {
function test_validateResponseAndDispute() public {
(bytes32 responseId, bytes32 disputeId) =

Check warning on line 216 in solidity/test/unit/Validator.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

'responseId' should start with _

Check warning on line 216 in solidity/test/unit/Validator.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

'disputeId' should start with _
validator.validateResponseAndDispute(mockRequest, mockResponse, mockDispute);
assertEq(responseId, keccak256(abi.encode(mockResponse)));
assertEq(disputeId, keccak256(abi.encode(mockDispute)));
}

function test_validateResponseAndDispute_InvalidResponseBody() public {
IOracle.Response memory response = mockResponse;

Check warning on line 223 in solidity/test/unit/Validator.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

'response' should start with _
response.requestId = bytes32('invalid');
vm.expectRevert(ValidatorLib.ValidatorLib_InvalidResponseBody.selector);
validator.validateResponseAndDispute(mockRequest, response, mockDispute);
}

function test_validateResponseAndDispute_InvalidDisputeBody() public {
IOracle.Dispute memory dispute = mockDispute;

Check warning on line 230 in solidity/test/unit/Validator.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

'dispute' should start with _
dispute.requestId = bytes32('invalid');
vm.expectRevert(ValidatorLib.ValidatorLib_InvalidDisputeBody.selector);
validator.validateResponseAndDispute(mockRequest, mockResponse, dispute);
}

function test_validateResponseAndDispute_InvalidDisputeBodyResponseId() public {
IOracle.Dispute memory dispute = mockDispute;

Check warning on line 237 in solidity/test/unit/Validator.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

'dispute' should start with _
dispute.responseId = bytes32('invalid');
vm.expectRevert(ValidatorLib.ValidatorLib_InvalidDisputeBody.selector);
validator.validateResponseAndDispute(mockRequest, mockResponse, dispute);
}

function test_validateResponseAndDispute_InvalidDispute() public {
vm.mockCall(
address(oracle), abi.encodeWithSelector(IOracle.disputeCreatedAt.selector, _getId(mockDispute)), abi.encode(0)
Expand Down

0 comments on commit eaec168

Please sign in to comment.