Skip to content

Commit

Permalink
Merge pull request #11 from defi-wonderland/perf/opo-602-arbitrator-m…
Browse files Browse the repository at this point in the history
…odule

perf: optimize `ArbitratorModule`
  • Loading branch information
0xmoebius authored Nov 12, 2023
2 parents adc063f + 2ff94bd commit 9eda7b0
Show file tree
Hide file tree
Showing 4 changed files with 455 additions and 466 deletions.
10 changes: 5 additions & 5 deletions docs/src/content/modules/resolution/arbitrator_module.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ The Arbitrator Module is a part of the dispute resolution system. It allows an e

### Key Methods

- `getStatus(bytes32 _disputeId)`: Returns the arbitration status of a dispute.
- `isValid(bytes32 _disputeId)`: Indicates whether the dispute has been arbitrated.
- `startResolution(bytes32 _disputeId)`: Starts the arbitration process by calling `resolve` on the arbitrator and flags the dispute as `Active`.
- `resolveDispute(bytes32 _disputeId)`: Resolves the dispute by getting the answer from the arbitrator and notifying the oracle.
- `decodeRequestData(bytes32 _requestId)`: Returns the decoded data for a request.
- `getStatus`: Returns the arbitration status of a dispute.
- `isValid`: Indicates whether the dispute has been arbitrated.
- `startResolution`: Starts the arbitration process by calling `resolve` on the arbitrator and flags the dispute as `Active`.
- `resolveDispute`: Resolves the dispute by getting the answer from the arbitrator and notifying the oracle.
- `decodeRequestData`: Returns the decoded data for a request.

### Request Parameters

Expand Down
131 changes: 69 additions & 62 deletions solidity/contracts/modules/resolution/ArbitratorModule.sol
Original file line number Diff line number Diff line change
@@ -1,62 +1,69 @@
// // 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 {IArbitratorModule} from '../../../interfaces/modules/resolution/IArbitratorModule.sol';
// import {IArbitrator} from '../../../interfaces/IArbitrator.sol';

// contract ArbitratorModule is Module, IArbitratorModule {
// /**
// * @notice The status of all disputes
// */
// mapping(bytes32 _disputeId => ArbitrationStatus _status) internal _disputeData;

// constructor(IOracle _oracle) Module(_oracle) {}

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

// /// @inheritdoc IArbitratorModule
// function decodeRequestData(bytes32 _requestId) public view returns (address _arbitrator) {
// _arbitrator = abi.decode(requestData[_requestId], (address));
// }

// /// @inheritdoc IArbitratorModule
// function getStatus(bytes32 _disputeId) external view returns (ArbitrationStatus _disputeStatus) {
// _disputeStatus = _disputeData[_disputeId];
// }

// /// @inheritdoc IArbitratorModule
// function startResolution(bytes32 _disputeId) external onlyOracle {
// IOracle.Dispute memory _dispute = ORACLE.getDispute(_disputeId);

// address _arbitrator = abi.decode(requestData[_dispute.requestId], (address));
// if (_arbitrator == address(0)) revert ArbitratorModule_InvalidArbitrator();

// _disputeData[_disputeId] = ArbitrationStatus.Active;
// IArbitrator(_arbitrator).resolve(_disputeId);

// emit ResolutionStarted(_dispute.requestId, _disputeId);
// }

// /// @inheritdoc IArbitratorModule
// function resolveDispute(bytes32 _disputeId) external onlyOracle {
// IOracle.Dispute memory _dispute = ORACLE.getDispute(_disputeId);
// if (_dispute.status != IOracle.DisputeStatus.Escalated) revert ArbitratorModule_InvalidDisputeId();

// address _arbitrator = abi.decode(requestData[_dispute.requestId], (address));
// IOracle.DisputeStatus _status = IArbitrator(_arbitrator).getAnswer(_disputeId);

// if (_status <= IOracle.DisputeStatus.Escalated) revert ArbitratorModule_InvalidResolutionStatus();
// _disputeData[_disputeId] = ArbitrationStatus.Resolved;

// ORACLE.updateDisputeStatus(_disputeId, _status);

// emit DisputeResolved(_dispute.requestId, _disputeId, _status);
// }
// }
// 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 {IArbitratorModule} from '../../../interfaces/modules/resolution/IArbitratorModule.sol';
import {IArbitrator} from '../../../interfaces/IArbitrator.sol';

contract ArbitratorModule is Module, IArbitratorModule {
/**
* @notice The status of all disputes
*/
mapping(bytes32 _disputeId => ArbitrationStatus _status) internal _disputeData;

constructor(IOracle _oracle) Module(_oracle) {}

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

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

/// @inheritdoc IArbitratorModule
function getStatus(bytes32 _disputeId) external view returns (ArbitrationStatus _disputeStatus) {
_disputeStatus = _disputeData[_disputeId];
}

/// @inheritdoc IArbitratorModule
function startResolution(
bytes32 _disputeId,
IOracle.Request calldata _request,
IOracle.Response calldata _response,

Check warning on line 38 in solidity/contracts/modules/resolution/ArbitratorModule.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

Variable "_response" is unused

Check warning on line 38 in solidity/contracts/modules/resolution/ArbitratorModule.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

Variable "_response" is unused
IOracle.Dispute calldata _dispute
) external onlyOracle {
RequestParameters memory _params = decodeRequestData(_request.resolutionModuleData);
if (_params.arbitrator == address(0)) revert ArbitratorModule_InvalidArbitrator();

_disputeData[_disputeId] = ArbitrationStatus.Active;
IArbitrator(_params.arbitrator).resolve(_disputeId);

emit ResolutionStarted(_dispute.requestId, _disputeId);
}

/// @inheritdoc IArbitratorModule
function resolveDispute(
bytes32 _disputeId,
IOracle.Request calldata _request,
IOracle.Response calldata _response,
IOracle.Dispute calldata _dispute
) external onlyOracle {
if (ORACLE.disputeStatus(_disputeId) != IOracle.DisputeStatus.Escalated) revert ArbitratorModule_InvalidDisputeId();

RequestParameters memory _params = decodeRequestData(_request.resolutionModuleData);
IOracle.DisputeStatus _status = IArbitrator(_params.arbitrator).getAnswer(_disputeId);

if (_status <= IOracle.DisputeStatus.Escalated) revert ArbitratorModule_InvalidResolutionStatus();
_disputeData[_disputeId] = ArbitrationStatus.Resolved;

ORACLE.updateDisputeStatus(_request, _response, _dispute, _status);

emit DisputeResolved(_dispute.requestId, _disputeId, _status);
}
}
166 changes: 95 additions & 71 deletions solidity/interfaces/modules/resolution/IArbitratorModule.sol
Original file line number Diff line number Diff line change
@@ -1,84 +1,108 @@
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.8.19;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

// import {IResolutionModule} from
// '@defi-wonderland/prophet-core-contracts/solidity/interfaces/modules/resolution/IResolutionModule.sol';
import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol';
import {IResolutionModule} from
'@defi-wonderland/prophet-core-contracts/solidity/interfaces/modules/resolution/IResolutionModule.sol';

// /*
// * @title ArbitratorModule
// * @notice Module allowing an external arbitrator contract
// * to resolve a dispute.
// */
// interface IArbitratorModule is IResolutionModule {
// /*///////////////////////////////////////////////////////////////
// ERRORS
// //////////////////////////////////////////////////////////////*/
/*
* @title ArbitratorModule
* @notice Module allowing an external arbitrator contract to resolve a dispute.
*/
interface IArbitratorModule is IResolutionModule {

Check warning on line 12 in solidity/interfaces/modules/resolution/IArbitratorModule.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

The order of members in the interface IArbitratorModule interfaces should be: Events, Errors, Enums, Structs, Functions

Check warning on line 12 in solidity/interfaces/modules/resolution/IArbitratorModule.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

The order of members in the interface IArbitratorModule interfaces should be: Events, Errors, Enums, Structs, Functions
/*///////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/

// /**
// * @notice Thrown when an unauthorized caller calls a function only the arbitrator can call
// */
// error ArbitratorModule_OnlyArbitrator();
/**
* @notice Thrown when trying to resolve a dispute that is not escalated
*/
error ArbitratorModule_InvalidDisputeId();

// /**
// * @notice Thrown when trying to resolve a dispute that is not escalated
// */
// error ArbitratorModule_InvalidDisputeId();
/**
* @notice Thrown when the arbitrator address is the address zero
*/
error ArbitratorModule_InvalidArbitrator();

// /**
// * @notice Thrown when the arbitrator address is the address zero
// */
// error ArbitratorModule_InvalidArbitrator();
/**
* @notice Thrown when the arbitrator returns an invalid resolution status
*/
error ArbitratorModule_InvalidResolutionStatus();

// /**
// * @notice Thrown when the arbitrator returns an invalid resolution status
// */
// error ArbitratorModule_InvalidResolutionStatus();
/*///////////////////////////////////////////////////////////////
STRUCTS
//////////////////////////////////////////////////////////////*/
/**
* @notice Parameters of the request as stored in the module
* @param arbitrator The address of the arbitrator
*/
struct RequestParameters {
address arbitrator;
}

// /*///////////////////////////////////////////////////////////////
// ENUMS
// //////////////////////////////////////////////////////////////*/
/*///////////////////////////////////////////////////////////////
ENUMS
//////////////////////////////////////////////////////////////*/

// /**
// * @notice Available status of the arbitration process
// */
// enum ArbitrationStatus {
// Unknown, // The arbitration process has not started (default)
// Active, // The arbitration process is active
// Resolved // The arbitration process is resolved
// }
/**
* @notice Available status of the arbitration process
*/
enum ArbitrationStatus {
Unknown, // The arbitration process has not started (default)
Active, // The arbitration process is active
Resolved // The arbitration process is resolved
}

// /*///////////////////////////////////////////////////////////////
// LOGIC
// //////////////////////////////////////////////////////////////*/
/*///////////////////////////////////////////////////////////////
LOGIC
//////////////////////////////////////////////////////////////*/

// /**
// * @notice Returns the current arbitration status of a dispute
// * @param _disputeId The ID of the dispute
// * @return _disputeStatus The `ArbitrationStatus` of the dispute
// */
// function getStatus(bytes32 _disputeId) external view returns (ArbitrationStatus _disputeStatus);
/**
* @notice Returns the decoded data for a request
*
* @param _data The encoded request parameters
* @return _params The struct containing the parameters for the request
*/
function decodeRequestData(bytes calldata _data) external view returns (RequestParameters memory _params);

// /**
// * @notice Starts the arbitration process by calling `resolve` on the
// * arbitrator and flags the dispute as Active
// * @dev Only callable by the Oracle
// * @dev Will revert if the arbitrator address is the address zero
// * @param _disputeId The ID of the dispute
// */
// function startResolution(bytes32 _disputeId) external;
/**
* @notice Returns the current arbitration status of a dispute
*
* @param _disputeId The ID of the dispute
* @return _disputeStatus The `ArbitrationStatus` of the dispute
*/
function getStatus(bytes32 _disputeId) external view returns (ArbitrationStatus _disputeStatus);

// /**
// * @notice Resolves the dispute by getting the answer from the arbitrator
// * and updating the dispute status
// * @dev Only callable by the Oracle
// * @param _disputeId The ID of the dispute
// */
// function resolveDispute(bytes32 _disputeId) external;
/**
* @notice Starts the arbitration process by calling `resolve` on the arbitrator and flags the dispute as Active
*
* @dev Only callable by the Oracle
* @dev Will revert if the arbitrator address is the address zero
* @param _disputeId The ID of the dispute
* @param _request The request
* @param _response The disputed response
* @param _dispute The dispute being sent to the resolution
*/
function startResolution(
bytes32 _disputeId,
IOracle.Request calldata _request,
IOracle.Response calldata _response,
IOracle.Dispute calldata _dispute
) external;

// /**
// * @notice Returns the decoded data for a request
// * @param _requestId The ID of the request
// * @return _arbitrator The address of the arbitrator
// */
// function decodeRequestData(bytes32 _requestId) external view returns (address _arbitrator);
// }
/**
* @notice Resolves the dispute by getting the answer from the arbitrator and updating the dispute status
*
* @dev Only callable by the Oracle
* @param _disputeId The ID of the dispute
* @param _request The request
* @param _response The disputed response
* @param _dispute The dispute that is being resolved
*/
function resolveDispute(
bytes32 _disputeId,
IOracle.Request calldata _request,
IOracle.Response calldata _response,
IOracle.Dispute calldata _dispute
) external;
}
Loading

0 comments on commit 9eda7b0

Please sign in to comment.