From 701ac852cb22726a5f484d02be6b2f2273423c48 Mon Sep 17 00:00:00 2001 From: Stanislav Breadless Date: Tue, 9 Jan 2024 23:57:32 +0100 Subject: [PATCH] fix sc tests --- .../contracts/test-contracts/KeccakTest.sol | 27 ++++++++++--------- system-contracts/package.json | 2 +- system-contracts/scripts/compile-yul.ts | 1 + system-contracts/test/Keccak256.spec.ts | 25 +++++++++-------- system-contracts/test/shared/constants.ts | 1 + 5 files changed, 30 insertions(+), 26 deletions(-) diff --git a/system-contracts/contracts/test-contracts/KeccakTest.sol b/system-contracts/contracts/test-contracts/KeccakTest.sol index 75a58ac70..813441d8d 100644 --- a/system-contracts/contracts/test-contracts/KeccakTest.sol +++ b/system-contracts/contracts/test-contracts/KeccakTest.sol @@ -7,6 +7,11 @@ import "../libraries/SystemContractsCaller.sol"; import "../Constants.sol"; import "../libraries/EfficientCall.sol"; +// In this test it is important to actuall change the real Keccak256's contract's bytecode, +// which requires changes in the real AccountCodeStorage contract +address constant REAL_DEPLOYER_SYSTEM_CONTRACT = address(0x8006); +address constant REAL_FORCE_DEPLOYER_ADDRESS = address(0x8007); + contract KeccakTest { bytes32 constant EMPTY_STRING_KECCAK = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470; @@ -82,9 +87,9 @@ contract KeccakTest { // Firstly, we reset keccak256 bytecode to be some random bytecode EfficientCall.mimicCall( gasleft(), - address(DEPLOYER_SYSTEM_CONTRACT), + address(REAL_DEPLOYER_SYSTEM_CONTRACT), eraseCallData, - FORCE_DEPLOYER, + REAL_FORCE_DEPLOYER_ADDRESS, false, false ); @@ -93,15 +98,13 @@ contract KeccakTest { try this.callKeccak(msg.data[0:0]) returns (bytes32) { revert("The keccak should not work anymore"); } catch {} - // bytes32 incorrectHash = this.callKeccak(msg.data[0:0]); - // require(incorrectHash == bytes32(0), "The keccak should now return always incorrect values"); // Upgrading it back to the correct version: EfficientCall.mimicCall( gasleft(), - address(DEPLOYER_SYSTEM_CONTRACT), + address(REAL_DEPLOYER_SYSTEM_CONTRACT), upgradeCalldata, - FORCE_DEPLOYER, + REAL_FORCE_DEPLOYER_ADDRESS, false, false ); @@ -114,9 +117,9 @@ contract KeccakTest { function keccakPerformUpgrade(bytes calldata upgradeCalldata) external { EfficientCall.mimicCall( gasleft(), - address(DEPLOYER_SYSTEM_CONTRACT), + address(REAL_DEPLOYER_SYSTEM_CONTRACT), upgradeCalldata, - FORCE_DEPLOYER, + REAL_FORCE_DEPLOYER_ADDRESS, false, false ); @@ -137,9 +140,9 @@ contract KeccakTest { // Firstly, we upgrade keccak256 bytecode to the correct version. EfficientCall.mimicCall( gasleft(), - address(DEPLOYER_SYSTEM_CONTRACT), + address(REAL_DEPLOYER_SYSTEM_CONTRACT), upgradeCalldata, - FORCE_DEPLOYER, + REAL_FORCE_DEPLOYER_ADDRESS, false, false ); @@ -158,9 +161,9 @@ contract KeccakTest { // Upgrading it back to the original version: EfficientCall.mimicCall( gasleft(), - address(DEPLOYER_SYSTEM_CONTRACT), + address(REAL_DEPLOYER_SYSTEM_CONTRACT), resetCalldata, - FORCE_DEPLOYER, + REAL_FORCE_DEPLOYER_ADDRESS, false, false ); diff --git a/system-contracts/package.json b/system-contracts/package.json index 1a7c39961..6867cdbec 100644 --- a/system-contracts/package.json +++ b/system-contracts/package.json @@ -60,7 +60,7 @@ "preprocess:bootloader": "rm -rf ./bootloader/build && yarn ts-node scripts/preprocess-bootloader.ts", "preprocess:system-contracts": "rm -rf ./contracts-preprocessed && ts-node scripts/preprocess-system-contracts.ts", "test": "yarn build:test-system-contracts && hardhat test --network zkSyncTestNode", - "test-node": "hardhat node-zksync --tag v0.0.1-alpha.boojum", + "test-node": "hardhat node-zksync --tag v0.0.1-vm1.4.1", "test:bootloader": "cd bootloader/test_infra && cargo run" } } diff --git a/system-contracts/scripts/compile-yul.ts b/system-contracts/scripts/compile-yul.ts index f237600f7..83ed95a8f 100644 --- a/system-contracts/scripts/compile-yul.ts +++ b/system-contracts/scripts/compile-yul.ts @@ -34,6 +34,7 @@ async function main() { program.command("compile-precompiles").action(async () => { await compileYulFolder("contracts-preprocessed"); await compileYulFolder("contracts-preprocessed/precompiles"); + await compileYulFolder("contracts-preprocessed/precompiles/test-contracts"); }); await program.parseAsync(process.argv); diff --git a/system-contracts/test/Keccak256.spec.ts b/system-contracts/test/Keccak256.spec.ts index 8866957cd..97e43afa1 100644 --- a/system-contracts/test/Keccak256.spec.ts +++ b/system-contracts/test/Keccak256.spec.ts @@ -1,7 +1,7 @@ import { CONTRACT_DEPLOYER_ADDRESS, hashBytecode } from "zksync-web3/build/src/utils"; -import type { KeccakTest } from "../typechain-types"; -import { KeccakTest__factory } from "../typechain-types"; -import { KECCAK256_CONTRACT_ADDRESS } from "./shared/constants"; +import type { KeccakTest } from "../typechain"; +import { KeccakTestFactory } from "../typechain"; +import { REAL_KECCAK256_CONTRACT_ADDRESS } from "./shared/constants"; import { getWallets, loadArtifact, publishBytecode, setCode, getCode } from "./shared/utils"; import { ethers } from "hardhat"; import { readYulBytecode } from "../scripts/utils"; @@ -9,6 +9,7 @@ import { Language } from "../scripts/constants"; import type { BytesLike } from "ethers"; import { expect } from "chai"; import * as hre from "hardhat"; +import { prepareEnvironment } from "./shared/mocks"; describe("Keccak256 tests", function () { let keccakTest: KeccakTest; @@ -22,9 +23,10 @@ describe("Keccak256 tests", function () { const KECCAK_TEST_ADDRESS = "0x0000000000000000000000000000000000009000"; before(async () => { + await prepareEnvironment(); await setCode(KECCAK_TEST_ADDRESS, (await loadArtifact("KeccakTest")).bytecode); - const keccakCode = await getCode(KECCAK256_CONTRACT_ADDRESS); + const keccakCode = await getCode(REAL_KECCAK256_CONTRACT_ADDRESS); oldKeccakCodeHash = ethers.utils.hexlify(hashBytecode(keccakCode)); const keccakMockCode = readYulBytecode({ @@ -36,7 +38,7 @@ describe("Keccak256 tests", function () { keccakMockCodeHash = ethers.utils.hexlify(hashBytecode(keccakMockCode)); - keccakTest = KeccakTest__factory.connect(KECCAK_TEST_ADDRESS, getWallets()[0]); + keccakTest = KeccakTestFactory.connect(KECCAK_TEST_ADDRESS, getWallets()[0]); const correctKeccakCode = readYulBytecode({ codeName: "Keccak256", path: "precompiles", @@ -44,18 +46,15 @@ describe("Keccak256 tests", function () { address: ethers.constants.AddressZero, }); - const correctContractDeployerCode = (await loadArtifact("ContractDeployer")).bytecode; - await setCode(CONTRACT_DEPLOYER_ADDRESS, correctContractDeployerCode); - - const emptyContractCode = (await loadArtifact("AlwaysRevert")).bytecode; + const alwaysRevertCode = (await loadArtifact("AlwaysRevert")).bytecode; await publishBytecode(keccakCode); await publishBytecode(correctKeccakCode); - await publishBytecode(emptyContractCode); + await publishBytecode(alwaysRevertCode); await publishBytecode(keccakMockCode); correctKeccakCodeHash = ethers.utils.hexlify(hashBytecode(correctKeccakCode)); - alwaysRevertCodeHash = ethers.utils.hexlify(hashBytecode(emptyContractCode)); + alwaysRevertCodeHash = ethers.utils.hexlify(hashBytecode(alwaysRevertCode)); }); it("zero pointer test", async () => { @@ -115,7 +114,7 @@ describe("Keccak256 tests", function () { await keccakTest.keccakPerformUpgrade(mockKeccakInput); - let keccakCode = await getCode(KECCAK256_CONTRACT_ADDRESS); + let keccakCode = await getCode(REAL_KECCAK256_CONTRACT_ADDRESS); let keccakCodeHash = ethers.utils.hexlify(hashBytecode(keccakCode)); expect(keccakCodeHash).to.eq(keccakMockCodeHash); @@ -125,7 +124,7 @@ describe("Keccak256 tests", function () { // previous one. await hre.network.provider.send("hardhat_mine", ["0x100"]); - keccakCode = await getCode(KECCAK256_CONTRACT_ADDRESS); + keccakCode = await getCode(REAL_KECCAK256_CONTRACT_ADDRESS); keccakCodeHash = ethers.utils.hexlify(hashBytecode(keccakCode)); expect(keccakCodeHash).to.eq(oldKeccakCodeHash); diff --git a/system-contracts/test/shared/constants.ts b/system-contracts/test/shared/constants.ts index 20f4586fd..a37b86196 100644 --- a/system-contracts/test/shared/constants.ts +++ b/system-contracts/test/shared/constants.ts @@ -18,6 +18,7 @@ export const TEST_COMPLEX_UPGRADER_CONTRACT_ADDRESS = "0x00000000000000000000000 export const REAL_EVENT_WRITER_CONTRACT_ADDRESS = "0x000000000000000000000000000000000000800d"; export const REAL_DEPLOYER_SYSTEM_CONTRACT_ADDRESS = "0x0000000000000000000000000000000000008006"; export const REAL_ACCOUNT_CODE_STORAGE_SYSTEM_CONTRACT_ADDRESS = "0x0000000000000000000000000000000000008002"; +export const REAL_KECCAK256_CONTRACT_ADDRESS = "0x0000000000000000000000000000000000008010"; export const EMPTY_STRING_KECCAK = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"; export const TWO_IN_256 = BigNumber.from(2).pow(256);