Skip to content

Commit

Permalink
fix sc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavBreadless committed Jan 9, 2024
1 parent 54c6e74 commit 701ac85
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 26 deletions.
27 changes: 15 additions & 12 deletions system-contracts/contracts/test-contracts/KeccakTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
);
Expand All @@ -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
);
Expand All @@ -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
);
Expand All @@ -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
);
Expand All @@ -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
);
Expand Down
2 changes: 1 addition & 1 deletion system-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
1 change: 1 addition & 0 deletions system-contracts/scripts/compile-yul.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 12 additions & 13 deletions system-contracts/test/Keccak256.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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";
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;
Expand All @@ -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({
Expand All @@ -36,26 +38,23 @@ 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",
lang: Language.Yul,
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 () => {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions system-contracts/test/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 701ac85

Please sign in to comment.