Skip to content

Commit

Permalink
commit with system upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysr committed Jun 12, 2024
1 parent 9b69905 commit 1c4baf2
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 66 deletions.
129 changes: 100 additions & 29 deletions l1-contracts/test/foundry/unit/concrete/Executor/Committing.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ contract CommittingTest is ExecutorTest {
wrongNewCommitBatchInfoArray[0] = wrongNewCommitBatchInfo;

vm.prank(validator);
// it goes through to the next revert
vm.expectRevert(bytes.concat("tb"));
executor.commitBatches(genesisStoredBatchInfo, wrongNewCommitBatchInfoArray);
}
Expand Down Expand Up @@ -485,6 +486,7 @@ contract CommittingTest is ExecutorTest {
for (uint256 i = 0; i < values.length; i++) {
bytes[] memory wrongL2Logs = Utils.createSystemLogsWithUpgradeTransaction(bytes32(""));
bytes[] memory tooManyLogs = new bytes[](15);

for (uint256 i = 0; i < wrongL2Logs.length; i++) {
tooManyLogs[i] = wrongL2Logs[i];
}
Expand Down Expand Up @@ -513,13 +515,14 @@ contract CommittingTest is ExecutorTest {
wrongNewCommitBatchInfo.systemLogs = Utils.encodePacked(logsWithWrongHash);
IExecutor.CommitBatchInfo[] memory wrongNewCommitBatchInfoArray = new IExecutor.CommitBatchInfo[](1);
wrongNewCommitBatchInfoArray[0] = wrongNewCommitBatchInfo;

vm.prank(validator);
vm.expectRevert(bytes.concat("ut"));
executor.commitBatches(genesisStoredBatchInfo, wrongNewCommitBatchInfoArray);
}

function test_RevertWhen_SystemLogsBadLength() public {
utils.util_setl2SystemContractsUpgradeTxHash(bytes32("not default"));
function test_RevertWhen_systemLogsBadLength() public {
utils.util_setL2SystemContractsUpgradeTxHash(bytes32("not default"));
bytes[] memory logs = Utils.createSystemLogsWithUpgradeTransaction(bytes32("not default"));
delete logs[0];

Expand All @@ -534,19 +537,97 @@ contract CommittingTest is ExecutorTest {
}

function test_RevertWhen_SystemLogIsMissing() public {
for (uint256 i = 0; i < 7; i++) {
bytes[] memory l2Logs = Utils.createSystemLogs();
delete l2Logs[i];
bytes[] memory logs = Utils.createSystemLogs();
delete logs[0];

IExecutor.CommitBatchInfo memory wrongNewCommitBatchInfo = newCommitBatchInfo;
wrongNewCommitBatchInfo.systemLogs = Utils.encodePacked(l2Logs);
IExecutor.CommitBatchInfo[] memory wrongNewCommitBatchInfoArray = new IExecutor.CommitBatchInfo[](1);
wrongNewCommitBatchInfoArray[0] = wrongNewCommitBatchInfo;
IExecutor.CommitBatchInfo memory wrongNewCommitBatchInfo = newCommitBatchInfo;
wrongNewCommitBatchInfo.systemLogs = Utils.encodePacked(logs);
IExecutor.CommitBatchInfo[] memory wrongNewCommitBatchInfoArray = new IExecutor.CommitBatchInfo[](1);
wrongNewCommitBatchInfoArray[0] = wrongNewCommitBatchInfo;

vm.prank(validator);
vm.expectRevert(bytes.concat("b7"));
executor.commitBatches(genesisStoredBatchInfo, wrongNewCommitBatchInfoArray);
}
vm.prank(validator);
vm.expectRevert(bytes.concat("b7"));
executor.commitBatches(genesisStoredBatchInfo, wrongNewCommitBatchInfoArray);
}

function test_successfullyCommitBatchWithSystemUpgrade() public {
// upgrade batch number must be zero to proceed
utils.util_setL2SystemContractsUpgradeBatchNumber(0);
// upgrade tx hash must be non zero to proceed
utils.util_setL2SystemContractsUpgradeTxHash(bytes32("not default"));

// first new batch logs must contain upgrade transaction
// ugrade batch number will be batch number of this batch
bytes[] memory upgradeLogs = Utils.createSystemLogsWithUpgradeTransaction(bytes32("not default"));
uint64 upgradeBatchNumber = 1;

upgradeLogs[uint256(SystemLogKey.PACKED_BATCH_AND_L2_BLOCK_TIMESTAMP_KEY)] = Utils.constructL2Log(
true,
L2_SYSTEM_CONTEXT_ADDRESS,
uint256(SystemLogKey.PACKED_BATCH_AND_L2_BLOCK_TIMESTAMP_KEY),
Utils.packBatchTimestampAndBlockTimestamp(currentTimestamp, currentTimestamp)
);
upgradeLogs[uint256(SystemLogKey.BLOB_ONE_HASH_KEY)] = Utils.constructL2Log(
true,
L2_PUBDATA_CHUNK_PUBLISHER_ADDR,
uint256(SystemLogKey.BLOB_ONE_HASH_KEY),
0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563
);

IExecutor.CommitBatchInfo memory upgradeBatch = newCommitBatchInfo;
upgradeBatch.batchNumber = upgradeBatchNumber;
upgradeBatch.systemLogs = Utils.encodePacked(upgradeLogs);
upgradeBatch.pubdataCommitments = abi.encodePacked(
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
bytes32(uint256(0xbeef))
);

bytes32[] memory blobHashes = new bytes32[](MAX_NUMBER_OF_BLOBS);
blobHashes[0] = 0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563;

bytes32[] memory blobCommitments = new bytes32[](MAX_NUMBER_OF_BLOBS);
blobCommitments[0] = bytes32(uint256(0xbeef));

bytes32 expectedBatchCommitment = Utils.createBatchCommitment(
upgradeBatch,
bytes32(""),
blobCommitments,
blobHashes
);

IExecutor.CommitBatchInfo[] memory batchInfoArray = new IExecutor.CommitBatchInfo[](1);
batchInfoArray[0] = upgradeBatch;

vm.prank(validator);
// solhint-disable-next-line func-named-parameters
vm.expectEmit(true, true, true, true, address(executor));
emit BlockCommit(1, upgradeBatch.newStateRoot, expectedBatchCommitment);
executor.commitBatches(genesisStoredBatchInfo, batchInfoArray);

assertEq(getters.getTotalBatchesCommitted(), 1);
assertEq(getters.getL2SystemContractsUpgradeBatchNumber(), upgradeBatchNumber);
assertEq(getters.getL2SystemContractsUpgradeTxHash(), bytes32("not default"));

IExecutor.StoredBatchInfo memory lastBatch = IExecutor.StoredBatchInfo({
batchNumber: 1,
batchHash: Utils.randomBytes32("newStateRoot"),
indexRepeatedStorageChanges: 0,
numberOfLayer1Txs: 0,
priorityOperationsHash: keccak256(""),
l2LogsTreeRoot: DEFAULT_L2_LOGS_TREE_ROOT_HASH,
timestamp: currentTimestamp,
commitment: expectedBatchCommitment
});

assertEq(utils.util_getStoredBatchHashes(1), keccak256(abi.encode(lastBatch)));

batchInfoArray[0].batchNumber += 1;

// this proves that upgrade batch number is not zero in second call
// and it doesn't go through branch with upgrade
vm.expectRevert(bytes.concat("ut"));
vm.prank(validator);
executor.commitBatches(lastBatch, batchInfoArray);
}

function test_SuccessfullyCommitBatch() public {
Expand Down Expand Up @@ -588,21 +669,12 @@ contract CommittingTest is ExecutorTest {
correctCommitBatchInfoArray[0] = correctNewCommitBatchInfo;

vm.prank(validator);

vm.recordLogs();
// solhint-disable-next-line func-named-parameters
vm.expectEmit(true, true, true, true, address(executor));
emit BlockCommit(1, correctNewCommitBatchInfo.newStateRoot, expectedBatchCommitment);

executor.commitBatches(genesisStoredBatchInfo, correctCommitBatchInfoArray);

Vm.Log[] memory entries = vm.getRecordedLogs();

assertEq(entries.length, 1);
assertEq(entries[0].topics[0], keccak256("BlockCommit(uint256,bytes32,bytes32)"));
assertEq(entries[0].topics[1], bytes32(uint256(1))); // batchNumber
assertEq(entries[0].topics[2], correctNewCommitBatchInfo.newStateRoot); // batchHash
assertEq(entries[0].topics[3], expectedBatchCommitment); // commitment

uint256 totalBatchesCommitted = getters.getTotalBatchesCommitted();
assertEq(totalBatchesCommitted, 1);
assertEq(getters.getTotalBatchesCommitted(), 1);
}

function test_SuccessfullyCommitBatchWithOneBlob() public {
Expand Down Expand Up @@ -709,13 +781,12 @@ contract CommittingTest is ExecutorTest {
correctCommitBatchInfoArray[0].pubdataCommitments = pubdataCommitment;

vm.prank(validator);
// solhint-disable-next-line func-named-parameters
vm.expectEmit(true, false, false, false, address(executor));
emit BlockCommit(1, bytes32(0), bytes32(0));
executor.commitBatches(genesisStoredBatchInfo, correctCommitBatchInfoArray);

uint256 totalBatchesCommitted = getters.getTotalBatchesCommitted();
assertEq(totalBatchesCommitted, 1);

assertEq(getters.getTotalBatchesCommitted(), 1);
vm.clearMockedCalls();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,38 +72,6 @@ contract ExecutorTest is Test {
return selectors;
}

function getGettersSelectors() public view returns (bytes4[] memory) {
bytes4[] memory selectors = new bytes4[](28);
selectors[0] = getters.getVerifier.selector;
selectors[1] = getters.getAdmin.selector;
selectors[2] = getters.getPendingAdmin.selector;
selectors[3] = getters.getTotalBlocksCommitted.selector;
selectors[4] = getters.getTotalBlocksVerified.selector;
selectors[5] = getters.getTotalBlocksExecuted.selector;
selectors[6] = getters.getTotalPriorityTxs.selector;
selectors[7] = getters.getFirstUnprocessedPriorityTx.selector;
selectors[8] = getters.getPriorityQueueSize.selector;
selectors[9] = getters.priorityQueueFrontOperation.selector;
selectors[10] = getters.isValidator.selector;
selectors[11] = getters.l2LogsRootHash.selector;
selectors[12] = getters.storedBatchHash.selector;
selectors[13] = getters.getL2BootloaderBytecodeHash.selector;
selectors[14] = getters.getL2DefaultAccountBytecodeHash.selector;
selectors[15] = getters.getVerifierParams.selector;
selectors[16] = getters.isDiamondStorageFrozen.selector;
selectors[17] = getters.getPriorityTxMaxGasLimit.selector;
selectors[18] = getters.isEthWithdrawalFinalized.selector;
selectors[19] = getters.facets.selector;
selectors[20] = getters.facetFunctionSelectors.selector;
selectors[21] = getters.facetAddresses.selector;
selectors[22] = getters.facetAddress.selector;
selectors[23] = getters.isFunctionFreezable.selector;
selectors[24] = getters.isFacetFreezable.selector;
selectors[25] = getters.getTotalBatchesCommitted.selector;
selectors[26] = getters.getTotalBatchesVerified.selector;
selectors[27] = getters.getTotalBatchesExecuted.selector;
return selectors;
}

function getMailboxSelectors() private view returns (bytes4[] memory) {
bytes4[] memory selectors = new bytes4[](6);
Expand Down Expand Up @@ -208,7 +176,7 @@ contract ExecutorTest is Test {
facet: address(getters),
action: Diamond.Action.Add,
isFreezable: false,
selectors: getGettersSelectors()
selectors: Utils.getGettersSelectors()
});
facetCuts[3] = Diamond.FacetCut({
facet: address(mailbox),
Expand Down
8 changes: 5 additions & 3 deletions l1-contracts/test/foundry/unit/concrete/Utils/Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ library Utils {
}

function getGettersSelectors() public pure returns (bytes4[] memory) {
bytes4[] memory selectors = new bytes4[](29);
bytes4[] memory selectors = new bytes4[](30);
selectors[0] = GettersFacet.getVerifier.selector;
selectors[1] = GettersFacet.getAdmin.selector;
selectors[2] = GettersFacet.getPendingAdmin.selector;
Expand Down Expand Up @@ -240,6 +240,7 @@ library Utils {
selectors[26] = GettersFacet.getTotalBatchesVerified.selector;
selectors[27] = GettersFacet.getTotalBatchesExecuted.selector;
selectors[28] = GettersFacet.getL2SystemContractsUpgradeTxHash.selector;
selectors[29] = GettersFacet.getL2SystemContractsUpgradeBatchNumber.selector;
return selectors;
}

Expand All @@ -256,7 +257,7 @@ library Utils {
}

function getUtilsFacetSelectors() public pure returns (bytes4[] memory) {
bytes4[] memory selectors = new bytes4[](40);
bytes4[] memory selectors = new bytes4[](41);
selectors[0] = UtilsFacet.util_setChainId.selector;
selectors[1] = UtilsFacet.util_getChainId.selector;
selectors[2] = UtilsFacet.util_setBridgehub.selector;
Expand Down Expand Up @@ -295,8 +296,9 @@ library Utils {
selectors[35] = UtilsFacet.util_getIsFrozen.selector;
selectors[36] = UtilsFacet.util_setTransactionFilterer.selector;
selectors[37] = UtilsFacet.util_setBaseTokenGasPriceMultiplierDenominator.selector;
selectors[38] = UtilsFacet.util_setl2SystemContractsUpgradeTxHash.selector;
selectors[38] = UtilsFacet.util_setL2SystemContractsUpgradeTxHash.selector;
selectors[39] = UtilsFacet.util_setTotalBatchesCommitted.selector;
selectors[40] = UtilsFacet.util_setL2SystemContractsUpgradeBatchNumber.selector;
return selectors;
}

Expand Down
6 changes: 5 additions & 1 deletion l1-contracts/test/foundry/unit/concrete/Utils/UtilsFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,18 @@ contract UtilsFacet is ZkSyncHyperchainBase {
return s.isFrozen;
}

function util_setl2SystemContractsUpgradeTxHash(bytes32 _l2SystemContractsUpgradeTxHash) external {
function util_setL2SystemContractsUpgradeTxHash(bytes32 _l2SystemContractsUpgradeTxHash) external {
s.l2SystemContractsUpgradeTxHash = _l2SystemContractsUpgradeTxHash;
}

function util_setTotalBatchesCommitted(uint256 _totalBatchesCommitted) external {
s.totalBatchesCommitted = _totalBatchesCommitted;
}

function util_setL2SystemContractsUpgradeBatchNumber(uint256 _l2SystemContractsUpgradeBatchNumber) external {
s.l2SystemContractsUpgradeBatchNumber = _l2SystemContractsUpgradeBatchNumber;
}

// add this to be excluded from coverage report
function test() internal virtual {}
}

0 comments on commit 1c4baf2

Please sign in to comment.