Skip to content

Commit

Permalink
perf: optimize ArbitratorModule
Browse files Browse the repository at this point in the history
  • Loading branch information
gas1cent committed Nov 7, 2023
1 parent ddf6f72 commit 4c2e666
Show file tree
Hide file tree
Showing 4 changed files with 444 additions and 462 deletions.
2 changes: 1 addition & 1 deletion docs/src/content/modules/resolution/arbitrator_module.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The Arbitrator Module is a part of the dispute resolution system. It allows an e
- `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.
- `decodeRequestData(bytes calldata _data)`: 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
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);
}
}
164 changes: 93 additions & 71 deletions solidity/interfaces/modules/resolution/IArbitratorModule.sol
Original file line number Diff line number Diff line change
@@ -1,84 +1,106 @@
// // 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 13 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 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();

// /*///////////////////////////////////////////////////////////////
// ENUMS
// //////////////////////////////////////////////////////////////*/
/*///////////////////////////////////////////////////////////////
STRUCTS
//////////////////////////////////////////////////////////////*/
/**
* @notice Parameters of the request as stored in the module
* @param arbitrator The address of the arbitrator
*/
struct RequestParameters {
address arbitrator;
}

// /**
// * @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
// }
/*///////////////////////////////////////////////////////////////
ENUMS
//////////////////////////////////////////////////////////////*/

// /*///////////////////////////////////////////////////////////////
// LOGIC
// //////////////////////////////////////////////////////////////*/
/**
* @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 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);
/*///////////////////////////////////////////////////////////////
LOGIC
//////////////////////////////////////////////////////////////*/

// /**
// * @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
*/
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
*/
function resolveDispute(
bytes32 _disputeId,
IOracle.Request calldata _request,
IOracle.Response calldata _response,
IOracle.Dispute calldata _dispute
) external;

/**
* @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);
}
Loading

0 comments on commit 4c2e666

Please sign in to comment.