|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "../interface/plugin/IMap.sol"; |
| 5 | +import "../interface/plugin/IEntrypointOverrideable.sol"; |
| 6 | +import "../../extension/Multicall.sol"; |
| 7 | +import "../../openzeppelin-presets/utils/EnumerableSet.sol"; |
| 8 | + |
| 9 | +library EntrypointOverrideableStorage { |
| 10 | + bytes32 public constant ENTRYPOINT_OVERRIDEABLE_STORAGE_POSITION = keccak256("entrypoint.overrideable.storage"); |
| 11 | + |
| 12 | + struct Data { |
| 13 | + EnumerableSet.Bytes32Set functions; |
| 14 | + mapping(bytes4 => address) extensionOverride; |
| 15 | + } |
| 16 | + |
| 17 | + function entrypointStorage() internal pure returns (Data storage entrypointData) { |
| 18 | + bytes32 position = ENTRYPOINT_OVERRIDEABLE_STORAGE_POSITION; |
| 19 | + assembly { |
| 20 | + entrypointData.slot := position |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +abstract contract EntrypointOverrideable is Multicall, IEntrypointOverrideable { |
| 26 | + using EnumerableSet for EnumerableSet.Bytes32Set; |
| 27 | + |
| 28 | + /*/////////////////////////////////////////////////////////////// |
| 29 | + State variables |
| 30 | + //////////////////////////////////////////////////////////////*/ |
| 31 | + |
| 32 | + address public immutable functionMap; |
| 33 | + |
| 34 | + /*/////////////////////////////////////////////////////////////// |
| 35 | + Constructor + initializer logic |
| 36 | + //////////////////////////////////////////////////////////////*/ |
| 37 | + |
| 38 | + constructor(address _functionMap) { |
| 39 | + functionMap = _functionMap; |
| 40 | + } |
| 41 | + |
| 42 | + /*/////////////////////////////////////////////////////////////// |
| 43 | + Generic contract logic |
| 44 | + //////////////////////////////////////////////////////////////*/ |
| 45 | + |
| 46 | + fallback() external payable virtual { |
| 47 | + address extension = _getExtensionOverride(msg.sig); |
| 48 | + if (extension == address(0)) { |
| 49 | + extension = IMap(functionMap).getExtensionForFunction(msg.sig); |
| 50 | + } |
| 51 | + |
| 52 | + _delegate(extension); |
| 53 | + } |
| 54 | + |
| 55 | + receive() external payable {} |
| 56 | + |
| 57 | + function _delegate(address implementation) internal virtual { |
| 58 | + assembly { |
| 59 | + // Copy msg.data. We take full control of memory in this inline assembly |
| 60 | + // block because it will not return to Solidity code. We overwrite the |
| 61 | + // Solidity scratch pad at memory position 0. |
| 62 | + calldatacopy(0, 0, calldatasize()) |
| 63 | + |
| 64 | + // Call the implementation. |
| 65 | + // out and outsize are 0 because we don't know the size yet. |
| 66 | + let result := delegatecall(gas(), implementation, 0, calldatasize(), 0, 0) |
| 67 | + |
| 68 | + // Copy the returned data. |
| 69 | + returndatacopy(0, 0, returndatasize()) |
| 70 | + |
| 71 | + switch result |
| 72 | + // delegatecall returns 0 on error. |
| 73 | + case 0 { |
| 74 | + revert(0, returndatasize()) |
| 75 | + } |
| 76 | + default { |
| 77 | + return(0, returndatasize()) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /*/////////////////////////////////////////////////////////////// |
| 83 | + External functions |
| 84 | + //////////////////////////////////////////////////////////////*/ |
| 85 | + |
| 86 | + function overrideExtensionForFunction(bytes4 _selector, address _extension) external { |
| 87 | + require(_canOverrideExtensions(), "Entrypoint: cannot override extensions."); |
| 88 | + |
| 89 | + EntrypointOverrideableStorage.Data storage data = EntrypointOverrideableStorage.entrypointStorage(); |
| 90 | + data.extensionOverride[_selector] = _extension; |
| 91 | + |
| 92 | + if (_extension != address(0)) { |
| 93 | + data.functions.add(bytes32(_selector)); |
| 94 | + } else { |
| 95 | + data.functions.remove(bytes32(_selector)); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + function getAllOverriden() external view returns (ExtensionMap[] memory functionExtensionPairs) { |
| 100 | + EntrypointOverrideableStorage.Data storage data = EntrypointOverrideableStorage.entrypointStorage(); |
| 101 | + uint256 len = data.functions.length(); |
| 102 | + functionExtensionPairs = new ExtensionMap[](len); |
| 103 | + |
| 104 | + for (uint256 i = 0; i < len; i += 1) { |
| 105 | + bytes4 selector = bytes4(data.functions.at(i)); |
| 106 | + functionExtensionPairs[i] = ExtensionMap(selector, data.extensionOverride[selector]); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /*/////////////////////////////////////////////////////////////// |
| 111 | + Internal functions |
| 112 | + //////////////////////////////////////////////////////////////*/ |
| 113 | + |
| 114 | + function _getExtensionOverride(bytes4 _selector) internal view returns (address) { |
| 115 | + EntrypointOverrideableStorage.Data storage data = EntrypointOverrideableStorage.entrypointStorage(); |
| 116 | + return data.extensionOverride[_selector]; |
| 117 | + } |
| 118 | + |
| 119 | + function _canOverrideExtensions() internal view virtual returns (bool); |
| 120 | +} |
0 commit comments