Skip to content
Verified contractPartialMIT

EraChainTypeManager

Address0xe642b64ac222503573ffbebfbd7bb7f072c99ef7
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.28

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.28+commit.7893614a
Language
solidity
Optimization
Yes, 9,999,999 runs
EVM version
prague
License
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,378,190
Creation transaction
0x087d522567…f55d378cbe

83 files

The source code is large: one file is shown at a time. Choose a file in the list.

contracts/state-transition/libraries/TransactionValidator.sol

// SPDX-License-Identifier: MIT
// We use a floating point pragma here so it can be used within other projects that interact with the ZKsync ecosystem without using our exact pragma version.
pragma solidity ^0.8.21;

import {Math} from "@openzeppelin/contracts-v4/utils/math/Math.sol";

import {L2CanonicalTransaction} from "../../common/Messaging.sol";
import {
    L1_TX_CALLDATA_FLOOR_PRICE_L2_GAS_ZKSYNC_OS,
    L1_TX_DELTA_544_ENCODING_BYTES,
    L1_TX_DELTA_FACTORY_DEPS_L2_GAS,
    L1_TX_DELTA_FACTORY_DEPS_PUBDATA,
    L1_TX_INTRINSIC_L2_GAS,
    L1_TX_INTRINSIC_L2_GAS_ZKSYNC_OS,
    L1_TX_INTRINSIC_PUBDATA,
    L1_TX_INTRINSIC_PUBDATA_ZKSYNC_OS,
    L1_TX_MIN_L2_GAS_BASE,
    MEMORY_OVERHEAD_GAS,
    TX_SLOT_OVERHEAD_L2_GAS,
    L1_TX_NATIVE_PER_GAS,
    MAX_NATIVE_COMPUTATIONAL_ZKSYNC_OS
} from "../../common/Config.sol";
import {
    InvalidUpgradeTxn,
    PubdataGreaterThanLimit,
    TooMuchGas,
    TxnBodyGasLimitNotEnoughGas,
    UpgradeTxVerifyParam,
    ValidateTxnNotEnoughGas
} from "../../common/L1ContractErrors.sol";

/// @title ZKsync Library for validating L1 -> L2 transactions
/// @author Matter Labs
/// @custom:security-contact [email protected]
library TransactionValidator {
    /// @dev Used to validate key properties of an L1->L2 transaction
    /// @param _transaction The transaction to validate
    /// @param _encoded The abi encoded bytes of the transaction
    /// @param _priorityTxMaxGasLimit The max gas limit, generally provided from Storage.sol
    /// @param _priorityTxMaxPubdata The maximal amount of pubdata that a single L1->L2 transaction can emit
    /// @param zksyncOS ZKsync OS state transition flag
    function validateL1ToL2Transaction(
        L2CanonicalTransaction memory _transaction,
        bytes memory _encoded,
        uint256 _priorityTxMaxGasLimit,
        uint256 _priorityTxMaxPubdata,
        bool zksyncOS
    ) internal pure {
        uint256 l2GasForTxBody = getTransactionBodyGasLimit(_transaction.gasLimit, _encoded.length, zksyncOS);

        // Ensuring that the transaction is provable
        if (l2GasForTxBody > _priorityTxMaxGasLimit) {
            revert TooMuchGas();
        }
        // Ensuring that the transaction cannot output more pubdata than is processable
        if (l2GasForTxBody / _transaction.gasPerPubdataByteLimit > _priorityTxMaxPubdata) {
            revert PubdataGreaterThanLimit(_priorityTxMaxPubdata, l2GasForTxBody / _transaction.gasPerPubdataByteLimit);
        }

        // Ensuring that the transaction covers the minimal costs for its processing:
        // hashing its content, publishing the factory dependencies, etc.
        if (
            // solhint-disable-next-line func-named-parameters
            getMinimalPriorityTransactionGasLimit(
                _encoded.length,
                _transaction.data.length,
                _transaction.factoryDeps.length,
                _transaction.gasPerPubdataByteLimit,
                zksyncOS
            ) > l2GasForTxBody
        ) {
            revert ValidateTxnNotEnoughGas();
        }
    }

    /// @dev Used to validate upgrade transactions
    /// @param _transaction The transaction to validate
    function validateUpgradeTransaction(L2CanonicalTransaction memory _transaction) internal pure {
        // Restrict from to be within system contract range (0...2^16 - 1)
        if (_transaction.from > type(uint16).max) {
            revert InvalidUpgradeTxn(UpgradeTxVerifyParam.From);
        }
        if (_transaction.to > type(uint160).max) {
            revert InvalidUpgradeTxn(UpgradeTxVerifyParam.To);
        }
        if (_transaction.paymaster != 0) {
            revert InvalidUpgradeTxn(UpgradeTxVerifyParam.Paymaster);
        }
        if (_transaction.value != 0) {
            revert InvalidUpgradeTxn(UpgradeTxVerifyParam.Value);
        }
        if (_transaction.maxFeePerGas != 0) {
            revert InvalidUpgradeTxn(UpgradeTxVerifyParam.MaxFeePerGas);
        }
        if (_transaction.maxPriorityFeePerGas != 0) {
            revert InvalidUpgradeTxn(UpgradeTxVerifyParam.MaxPriorityFeePerGas);
        }
        if (_transaction.reserved[0] != 0) {
            revert InvalidUpgradeTxn(UpgradeTxVerifyParam.Reserved0);
        }
        if (_transaction.reserved[1] > type(uint160).max) {
            revert InvalidUpgradeTxn(UpgradeTxVerifyParam.Reserved1);
        }
        if (_transaction.reserved[2] != 0) {
            revert InvalidUpgradeTxn(UpgradeTxVerifyParam.Reserved2);
        }
        if (_transaction.reserved[3] != 0) {
            revert InvalidUpgradeTxn(UpgradeTxVerifyParam.Reserved3);
        }
        if (_transaction.signature.length != 0) {
            revert InvalidUpgradeTxn(UpgradeTxVerifyParam.Signature);
        }
        if (_transaction.paymasterInput.length != 0) {
            revert InvalidUpgradeTxn(UpgradeTxVerifyParam.PaymasterInput);
        }
        if (_transaction.reservedDynamic.length != 0) {
            revert InvalidUpgradeTxn(UpgradeTxVerifyParam.ReservedDynamic);
        }
    }

    /// @dev Calculates the approximate minimum gas limit required for executing a priority transaction.
    /// @param _encodingLength The length of the priority transaction encoding in bytes.
    /// @param _numberOfFactoryDependencies The number of new factory dependencies that will be added.
    /// @param _l2GasPricePerPubdata The L2 gas price for publishing the priority transaction on L2.
    /// @param zksyncOS ZKsync OS state transition flag
    /// @return The minimum gas limit required to execute the priority transaction.
    /// Note: The calculation includes the main cost of the priority transaction, however, in reality, the operator can spend a little more gas on overheads.
    function getMinimalPriorityTransactionGasLimit(
        uint256 _encodingLength,
        uint256 _calldataLength,
        uint256 _numberOfFactoryDependencies,
        uint256 _l2GasPricePerPubdata,
        bool zksyncOS
    ) internal pure returns (uint256) {
        if (zksyncOS) {
            // Due to double account resources model in zksync os, we need to calculate 2 things for minimal gas limit:
            // 1. Intrinsic tx cost in gas
            // 2. Intrinsic tx cost in native resources
            // And then take the bigger value

            // 1. Intrinsic tx cost in gas
            uint256 intrinsicGasCost = L1_TX_INTRINSIC_L2_GAS_ZKSYNC_OS;
            // we are overcharging using floor non-zero byte cost to cover the worst case
            intrinsicGasCost += L1_TX_CALLDATA_FLOOR_PRICE_L2_GAS_ZKSYNC_OS * _calldataLength;

            // 2. Intrinsic tx cost in native resources
            // Since we are using huge `L1_TX_NATIVE_PER_GAS` ratio, it mostly consists of pubdata cost.
            uint256 intrinsicPubdataGasCost = L1_TX_INTRINSIC_PUBDATA_ZKSYNC_OS * _l2GasPricePerPubdata;
            // And because of huge ratio, we are overestimating using `MAX_NATIVE_COMPUTATIONAL_ZKSYNC_OS`.
            // Actual intrinsic cost is much lower, but even with `MAX_NATIVE_COMPUTATIONAL_ZKSYNC_OS`
            // it will be around 343 gas and overestimate makes code safer and easier to support
            uint256 gasForIntrinsicNative = intrinsicPubdataGasCost +
                Math.ceilDiv(MAX_NATIVE_COMPUTATIONAL_ZKSYNC_OS, L1_TX_NATIVE_PER_GAS);

            return Math.max(intrinsicGasCost, gasForIntrinsicNative);
        } else {
            uint256 costForComputation;
            {
                // Adding the intrinsic cost for the transaction, i.e. auxiliary prices which cannot be easily accounted for
                costForComputation = L1_TX_INTRINSIC_L2_GAS;

                // Taking into account the hashing costs that depend on the length of the transaction
                // Note that L1_TX_DELTA_544_ENCODING_BYTES is the delta in the price for every 544 bytes of
                // the transaction's encoding. It is taken as LCM between 136 and 32 (the length for each keccak256 round
                // and the size of each new encoding word).
                costForComputation += Math.ceilDiv(_encodingLength * L1_TX_DELTA_544_ENCODING_BYTES, 544);

                // Taking into the account the additional costs of providing new factory dependencies
                costForComputation += _numberOfFactoryDependencies * L1_TX_DELTA_FACTORY_DEPS_L2_GAS;

                // There is a minimal amount of computational L2 gas that the transaction should cover
                costForComputation = Math.max(costForComputation, L1_TX_MIN_L2_GAS_BASE);
            }

            uint256 costForPubdata = 0;
            {
                // Adding the intrinsic cost for the transaction, i.e. auxiliary prices which cannot be easily accounted for
                costForPubdata = L1_TX_INTRINSIC_PUBDATA * _l2GasPricePerPubdata;

                // Taking into the account the additional costs of providing new factory dependencies
                costForPubdata +=
                    _numberOfFactoryDependencies *
                    L1_TX_DELTA_FACTORY_DEPS_PUBDATA *
                    _l2GasPricePerPubdata;
            }

            return costForComputation + costForPubdata;
        }
    }

    /// @notice Based on the full L2 gas limit (that includes the batch overhead) and other
    /// properties of the transaction, returns the l2GasLimit for the body of the transaction (the actual execution).
    /// @param _totalGasLimit The L2 gas limit that includes both the overhead for processing the batch
    /// and the L2 gas needed to process the transaction itself (i.e. the actual l2GasLimit that will be used for the transaction).
    /// @param _encodingLength The length of the ABI-encoding of the transaction.
    /// @param zksyncOS ZKsync OS state transition flag
    function getTransactionBodyGasLimit(
        uint256 _totalGasLimit,
        uint256 _encodingLength,
        bool zksyncOS
    ) internal pure returns (uint256 txBodyGasLimit) {
        // There is no overhead in ZKsync OS
        if (zksyncOS) {
            return _totalGasLimit;
        }
        uint256 overhead = getOverheadForTransaction(_encodingLength);

        // provided gas limit doesn't cover transaction overhead
        if (_totalGasLimit < overhead) {
            revert TxnBodyGasLimitNotEnoughGas();
        }
        unchecked {
            // We enforce the fact that `_totalGasLimit >= overhead` explicitly above.
            txBodyGasLimit = _totalGasLimit - overhead;
        }
    }

    /// @notice Based on the total L2 gas limit and several other parameters of the transaction
    /// returns the part of the L2 gas that will be spent on the batch's overhead.
    /// @dev The details of how this function works can be checked in the documentation
    /// of the fee model of ZKsync. The appropriate comments are also present
    /// in the Rust implementation description of function `get_maximal_allowed_overhead`.
    /// @param _encodingLength The length of the binary encoding of the transaction in bytes
    function getOverheadForTransaction(
        uint256 _encodingLength
    ) internal pure returns (uint256 batchOverheadForTransaction) {
        // The overhead from taking up the transaction's slot
        batchOverheadForTransaction = TX_SLOT_OVERHEAD_L2_GAS;

        // The overhead for occupying the bootloader memory can be derived from encoded_len
        uint256 overheadForLength = MEMORY_OVERHEAD_GAS * _encodingLength;
        batchOverheadForTransaction = Math.max(batchOverheadForTransaction, overheadForLength);
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 9999999,
    "enabled": true
  },
  "evmVersion": "prague",
  "remappings": [
    "@ensdomains/=node_modules/@ensdomains/",
    "ds-test/=lib/forge-std/lib/ds-test/src/",
    "eth-gas-reporter/=node_modules/eth-gas-reporter/",
    "forge-std/=lib/forge-std/src/",
    "hardhat/=node_modules/hardhat/",
    "murky/=lib/murky/src/",
    "foundry-test/=test/foundry/",
    "l2-contracts/=../l2-contracts/contracts/",
    "system-contracts/=../system-contracts/",
    "@openzeppelin/contracts-v4/=lib/openzeppelin-contracts-v4/contracts/",
    "@openzeppelin/contracts-upgradeable-v4/=lib/openzeppelin-contracts-upgradeable-v4/contracts/",
    "test-utils/=test/foundry/l1/integration/utils/",
    "openzeppelin-contracts-upgradeable-v4/=lib/openzeppelin-contracts-upgradeable-v4/",
    "openzeppelin-contracts-v4/=lib/openzeppelin-contracts-v4/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.