Skip to content

Commit

Permalink
Merge branch 'dev' into perf/opo-596-root-verification-module
Browse files Browse the repository at this point in the history
  • Loading branch information
gas1cent committed Nov 11, 2023
2 parents 5921709 + 6eb1aff commit 7d5e239
Show file tree
Hide file tree
Showing 15 changed files with 1,233 additions and 1,612 deletions.
7 changes: 3 additions & 4 deletions docs/src/content/modules/dispute/circuit_resolver_module.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ The Circuit Resolver Module is a pre-dispute module that allows disputers to ver

### Key Methods

- `decodeRequestData(bytes32 _requestId)`: Returns the decoded data for a request.
- `disputeResponse(bytes32 _requestId, bytes32 _responseId, address _disputer, address _proposer)`: Verifies the ZK circuit and compares it to the proposed one. Updates the dispute status after checking if the disputed response is indeed wrong.
- `onDisputeStatusChange(bytes32 _requestId, IOracle.Dispute memory _dispute)`: Updates the status of the dispute and resolves it by proposing the correct circuit as a response and finalizing the request.
- `disputeEscalated(bytes32 _disputeId)`: This function is present to comply with the module interface but it is not implemented since this is a pre-dispute module.
- `decodeRequestData`: Returns the decoded data for a request.
- `disputeResponse`: Verifies the ZK circuit and compares it to the proposed one. Updates the dispute status after checking if the disputed response is indeed wrong.
- `onDisputeStatusChange`: Updates the status of the dispute and resolves it by proposing the correct circuit as a response and finalizing the request.

### Request Parameters

Expand Down
5 changes: 2 additions & 3 deletions docs/src/content/modules/finality/callback_module.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ The Callback Module is a finality module that allows users to call a function on

### Key Methods

- `decodeRequestData(bytes32 _requestId)`: Returns the decoded data for a request.
- `finalizeRequest(bytes32 _requestId, address)`: Executing the callback call on the target.
- `decodeRequestData`: Returns the decoded data for a request.
- `finalizeRequest`: Executing the callback call on the target.

### Request Parameters

Expand All @@ -25,4 +25,3 @@ As any finality module, the `CallbackModule` implements the `finalizeRequest` fu
## 4. Gotchas

- The success of the callback call in `finalizeRequest` is purposely not checked, specifying a function or parameters that lead to a revert will not stop the request from being finalized.
- The target must be a contract.
13 changes: 5 additions & 8 deletions docs/src/content/modules/response/bonded_response_module.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ The Bonded Response Module is a contract that allows users to propose a response

### Key Methods

- `decodeRequestData(bytes32 _requestId)`: Returns the decoded data for a request.
- `propose(bytes32 _requestId, address _proposer, bytes calldata _responseData)`: Proposes a response for a request, bonding the proposer's tokens.
- `deleteResponse(bytes32 _requestId, bytes32 _responseId, address _proposer)`: Allows a user to delete an undisputed response they proposed before the deadline, releasing the bond.
- `finalizeRequest(bytes32 _requestId, address _finalizer)`: Finalizes the request.
- `decodeRequestData`: Returns the decoded data for a request.
- `propose`: Proposes a response for a request, bonding the proposer's tokens.
- `finalizeRequest`: Finalizes the request.

### Request Parameters

Expand All @@ -24,14 +23,12 @@ The Bonded Response Module is a contract that allows users to propose a response

## 3. Key Mechanisms & Concepts

- Deleting a response: If a proposer realizes the response they've submitted is incorrect, they can delete it. Note that disputed responses cannot be taken back.

- Early finalization: It is possible for pre-dispute modules to atomically calculate the correct response on-chain, decide on the result of a dispute and finalize the request before its deadline.

- Dispute window: Prevents proposers from submitting a response 1 block before the deadline and finalizing it in the next block, leaving disputers no time to dispute the response.

## 4. Gotchas

- In case of no valid responses, a request can be finalized after the deadline and the requester will get back their tokens.
- A proposer might submit a response 1 block before the deadline and finalize it in the next block, making it impossible to dispute.
- Users cannot propose a response after the deadline for a request.
- Users cannot propose a response if an undisputed response has already been proposed.
- Users cannot delete a response after the proposing deadline.
184 changes: 93 additions & 91 deletions solidity/contracts/modules/dispute/CircuitResolverModule.sol
Original file line number Diff line number Diff line change
@@ -1,91 +1,93 @@
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.8.19;

// // solhint-disable-next-line no-unused-import
// import {Module, IModule} from '@defi-wonderland/prophet-core-contracts/solidity/contracts/Module.sol';
// import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol';

// import {ICircuitResolverModule} from '../../../interfaces/modules/dispute/ICircuitResolverModule.sol';

// contract CircuitResolverModule is Module, ICircuitResolverModule {
// constructor(IOracle _oracle) Module(_oracle) {}

// mapping(bytes32 _requestId => bytes _correctResponse) internal _correctResponses;

// /// @inheritdoc IModule
// function moduleName() external pure returns (string memory _moduleName) {
// return 'CircuitResolverModule';
// }

// /// @inheritdoc ICircuitResolverModule
// function decodeRequestData(bytes32 _requestId) public view returns (RequestParameters memory _params) {
// _params = abi.decode(requestData[_requestId], (RequestParameters));
// }

// /// @inheritdoc ICircuitResolverModule
// function disputeEscalated(bytes32 _disputeId) external onlyOracle {}

// /// @inheritdoc ICircuitResolverModule
// function onDisputeStatusChange(bytes32, /* _disputeId */ IOracle.Dispute memory _dispute) external onlyOracle {
// RequestParameters memory _params = decodeRequestData(_dispute.requestId);

// IOracle.Response memory _response = ORACLE.getResponse(_dispute.responseId);

// bytes memory _correctResponse = _correctResponses[_dispute.requestId];
// bool _won = _response.response.length != _correctResponse.length
// || keccak256(_response.response) != keccak256(_correctResponse);

// if (_won) {
// _params.accountingExtension.pay({
// _requestId: _dispute.requestId,
// _payer: _dispute.proposer,
// _receiver: _dispute.disputer,
// _token: _params.bondToken,
// _amount: _params.bondSize
// });
// bytes32 _correctResponseId =
// ORACLE.proposeResponse(_dispute.disputer, _dispute.requestId, abi.encode(_correctResponses[_dispute.requestId]));
// ORACLE.finalize(_dispute.requestId, _correctResponseId);
// } else {
// ORACLE.finalize(_dispute.requestId, _dispute.responseId);
// }

// delete _correctResponses[_dispute.requestId];

// emit DisputeStatusChanged({
// _requestId: _dispute.requestId,
// _responseId: _dispute.responseId,
// _disputer: _dispute.disputer,
// _proposer: _dispute.proposer,
// _status: _dispute.status
// });
// }

// /// @inheritdoc ICircuitResolverModule
// function disputeResponse(
// bytes32 _requestId,
// bytes32 _responseId,
// address _disputer,
// address _proposer
// ) external onlyOracle returns (IOracle.Dispute memory _dispute) {
// IOracle.Response memory _response = ORACLE.getResponse(_responseId);
// RequestParameters memory _params = decodeRequestData(_requestId);

// (, bytes memory _correctResponse) = _params.verifier.call(_params.callData);
// _correctResponses[_requestId] = _correctResponse;

// bool _won = _response.response.length != _correctResponse.length
// || keccak256(_response.response) != keccak256(_correctResponse);

// _dispute = IOracle.Dispute({
// disputer: _disputer,
// responseId: _responseId,
// proposer: _proposer,
// requestId: _requestId,
// status: _won ? IOracle.DisputeStatus.Won : IOracle.DisputeStatus.Lost,
// createdAt: block.timestamp
// });

// emit ResponseDisputed(_requestId, _responseId, _disputer, _proposer);
// }
// }
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

// solhint-disable-next-line no-unused-import
import {Module, IModule} from '@defi-wonderland/prophet-core-contracts/solidity/contracts/Module.sol';
import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol';

import {ICircuitResolverModule} from '../../../interfaces/modules/dispute/ICircuitResolverModule.sol';

contract CircuitResolverModule is Module, ICircuitResolverModule {
constructor(IOracle _oracle) Module(_oracle) {}

mapping(bytes32 _requestId => bytes _correctResponse) internal _correctResponses;

/// @inheritdoc IModule
function moduleName() external pure returns (string memory _moduleName) {
return 'CircuitResolverModule';
}

/// @inheritdoc ICircuitResolverModule
function decodeRequestData(bytes calldata _data) public pure returns (RequestParameters memory _params) {
_params = abi.decode(_data, (RequestParameters));
}

/// @inheritdoc ICircuitResolverModule
function onDisputeStatusChange(
bytes32 _disputeId,
IOracle.Request calldata _request,
IOracle.Response calldata _response,
IOracle.Dispute calldata _dispute
) external onlyOracle {
// TODO: Call `disputeStatus` to check the current status instead of reading from `_correctResponses`
RequestParameters memory _params = decodeRequestData(_request.disputeModuleData);

bytes memory _correctResponse = _correctResponses[_dispute.requestId];
bool _won = _response.response.length != _correctResponse.length
|| keccak256(_response.response) != keccak256(_correctResponse);

if (_won) {
_params.accountingExtension.pay({
_requestId: _dispute.requestId,
_payer: _dispute.proposer,
_receiver: _dispute.disputer,
_token: _params.bondToken,
_amount: _params.bondSize
});

IOracle.Response memory _newResponse =
IOracle.Response({requestId: _dispute.requestId, proposer: _dispute.disputer, response: _correctResponse});

emit DisputeStatusChanged({_disputeId: _disputeId, _dispute: _dispute, _status: IOracle.DisputeStatus.Won});

ORACLE.proposeResponse(_request, _newResponse);
ORACLE.finalize(_request, _newResponse);
} else {
emit DisputeStatusChanged({_disputeId: _disputeId, _dispute: _dispute, _status: IOracle.DisputeStatus.Lost});

ORACLE.finalize(_request, _response);
}

delete _correctResponses[_dispute.requestId];
}

/// @inheritdoc ICircuitResolverModule
function disputeResponse(
IOracle.Request calldata _request,
IOracle.Response calldata _response,
IOracle.Dispute calldata _dispute
) external onlyOracle {
RequestParameters memory _params = decodeRequestData(_request.disputeModuleData);

(bool _success, bytes memory _correctResponse) = _params.verifier.call(_params.callData);

if (!_success) revert CircuitResolverModule_VerificationFailed();

_correctResponses[_response.requestId] = _correctResponse;

IOracle.DisputeStatus _status = _response.response.length != _correctResponse.length
|| keccak256(_response.response) != keccak256(_correctResponse)
? IOracle.DisputeStatus.Won
: IOracle.DisputeStatus.Lost;

emit ResponseDisputed({
_requestId: _response.requestId,
_responseId: _dispute.responseId,
_disputeId: _getId(_dispute),
_dispute: _dispute,
_blockNumber: block.number
});

ORACLE.updateDisputeStatus(_request, _response, _dispute, _status);
}
}
64 changes: 28 additions & 36 deletions solidity/contracts/modules/finality/CallbackModule.sol
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.8.19;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

// // solhint-disable-next-line no-unused-import
// import {Module, IModule} from '@defi-wonderland/prophet-core-contracts/solidity/contracts/Module.sol';
// import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol';
// solhint-disable-next-line no-unused-import
import {Module, IModule} from '@defi-wonderland/prophet-core-contracts/solidity/contracts/Module.sol';
import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol';

// import {ICallbackModule} from '../../../interfaces/modules/finality/ICallbackModule.sol';
import {ICallbackModule} from '../../../interfaces/modules/finality/ICallbackModule.sol';

// contract CallbackModule is Module, ICallbackModule {
// constructor(IOracle _oracle) Module(_oracle) {}
contract CallbackModule is Module, ICallbackModule {
constructor(IOracle _oracle) Module(_oracle) {}

// /// @inheritdoc IModule
// function moduleName() public pure returns (string memory _moduleName) {
// _moduleName = 'CallbackModule';
// }
/// @inheritdoc IModule
function moduleName() public pure returns (string memory _moduleName) {
_moduleName = 'CallbackModule';
}

// /// @inheritdoc ICallbackModule
// function decodeRequestData(bytes32 _requestId) public view returns (RequestParameters memory _params) {
// _params = abi.decode(requestData[_requestId], (RequestParameters));
// }
/// @inheritdoc ICallbackModule
function decodeRequestData(bytes calldata _data) public pure returns (RequestParameters memory _params) {
_params = abi.decode(_data, (RequestParameters));
}

// /**
// * @notice Checks if the target address has code (i.e. is a contract)
// * @param _data The encoded data for the request
// */
// function _afterSetupRequest(bytes32, bytes calldata _data) internal view override {
// RequestParameters memory _params = abi.decode(_data, (RequestParameters));
// if (_params.target.code.length == 0) revert CallbackModule_TargetHasNoCode();
// }

// /// @inheritdoc ICallbackModule
// function finalizeRequest(
// bytes32 _requestId,
// address _finalizer
// ) external override(Module, ICallbackModule) onlyOracle {
// RequestParameters memory _params = decodeRequestData(_requestId);
// _params.target.call(_params.data);
// emit Callback(_requestId, _params.target, _params.data);
// emit RequestFinalized(_requestId, _finalizer);
// }
// }
/// @inheritdoc ICallbackModule
function finalizeRequest(
IOracle.Request calldata _request,
IOracle.Response calldata _response,
address _finalizer
) external override(Module, ICallbackModule) onlyOracle {
RequestParameters memory _params = decodeRequestData(_request.finalityModuleData);
_params.target.call(_params.data);
emit Callback(_response.requestId, _params.target, _params.data);
emit RequestFinalized(_response.requestId, _response, _finalizer);
}
}
Loading

0 comments on commit 7d5e239

Please sign in to comment.