Skip to content

Commit

Permalink
fix tests and added custom errors
Browse files Browse the repository at this point in the history
  • Loading branch information
koloz193 committed Jun 4, 2024
1 parent 70589f0 commit b59d78a
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 148 deletions.
54 changes: 27 additions & 27 deletions system-contracts/SystemContractsHashes.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,49 @@
"contractName": "AccountCodeStorage",
"bytecodePath": "artifacts-zk/contracts-preprocessed/AccountCodeStorage.sol/AccountCodeStorage.json",
"sourceCodePath": "contracts-preprocessed/AccountCodeStorage.sol",
"bytecodeHash": "0x0100005db7d0621192834d201f8bcd14c6ac987b95f422ca8bb28d500748f1fa",
"bytecodeHash": "0x0100005dafb5e98ac74d0bad977db90b79247f2cec0b5ca26b8e1cdc6e27a539",
"sourceCodeHash": "0x69d2533e5481ff13e65f4442e650f4b90c46a48ac643cac9798bbbf421194353"
},
{
"contractName": "BootloaderUtilities",
"bytecodePath": "artifacts-zk/contracts-preprocessed/BootloaderUtilities.sol/BootloaderUtilities.json",
"sourceCodePath": "contracts-preprocessed/BootloaderUtilities.sol",
"bytecodeHash": "0x010007c7e006453f3693c1f1c7500b74c594e7a627076be9d2dcb35c1c268dc3",
"bytecodeHash": "0x010007c7c933ed7e22c7dfbf11bd4abee8ae77f0f82236c7fb6222c029db58f0",
"sourceCodeHash": "0x26060f33c7c63bd1f8a1a2f3b368b97ef8dd939bc53e95090f2c556248b99dce"
},
{
"contractName": "ComplexUpgrader",
"bytecodePath": "artifacts-zk/contracts-preprocessed/ComplexUpgrader.sol/ComplexUpgrader.json",
"sourceCodePath": "contracts-preprocessed/ComplexUpgrader.sol",
"bytecodeHash": "0x0100004db8b96797e7d854f2c67c2f13d3920dcf82602f77a6ce0e2353304b37",
"bytecodeHash": "0x0100004d4db5487882e98677cffeccc04defabf73c17442d27c16f3ccaf54b0f",
"sourceCodeHash": "0xdde7c49a94cc3cd34c3e7ced1b5ba45e4740df68d26243871edbe393e7298f7a"
},
{
"contractName": "Compressor",
"bytecodePath": "artifacts-zk/contracts-preprocessed/Compressor.sol/Compressor.json",
"sourceCodePath": "contracts-preprocessed/Compressor.sol",
"bytecodeHash": "0x0100014387fd8393671b5edd2cc5bb58f72d357c033a44c6274fe92abf19f96d",
"bytecodeHash": "0x01000143bb2e6fb5193858a708d918f35ec7b472cb188f9ea2551d162f924b37",
"sourceCodeHash": "0x63f5f3a541ac5b59736f9117b15f293346d1842fbfe75219f7961e73185315c5"
},
{
"contractName": "ContractDeployer",
"bytecodePath": "artifacts-zk/contracts-preprocessed/ContractDeployer.sol/ContractDeployer.json",
"sourceCodePath": "contracts-preprocessed/ContractDeployer.sol",
"bytecodeHash": "0x010004e5219e70c32d67ed1df0c46983a83a382e482abb5c4310ad554f55f738",
"bytecodeHash": "0x010004e51a3464180f9b3d8d4dd25b4fea1eb6707bd0ec385b597c077304dc22",
"sourceCodeHash": "0x28208c532ed8851077a9bb0a87636215840ce964397ab0767692f956a0fd11b3"
},
{
"contractName": "Create2Factory",
"bytecodePath": "artifacts-zk/contracts-preprocessed/Create2Factory.sol/Create2Factory.json",
"sourceCodePath": "contracts-preprocessed/Create2Factory.sol",
"bytecodeHash": "0x01000049cd4b556f8ebc05b0bd35d9996e0b65412343bcc8eb6ce14d958dd21f",
"bytecodeHash": "0x0100004979f8501200ee1b396c2071f410f25808e2dc063d461a84f807924e03",
"sourceCodeHash": "0x217e65f55c8add77982171da65e0db8cc10141ba75159af582973b332a4e098a"
},
{
"contractName": "DefaultAccount",
"bytecodePath": "artifacts-zk/contracts-preprocessed/DefaultAccount.sol/DefaultAccount.json",
"sourceCodePath": "contracts-preprocessed/DefaultAccount.sol",
"bytecodeHash": "0x0100055dbcf921fb36006fa42209bda58bc468eb33257b1782e854ed29273e64",
"bytecodeHash": "0x0100055df0cc611e801b9815dc96942a937439c9cf27070c6165824c16444c02",
"sourceCodeHash": "0xeb5ac8fc83e1c8619db058a9b6973958bd6ed1b6f4938f8f4541d702f12e085d"
},
{
Expand All @@ -59,64 +59,64 @@
"contractName": "GasBoundCaller",
"bytecodePath": "artifacts-zk/contracts-preprocessed/GasBoundCaller.sol/GasBoundCaller.json",
"sourceCodePath": "contracts-preprocessed/GasBoundCaller.sol",
"bytecodeHash": "0x010000abbab74213f27d4d7f132761f7315ad84dd2239195f336aa910c9c92fc",
"bytecodeHash": "0x010000abdf7f8b171233de7ec0ff8ef7d53111bab7b57f9e4803241ccd1ff9cd",
"sourceCodeHash": "0x7774719fe82d1664af19f3f30e9ca2223784374367d32399638671722e9da850"
},
{
"contractName": "ImmutableSimulator",
"bytecodePath": "artifacts-zk/contracts-preprocessed/ImmutableSimulator.sol/ImmutableSimulator.json",
"sourceCodePath": "contracts-preprocessed/ImmutableSimulator.sol",
"bytecodeHash": "0x0100003b91f0ceda986543b6b45811b72cb492cdcb80c42d8600185e33f72251",
"bytecodeHash": "0x0100003baf287e8e35f78679a2688bd30a6ddf7eb79a58edf806588e53e8ef51",
"sourceCodeHash": "0x4212e99cbc1722887cfb5b4cb967f278ac8642834786f0e3c6f3b324a9316815"
},
{
"contractName": "KnownCodesStorage",
"bytecodePath": "artifacts-zk/contracts-preprocessed/KnownCodesStorage.sol/KnownCodesStorage.json",
"sourceCodePath": "contracts-preprocessed/KnownCodesStorage.sol",
"bytecodeHash": "0x0100006f8342acd40c21d8e535e8ceb65d370ad76a116f3a893fa67452bb616f",
"bytecodeHash": "0x0100006f1b48ded2d7c88e51986c215b27b24485cb46abdfe3507e906d658a84",
"sourceCodeHash": "0x8da495a9fc5aa0d7d20a165a4fc8bc77012bec29c472015ea5ecc0a2bd706137"
},
{
"contractName": "L1Messenger",
"bytecodePath": "artifacts-zk/contracts-preprocessed/L1Messenger.sol/L1Messenger.json",
"sourceCodePath": "contracts-preprocessed/L1Messenger.sol",
"bytecodeHash": "0x0100029361cc1606b2e55c12bef0a9ccbb36f70f6a12b41593b34053ea93f433",
"bytecodeHash": "0x0100029315ffdead961c7cfac762fc56ad9e76bb7284454e371efe91976ce6f3",
"sourceCodeHash": "0x3e4c749b52c9fd8e6040b836dac10df29305ee0fc4fc9418326d3197e1630c38"
},
{
"contractName": "L2BaseToken",
"bytecodePath": "artifacts-zk/contracts-preprocessed/L2BaseToken.sol/L2BaseToken.json",
"sourceCodePath": "contracts-preprocessed/L2BaseToken.sol",
"bytecodeHash": "0x010001055346ab8d4eb1b48ef7e768a95930d4ef6300c57ca173f4a76d1a9258",
"bytecodeHash": "0x010001052dc51ca38efa8eae4a0348b80c447f5ebd2cb92edd64913d0cb12a8d",
"sourceCodeHash": "0x4cdafafd4cfdf410b31641e14487ea657be3af25e5ec1754fcd7ad67ec23d8be"
},
{
"contractName": "MsgValueSimulator",
"bytecodePath": "artifacts-zk/contracts-preprocessed/MsgValueSimulator.sol/MsgValueSimulator.json",
"sourceCodePath": "contracts-preprocessed/MsgValueSimulator.sol",
"bytecodeHash": "0x0100005d606dc17844411ee44a09b08e574347fd92fb4df7d1739fe02ebae2cc",
"bytecodeHash": "0x0100005db6fcdf4ba05bc721e41f70fd43b529d3de9d4cd8f2c7f52528d9fbcb",
"sourceCodeHash": "0x4834adf62dbaefa1a1c15d36b5ad1bf2826e7d888a17be495f7ed4e4ea381aa8"
},
{
"contractName": "NonceHolder",
"bytecodePath": "artifacts-zk/contracts-preprocessed/NonceHolder.sol/NonceHolder.json",
"sourceCodePath": "contracts-preprocessed/NonceHolder.sol",
"bytecodeHash": "0x010000dbe03a15e6478090c69b0565c273a9cb034c8af48b70d8e71baf5ecf94",
"bytecodeHash": "0x010000db47ecf45f9066a2c8439858a282bed60e1442ac42e8a2031298a3d139",
"sourceCodeHash": "0xaa2ed3a26af30032c00a612ac327e0cdf5288b7c932ae903462355f863f950cb"
},
{
"contractName": "PubdataChunkPublisher",
"bytecodePath": "artifacts-zk/contracts-preprocessed/PubdataChunkPublisher.sol/PubdataChunkPublisher.json",
"sourceCodePath": "contracts-preprocessed/PubdataChunkPublisher.sol",
"bytecodeHash": "0x01000047abbd0db8bc506c1cb48e3aab878da3e5ea1bbacf1d4b5f46391f2493",
"bytecodeHash": "0x01000047d4d187ebf3a5b5bab2223266287450c03786b08764fa28fce1685a2e",
"sourceCodeHash": "0x0568a9a12bdac94c9e055ca303824a6bf4dc4aa503cfe9a2586c7d3dda8d45da"
},
{
"contractName": "SystemContext",
"bytecodePath": "artifacts-zk/contracts-preprocessed/SystemContext.sol/SystemContext.json",
"sourceCodePath": "contracts-preprocessed/SystemContext.sol",
"bytecodeHash": "0x010001a55d8778874657090f2db3a8aeabd491a5f3352f04c89eadabdd92e6ba",
"sourceCodeHash": "0xf23d12ad2f17ad3b26e909fabfdcfaf4e1d158923e7cb8eeb9b5965a0b464406"
"bytecodeHash": "0x0100014dbed1c1386d590aa794a86bbf11a64ff9fb33a07683bae33431d09f3a",
"sourceCodeHash": "0xbfc43c7bc3b030db5879a3fa9a79a676d2df9ea88139b6e6c47f455c3861a080"
},
{
"contractName": "EventWriter",
Expand Down Expand Up @@ -185,35 +185,35 @@
"contractName": "bootloader_test",
"bytecodePath": "bootloader/build/artifacts/bootloader_test.yul.zbin",
"sourceCodePath": "bootloader/build/bootloader_test.yul",
"bytecodeHash": "0x010003cbda052f30cd056d0c758c7cc5be2c31f77461591d4be73dc3291b6ca5",
"sourceCodeHash": "0xa13475403f910dff1ea0cf7ebf80633d9dcc37b4f8dd9f0c678a935793580932"
"bytecodeHash": "0x010003cb0abde7fd0b5dbb10b11735ac146b8bc50d8d7b3b8b2ef19bc7c3ed8c",
"sourceCodeHash": "0x300a6e8170fa1b0d215d64e3d0c782b74077a1098f3131b1877fd2af38842939"
},
{
"contractName": "fee_estimate",
"bytecodePath": "bootloader/build/artifacts/fee_estimate.yul.zbin",
"sourceCodePath": "bootloader/build/fee_estimate.yul",
"bytecodeHash": "0x010009519c960b6baa12091cd1f8538d00fa0d7505c81943ba6e379a5f1c9c9e",
"sourceCodeHash": "0xbcdce81799656df718b5dfff4ba6ce8aac82087a78c0c33edb0675e55bb8364c"
"bytecodeHash": "0x01000947fa55ae0a6d0c7e101b5264c6bb6f1756a955a3e7764fe4b9378d8f9c",
"sourceCodeHash": "0x728635bea9d410a7400a180fb86ecccba8d3597db09ecafeea128b9c6c0374dc"
},
{
"contractName": "gas_test",
"bytecodePath": "bootloader/build/artifacts/gas_test.yul.zbin",
"sourceCodePath": "bootloader/build/gas_test.yul",
"bytecodeHash": "0x010008d7f0ddbde3a633aa89aff433666ba28dcf3adb35deb26a207ea1824454",
"sourceCodeHash": "0xe08f6eaca1995981baf257dd86e5dec8b62c1b5ac53a1173142cebca510b7859"
"bytecodeHash": "0x010008cd45684c5ee2054ad2e450d8f80ab83823a4e962399c377c3222b85496",
"sourceCodeHash": "0xcb2037625380001e9e740f77db701d6bbc8c57008860b76ecaa2f3f52d0f3060"
},
{
"contractName": "playground_batch",
"bytecodePath": "bootloader/build/artifacts/playground_batch.yul.zbin",
"sourceCodePath": "bootloader/build/playground_batch.yul",
"bytecodeHash": "0x01000957fe599ea1dfc3fc958c63b11f46a726d9462078b77c3f28c4db21446c",
"sourceCodeHash": "0x97e33d4d96f9ee25e171add92ec7d725de58904dfc68466e4aae1807e9997dc2"
"bytecodeHash": "0x0100094b444f930471b7c8308557672f9d32173763d0668aee7f183101940752",
"sourceCodeHash": "0xdd861731b1cf0e4dad73f2184c79555958fcf3ab51bd9c454ae481694d3db17a"
},
{
"contractName": "proved_batch",
"bytecodePath": "bootloader/build/artifacts/proved_batch.yul.zbin",
"sourceCodePath": "bootloader/build/proved_batch.yul",
"bytecodeHash": "0x010008e756cd4ef7a22e5bf21b357fda5c90223b7947f38cd3749826a6faff68",
"sourceCodeHash": "0x62556a75277d8c72961a1fb9e1d0e1f1cef41c0e8f8d26a6e85cda336a734834"
"bytecodeHash": "0x010008ddc74dd979a491366109361609a0f7afc2626a4bca328593c1a185448b",
"sourceCodeHash": "0x2ff510aebe8b803393c5ecac9d7901630ee7c35931a91a8ce7c3b29e5d9ff803"
}
]
33 changes: 0 additions & 33 deletions system-contracts/bootloader/test_infra/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 41 additions & 29 deletions system-contracts/contracts/SystemContext.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// SPDX-License-Identifier: MIT

// solhint-disable reason-string, gas-custom-errors

pragma solidity 0.8.20;

import {ISystemContext} from "./interfaces/ISystemContext.sol";
import {ISystemContract} from "./interfaces/ISystemContract.sol";
import {ISystemContextDeprecated} from "./interfaces/ISystemContextDeprecated.sol";
import {SystemContractHelper} from "./libraries/SystemContractHelper.sol";
import {BOOTLOADER_FORMAL_ADDRESS, SystemLogKey} from "./Constants.sol";
import {UpgradeTxMustBeFirst, L2BlockCannotBeZero, HashMismatch, NonIncreasingTimestamp, L2BlockNumberAlreadyUsed, TimestampNotEqual, InvalidL2BlockNumber, L2BatchCannotBeZero, ValuesNotEqual} from "./SystemContractErrors.sol";

/**
* @author Matter Labs
Expand Down Expand Up @@ -76,7 +75,6 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr
BlockInfo internal DEPRECATED_currentL2VirtualBlockInfo;
uint256 internal DEPRECATED_virtualBlockUpgradeInfo;


/// @notice Set the chainId origin.
/// @param _newChainId The chainId
function setChainId(uint256 _newChainId) external onlyCallFromForceDeployer {
Expand Down Expand Up @@ -225,14 +223,20 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr
/// @param _expectedPrevL2BlockHash The expected hash of the previous L2 block.
/// @param _isFirstInBatch Whether this method is called for the first time in the batch.
function _upgradeL2Blocks(uint128 _l2BlockNumber, bytes32 _expectedPrevL2BlockHash, bool _isFirstInBatch) internal {
require(_isFirstInBatch, "Upgrade transaction must be first");
if (!_isFirstInBatch) {
revert UpgradeTxMustBeFirst();
}

// This is how it will be commonly done in practice, but it will simplify some logic later
require(_l2BlockNumber > 0, "L2 block number is never expected to be zero");
if (_l2BlockNumber == 0) {
revert L2BlockCannotBeZero();
}

unchecked {
bytes32 correctPrevBlockHash = _calculateLegacyL2BlockHash(_l2BlockNumber - 1);
require(correctPrevBlockHash == _expectedPrevL2BlockHash, "The previous L2 block hash is incorrect");
if (correctPrevBlockHash != _expectedPrevL2BlockHash) {
revert HashMismatch(correctPrevBlockHash, _expectedPrevL2BlockHash);
}

// Whenever we'll be queried about the hashes of the blocks before the upgrade,
// we'll use batches' hashes, so we don't need to store 256 previous hashes.
Expand Down Expand Up @@ -279,10 +283,9 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr
// We check that the timestamp of the L2 block is consistent with the timestamp of the batch.
if (_isFirstInBatch) {
uint128 currentBatchTimestamp = currentBatchInfo.timestamp;
require(
_l2BlockTimestamp >= currentBatchTimestamp,
"The timestamp of the L2 block must be greater than or equal to the timestamp of the current batch"
);
if (_l2BlockTimestamp < currentBatchTimestamp) {
revert NonIncreasingTimestamp();
}
}

(uint128 currentL2BlockNumber, uint128 currentL2BlockTimestamp) = getL2BlockNumberAndTimestamp();
Expand All @@ -294,12 +297,15 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr

_setNewL2BlockData(_l2BlockNumber, _l2BlockTimestamp, _expectedPrevL2BlockHash);
} else if (currentL2BlockNumber == _l2BlockNumber) {
require(!_isFirstInBatch, "Can not reuse L2 block number from the previous batch");
require(currentL2BlockTimestamp == _l2BlockTimestamp, "The timestamp of the same L2 block must be same");
require(
_expectedPrevL2BlockHash == _getLatest257L2blockHash(_l2BlockNumber - 1),
"The previous hash of the same L2 block must be same"
);
if (_isFirstInBatch) {
revert L2BlockNumberAlreadyUsed();
}
if (currentL2BlockTimestamp != _l2BlockTimestamp) {
revert TimestampNotEqual(currentL2BlockTimestamp, _l2BlockTimestamp);
}
if (_expectedPrevL2BlockHash != _getLatest257L2blockHash(_l2BlockNumber - 1)) {
revert HashMismatch(_getLatest257L2blockHash(_l2BlockNumber - 1), _expectedPrevL2BlockHash);
}
} else if (currentL2BlockNumber + 1 == _l2BlockNumber) {
// From the checks in _upgradeL2Blocks it is known that currentL2BlockNumber can not be 0
bytes32 prevL2BlockHash = _getLatest257L2blockHash(currentL2BlockNumber - 1);
Expand All @@ -311,16 +317,17 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr
currentL2BlockTxsRollingHash
);

require(_expectedPrevL2BlockHash == pendingL2BlockHash, "The current L2 block hash is incorrect");
require(
_l2BlockTimestamp > currentL2BlockTimestamp,
"The timestamp of the new L2 block must be greater than the timestamp of the previous L2 block"
);
if (_expectedPrevL2BlockHash != pendingL2BlockHash) {
revert HashMismatch(pendingL2BlockHash, _expectedPrevL2BlockHash);
}
if (_l2BlockTimestamp <= currentL2BlockTimestamp) {
revert NonIncreasingTimestamp();
}

// Since the new block is created, we'll clear out the rolling hash
_setNewL2BlockData(_l2BlockNumber, _l2BlockTimestamp, _expectedPrevL2BlockHash);
} else {
revert("Invalid new L2 block number");
revert InvalidL2BlockNumber();
}
}

Expand All @@ -337,7 +344,9 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr
(, uint128 currentL2BlockTimestamp) = getL2BlockNumberAndTimestamp();

// The structure of the "setNewBatch" implies that currentBatchNumber > 0, but we still double check it
require(currentBatchNumber > 0, "The current batch number must be greater than 0");
if (currentBatchNumber == 0) {
revert L2BatchCannotBeZero();
}

// In order to spend less pubdata, the packed version is published
uint256 packedTimestamps = (uint256(currentBatchTimestamp) << 128) | currentL2BlockTimestamp;
Expand All @@ -353,10 +362,9 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr
/// @param _newTimestamp The timestamp of the new batch.
function _ensureBatchConsistentWithL2Block(uint128 _newTimestamp) internal view {
uint128 currentBlockTimestamp = currentL2BlockInfo.timestamp;
require(
_newTimestamp > currentBlockTimestamp,
"The timestamp of the batch must be greater than the timestamp of the previous block"
);
if (_newTimestamp <= currentBlockTimestamp) {
revert NonIncreasingTimestamp();
}
}

/// @notice Increments the current batch number and sets the new timestamp
Expand All @@ -375,8 +383,12 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, ISystemContr
uint256 _baseFee
) external onlyCallFromBootloader {
(uint128 previousBatchNumber, uint128 previousBatchTimestamp) = getBatchNumberAndTimestamp();
require(_newTimestamp > previousBatchTimestamp, "Timestamps should be incremental");
require(previousBatchNumber + 1 == _expectedNewNumber, "The provided batch number is not correct");
if (_newTimestamp <= previousBatchTimestamp) {
revert NonIncreasingTimestamp();
}
if (previousBatchNumber + 1 != _expectedNewNumber) {
revert ValuesNotEqual(previousBatchNumber + 1, _expectedNewNumber);
}

_ensureBatchConsistentWithL2Block(_newTimestamp);

Expand Down
Loading

0 comments on commit b59d78a

Please sign in to comment.