Skip to content

Commit

Permalink
Merge branch 'release-v23' of ssh://github.com/matter-labs/era-contra…
Browse files Browse the repository at this point in the history
…cts into kl/custom-asset-bridging
  • Loading branch information
kelemeno committed May 31, 2024
2 parents d04135a + 16ae765 commit e036987
Show file tree
Hide file tree
Showing 19 changed files with 495 additions and 101 deletions.
5 changes: 3 additions & 2 deletions l1-contracts/contracts/common/Config.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ uint256 constant PRIORITY_OPERATION_L2_TX_TYPE = 255;
/// @dev Denotes the type of the zkSync transaction that is used for system upgrades.
uint256 constant SYSTEM_UPGRADE_L2_TX_TYPE = 254;

/// @dev The maximal allowed difference between protocol versions in an upgrade. The 100 gap is needed
/// @dev The maximal allowed difference between protocol minor versions in an upgrade. The 100 gap is needed
/// in case a protocol version has been tested on testnet, but then not launched on mainnet, e.g.
/// due to a bug found.
uint256 constant MAX_ALLOWED_PROTOCOL_VERSION_DELTA = 100;
/// We are allowed to jump at most 100 minor versions at a time. The major version is always expected to be 0.
uint256 constant MAX_ALLOWED_MINOR_VERSION_DELTA = 100;

/// @dev The amount of time in seconds the validator has to process the priority transaction
/// NOTE: The constant is set to zero for the Alpha release period
Expand Down
48 changes: 48 additions & 0 deletions l1-contracts/contracts/common/libraries/SemVer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

/// @dev The number of bits dedicated to the "patch" portion of the protocol version.
/// This also defines the bit starting from which the "minor" part is located.
uint256 constant SEMVER_MINOR_OFFSET = 32;

/// @dev The number of bits dedicated to the "patch" and "minor" portions of the protocol version.
/// This also defines the bit starting from which the "major" part is located.
/// Note, that currently, only major version of "0" is supported.
uint256 constant SEMVER_MAJOR_OFFSET = 64;

/**
* @author Matter Labs
* @custom:security-contact [email protected]
* @notice The library for managing SemVer for the protocol version.
*/
library SemVer {
/// @notice Unpacks the SemVer version from a single uint256 into major, minor and patch components.
/// @param _packedProtocolVersion The packed protocol version.
/// @return major The major version.
/// @return minor The minor version.
/// @return patch The patch version.
function unpackSemVer(
uint96 _packedProtocolVersion
) internal pure returns (uint32 major, uint32 minor, uint32 patch) {
patch = uint32(_packedProtocolVersion);
minor = uint32(_packedProtocolVersion >> SEMVER_MINOR_OFFSET);
major = uint32(_packedProtocolVersion >> SEMVER_MAJOR_OFFSET);
}

/// @notice Packs the SemVer version from the major, minor and patch components into a single uint96.
/// @param _major The major version.
/// @param _minor The minor version.
/// @param _patch The patch version.
/// @return packedProtocolVersion The packed protocol version.
function packSemVer(
uint32 _major,
uint32 _minor,
uint32 _patch
) internal pure returns (uint96 packedProtocolVersion) {
packedProtocolVersion =
uint96(_patch) |
(uint96(_minor) << SEMVER_MINOR_OFFSET) |
(uint96(_major) << SEMVER_MAJOR_OFFSET);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ contract CustomUpgradeTest is BaseZkSyncUpgrade {
/// @notice The main function that will be called by the upgrade proxy.
/// @param _proposedUpgrade The upgrade to be executed.
function upgrade(ProposedUpgrade calldata _proposedUpgrade) public override returns (bytes32) {
_setNewProtocolVersion(_proposedUpgrade.newProtocolVersion);
(uint32 newMinorVersion, bool isPatchOnly) = _setNewProtocolVersion(_proposedUpgrade.newProtocolVersion);
_upgradeL1Contract(_proposedUpgrade.l1ContractsUpgradeCalldata);
_upgradeVerifier(_proposedUpgrade.verifier, _proposedUpgrade.verifierParams);
_setBaseSystemContracts(_proposedUpgrade.bootloaderHash, _proposedUpgrade.defaultAccountHash);
_setBaseSystemContracts(_proposedUpgrade.bootloaderHash, _proposedUpgrade.defaultAccountHash, isPatchOnly);

bytes32 txHash;
txHash = _setL2SystemContractUpgrade(
_proposedUpgrade.l2ProtocolUpgradeTx,
_proposedUpgrade.factoryDeps,
_proposedUpgrade.newProtocolVersion
newMinorVersion,
isPatchOnly
);

_postUpgrade(_proposedUpgrade.postUpgradeCalldata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,6 @@ interface IStateTransitionManager {
uint256 _oldProtocolVersion,
Diamond.DiamondCutData calldata _diamondCut
) external;

function getSemverProtocolVersion() external view returns (uint32, uint32, uint32);
}
22 changes: 17 additions & 5 deletions l1-contracts/contracts/state-transition/StateTransitionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pragma solidity 0.8.24;

import {EnumerableMap} from "@openzeppelin/contracts/utils/structs/EnumerableMap.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";

import {Diamond} from "./libraries/Diamond.sol";
import {DiamondProxy} from "./chain-deps/DiamondProxy.sol";
Expand All @@ -21,6 +22,7 @@ import {ProposedUpgrade} from "../upgrades/BaseZkSyncUpgrade.sol";
import {ReentrancyGuard} from "../common/ReentrancyGuard.sol";
import {REQUIRED_L2_GAS_PRICE_PER_PUBDATA, L2_TO_L1_LOG_SERIALIZE_SIZE, DEFAULT_L2_LOGS_TREE_ROOT_HASH, EMPTY_STRING_KECCAK, SYSTEM_UPGRADE_L2_TX_TYPE, PRIORITY_TX_MAX_GAS_LIMIT} from "../common/Config.sol";
import {VerifierParams} from "./chain-interfaces/IVerifier.sol";
import {SemVer} from "../common/libraries/SemVer.sol";

/// @title State Transition Manager contract
/// @author Matter Labs
Expand All @@ -47,7 +49,7 @@ contract StateTransitionManager is IStateTransitionManager, ReentrancyGuard, Own
/// @dev The genesisUpgrade contract address, used to setChainId
address public genesisUpgrade;

/// @dev The current protocolVersion
/// @dev The current packed protocolVersion. To access human-readable version, use `getSemverProtocolVersion` function.
uint256 public protocolVersion;

/// @dev The timestamp when protocolVersion can be last used
Expand Down Expand Up @@ -84,6 +86,12 @@ contract StateTransitionManager is IStateTransitionManager, ReentrancyGuard, Own
_;
}

/// @return The tuple of (major, minor, patch) protocol version.
function getSemverProtocolVersion() external view returns (uint32, uint32, uint32) {
// slither-disable-next-line unused-return
return SemVer.unpackSemVer(SafeCast.toUint96(protocolVersion));
}

/// @notice Returns all the registered hyperchain addresses
function getAllHyperchains() public view override returns (address[] memory chainAddresses) {
uint256[] memory keys = hyperchainMap.keys();
Expand Down Expand Up @@ -280,6 +288,10 @@ contract StateTransitionManager is IStateTransitionManager, ReentrancyGuard, Own
uint256[] memory uintEmptyArray;
bytes[] memory bytesEmptyArray;

uint256 cachedProtocolVersion = protocolVersion;
// slither-disable-next-line unused-return
(, uint32 minorVersion, ) = SemVer.unpackSemVer(SafeCast.toUint96(cachedProtocolVersion));

L2CanonicalTransaction memory l2ProtocolUpgradeTx = L2CanonicalTransaction({
txType: SYSTEM_UPGRADE_L2_TX_TYPE,
from: uint256(uint160(L2_FORCE_DEPLOYER_ADDR)),
Expand All @@ -289,8 +301,8 @@ contract StateTransitionManager is IStateTransitionManager, ReentrancyGuard, Own
maxFeePerGas: uint256(0),
maxPriorityFeePerGas: uint256(0),
paymaster: uint256(0),
// Note, that the protocol version is used as "nonce" for system upgrade transactions
nonce: protocolVersion,
// Note, that the `minor` of the protocol version is used as "nonce" for system upgrade transactions
nonce: uint256(minorVersion),
value: 0,
reserved: [uint256(0), 0, 0, 0],
data: systemContextCalldata,
Expand All @@ -314,7 +326,7 @@ contract StateTransitionManager is IStateTransitionManager, ReentrancyGuard, Own
l1ContractsUpgradeCalldata: new bytes(0),
postUpgradeCalldata: new bytes(0),
upgradeTimestamp: 0,
newProtocolVersion: protocolVersion
newProtocolVersion: cachedProtocolVersion
});

Diamond.FacetCut[] memory emptyArray;
Expand All @@ -325,7 +337,7 @@ contract StateTransitionManager is IStateTransitionManager, ReentrancyGuard, Own
});

IAdmin(_chainContract).executeUpgrade(cutData);
emit SetChainIdUpgrade(_chainContract, l2ProtocolUpgradeTx, protocolVersion);
emit SetChainIdUpgrade(_chainContract, l2ProtocolUpgradeTx, cachedProtocolVersion);
}

/// @dev used to register already deployed hyperchain contracts
Expand Down
8 changes: 4 additions & 4 deletions l1-contracts/contracts/state-transition/Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ contract Verifier is IVerifier {
function _loadVerificationKey() internal pure virtual {
assembly {
// gate setup commitments
mstore(VK_GATE_SETUP_0_X_SLOT, 0x1f718b0d8640b18fcb605c0b362408e5b96391e1ea32b12332829e557aa50925)
mstore(VK_GATE_SETUP_0_Y_SLOT, 0x0b0186811e335624d3034ed7a7fe02b1a259d5d37fef68694f188924b8d5cea0)
mstore(VK_GATE_SETUP_0_X_SLOT, 0x110deb1e0863737f9a3d7b4de641a03aa00a77bc9f1a05acc9d55b76ab9fdd4d)
mstore(VK_GATE_SETUP_0_Y_SLOT, 0x2c9dc252441e9298b7f6df6335a252517b7bccb924adf537b87c5cd3383fd7a9)
mstore(VK_GATE_SETUP_1_X_SLOT, 0x04659caf7b05471ba5ba85b1ab62267aa6c456836e625f169f7119d55b9462d2)
mstore(VK_GATE_SETUP_1_Y_SLOT, 0x0ea63403692148d2ad22189a1e5420076312f4d46e62036a043a6b0b84d5b410)
mstore(VK_GATE_SETUP_2_X_SLOT, 0x0e6696d09d65fce1e42805be03fca1f14aea247281f688981f925e77d4ce2291)
Expand All @@ -295,8 +295,8 @@ contract Verifier is IVerifier {
mstore(VK_GATE_SETUP_4_Y_SLOT, 0x22e404bc91350f3bc7daad1d1025113742436983c85eac5ab7b42221a181b81e)
mstore(VK_GATE_SETUP_5_X_SLOT, 0x0d9b29613037a5025655c82b143d2b7449c98f3aea358307c8529249cc54f3b9)
mstore(VK_GATE_SETUP_5_Y_SLOT, 0x15b3c4c946ad1babfc4c03ff7c2423fd354af3a9305c499b7fb3aaebe2fee746)
mstore(VK_GATE_SETUP_6_X_SLOT, 0x0ab21cb590aca747d70d9be12b035c786f5a42e002e621627189d5ef13561ce1)
mstore(VK_GATE_SETUP_6_Y_SLOT, 0x0d3c93aafe7eeebad42b789a046b02bb2d92193f6af460e4104ad7ac759ef82a)
mstore(VK_GATE_SETUP_6_X_SLOT, 0x2a4cb6c495dbc7201142cc773da895ae2046e790073988fb850aca6aead27b8a)
mstore(VK_GATE_SETUP_6_Y_SLOT, 0x28ef9200c3cb67da82030520d640292014f5f7c2e2909da608812e04671a3acf)
mstore(VK_GATE_SETUP_7_X_SLOT, 0x283344a1ab3e55ecfd904d0b8e9f4faea338df5a4ead2fa9a42f0e103da40abc)
mstore(VK_GATE_SETUP_7_Y_SLOT, 0x223b37b83b9687512d322993edd70e508dd80adb10bcf7321a3cc8a44c269521)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

pragma solidity 0.8.24;

import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";

import {ZkSyncHyperchainBase} from "./ZkSyncHyperchainBase.sol";
import {PubdataPricingMode} from "../ZkSyncHyperchainStorage.sol";
import {VerifierParams} from "../../../state-transition/chain-interfaces/IVerifier.sol";
Expand All @@ -10,6 +12,7 @@ import {PriorityQueue, PriorityOperation} from "../../../state-transition/librar
import {UncheckedMath} from "../../../common/libraries/UncheckedMath.sol";
import {IGetters} from "../../chain-interfaces/IGetters.sol";
import {ILegacyGetters} from "../../chain-interfaces/ILegacyGetters.sol";
import {SemVer} from "../../../common/libraries/SemVer.sol";

// While formally the following import is not used, it is needed to inherit documentation from it
import {IZkSyncHyperchainBase} from "../../chain-interfaces/IZkSyncHyperchainBase.sol";
Expand Down Expand Up @@ -148,6 +151,12 @@ contract GettersFacet is ZkSyncHyperchainBase, IGetters, ILegacyGetters {
return s.protocolVersion;
}

/// @inheritdoc IGetters
function getSemverProtocolVersion() external view returns (uint32, uint32, uint32) {
// slither-disable-next-line unused-return
return SemVer.unpackSemVer(SafeCast.toUint96(s.protocolVersion));
}

/// @inheritdoc IGetters
function getL2SystemContractsUpgradeTxHash() external view returns (bytes32) {
return s.l2SystemContractsUpgradeTxHash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ interface IGetters is IZkSyncHyperchainBase {
/// @return Whether the diamond is frozen or not
function isDiamondStorageFrozen() external view returns (bool);

/// @return The current protocol version
/// @return The current packed protocol version. To access human-readable version, use `getSemverProtocolVersion` function.
function getProtocolVersion() external view returns (uint256);

/// @return The tuple of (major, minor, patch) protocol version.
function getSemverProtocolVersion() external view returns (uint32, uint32, uint32);

/// @return The upgrade system contract transaction hash, 0 if the upgrade is not initialized
function getL2SystemContractsUpgradeTxHash() external view returns (bytes32);

Expand Down
Loading

0 comments on commit e036987

Please sign in to comment.