Skip to content

Commit

Permalink
feat: validator (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xShaito authored Aug 2, 2024
1 parent c25103e commit d01bc1a
Show file tree
Hide file tree
Showing 5 changed files with 396 additions and 131 deletions.
113 changes: 3 additions & 110 deletions solidity/contracts/Module.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ pragma solidity ^0.8.19;

import {IModule} from '../interfaces/IModule.sol';
import {IOracle} from '../interfaces/IOracle.sol';
import {Validator} from './Validator.sol';

abstract contract Module is IModule {
/// @inheritdoc IModule
IOracle public immutable ORACLE;

constructor(IOracle _oracle) payable {
ORACLE = _oracle;
}
abstract contract Module is Validator, IModule {
constructor(IOracle _oracle) payable Validator(_oracle) {}

/**
* @notice Checks that the caller is the oracle
Expand All @@ -29,107 +25,4 @@ abstract contract Module is IModule {

/// @inheritdoc IModule
function validateParameters(bytes calldata _encodedParameters) external view virtual returns (bool _valid) {}

/**
* @notice Computes the id a given request
*
* @param _request The request to compute the id for
* @return _id The id the request
*/
function _getId(IOracle.Request calldata _request) internal pure returns (bytes32 _id) {
_id = keccak256(abi.encode(_request));
}

/**
* @notice Computes the id a given response
*
* @param _response The response to compute the id for
* @return _id The id the response
*/
function _getId(IOracle.Response calldata _response) internal pure returns (bytes32 _id) {
_id = keccak256(abi.encode(_response));
}

/**
* @notice Computes the id a given dispute
*
* @param _dispute The dispute to compute the id for
* @return _id The id the dispute
*/
function _getId(IOracle.Dispute calldata _dispute) internal pure returns (bytes32 _id) {
_id = keccak256(abi.encode(_dispute));
}

/**
* @notice Validates the correctness of a request-response pair
*
* @param _request The request to compute the id for
* @param _response The response to compute the id for
* @return _responseId The id the response
*/
function _validateResponse(
IOracle.Request calldata _request,
IOracle.Response calldata _response
) internal pure returns (bytes32 _responseId) {
bytes32 _requestId = _getId(_request);
_responseId = _getId(_response);

if (_response.requestId != _requestId) revert Module_InvalidResponseBody();
}

/**
* @notice Validates the correctness of a request-dispute pair
*
* @param _request The request to compute the id for
* @param _dispute The dispute to compute the id for
* @return _disputeId The id the dispute
*/
function _validateDispute(
IOracle.Request calldata _request,
IOracle.Dispute calldata _dispute
) internal pure returns (bytes32 _disputeId) {
bytes32 _requestId = _getId(_request);
_disputeId = _getId(_dispute);

if (_dispute.requestId != _requestId) revert Module_InvalidDisputeBody();
}

/**
* @notice Validates the correctness of a response-dispute pair
*
* @param _response The response to compute the id for
* @param _dispute The dispute to compute the id for
* @return _disputeId The id the dispute
*/
function _validateDispute(
IOracle.Response calldata _response,
IOracle.Dispute calldata _dispute
) internal pure returns (bytes32 _disputeId) {
bytes32 _responseId = _getId(_response);
_disputeId = _getId(_dispute);

if (_dispute.responseId != _responseId) revert Module_InvalidDisputeBody();
}

/**
* @notice Validates the correctness of a request-response-dispute triplet
*
* @param _request The request to compute the id for
* @param _response The response to compute the id for
* @param _dispute The dispute to compute the id for
* @return _responseId The id the response
* @return _disputeId The id the dispute
*/
function _validateResponseAndDispute(
IOracle.Request calldata _request,
IOracle.Response calldata _response,
IOracle.Dispute calldata _dispute
) internal pure returns (bytes32 _responseId, bytes32 _disputeId) {
bytes32 _requestId = _getId(_request);
_responseId = _getId(_response);
_disputeId = _getId(_dispute);

if (_response.requestId != _requestId) revert Module_InvalidResponseBody();
if (_dispute.requestId != _requestId || _dispute.responseId != _responseId) revert Module_InvalidDisputeBody();
}
}
121 changes: 121 additions & 0 deletions solidity/contracts/Validator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import {IOracle} from '../interfaces/IOracle.sol';
import {IValidator} from '../interfaces/IValidator.sol';

contract Validator is IValidator {
/// @inheritdoc IValidator
IOracle public immutable ORACLE;

constructor(IOracle _oracle) payable {
ORACLE = _oracle;
}

/**
* @notice Computes the id a given request
*
* @param _request The request to compute the id for
* @return _id The id the request
*/
function _getId(IOracle.Request calldata _request) internal pure returns (bytes32 _id) {
_id = keccak256(abi.encode(_request));
}

/**
* @notice Computes the id a given response
*
* @param _response The response to compute the id for
* @return _id The id the response
*/
function _getId(IOracle.Response calldata _response) internal pure returns (bytes32 _id) {
_id = keccak256(abi.encode(_response));
}

/**
* @notice Computes the id a given dispute
*
* @param _dispute The dispute to compute the id for
* @return _id The id the dispute
*/
function _getId(IOracle.Dispute calldata _dispute) internal pure returns (bytes32 _id) {
_id = keccak256(abi.encode(_dispute));
}

/**
* @notice Validates the correctness and existance of a request-response pair
*
* @param _request The request to compute the id for
* @param _response The response to compute the id for
* @return _responseId The id the response
*/
function _validateResponse(
IOracle.Request calldata _request,
IOracle.Response calldata _response
) internal view returns (bytes32 _responseId) {
bytes32 _requestId = _getId(_request);
_responseId = _getId(_response);

if (_response.requestId != _requestId) revert Validator_InvalidResponseBody();
if (ORACLE.responseCreatedAt(_responseId) == 0) revert Validator_InvalidResponse();
}

/**
* @notice Validates the correctness of a request-dispute pair
*
* @param _request The request to compute the id for
* @param _dispute The dispute to compute the id for
* @return _disputeId The id the dispute
*/
function _validateDispute(
IOracle.Request calldata _request,
IOracle.Dispute calldata _dispute
) internal view returns (bytes32 _disputeId) {
bytes32 _requestId = _getId(_request);
_disputeId = _getId(_dispute);

if (_dispute.requestId != _requestId) revert Validator_InvalidDisputeBody();
if (ORACLE.disputeCreatedAt(_disputeId) == 0) revert Validator_InvalidDispute();
}

/**
* @notice Validates the correctness of a response-dispute pair
*
* @param _response The response to compute the id for
* @param _dispute The dispute to compute the id for
* @return _disputeId The id the dispute
*/
function _validateDispute(
IOracle.Response calldata _response,
IOracle.Dispute calldata _dispute
) internal view returns (bytes32 _disputeId) {
bytes32 _responseId = _getId(_response);
_disputeId = _getId(_dispute);

if (_dispute.responseId != _responseId) revert Validator_InvalidDisputeBody();
if (ORACLE.disputeCreatedAt(_disputeId) == 0) revert Validator_InvalidDispute();
}

/**
* @notice Validates the correctness of a request-response-dispute triplet
*
* @param _request The request to compute the id for
* @param _response The response to compute the id for
* @param _dispute The dispute to compute the id for
* @return _responseId The id the response
* @return _disputeId The id the dispute
*/
function _validateResponseAndDispute(
IOracle.Request calldata _request,
IOracle.Response calldata _response,
IOracle.Dispute calldata _dispute
) internal view returns (bytes32 _responseId, bytes32 _disputeId) {
bytes32 _requestId = _getId(_request);
_responseId = _getId(_response);
_disputeId = _getId(_dispute);

if (_response.requestId != _requestId) revert Validator_InvalidResponseBody();
if (_dispute.requestId != _requestId || _dispute.responseId != _responseId) revert Validator_InvalidDisputeBody();
if (ORACLE.disputeCreatedAt(_disputeId) == 0) revert Validator_InvalidDispute();
}
}
21 changes: 0 additions & 21 deletions solidity/interfaces/IModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,6 @@ interface IModule {
*/
error Module_OnlyOracle();

/**
* @notice Thrown when the response provided does not match the request
*/
error Module_InvalidResponseBody();

/**
* @notice Thrown when the dispute provided does not match the request or response
*/
error Module_InvalidDisputeBody();

/*///////////////////////////////////////////////////////////////
VARIABLES
//////////////////////////////////////////////////////////////*/

/**
* @notice Returns the address of the oracle
*
* @return _oracle The address of the oracle
*/
function ORACLE() external view returns (IOracle _oracle);

/*///////////////////////////////////////////////////////////////
LOGIC
//////////////////////////////////////////////////////////////*/
Expand Down
45 changes: 45 additions & 0 deletions solidity/interfaces/IValidator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import {IOracle} from './IOracle.sol';

/**
* @title Validator
* @notice Contract to validate requests, responses, and disputes
*/
interface IValidator {
/*///////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/

/**
* @notice Thrown when the response provided does not match the request
*/
error Validator_InvalidResponseBody();

/**
* @notice Thrown when the dispute provided does not match the request or response
*/
error Validator_InvalidDisputeBody();

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

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

/*///////////////////////////////////////////////////////////////
VARIABLES
//////////////////////////////////////////////////////////////*/

/**
* @notice Returns the address of the oracle
*
* @return _oracle The address of the oracle
*/
function ORACLE() external view returns (IOracle _oracle);
}
Loading

0 comments on commit d01bc1a

Please sign in to comment.