From 70b55c6ed7721cf0bfae8c0e32b869d4aa65498b Mon Sep 17 00:00:00 2001 From: furkhat Date: Thu, 23 Jan 2020 01:07:29 +0600 Subject: [PATCH] fix bugs & adjust tests --- contracts/contracts/Franklin.sol | 6 ++-- contracts/test/fails_tests.ts | 6 ++++ contracts/test/helpers.ts | 1 + contracts/test/integration_tests.ts | 43 ++++++++++------------------- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/contracts/contracts/Franklin.sol b/contracts/contracts/Franklin.sol index fd3b3af02c..ed2c8357d8 100644 --- a/contracts/contracts/Franklin.sol +++ b/contracts/contracts/Franklin.sol @@ -233,11 +233,11 @@ contract Franklin { } for (uint32 i = firstPendingWithdrawalIndex; i < firstPendingWithdrawalIndex + toProcess; ++i) { - // send fails are ignored hence there is always a direct way to withdraw. - delete pendingWithdrawals[i]; - uint16 tokenId = pendingWithdrawals[i].tokenId; address to = pendingWithdrawals[i].to; + // send fails are ignored hence there is always a direct way to withdraw. + delete pendingWithdrawals[i]; + uint128 amount = balancesToWithdraw[to][tokenId]; // amount is zero means funds has been withdrawn with withdrawETH or withdrawERC20 if (amount != 0) { diff --git a/contracts/test/fails_tests.ts b/contracts/test/fails_tests.ts index 0f77258feb..2b5125993b 100644 --- a/contracts/test/fails_tests.ts +++ b/contracts/test/fails_tests.ts @@ -112,6 +112,7 @@ describe("PLANNED FAILS", function () { const code3 = await provider.call(tx3, tx3.blockNumber); const reason3 = hex_to_ascii(code3.substr(138)); + expect(reason3.substring(0, 5)).equal("fd011"); console.log(" + ERC20 deposit: Wrong tx value (msg.value < fee) passed"); @@ -533,6 +534,7 @@ describe("PLANNED FAILS", function () { const code6 = await provider.call(tx6, tx6.blockNumber); const reason6 = hex_to_ascii(code6.substr(138)); + expect(reason6.substring(0, 5)).equal("fvs11"); console.log(" + Wrong priority operation - different data passed"); @@ -551,6 +553,7 @@ describe("PLANNED FAILS", function () { const code7 = await provider.call(tx7, tx7.blockNumber); const reason7 = hex_to_ascii(code7.substr(138)); + expect(reason7.substring(0, 5)).equal("grr21"); console.log(" + Not governor passed"); }); @@ -576,6 +579,7 @@ describe("PLANNED FAILS", function () { const code1 = await provider.call(tx1, tx1.blockNumber); const reason1 = hex_to_ascii(code1.substr(138)); + expect(reason1.substring(0, 5)).equal("fvk11"); console.log(" + Wrong verify number passed"); @@ -588,6 +592,7 @@ describe("PLANNED FAILS", function () { const code2 = await provider.call(tx2, tx2.blockNumber); const reason2 = hex_to_ascii(code2.substr(138)); + expect(reason2.substring(0, 5)).equal("grr21"); console.log(" + Not governor passed"); }); @@ -636,6 +641,7 @@ describe("PLANNED FAILS", function () { const code1 = await provider.call(prTx2, prTx2.blockNumber); const reason1 = hex_to_ascii(code1.substr(138)); + expect(reason1.substring(0, 5)).equal("pcs11"); console.log(" + Set franklin address twice will not work passed"); }); diff --git a/contracts/test/helpers.ts b/contracts/test/helpers.ts index 9eea547d50..1b80fe8a6b 100644 --- a/contracts/test/helpers.ts +++ b/contracts/test/helpers.ts @@ -1,4 +1,5 @@ +import { BigNumber } from "ethers/utils"; import { utils as syncutils } from "zksync"; import { BN } from "bn.js"; diff --git a/contracts/test/integration_tests.ts b/contracts/test/integration_tests.ts index 45f46afa23..da93f76cb6 100644 --- a/contracts/test/integration_tests.ts +++ b/contracts/test/integration_tests.ts @@ -15,8 +15,8 @@ import { import { expect, use } from "chai"; import { solidity } from "ethereum-waffle"; -import { bigNumberify, parseEther, hexlify, formatEther } from "ethers/utils"; -import { createDepositPublicData, createWithdrawPublicData, createFullExitPublicData } from "./helpers"; +import { bigNumberify, parseEther, hexlify, formatEther, BigNumber } from "ethers/utils"; +import { createDepositPublicData, createWithdrawPublicData, createFullExitPublicData, hex_to_ascii } from "./helpers"; use(solidity); @@ -113,6 +113,7 @@ describe("INTEGRATION", function () { // Commit block with eth partial exit. const exitValue = parseEther("0.2"); + const exitBlockPublicData = createWithdrawPublicData(tokenId, hexlify(exitValue), exitWallet.address); const partExTx = await franklinDeployedContract.commitBlock(2, 22, @@ -163,8 +164,8 @@ describe("INTEGRATION", function () { expect(verifiedEvent2.blockNumber).equal(2); - let v = await franklinDeployedContract.completeWithdrawals(1); - await v.wait(); + await (await franklinDeployedContract.completeWithdrawals(1)).wait(); + const afterPartExitBalance = await exitWallet.getBalance(); expect(afterPartExitBalance.sub(beforePartExitBalance)).eq(exitValue); @@ -172,7 +173,6 @@ describe("INTEGRATION", function () { // Full exit eth const fullExitAmount = parseEther("0.096778"); // amount after: tx value - some counted fee - exit amount - const fullExitMinusGas = parseEther("0.096047605"); const accId = 0; const pubkey = "0x0000000000000000000000000000000000000000000000000000000000000000"; const signature = Buffer.from("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "hex"); @@ -227,6 +227,7 @@ describe("INTEGRATION", function () { const verifyFullExTx = await franklinDeployedContract.verifyBlock(3, dummyBlockProof, { gasLimit: bigNumberify("500000") }); const verifyFullExReceipt = await verifyFullExTx.wait(); + let gasUsed = verifyFullExReceipt.gasUsed.mul(await provider.getGasPrice()); const verifyEvents = verifyFullExReceipt.events; const verifiedEvent3 = verifyEvents.pop().args; @@ -236,26 +237,15 @@ describe("INTEGRATION", function () { expect(await priorityQueueDeployedContract.totalOpenPriorityRequests()).equal(0); expect(await priorityQueueDeployedContract.firstPriorityRequestId()).equal(2); - v = await franklinDeployedContract.completeWithdrawals(1); - await v.wait(); + //// Withdraw accumulated fees eth for wallet and other funds + const completeWithdrawalsReceipt = await (await franklinDeployedContract.completeWithdrawals(1)).wait(); + gasUsed = gasUsed.add(completeWithdrawalsReceipt.gasUsed.mul(await provider.getGasPrice())); + const accumFees = parseEther("0.006282"); const afterFullExitBalance = await wallet.getBalance(); - expect(afterFullExitBalance.sub(beforeFullExitBalance)).eq(fullExitMinusGas); // full exit amount minus gas fee for send transaction + expect(afterFullExitBalance.sub(beforeFullExitBalance.add(accumFees))).eq(fullExitAmount.sub(gasUsed)); - console.log("Full exit verified"); - - // Withdraw accumulated fees eth for wallet - const accumFees = parseEther("0.006282"); - const oldBalance = await wallet.getBalance(); - const balanceToWithdraw = await franklinDeployedContract.balancesToWithdraw(wallet.address, 0); - const exitTx = await franklinDeployedContract.withdrawETH(balanceToWithdraw); - const exitTxReceipt = await exitTx.wait(); - const gasUsed = exitTxReceipt.gasUsed.mul(await provider.getGasPrice()); - const newBalance = await wallet.getBalance(); - expect(newBalance.sub(oldBalance).add(gasUsed)).eq(accumFees); - expect(await franklinDeployedContract.balancesToWithdraw(wallet.address, 0)).equal(bigNumberify(0)); - - console.log("Withdrawed to wallet"); + console.log("Full exit verified and withdrawed to wallet"); console.log(" + ETH Integration passed") }); @@ -362,14 +352,11 @@ describe("INTEGRATION", function () { const verifyPartExReceipt = await verifyPartExTx.wait(); const verifyPartExEvents = verifyPartExReceipt.events; - console.log("verifyPartExEvents:", verifyPartExEvents); - const verifiedEvent2 = verifyPartExEvents.pop().args; expect(verifiedEvent2.blockNumber).equal(2); - let v = await franklinDeployedContract.completeWithdrawals(1); - await v.wait(); + await (await franklinDeployedContract.completeWithdrawals(1)).wait(); const newBalance1 = await erc20DeployedToken.balanceOf(exitWallet.address); expect(newBalance1.sub(oldBalance1)).eq(exitValue); @@ -440,9 +427,7 @@ describe("INTEGRATION", function () { expect(await priorityQueueDeployedContract.totalOpenPriorityRequests()).equal(0); expect(await priorityQueueDeployedContract.firstPriorityRequestId()).equal(2); - v = await franklinDeployedContract.completeWithdrawals(1); - await v.wait(); - + await (await franklinDeployedContract.completeWithdrawals(1)).wait(); const newBalance2 = await erc20DeployedToken.balanceOf(wallet.address); expect(newBalance2.sub(oldBalance2)).eq(fullExitAmount);