Skip to content

Commit

Permalink
feat: validate response and dispute
Browse files Browse the repository at this point in the history
  • Loading branch information
0xShaito committed Aug 1, 2024
1 parent c25103e commit e49966c
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
23 changes: 23 additions & 0 deletions solidity/contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,29 @@ contract Oracle is IOracle {
emit DisputeStatusUpdated(_disputeId, _status, block.number);
}

/// @inheritdoc IOracle
function validateDispute(
Request calldata _request,
Response calldata _response,
Dispute calldata _dispute
) external view returns (bytes32 _disputeId) {
_disputeId = _validateDispute(_request, _response, _dispute);
if (disputeCreatedAt[_disputeId] == 0) {
revert Oracle_InvalidDisputeBody();
}
}

/// @inheritdoc IOracle
function validateResponse(
Request calldata _request,
Response calldata _response
) external view returns (bytes32 _responseId) {
_responseId = _validateResponse(_request, _response);
if (responseCreatedAt[_responseId] == 0) {
revert Oracle_InvalidResponseBody();
}
}

/**
* @notice Matches a bytes32 value against an encoded list of values
*
Expand Down
26 changes: 26 additions & 0 deletions solidity/interfaces/IOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,32 @@ interface IOracle {
DisputeStatus _status
) external;

/**
* @notice Validates a dispute
*
* @param _request The request
* @param _response The disputed response
* @param _dispute The dispute that is being validated
* @return _disputeId The id of the dispute
*/
function validateDispute(
Request calldata _request,
Response calldata _response,
Dispute calldata _dispute
) external view returns (bytes32 _disputeId);

/**
* @notice Validates a response
*
* @param _request The request
* @param _response The response that is being validated
* @return _responseId The id of the response
*/
function validateResponse(
Request calldata _request,
Response calldata _response
) external view returns (bytes32 _responseId);

/**
* @notice Checks if the given address is a module used in the request
*
Expand Down
98 changes: 98 additions & 0 deletions solidity/test/unit/Oracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ contract MockOracle is Oracle {
requestCreatedAt[_requestId] = _requestCreatedAt;
}

function mock_setResponseCreatedAt(bytes32 _responseId, uint128 _responseCreatedAt) external {
responseCreatedAt[_responseId] = _responseCreatedAt;
}

function mock_setDisputeCreatedAt(bytes32 _disputeId, uint128 _disputeCreatedAt) external {
disputeCreatedAt[_disputeId] = _disputeCreatedAt;
}

function mock_setTotalRequestCount(uint256 _totalRequestCount) external {
totalRequestCount = _totalRequestCount;
}
Expand Down Expand Up @@ -1133,3 +1141,93 @@ contract Oracle_Unit_EscalateDispute is BaseTest {
oracle.escalateDispute(mockRequest, mockResponse, mockDispute);
}
}

contract Oracle_Unit_ValidateResponse is BaseTest {
/**
* @notice Test if the response is validated correctly
*/
function test_validateResponse(uint128 _creation) public {
vm.assume(_creation > 0);
// Set the response creation time to 0
oracle.mock_setResponseCreatedAt(_getId(mockResponse), _creation);
// Check: no revert?
oracle.validateResponse(mockRequest, mockResponse);
}

/**
* @notice Test reverts when the response requestId does not match
*/
function test_invalidResponseBody(bytes32 _requestId) public {
vm.assume(_requestId != _getId(mockRequest));
// Change the request id for the response
mockResponse.requestId = _requestId;

vm.expectRevert(IOracle.Oracle_InvalidResponseBody.selector);
oracle.validateResponse(mockRequest, mockResponse);
}

/**
* @notice Test reverts when the response creation time is 0
*/
function test_responseNotCreated() public {
vm.expectRevert(IOracle.Oracle_InvalidResponseBody.selector);
oracle.validateResponse(mockRequest, mockResponse);
}
}

contract Oracle_Unit_ValidateDispute is BaseTest {
/**
* @notice Test if the response is validated correctly
*/
function test_validateDispute(uint128 _creation) public {
vm.assume(_creation > 0);
// Set the response creation time to 0
oracle.mock_setDisputeCreatedAt(_getId(mockDispute), _creation);
// Check: no revert?
oracle.validateDispute(mockRequest, mockResponse, mockDispute);
}

/**
* @notice Test reverts when the dispute requestId does not match
*/
function test_invalidDisputeBody_DisputeRequestId(bytes32 _requestId) public {
vm.assume(_requestId != _getId(mockRequest));
// Change the request id for the response
mockDispute.requestId = _requestId;

vm.expectRevert(IOracle.Oracle_InvalidDisputeBody.selector);
oracle.validateDispute(mockRequest, mockResponse, mockDispute);
}

/**
* @notice Test reverts when the response requestId does not match
*/
function test_invalidDisputeBody_ResponseRequestId(bytes32 _requestId) public {
vm.assume(_requestId != _getId(mockRequest));
// Change the request id for the response
mockResponse.requestId = _requestId;

vm.expectRevert(IOracle.Oracle_InvalidDisputeBody.selector);
oracle.validateDispute(mockRequest, mockResponse, mockDispute);
}

/**
* @notice Test reverts when the dispute responseId does not match
*/
function test_invalidDisputeBody_DisputeResponseId(bytes32 _responseId) public {
vm.assume(_responseId != _getId(mockResponse));
// Change the request id for the response
mockDispute.responseId = _responseId;

vm.expectRevert(IOracle.Oracle_InvalidDisputeBody.selector);
oracle.validateDispute(mockRequest, mockResponse, mockDispute);
}

/**
* @notice Test reverts when the dispute creation time is 0
*/
function test_responseNotCreated() public {
vm.expectRevert(IOracle.Oracle_InvalidDisputeBody.selector);
oracle.validateDispute(mockRequest, mockResponse, mockDispute);
}
}

0 comments on commit e49966c

Please sign in to comment.