Skip to content

Commit

Permalink
StarkEx v4.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gkaempfer committed Oct 13, 2022
1 parent df807db commit 210bd5f
Show file tree
Hide file tree
Showing 11 changed files with 5,874 additions and 35 deletions.
2,950 changes: 2,950 additions & 0 deletions scalable-dex/abi/StarkExchange.abi

Large diffs are not rendered by default.

2,854 changes: 2,854 additions & 0 deletions scalable-dex/abi/StarkPerpetual.abi

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions scalable-dex/contracts/src/committee/Committee.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ contract Committee is FactRegistry, IAvailabilityVerifier, Identity {
/// @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(
Expand All @@ -25,8 +26,8 @@ contract Committee is FactRegistry, IAvailabilityVerifier, Identity {
signaturesRequired = numSignaturesRequired;
}

function identify() external pure override returns (string memory) {
return "StarkWare_Committee_2019_1";
function identify() external pure virtual override returns (string memory) {
return "StarkWare_Committee_2022_2";
}

/// @dev Verifies the availability proof. Reverts if invalid.
Expand Down
24 changes: 23 additions & 1 deletion scalable-dex/contracts/src/interactions/StateRoot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ pragma solidity ^0.6.12;

import "../interfaces/MStateRoot.sol";
import "../components/MainStorage.sol";
import "../libraries/LibConstants.sol";

contract StateRoot is MainStorage, MStateRoot {
contract StateRoot is MainStorage, LibConstants, MStateRoot {
function initialize(
uint256 initialSequenceNumber,
uint256 initialValidiumVaultRoot,
Expand Down Expand Up @@ -58,4 +59,25 @@ contract StateRoot is MainStorage, MStateRoot {
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));
}
}
30 changes: 30 additions & 0 deletions scalable-dex/contracts/src/interfaces/MStateRoot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,34 @@ abstract contract MStateRoot {
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);
}
6 changes: 3 additions & 3 deletions scalable-dex/contracts/src/starkex/StarkExchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.6.12;
import "../interfaces/MainDispatcher.sol";

contract StarkExchange is MainDispatcher {
string public constant VERSION = "4.5.0";
string public constant VERSION = "4.5.1";

// Salt for a 8 bit unique spread of all relevant selectors. Pre-caclulated.
// ---------- The following code was auto-generated. PLEASE DO NOT EDIT. ----------
Expand Down Expand Up @@ -43,9 +43,9 @@ contract StarkExchange is MainDispatcher {
} else if (index == 2) {
id = "StarkWare_TokensAndRamping_2022_2";
} else if (index == 3) {
id = "StarkWare_StarkExState_2022_4";
id = "StarkWare_StarkExState_2022_5";
} else if (index == 4) {
id = "StarkWare_ForcedActions_2022_2";
id = "StarkWare_ForcedActions_2022_3";
} else if (index == 5) {
id = "StarkWare_OnchainVaults_2022_2";
} else if (index == 6) {
Expand Down
13 changes: 5 additions & 8 deletions scalable-dex/contracts/src/starkex/components/Escapes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ abstract contract Escapes is
escapeVerifierAddress = escapeVerifier;
}

function isRollupVault(uint256 vaultId) internal pure returns (bool) {
return (vaultId >> ROLLUP_VAULTS_BIT) != 0;
}

/*
Escape when the contract is frozen.
*/
Expand All @@ -48,19 +44,20 @@ abstract contract Escapes is
uint256 assetId,
uint256 quantizedAmount
) external onlyFrozen {
require(isVaultInRange(vaultId), "OUT_OF_RANGE_VAULT_ID");
require(!escapesUsed[vaultId], "ESCAPE_ALREADY_USED");

// Escape can be used only once.
escapesUsed[vaultId] = true;
escapesUsedCount += 1;

// Select a vault tree to escape from, based on the vault id.
(uint256 root, uint256 treeHeight) = isRollupVault(vaultId)
? (getRollupVaultRoot(), getRollupTreeHeight())
: (getValidiumVaultRoot(), getValidiumTreeHeight());
(uint256 root, uint256 treeHeight) = isValidiumVault(vaultId)
? (getValidiumVaultRoot(), getValidiumTreeHeight())
: (getRollupVaultRoot(), getRollupTreeHeight());

// The index of vaultId leaf in its tree doesn't include the rollup bit flag.
uint256 vaultLeafIndex = (vaultId & (2**ROLLUP_VAULTS_BIT - 1));
uint256 vaultLeafIndex = getVaultLeafIndex(vaultId);

bytes32 claimHash = keccak256(
abi.encode(ownerKey, assetId, quantizedAmount, root, treeHeight, vaultLeafIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "../interfaces/MStarkExForcedActionState.sol";
import "../../interactions/StateRoot.sol";
import "../../interfaces/MFreezable.sol";
import "../../interfaces/MKeyGetters.sol";
import "../../libraries/LibConstants.sol";

/**
At any point in time, a user may opt to perform a full withdrawal request for a given off-chain
Expand Down Expand Up @@ -35,26 +34,9 @@ import "../../libraries/LibConstants.sol";
cost of the request exceed 1M gas.
*/
abstract contract FullWithdrawals is
StateRoot,
LibConstants,
MStarkExForcedActionState,
MFreezable,
MKeyGetters
{
abstract contract FullWithdrawals is StateRoot, MStarkExForcedActionState, MFreezable, MKeyGetters {
event LogFullWithdrawalRequest(uint256 ownerKey, uint256 vaultId);

function isVaultInRange(uint256 vaultId) private view returns (bool) {
// Retruns true if vaultId is in the validium vaults tree.
if (vaultId < 2**getValidiumTreeHeight()) {
return true;
}
// 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 fullWithdrawalRequest(uint256 ownerKey, uint256 vaultId)
external
notFrozen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ contract ForcedActions is
}

function identify() external pure override returns (string memory) {
return "StarkWare_ForcedActions_2022_2";
return "StarkWare_ForcedActions_2022_3";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ contract StarkExState is
initValues := add(32, _data)
}
require(initValues.globalConfigCode < K_MODULUS, "GLOBAL_CONFIG_CODE >= PRIME");
require(initValues.validiumTreeHeight < ROLLUP_VAULTS_BIT, "INVALID_VALIDIUM_HEIGHT");
require(initValues.rollupTreeHeight < ROLLUP_VAULTS_BIT, "INVALID_ROLLUP_HEIGHT");

initGovernance();
StarkExOperator.initialize();
Expand Down Expand Up @@ -102,6 +104,6 @@ contract StarkExState is
}

function identify() external pure override returns (string memory) {
return "StarkWare_StarkExState_2022_4";
return "StarkWare_StarkExState_2022_5";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ contract V3toV45ChangesExternalInitializer is ExternalInitializer, MainStorage,
) = abi.decode(data, (address, bytes32, uint256, uint256, uint256, uint256, uint256));

require(newGlobalConfigCode < K_MODULUS, "GLOBAL_CONFIG_CODE >= PRIME");
require(newRollupTreeHeigh < ROLLUP_VAULTS_BIT, "INVALID_ROLLUP_HEIGHT");

// Flush the current verifiers & availabilityVerifier list.
delete verifiersChain.list;
Expand Down

0 comments on commit 210bd5f

Please sign in to comment.