Skip to content

Commit

Permalink
Merge pull request #15 from defi-wonderland/perf/opo-598-multiple-cal…
Browse files Browse the repository at this point in the history
…lbacks-module

perf: optimize `MultipleCallbacksModule`
  • Loading branch information
0xmoebius authored Nov 12, 2023
2 parents c96f6cd + a29dfab commit 62f60ae
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ The `MultipleCallbacksModule` is a finality module that allows users to make mul

### Key Methods

- `decodeRequestData(bytes32 _requestId)`: Returns the decoded data for a request. The returned data includes the target addresses for the callback and the calldata forwarded to the targets.
- `finalizeRequest(bytes32 _requestId, address)`: Finalizes the request by executing the callback calls on the targets.
- `decodeRequestData`: Returns the decoded data for a request. The returned data includes the target addresses for the callback and the calldata forwarded to the targets.
- `finalizeRequest`: Finalizes the request by executing the callback calls on the targets.

### Request Parameters

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"package.json": "sort-package-json"
},
"dependencies": {
"@defi-wonderland/prophet-core-contracts": "0.0.0-a1d2cc55",
"@defi-wonderland/prophet-core-contracts": "0.0.0-1ae08a81",
"@defi-wonderland/solidity-utils": "0.0.0-3e9c8e8b",
"@openzeppelin/contracts": "^4.9.3",
"ds-test": "https://github.com/dapphub/ds-test.git#e282159d5170298eb2455a6c05280ab5a73a4ef0",
Expand Down
100 changes: 42 additions & 58 deletions solidity/contracts/modules/finality/MultipleCallbacksModule.sol
Original file line number Diff line number Diff line change
@@ -1,58 +1,42 @@
// // 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 {IMultipleCallbacksModule} from '../../../interfaces/modules/finality/IMultipleCallbacksModule.sol';

// contract MultipleCallbacksModule is Module, IMultipleCallbacksModule {
// constructor(IOracle _oracle) Module(_oracle) {}

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

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

// /**
// * @notice Checks if the target addresses have code and the calldata amount matches the targets amount
// * @param _data The ABI encoded address of the target contracts and the calldata to be executed
// */
// function _afterSetupRequest(bytes32, bytes calldata _data) internal view override {
// RequestParameters memory _params = abi.decode(_data, (RequestParameters));
// uint256 _length = _params.targets.length;
// if (_length != _params.data.length) revert MultipleCallbackModule_InvalidParameters();

// for (uint256 _i; _i < _length;) {
// if (_params.targets[_i].code.length == 0) revert MultipleCallbackModule_TargetHasNoCode();
// unchecked {
// ++_i;
// }
// }
// }

// /// @inheritdoc IMultipleCallbacksModule
// function finalizeRequest(
// bytes32 _requestId,
// address _finalizer
// ) external override(IMultipleCallbacksModule, Module) onlyOracle {
// RequestParameters memory _params = decodeRequestData(_requestId);
// uint256 _length = _params.targets.length;

// for (uint256 _i; _i < _length;) {
// _params.targets[_i].call(_params.data[_i]);
// emit Callback(_requestId, _params.targets[_i], _params.data[_i]);
// unchecked {
// ++_i;
// }
// }

// emit RequestFinalized(_requestId, _finalizer);
// }
// }
// 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 {IMultipleCallbacksModule} from '../../../interfaces/modules/finality/IMultipleCallbacksModule.sol';

contract MultipleCallbacksModule is Module, IMultipleCallbacksModule {
constructor(IOracle _oracle) Module(_oracle) {}

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

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

/// @inheritdoc IMultipleCallbacksModule
function finalizeRequest(
IOracle.Request calldata _request,
IOracle.Response calldata _response,
address _finalizer
) external override(IMultipleCallbacksModule, Module) onlyOracle {
RequestParameters memory _params = decodeRequestData(_request.finalityModuleData);
uint256 _length = _params.targets.length;

for (uint256 _i; _i < _length;) {
_params.targets[_i].call(_params.data[_i]);
emit Callback(_response.requestId, _params.targets[_i], _params.data[_i]);
unchecked {
++_i;
}
}

emit RequestFinalized(_response.requestId, _response, _finalizer);
}
}
118 changes: 57 additions & 61 deletions solidity/interfaces/modules/finality/IMultipleCallbacksModule.sol
Original file line number Diff line number Diff line change
@@ -1,69 +1,65 @@
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.8.19;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

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

// /**
// * @title MultipleCallbackModule
// * @notice Module allowing users to make multiple calls to different contracts
// * as a result of a request being finalized.
// */
// interface IMultipleCallbacksModule is IFinalityModule {
// /*///////////////////////////////////////////////////////////////
// EVENTS
// //////////////////////////////////////////////////////////////*/
/**
* @title MultipleCallbackModule
* @notice Module allowing users to make multiple calls to different contracts
* as a result of a request being finalized.
*/
interface IMultipleCallbacksModule is IFinalityModule {
/*///////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/

// /**
// * @notice A callback has been executed
// * @param _requestId The id of the request being finalized
// * @param _target The target address for the callback
// * @param _data The calldata forwarded to the _target
// */
// event Callback(bytes32 indexed _requestId, address indexed _target, bytes _data);
/**
* @notice A callback has been executed
* @param _requestId The id of the request being finalized
* @param _target The target address for the callback
* @param _data The calldata forwarded to the _target
*/
event Callback(bytes32 indexed _requestId, address indexed _target, bytes _data);

// /*///////////////////////////////////////////////////////////////
// ERRORS
// //////////////////////////////////////////////////////////////*/
/*///////////////////////////////////////////////////////////////
STRUCTS
//////////////////////////////////////////////////////////////*/

// /**
// * @notice Thrown when then target address has no code (i.e. is not a contract)
// */
// error MultipleCallbackModule_TargetHasNoCode();
/**
* @notice Parameters of the request as stored in the module
* @param targets The target addresses for the callback
* @param data The calldata forwarded to the targets
*/
struct RequestParameters {
address[] targets;
bytes[] data;
}

// /**
// * @notice Thrown when the targets array and the data array have different lengths
// */
// error MultipleCallbackModule_InvalidParameters();
/*///////////////////////////////////////////////////////////////
LOGIC
//////////////////////////////////////////////////////////////*/

// /*///////////////////////////////////////////////////////////////
// STRUCTS
// //////////////////////////////////////////////////////////////*/
/**
* @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 Parameters of the request as stored in the module
// * @param targets The target addresses for the callback
// * @param data The calldata forwarded to the targets
// */
// struct RequestParameters {
// address[] targets;
// bytes[] data;
// }
// /*///////////////////////////////////////////////////////////////
// LOGIC
// //////////////////////////////////////////////////////////////*/

// /**
// * @notice Returns the decoded data for a request
// * @param _requestId The id of the request
// * @return _params The struct containing the parameters for the request
// */
// function decodeRequestData(bytes32 _requestId) external view returns (RequestParameters memory _params);

// /**
// * @notice Finalizes the request by executing the callback calls on the targets
// * @dev The success of the callback calls is purposely not checked
// * @param _requestId The id of the request
// */
// function finalizeRequest(bytes32 _requestId, address) external;
// }
/**
* @notice Finalizes the request by executing the callback calls on the targets
*
* @dev The success of the callback calls is purposely not checked
* @param _request The request being finalized
* @param _response The response
* @param _finalizer The address finalizing the request
*/
function finalizeRequest(
IOracle.Request calldata _request,
IOracle.Response calldata _response,
address _finalizer
) external;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ contract BaseTest is Test, Helpers {
MockVerifier public mockVerifier;

// Events
event DisputeStatusChanged(bytes32 _disputeId, IOracle.Dispute _dispute, IOracle.DisputeStatus _status);
event DisputeStatusChanged(bytes32 indexed _disputeId, IOracle.Dispute _dispute, IOracle.DisputeStatus _status);
event ResponseDisputed(
bytes32 indexed _requestId,
bytes32 indexed _responseId,
Expand Down
Loading

0 comments on commit 62f60ae

Please sign in to comment.