Skip to content

Commit

Permalink
sync with latest changes to system contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavBreadless committed Jan 9, 2024
2 parents d532dc4 + 6bd48d8 commit 64c742d
Show file tree
Hide file tree
Showing 20 changed files with 144 additions and 51 deletions.
17 changes: 9 additions & 8 deletions system-contracts/bootloader/bootloader.yul
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ object "Bootloader" {
}

/// @dev The slot from which the scratch space starts.
/// Scatch space is used for various temporary values
/// Scratch space is used for various temporary values
function SCRATCH_SPACE_BEGIN_SLOT() -> ret {
ret := 8
}
Expand Down Expand Up @@ -353,7 +353,7 @@ object "Bootloader" {

/// @dev Slots needed to store L1 Messenger pubdata.
/// @dev Note that are many more these than the maximal pubdata in batch, since
/// it needs to also accomodate uncompressed state diffs that are required for the state diff
/// it needs to also accommodate uncompressed state diffs that are required for the state diff
/// compression verification.
function OPERATOR_PROVIDED_L1_MESSENGER_PUBDATA_SLOTS() -> ret {
ret := {{OPERATOR_PROVIDED_L1_MESSENGER_PUBDATA_SLOTS}}
Expand Down Expand Up @@ -1689,7 +1689,6 @@ object "Bootloader" {
if gt(operatorOverheadForTransaction, txTotalGasLimit) {
assertionError("Overhead higher than gasLimit")
}
let txGasLimit := min(safeSub(txTotalGasLimit, operatorOverheadForTransaction, "www"), MAX_GAS_PER_TRANSACTION())

let requiredOverhead := getTransactionUpfrontOverhead(txEncodeLen)

Expand Down Expand Up @@ -2716,7 +2715,7 @@ object "Bootloader" {
)
}
default {
// For L2 transactions, we use near call panic, it will triger the validation
// For L2 transactions, we use near call panic, it will trigger the validation
// step of the transaction to fail, returning a consistent error message.
nearCallPanic()
}
Expand Down Expand Up @@ -3696,15 +3695,17 @@ object "Bootloader" {

validateOperatorProvidedPrices(FAIR_L2_GAS_PRICE, FAIR_PUBDATA_PRICE)



<!-- @if BOOTLOADER_TYPE=='proved_batch' -->

let baseFee := 0

baseFee, GAS_PRICE_PER_PUBDATA := getFeeParams(
FAIR_PUBDATA_PRICE,
FAIR_L2_GAS_PRICE
)

<!-- @if BOOTLOADER_TYPE=='proved_batch' -->

// Only for the proved batch we enforce that the baseFee proposed
// by the operator is equal to the expected one. For the playground batch, we allow
// the operator to provide any baseFee the operator wants.s
Expand Down Expand Up @@ -3833,7 +3834,7 @@ object "Bootloader" {
setTxOrigin(0)
setGasPrice(0)

// Transfering all the ETH received in the block to the operator
// Transferring all the ETH received in the block to the operator
directETHTransfer(
selfbalance(),
OPERATOR_ADDRESS
Expand All @@ -3846,7 +3847,7 @@ object "Bootloader" {
// So we need to have this method to reflect it in the system contracts too.
//
// The reason is that as of now our node requires that each storage write (event, etc) belongs to a particular
// L2 block. In case a batch is sealed by timeout (i.e. the resources of the batch have not been exhaused, but we need
// L2 block. In case a batch is sealed by timeout (i.e. the resources of the batch have not been exhausted, but we need
// to seal it to assure timely finality), we need to process sending funds to the operator *after* the last
// non-empty L2 block has been already sealed. We can not override old L2 blocks, so we need to create a new empty "fictive" block for it.
//
Expand Down
48 changes: 48 additions & 0 deletions system-contracts/bootloader/tests/bootloader/bootloader_test.yul
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,51 @@ function TEST_simple_transaction() {
let innerTxDataOffset := add(txDataOffset, 0x20)
testing_assertEq(getGasPerPubdataByteLimit(innerTxDataOffset), 0xc350, "Invalid pubdata limit")
}

function TEST_getTransactionUpfrontOverhead() {
// For very large transactions it should be proportional to the memory,
// but for small ones, the transaction slots are more important

let smallTxOverhead := getTransactionUpfrontOverhead(32)
let largeTxOverhead := getTransactionUpfrontOverhead(1000000)

testing_assertEq(smallTxOverhead, TX_SLOT_OVERHEAD_GAS(), "Invalid small tx overhead")
testing_assertEq(largeTxOverhead, mul(1000000, MEMORY_OVERHEAD_GAS()), "Invalid small tx overhead")
}

function TEST_getFeeParams_HighPubdataPrice() {
// Under very large L1 gas price, the L2 base fee will start rising to ensure the
// boundary on the gasLimit

// 15k gwei L1 pubdata price
let veryHighL1PubdataPrice := 15000000000000
// 0.1 gwei L2 base fee
let l2GasPrice := 100000000

let baseFee, gasPricePerPubdata := getFeeParams(
veryHighL1PubdataPrice,
// 0.1 gwei L2 base fee
l2GasPrice
)

testing_assertEq(baseFee, div(veryHighL1PubdataPrice, MAX_L2_GAS_PER_PUBDATA()), "Invalid base fee")
testing_assertEq(gasPricePerPubdata, MAX_L2_GAS_PER_PUBDATA(), "Invalid gasPricePerPubdata")
}

function TEST_getFeeParams_LowPubdataPrice() {
// Under low to medium pubdata price, the baseFee is equal to the fair gas price,
// while the gas per pubdata pubdata is derived by strict division

// 0.2 gwei L1 pubdata price
let veryLowL1GasPrice := 200000000
// 0.1 gwei L2 base fee
let l2GasPrice := 100000000

let baseFee, gasPricePerPubdata := getFeeParams(
veryLowL1GasPrice,
l2GasPrice
)

testing_assertEq(baseFee, l2GasPrice, "Invalid base fee")
testing_assertEq(gasPricePerPubdata, div(veryLowL1GasPrice, l2GasPrice), "Invalid gasPricePerPubdata")
}
8 changes: 4 additions & 4 deletions system-contracts/contracts/BootloaderUtilities.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

pragma solidity 0.8.20;

import "./interfaces/IBootloaderUtilities.sol";
import "./libraries/TransactionHelper.sol";
import "./libraries/RLPEncoder.sol";
import "./libraries/EfficientCall.sol";
import {IBootloaderUtilities} from "./interfaces/IBootloaderUtilities.sol";
import {Transaction, TransactionHelper, EIP_712_TX_TYPE, LEGACY_TX_TYPE, EIP_2930_TX_TYPE, EIP_1559_TX_TYPE} from "./libraries/TransactionHelper.sol";
import {RLPEncoder} from "./libraries/RLPEncoder.sol";
import {EfficientCall} from "./libraries/EfficientCall.sol";

/**
* @author Matter Labs
Expand Down
4 changes: 2 additions & 2 deletions system-contracts/contracts/ImmutableSimulator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity 0.8.20;

import "./interfaces/IImmutableSimulator.sol";
import {IImmutableSimulator, ImmutableData} from "./interfaces/IImmutableSimulator.sol";
import {DEPLOYER_SYSTEM_CONTRACT} from "./Constants.sol";

/**
Expand All @@ -18,7 +18,7 @@ import {DEPLOYER_SYSTEM_CONTRACT} from "./Constants.sol";
contract ImmutableSimulator is IImmutableSimulator {
/// @dev mapping (contract address) => (index of immutable variable) => value
/// @notice that address uses `uint256` type to leave the option to introduce 32-byte address space in future.
mapping(uint256 => mapping(uint256 => bytes32)) internal immutableDataStorage;
mapping(uint256 contractAddress => mapping(uint256 index => bytes32 value)) internal immutableDataStorage;

/// @notice Method that returns the immutable with a certain index for a user.
/// @param _dest The address which the immutable belongs to.
Expand Down
1 change: 0 additions & 1 deletion system-contracts/contracts/KnownCodesStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pragma solidity 0.8.20;
import {IKnownCodesStorage} from "./interfaces/IKnownCodesStorage.sol";
import {ISystemContract} from "./interfaces/ISystemContract.sol";
import {Utils} from "./libraries/Utils.sol";
import {SystemContractHelper} from "./libraries/SystemContractHelper.sol";
import {COMPRESSOR_CONTRACT, L1_MESSENGER_CONTRACT} from "./Constants.sol";

/**
Expand Down
4 changes: 2 additions & 2 deletions system-contracts/contracts/L1Messenger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ contract L1Messenger is IL1Messenger, ISystemContract {

/// Check State Diffs
/// encoding is as follows:
/// header (1 byte version, 3 bytes total len of compressed, 1 byte enumeration index size, 2 bytes number of initial writes)
/// body (N bytes of initial writes [32 byte derived key || compressed value], M bytes repeated writes [enumeration index || compressed value])
/// header (1 byte version, 3 bytes total len of compressed, 1 byte enumeration index size)
/// body (`compressedStateDiffSize` bytes, 4 bytes number of state diffs, `numberOfStateDiffs` * `STATE_DIFF_ENTRY_SIZE` bytes for the uncompressed state diffs)
/// encoded state diffs: [20bytes address][32bytes key][32bytes derived key][8bytes enum index][32bytes initial value][32bytes final value]
require(
uint256(uint8(bytes1(_totalL2ToL1PubdataAndStateDiffs[calldataPtr]))) ==
Expand Down
2 changes: 1 addition & 1 deletion system-contracts/contracts/L2EthToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {IMailbox} from "./interfaces/IMailbox.sol";
*/
contract L2EthToken is IEthToken, ISystemContract {
/// @notice The balances of the users.
mapping(address => uint256) internal balance;
mapping(address account => uint256 balance) internal balance;

/// @notice The total amount of tokens that have been minted.
uint256 public override totalSupply;
Expand Down
10 changes: 7 additions & 3 deletions system-contracts/contracts/MsgValueSimulator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

pragma solidity 0.8.20;

import "./libraries/Utils.sol";
import "./libraries/EfficientCall.sol";
import "./interfaces/ISystemContract.sol";
import {Utils} from "./libraries/Utils.sol";
import {EfficientCall} from "./libraries/EfficientCall.sol";
import {ISystemContract} from "./interfaces/ISystemContract.sol";
import {SystemContractHelper} from "./libraries/SystemContractHelper.sol";
import {MSG_VALUE_SIMULATOR_IS_SYSTEM_BIT, ETH_TOKEN_SYSTEM_CONTRACT} from "./Constants.sol";

Expand Down Expand Up @@ -32,6 +32,10 @@ contract MsgValueSimulator is ISystemContract {
to = address(uint160(addressAsUint));
}

/// @notice The fallback function that is the main entry point for the MsgValueSimulator.
/// @dev The contract accepts value, the callee and whether the call should a system one via its ABI params.
/// @param _data The calldata to be passed to the callee.
/// @return The return data from the callee.
fallback(bytes calldata _data) external onlySystemCall returns (bytes memory) {
(uint256 value, bool isSystemCall, address to) = _getAbiParams();

Expand Down
16 changes: 10 additions & 6 deletions system-contracts/contracts/NonceHolder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

pragma solidity 0.8.20;

import "./interfaces/INonceHolder.sol";
import "./interfaces/IContractDeployer.sol";
import {INonceHolder} from "./interfaces/INonceHolder.sol";
import {IContractDeployer} from "./interfaces/IContractDeployer.sol";
import {ISystemContract} from "./interfaces/ISystemContract.sol";
import {DEPLOYER_SYSTEM_CONTRACT} from "./Constants.sol";

Expand All @@ -25,20 +25,20 @@ import {DEPLOYER_SYSTEM_CONTRACT} from "./Constants.sol";
* here serve more as a help to users to prevent from doing mistakes, rather than any invariants.
*/
contract NonceHolder is INonceHolder, ISystemContract {
uint256 constant DEPLOY_NONCE_MULTIPLIER = 2 ** 128;
uint256 constant private DEPLOY_NONCE_MULTIPLIER = 2 ** 128;
/// The minNonce can be increased by at 2^32 at a time to prevent it from
/// overflowing beyond 2**128.
uint256 constant MAXIMAL_MIN_NONCE_INCREMENT = 2 ** 32;
uint256 constant private MAXIMAL_MIN_NONCE_INCREMENT = 2 ** 32;

/// RawNonces for accounts are stored in format
/// minNonce + 2^128 * deploymentNonce, where deploymentNonce
/// is the nonce used for deploying smart contracts.
mapping(uint256 => uint256) internal rawNonces;
mapping(uint256 account => uint256 packedMinAndDeploymentNonce) internal rawNonces;

/// Mapping of values under nonces for accounts.
/// The main key of the mapping is the 256-bit address of the account, while the
/// inner mapping is a mapping from a nonce to the value stored there.
mapping(uint256 => mapping(uint256 => uint256)) internal nonceValues;
mapping(uint256 account => mapping(uint256 nonceKey => uint256 value)) internal nonceValues;

/// @notice Returns the current minimal nonce for account.
/// @param _address The account to return the minimal nonce for
Expand Down Expand Up @@ -147,6 +147,10 @@ contract NonceHolder is INonceHolder, ISystemContract {
(prevDeploymentNonce, ) = _splitRawNonce(oldRawNonce);
}

/// @notice A method that checks whether the nonce has been used before.
/// @param _address The address the nonce of which is being checked.
/// @param _nonce The nonce value which is check.
/// @return `true` if the nonce has been used, `false` otherwise.
function isNonceUsed(address _address, uint256 _nonce) public view returns (bool) {
uint256 addressAsKey = uint256(uint160(_address));
return (_nonce < getMinNonce(_address) || nonceValues[addressAsKey][_nonce] > 0);
Expand Down
12 changes: 6 additions & 6 deletions system-contracts/contracts/SystemContext.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr
address public coinbase = BOOTLOADER_FORMAL_ADDRESS;

/// @notice Formal `block.difficulty` parameter.
uint256 public difficulty = 2500000000000000;
uint256 public difficulty = 2.5e15;

/// @notice The `block.basefee`.
/// @dev It is currently a constant.
Expand All @@ -52,7 +52,7 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr

/// @notice The hashes of batches.
/// @dev It stores batch hashes for all previous batches.
mapping(uint256 => bytes32) internal batchHash;
mapping(uint256 batchNumber => bytes32 batchHash) internal batchHashes;

/// @notice The number and the timestamp of the current L2 block.
BlockInfo internal currentL2BlockInfo;
Expand Down Expand Up @@ -117,7 +117,7 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr
} else if (_block < currentVirtualBlockUpgradeInfo.virtualBlockStartBatch) {
// Note, that we will get into this branch only for a brief moment of time, right after the upgrade
// for virtual blocks before 256 virtual blocks are produced.
hash = batchHash[_block];
hash = batchHashes[_block];
} else if (
_block >= currentVirtualBlockUpgradeInfo.virtualBlockFinishL2Block &&
currentVirtualBlockUpgradeInfo.virtualBlockFinishL2Block > 0
Expand All @@ -135,7 +135,7 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr
/// @param _batchNumber The number of the batch.
/// @return hash The hash of the batch.
function getBatchHash(uint256 _batchNumber) external view returns (bytes32 hash) {
hash = batchHash[_batchNumber];
hash = batchHashes[_batchNumber];
}

/// @notice Returns the current batch's number and timestamp.
Expand Down Expand Up @@ -424,7 +424,7 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr

_ensureBatchConsistentWithL2Block(_newTimestamp);

batchHash[previousBatchNumber] = _prevBatchHash;
batchHashes[previousBatchNumber] = _prevBatchHash;

// Setting new block number and timestamp
BlockInfo memory newBlockInfo = BlockInfo({number: previousBatchNumber + 1, timestamp: _newTimestamp});
Expand Down Expand Up @@ -478,6 +478,6 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr
/// @notice Returns the hash of the given batch.
/// @dev Deprecated in favor of getBatchHash.
function blockHash(uint256 _blockNumber) external view returns (bytes32 hash) {
hash = batchHash[_blockNumber];
hash = batchHashes[_blockNumber];
}
}
5 changes: 5 additions & 0 deletions system-contracts/contracts/interfaces/IComplexUpgrader.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

pragma solidity 0.8.20;

/**
* @author Matter Labs
* @custom:security-contact [email protected]
* @notice The interface for the ComplexUpgrader contract.
*/
interface IComplexUpgrader {
function upgrade(address _delegateTo, bytes calldata _calldata) external payable;
}
6 changes: 6 additions & 0 deletions system-contracts/contracts/interfaces/ICompressor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ uint8 constant LENGTH_BITS_OFFSET = 3;
// The maximal length in bytes that an enumeration index can have.
uint8 constant MAX_ENUMERATION_INDEX_SIZE = 8;

/**
* @author Matter Labs
* @custom:security-contact [email protected]
* @notice The interface for the Compressor contract, responsible for verifying the correctness of
* the compression of the state diffs and bytecodes.
*/
interface ICompressor {
function publishCompressedBytecode(
bytes calldata _bytecode,
Expand Down
6 changes: 6 additions & 0 deletions system-contracts/contracts/interfaces/IKnownCodesStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

pragma solidity 0.8.20;

/**
* @author Matter Labs
* @custom:security-contact [email protected]
* @notice The interface for the KnownCodesStorage contract, which is responsible
* for storing the hashes of the bytecodes that have been published to the network.
*/
interface IKnownCodesStorage {
event MarkedAsKnown(bytes32 indexed bytecodeHash, bool indexed sendBytecodeToL1);

Expand Down
5 changes: 5 additions & 0 deletions system-contracts/contracts/interfaces/IL1Messenger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ bytes32 constant L2_L1_LOGS_TREE_DEFAULT_LEAF_HASH = 0x72abee45b59e344af8a6e5202
/// @dev The current version of state diff compression being used.
uint256 constant STATE_DIFF_COMPRESSION_VERSION_NUMBER = 1;

/**
* @author Matter Labs
* @custom:security-contact [email protected]
* @notice The interface of the L1 Messenger contract, responsible for sending messages to L1.
*/
interface IL1Messenger {
// Possibly in the future we will be able to track the messages sent to L1 with
// some hooks in the VM. For now, it is much easier to track them with L2 events.
Expand Down
1 change: 1 addition & 0 deletions system-contracts/contracts/interfaces/ISystemContext.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.20;

/**
* @author Matter Labs
* @custom:security-contact [email protected]
* @notice Contract that stores some of the context variables, that may be either
* block-scoped, tx-scoped or system-wide.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.20;

/**
* @author Matter Labs
* @custom:security-contact [email protected]
* @notice The interface with deprecated functions of the SystemContext contract. It is aimed for backward compatibility.
*/
interface ISystemContextDeprecated {
Expand Down
15 changes: 11 additions & 4 deletions system-contracts/contracts/interfaces/ISystemContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ pragma solidity 0.8.20;
import {SystemContractHelper} from "../libraries/SystemContractHelper.sol";
import {BOOTLOADER_FORMAL_ADDRESS} from "../Constants.sol";

/// @dev Solidity does not allow exporting modifiers via libraries, so
/// the only way to do reuse modifiers is to have a base contract
/// @dev Never add storage variables into this contract as some
/// system contracts rely on this abstract contract as on interface!


/**
* @author Matter Labs
* @custom:security-contact [email protected]
* @notice An abstract contract that is used to reuse modifiers across the system contracts.
* @dev Solidity does not allow exporting modifiers via libraries, so
* the only way to do reuse modifiers is to have a base contract
* @dev Never add storage variables into this contract as some
* system contracts rely on this abstract contract as on interface!
*/
abstract contract ISystemContract {
/// @notice Modifier that makes sure that the method
/// can only be called via a system call.
Expand Down
Loading

0 comments on commit 64c742d

Please sign in to comment.