From b7388a98749c40bd1a6b35fb3258bd7394fcf1f9 Mon Sep 17 00:00:00 2001 From: Uacias Date: Mon, 15 Jan 2024 08:10:26 +0000 Subject: [PATCH 01/13] nonceHolder tests init --- system-contracts/test/NonceHolder.spec.ts | 42 +++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 system-contracts/test/NonceHolder.spec.ts diff --git a/system-contracts/test/NonceHolder.spec.ts b/system-contracts/test/NonceHolder.spec.ts new file mode 100644 index 000000000..9a1b2e6c9 --- /dev/null +++ b/system-contracts/test/NonceHolder.spec.ts @@ -0,0 +1,42 @@ + +import { NonceHolder, NonceHolderFactory } from "../typechain"; +import { TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS } from "./shared/constants"; +import { prepareEnvironment } from "./shared/mocks"; +import { deployContractOnAddress, getWallets } from "./shared/utils"; + +describe("NonceHolder tests", () => { + const wallet = getWallets()[0]; + let nonceHolder: NonceHolder; + + before(async () => { + await prepareEnvironment(); + deployContractOnAddress(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, "NonceHolder"); + nonceHolder = NonceHolderFactory.connect(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, wallet); + }) + + describe("getMinNonce", async () => { + it("should get 1st account nonce", async () => { + const minNonce = await nonceHolder.getMinNonce(wallet.address); + console.log(minNonce); + + }) + + + }) + + describe("getRawNonce", async () => { + it("should get 1st account nonce", async () => { + const rawNonce = await nonceHolder.getRawNonce(wallet.address); + console.log(rawNonce); + }); + }) + + describe("getMinNonce", async () => { + + }) + + describe("getMinNonce", async () => { + + }) + +}); \ No newline at end of file From 11b0e04daedc786e32c927c645d0e4ec3455809c Mon Sep 17 00:00:00 2001 From: Uacias Date: Wed, 17 Jan 2024 14:19:30 +0000 Subject: [PATCH 02/13] NonceHolder tests --- system-contracts/test/NonceHolder.spec.ts | 221 ++++++++++++++++++---- system-contracts/test/shared/mocks.ts | 2 + 2 files changed, 188 insertions(+), 35 deletions(-) diff --git a/system-contracts/test/NonceHolder.spec.ts b/system-contracts/test/NonceHolder.spec.ts index 9a1b2e6c9..162512e7c 100644 --- a/system-contracts/test/NonceHolder.spec.ts +++ b/system-contracts/test/NonceHolder.spec.ts @@ -1,42 +1,193 @@ - -import { NonceHolder, NonceHolderFactory } from "../typechain"; -import { TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS } from "./shared/constants"; -import { prepareEnvironment } from "./shared/mocks"; +import { expect } from "chai"; +import type { NonceHolder } from "../typechain"; +import { NonceHolderFactory } from "../typechain"; +import { + TEST_BOOTLOADER_FORMAL_ADDRESS, + TEST_DEPLOYER_SYSTEM_CONTRACT_ADDRESS, + TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, +} from "./shared/constants"; +import { prepareEnvironment, setResult } from "./shared/mocks"; import { deployContractOnAddress, getWallets } from "./shared/utils"; +import { ethers, network } from "hardhat"; describe("NonceHolder tests", () => { - const wallet = getWallets()[0]; - let nonceHolder: NonceHolder; - - before(async () => { - await prepareEnvironment(); - deployContractOnAddress(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, "NonceHolder"); - nonceHolder = NonceHolderFactory.connect(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, wallet); - }) - - describe("getMinNonce", async () => { - it("should get 1st account nonce", async () => { - const minNonce = await nonceHolder.getMinNonce(wallet.address); - console.log(minNonce); - - }) - - - }) - - describe("getRawNonce", async () => { - it("should get 1st account nonce", async () => { - const rawNonce = await nonceHolder.getRawNonce(wallet.address); - console.log(rawNonce); + const wallet = getWallets()[0]; + let nonceHolder: NonceHolder; + let nonceHolderAccount: ethers.Signer; + let deployerAccount: ethers.Signer; + + before(async () => { + await prepareEnvironment(); + deployContractOnAddress(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, "NonceHolder"); + nonceHolder = NonceHolderFactory.connect(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, wallet); + nonceHolderAccount = await ethers.getImpersonatedSigner(TEST_BOOTLOADER_FORMAL_ADDRESS); + deployerAccount = await ethers.getImpersonatedSigner(TEST_DEPLOYER_SYSTEM_CONTRACT_ADDRESS); + }); + + after(async () => { + await network.provider.request({ + method: "hardhat_stopImpersonatingAccount", + params: [TEST_DEPLOYER_SYSTEM_CONTRACT_ADDRESS], + }); + + await network.provider.request({ + method: "hardhat_stopImpersonatingAccount", + params: [TEST_BOOTLOADER_FORMAL_ADDRESS], + }); + }); + + describe("increaseMinNonce", () => { + it("should revert This method require system call flag", async () => { + await expect(nonceHolder.increaseMinNonce(5)) + .to.be.rejectedWith("This method require system call flag") + .to.be.rejectedWith("This method require system call flag"); + }); + + it("should increase account minNonce", async () => { + await nonceHolder.connect(deployerAccount).increaseMinNonce(123); + await nonceHolder.connect(deployerAccount).increaseMinNonce(123); + }); + }); + + describe("getMinNonce", async () => { + it("should get account nonce", async () => { + const result = await nonceHolder.getMinNonce(deployerAccount.address); + expect(result).to.equal(123); + }); + }); + + describe("getRawNonce", async () => { + it("should get account raw nonce", async () => { + const result = await nonceHolder.getRawNonce(deployerAccount.address); + expect(result).to.equal(123); + }); + }); + + describe("incrementMinNonceIfEquals", async () => { + it("should revert This method require system call flag", async () => { + const expectedNonce = await nonceHolder.getMinNonce(deployerAccount.address); + await expect(nonceHolder.incrementMinNonceIfEquals(expectedNonce)).to.be.rejectedWith( + "This method require system call flag" + ); + }); + + it("should revert Incorrect nonce", async () => { + const expectedNonce = 2222222; + await expect(nonceHolder.connect(deployerAccount).incrementMinNonceIfEquals(expectedNonce)).to.be.rejectedWith( + "Incorrect nonce" + ); + }); + + it("should increment minNonce if equals to expected", async () => { + const expectedNonce = await nonceHolder.getMinNonce(deployerAccount.address); + await nonceHolder.connect(deployerAccount).incrementMinNonceIfEquals(expectedNonce); + }); + }); + + describe("incrementDeploymentNonce", async () => { + it("should revert Only the contract deployer can increment the deployment nonce", async () => { + await expect(nonceHolder.incrementDeploymentNonce(nonceHolderAccount.address)).to.be.rejectedWith( + "Only the contract deployer can increment the deployment nonce" + ); + }); + + it("should increment deployment nonce", async () => { + await nonceHolder.connect(deployerAccount).incrementDeploymentNonce(nonceHolderAccount.address); + }); + }); + + describe("getDeploymentNonce", async () => { + it("should get deployment nonce", async () => { + const result = await nonceHolder.getDeploymentNonce(nonceHolderAccount.address); + expect(result).to.equal(1); + }); + }); + + describe("setValueUnderNonce", async () => { + it("should revert Nonce value cannot be set to 0", async () => { + const accountInfo = [1, 0]; + + const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [accountInfo]); + + await setResult("ContractDeployer", "getAccountInfo", [nonceHolderAccount.address], { + failure: false, + returnData: encodedAccountInfo, + }); + await expect(nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(124, 0)).to.be.rejectedWith( + "Nonce value cannot be set to 0" + ); + }); + + it("should revert Previous nonce has not been used", async () => { + const accountInfo = [1, 0]; + + const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [accountInfo]); + + await setResult("ContractDeployer", "getAccountInfo", [nonceHolderAccount.address], { + failure: false, + returnData: encodedAccountInfo, + }); + await expect(nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(123, 111)).to.be.rejectedWith( + "Previous nonce has not been used" + ); + }); + + it("should emit ValueSetUnderNonce event", async () => { + const expectedNonce = await nonceHolder.getMinNonce(nonceHolderAccount.address); + await nonceHolder.connect(nonceHolderAccount).incrementMinNonceIfEquals(expectedNonce); + const accountInfo = [1, 0]; + + const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [accountInfo]); + + await setResult("ContractDeployer", "getAccountInfo", [nonceHolderAccount.address], { + failure: false, + returnData: encodedAccountInfo, }); - }) + await expect(nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(expectedNonce.add(1), 333)) + .to.emit(nonceHolder, "ValueSetUnderNonce") + .withArgs(nonceHolderAccount.address, expectedNonce.add(1), 333); + }); + }); + + describe("getValueUnderNonce", () => { + it("should get value under nonce", async () => { + const key = await nonceHolder.getMinNonce(nonceHolderAccount.address); + const storedValue = await nonceHolder.connect(nonceHolderAccount).getValueUnderNonce(key); + expect(storedValue).to.equal(333); + }); + }); + + describe("isNonceUsed", () => { + it("used nonce", async () => { + const isUsed = await nonceHolder.isNonceUsed(nonceHolderAccount.address, 1); + expect(isUsed).to.equal(true); + }); + + it("not used nonce", async () => { + const isUsed = await nonceHolder.isNonceUsed(nonceHolderAccount.address, 2222222); + expect(isUsed).to.equal(false); + }); + }); + + describe("validateNonceUsage", () => { + it("used nonce & should not be used", async () => { + await expect(nonceHolder.validateNonceUsage(nonceHolderAccount.address, 1, false)).to.be.rejectedWith( + "Reusing the same nonce twice" + ); + }); - describe("getMinNonce", async () => { - - }) + it("used nonce & should be used", async () => { + await nonceHolder.validateNonceUsage(nonceHolderAccount.address, 1, true); + }); - describe("getMinNonce", async () => { - - }) + it("not used nonce & should be used", async () => { + await expect(nonceHolder.validateNonceUsage(nonceHolderAccount.address, 222, true)).to.be.rejectedWith( + "The nonce was not set as used" + ); + }); -}); \ No newline at end of file + it("not used nonce & should not be used", async () => { + await nonceHolder.validateNonceUsage(nonceHolderAccount.address, 222, false); + }); + }); +}); diff --git a/system-contracts/test/shared/mocks.ts b/system-contracts/test/shared/mocks.ts index 2b8c35654..4dd51671e 100644 --- a/system-contracts/test/shared/mocks.ts +++ b/system-contracts/test/shared/mocks.ts @@ -2,6 +2,7 @@ import { ethers } from "hardhat"; import type { MockContract } from "../../typechain"; import { MockContractFactory } from "../../typechain"; import { + TEST_DEPLOYER_SYSTEM_CONTRACT_ADDRESS, TEST_ACCOUNT_CODE_STORAGE_SYSTEM_CONTRACT_ADDRESS, TEST_BOOTLOADER_FORMAL_ADDRESS, TEST_BASE_TOKEN_SYSTEM_CONTRACT_ADDRESS, @@ -26,6 +27,7 @@ type CallResult = { const TEST_SYSTEM_CONTRACTS_MOCKS = { Compressor: TEST_COMPRESSOR_CONTRACT_ADDRESS, SystemContext: TEST_SYSTEM_CONTEXT_CONTRACT_ADDRESS, + ContractDeployer: TEST_DEPLOYER_SYSTEM_CONTRACT_ADDRESS, NonceHolder: TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, L1Messenger: TEST_L1_MESSENGER_SYSTEM_CONTRACT_ADDRESS, KnownCodesStorage: TEST_KNOWN_CODE_STORAGE_CONTRACT_ADDRESS, From af1a8904da225bbd2e1da20911b99f1c4defb550 Mon Sep 17 00:00:00 2001 From: Uacias Date: Tue, 23 Jan 2024 11:37:55 +0000 Subject: [PATCH 03/13] increaseMinNonce 3 calls --- system-contracts/test/NonceHolder.spec.ts | 67 +++++++++++------------ 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/system-contracts/test/NonceHolder.spec.ts b/system-contracts/test/NonceHolder.spec.ts index 162512e7c..ceb386367 100644 --- a/system-contracts/test/NonceHolder.spec.ts +++ b/system-contracts/test/NonceHolder.spec.ts @@ -37,67 +37,69 @@ describe("NonceHolder tests", () => { }); describe("increaseMinNonce", () => { - it("should revert This method require system call flag", async () => { - await expect(nonceHolder.increaseMinNonce(5)) - .to.be.rejectedWith("This method require system call flag") - .to.be.rejectedWith("This method require system call flag"); - }); - + // expected revert doesn't work + // it("should revert This method require system call flag", async () => { + // await (nonceHolder.increaseMinNonce(123)) + // }); + + // ?min nonce changes after 3 calls? + // 1st cal call revert exception + // 2 calls minnonce == 0 + // 3 calls minnonce == 2**4 it("should increase account minNonce", async () => { - await nonceHolder.connect(deployerAccount).increaseMinNonce(123); - await nonceHolder.connect(deployerAccount).increaseMinNonce(123); + await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(2 ** 4); + await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(2 ** 4); + await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(2 ** 4); + const result = await nonceHolder.getMinNonce(nonceHolderAccount.address); + expect(result).to.equal(2 ** 4); }); }); describe("getMinNonce", async () => { - it("should get account nonce", async () => { - const result = await nonceHolder.getMinNonce(deployerAccount.address); - expect(result).to.equal(123); + it("should get account min nonce", async () => { + const result = await nonceHolder.getMinNonce(nonceHolderAccount.address); + expect(result).to.equal(2 ** 4); }); }); describe("getRawNonce", async () => { it("should get account raw nonce", async () => { - const result = await nonceHolder.getRawNonce(deployerAccount.address); - expect(result).to.equal(123); + const result = await nonceHolder.getRawNonce(nonceHolderAccount.address); + expect(result).to.equal(2 ** 4); }); }); describe("incrementMinNonceIfEquals", async () => { it("should revert This method require system call flag", async () => { - const expectedNonce = await nonceHolder.getMinNonce(deployerAccount.address); + const expectedNonce = await nonceHolder.getMinNonce(nonceHolderAccount.address); await expect(nonceHolder.incrementMinNonceIfEquals(expectedNonce)).to.be.rejectedWith( "This method require system call flag" ); }); it("should revert Incorrect nonce", async () => { - const expectedNonce = 2222222; - await expect(nonceHolder.connect(deployerAccount).incrementMinNonceIfEquals(expectedNonce)).to.be.rejectedWith( + await expect(nonceHolder.connect(nonceHolderAccount).incrementMinNonceIfEquals(2222222)).to.be.rejectedWith( "Incorrect nonce" ); }); it("should increment minNonce if equals to expected", async () => { - const expectedNonce = await nonceHolder.getMinNonce(deployerAccount.address); - await nonceHolder.connect(deployerAccount).incrementMinNonceIfEquals(expectedNonce); + const expectedNonce = await nonceHolder.getMinNonce(nonceHolderAccount.address); + await nonceHolder.connect(nonceHolderAccount).incrementMinNonceIfEquals(expectedNonce); + const result = await nonceHolder.getMinNonce(nonceHolderAccount.address); + expect(result).to.equal(expectedNonce.add(1)); }); }); describe("incrementDeploymentNonce", async () => { it("should revert Only the contract deployer can increment the deployment nonce", async () => { - await expect(nonceHolder.incrementDeploymentNonce(nonceHolderAccount.address)).to.be.rejectedWith( - "Only the contract deployer can increment the deployment nonce" - ); + await expect( + nonceHolder.connect(nonceHolderAccount).incrementDeploymentNonce(deployerAccount.address) + ).to.be.rejectedWith("Only the contract deployer can increment the deployment nonce"); }); it("should increment deployment nonce", async () => { await nonceHolder.connect(deployerAccount).incrementDeploymentNonce(nonceHolderAccount.address); - }); - }); - - describe("getDeploymentNonce", async () => { - it("should get deployment nonce", async () => { const result = await nonceHolder.getDeploymentNonce(nonceHolderAccount.address); expect(result).to.equal(1); }); @@ -106,9 +108,7 @@ describe("NonceHolder tests", () => { describe("setValueUnderNonce", async () => { it("should revert Nonce value cannot be set to 0", async () => { const accountInfo = [1, 0]; - const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [accountInfo]); - await setResult("ContractDeployer", "getAccountInfo", [nonceHolderAccount.address], { failure: false, returnData: encodedAccountInfo, @@ -120,9 +120,7 @@ describe("NonceHolder tests", () => { it("should revert Previous nonce has not been used", async () => { const accountInfo = [1, 0]; - const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [accountInfo]); - await setResult("ContractDeployer", "getAccountInfo", [nonceHolderAccount.address], { failure: false, returnData: encodedAccountInfo, @@ -136,9 +134,7 @@ describe("NonceHolder tests", () => { const expectedNonce = await nonceHolder.getMinNonce(nonceHolderAccount.address); await nonceHolder.connect(nonceHolderAccount).incrementMinNonceIfEquals(expectedNonce); const accountInfo = [1, 0]; - const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [accountInfo]); - await setResult("ContractDeployer", "getAccountInfo", [nonceHolderAccount.address], { failure: false, returnData: encodedAccountInfo, @@ -146,6 +142,9 @@ describe("NonceHolder tests", () => { await expect(nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(expectedNonce.add(1), 333)) .to.emit(nonceHolder, "ValueSetUnderNonce") .withArgs(nonceHolderAccount.address, expectedNonce.add(1), 333); + const key = await nonceHolder.getMinNonce(nonceHolderAccount.address); + const storedValue = await nonceHolder.connect(nonceHolderAccount).getValueUnderNonce(key); + expect(storedValue).to.equal(333); }); }); @@ -181,13 +180,13 @@ describe("NonceHolder tests", () => { }); it("not used nonce & should be used", async () => { - await expect(nonceHolder.validateNonceUsage(nonceHolderAccount.address, 222, true)).to.be.rejectedWith( + await expect(nonceHolder.validateNonceUsage(nonceHolderAccount.address, 2 ** 16, true)).to.be.rejectedWith( "The nonce was not set as used" ); }); it("not used nonce & should not be used", async () => { - await nonceHolder.validateNonceUsage(nonceHolderAccount.address, 222, false); + await nonceHolder.validateNonceUsage(nonceHolderAccount.address, 2 ** 16, false); }); }); }); From f91f58073b419fe11b2b56cadf5a921214e06315 Mon Sep 17 00:00:00 2001 From: Uacias Date: Tue, 30 Jan 2024 12:58:08 +0000 Subject: [PATCH 04/13] await fix --- system-contracts/test/NonceHolder.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system-contracts/test/NonceHolder.spec.ts b/system-contracts/test/NonceHolder.spec.ts index ceb386367..3a0e77a2b 100644 --- a/system-contracts/test/NonceHolder.spec.ts +++ b/system-contracts/test/NonceHolder.spec.ts @@ -18,7 +18,7 @@ describe("NonceHolder tests", () => { before(async () => { await prepareEnvironment(); - deployContractOnAddress(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, "NonceHolder"); + await deployContractOnAddress(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, "NonceHolder"); nonceHolder = NonceHolderFactory.connect(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, wallet); nonceHolderAccount = await ethers.getImpersonatedSigner(TEST_BOOTLOADER_FORMAL_ADDRESS); deployerAccount = await ethers.getImpersonatedSigner(TEST_DEPLOYER_SYSTEM_CONTRACT_ADDRESS); From 9d26b7dec13847076c25ed926d8e9aeb74a8f287 Mon Sep 17 00:00:00 2001 From: Uacias Date: Tue, 30 Jan 2024 13:01:27 +0000 Subject: [PATCH 05/13] rm unnecessary increaseMinNonce --- system-contracts/test/NonceHolder.spec.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/system-contracts/test/NonceHolder.spec.ts b/system-contracts/test/NonceHolder.spec.ts index 3a0e77a2b..031bac2ef 100644 --- a/system-contracts/test/NonceHolder.spec.ts +++ b/system-contracts/test/NonceHolder.spec.ts @@ -47,8 +47,6 @@ describe("NonceHolder tests", () => { // 2 calls minnonce == 0 // 3 calls minnonce == 2**4 it("should increase account minNonce", async () => { - await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(2 ** 4); - await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(2 ** 4); await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(2 ** 4); const result = await nonceHolder.getMinNonce(nonceHolderAccount.address); expect(result).to.equal(2 ** 4); From 02bd15ed76fdb489941feeddc2be923901f1e7c0 Mon Sep 17 00:00:00 2001 From: Uacias Date: Tue, 30 Jan 2024 13:06:16 +0000 Subject: [PATCH 06/13] rm comments, add negative test case --- system-contracts/test/NonceHolder.spec.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/system-contracts/test/NonceHolder.spec.ts b/system-contracts/test/NonceHolder.spec.ts index 031bac2ef..af8d16685 100644 --- a/system-contracts/test/NonceHolder.spec.ts +++ b/system-contracts/test/NonceHolder.spec.ts @@ -37,15 +37,10 @@ describe("NonceHolder tests", () => { }); describe("increaseMinNonce", () => { - // expected revert doesn't work - // it("should revert This method require system call flag", async () => { - // await (nonceHolder.increaseMinNonce(123)) - // }); - - // ?min nonce changes after 3 calls? - // 1st cal call revert exception - // 2 calls minnonce == 0 - // 3 calls minnonce == 2**4 + it("should revert This method require system call flag", async () => { + await expect(nonceHolder.increaseMinNonce(123)).to.be.rejectedWith("This method require system call flag"); + }); + it("should increase account minNonce", async () => { await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(2 ** 4); const result = await nonceHolder.getMinNonce(nonceHolderAccount.address); From 79bc109692050f1b86c9413dbed4553e83cfaead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Zaj=C4=85c?= Date: Tue, 27 Feb 2024 19:18:47 +0100 Subject: [PATCH 07/13] fixes from PR comments --- system-contracts/test/NonceHolder.spec.ts | 104 ++++++++++++++++------ 1 file changed, 79 insertions(+), 25 deletions(-) diff --git a/system-contracts/test/NonceHolder.spec.ts b/system-contracts/test/NonceHolder.spec.ts index af8d16685..68c4cddba 100644 --- a/system-contracts/test/NonceHolder.spec.ts +++ b/system-contracts/test/NonceHolder.spec.ts @@ -9,6 +9,7 @@ import { import { prepareEnvironment, setResult } from "./shared/mocks"; import { deployContractOnAddress, getWallets } from "./shared/utils"; import { ethers, network } from "hardhat"; +import { BigNumber } from "ethers"; describe("NonceHolder tests", () => { const wallet = getWallets()[0]; @@ -36,29 +37,45 @@ describe("NonceHolder tests", () => { }); }); - describe("increaseMinNonce", () => { - it("should revert This method require system call flag", async () => { - await expect(nonceHolder.increaseMinNonce(123)).to.be.rejectedWith("This method require system call flag"); + describe("increaseMinNonce and getters", () => { + it("should increase account minNonce by 1", async () => { + const nonceBefore = await nonceHolder.getMinNonce(nonceHolderAccount.address); + await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(1); + const result = await nonceHolder.getMinNonce(nonceHolderAccount.address); + expect(result).to.equal(nonceBefore.add(1)); + }); + + it("should stay the same", async () => { + const nonceBefore = await nonceHolder.getMinNonce(nonceHolderAccount.address); + const rawNonceBefore = await nonceHolder.getRawNonce(nonceHolderAccount.address); + await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(0); + const nonceAfter = await nonceHolder.getMinNonce(nonceHolderAccount.address); + const rawNonceAfter = await nonceHolder.getRawNonce(nonceHolderAccount.address); + + expect(nonceBefore).to.equal(nonceAfter); + expect(rawNonceBefore).to.equal(rawNonceAfter); }); - it("should increase account minNonce", async () => { + it("should increase account minNonce by many", async () => { + const nonceBefore = await nonceHolder.getMinNonce(nonceHolderAccount.address); await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(2 ** 4); const result = await nonceHolder.getMinNonce(nonceHolderAccount.address); - expect(result).to.equal(2 ** 4); + expect(result).to.equal(nonceBefore.add(2 ** 4)); }); - }); - describe("getMinNonce", async () => { - it("should get account min nonce", async () => { + it("should fail with too high", async () => { + const nonceBefore = await nonceHolder.getMinNonce(nonceHolderAccount.address); + await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(2 ** 4 + 1); const result = await nonceHolder.getMinNonce(nonceHolderAccount.address); - expect(result).to.equal(2 ** 4); + expect(result).to.equal(nonceBefore.add(2 ** 4 + 1)); + + // await expect(nonceHolder.connect(nonceHolderAccount).increaseMinNonce(2 ** 4 + 1)).to.be.rejectedWith( + // "The value for incrementing the nonce is too high" + // ); }); - }); - describe("getRawNonce", async () => { - it("should get account raw nonce", async () => { - const result = await nonceHolder.getRawNonce(nonceHolderAccount.address); - expect(result).to.equal(2 ** 4); + it("should revert This method require system call flag", async () => { + await expect(nonceHolder.increaseMinNonce(123)).to.be.rejectedWith("This method require system call flag"); }); }); @@ -92,13 +109,18 @@ describe("NonceHolder tests", () => { }); it("should increment deployment nonce", async () => { + const nonceBefore = await nonceHolder.getDeploymentNonce(nonceHolderAccount.address); + const rawNonceBefore = await nonceHolder.getRawNonce(nonceHolderAccount.address); await nonceHolder.connect(deployerAccount).incrementDeploymentNonce(nonceHolderAccount.address); - const result = await nonceHolder.getDeploymentNonce(nonceHolderAccount.address); - expect(result).to.equal(1); + const nonceAfter = await nonceHolder.getDeploymentNonce(nonceHolderAccount.address); + const rawNonceAfter = await nonceHolder.getRawNonce(nonceHolderAccount.address); + + expect(nonceAfter).to.equal(nonceBefore.add(BigNumber.from(1))); + expect(rawNonceAfter).to.equal(rawNonceBefore.add(BigNumber.from(2).pow(128))); }); }); - describe("setValueUnderNonce", async () => { + describe("setValueUnderNonce and getValueUnderNonce", async () => { it("should revert Nonce value cannot be set to 0", async () => { const accountInfo = [1, 0]; const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [accountInfo]); @@ -118,7 +140,7 @@ describe("NonceHolder tests", () => { failure: false, returnData: encodedAccountInfo, }); - await expect(nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(123, 111)).to.be.rejectedWith( + await expect(nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(443, 111)).to.be.rejectedWith( "Previous nonce has not been used" ); }); @@ -139,25 +161,57 @@ describe("NonceHolder tests", () => { const storedValue = await nonceHolder.connect(nonceHolderAccount).getValueUnderNonce(key); expect(storedValue).to.equal(333); }); - }); - describe("getValueUnderNonce", () => { - it("should get value under nonce", async () => { + it("should emit ValueSetUnderNonce event", async () => { + const currentNonce = await nonceHolder.getMinNonce(nonceHolderAccount.address); + await nonceHolder.connect(nonceHolderAccount).incrementMinNonceIfEquals(currentNonce); + const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [[1, 0]]); + await setResult("ContractDeployer", "getAccountInfo", [nonceHolderAccount.address], { + failure: false, + returnData: encodedAccountInfo, + }); + + await expect(nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(currentNonce.add(1), 111)) + .to.emit(nonceHolder, "ValueSetUnderNonce") + .withArgs(nonceHolderAccount.address, currentNonce.add(1), 111); + + await expect( + nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(currentNonce.add(3), 333) + ).to.rejectedWith("Previous nonce has not been used"); + + await expect(nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(currentNonce.add(2), 222)) + .to.emit(nonceHolder, "ValueSetUnderNonce") + .withArgs(nonceHolderAccount.address, currentNonce.add(2), 222); + const key = await nonceHolder.getMinNonce(nonceHolderAccount.address); const storedValue = await nonceHolder.connect(nonceHolderAccount).getValueUnderNonce(key); - expect(storedValue).to.equal(333); + expect(storedValue).to.equal(111); + const storedValueNext = await nonceHolder.connect(nonceHolderAccount).getValueUnderNonce(key.add(1)); + expect(storedValueNext).to.equal(222); }); }); describe("isNonceUsed", () => { - it("used nonce", async () => { + it("used nonce because it too small", async () => { const isUsed = await nonceHolder.isNonceUsed(nonceHolderAccount.address, 1); expect(isUsed).to.equal(true); }); + it("used nonce because set", async () => { + const currentNonce = await nonceHolder.getMinNonce(nonceHolderAccount.address); + const checkedNonce = currentNonce.add(1); + await nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(checkedNonce, 5); + + const isUsed = await nonceHolder.isNonceUsed(nonceHolderAccount.address, checkedNonce); + expect(isUsed).to.equal(true); + }); + it("not used nonce", async () => { - const isUsed = await nonceHolder.isNonceUsed(nonceHolderAccount.address, 2222222); - expect(isUsed).to.equal(false); + const currentNonce = await nonceHolder.getMinNonce(nonceHolderAccount.address); + const checkedNonce = currentNonce.add(2137 * 2 ** 10); + + const isUsed = await nonceHolder.isNonceUsed(nonceHolderAccount.address, checkedNonce); + expect(isUsed).to.be.false; }); }); From cd441148405928b4024f4f5c623a7277fd871a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Zaj=C4=85c?= Date: Wed, 28 Feb 2024 12:01:10 +0100 Subject: [PATCH 08/13] using separate account in tests --- system-contracts/test/NonceHolder.spec.ts | 131 +++++++++++----------- 1 file changed, 68 insertions(+), 63 deletions(-) diff --git a/system-contracts/test/NonceHolder.spec.ts b/system-contracts/test/NonceHolder.spec.ts index 68c4cddba..7f3c1d4b1 100644 --- a/system-contracts/test/NonceHolder.spec.ts +++ b/system-contracts/test/NonceHolder.spec.ts @@ -14,14 +14,19 @@ import { BigNumber } from "ethers"; describe("NonceHolder tests", () => { const wallet = getWallets()[0]; let nonceHolder: NonceHolder; + let systemNonceHolder: NonceHolder; let nonceHolderAccount: ethers.Signer; + let systemAccount: ethers.Signer; let deployerAccount: ethers.Signer; before(async () => { await prepareEnvironment(); + await deployContractOnAddress(await wallet.getAddress(), "NonceHolder"); await deployContractOnAddress(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, "NonceHolder"); - nonceHolder = NonceHolderFactory.connect(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, wallet); - nonceHolderAccount = await ethers.getImpersonatedSigner(TEST_BOOTLOADER_FORMAL_ADDRESS); + nonceHolder = NonceHolderFactory.connect(await wallet.getAddress(), wallet); + systemNonceHolder = NonceHolderFactory.connect(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, wallet); + nonceHolderAccount = await ethers.getImpersonatedSigner(await wallet.getAddress()); + systemAccount = await ethers.getImpersonatedSigner("0x000000000000000000000000000000000000900b"); deployerAccount = await ethers.getImpersonatedSigner(TEST_DEPLOYER_SYSTEM_CONTRACT_ADDRESS); }); @@ -39,39 +44,39 @@ describe("NonceHolder tests", () => { describe("increaseMinNonce and getters", () => { it("should increase account minNonce by 1", async () => { - const nonceBefore = await nonceHolder.getMinNonce(nonceHolderAccount.address); - await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(1); - const result = await nonceHolder.getMinNonce(nonceHolderAccount.address); - expect(result).to.equal(nonceBefore.add(1)); + const nonceBefore = await nonceHolder.getMinNonce(systemAccount.address); + await nonceHolder.connect(systemAccount).increaseMinNonce(1); + const nonceAfter = await nonceHolder.getMinNonce(systemAccount.address); + expect(nonceAfter).to.equal(nonceBefore.add(1)); }); it("should stay the same", async () => { - const nonceBefore = await nonceHolder.getMinNonce(nonceHolderAccount.address); - const rawNonceBefore = await nonceHolder.getRawNonce(nonceHolderAccount.address); - await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(0); - const nonceAfter = await nonceHolder.getMinNonce(nonceHolderAccount.address); - const rawNonceAfter = await nonceHolder.getRawNonce(nonceHolderAccount.address); + const nonceBefore = await nonceHolder.getMinNonce(systemAccount.address); + const rawNonceBefore = await nonceHolder.getRawNonce(systemAccount.address); + await nonceHolder.connect(systemAccount).increaseMinNonce(0); + const nonceAfter = await nonceHolder.getMinNonce(systemAccount.address); + const rawNonceAfter = await nonceHolder.getRawNonce(systemAccount.address); expect(nonceBefore).to.equal(nonceAfter); expect(rawNonceBefore).to.equal(rawNonceAfter); }); - it("should increase account minNonce by many", async () => { - const nonceBefore = await nonceHolder.getMinNonce(nonceHolderAccount.address); - await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(2 ** 4); - const result = await nonceHolder.getMinNonce(nonceHolderAccount.address); - expect(result).to.equal(nonceBefore.add(2 ** 4)); - }); + // it("should increase account minNonce by many", async () => { + // const nonceBefore = await nonceHolder.getMinNonce(systemAccount.address); + // await nonceHolder.connect(systemAccount).increaseMinNonce(2 ** 4); + // const result = await nonceHolder.getMinNonce(systemAccount.address); + // expect(result).to.equal(nonceBefore.add(2 ** 4)); + // }); it("should fail with too high", async () => { - const nonceBefore = await nonceHolder.getMinNonce(nonceHolderAccount.address); - await nonceHolder.connect(nonceHolderAccount).increaseMinNonce(2 ** 4 + 1); - const result = await nonceHolder.getMinNonce(nonceHolderAccount.address); + const nonceBefore = await nonceHolder.getMinNonce(systemAccount.address); + await nonceHolder.connect(systemAccount).increaseMinNonce(2 ** 4 + 1); + const result = await nonceHolder.getMinNonce(systemAccount.address); expect(result).to.equal(nonceBefore.add(2 ** 4 + 1)); - // await expect(nonceHolder.connect(nonceHolderAccount).increaseMinNonce(2 ** 4 + 1)).to.be.rejectedWith( - // "The value for incrementing the nonce is too high" - // ); + await expect( + nonceHolder.connect(systemAccount).increaseMinNonce(BigNumber.from(2).pow(32).add(1)) + ).to.be.rejectedWith("The value for incrementing the nonce is too high"); }); it("should revert This method require system call flag", async () => { @@ -81,22 +86,22 @@ describe("NonceHolder tests", () => { describe("incrementMinNonceIfEquals", async () => { it("should revert This method require system call flag", async () => { - const expectedNonce = await nonceHolder.getMinNonce(nonceHolderAccount.address); - await expect(nonceHolder.incrementMinNonceIfEquals(expectedNonce)).to.be.rejectedWith( + const expectedNonce = await nonceHolder.getMinNonce(systemAccount.address); + await expect(systemNonceHolder.incrementMinNonceIfEquals(expectedNonce)).to.be.rejectedWith( "This method require system call flag" ); }); it("should revert Incorrect nonce", async () => { - await expect(nonceHolder.connect(nonceHolderAccount).incrementMinNonceIfEquals(2222222)).to.be.rejectedWith( + await expect(nonceHolder.connect(systemAccount).incrementMinNonceIfEquals(2222222)).to.be.rejectedWith( "Incorrect nonce" ); }); it("should increment minNonce if equals to expected", async () => { - const expectedNonce = await nonceHolder.getMinNonce(nonceHolderAccount.address); - await nonceHolder.connect(nonceHolderAccount).incrementMinNonceIfEquals(expectedNonce); - const result = await nonceHolder.getMinNonce(nonceHolderAccount.address); + const expectedNonce = await nonceHolder.getMinNonce(systemAccount.address); + await nonceHolder.connect(systemAccount).incrementMinNonceIfEquals(expectedNonce); + const result = await nonceHolder.getMinNonce(systemAccount.address); expect(result).to.equal(expectedNonce.add(1)); }); }); @@ -124,11 +129,11 @@ describe("NonceHolder tests", () => { it("should revert Nonce value cannot be set to 0", async () => { const accountInfo = [1, 0]; const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [accountInfo]); - await setResult("ContractDeployer", "getAccountInfo", [nonceHolderAccount.address], { + await setResult("ContractDeployer", "getAccountInfo", [systemAccount.address], { failure: false, returnData: encodedAccountInfo, }); - await expect(nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(124, 0)).to.be.rejectedWith( + await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(124, 0)).to.be.rejectedWith( "Nonce value cannot be set to 0" ); }); @@ -136,104 +141,104 @@ describe("NonceHolder tests", () => { it("should revert Previous nonce has not been used", async () => { const accountInfo = [1, 0]; const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [accountInfo]); - await setResult("ContractDeployer", "getAccountInfo", [nonceHolderAccount.address], { + await setResult("ContractDeployer", "getAccountInfo", [systemAccount.address], { failure: false, returnData: encodedAccountInfo, }); - await expect(nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(443, 111)).to.be.rejectedWith( + await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(443, 111)).to.be.rejectedWith( "Previous nonce has not been used" ); }); it("should emit ValueSetUnderNonce event", async () => { - const expectedNonce = await nonceHolder.getMinNonce(nonceHolderAccount.address); - await nonceHolder.connect(nonceHolderAccount).incrementMinNonceIfEquals(expectedNonce); + const expectedNonce = await nonceHolder.getMinNonce(systemAccount.address); + await nonceHolder.connect(systemAccount).incrementMinNonceIfEquals(expectedNonce); const accountInfo = [1, 0]; const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [accountInfo]); - await setResult("ContractDeployer", "getAccountInfo", [nonceHolderAccount.address], { + await setResult("ContractDeployer", "getAccountInfo", [systemAccount.address], { failure: false, returnData: encodedAccountInfo, }); - await expect(nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(expectedNonce.add(1), 333)) + await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(expectedNonce.add(1), 333)) .to.emit(nonceHolder, "ValueSetUnderNonce") - .withArgs(nonceHolderAccount.address, expectedNonce.add(1), 333); - const key = await nonceHolder.getMinNonce(nonceHolderAccount.address); - const storedValue = await nonceHolder.connect(nonceHolderAccount).getValueUnderNonce(key); + .withArgs(systemAccount.address, expectedNonce.add(1), 333); + const key = await nonceHolder.getMinNonce(systemAccount.address); + const storedValue = await nonceHolder.connect(systemAccount).getValueUnderNonce(key); expect(storedValue).to.equal(333); }); it("should emit ValueSetUnderNonce event", async () => { - const currentNonce = await nonceHolder.getMinNonce(nonceHolderAccount.address); - await nonceHolder.connect(nonceHolderAccount).incrementMinNonceIfEquals(currentNonce); + const currentNonce = await nonceHolder.getMinNonce(systemAccount.address); + await nonceHolder.connect(systemAccount).incrementMinNonceIfEquals(currentNonce); const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [[1, 0]]); - await setResult("ContractDeployer", "getAccountInfo", [nonceHolderAccount.address], { + await setResult("ContractDeployer", "getAccountInfo", [systemAccount.address], { failure: false, returnData: encodedAccountInfo, }); - await expect(nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(currentNonce.add(1), 111)) + await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce.add(1), 111)) .to.emit(nonceHolder, "ValueSetUnderNonce") - .withArgs(nonceHolderAccount.address, currentNonce.add(1), 111); + .withArgs(systemAccount.address, currentNonce.add(1), 111); - await expect( - nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(currentNonce.add(3), 333) - ).to.rejectedWith("Previous nonce has not been used"); + await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce.add(3), 333)).to.rejectedWith( + "Previous nonce has not been used" + ); - await expect(nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(currentNonce.add(2), 222)) + await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce.add(2), 222)) .to.emit(nonceHolder, "ValueSetUnderNonce") - .withArgs(nonceHolderAccount.address, currentNonce.add(2), 222); + .withArgs(systemAccount.address, currentNonce.add(2), 222); - const key = await nonceHolder.getMinNonce(nonceHolderAccount.address); - const storedValue = await nonceHolder.connect(nonceHolderAccount).getValueUnderNonce(key); + const key = await nonceHolder.getMinNonce(systemAccount.address); + const storedValue = await nonceHolder.connect(systemAccount).getValueUnderNonce(key); expect(storedValue).to.equal(111); - const storedValueNext = await nonceHolder.connect(nonceHolderAccount).getValueUnderNonce(key.add(1)); + const storedValueNext = await nonceHolder.connect(systemAccount).getValueUnderNonce(key.add(1)); expect(storedValueNext).to.equal(222); }); }); describe("isNonceUsed", () => { it("used nonce because it too small", async () => { - const isUsed = await nonceHolder.isNonceUsed(nonceHolderAccount.address, 1); + const isUsed = await nonceHolder.isNonceUsed(systemAccount.address, 1); expect(isUsed).to.equal(true); }); it("used nonce because set", async () => { - const currentNonce = await nonceHolder.getMinNonce(nonceHolderAccount.address); + const currentNonce = await nonceHolder.getMinNonce(systemAccount.address); const checkedNonce = currentNonce.add(1); - await nonceHolder.connect(nonceHolderAccount).setValueUnderNonce(checkedNonce, 5); + await nonceHolder.connect(systemAccount).setValueUnderNonce(checkedNonce, 5); - const isUsed = await nonceHolder.isNonceUsed(nonceHolderAccount.address, checkedNonce); + const isUsed = await nonceHolder.isNonceUsed(systemAccount.address, checkedNonce); expect(isUsed).to.equal(true); }); it("not used nonce", async () => { - const currentNonce = await nonceHolder.getMinNonce(nonceHolderAccount.address); + const currentNonce = await nonceHolder.getMinNonce(systemAccount.address); const checkedNonce = currentNonce.add(2137 * 2 ** 10); - const isUsed = await nonceHolder.isNonceUsed(nonceHolderAccount.address, checkedNonce); + const isUsed = await nonceHolder.isNonceUsed(systemAccount.address, checkedNonce); expect(isUsed).to.be.false; }); }); describe("validateNonceUsage", () => { it("used nonce & should not be used", async () => { - await expect(nonceHolder.validateNonceUsage(nonceHolderAccount.address, 1, false)).to.be.rejectedWith( + await expect(nonceHolder.validateNonceUsage(systemAccount.address, 1, false)).to.be.rejectedWith( "Reusing the same nonce twice" ); }); it("used nonce & should be used", async () => { - await nonceHolder.validateNonceUsage(nonceHolderAccount.address, 1, true); + await nonceHolder.validateNonceUsage(systemAccount.address, 1, true); }); it("not used nonce & should be used", async () => { - await expect(nonceHolder.validateNonceUsage(nonceHolderAccount.address, 2 ** 16, true)).to.be.rejectedWith( + await expect(nonceHolder.validateNonceUsage(systemAccount.address, 2 ** 16, true)).to.be.rejectedWith( "The nonce was not set as used" ); }); it("not used nonce & should not be used", async () => { - await nonceHolder.validateNonceUsage(nonceHolderAccount.address, 2 ** 16, false); + await nonceHolder.validateNonceUsage(systemAccount.address, 2 ** 16, false); }); }); }); From 8d338b3c4c4678ab93f13f42488f31457291b806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Zaj=C4=85c?= Date: Fri, 22 Mar 2024 15:44:53 +0000 Subject: [PATCH 09/13] pr fixes --- system-contracts/test/NonceHolder.spec.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/system-contracts/test/NonceHolder.spec.ts b/system-contracts/test/NonceHolder.spec.ts index 7f3c1d4b1..2e9868c93 100644 --- a/system-contracts/test/NonceHolder.spec.ts +++ b/system-contracts/test/NonceHolder.spec.ts @@ -14,7 +14,6 @@ import { BigNumber } from "ethers"; describe("NonceHolder tests", () => { const wallet = getWallets()[0]; let nonceHolder: NonceHolder; - let systemNonceHolder: NonceHolder; let nonceHolderAccount: ethers.Signer; let systemAccount: ethers.Signer; let deployerAccount: ethers.Signer; @@ -24,7 +23,6 @@ describe("NonceHolder tests", () => { await deployContractOnAddress(await wallet.getAddress(), "NonceHolder"); await deployContractOnAddress(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, "NonceHolder"); nonceHolder = NonceHolderFactory.connect(await wallet.getAddress(), wallet); - systemNonceHolder = NonceHolderFactory.connect(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, wallet); nonceHolderAccount = await ethers.getImpersonatedSigner(await wallet.getAddress()); systemAccount = await ethers.getImpersonatedSigner("0x000000000000000000000000000000000000900b"); deployerAccount = await ethers.getImpersonatedSigner(TEST_DEPLOYER_SYSTEM_CONTRACT_ADDRESS); @@ -61,12 +59,12 @@ describe("NonceHolder tests", () => { expect(rawNonceBefore).to.equal(rawNonceAfter); }); - // it("should increase account minNonce by many", async () => { - // const nonceBefore = await nonceHolder.getMinNonce(systemAccount.address); - // await nonceHolder.connect(systemAccount).increaseMinNonce(2 ** 4); - // const result = await nonceHolder.getMinNonce(systemAccount.address); - // expect(result).to.equal(nonceBefore.add(2 ** 4)); - // }); + it("should increase account minNonce by many", async () => { + const nonceBefore = await nonceHolder.getMinNonce(systemAccount.address); + await nonceHolder.connect(systemAccount).increaseMinNonce(2 ** 4); + const result = await nonceHolder.getMinNonce(systemAccount.address); + expect(result).to.equal(nonceBefore.add(2 ** 4)); + }); it("should fail with too high", async () => { const nonceBefore = await nonceHolder.getMinNonce(systemAccount.address); @@ -87,7 +85,7 @@ describe("NonceHolder tests", () => { describe("incrementMinNonceIfEquals", async () => { it("should revert This method require system call flag", async () => { const expectedNonce = await nonceHolder.getMinNonce(systemAccount.address); - await expect(systemNonceHolder.incrementMinNonceIfEquals(expectedNonce)).to.be.rejectedWith( + await expect(nonceHolder.incrementMinNonceIfEquals(expectedNonce)).to.be.rejectedWith( "This method require system call flag" ); }); @@ -167,7 +165,7 @@ describe("NonceHolder tests", () => { expect(storedValue).to.equal(333); }); - it("should emit ValueSetUnderNonce event", async () => { + it("should emit ValueSetUnderNonce event not in order", async () => { const currentNonce = await nonceHolder.getMinNonce(systemAccount.address); await nonceHolder.connect(systemAccount).incrementMinNonceIfEquals(currentNonce); const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [[1, 0]]); From 09404f05e56f5efae8bdbfe4b34bea91e522d219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Zaj=C4=85c?= Date: Tue, 16 Apr 2024 15:32:39 +0000 Subject: [PATCH 10/13] arbitrary ordering test --- system-contracts/test/NonceHolder.spec.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/system-contracts/test/NonceHolder.spec.ts b/system-contracts/test/NonceHolder.spec.ts index 2e9868c93..fbc791d94 100644 --- a/system-contracts/test/NonceHolder.spec.ts +++ b/system-contracts/test/NonceHolder.spec.ts @@ -165,10 +165,10 @@ describe("NonceHolder tests", () => { expect(storedValue).to.equal(333); }); - it("should emit ValueSetUnderNonce event not in order", async () => { + it("should emit ValueSetUnderNonce event arbitrary ordering", async () => { const currentNonce = await nonceHolder.getMinNonce(systemAccount.address); await nonceHolder.connect(systemAccount).incrementMinNonceIfEquals(currentNonce); - const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [[1, 0]]); + const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [[1, 1]]); await setResult("ContractDeployer", "getAccountInfo", [systemAccount.address], { failure: false, returnData: encodedAccountInfo, @@ -178,9 +178,9 @@ describe("NonceHolder tests", () => { .to.emit(nonceHolder, "ValueSetUnderNonce") .withArgs(systemAccount.address, currentNonce.add(1), 111); - await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce.add(3), 333)).to.rejectedWith( - "Previous nonce has not been used" - ); + await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce.add(3), 333)) + .to.emit(nonceHolder, "ValueSetUnderNonce") + .withArgs(systemAccount.address, currentNonce.add(3), 333); await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce.add(2), 222)) .to.emit(nonceHolder, "ValueSetUnderNonce") From d97d247227ac503758321599e0ca34346210136a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Zaj=C4=85c?= Date: Tue, 16 Apr 2024 15:53:42 +0000 Subject: [PATCH 11/13] minor cleanup --- system-contracts/test/NonceHolder.spec.ts | 54 +++++++++++++---------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/system-contracts/test/NonceHolder.spec.ts b/system-contracts/test/NonceHolder.spec.ts index fbc791d94..644ad9cf5 100644 --- a/system-contracts/test/NonceHolder.spec.ts +++ b/system-contracts/test/NonceHolder.spec.ts @@ -2,9 +2,9 @@ import { expect } from "chai"; import type { NonceHolder } from "../typechain"; import { NonceHolderFactory } from "../typechain"; import { - TEST_BOOTLOADER_FORMAL_ADDRESS, TEST_DEPLOYER_SYSTEM_CONTRACT_ADDRESS, TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, + TEST_SYSTEM_CONTEXT_CONTRACT_ADDRESS, } from "./shared/constants"; import { prepareEnvironment, setResult } from "./shared/mocks"; import { deployContractOnAddress, getWallets } from "./shared/utils"; @@ -14,17 +14,16 @@ import { BigNumber } from "ethers"; describe("NonceHolder tests", () => { const wallet = getWallets()[0]; let nonceHolder: NonceHolder; - let nonceHolderAccount: ethers.Signer; let systemAccount: ethers.Signer; let deployerAccount: ethers.Signer; before(async () => { await prepareEnvironment(); - await deployContractOnAddress(await wallet.getAddress(), "NonceHolder"); await deployContractOnAddress(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, "NonceHolder"); - nonceHolder = NonceHolderFactory.connect(await wallet.getAddress(), wallet); - nonceHolderAccount = await ethers.getImpersonatedSigner(await wallet.getAddress()); - systemAccount = await ethers.getImpersonatedSigner("0x000000000000000000000000000000000000900b"); + nonceHolder = NonceHolderFactory.connect(TEST_NONCE_HOLDER_SYSTEM_CONTRACT_ADDRESS, wallet); + + // Using a system account to satisfy the `onlySystemCall` modifier. + systemAccount = await ethers.getImpersonatedSigner(TEST_SYSTEM_CONTEXT_CONTRACT_ADDRESS); deployerAccount = await ethers.getImpersonatedSigner(TEST_DEPLOYER_SYSTEM_CONTRACT_ADDRESS); }); @@ -33,19 +32,18 @@ describe("NonceHolder tests", () => { method: "hardhat_stopImpersonatingAccount", params: [TEST_DEPLOYER_SYSTEM_CONTRACT_ADDRESS], }); - - await network.provider.request({ - method: "hardhat_stopImpersonatingAccount", - params: [TEST_BOOTLOADER_FORMAL_ADDRESS], - }); }); describe("increaseMinNonce and getters", () => { it("should increase account minNonce by 1", async () => { const nonceBefore = await nonceHolder.getMinNonce(systemAccount.address); + const rawNonceBefore = await nonceHolder.getRawNonce(systemAccount.address); await nonceHolder.connect(systemAccount).increaseMinNonce(1); const nonceAfter = await nonceHolder.getMinNonce(systemAccount.address); + const rawNonceAfter = await nonceHolder.getRawNonce(systemAccount.address); + expect(nonceAfter).to.equal(nonceBefore.add(1)); + expect(rawNonceAfter).to.equal(rawNonceBefore.add(1)); }); it("should stay the same", async () => { @@ -61,20 +59,28 @@ describe("NonceHolder tests", () => { it("should increase account minNonce by many", async () => { const nonceBefore = await nonceHolder.getMinNonce(systemAccount.address); + const rawNonceBefore = await nonceHolder.getRawNonce(systemAccount.address); await nonceHolder.connect(systemAccount).increaseMinNonce(2 ** 4); - const result = await nonceHolder.getMinNonce(systemAccount.address); - expect(result).to.equal(nonceBefore.add(2 ** 4)); + const nonceAfter = await nonceHolder.getMinNonce(systemAccount.address); + const rawNonceAfter = await nonceHolder.getRawNonce(systemAccount.address); + + expect(nonceAfter).to.equal(nonceBefore.add(2 ** 4)); + expect(rawNonceAfter).to.equal(rawNonceBefore.add(2 ** 4)); }); it("should fail with too high", async () => { const nonceBefore = await nonceHolder.getMinNonce(systemAccount.address); - await nonceHolder.connect(systemAccount).increaseMinNonce(2 ** 4 + 1); - const result = await nonceHolder.getMinNonce(systemAccount.address); - expect(result).to.equal(nonceBefore.add(2 ** 4 + 1)); + const rawNonceBefore = await nonceHolder.getRawNonce(systemAccount.address); await expect( nonceHolder.connect(systemAccount).increaseMinNonce(BigNumber.from(2).pow(32).add(1)) ).to.be.rejectedWith("The value for incrementing the nonce is too high"); + + const nonceAfter = await nonceHolder.getMinNonce(systemAccount.address); + const rawNonceAfter = await nonceHolder.getRawNonce(systemAccount.address); + + expect(nonceAfter).to.equal(nonceBefore); + expect(rawNonceAfter).to.equal(rawNonceBefore); }); it("should revert This method require system call flag", async () => { @@ -106,17 +112,17 @@ describe("NonceHolder tests", () => { describe("incrementDeploymentNonce", async () => { it("should revert Only the contract deployer can increment the deployment nonce", async () => { - await expect( - nonceHolder.connect(nonceHolderAccount).incrementDeploymentNonce(deployerAccount.address) - ).to.be.rejectedWith("Only the contract deployer can increment the deployment nonce"); + await expect(nonceHolder.incrementDeploymentNonce(deployerAccount.address)).to.be.rejectedWith( + "Only the contract deployer can increment the deployment nonce" + ); }); it("should increment deployment nonce", async () => { - const nonceBefore = await nonceHolder.getDeploymentNonce(nonceHolderAccount.address); - const rawNonceBefore = await nonceHolder.getRawNonce(nonceHolderAccount.address); - await nonceHolder.connect(deployerAccount).incrementDeploymentNonce(nonceHolderAccount.address); - const nonceAfter = await nonceHolder.getDeploymentNonce(nonceHolderAccount.address); - const rawNonceAfter = await nonceHolder.getRawNonce(nonceHolderAccount.address); + const nonceBefore = await nonceHolder.getDeploymentNonce(wallet.address); + const rawNonceBefore = await nonceHolder.getRawNonce(wallet.address); + await nonceHolder.connect(deployerAccount).incrementDeploymentNonce(wallet.address); + const nonceAfter = await nonceHolder.getDeploymentNonce(wallet.address); + const rawNonceAfter = await nonceHolder.getRawNonce(wallet.address); expect(nonceAfter).to.equal(nonceBefore.add(BigNumber.from(1))); expect(rawNonceAfter).to.equal(rawNonceBefore.add(BigNumber.from(2).pow(128))); From 8343b1f2234d6d09a2e93a34dae8e3a75be0d669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Zaj=C4=85c?= Date: Tue, 7 May 2024 11:25:03 +0000 Subject: [PATCH 12/13] final nonce changes --- system-contracts/test/NonceHolder.spec.ts | 48 ++++++++++++++--------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/system-contracts/test/NonceHolder.spec.ts b/system-contracts/test/NonceHolder.spec.ts index 644ad9cf5..c3e9e4583 100644 --- a/system-contracts/test/NonceHolder.spec.ts +++ b/system-contracts/test/NonceHolder.spec.ts @@ -28,6 +28,10 @@ describe("NonceHolder tests", () => { }); after(async () => { + await network.provider.request({ + method: "hardhat_stopImpersonatingAccount", + params: [TEST_SYSTEM_CONTEXT_CONTRACT_ADDRESS], + }); await network.provider.request({ method: "hardhat_stopImpersonatingAccount", params: [TEST_DEPLOYER_SYSTEM_CONTRACT_ADDRESS], @@ -155,48 +159,54 @@ describe("NonceHolder tests", () => { }); it("should emit ValueSetUnderNonce event", async () => { - const expectedNonce = await nonceHolder.getMinNonce(systemAccount.address); - await nonceHolder.connect(systemAccount).incrementMinNonceIfEquals(expectedNonce); + const currentNonce = await nonceHolder.getMinNonce(systemAccount.address); + const valueBefore = await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce); + const value = valueBefore.add(42); + const accountInfo = [1, 0]; const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [accountInfo]); await setResult("ContractDeployer", "getAccountInfo", [systemAccount.address], { failure: false, returnData: encodedAccountInfo, }); - await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(expectedNonce.add(1), 333)) + await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce, value)) .to.emit(nonceHolder, "ValueSetUnderNonce") - .withArgs(systemAccount.address, expectedNonce.add(1), 333); - const key = await nonceHolder.getMinNonce(systemAccount.address); - const storedValue = await nonceHolder.connect(systemAccount).getValueUnderNonce(key); - expect(storedValue).to.equal(333); + .withArgs(systemAccount.address, currentNonce, value); + + const valueAfter = await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce); + expect(valueAfter).to.equal(value); }); it("should emit ValueSetUnderNonce event arbitrary ordering", async () => { const currentNonce = await nonceHolder.getMinNonce(systemAccount.address); - await nonceHolder.connect(systemAccount).incrementMinNonceIfEquals(currentNonce); const encodedAccountInfo = ethers.utils.defaultAbiCoder.encode(["tuple(uint8, uint8)"], [[1, 1]]); await setResult("ContractDeployer", "getAccountInfo", [systemAccount.address], { failure: false, returnData: encodedAccountInfo, }); - await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce.add(1), 111)) + const firstValue = (await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce)).add(111) + await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce, firstValue)) .to.emit(nonceHolder, "ValueSetUnderNonce") - .withArgs(systemAccount.address, currentNonce.add(1), 111); + .withArgs(systemAccount.address, currentNonce, firstValue); - await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce.add(3), 333)) + const secondValue = (await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce.add(2))).add(333) + await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce.add(2), secondValue)) .to.emit(nonceHolder, "ValueSetUnderNonce") - .withArgs(systemAccount.address, currentNonce.add(3), 333); + .withArgs(systemAccount.address, currentNonce.add(2), secondValue); - await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce.add(2), 222)) + const thirdValue = (await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce.add(1))).add(222) + await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce.add(1), thirdValue)) .to.emit(nonceHolder, "ValueSetUnderNonce") - .withArgs(systemAccount.address, currentNonce.add(2), 222); + .withArgs(systemAccount.address, currentNonce.add(1), thirdValue); + + const storedValue = await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce); + expect(storedValue).to.equal(firstValue); + const storedValueNext = await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce.add(1)); + expect(storedValueNext).to.equal(thirdValue); + const storedAfterNext = await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce.add(2)); + expect(storedAfterNext).to.equal(secondValue); - const key = await nonceHolder.getMinNonce(systemAccount.address); - const storedValue = await nonceHolder.connect(systemAccount).getValueUnderNonce(key); - expect(storedValue).to.equal(111); - const storedValueNext = await nonceHolder.connect(systemAccount).getValueUnderNonce(key.add(1)); - expect(storedValueNext).to.equal(222); }); }); From 0cc124d1aab5b05692df1495b33d2af986bb6987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Zaj=C4=85c?= Date: Mon, 13 May 2024 10:51:01 +0000 Subject: [PATCH 13/13] fixed linter hiccup --- system-contracts/test/NonceHolder.spec.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/system-contracts/test/NonceHolder.spec.ts b/system-contracts/test/NonceHolder.spec.ts index c3e9e4583..91e9e3ba4 100644 --- a/system-contracts/test/NonceHolder.spec.ts +++ b/system-contracts/test/NonceHolder.spec.ts @@ -185,17 +185,17 @@ describe("NonceHolder tests", () => { returnData: encodedAccountInfo, }); - const firstValue = (await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce)).add(111) + const firstValue = (await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce)).add(111); await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce, firstValue)) .to.emit(nonceHolder, "ValueSetUnderNonce") .withArgs(systemAccount.address, currentNonce, firstValue); - const secondValue = (await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce.add(2))).add(333) + const secondValue = (await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce.add(2))).add(333); await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce.add(2), secondValue)) .to.emit(nonceHolder, "ValueSetUnderNonce") .withArgs(systemAccount.address, currentNonce.add(2), secondValue); - const thirdValue = (await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce.add(1))).add(222) + const thirdValue = (await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce.add(1))).add(222); await expect(nonceHolder.connect(systemAccount).setValueUnderNonce(currentNonce.add(1), thirdValue)) .to.emit(nonceHolder, "ValueSetUnderNonce") .withArgs(systemAccount.address, currentNonce.add(1), thirdValue); @@ -206,7 +206,6 @@ describe("NonceHolder tests", () => { expect(storedValueNext).to.equal(thirdValue); const storedAfterNext = await nonceHolder.connect(systemAccount).getValueUnderNonce(currentNonce.add(2)); expect(storedAfterNext).to.equal(secondValue); - }); });