-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from defi-wonderland/perf/opo-598-multiple-cal…
…lbacks-module perf: optimize `MultipleCallbacksModule`
- Loading branch information
Showing
7 changed files
with
206 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 42 additions & 58 deletions
100
solidity/contracts/modules/finality/MultipleCallbacksModule.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
118
solidity/interfaces/modules/finality/IMultipleCallbacksModule.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.