From 19de69463d1c5ba6c9854f5946a3ac5aabecf115 Mon Sep 17 00:00:00 2001 From: Roger <50648015+RogerLamTd@users.noreply.github.com> Date: Sun, 18 Dec 2022 11:49:43 +0800 Subject: [PATCH] refactor(test): Refactoring fixture() pattern to before/beforeEach pattern for tests (#451) * refactor libbridgedata tests * fixed bug where since describe block is async, mocha doesn't run tests * converted all bridge related tests from fixture to before/beforeEach aside from Bridge.test.ts * refactored L1 unit tests from fixtures to before/beforeEach * refactor remaining L2, thirdparty test fixtures to before/beforeEach * simplify * Apply suggestions from code review Co-authored-by: David <104078303+davidtaikocha@users.noreply.github.com> * fix Co-authored-by: David <104078303+davidtaikocha@users.noreply.github.com> --- packages/protocol/test/ConfigManager.test.ts | 7 +- packages/protocol/test/L1/TaikoL1.test.ts | 36 +++-- packages/protocol/test/L1/TkoToken.test.ts | 62 +++------ packages/protocol/test/L2/TaikoL2.test.ts | 26 ++-- .../protocol/test/bridge/BridgedERC20.test.ts | 131 ++++++------------ .../protocol/test/bridge/EtherVault.test.ts | 63 ++------- .../protocol/test/bridge/TokenVault.test.ts | 85 ++++-------- .../test/bridge/libs/LibBridgeData.test.ts | 46 +++--- .../test/bridge/libs/LibBridgeInvoke.test.ts | 42 ++---- .../test/bridge/libs/LibBridgeProcess.test.ts | 77 ++++------ .../test/bridge/libs/LibBridgeRetry.test.ts | 58 +++----- .../test/bridge/libs/LibBridgeSend.test.ts | 62 +++------ .../test/bridge/libs/LibBridgeSignal.test.ts | 46 +++--- .../test/thirdparty/AddressManager.test.ts | 26 ++-- .../test/thirdparty/LibMerkleTrie.test.ts | 53 ++----- .../thirdparty/LibSecureMerkleTrie.test.ts | 54 ++------ 16 files changed, 278 insertions(+), 596 deletions(-) diff --git a/packages/protocol/test/ConfigManager.test.ts b/packages/protocol/test/ConfigManager.test.ts index 40a6125690..64ad39de07 100644 --- a/packages/protocol/test/ConfigManager.test.ts +++ b/packages/protocol/test/ConfigManager.test.ts @@ -1,11 +1,12 @@ import { expect } from "chai" +import { ConfigManager } from "../typechain" const hre = require("hardhat") const ethers = hre.ethers describe("ConfigManager", function () { - let configManager: any - let testKey: any - let testName: any + let configManager: ConfigManager + let testKey: string + let testName: string before(async function () { configManager = await ( diff --git a/packages/protocol/test/L1/TaikoL1.test.ts b/packages/protocol/test/L1/TaikoL1.test.ts index 598dd4568e..4a723279f0 100644 --- a/packages/protocol/test/L1/TaikoL1.test.ts +++ b/packages/protocol/test/L1/TaikoL1.test.ts @@ -1,9 +1,12 @@ import { expect } from "chai" import { ethers } from "hardhat" +import { TaikoL1 } from "../../typechain" describe("TaikoL1", function () { - async function deployTaikoL1Fixture() { - // Deploying addressManager Contract + let taikoL1: TaikoL1 + let genesisHash: string + + beforeEach(async function () { const addressManager = await ( await ethers.getContractFactory("AddressManager") ).deploy() @@ -39,24 +42,21 @@ describe("TaikoL1", function () { await ethers.getContractFactory("V1Verifying") ).deploy() - const TaikoL1Factory = await ethers.getContractFactory("TaikoL1", { - libraries: { - V1Verifying: v1Verifying.address, - V1Proposing: v1Proposing.address, - V1Proving: v1Proving.address, - }, - }) - - const genesisHash = randomBytes32() - const taikoL1 = await TaikoL1Factory.deploy() + genesisHash = randomBytes32() + taikoL1 = await ( + await ethers.getContractFactory("TaikoL1", { + libraries: { + V1Verifying: v1Verifying.address, + V1Proposing: v1Proposing.address, + V1Proving: v1Proving.address, + }, + }) + ).deploy() await taikoL1.init(addressManager.address, genesisHash) - - return { taikoL1, genesisHash } - } + }) describe("getLatestSyncedHeader()", async function () { it("should be genesisHash because no headers have been synced", async function () { - const { taikoL1, genesisHash } = await deployTaikoL1Fixture() const hash = await taikoL1.getLatestSyncedHeader() expect(hash).to.be.eq(genesisHash) }) @@ -64,12 +64,10 @@ describe("TaikoL1", function () { describe("getSyncedHeader()", async function () { it("should revert because header number has not been synced", async function () { - const { taikoL1 } = await deployTaikoL1Fixture() await expect(taikoL1.getSyncedHeader(1)).to.be.revertedWith("L1:id") }) it("should return appropraite hash for header", async function () { - const { taikoL1, genesisHash } = await deployTaikoL1Fixture() const hash = await taikoL1.getSyncedHeader(0) expect(hash).to.be.eq(genesisHash) }) @@ -77,8 +75,6 @@ describe("TaikoL1", function () { describe("getBlockProvers()", async function () { it("should return empty list when there is no proof for that block", async function () { - const { taikoL1 } = await deployTaikoL1Fixture() - const provers = await taikoL1.getBlockProvers( Math.ceil(Math.random() * 1024), randomBytes32() diff --git a/packages/protocol/test/L1/TkoToken.test.ts b/packages/protocol/test/L1/TkoToken.test.ts index ddc8e87a05..f7dfe55c13 100644 --- a/packages/protocol/test/L1/TkoToken.test.ts +++ b/packages/protocol/test/L1/TkoToken.test.ts @@ -6,21 +6,28 @@ import { ERC20_BURN_AMOUNT_EXCEEDED, ERC20_TRANSFER_AMOUNT_EXCEEDED, } from "../constants/errors" +import { BigNumber } from "ethers" describe("TokenVault", function () { - async function deployTkoTokenFixture() { - const [owner, nonOwner, protoBroker] = await ethers.getSigners() + let owner: any + let nonOwner: any + let protoBroker: any + let token: TkoToken + let amountMinted: BigNumber + + before(async function () { + ;[owner, nonOwner, protoBroker] = await ethers.getSigners() + }) - // Deploying addressManager Contract + beforeEach(async function () { const addressManager: AddressManager = await ( await ethers.getContractFactory("AddressManager") ).deploy() await addressManager.init() - const TkoTokenFactory = await ethers.getContractFactory("TkoToken") - - const token: TkoToken = await TkoTokenFactory.connect(owner).deploy() - + token = await (await ethers.getContractFactory("TkoToken")) + .connect(owner) + .deploy() await token.init(addressManager.address) const { chainId } = await ethers.provider.getNetwork() @@ -29,44 +36,28 @@ describe("TokenVault", function () { `${chainId}.proto_broker`, protoBroker.address ) - const amountMinted = ethers.utils.parseEther("100") + + amountMinted = ethers.utils.parseEther("100") await token.connect(protoBroker).mint(owner.address, amountMinted) const ownerBalance = await token.balanceOf(owner.address) expect(ownerBalance).to.be.eq(amountMinted) - - return { - owner, - nonOwner, - token, - addressManager, - amountMinted, - protoBroker, - } - } + }) describe("mint()", async () => { it("throws when to is equal to the zero address", async () => { - const { token, protoBroker } = await deployTkoTokenFixture() - await expect( token.connect(protoBroker).mint(ethers.constants.AddressZero, 1) ).to.be.revertedWith("TKO: invalid address") }) it("throws when minter is not the protoBroker", async () => { - const { owner, token, amountMinted, nonOwner } = - await deployTkoTokenFixture() - await expect( token.connect(owner).mint(nonOwner.address, amountMinted.add(1)) ).to.be.revertedWith(ADDRESS_RESOLVER_DENIED) }) it("succeeds", async () => { - const { token, amountMinted, nonOwner, protoBroker } = - await deployTkoTokenFixture() - const originalBalance = await token.balanceOf(nonOwner.address) await token @@ -82,26 +73,18 @@ describe("TokenVault", function () { describe("burn()", async () => { it("throws when to is equal to the zero address", async () => { - const { token, protoBroker } = await deployTkoTokenFixture() - await expect( token.connect(protoBroker).burn(ethers.constants.AddressZero, 1) ).to.be.revertedWith("TKO: invalid address") }) it("throws when burner is not the protoBroker", async () => { - const { owner, token, amountMinted, nonOwner } = - await deployTkoTokenFixture() - await expect( token.connect(owner).burn(nonOwner.address, amountMinted.add(1)) ).to.be.revertedWith(ADDRESS_RESOLVER_DENIED) }) it("throws when account balance is < amount requested to burn", async () => { - const { owner, protoBroker, token, amountMinted } = - await deployTkoTokenFixture() - await expect( token .connect(protoBroker) @@ -110,9 +93,6 @@ describe("TokenVault", function () { }) it("succeeds", async () => { - const { token, amountMinted, owner, protoBroker } = - await deployTkoTokenFixture() - const originalBalance = await token.balanceOf(owner.address) await token.connect(protoBroker).burn(owner.address, amountMinted) @@ -126,17 +106,12 @@ describe("TokenVault", function () { describe("transfer()", async () => { it("throws when to is equal to the contract address", async () => { - const { owner, token } = await deployTkoTokenFixture() - await expect( token.connect(owner).transfer(token.address, 1) ).to.be.revertedWith("TKO: invalid to") }) it("throws when transfer is > user's amount", async () => { - const { owner, token, amountMinted, nonOwner } = - await deployTkoTokenFixture() - await expect( token .connect(owner) @@ -145,9 +120,6 @@ describe("TokenVault", function () { }) it("succeeds", async () => { - const { owner, token, amountMinted, nonOwner } = - await deployTkoTokenFixture() - const originalBalance = await token.balanceOf(nonOwner.address) await token.connect(owner).transfer(nonOwner.address, amountMinted) diff --git a/packages/protocol/test/L2/TaikoL2.test.ts b/packages/protocol/test/L2/TaikoL2.test.ts index c4c1491bf5..f6186697db 100644 --- a/packages/protocol/test/L2/TaikoL2.test.ts +++ b/packages/protocol/test/L2/TaikoL2.test.ts @@ -1,9 +1,11 @@ import { expect } from "chai" import { ethers } from "hardhat" +import { TaikoL2 } from "../../typechain" describe("TaikoL2", function () { - async function deployTaikoL2Fixture() { - // Deploying addressManager Contract + let taikoL2: TaikoL2 + + beforeEach(async function () { const addressManager = await ( await ethers.getContractFactory("AddressManager") ).deploy() @@ -14,19 +16,17 @@ describe("TaikoL2", function () { await ethers.getContractFactory("LibTxDecoder") ).deploy() - const taikoL2Factory = await ethers.getContractFactory("TaikoL2", { - libraries: { - LibTxDecoder: libTxDecoder.address, - }, - }) - const taikoL2 = await taikoL2Factory.deploy(addressManager.address) - - return { taikoL2 } - } + taikoL2 = await ( + await ethers.getContractFactory("TaikoL2", { + libraries: { + LibTxDecoder: libTxDecoder.address, + }, + }) + ).deploy(addressManager.address) + }) describe("anchor()", async function () { it("should revert since ancestor hashes not written", async function () { - const { taikoL2 } = await deployTaikoL2Fixture() await expect( taikoL2.anchor(Math.ceil(Math.random() * 1024), randomBytes32()) ).to.be.revertedWith("L2:publicInputHash") @@ -35,7 +35,6 @@ describe("TaikoL2", function () { describe("getLatestSyncedHeader()", async function () { it("should be 0 because no headers have been synced", async function () { - const { taikoL2 } = await deployTaikoL2Fixture() const hash = await taikoL2.getLatestSyncedHeader() expect(hash).to.be.eq(ethers.constants.HashZero) }) @@ -43,7 +42,6 @@ describe("TaikoL2", function () { describe("getSyncedHeader()", async function () { it("should be 0 because header number has not been synced", async function () { - const { taikoL2 } = await deployTaikoL2Fixture() const hash = await taikoL2.getSyncedHeader(1) expect(hash).to.be.eq(ethers.constants.HashZero) }) diff --git a/packages/protocol/test/bridge/BridgedERC20.test.ts b/packages/protocol/test/bridge/BridgedERC20.test.ts index 47bedca9c1..f809f45cd5 100644 --- a/packages/protocol/test/bridge/BridgedERC20.test.ts +++ b/packages/protocol/test/bridge/BridgedERC20.test.ts @@ -11,37 +11,33 @@ import { const WETH_GOERLI = "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6" const CHAIN_ID_GOERLI = 5 describe("BridgedERC20", function () { - async function deployUninitializedBridgedERC20Fixture() { - const [owner, nonOwner] = await ethers.getSigners() + let owner: any + let tokenVault: any + let accountWithTokens: any - // Deploying addressManager Contract - const addressManager: AddressManager = await ( - await ethers.getContractFactory("AddressManager") - ).deploy() - await addressManager.init() + // uninitialized BridgedERC20 for testing + let unInitAddressManager: AddressManager + let unInitERC20: BridgedERC20 - const BridgedERC20Factory = await ethers.getContractFactory( - "BridgedERC20" - ) + // properly initialized BridgedERC20 for testing + let addressManager: AddressManager + let erc20: BridgedERC20 - const erc20: BridgedERC20 = await BridgedERC20Factory.connect( - owner - ).deploy() + before(async function () { + ;[owner, tokenVault, accountWithTokens] = await ethers.getSigners() + }) - return { - owner, - nonOwner, - addressManager, - erc20, - } - } + beforeEach(async function () { + unInitAddressManager = await ( + await ethers.getContractFactory("AddressManager") + ).deploy() + await unInitAddressManager.init() - async function deployBridgedERC20Fixture() { - const [owner, tokenVault, nonOwner, accountWithTokens] = - await ethers.getSigners() + unInitERC20 = await (await ethers.getContractFactory("BridgedERC20")) + .connect(owner) + .deploy() - // Deploying addressManager Contract - const addressManager: AddressManager = await ( + addressManager = await ( await ethers.getContractFactory("AddressManager") ).deploy() await addressManager.init() @@ -53,13 +49,9 @@ describe("BridgedERC20", function () { tokenVault.address ) - const BridgedERC20Factory = await ethers.getContractFactory( - "BridgedERC20" - ) - - const erc20: BridgedERC20 = await BridgedERC20Factory.connect( - owner - ).deploy() + erc20 = await (await ethers.getContractFactory("BridgedERC20")) + .connect(owner) + .deploy() await erc20 .connect(owner) @@ -78,27 +70,15 @@ describe("BridgedERC20", function () { accountWithTokens.address, ethers.utils.parseEther("1.0") ) - - return { - owner, - nonOwner, - addressManager, - erc20, - tokenVault, - accountWithTokens, - } - } + }) describe("init()", function () { it("inits when srctoken is not 0, srcChainId is not 0, srcChainId is not the current blocks chain id, symbol is not 0 length, name is not 0 length", async () => { - const { owner, erc20, addressManager } = - await deployUninitializedBridgedERC20Fixture() - await expect( - erc20 + unInitERC20 .connect(owner) .init( - addressManager.address, + unInitAddressManager.address, WETH_GOERLI, CHAIN_ID_GOERLI, 18, @@ -107,15 +87,13 @@ describe("BridgedERC20", function () { ) ).not.to.be.revertedWith("BE:params") }) - it("throws when _srcToken is address 0 ", async () => { - const { owner, erc20, addressManager } = - await deployUninitializedBridgedERC20Fixture() + it("throws when _srcToken is address 0 ", async () => { await expect( - erc20 + unInitERC20 .connect(owner) .init( - addressManager.address, + unInitAddressManager.address, ethers.constants.AddressZero, CHAIN_ID_GOERLI, 18, @@ -126,14 +104,11 @@ describe("BridgedERC20", function () { }) it("throws when _srcChainId is 0", async () => { - const { owner, erc20, addressManager } = - await deployUninitializedBridgedERC20Fixture() - await expect( - erc20 + unInitERC20 .connect(owner) .init( - addressManager.address, + unInitAddressManager.address, WETH_GOERLI, 0, 18, @@ -144,14 +119,11 @@ describe("BridgedERC20", function () { }) it("throws when _symbol is 0 length", async () => { - const { owner, erc20, addressManager } = - await deployUninitializedBridgedERC20Fixture() - await expect( - erc20 + unInitERC20 .connect(owner) .init( - addressManager.address, + unInitAddressManager.address, WETH_GOERLI, CHAIN_ID_GOERLI, 18, @@ -162,14 +134,11 @@ describe("BridgedERC20", function () { }) it("throws when _name is 0 length", async () => { - const { owner, erc20, addressManager } = - await deployUninitializedBridgedERC20Fixture() - await expect( - erc20 + unInitERC20 .connect(owner) .init( - addressManager.address, + unInitAddressManager.address, WETH_GOERLI, CHAIN_ID_GOERLI, 18, @@ -180,15 +149,12 @@ describe("BridgedERC20", function () { }) it("throws when _srcChainId is equal to block.chainid", async () => { - const { owner, erc20, addressManager } = - await deployUninitializedBridgedERC20Fixture() - const network = await ethers.provider.getNetwork() await expect( - erc20 + unInitERC20 .connect(owner) .init( - addressManager.address, + unInitAddressManager.address, WETH_GOERLI, network.chainId, 18, @@ -201,8 +167,6 @@ describe("BridgedERC20", function () { describe("source()", function () { it("returns srcToken and srcChainId", async () => { - const { erc20 } = await deployBridgedERC20Fixture() - const [srcToken, srcChainId] = await erc20.source() expect(srcToken).to.be.eq(WETH_GOERLI) @@ -212,7 +176,6 @@ describe("BridgedERC20", function () { describe("bridgeMintTo()", function () { it("throws when not called by token_vault", async () => { - const { owner, erc20 } = await deployBridgedERC20Fixture() const amount = BigNumber.from(1) await expect( erc20.bridgeMintTo(owner.address, amount) @@ -220,8 +183,6 @@ describe("BridgedERC20", function () { }) it("successfully mintes and emits BridgeMint when called by token_vault, balance inceases for account specified, burns and emits BridgeBurn", async () => { - const { owner, erc20, tokenVault } = - await deployBridgedERC20Fixture() const amount = BigNumber.from(150) const initialBalance = await erc20.balanceOf(owner.address) @@ -252,7 +213,6 @@ describe("BridgedERC20", function () { describe("bridgeBurnFrom()", function () { it("throws when not called by token_vault", async () => { - const { owner, erc20 } = await deployBridgedERC20Fixture() const amount = BigNumber.from(1) await expect( erc20.bridgeBurnFrom(owner.address, amount) @@ -260,9 +220,6 @@ describe("BridgedERC20", function () { }) it("can not burn an amount greater than was minted", async () => { - const { accountWithTokens, erc20, tokenVault } = - await deployBridgedERC20Fixture() - const initialBalance = await erc20.balanceOf( accountWithTokens.address ) @@ -280,9 +237,6 @@ describe("BridgedERC20", function () { describe("transferFrom()", function () { it("throws when trying to transfer to itself", async () => { - const { accountWithTokens, erc20 } = - await deployBridgedERC20Fixture() - await expect( erc20 .connect(accountWithTokens) @@ -293,18 +247,12 @@ describe("BridgedERC20", function () { describe("transfer()", function () { it("throws when trying to transfer to itself", async () => { - const { accountWithTokens, erc20 } = - await deployBridgedERC20Fixture() - await expect( erc20.connect(accountWithTokens).transfer(erc20.address, 1) ).to.be.revertedWith("BE:to") }) it("throws when trying to transfer amount greater than holder owns", async () => { - const { accountWithTokens, owner, erc20 } = - await deployBridgedERC20Fixture() - const initialBalance = await erc20.balanceOf( accountWithTokens.address ) @@ -315,9 +263,8 @@ describe("BridgedERC20", function () { .transfer(owner.address, initialBalance.add(1)) ).to.be.revertedWith(ERC20_TRANSFER_AMOUNT_EXCEEDED) }) + it("transfers, emits Transfer event, balances are correct after transfer", async () => { - const { accountWithTokens, owner, erc20 } = - await deployBridgedERC20Fixture() const initialRecipientBalance = await erc20.balanceOf(owner.address) const initialAccountWithTokensBalance = await erc20.balanceOf( accountWithTokens.address diff --git a/packages/protocol/test/bridge/EtherVault.test.ts b/packages/protocol/test/bridge/EtherVault.test.ts index 9103ba9ee8..91bad88ddc 100644 --- a/packages/protocol/test/bridge/EtherVault.test.ts +++ b/packages/protocol/test/bridge/EtherVault.test.ts @@ -4,25 +4,26 @@ import { ethers } from "hardhat" import { BigNumber } from "ethers" describe("EtherVault", function () { - async function deployEtherVaultFixture() { - const [owner, nonOwner, authorized, notAuthorized] = + let owner: any + let nonOwner: any + let authorized: any + let notAuthorized: any + let etherVault: EtherVault + + before(async function () { + ;[owner, nonOwner, authorized, notAuthorized] = await ethers.getSigners() + }) - // Deploying addressManager Contract + beforeEach(async function () { const addressManager: AddressManager = await ( await ethers.getContractFactory("AddressManager") ).deploy() await addressManager.init() - // todo: should use EtherVault__factory typing to infer the returned - // EtherVault type on creation, but eslint doesnt allow - // non-camel case in this repo. - const EtherVaultFactory = await ethers.getContractFactory("EtherVault") - - const etherVault: EtherVault = await EtherVaultFactory.connect( - owner - ).deploy() - + etherVault = await (await ethers.getContractFactory("EtherVault")) + .connect(owner) + .deploy() await etherVault.init(addressManager.address) await etherVault.connect(owner).authorize(authorized.address, true) @@ -38,22 +39,10 @@ describe("EtherVault", function () { expect(await ethers.provider.getBalance(etherVault.address)).to.be.eq( ethers.utils.parseEther("1.0") ) - - return { - etherVault, - owner, - nonOwner, - authorized, - notAuthorized, - EtherVaultFactory, - } - } + }) describe("receive()", async function () { it("throws if not authorized and balance > 0", async () => { - const { notAuthorized, etherVault } = - await deployEtherVaultFixture() - const balance = await ethers.provider.getBalance(etherVault.address) expect(balance).to.not.be.eq(BigNumber.from(0)) await expect( @@ -65,8 +54,6 @@ describe("EtherVault", function () { }) it("receives if authorized and balance > 0", async () => { - const { authorized, etherVault } = await deployEtherVaultFixture() - const amount = BigNumber.from(1) const originalBalance = await ethers.provider.getBalance( etherVault.address @@ -82,10 +69,9 @@ describe("EtherVault", function () { expect(newBalance).to.be.eq(amount.add(originalBalance)) }) }) + describe("receiveEther()", async function () { it("throws if not enough ether to send", async () => { - const { authorized, etherVault } = await deployEtherVaultFixture() - const balance = await ethers.provider.getBalance(etherVault.address) const additionalAmount = 1 await expect( @@ -96,17 +82,12 @@ describe("EtherVault", function () { }) it("throws if not authorized", async () => { - const { notAuthorized, etherVault } = - await deployEtherVaultFixture() - await expect( etherVault.connect(notAuthorized).receiveEther(1) ).to.be.revertedWith("EV:denied") }) it("sends ether to caller", async () => { - const { authorized, etherVault } = await deployEtherVaultFixture() - const amount = 100000 const originalBalance = await ethers.provider.getBalance( authorized.address @@ -129,9 +110,6 @@ describe("EtherVault", function () { describe("authorize()", async function () { it("throws when not called by owner", async () => { - const { nonOwner, notAuthorized, etherVault } = - await deployEtherVaultFixture() - await expect( etherVault .connect(nonOwner) @@ -140,8 +118,6 @@ describe("EtherVault", function () { }) it("throws when address is 0", async () => { - const { owner, etherVault } = await deployEtherVaultFixture() - await expect( etherVault .connect(owner) @@ -150,18 +126,12 @@ describe("EtherVault", function () { }) it("throws when authorized state is the same as input", async () => { - const { owner, authorized, etherVault } = - await deployEtherVaultFixture() - await expect( etherVault.connect(owner).authorize(authorized.address, true) ).to.be.revertedWith("EV:param") }) it("emits Authorized event upon success", async () => { - const { owner, notAuthorized, etherVault } = - await deployEtherVaultFixture() - await expect( etherVault.connect(owner).authorize(notAuthorized.address, true) ) @@ -170,9 +140,6 @@ describe("EtherVault", function () { }) it("address is authorized in mapping, can de-authorize", async () => { - const { owner, notAuthorized, etherVault } = - await deployEtherVaultFixture() - await etherVault .connect(owner) .authorize(notAuthorized.address, true) diff --git a/packages/protocol/test/bridge/TokenVault.test.ts b/packages/protocol/test/bridge/TokenVault.test.ts index 8522562524..f256c65383 100644 --- a/packages/protocol/test/bridge/TokenVault.test.ts +++ b/packages/protocol/test/bridge/TokenVault.test.ts @@ -21,20 +21,25 @@ const weth: CanonicalERC20 = { } describe("TokenVault", function () { - async function deployTokenVaultFixture() { - const [owner, nonOwner] = await ethers.getSigners() + let owner: any + let nonOwner: any + let tokenVault: TokenVault + const defaultProcessingFee = 10 + const destChainId = 167001 + + before(async function () { + ;[owner, nonOwner] = await ethers.getSigners() + }) - // Deploying addressManager Contract + beforeEach(async function () { const tokenVaultAddressManager: AddressManager = await ( await ethers.getContractFactory("AddressManager") ).deploy() await tokenVaultAddressManager.init() - const TokenVaultFactory = await ethers.getContractFactory("TokenVault") - - const tokenVault: TokenVault = await TokenVaultFactory.connect( - owner - ).deploy() + tokenVault = await (await ethers.getContractFactory("TokenVault")) + .connect(owner) + .deploy() await tokenVault.init(tokenVaultAddressManager.address) @@ -50,18 +55,10 @@ describe("TokenVault", function () { `${network.chainId}.bridge`, testMessageSender.address ) - return { - owner, - nonOwner, - tokenVault, - tokenVaultAddressManager, - } - } + }) describe("receiveERC20()", async () => { it("throws when named 'bridge' is not the caller", async () => { - const { owner, nonOwner, tokenVault } = - await deployTokenVaultFixture() const amount = BigNumber.from(1) await expect( @@ -77,16 +74,12 @@ describe("TokenVault", function () { describe("sendEther()", async () => { it("throws when msg.value is 0", async () => { - const { owner, tokenVault } = await deployTokenVaultFixture() - - const processingFee = 10 - await expect( tokenVault.sendEther( - 167001, + destChainId, owner.address, 10000, - processingFee, + defaultProcessingFee, owner.address, "" ) @@ -94,71 +87,55 @@ describe("TokenVault", function () { }) it("throws when msg.value - processing fee is 0", async () => { - const { owner, tokenVault } = await deployTokenVaultFixture() - - const processingFee = 10 - await expect( tokenVault.sendEther( - 167001, + destChainId, owner.address, 10000, - processingFee, + defaultProcessingFee, owner.address, "", { - value: processingFee, + value: defaultProcessingFee, } ) ).to.be.revertedWith("V:msgValue") }) it("throws when msg.value is < processingFee", async () => { - const { owner, tokenVault } = await deployTokenVaultFixture() - - const processingFee = 10 - await expect( tokenVault.sendEther( - 167001, + destChainId, owner.address, 10000, - processingFee, + defaultProcessingFee, owner.address, "", { - value: processingFee - 1, + value: defaultProcessingFee - 1, } ) ).to.be.revertedWith("V:msgValue") }) it("throws when to is 0", async () => { - const { owner, tokenVault } = await deployTokenVaultFixture() - - const processingFee = 10 - await expect( tokenVault.sendEther( - 167001, + destChainId, ethers.constants.AddressZero, 10000, - processingFee, + defaultProcessingFee, owner.address, "", { - value: processingFee - 1, + value: defaultProcessingFee - 1, } ) ).to.be.revertedWith("V:to") }) it("succeeds with processingFee", async () => { - const { owner, tokenVault } = await deployTokenVaultFixture() - - const processingFee = 10 const depositValue = 1000 - const destChainId = 167001 const testSignal = "0x3fd54831f488a22b28398de0c567a3b064b937f54f81739ae9bd545967f3abab" @@ -168,7 +145,7 @@ describe("TokenVault", function () { destChainId, owner.address, 10000, - processingFee, + defaultProcessingFee, owner.address, "", { @@ -180,17 +157,13 @@ describe("TokenVault", function () { .withArgs( owner.address, destChainId, - depositValue - processingFee, + depositValue - defaultProcessingFee, testSignal ) }) it("succeeds with 0 processingFee", async () => { - const { owner, tokenVault } = await deployTokenVaultFixture() - - const processingFee = 0 const depositValue = 1000 - const destChainId = 167001 const testSignal = "0x3fd54831f488a22b28398de0c567a3b064b937f54f81739ae9bd545967f3abab" @@ -200,7 +173,7 @@ describe("TokenVault", function () { destChainId, owner.address, 10000, - processingFee, + defaultProcessingFee, owner.address, "", { @@ -212,7 +185,7 @@ describe("TokenVault", function () { .withArgs( owner.address, destChainId, - depositValue - processingFee, + depositValue - defaultProcessingFee, testSignal ) }) diff --git a/packages/protocol/test/bridge/libs/LibBridgeData.test.ts b/packages/protocol/test/bridge/libs/LibBridgeData.test.ts index 7da0b4791e..38bdf75dc5 100644 --- a/packages/protocol/test/bridge/libs/LibBridgeData.test.ts +++ b/packages/protocol/test/bridge/libs/LibBridgeData.test.ts @@ -6,14 +6,17 @@ import { MessageStatus } from "../../../tasks/utils" import { Message } from "../../utils/message" describe("LibBridgeData", function () { - async function deployLibBridgeDataFixture() { - const [owner, nonOwner] = await ethers.getSigners() + let owner: any + let nonOwner: any + let testMessage: Message + let testTypes: any + let testVar: any + let libData: TestLibBridgeData - const libData: TestLibBridgeData = await ( - await ethers.getContractFactory("TestLibBridgeData") - ).deploy() + before(async function () { + ;[owner, nonOwner] = await ethers.getSigners() - const testMessage: Message = { + testMessage = { id: 1, sender: owner.address, srcChainId: 1, @@ -28,30 +31,22 @@ describe("LibBridgeData", function () { data: ethers.constants.HashZero, memo: "", } - - const testTypes = [ + testTypes = [ "string", "tuple(uint256 id, address sender, uint256 srcChainId, uint256 destChainId, address owner, address to, address refundAddress, uint256 depositValue, uint256 callValue, uint256 processingFee, uint256 gasLimit, bytes data, string memo)", ] - const testVar = [K_BRIDGE_MESSAGE, testMessage] + testVar = [K_BRIDGE_MESSAGE, testMessage] + }) - return { - owner, - nonOwner, - libData, - testMessage, - testTypes, - testVar, - } - } + beforeEach(async function () { + libData = await ( + await ethers.getContractFactory("TestLibBridgeData") + ).deploy() + }) describe("hashMessage()", async function () { it("should return properly hashed message", async function () { - const { libData, testMessage, testTypes } = - await deployLibBridgeDataFixture() - - const testVar = [K_BRIDGE_MESSAGE, testMessage] const hashed = await libData.hashMessage(testMessage) const expectedEncoded = ethers.utils.defaultAbiCoder.encode( testTypes, @@ -64,9 +59,6 @@ describe("LibBridgeData", function () { }) it("should return properly hashed message from actual bridge message", async function () { - const { libData } = await deployLibBridgeDataFixture() - // dummy struct to test with - const testMessage: Message = { id: 0, sender: "0xDA1Ea1362475997419D2055dD43390AEE34c6c37", @@ -93,8 +85,6 @@ describe("LibBridgeData", function () { describe("updateMessageStatus()", async function () { it("should emit upon successful change, and value should be changed correctly", async function () { - const { libData, testMessage } = await deployLibBridgeDataFixture() - const signal = await libData.hashMessage(testMessage) expect( @@ -107,8 +97,6 @@ describe("LibBridgeData", function () { }) it("unchanged MessageStatus should not emit event", async function () { - const { libData, testMessage } = await deployLibBridgeDataFixture() - const signal = await libData.hashMessage(testMessage) await libData.updateMessageStatus(signal, MessageStatus.NEW) diff --git a/packages/protocol/test/bridge/libs/LibBridgeInvoke.test.ts b/packages/protocol/test/bridge/libs/LibBridgeInvoke.test.ts index eba7340ca9..1ca465d119 100644 --- a/packages/protocol/test/bridge/libs/LibBridgeInvoke.test.ts +++ b/packages/protocol/test/bridge/libs/LibBridgeInvoke.test.ts @@ -8,32 +8,27 @@ import { } from "../../../typechain" describe("LibBridgeInvoke", function () { - async function deployLibBridgeDataFixture() { - const libData: TestLibBridgeData = await ( - await ethers.getContractFactory("TestLibBridgeData") - ).deploy() - return { libData } - } + let owner: any + let nonOwner: any + let libInvoke: TestLibBridgeInvoke + let libData: TestLibBridgeData - async function deployLibBridgeInvokeFixture() { - const [owner, nonOwner] = await ethers.getSigners() + before(async function () { + ;[owner, nonOwner] = await ethers.getSigners() + }) - const libInvoke: TestLibBridgeInvoke = await ( + beforeEach(async function () { + libInvoke = await ( await ethers.getContractFactory("TestLibBridgeInvoke") - ) - .connect(owner) - .deploy() + ).deploy() - return { owner, nonOwner, libInvoke } - } + libData = await ( + await ethers.getContractFactory("TestLibBridgeData") + ).deploy() + }) describe("invokeMessageCall()", async function () { it("should throw when gasLimit <= 0", async function () { - const { owner, nonOwner, libInvoke } = - await deployLibBridgeInvokeFixture() - - const { libData } = await deployLibBridgeDataFixture() - const message: Message = { id: 1, sender: owner.address, @@ -58,11 +53,6 @@ describe("LibBridgeInvoke", function () { }) it("should emit event with success false if message does not actually invoke", async function () { - const { owner, nonOwner, libInvoke } = - await deployLibBridgeInvokeFixture() - - const { libData } = await deployLibBridgeDataFixture() - const message: Message = { id: 1, sender: owner.address, @@ -89,10 +79,6 @@ describe("LibBridgeInvoke", function () { }) it("should emit event with success true if message invokes successfully", async function () { - const { owner, libInvoke } = await deployLibBridgeInvokeFixture() - - const { libData } = await deployLibBridgeDataFixture() - const testReceiver: TestReceiver = await ( await ethers.getContractFactory("TestReceiver") ).deploy() diff --git a/packages/protocol/test/bridge/libs/LibBridgeProcess.test.ts b/packages/protocol/test/bridge/libs/LibBridgeProcess.test.ts index 431028b9ed..1e984c4481 100644 --- a/packages/protocol/test/bridge/libs/LibBridgeProcess.test.ts +++ b/packages/protocol/test/bridge/libs/LibBridgeProcess.test.ts @@ -8,12 +8,11 @@ import { Message } from "../../utils/message" import { AddressManager, EtherVault, - LibTrieProof, TestLibBridgeData, TestLibBridgeProcess, } from "../../../typechain" -describe("LibBridgeProcess", function () { +describe("LibBridgeProcess", async function () { function getStateSlot() { const buildInfoDir = path.join( __dirname, @@ -38,28 +37,30 @@ describe("LibBridgeProcess", function () { throw new Error("TestLibBridgeProcess.state slot number not found") } - async function deployLibBridgeProcessFixture() { - const [owner, nonOwner, etherVaultOwner] = await ethers.getSigners() - - // slot number of IBridge.State for TestLibBridgeProcess. - // mapping destChains is at position 0 - // mapping messageStatus is at position 1 - // nextMessageId is at position 2 - // Context takes up 3 slots, starts at position 3 - const stateSlot = getStateSlot() - - const srcChainId = 1 - - const messageOwner = ethers.Wallet.createRandom() + let owner: any + let nonOwner: any + let etherVaultOwner: any + let addressManager: AddressManager + let etherVault: EtherVault + let libTrieLink + let libProcessLink + let libProcess: TestLibBridgeProcess + let testLibData: TestLibBridgeData + const stateSlot = getStateSlot() + const srcChainId = 1 + const blockChainId = hre.network.config.chainId ?? 0 + + before(async function () { + ;[owner, nonOwner, etherVaultOwner] = await ethers.getSigners() + }) - const addressManager: AddressManager = await ( + beforeEach(async function () { + addressManager = await ( await ethers.getContractFactory("AddressManager") ).deploy() await addressManager.init() - const etherVault: EtherVault = await ( - await ethers.getContractFactory("EtherVault") - ) + etherVault = await (await ethers.getContractFactory("EtherVault")) .connect(etherVaultOwner) .deploy() @@ -79,14 +80,12 @@ describe("LibBridgeProcess", function () { value: ethers.utils.parseEther("10.0"), }) - const libTrieLink: LibTrieProof = await ( - await ethers.getContractFactory("LibTrieProof") - ) + libTrieLink = await (await ethers.getContractFactory("LibTrieProof")) .connect(owner) .deploy() await libTrieLink.deployed() - const libProcessLink = await ( + libProcessLink = await ( await ethers.getContractFactory("LibBridgeProcess", { libraries: { LibTrieProof: libTrieLink.address, @@ -97,7 +96,7 @@ describe("LibBridgeProcess", function () { .deploy() await libProcessLink.deployed() - const libProcess: TestLibBridgeProcess = await ( + libProcess = await ( await ethers.getContractFactory("TestLibBridgeProcess", { libraries: { LibBridgeProcess: libProcessLink.address, @@ -109,31 +108,17 @@ describe("LibBridgeProcess", function () { await libProcess.init(addressManager.address) - const testLibData: TestLibBridgeData = await ( + testLibData = await ( await ethers.getContractFactory("TestLibBridgeData") ).deploy() await etherVault .connect(etherVaultOwner) .authorize(libProcess.address, true) - - return { - owner, - srcChainId, - messageOwner, - libProcess, - stateSlot, - blockChainId, - nonOwner, - testLibData, - addressManager, - } - } + }) describe("processMessage()", async function () { it("should throw if gaslimit == 0 & msg.sender != message.owner", async function () { - const { owner, srcChainId, nonOwner, libProcess, blockChainId } = - await deployLibBridgeProcessFixture() const message: Message = { id: 1, sender: nonOwner.address, @@ -155,8 +140,6 @@ describe("LibBridgeProcess", function () { }) it("should throw if message.destChain != block.chainId", async function () { - const { owner, srcChainId, nonOwner, libProcess, blockChainId } = - await deployLibBridgeProcessFixture() const badBlockChainId = blockChainId + 1 const message: Message = { id: 1, @@ -179,16 +162,6 @@ describe("LibBridgeProcess", function () { }) it("should throw if message's status is not NEW", async function () { - const { - owner, - srcChainId, - nonOwner, - libProcess, - testLibData, - blockChainId, - stateSlot, - } = await deployLibBridgeProcessFixture() - const message: Message = { id: 1, sender: owner.address, diff --git a/packages/protocol/test/bridge/libs/LibBridgeRetry.test.ts b/packages/protocol/test/bridge/libs/LibBridgeRetry.test.ts index a655b4e7c0..f336e097b5 100644 --- a/packages/protocol/test/bridge/libs/LibBridgeRetry.test.ts +++ b/packages/protocol/test/bridge/libs/LibBridgeRetry.test.ts @@ -13,10 +13,21 @@ import { } from "../../../typechain" describe("LibBridgeRetry", function () { - async function deployLibBridgeRetryFixture() { - const [owner, refundAddress, nonOwner, etherVaultOwner] = + let owner: any + let nonOwner: any + let refundAddress: any + let etherVaultOwner: any + let etherVault: EtherVault + let libRetry: TestLibBridgeRetry + let badLibRetry: TestLibBridgeRetry + let testLibData: TestLibBridgeData + + before(async function () { + ;[owner, nonOwner, refundAddress, etherVaultOwner] = await ethers.getSigners() + }) + beforeEach(async function () { const addressManager: AddressManager = await ( await ethers.getContractFactory("AddressManager") ).deploy() @@ -27,9 +38,7 @@ describe("LibBridgeRetry", function () { ).deploy() await badAddressManager.init() - const etherVault: EtherVault = await ( - await ethers.getContractFactory("EtherVault") - ) + etherVault = await (await ethers.getContractFactory("EtherVault")) .connect(etherVaultOwner) .deploy() @@ -67,11 +76,11 @@ describe("LibBridgeRetry", function () { }) ).connect(owner) - const libRetry: TestLibBridgeRetry = await libRetryFactory.deploy() + libRetry = await libRetryFactory.deploy() await libRetry.init(addressManager.address) await libRetry.deployed() - const badLibRetry: TestLibBridgeRetry = await libRetryFactory.deploy() + badLibRetry = await libRetryFactory.deploy() await badLibRetry.init(badAddressManager.address) await badLibRetry.deployed() @@ -79,26 +88,13 @@ describe("LibBridgeRetry", function () { .connect(etherVaultOwner) .authorize(libRetry.address, true) - const testLibData: TestLibBridgeData = await ( + testLibData = await ( await ethers.getContractFactory("TestLibBridgeData") ).deploy() - - return { - owner, - refundAddress, - nonOwner, - libRetry, - testLibData, - badLibRetry, - etherVault, - } - } + }) describe("retryMessage()", async function () { it("should throw if message.gaslimit == 0 && msg.sender != message.owner", async function () { - const { owner, nonOwner, libRetry } = - await deployLibBridgeRetryFixture() - const message: Message = { id: 1, sender: owner.address, @@ -121,9 +117,6 @@ describe("LibBridgeRetry", function () { }) it("should throw if lastAttempt == true && msg.sender != message.owner", async function () { - const { owner, nonOwner, libRetry } = - await deployLibBridgeRetryFixture() - const message: Message = { id: 1, sender: owner.address, @@ -146,9 +139,6 @@ describe("LibBridgeRetry", function () { }) it("should throw if message status is not RETRIABLE", async function () { - const { owner, nonOwner, libRetry } = - await deployLibBridgeRetryFixture() - const message: Message = { id: 1, sender: owner.address, @@ -171,9 +161,6 @@ describe("LibBridgeRetry", function () { }) it("if etherVault resolves to address(0), retry should fail and messageStatus should not change if not lastAttempt since no ether received", async function () { - const { owner, refundAddress, testLibData, badLibRetry } = - await deployLibBridgeRetryFixture() - const testReceiver: TestReceiver = await ( await ethers.getContractFactory("TestReceiver") ).deploy() @@ -223,9 +210,6 @@ describe("LibBridgeRetry", function () { }) it("should fail, but since lastAttempt == true messageStatus should be set to DONE", async function () { - const { owner, libRetry, refundAddress, testLibData } = - await deployLibBridgeRetryFixture() - const testBadReceiver: TestBadReceiver = await ( await ethers.getContractFactory("TestBadReceiver") ).deploy() @@ -278,9 +262,6 @@ describe("LibBridgeRetry", function () { }) it("should fail, messageStatus is still RETRIABLE and balance is returned to etherVault", async function () { - const { owner, libRetry, testLibData, etherVault } = - await deployLibBridgeRetryFixture() - const testBadReceiver: TestBadReceiver = await ( await ethers.getContractFactory("TestBadReceiver") ).deploy() @@ -335,9 +316,6 @@ describe("LibBridgeRetry", function () { }) it("should succeed, set message status to done, invoke message succesfsully", async function () { - const { owner, libRetry, refundAddress, testLibData } = - await deployLibBridgeRetryFixture() - const testReceiver: TestReceiver = await ( await ethers.getContractFactory("TestReceiver") ).deploy() diff --git a/packages/protocol/test/bridge/libs/LibBridgeSend.test.ts b/packages/protocol/test/bridge/libs/LibBridgeSend.test.ts index 23416ecd0a..8a80bf22b1 100644 --- a/packages/protocol/test/bridge/libs/LibBridgeSend.test.ts +++ b/packages/protocol/test/bridge/libs/LibBridgeSend.test.ts @@ -8,9 +8,20 @@ import { import { Message } from "../../utils/message" describe("LibBridgeSend", function () { - async function deployLibBridgeSendFixture() { - const [owner, nonOwner, etherVaultOwner] = await ethers.getSigners() + let owner: any + let nonOwner: any + let etherVaultOwner: any + let libSend: TestLibBridgeSend + let blockChainId: number + const enabledDestChainId = 100 + const srcChainId = 1 + + before(async function () { + ;[owner, nonOwner, etherVaultOwner] = await ethers.getSigners() + blockChainId = hre.network.config.chainId ?? 0 + }) + beforeEach(async function () { const addressManager: AddressManager = await ( await ethers.getContractFactory("AddressManager") ).deploy() @@ -25,15 +36,12 @@ describe("LibBridgeSend", function () { await etherVault.deployed() await etherVault.init(addressManager.address) - const blockChainId = hre.network.config.chainId ?? 0 await addressManager.setAddress( `${blockChainId}.ether_vault`, etherVault.address ) - const libSend: TestLibBridgeSend = await ( - await ethers.getContractFactory("TestLibBridgeSend") - ) + libSend = await (await ethers.getContractFactory("TestLibBridgeSend")) .connect(owner) .deploy() @@ -41,50 +49,30 @@ describe("LibBridgeSend", function () { await etherVault .connect(etherVaultOwner) .authorize(libSend.address, true) + }) - const srcChainId = 1 - - const enabledDestChainId = 100 - - return { owner, nonOwner, libSend, srcChainId, enabledDestChainId } - } describe("enableDestChain()", async function () { it("should throw when chainId <= 0", async function () { - const { libSend } = await deployLibBridgeSendFixture() - await expect(libSend.enableDestChain(0, true)).to.be.revertedWith( "B:chainId" ) }) it("should throw when chainId == block.chainId", async function () { - const { libSend } = await deployLibBridgeSendFixture() - - const blockChainId = hre.network.config.chainId ?? 0 - await expect( libSend.enableDestChain(blockChainId, true) ).to.be.revertedWith("B:chainId") }) it("should emit DestChainEnabled() event", async function () { - const { libSend } = await deployLibBridgeSendFixture() - - let blockChainId = hre.network.config.chainId ?? 0 - blockChainId += 1 - - expect(await libSend.enableDestChain(blockChainId, true)).to.emit( - libSend, - "DestChainEnabled" - ) + expect( + await libSend.enableDestChain(enabledDestChainId, true) + ).to.emit(libSend, "DestChainEnabled") }) }) describe("sendMessage()", async function () { it("should throw when message.owner == address(0)", async function () { - const { owner, nonOwner, libSend, srcChainId } = - await deployLibBridgeSendFixture() - const nonEnabledDestChain = 2 const message: Message = { @@ -109,11 +97,6 @@ describe("LibBridgeSend", function () { }) it("should throw when destchainId == block.chainId", async function () { - const { owner, nonOwner, libSend, srcChainId } = - await deployLibBridgeSendFixture() - - const blockChainId = hre.network.config.chainId ?? 1 - const message: Message = { id: 1, sender: owner.address, @@ -136,9 +119,6 @@ describe("LibBridgeSend", function () { }) it("should throw when destChainId has not yet been enabled", async function () { - const { owner, nonOwner, libSend, srcChainId } = - await deployLibBridgeSendFixture() - const nonEnabledDestChain = 2 const message: Message = { @@ -163,9 +143,6 @@ describe("LibBridgeSend", function () { }) it("should throw when expectedAmount != msg.value", async function () { - const { owner, nonOwner, libSend, srcChainId, enabledDestChainId } = - await deployLibBridgeSendFixture() - await libSend.enableDestChain(enabledDestChainId, true) const message: Message = { @@ -190,9 +167,6 @@ describe("LibBridgeSend", function () { }) it("should emit MessageSent() event and signal should be hashed correctly", async function () { - const { owner, nonOwner, libSend, srcChainId, enabledDestChainId } = - await deployLibBridgeSendFixture() - await libSend.enableDestChain(enabledDestChainId, true) const message: Message = { diff --git a/packages/protocol/test/bridge/libs/LibBridgeSignal.test.ts b/packages/protocol/test/bridge/libs/LibBridgeSignal.test.ts index 7ea337d1f1..7afefb24c6 100644 --- a/packages/protocol/test/bridge/libs/LibBridgeSignal.test.ts +++ b/packages/protocol/test/bridge/libs/LibBridgeSignal.test.ts @@ -3,21 +3,23 @@ import { ethers } from "hardhat" import { TestLibBridgeData, TestLibBridgeSignal } from "../../../typechain" import { Message } from "../../utils/message" -describe("integration:LibBridgeSignal", function () { - async function deployLibBridgeSignalFixture() { - const [owner, nonOwner] = await ethers.getSigners() +describe("LibBridgeSignal", function () { + let owner: any + let nonOwner: any + let testMessage: Message + let libData: TestLibBridgeData + let libSignal: TestLibBridgeSignal - const libSignal: TestLibBridgeSignal = await ( - await ethers.getContractFactory("TestLibBridgeSignal") - ).deploy() + before(async function () { + ;[owner, nonOwner] = await ethers.getSigners() - const testMessage: Message = { + testMessage = { id: 1, sender: owner.address, srcChainId: 1, destChainId: 2, owner: owner.address, - to: owner.address, + to: nonOwner.address, refundAddress: owner.address, depositValue: 0, callValue: 0, @@ -26,23 +28,20 @@ describe("integration:LibBridgeSignal", function () { data: ethers.constants.HashZero, memo: "", } + }) - return { owner, nonOwner, libSignal, testMessage } - } - async function deployLibBridgeDataFixture() { - const libData: TestLibBridgeData = await ( + beforeEach(async function () { + libData = await ( await ethers.getContractFactory("TestLibBridgeData") ).deploy() - return { libData } - } + + libSignal = await ( + await ethers.getContractFactory("TestLibBridgeSignal") + ).deploy() + }) describe("sendSignal()", async function () { it("throws when sender is zero address", async function () { - const { libSignal, testMessage } = - await deployLibBridgeSignalFixture() - - const { libData } = await deployLibBridgeDataFixture() - const signal = await libData.hashMessage(testMessage) await expect( @@ -51,8 +50,6 @@ describe("integration:LibBridgeSignal", function () { }) it("throws when signal is zero", async function () { - const { owner, libSignal } = await deployLibBridgeSignalFixture() - await expect( libSignal.sendSignal(owner.address, ethers.constants.HashZero) ).to.be.revertedWith("B:signal") @@ -60,12 +57,7 @@ describe("integration:LibBridgeSignal", function () { }) describe("isSignalSent()", async function () { - it("properly sent message should be received", async function () { - const { owner, libSignal, testMessage } = - await deployLibBridgeSignalFixture() - - const { libData } = await deployLibBridgeDataFixture() - + it("properly sent signal should change storage value", async function () { const signal = await libData.hashMessage(testMessage) await libSignal.sendSignal(owner.address, signal) diff --git a/packages/protocol/test/thirdparty/AddressManager.test.ts b/packages/protocol/test/thirdparty/AddressManager.test.ts index 2c1e65e945..33d6430ab4 100644 --- a/packages/protocol/test/thirdparty/AddressManager.test.ts +++ b/packages/protocol/test/thirdparty/AddressManager.test.ts @@ -3,26 +3,23 @@ import { AddressManager } from "../../typechain" import { ethers } from "hardhat" describe("AddressManager", function () { - async function deployAddressManagerFixture() { - const [owner, nonOwner] = await ethers.getSigners() + let owner: any + let nonOwner: any + let addressManager: AddressManager - // Deploying addressManager Contract - const addressManager: AddressManager = await ( + before(async function () { + ;[owner, nonOwner] = await ethers.getSigners() + }) + + beforeEach(async function () { + addressManager = await ( await ethers.getContractFactory("AddressManager") ).deploy() await addressManager.init() - return { - owner, - nonOwner, - addressManager, - } - } + }) describe("setAddress()", async () => { it("throws when non-owner calls", async () => { - const { nonOwner, addressManager } = - await deployAddressManagerFixture() - const name = "fakename" await expect( addressManager @@ -32,9 +29,6 @@ describe("AddressManager", function () { }) it("emits setAddress event", async () => { - const { owner, nonOwner, addressManager } = - await deployAddressManagerFixture() - const name = "fakename" await expect( addressManager.connect(owner).setAddress(name, nonOwner.address) diff --git a/packages/protocol/test/thirdparty/LibMerkleTrie.test.ts b/packages/protocol/test/thirdparty/LibMerkleTrie.test.ts index b63e99d169..f55391180d 100644 --- a/packages/protocol/test/thirdparty/LibMerkleTrie.test.ts +++ b/packages/protocol/test/thirdparty/LibMerkleTrie.test.ts @@ -5,45 +5,27 @@ import { TestLibMerkleTrie } from "../../typechain" import { MerkleTrie } from "../utils/trie" import { randomBytes } from "crypto" -const defaultAmountOfNodes = 32 -const defaultNodeLength = 32 describe("LibMerkleTrie", function () { - async function deployLibMerkleTrieFixture( - amountOfNodes: number, - len: number - ) { - const [owner] = await ethers.getSigners() + let libMerkleTrie: TestLibMerkleTrie + let defaultMerkleTrie: MerkleTrie + const defaultAmountOfNodes = 32 + const defaultNodeLength = 32 - const LibMerkleTrieFactory = await ethers.getContractFactory( - "TestLibMerkleTrie" - ) - - const libMerkleTrie: TestLibMerkleTrie = - await LibMerkleTrieFactory.deploy() + beforeEach(async function () { + libMerkleTrie = await ( + await ethers.getContractFactory("TestLibMerkleTrie") + ).deploy() - const defaultMerkleTrie = new MerkleTrie( - amountOfNodes, - len, + defaultMerkleTrie = new MerkleTrie( + defaultAmountOfNodes, + defaultNodeLength, () => new BaseTrie() ) await defaultMerkleTrie.init() - - return { - owner, - libMerkleTrie, - defaultMerkleTrie, - LibMerkleTrieFactory, - } - } + }) describe("verifyInclusionProof()", () => { it(`is included, ${defaultAmountOfNodes} bytes, ${defaultNodeLength} node length`, async () => { - const { libMerkleTrie, defaultMerkleTrie } = - await deployLibMerkleTrieFixture( - defaultAmountOfNodes, - defaultNodeLength - ) - for (const n of defaultMerkleTrie.nodes) { const key = n.key const t = await defaultMerkleTrie.makeTest(key) @@ -60,12 +42,6 @@ describe("LibMerkleTrie", function () { describe("get()", () => { it(`is included`, async () => { - const { libMerkleTrie, defaultMerkleTrie } = - await deployLibMerkleTrieFixture( - defaultAmountOfNodes, - defaultNodeLength - ) - const key = defaultMerkleTrie.nodes[0].key const t = await defaultMerkleTrie.makeTest(key) const isIncluded = await libMerkleTrie.get( @@ -80,11 +56,6 @@ describe("LibMerkleTrie", function () { ) }) it(`is not included`, async () => { - const { libMerkleTrie, defaultMerkleTrie } = - await deployLibMerkleTrieFixture( - defaultAmountOfNodes, - defaultNodeLength - ) const t = await defaultMerkleTrie.makeTest( defaultMerkleTrie.nodes[0].key ) diff --git a/packages/protocol/test/thirdparty/LibSecureMerkleTrie.test.ts b/packages/protocol/test/thirdparty/LibSecureMerkleTrie.test.ts index df2092d14f..41e5979e6c 100644 --- a/packages/protocol/test/thirdparty/LibSecureMerkleTrie.test.ts +++ b/packages/protocol/test/thirdparty/LibSecureMerkleTrie.test.ts @@ -5,45 +5,27 @@ import { SecureTrie } from "merkle-patricia-tree" import { TestLibSecureMerkleTrie } from "../../typechain" import { MerkleTrie } from "../utils/trie" -const defaultAmountOfNodes = 32 -const defaultNodeLength = 32 describe("LibSecureMerkleTrie", function () { - async function deploylibSecureMerkleTrieFixture( - amountOfNodes: number, - len: number - ) { - const [owner] = await ethers.getSigners() + let libSecureMerkleTrie: TestLibSecureMerkleTrie + let defaultSecureMerkleTrie: MerkleTrie + const defaultAmountOfNodes = 32 + const defaultNodeLength = 32 - const LibSecureMerkleTrieFactory = await ethers.getContractFactory( - "TestLibSecureMerkleTrie" - ) - - const libSecureMerkleTrie: TestLibSecureMerkleTrie = - await LibSecureMerkleTrieFactory.deploy() + beforeEach(async function () { + libSecureMerkleTrie = await ( + await ethers.getContractFactory("TestLibSecureMerkleTrie") + ).deploy() - const defaultSecureMerkleTrie = new MerkleTrie( - amountOfNodes, - len, + defaultSecureMerkleTrie = new MerkleTrie( + defaultAmountOfNodes, + defaultNodeLength, () => new SecureTrie() ) await defaultSecureMerkleTrie.init() - - return { - owner, - libSecureMerkleTrie, - defaultSecureMerkleTrie, - LibSecureMerkleTrieFactory, - } - } + }) describe("verifyInclusionProof()", () => { it(`is included, ${defaultAmountOfNodes} bytes, ${defaultNodeLength} node length`, async () => { - const { libSecureMerkleTrie, defaultSecureMerkleTrie } = - await deploylibSecureMerkleTrieFixture( - defaultAmountOfNodes, - defaultNodeLength - ) - const n = defaultSecureMerkleTrie.nodes[0] const key = n.key const t = await defaultSecureMerkleTrie.makeTest(key) @@ -59,12 +41,6 @@ describe("LibSecureMerkleTrie", function () { describe("get()", () => { it(`is included`, async () => { - const { libSecureMerkleTrie, defaultSecureMerkleTrie } = - await deploylibSecureMerkleTrieFixture( - defaultAmountOfNodes, - defaultNodeLength - ) - const key = defaultSecureMerkleTrie.nodes[0].key const t = await defaultSecureMerkleTrie.makeTest(key) const isIncluded = await libSecureMerkleTrie.get( @@ -78,12 +54,8 @@ describe("LibSecureMerkleTrie", function () { ethers.utils.hexlify(defaultSecureMerkleTrie.nodes[0].value) ) }) + it(`is not included`, async () => { - const { libSecureMerkleTrie, defaultSecureMerkleTrie } = - await deploylibSecureMerkleTrieFixture( - defaultAmountOfNodes, - defaultNodeLength - ) const t = await defaultSecureMerkleTrie.makeTest( defaultSecureMerkleTrie.nodes[0].key )