Transaction Hash:
Block:
20462098 at Aug-05-2024 11:34:59 AM +UTC
Transaction Fee:
0.0060652703844603 ETH
$13.70
Gas Used:
160,290 Gas / 37.83935607 Gwei
Emitted Events:
164 |
Proxy.0x9866f8ddfe70bb512b2f2b28b49d4017c43f7ba775f1a20c61c13eea8cdac111( 0x9866f8ddfe70bb512b2f2b28b49d4017c43f7ba775f1a20c61c13eea8cdac111, 78816c1142a5b55ff4f27383b191e3b909acabe8f4a9630d00699bd06e408823 )
|
165 |
Proxy.0x2672b53d25204094519f7b0fba8d2b5cd0cc1e426f49554c89461cdb9dcec08f( 0x2672b53d25204094519f7b0fba8d2b5cd0cc1e426f49554c89461cdb9dcec08f, 0000000000000000000000000000000000000000000000000000000000000105, 0000000000000000000000000000000000000000000000000000000000000105 )
|
Account State Difference:
Address | Before | After | State Difference | ||
---|---|---|---|---|---|
0x95222290...5CC4BAfe5
Miner
| (beaverbuild) | 6.29030679653697959 Eth | 6.29054723153697959 Eth | 0.000240435 | |
0xCBe6fbf5...05890c4bE |
5.697066111376305197 Eth
Nonce: 523
|
5.691000840991844897 Eth
Nonce: 524
| 0.0060652703844603 | ||
0xfAaE2946...9fA7d83DD |
Execution Trace
Proxy.538f9406( )
StarkPerpetual.538f9406( )
PerpetualState.updateState( programOutput=[2709739437445365581839607575480067270776788628024404825133389496524515212850, 0, 10, 339248760150588245376950366141153280, 3227474820798936117769241731037441376655836565025668355812772465413527090458, 344278863666843608900923772802957312, 777336590596743353097754011184648664781033590609845071754567471072205620859, 344400638885778253860530994056003584, 2835001893685947666376121498030164113839277334081255538282824190539164388563, 354684143347722343183560610217459712, 1892704610648537240350416598274027060870039100740952142714396830528897062360, 359977924069045087395084506562035712, 801768444596032524967613964230879666515400286406149963657831743137904493300, 396101380212431435334177334145056768, 102872403802112671469570821928696405701440538299218037795054959592026784188, 396323605936767383653495383972118528, 2125545610456369765608388126250331295283802027854598857071512446248012166545, 401131892570687552113786006049652736, 3532574809517450733593392881089714264060214291098884754589458368765819293566, 432568984951955547080127463915782144, 2409116671548289620469958353361823063575071672439171865370488937823736240344, 458591633384881771471787532316835840, 3365636747832722437945332534052394150904470008774008947776009987469882110216, 48, 1664299737092915359470080425548338958128374789480578687556117550396084523911, 64, 3084478912188529581394753880010736124557798654442544476574134738434648462716, 64, 10, 339248760150588245376950366141153280, 9223372036857740841, 344278863666843608900923772802957312, 9223372036870705212, 344400638885778253860530994056003584, 9223372036705443422, 354684143347722343183560610217459712, 9223372036854911931, 359977924069045087395084506562035712, 9223372036850079708, 396101380212431435334177334145056768, 9223372036856235383, 396323605936767383653495383972118528, 9223372036855964204, 401131892570687552113786006049652736, 9223372036855334476, 432568984951955547080127463915782144, 9223372036850071045, 458591633384881771471787532316835840, 9223372036855427172, 1722841200, 10, 339248760150588245376950366141153280, 789101297, 344278863666843608900923772802957312, 1838163498, 344400638885778253860530994056003584, 22224169270, 354684143347722343183560610217459712, 36898494, 359977924069045087395084506562035712, 9846845518, 396101380212431435334177334145056768, 373933031, 396323605936767383653495383972118528, 2253926998, 401131892570687552113786006049652736, 152254443, 432568984951955547080127463915782144, 5070151942, 458591633384881771471787532316835840, 196379555, 1722842081, 48, 107904308052203327410104765882143509758694705235231034015768789278036256984, 64, 2096432624627322562884126123263019945757518322612881723736278928955955123233, 64, 10, 339248760150588245376950366141153280, 9223372036857740841, 344278863666843608900923772802957312, 9223372036870705212, 344400638885778253860530994056003584, 9223372036705443422, 354684143347722343183560610217459712, 9223372036854911931, 359977924069045087395084506562035712, 9223372036850079708, 396101380212431435334177334145056768, 9223372036856235383, 396323605936767383653495383972118528, 9223372036855964204, 401131892570687552113786006049652736, 9223372036855334476, 432568984951955547080127463915782144, 9223372036850071045, 458591633384881771471787532316835840, 9223372036855427172, 1722841200, 10, 339248760150588245376950366141153280, 824247453, 344278863666843608900923772802957312, 1891934702, 344400638885778253860530994056003584, 22722992142, 354684143347722343183560610217459712, 38557425, 359977924069045087395084506562035712, 10150690475, 396101380212431435334177334145056768, 392792477, 396323605936767383653495383972118528, 2343072894, 401131892570687552113786006049652736, 160004282, 432568984951955547080127463915782144, 5307109604, 458591633384881771471787532316835840, 203477614, 1722844052, 479287, 0, 0, 0, 0], applicationData=[261, 260, 0] )
FinalizableGpsFactAdapter.isValid( fact=78816C1142A5B55FF4F27383B191E3B909ACABE8F4A9630D00699BD06E408823 ) => ( True )
Proxy.6a938567( )
CallProxy.isValid( fact=BF2928BA0169304EE260251027BCED66379C42B84CB5B55905B4CA1560753A2F ) => ( True )
-
GpsStatementVerifier.isValid( fact=BF2928BA0169304EE260251027BCED66379C42B84CB5B55905B4CA1560753A2F ) => ( True )
-
-
FinalizableCommittee.isValid( fact=3787E640FC31F617342EE5DA7CA34BD766C39089F712BE0B3EB6F98DB02AF6D7 ) => ( True )
File 1 of 8: Proxy
File 2 of 8: StarkPerpetual
File 3 of 8: PerpetualState
File 4 of 8: FinalizableGpsFactAdapter
File 5 of 8: Proxy
File 6 of 8: CallProxy
File 7 of 8: GpsStatementVerifier
File 8 of 8: FinalizableCommittee
/* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: MIT // Based on OpenZeppelin Contract (access/AccessControl.sol) // StarkWare modification (storage slot, change to library). pragma solidity ^0.8.0; import "third_party/open_zeppelin/utils/Strings.sol"; /* Library module that allows using contracts to implement role-based access control mechanisms. This is a lightweight version that doesn't allow enumerating role members except through off-chain means by accessing the contract event logs. Some applications may benefit from on-chain enumerability, for those cases see {AccessControlEnumerable}. Roles are referred to by their `bytes32` identifier. These should be exposed in the external API and be unique. The best way to achieve this is by using `public constant` hash digests: ``` bytes32 public constant MY_ROLE = keccak256("MY_ROLE"); ``` Roles can be used to represent a set of permissions. To restrict access to a function call, use {hasRole}: ``` function foo() public { require(hasRole(MY_ROLE, msg.sender)); ... } ``` Roles can be granted and revoked dynamically via the {grantRole} and {revokeRole} functions. Each role has an associated admin role, and only accounts that have a role's admin role can call {grantRole} and {revokeRole}. By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means that only accounts with this role will be able to grant or revoke other roles. More complex role relationships can be created by using {_setRoleAdmin}. WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to grant and revoke this role. Extra precautions should be taken to secure accounts that have been granted it. OpenZeppelin implementation changed as following: 1. Converted to library. 2. Storage valiable {_roles} moved outside of linear storage, to avoid potential storage conflicts or corruption. 3. Removed ERC165 support. */ library AccessControl { /* Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole` `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite {RoleAdminChanged} not being emitted signaling this. Available since v3.1. */ event RoleAdminChanged( bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole ); /* Emitted when `account` is granted `role`. `sender` is the account that originated the contract call, an admin role bearer except when using {AccessControl-_setupRole}. */ event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender); /* Emitted when `account` is revoked `role`. `sender` is the account that originated the contract call: - if using `revokeRole`, it is the admin role bearer - if using `renounceRole`, it is the role bearer (i.e. `account`). */ event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender); struct RoleData { mapping(address => bool) members; bytes32 adminRole; } // Context interface functions. function _msgSender() internal view returns (address) { return msg.sender; } function _msgData() internal pure returns (bytes calldata) { return msg.data; } // The storage variable `_roles` is located away from the contract linear area (low storage addresses) // to prevent potential collision/corruption in upgrade scenario. // Slot = Web3.keccak(text="AccesControl_Storage_Slot"). bytes32 constant rolesSlot = 0x53e43b954ba190a7e49386f1f78b01dcd9f628db23f432fa029a7dfd6d98e8fb; function _roles() private pure returns (mapping(bytes32 => RoleData) storage roles) { assembly { roles.slot := rolesSlot } } bytes32 constant DEFAULT_ADMIN_ROLE = 0x00; /* Modifier that checks that an account has a specific role. Reverts with a standardized message including the required role. The format of the revert reason is given by the following regular expression: /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/ Available since v4.1. */ modifier onlyRole(bytes32 role) { _checkRole(role); _; } /* Returns `true` if `account` has been granted `role`. */ function hasRole(bytes32 role, address account) internal view returns (bool) { return _roles()[role].members[account]; } /* Revert with a standard message if `_msgSender()` is missing `role`. Overriding this function changes the behavior of the {onlyRole} modifier. Format of the revert message is described in {_checkRole}. Available since v4.6. */ function _checkRole(bytes32 role) internal view { _checkRole(role, _msgSender()); } /* Revert with a standard message if `account` is missing `role`. The format of the revert reason is given by the following regular expression: /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/. */ function _checkRole(bytes32 role, address account) internal view { if (!hasRole(role, account)) { revert( string( abi.encodePacked( "AccessControl: account ", Strings.toHexString(uint160(account), 20), " is missing role ", Strings.toHexString(uint256(role), 32) ) ) ); } } /* Returns the admin role that controls `role`. See {grantRole} and {revokeRole}. To change a role's admin, use {_setRoleAdmin}. */ function getRoleAdmin(bytes32 role) internal view returns (bytes32) { return _roles()[role].adminRole; } /* Grants `role` to `account`. If `account` had not been already granted `role`, emits a {RoleGranted} event. Requirements: - the caller must have ``role``'s admin role. May emit a {RoleGranted} event. */ function grantRole(bytes32 role, address account) internal onlyRole(getRoleAdmin(role)) { _grantRole(role, account); } /* Revokes `role` from `account`. If `account` had been granted `role`, emits a {RoleRevoked} event. Requirements: - the caller must have ``role``'s admin role. * May emit a {RoleRevoked} event. */ function revokeRole(bytes32 role, address account) internal onlyRole(getRoleAdmin(role)) { _revokeRole(role, account); } /* Revokes `role` from the calling account. Roles are often managed via {grantRole} and {revokeRole}: this function's purpose is to provide a mechanism for accounts to lose their privileges if they are compromised (such as when a trusted device is misplaced). If the calling account had been revoked `role`, emits a {RoleRevoked} event. Requirements: - the caller must be `account`. May emit a {RoleRevoked} event. */ function renounceRole(bytes32 role, address account) internal { require(account == _msgSender(), "AccessControl: can only renounce roles for self"); _revokeRole(role, account); } /* Grants `role` to `account`. If `account` had not been already granted `role`, emits a {RoleGranted} event. Note that unlike {grantRole}, this function doesn't perform any checks on the calling account. May emit a {RoleGranted} event. [WARNING]virtual ==== This function should only be called from the constructor when setting up the initial roles for the system. Using this function in any other way is effectively circumventing the admin system imposed by {AccessControl}. ==== NOTE: This function is deprecated in favor of {_grantRole}. */ function _setupRole(bytes32 role, address account) internal { _grantRole(role, account); } /* Sets `adminRole` as ``role``'s admin role. Emits a {RoleAdminChanged} event. */ function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal { bytes32 previousAdminRole = getRoleAdmin(role); _roles()[role].adminRole = adminRole; emit RoleAdminChanged(role, previousAdminRole, adminRole); } /* Grants `role` to `account`. Internal function without access restriction. May emit a {RoleGranted} event. */ function _grantRole(bytes32 role, address account) internal { if (!hasRole(role, account)) { _roles()[role].members[account] = true; emit RoleGranted(role, account, _msgSender()); } } /* Revokes `role` from `account`. Internal function without access restriction. May emit a {RoleRevoked} event. */ function _revokeRole(bytes32 role, address account) internal { if (hasRole(role, account)) { _roles()[role].members[account] = false; emit RoleRevoked(role, account, _msgSender()); } } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.8.0; /* Common Utility Libraries. I. Addresses (extending address). */ library Addresses { /* Note: isContract function has some known limitation. See https://github.com/OpenZeppelin/ openzeppelin-contracts/blob/master/contracts/utils/Address.sol. */ function isContract(address account) internal view returns (bool) { uint256 size; assembly { size := extcodesize(account) } return size > 0; } function performEthTransfer(address recipient, uint256 amount) internal { if (amount == 0) return; (bool success, ) = recipient.call{value: amount}(""); // NOLINT: low-level-calls. require(success, "ETH_TRANSFER_FAILED"); } /* Safe wrapper around ERC20/ERC721 calls. This is required because many deployed ERC20 contracts don't return a value. See https://github.com/ethereum/solidity/issues/4116. */ function safeTokenContractCall(address tokenAddress, bytes memory callData) internal { require(isContract(tokenAddress), "BAD_TOKEN_ADDRESS"); // NOLINTNEXTLINE: low-level-calls. (bool success, bytes memory returndata) = tokenAddress.call(callData); require(success, string(returndata)); if (returndata.length > 0) { require(abi.decode(returndata, (bool)), "TOKEN_OPERATION_FAILED"); } } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.8.0; /* Library to provide basic storage, in storage location out of the low linear address space. New types of storage variables should be added here upon need. */ library NamedStorage { function bytes32ToBoolMapping(string memory tag_) internal pure returns (mapping(bytes32 => bool) storage randomVariable) { bytes32 location = keccak256(abi.encodePacked(tag_)); assembly { randomVariable.slot := location } } function bytes32ToUint256Mapping(string memory tag_) internal pure returns (mapping(bytes32 => uint256) storage randomVariable) { bytes32 location = keccak256(abi.encodePacked(tag_)); assembly { randomVariable.slot := location } } function addressToUint256Mapping(string memory tag_) internal pure returns (mapping(address => uint256) storage randomVariable) { bytes32 location = keccak256(abi.encodePacked(tag_)); assembly { randomVariable.slot := location } } function bytes32ToAddressMapping(string memory tag_) internal pure returns (mapping(bytes32 => address) storage randomVariable) { bytes32 location = keccak256(abi.encodePacked(tag_)); assembly { randomVariable.slot := location } } function uintToAddressMapping(string memory tag_) internal pure returns (mapping(uint256 => address) storage randomVariable) { bytes32 location = keccak256(abi.encodePacked(tag_)); assembly { randomVariable.slot := location } } function addressToAddressMapping(string memory tag_) internal pure returns (mapping(address => address) storage randomVariable) { bytes32 location = keccak256(abi.encodePacked(tag_)); assembly { randomVariable.slot := location } } function addressToAddressListMapping(string memory tag_) internal pure returns (mapping(address => address[]) storage randomVariable) { bytes32 location = keccak256(abi.encodePacked(tag_)); assembly { randomVariable.slot := location } } function addressToBoolMapping(string memory tag_) internal pure returns (mapping(address => bool) storage randomVariable) { bytes32 location = keccak256(abi.encodePacked(tag_)); assembly { randomVariable.slot := location } } function getUintValue(string memory tag_) internal view returns (uint256 retVal) { bytes32 slot = keccak256(abi.encodePacked(tag_)); assembly { retVal := sload(slot) } } function setUintValue(string memory tag_, uint256 value) internal { bytes32 slot = keccak256(abi.encodePacked(tag_)); assembly { sstore(slot, value) } } function setUintValueOnce(string memory tag_, uint256 value) internal { require(getUintValue(tag_) == 0, "ALREADY_SET"); setUintValue(tag_, value); } function getAddressValue(string memory tag_) internal view returns (address retVal) { bytes32 slot = keccak256(abi.encodePacked(tag_)); assembly { retVal := sload(slot) } } function setAddressValue(string memory tag_, address value) internal { bytes32 slot = keccak256(abi.encodePacked(tag_)); assembly { sstore(slot, value) } } function setAddressValueOnce(string memory tag_, address value) internal { require(getAddressValue(tag_) == address(0x0), "ALREADY_SET"); setAddressValue(tag_, value); } function getBoolValue(string memory tag_) internal view returns (bool retVal) { bytes32 slot = keccak256(abi.encodePacked(tag_)); assembly { retVal := sload(slot) } } function setBoolValue(string memory tag_, bool value) internal { bytes32 slot = keccak256(abi.encodePacked(tag_)); assembly { sstore(slot, value) } } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.8.20; import "starkware/solidity/upgrade/ProxyStorage.sol"; import "starkware/solidity/upgrade/StorageSlots.sol"; import "starkware/solidity/components/ProxyRoles.sol"; import "starkware/solidity/libraries/Addresses.sol"; /** The Proxy contract implements delegation of calls to other contracts (`implementations`), with proper forwarding of return values and revert reasons. This pattern allows retaining the contract storage while replacing implementation code. The following operations are supported by the proxy contract: - :sol:func:`addImplementation`: Defines a new implementation, the data with which it should be initialized and whether this will be the last version of implementation. - :sol:func:`upgradeTo`: Once an implementation is added, the governor may upgrade to that implementation only after a safety time period has passed (time lock), the current implementation is not the last version and the implementation is not frozen (see :sol:mod:`FullWithdrawals`). - :sol:func:`removeImplementation`: Any announced implementation may be removed. Removing an implementation is especially important once it has been used for an upgrade in order to avoid an additional unwanted revert to an older version. The only entity allowed to perform the above operations is the proxy governor (see :sol:mod:`ProxyGovernance`). Every implementation is required to have an `initialize` function that replaces the constructor of a normal contract. Furthermore, the only parameter of this function is an array of bytes (`data`) which may be decoded arbitrarily by the `initialize` function. It is up to the implementation to ensure that this function cannot be run more than once if so desired. When an implementation is added (:sol:func:`addImplementation`) the initialization `data` is also announced, allowing users of the contract to analyze the full effect of an upgrade to the new implementation. During an :sol:func:`upgradeTo`, the `data` is provided again and only if it is identical to the announced `data` is the upgrade performed by pointing the proxy to the new implementation and calling its `initialize` function with this `data`. ProxyStorage contains the storage variables required by the Proxy. The Proxy storage variables are not in the low slot addresses (a.k.a linear storage) - to avoid storage collision. */ contract Proxy is ProxyStorage, StorageSlots, ProxyRoles { // Emitted when the active implementation is replaced. event ImplementationUpgraded(address indexed implementation, bytes initializer); // Emitted when an implementation is submitted as an upgrade candidate and a time lock // is activated. event ImplementationAdded(address indexed implementation, bytes initializer, bool finalize); // Emitted when an implementation is removed from the list of upgrade candidates. event ImplementationRemoved(address indexed implementation, bytes initializer, bool finalize); // Emitted when the implementation is finalized. event FinalizedImplementation(address indexed implementation); using Addresses for address; uint256 public constant MAX_UPGRADE_DELAY = 180 days; string public constant PROXY_VERSION = "5.0.2"; // Initialize Roles(false) so that we cannot renounce governance. // And assign all governanec roels to the deployer. constructor(uint256 upgradeActivationDelay) ProxyRoles(false, true) { setUpgradeActivationDelay(upgradeActivationDelay); setEnableWindowDuration(14 days); } /* Stores the upgrade activation delay (in seconds) in the appropriate slot. this function does not validate the delay value, as it's checked in the getter. */ function setUpgradeActivationDelay(uint256 delayInSeconds) private { bytes32 slot = UPGRADE_DELAY_SLOT; assembly { sstore(slot, delayInSeconds) } } /* Reads the upgrade activation delay (in seconds) at the appropriate slot. The returned value is capped at MAX_UPGRADE_DELAY. It is safer to do the capping in the getter because an upgrade flow might modify this value without going through the setter function. */ function getUpgradeActivationDelay() public view returns (uint256 delay) { bytes32 slot = UPGRADE_DELAY_SLOT; assembly { delay := sload(slot) } delay = (delay < MAX_UPGRADE_DELAY) ? delay : MAX_UPGRADE_DELAY; return delay; } function getEnableWindowDuration() public view returns (uint256 duration) { bytes32 slot = ENABLE_WINDOW_DURATION_SLOT; assembly { duration := sload(slot) } } function setEnableWindowDuration(uint256 durationInSeconds) private { bytes32 slot = ENABLE_WINDOW_DURATION_SLOT; assembly { sstore(slot, durationInSeconds) } } /* Returns the address of the current implementation. */ // NOLINTNEXTLINE external-function. function implementation() public view returns (address _implementation) { bytes32 slot = IMPLEMENTATION_SLOT; assembly { _implementation := sload(slot) } } /* Returns true if the implementation is frozen. If the implementation was not assigned yet, returns false. */ function implementationIsFrozen() private returns (bool) { address _implementation = implementation(); // We can't call low level implementation before it's assigned. (i.e. ZERO). if (_implementation == address(0x0)) { return false; } // NOLINTNEXTLINE: low-level-calls. (bool success, bytes memory returndata) = _implementation.delegatecall( abi.encodeWithSignature("isFrozen()") ); require(success, string(returndata)); return abi.decode(returndata, (bool)); } /* This method blocks delegation to initialize(). Only upgradeTo should be able to delegate call to initialize(). */ function initialize( bytes calldata /*data*/ ) external pure { revert("CANNOT_CALL_INITIALIZE"); } modifier notFinalized() { require(isNotFinalized(), "IMPLEMENTATION_FINALIZED"); _; } /* Forbids calling the function if the implementation is frozen. This modifier relies on the lower level (logical contract) implementation of isFrozen(). */ modifier notFrozen() { require(!implementationIsFrozen(), "STATE_IS_FROZEN"); _; } /* This entry point serves only transactions with empty calldata. (i.e. pure value transfer tx). We don't expect to receive such, thus block them. */ receive() external payable { revert("CONTRACT_NOT_EXPECTED_TO_RECEIVE"); } /* Contract's default function. Delegates execution to the implementation contract. It returns back to the external caller whatever the implementation delegated code returns. */ fallback() external payable { address _implementation = implementation(); require(_implementation != address(0x0), "MISSING_IMPLEMENTATION"); assembly { // Copy msg.data. We take full control of memory in this inline assembly // block because it will not return to Solidity code. We overwrite the // Solidity scratch pad at memory position 0. calldatacopy(0, 0, calldatasize()) // Call the implementation. // out and outsize are 0 for now, as we don't know the out size yet. let result := delegatecall(gas(), _implementation, 0, calldatasize(), 0, 0) // Copy the returned data. returndatacopy(0, 0, returndatasize()) switch result // delegatecall returns 0 on error. case 0 { revert(0, returndatasize()) } default { return(0, returndatasize()) } } } /* Sets the implementation address of the proxy. */ function setImplementation(address newImplementation) private { bytes32 slot = IMPLEMENTATION_SLOT; assembly { sstore(slot, newImplementation) } } /* Returns true if the contract is not in the finalized state. */ function isNotFinalized() public view returns (bool notFinal) { bytes32 slot = FINALIZED_STATE_SLOT; uint256 slotValue; assembly { slotValue := sload(slot) } notFinal = (slotValue == 0); } /* Marks the current implementation as finalized. */ function setFinalizedFlag() private { bytes32 slot = FINALIZED_STATE_SLOT; assembly { sstore(slot, 0x1) } } /* Introduce an implementation and its initialization vector, and start the time-lock before it can be upgraded to. addImplementation is not blocked when frozen or finalized. (upgradeTo API is blocked when finalized or frozen). */ function addImplementation( address newImplementation, bytes calldata data, bool finalize ) external onlyUpgradeGovernor { require(newImplementation.isContract(), "ADDRESS_NOT_CONTRACT"); bytes32 implVectorHash = keccak256(abi.encode(newImplementation, data, finalize)); uint256 activationTime = block.timestamp + getUpgradeActivationDelay(); uint256 lastActivationTime = activationTime + getEnableWindowDuration(); enabledTime()[implVectorHash] = activationTime; expirationTime()[implVectorHash] = lastActivationTime; emit ImplementationAdded(newImplementation, data, finalize); } /* Removes a candidate implementation. Note that it is possible to remove the current implementation. Doing so doesn't affect the current implementation, but rather revokes it as a future candidate. */ function removeImplementation( address removedImplementation, bytes calldata data, bool finalize ) external onlyUpgradeGovernor { bytes32 implVectorHash = keccak256(abi.encode(removedImplementation, data, finalize)); // If we have initializer, we set the hash of it. uint256 activationTime = enabledTime()[implVectorHash]; require(activationTime > 0, "UNKNOWN_UPGRADE_INFORMATION"); delete enabledTime()[implVectorHash]; delete expirationTime()[implVectorHash]; emit ImplementationRemoved(removedImplementation, data, finalize); } /* Upgrades the proxy to a new implementation, with its initialization. to upgrade successfully, implementation must have been added time-lock agreeably before, and the init vector must be identical ot the one submitted before. Upon assignment of new implementation address, its initialize will be called with the initializing vector (even if empty). Therefore, the implementation MUST must have such a method. Note - Initialization data is committed to in advance, therefore it must remain valid until the actual contract upgrade takes place. Care should be taken regarding initialization data and flow when planning the contract upgrade. When planning contract upgrade, special care is also needed with regard to governance (See comments in Governance.sol). */ // NOLINTNEXTLINE: reentrancy-events timestamp. function upgradeTo( address newImplementation, bytes calldata data, bool finalize ) external payable onlyUpgradeGovernor notFinalized notFrozen { bytes32 implVectorHash = keccak256(abi.encode(newImplementation, data, finalize)); uint256 activationTime = enabledTime()[implVectorHash]; uint256 lastActivationTime = expirationTime()[implVectorHash]; require(activationTime > 0, "UNKNOWN_UPGRADE_INFORMATION"); require(newImplementation.isContract(), "ADDRESS_NOT_CONTRACT"); // On the first time an implementation is set - time-lock should not be enforced. require( activationTime <= block.timestamp || implementation() == address(0x0), "UPGRADE_NOT_ENABLED_YET" ); require(lastActivationTime >= block.timestamp, "IMPLEMENTATION_EXPIRED"); setImplementation(newImplementation); // NOLINTNEXTLINE: low-level-calls controlled-delegatecall. (bool success, bytes memory returndata) = newImplementation.delegatecall( abi.encodeWithSelector(this.initialize.selector, data) ); require(success, string(returndata)); // Verify that the new implementation is not frozen post initialization. // NOLINTNEXTLINE: low-level-calls controlled-delegatecall. (success, returndata) = newImplementation.delegatecall( abi.encodeWithSignature("isFrozen()") ); require(success, "CALL_TO_ISFROZEN_REVERTED"); require(!abi.decode(returndata, (bool)), "NEW_IMPLEMENTATION_FROZEN"); // Remove implementation activation entries. delete enabledTime()[implVectorHash]; delete expirationTime()[implVectorHash]; emit ImplementationUpgraded(newImplementation, data); if (finalize) { setFinalizedFlag(); emit FinalizedImplementation(newImplementation); } } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.8.0; import "starkware/solidity/libraries/RolesLib.sol"; abstract contract ProxyRoles { // This flag dermine if the GOVERNANCE_ADMIN role can be renounced. bool immutable fullyRenouncable; constructor(bool renounceable, bool assignAllGovernors) { fullyRenouncable = renounceable; \taddress caller = AccessControl._msgSender(); \t// True will assign all governance roles to deployer. RolesLib.initialize(caller, caller, assignAllGovernors); } // MODIFIERS. modifier onlyUpgradeGovernor() { require(isUpgradeGovernor(AccessControl._msgSender()), "ONLY_UPGRADE_GOVERNOR"); _; } modifier notSelf(address account) { require(account != AccessControl._msgSender(), "CANNOT_PERFORM_ON_SELF"); _; } // Is holding role. function isGovernanceAdmin(address account) public view returns (bool) { return AccessControl.hasRole(GOVERNANCE_ADMIN, account); } function isUpgradeGovernor(address account) public view returns (bool) { return AccessControl.hasRole(UPGRADE_GOVERNOR, account); } // Register Role. function registerAppGovernor(address account) external { AccessControl.grantRole(APP_GOVERNOR, account); } function registerAppRoleAdmin(address account) external { AccessControl.grantRole(APP_ROLE_ADMIN, account); } function registerGovernanceAdmin(address account) external { AccessControl.grantRole(GOVERNANCE_ADMIN, account); } function registerSecurityAdmin(address account) external { AccessControl.grantRole(SECURITY_ADMIN, account); } function registerSecurityAgent(address account) external { AccessControl.grantRole(SECURITY_AGENT, account); } function registerUpgradeGovernor(address account) external { AccessControl.grantRole(UPGRADE_GOVERNOR, account); } // Revoke Role. function revokeAppGovernor(address account) external { AccessControl.revokeRole(APP_GOVERNOR, account); } function revokeAppRoleAdmin(address account) external notSelf(account) { AccessControl.revokeRole(APP_ROLE_ADMIN, account); } function revokeGovernanceAdmin(address account) external notSelf(account) { AccessControl.revokeRole(GOVERNANCE_ADMIN, account); } function revokeOperator(address account) external { AccessControl.revokeRole(OPERATOR, account); } function revokeSecurityAdmin(address account) external notSelf(account) { AccessControl.revokeRole(SECURITY_ADMIN, account); } function revokeSecurityAgent(address account) external { AccessControl.revokeRole(SECURITY_AGENT, account); } function revokeTokenAdmin(address account) external { AccessControl.revokeRole(TOKEN_ADMIN, account); } function revokeUpgradeGovernor(address account) external { AccessControl.revokeRole(UPGRADE_GOVERNOR, account); } // Renounce Role. function renounceRole(bytes32 role, address account) external { if (role == GOVERNANCE_ADMIN && !fullyRenouncable) { revert("CANNOT_RENOUNCE_GOVERNANCE_ADMIN"); } AccessControl.renounceRole(role, account); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.8.0; import "starkware/solidity/libraries/NamedStorage.sol"; /* Holds the Proxy-specific state variables. to prevent collision hazard. */ contract ProxyStorage { // Random storage slot tags. string constant ENABLED_TIME_TAG = "PROXY_5_ENABLED_TIME"; string constant DISABLED_TIME_TAG = "PROXY_5_DISABLED_TIME"; string constant INTIALIZED_TAG = "PROXY_5_INITIALIZED"; // The time after which we can switch to the implementation. // Hash(implementation, data, finalize) => time. function enabledTime() internal pure returns (mapping(bytes32 => uint256) storage) { return NamedStorage.bytes32ToUint256Mapping(ENABLED_TIME_TAG); } // The time after which we can NO LONGER switch to the implementation. // Implementation is valid to switch in time t, enableTime <= t <= disableTime. // Hash(implementation, data, finalize) => time. function expirationTime() internal pure returns (mapping(bytes32 => uint256) storage) { return NamedStorage.bytes32ToUint256Mapping(DISABLED_TIME_TAG); } // A central storage of the flags whether implementation has been initialized. // Note - it can be used flexibly enough to accommodate multiple levels of initialization // (i.e. using different key salting schemes for different initialization levels). function initialized() internal pure returns (mapping(bytes32 => bool) storage) { return NamedStorage.bytes32ToBoolMapping(INTIALIZED_TAG); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.8.0; import "starkware/solidity/libraries/AccessControl.sol"; // int.from_bytes(Web3.keccak(text="ROLE_APP_GOVERNOR"), "big") & MASK_250 . bytes32 constant APP_GOVERNOR = bytes32( uint256(0xd2ead78c620e94b02d0a996e99298c59ddccfa1d8a0149080ac3a20de06068) ); // int.from_bytes(Web3.keccak(text="ROLE_APP_ROLE_ADMIN"), "big") & MASK_250 . bytes32 constant APP_ROLE_ADMIN = bytes32( uint256(0x03e615638e0b79444a70f8c695bf8f2a47033bf1cf95691ec3130f64939cee99) ); // int.from_bytes(Web3.keccak(text="ROLE_GOVERNANCE_ADMIN"), "big") & MASK_250 . bytes32 constant GOVERNANCE_ADMIN = bytes32( uint256(0x03711c9d994faf6055172091cb841fd4831aa743e6f3315163b06a122c841846) ); // int.from_bytes(Web3.keccak(text="ROLE_OPERATOR"), "big") & MASK_250 . bytes32 constant OPERATOR = bytes32( uint256(0x023edb77f7c8cc9e38e8afe78954f703aeeda7fffe014eeb6e56ea84e62f6da7) ); // int.from_bytes(Web3.keccak(text="ROLE_SECURITY_ADMIN"), "big") & MASK_250 . bytes32 constant SECURITY_ADMIN = bytes32( uint256(0x026bd110619d11cfdfc28e281df893bc24828e89177318e9dbd860cdaedeb6b3) ); // int.from_bytes(Web3.keccak(text="ROLE_SECURITY_AGENT"), "big") & MASK_250 . bytes32 constant SECURITY_AGENT = bytes32( uint256(0x037693ba312785932d430dccf0f56ffedd0aa7c0f8b6da2cc4530c2717689b96) ); // int.from_bytes(Web3.keccak(text="ROLE_TOKEN_ADMIN"), "big") & MASK_250 . bytes32 constant TOKEN_ADMIN = bytes32( uint256(0x0128d63adbf6b09002c26caf55c47e2f26635807e3ef1b027218aa74c8d61a3e) ); // int.from_bytes(Web3.keccak(text="ROLE_UPGRADE_GOVERNOR"), "big") & MASK_250 . bytes32 constant UPGRADE_GOVERNOR = bytes32( uint256(0x0251e864ca2a080f55bce5da2452e8cfcafdbc951a3e7fff5023d558452ec228) ); /* Role | Role Admin ---------------------------------------- GOVERNANCE_ADMIN | GOVERNANCE_ADMIN UPGRADE_GOVERNOR | GOVERNANCE_ADMIN APP_ROLE_ADMIN | GOVERNANCE_ADMIN APP_GOVERNOR | APP_ROLE_ADMIN OPERATOR | APP_ROLE_ADMIN TOKEN_ADMIN | APP_ROLE_ADMIN SECURITY_ADMIN | SECURITY_ADMIN SECURITY_AGENT | SECURITY_ADMIN . */ library RolesLib { // INITIALIZERS. function governanceRolesInitialized() internal view returns (bool) { return AccessControl.getRoleAdmin(GOVERNANCE_ADMIN) != bytes32(0x00); } function securityRolesInitialized() internal view returns (bool) { return AccessControl.getRoleAdmin(SECURITY_ADMIN) != bytes32(0x00); } function initialize() internal { address provisional = AccessControl._msgSender(); initialize(provisional, provisional); } function initialize(address provisionalGovernor, address provisionalSecAdmin) internal { initialize(provisionalGovernor, provisionalSecAdmin, false); } function initialize(address provisionalGovernor, address provisionalSecAdmin, bool subGovernors) internal { if (governanceRolesInitialized()) { // Support Proxied contract initialization. // In case the Proxy already initialized the roles, // init will succeed IFF the provisionalGovernor is already `GovernanceAdmin`. require( AccessControl.hasRole(GOVERNANCE_ADMIN, provisionalGovernor), "ROLES_ALREADY_INITIALIZED" ); } else { initGovernanceRoles(provisionalGovernor, subGovernors); } if (securityRolesInitialized()) { // If SecurityAdmin initialized, // then provisionalSecAdmin must already be a `SecurityAdmin`. // If it's not initilized - initialize it. require( AccessControl.hasRole(SECURITY_ADMIN, provisionalSecAdmin), "SECURITY_ROLES_ALREADY_INITIALIZED" ); } else { initSecurityRoles(provisionalSecAdmin); } } function initSecurityRoles(address provisionalSecAdmin) private { AccessControl._setRoleAdmin(SECURITY_ADMIN, SECURITY_ADMIN); AccessControl._setRoleAdmin(SECURITY_AGENT, SECURITY_ADMIN); AccessControl._grantRole(SECURITY_ADMIN, provisionalSecAdmin); } function initGovernanceRoles(address provisionalGovernor, bool subGovernors) private { AccessControl._grantRole(GOVERNANCE_ADMIN, provisionalGovernor); AccessControl._setRoleAdmin(APP_GOVERNOR, APP_ROLE_ADMIN); AccessControl._setRoleAdmin(APP_ROLE_ADMIN, GOVERNANCE_ADMIN); AccessControl._setRoleAdmin(GOVERNANCE_ADMIN, GOVERNANCE_ADMIN); AccessControl._setRoleAdmin(OPERATOR, APP_ROLE_ADMIN); AccessControl._setRoleAdmin(TOKEN_ADMIN, APP_ROLE_ADMIN); AccessControl._setRoleAdmin(UPGRADE_GOVERNOR, GOVERNANCE_ADMIN); \tif (subGovernors) { \t AccessControl._grantRole(APP_ROLE_ADMIN, provisionalGovernor); \t AccessControl._grantRole(GOVERNANCE_ADMIN, provisionalGovernor); \t AccessControl._grantRole(UPGRADE_GOVERNOR, provisionalGovernor); \t} } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.8.0; /** StorageSlots holds the arbitrary storage slots used throughout the Proxy pattern. Storage address slots are a mechanism to define an arbitrary location, that will not be overlapped by the logical contracts. */ contract StorageSlots { // Storage slot with the address of the current implementation. // We need to keep this variable stored outside of the commonly used space, // so that it's not overrun by the logical implementation (the proxied contract). // Web3.keccak(text="StarkWare2019.implemntation-slot"). bytes32 internal constant IMPLEMENTATION_SLOT = 0x177667240aeeea7e35eabe3a35e18306f336219e1386f7710a6bf8783f761b24; // Storage slot with the address of the call-proxy current implementation. // We need to keep this variable stored outside of the commonly used space. // so that it's not overrun by the logical implementation (the proxied contract). // Web3.keccak(text="StarkWare2020.CallProxy.Implemntation.Slot"). bytes32 internal constant CALL_PROXY_IMPL_SLOT = 0x7184681641399eb4ad2fdb92114857ee6ff239f94ad635a1779978947b8843be; // This storage slot stores the finalization flag. // Once the value stored in this slot is set to non-zero // the proxy blocks implementation upgrades. // The current implementation is then referred to as Finalized. // Web3.keccak(text="StarkWare2019.finalization-flag-slot"). bytes32 internal constant FINALIZED_STATE_SLOT = 0x7d433c6f837e8f93009937c466c82efbb5ba621fae36886d0cac433c5d0aa7d2; // Storage slot to hold the upgrade delay (time-lock). // The intention of this slot is to allow modification using an EIC. // Web3.keccak(text="StarkWare.Upgradibility.Delay.Slot"). bytes32 public constant UPGRADE_DELAY_SLOT = 0xc21dbb3089fcb2c4f4c6a67854ab4db2b0f233ea4b21b21f912d52d18fc5db1f; // Storage slot to hold the upgrade eanbled duration in seconds. // The intention of this slot is to allow modification using an EIC. // Web3.keccak(text="StarkWare.Upgradibility.EnableWindowDuration.Slot"). bytes32 public constant ENABLE_WINDOW_DURATION_SLOT = 0xb00a6109e73dbe7bbf8d3f18fb9221d2d024dc2671e3d5ff02532ccc40590738; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.7.0) (utils/Strings.sol) pragma solidity ^0.8.0; /** * @dev String operations. */ library Strings { bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef"; uint8 private constant _ADDRESS_LENGTH = 20; /** * @dev Converts a `uint256` to its ASCII `string` decimal representation. */ function toString(uint256 value) internal pure returns (string memory) { // Inspired by OraclizeAPI's implementation - MIT licence // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol if (value == 0) { return "0"; } uint256 temp = value; uint256 digits; while (temp != 0) { digits++; temp /= 10; } bytes memory buffer = new bytes(digits); while (value != 0) { digits -= 1; buffer[digits] = bytes1(uint8(48 + uint256(value % 10))); value /= 10; } return string(buffer); } /** * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation. */ function toHexString(uint256 value) internal pure returns (string memory) { if (value == 0) { return "0x00"; } uint256 temp = value; uint256 length = 0; while (temp != 0) { length++; temp >>= 8; } return toHexString(value, length); } /** * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length. */ function toHexString(uint256 value, uint256 length) internal pure returns (string memory) { bytes memory buffer = new bytes(2 * length + 2); buffer[0] = "0"; buffer[1] = "x"; for (uint256 i = 2 * length + 1; i > 1; --i) { buffer[i] = _HEX_SYMBOLS[value & 0xf]; value >>= 4; } require(value == 0, "Strings: hex length insufficient"); return string(buffer); } /** * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation. */ function toHexString(address addr) internal pure returns (string memory) { return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH); } }
File 2 of 8: StarkPerpetual
/* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; /* Common Utility Libraries. I. Addresses (extending address). */ library Addresses { /* Note: isContract function has some known limitation. See https://github.com/OpenZeppelin/ openzeppelin-contracts/blob/master/contracts/utils/Address.sol. */ function isContract(address account) internal view returns (bool) { uint256 size; assembly { size := extcodesize(account) } return size > 0; } function performEthTransfer(address recipient, uint256 amount) internal { if (amount == 0) return; (bool success, ) = recipient.call{value: amount}(""); // NOLINT: low-level-calls. require(success, "ETH_TRANSFER_FAILED"); } /* Safe wrapper around ERC20/ERC721 calls. This is required because many deployed ERC20 contracts don't return a value. See https://github.com/ethereum/solidity/issues/4116. */ function safeTokenContractCall(address tokenAddress, bytes memory callData) internal { require(isContract(tokenAddress), "BAD_TOKEN_ADDRESS"); // NOLINTNEXTLINE: low-level-calls. (bool success, bytes memory returndata) = tokenAddress.call(callData); require(success, string(returndata)); if (returndata.length > 0) { require(abi.decode(returndata, (bool)), "TOKEN_OPERATION_FAILED"); } } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; /* This contract provides means to block direct call of an external function. A derived contract (e.g. MainDispatcherBase) should decorate sensitive functions with the notCalledDirectly modifier, thereby preventing it from being called directly, and allowing only calling using delegate_call. */ abstract contract BlockDirectCall { address immutable this_; constructor() internal { this_ = address(this); } modifier notCalledDirectly() { require(this_ != address(this), "DIRECT_CALL_DISALLOWED"); _; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../interfaces/MGovernance.sol"; /* Implements Generic Governance, applicable for both proxy and main contract, and possibly others. Notes: The use of the same function names by both the Proxy and a delegated implementation is not possible since calling the implementation functions is done via the default function of the Proxy. For this reason, for example, the implementation of MainContract (MainGovernance) exposes mainIsGovernor, which calls the internal _isGovernor method. */ struct GovernanceInfoStruct { mapping(address => bool) effectiveGovernors; address candidateGovernor; bool initialized; } abstract contract Governance is MGovernance { event LogNominatedGovernor(address nominatedGovernor); event LogNewGovernorAccepted(address acceptedGovernor); event LogRemovedGovernor(address removedGovernor); event LogNominationCancelled(); function getGovernanceInfo() internal view virtual returns (GovernanceInfoStruct storage); /* Current code intentionally prevents governance re-initialization. This may be a problem in an upgrade situation, in a case that the upgrade-to implementation performs an initialization (for real) and within that calls initGovernance(). Possible workarounds: 1. Clearing the governance info altogether by changing the MAIN_GOVERNANCE_INFO_TAG. This will remove existing main governance information. 2. Modify the require part in this function, so that it will exit quietly when trying to re-initialize (uncomment the lines below). */ function initGovernance() internal { GovernanceInfoStruct storage gub = getGovernanceInfo(); require(!gub.initialized, "ALREADY_INITIALIZED"); gub.initialized = true; // to ensure acceptNewGovernor() won't fail. // Add the initial governer. acceptNewGovernor(msg.sender); } function _isGovernor(address user) internal view override returns (bool) { GovernanceInfoStruct storage gub = getGovernanceInfo(); return gub.effectiveGovernors[user]; } /* Cancels the nomination of a governor candidate. */ function _cancelNomination() internal onlyGovernance { GovernanceInfoStruct storage gub = getGovernanceInfo(); if (gub.candidateGovernor != address(0x0)) { gub.candidateGovernor = address(0x0); emit LogNominationCancelled(); } } function _nominateNewGovernor(address newGovernor) internal onlyGovernance { GovernanceInfoStruct storage gub = getGovernanceInfo(); require(newGovernor != address(0x0), "BAD_ADDRESS"); require(!_isGovernor(newGovernor), "ALREADY_GOVERNOR"); require(gub.candidateGovernor == address(0x0), "OTHER_CANDIDATE_PENDING"); gub.candidateGovernor = newGovernor; emit LogNominatedGovernor(newGovernor); } /* The acceptNewGovernor is called in two cases: 1. by _acceptGovernance when a new governor accepts its role. 2. by initGovernance to add the initial governor. The difference is that the init path skips the nominate step that would fail because of the onlyGovernance modifier. */ function acceptNewGovernor(address newGovernor) private { require(!_isGovernor(newGovernor), "ALREADY_GOVERNOR"); GovernanceInfoStruct storage gub = getGovernanceInfo(); gub.effectiveGovernors[newGovernor] = true; // Emit governance information. emit LogNewGovernorAccepted(newGovernor); } function _acceptGovernance() internal { // The new governor was proposed as a candidate by the current governor. GovernanceInfoStruct storage gub = getGovernanceInfo(); require(msg.sender == gub.candidateGovernor, "ONLY_CANDIDATE_GOVERNOR"); // Update state. acceptNewGovernor(msg.sender); gub.candidateGovernor = address(0x0); } /* Remove a governor from office. */ function _removeGovernor(address governorForRemoval) internal onlyGovernance { require(msg.sender != governorForRemoval, "GOVERNOR_SELF_REMOVE"); GovernanceInfoStruct storage gub = getGovernanceInfo(); require(_isGovernor(governorForRemoval), "NOT_GOVERNOR"); gub.effectiveGovernors[governorForRemoval] = false; emit LogRemovedGovernor(governorForRemoval); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import {GovernanceInfoStruct} from "./Governance.sol"; /* Holds the governance slots for ALL entities, including proxy and the main contract. */ contract GovernanceStorage { // A map from a Governor tag to its own GovernanceInfoStruct. mapping(string => GovernanceInfoStruct) internal governanceInfo; //NOLINT uninitialized-state. } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; /* Interface for a generic dispatcher to use, which the concrete dispatcher must implement. It contains the functions that are specific to the concrete dispatcher instance. The interface is implemented as contract, because interface implies all methods external. */ abstract contract IDispatcherBase { function getSubContract(bytes4 selector) public view virtual returns (address); function setSubContractAddress(uint256 index, address subContract) internal virtual; function getNumSubcontracts() internal pure virtual returns (uint256); function validateSubContractIndex(uint256 index, address subContract) internal pure virtual; /* Ensures initializer can be called. Reverts otherwise. */ function initializationSentinel() internal view virtual; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; interface Identity { /* Allows a caller to ensure that the provided address is of the expected type and version. */ function identify() external pure returns (string memory); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; abstract contract MGovernance { function _isGovernor(address user) internal view virtual returns (bool); /* Allows calling the function only by a Governor. */ modifier onlyGovernance() { require(_isGovernor(msg.sender), "ONLY_GOVERNANCE"); _; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../components/MainStorage.sol"; import "./MainDispatcherBase.sol"; abstract contract MainDispatcher is MainStorage, MainDispatcherBase { uint256 constant SUBCONTRACT_BITS = 4; function magicSalt() internal pure virtual returns (uint256); function handlerMapSection(uint256 section) internal pure virtual returns (uint256); function expectedIdByIndex(uint256 index) internal pure virtual returns (string memory id); function validateSubContractIndex(uint256 index, address subContract) internal pure override { string memory id = SubContractor(subContract).identify(); bytes32 hashed_expected_id = keccak256(abi.encodePacked(expectedIdByIndex(index))); require( hashed_expected_id == keccak256(abi.encodePacked(id)), "MISPLACED_INDEX_OR_BAD_CONTRACT_ID" ); // Gets the list of critical selectors from the sub-contract and checks that the selector // is mapped to that sub-contract. bytes4[] memory selectorsToValidate = SubContractor(subContract).validatedSelectors(); for (uint256 i = 0; i < selectorsToValidate.length; i++) { require( getSubContractIndex(selectorsToValidate[i]) == index, "INCONSISTENT_DISPATCHER_MAP" ); } } function handlingContractId(bytes4 selector) external pure virtual returns (string memory id) { uint256 index = getSubContractIndex(selector); return expectedIdByIndex(index); } /* Returns the index in subContracts where the address of the sub-contract implementing the function with the queried selector is held. Note: The nature of the sub-contracts handler map is such that all the required selectors are mapped. However, other selectors, such that are not implemented in any subcontract, may also return a sub-contract address. This behavior is by-design, and not a problem. */ function getSubContractIndex(bytes4 selector) internal pure returns (uint256) { uint256 location = 0xFF & uint256(keccak256(abi.encodePacked(selector, magicSalt()))); uint256 offset = (SUBCONTRACT_BITS * location) % 256; // We have 64 locations in each register, hence the >> 6 (i.e. location // 64). return (handlerMapSection(location >> 6) >> offset) & 0xF; } /* Returns the address of the sub-contract that would be delegated to handle a call with the queried selector. (see note above). */ function getSubContract(bytes4 selector) public view override returns (address) { return subContracts[getSubContractIndex(selector)]; } function setSubContractAddress(uint256 index, address subContractAddress) internal override { subContracts[index] = subContractAddress; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./SubContractor.sol"; import "./IDispatcherBase.sol"; import "../interfaces/BlockDirectCall.sol"; import "../libraries/Addresses.sol"; abstract contract MainDispatcherBase is IDispatcherBase, BlockDirectCall { using Addresses for address; /* This entry point serves only transactions with empty calldata. (i.e. pure value transfer tx). We don't expect to receive such, thus block them. */ receive() external payable { revert("CONTRACT_NOT_EXPECTED_TO_RECEIVE"); } fallback() external payable { address subContractAddress = getSubContract(msg.sig); require(subContractAddress != address(0x0), "NO_CONTRACT_FOR_FUNCTION"); assembly { // Copy msg.data. We take full control of memory in this inline assembly // block because it will not return to Solidity code. We overwrite the // Solidity scratch pad at memory position 0. calldatacopy(0, 0, calldatasize()) // Call the implementation. // out and outsize are 0 for now, as we don"t know the out size yet. let result := delegatecall(gas(), subContractAddress, 0, calldatasize(), 0, 0) // Copy the returned data. returndatacopy(0, 0, returndatasize()) switch result // delegatecall returns 0 on error. case 0 { revert(0, returndatasize()) } default { return(0, returndatasize()) } } } /* 1. Extract subcontracts. 2. Verify correct sub-contract initializer size. 3. Extract sub-contract initializer data. 4. Call sub-contract initializer. The init data bytes passed to initialize are structed as following: I. N slots (uin256 size) addresses of the deployed sub-contracts. II. An address of an external initialization contract (optional, or ZERO_ADDRESS). III. (Up to) N bytes sections of the sub-contracts initializers. If already initialized (i.e. upgrade) we expect the init data to be consistent with this. and if a different size of init data is expected when upgrading, the initializerSize should reflect this. If an external initializer contract is not used, ZERO_ADDRESS is passed in its slot. If the external initializer contract is used, all the remaining init data is passed to it, and internal initialization will not occur. External Initialization Contract -------------------------------- External Initialization Contract (EIC) is a hook for custom initialization. Typically in an upgrade flow, the expected initialization contains only the addresses of the sub-contracts. Normal initialization of the sub-contracts is such that is not needed in an upgrade, and actually may be very dangerous, as changing of state on a working system may corrupt it. In the event that some state initialization is required, the EIC is a hook that allows this. It may be deployed and called specifically for this purpose. The address of the EIC must be provided (if at all) when a new implementation is added to a Proxy contract (as part of the initialization vector). Hence, it is considered part of the code open to reviewers prior to a time-locked upgrade. When a custom initialization is performed using an EIC, the main dispatcher initialize extracts and stores the sub-contracts addresses, and then yields to the EIC, skipping the rest of its initialization code. Flow of MainDispatcher initialize --------------------------------- 1. Extraction and assignment of subcontracts addresses Main dispatcher expects a valid and consistent set of addresses in the passed data. It validates that, extracts the addresses from the data, and validates that the addresses are of the expected type and order. Then those addresses are stored. 2. Extraction of EIC address The address of the EIC is extracted from the data. External Initializer Contract is optional. ZERO_ADDRESS indicates it is not used. 3a. EIC is used Dispatcher calls the EIC initialize function with the remaining data. Note - In this option 3b is not performed. 3b. EIC is not used If there is additional initialization data then: I. Sentitenl function is called to permit subcontracts initialization. II. Dispatcher loops through the subcontracts and for each one it extracts the initializing data and passes it to the subcontract's initialize function. */ function initialize(bytes calldata data) external virtual notCalledDirectly { // Number of sub-contracts. uint256 nSubContracts = getNumSubcontracts(); // We support currently 4 bits per contract, i.e. 16, reserving 00 leads to 15. require(nSubContracts <= 15, "TOO_MANY_SUB_CONTRACTS"); // Sum of subcontract initializers. Aggregated for verification near the end. uint256 totalInitSizes = 0; // Offset (within data) of sub-contract initializer vector. // Just past the sub-contract+eic addresses. uint256 initDataContractsOffset = 32 * (nSubContracts + 1); // Init data MUST include addresses for all sub-contracts + EIC. require(data.length >= initDataContractsOffset, "SUB_CONTRACTS_NOT_PROVIDED"); // Size of passed data, excluding sub-contract addresses. uint256 additionalDataSize = data.length - initDataContractsOffset; // Extract & update contract addresses. for (uint256 nContract = 1; nContract <= nSubContracts; nContract++) { // Extract sub-contract address. address contractAddress = abi.decode( data[32 * (nContract - 1):32 * nContract], (address) ); validateSubContractIndex(nContract, contractAddress); // Contracts are indexed from 1 and 0 is not in use here. setSubContractAddress(nContract, contractAddress); } // Check if we have an external initializer contract. address externalInitializerAddr = abi.decode( data[initDataContractsOffset - 32:initDataContractsOffset], (address) ); // 3(a). Yield to EIC initialization. if (externalInitializerAddr != address(0x0)) { callExternalInitializer(externalInitializerAddr, data[initDataContractsOffset:]); return; } // 3(b). Subcontracts initialization. // I. If no init data passed besides sub-contracts, return. if (additionalDataSize == 0) { return; } // Just to be on the safe side. assert(externalInitializerAddr == address(0x0)); // II. Gate further initialization. initializationSentinel(); // III. Loops through the subcontracts, extracts their data and calls their initializer. for (uint256 nContract = 1; nContract <= nSubContracts; nContract++) { // Extract sub-contract address. address contractAddress = abi.decode( data[32 * (nContract - 1):32 * nContract], (address) ); // The initializerSize is called via delegatecall, so that it can relate to the state, // and not only to the new contract code. (e.g. return 0 if state-intialized else 192). // NOLINTNEXTLINE: controlled-delegatecall low-level-calls calls-loop. (bool success, bytes memory returndata) = contractAddress.delegatecall( abi.encodeWithSelector(SubContractor(contractAddress).initializerSize.selector) ); require(success, string(returndata)); uint256 initSize = abi.decode(returndata, (uint256)); require(initSize <= additionalDataSize, "INVALID_INITIALIZER_SIZE"); require(totalInitSizes + initSize <= additionalDataSize, "INVALID_INITIALIZER_SIZE"); if (initSize == 0) { continue; } // Call sub-contract initializer. // NOLINTNEXTLINE: controlled-delegatecall calls-loop. (success, returndata) = contractAddress.delegatecall( abi.encodeWithSelector( this.initialize.selector, data[initDataContractsOffset:initDataContractsOffset + initSize] ) ); require(success, string(returndata)); totalInitSizes += initSize; initDataContractsOffset += initSize; } require(additionalDataSize == totalInitSizes, "MISMATCHING_INIT_DATA_SIZE"); } function callExternalInitializer(address externalInitializerAddr, bytes calldata extInitData) private { require(externalInitializerAddr.isContract(), "NOT_A_CONTRACT"); // NOLINTNEXTLINE: low-level-calls, controlled-delegatecall. (bool success, bytes memory returndata) = externalInitializerAddr.delegatecall( abi.encodeWithSelector(this.initialize.selector, extInitData) ); require(success, string(returndata)); require(returndata.length == 0, string(returndata)); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../upgrade/ProxyStorage.sol"; import "../libraries/Addresses.sol"; import {ApprovalChainData} from "../libraries/StarkExTypes.sol"; /* Holds ALL the main contract state (storage) variables. */ contract MainStorage is ProxyStorage { uint256 internal constant LAYOUT_LENGTH = 2**64; address escapeVerifierAddress; // NOLINT: constable-states. // Global dex-frozen flag. bool stateFrozen; // NOLINT: constable-states. // Time when unFreeze can be successfully called (UNFREEZE_DELAY after freeze). uint256 unFreezeTime; // NOLINT: constable-states. // Pending deposits. // A map STARK key => asset id => vault id => quantized amount. mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) pendingDeposits; // Cancellation requests. // A map STARK key => asset id => vault id => request timestamp. mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) cancellationRequests; // Pending withdrawals. // A map STARK key => asset id => quantized amount. mapping(uint256 => mapping(uint256 => uint256)) pendingWithdrawals; // vault_id => escape used boolean. mapping(uint256 => bool) escapesUsed; // Number of escapes that were performed when frozen. uint256 escapesUsedCount; // NOLINT: constable-states. // NOTE: fullWithdrawalRequests is deprecated, and replaced by forcedActionRequests. // NOLINTNEXTLINE naming-convention. mapping(uint256 => mapping(uint256 => uint256)) fullWithdrawalRequests_DEPRECATED; // State sequence number. uint256 sequenceNumber; // NOLINT: constable-states uninitialized-state. // Validium Vaults Tree Root & Height. uint256 validiumVaultRoot; // NOLINT: constable-states uninitialized-state. uint256 validiumTreeHeight; // NOLINT: constable-states uninitialized-state. // Order Tree Root & Height. uint256 orderRoot; // NOLINT: constable-states uninitialized-state. uint256 orderTreeHeight; // NOLINT: constable-states uninitialized-state. // True if and only if the address is allowed to add tokens. mapping(address => bool) tokenAdmins; // This mapping is no longer in use, remains for backwards compatibility. mapping(address => bool) userAdmins_DEPRECATED; // NOLINT: naming-convention. // True if and only if the address is an operator (allowed to update state). mapping(address => bool) operators; // NOLINT: uninitialized-state. // Mapping of contract ID to asset data. mapping(uint256 => bytes) assetTypeToAssetInfo; // NOLINT: uninitialized-state. // Mapping of registered contract IDs. mapping(uint256 => bool) registeredAssetType; // NOLINT: uninitialized-state. // Mapping from contract ID to quantum. mapping(uint256 => uint256) assetTypeToQuantum; // NOLINT: uninitialized-state. // This mapping is no longer in use, remains for backwards compatibility. mapping(address => uint256) starkKeys_DEPRECATED; // NOLINT: naming-convention. // Mapping from STARK public key to the Ethereum public key of its owner. mapping(uint256 => address) ethKeys; // NOLINT: uninitialized-state. // Timelocked state transition and availability verification chain. ApprovalChainData verifiersChain; ApprovalChainData availabilityVerifiersChain; // Batch id of last accepted proof. uint256 lastBatchId; // NOLINT: constable-states uninitialized-state. // Mapping between sub-contract index to sub-contract address. mapping(uint256 => address) subContracts; // NOLINT: uninitialized-state. mapping(uint256 => bool) permissiveAssetType_DEPRECATED; // NOLINT: naming-convention. // ---- END OF MAIN STORAGE AS DEPLOYED IN STARKEX2.0 ---- // Onchain-data version configured for the system. uint256 onchainDataVersion_DEPRECATED; // NOLINT: naming-convention constable-states. // Counter of forced action request in block. The key is the block number. mapping(uint256 => uint256) forcedRequestsInBlock; // ForcedAction requests: actionHash => requestTime. mapping(bytes32 => uint256) forcedActionRequests; // Mapping for timelocked actions. // A actionKey => activation time. mapping(bytes32 => uint256) actionsTimeLock; // Append only list of requested forced action hashes. bytes32[] actionHashList; // ---- END OF MAIN STORAGE AS DEPLOYED IN STARKEX3.0 ---- // ---- END OF MAIN STORAGE AS DEPLOYED IN STARKEX4.0 ---- // Rollup Vaults Tree Root & Height. uint256 rollupVaultRoot; // NOLINT: constable-states uninitialized-state. uint256 rollupTreeHeight; // NOLINT: constable-states uninitialized-state. uint256 globalConfigCode; // NOLINT: constable-states uninitialized-state. // Reserved storage space for Extensibility. // Every added MUST be added above the end gap, and the __endGap size must be reduced // accordingly. // NOLINTNEXTLINE: naming-convention. uint256[LAYOUT_LENGTH - 40] private __endGap; // __endGap complements layout to LAYOUT_LENGTH. } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../../components/MainStorage.sol"; /* Extends MainStorage, holds Perpetual App specific state (storage) variables. ALL State variables that are common to all applications, reside in MainStorage, whereas ALL the Perpetual app specific ones reside here. */ contract PerpetualStorage is MainStorage { uint256 systemAssetType; // NOLINT: constable-states uninitialized-state. bytes32 public globalConfigurationHash; // NOLINT: constable-states uninitialized-state. mapping(uint256 => bytes32) public configurationHash; // NOLINT: uninitialized-state. bytes32 sharedStateHash; // NOLINT: constable-states uninitialized-state. // Configuration apply time-lock. // The delay is held in storage (and not constant) // So that it can be modified during upgrade. uint256 public configurationDelay; // NOLINT: constable-states. // Reserved storage space for Extensibility. // Every added MUST be added above the end gap, and the __endGap size must be reduced // accordingly. // NOLINTNEXTLINE: naming-convention shadowing-abstract. uint256[LAYOUT_LENGTH - 5] private __endGap; // __endGap complements layout to LAYOUT_LENGTH. } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../components/GovernanceStorage.sol"; /* Holds the Proxy-specific state variables. This contract is inherited by the GovernanceStorage (and indirectly by MainStorage) to prevent collision hazard. */ contract ProxyStorage is GovernanceStorage { // NOLINTNEXTLINE: naming-convention uninitialized-state. mapping(address => bytes32) internal initializationHash_DEPRECATED; // The time after which we can switch to the implementation. // Hash(implementation, data, finalize) => time. mapping(bytes32 => uint256) internal enabledTime; // A central storage of the flags whether implementation has been initialized. // Note - it can be used flexibly enough to accommodate multiple levels of initialization // (i.e. using different key salting schemes for different initialization levels). mapping(bytes32 => bool) internal initialized; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; // Structure representing a list of verifiers (validity/availability). // A statement is valid only if all the verifiers in the list agree on it. // Adding a verifier to the list is immediate - this is used for fast resolution of // any soundness issues. // Removing a verifier from the list is time-locked, to ensure that any user of the system // not content with the announced removal has ample time to leave the system before it is // removed. struct ApprovalChainData { address[] verifiers; // Represents the time after which the verifier with the given address can be removed. // Removal of the verifier with address A is allowed only in the case the value // of verifierAllowedRemovalTime[A] != 0 and verifierAllowedRemovalTime[A] < (current time). mapping(address => uint256) verifierAllowedRemovalTime; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../interfaces/MainDispatcher.sol"; import "./components/PerpetualStorage.sol"; contract StarkPerpetual is MainDispatcher, PerpetualStorage { string public constant VERSION = "3.2.0"; // Salt for a 8 bit unique spread of all relevant selectors. Pre-caclulated. // ---------- The following code was auto-generated. PLEASE DO NOT EDIT. ---------- uint256 constant MAGIC_SALT = 349548; uint256 constant IDX_MAP_0 = 0x2124100000000202022040002000000020022010011100002003000001000000; uint256 constant IDX_MAP_1 = 0x10003203010020002002200030330000100020300002001003030000100202; uint256 constant IDX_MAP_2 = 0x3000002000310000040022000010000000100013430002033100300200000302; uint256 constant IDX_MAP_3 = 0x1103103010020000102000000400320031030100023000004000300000000000; // ---------- End of auto-generated code. ---------- function getNumSubcontracts() internal pure override returns (uint256) { return 4; } function magicSalt() internal pure override returns (uint256) { return MAGIC_SALT; } function handlerMapSection(uint256 section) internal pure override returns (uint256) { if (section == 0) { return IDX_MAP_0; } else if (section == 1) { return IDX_MAP_1; } else if (section == 2) { return IDX_MAP_2; } else if (section == 3) { return IDX_MAP_3; } revert("BAD_IDX_MAP_SECTION"); } function expectedIdByIndex(uint256 index) internal pure override returns (string memory id) { if (index == 1) { id = "StarkWare_AllVerifiers_2022_2"; } else if (index == 2) { id = "StarkWare_PerpetualTokensAndRamping_2022_2"; } else if (index == 3) { id = "StarkWare_PerpetualState_2023_4"; } else if (index == 4) { id = "StarkWare_PerpetualForcedActions_2022_2"; } else { revert("UNEXPECTED_INDEX"); } } function initializationSentinel() internal view override { string memory REVERT_MSG = "INITIALIZATION_BLOCKED"; // This initializer sets state etc. It must not be applied twice. // I.e. it can run only when the state is still empty. require(int256(sharedStateHash) == 0, REVERT_MSG); require(int256(globalConfigurationHash) == 0, REVERT_MSG); require(systemAssetType == 0, REVERT_MSG); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./Identity.sol"; interface SubContractor is Identity { function initialize(bytes calldata data) external; function initializerSize() external view returns (uint256); /* Returns an array with selectors for validation. These selectors are the critical ones for maintaining self custody and anti censorship. During the upgrade process, as part of the sub-contract validation, the MainDispatcher validates that the selectos are mapped to the correct sub-contract. */ function validatedSelectors() external pure returns (bytes4[] memory); }
File 3 of 8: PerpetualState
/* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../libraries/LibConstants.sol"; import "../interfaces/MAcceptModifications.sol"; import "../interfaces/MTokenQuantization.sol"; import "../components/MainStorage.sol"; /* Interface containing actions a verifier can invoke on the state. The contract containing the state should implement these and verify correctness. */ abstract contract AcceptModifications is MainStorage, LibConstants, MAcceptModifications, MTokenQuantization { event LogWithdrawalAllowed( uint256 ownerKey, uint256 assetType, uint256 nonQuantizedAmount, uint256 quantizedAmount ); event LogNftWithdrawalAllowed(uint256 ownerKey, uint256 assetId); event LogAssetWithdrawalAllowed(uint256 ownerKey, uint256 assetId, uint256 quantizedAmount); event LogMintableWithdrawalAllowed(uint256 ownerKey, uint256 assetId, uint256 quantizedAmount); /* Transfers funds from the on-chain deposit area to the off-chain area. Implemented in the Deposits contracts. */ function acceptDeposit( uint256 ownerKey, uint256 vaultId, uint256 assetId, uint256 quantizedAmount ) internal virtual override { // Fetch deposit. require( pendingDeposits[ownerKey][assetId][vaultId] >= quantizedAmount, "DEPOSIT_INSUFFICIENT" ); // Subtract accepted quantized amount. pendingDeposits[ownerKey][assetId][vaultId] -= quantizedAmount; } /* Transfers funds from the off-chain area to the on-chain withdrawal area. */ function allowWithdrawal( uint256 ownerKey, uint256 assetId, uint256 quantizedAmount ) internal override { // Fetch withdrawal. uint256 withdrawal = pendingWithdrawals[ownerKey][assetId]; // Add accepted quantized amount. withdrawal += quantizedAmount; require(withdrawal >= quantizedAmount, "WITHDRAWAL_OVERFLOW"); // Store withdrawal. pendingWithdrawals[ownerKey][assetId] = withdrawal; // Log event. uint256 presumedAssetType = assetId; if (registeredAssetType[presumedAssetType]) { emit LogWithdrawalAllowed( ownerKey, presumedAssetType, fromQuantized(presumedAssetType, quantizedAmount), quantizedAmount ); } else if ( assetId == ((assetId & (MASK_240 | NON_UNIQUE_MINTABLE_ASSET_ID_FLAG | MINTABLE_ERC20_ASSET_ID_FLAG)) | MINTABLE_ASSET_ID_FLAG) ) { emit LogMintableWithdrawalAllowed(ownerKey, assetId, quantizedAmount); } else { // Default case is Non-Mintable ERC721 or ERC1155 asset id. // In ERC721 and ERC1155 cases, assetId is not the assetType. require(assetId == assetId & MASK_250, "INVALID_ASSET_ID"); // If withdrawal amount is 1, the asset could be either NFT or SFT. In that case, both // NFT and general events will be emitted so that the listened for event is captured. // When withdrawal is greater than 1, it must be SFT and only one event will be emitted. if (withdrawal <= 1) { emit LogNftWithdrawalAllowed(ownerKey, assetId); } emit LogAssetWithdrawalAllowed(ownerKey, assetId, quantizedAmount); } } // Verifier authorizes withdrawal. function acceptWithdrawal( uint256 ownerKey, uint256 assetId, uint256 quantizedAmount ) internal virtual override { allowWithdrawal(ownerKey, assetId, quantizedAmount); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../components/MainStorage.sol"; import "../libraries/LibConstants.sol"; /* Calculation action hash for the various forced actions in a generic manner. */ contract ActionHash is MainStorage, LibConstants { function getActionHash(string memory actionName, bytes memory packedActionParameters) internal pure returns (bytes32 actionHash) { actionHash = keccak256(abi.encodePacked(actionName, packedActionParameters)); } function setActionHash(bytes32 actionHash, bool premiumCost) internal { // The rate of forced trade requests is restricted. // First restriction is by capping the number of requests in a block. // User can override this cap by requesting with a permium flag set, // in this case, the gas cost is high (~1M) but no "technical" limit is set. // However, the high gas cost creates an obvious limitation due to the block gas limit. if (premiumCost) { for (uint256 i = 0; i < 21129; i++) {} } else { require( forcedRequestsInBlock[block.number] < MAX_FORCED_ACTIONS_REQS_PER_BLOCK, "MAX_REQUESTS_PER_BLOCK_REACHED" ); forcedRequestsInBlock[block.number] += 1; } forcedActionRequests[actionHash] = block.timestamp; actionHashList.push(actionHash); } function getActionCount() external view returns (uint256) { return actionHashList.length; } function getActionHashByIndex(uint256 actionIndex) external view returns (bytes32) { require(actionIndex < actionHashList.length, "ACTION_INDEX_TOO_HIGH"); return actionHashList[actionIndex]; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; /* Common Utility Libraries. I. Addresses (extending address). */ library Addresses { /* Note: isContract function has some known limitation. See https://github.com/OpenZeppelin/ openzeppelin-contracts/blob/master/contracts/utils/Address.sol. */ function isContract(address account) internal view returns (bool) { uint256 size; assembly { size := extcodesize(account) } return size > 0; } function performEthTransfer(address recipient, uint256 amount) internal { if (amount == 0) return; (bool success, ) = recipient.call{value: amount}(""); // NOLINT: low-level-calls. require(success, "ETH_TRANSFER_FAILED"); } /* Safe wrapper around ERC20/ERC721 calls. This is required because many deployed ERC20 contracts don't return a value. See https://github.com/ethereum/solidity/issues/4116. */ function safeTokenContractCall(address tokenAddress, bytes memory callData) internal { require(isContract(tokenAddress), "BAD_TOKEN_ADDRESS"); // NOLINTNEXTLINE: low-level-calls. (bool success, bytes memory returndata) = tokenAddress.call(callData); require(success, string(returndata)); if (returndata.length > 0) { require(abi.decode(returndata, (bool)), "TOKEN_OPERATION_FAILED"); } } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../PerpetualConstants.sol"; import "../components/PerpetualStorage.sol"; import "../../interfaces/MGovernance.sol"; /** Configuration contract facilitates storing system configuration hashes. A configuration item hash can be stored only once, and cannot be altered or removed. If there is a need for a configuration change (not addition of new one), it shall be performed via upgrade using a dedicated External Initializing Contract (EIC). */ abstract contract Configuration is PerpetualStorage, PerpetualConstants, MGovernance { // This key is used in for the actionsTimeLock. uint256 constant GLOBAL_CONFIG_KEY = uint256(~0); event LogGlobalConfigurationRegistered(bytes32 configHash); event LogGlobalConfigurationApplied(bytes32 configHash); event LogGlobalConfigurationRemoved(bytes32 configHash); event LogAssetConfigurationRegistered(uint256 assetId, bytes32 configHash); event LogAssetConfigurationApplied(uint256 assetId, bytes32 configHash); event LogAssetConfigurationRemoved(uint256 assetId, bytes32 configHash); /* Configuration delay is set during initialization. It is designed to be changed only through upgrade cycle, by altering the storage variable. */ function initialize(uint256 delay) internal { configurationDelay = delay; } /* Register global configuration hash, for applying once configuration delay time-lock expires. */ function registerGlobalConfigurationChange(bytes32 configHash) external onlyGovernance { require(uint256(configHash) < K_MODULUS, "INVALID_CONFIG_HASH"); bytes32 actionKey = keccak256(abi.encodePacked(GLOBAL_CONFIG_KEY, configHash)); actionsTimeLock[actionKey] = block.timestamp + configurationDelay; emit LogGlobalConfigurationRegistered(configHash); } /* Applies global configuration hash. */ function applyGlobalConfigurationChange(bytes32 configHash) external onlyGovernance { bytes32 actionKey = keccak256(abi.encode(GLOBAL_CONFIG_KEY, configHash)); uint256 activationTime = actionsTimeLock[actionKey]; require(activationTime > 0, "CONFIGURATION_NOT_REGSITERED"); require(activationTime <= block.timestamp, "CONFIGURATION_NOT_ENABLE_YET"); globalConfigurationHash = configHash; emit LogGlobalConfigurationApplied(configHash); } function removeGlobalConfigurationChange(bytes32 configHash) external onlyGovernance { bytes32 actionKey = keccak256(abi.encodePacked(GLOBAL_CONFIG_KEY, configHash)); require(actionsTimeLock[actionKey] > 0, "CONFIGURATION_NOT_REGSITERED"); delete actionsTimeLock[actionKey]; emit LogGlobalConfigurationRemoved(configHash); } /* Register an asset configuration hash, for applying once configuration delay time-lock expires. */ function registerAssetConfigurationChange(uint256 assetId, bytes32 configHash) external onlyGovernance { require(assetId < PERPETUAL_ASSET_ID_UPPER_BOUND, "INVALID_ASSET_ID"); require(uint256(configHash) < K_MODULUS, "INVALID_CONFIG_HASH"); bytes32 actionKey = keccak256(abi.encode(assetId, configHash)); actionsTimeLock[actionKey] = block.timestamp + configurationDelay; emit LogAssetConfigurationRegistered(assetId, configHash); } /* Applies asset configuration hash. */ function applyAssetConfigurationChange(uint256 assetId, bytes32 configHash) external onlyGovernance { bytes32 actionKey = keccak256(abi.encode(assetId, configHash)); uint256 activationTime = actionsTimeLock[actionKey]; require(activationTime > 0, "CONFIGURATION_NOT_REGSITERED"); require(activationTime <= block.timestamp, "CONFIGURATION_NOT_ENABLE_YET"); configurationHash[assetId] = configHash; emit LogAssetConfigurationApplied(assetId, configHash); } function removeAssetConfigurationChange(uint256 assetId, bytes32 configHash) external onlyGovernance { bytes32 actionKey = keccak256(abi.encode(assetId, configHash)); require(actionsTimeLock[actionKey] > 0, "CONFIGURATION_NOT_REGSITERED"); delete actionsTimeLock[actionKey]; emit LogAssetConfigurationRemoved(assetId, configHash); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../components/PerpetualStorage.sol"; import "../interfaces/MForcedTradeActionState.sol"; import "../../components/ActionHash.sol"; /* ForcedTrade specific action hashses. */ contract ForcedTradeActionState is PerpetualStorage, ActionHash, MForcedTradeActionState { function forcedTradeActionHash( uint256 starkKeyA, uint256 starkKeyB, uint256 vaultIdA, uint256 vaultIdB, uint256 collateralAssetId, uint256 syntheticAssetId, uint256 amountCollateral, uint256 amountSynthetic, bool aIsBuyingSynthetic, uint256 nonce ) internal pure override returns (bytes32) { return getActionHash( "FORCED_TRADE", abi.encodePacked( starkKeyA, starkKeyB, vaultIdA, vaultIdB, collateralAssetId, syntheticAssetId, amountCollateral, amountSynthetic, aIsBuyingSynthetic, nonce ) ); } function clearForcedTradeRequest( uint256 starkKeyA, uint256 starkKeyB, uint256 vaultIdA, uint256 vaultIdB, uint256 collateralAssetId, uint256 syntheticAssetId, uint256 amountCollateral, uint256 amountSynthetic, bool aIsBuyingSynthetic, uint256 nonce ) internal override { /* We don't clear the entry, but set the time to max so that it cannot be replayed. */ bytes32 actionHash = forcedTradeActionHash( starkKeyA, starkKeyB, vaultIdA, vaultIdB, collateralAssetId, syntheticAssetId, amountCollateral, amountSynthetic, aIsBuyingSynthetic, nonce ); // A cleared ForcedTrade action is marked with ~0 and not zero, to prevent party A from // replaying the trade without a new signature from party B. require(forcedActionRequests[actionHash] != uint256(~0), "ACTION_ALREADY_CLEARED"); require(forcedActionRequests[actionHash] != 0, "NON_EXISTING_ACTION"); forcedActionRequests[actionHash] = uint256(~0); } function getForcedTradeRequest( uint256 starkKeyA, uint256 starkKeyB, uint256 vaultIdA, uint256 vaultIdB, uint256 collateralAssetId, uint256 syntheticAssetId, uint256 amountCollateral, uint256 amountSynthetic, bool aIsBuyingSynthetic, uint256 nonce ) public view override returns (uint256) { return forcedActionRequests[ forcedTradeActionHash( starkKeyA, starkKeyB, vaultIdA, vaultIdB, collateralAssetId, syntheticAssetId, amountCollateral, amountSynthetic, aIsBuyingSynthetic, nonce ) ]; } function setForcedTradeRequest( uint256 starkKeyA, uint256 starkKeyB, uint256 vaultIdA, uint256 vaultIdB, uint256 collateralAssetId, uint256 syntheticAssetId, uint256 amountCollateral, uint256 amountSynthetic, bool aIsBuyingSynthetic, uint256 nonce, bool premiumCost ) internal override { bytes32 actionHash = forcedTradeActionHash( starkKeyA, starkKeyB, vaultIdA, vaultIdB, collateralAssetId, syntheticAssetId, amountCollateral, amountSynthetic, aIsBuyingSynthetic, nonce ); // NOLINTNEXTLINE: timestamp. require(forcedActionRequests[actionHash] == 0, "FORCED_TRADE_REPLAYED"); setActionHash(actionHash, premiumCost); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../components/PerpetualStorage.sol"; import "../interfaces/MForcedWithdrawalActionState.sol"; import "../../components/ActionHash.sol"; /* ForcedWithdrawal specific action hashses. */ contract ForcedWithdrawalActionState is PerpetualStorage, ActionHash, MForcedWithdrawalActionState { function forcedWithdrawActionHash( uint256 starkKey, uint256 vaultId, uint256 quantizedAmount ) internal pure override returns (bytes32) { return getActionHash("FORCED_WITHDRAWAL", abi.encode(starkKey, vaultId, quantizedAmount)); } function clearForcedWithdrawalRequest( uint256 starkKey, uint256 vaultId, uint256 quantizedAmount ) internal override { bytes32 actionHash = forcedWithdrawActionHash(starkKey, vaultId, quantizedAmount); require(forcedActionRequests[actionHash] != 0, "NON_EXISTING_ACTION"); delete forcedActionRequests[actionHash]; } function getForcedWithdrawalRequest( uint256 starkKey, uint256 vaultId, uint256 quantizedAmount ) public view override returns (uint256) { // Return request value. Expect zero if the request doesn't exist or has been serviced, and // a non-zero value otherwise. return forcedActionRequests[forcedWithdrawActionHash(starkKey, vaultId, quantizedAmount)]; } function setForcedWithdrawalRequest( uint256 starkKey, uint256 vaultId, uint256 quantizedAmount, bool premiumCost ) internal override { setActionHash(forcedWithdrawActionHash(starkKey, vaultId, quantizedAmount), premiumCost); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../libraries/LibConstants.sol"; import "../interfaces/MFreezable.sol"; import "../interfaces/MGovernance.sol"; import "./MainStorage.sol"; /* Implements MFreezable. */ abstract contract Freezable is MainStorage, LibConstants, MGovernance, MFreezable { event LogFrozen(); event LogUnFrozen(); function isFrozen() public view override returns (bool) { return stateFrozen; } function validateFreezeRequest(uint256 requestTime) internal override { require(requestTime != 0, "FORCED_ACTION_UNREQUESTED"); // Verify timer on escape request. uint256 freezeTime = requestTime + FREEZE_GRACE_PERIOD; // Prevent wraparound. assert(freezeTime >= FREEZE_GRACE_PERIOD); require(block.timestamp >= freezeTime, "FORCED_ACTION_PENDING"); // NOLINT: timestamp. // Forced action requests placed before freeze, are no longer valid after the un-freeze. require(freezeTime > unFreezeTime, "REFREEZE_ATTEMPT"); } function freeze() internal override notFrozen { unFreezeTime = block.timestamp + UNFREEZE_DELAY; // Update state. stateFrozen = true; // Log event. emit LogFrozen(); } function unFreeze() external onlyFrozen onlyGovernance { require(block.timestamp >= unFreezeTime, "UNFREEZE_NOT_ALLOWED_YET"); // Update state. stateFrozen = false; // Increment roots to invalidate them, w/o losing information. validiumVaultRoot += 1; rollupVaultRoot += 1; orderRoot += 1; // Log event. emit LogUnFrozen(); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../interfaces/MGovernance.sol"; /* Implements Generic Governance, applicable for both proxy and main contract, and possibly others. Notes: The use of the same function names by both the Proxy and a delegated implementation is not possible since calling the implementation functions is done via the default function of the Proxy. For this reason, for example, the implementation of MainContract (MainGovernance) exposes mainIsGovernor, which calls the internal _isGovernor method. */ struct GovernanceInfoStruct { mapping(address => bool) effectiveGovernors; address candidateGovernor; bool initialized; } abstract contract Governance is MGovernance { event LogNominatedGovernor(address nominatedGovernor); event LogNewGovernorAccepted(address acceptedGovernor); event LogRemovedGovernor(address removedGovernor); event LogNominationCancelled(); function getGovernanceInfo() internal view virtual returns (GovernanceInfoStruct storage); /* Current code intentionally prevents governance re-initialization. This may be a problem in an upgrade situation, in a case that the upgrade-to implementation performs an initialization (for real) and within that calls initGovernance(). Possible workarounds: 1. Clearing the governance info altogether by changing the MAIN_GOVERNANCE_INFO_TAG. This will remove existing main governance information. 2. Modify the require part in this function, so that it will exit quietly when trying to re-initialize (uncomment the lines below). */ function initGovernance() internal { GovernanceInfoStruct storage gub = getGovernanceInfo(); require(!gub.initialized, "ALREADY_INITIALIZED"); gub.initialized = true; // to ensure acceptNewGovernor() won't fail. // Add the initial governer. acceptNewGovernor(msg.sender); } function _isGovernor(address user) internal view override returns (bool) { GovernanceInfoStruct storage gub = getGovernanceInfo(); return gub.effectiveGovernors[user]; } /* Cancels the nomination of a governor candidate. */ function _cancelNomination() internal onlyGovernance { GovernanceInfoStruct storage gub = getGovernanceInfo(); if (gub.candidateGovernor != address(0x0)) { gub.candidateGovernor = address(0x0); emit LogNominationCancelled(); } } function _nominateNewGovernor(address newGovernor) internal onlyGovernance { GovernanceInfoStruct storage gub = getGovernanceInfo(); require(newGovernor != address(0x0), "BAD_ADDRESS"); require(!_isGovernor(newGovernor), "ALREADY_GOVERNOR"); require(gub.candidateGovernor == address(0x0), "OTHER_CANDIDATE_PENDING"); gub.candidateGovernor = newGovernor; emit LogNominatedGovernor(newGovernor); } /* The acceptNewGovernor is called in two cases: 1. by _acceptGovernance when a new governor accepts its role. 2. by initGovernance to add the initial governor. The difference is that the init path skips the nominate step that would fail because of the onlyGovernance modifier. */ function acceptNewGovernor(address newGovernor) private { require(!_isGovernor(newGovernor), "ALREADY_GOVERNOR"); GovernanceInfoStruct storage gub = getGovernanceInfo(); gub.effectiveGovernors[newGovernor] = true; // Emit governance information. emit LogNewGovernorAccepted(newGovernor); } function _acceptGovernance() internal { // The new governor was proposed as a candidate by the current governor. GovernanceInfoStruct storage gub = getGovernanceInfo(); require(msg.sender == gub.candidateGovernor, "ONLY_CANDIDATE_GOVERNOR"); // Update state. acceptNewGovernor(msg.sender); gub.candidateGovernor = address(0x0); } /* Remove a governor from office. */ function _removeGovernor(address governorForRemoval) internal onlyGovernance { require(msg.sender != governorForRemoval, "GOVERNOR_SELF_REMOVE"); GovernanceInfoStruct storage gub = getGovernanceInfo(); require(_isGovernor(governorForRemoval), "NOT_GOVERNOR"); gub.effectiveGovernors[governorForRemoval] = false; emit LogRemovedGovernor(governorForRemoval); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import {GovernanceInfoStruct} from "./Governance.sol"; /* Holds the governance slots for ALL entities, including proxy and the main contract. */ contract GovernanceStorage { // A map from a Governor tag to its own GovernanceInfoStruct. mapping(string => GovernanceInfoStruct) internal governanceInfo; //NOLINT uninitialized-state. } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; /* The Fact Registry design pattern is a way to separate cryptographic verification from the business logic of the contract flow. A fact registry holds a hash table of verified "facts" which are represented by a hash of claims that the registry hash check and found valid. This table may be queried by accessing the isValid() function of the registry with a given hash. In addition, each fact registry exposes a registry specific function for submitting new claims together with their proofs. The information submitted varies from one registry to the other depending of the type of fact requiring verification. For further reading on the Fact Registry design pattern see this `StarkWare blog post <https://medium.com/starkware/the-fact-registry-a64aafb598b6>`_. */ interface IFactRegistry { /* Returns true if the given fact was previously registered in the contract. */ function isValid(bytes32 fact) external view returns (bool); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; interface Identity { /* Allows a caller to ensure that the provided address is of the expected type and version. */ function identify() external pure returns (string memory); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; contract LibConstants { // Durations for time locked mechanisms (in seconds). // Note that it is known that miners can manipulate block timestamps // up to a deviation of a few seconds. // This mechanism should not be used for fine grained timing. // The time required to cancel a deposit, in the case the operator does not move the funds // to the off-chain storage. uint256 public constant DEPOSIT_CANCEL_DELAY = 2 days; // The time required to freeze the exchange, in the case the operator does not execute a // requested full withdrawal. uint256 public constant FREEZE_GRACE_PERIOD = 7 days; // The time after which the exchange may be unfrozen after it froze. This should be enough time // for users to perform escape hatches to get back their funds. uint256 public constant UNFREEZE_DELAY = 365 days; // Maximal number of verifiers which may co-exist. uint256 public constant MAX_VERIFIER_COUNT = uint256(64); // The time required to remove a verifier in case of a verifier upgrade. uint256 public constant VERIFIER_REMOVAL_DELAY = FREEZE_GRACE_PERIOD + (21 days); address constant ZERO_ADDRESS = address(0x0); uint256 constant K_MODULUS = 0x800000000000011000000000000000000000000000000000000000000000001; uint256 constant K_BETA = 0x6f21413efbe40de150e596d72f7a8c5609ad26c15c915c1f4cdfcb99cee9e89; uint256 internal constant MASK_250 = 0x03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; uint256 internal constant MASK_240 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; uint256 public constant MAX_FORCED_ACTIONS_REQS_PER_BLOCK = 10; uint256 constant QUANTUM_UPPER_BOUND = 2**128; // All mintable asset ids have the MINTABLE_ASSET_ID_FLAG (251st bit) set. // Mintable ERC1155 and ERC20 assets have NON_UNIQUE_MINTABLE_ASSET_ID_FLAG (250th bit) set too. // Mintable ERC20s also have the MINTABLE_ERC20_ASSET_ID_FLAG (249th bit) set. // I.e. mintable asset ids that start with: 100 => ERC721, 110 => ERC1155, 111 => ERC20. // All non-mintable asset ids have the MINTABLE_ASSET_ID_FLAG bit off. uint256 internal constant MINTABLE_ASSET_ID_FLAG = 1 << 250; uint256 internal constant NON_UNIQUE_MINTABLE_ASSET_ID_FLAG = 1 << 249; uint256 internal constant MINTABLE_ERC20_ASSET_ID_FLAG = 1 << 248; // Bit 64 (indexed 63, counting from 0) is a flag indicating a rollup vault id. uint256 constant ROLLUP_VAULTS_BIT = 63; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; /* Interface containing actions a verifier can invoke on the state. The contract containing the state should implement these and verify correctness. */ abstract contract MAcceptModifications { function acceptDeposit( uint256 ownerKey, uint256 vaultId, uint256 assetId, uint256 quantizedAmount ) internal virtual; function allowWithdrawal( uint256 ownerKey, uint256 assetId, uint256 quantizedAmount ) internal virtual; function acceptWithdrawal( uint256 ownerKey, uint256 assetId, uint256 quantizedAmount ) internal virtual; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; abstract contract MForcedTradeActionState { function forcedTradeActionHash( uint256 starkKeyA, uint256 starkKeyB, uint256 vaultIdA, uint256 vaultIdB, uint256 collateralAssetId, uint256 syntheticAssetId, uint256 amountCollateral, uint256 amountSynthetic, bool aIsBuyingSynthetic, uint256 nonce ) internal pure virtual returns (bytes32); function clearForcedTradeRequest( uint256 starkKeyA, uint256 starkKeyB, uint256 vaultIdA, uint256 vaultIdB, uint256 collateralAssetId, uint256 syntheticAssetId, uint256 amountCollateral, uint256 amountSynthetic, bool aIsBuyingSynthetic, uint256 nonce ) internal virtual; // NOLINTNEXTLINE: external-function. function getForcedTradeRequest( uint256 starkKeyA, uint256 starkKeyB, uint256 vaultIdA, uint256 vaultIdB, uint256 collateralAssetId, uint256 syntheticAssetId, uint256 amountCollateral, uint256 amountSynthetic, bool aIsBuyingSynthetic, uint256 nonce ) public view virtual returns (uint256 res); function setForcedTradeRequest( uint256 starkKeyA, uint256 starkKeyB, uint256 vaultIdA, uint256 vaultIdB, uint256 collateralAssetId, uint256 syntheticAssetId, uint256 amountCollateral, uint256 amountSynthetic, bool aIsBuyingSynthetic, uint256 nonce, bool premiumCost ) internal virtual; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; abstract contract MForcedWithdrawalActionState { function forcedWithdrawActionHash( uint256 starkKey, uint256 vaultId, uint256 quantizedAmount ) internal pure virtual returns (bytes32); function clearForcedWithdrawalRequest( uint256 starkKey, uint256 vaultId, uint256 quantizedAmount ) internal virtual; // NOLINTNEXTLINE: external-function. function getForcedWithdrawalRequest( uint256 starkKey, uint256 vaultId, uint256 quantizedAmount ) public view virtual returns (uint256 res); function setForcedWithdrawalRequest( uint256 starkKey, uint256 vaultId, uint256 quantizedAmount, bool premiumCost ) internal virtual; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; abstract contract MFreezable { /* Returns true if the exchange is frozen. */ function isFrozen() public view virtual returns (bool); // NOLINT: external-function. /* Forbids calling the function if the exchange is frozen. */ modifier notFrozen() { require(!isFrozen(), "STATE_IS_FROZEN"); _; } function validateFreezeRequest(uint256 requestTime) internal virtual; /* Allows calling the function only if the exchange is frozen. */ modifier onlyFrozen() { require(isFrozen(), "STATE_NOT_FROZEN"); _; } /* Freezes the exchange. */ function freeze() internal virtual; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; abstract contract MGovernance { function _isGovernor(address user) internal view virtual returns (bool); /* Allows calling the function only by a Governor. */ modifier onlyGovernance() { require(_isGovernor(msg.sender), "ONLY_GOVERNANCE"); _; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./MGovernance.sol"; abstract contract MOperator { event LogOperatorAdded(address operator); event LogOperatorRemoved(address operator); function isOperator(address user) public view virtual returns (bool); modifier onlyOperator() { require(isOperator(msg.sender), "ONLY_OPERATOR"); _; } function registerOperator(address newOperator) external virtual; function unregisterOperator(address removedOperator) external virtual; function getOperators() internal view virtual returns (mapping(address => bool) storage); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; abstract contract MStateRoot { function getValidiumVaultRoot() public view virtual returns (uint256); function getValidiumTreeHeight() public view virtual returns (uint256); function getRollupVaultRoot() public view virtual returns (uint256); function getRollupTreeHeight() public view virtual returns (uint256); /* Returns true iff vaultId is in the valid vault ids range, i.e. could appear in either the validium or rollup vaults trees. */ function isVaultInRange(uint256 vaultId) internal view virtual returns (bool); /* Returns true if vaultId is a valid validium vault id. Note: when this function returns false it might mean that vaultId is invalid and does not guarantee that vaultId is a valid rollup vault id. */ function isValidiumVault(uint256 vaultId) internal view virtual returns (bool); /* Returns true if vaultId is a valid rollup vault id. Note: when this function returns false it might mean that vaultId is invalid and does not guarantee that vaultId is a valid validium vault id. */ function isRollupVault(uint256 vaultId) internal view virtual returns (bool); /* Given a valid vaultId, returns its leaf index in the validium/rollup tree. Note: this function does not assert the validity of vaultId, make sure to explicitly assert it when required. */ function getVaultLeafIndex(uint256 vaultId) internal pure virtual returns (uint256); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; abstract contract MTokenQuantization { function fromQuantized(uint256 presumedAssetType, uint256 quantizedAmount) internal view virtual returns (uint256 amount); // NOLINTNEXTLINE: external-function. function getQuantum(uint256 presumedAssetType) public view virtual returns (uint256 quantum); function toQuantized(uint256 presumedAssetType, uint256 amount) internal view virtual returns (uint256 quantizedAmount); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./Governance.sol"; import "./GovernanceStorage.sol"; /** The StarkEx contract is governed by one or more Governors of which the initial one is the deployer of the contract. A governor has the sole authority to perform the following operations: 1. Nominate additional governors (:sol:func:`mainNominateNewGovernor`) 2. Remove other governors (:sol:func:`mainRemoveGovernor`) 3. Add new :sol:mod:`Verifiers` and :sol:mod:`AvailabilityVerifiers` 4. Remove :sol:mod:`Verifiers` and :sol:mod:`AvailabilityVerifiers` after a timelock allows it 5. Nominate Operators (see :sol:mod:`Operator`) and Token Administrators (see :sol:mod:`TokenRegister`) Adding governors is performed in a two step procedure: 1. First, an existing governor nominates a new governor (:sol:func:`mainNominateNewGovernor`) 2. Then, the new governor must accept governance to become a governor (:sol:func:`mainAcceptGovernance`) This two step procedure ensures that a governor public key cannot be nominated unless there is an entity that has the corresponding private key. This is intended to prevent errors in the addition process. The governor private key should typically be held in a secure cold wallet. */ /* Implements Governance for the StarkDex main contract. The wrapper methods (e.g. mainIsGovernor wrapping _isGovernor) are needed to give the method unique names. Both Proxy and StarkExchange inherit from Governance. Thus, the logical contract method names must have unique names in order for the proxy to successfully delegate to them. */ contract MainGovernance is GovernanceStorage, Governance { // The tag is the sting key that is used in the Governance storage mapping. string public constant MAIN_GOVERNANCE_INFO_TAG = "StarkEx.Main.2019.GovernorsInformation"; /* Returns the GovernanceInfoStruct associated with the governance tag. */ function getGovernanceInfo() internal view override returns (GovernanceInfoStruct storage) { return governanceInfo[MAIN_GOVERNANCE_INFO_TAG]; } function mainIsGovernor(address user) external view returns (bool) { return _isGovernor(user); } function mainNominateNewGovernor(address newGovernor) external { _nominateNewGovernor(newGovernor); } function mainRemoveGovernor(address governorForRemoval) external { _removeGovernor(governorForRemoval); } function mainAcceptGovernance() external { _acceptGovernance(); } function mainCancelNomination() external { _cancelNomination(); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../upgrade/ProxyStorage.sol"; import "../libraries/Addresses.sol"; import {ApprovalChainData} from "../libraries/StarkExTypes.sol"; /* Holds ALL the main contract state (storage) variables. */ contract MainStorage is ProxyStorage { uint256 internal constant LAYOUT_LENGTH = 2**64; address escapeVerifierAddress; // NOLINT: constable-states. // Global dex-frozen flag. bool stateFrozen; // NOLINT: constable-states. // Time when unFreeze can be successfully called (UNFREEZE_DELAY after freeze). uint256 unFreezeTime; // NOLINT: constable-states. // Pending deposits. // A map STARK key => asset id => vault id => quantized amount. mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) pendingDeposits; // Cancellation requests. // A map STARK key => asset id => vault id => request timestamp. mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) cancellationRequests; // Pending withdrawals. // A map STARK key => asset id => quantized amount. mapping(uint256 => mapping(uint256 => uint256)) pendingWithdrawals; // vault_id => escape used boolean. mapping(uint256 => bool) escapesUsed; // Number of escapes that were performed when frozen. uint256 escapesUsedCount; // NOLINT: constable-states. // NOTE: fullWithdrawalRequests is deprecated, and replaced by forcedActionRequests. // NOLINTNEXTLINE naming-convention. mapping(uint256 => mapping(uint256 => uint256)) fullWithdrawalRequests_DEPRECATED; // State sequence number. uint256 sequenceNumber; // NOLINT: constable-states uninitialized-state. // Validium Vaults Tree Root & Height. uint256 validiumVaultRoot; // NOLINT: constable-states uninitialized-state. uint256 validiumTreeHeight; // NOLINT: constable-states uninitialized-state. // Order Tree Root & Height. uint256 orderRoot; // NOLINT: constable-states uninitialized-state. uint256 orderTreeHeight; // NOLINT: constable-states uninitialized-state. // True if and only if the address is allowed to add tokens. mapping(address => bool) tokenAdmins; // This mapping is no longer in use, remains for backwards compatibility. mapping(address => bool) userAdmins_DEPRECATED; // NOLINT: naming-convention. // True if and only if the address is an operator (allowed to update state). mapping(address => bool) operators; // NOLINT: uninitialized-state. // Mapping of contract ID to asset data. mapping(uint256 => bytes) assetTypeToAssetInfo; // NOLINT: uninitialized-state. // Mapping of registered contract IDs. mapping(uint256 => bool) registeredAssetType; // NOLINT: uninitialized-state. // Mapping from contract ID to quantum. mapping(uint256 => uint256) assetTypeToQuantum; // NOLINT: uninitialized-state. // This mapping is no longer in use, remains for backwards compatibility. mapping(address => uint256) starkKeys_DEPRECATED; // NOLINT: naming-convention. // Mapping from STARK public key to the Ethereum public key of its owner. mapping(uint256 => address) ethKeys; // NOLINT: uninitialized-state. // Timelocked state transition and availability verification chain. ApprovalChainData verifiersChain; ApprovalChainData availabilityVerifiersChain; // Batch id of last accepted proof. uint256 lastBatchId; // NOLINT: constable-states uninitialized-state. // Mapping between sub-contract index to sub-contract address. mapping(uint256 => address) subContracts; // NOLINT: uninitialized-state. mapping(uint256 => bool) permissiveAssetType_DEPRECATED; // NOLINT: naming-convention. // ---- END OF MAIN STORAGE AS DEPLOYED IN STARKEX2.0 ---- // Onchain-data version configured for the system. uint256 onchainDataVersion_DEPRECATED; // NOLINT: naming-convention constable-states. // Counter of forced action request in block. The key is the block number. mapping(uint256 => uint256) forcedRequestsInBlock; // ForcedAction requests: actionHash => requestTime. mapping(bytes32 => uint256) forcedActionRequests; // Mapping for timelocked actions. // A actionKey => activation time. mapping(bytes32 => uint256) actionsTimeLock; // Append only list of requested forced action hashes. bytes32[] actionHashList; // ---- END OF MAIN STORAGE AS DEPLOYED IN STARKEX3.0 ---- // ---- END OF MAIN STORAGE AS DEPLOYED IN STARKEX4.0 ---- // Rollup Vaults Tree Root & Height. uint256 rollupVaultRoot; // NOLINT: constable-states uninitialized-state. uint256 rollupTreeHeight; // NOLINT: constable-states uninitialized-state. uint256 globalConfigCode; // NOLINT: constable-states uninitialized-state. // Reserved storage space for Extensibility. // Every added MUST be added above the end gap, and the __endGap size must be reduced // accordingly. // NOLINTNEXTLINE: naming-convention. uint256[LAYOUT_LENGTH - 40] private __endGap; // __endGap complements layout to LAYOUT_LENGTH. } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; library OnchainDataFactTreeEncoder { struct DataAvailabilityFact { uint256 onchainDataHash; uint256 onchainDataSize; } // The number of additional words appended to the public input when using the // OnchainDataFactTreeEncoder format. uint256 internal constant ONCHAIN_DATA_FACT_ADDITIONAL_WORDS = 2; /* Encodes a GPS fact Merkle tree where the root has two children. The left child contains the data we care about and the right child contains on-chain data for the fact. */ function encodeFactWithOnchainData( uint256[] calldata programOutput, DataAvailabilityFact memory factData ) internal pure returns (bytes32) { // The state transition fact is computed as a Merkle tree, as defined in // GpsOutputParser. // // In our case the fact tree looks as follows: // The root has two children. // The left child is a leaf that includes the main part - the information regarding // the state transition required by this contract. // The right child contains the onchain-data which shouldn't be accessed by this // contract, so we are only given its hash and length // (it may be a leaf or an inner node, this has no effect on this contract). // Compute the hash without the two additional fields. uint256 mainPublicInputLen = programOutput.length; bytes32 mainPublicInputHash = keccak256(abi.encodePacked(programOutput)); // Compute the hash of the fact Merkle tree. bytes32 hashResult = keccak256( abi.encodePacked( mainPublicInputHash, mainPublicInputLen, factData.onchainDataHash, mainPublicInputLen + factData.onchainDataSize ) ); // Add one to the hash to indicate it represents an inner node, rather than a leaf. return bytes32(uint256(hashResult) + 1); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../interfaces/MOperator.sol"; import "../interfaces/MGovernance.sol"; /** The Operator of the contract is the entity entitled to submit state update requests by calling :sol:func:`updateState`. An Operator may be instantly appointed or removed by the contract Governor (see :sol:mod:`Governance`). Typically, the Operator is the hot wallet of the service submitting proofs for state updates. */ abstract contract Operator is MGovernance, MOperator { function registerOperator(address newOperator) external override onlyGovernance { if (!isOperator(newOperator)) { getOperators()[newOperator] = true; emit LogOperatorAdded(newOperator); } } function unregisterOperator(address removedOperator) external override onlyGovernance { if (isOperator(removedOperator)) { getOperators()[removedOperator] = false; emit LogOperatorRemoved(removedOperator); } } function isOperator(address user) public view override returns (bool) { return getOperators()[user]; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../libraries/LibConstants.sol"; contract PerpetualConstants is LibConstants { uint256 constant PERPETUAL_POSITION_ID_UPPER_BOUND = 2**64; uint256 constant PERPETUAL_AMOUNT_UPPER_BOUND = 2**64; uint256 constant PERPETUAL_TIMESTAMP_BITS = 32; uint256 constant PERPETUAL_ASSET_ID_UPPER_BOUND = 2**120; uint256 constant PERPETUAL_SYSTEM_TIME_LAG_BOUND = 7 days; uint256 constant PERPETUAL_SYSTEM_TIME_ADVANCE_BOUND = 4 hours; uint256 constant PERPETUAL_CONFIGURATION_DELAY = 0; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../../libraries/LibConstants.sol"; import "../../interfaces/MAcceptModifications.sol"; import "../../interfaces/MFreezable.sol"; import "../../interfaces/IFactRegistry.sol"; import "./PerpetualStorage.sol"; /** Escaping the exchange is the last resort for users that wish to withdraw their funds without relying on off-chain exchange services. The Escape functionality may only be invoked once the contract has become frozen. This will be as the result of an unserviced forcedAction request At that point, any escaper entity may perform an escape operation as follows: 1. Escapers call the :sol:mod:`PerpetualEscapeVerifier` contract with the Merkle proof for the vault to be evicted and the shared state. If the proof is valid, this results in the registration of said proof. 2. Escapers call :sol:func:`escape` function with the starkKey, vaultId and quantizedAmount matching the proof from step 1. 3. The owner of the vault may then withdraw this amount from the pending withdrawals account by calling the normal withdraw function (see :sol:mod:`Withdrawals`) to transfer the funds to the users ERC20 account. Note that while anyone can perform the initial steps of the escape operation (including the exchange operator, for example), only the owner of the vault may perform the final step of transferring the funds. */ abstract contract PerpetualEscapes is PerpetualStorage, MAcceptModifications, MFreezable { function initialize(address escapeVerifier) internal { escapeVerifierAddress = escapeVerifier; } /* Escape when the contract is frozen. */ function escape( uint256 starkKey, uint256 vaultId, uint256 quantizedAmount ) external onlyFrozen { require(!escapesUsed[vaultId], "ESCAPE_ALREADY_USED"); // Escape can be used only once. escapesUsed[vaultId] = true; escapesUsedCount += 1; bytes32 claimHash = keccak256( abi.encode(starkKey, quantizedAmount, sharedStateHash, vaultId) ); IFactRegistry escapeVerifier = IFactRegistry(escapeVerifierAddress); require(escapeVerifier.isValid(claimHash), "ESCAPE_LACKS_PROOF"); allowWithdrawal(starkKey, systemAssetType, quantizedAmount); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../components/PerpetualEscapes.sol"; import "../components/UpdatePerpetualState.sol"; import "../components/Configuration.sol"; import "../interactions/ForcedTradeActionState.sol"; import "../interactions/ForcedWithdrawalActionState.sol"; import "../../components/Freezable.sol"; import "../../components/MainGovernance.sol"; import "../../components/StarkExOperator.sol"; import "../../interactions/AcceptModifications.sol"; import "../../interactions/StateRoot.sol"; import "../../interactions/TokenQuantization.sol"; import "../../interfaces/SubContractor.sol"; contract PerpetualState is MainGovernance, SubContractor, Configuration, StarkExOperator, Freezable, AcceptModifications, TokenQuantization, ForcedTradeActionState, ForcedWithdrawalActionState, StateRoot, PerpetualEscapes, UpdatePerpetualState { // Empty state is 8 words (256 bytes) To pass as uint[] we need also head & len fields (64). uint256 constant INITIALIZER_SIZE = 384; // Padded address(32), uint(32), Empty state(256+64). /* Initialization flow: 1. Extract initialization parameters from data. 2. Call internalInitializer with those parameters. */ function initialize(bytes calldata data) external override { // This initializer sets roots etc. It must not be applied twice. // I.e. it can run only when the state is still empty. require(sharedStateHash == bytes32(0x0), "STATE_ALREADY_INITIALIZED"); require(configurationHash[GLOBAL_CONFIG_KEY] == bytes32(0x0), "STATE_ALREADY_INITIALIZED"); require(data.length == INITIALIZER_SIZE, "INCORRECT_INIT_DATA_SIZE_384"); ( address escapeVerifierAddress_, uint256 initialSequenceNumber, uint256[] memory initialState ) = abi.decode(data, (address, uint256, uint256[])); initGovernance(); Configuration.initialize(PERPETUAL_CONFIGURATION_DELAY); StarkExOperator.initialize(); // The StateRoot struct has room for 3 trees for compatability with StarkEx. // Perpertual only uses two of these trees. StateRoot.initialize( initialSequenceNumber, uint256(-1), // NOT IN USE. initialState[0], // PositionTreeRoots. initialState[2], // OrderTreeRoots. uint256(-1), // NOT IN USE. initialState[1], // PositionTreeHeights. initialState[3] // OrderTreeHeights. ); sharedStateHash = keccak256(abi.encodePacked(initialState)); PerpetualEscapes.initialize(escapeVerifierAddress_); } /* The call to initializerSize is done from MainDispatcherBase using delegatecall, thus the existing state is already accessible. */ function initializerSize() external view override returns (uint256) { return INITIALIZER_SIZE; } function validatedSelectors() external pure override returns (bytes4[] memory selectors) { uint256 len_ = 1; uint256 index_ = 0; selectors = new bytes4[](len_); selectors[index_++] = PerpetualEscapes.escape.selector; require(index_ == len_, "INCORRECT_SELECTORS_ARRAY_LENGTH"); } function identify() external pure override returns (string memory) { return "StarkWare_PerpetualState_2023_4"; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../../components/MainStorage.sol"; /* Extends MainStorage, holds Perpetual App specific state (storage) variables. ALL State variables that are common to all applications, reside in MainStorage, whereas ALL the Perpetual app specific ones reside here. */ contract PerpetualStorage is MainStorage { uint256 systemAssetType; // NOLINT: constable-states uninitialized-state. bytes32 public globalConfigurationHash; // NOLINT: constable-states uninitialized-state. mapping(uint256 => bytes32) public configurationHash; // NOLINT: uninitialized-state. bytes32 sharedStateHash; // NOLINT: constable-states uninitialized-state. // Configuration apply time-lock. // The delay is held in storage (and not constant) // So that it can be modified during upgrade. uint256 public configurationDelay; // NOLINT: constable-states. // Reserved storage space for Extensibility. // Every added MUST be added above the end gap, and the __endGap size must be reduced // accordingly. // NOLINTNEXTLINE: naming-convention shadowing-abstract. uint256[LAYOUT_LENGTH - 5] private __endGap; // __endGap complements layout to LAYOUT_LENGTH. } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; contract ProgramOutputOffsets { // The following constants are offsets of data expected in the program output. // The offsets here are of the fixed fields. uint256 internal constant PROG_OUT_GENERAL_CONFIG_HASH = 0; uint256 internal constant PROG_OUT_DATA_AVAILABILTY_MODE = 1; uint256 internal constant PROG_OUT_N_ASSET_CONFIGS = 2; uint256 internal constant PROG_OUT_ASSET_CONFIG_HASHES = 3; /* Additional mandatory fields of a single word: - Previous state size 3 - New state size 4 - Vault tree height 5 - Order tree height 6 - Expiration timestamp 7 - No. of Modifications 8. */ uint256 internal constant PROG_OUT_N_WORDS_MIN_SIZE = 9; uint256 internal constant PROG_OUT_N_WORDS_PER_ASSET_CONFIG = 2; uint256 internal constant PROG_OUT_N_WORDS_PER_MODIFICATION = 3; uint256 internal constant ASSET_CONFIG_OFFSET_ASSET_ID = 0; uint256 internal constant ASSET_CONFIG_OFFSET_CONFIG_HASH = 1; uint256 internal constant MODIFICATIONS_OFFSET_STARKKEY = 0; uint256 internal constant MODIFICATIONS_OFFSET_POS_ID = 1; uint256 internal constant MODIFICATIONS_OFFSET_BIASED_DIFF = 2; uint256 internal constant STATE_OFFSET_VAULTS_ROOT = 0; uint256 internal constant STATE_OFFSET_VAULTS_HEIGHT = 1; uint256 internal constant STATE_OFFSET_ORDERS_ROOT = 2; uint256 internal constant STATE_OFFSET_ORDERS_HEIGHT = 3; uint256 internal constant STATE_OFFSET_N_FUNDING = 4; uint256 internal constant STATE_OFFSET_FUNDING = 5; // Data availability mode. uint256 internal constant VALIDIUM_MODE = 0; uint256 internal constant ROLLUP_MODE = 1; // The following constants are offsets of data expected in the application data. uint256 internal constant APP_DATA_BATCH_ID_OFFSET = 0; uint256 internal constant APP_DATA_PREVIOUS_BATCH_ID_OFFSET = 1; uint256 internal constant APP_DATA_N_CONDITIONAL_TRANSFER = 2; uint256 internal constant APP_DATA_CONDITIONAL_TRANSFER_DATA_OFFSET = 3; uint256 internal constant APP_DATA_N_WORDS_PER_CONDITIONAL_TRANSFER = 2; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../components/GovernanceStorage.sol"; /* Holds the Proxy-specific state variables. This contract is inherited by the GovernanceStorage (and indirectly by MainStorage) to prevent collision hazard. */ contract ProxyStorage is GovernanceStorage { // NOLINTNEXTLINE: naming-convention uninitialized-state. mapping(address => bytes32) internal initializationHash_DEPRECATED; // The time after which we can switch to the implementation. // Hash(implementation, data, finalize) => time. mapping(bytes32 => uint256) internal enabledTime; // A central storage of the flags whether implementation has been initialized. // Note - it can be used flexibly enough to accommodate multiple levels of initialization // (i.e. using different key salting schemes for different initialization levels). mapping(bytes32 => bool) internal initialized; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./Operator.sol"; import "./MainStorage.sol"; /** Operator implementation for StarkEx (StarkExchange & StarkPerpetual). */ abstract contract StarkExOperator is MainStorage, Operator { function initialize() internal { getOperators()[msg.sender] = true; emit LogOperatorAdded(msg.sender); } function getOperators() internal view override returns (mapping(address => bool) storage) { return operators; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; // Structure representing a list of verifiers (validity/availability). // A statement is valid only if all the verifiers in the list agree on it. // Adding a verifier to the list is immediate - this is used for fast resolution of // any soundness issues. // Removing a verifier from the list is time-locked, to ensure that any user of the system // not content with the announced removal has ample time to leave the system before it is // removed. struct ApprovalChainData { address[] verifiers; // Represents the time after which the verifier with the given address can be removed. // Removal of the verifier with address A is allowed only in the case the value // of verifierAllowedRemovalTime[A] != 0 and verifierAllowedRemovalTime[A] < (current time). mapping(address => uint256) verifierAllowedRemovalTime; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../interfaces/MStateRoot.sol"; import "../components/MainStorage.sol"; import "../libraries/LibConstants.sol"; contract StateRoot is MainStorage, LibConstants, MStateRoot { function initialize( uint256 initialSequenceNumber, uint256 initialValidiumVaultRoot, uint256 initialRollupVaultRoot, uint256 initialOrderRoot, uint256 initialValidiumTreeHeight, uint256 initialRollupTreeHeight, uint256 initialOrderTreeHeight ) internal { sequenceNumber = initialSequenceNumber; validiumVaultRoot = initialValidiumVaultRoot; rollupVaultRoot = initialRollupVaultRoot; orderRoot = initialOrderRoot; validiumTreeHeight = initialValidiumTreeHeight; rollupTreeHeight = initialRollupTreeHeight; orderTreeHeight = initialOrderTreeHeight; } function getValidiumVaultRoot() public view override returns (uint256) { return validiumVaultRoot; } function getValidiumTreeHeight() public view override returns (uint256) { return validiumTreeHeight; } function getRollupVaultRoot() public view override returns (uint256) { return rollupVaultRoot; } function getRollupTreeHeight() public view override returns (uint256) { return rollupTreeHeight; } function getOrderRoot() external view returns (uint256) { return orderRoot; } function getOrderTreeHeight() external view returns (uint256) { return orderTreeHeight; } function getSequenceNumber() external view returns (uint256) { return sequenceNumber; } function getLastBatchId() external view returns (uint256) { return lastBatchId; } function getGlobalConfigCode() external view returns (uint256) { return globalConfigCode; } function isVaultInRange(uint256 vaultId) internal view override returns (bool) { return (isValidiumVault(vaultId) || isRollupVault(vaultId)); } function isValidiumVault(uint256 vaultId) internal view override returns (bool) { // Return true iff vaultId is in the validium vaults tree. return vaultId < 2**getValidiumTreeHeight(); } function isRollupVault(uint256 vaultId) internal view override returns (bool) { // Return true iff vaultId is in the rollup vaults tree. uint256 rollupLowerBound = 2**ROLLUP_VAULTS_BIT; uint256 rollupUpperBound = rollupLowerBound + 2**getRollupTreeHeight(); return (rollupLowerBound <= vaultId && vaultId < rollupUpperBound); } function getVaultLeafIndex(uint256 vaultId) internal pure override returns (uint256) { // Return the index of vaultId leaf in its tree, which doesn't include the rollup bit flag. return (vaultId & (2**ROLLUP_VAULTS_BIT - 1)); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./Identity.sol"; interface SubContractor is Identity { function initialize(bytes calldata data) external; function initializerSize() external view returns (uint256); /* Returns an array with selectors for validation. These selectors are the critical ones for maintaining self custody and anti censorship. During the upgrade process, as part of the sub-contract validation, the MainDispatcher validates that the selectos are mapped to the correct sub-contract. */ function validatedSelectors() external pure returns (bytes4[] memory); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../components/MainStorage.sol"; import "../interfaces/MTokenQuantization.sol"; contract TokenQuantization is MainStorage, MTokenQuantization { function fromQuantized(uint256 presumedAssetType, uint256 quantizedAmount) internal view override returns (uint256 amount) { uint256 quantum = getQuantum(presumedAssetType); amount = quantizedAmount * quantum; require(amount / quantum == quantizedAmount, "DEQUANTIZATION_OVERFLOW"); } function getQuantum(uint256 presumedAssetType) public view override returns (uint256 quantum) { if (!registeredAssetType[presumedAssetType]) { // Default quantization, for NFTs etc. quantum = 1; } else { // Retrieve registration. quantum = assetTypeToQuantum[presumedAssetType]; } } function toQuantized(uint256 presumedAssetType, uint256 amount) internal view override returns (uint256 quantizedAmount) { uint256 quantum = getQuantum(presumedAssetType); require(amount % quantum == 0, "INVALID_AMOUNT"); quantizedAmount = amount / quantum; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./PerpetualStorage.sol"; import "../interfaces/MForcedTradeActionState.sol"; import "../interfaces/MForcedWithdrawalActionState.sol"; import "../PerpetualConstants.sol"; import "../ProgramOutputOffsets.sol"; import "../../components/OnchainDataFactTreeEncoder.sol"; import "../../components/VerifyFactChain.sol"; import "../../interfaces/MAcceptModifications.sol"; import "../../interfaces/MFreezable.sol"; import "../../interfaces/MOperator.sol"; import "../../libraries/Addresses.sol"; /** TO-DO:DOC. */ abstract contract UpdatePerpetualState is PerpetualStorage, PerpetualConstants, MForcedTradeActionState, MForcedWithdrawalActionState, VerifyFactChain, MAcceptModifications, MFreezable, MOperator, ProgramOutputOffsets { event LogUpdateState(uint256 sequenceNumber, uint256 batchId); event LogStateTransitionFact(bytes32 stateTransitionFact); enum ForcedAction { Withdrawal, Trade } struct ProgramOutputMarkers { uint256 globalConfigurationHash; uint256 nAssets; uint256 assetConfigOffset; uint256 prevSharedStateSize; uint256 prevSharedStateOffset; uint256 newSharedStateSize; uint256 newSharedStateOffset; uint256 newVaultsRoot; uint256 newVaultsHeight; uint256 newOrdersRoot; uint256 newOrdersHeight; uint256 newSystemTime; uint256 dataAvailabilityMode; uint256 expirationTimestamp; uint256 nModifications; uint256 modificationsOffset; uint256 forcedActionsSize; uint256 nForcedActions; uint256 forcedActionsOffset; uint256 nConditions; uint256 conditionsOffset; } function updateState(uint256[] calldata programOutput, uint256[] calldata applicationData) external notFrozen onlyOperator { ProgramOutputMarkers memory outputMarkers = parseProgramOutput(programOutput); require( outputMarkers.expirationTimestamp < 2**PERPETUAL_TIMESTAMP_BITS, "Expiration timestamp is out of range." ); require( outputMarkers.newSystemTime > (block.timestamp - PERPETUAL_SYSTEM_TIME_LAG_BOUND), "SYSTEM_TIME_OUTDATED" ); require( outputMarkers.newSystemTime < (block.timestamp + PERPETUAL_SYSTEM_TIME_ADVANCE_BOUND), "SYSTEM_TIME_INVALID" ); require( outputMarkers.expirationTimestamp > block.timestamp / 3600, "BATCH_TIMESTAMP_EXPIRED" ); require( outputMarkers.newVaultsHeight == programOutput[outputMarkers.prevSharedStateOffset + STATE_OFFSET_VAULTS_HEIGHT], "INCONSISTENT_POSITION_TREE_HEIGHT" ); require( outputMarkers.newOrdersHeight == programOutput[outputMarkers.prevSharedStateOffset + STATE_OFFSET_ORDERS_HEIGHT], "INCONSISTENT_ORDER_TREE_HEIGHT" ); validateConfigHashes(programOutput, outputMarkers); // Caclulate previous shared state hash, and compare with stored one. bytes32 prevStateHash = keccak256( abi.encodePacked( programOutput[outputMarkers.prevSharedStateOffset:outputMarkers .prevSharedStateOffset + outputMarkers.prevSharedStateSize] ) ); require(prevStateHash == sharedStateHash, "INVALID_PREVIOUS_SHARED_STATE"); require( applicationData[APP_DATA_PREVIOUS_BATCH_ID_OFFSET] == lastBatchId, "WRONG_PREVIOUS_BATCH_ID" ); bytes32 stateTransitionFact; if (outputMarkers.dataAvailabilityMode == VALIDIUM_MODE) { stateTransitionFact = keccak256(abi.encodePacked(programOutput)); } else { require( outputMarkers.dataAvailabilityMode == ROLLUP_MODE, "UNSUPPORTED_DATA_AVAILABILITY_MODE" ); require( programOutput.length >= outputMarkers.forcedActionsOffset + OnchainDataFactTreeEncoder.ONCHAIN_DATA_FACT_ADDITIONAL_WORDS, "programOutput does not contain all required fields." ); stateTransitionFact = OnchainDataFactTreeEncoder.encodeFactWithOnchainData( programOutput[:programOutput.length - OnchainDataFactTreeEncoder.ONCHAIN_DATA_FACT_ADDITIONAL_WORDS], OnchainDataFactTreeEncoder.DataAvailabilityFact({ onchainDataHash: programOutput[programOutput.length - 2], onchainDataSize: programOutput[programOutput.length - 1] }) ); } emit LogStateTransitionFact(stateTransitionFact); verifyFact( verifiersChain, stateTransitionFact, "NO_STATE_TRANSITION_VERIFIERS", "NO_STATE_TRANSITION_PROOF" ); if (outputMarkers.dataAvailabilityMode == VALIDIUM_MODE) { bytes32 availabilityFact = keccak256( abi.encodePacked( outputMarkers.newVaultsRoot, outputMarkers.newVaultsHeight, outputMarkers.newOrdersRoot, outputMarkers.newOrdersHeight, sequenceNumber + 1 ) ); verifyFact( availabilityVerifiersChain, availabilityFact, "NO_AVAILABILITY_VERIFIERS", "NO_AVAILABILITY_PROOF" ); } performUpdateState(programOutput, outputMarkers, applicationData); } function validateConfigHashes( uint256[] calldata programOutput, ProgramOutputMarkers memory markers ) internal view { require(globalConfigurationHash != bytes32(0), "GLOBAL_CONFIGURATION_NOT_SET"); require( globalConfigurationHash == bytes32(markers.globalConfigurationHash), "GLOBAL_CONFIGURATION_MISMATCH" ); uint256 offset = markers.assetConfigOffset; for (uint256 i = 0; i < markers.nAssets; i++) { uint256 assetId = programOutput[offset + ASSET_CONFIG_OFFSET_ASSET_ID]; bytes32 assetConfigHash = bytes32( programOutput[offset + ASSET_CONFIG_OFFSET_CONFIG_HASH] ); require(configurationHash[assetId] == assetConfigHash, "ASSET_CONFIGURATION_MISMATCH"); offset += PROG_OUT_N_WORDS_PER_ASSET_CONFIG; } } function parseProgramOutput(uint256[] calldata programOutput) internal pure returns (ProgramOutputMarkers memory) { require( programOutput.length >= PROG_OUT_N_WORDS_MIN_SIZE, "programOutput does not contain all required fields." ); ProgramOutputMarkers memory markers; // NOLINT: uninitialized-local. markers.globalConfigurationHash = programOutput[PROG_OUT_GENERAL_CONFIG_HASH]; markers.dataAvailabilityMode = programOutput[PROG_OUT_DATA_AVAILABILTY_MODE]; markers.nAssets = programOutput[PROG_OUT_N_ASSET_CONFIGS]; require(markers.nAssets < 2**16, "ILLEGAL_NUMBER_OF_ASSETS"); uint256 offset = PROG_OUT_ASSET_CONFIG_HASHES; markers.assetConfigOffset = offset; offset += markers.nAssets * PROG_OUT_N_WORDS_PER_ASSET_CONFIG; require( programOutput.length >= offset + 1, // Adding +1 for the next mandatory field. "programOutput invalid size (nAssetConfig)" ); markers.prevSharedStateSize = programOutput[offset++]; markers.prevSharedStateOffset = offset; offset += markers.prevSharedStateSize; require( programOutput.length >= offset + 1, // Adding +1 for the next mandatory field. "programOutput invalid size (prevState)" ); markers.newSharedStateSize = programOutput[offset++]; markers.newSharedStateOffset = offset; offset += markers.newSharedStateSize; require( programOutput.length >= offset + 2, // Adding +2 for the next mandatory fields. "programOutput invalid size (newState)" ); // System time is the last field in the state. markers.newSystemTime = programOutput[offset - 1]; markers.newVaultsRoot = programOutput[ markers.newSharedStateOffset + STATE_OFFSET_VAULTS_ROOT ]; markers.newVaultsHeight = programOutput[ markers.newSharedStateOffset + STATE_OFFSET_VAULTS_HEIGHT ]; markers.newOrdersRoot = programOutput[ markers.newSharedStateOffset + STATE_OFFSET_ORDERS_ROOT ]; markers.newOrdersHeight = programOutput[ markers.newSharedStateOffset + STATE_OFFSET_ORDERS_HEIGHT ]; markers.expirationTimestamp = programOutput[offset++]; markers.nModifications = programOutput[offset++]; markers.modificationsOffset = offset; offset += markers.nModifications * PROG_OUT_N_WORDS_PER_MODIFICATION; markers.forcedActionsSize = programOutput[offset++]; markers.nForcedActions = programOutput[offset++]; markers.forcedActionsOffset = offset; offset += markers.forcedActionsSize; markers.nConditions = programOutput[offset++]; markers.conditionsOffset = offset; offset += markers.nConditions; // In ROLLUP_MODE the Onchain Data info appears at the end of programOutput. if (markers.dataAvailabilityMode == ROLLUP_MODE) { offset += OnchainDataFactTreeEncoder.ONCHAIN_DATA_FACT_ADDITIONAL_WORDS; } require( programOutput.length == offset, "programOutput invalid size (mods/forced/conditions)" ); return markers; } function performUpdateState( uint256[] calldata programOutput, ProgramOutputMarkers memory markers, uint256[] calldata applicationData ) internal { sharedStateHash = keccak256( abi.encodePacked( programOutput[markers.newSharedStateOffset:markers.newSharedStateOffset + markers.newSharedStateSize] ) ); sequenceNumber += 1; uint256 batchId = applicationData[APP_DATA_BATCH_ID_OFFSET]; lastBatchId = batchId; sendModifications(programOutput, markers, applicationData); verifyConditionalTransfers(programOutput, markers, applicationData); clearForcedActionsFlags(programOutput, markers); emit LogUpdateState(sequenceNumber, batchId); } /* Goes through the program output forced actions section, extract each forced action, and if valid and its flag exists, clears it. If invalid, or not flag not exist - revert. */ function clearForcedActionsFlags( uint256[] calldata programOutput, ProgramOutputMarkers memory markers ) private { uint256 offset = markers.forcedActionsOffset; for (uint256 i = 0; i < markers.nForcedActions; i++) { ForcedAction forcedActionType = ForcedAction(programOutput[offset++]); if (forcedActionType == ForcedAction.Withdrawal) { offset = clearForcedWithdrawal(programOutput, offset); } else if (forcedActionType == ForcedAction.Trade) { offset = clearForcedTrade(programOutput, offset); } else { revert("UNKNOWN_FORCED_ACTION_TYPE"); } } // Ensure all sizes are matching (this is not checked in parsing). require(markers.forcedActionsOffset + markers.forcedActionsSize == offset, "SIZE_MISMATCH"); } function clearForcedWithdrawal(uint256[] calldata programOutput, uint256 offset) private returns (uint256) { uint256 starkKey = programOutput[offset++]; uint256 vaultId = programOutput[offset++]; uint256 quantizedAmount = programOutput[offset++]; clearForcedWithdrawalRequest(starkKey, vaultId, quantizedAmount); return offset; } function clearForcedTrade(uint256[] calldata programOutput, uint256 offset) private returns (uint256) { uint256 starkKeyA = programOutput[offset++]; uint256 starkKeyB = programOutput[offset++]; uint256 vaultIdA = programOutput[offset++]; uint256 vaultIdB = programOutput[offset++]; // CollateralAssetId Not taken from progOutput. We use systemAssetType. uint256 syntheticAssetId = programOutput[offset++]; uint256 amountCollateral = programOutput[offset++]; uint256 amountSynthetic = programOutput[offset++]; bool aIsBuyingSynthetic = (programOutput[offset++] != 0); uint256 nonce = programOutput[offset++]; clearForcedTradeRequest( starkKeyA, starkKeyB, vaultIdA, vaultIdB, systemAssetType, syntheticAssetId, amountCollateral, amountSynthetic, aIsBuyingSynthetic, nonce ); return offset; } function verifyConditionalTransfers( uint256[] calldata programOutput, ProgramOutputMarkers memory markers, uint256[] calldata applicationData ) private view { require(applicationData.length >= APP_DATA_N_CONDITIONAL_TRANSFER, "APP_DATA_TOO_SHORT"); require( applicationData[APP_DATA_N_CONDITIONAL_TRANSFER] == markers.nConditions, "N_CONDITIONS_MISMATCH" ); require( applicationData.length >= APP_DATA_CONDITIONAL_TRANSFER_DATA_OFFSET + markers.nConditions * APP_DATA_N_WORDS_PER_CONDITIONAL_TRANSFER, "BAD_APP_DATA_SIZE" ); uint256 conditionsOffset = markers.conditionsOffset; uint256 preImageOffset = APP_DATA_CONDITIONAL_TRANSFER_DATA_OFFSET; // Conditional Transfers appear after all other modifications. for (uint256 i = 0; i < markers.nConditions; i++) { address transferRegistry = address(applicationData[preImageOffset]); bytes32 transferFact = bytes32(applicationData[preImageOffset + 1]); uint256 condition = programOutput[conditionsOffset]; // The condition is the 250 LS bits of keccak256 of the fact registry & fact. require( condition == uint256(keccak256(abi.encodePacked(transferRegistry, transferFact))) & MASK_250, "Condition mismatch." ); // NOLINTNEXTLINE: low-level-calls-loop reentrancy-events. (bool success, bytes memory returndata) = transferRegistry.staticcall( abi.encodeWithSignature("isValid(bytes32)", transferFact) ); require(success && returndata.length == 32, "BAD_FACT_REGISTRY_CONTRACT"); require( abi.decode(returndata, (bool)), "Condition for the conditional transfer was not met." ); conditionsOffset += 1; preImageOffset += APP_DATA_N_WORDS_PER_CONDITIONAL_TRANSFER; } } function sendModifications( uint256[] calldata programOutput, ProgramOutputMarkers memory markers, uint256[] calldata /*applicationData*/ ) private { uint256 assetId = systemAssetType; require(assetId < K_MODULUS, "Asset id >= PRIME"); uint256 offset = markers.modificationsOffset; for (uint256 i = 0; i < markers.nModifications; i++) { uint256 starkKey = programOutput[offset + MODIFICATIONS_OFFSET_STARKKEY]; uint256 vaultId = programOutput[offset + MODIFICATIONS_OFFSET_POS_ID]; uint256 biasedDiff = programOutput[offset + MODIFICATIONS_OFFSET_BIASED_DIFF]; // Biased representation. // biased_delta is in range [0, 2**65), where 2**64 means 0 change. // The effective difference is biased_delta - 2**64. require(biasedDiff < (1 << 65), "Illegal Balance Diff"); int256 balanceDiff = int256(biasedDiff - (1 << 64)); require(starkKey < K_MODULUS, "Stark key >= PRIME"); if (balanceDiff > 0) { // This is a deposit. acceptDeposit(starkKey, vaultId, assetId, uint256(balanceDiff)); } else if (balanceDiff < 0) { // This is a withdrawal. acceptWithdrawal(starkKey, assetId, uint256(-balanceDiff)); } offset += PROG_OUT_N_WORDS_PER_MODIFICATION; } } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./MainStorage.sol"; import "../interfaces/IFactRegistry.sol"; import {ApprovalChainData} from "../libraries/StarkExTypes.sol"; contract VerifyFactChain is MainStorage { function verifyFact( ApprovalChainData storage chain, bytes32 fact, string memory noVerifiersErrorMessage, string memory invalidFactErrorMessage ) internal view { address[] storage verifiers = chain.verifiers; uint256 n_entries = verifiers.length; require(n_entries > 0, noVerifiersErrorMessage); for (uint256 i = 0; i < n_entries; i++) { // NOLINTNEXTLINE: calls-loop. require(IFactRegistry(verifiers[i]).isValid(fact), invalidFactErrorMessage); } } }
File 4 of 8: FinalizableGpsFactAdapter
/* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../interfaces/SimpleAdminable.sol"; /** A simple base class for finalizable contracts. */ abstract contract Finalizable is SimpleAdminable { event Finalized(); bool finalized; function isFinalized() public view returns (bool) { return finalized; } modifier notFinalized() { require(!isFinalized(), "FINALIZED"); _; } function finalize() external onlyAdmin notFinalized { finalized = true; emit Finalized(); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./Finalizable.sol"; import "./GpsFactRegistryAdapter.sol"; import "../interfaces/IQueryableFactRegistry.sol"; /** A finalizable version of GpsFactRegistryAdapter. It allows resetting the gps program hash, until finalized. */ contract FinalizableGpsFactAdapter is GpsFactRegistryAdapter, Finalizable { constructor(IQueryableFactRegistry gpsStatementContract, uint256 programHash_) public GpsFactRegistryAdapter(gpsStatementContract, programHash_) {} function setProgramHash(uint256 newProgramHash) external notFinalized onlyAdmin { programHash = newProgramHash; } function identify() external pure override returns (string memory) { return "StarkWare_FinalizableGpsFactAdapter_2022_1"; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../interfaces/Identity.sol"; import "../interfaces/IQueryableFactRegistry.sol"; /* The GpsFactRegistryAdapter contract is used as an adapter between a Dapp contract and a GPS fact registry. An isValid(fact) query is answered by querying the GPS contract about new_fact := keccak256(programHash, fact). The goal of this contract is to simplify the verifier upgradability logic in the Dapp contract by making the upgrade flow the same regardless of whether the update is to the program hash or the gpsContractAddress. */ contract GpsFactRegistryAdapter is IQueryableFactRegistry, Identity { IQueryableFactRegistry public gpsContract; uint256 public programHash; constructor(IQueryableFactRegistry gpsStatementContract, uint256 programHash_) public { gpsContract = gpsStatementContract; programHash = programHash_; } function identify() external pure virtual override returns (string memory) { return "StarkWare_GpsFactRegistryAdapter_2020_1"; } /* Checks if a fact has been verified. */ function isValid(bytes32 fact) external view override returns (bool) { return gpsContract.isValid(keccak256(abi.encode(programHash, fact))); } /* Indicates whether at least one fact was registered. */ function hasRegisteredFact() external view override returns (bool) { return gpsContract.hasRegisteredFact(); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity >=0.6.12; /* The Fact Registry design pattern is a way to separate cryptographic verification from the business logic of the contract flow. A fact registry holds a hash table of verified "facts" which are represented by a hash of claims that the registry hash check and found valid. This table may be queried by accessing the isValid() function of the registry with a given hash. In addition, each fact registry exposes a registry specific function for submitting new claims together with their proofs. The information submitted varies from one registry to the other depending of the type of fact requiring verification. For further reading on the Fact Registry design pattern see this `StarkWare blog post <https://medium.com/starkware/the-fact-registry-a64aafb598b6>`_. */ interface IFactRegistry { /* Returns true if the given fact was previously registered in the contract. */ function isValid(bytes32 fact) external view returns (bool); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./IFactRegistry.sol"; /* Extends the IFactRegistry interface with a query method that indicates whether the fact registry has successfully registered any fact or is still empty of such facts. */ interface IQueryableFactRegistry is IFactRegistry { /* Returns true if at least one fact has been registered. */ function hasRegisteredFact() external view returns (bool); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity >=0.6.12; interface Identity { /* Allows a caller to ensure that the provided address is of the expected type and version. */ function identify() external pure returns (string memory); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; abstract contract SimpleAdminable { address owner; address ownerCandidate; mapping(address => bool) admins; constructor() internal { owner = msg.sender; admins[msg.sender] = true; } // Admin/Owner Modifiers. modifier onlyOwner() { require(isOwner(msg.sender), "ONLY_OWNER"); _; } function isOwner(address testedAddress) public view returns (bool) { return owner == testedAddress; } modifier onlyAdmin() { require(isAdmin(msg.sender), "ONLY_ADMIN"); _; } function isAdmin(address testedAddress) public view returns (bool) { return admins[testedAddress]; } function registerAdmin(address newAdmin) external onlyOwner { if (!isAdmin(newAdmin)) { admins[newAdmin] = true; } } function removeAdmin(address removedAdmin) external onlyOwner { require(!isOwner(removedAdmin), "OWNER_CANNOT_BE_REMOVED_AS_ADMIN"); delete admins[removedAdmin]; } function nominateNewOwner(address newOwner) external onlyOwner { require(!isOwner(newOwner), "ALREADY_OWNER"); ownerCandidate = newOwner; } function acceptOwnership() external { // Previous owner is still an admin. require(msg.sender == ownerCandidate, "NOT_A_CANDIDATE"); owner = ownerCandidate; admins[ownerCandidate] = true; ownerCandidate = address(0x0); } }
File 5 of 8: Proxy
{"Common.sol":{"content":"/*\n Copyright 2019-2021 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\n/*\n Common Utility librarries.\n I. Addresses (extending address).\n*/\nlibrary Addresses {\n function isContract(address account) internal view returns (bool) {\n uint256 size;\n assembly {\n size := extcodesize(account)\n }\n return size \u003e 0;\n }\n\n function performEthTransfer(address recipient, uint256 amount) internal {\n (bool success, ) = recipient.call{value: amount}(\"\"); // NOLINT: low-level-calls.\n require(success, \"ETH_TRANSFER_FAILED\");\n }\n\n /*\n Safe wrapper around ERC20/ERC721 calls.\n This is required because many deployed ERC20 contracts don\u0027t return a value.\n See https://github.com/ethereum/solidity/issues/4116.\n */\n function safeTokenContractCall(address tokenAddress, bytes memory callData) internal {\n require(isContract(tokenAddress), \"BAD_TOKEN_ADDRESS\");\n // NOLINTNEXTLINE: low-level-calls.\n (bool success, bytes memory returndata) = tokenAddress.call(callData);\n require(success, string(returndata));\n\n if (returndata.length \u003e 0) {\n require(abi.decode(returndata, (bool)), \"TOKEN_OPERATION_FAILED\");\n }\n }\n\n /*\n Validates that the passed contract address is of a real contract,\n and that its id hash (as infered fromn identify()) matched the expected one.\n */\n function validateContractId(address contractAddress, bytes32 expectedIdHash) internal {\n require(isContract(contractAddress), \"ADDRESS_NOT_CONTRACT\");\n (bool success, bytes memory returndata) = contractAddress.call( // NOLINT: low-level-calls.\n abi.encodeWithSignature(\"identify()\")\n );\n require(success, \"FAILED_TO_IDENTIFY_CONTRACT\");\n string memory realContractId = abi.decode(returndata, (string));\n require(\n keccak256(abi.encodePacked(realContractId)) == expectedIdHash,\n \"UNEXPECTED_CONTRACT_IDENTIFIER\"\n );\n }\n\n /*\n Similar to safeTokenContractCall, but always ignores the return value.\n\n Assumes some other method is used to detect the failures\n (e.g. balance is checked before and after the call).\n */\n function uncheckedTokenContractCall(address tokenAddress, bytes memory callData) internal {\n // NOLINTNEXTLINE: low-level-calls.\n (bool success, bytes memory returndata) = tokenAddress.call(callData);\n require(success, string(returndata));\n }\n}\n\n/*\n II. StarkExTypes - Common data types.\n*/\nlibrary StarkExTypes {\n // Structure representing a list of verifiers (validity/availability).\n // A statement is valid only if all the verifiers in the list agree on it.\n // Adding a verifier to the list is immediate - this is used for fast resolution of\n // any soundness issues.\n // Removing from the list is time-locked, to ensure that any user of the system\n // not content with the announced removal has ample time to leave the system before it is\n // removed.\n struct ApprovalChainData {\n address[] list;\n // Represents the time after which the verifier with the given address can be removed.\n // Removal of the verifier with address A is allowed only in the case the value\n // of unlockedForRemovalTime[A] != 0 and unlockedForRemovalTime[A] \u003c (current time).\n mapping(address =\u003e uint256) unlockedForRemovalTime;\n }\n}\n"},"Governance.sol":{"content":"/*\n Copyright 2019-2021 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\nimport \"MGovernance.sol\";\n\n/*\n Implements Generic Governance, applicable for both proxy and main contract, and possibly others.\n Notes:\n The use of the same function names by both the Proxy and a delegated implementation\n is not possible since calling the implementation functions is done via the default function\n of the Proxy. For this reason, for example, the implementation of MainContract (MainGovernance)\n exposes mainIsGovernor, which calls the internal isGovernor method.\n*/\nabstract contract Governance is MGovernance {\n event LogNominatedGovernor(address nominatedGovernor);\n event LogNewGovernorAccepted(address acceptedGovernor);\n event LogRemovedGovernor(address removedGovernor);\n event LogNominationCancelled();\n\n function getGovernanceInfo() internal view virtual returns (GovernanceInfoStruct storage);\n\n /*\n Current code intentionally prevents governance re-initialization.\n This may be a problem in an upgrade situation, in a case that the upgrade-to implementation\n performs an initialization (for real) and within that calls initGovernance().\n\n Possible workarounds:\n 1. Clearing the governance info altogether by changing the MAIN_GOVERNANCE_INFO_TAG.\n This will remove existing main governance information.\n 2. Modify the require part in this function, so that it will exit quietly\n when trying to re-initialize (uncomment the lines below).\n */\n function initGovernance() internal {\n GovernanceInfoStruct storage gub = getGovernanceInfo();\n require(!gub.initialized, \"ALREADY_INITIALIZED\");\n gub.initialized = true; // to ensure addGovernor() won\u0027t fail.\n // Add the initial governer.\n addGovernor(msg.sender);\n }\n\n function isGovernor(address testGovernor) internal view override returns (bool) {\n GovernanceInfoStruct storage gub = getGovernanceInfo();\n return gub.effectiveGovernors[testGovernor];\n }\n\n /*\n Cancels the nomination of a governor candidate.\n */\n function cancelNomination() internal onlyGovernance {\n GovernanceInfoStruct storage gub = getGovernanceInfo();\n gub.candidateGovernor = address(0x0);\n emit LogNominationCancelled();\n }\n\n function nominateNewGovernor(address newGovernor) internal onlyGovernance {\n GovernanceInfoStruct storage gub = getGovernanceInfo();\n require(!isGovernor(newGovernor), \"ALREADY_GOVERNOR\");\n gub.candidateGovernor = newGovernor;\n emit LogNominatedGovernor(newGovernor);\n }\n\n /*\n The addGovernor is called in two cases:\n 1. by acceptGovernance when a new governor accepts its role.\n 2. by initGovernance to add the initial governor.\n The difference is that the init path skips the nominate step\n that would fail because of the onlyGovernance modifier.\n */\n function addGovernor(address newGovernor) private {\n require(!isGovernor(newGovernor), \"ALREADY_GOVERNOR\");\n GovernanceInfoStruct storage gub = getGovernanceInfo();\n gub.effectiveGovernors[newGovernor] = true;\n }\n\n function acceptGovernance() internal {\n // The new governor was proposed as a candidate by the current governor.\n GovernanceInfoStruct storage gub = getGovernanceInfo();\n require(msg.sender == gub.candidateGovernor, \"ONLY_CANDIDATE_GOVERNOR\");\n\n // Update state.\n addGovernor(gub.candidateGovernor);\n gub.candidateGovernor = address(0x0);\n\n // Send a notification about the change of governor.\n emit LogNewGovernorAccepted(msg.sender);\n }\n\n /*\n Remove a governor from office.\n */\n function removeGovernor(address governorForRemoval) internal onlyGovernance {\n require(msg.sender != governorForRemoval, \"GOVERNOR_SELF_REMOVE\");\n GovernanceInfoStruct storage gub = getGovernanceInfo();\n require(isGovernor(governorForRemoval), \"NOT_GOVERNOR\");\n gub.effectiveGovernors[governorForRemoval] = false;\n emit LogRemovedGovernor(governorForRemoval);\n }\n}\n"},"GovernanceStorage.sol":{"content":"/*\n Copyright 2019-2021 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\nimport \"MGovernance.sol\";\n\n/*\n Holds the governance slots for ALL entities, including proxy and the main contract.\n*/\ncontract GovernanceStorage {\n // A map from a Governor tag to its own GovernanceInfoStruct.\n mapping(string =\u003e GovernanceInfoStruct) internal governanceInfo; //NOLINT uninitialized-state.\n}\n"},"MGovernance.sol":{"content":"/*\n Copyright 2019-2021 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\nstruct GovernanceInfoStruct {\n mapping(address =\u003e bool) effectiveGovernors;\n address candidateGovernor;\n bool initialized;\n}\n\nabstract contract MGovernance {\n function isGovernor(address testGovernor) internal view virtual returns (bool);\n\n /*\n Allows calling the function only by a Governor.\n */\n modifier onlyGovernance() {\n require(isGovernor(msg.sender), \"ONLY_GOVERNANCE\");\n _;\n }\n}\n"},"Proxy.sol":{"content":"/*\n Copyright 2019-2021 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\nimport \"ProxyGovernance.sol\";\nimport \"ProxyStorage.sol\";\nimport \"StorageSlots.sol\";\nimport \"Common.sol\";\n\n/**\n The Proxy contract implements delegation of calls to other contracts (`implementations`), with\n proper forwarding of return values and revert reasons. This pattern allows retaining the contract\n storage while replacing implementation code.\n\n The following operations are supported by the proxy contract:\n\n - :sol:func:`addImplementation`: Defines a new implementation, the data with which it should be initialized and whether this will be the last version of implementation.\n - :sol:func:`upgradeTo`: Once an implementation is added, the governor may upgrade to that implementation only after a safety time period has passed (time lock), the current implementation is not the last version and the implementation is not frozen (see :sol:mod:`FullWithdrawals`).\n - :sol:func:`removeImplementation`: Any announced implementation may be removed. Removing an implementation is especially important once it has been used for an upgrade in order to avoid an additional unwanted revert to an older version.\n\n The only entity allowed to perform the above operations is the proxy governor\n (see :sol:mod:`ProxyGovernance`).\n\n Every implementation is required to have an `initialize` function that replaces the constructor\n of a normal contract. Furthermore, the only parameter of this function is an array of bytes\n (`data`) which may be decoded arbitrarily by the `initialize` function. It is up to the\n implementation to ensure that this function cannot be run more than once if so desired.\n\n When an implementation is added (:sol:func:`addImplementation`) the initialization `data` is also\n announced, allowing users of the contract to analyze the full effect of an upgrade to the new\n implementation. During an :sol:func:`upgradeTo`, the `data` is provided again and only if it is\n identical to the announced `data` is the upgrade performed by pointing the proxy to the new\n implementation and calling its `initialize` function with this `data`.\n\n It is the responsibility of the implementation not to overwrite any storage belonging to the\n proxy (`ProxyStorage`). In addition, upon upgrade, the new implementation is assumed to be\n backward compatible with previous implementations with respect to the storage used until that\n point.\n*/\ncontract Proxy is ProxyStorage, ProxyGovernance, StorageSlots {\n // Emitted when the active implementation is replaced.\n event ImplementationUpgraded(address indexed implementation, bytes initializer);\n\n // Emitted when an implementation is submitted as an upgrade candidate and a time lock\n // is activated.\n event ImplementationAdded(address indexed implementation, bytes initializer, bool finalize);\n\n // Emitted when an implementation is removed from the list of upgrade candidates.\n event ImplementationRemoved(address indexed implementation, bytes initializer, bool finalize);\n\n // Emitted when the implementation is finalized.\n event FinalizedImplementation(address indexed implementation);\n\n using Addresses for address;\n\n string public constant PROXY_VERSION = \"3.0.0\";\n\n constructor(uint256 upgradeActivationDelay) public {\n initGovernance();\n setUpgradeActivationDelay(upgradeActivationDelay);\n }\n\n function setUpgradeActivationDelay(uint256 delayInSeconds) private {\n bytes32 slot = UPGRADE_DELAY_SLOT;\n assembly {\n sstore(slot, delayInSeconds)\n }\n }\n\n function getUpgradeActivationDelay() public view returns (uint256 delay) {\n bytes32 slot = UPGRADE_DELAY_SLOT;\n assembly {\n delay := sload(slot)\n }\n return delay;\n }\n\n /*\n Returns the address of the current implementation.\n */\n // NOLINTNEXTLINE external-function.\n function implementation() public view returns (address _implementation) {\n bytes32 slot = IMPLEMENTATION_SLOT;\n assembly {\n _implementation := sload(slot)\n }\n }\n\n /*\n Returns true if the implementation is frozen.\n If the implementation was not assigned yet, returns false.\n */\n function implementationIsFrozen() private returns (bool) {\n address _implementation = implementation();\n\n // We can\u0027t call low level implementation before it\u0027s assigned. (i.e. ZERO).\n if (_implementation == address(0x0)) {\n return false;\n }\n\n // NOLINTNEXTLINE: low-level-calls.\n (bool success, bytes memory returndata) = _implementation.delegatecall(\n abi.encodeWithSignature(\"isFrozen()\")\n );\n require(success, string(returndata));\n return abi.decode(returndata, (bool));\n }\n\n /*\n This method blocks delegation to initialize().\n Only upgradeTo should be able to delegate call to initialize().\n */\n function initialize(\n bytes calldata /*data*/\n ) external pure {\n revert(\"CANNOT_CALL_INITIALIZE\");\n }\n\n modifier notFinalized() {\n require(isNotFinalized(), \"IMPLEMENTATION_FINALIZED\");\n _;\n }\n\n /*\n Forbids calling the function if the implementation is frozen.\n This modifier relies on the lower level (logical contract) implementation of isFrozen().\n */\n modifier notFrozen() {\n require(!implementationIsFrozen(), \"STATE_IS_FROZEN\");\n _;\n }\n\n /*\n This entry point serves only transactions with empty calldata. (i.e. pure value transfer tx).\n We don\u0027t expect to receive such, thus block them.\n */\n receive() external payable {\n revert(\"CONTRACT_NOT_EXPECTED_TO_RECEIVE\");\n }\n\n /*\n Contract\u0027s default function. Delegates execution to the implementation contract.\n It returns back to the external caller whatever the implementation delegated code returns.\n */\n fallback() external payable {\n address _implementation = implementation();\n require(_implementation != address(0x0), \"MISSING_IMPLEMENTATION\");\n\n assembly {\n // Copy msg.data. We take full control of memory in this inline assembly\n // block because it will not return to Solidity code. We overwrite the\n // Solidity scratch pad at memory position 0.\n calldatacopy(0, 0, calldatasize())\n\n // Call the implementation.\n // out and outsize are 0 for now, as we don\u0027t know the out size yet.\n let result := delegatecall(gas(), _implementation, 0, calldatasize(), 0, 0)\n\n // Copy the returned data.\n returndatacopy(0, 0, returndatasize())\n\n switch result\n // delegatecall returns 0 on error.\n case 0 {\n revert(0, returndatasize())\n }\n default {\n return(0, returndatasize())\n }\n }\n }\n\n /*\n Sets the implementation address of the proxy.\n */\n function setImplementation(address newImplementation) private {\n bytes32 slot = IMPLEMENTATION_SLOT;\n assembly {\n sstore(slot, newImplementation)\n }\n }\n\n /*\n Returns true if the contract is not in the finalized state.\n */\n function isNotFinalized() public view returns (bool notFinal) {\n bytes32 slot = FINALIZED_STATE_SLOT;\n uint256 slotValue;\n assembly {\n slotValue := sload(slot)\n }\n notFinal = (slotValue == 0);\n }\n\n /*\n Marks the current implementation as finalized.\n */\n function setFinalizedFlag() private {\n bytes32 slot = FINALIZED_STATE_SLOT;\n assembly {\n sstore(slot, 0x1)\n }\n }\n\n /*\n Introduce an implementation and its initialization vector,\n and start the time-lock before it can be upgraded to.\n addImplementation is not blocked when frozen or finalized.\n (upgradeTo API is blocked when finalized or frozen).\n */\n function addImplementation(\n address newImplementation,\n bytes calldata data,\n bool finalize\n ) external onlyGovernance {\n require(newImplementation.isContract(), \"ADDRESS_NOT_CONTRACT\");\n\n bytes32 implVectorHash = keccak256(abi.encode(newImplementation, data, finalize));\n\n uint256 activationTime = block.timestamp + getUpgradeActivationDelay();\n\n // First implementation should not have time-lock.\n if (implementation() == address(0x0)) {\n activationTime = block.timestamp;\n }\n\n enabledTime[implVectorHash] = activationTime;\n emit ImplementationAdded(newImplementation, data, finalize);\n }\n\n /*\n Removes a candidate implementation.\n Note that it is possible to remove the current implementation. Doing so doesn\u0027t affect the\n current implementation, but rather revokes it as a future candidate.\n */\n function removeImplementation(\n address removedImplementation,\n bytes calldata data,\n bool finalize\n ) external onlyGovernance {\n bytes32 implVectorHash = keccak256(abi.encode(removedImplementation, data, finalize));\n\n // If we have initializer, we set the hash of it.\n uint256 activationTime = enabledTime[implVectorHash];\n require(activationTime \u003e 0, \"UNKNOWN_UPGRADE_INFORMATION\");\n delete enabledTime[implVectorHash];\n emit ImplementationRemoved(removedImplementation, data, finalize);\n }\n\n /*\n Upgrades the proxy to a new implementation, with its initialization.\n to upgrade successfully, implementation must have been added time-lock agreeably\n before, and the init vector must be identical ot the one submitted before.\n\n Upon assignment of new implementation address,\n its initialize will be called with the initializing vector (even if empty).\n Therefore, the implementation MUST must have such a method.\n\n Note - Initialization data is committed to in advance, therefore it must remain valid\n until the actual contract upgrade takes place.\n\n Care should be taken regarding initialization data and flow when planning the contract upgrade.\n\n When planning contract upgrade, special care is also needed with regard to governance\n (See comments in Governance.sol).\n */\n // NOLINTNEXTLINE: reentrancy-events timestamp.\n function upgradeTo(\n address newImplementation,\n bytes calldata data,\n bool finalize\n ) external payable onlyGovernance notFinalized notFrozen {\n bytes32 implVectorHash = keccak256(abi.encode(newImplementation, data, finalize));\n uint256 activationTime = enabledTime[implVectorHash];\n require(activationTime \u003e 0, \"UNKNOWN_UPGRADE_INFORMATION\");\n require(newImplementation.isContract(), \"ADDRESS_NOT_CONTRACT\");\n // NOLINTNEXTLINE: timestamp.\n require(activationTime \u003c= block.timestamp, \"UPGRADE_NOT_ENABLED_YET\");\n\n setImplementation(newImplementation);\n\n // NOLINTNEXTLINE: low-level-calls controlled-delegatecall.\n (bool success, bytes memory returndata) = newImplementation.delegatecall(\n abi.encodeWithSelector(this.initialize.selector, data)\n );\n require(success, string(returndata));\n\n // Verify that the new implementation is not frozen post initialization.\n // NOLINTNEXTLINE: low-level-calls controlled-delegatecall.\n (success, returndata) = newImplementation.delegatecall(\n abi.encodeWithSignature(\"isFrozen()\")\n );\n require(success, \"CALL_TO_ISFROZEN_REVERTED\");\n require(!abi.decode(returndata, (bool)), \"NEW_IMPLEMENTATION_FROZEN\");\n\n if (finalize) {\n setFinalizedFlag();\n emit FinalizedImplementation(newImplementation);\n }\n\n emit ImplementationUpgraded(newImplementation, data);\n }\n}\n"},"ProxyGovernance.sol":{"content":"/*\n Copyright 2019-2021 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\nimport \"Governance.sol\";\nimport \"GovernanceStorage.sol\";\n\n/**\n The Proxy contract is governed by one or more Governors of which the initial one is the\n deployer of the contract.\n\n A governor has the sole authority to perform the following operations:\n\n 1. Nominate additional governors (:sol:func:`proxyNominateNewGovernor`)\n 2. Remove other governors (:sol:func:`proxyRemoveGovernor`)\n 3. Add new `implementations` (proxied contracts)\n 4. Remove (new or old) `implementations`\n 5. Update `implementations` after a timelock allows it\n\n Adding governors is performed in a two step procedure:\n\n 1. First, an existing governor nominates a new governor (:sol:func:`proxyNominateNewGovernor`)\n 2. Then, the new governor must accept governance to become a governor (:sol:func:`proxyAcceptGovernance`)\n\n This two step procedure ensures that a governor public key cannot be nominated unless there is an\n entity that has the corresponding private key. This is intended to prevent errors in the addition\n process.\n\n The governor private key should typically be held in a secure cold wallet or managed via a\n multi-sig contract.\n*/\n/*\n Implements Governance for the proxy contract.\n It is a thin wrapper to the Governance contract,\n which is needed so that it can have non-colliding function names,\n and a specific tag (key) to allow unique state storage.\n*/\ncontract ProxyGovernance is GovernanceStorage, Governance {\n // The tag is the string key that is used in the Governance storage mapping.\n string public constant PROXY_GOVERNANCE_TAG = \"StarkEx.Proxy.2019.GovernorsInformation\";\n\n /*\n Returns the GovernanceInfoStruct associated with the governance tag.\n */\n function getGovernanceInfo() internal view override returns (GovernanceInfoStruct storage) {\n return governanceInfo[PROXY_GOVERNANCE_TAG];\n }\n\n function proxyIsGovernor(address testGovernor) external view returns (bool) {\n return isGovernor(testGovernor);\n }\n\n function proxyNominateNewGovernor(address newGovernor) external {\n nominateNewGovernor(newGovernor);\n }\n\n function proxyRemoveGovernor(address governorForRemoval) external {\n removeGovernor(governorForRemoval);\n }\n\n function proxyAcceptGovernance() external {\n acceptGovernance();\n }\n\n function proxyCancelNomination() external {\n cancelNomination();\n }\n}\n"},"ProxyStorage.sol":{"content":"/*\n Copyright 2019-2021 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\nimport \"GovernanceStorage.sol\";\n\n/*\n Holds the Proxy-specific state variables.\n This contract is inherited by the GovernanceStorage (and indirectly by MainStorage)\n to prevent collision hazard.\n*/\ncontract ProxyStorage is GovernanceStorage {\n // NOLINTNEXTLINE: naming-convention uninitialized-state.\n mapping(address =\u003e bytes32) internal initializationHash_DEPRECATED;\n\n // The time after which we can switch to the implementation.\n // Hash(implementation, data, finalize) =\u003e time.\n mapping(bytes32 =\u003e uint256) internal enabledTime;\n\n // A central storage of the flags whether implementation has been initialized.\n // Note - it can be used flexibly enough to accommodate multiple levels of initialization\n // (i.e. using different key salting schemes for different initialization levels).\n mapping(bytes32 =\u003e bool) internal initialized;\n}\n"},"StorageSlots.sol":{"content":"/*\n Copyright 2019-2021 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\n/**\n StorageSlots holds the arbitrary storage slots used throughout the Proxy pattern.\n Storage address slots are a mechanism to define an arbitrary location, that will not be\n overlapped by the logical contracts.\n*/\ncontract StorageSlots {\n // Storage slot with the address of the current implementation.\n // The address of the slot is keccak256(\"StarkWare2019.implemntation-slot\").\n // We need to keep this variable stored outside of the commonly used space,\n // so that it\u0027s not overrun by the logical implementation (the proxied contract).\n bytes32 internal constant IMPLEMENTATION_SLOT =\n 0x177667240aeeea7e35eabe3a35e18306f336219e1386f7710a6bf8783f761b24;\n\n // Storage slot with the address of the call-proxy current implementation.\n // The address of the slot is keccak256(\"\u0027StarkWare2020.CallProxy.Implemntation.Slot\u0027\").\n // We need to keep this variable stored outside of the commonly used space.\n // so that it\u0027s not overrun by the logical implementation (the proxied contract).\n bytes32 internal constant CALL_PROXY_IMPL_SLOT =\n 0x7184681641399eb4ad2fdb92114857ee6ff239f94ad635a1779978947b8843be;\n\n // This storage slot stores the finalization flag.\n // Once the value stored in this slot is set to non-zero\n // the proxy blocks implementation upgrades.\n // The current implementation is then referred to as Finalized.\n // Web3.solidityKeccak([\u0027string\u0027], [\"StarkWare2019.finalization-flag-slot\"]).\n bytes32 internal constant FINALIZED_STATE_SLOT =\n 0x7d433c6f837e8f93009937c466c82efbb5ba621fae36886d0cac433c5d0aa7d2;\n\n // Storage slot to hold the upgrade delay (time-lock).\n // The intention of this slot is to allow modification using an EIC.\n // Web3.solidityKeccak([\u0027string\u0027], [\u0027StarkWare.Upgradibility.Delay.Slot\u0027]).\n bytes32 public constant UPGRADE_DELAY_SLOT =\n 0xc21dbb3089fcb2c4f4c6a67854ab4db2b0f233ea4b21b21f912d52d18fc5db1f;\n}\n"}}
File 6 of 8: CallProxy
{"BlockDirectCall.sol":{"content":"/*\n Copyright 2019-2022 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\n/*\n This contract provides means to block direct call of an external function.\n A derived contract (e.g. MainDispatcherBase) should decorate sensitive functions with the\n notCalledDirectly modifier, thereby preventing it from being called directly, and allowing only calling\n using delegate_call.\n\n This Guard contract uses pseudo-random slot, So each deployed contract would have its own guard.\n*/\nabstract contract BlockDirectCall {\n bytes32 immutable UNIQUE_SAFEGUARD_SLOT; // NOLINT naming-convention.\n\n constructor() internal {\n // The slot is pseudo-random to allow hierarchy of contracts with guarded functions.\n bytes32 slot = keccak256(abi.encode(this, block.timestamp, gasleft()));\n UNIQUE_SAFEGUARD_SLOT = slot;\n assembly {\n sstore(slot, 42)\n }\n }\n\n modifier notCalledDirectly() {\n {\n // Prevent too many local variables in stack.\n uint256 safeGuardValue;\n bytes32 slot = UNIQUE_SAFEGUARD_SLOT;\n assembly {\n safeGuardValue := sload(slot)\n }\n require(safeGuardValue == 0, \"DIRECT_CALL_DISALLOWED\");\n }\n _;\n }\n}\n"},"CallProxy.sol":{"content":"/*\n Copyright 2019-2022 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\nimport \"IFactRegistry.sol\";\nimport \"StorageSlots.sol\";\nimport \"BlockDirectCall.sol\";\nimport \"Common.sol\";\n\n/**\n CallProxy is a \u0027call\u0027 based proxy.\n It is a facade to a real implementation,\n only that unlike the Proxy pattern, it uses call and not delegatecall,\n so that the state is recorded on the called contract.\n\n This contract is expected to be placed behind the regular proxy,\n thus:\n 1. Implementation address is stored in a hashed slot (other than proxy\u0027s one...).\n 2. No state variable is allowed in low address ranges.\n 3. Setting of implementation is done in initialize.\n 4. isFrozen and initialize are implemented, to be compliant with Proxy.\n\n This implementation is intentionally minimal,\n and has no management or governance.\n The assumption is that if a different implementation is needed, it will be performed\n in an upgradeTo a new deployed CallProxy, pointing to a new implementation.\n*/\n// NOLINTNEXTLINE locked-ether.\ncontract CallProxy is BlockDirectCall, StorageSlots {\n using Addresses for address;\n\n string public constant CALL_PROXY_VERSION = \"3.1.0\";\n\n // Proxy client - initialize \u0026 isFrozen.\n // NOLINTNEXTLINE: external-function.\n function isFrozen() public pure returns (bool) {\n return false;\n }\n\n /*\n This function is called by the Proxy upon activating an implementation.\n The data passed in to this function contains the implementation address,\n and if applicable, an address of an EIC (ExternalInitializerContract) and its data.\n\n The expected data format is as following:\n\n Case I (no EIC):\n data.length == 64.\n [0 :32] implementation address\n [32:64] Zero address.\n\n Case II (EIC):\n data length \u003e= 64\n [0 :32] implementation address\n [32:64] EIC address\n [64: ] EIC init data.\n */\n function initialize(bytes calldata data) external notCalledDirectly {\n require(data.length \u003e= 64, \"INCORRECT_DATA_SIZE\");\n (address impl, address eic) = abi.decode(data, (address, address));\n require(impl.isContract(), \"ADDRESS_NOT_CONTRACT\");\n setCallProxyImplementation(impl);\n if (eic != address(0x0)) {\n callExternalInitializer(eic, data[64:]);\n } else {\n require(data.length == 64, \"INVALID_INIT_DATA\");\n }\n }\n\n function callExternalInitializer(address externalInitializerAddr, bytes calldata eicData)\n private\n {\n require(externalInitializerAddr.isContract(), \"EIC_NOT_A_CONTRACT\");\n\n // NOLINTNEXTLINE: low-level-calls, controlled-delegatecall.\n (bool success, bytes memory returndata) = externalInitializerAddr.delegatecall(\n abi.encodeWithSelector(this.initialize.selector, eicData)\n );\n require(success, string(returndata));\n require(returndata.length == 0, string(returndata));\n }\n\n /*\n Returns the call proxy implementation address.\n */\n function callProxyImplementation() public view returns (address _implementation) {\n bytes32 slot = CALL_PROXY_IMPL_SLOT;\n assembly {\n _implementation := sload(slot)\n }\n }\n\n /*\n Sets the call proxy implementation address.\n */\n function setCallProxyImplementation(address newImplementation) private {\n bytes32 slot = CALL_PROXY_IMPL_SLOT;\n assembly {\n sstore(slot, newImplementation)\n }\n }\n\n /*\n An explicit isValid entry point, used to make isValid a part of the ABI and visible\n on Etherscan (and alike).\n */\n function isValid(bytes32 fact) external view returns (bool) {\n return IFactRegistry(callProxyImplementation()).isValid(fact);\n }\n\n /*\n This entry point serves only transactions with empty calldata. (i.e. pure value transfer tx).\n We don\u0027t expect to receive such, thus block them.\n */\n receive() external payable {\n revert(\"CONTRACT_NOT_EXPECTED_TO_RECEIVE\");\n }\n\n /*\n Contract\u0027s default function. Pass execution to the implementation contract (using call).\n It returns back to the external caller whatever the implementation called code returns.\n */\n fallback() external payable {\n // NOLINT locked-ether.\n address _implementation = callProxyImplementation();\n require(_implementation != address(0x0), \"MISSING_IMPLEMENTATION\");\n uint256 value = msg.value;\n assembly {\n // Copy msg.data. We take full control of memory in this inline assembly\n // block because it will not return to Solidity code. We overwrite the\n // Solidity scratch pad at memory position 0.\n calldatacopy(0, 0, calldatasize())\n\n // Call the implementation.\n // out and outsize are 0 for now, as we don\u0027t know the out size yet.\n let result := call(gas(), _implementation, value, 0, calldatasize(), 0, 0)\n\n // Copy the returned data.\n returndatacopy(0, 0, returndatasize())\n\n switch result\n // delegatecall returns 0 on error.\n case 0 {\n revert(0, returndatasize())\n }\n default {\n return(0, returndatasize())\n }\n }\n }\n}\n"},"Common.sol":{"content":"/*\n Copyright 2019-2022 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\n/*\n Common Utility librarries.\n I. Addresses (extending address).\n*/\nlibrary Addresses {\n function isContract(address account) internal view returns (bool) {\n uint256 size;\n assembly {\n size := extcodesize(account)\n }\n return size \u003e 0;\n }\n\n function performEthTransfer(address recipient, uint256 amount) internal {\n (bool success, ) = recipient.call{value: amount}(\"\"); // NOLINT: low-level-calls.\n require(success, \"ETH_TRANSFER_FAILED\");\n }\n\n /*\n Safe wrapper around ERC20/ERC721 calls.\n This is required because many deployed ERC20 contracts don\u0027t return a value.\n See https://github.com/ethereum/solidity/issues/4116.\n */\n function safeTokenContractCall(address tokenAddress, bytes memory callData) internal {\n require(isContract(tokenAddress), \"BAD_TOKEN_ADDRESS\");\n // NOLINTNEXTLINE: low-level-calls.\n (bool success, bytes memory returndata) = tokenAddress.call(callData);\n require(success, string(returndata));\n\n if (returndata.length \u003e 0) {\n require(abi.decode(returndata, (bool)), \"TOKEN_OPERATION_FAILED\");\n }\n }\n\n /*\n Validates that the passed contract address is of a real contract,\n and that its id hash (as infered fromn identify()) matched the expected one.\n */\n function validateContractId(address contractAddress, bytes32 expectedIdHash) internal {\n require(isContract(contractAddress), \"ADDRESS_NOT_CONTRACT\");\n (bool success, bytes memory returndata) = contractAddress.call( // NOLINT: low-level-calls.\n abi.encodeWithSignature(\"identify()\")\n );\n require(success, \"FAILED_TO_IDENTIFY_CONTRACT\");\n string memory realContractId = abi.decode(returndata, (string));\n require(\n keccak256(abi.encodePacked(realContractId)) == expectedIdHash,\n \"UNEXPECTED_CONTRACT_IDENTIFIER\"\n );\n }\n\n /*\n Similar to safeTokenContractCall, but always ignores the return value.\n\n Assumes some other method is used to detect the failures\n (e.g. balance is checked before and after the call).\n */\n function uncheckedTokenContractCall(address tokenAddress, bytes memory callData) internal {\n // NOLINTNEXTLINE: low-level-calls.\n (bool success, bytes memory returndata) = tokenAddress.call(callData);\n require(success, string(returndata));\n }\n}\n\n/*\n II. StarkExTypes - Common data types.\n*/\nlibrary StarkExTypes {\n // Structure representing a list of verifiers (validity/availability).\n // A statement is valid only if all the verifiers in the list agree on it.\n // Adding a verifier to the list is immediate - this is used for fast resolution of\n // any soundness issues.\n // Removing from the list is time-locked, to ensure that any user of the system\n // not content with the announced removal has ample time to leave the system before it is\n // removed.\n struct ApprovalChainData {\n address[] list;\n // Represents the time after which the verifier with the given address can be removed.\n // Removal of the verifier with address A is allowed only in the case the value\n // of unlockedForRemovalTime[A] != 0 and unlockedForRemovalTime[A] \u003c (current time).\n mapping(address =\u003e uint256) unlockedForRemovalTime;\n }\n}\n"},"IFactRegistry.sol":{"content":"/*\n Copyright 2019-2022 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\n/*\n The Fact Registry design pattern is a way to separate cryptographic verification from the\n business logic of the contract flow.\n\n A fact registry holds a hash table of verified \"facts\" which are represented by a hash of claims\n that the registry hash check and found valid. This table may be queried by accessing the\n isValid() function of the registry with a given hash.\n\n In addition, each fact registry exposes a registry specific function for submitting new claims\n together with their proofs. The information submitted varies from one registry to the other\n depending of the type of fact requiring verification.\n\n For further reading on the Fact Registry design pattern see this\n `StarkWare blog post \u003chttps://medium.com/starkware/the-fact-registry-a64aafb598b6\u003e`_.\n*/\ninterface IFactRegistry {\n /*\n Returns true if the given fact was previously registered in the contract.\n */\n function isValid(bytes32 fact) external view returns (bool);\n}\n"},"StorageSlots.sol":{"content":"/*\n Copyright 2019-2022 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\n/**\n StorageSlots holds the arbitrary storage slots used throughout the Proxy pattern.\n Storage address slots are a mechanism to define an arbitrary location, that will not be\n overlapped by the logical contracts.\n*/\ncontract StorageSlots {\n // Storage slot with the address of the current implementation.\n // The address of the slot is keccak256(\"StarkWare2019.implemntation-slot\").\n // We need to keep this variable stored outside of the commonly used space,\n // so that it\u0027s not overrun by the logical implementation (the proxied contract).\n bytes32 internal constant IMPLEMENTATION_SLOT =\n 0x177667240aeeea7e35eabe3a35e18306f336219e1386f7710a6bf8783f761b24;\n\n // Storage slot with the address of the call-proxy current implementation.\n // The address of the slot is keccak256(\"\u0027StarkWare2020.CallProxy.Implemntation.Slot\u0027\").\n // We need to keep this variable stored outside of the commonly used space.\n // so that it\u0027s not overrun by the logical implementation (the proxied contract).\n bytes32 internal constant CALL_PROXY_IMPL_SLOT =\n 0x7184681641399eb4ad2fdb92114857ee6ff239f94ad635a1779978947b8843be;\n\n // This storage slot stores the finalization flag.\n // Once the value stored in this slot is set to non-zero\n // the proxy blocks implementation upgrades.\n // The current implementation is then referred to as Finalized.\n // Web3.solidityKeccak([\u0027string\u0027], [\"StarkWare2019.finalization-flag-slot\"]).\n bytes32 internal constant FINALIZED_STATE_SLOT =\n 0x7d433c6f837e8f93009937c466c82efbb5ba621fae36886d0cac433c5d0aa7d2;\n\n // Storage slot to hold the upgrade delay (time-lock).\n // The intention of this slot is to allow modification using an EIC.\n // Web3.solidityKeccak([\u0027string\u0027], [\u0027StarkWare.Upgradibility.Delay.Slot\u0027]).\n bytes32 public constant UPGRADE_DELAY_SLOT =\n 0xc21dbb3089fcb2c4f4c6a67854ab4db2b0f233ea4b21b21f912d52d18fc5db1f;\n}\n"}}
File 7 of 8: GpsStatementVerifier
/* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity >=0.6.12; /* Common Utility Libraries. I. Addresses (extending address). */ library Addresses { /* Note: isContract function has some known limitation. See https://github.com/OpenZeppelin/ openzeppelin-contracts/blob/master/contracts/utils/Address.sol. */ function isContract(address account) internal view returns (bool) { uint256 size; assembly { size := extcodesize(account) } return size > 0; } function performEthTransfer(address recipient, uint256 amount) internal { if (amount == 0) return; (bool success, ) = recipient.call{value: amount}(""); // NOLINT: low-level-calls. require(success, "ETH_TRANSFER_FAILED"); } /* Safe wrapper around ERC20/ERC721 calls. This is required because many deployed ERC20 contracts don't return a value. See https://github.com/ethereum/solidity/issues/4116. */ function safeTokenContractCall(address tokenAddress, bytes memory callData) internal { require(isContract(tokenAddress), "BAD_TOKEN_ADDRESS"); // NOLINTNEXTLINE: low-level-calls. (bool success, bytes memory returndata) = tokenAddress.call(callData); require(success, string(returndata)); if (returndata.length > 0) { require(abi.decode(returndata, (bool)), "TOKEN_OPERATION_FAILED"); } } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // ---------- The following code was auto-generated. PLEASE DO NOT EDIT. ---------- // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; contract CairoBootloaderProgramSize { uint256 internal constant PROGRAM_SIZE = 794; } contract CairoBootloaderProgram is CairoBootloaderProgramSize { function getCompiledProgram() external pure returns (uint256[PROGRAM_SIZE] memory) { return [ 290341444919459839, 11, 1226245742482522112, 778, 74168662805676031, 0, 2345108766317314046, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020480, 5198420613823102976, 3618502788666131213697322783095070105623107215331596699973092056135872020479, 2345108766317314046, 146226256843603965, 4, 5191102238658887680, 2345108766317314046, 290341444919459839, 3, 4632937381316558848, 4612671182992932865, 4612671182992998402, 146226256843603968, 4, 74168662805676031, 4, 4612671182993063937, 4612671182993129474, 5198983563776196608, 1, 5198983563776262144, 1, 5200109459388203008, 5200109459388268544, 5198983563776458752, 3618502788666131213697322783095070105623107215331596699973092056135872020480, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020458, 2345108766317314046, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020449, 5207990763031068672, 10, 5198420613823168512, 12, 5191102230068953088, 5191102234363920384, 5191102242953854976, 5201798292068466688, 5191102238658887680, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020444, 4623648689905041407, 291467327646433279, 2345108766317314046, 5199827962936983548, 5208553695804948479, 4612389708016287743, 5198983563776262144, 1, 2345108766317314046, 146226256843603965, 4, 5191102230068953088, 2345108766317314046, 5191102230068953088, 5188850460319711232, 5188850460319776768, 5188850460319842304, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020467, 5198983563776262144, 1, 5198983563776327680, 1, 5198983563776393216, 1, 5198983563776458752, 3618502788666131213697322783095070105623107215331596699973092056135872020480, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020463, 2345108766317314046, 5189976364521848832, 0, 5189976364521848832, 0, 5189976364521848832, 0, 5191102247248822272, 5191102238658887680, 290341444919459839, 1, 145944781866893311, 4, 74168662805676031, 58, 5188287510366486528, 5201798304953368576, 4611826758062997503, 5188287514661322752, 5201798304953303040, 4611826762357833727, 4611826766652801016, 5188287523251191808, 5188287518956093440, 5201798304953696256, 4611826783832473599, 5188287527545962496, 5188287523250864128, 5201798304953696256, 4611826788127244287, 5188287531840733184, 4611826792422146047, 5188287549020536832, 5188287527545569280, 5201798304953696256, 4611826809601818623, 5188287553315307520, 5188287531840339968, 5201798304953696256, 4611826813896589311, 5188287557610078208, 4611826818191491071, 5188287574789881856, 5188287536135045120, 5201798304953696256, 4611826835371163647, 5188287579084652544, 5188287540429815808, 5201798304953696256, 4611826839665934335, 5188287583379423232, 4611826843960836095, 5188287600559226880, 5188287544724520960, 5201798304953696256, 4611826861140508671, 5188287604853997568, 5188287549019291648, 5201798304953696256, 4611826865435279359, 5188287609148768256, 4611826869730181119, 5188287626328571904, 5188287630623473664, 5188287634918375424, 5198420613820743680, 10, 5198420613820743680, 30, 74168662805676031, 3618502788666131213697322783095070105623107215331596699973092056135872020421, 290341444919459839, 1, 145944781866893311, 4, 74168662805676031, 18, 5188287510366420992, 5201798304953303040, 4611826758062931967, 5188287514661257216, 5201798304953237504, 4611826762357768191, 4611826766652735479, 5188287523251126272, 5188287527546028032, 5188287531840929792, 5198420613822513152, 2, 5198420613822513152, 6, 74168662805676031, 3618502788666131213697322783095070105623107215331596699973092056135872020399, 5200109442208464896, 5201798287773958143, 145944781866893311, 11, 5198420613822644224, 1, 4611826758062866431, 4611826762357833719, 4611826766652801016, 5198420613822840832, 6, 5188287523251126272, 2345108766317314046, 4613515612218425343, 1, 5188287510366289920, 5201798304953171968, 4611826758062800895, 5198420613822578688, 1, 4611826762357702655, 4611826766652669942, 5198420613822709760, 6, 5188287523250995200, 2345108766317314046, 5188850460319907840, 5202361254907052032, 5191102242953854976, 5188287510366552064, 5188287506071519232, 5188287510366486527, 4611826762357964797, 5198420613822906368, 3618502788666131213697322783095070105623107215331596699973092056135872020480, 5198420613822906368, 3, 5188287518956224512, 4623085744246521853, 145944781866893308, 3618502788666131213697322783095070105623107215331596699973092056135872020472, 2345108766317314046, 5198983563776458752, 3618502788666131213697322783095070105623107215331596699973092056135872020480, 145944781866893311, 12, 5191102238658887680, 5188850460319842304, 5198983563776393216, 1, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020339, 5191102234363920384, 5193354047062507520, 5193354047062507520, 2345108766317314046, 5191102234363920384, 5191102242953854976, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020449, 5193354051357474816, 5191102238658887680, 5193354047062507520, 2345108766317314046, 290341444919459839, 30, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020248, 4617174774030761984, 4612671182992932866, 5189976364521848832, 0, 4612389712311713791, 5188850464614612992, 5188850490384416768, 5191102264428691456, 5191102247248822272, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020446, 4612389712311779327, 4622804286450008067, 4, 4612671195878359044, 5200109476568596480, 5188850468910104576, 4625619027626983429, 4622804286450073606, 2, 4617174761145860103, 4612671191582867464, 4612671195877834761, 4612671200172802058, 4612671204467769355, 4612671208762736652, 4617174765440827405, 4612671217352671246, 4612671221647638543, 4612671225942605840, 5191102238658887680, 5198983563776655360, 6, 5189976364521848832, 11, 5191102273018626048, 5191102277313593344, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020241, 1191342862550269952, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020207, 4623648724266090495, 5191102238658887680, 5198983563776655360, 18, 5191102273018626048, 5191102333148168192, 5189976364521848832, 11, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020202, 4623648719970271231, 5191102234363920384, 5198983563776655360, 6, 5198983563776655360, 18, 5191102242953854976, 5189976364521848832, 11, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020239, 4623930190653653010, 4612671182993522717, 5198983563776655360, 18, 5193354051357474816, 2345108766317314046, 290341444919459839, 35, 4622804286449090561, 1, 4614922931267403778, 4614922982807011331, 4614922939857338372, 4614922944152305669, 4614922948447272966, 4614922952742240263, 4614922957037207560, 4614922961332174857, 4614922965627142154, 4614922969922109451, 4613797087195136012, 122550255383924, 4613797087195136013, 8098989891770344814, 4613797087195136014, 138277649577220228665140075, 4613797087195136015, 435459224417, 4613797087195136016, 27700496658166629, 4613797087195136017, 435458895728, 4613797087195136018, 118083203326315, 4613797087195136019, 8101821134059892590, 4613797087195136020, 9062164042692704905798619969846, 4613797087195136021, 27413455001448292, 4613797087195136022, 30809880779386724, 4613797087195136023, 1, 4613797087195136024, 3, 4613797087195136025, 1, 4613797087195136026, 2, 4613797087195136027, 5, 4613797087195136028, 7, 4613797087195136029, 16, 4613797087195136030, 6, 4613797087195136031, 1, 4613797087195136032, 7, 4613797087195136033, 7, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020119, 5198420613823102976, 1, 5191102212889083904, 5198420613822971904, 12, 5198420613822906368, 23, 5188850460319252480, 1226245742482522112, 38, 4614641507830300671, 4617174774030762018, 5188850468912267264, 5201798300658860031, 5189976364521848832, 64, 1226245742482522112, 14, 5188850460322332672, 5188850464617299968, 5188850468912267264, 5188850473207234560, 5188850477502201856, 5188850481797169152, 5188850486092136448, 5188850490387103744, 5188850494682071040, 5188850498977038336, 5188850503272005632, 2345108766317314046, 146226256843603965, 5, 4613797087195135996, 0, 2345108766317314046, 290341444919459839, 1, 5201798304953761792, 5202079779930537980, 4634344751905079295, 5193354047062507520, 5198983563776458752, 3618502788666131213697322783095070105623107215331596699973092056135872020480, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020468, 2345108766317314046, 146226256843603965, 5, 5191102230068953088, 5191102234363920384, 2345108766317314046, 290341444919459839, 1, 5191102230068953088, 5191102234363920384, 5191102238658887680, 5191102242953854976, 5193354038472572928, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020290, 5191102238658887680, 5191102242953854976, 5198983563776458752, 3618502788666131213697322783095070105623107215331596699973092056135872020480, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020463, 2345108766317314046, 4612671182993129469, 5198983563776393216, 1, 2345108766317314046, 5191102238658887680, 5199827967231950845, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020475, 2345108766317314046, 5191102238658887680, 5191102242953854976, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020470, 5191102242953854976, 5191102247248822272, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020470, 2345108766317314046, 290341444919459839, 1, 5191102230068953088, 5191102260133724160, 5198983563776393216, 3618502788666131213697322783095070105623107215331596699973092056135872020480, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020466, 5209116658642944000, 5202361254906855424, 4612108233039904765, 5193354047062507520, 5193354051357474816, 2345108766317314046, 4612671182993063932, 4612671187288031229, 5198983563776327680, 3, 5188850468909711360, 2345108766317314046, 290341444919459839, 2, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020010, 4613797087195136000, 0, 4613797087195136001, 0, 5193354051357474816, 2345108766317314046, 290341444919459839, 2, 5191102234363920384, 5191102242953854976, 5191102247248822272, 5188850460319776768, 1226245742482522112, 16, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872019994, 4617174769735794688, 5188850464614744064, 4623367219223429121, 5193354038472572928, 5193354042767540224, 2345108766317314046, 5191102242953854976, 5188850460319907840, 5188850464614875136, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020446, 2345108766317314046, 146226256843603964, 5, 5191102234363920384, 5191102247248822272, 2345108766317314046, 290341444919459839, 1, 5198983563776393216, 3618502788666131213697322783095070105623107215331596699973092056135872020480, 4626181977580208128, 5191102238658887680, 5191102234363920384, 5191102247248822272, 5202079771340603392, 4611826758063063038, 5188287510366420992, 4611826762357964799, 5198420613822906368, 1, 5198420613822906368, 3, 5188287518956224512, 145944781866893307, 3618502788666131213697322783095070105623107215331596699973092056135872020472, 2345108766317314046, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020424, 5191102238658887680, 5193354051357474816, 5191102242953854976, 5191102247248822272, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020428, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020442, 2345108766317314046, 146226256843603965, 3, 2345108766317314046, 5191102238658887680, 5191102242953854976, 5188287510366617600, 4611826758063063039, 5198420613823037441, 1, 5198420613823037441, 1, 722405534170316798, 3618502788666131213697322783095070105623107215331596699973092056135872020475, 4623648689905041407, 2345108766317314046, 290341444919459839, 14, 5191102260133724160, 5191102208594116608, 5191102212889083904, 5191102217184051200, 5191102221479018496, 5191102225773985792, 5191102230068953088, 5191102234363920384, 5191102238658887680, 5191102242953854976, 5191102247248822272, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020220, 4617174743965990913, 4617174748260958210, 4617174752555925507, 4617174756850892804, 4617174761145860101, 4617174765440827398, 4617174769735794695, 4617174774030761992, 4617174778325729289, 4617174735376056330, 4614922926972436492, 5191102204299149312, 5193354012702769152, 5191102307378364416, 1226245742482522112, 75, 4617174774030761997, 5198420613823102976, 1, 5193354051357474816, 5191102285903527936, 5191102264428691456, 5189976364521848832, 0, 5198983563776655360, 1, 5191102307378364416, 5188850460320104448, 5191102311673331712, 1226245742482522112, 16, 4617174778325729290, 4612389708017532926, 5193354034177605632, 5193354034177605632, 5193354038472572928, 5191102268723658752, 5191102273018626048, 5191102277313593344, 5191102281608560640, 5193354012702769152, 5191102290198495232, 5191102294493462528, 5191102298788429824, 2345108766317314046, 146226256843603964, 9, 5191102212889083904, 5191102217184051200, 5191102221479018496, 5191102225773985792, 5191102230068953088, 5191102234363920384, 2345108766317314046, 290341444919459839, 0, 290341444919459839, 1, 145944781866893311, 14, 5191102212889083904, 5191102217184051200, 5191102221479018496, 5191102225773985792, 5191102230068953088, 5191102234363920384, 5191102238658887680, 5191102247248822272, 1226245742482522112, 39, 74168662805676031, 12, 5191102212889083904, 5191102217184051200, 5191102221479018496, 5191102225773985792, 5191102230068953088, 5191102234363920384, 5191102238658887680, 5191102247248822272, 1226245742482522112, 74, 5191102238658887680, 5198983563776393216, 3618502788666131213697322783095070105623107215331596699973092056135872020480, 5191102247248822272, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020440, 2345108766317314046, 5188850460319907840, 4612389708016353279, 5188850473204809728, 4612389712311320575, 5191102242953854976, 5188850468909842432, 5188850464614875136, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020344, 4612389716606287871, 5198983563776327680, 3, 5193354047062507520, 2345108766317314046, 5198983563776458752, 2, 5191102247248822272, 2345108766317314046, 290341444919459839, 4, 5191102225773985792, 5191102264428691456, 5191102260133724160, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872019877, 5191102238658887680, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020469, 5189976364521848832, 4, 4611826758063128575, 5191102230068953088, 5188850468909776896, 5189976364521848832, 1, 5188850464614809600, 5188287514661257216, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020237, 5188850460319842304, 4611826758060965887, 4611826762355933145, 4622241336494227458, 2, 4614922982807011331, 5191102217184051200, 5191102221479018496, 5193353879558782976, 5193354034177605632, 5191102234363920384, 5198983563776655360, 1, 5191102242953854976, 5188850460320104448, 5191102247248822272, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020382, 4623648711380271103, 5193354034177605632, 5193354034177605632, 5193354034177605632, 5193354034177605632, 5193354034177605632, 5191102268723658752, 2345108766317314046, 290341444919459839, 4, 5191102238658887680, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020427, 4617174774030761984, 5188287514661584896, 5188850473204744192, 5201798304953827326, 145944781866893311, 16, 4613797087195136003, 4, 5188287510366420992, 4623648719970074623, 4612671182993391618, 5198983563776655360, 1, 5191102247248822272, 5189976364521848832, 3, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020276, 74168662805676031, 6, 4612108233039708161, 4612108237334675458, 4613797087195136003, 0, 4612671182992736257, 4612671187287703554, 5198983563776000000, 2, 5200109472273432576, 5198983563776720896, 3618502788666131213697322783095070105623107215331596699973092056135872020479, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020261, 5198983563776000000, 2, 5198983563776720896, 3618502788666131213697322783095070105623107215331596699973092056135872020479, 5200109472273432576, 5198983563776720896, 3618502788666131213697322783095070105623107215331596699973092056135872020479, 5201798296363630592, 5191102221479018496, 5191102225773985792, 5191102230068953088, 5198983563776262144, 1, 5201798283478532096, 2345108766317314046, 5191102204299149312, 5191102208594116608, 5191102212889083904, 5191102217184051200, 5191102221479018496, 5191102225773985792, 5191102230068953088, 5191102234363920384, 5191102238658887680, 5191102242953854976, 5191102247248822272, 1226245742482522112, 3618502788666131213697322783095070105623107215331596699973092056135872020248, 2345108766317314046 ]; } } // ---------- End of auto-generated code. ---------- /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; abstract contract CairoVerifierContract { function verifyProofExternal( uint256[] calldata proofParams, uint256[] calldata proof, uint256[] calldata publicInput ) external virtual; /* Returns information that is related to the layout. publicMemoryOffset is the offset of the public memory pages' information in the public input. selectedBuiltins is a bit-map of builtins that are present in the layout. */ function getLayoutInfo() external pure virtual returns (uint256 publicMemoryOffset, uint256 selectedBuiltins); uint256 internal constant OUTPUT_BUILTIN_BIT = 0; uint256 internal constant PEDERSEN_BUILTIN_BIT = 1; uint256 internal constant RANGE_CHECK_BUILTIN_BIT = 2; uint256 internal constant ECDSA_BUILTIN_BIT = 3; uint256 internal constant BITWISE_BUILTIN_BIT = 4; uint256 internal constant EC_OP_BUILTIN_BIT = 5; uint256 internal constant KECCAK_BUILTIN_BIT = 6; uint256 internal constant POSEIDON_BUILTIN_BIT = 7; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // ---------- The following code was auto-generated. PLEASE DO NOT EDIT. ---------- // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./PageInfo.sol"; contract CpuPublicInputOffsetsBase is PageInfo { // The following constants are offsets of data expected in the public input. uint256 internal constant OFFSET_N_VERIFIER_FRIENDLY_LAYERS = 0; uint256 internal constant OFFSET_LOG_N_STEPS = 1; uint256 internal constant OFFSET_RC_MIN = 2; uint256 internal constant OFFSET_RC_MAX = 3; uint256 internal constant OFFSET_LAYOUT_CODE = 4; uint256 internal constant OFFSET_PROGRAM_BEGIN_ADDR = 5; uint256 internal constant OFFSET_PROGRAM_STOP_PTR = 6; uint256 internal constant OFFSET_EXECUTION_BEGIN_ADDR = 7; uint256 internal constant OFFSET_EXECUTION_STOP_PTR = 8; uint256 internal constant OFFSET_OUTPUT_BEGIN_ADDR = 9; uint256 internal constant OFFSET_OUTPUT_STOP_PTR = 10; uint256 internal constant OFFSET_PEDERSEN_BEGIN_ADDR = 11; uint256 internal constant OFFSET_PEDERSEN_STOP_PTR = 12; uint256 internal constant OFFSET_RANGE_CHECK_BEGIN_ADDR = 13; uint256 internal constant OFFSET_RANGE_CHECK_STOP_PTR = 14; // The program segment starts from 1, so that memory address 0 is kept for the null pointer. uint256 internal constant INITIAL_PC = 1; // The first Cairo instructions are: // ap += n_args; call main; jmp rel 0. // As the first two instructions occupy 2 cells each, the "jmp rel 0" instruction is at // offset 4 relative to INITIAL_PC. uint256 internal constant FINAL_PC = INITIAL_PC + 4; } // ---------- End of auto-generated code. ---------- /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../interfaces/IQueryableFactRegistry.sol"; contract FactRegistry is IQueryableFactRegistry { // Mapping: fact hash -> true. mapping(bytes32 => bool) private verifiedFact; // Indicates whether the Fact Registry has at least one fact registered. bool anyFactRegistered = false; /* Checks if a fact was registered. */ function isValid(bytes32 fact) external view virtual override returns (bool) { return internalIsValid(fact); } /* The internal implementation that checks if the fact was registered. */ function internalIsValid(bytes32 fact) internal view virtual returns (bool) { return verifiedFact[fact]; } function registerFact(bytes32 factHash) internal { // This function stores the fact hash in the mapping. verifiedFact[factHash] = true; // Mark first time off. if (!anyFactRegistered) { anyFactRegistered = true; } } /* Indicates whether at least one fact was registered. */ function hasRegisteredFact() external view override returns (bool) { return anyFactRegistered; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../../components/ReferableFactRegistry.sol"; import "../cpu/CpuPublicInputOffsetsBase.sol"; /* A utility contract to parse the GPS output. See registerGpsFacts for more details. */ contract GpsOutputParser is CpuPublicInputOffsetsBase, ReferableFactRegistry { uint256 internal constant METADATA_TASKS_OFFSET = 1; uint256 internal constant METADATA_OFFSET_TASK_OUTPUT_SIZE = 0; uint256 internal constant METADATA_OFFSET_TASK_PROGRAM_HASH = 1; uint256 internal constant METADATA_OFFSET_TASK_N_TREE_PAIRS = 2; uint256 internal constant METADATA_TASK_HEADER_SIZE = 3; uint256 internal constant METADATA_OFFSET_TREE_PAIR_N_PAGES = 0; uint256 internal constant METADATA_OFFSET_TREE_PAIR_N_NODES = 1; uint256 internal constant NODE_STACK_OFFSET_HASH = 0; uint256 internal constant NODE_STACK_OFFSET_END = 1; // The size of each node in the node stack. uint256 internal constant NODE_STACK_ITEM_SIZE = 2; uint256 internal constant FIRST_CONTINUOUS_PAGE_INDEX = 1; /* Logs the program output fact together with the relevant continuous memory pages' hashes. The event is emitted for each registered fact. */ event LogMemoryPagesHashes(bytes32 programOutputFact, bytes32[] pagesHashes); constructor(address refFactRegistry, uint256 referralDuration) public ReferableFactRegistry(refFactRegistry, referralDuration) {} /* Parses the GPS program output (using taskMetadata, which should be verified by the caller), and registers the facts of the tasks which were executed. The first entry in taskMetadata is the number of tasks. For each task, the structure is as follows: 1. Size (including the size and hash fields). 2. Program hash. 3. The number of pairs in the Merkle tree structure (see below). 4. The Merkle tree structure (see below). The fact of each task is stored as a (non-binary) Merkle tree. Leaf nodes are labeled with the hash of their data. Each non-leaf node is labeled as 1 + the hash of (node0, end0, node1, end1, ...) where node* is a label of a child children and end* is the total number of data words up to and including that node and its children (including the previous sibling nodes). We add 1 to the result of the hash to prevent an attacker from using a preimage of a leaf node as a preimage of a non-leaf hash and vice versa. The structure of the tree is passed as a list of pairs (n_pages, n_nodes), and the tree is constructed using a stack of nodes (initialized to an empty stack) by repeating for each pair: 1. Add n_pages to the stack of nodes. 2. Pop the top n_nodes, construct a parent node for them, and push it back to the stack. After applying the steps above, the stack much contain exactly one node, which will constitute the root of the Merkle tree. For example, [(2, 2)] will create a Merkle tree with a root and two direct children, while [(3, 2), (0, 2)] will create a Merkle tree with a root whose left child is a leaf and right child has two leaf children. Assumptions: taskMetadata and cairoAuxInput are verified externally. */ function registerGpsFacts( uint256[] calldata taskMetadata, uint256[] memory publicMemoryPages, uint256 outputStartAddress ) internal { uint256 totalNumPages = publicMemoryPages[0]; // Allocate some of the loop variables here to avoid the stack-too-deep error. uint256 task; uint256 nTreePairs; uint256 nTasks = taskMetadata[0]; // Contains fact hash with the relevant memory pages' hashes. // Size is bounded from above with the total number of pages. Three extra places are // dedicated for the fact hash and the array address and length. uint256[] memory pageHashesLogData = new uint256[](totalNumPages + 3); // Relative address to the beginning of the memory pages' hashes in the array. pageHashesLogData[1] = 0x40; uint256 taskMetadataOffset = METADATA_TASKS_OFFSET; // Skip the first 6 output cells, which contain the bootloader config (3 cells), the number // of tasks and the size and program hash of the first task. curAddr points to the output of // the first task. uint256 curAddr = outputStartAddress + 6; // Skip the main page. uint256 curPage = FIRST_CONTINUOUS_PAGE_INDEX; // Bound the size of the stack by the total number of pages. // TODO(lior, 15/04/2022): Get a better bound on the size of the stack. uint256[] memory nodeStack = new uint256[](NODE_STACK_ITEM_SIZE * totalNumPages); // Copy to memory to workaround the "stack too deep" error. uint256[] memory taskMetadataCopy = taskMetadata; uint256[PAGE_INFO_SIZE] memory pageInfoPtr; assembly { // Skip the array length and the first page. pageInfoPtr := add(add(publicMemoryPages, 0x20), PAGE_INFO_SIZE_IN_BYTES) } // Register the fact for each task. for (task = 0; task < nTasks; task++) { uint256 curOffset = 0; uint256 firstPageOfTask = curPage; nTreePairs = taskMetadataCopy[taskMetadataOffset + METADATA_OFFSET_TASK_N_TREE_PAIRS]; // Build the Merkle tree using a stack (see the function documentation) to compute // the fact. uint256 nodeStackLen = 0; for (uint256 treePair = 0; treePair < nTreePairs; treePair++) { // Add nPages to the stack of nodes. uint256 nPages = taskMetadataCopy[ taskMetadataOffset + METADATA_TASK_HEADER_SIZE + 2 * treePair + METADATA_OFFSET_TREE_PAIR_N_PAGES ]; // Ensure 'nPages' is bounded from above as a sanity check // (the bound is somewhat arbitrary). require(nPages < 2**20, "Invalid value of n_pages in tree structure."); for (uint256 i = 0; i < nPages; i++) { (uint256 pageSize, uint256 pageHash) = pushPageToStack( pageInfoPtr, curAddr, curOffset, nodeStack, nodeStackLen ); pageHashesLogData[curPage - firstPageOfTask + 3] = pageHash; curPage += 1; nodeStackLen += 1; curAddr += pageSize; curOffset += pageSize; assembly { pageInfoPtr := add(pageInfoPtr, PAGE_INFO_SIZE_IN_BYTES) } } // Pop the top n_nodes, construct a parent node for them, and push it back to the // stack. uint256 nNodes = taskMetadataCopy[ taskMetadataOffset + METADATA_TASK_HEADER_SIZE + 2 * treePair + METADATA_OFFSET_TREE_PAIR_N_NODES ]; if (nNodes != 0) { nodeStackLen = constructNode(nodeStack, nodeStackLen, nNodes); } } require(nodeStackLen == 1, "Node stack must contain exactly one item."); uint256 programHash = taskMetadataCopy[ taskMetadataOffset + METADATA_OFFSET_TASK_PROGRAM_HASH ]; // Verify that the sizes of the pages correspond to the task output, to make // sure that the computed hash is indeed the hash of the entire output of the task. { uint256 outputSize = taskMetadataCopy[ taskMetadataOffset + METADATA_OFFSET_TASK_OUTPUT_SIZE ]; require( nodeStack[NODE_STACK_OFFSET_END] + 2 == outputSize, "The sum of the page sizes does not match output size." ); } uint256 programOutputFact = nodeStack[NODE_STACK_OFFSET_HASH]; bytes32 fact = keccak256(abi.encode(programHash, programOutputFact)); // Update taskMetadataOffset. taskMetadataOffset += METADATA_TASK_HEADER_SIZE + 2 * nTreePairs; { // Log the output Merkle root with the hashes of the relevant memory pages. // Instead of emit, we use log1 https://docs.soliditylang.org/en/v0.4.24/assembly.html, // https://docs.soliditylang.org/en/v0.6.2/abi-spec.html#use-of-dynamic-types. bytes32 logHash = keccak256("LogMemoryPagesHashes(bytes32,bytes32[])"); assembly { let buf := add(pageHashesLogData, 0x20) // Number of memory pages that are relevant for this fact. let length := sub(curPage, firstPageOfTask) mstore(buf, programOutputFact) mstore(add(buf, 0x40), length) log1(buf, mul(add(length, 3), 0x20), logHash) } } registerFact(fact); // Move curAddr to the output of the next task (skipping the size and hash fields). curAddr += 2; } require(totalNumPages == curPage, "Not all memory pages were processed."); } /* Push one page (curPage) to the top of the node stack. curAddr is the memory address, curOffset is the offset from the beginning of the task output. Verifies that the page has the right start address and returns the page size and the page hash. */ function pushPageToStack( uint256[PAGE_INFO_SIZE] memory pageInfoPtr, uint256 curAddr, uint256 curOffset, uint256[] memory nodeStack, uint256 nodeStackLen ) private pure returns (uint256 pageSize, uint256 pageHash) { // Read the first address, page size and hash. uint256 pageAddr = pageInfoPtr[PAGE_INFO_ADDRESS_OFFSET]; pageSize = pageInfoPtr[PAGE_INFO_SIZE_OFFSET]; pageHash = pageInfoPtr[PAGE_INFO_HASH_OFFSET]; // Ensure 'pageSize' is bounded as a sanity check (the bound is somewhat arbitrary). require(pageSize < 2**30, "Invalid page size."); require(pageAddr == curAddr, "Invalid page address."); nodeStack[NODE_STACK_ITEM_SIZE * nodeStackLen + NODE_STACK_OFFSET_END] = curOffset + pageSize; nodeStack[NODE_STACK_ITEM_SIZE * nodeStackLen + NODE_STACK_OFFSET_HASH] = pageHash; } /* Pops the top nNodes nodes from the stack and pushes one parent node instead. Returns the new value of nodeStackLen. */ function constructNode( uint256[] memory nodeStack, uint256 nodeStackLen, uint256 nNodes ) private pure returns (uint256) { require(nNodes <= nodeStackLen, "Invalid value of n_nodes in tree structure."); // The end of the node is the end of the last child. uint256 newNodeEnd = nodeStack[ NODE_STACK_ITEM_SIZE * (nodeStackLen - 1) + NODE_STACK_OFFSET_END ]; uint256 newStackLen = nodeStackLen - nNodes; // Compute node hash. uint256 nodeStart = 0x20 + newStackLen * NODE_STACK_ITEM_SIZE * 0x20; uint256 newNodeHash; assembly { newNodeHash := keccak256( add(nodeStack, nodeStart), mul( nNodes, // NODE_STACK_ITEM_SIZE * 0x20 = 0x40 ) ) } nodeStack[NODE_STACK_ITEM_SIZE * newStackLen + NODE_STACK_OFFSET_END] = newNodeEnd; // Add one to the new node hash to distinguish it from the hash of a leaf (a page). nodeStack[NODE_STACK_ITEM_SIZE * newStackLen + NODE_STACK_OFFSET_HASH] = newNodeHash + 1; return newStackLen + 1; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../cpu/CairoBootloaderProgram.sol"; import "../cpu/CairoVerifierContract.sol"; import "../cpu/MemoryPageFactRegistry.sol"; import "../../interfaces/Identity.sol"; import "../PrimeFieldElement0.sol"; import "./GpsOutputParser.sol"; contract GpsStatementVerifier is GpsOutputParser, Identity, CairoBootloaderProgramSize, PrimeFieldElement0 { CairoBootloaderProgram bootloaderProgramContractAddress; MemoryPageFactRegistry memoryPageFactRegistry; CairoVerifierContract[] cairoVerifierContractAddresses; uint256 internal constant N_BUILTINS = 11; uint256 internal constant N_MAIN_ARGS = N_BUILTINS; uint256 internal constant N_MAIN_RETURN_VALUES = N_BUILTINS; // Cairo verifier program hash. uint256 immutable hashedSupportedCairoVerifiers_; // Simple bootloader program hash. uint256 immutable simpleBootloaderProgramHash_; // Applicative bootloader program hash. uint256 immutable applicativeBootloaderProgramHash_; /* Constructs an instance of GpsStatementVerifier. bootloaderProgramContract is the address of the bootloader program contract and cairoVerifierContracts is a list of cairoVerifiers indexed by their id. */ constructor( address bootloaderProgramContract, address memoryPageFactRegistry_, address[] memory cairoVerifierContracts, uint256 simpleBootloaderProgramHash, uint256 applicativeBootloaderProgramHash, uint256 hashedSupportedCairoVerifiers, address referenceVerifier, uint256 referralDurationSeconds ) public GpsOutputParser(referenceVerifier, referralDurationSeconds) { bootloaderProgramContractAddress = CairoBootloaderProgram(bootloaderProgramContract); memoryPageFactRegistry = MemoryPageFactRegistry(memoryPageFactRegistry_); cairoVerifierContractAddresses = new CairoVerifierContract[](cairoVerifierContracts.length); for (uint256 i = 0; i < cairoVerifierContracts.length; ++i) { cairoVerifierContractAddresses[i] = CairoVerifierContract(cairoVerifierContracts[i]); } hashedSupportedCairoVerifiers_ = hashedSupportedCairoVerifiers; simpleBootloaderProgramHash_ = simpleBootloaderProgramHash; applicativeBootloaderProgramHash_ = applicativeBootloaderProgramHash; } function identify() external pure override returns (string memory) { return "StarkWare_GpsStatementVerifier_2024_10"; } /* Returns the bootloader config. */ function getBootloaderConfig() external view returns ( uint256, uint256, uint256 ) { return ( simpleBootloaderProgramHash_, applicativeBootloaderProgramHash_, hashedSupportedCairoVerifiers_ ); } /* Verifies a proof and registers the corresponding facts. For the structure of cairoAuxInput, see cpu/CpuPublicInputOffsets.sol. taskMetadata is structured as follows: 1. Number of tasks. 2. For each task: 1. Task output size (including program hash and size). 2. Program hash. */ function verifyProofAndRegister( uint256[] calldata proofParams, uint256[] calldata proof, uint256[] calldata taskMetadata, uint256[] calldata cairoAuxInput, uint256 cairoVerifierId ) external { require( cairoVerifierId < cairoVerifierContractAddresses.length, "cairoVerifierId is out of range." ); CairoVerifierContract cairoVerifier = cairoVerifierContractAddresses[cairoVerifierId]; // The values z and alpha are used only for the fact registration of the main page. // They are not part of the public input of CpuVerifier as they are computed there. // Take the relevant slice from 'cairoAuxInput'. uint256[] calldata cairoPublicInput = ( cairoAuxInput[:cairoAuxInput.length - // z and alpha. 2] ); uint256[] memory publicMemoryPages; { (uint256 publicMemoryOffset, uint256 selectedBuiltins) = cairoVerifier.getLayoutInfo(); require(cairoAuxInput.length > publicMemoryOffset, "Invalid cairoAuxInput length."); publicMemoryPages = (uint256[])(cairoPublicInput[publicMemoryOffset:]); uint256 nPages = publicMemoryPages[0]; require(nPages < 10000, "Invalid nPages."); // Validate publicMemoryPages.length. // Each page has a page info and a cumulative product. // There is no 'page address' in the page info for page 0, but this 'free' slot is // used to store the number of pages. require( publicMemoryPages.length == nPages * (PAGE_INFO_SIZE + 1), "Invalid publicMemoryPages length." ); // Process public memory. ( uint256 publicMemoryLength, uint256 memoryHash, uint256 prod ) = registerPublicMemoryMainPage(taskMetadata, cairoAuxInput, selectedBuiltins); // Make sure the first page is valid. // If the size or the hash are invalid, it may indicate that there is a mismatch // between the prover and the verifier on the bootloader program or bootloader config. require( publicMemoryPages[PAGE_INFO_SIZE_OFFSET] == publicMemoryLength, "Invalid size for memory page 0." ); require( publicMemoryPages[PAGE_INFO_HASH_OFFSET] == memoryHash, "Invalid hash for memory page 0." ); require( publicMemoryPages[nPages * PAGE_INFO_SIZE] == prod, "Invalid cumulative product for memory page 0." ); } // NOLINTNEXTLINE: reentrancy-benign. cairoVerifier.verifyProofExternal(proofParams, proof, (uint256[])(cairoPublicInput)); registerGpsFacts(taskMetadata, publicMemoryPages, cairoAuxInput[OFFSET_OUTPUT_BEGIN_ADDR]); } /* Registers the fact for memory page 0, which includes: 1. The bootloader program, 2. Arguments and return values of main() 3. Some of the data required for computing the task facts. which is represented in taskMetadata. Returns information on the registered fact. Arguments: selectedBuiltins: A bit-map of builtins that are present in the layout. See CairoVerifierContract.sol for more information. taskMetadata: Per task metadata. cairoAuxInput: Auxiliary input for the cairo verifier. Assumptions: cairoAuxInput is connected to the public input, which is verified by cairoVerifierContractAddresses. Guarantees: taskMetadata is consistent with the public memory, with some sanity checks. */ function registerPublicMemoryMainPage( uint256[] calldata taskMetadata, uint256[] calldata cairoAuxInput, uint256 selectedBuiltins ) private returns ( uint256 publicMemoryLength, uint256 memoryHash, uint256 prod ) { uint256 nTasks = taskMetadata[0]; // Ensure 'nTasks' is bounded as a sanity check (the bound is somewhat arbitrary). require(nTasks < 2**30, "Invalid number of tasks."); // Public memory length. publicMemoryLength = (PROGRAM_SIZE + // return fp and pc = 2 + N_MAIN_ARGS + N_MAIN_RETURN_VALUES + // Bootloader config size = 3 + // Number of tasks cell = 1 + 2 * nTasks); uint256[] memory publicMemory = new uint256[](MEMORY_PAIR_SIZE * publicMemoryLength); uint256 offset = 0; // Write public memory, which is a list of pairs (address, value). { // Program segment. uint256[PROGRAM_SIZE] memory bootloaderProgram = bootloaderProgramContractAddress .getCompiledProgram(); for (uint256 i = 0; i < bootloaderProgram.length; i++) { // Force that memory[i + INITIAL_PC] = bootloaderProgram[i]. publicMemory[offset] = i + INITIAL_PC; publicMemory[offset + 1] = bootloaderProgram[i]; offset += 2; } } { // Execution segment - Make sure [initial_fp - 2] = initial_fp and . // This is required for the "safe call" feature (that is, all "call" instructions will // return, even if the called function is malicious). // It guarantees that it's not possible to create a cycle in the call stack. uint256 initialFp = cairoAuxInput[OFFSET_EXECUTION_BEGIN_ADDR]; require(initialFp >= 2, "Invalid execution begin address."); publicMemory[offset + 0] = initialFp - 2; publicMemory[offset + 1] = initialFp; // Make sure [initial_fp - 1] = 0. publicMemory[offset + 2] = initialFp - 1; publicMemory[offset + 3] = 0; offset += 4; // Execution segment: Enforce main's arguments and return values. // Note that the page hash depends on the order of the (address, value) pair in the // publicMemory and consequently the arguments must be written before the return values. uint256 returnValuesAddress = cairoAuxInput[OFFSET_EXECUTION_STOP_PTR] - N_BUILTINS; uint256 builtinSegmentInfoOffset = OFFSET_OUTPUT_BEGIN_ADDR; for (uint256 i = 0; i < N_BUILTINS; i++) { // Write argument address. publicMemory[offset] = initialFp + i; uint256 returnValueOffset = offset + 2 * N_BUILTINS; // Write return value address. publicMemory[returnValueOffset] = returnValuesAddress + i; // Write values. if ((selectedBuiltins & 1) != 0) { // Set the argument to the builtin start pointer. publicMemory[offset + 1] = cairoAuxInput[builtinSegmentInfoOffset]; // Set the return value to the builtin stop pointer. publicMemory[returnValueOffset + 1] = cairoAuxInput[ builtinSegmentInfoOffset + 1 ]; builtinSegmentInfoOffset += 2; } else { // Builtin is not present in layout, set the argument value and return value to 0. publicMemory[offset + 1] = 0; publicMemory[returnValueOffset + 1] = 0; } offset += 2; selectedBuiltins >>= 1; } require(selectedBuiltins == 0, "SELECTED_BUILTINS_VECTOR_IS_TOO_LONG"); // Skip the return values which were already written. offset += 2 * N_BUILTINS; } // Program output. { { uint256 outputAddress = cairoAuxInput[OFFSET_OUTPUT_BEGIN_ADDR]; // Force that memory[outputAddress: outputAddress + 3] contain the bootloader config // (which is 3 words size). publicMemory[offset + 0] = outputAddress; publicMemory[offset + 1] = simpleBootloaderProgramHash_; publicMemory[offset + 2] = outputAddress + 1; publicMemory[offset + 3] = applicativeBootloaderProgramHash_; publicMemory[offset + 4] = outputAddress + 2; publicMemory[offset + 5] = hashedSupportedCairoVerifiers_; // Force that memory[outputAddress + 3] = nTasks. publicMemory[offset + 6] = outputAddress + 3; publicMemory[offset + 7] = nTasks; offset += 8; outputAddress += 4; uint256[] calldata taskMetadataSlice = taskMetadata[METADATA_TASKS_OFFSET:]; for (uint256 task = 0; task < nTasks; task++) { uint256 outputSize = taskMetadataSlice[METADATA_OFFSET_TASK_OUTPUT_SIZE]; // Ensure 'outputSize' is at least 2 and bounded from above as a sanity check // (the bound is somewhat arbitrary). require(2 <= outputSize && outputSize < 2**30, "Invalid task output size."); uint256 programHash = taskMetadataSlice[METADATA_OFFSET_TASK_PROGRAM_HASH]; uint256 nTreePairs = taskMetadataSlice[METADATA_OFFSET_TASK_N_TREE_PAIRS]; // Ensure 'nTreePairs' is at least 1 and bounded from above as a sanity check // (the bound is somewhat arbitrary). require( 1 <= nTreePairs && nTreePairs < 2**20, "Invalid number of pairs in the Merkle tree structure." ); // Force that memory[outputAddress] = outputSize. publicMemory[offset + 0] = outputAddress; publicMemory[offset + 1] = outputSize; // Force that memory[outputAddress + 1] = programHash. publicMemory[offset + 2] = outputAddress + 1; publicMemory[offset + 3] = programHash; offset += 4; outputAddress += outputSize; taskMetadataSlice = taskMetadataSlice[METADATA_TASK_HEADER_SIZE + 2 * nTreePairs:]; } require(taskMetadataSlice.length == 0, "Invalid length of taskMetadata."); require( cairoAuxInput[OFFSET_OUTPUT_STOP_PTR] == outputAddress, "Inconsistent program output length." ); } } require(publicMemory.length == offset, "Not all Cairo public inputs were written."); uint256 z = cairoAuxInput[cairoAuxInput.length - 2]; uint256 alpha = cairoAuxInput[cairoAuxInput.length - 1]; bytes32 factHash; (factHash, memoryHash, prod) = memoryPageFactRegistry.registerRegularMemoryPage( publicMemory, z, alpha, K_MODULUS ); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity >=0.6.12; /* The Fact Registry design pattern is a way to separate cryptographic verification from the business logic of the contract flow. A fact registry holds a hash table of verified "facts" which are represented by a hash of claims that the registry hash check and found valid. This table may be queried by accessing the isValid() function of the registry with a given hash. In addition, each fact registry exposes a registry specific function for submitting new claims together with their proofs. The information submitted varies from one registry to the other depending of the type of fact requiring verification. For further reading on the Fact Registry design pattern see this `StarkWare blog post <https://medium.com/starkware/the-fact-registry-a64aafb598b6>`_. */ interface IFactRegistry { /* Returns true if the given fact was previously registered in the contract. */ function isValid(bytes32 fact) external view returns (bool); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; struct MemoryPageEntry { uint256 startAddr; uint256[] values; uint256 z; uint256 alpha; uint256 prime; } interface IMemoryPageRegistry { function registerContinuousMemoryPage( uint256 startAddr, uint256[] memory values, uint256 z, uint256 alpha, uint256 prime ) external returns ( bytes32, uint256, uint256 ); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./IFactRegistry.sol"; /* Extends the IFactRegistry interface with a query method that indicates whether the fact registry has successfully registered any fact or is still empty of such facts. */ interface IQueryableFactRegistry is IFactRegistry { /* Returns true if at least one fact has been registered. */ function hasRegisteredFact() external view returns (bool); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity >=0.6.12; interface Identity { /* Allows a caller to ensure that the provided address is of the expected type and version. */ function identify() external pure returns (string memory); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; pragma experimental ABIEncoderV2; import "./IMemoryPageRegistry.sol"; import "../../components/FactRegistry.sol"; contract MemoryPageFactRegistryConstants { // A page based on a list of pairs (address, value). // In this case, memoryHash = hash(address, value, address, value, address, value, ...). uint256 internal constant REGULAR_PAGE = 0; // A page based on adjacent memory cells, starting from a given address. // In this case, memoryHash = hash(value, value, value, ...). uint256 internal constant CONTINUOUS_PAGE = 1; } /* A fact registry for the claim: I know n pairs (addr, value) for which the hash of the pairs is memoryHash, and the cumulative product: \\prod_i( z - (addr_i + alpha * value_i) ) is prod. The exact format of the hash depends on the type of the page (see MemoryPageFactRegistryConstants). The fact consists of (pageType, prime, n, z, alpha, prod, memoryHash, address). Note that address is only available for CONTINUOUS_PAGE, and otherwise it is 0. */ contract MemoryPageFactRegistry is FactRegistry, MemoryPageFactRegistryConstants, IMemoryPageRegistry { event LogMemoryPageFactRegular(bytes32 factHash, uint256 memoryHash, uint256 prod); event LogMemoryPageFactContinuous(bytes32 factHash, uint256 memoryHash, uint256 prod); /* Registers a fact based of the given memory (address, value) pairs (REGULAR_PAGE). */ function registerRegularMemoryPage( uint256[] calldata memoryPairs, uint256 z, uint256 alpha, uint256 prime ) external returns ( bytes32 factHash, uint256 memoryHash, uint256 prod ) { // Ensure 'memoryPairs.length' is bounded as a sanity check (the bound is somewhat arbitrary). require(memoryPairs.length < 2**20, "Too many memory values."); require(memoryPairs.length % 2 == 0, "Size of memoryPairs must be even."); require(z < prime, "Invalid value of z."); require(alpha < prime, "Invalid value of alpha."); (factHash, memoryHash, prod) = computeFactHash(memoryPairs, z, alpha, prime); emit LogMemoryPageFactRegular(factHash, memoryHash, prod); registerFact(factHash); } function computeFactHash( uint256[] memory memoryPairs, uint256 z, uint256 alpha, uint256 prime ) private pure returns ( bytes32 factHash, uint256 memoryHash, uint256 prod ) { uint256 memorySize = memoryPairs.length / 2; // NOLINT: divide-before-multiply. prod = 1; assembly { let memoryPtr := add(memoryPairs, 0x20) // Each value of memoryPairs is a pair: (address, value). let lastPtr := add(memoryPtr, mul(memorySize, 0x40)) for { let ptr := memoryPtr } lt(ptr, lastPtr) { ptr := add(ptr, 0x40) } { // Compute address + alpha * value. let address_value_lin_comb := addmod( // address= mload(ptr), mulmod( // value= mload(add(ptr, 0x20)), alpha, prime ), prime ) prod := mulmod(prod, add(z, sub(prime, address_value_lin_comb)), prime) } memoryHash := keccak256( memoryPtr, mul( // 0x20 * 2. 0x40, memorySize ) ) } factHash = keccak256( abi.encodePacked( REGULAR_PAGE, prime, memorySize, z, alpha, prod, memoryHash, uint256(0) ) ); } /* Receives a list of MemoryPageEntry. Each element in the list holds arguments for a seperate call to registerContinuousMemoryPage. */ function registerContinuousPageBatch(MemoryPageEntry[] calldata memoryPageEntries) external { for (uint256 i = 0; i < memoryPageEntries.length; i++) { registerContinuousMemoryPage( memoryPageEntries[i].startAddr, memoryPageEntries[i].values, memoryPageEntries[i].z, memoryPageEntries[i].alpha, memoryPageEntries[i].prime ); } } /* Registers a fact based on the given values, assuming continuous addresses. values should be [value at startAddr, value at (startAddr + 1), ...]. */ function registerContinuousMemoryPage( // NOLINT: external-function. uint256 startAddr, uint256[] memory values, uint256 z, uint256 alpha, uint256 prime ) public override returns ( bytes32 factHash, uint256 memoryHash, uint256 prod ) { require(values.length < 2**20, "Too many memory values."); require(prime < 2**254, "prime is too big for the optimizations in this function."); require(z < prime, "Invalid value of z."); require(alpha < prime, "Invalid value of alpha."); // Ensure 'startAddr' less then prime and bounded as a sanity check (the bound is somewhat arbitrary). require((startAddr < prime) && (startAddr < 2**64), "Invalid value of startAddr."); uint256 nValues = values.length; assembly { // Initialize prod to 1. prod := 1 // Initialize valuesPtr to point to the first value in the array. let valuesPtr := add(values, 0x20) let minus_z := mod(sub(prime, z), prime) // Start by processing full batches of 8 cells, addr represents the last address in each // batch. let addr := add(startAddr, 7) let lastAddr := add(startAddr, nValues) for { } lt(addr, lastAddr) { addr := add(addr, 8) } { // Compute the product of (lin_comb - z) instead of (z - lin_comb), since we're // doing an even number of iterations, the result is the same. prod := mulmod( prod, mulmod( add(add(sub(addr, 7), mulmod(mload(valuesPtr), alpha, prime)), minus_z), add( add(sub(addr, 6), mulmod(mload(add(valuesPtr, 0x20)), alpha, prime)), minus_z ), prime ), prime ) prod := mulmod( prod, mulmod( add( add(sub(addr, 5), mulmod(mload(add(valuesPtr, 0x40)), alpha, prime)), minus_z ), add( add(sub(addr, 4), mulmod(mload(add(valuesPtr, 0x60)), alpha, prime)), minus_z ), prime ), prime ) prod := mulmod( prod, mulmod( add( add(sub(addr, 3), mulmod(mload(add(valuesPtr, 0x80)), alpha, prime)), minus_z ), add( add(sub(addr, 2), mulmod(mload(add(valuesPtr, 0xa0)), alpha, prime)), minus_z ), prime ), prime ) prod := mulmod( prod, mulmod( add( add(sub(addr, 1), mulmod(mload(add(valuesPtr, 0xc0)), alpha, prime)), minus_z ), add(add(addr, mulmod(mload(add(valuesPtr, 0xe0)), alpha, prime)), minus_z), prime ), prime ) valuesPtr := add(valuesPtr, 0x100) } // Handle leftover. // Translate addr to the beginning of the last incomplete batch. addr := sub(addr, 7) for { } lt(addr, lastAddr) { addr := add(addr, 1) } { let address_value_lin_comb := addmod( addr, mulmod(mload(valuesPtr), alpha, prime), prime ) prod := mulmod(prod, add(z, sub(prime, address_value_lin_comb)), prime) valuesPtr := add(valuesPtr, 0x20) } memoryHash := keccak256(add(values, 0x20), mul(0x20, nValues)) } factHash = keccak256( abi.encodePacked(CONTINUOUS_PAGE, prime, nValues, z, alpha, prod, memoryHash, startAddr) ); emit LogMemoryPageFactContinuous(factHash, memoryHash, prod); registerFact(factHash); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; contract PageInfo { uint256 public constant PAGE_INFO_SIZE = 3; // PAGE_INFO_SIZE_IN_BYTES cannot reference PAGE_INFO_SIZE as only direct constants are // supported in assembly. uint256 public constant PAGE_INFO_SIZE_IN_BYTES = 3 * 32; uint256 public constant PAGE_INFO_ADDRESS_OFFSET = 0; uint256 public constant PAGE_INFO_SIZE_OFFSET = 1; uint256 public constant PAGE_INFO_HASH_OFFSET = 2; // A regular page entry is a (address, value) pair stored as 2 uint256 words. uint256 internal constant MEMORY_PAIR_SIZE = 2; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; contract PrimeFieldElement0 { uint256 internal constant K_MODULUS = 0x800000000000011000000000000000000000000000000000000000000000001; uint256 internal constant K_MONTGOMERY_R = 0x7fffffffffffdf0ffffffffffffffffffffffffffffffffffffffffffffffe1; uint256 internal constant K_MONTGOMERY_R_INV = 0x40000000000001100000000000012100000000000000000000000000000000; uint256 internal constant GENERATOR_VAL = 3; uint256 internal constant ONE_VAL = 1; function fromMontgomery(uint256 val) internal pure returns (uint256 res) { // uint256 res = fmul(val, kMontgomeryRInv); assembly { res := mulmod(val, K_MONTGOMERY_R_INV, K_MODULUS) } return res; } function fromMontgomeryBytes(bytes32 bs) internal pure returns (uint256) { // Assuming bs is a 256bit bytes object, in Montgomery form, it is read into a field // element. uint256 res = uint256(bs); return fromMontgomery(res); } function toMontgomeryInt(uint256 val) internal pure returns (uint256 res) { //uint256 res = fmul(val, kMontgomeryR); assembly { res := mulmod(val, K_MONTGOMERY_R, K_MODULUS) } return res; } function fmul(uint256 a, uint256 b) internal pure returns (uint256 res) { //uint256 res = mulmod(a, b, kModulus); assembly { res := mulmod(a, b, K_MODULUS) } return res; } function fadd(uint256 a, uint256 b) internal pure returns (uint256 res) { // uint256 res = addmod(a, b, kModulus); assembly { res := addmod(a, b, K_MODULUS) } return res; } function fsub(uint256 a, uint256 b) internal pure returns (uint256 res) { // uint256 res = addmod(a, kModulus - b, kModulus); assembly { res := addmod(a, sub(K_MODULUS, b), K_MODULUS) } return res; } function fpow(uint256 val, uint256 exp) internal view returns (uint256) { return expmod(val, exp, K_MODULUS); } function expmod( uint256 base, uint256 exponent, uint256 modulus ) private view returns (uint256 res) { assembly { let p := mload(0x40) mstore(p, 0x20) // Length of Base. mstore(add(p, 0x20), 0x20) // Length of Exponent. mstore(add(p, 0x40), 0x20) // Length of Modulus. mstore(add(p, 0x60), base) // Base. mstore(add(p, 0x80), exponent) // Exponent. mstore(add(p, 0xa0), modulus) // Modulus. // Call modexp precompile. if iszero(staticcall(gas(), 0x05, p, 0xc0, p, 0x20)) { revert(0, 0) } res := mload(p) } } function inverse(uint256 val) internal view returns (uint256) { return expmod(val, K_MODULUS - 2, K_MODULUS); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./FactRegistry.sol"; import "../libraries/Addresses.sol"; /* ReferableFactRegistry extends FactRegistry, so that it can be deployed with a reference FactRegistry. The reference FactRegistry is used as a secondary fact registry. When the contract is queried for fact validity using isValid(), if the queried fact is not in the local fact registry, the call is passed to the reference. The reference FactRegistry is active only for a pre-defined duration (in seconds). After that duration expires, the reference FactRegistry can not be queried anymore. */ contract ReferableFactRegistry is FactRegistry { IFactRegistry public referenceFactRegistry; uint256 public referralExpirationTime; using Addresses for address; constructor(address refFactRegistry, uint256 referralDurationSeconds) public { // Allow 0 address, i.e. no referral. if (refFactRegistry != address(0)) { referenceFactRegistry = IFactRegistry(refFactRegistry); // NOLINTNEXTLINE: no-block-members. referralExpirationTime = block.timestamp + referralDurationSeconds; require(referralExpirationTime >= block.timestamp, "DURATION_WRAP_AROUND"); require(refFactRegistry.isContract(), "REFERENCE_NOT_CONTRACT"); require(refFactRegistry != address(this), "SELF_ASSIGNMENT"); // NOLINTNEXTLINE: reentrancy-benign no-low-level-calls. (bool success, ) = refFactRegistry.staticcall( abi.encodeWithSelector( IFactRegistry(refFactRegistry).isValid.selector, bytes32(0x0) ) ); require(success, "REFERENCE_NOT_FACT_REGISTRY"); } } /* Checks if a fact was registered. */ function isValid(bytes32 fact) external view virtual override returns (bool) { if (internalIsValid(fact)) { return true; } return isValidOnReference(fact); } /* Checks if the fact is stored in the local fact registry. */ function localIsValid(bytes32 fact) external view returns (bool) { return internalIsValid(fact); } function isReferralActive() internal view returns (bool) { // solium-disable-next-line security/no-block-members return block.timestamp < referralExpirationTime; } /* Checks if a fact has been verified by the reference IFactRegistry. */ function isValidOnReference(bytes32 fact) internal view returns (bool) { if (!isReferralActive()) { return false; } return referenceFactRegistry.isValid(fact); } }
File 8 of 8: FinalizableCommittee
/* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../components/FactRegistry.sol"; import "../interfaces/IAvailabilityVerifier.sol"; import "../interfaces/Identity.sol"; contract Committee is FactRegistry, IAvailabilityVerifier, Identity { uint256 constant SIGNATURE_LENGTH = 32 * 2 + 1; // r(32) + s(32) + v(1). uint256 public signaturesRequired; mapping(address => bool) public isMember; /// @dev Contract constructor sets initial members and required number of signatures. /// @param committeeMembers List of committee members. /// @param numSignaturesRequired Number of required signatures. constructor(address[] memory committeeMembers, uint256 numSignaturesRequired) public { require(numSignaturesRequired > 0, "NO_REQUIRED_SIGNATURES"); require(numSignaturesRequired <= committeeMembers.length, "TOO_MANY_REQUIRED_SIGNATURES"); for (uint256 idx = 0; idx < committeeMembers.length; idx++) { require( !isMember[committeeMembers[idx]] && (committeeMembers[idx] != address(0)), "NON_UNIQUE_COMMITTEE_MEMBERS" ); isMember[committeeMembers[idx]] = true; } signaturesRequired = numSignaturesRequired; } function identify() external pure virtual override returns (string memory) { return "StarkWare_Committee_2022_2"; } /// @dev Verifies the availability proof. Reverts if invalid. /// An availability proof should have a form of a concatenation of ec-signatures by signatories. /// Signatures should be sorted by signatory address ascendingly. /// Signatures should be 65 bytes long. r(32) + s(32) + v(1). /// There should be at least the number of required signatures as defined in this contract /// and all signatures provided should be from signatories. /// /// See :sol:mod:`AvailabilityVerifiers` for more information on when this is used. /// /// @param claimHash The hash of the claim the committee is signing on. /// The format is keccak256(abi.encodePacked( /// newValidiumVaultRoot, validiumTreeHeight, newOrderRoot, orderTreeHeight sequenceNumber)) /// @param availabilityProofs Concatenated ec signatures by committee members. function verifyAvailabilityProof(bytes32 claimHash, bytes calldata availabilityProofs) external override { require( availabilityProofs.length >= signaturesRequired * SIGNATURE_LENGTH, "INVALID_AVAILABILITY_PROOF_LENGTH" ); uint256 offset = 0; address prevRecoveredAddress = address(0); for (uint256 proofIdx = 0; proofIdx < signaturesRequired; proofIdx++) { bytes32 r = bytesToBytes32(availabilityProofs, offset); bytes32 s = bytesToBytes32(availabilityProofs, offset + 32); uint8 v = uint8(availabilityProofs[offset + 64]); offset += SIGNATURE_LENGTH; address recovered = ecrecover(claimHash, v, r, s); // Signatures should be sorted off-chain before submitting to enable cheap uniqueness // check on-chain. require(isMember[recovered], "AVAILABILITY_PROVER_NOT_IN_COMMITTEE"); require(recovered > prevRecoveredAddress, "NON_SORTED_SIGNATURES"); prevRecoveredAddress = recovered; } registerFact(claimHash); } function bytesToBytes32(bytes memory array, uint256 offset) private pure returns (bytes32 result) { // Arrays are prefixed by a 256 bit length parameter. uint256 actualOffset = offset + 32; // Read the bytes32 from array memory. assembly { result := mload(add(array, actualOffset)) } } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../interfaces/IQueryableFactRegistry.sol"; contract FactRegistry is IQueryableFactRegistry { // Mapping: fact hash -> true. mapping(bytes32 => bool) private verifiedFact; // Indicates whether the Fact Registry has at least one fact registered. bool anyFactRegistered = false; /* Checks if a fact was registered. */ function isValid(bytes32 fact) external view virtual override returns (bool) { return internalIsValid(fact); } /* The internal implementation that checks if the fact was registered. */ function internalIsValid(bytes32 fact) internal view virtual returns (bool) { return verifiedFact[fact]; } function registerFact(bytes32 factHash) internal { // This function stores the fact hash in the mapping. verifiedFact[factHash] = true; // Mark first time off. if (!anyFactRegistered) { anyFactRegistered = true; } } /* Indicates whether at least one fact was registered. */ function hasRegisteredFact() external view override returns (bool) { return anyFactRegistered; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../interfaces/SimpleAdminable.sol"; /** A simple base class for finalizable contracts. */ abstract contract Finalizable is SimpleAdminable { event Finalized(); bool finalized; function isFinalized() public view returns (bool) { return finalized; } modifier notFinalized() { require(!isFinalized(), "FINALIZED"); _; } function finalize() external onlyAdmin notFinalized { finalized = true; emit Finalized(); } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "../components/Finalizable.sol"; import "./Committee.sol"; /** A finalizable version of Committee. Until finalized, it allows adding new members and incrementing the number of required signers. */ contract FinalizableCommittee is Finalizable, Committee { event RequiredSignersIncrement(uint256 newRequiredSigners); event NewMemberAdded(address newMember); uint256 private _memberCount; constructor(address[] memory committeeMembers, uint256 numSignaturesRequired) public Committee(committeeMembers, numSignaturesRequired) { _memberCount = committeeMembers.length; } function incrementRequiredSigners() external notFinalized onlyAdmin { require(signaturesRequired < _memberCount, "TOO_MANY_REQUIRED_SIGNATURES"); signaturesRequired += 1; emit RequiredSignersIncrement(signaturesRequired); } function addCommitteeMemeber(address newMember) external notFinalized onlyAdmin { require(newMember != address(0x0), "INVALID_MEMBER"); require(!isMember[newMember], "ALREADY_MEMBER"); isMember[newMember] = true; _memberCount += 1; emit NewMemberAdded(newMember); } function identify() external pure override returns (string memory) { return "StarkWare_FinalizableCommittee_2022_1"; } } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; interface IAvailabilityVerifier { /* Verifies the availability proof. Reverts if invalid. */ function verifyAvailabilityProof(bytes32 claimHash, bytes calldata availabilityProofs) external; } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity >=0.6.12; /* The Fact Registry design pattern is a way to separate cryptographic verification from the business logic of the contract flow. A fact registry holds a hash table of verified "facts" which are represented by a hash of claims that the registry hash check and found valid. This table may be queried by accessing the isValid() function of the registry with a given hash. In addition, each fact registry exposes a registry specific function for submitting new claims together with their proofs. The information submitted varies from one registry to the other depending of the type of fact requiring verification. For further reading on the Fact Registry design pattern see this `StarkWare blog post <https://medium.com/starkware/the-fact-registry-a64aafb598b6>`_. */ interface IFactRegistry { /* Returns true if the given fact was previously registered in the contract. */ function isValid(bytes32 fact) external view returns (bool); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; import "./IFactRegistry.sol"; /* Extends the IFactRegistry interface with a query method that indicates whether the fact registry has successfully registered any fact or is still empty of such facts. */ interface IQueryableFactRegistry is IFactRegistry { /* Returns true if at least one fact has been registered. */ function hasRegisteredFact() external view returns (bool); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity >=0.6.12; interface Identity { /* Allows a caller to ensure that the provided address is of the expected type and version. */ function identify() external pure returns (string memory); } /* Copyright 2019-2024 StarkWare Industries Ltd. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.starkware.co/open-source-license/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // SPDX-License-Identifier: Apache-2.0. pragma solidity ^0.6.12; abstract contract SimpleAdminable { address owner; address ownerCandidate; mapping(address => bool) admins; constructor() internal { owner = msg.sender; admins[msg.sender] = true; } // Admin/Owner Modifiers. modifier onlyOwner() { require(isOwner(msg.sender), "ONLY_OWNER"); _; } function isOwner(address testedAddress) public view returns (bool) { return owner == testedAddress; } modifier onlyAdmin() { require(isAdmin(msg.sender), "ONLY_ADMIN"); _; } function isAdmin(address testedAddress) public view returns (bool) { return admins[testedAddress]; } function registerAdmin(address newAdmin) external onlyOwner { if (!isAdmin(newAdmin)) { admins[newAdmin] = true; } } function removeAdmin(address removedAdmin) external onlyOwner { require(!isOwner(removedAdmin), "OWNER_CANNOT_BE_REMOVED_AS_ADMIN"); delete admins[removedAdmin]; } function nominateNewOwner(address newOwner) external onlyOwner { require(!isOwner(newOwner), "ALREADY_OWNER"); ownerCandidate = newOwner; } function acceptOwnership() external { // Previous owner is still an admin. require(msg.sender == ownerCandidate, "NOT_A_CANDIDATE"); owner = ownerCandidate; admins[ownerCandidate] = true; ownerCandidate = address(0x0); } }