Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: checks createAt #37

Merged
merged 11 commits into from
Aug 6, 2024
11 changes: 4 additions & 7 deletions solidity/contracts/Module.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ 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 Validator, IModule {
/// @inheritdoc IModule
IOracle public immutable ORACLE;
import {ValidatorLib} from '../lib/ValidatorLib.sol';
import {Validator} from './Validator.sol';

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

/**
* @notice Checks that the caller is the oracle
Expand Down
20 changes: 10 additions & 10 deletions solidity/contracts/Oracle.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

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

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

import {IDisputeModule} from '../interfaces/modules/dispute/IDisputeModule.sol';
Expand All @@ -11,8 +9,9 @@ import {IFinalityModule} from '../interfaces/modules/finality/IFinalityModule.so
import {IRequestModule} from '../interfaces/modules/request/IRequestModule.sol';
import {IResolutionModule} from '../interfaces/modules/resolution/IResolutionModule.sol';
import {IResponseModule} from '../interfaces/modules/response/IResponseModule.sol';
import {ValidatorLib} from '../lib/ValidatorLib.sol';

contract Oracle is Validator, IOracle {
contract Oracle is IOracle {
/// @inheritdoc IOracle
mapping(bytes32 _requestId => uint128 _finalizedAt) public finalizedAt;

Expand Down Expand Up @@ -106,7 +105,7 @@ contract Oracle is Validator, IOracle {
Request calldata _request,
Response calldata _response
) external returns (bytes32 _responseId) {
_responseId = _validateResponse(_request, _response);
_responseId = ValidatorLib._validateResponse(_request, _response);
0xShaito marked this conversation as resolved.
Show resolved Hide resolved

// The caller must be the proposer, unless the response is coming from a dispute module
if (msg.sender != _response.proposer && msg.sender != address(_request.disputeModule)) {
Expand Down Expand Up @@ -136,7 +135,8 @@ contract Oracle is Validator, IOracle {
Response calldata _response,
Dispute calldata _dispute
) external returns (bytes32 _disputeId) {
(bytes32 _responseId, bytes32 _disputeId) = _validateResponseAndDispute(_request, _response, _dispute);
bytes32 _responseId;
(_responseId, _disputeId) = ValidatorLib._validateResponseAndDispute(_request, _response, _dispute);

if (responseCreatedAt[_responseId] == 0) {
revert Oracle_InvalidResponseBody();
Expand Down Expand Up @@ -170,7 +170,7 @@ contract Oracle is Validator, IOracle {

/// @inheritdoc IOracle
function escalateDispute(Request calldata _request, Response calldata _response, Dispute calldata _dispute) external {
(, bytes32 _disputeId) = _validateResponseAndDispute(_request, _response, _dispute);
(, bytes32 _disputeId) = ValidatorLib._validateResponseAndDispute(_request, _response, _dispute);

if (disputeCreatedAt[_disputeId] == 0) {
revert Oracle_InvalidDisputeBody();
Expand Down Expand Up @@ -200,7 +200,7 @@ contract Oracle is Validator, IOracle {

/// @inheritdoc IOracle
function resolveDispute(Request calldata _request, Response calldata _response, Dispute calldata _dispute) external {
(, bytes32 _disputeId) = _validateResponseAndDispute(_request, _response, _dispute);
(, bytes32 _disputeId) = ValidatorLib._validateResponseAndDispute(_request, _response, _dispute);

if (disputeCreatedAt[_disputeId] == 0) {
revert Oracle_InvalidDisputeBody();
Expand Down Expand Up @@ -232,7 +232,7 @@ contract Oracle is Validator, IOracle {
Dispute calldata _dispute,
DisputeStatus _status
) external {
(, bytes32 _disputeId) = _validateResponseAndDispute(_request, _response, _dispute);
(, bytes32 _disputeId) = ValidatorLib._validateResponseAndDispute(_request, _response, _dispute);

if (disputeCreatedAt[_disputeId] == 0) {
revert Oracle_InvalidDisputeBody();
Expand Down Expand Up @@ -346,7 +346,7 @@ contract Oracle is Validator, IOracle {
* @return _requestId The id of the finalized request
*/
function _finalizeWithoutResponse(IOracle.Request calldata _request) internal view returns (bytes32 _requestId) {
_requestId = _getId(_request);
_requestId = ValidatorLib._getId(_request);

if (requestCreatedAt[_requestId] == 0) {
revert Oracle_InvalidRequestBody();
Expand Down Expand Up @@ -385,7 +385,7 @@ contract Oracle is Validator, IOracle {
IOracle.Request calldata _request,
IOracle.Response calldata _response
) internal returns (bytes32 _requestId, bytes32 _responseId) {
_responseId = _validateResponse(_request, _response);
_responseId = ValidatorLib._validateResponse(_request, _response);

if (responseCreatedAt[_responseId] == 0) {
revert Oracle_InvalidResponseBody();
0xJabberwock marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
50 changes: 26 additions & 24 deletions solidity/contracts/Validator.sol
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

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

contract Validator is IValidator {
import {ValidatorLib} from '../lib/ValidatorLib.sol';

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

constructor(IOracle _oracle) {
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));
_id = ValidatorLib._getId(_request);
}

/**
Expand All @@ -22,7 +30,7 @@ contract Validator is IValidator {
* @return _id The id the response
*/
function _getId(IOracle.Response calldata _response) internal pure returns (bytes32 _id) {
_id = keccak256(abi.encode(_response));
_id = ValidatorLib._getId(_response);
}

/**
Expand All @@ -32,7 +40,7 @@ contract Validator is IValidator {
* @return _id The id the dispute
*/
function _getId(IOracle.Dispute calldata _dispute) internal pure returns (bytes32 _id) {
_id = keccak256(abi.encode(_dispute));
_id = ValidatorLib._getId(_dispute);
}

/**
Expand All @@ -45,11 +53,10 @@ contract Validator is IValidator {
function _validateResponse(
IOracle.Request calldata _request,
IOracle.Response calldata _response
) internal pure returns (bytes32 _responseId) {
bytes32 _requestId = _getId(_request);
_responseId = _getId(_response);
) internal view returns (bytes32 _responseId) {
_responseId = ValidatorLib._validateResponse(_request, _response);

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

/**
Expand All @@ -62,11 +69,10 @@ contract Validator is IValidator {
function _validateDispute(
IOracle.Request calldata _request,
IOracle.Dispute calldata _dispute
) internal pure returns (bytes32 _disputeId) {
bytes32 _requestId = _getId(_request);
_disputeId = _getId(_dispute);
) internal view returns (bytes32 _disputeId) {
_disputeId = ValidatorLib._validateDispute(_request, _dispute);

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

/**
Expand All @@ -79,11 +85,10 @@ contract Validator is IValidator {
function _validateDispute(
IOracle.Response calldata _response,
IOracle.Dispute calldata _dispute
) internal pure returns (bytes32 _disputeId) {
bytes32 _responseId = _getId(_response);
_disputeId = _getId(_dispute);
) internal view returns (bytes32 _disputeId) {
_disputeId = ValidatorLib._validateDispute(_response, _dispute);

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

/**
Expand All @@ -99,12 +104,9 @@ contract Validator is IValidator {
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);
) internal view returns (bytes32 _responseId, bytes32 _disputeId) {
(_responseId, _disputeId) = ValidatorLib._validateResponseAndDispute(_request, _response, _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();
}
}
9 changes: 0 additions & 9 deletions solidity/interfaces/IModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ interface IModule {
*/
error Module_OnlyOracle();

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

/**
* @notice The oracle contract
*/
function ORACLE() external view returns (IOracle _oracle);

/*///////////////////////////////////////////////////////////////
LOGIC
//////////////////////////////////////////////////////////////*/
Expand Down
18 changes: 8 additions & 10 deletions solidity/interfaces/IValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ 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
*/
Expand All @@ -31,4 +21,12 @@ interface IValidator {
* @notice Thrown when the dispute provided does not exist
*/
error Validator_InvalidDispute();
/*///////////////////////////////////////////////////////////////
VARIABLES
//////////////////////////////////////////////////////////////*/

/**
* @notice The oracle contract
*/
function ORACLE() external view returns (IOracle _oracle);
}
121 changes: 121 additions & 0 deletions solidity/lib/ValidatorLib.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';

library ValidatorLib {
/**
* @notice Thrown when the response provided does not match the request
*/
error ValidatorLib_InvalidResponseBody();

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

/**
* @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) public 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) public 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) public 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
) public pure returns (bytes32 _responseId) {
bytes32 _requestId = _getId(_request);
_responseId = _getId(_response);

if (_response.requestId != _requestId) revert ValidatorLib_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
) public pure returns (bytes32 _disputeId) {
bytes32 _requestId = _getId(_request);
_disputeId = _getId(_dispute);

if (_dispute.requestId != _requestId) revert ValidatorLib_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
) public pure returns (bytes32 _disputeId) {
bytes32 _responseId = _getId(_response);
_disputeId = _getId(_dispute);

if (_dispute.responseId != _responseId) revert ValidatorLib_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
) public pure returns (bytes32 _responseId, bytes32 _disputeId) {
bytes32 _requestId = _getId(_request);
_responseId = _getId(_response);
_disputeId = _getId(_dispute);

if (_response.requestId != _requestId) revert ValidatorLib_InvalidResponseBody();
if (_dispute.requestId != _requestId || _dispute.responseId != _responseId) {
revert ValidatorLib_InvalidDisputeBody();
}
}
}
2 changes: 0 additions & 2 deletions solidity/scripts/Deploy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ pragma solidity ^0.8.19;
import {Script, console} from 'forge-std/Script.sol';

import {Oracle} from '../contracts/Oracle.sol';
import {Validator} from '../contracts/Validator.sol';

// solhint-disable no-console
contract Deploy is Script {
Oracle oracle;
Validator validator;

function run() public {
address deployer = vm.rememberKey(vm.envUint('DEPLOYER_PRIVATE_KEY'));
Expand Down
Loading
Loading