Skip to content

Commit 07427fe

Browse files
authored
Optimzed callback execution (#135)
* gas benchmark * optimize execute callback function * fix typo * optimize execute callback view function * optimize callback mode loop
1 parent 3faa9d3 commit 07427fe

File tree

2 files changed

+44
-46
lines changed

2 files changed

+44
-46
lines changed

.gas-snapshot

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
CoreBenchmark:test_core_callCallbackFunction_required() (gas: 46411)
2-
CoreBenchmark:test_core_callFunction_callback_callbackFunctionRequired() (gas: 44067)
3-
CoreBenchmark:test_core_callFunction_notPermissionedDelegate() (gas: 18029)
4-
CoreBenchmark:test_core_callFunction_notPermissionedExternal() (gas: 17985)
5-
CoreBenchmark:test_deployCore() (gas: 1920924)
6-
CoreBenchmark:test_deployExtension() (gas: 258057)
7-
CoreBenchmark:test_extension_callFunction_notPermissionedDelegate() (gas: 8294)
8-
CoreBenchmark:test_extension_callFunction_notPermissionedExternal() (gas: 8314)
9-
CoreBenchmark:test_installExtension() (gas: 330369)
10-
CoreBenchmark:test_uninstallExtension() (gas: 18385)
1+
CoreBenchmark:test_core_callCallbackFunction_required() (gas: 67409)
2+
CoreBenchmark:test_core_callFunction_callback_callbackFunctionRequired() (gas: 65131)
3+
CoreBenchmark:test_core_callFunction_notPermissionedDelegate() (gas: 39115)
4+
CoreBenchmark:test_core_callFunction_notPermissionedExternal() (gas: 39093)
5+
CoreBenchmark:test_deployCore() (gas: 2139892)
6+
CoreBenchmark:test_deployModule() (gas: 350173)
7+
CoreBenchmark:test_installModule() (gas: 352153)
8+
CoreBenchmark:test_module_callFunction_notPermissionedDelegate() (gas: 29381)
9+
CoreBenchmark:test_module_callFunction_notPermissionedExternal() (gas: 29379)
10+
CoreBenchmark:test_uninstallModule() (gas: 91332)

src/Core.sol

+34-36
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ abstract contract Core is ICore, OwnableRoles, ReentrancyGuard {
3838
EVENTS
3939
//////////////////////////////////////////////////////////////*/
4040

41-
/// @notice Emitted when an module is installed.
41+
/// @notice Emitted when a module is installed.
4242
event ModuleInstalled(address caller, address implementation, address installedModule);
4343

44-
/// @notice Emitted when an module is uninstalled.
44+
/// @notice Emitted when a module is uninstalled.
4545
event ModuleUninstalled(address caller, address implementation, address installedModule);
4646

4747
/*//////////////////////////////////////////////////////////////
@@ -125,7 +125,7 @@ abstract contract Core is ICore, OwnableRoles, ReentrancyGuard {
125125
EXTERNAL FUNCTIONS
126126
//////////////////////////////////////////////////////////////*/
127127

128-
/// @notice Installs an module contract.
128+
/// @notice Installs a module contract.
129129
function installModule(address _module, bytes calldata _data)
130130
external
131131
payable
@@ -135,7 +135,7 @@ abstract contract Core is ICore, OwnableRoles, ReentrancyGuard {
135135
_installModule(_module, _data);
136136
}
137137

138-
/// @notice Uninstalls an module contract.
138+
/// @notice Uninstalls a module contract.
139139
function uninstallModule(address _module, bytes calldata _data)
140140
external
141141
payable
@@ -171,7 +171,7 @@ abstract contract Core is ICore, OwnableRoles, ReentrancyGuard {
171171
return false;
172172
}
173173

174-
/// @dev Installs an module contract.
174+
/// @dev Installs a module contract.
175175
function _installModule(address _module, bytes memory _data) internal {
176176
if (!modules.add(_module)) {
177177
revert ModuleAlreadyInstalled();
@@ -253,7 +253,7 @@ abstract contract Core is ICore, OwnableRoles, ReentrancyGuard {
253253
emit ModuleInstalled(msg.sender, _module, _module);
254254
}
255255

256-
/// @notice Uninstalls an module contract.
256+
/// @notice Uninstalls a module contract.
257257
function _uninstallModule(address _module, bytes memory _data) internal {
258258
// Check: remove and check if the module is installed
259259
if (!modules.remove(_module)) {
@@ -288,7 +288,7 @@ abstract contract Core is ICore, OwnableRoles, ReentrancyGuard {
288288
emit ModuleUninstalled(msg.sender, _module, _module);
289289
}
290290

291-
/// @dev Calls an module callback function and checks whether it is optional or required.
291+
/// @dev Calls a module callback function and checks whether it is optional or required.
292292
function _executeCallbackFunction(bytes4 _selector, bytes memory _abiEncodedCalldata)
293293
internal
294294
nonReentrant
@@ -301,29 +301,28 @@ abstract contract Core is ICore, OwnableRoles, ReentrancyGuard {
301301
revert CallbackFunctionNotSupported();
302302
}
303303

304-
// Get callback mode -- required or not required.
305-
SupportedCallbackFunction[] memory functions = getSupportedCallbackFunctions();
306-
uint256 len = functions.length;
307-
308-
CallbackMode callbackMode;
309-
for (uint256 i = 0; i < len; i++) {
310-
if (functions[i].selector == _selector) {
311-
callbackMode = functions[i].mode;
312-
break;
313-
}
314-
}
315-
316304
if (callbackFunction.implementation != address(0)) {
317305
(success, returndata) = callbackFunction.implementation.delegatecall(_abiEncodedCalldata);
318306
if (!success) {
319307
_revert(returndata, CallbackExecutionReverted.selector);
320308
}
321-
} else if (callbackMode == CallbackMode.REQUIRED) {
322-
revert CallbackFunctionRequired();
309+
} else {
310+
// Get callback mode -- required or not required.
311+
SupportedCallbackFunction[] memory functions = getSupportedCallbackFunctions();
312+
uint256 len = functions.length;
313+
314+
for (uint256 i = 0; i < len; i++) {
315+
if (functions[i].selector == _selector) {
316+
if (functions[i].mode == CallbackMode.REQUIRED) {
317+
revert CallbackFunctionRequired();
318+
}
319+
break;
320+
}
321+
}
323322
}
324323
}
325324

326-
/// @dev Calls an module callback function and checks whether it is optional or required.
325+
/// @dev Calls a module callback function and checks whether it is optional or required.
327326
function _executeCallbackFunctionView(bytes4 _selector, bytes memory _abiEncodedCalldata)
328327
internal
329328
view
@@ -336,25 +335,24 @@ abstract contract Core is ICore, OwnableRoles, ReentrancyGuard {
336335
revert CallbackFunctionNotSupported();
337336
}
338337

339-
// Get callback mode -- required or not required.
340-
SupportedCallbackFunction[] memory functions = getSupportedCallbackFunctions();
341-
uint256 len = functions.length;
342-
343-
CallbackMode callbackMode;
344-
for (uint256 i = 0; i < len; i++) {
345-
if (functions[i].selector == _selector) {
346-
callbackMode = functions[i].mode;
347-
break;
348-
}
349-
}
350-
351338
if (callbackFunction.implementation != address(0)) {
352339
(success, returndata) = address(this).staticcall(_abiEncodedCalldata);
353340
if (!success) {
354341
_revert(returndata, CallbackExecutionReverted.selector);
355342
}
356-
} else if (callbackMode == CallbackMode.REQUIRED) {
357-
revert CallbackFunctionRequired();
343+
} else {
344+
// Get callback mode -- required or not required.
345+
SupportedCallbackFunction[] memory functions = getSupportedCallbackFunctions();
346+
uint256 len = functions.length;
347+
348+
for (uint256 i = 0; i < len; i++) {
349+
if (functions[i].selector == _selector) {
350+
if (functions[i].mode == CallbackMode.REQUIRED) {
351+
revert CallbackFunctionRequired();
352+
}
353+
break;
354+
}
355+
}
358356
}
359357
}
360358

0 commit comments

Comments
 (0)