Skip to content

Commit

Permalink
Merge pull request spherex-xyz#10 from spherex-xyz/feature/beacon-cto…
Browse files Browse the repository at this point in the history
…r-args

Feature/beacon ctor args
  • Loading branch information
idoh-spherex committed Oct 11, 2023
2 parents 5a3be95 + c700721 commit 0801e6a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
11 changes: 9 additions & 2 deletions src/ISphereXEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ interface ISphereXEngine {

function addAllowedSenderOnChain(address sender) external;


/**
* This function is taken as is from OZ IERC165, we don't inherit from OZ
* to avoid collisions with the customer OZ version.
*
*
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
Expand All @@ -41,5 +40,13 @@ interface ISphereXEngine {
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}

/**
* @dev this struct is used to reduce the stack usage of the modifiers.
*/
struct ModifierLocals {
bytes32[] storageSlots;
bytes32[] valuesBefore;
uint256 gas;
}
32 changes: 23 additions & 9 deletions src/ProtectedProxies/ProtectedBeaconProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,37 @@ import {BeaconProxy, Proxy} from "@openzeppelin/contracts/proxy/beacon/BeaconPro
import {Address} from "@openzeppelin/contracts/utils/Address.sol";

import {SphereXProtectedProxy} from "../SphereXProtectedProxy.sol";
import {ISphereXEngine} from "../ISphereXEngine.sol";
import {ISphereXEngine, ModifierLocals} from "../ISphereXEngine.sol";
import {ISphereXBeacon} from "./ISphereXBeacon.sol";

/**
* @title BeaconProxy implementation with spherex's protection
*/
contract ProtectedBeaconProxy is SphereXProtectedProxy, BeaconProxy {
constructor(address beacon, bytes memory data)
SphereXProtectedProxy(msg.sender, address(0), address(0))
BeaconProxy(beacon, data)
{}
contract ProtectedBeaconProxy is BeaconProxy {
constructor(address beacon, bytes memory data) BeaconProxy(beacon, data) {}

/**
* @dev This is used since both SphereXProtectedProxy and BeaconProxy implements Proxy.sol _delegate.
* Internal function that reads values from given storage slots and returns them
* @param storageSlots list of storage slots to read
* @return list of values read from the various storage slots
*/
function _delegate(address implementation) internal virtual override(Proxy, SphereXProtectedProxy) {
SphereXProtectedProxy._delegate(implementation);
function _readStorage(bytes32[] memory storageSlots) internal view returns (bytes32[] memory) {
uint256 arrayLength = storageSlots.length;
bytes32[] memory values = new bytes32[](arrayLength);
// create the return array data

for (uint256 i = 0; i < arrayLength; i++) {
bytes32 slot = storageSlots[i];
bytes32 temp_value;
// solhint-disable-next-line no-inline-assembly
// slither-disable-next-line assembly
assembly {
temp_value := sload(slot)
}

values[i] = temp_value;
}
return values;
}

function _before(address engine) private returns (ModifierLocals memory locals) {
Expand Down
4 changes: 2 additions & 2 deletions src/ProtectedProxies/SphereXUpgradeableBeacon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {UpgradeableBeacon} from "@openzeppelin/contracts/proxy/beacon/Upgradeabl
import {ISphereXBeacon} from "./ISphereXBeacon.sol";

contract SphereXUpgradeableBeacon is SphereXProxyBase, UpgradeableBeacon, ISphereXBeacon {
constructor(address implementation)
SphereXProxyBase(msg.sender, address(0), address(0))
constructor(address implementation, address admin, address operator, address engine)
SphereXProxyBase(admin, operator, engine)
UpgradeableBeacon(implementation)
{}

Expand Down
11 changes: 1 addition & 10 deletions src/SphereXProtectedBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pragma solidity ^0.8.0;

import {ISphereXEngine} from "./ISphereXEngine.sol";
import {ISphereXEngine, ModifierLocals} from "./ISphereXEngine.sol";

/**
* @title SphereX base Customer contract template
Expand All @@ -20,15 +20,6 @@ abstract contract SphereXProtectedBase {
bytes32 private constant SPHEREX_ENGINE_STORAGE_SLOT =
bytes32(uint256(keccak256("eip1967.spherex.spherex_engine")) - 1);

/**
* @dev this struct is used to reduce the stack usage of the modifiers.
*/
struct ModifierLocals {
bytes32[] storageSlots;
bytes32[] valuesBefore;
uint256 gas;
}

event ChangedSpherexOperator(address oldSphereXAdmin, address newSphereXAdmin);
event ChangedSpherexEngineAddress(address oldEngineAddress, address newEngineAddress);
event SpherexAdminTransferStarted(address currentAdmin, address pendingAdmin);
Expand Down

0 comments on commit 0801e6a

Please sign in to comment.