Skip to content

Commit

Permalink
perf: optimize CallbackModule (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
gas1cent authored Nov 11, 2023
1 parent c82e476 commit c6d4e8d
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 255 deletions.
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.
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);
}
}
128 changes: 63 additions & 65 deletions solidity/interfaces/modules/finality/ICallbackModule.sol
Original file line number Diff line number Diff line change
@@ -1,65 +1,63 @@
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.8.19;

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

// /**
// * @title CallbackModule
// * @notice Module allowing users to call a function on a contract
// * as a result of a request being finalized.
// */
// interface ICallbackModule 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);

// /*///////////////////////////////////////////////////////////////
// ERRORS
// //////////////////////////////////////////////////////////////*/

// /**
// * @notice Thrown when the target address has no code (i.e. is not a contract)
// */
// error CallbackModule_TargetHasNoCode();

// /*///////////////////////////////////////////////////////////////
// STRUCTS
// //////////////////////////////////////////////////////////////*/

// /**
// * @notice Parameters of the request as stored in the module
// * @param target The target address for the callback
// * @param data The calldata forwarded to the _target
// */
// struct RequestParameters {
// address target;
// 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 call on the target
// * @dev The success of the callback call is purposely not checked
// * @param _requestId The id of the request
// */
// function finalizeRequest(bytes32 _requestId, address) external;
// }
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

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 CallbackModule
* @notice Module allowing users to call a function on a contract
* as a result of a request being finalized.
*/
interface ICallbackModule 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);

/*///////////////////////////////////////////////////////////////
STRUCTS
//////////////////////////////////////////////////////////////*/

/**
* @notice Parameters of the request as stored in the module
* @param target The target address for the callback
* @param data The calldata forwarded to the _target
*/
struct RequestParameters {
address target;
bytes data;
}

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

/**
* @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 Finalizes the request by executing the callback call on the target
* @dev The success of the callback call is purposely not checked
* @param _request The request being finalized
* @param _response The final response
* @param _finalizer The address that initiated the finalization
*/
function finalizeRequest(
IOracle.Request calldata _request,
IOracle.Response calldata _response,
address _finalizer
) external;
}
Loading

0 comments on commit c6d4e8d

Please sign in to comment.