From 0f30fd3dcdf008aeaff0a05fa04367be5cf8d240 Mon Sep 17 00:00:00 2001 From: Bence Haromi Date: Mon, 2 Oct 2023 13:12:43 +0100 Subject: [PATCH 01/10] test: temporarily removing DiamondCut and Executor foundry tests --- .../unit/concrete/DiamondCut/FacetCut.t.sol | 379 ----------- .../concrete/DiamondCut/Initialization.t.sol | 77 --- .../concrete/DiamondCut/UpgradeLogic.t.sol | 422 ------------ .../DiamondCut/_DiamondCut_Shared.t.sol | 52 -- .../concrete/Executor/Authorization.t.sol | 69 -- .../unit/concrete/Executor/Committing.t.sol | 618 ------------------ .../unit/concrete/Executor/Executing.t.sol | 247 ------- .../unit/concrete/Executor/Proving.t.sol | 96 --- .../unit/concrete/Executor/Reverting.t.sol | 75 --- .../concrete/Executor/_Executor_Shared.t.sol | 220 ------- 10 files changed, 2255 deletions(-) delete mode 100644 ethereum/test/foundry/unit/concrete/DiamondCut/FacetCut.t.sol delete mode 100644 ethereum/test/foundry/unit/concrete/DiamondCut/Initialization.t.sol delete mode 100644 ethereum/test/foundry/unit/concrete/DiamondCut/UpgradeLogic.t.sol delete mode 100644 ethereum/test/foundry/unit/concrete/DiamondCut/_DiamondCut_Shared.t.sol delete mode 100644 ethereum/test/foundry/unit/concrete/Executor/Authorization.t.sol delete mode 100644 ethereum/test/foundry/unit/concrete/Executor/Committing.t.sol delete mode 100644 ethereum/test/foundry/unit/concrete/Executor/Executing.t.sol delete mode 100644 ethereum/test/foundry/unit/concrete/Executor/Proving.t.sol delete mode 100644 ethereum/test/foundry/unit/concrete/Executor/Reverting.t.sol delete mode 100644 ethereum/test/foundry/unit/concrete/Executor/_Executor_Shared.t.sol diff --git a/ethereum/test/foundry/unit/concrete/DiamondCut/FacetCut.t.sol b/ethereum/test/foundry/unit/concrete/DiamondCut/FacetCut.t.sol deleted file mode 100644 index 6e1eb435c..000000000 --- a/ethereum/test/foundry/unit/concrete/DiamondCut/FacetCut.t.sol +++ /dev/null @@ -1,379 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.17; - -// solhint-disable max-line-length - -import {DiamondCutTest} from "./_DiamondCut_Shared.t.sol"; -import {DiamondCutTestContract} from "../../../../../cache/solpp-generated-contracts/dev-contracts/test/DiamondCutTestContract.sol"; -import {ExecutorFacet} from "../../../../../cache/solpp-generated-contracts/zksync/facets/Executor.sol"; -import {GettersFacet} from "../../../../../cache/solpp-generated-contracts/zksync/facets/Getters.sol"; -import {MailboxFacet} from "../../../../../cache/solpp-generated-contracts/zksync/facets/Mailbox.sol"; -import {Diamond} from "../../../../../cache/solpp-generated-contracts/zksync/libraries/Diamond.sol"; - -// solhint-enable max-line-length - -contract FacetCutTest is DiamondCutTest { - MailboxFacet private mailboxFacet; - ExecutorFacet private executorFacet1; - ExecutorFacet private executorFacet2; - - function getMailboxSelectors() private view returns (bytes4[] memory) { - bytes4[] memory selectors = new bytes4[](6); - selectors[0] = mailboxFacet.proveL2MessageInclusion.selector; - selectors[1] = mailboxFacet.proveL2LogInclusion.selector; - selectors[2] = mailboxFacet.proveL1ToL2TransactionStatus.selector; - selectors[3] = mailboxFacet.finalizeEthWithdrawal.selector; - selectors[4] = mailboxFacet.requestL2Transaction.selector; - selectors[5] = mailboxFacet.l2TransactionBaseCost.selector; - return selectors; - } - - function getExecutorSelectors() private view returns (bytes4[] memory) { - bytes4[] memory selectors = new bytes4[](4); - selectors[0] = executorFacet1.commitBlocks.selector; - selectors[1] = executorFacet1.proveBlocks.selector; - selectors[2] = executorFacet1.executeBlocks.selector; - selectors[3] = executorFacet1.revertBlocks.selector; - return selectors; - } - - function setUp() public { - diamondCutTestContract = new DiamondCutTestContract(); - mailboxFacet = new MailboxFacet(); - gettersFacet = new GettersFacet(); - executorFacet1 = new ExecutorFacet(); - executorFacet2 = new ExecutorFacet(); - } - - function test_AddingFacetsToFreeSelectors() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](3); - facetCuts[0] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Add, - isFreezable: false, - selectors: getMailboxSelectors() - }); - facetCuts[1] = Diamond.FacetCut({ - facet: address(gettersFacet), - action: Diamond.Action.Add, - isFreezable: false, - selectors: getGettersSelectors() - }); - facetCuts[2] = Diamond.FacetCut({ - facet: address(executorFacet1), - action: Diamond.Action.Add, - isFreezable: false, - selectors: getExecutorSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: 0x0000000000000000000000000000000000000000, - initCalldata: bytes("") - }); - - uint256 numOfFacetsBefore = diamondCutTestContract.facetAddresses().length; - - diamondCutTestContract.diamondCut(diamondCutData); - - uint256 numOfFacetsAfter = diamondCutTestContract.facetAddresses().length; - - assertEq(numOfFacetsBefore + facetCuts.length, numOfFacetsAfter, "wrong number of facets added"); - } - - function test_RevertWhen_AddingFacetToOccupiedSelector() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Add, - isFreezable: false, - selectors: getMailboxSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - diamondCutTestContract.diamondCut(diamondCutData); - - vm.expectRevert(abi.encodePacked("J")); - diamondCutTestContract.diamondCut(diamondCutData); - } - - function test_RevertWhen_AddingFacetWithZeroAddress() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(0), - action: Diamond.Action.Add, - isFreezable: false, - selectors: getMailboxSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - vm.expectRevert(abi.encodePacked("G")); - diamondCutTestContract.diamondCut(diamondCutData); - } - - function test_RevertWhen_ReplacingFacetFromFreeSelector() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Replace, - isFreezable: false, - selectors: getMailboxSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - vm.expectRevert(abi.encodePacked("L")); - diamondCutTestContract.diamondCut(diamondCutData); - } - - function test_RevertWhen_RemovingFacetFromFreeSelector() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Remove, - isFreezable: false, - selectors: getMailboxSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - vm.expectRevert(abi.encodePacked("a1")); - diamondCutTestContract.diamondCut(diamondCutData); - } - - function test_ReplaceFacetForOccupiedSelector() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](2); - facetCuts[0] = Diamond.FacetCut({ - facet: address(executorFacet1), - action: Diamond.Action.Add, - isFreezable: false, - selectors: getExecutorSelectors() - }); - facetCuts[1] = Diamond.FacetCut({ - facet: address(executorFacet2), - action: Diamond.Action.Replace, - isFreezable: false, - selectors: getExecutorSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - diamondCutTestContract.diamondCut(diamondCutData); - } - - function test_RemovingFacetFromOccupiedSelector() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](2); - facetCuts[0] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Add, - isFreezable: false, - selectors: getMailboxSelectors() - }); - facetCuts[1] = Diamond.FacetCut({ - facet: address(0), - action: Diamond.Action.Remove, - isFreezable: false, - selectors: getMailboxSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - diamondCutTestContract.diamondCut(diamondCutData); - } - - function test_AddingFacetAfterRemoving() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](3); - facetCuts[0] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Add, - isFreezable: false, - selectors: getMailboxSelectors() - }); - facetCuts[1] = Diamond.FacetCut({ - facet: address(0), - action: Diamond.Action.Remove, - isFreezable: false, - selectors: getMailboxSelectors() - }); - facetCuts[2] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Add, - isFreezable: false, - selectors: getMailboxSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - diamondCutTestContract.diamondCut(diamondCutData); - } - - function test_ReplacingASelectorFacetWithItself() public { - bytes4[] memory selectors = new bytes4[](1); - selectors[0] = 0x00000005; - - Diamond.FacetCut[] memory facetCuts1 = new Diamond.FacetCut[](1); - facetCuts1[0] = Diamond.FacetCut({ - facet: address(0x000000000000000000000000000000000000000A), - action: Diamond.Action.Add, - isFreezable: true, - selectors: selectors - }); - - Diamond.DiamondCutData memory diamondCutData1 = Diamond.DiamondCutData({ - facetCuts: facetCuts1, - initAddress: address(0), - initCalldata: bytes("") - }); - - diamondCutTestContract.diamondCut(diamondCutData1); - - uint256 numOfFacetsAfterAdd = diamondCutTestContract.facetAddresses().length; - - Diamond.FacetCut[] memory facetCuts2 = new Diamond.FacetCut[](1); - facetCuts2[0] = Diamond.FacetCut({ - facet: address(0x000000000000000000000000000000000000000A), - action: Diamond.Action.Replace, - isFreezable: false, - selectors: selectors - }); - - Diamond.DiamondCutData memory diamondCutData2 = Diamond.DiamondCutData({ - facetCuts: facetCuts2, - initAddress: address(0), - initCalldata: bytes("") - }); - - diamondCutTestContract.diamondCut(diamondCutData2); - - uint256 numOfFacetsAfterReplace = diamondCutTestContract.facetAddresses().length; - - assertEq(numOfFacetsAfterAdd, numOfFacetsAfterReplace); - } - - function test_RevertWhen_AddingFacetWithDifferentFreezabilityThanExistingFacets() public { - bytes4[] memory selectors1 = new bytes4[](1); - selectors1[0] = 0x00000001; - - bytes4[] memory selectors2 = new bytes4[](1); - selectors2[0] = 0x00000002; - - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](2); - facetCuts[0] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Add, - isFreezable: false, - selectors: selectors1 - }); - facetCuts[1] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Add, - isFreezable: true, - selectors: selectors2 - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - vm.expectRevert(abi.encodePacked("J1")); - diamondCutTestContract.diamondCut(diamondCutData); - } - - function test_RevertWhen_ReplacingFacetWithDifferentFreezabilityThanExistingFacets() public { - bytes4[] memory selectors1 = new bytes4[](1); - selectors1[0] = 0x00000001; - bytes4[] memory selectors2 = new bytes4[](1); - selectors2[0] = 0x00000002; - - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](3); - facetCuts[0] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Add, - isFreezable: false, - selectors: selectors1 - }); - facetCuts[1] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Add, - isFreezable: false, - selectors: selectors2 - }); - facetCuts[2] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Replace, - isFreezable: true, - selectors: selectors2 - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - vm.expectRevert(abi.encodePacked("J1")); - diamondCutTestContract.diamondCut(diamondCutData); - } - - function test_ChangingFacetFreezability() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](3); - facetCuts[0] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Add, - isFreezable: false, - selectors: getMailboxSelectors() - }); - facetCuts[1] = Diamond.FacetCut({ - facet: address(0), - action: Diamond.Action.Remove, - isFreezable: false, - selectors: getMailboxSelectors() - }); - facetCuts[2] = Diamond.FacetCut({ - facet: address(mailboxFacet), - action: Diamond.Action.Add, - isFreezable: true, - selectors: getMailboxSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - diamondCutTestContract.diamondCut(diamondCutData); - } -} diff --git a/ethereum/test/foundry/unit/concrete/DiamondCut/Initialization.t.sol b/ethereum/test/foundry/unit/concrete/DiamondCut/Initialization.t.sol deleted file mode 100644 index 5606f8620..000000000 --- a/ethereum/test/foundry/unit/concrete/DiamondCut/Initialization.t.sol +++ /dev/null @@ -1,77 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.17; - -// solhint-disable max-line-length - -import {DiamondCutTest} from "./_DiamondCut_Shared.t.sol"; -import {RevertFallback} from "../../../../../cache/solpp-generated-contracts/dev-contracts/RevertFallback.sol"; -import {ReturnSomething} from "../../../../../cache/solpp-generated-contracts/dev-contracts/ReturnSomething.sol"; -import {DiamondCutTestContract} from "../../../../../cache/solpp-generated-contracts/dev-contracts/test/DiamondCutTestContract.sol"; -import {Diamond} from "../../../../../cache/solpp-generated-contracts/zksync/libraries/Diamond.sol"; - -// solhint-enable max-line-length - -contract InitializationTest is DiamondCutTest { - address private revertFallbackAddress; - address private returnSomethingAddress; - address private signerAddress; // EOA - - function setUp() public { - signerAddress = makeAddr("signer"); - diamondCutTestContract = new DiamondCutTestContract(); - revertFallbackAddress = address(new RevertFallback()); - returnSomethingAddress = address(new ReturnSomething()); - } - - function test_RevertWhen_DelegateCallToFailedContract() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](0); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: revertFallbackAddress, - initCalldata: bytes("") - }); - - vm.expectRevert(abi.encodePacked("I")); - diamondCutTestContract.diamondCut(diamondCutData); - } - - function test_ReverWhen_DelegateCallToEOA() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](0); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: signerAddress, - initCalldata: bytes("") - }); - - vm.expectRevert(abi.encodePacked("lp")); - diamondCutTestContract.diamondCut(diamondCutData); - } - - function test_RevertWhen_InitializingDiamondCutWithZeroAddressAndNonZeroData() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](0); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("0x11") - }); - - vm.expectRevert(abi.encodePacked("H")); - diamondCutTestContract.diamondCut(diamondCutData); - } - - function test_RevertWhen_DelegateCallToAContractWithWrongReturn() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](0); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: returnSomethingAddress, - initCalldata: bytes("") - }); - - vm.expectRevert(abi.encodePacked("lp1")); - diamondCutTestContract.diamondCut(diamondCutData); - } -} diff --git a/ethereum/test/foundry/unit/concrete/DiamondCut/UpgradeLogic.t.sol b/ethereum/test/foundry/unit/concrete/DiamondCut/UpgradeLogic.t.sol deleted file mode 100644 index b3dcfde07..000000000 --- a/ethereum/test/foundry/unit/concrete/DiamondCut/UpgradeLogic.t.sol +++ /dev/null @@ -1,422 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.17; - -// solhint-disable max-line-length - -import {DiamondCutTest} from "./_DiamondCut_Shared.t.sol"; -import {Utils} from "../Utils/Utils.sol"; -import {DiamondCutTestContract} from "../../../../../cache/solpp-generated-contracts/dev-contracts/test/DiamondCutTestContract.sol"; -import {DiamondInit} from "../../../../../cache/solpp-generated-contracts/zksync/DiamondInit.sol"; -import {DiamondProxy} from "../../../../../cache/solpp-generated-contracts/zksync/DiamondProxy.sol"; -import {VerifierParams} from "../../../../../cache/solpp-generated-contracts/zksync/Storage.sol"; -import {DiamondCutFacet} from "../../../../../cache/solpp-generated-contracts/zksync/facets/DiamondCut.sol"; -import {GettersFacet} from "../../../../../cache/solpp-generated-contracts/zksync/facets/Getters.sol"; -import {Diamond} from "../../../../../cache/solpp-generated-contracts/zksync/libraries/Diamond.sol"; - -// solhint-enable max-line-length - -contract UpgradeLogicTest is DiamondCutTest { - DiamondProxy private diamondProxy; - DiamondInit private diamondInit; - DiamondCutFacet private diamondCutFacet; - DiamondCutFacet private proxyAsDiamondCut; - GettersFacet private proxyAsGetters; - address private governor; - address private randomSigner; - - function getDiamondCutSelectors() private view returns (bytes4[] memory) { - bytes4[] memory dcSelectors = new bytes4[](8); - dcSelectors[0] = diamondCutFacet.proposeTransparentUpgrade.selector; - dcSelectors[1] = diamondCutFacet.proposeShadowUpgrade.selector; - dcSelectors[2] = diamondCutFacet.cancelUpgradeProposal.selector; - dcSelectors[3] = diamondCutFacet.securityCouncilUpgradeApprove.selector; - dcSelectors[4] = diamondCutFacet.executeUpgrade.selector; - dcSelectors[5] = diamondCutFacet.freezeDiamond.selector; - dcSelectors[6] = diamondCutFacet.unfreezeDiamond.selector; - dcSelectors[7] = diamondCutFacet.upgradeProposalHash.selector; - return dcSelectors; - } - - function setUp() public { - governor = makeAddr("governor"); - randomSigner = makeAddr("randomSigner"); - - diamondCutTestContract = new DiamondCutTestContract(); - diamondInit = new DiamondInit(); - diamondCutFacet = new DiamondCutFacet(); - gettersFacet = new GettersFacet(); - - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](2); - facetCuts[0] = Diamond.FacetCut({ - facet: address(diamondCutFacet), - action: Diamond.Action.Add, - isFreezable: false, - selectors: getDiamondCutSelectors() - }); - facetCuts[1] = Diamond.FacetCut({ - facet: address(gettersFacet), - action: Diamond.Action.Add, - isFreezable: true, - selectors: getGettersSelectors() - }); - - VerifierParams memory dummyVerifierParams = VerifierParams({ - recursionNodeLevelVkHash: 0, - recursionLeafLevelVkHash: 0, - recursionCircuitsSetVksHash: 0 - }); - - bytes memory diamondInitCalldata = abi.encodeWithSelector( - diamondInit.initialize.selector, - 0x03752D8252d67f99888E741E3fB642803B29B155, - governor, - 0x02c775f0a90abf7a0e8043f2fdc38f0580ca9f9996a895d05a501bfeaa3b2e21, - 0, - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x70a0F165d6f8054d0d0CF8dFd4DD2005f0AF6B55, - dummyVerifierParams, - false, - 0x0100000000000000000000000000000000000000000000000000000000000000, - 0x0100000000000000000000000000000000000000000000000000000000000000, - 500000 // priority tx max L2 gas limit - ); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(diamondInit), - initCalldata: diamondInitCalldata - }); - - diamondProxy = new DiamondProxy(block.chainid, diamondCutData); - proxyAsDiamondCut = DiamondCutFacet(address(diamondProxy)); - proxyAsGetters = GettersFacet(address(diamondProxy)); - } - - function test_RevertWhen_EmergencyFreezeWhenUnauthurizedGovernor() public { - vm.startPrank(randomSigner); - - vm.expectRevert(abi.encodePacked("1g")); - proxyAsDiamondCut.freezeDiamond(); - } - - function test_RevertWhen_DoubleFreezingByGovernor() public { - vm.startPrank(governor); - - proxyAsDiamondCut.freezeDiamond(); - - vm.expectRevert(abi.encodePacked("a9")); - proxyAsDiamondCut.freezeDiamond(); - } - - function test_RevertWhen_UnfreezingWhenNotFrozen() public { - vm.startPrank(governor); - - vm.expectRevert(abi.encodePacked("a7")); - proxyAsDiamondCut.unfreezeDiamond(); - } - - function test_RevertWhen_ExecutingUnapprovedProposalWHenDiamondStorageIsFrozen() public { - vm.startPrank(governor); - - proxyAsDiamondCut.freezeDiamond(); - - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(gettersFacet), - action: Diamond.Action.Replace, - isFreezable: true, - selectors: getGettersSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - proxyAsDiamondCut.proposeTransparentUpgrade(diamondCutData, 1); - - vm.expectRevert(abi.encodePacked("f3")); - proxyAsDiamondCut.executeUpgrade(diamondCutData, 0); - } - - function test_RevertWhen_ExecutingProposalWithDifferentInitAddress() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(gettersFacet), - action: Diamond.Action.Add, - isFreezable: true, - selectors: getGettersSelectors() - }); - - Diamond.DiamondCutData memory proposedDiamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - Diamond.DiamondCutData memory executedDiamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(1), - initCalldata: bytes("") - }); - - uint40 nextProposalId = uint40(proxyAsGetters.getCurrentProposalId() + 1); - - vm.startPrank(governor); - - proxyAsDiamondCut.proposeTransparentUpgrade(proposedDiamondCutData, nextProposalId); - - vm.expectRevert(abi.encodePacked("a4")); - proxyAsDiamondCut.executeUpgrade(executedDiamondCutData, 0); - } - - function test_RevertWhen_ExecutingProposalWithDifferentFacetCut() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(gettersFacet), - action: Diamond.Action.Replace, - isFreezable: true, - selectors: getGettersSelectors() - }); - - Diamond.FacetCut[] memory invalidFacetCuts = new Diamond.FacetCut[](1); - invalidFacetCuts[0] = Diamond.FacetCut({ - facet: address(gettersFacet), - action: Diamond.Action.Replace, - isFreezable: false, - selectors: getGettersSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - Diamond.DiamondCutData memory invalidDiamondCutData = Diamond.DiamondCutData({ - facetCuts: invalidFacetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - uint40 nextProposalId = uint40(proxyAsGetters.getCurrentProposalId() + 1); - - vm.startPrank(governor); - proxyAsDiamondCut.proposeTransparentUpgrade(diamondCutData, nextProposalId); - - vm.expectRevert(abi.encodePacked("a4")); - proxyAsDiamondCut.executeUpgrade(invalidDiamondCutData, 0); - } - - function test_RevertWhen_CancelingEmptyProposal() public { - bytes32 proposalHash = proxyAsGetters.getProposedUpgradeHash(); - - vm.startPrank(governor); - - vm.expectRevert(abi.encodePacked("a3")); - proxyAsDiamondCut.cancelUpgradeProposal(proposalHash); - } - - function test_ProposeAndExecuteDiamondCut() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(gettersFacet), - action: Diamond.Action.Replace, - isFreezable: true, - selectors: getGettersSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - uint40 nextProposalId = uint40(proxyAsGetters.getCurrentProposalId() + 1); - - vm.startPrank(governor); - - proxyAsDiamondCut.proposeTransparentUpgrade(diamondCutData, nextProposalId); - - proxyAsDiamondCut.executeUpgrade(diamondCutData, 0); - - bytes4[] memory gettersFacetSelectors = getGettersSelectors(); - for (uint256 i = 0; i < gettersFacetSelectors.length; i++) { - bytes4 selector = gettersFacetSelectors[i]; - - address addr = proxyAsGetters.facetAddress(selector); - assertEq(addr, address(gettersFacet), "facet address mismatch"); - - bool isFreezable = proxyAsGetters.isFunctionFreezable(selector); - assertTrue(isFreezable, "isFreezable mismatch"); - } - } - - function test_RevertWhen_ExecutingSameProposalTwoTimes() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(gettersFacet), - action: Diamond.Action.Replace, - isFreezable: true, - selectors: getGettersSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - uint40 nextProposalId = uint40(proxyAsGetters.getCurrentProposalId() + 1); - - vm.startPrank(governor); - - proxyAsDiamondCut.proposeTransparentUpgrade(diamondCutData, nextProposalId); - - proxyAsDiamondCut.executeUpgrade(diamondCutData, 0); - - vm.expectRevert(abi.encodePacked("ab")); - proxyAsDiamondCut.executeUpgrade(diamondCutData, 0); - } - - function test_RevertWhen_ProposingAnAlreadyPropsedUpgrade() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(gettersFacet), - action: Diamond.Action.Replace, - isFreezable: true, - selectors: getGettersSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - uint40 nextProposalId = uint40(proxyAsGetters.getCurrentProposalId() + 1); - - vm.startPrank(governor); - - proxyAsDiamondCut.proposeTransparentUpgrade(diamondCutData, nextProposalId); - - vm.expectRevert(abi.encodePacked("a8")); - proxyAsDiamondCut.proposeTransparentUpgrade(diamondCutData, nextProposalId); - } - - function test_RevertWhen_ExecutingUnapprovedShadowUpgrade() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(gettersFacet), - action: Diamond.Action.Replace, - isFreezable: true, - selectors: getGettersSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - uint40 nextProposalId = uint40(proxyAsGetters.getCurrentProposalId() + 1); - - vm.startPrank(governor); - - bytes32 executingProposalHash = proxyAsDiamondCut.upgradeProposalHash(diamondCutData, nextProposalId, 0); - - proxyAsDiamondCut.proposeShadowUpgrade(executingProposalHash, nextProposalId); - - vm.expectRevert(abi.encodePacked("av")); - proxyAsDiamondCut.executeUpgrade(diamondCutData, 0); - } - - function test_RevertWhen_ProposingShadowUpgradeWithWrongProposalId() public { - uint40 nextProposalId = uint40(proxyAsGetters.getCurrentProposalId() + 1); - - vm.startPrank(governor); - - bytes32 porposalHash = Utils.randomBytes32("porposalHash"); - - vm.expectRevert(abi.encodePacked("ya")); - proxyAsDiamondCut.proposeShadowUpgrade(porposalHash, nextProposalId + 1); - } - - function test_RevertWhen_ProposingTransparentUpgradeWithWrongProposalId() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(gettersFacet), - action: Diamond.Action.Replace, - isFreezable: true, - selectors: getGettersSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - uint40 currentProposalId = uint40(proxyAsGetters.getCurrentProposalId()); - - vm.startPrank(governor); - - vm.expectRevert(abi.encodePacked("yb")); - proxyAsDiamondCut.proposeTransparentUpgrade(diamondCutData, currentProposalId); - } - - function test_RevertWhen_CancellingUpgradeProposalWithWrongHash() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(gettersFacet), - action: Diamond.Action.Replace, - isFreezable: true, - selectors: getGettersSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - uint40 nextProposalId = uint40(proxyAsGetters.getCurrentProposalId() + 1); - - vm.startPrank(governor); - - proxyAsDiamondCut.proposeTransparentUpgrade(diamondCutData, nextProposalId); - - bytes32 proposedUpgradeHash = Utils.randomBytes32("proposedUpgradeHash"); - - vm.expectRevert(abi.encodePacked("rx")); - proxyAsDiamondCut.cancelUpgradeProposal(proposedUpgradeHash); - } - - function test_RevertWhen_ExecutingTransparentUpgradeWithNonZeroSalt() public { - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](1); - facetCuts[0] = Diamond.FacetCut({ - facet: address(gettersFacet), - action: Diamond.Action.Replace, - isFreezable: true, - selectors: getGettersSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(0), - initCalldata: bytes("") - }); - - uint40 nextProposalId = uint40(proxyAsGetters.getCurrentProposalId() + 1); - - vm.startPrank(governor); - - proxyAsDiamondCut.proposeTransparentUpgrade(diamondCutData, nextProposalId); - - bytes32 proposalSalt = Utils.randomBytes32("proposalSalt"); - - vm.expectRevert(abi.encodePacked("po")); - proxyAsDiamondCut.executeUpgrade(diamondCutData, proposalSalt); - } -} diff --git a/ethereum/test/foundry/unit/concrete/DiamondCut/_DiamondCut_Shared.t.sol b/ethereum/test/foundry/unit/concrete/DiamondCut/_DiamondCut_Shared.t.sol deleted file mode 100644 index a662c1536..000000000 --- a/ethereum/test/foundry/unit/concrete/DiamondCut/_DiamondCut_Shared.t.sol +++ /dev/null @@ -1,52 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.17; - -// solhint-disable max-line-length - -import {Test} from "forge-std/Test.sol"; -import {DiamondCutTestContract} from "../../../../../cache/solpp-generated-contracts/dev-contracts/test/DiamondCutTestContract.sol"; -import {GettersFacet} from "../../../../../cache/solpp-generated-contracts/zksync/facets/Getters.sol"; - -// solhint-enable max-line-length - -contract DiamondCutTest is Test { - DiamondCutTestContract internal diamondCutTestContract; - GettersFacet internal gettersFacet; - - function getGettersSelectors() public view returns (bytes4[] memory) { - bytes4[] memory selectors = new bytes4[](32); - selectors[0] = gettersFacet.getVerifier.selector; - selectors[1] = gettersFacet.getGovernor.selector; - selectors[2] = gettersFacet.getPendingGovernor.selector; - selectors[3] = gettersFacet.getTotalBlocksCommitted.selector; - selectors[4] = gettersFacet.getTotalBlocksVerified.selector; - selectors[5] = gettersFacet.getTotalBlocksExecuted.selector; - selectors[6] = gettersFacet.getTotalPriorityTxs.selector; - selectors[7] = gettersFacet.getFirstUnprocessedPriorityTx.selector; - selectors[8] = gettersFacet.getPriorityQueueSize.selector; - selectors[9] = gettersFacet.priorityQueueFrontOperation.selector; - selectors[10] = gettersFacet.isValidator.selector; - selectors[11] = gettersFacet.l2LogsRootHash.selector; - selectors[12] = gettersFacet.storedBlockHash.selector; - selectors[13] = gettersFacet.getL2BootloaderBytecodeHash.selector; - selectors[14] = gettersFacet.getL2DefaultAccountBytecodeHash.selector; - selectors[15] = gettersFacet.getVerifierParams.selector; - selectors[16] = gettersFacet.isDiamondStorageFrozen.selector; - selectors[17] = gettersFacet.getSecurityCouncil.selector; - selectors[18] = gettersFacet.getUpgradeProposalState.selector; - selectors[19] = gettersFacet.getProposedUpgradeHash.selector; - selectors[20] = gettersFacet.getProposedUpgradeTimestamp.selector; - selectors[21] = gettersFacet.getCurrentProposalId.selector; - selectors[22] = gettersFacet.isApprovedBySecurityCouncil.selector; - selectors[23] = gettersFacet.getPriorityTxMaxGasLimit.selector; - selectors[24] = gettersFacet.getAllowList.selector; - selectors[25] = gettersFacet.isEthWithdrawalFinalized.selector; - selectors[26] = gettersFacet.facets.selector; - selectors[27] = gettersFacet.facetFunctionSelectors.selector; - selectors[28] = gettersFacet.facetAddresses.selector; - selectors[29] = gettersFacet.facetAddress.selector; - selectors[30] = gettersFacet.isFunctionFreezable.selector; - selectors[31] = gettersFacet.isFacetFreezable.selector; - return selectors; - } -} diff --git a/ethereum/test/foundry/unit/concrete/Executor/Authorization.t.sol b/ethereum/test/foundry/unit/concrete/Executor/Authorization.t.sol deleted file mode 100644 index df8542c6a..000000000 --- a/ethereum/test/foundry/unit/concrete/Executor/Authorization.t.sol +++ /dev/null @@ -1,69 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.17; - -import {ExecutorTest} from "./_Executor_Shared.t.sol"; -import {Utils} from "../Utils/Utils.sol"; -import {IExecutor} from "../../../../../cache/solpp-generated-contracts/zksync/interfaces/IExecutor.sol"; - -contract AuthorizationTest is ExecutorTest { - IExecutor.StoredBlockInfo private storedBlockInfo; - IExecutor.CommitBlockInfo private commitBlockInfo; - - function setUp() public { - storedBlockInfo = IExecutor.StoredBlockInfo({ - blockNumber: 1, - blockHash: Utils.randomBytes32("blockHash"), - indexRepeatedStorageChanges: 0, - numberOfLayer1Txs: 0, - priorityOperationsHash: keccak256(""), - l2LogsTreeRoot: Utils.randomBytes32("l2LogsTreeRoot"), - timestamp: 0, - commitment: Utils.randomBytes32("commitment") - }); - - commitBlockInfo = IExecutor.CommitBlockInfo({ - blockNumber: 0, - timestamp: 0, - indexRepeatedStorageChanges: 0, - newStateRoot: Utils.randomBytes32("newStateRoot"), - numberOfLayer1Txs: 0, - l2LogsTreeRoot: Utils.randomBytes32("l2LogsTreeRoot"), - priorityOperationsHash: keccak256(""), - initialStorageChanges: bytes(""), - repeatedStorageChanges: bytes(""), - l2Logs: bytes(""), - l2ArbitraryLengthMessages: new bytes[](0), - factoryDeps: new bytes[](0) - }); - } - - function test_RevertWhen_CommitingByUnauthorisedAddress() public { - IExecutor.CommitBlockInfo[] memory commitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - commitBlockInfoArray[0] = commitBlockInfo; - - vm.prank(randomSigner); - - vm.expectRevert(bytes.concat("1h")); - executor.commitBlocks(storedBlockInfo, commitBlockInfoArray); - } - - function test_RevertWhen_ProvingByUnauthorisedAddress() public { - IExecutor.StoredBlockInfo[] memory storedBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - storedBlockInfoArray[0] = storedBlockInfo; - - vm.prank(owner); - - vm.expectRevert(bytes.concat("1h")); - executor.proveBlocks(storedBlockInfo, storedBlockInfoArray, proofInput); - } - - function test_RevertWhen_ExecutingByUnauthorizedAddress() public { - IExecutor.StoredBlockInfo[] memory storedBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - storedBlockInfoArray[0] = storedBlockInfo; - - vm.prank(randomSigner); - - vm.expectRevert(bytes.concat("1h")); - executor.executeBlocks(storedBlockInfoArray); - } -} diff --git a/ethereum/test/foundry/unit/concrete/Executor/Committing.t.sol b/ethereum/test/foundry/unit/concrete/Executor/Committing.t.sol deleted file mode 100644 index b07c59062..000000000 --- a/ethereum/test/foundry/unit/concrete/Executor/Committing.t.sol +++ /dev/null @@ -1,618 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.17; - -import {Vm} from "forge-std/Test.sol"; -import {ExecutorTest} from "./_Executor_Shared.t.sol"; -import {Utils} from "../Utils/Utils.sol"; -import {L2_BOOTLOADER_ADDRESS} from "../../../../../cache/solpp-generated-contracts/common/L2ContractAddresses.sol"; -import {IExecutor} from "../../../../../cache/solpp-generated-contracts/zksync/interfaces/IExecutor.sol"; - -contract CommittingTest is ExecutorTest { - function test_RevertWhen_ComittingWithWrongLastCommittedBlockData() public { - IExecutor.CommitBlockInfo[] memory newCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - newCommitBlockInfoArray[0] = newCommitBlockInfo; - - IExecutor.StoredBlockInfo memory wrongGenesisStoredBlockInfo = genesisStoredBlockInfo; - wrongGenesisStoredBlockInfo.timestamp = 1000; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("i")); - executor.commitBlocks(wrongGenesisStoredBlockInfo, newCommitBlockInfoArray); - } - - function test_RevertWhen_ComittingWithWrongOrderOfBlocks() public { - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.blockNumber = 2; // wrong block number - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("f")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithWrongNewBlockTimestamp() public { - bytes32 wrongNewBlockTimestamp = Utils.randomBytes32("wrongNewBlockTimestamp"); - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - wrongNewBlockTimestamp, - bytes32("") - ); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("tb")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithTooSmallNewBlockTimestamp() public { - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - address(L2_SYSTEM_CONTEXT_ADDRESS), - Utils.packBatchTimestampAndBlockTimestamp(1, 1), - bytes32("") - ); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - wrongNewCommitBlockInfo.timestamp = 1; // too small - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("h1")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingTooBigLastL2BlockTimestamp() public { - uint64 wrongL2BlockTimestamp = 0xffffffff; - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - address(L2_SYSTEM_CONTEXT_ADDRESS), - Utils.packBatchTimestampAndBlockTimestamp(wrongL2BlockTimestamp, wrongL2BlockTimestamp), - bytes32("") - ); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - wrongNewCommitBlockInfo.timestamp = wrongL2BlockTimestamp; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("h2")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithWrongPreviousBlockHash() public { - bytes32 wrongPreviousBlockHash = Utils.randomBytes32("wrongPreviousBlockHash"); - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - uint256(currentTimestamp), - wrongPreviousBlockHash - ); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("l")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithoutProcessingSystemContextLog() public { - bytes memory wrongL2Logs = abi.encodePacked(bytes4(0x00000000)); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("by")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithProcessingSystemContextLogTwice() public { - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000002), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - uint256(currentTimestamp), - bytes32(""), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - uint256(currentTimestamp), - bytes32("") - ); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("fx")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_UnexpectedL2ToL1Log() public { - address unexpectedAddress = address(0); - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - unexpectedAddress, - uint256(currentTimestamp), - bytes32("") - ); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("ne")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithWrongCanonicalTxHash() public { - bytes32 canonicalTxHash = Utils.randomBytes32("canonicalTxHash"); - - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000002), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - uint256(currentTimestamp), - bytes32(""), - bytes4(0x00010000), - L2_BOOTLOADER_ADDRESS, - canonicalTxHash, - uint256(1) - ); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("t")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithWrongNumberOfLayer1txs() public { - bytes32 arbitraryCanonicalTxHash = Utils.randomBytes32("arbitraryCanonicalTxHash"); - bytes32 chainedPriorityTxHash = keccak256(bytes.concat(keccak256(""), arbitraryCanonicalTxHash)); - - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000002), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - uint256(currentTimestamp), - bytes32(""), - bytes4(0x00010000), - L2_BOOTLOADER_ADDRESS, - arbitraryCanonicalTxHash, - uint256(1) - ); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - wrongNewCommitBlockInfo.priorityOperationsHash = bytes32(chainedPriorityTxHash); - wrongNewCommitBlockInfo.numberOfLayer1Txs = 2; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("ta")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithWrongFactoryDepsData() public { - bytes32 randomFactoryDeps0 = Utils.randomBytes32("randomFactoryDeps0"); - bytes32 randomFactoryDeps1 = Utils.randomBytes32("randomFactoryDeps1"); - - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000002), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - uint256(currentTimestamp), - bytes32(""), - bytes4(0x00010000), - L2_KNOWN_CODE_STORAGE_ADDRESS, - randomFactoryDeps0 - ); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - - bytes[] memory factoryDeps = new bytes[](1); - factoryDeps[0] = bytes.concat(randomFactoryDeps1); - - wrongNewCommitBlockInfo.factoryDeps = factoryDeps; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("k3")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithWrongFactoryDepsArrayLength() public { - bytes32 arbitraryBytecode = Utils.randomBytes32("arbitraryBytecode"); - bytes32 arbitraryBytecodeHash = sha256(bytes.concat(arbitraryBytecode)); - uint256 arbitraryBytecodeHashManipulated1 = uint256(arbitraryBytecodeHash) & - 0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; - uint256 arbitraryBytecodeHashManipulated2 = arbitraryBytecodeHashManipulated1 | - 0x0100000100000000000000000000000000000000000000000000000000000000; - - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000002), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - uint256(currentTimestamp), - bytes32(""), - bytes4(0x00010000), - L2_KNOWN_CODE_STORAGE_ADDRESS, - uint256(arbitraryBytecodeHashManipulated2) - ); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - - bytes[] memory factoryDeps = new bytes[](2); - factoryDeps[0] = bytes.concat(arbitraryBytecode); - factoryDeps[1] = bytes.concat(arbitraryBytecode); - - wrongNewCommitBlockInfo.factoryDeps = factoryDeps; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("ym")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_ComittingWithWrongHashedMessage() public { - bytes32 randomL2LogValue = Utils.randomBytes32("randomL2LogValue"); - - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000002), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - uint256(currentTimestamp), - bytes32(""), - bytes4(0x00010000), - L2_TO_L1_MESSENGER, - bytes32(""), - randomL2LogValue - ); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - - bytes32 randomL2Message = Utils.randomBytes32("randomL2Message"); - - bytes[] memory l2ArbitraryLengthMessages = new bytes[](1); - l2ArbitraryLengthMessages[0] = bytes.concat(randomL2Message); - - wrongNewCommitBlockInfo.l2ArbitraryLengthMessages = l2ArbitraryLengthMessages; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("k2")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithWrongNumberOfMessages() public { - bytes memory arbitraryMessage = abi.encodePacked(uint8(0xaa)); - bytes32 arbitraryHashedMessage = keccak256(arbitraryMessage); - - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000002), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - uint256(currentTimestamp), - bytes32(""), - bytes4(0x00010000), - L2_TO_L1_MESSENGER, - bytes32(""), - arbitraryHashedMessage - ); - - bytes[] memory l2ArbitraryLengthMessagesArray = new bytes[](2); - l2ArbitraryLengthMessagesArray[0] = arbitraryMessage; - l2ArbitraryLengthMessagesArray[1] = arbitraryMessage; - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - wrongNewCommitBlockInfo.l2ArbitraryLengthMessages = l2ArbitraryLengthMessagesArray; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - vm.expectRevert(bytes.concat("pl")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithWrongBytecodeLength() public { - bytes32 randomFactoryDeps0 = Utils.randomBytes32("randomFactoryDeps0"); - - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000002), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - uint256(currentTimestamp), - bytes32(""), - bytes4(0x00010000), - L2_KNOWN_CODE_STORAGE_ADDRESS, - randomFactoryDeps0 - ); - - bytes20 randomFactoryDeps1 = bytes20(randomFactoryDeps0); - - bytes[] memory factoryDeps = new bytes[](1); - factoryDeps[0] = bytes.concat(randomFactoryDeps1); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - wrongNewCommitBlockInfo.factoryDeps = factoryDeps; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("bl")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithWrongNumberOfWordsInBytecode() public { - bytes32 randomFactoryDeps0 = Utils.randomBytes32("randomFactoryDeps0"); - - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000002), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - uint256(currentTimestamp), - bytes32(""), - bytes4(0x00010000), - L2_KNOWN_CODE_STORAGE_ADDRESS, - randomFactoryDeps0 - ); - - bytes memory randomFactoryDeps1 = bytes.concat(randomFactoryDeps0, randomFactoryDeps0); - - bytes[] memory factoryDeps = new bytes[](1); - factoryDeps[0] = bytes.concat(randomFactoryDeps1); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - wrongNewCommitBlockInfo.factoryDeps = factoryDeps; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("pr")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithWrongRepeatedStorageWrites() public { - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - Utils.packBatchTimestampAndBlockTimestamp(currentTimestamp, currentTimestamp), - bytes32("") - ); - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - wrongNewCommitBlockInfo.indexRepeatedStorageChanges = 0; - wrongNewCommitBlockInfo.initialStorageChanges = "0x00000001"; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("yq")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingWithTooLongL2Logs() public { - // uint256 constant MAX_L2_TO_L1_LOGS_COMMITMENT_BYTES = 4 + L2_TO_L1_LOG_SERIALIZE_SIZE * 512; - bytes memory arr1; - - for (uint16 i = 0; i < 512; i++) { - arr1 = abi.encodePacked(arr1, bytes4(0x00000000), L2_TO_L1_MESSENGER, bytes32(""), keccak256("")); - } - - bytes memory wrongL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - Utils.packBatchTimestampAndBlockTimestamp(currentTimestamp, currentTimestamp), - bytes32(""), - arr1 - ); - - bytes[] memory l2ArbitraryLengthMessages = new bytes[](512); - - for (uint16 i = 0; i < l2ArbitraryLengthMessages.length; i++) { - l2ArbitraryLengthMessages[i] = bytes(""); - } - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = wrongL2Logs; - wrongNewCommitBlockInfo.l2ArbitraryLengthMessages = l2ArbitraryLengthMessages; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("pu")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingTooLongRepeatedStorageChanges() public { - bytes memory correctL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - Utils.packBatchTimestampAndBlockTimestamp(currentTimestamp, currentTimestamp), - bytes32("") - ); - - // 7565 * 40 bytes = 302600 bytes long repeatedStorageChanges - // which is longer than 302564 (MAX_REPEATED_STORAGE_CHANGES_COMMITMENT_BYTES = - // 4 + REPEATED_STORAGE_CHANGE_SERIALIZE_SIZE * 7564) - uint256 wrongRepeatedStorageChangesLen = 7565 * 40; - bytes memory wrongRepeatedStorageChanges = new bytes(wrongRepeatedStorageChangesLen); - - assembly { - let ptr := add(wrongRepeatedStorageChanges, 32) - let end := add(ptr, wrongRepeatedStorageChangesLen) - - for { - - } lt(ptr, end) { - - } { - mstore(ptr, 0x0000000000000000000000000000000000000000000000000000000000000000) - ptr := add(ptr, 40) - } - } - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = correctL2Logs; - wrongNewCommitBlockInfo.repeatedStorageChanges = wrongRepeatedStorageChanges; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("py")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_RevertWhen_CommittingTooLongInitialStorageChanges() public { - bytes memory correctL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - Utils.packBatchTimestampAndBlockTimestamp(currentTimestamp, currentTimestamp), - bytes32("") - ); - - // 4766 * 64 bytes = 305024 bytes long initialStorageChangesLen - // which is longer than 304964 (MAX_INITIAL_STORAGE_CHANGES_COMMITMENT_BYTES = - // 4 + INITIAL_STORAGE_CHANGE_SERIALIZE_SIZE * 4765) - uint256 wrongInitialStorageChangesLen = 4766 * 64; - bytes memory wrongInitialStorageChanges = new bytes(wrongInitialStorageChangesLen); - - assembly { - let ptr := add(wrongInitialStorageChanges, 32) - let end := add(ptr, wrongInitialStorageChangesLen) - - for { - - } lt(ptr, end) { - - } { - mstore(ptr, 0x0000000000000000000000000000000000000000000000000000000000000000) - ptr := add(ptr, 64) - } - } - - IExecutor.CommitBlockInfo memory wrongNewCommitBlockInfo = newCommitBlockInfo; - wrongNewCommitBlockInfo.l2Logs = correctL2Logs; - wrongNewCommitBlockInfo.initialStorageChanges = wrongInitialStorageChanges; - - IExecutor.CommitBlockInfo[] memory wrongNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - wrongNewCommitBlockInfoArray[0] = wrongNewCommitBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("pf")); - executor.commitBlocks(genesisStoredBlockInfo, wrongNewCommitBlockInfoArray); - } - - function test_SuccessfullyCommitBlock() public { - bytes memory correctL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - Utils.packBatchTimestampAndBlockTimestamp(currentTimestamp, currentTimestamp), - bytes32("") - ); - - IExecutor.CommitBlockInfo memory correctNewCommitBlockInfo = newCommitBlockInfo; - correctNewCommitBlockInfo.l2Logs = correctL2Logs; - - IExecutor.CommitBlockInfo[] memory commitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - commitBlockInfoArray[0] = correctNewCommitBlockInfo; - - vm.prank(validator); - - vm.recordLogs(); - - executor.commitBlocks(genesisStoredBlockInfo, commitBlockInfoArray); - - 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))); // blockNumber - - uint256 totalBlocksCommitted = getters.getTotalBlocksCommitted(); - assertEq(totalBlocksCommitted, 1); - } -} diff --git a/ethereum/test/foundry/unit/concrete/Executor/Executing.t.sol b/ethereum/test/foundry/unit/concrete/Executor/Executing.t.sol deleted file mode 100644 index bbfaa2d29..000000000 --- a/ethereum/test/foundry/unit/concrete/Executor/Executing.t.sol +++ /dev/null @@ -1,247 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.17; - -// solhint-disable max-line-length - -import {Vm} from "forge-std/Test.sol"; -import {ExecutorTest} from "./_Executor_Shared.t.sol"; -import {Utils} from "../Utils/Utils.sol"; -import {L2_BOOTLOADER_ADDRESS} from "../../../../../cache/solpp-generated-contracts/common/L2ContractAddresses.sol"; -import {COMMIT_TIMESTAMP_NOT_OLDER, REQUIRED_L2_GAS_PRICE_PER_PUBDATA} from "../../../../../cache/solpp-generated-contracts/zksync/Config.sol"; -import {IExecutor} from "../../../../../cache/solpp-generated-contracts/zksync/interfaces/IExecutor.sol"; - -// solhint-enable max-line-length - -contract ExecutingTest is ExecutorTest { - function setUp() public { - vm.warp(COMMIT_TIMESTAMP_NOT_OLDER + 1); - currentTimestamp = block.timestamp; - - bytes memory correctL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - Utils.packBatchTimestampAndBlockTimestamp(currentTimestamp, currentTimestamp), - bytes32("") - ); - - newCommitBlockInfo.l2Logs = correctL2Logs; - newCommitBlockInfo.timestamp = uint64(currentTimestamp); - - IExecutor.CommitBlockInfo[] memory commitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - commitBlockInfoArray[0] = newCommitBlockInfo; - - vm.prank(validator); - vm.recordLogs(); - executor.commitBlocks(genesisStoredBlockInfo, commitBlockInfoArray); - Vm.Log[] memory entries = vm.getRecordedLogs(); - - newStoredBlockInfo = IExecutor.StoredBlockInfo({ - blockNumber: 1, - blockHash: entries[0].topics[2], - indexRepeatedStorageChanges: 0, - numberOfLayer1Txs: 0, - priorityOperationsHash: keccak256(""), - l2LogsTreeRoot: 0, - timestamp: currentTimestamp, - commitment: entries[0].topics[3] - }); - - IExecutor.StoredBlockInfo[] memory storedBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - storedBlockInfoArray[0] = newStoredBlockInfo; - - vm.prank(validator); - executor.proveBlocks(genesisStoredBlockInfo, storedBlockInfoArray, proofInput); - } - - function test_RevertWhen_ExecutingBlockWithWrongBlockNumber() public { - IExecutor.StoredBlockInfo memory wrongNewStoredBlockInfo = newStoredBlockInfo; - wrongNewStoredBlockInfo.blockNumber = 10; // Correct is 1 - - IExecutor.StoredBlockInfo[] memory storedBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - storedBlockInfoArray[0] = wrongNewStoredBlockInfo; - - vm.prank(validator); - vm.expectRevert(bytes.concat("k")); - executor.executeBlocks(storedBlockInfoArray); - } - - function test_RevertWhen_ExecutingBlockWithWrongData() public { - IExecutor.StoredBlockInfo memory wrongNewStoredBlockInfo = newStoredBlockInfo; - wrongNewStoredBlockInfo.timestamp = 0; // incorrect timestamp - - IExecutor.StoredBlockInfo[] memory storedBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - storedBlockInfoArray[0] = wrongNewStoredBlockInfo; - - vm.prank(validator); - vm.expectRevert(bytes.concat("exe10")); - executor.executeBlocks(storedBlockInfoArray); - } - - function test_RevertWhen_ExecutingRevertedBlockWithoutCommittingAndProvingAgain() public { - vm.prank(validator); - executor.revertBlocks(0); - - IExecutor.StoredBlockInfo[] memory storedBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - storedBlockInfoArray[0] = newStoredBlockInfo; - - vm.prank(validator); - vm.expectRevert(bytes.concat("n")); - executor.executeBlocks(storedBlockInfoArray); - } - - function test_RevertWhen_ExecutingUnavailablePriorityOperationHash() public { - vm.prank(validator); - executor.revertBlocks(0); - - bytes32 arbitraryCanonicalTxHash = Utils.randomBytes32("arbitraryCanonicalTxHash"); - bytes32 chainedPriorityTxHash = keccak256(bytes.concat(keccak256(""), arbitraryCanonicalTxHash)); - - bytes memory correctL2Logs = abi.encodePacked( - bytes4(0x00000002), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - Utils.packBatchTimestampAndBlockTimestamp(currentTimestamp, currentTimestamp), - bytes32(""), - bytes4(0x00010000), - L2_BOOTLOADER_ADDRESS, - arbitraryCanonicalTxHash, - uint256(1) - ); - - IExecutor.CommitBlockInfo memory correctNewCommitBlockInfo = newCommitBlockInfo; - correctNewCommitBlockInfo.l2Logs = correctL2Logs; - correctNewCommitBlockInfo.priorityOperationsHash = chainedPriorityTxHash; - correctNewCommitBlockInfo.numberOfLayer1Txs = 1; - - IExecutor.CommitBlockInfo[] memory correctNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - correctNewCommitBlockInfoArray[0] = correctNewCommitBlockInfo; - - vm.prank(validator); - vm.recordLogs(); - executor.commitBlocks(genesisStoredBlockInfo, correctNewCommitBlockInfoArray); - Vm.Log[] memory entries = vm.getRecordedLogs(); - - IExecutor.StoredBlockInfo memory correctNewStoredBlockInfo = newStoredBlockInfo; - correctNewStoredBlockInfo.blockHash = entries[0].topics[2]; - correctNewStoredBlockInfo.numberOfLayer1Txs = 1; - correctNewStoredBlockInfo.priorityOperationsHash = chainedPriorityTxHash; - correctNewStoredBlockInfo.commitment = entries[0].topics[3]; - - IExecutor.StoredBlockInfo[] memory correctNewStoredBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - correctNewStoredBlockInfoArray[0] = correctNewStoredBlockInfo; - - vm.prank(validator); - executor.proveBlocks(genesisStoredBlockInfo, correctNewStoredBlockInfoArray, proofInput); - - vm.prank(validator); - vm.expectRevert(bytes.concat("s")); - executor.executeBlocks(correctNewStoredBlockInfoArray); - } - - function test_RevertWhen_ExecutingWithUnmatchedPriorityOperationHash() public { - vm.prank(validator); - executor.revertBlocks(0); - - bytes32 arbitraryCanonicalTxHash = Utils.randomBytes32("arbitraryCanonicalTxHash"); - bytes32 chainedPriorityTxHash = keccak256(bytes.concat(keccak256(""), arbitraryCanonicalTxHash)); - - bytes memory correctL2Logs = abi.encodePacked( - bytes4(0x00000002), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - Utils.packBatchTimestampAndBlockTimestamp(currentTimestamp, currentTimestamp), - bytes32(""), - bytes4(0x00010000), - L2_BOOTLOADER_ADDRESS, - arbitraryCanonicalTxHash, - uint256(1) - ); - - IExecutor.CommitBlockInfo memory correctNewCommitBlockInfo = newCommitBlockInfo; - correctNewCommitBlockInfo.l2Logs = correctL2Logs; - correctNewCommitBlockInfo.priorityOperationsHash = chainedPriorityTxHash; - correctNewCommitBlockInfo.numberOfLayer1Txs = 1; - - IExecutor.CommitBlockInfo[] memory correctNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - correctNewCommitBlockInfoArray[0] = correctNewCommitBlockInfo; - - vm.prank(validator); - vm.recordLogs(); - executor.commitBlocks(genesisStoredBlockInfo, correctNewCommitBlockInfoArray); - Vm.Log[] memory entries = vm.getRecordedLogs(); - - IExecutor.StoredBlockInfo memory correctNewStoredBlockInfo = newStoredBlockInfo; - correctNewStoredBlockInfo.blockHash = entries[0].topics[2]; - correctNewStoredBlockInfo.numberOfLayer1Txs = 1; - correctNewStoredBlockInfo.priorityOperationsHash = chainedPriorityTxHash; - correctNewStoredBlockInfo.commitment = entries[0].topics[3]; - - IExecutor.StoredBlockInfo[] memory correctNewStoredBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - correctNewStoredBlockInfoArray[0] = correctNewStoredBlockInfo; - - vm.prank(validator); - executor.proveBlocks(genesisStoredBlockInfo, correctNewStoredBlockInfoArray, proofInput); - - bytes32 randomFactoryDeps0 = Utils.randomBytes32("randomFactoryDeps0"); - - bytes[] memory factoryDeps = new bytes[](1); - factoryDeps[0] = bytes.concat(randomFactoryDeps0); - - uint256 gasPrice = 1000000000; - uint256 l2GasLimit = 1000000; - uint256 baseCost = mailbox.l2TransactionBaseCost(gasPrice, l2GasLimit, REQUIRED_L2_GAS_PRICE_PER_PUBDATA); - uint256 l2Value = 10 ether; - uint256 totalCost = baseCost + l2Value; - - mailbox.requestL2Transaction{value: totalCost}( - address(0), - l2Value, - bytes(""), - l2GasLimit, - REQUIRED_L2_GAS_PRICE_PER_PUBDATA, - factoryDeps, - address(0) - ); - - vm.prank(validator); - vm.expectRevert(bytes.concat("x")); - executor.executeBlocks(correctNewStoredBlockInfoArray); - } - - function test_RevertWhen_CommittingBlockWithWrongPreviousBlockHash() public { - bytes memory correctL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - Utils.packBatchTimestampAndBlockTimestamp(currentTimestamp, currentTimestamp), - bytes32("") - ); - - IExecutor.CommitBlockInfo memory correctNewCommitBlockInfo = newCommitBlockInfo; - correctNewCommitBlockInfo.l2Logs = correctL2Logs; - - IExecutor.CommitBlockInfo[] memory correctNewCommitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - correctNewCommitBlockInfoArray[0] = correctNewCommitBlockInfo; - - bytes32 wrongPreviousBlockHash = Utils.randomBytes32("wrongPreviousBlockHash"); - - IExecutor.StoredBlockInfo memory genesisBlock = genesisStoredBlockInfo; - genesisBlock.blockHash = wrongPreviousBlockHash; - - vm.prank(validator); - vm.expectRevert(bytes.concat("i")); - executor.commitBlocks(genesisBlock, correctNewCommitBlockInfoArray); - } - - function test_ShouldExecuteBlockSuccessfully() public { - IExecutor.StoredBlockInfo[] memory storedBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - storedBlockInfoArray[0] = newStoredBlockInfo; - - vm.prank(validator); - executor.executeBlocks(storedBlockInfoArray); - - uint256 totalBlocksExecuted = getters.getTotalBlocksExecuted(); - assertEq(totalBlocksExecuted, 1); - } -} diff --git a/ethereum/test/foundry/unit/concrete/Executor/Proving.t.sol b/ethereum/test/foundry/unit/concrete/Executor/Proving.t.sol deleted file mode 100644 index 806db9d02..000000000 --- a/ethereum/test/foundry/unit/concrete/Executor/Proving.t.sol +++ /dev/null @@ -1,96 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.17; - -import {Vm} from "forge-std/Test.sol"; -import {ExecutorTest} from "./_Executor_Shared.t.sol"; -import {Utils} from "../Utils/Utils.sol"; -import {COMMIT_TIMESTAMP_NOT_OLDER} from "../../../../../cache/solpp-generated-contracts/zksync/Config.sol"; -import {IExecutor} from "../../../../../cache/solpp-generated-contracts/zksync/interfaces/IExecutor.sol"; - -contract ProvingTest is ExecutorTest { - function setUp() public { - vm.warp(COMMIT_TIMESTAMP_NOT_OLDER + 1); - currentTimestamp = block.timestamp; - - bytes memory correctL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - Utils.packBatchTimestampAndBlockTimestamp(currentTimestamp, currentTimestamp), - bytes32("") - ); - - newCommitBlockInfo.timestamp = uint64(currentTimestamp); - newCommitBlockInfo.l2Logs = correctL2Logs; - - IExecutor.CommitBlockInfo[] memory commitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - commitBlockInfoArray[0] = newCommitBlockInfo; - - vm.prank(validator); - vm.recordLogs(); - executor.commitBlocks(genesisStoredBlockInfo, commitBlockInfoArray); - Vm.Log[] memory entries = vm.getRecordedLogs(); - - newStoredBlockInfo = IExecutor.StoredBlockInfo({ - blockNumber: 1, - blockHash: entries[0].topics[2], - indexRepeatedStorageChanges: 0, - numberOfLayer1Txs: 0, - priorityOperationsHash: keccak256(""), - l2LogsTreeRoot: 0, - timestamp: currentTimestamp, - commitment: entries[0].topics[3] - }); - } - - function test_RevertWhen_ProvingWithWrongPreviousBlockData() public { - IExecutor.StoredBlockInfo memory wrongPreviousStoredBlockInfo = genesisStoredBlockInfo; - wrongPreviousStoredBlockInfo.blockNumber = 10; // Correct is 0 - - IExecutor.StoredBlockInfo[] memory storedBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - storedBlockInfoArray[0] = newStoredBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("t1")); - executor.proveBlocks(wrongPreviousStoredBlockInfo, storedBlockInfoArray, proofInput); - } - - function test_RevertWhen_ProvingWithWrongCommittedBlock() public { - IExecutor.StoredBlockInfo memory wrongNewStoredBlockInfo = newStoredBlockInfo; - wrongNewStoredBlockInfo.blockNumber = 10; // Correct is 1 - - IExecutor.StoredBlockInfo[] memory storedBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - storedBlockInfoArray[0] = wrongNewStoredBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("o1")); - executor.proveBlocks(genesisStoredBlockInfo, storedBlockInfoArray, proofInput); - } - - function test_RevertWhen_ProvingRevertedBlockWithoutCommittingAgain() public { - vm.prank(validator); - executor.revertBlocks(0); - - IExecutor.StoredBlockInfo[] memory storedBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - storedBlockInfoArray[0] = newStoredBlockInfo; - - vm.prank(validator); - - vm.expectRevert(bytes.concat("q")); - executor.proveBlocks(genesisStoredBlockInfo, storedBlockInfoArray, proofInput); - } - - function test_SuccessfulProve() public { - IExecutor.StoredBlockInfo[] memory storedBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - storedBlockInfoArray[0] = newStoredBlockInfo; - - vm.prank(validator); - - executor.proveBlocks(genesisStoredBlockInfo, storedBlockInfoArray, proofInput); - - uint256 totalBlocksVerified = getters.getTotalBlocksVerified(); - assertEq(totalBlocksVerified, 1); - } -} diff --git a/ethereum/test/foundry/unit/concrete/Executor/Reverting.t.sol b/ethereum/test/foundry/unit/concrete/Executor/Reverting.t.sol deleted file mode 100644 index 44d8f7a86..000000000 --- a/ethereum/test/foundry/unit/concrete/Executor/Reverting.t.sol +++ /dev/null @@ -1,75 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.17; - -import {Vm} from "forge-std/Test.sol"; -import {ExecutorTest} from "./_Executor_Shared.t.sol"; -import {Utils} from "../Utils/Utils.sol"; -import {COMMIT_TIMESTAMP_NOT_OLDER} from "../../../../../cache/solpp-generated-contracts/zksync/Config.sol"; -import {IExecutor} from "../../../../../cache/solpp-generated-contracts/zksync/interfaces/IExecutor.sol"; - -contract RevertingTest is ExecutorTest { - function setUp() public { - vm.warp(COMMIT_TIMESTAMP_NOT_OLDER + 1); - currentTimestamp = block.timestamp; - - bytes memory correctL2Logs = abi.encodePacked( - bytes4(0x00000001), - bytes4(0x00000000), - L2_SYSTEM_CONTEXT_ADDRESS, - Utils.packBatchTimestampAndBlockTimestamp(currentTimestamp, currentTimestamp), - bytes32("") - ); - - newCommitBlockInfo.timestamp = uint64(currentTimestamp); - newCommitBlockInfo.l2Logs = correctL2Logs; - - IExecutor.CommitBlockInfo[] memory commitBlockInfoArray = new IExecutor.CommitBlockInfo[](1); - commitBlockInfoArray[0] = newCommitBlockInfo; - - vm.prank(validator); - vm.recordLogs(); - executor.commitBlocks(genesisStoredBlockInfo, commitBlockInfoArray); - Vm.Log[] memory entries = vm.getRecordedLogs(); - - newStoredBlockInfo = IExecutor.StoredBlockInfo({ - blockNumber: 1, - blockHash: entries[0].topics[2], - indexRepeatedStorageChanges: 0, - numberOfLayer1Txs: 0, - priorityOperationsHash: keccak256(""), - l2LogsTreeRoot: 0, - timestamp: currentTimestamp, - commitment: entries[0].topics[3] - }); - - IExecutor.StoredBlockInfo[] memory storedBlockInfoArray = new IExecutor.StoredBlockInfo[](1); - storedBlockInfoArray[0] = newStoredBlockInfo; - - vm.prank(validator); - - executor.proveBlocks(genesisStoredBlockInfo, storedBlockInfoArray, proofInput); - } - - function test_RevertWhen_RevertingMoreBlocksThanAlreadyCommitted() public { - vm.prank(validator); - vm.expectRevert(bytes.concat("v1")); - executor.revertBlocks(10); - } - - function test_SuccessfulRevert() public { - uint256 totalBlocksCommittedBefore = getters.getTotalBlocksCommitted(); - assertEq(totalBlocksCommittedBefore, 1, "totalBlocksCommittedBefore"); - - uint256 totalBlocksVerifiedBefore = getters.getTotalBlocksVerified(); - assertEq(totalBlocksVerifiedBefore, 1, "totalBlocksVerifiedBefore"); - - vm.prank(validator); - executor.revertBlocks(0); - - uint256 totalBlocksCommitted = getters.getTotalBlocksCommitted(); - assertEq(totalBlocksCommitted, 0, "totalBlocksCommitted"); - - uint256 totalBlocksVerified = getters.getTotalBlocksVerified(); - assertEq(totalBlocksVerified, 0, "totalBlocksVerified"); - } -} diff --git a/ethereum/test/foundry/unit/concrete/Executor/_Executor_Shared.t.sol b/ethereum/test/foundry/unit/concrete/Executor/_Executor_Shared.t.sol deleted file mode 100644 index 3a5d58bff..000000000 --- a/ethereum/test/foundry/unit/concrete/Executor/_Executor_Shared.t.sol +++ /dev/null @@ -1,220 +0,0 @@ -// SPDX-License-Identifier: MIT - -pragma solidity ^0.8.17; - -import {Test} from "forge-std/Test.sol"; -import {Utils} from "../Utils/Utils.sol"; -import {AllowList} from "../../../../../cache/solpp-generated-contracts/common/AllowList.sol"; -import {IAllowList} from "../../../../../cache/solpp-generated-contracts/common/interfaces/IAllowList.sol"; -import {COMMIT_TIMESTAMP_NOT_OLDER} from "../../../../../cache/solpp-generated-contracts/zksync/Config.sol"; -import {DiamondInit} from "../../../../../cache/solpp-generated-contracts/zksync/DiamondInit.sol"; -import {DiamondProxy} from "../../../../../cache/solpp-generated-contracts/zksync/DiamondProxy.sol"; -import {VerifierParams} from "../../../../../cache/solpp-generated-contracts/zksync/Storage.sol"; -import {ExecutorFacet} from "../../../../../cache/solpp-generated-contracts/zksync/facets/Executor.sol"; -import {GettersFacet} from "../../../../../cache/solpp-generated-contracts/zksync/facets/Getters.sol"; -import {GovernanceFacet} from "../../../../../cache/solpp-generated-contracts/zksync/facets/Governance.sol"; -import {MailboxFacet} from "../../../../../cache/solpp-generated-contracts/zksync/facets/Mailbox.sol"; -import {IExecutor} from "../../../../../cache/solpp-generated-contracts/zksync/interfaces/IExecutor.sol"; -import {Diamond} from "../../../../../cache/solpp-generated-contracts/zksync/libraries/Diamond.sol"; - -contract ExecutorTest is Test { - address internal constant L2_SYSTEM_CONTEXT_ADDRESS = 0x000000000000000000000000000000000000800B; - address internal constant L2_KNOWN_CODE_STORAGE_ADDRESS = 0x0000000000000000000000000000000000008004; - address internal constant L2_TO_L1_MESSENGER = 0x0000000000000000000000000000000000008008; - - address internal owner; - address internal validator; - address internal randomSigner; - AllowList internal allowList; - GovernanceFacet internal governance; - ExecutorFacet internal executor; - GettersFacet internal getters; - MailboxFacet internal mailbox; - bytes32 internal newCommittedBlockBlockHash; - bytes32 internal newCommittedBlockCommitment; - uint256 internal currentTimestamp; - IExecutor.CommitBlockInfo internal newCommitBlockInfo; - IExecutor.StoredBlockInfo internal newStoredBlockInfo; - - IExecutor.StoredBlockInfo internal genesisStoredBlockInfo; - IExecutor.ProofInput internal proofInput; - - function getGovernanceSelectors() private view returns (bytes4[] memory) { - bytes4[] memory selectors = new bytes4[](5); - selectors[0] = governance.setPendingGovernor.selector; - selectors[1] = governance.acceptGovernor.selector; - selectors[2] = governance.setValidator.selector; - selectors[3] = governance.setPorterAvailability.selector; - selectors[4] = governance.setPriorityTxMaxGasLimit.selector; - return selectors; - } - - function getExecutorSelectors() private view returns (bytes4[] memory) { - bytes4[] memory selectors = new bytes4[](4); - selectors[0] = executor.commitBlocks.selector; - selectors[1] = executor.proveBlocks.selector; - selectors[2] = executor.executeBlocks.selector; - selectors[3] = executor.revertBlocks.selector; - return selectors; - } - - function getGettersSelectors() public view returns (bytes4[] memory) { - bytes4[] memory selectors = new bytes4[](32); - selectors[0] = getters.getVerifier.selector; - selectors[1] = getters.getGovernor.selector; - selectors[2] = getters.getPendingGovernor.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.storedBlockHash.selector; - selectors[13] = getters.getL2BootloaderBytecodeHash.selector; - selectors[14] = getters.getL2DefaultAccountBytecodeHash.selector; - selectors[15] = getters.getVerifierParams.selector; - selectors[16] = getters.isDiamondStorageFrozen.selector; - selectors[17] = getters.getSecurityCouncil.selector; - selectors[18] = getters.getUpgradeProposalState.selector; - selectors[19] = getters.getProposedUpgradeHash.selector; - selectors[20] = getters.getProposedUpgradeTimestamp.selector; - selectors[21] = getters.getCurrentProposalId.selector; - selectors[22] = getters.isApprovedBySecurityCouncil.selector; - selectors[23] = getters.getPriorityTxMaxGasLimit.selector; - selectors[24] = getters.getAllowList.selector; - selectors[25] = getters.isEthWithdrawalFinalized.selector; - selectors[26] = getters.facets.selector; - selectors[27] = getters.facetFunctionSelectors.selector; - selectors[28] = getters.facetAddresses.selector; - selectors[29] = getters.facetAddress.selector; - selectors[30] = getters.isFunctionFreezable.selector; - selectors[31] = getters.isFacetFreezable.selector; - return selectors; - } - - function getMailboxSelectors() private view returns (bytes4[] memory) { - bytes4[] memory selectors = new bytes4[](6); - selectors[0] = mailbox.proveL2MessageInclusion.selector; - selectors[1] = mailbox.proveL2LogInclusion.selector; - selectors[2] = mailbox.proveL1ToL2TransactionStatus.selector; - selectors[3] = mailbox.finalizeEthWithdrawal.selector; - selectors[4] = mailbox.requestL2Transaction.selector; - selectors[5] = mailbox.l2TransactionBaseCost.selector; - return selectors; - } - - constructor() { - owner = makeAddr("owner"); - validator = makeAddr("validator"); - randomSigner = makeAddr("randomSigner"); - - executor = new ExecutorFacet(); - governance = new GovernanceFacet(); - getters = new GettersFacet(); - mailbox = new MailboxFacet(); - - allowList = new AllowList(owner); - DiamondInit diamondInit = new DiamondInit(); - - bytes8 dummyHash = 0x1234567890123456; - address dummyAddress = makeAddr("dummyAddress"); - bytes memory diamondInitData = abi.encodeWithSelector( - diamondInit.initialize.selector, - dummyAddress, //verifier - owner, - 0, - 0, - 0, - allowList, - VerifierParams({recursionNodeLevelVkHash: 0, recursionLeafLevelVkHash: 0, recursionCircuitsSetVksHash: 0}), - false, - dummyHash, - dummyHash, - 100000000000 - ); - - Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](4); - facetCuts[0] = Diamond.FacetCut({ - facet: address(governance), - action: Diamond.Action.Add, - isFreezable: true, - selectors: getGovernanceSelectors() - }); - facetCuts[1] = Diamond.FacetCut({ - facet: address(executor), - action: Diamond.Action.Add, - isFreezable: true, - selectors: getExecutorSelectors() - }); - facetCuts[2] = Diamond.FacetCut({ - facet: address(getters), - action: Diamond.Action.Add, - isFreezable: false, - selectors: getGettersSelectors() - }); - facetCuts[3] = Diamond.FacetCut({ - facet: address(mailbox), - action: Diamond.Action.Add, - isFreezable: true, - selectors: getMailboxSelectors() - }); - - Diamond.DiamondCutData memory diamondCutData = Diamond.DiamondCutData({ - facetCuts: facetCuts, - initAddress: address(diamondInit), - initCalldata: diamondInitData - }); - - uint256 chainId = block.chainid; - DiamondProxy diamondProxy = new DiamondProxy(chainId, diamondCutData); - - vm.prank(owner); - allowList.setAccessMode(address(diamondProxy), IAllowList.AccessMode.Public); - - executor = ExecutorFacet(address(diamondProxy)); - getters = GettersFacet(address(diamondProxy)); - mailbox = MailboxFacet(address(diamondProxy)); - governance = GovernanceFacet(address(diamondProxy)); - - vm.prank(owner); - governance.setValidator(validator, true); - - uint256[] memory recursiveAggregationInput; - uint256[] memory serializedProof; - proofInput = IExecutor.ProofInput(recursiveAggregationInput, serializedProof); - - genesisStoredBlockInfo = IExecutor.StoredBlockInfo({ - blockNumber: 0, - blockHash: 0, - indexRepeatedStorageChanges: 0, - numberOfLayer1Txs: 0, - priorityOperationsHash: keccak256(""), - l2LogsTreeRoot: 0, - timestamp: 0, - commitment: 0 - }); - - // foundry's default value is 1 for the block's timestamp, it is expected - // that block.timestamp > COMMIT_TIMESTAMP_NOT_OLDER + 1 - vm.warp(COMMIT_TIMESTAMP_NOT_OLDER + 1 + 1); - currentTimestamp = block.timestamp; - - newCommitBlockInfo = IExecutor.CommitBlockInfo({ - blockNumber: 1, - timestamp: uint64(currentTimestamp), - indexRepeatedStorageChanges: 0, - newStateRoot: Utils.randomBytes32("newStateRoot"), - numberOfLayer1Txs: 0, - l2LogsTreeRoot: 0, - priorityOperationsHash: keccak256(""), - initialStorageChanges: abi.encodePacked(uint256(0x00000000)), - repeatedStorageChanges: bytes(""), - l2Logs: bytes(""), - l2ArbitraryLengthMessages: new bytes[](0), - factoryDeps: new bytes[](0) - }); - } -} From 222bd2f3cd43d96453325b79010a6450143e8e80 Mon Sep 17 00:00:00 2001 From: Bence Haromi Date: Wed, 4 Oct 2023 15:35:13 +0100 Subject: [PATCH 02/10] test(BridgeheadMailbox): IsEthWithdrawalFinalized tests --- .../IsEthWithdrawalFinalized.t.sol | 52 +++++++++++++++++++ .../_BridgeheadMailbox_Shared.t.sol | 37 +++++++++++++ .../Bridgehead/Registry/NewChain.t.sol | 10 ---- .../Bridgehead/_Bridgehead_Shared.t.sol | 10 ++++ 4 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/IsEthWithdrawalFinalized.t.sol create mode 100644 ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/_BridgeheadMailbox_Shared.t.sol diff --git a/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/IsEthWithdrawalFinalized.t.sol b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/IsEthWithdrawalFinalized.t.sol new file mode 100644 index 000000000..04c7667ff --- /dev/null +++ b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/IsEthWithdrawalFinalized.t.sol @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.17; + +/* solhint-disable max-line-length */ + +import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol"; +import {IChainGetters} from "../../../../../../cache/solpp-generated-contracts/bridgehead/chain-interfaces/IChainGetters.sol"; + +/* solhint-enable max-line-length */ + +contract IsEthWithdrawalFinalizedTest is BridgeheadMailboxTest { + uint256 internal l2MessageIndex; + uint256 internal l2TxNumberInBlock; + + function setUp() public { + l2MessageIndex = 123456789; + l2TxNumberInBlock = 23456; + } + + function test_WhenChainContractReturnsTrue() public { + vm.mockCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector(IChainGetters.isEthWithdrawalFinalized.selector, l2MessageIndex, l2TxNumberInBlock), + abi.encode(true) + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector(IChainGetters.isEthWithdrawalFinalized.selector, l2MessageIndex, l2TxNumberInBlock) + ); + + bool res = bridgehead.isEthWithdrawalFinalized(chainId, l2MessageIndex, l2TxNumberInBlock); + assertEq(res, true, "ETH withdrawal should be finalized"); + } + + function test_WhenChainContractReturnsFalse() public { + vm.mockCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector(IChainGetters.isEthWithdrawalFinalized.selector, l2MessageIndex, l2TxNumberInBlock), + abi.encode(false) + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector(IChainGetters.isEthWithdrawalFinalized.selector, l2MessageIndex, l2TxNumberInBlock) + ); + + bool res = bridgehead.isEthWithdrawalFinalized(chainId, l2MessageIndex, l2TxNumberInBlock); + assertEq(res, false, "ETH withdrawal should not be finalized"); + } +} diff --git a/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/_BridgeheadMailbox_Shared.t.sol b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/_BridgeheadMailbox_Shared.t.sol new file mode 100644 index 000000000..5d446d9e6 --- /dev/null +++ b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/_BridgeheadMailbox_Shared.t.sol @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.17; + +/* solhint-disable max-line-length */ + +import {BridgeheadTest} from "../_Bridgehead_Shared.t.sol"; +import {IAllowList} from "../../../../../../cache/solpp-generated-contracts/common/interfaces/IAllowList.sol"; +import {IBridgeheadChain} from "../../../../../../cache/solpp-generated-contracts/bridgehead/chain-interfaces/IBridgeheadChain.sol"; +import {IProofForBridgehead} from "../../../../../../cache/solpp-generated-contracts/proof-system/proof-system-interfaces/IProofForBridgehead.sol"; + +/* solhint-enable max-line-length */ + +contract BridgeheadMailboxTest is BridgeheadTest { + uint256 internal chainId; + address internal chainProofSystem; + address internal chainGovernor; + IAllowList internal chainAllowList; + + constructor() { + chainId = 987654321; + chainProofSystem = makeAddr("chainProofSystem"); + chainGovernor = makeAddr("chainGovernor"); + chainAllowList = IAllowList(makeAddr("chainAllowList")); + + vm.mockCall( + bridgehead.getChainImplementation(), + abi.encodeWithSelector(IBridgeheadChain.initialize.selector), + "" + ); + vm.mockCall(chainProofSystem, abi.encodeWithSelector(IProofForBridgehead.newChain.selector), ""); + + vm.startPrank(GOVERNOR); + bridgehead.newProofSystem(chainProofSystem); + bridgehead.newChain(chainId, chainProofSystem, chainGovernor, chainAllowList, getDiamondCutData()); + } +} diff --git a/ethereum/test/foundry/unit/concrete/Bridgehead/Registry/NewChain.t.sol b/ethereum/test/foundry/unit/concrete/Bridgehead/Registry/NewChain.t.sol index 919dbc16d..f649bbb5a 100644 --- a/ethereum/test/foundry/unit/concrete/Bridgehead/Registry/NewChain.t.sol +++ b/ethereum/test/foundry/unit/concrete/Bridgehead/Registry/NewChain.t.sol @@ -7,7 +7,6 @@ pragma solidity ^0.8.17; import {RegistryTest} from "./_Registry_Shared.t.sol"; import {IAllowList} from "../../../../../../cache/solpp-generated-contracts/common/interfaces/IAllowList.sol"; import {AllowList} from "../../../../../../cache/solpp-generated-contracts/common/AllowList.sol"; -import {Diamond} from "../../../../../../cache/solpp-generated-contracts/common/libraries/Diamond.sol"; import {IProofForBridgehead} from "../../../../../../cache/solpp-generated-contracts/proof-system/proof-system-interfaces/IProofForBridgehead.sol"; import {TransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; import {IBridgeheadChain} from "../../../../../../cache/solpp-generated-contracts/bridgehead/chain-interfaces/IBridgeheadChain.sol"; @@ -31,15 +30,6 @@ contract NewChainTest is RegistryTest { bridgehead.newProofSystem(proofSystemAddress); } - function getDiamondCutData() internal pure returns (Diamond.DiamondCutData memory) { - return - Diamond.DiamondCutData({ - facetCuts: new Diamond.FacetCut[](0), - initAddress: address(0x3030303030303030), - initCalldata: bytes("") - }); - } - function getChainContractAddress() internal returns (address chainContractAddress) { vm.mockCall( bridgehead.getChainImplementation(), diff --git a/ethereum/test/foundry/unit/concrete/Bridgehead/_Bridgehead_Shared.t.sol b/ethereum/test/foundry/unit/concrete/Bridgehead/_Bridgehead_Shared.t.sol index f3ad14f39..8f9c507f2 100644 --- a/ethereum/test/foundry/unit/concrete/Bridgehead/_Bridgehead_Shared.t.sol +++ b/ethereum/test/foundry/unit/concrete/Bridgehead/_Bridgehead_Shared.t.sol @@ -5,6 +5,7 @@ pragma solidity ^0.8.17; import {Test} from "forge-std/Test.sol"; import {Bridgehead} from "../../../../../cache/solpp-generated-contracts/bridgehead/Bridgehead.sol"; import {IAllowList} from "../../../../../cache/solpp-generated-contracts/common/interfaces/IAllowList.sol"; +import {Diamond} from "../../../../../cache/solpp-generated-contracts/common/libraries/Diamond.sol"; contract BridgeheadTest is Test { Bridgehead internal bridgehead; @@ -21,4 +22,13 @@ contract BridgeheadTest is Test { bridgehead = new Bridgehead(); bridgehead.initialize(governor, chainImplementation, chainProxyAdmin, allowList, priorityTxMaxGasLimit); } + + function getDiamondCutData() internal pure returns (Diamond.DiamondCutData memory) { + return + Diamond.DiamondCutData({ + facetCuts: new Diamond.FacetCut[](0), + initAddress: address(0x3030303030303030), + initCalldata: bytes("") + }); + } } From 2f38f6b7282560484b28a993c13c98cd70bfc1f1 Mon Sep 17 00:00:00 2001 From: Bence Haromi Date: Wed, 4 Oct 2023 16:16:25 +0100 Subject: [PATCH 03/10] test(BridgeheadMailbox): ProveL2MessageInclusion tests --- .../ProveL2MessageInclusion.t.sol | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/ProveL2MessageInclusion.t.sol diff --git a/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/ProveL2MessageInclusion.t.sol b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/ProveL2MessageInclusion.t.sol new file mode 100644 index 000000000..657cac050 --- /dev/null +++ b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/ProveL2MessageInclusion.t.sol @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.17; + +/* solhint-disable max-line-length */ + +import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol"; +import {L2Message} from "../../../../../../cache/solpp-generated-contracts/common/Messaging.sol"; +import {IMailbox} from "../../../../../../cache/solpp-generated-contracts/bridgehead/chain-interfaces/IMailbox.sol"; + +/* solhint-enable max-line-length */ + +contract ProveL2MessageInclusionTest is BridgeheadMailboxTest { + uint256 internal blockNumber; + uint256 internal index; + L2Message internal message; + bytes32[] internal proof; + + function setUp() public { + blockNumber = 3456789; + index = 234567890; + proof = new bytes32[](1); + + uint16 txNumberInBlock = 12345; + address sender = makeAddr("sender"); + bytes memory data = "data"; + message = L2Message(txNumberInBlock, sender, data); + } + + function test_WhenChainContractReturnsTrue() public { + vm.mockCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector(IMailbox.proveL2MessageInclusion.selector, blockNumber, index, message, proof), + abi.encode(true) + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector(IMailbox.proveL2MessageInclusion.selector, blockNumber, index, message, proof) + ); + + bool res = bridgehead.proveL2MessageInclusion(chainId, blockNumber, index, message, proof); + assertEq(res, true, "L2 message should be included"); + } + + function test_WhenChainContractReturnsFalse() public { + vm.mockCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector(IMailbox.proveL2MessageInclusion.selector, blockNumber, index, message, proof), + abi.encode(false) + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector(IMailbox.proveL2MessageInclusion.selector, blockNumber, index, message, proof) + ); + + bool res = bridgehead.proveL2MessageInclusion(chainId, blockNumber, index, message, proof); + assertEq(res, false, "L2 message should not be included"); + } +} From ca245053ead12d1729afdd5649b41a5b90ea45bf Mon Sep 17 00:00:00 2001 From: Bence Haromi Date: Thu, 5 Oct 2023 10:25:34 +0100 Subject: [PATCH 04/10] test(BridgeheadMailbox): ProveL2LogInclusion tests --- .../ProveL2LogInclusion.t.sol | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/ProveL2LogInclusion.t.sol diff --git a/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/ProveL2LogInclusion.t.sol b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/ProveL2LogInclusion.t.sol new file mode 100644 index 000000000..a0c941dde --- /dev/null +++ b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/ProveL2LogInclusion.t.sol @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.17; + +/* solhint-disable max-line-length */ + +import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol"; +import {L2Log} from "../../../../../../cache/solpp-generated-contracts/common/Messaging.sol"; +import {IMailbox} from "../../../../../../cache/solpp-generated-contracts/bridgehead/chain-interfaces/IMailbox.sol"; + +/* solhint-enable max-line-length */ + +contract ProveL2LogInclusionTest is BridgeheadMailboxTest { + uint256 internal blockNumber; + uint256 internal index; + L2Log internal l2Log; + bytes32[] internal proof; + + function setUp() public { + blockNumber = 3456789; + index = 234567890; + proof = new bytes32[](1); + + uint8 l2ShardId = 0; + bool isService = false; + uint16 txNumberInBlock = 12345; + address sender = makeAddr("sender"); + bytes32 key = "key"; + bytes32 value = "value"; + l2Log = L2Log(l2ShardId, isService, txNumberInBlock, sender, key, value); + } + + function test_WhenChainContractReturnsTrue() public { + vm.mockCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector(IMailbox.proveL2LogInclusion.selector, blockNumber, index, l2Log, proof), + abi.encode(true) + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector(IMailbox.proveL2LogInclusion.selector, blockNumber, index, l2Log, proof) + ); + + bool res = bridgehead.proveL2LogInclusion(chainId, blockNumber, index, l2Log, proof); + assertEq(res, true, "L2 log should be included"); + } + + function test_WhenChainContractReturnsFalse() public { + vm.mockCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector(IMailbox.proveL2LogInclusion.selector, blockNumber, index, l2Log, proof), + abi.encode(false) + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector(IMailbox.proveL2LogInclusion.selector, blockNumber, index, l2Log, proof) + ); + + bool res = bridgehead.proveL2LogInclusion(chainId, blockNumber, index, l2Log, proof); + assertEq(res, false, "L2 log should not be included"); + } +} From f56bf8aa4869d7fe6232782594583406e4f7233a Mon Sep 17 00:00:00 2001 From: Bence Haromi Date: Thu, 5 Oct 2023 10:38:21 +0100 Subject: [PATCH 05/10] test(BridgeheadMailbox): ProveL1ToL2TransactionStatus tests --- .../ProveL1ToL2TransactionStatus.t.sol | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/ProveL1ToL2TransactionStatus.t.sol diff --git a/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/ProveL1ToL2TransactionStatus.t.sol b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/ProveL1ToL2TransactionStatus.t.sol new file mode 100644 index 000000000..a3884c9d1 --- /dev/null +++ b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/ProveL1ToL2TransactionStatus.t.sol @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.17; + +/* solhint-disable max-line-length */ + +import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol"; +import {TxStatus} from "../../../../../../cache/solpp-generated-contracts/common/Messaging.sol"; +import {IMailbox} from "../../../../../../cache/solpp-generated-contracts/bridgehead/chain-interfaces/IMailbox.sol"; + +/* solhint-enable max-line-length */ + +contract ProveL1ToL2TransactionStatusTest is BridgeheadMailboxTest { + uint256 internal blockNumber; + bytes32 internal l2TxHash; + uint256 internal l2BlockNumber; + uint256 internal l2MessageIndex; + uint16 internal l2TxNumberInBlock; + bytes32[] internal merkleProof; + TxStatus internal status; + + function setUp() public { + l2TxHash = bytes32(uint256(123456789)); + l2BlockNumber = 3456789; + l2MessageIndex = 234567890; + l2TxNumberInBlock = 12345; + merkleProof = new bytes32[](1); + status = TxStatus.Success; + } + + function test_WhenChainContractReturnsTrue() public { + vm.mockCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector( + IMailbox.proveL1ToL2TransactionStatus.selector, + l2TxHash, + l2BlockNumber, + l2MessageIndex, + l2TxNumberInBlock, + merkleProof, + status + ), + abi.encode(true) + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector( + IMailbox.proveL1ToL2TransactionStatus.selector, + l2TxHash, + l2BlockNumber, + l2MessageIndex, + l2TxNumberInBlock, + merkleProof, + status + ) + ); + + bool res = bridgehead.proveL1ToL2TransactionStatus( + chainId, + l2TxHash, + l2BlockNumber, + l2MessageIndex, + l2TxNumberInBlock, + merkleProof, + status + ); + assertEq(res, true, "L1 to L2 transaction status should be proven"); + } + + function test_WhenChainContractReturnsFalse() public { + vm.mockCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector( + IMailbox.proveL1ToL2TransactionStatus.selector, + l2TxHash, + l2BlockNumber, + l2MessageIndex, + l2TxNumberInBlock, + merkleProof, + status + ), + abi.encode(false) + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector( + IMailbox.proveL1ToL2TransactionStatus.selector, + l2TxHash, + l2BlockNumber, + l2MessageIndex, + l2TxNumberInBlock, + merkleProof, + status + ) + ); + + bool res = bridgehead.proveL1ToL2TransactionStatus( + chainId, + l2TxHash, + l2BlockNumber, + l2MessageIndex, + l2TxNumberInBlock, + merkleProof, + status + ); + assertEq(res, false, "L1 to L2 transaction status should not be proven"); + } +} From 3c800d5cf8620923fedd5b7d9cb6b6e12988ec04 Mon Sep 17 00:00:00 2001 From: Bence Haromi Date: Thu, 5 Oct 2023 11:07:58 +0100 Subject: [PATCH 06/10] test(BridgeheadMailbox): RequestL2Transaction tests --- .../RequestL2Transaction.t.sol | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/RequestL2Transaction.t.sol diff --git a/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/RequestL2Transaction.t.sol b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/RequestL2Transaction.t.sol new file mode 100644 index 000000000..ea135d879 --- /dev/null +++ b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/RequestL2Transaction.t.sol @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.17; + +/* solhint-disable max-line-length */ + +import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol"; +import {IMailbox} from "../../../../../../cache/solpp-generated-contracts/bridgehead/chain-interfaces/IMailbox.sol"; + +/* solhint-enable max-line-length */ + +contract RequestL2TransactionTest is BridgeheadMailboxTest { + address internal contractL2; + uint256 internal l2Value; + bytes internal calldataBytes; + uint256 internal l2GasLimit; + uint256 internal l2GasPerPubdataByteLimit; + bytes[] internal factoryDeps; + address internal refundRecipient; + address internal msgSender; + uint256 internal msgValue; + + function setUp() public { + contractL2 = makeAddr("contractL2"); + l2Value = 123456789; + calldataBytes = "calldataBytes"; + l2GasLimit = 234567890; + l2GasPerPubdataByteLimit = 345678901; + factoryDeps = new bytes[](1); + refundRecipient = makeAddr("refundRecipient"); + msgSender = makeAddr("msgSender"); + vm.deal(msgSender, 100 ether); + msgValue = 456789012; + } + + function test_RevertWhen_InternalCallReverts() public { + bytes memory revertMessage = "random revert"; + + vm.mockCallRevert( + bridgehead.getChainContract(chainId), + msgValue, + abi.encodeWithSelector( + IMailbox.requestL2TransactionBridgehead.selector, + msgSender, + contractL2, + l2Value, + calldataBytes, + l2GasLimit, + l2GasPerPubdataByteLimit, + factoryDeps, + refundRecipient + ), + revertMessage + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + msgValue, + abi.encodeWithSelector( + IMailbox.requestL2TransactionBridgehead.selector, + msgSender, + contractL2, + l2Value, + calldataBytes, + l2GasLimit, + l2GasPerPubdataByteLimit, + factoryDeps, + refundRecipient + ) + ); + + vm.expectRevert(revertMessage); + vm.startPrank(msgSender); + bridgehead.requestL2Transaction{value: msgValue}( + chainId, + contractL2, + l2Value, + calldataBytes, + l2GasLimit, + l2GasPerPubdataByteLimit, + factoryDeps, + refundRecipient + ); + } + + function test_ShouldReturnReceivedCanonicalTxHash() public { + bytes32 expectedCanonicalTxHash = bytes32(uint256(123456789)); + + vm.mockCall( + bridgehead.getChainContract(chainId), + msgValue, + abi.encodeWithSelector( + IMailbox.requestL2TransactionBridgehead.selector, + msgSender, + contractL2, + l2Value, + calldataBytes, + l2GasLimit, + l2GasPerPubdataByteLimit, + factoryDeps, + refundRecipient + ), + abi.encode(expectedCanonicalTxHash) + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + msgValue, + abi.encodeWithSelector( + IMailbox.requestL2TransactionBridgehead.selector, + msgSender, + contractL2, + l2Value, + calldataBytes, + l2GasLimit, + l2GasPerPubdataByteLimit, + factoryDeps, + refundRecipient + ) + ); + + vm.startPrank(msgSender); + bytes32 canonicalTxHash = bridgehead.requestL2Transaction{value: msgValue}( + chainId, + contractL2, + l2Value, + calldataBytes, + l2GasLimit, + l2GasPerPubdataByteLimit, + factoryDeps, + refundRecipient + ); + assertEq(canonicalTxHash, expectedCanonicalTxHash, "Canonical transaction hash should be returned"); + } +} From d7fb613a605b12d21e41696adf5466d0346f7c58 Mon Sep 17 00:00:00 2001 From: Bence Haromi Date: Thu, 5 Oct 2023 11:15:42 +0100 Subject: [PATCH 07/10] test(BridgeheadMailbox): FinalizeEthWithdrawal tests --- .../FinalizeEthWithdrawal.t.sol | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/FinalizeEthWithdrawal.t.sol diff --git a/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/FinalizeEthWithdrawal.t.sol b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/FinalizeEthWithdrawal.t.sol new file mode 100644 index 000000000..f5a55bacc --- /dev/null +++ b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/FinalizeEthWithdrawal.t.sol @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.17; + +/* solhint-disable max-line-length */ + +import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol"; +import {IMailbox} from "../../../../../../cache/solpp-generated-contracts/bridgehead/chain-interfaces/IMailbox.sol"; + +/* solhint-enable max-line-length */ + +contract FinalizeEthWithdrawalTest is BridgeheadMailboxTest { + uint256 internal l2BlockNumber; + uint256 internal l2MessageIndex; + uint16 internal l2TxNumberInBlock; + bytes internal message; + bytes32[] internal merkleProof; + address internal msgSender; + + function setUp() public { + l2BlockNumber = 3456789; + l2MessageIndex = 234567890; + l2TxNumberInBlock = 12345; + message = "message"; + merkleProof = new bytes32[](1); + msgSender = makeAddr("msgSender"); + } + + function test_RevertWhen_InternalCallReverts() public { + bytes memory revertMessage = "random revert"; + + vm.mockCallRevert( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector( + IMailbox.finalizeEthWithdrawalBridgehead.selector, + msgSender, + l2BlockNumber, + l2MessageIndex, + l2TxNumberInBlock, + message, + merkleProof + ), + revertMessage + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector( + IMailbox.finalizeEthWithdrawalBridgehead.selector, + msgSender, + l2BlockNumber, + l2MessageIndex, + l2TxNumberInBlock, + message, + merkleProof + ) + ); + + vm.expectRevert(revertMessage); + vm.startPrank(msgSender); + bridgehead.finalizeEthWithdrawal( + chainId, + l2BlockNumber, + l2MessageIndex, + l2TxNumberInBlock, + message, + merkleProof + ); + } + + function test_ShouldReturnReceivedCanonicalTxHash() public { + vm.mockCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector( + IMailbox.finalizeEthWithdrawalBridgehead.selector, + msgSender, + l2BlockNumber, + l2MessageIndex, + l2TxNumberInBlock, + message, + merkleProof + ), + "" + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector( + IMailbox.finalizeEthWithdrawalBridgehead.selector, + msgSender, + l2BlockNumber, + l2MessageIndex, + l2TxNumberInBlock, + message, + merkleProof + ) + ); + + vm.startPrank(msgSender); + bridgehead.finalizeEthWithdrawal( + chainId, + l2BlockNumber, + l2MessageIndex, + l2TxNumberInBlock, + message, + merkleProof + ); + } +} From f7efdccdf763597a8ca1be9c8ec4540d4413b167 Mon Sep 17 00:00:00 2001 From: Bence Haromi Date: Thu, 5 Oct 2023 11:38:43 +0100 Subject: [PATCH 08/10] test(BridgeheadMailbox): Deposit tests --- .../BridgeheadMailbox/Deposit.t.sol | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/Deposit.t.sol diff --git a/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/Deposit.t.sol b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/Deposit.t.sol new file mode 100644 index 000000000..19b5d7042 --- /dev/null +++ b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/Deposit.t.sol @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.17; + +/* solhint-disable max-line-length */ + +import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol"; + +/* solhint-enable max-line-length */ + +contract DepositTest is BridgeheadMailboxTest { + function test_RevertWhen_CalledByNonChainContract() public { + address nonChainContract = makeAddr("nonChainContract"); + + vm.expectRevert(abi.encodePacked("12c")); + vm.startPrank(nonChainContract); + bridgehead.deposit(chainId); + } + + function test_SuccessfullIfCalledByChainContract() public { + address chainContract = bridgehead.getChainContract(chainId); + + vm.startPrank(chainContract); + bridgehead.deposit(chainId); + } +} From 4c9c1404cabbeb2c4080276f7f33ed3129f86870 Mon Sep 17 00:00:00 2001 From: Bence Haromi Date: Thu, 5 Oct 2023 11:45:54 +0100 Subject: [PATCH 09/10] test(BridgeheadMailbox): L2TransactionBaseCost tests --- .../L2TransactionBaseCost.t.sol | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/L2TransactionBaseCost.t.sol diff --git a/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/L2TransactionBaseCost.t.sol b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/L2TransactionBaseCost.t.sol new file mode 100644 index 000000000..3df0102e9 --- /dev/null +++ b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/L2TransactionBaseCost.t.sol @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.17; + +/* solhint-disable max-line-length */ + +import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol"; +import {IMailbox} from "../../../../../../cache/solpp-generated-contracts/bridgehead/chain-interfaces/IMailbox.sol"; + +/* solhint-enable max-line-length */ + +contract L2TransactionBaseCostTest is BridgeheadMailboxTest { + uint256 internal gasPrice; + uint256 internal l2GasLimit; + uint256 internal l2GasPerPubdataByteLimit; + + function setUp() public { + gasPrice = 123456789; + l2GasLimit = 234567890; + l2GasPerPubdataByteLimit = 345678901; + } + + function test_RevertWhen_InternalCallReverts() public { + bytes memory revertMessage = "random revert"; + + vm.mockCallRevert( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector( + IMailbox.l2TransactionBaseCost.selector, + gasPrice, + l2GasLimit, + l2GasPerPubdataByteLimit + ), + revertMessage + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector( + IMailbox.l2TransactionBaseCost.selector, + gasPrice, + l2GasLimit, + l2GasPerPubdataByteLimit + ) + ); + + vm.expectRevert(revertMessage); + bridgehead.l2TransactionBaseCost(chainId, gasPrice, l2GasLimit, l2GasPerPubdataByteLimit); + } + + function test_ShouldReturnReceivedCanonicalTxHash() public { + uint256 expectedBaseCost = 123456789; + + vm.mockCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector( + IMailbox.l2TransactionBaseCost.selector, + gasPrice, + l2GasLimit, + l2GasPerPubdataByteLimit + ), + abi.encode(expectedBaseCost) + ); + + vm.expectCall( + bridgehead.getChainContract(chainId), + abi.encodeWithSelector( + IMailbox.l2TransactionBaseCost.selector, + gasPrice, + l2GasLimit, + l2GasPerPubdataByteLimit + ) + ); + + uint256 baseCost = bridgehead.l2TransactionBaseCost(chainId, gasPrice, l2GasLimit, l2GasPerPubdataByteLimit); + assertEq(baseCost, expectedBaseCost); + } +} From 394809745a92b724831a99dd5ffe5af1a6ffc338 Mon Sep 17 00:00:00 2001 From: Bence Haromi Date: Thu, 5 Oct 2023 15:39:10 +0100 Subject: [PATCH 10/10] test(BridgeheadMailbox): WithdrawFunds tests --- .../BridgeheadMailbox/WithdrawFunds.t.sol | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/WithdrawFunds.t.sol diff --git a/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/WithdrawFunds.t.sol b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/WithdrawFunds.t.sol new file mode 100644 index 000000000..c564a9106 --- /dev/null +++ b/ethereum/test/foundry/unit/concrete/Bridgehead/BridgeheadMailbox/WithdrawFunds.t.sol @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.17; + +/* solhint-disable max-line-length */ + +import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol"; + +/* solhint-enable max-line-length */ + +contract WithdrawFundsTest is BridgeheadMailboxTest { + address internal to; + uint256 internal amount; + + function setUp() public { + to = makeAddr("to"); + amount = 123456789; + } + + function test_RevertWhen_CalledByNonChainContract() public { + address nonChainContract = makeAddr("nonChainContract"); + + vm.expectRevert(abi.encodePacked("12c")); + vm.startPrank(nonChainContract); + bridgehead.withdrawFunds(chainId, to, amount); + } + + function test_RevertWhen_NotEnoughBalance() public { + address chainContract = bridgehead.getChainContract(chainId); + + // setting the balance of the bridgehead to 0 and trying to withdraw 1 ether + vm.deal(chainContract, 0 ether); + amount = 1 ether; + + vm.expectRevert(abi.encodePacked("pz")); + vm.startPrank(chainContract); + bridgehead.withdrawFunds(chainId, to, amount); + } + + function test_SuccessfulWithdraw() public { + address chainContract = bridgehead.getChainContract(chainId); + + vm.deal(address(bridgehead), 100 ether); + amount = 10 ether; + + vm.startPrank(chainContract); + bridgehead.withdrawFunds(chainId, to, amount); + } +}