From acaaba755ffd0bfa13214b8a70af7cef10df3909 Mon Sep 17 00:00:00 2001 From: gabriel-aranha-cw <166405807+gabriel-aranha-cw@users.noreply.github.com> Date: Thu, 23 May 2024 13:53:27 -0300 Subject: [PATCH] allow block mode customization on e2e tests (#904) * refactor e2e tests into separate folder depending on block-mode * improve * improve * enable conditional on mining mode on hardhat * improve condition * initial e2e multiple txs test * fix * fix naming --- .github/workflows/e2e-test.yml | 54 ++++++++++++++----- e2e/hardhat.config.ts | 3 +- e2e/test/{ => automine}/e2e-json-rpc.test.ts | 6 +-- .../e2e-tx-parallel-contract.test.ts | 4 +- .../e2e-tx-parallel-transfer.test.ts | 4 +- .../e2e-tx-serial-contract.test.ts | 4 +- .../e2e-tx-serial-transfer.test.ts | 6 +-- e2e/test/external/e2e-multiple-tx.test.ts | 51 ++++++++++++++++++ justfile | 30 +++++------ 9 files changed, 122 insertions(+), 40 deletions(-) rename e2e/test/{ => automine}/e2e-json-rpc.test.ts (98%) rename e2e/test/{ => automine}/e2e-tx-parallel-contract.test.ts (98%) rename e2e/test/{ => automine}/e2e-tx-parallel-transfer.test.ts (93%) rename e2e/test/{ => automine}/e2e-tx-serial-contract.test.ts (99%) rename e2e/test/{ => automine}/e2e-tx-serial-transfer.test.ts (97%) create mode 100644 e2e/test/external/e2e-multiple-tx.test.ts diff --git a/.github/workflows/e2e-test.yml b/.github/workflows/e2e-test.yml index 0d6e4ce01..f7c45d237 100644 --- a/.github/workflows/e2e-test.yml +++ b/.github/workflows/e2e-test.yml @@ -25,34 +25,64 @@ on: - 'Cargo.toml' jobs: - e2e-stratus: - name: E2E Stratus in-memory + e2e-automine-stratus: + name: E2E Automine Stratus in-memory uses: ./.github/workflows/_setup-e2e.yml with: - justfile_recipe: 'e2e-stratus' + justfile_recipe: 'e2e-stratus automine' concurrency: - group: ${{ github.workflow }}-inmemory-${{ github.ref || github.run_id }} + group: ${{ github.workflow }}-automine-inmemory-${{ github.ref || github.run_id }} cancel-in-progress: true - e2e-stratus-postgres: - name: E2E Stratus Postgres + e2e-automine-stratus-postgres: + name: E2E Automine Stratus Postgres uses: ./.github/workflows/_setup-e2e.yml with: - justfile_recipe: 'e2e-stratus-postgres' + justfile_recipe: 'e2e-stratus-postgres automine' concurrency: - group: ${{ github.workflow }}-postgres-${{ github.ref || github.run_id }} + group: ${{ github.workflow }}-automine-postgres-${{ github.ref || github.run_id }} cancel-in-progress: true - e2e-stratus-rocks: - name: E2E Stratus Rocks + e2e-automine-stratus-rocks: + name: E2E Automine Stratus Rocks uses: ./.github/workflows/_setup-e2e.yml with: - justfile_recipe: 'e2e-stratus-rocks' + justfile_recipe: 'e2e-stratus-rocks automine' concurrency: - group: ${{ github.workflow }}-rocks-${{ github.ref || github.run_id }} + group: ${{ github.workflow }}-automine-rocks-${{ github.ref || github.run_id }} + cancel-in-progress: true + + e2e-external-stratus: + name: E2E External Stratus in-memory + uses: ./.github/workflows/_setup-e2e.yml + with: + justfile_recipe: 'e2e-stratus external' + + concurrency: + group: ${{ github.workflow }}-external-inmemory-${{ github.ref || github.run_id }} + cancel-in-progress: true + + e2e-external-stratus-postgres: + name: E2E External Stratus Postgres + uses: ./.github/workflows/_setup-e2e.yml + with: + justfile_recipe: 'e2e-stratus-postgres external' + + concurrency: + group: ${{ github.workflow }}-external-postgres-${{ github.ref || github.run_id }} + cancel-in-progress: true + + e2e-external-stratus-rocks: + name: E2E External Stratus Rocks + uses: ./.github/workflows/_setup-e2e.yml + with: + justfile_recipe: 'e2e-stratus-rocks external' + + concurrency: + group: ${{ github.workflow }}-external-rocks-${{ github.ref || github.run_id }} cancel-in-progress: true e2e-clock-stratus: diff --git a/e2e/hardhat.config.ts b/e2e/hardhat.config.ts index f27b89901..10acedccc 100644 --- a/e2e/hardhat.config.ts +++ b/e2e/hardhat.config.ts @@ -21,7 +21,8 @@ const config: HardhatUserConfig = { gasPrice: 0, initialBaseFeePerGas: 0, mining: { - auto: true, + auto: process.env.BLOCK_MODE === 'automine', + interval: process.env.BLOCK_MODE === 'automine' ? undefined : 0 }, accounts: { mnemonic: ACCOUNTS_MNEMONIC, diff --git a/e2e/test/e2e-json-rpc.test.ts b/e2e/test/automine/e2e-json-rpc.test.ts similarity index 98% rename from e2e/test/e2e-json-rpc.test.ts rename to e2e/test/automine/e2e-json-rpc.test.ts index c12ab1220..2c0a66e1f 100644 --- a/e2e/test/e2e-json-rpc.test.ts +++ b/e2e/test/automine/e2e-json-rpc.test.ts @@ -1,8 +1,8 @@ import { expect } from "chai"; import { Block } from "web3-types"; -import { ALICE, BOB } from "./helpers/account"; -import { isStratus } from "./helpers/network"; +import { ALICE, BOB } from "../helpers/account"; +import { isStratus } from "../helpers/network"; import { CHAIN_ID, CHAIN_ID_DEC, @@ -15,7 +15,7 @@ import { sendReset, subscribeAndGetEvent, subscribeAndGetEventWithContract, -} from "./helpers/rpc"; +} from "../helpers/rpc"; describe("JSON-RPC", () => { describe("State", () => { diff --git a/e2e/test/e2e-tx-parallel-contract.test.ts b/e2e/test/automine/e2e-tx-parallel-contract.test.ts similarity index 98% rename from e2e/test/e2e-tx-parallel-contract.test.ts rename to e2e/test/automine/e2e-tx-parallel-contract.test.ts index 8c0f3c6b9..297955098 100644 --- a/e2e/test/e2e-tx-parallel-contract.test.ts +++ b/e2e/test/automine/e2e-tx-parallel-contract.test.ts @@ -1,7 +1,7 @@ import { expect } from "chai"; import { TestContractBalances, TestContractCounter } from "../typechain-types"; -import { ALICE, BOB, CHARLIE, randomAccounts } from "./helpers/account"; +import { ALICE, BOB, CHARLIE, randomAccounts } from "../helpers/account"; import { TX_PARAMS, deployTestContractBalances, @@ -9,7 +9,7 @@ import { sendGetNonce, sendRawTransactions, sendReset, -} from "./helpers/rpc"; +} from "../helpers/rpc"; describe("Transaction: parallel TestContractBalances", async () => { var _contract: TestContractBalances; diff --git a/e2e/test/e2e-tx-parallel-transfer.test.ts b/e2e/test/automine/e2e-tx-parallel-transfer.test.ts similarity index 93% rename from e2e/test/e2e-tx-parallel-transfer.test.ts rename to e2e/test/automine/e2e-tx-parallel-transfer.test.ts index e52e5c48f..d61fc5e93 100644 --- a/e2e/test/e2e-tx-parallel-transfer.test.ts +++ b/e2e/test/automine/e2e-tx-parallel-transfer.test.ts @@ -1,7 +1,7 @@ import { expect } from "chai"; -import { TEST_ACCOUNTS, randomAccounts } from "./helpers/account"; -import { sendGetBalance, sendRawTransactions, sendReset } from "./helpers/rpc"; +import { TEST_ACCOUNTS, randomAccounts } from "../helpers/account"; +import { sendGetBalance, sendRawTransactions, sendReset } from "../helpers/rpc"; describe("Transaction: parallel transfer", () => { it("Resets blockchain", async () => { diff --git a/e2e/test/e2e-tx-serial-contract.test.ts b/e2e/test/automine/e2e-tx-serial-contract.test.ts similarity index 99% rename from e2e/test/e2e-tx-serial-contract.test.ts rename to e2e/test/automine/e2e-tx-serial-contract.test.ts index 53dcb9d5b..38f7c295c 100644 --- a/e2e/test/e2e-tx-serial-contract.test.ts +++ b/e2e/test/automine/e2e-tx-serial-contract.test.ts @@ -1,7 +1,7 @@ import { expect } from "chai"; import { TestContractBalances } from "../typechain-types"; -import { CHARLIE } from "./helpers/account"; +import { CHARLIE } from "../helpers/account"; import { calculateSlotPosition, CHAIN_ID, @@ -14,7 +14,7 @@ import { sendReset, toHex, toPaddedHex, -} from "./helpers/rpc"; +} from "../helpers/rpc"; import { Transaction } from "web3-types"; import { network } from "hardhat"; diff --git a/e2e/test/e2e-tx-serial-transfer.test.ts b/e2e/test/automine/e2e-tx-serial-transfer.test.ts similarity index 97% rename from e2e/test/e2e-tx-serial-transfer.test.ts rename to e2e/test/automine/e2e-tx-serial-transfer.test.ts index 61577c757..7d7dc9231 100644 --- a/e2e/test/e2e-tx-serial-transfer.test.ts +++ b/e2e/test/automine/e2e-tx-serial-transfer.test.ts @@ -2,8 +2,8 @@ import { expect } from "chai"; import { keccak256 } from "ethers"; import { Block, Transaction, TransactionReceipt } from "web3-types"; -import { ALICE, Account, BOB, randomAccounts } from "./helpers/account"; -import { isStratus } from "./helpers/network"; +import { ALICE, Account, BOB, randomAccounts } from "../helpers/account"; +import { isStratus } from "../helpers/network"; import { CHAIN_ID, HASH_EMPTY_TRANSACTIONS, @@ -19,7 +19,7 @@ import { sendGetBalance, sendRawTransaction, sendReset, -} from "./helpers/rpc"; +} from "../helpers/rpc"; describe("Transaction: serial transfer", () => { var _tx: Transaction; diff --git a/e2e/test/external/e2e-multiple-tx.test.ts b/e2e/test/external/e2e-multiple-tx.test.ts new file mode 100644 index 000000000..31f8d1b0b --- /dev/null +++ b/e2e/test/external/e2e-multiple-tx.test.ts @@ -0,0 +1,51 @@ +import { expect } from "chai"; + +import { TEST_ACCOUNTS, randomAccounts } from "../helpers/account"; +import { send, sendGetBalance, sendRawTransactions, sendReset } from "../helpers/rpc"; + +describe("Multiple Transactions Per Block", () => { + it("Resets blockchain", async () => { + await sendReset(); + }); + it("Send multiple transactions and mine block", async () => { + const counterParty = randomAccounts(1)[0]; + expect(await sendGetBalance(counterParty.address)).eq(0); + + let expectedCounterPartyBalance = 0; + const signedTxs = []; + for (let i = 0; i < TEST_ACCOUNTS.length; i++) { + const amount = i + 1; + const account = TEST_ACCOUNTS[i]; + signedTxs.push(await account.signWeiTransfer(counterParty.address, amount)); + expectedCounterPartyBalance += amount; + } + + for (const account of randomAccounts(100)) { + signedTxs.push(await account.signWeiTransfer(counterParty.address, 0)); + } + + const txHashes = await sendRawTransactions(signedTxs); + + const latestBlockBeforeMining = await send('eth_getBlockByNumber', ['latest', true]); + + // mine the block + await send("evm_mine", []); + + // get the latest block after mining + const latestBlockAfterMining = await send('eth_getBlockByNumber', ['latest', true]); + + // check if block was mined + expect(latestBlockAfterMining).to.exist; + + // check if mined block is different from the latest block before mining + expect(latestBlockAfterMining.hash).to.not.equal(latestBlockBeforeMining.hash); + + // check if all transactions are in the block + for (let txHash of txHashes) { + expect(latestBlockAfterMining.transactions.map((tx: any) => tx.hash)).to.include(txHash); + } + + // check counterParty balance + expect(await sendGetBalance(counterParty.address)).eq(expectedCounterPartyBalance); + }); +}); \ No newline at end of file diff --git a/justfile b/justfile index 2bf67ea6b..4fbccce6f 100644 --- a/justfile +++ b/justfile @@ -160,7 +160,7 @@ test-int name="'*'": # ------------------------------------------------------------------------------ # E2E: Execute Hardhat tests in the specified network -e2e network="stratus" test="": +e2e network="stratus" block-mode="automine" test="": #!/bin/bash if [ -d e2e ]; then cd e2e @@ -170,32 +170,32 @@ e2e network="stratus" test="": fi if [ -z "{{test}}" ]; then - npx hardhat test test/*.test.ts --network {{network}} + npx hardhat test test/{{block-mode}}/*.test.ts --network {{network}} else - npx hardhat test test/*.test.ts --network {{network}} --grep "{{test}}" + npx hardhat test test/{{block-mode}}/*.test.ts --network {{network}} --grep "{{test}}" fi # E2E: Starts and execute Hardhat tests in Hardhat -e2e-hardhat test="": +e2e-hardhat block-mode="automine" test="": #!/bin/bash if [ -d e2e ]; then cd e2e fi echo "-> Starting Hardhat" - npx hardhat node & + BLOCK_MODE={{block-mode}} npx hardhat node & echo "-> Waiting Hardhat to start" wait-service --tcp localhost:8545 -- echo echo "-> Running E2E tests" - just e2e hardhat {{test}} + just e2e hardhat {{block-mode}} {{test}} echo "-> Killing Hardhat" killport 8545 # E2E: Starts and execute Hardhat tests in Stratus -e2e-stratus test="": +e2e-stratus block-mode="automine" test="": #!/bin/bash if [ -d e2e ]; then cd e2e @@ -203,13 +203,13 @@ e2e-stratus test="": echo "-> Starting Stratus" just build || exit 1 - just run -a 0.0.0.0:3000 > stratus.log & + just run -a 0.0.0.0:3000 --block-mode {{block-mode}} > stratus.log & echo "-> Waiting Stratus to start" wait-service --tcp 0.0.0.0:3000 -t {{ wait_service_timeout }} -- echo echo "-> Running E2E tests" - just e2e stratus {{test}} + just e2e stratus {{block-mode}} {{test}} result_code=$? echo "-> Killing Stratus" @@ -217,7 +217,7 @@ e2e-stratus test="": exit $result_code # E2E: Starts and execute Hardhat tests in Stratus -e2e-stratus-rocks test="": +e2e-stratus-rocks block-mode="automine" test="": #!/bin/bash if [ -d e2e ]; then cd e2e @@ -225,13 +225,13 @@ e2e-stratus-rocks test="": echo "-> Starting Stratus" just build || exit 1 - just run -a 0.0.0.0:3000 --perm-storage=rocks > stratus.log & + just run -a 0.0.0.0:3000 --block-mode {{block-mode}} --perm-storage=rocks > stratus.log & echo "-> Waiting Stratus to start" wait-service --tcp 0.0.0.0:3000 -t {{ wait_service_timeout }} -- echo echo "-> Running E2E tests" - just e2e stratus {{test}} + just e2e stratus {{block-mode}} {{test}} result_code=$? echo "-> Killing Stratus" @@ -239,7 +239,7 @@ e2e-stratus-rocks test="": exit $result_code # E2E: Starts and execute Hardhat tests in Stratus -e2e-stratus-postgres test="": +e2e-stratus-postgres block-mode="automine" test="": #!/bin/bash if [ -d e2e ]; then cd e2e @@ -254,13 +254,13 @@ e2e-stratus-postgres test="": echo "-> Starting Stratus" just build || exit 1 - just run -a 0.0.0.0:3000 --perm-storage {{ database_url }} > stratus.log & + just run -a 0.0.0.0:3000 --block-mode {{block-mode}} --perm-storage {{ database_url }} > stratus.log & echo "-> Waiting Stratus to start" wait-service --tcp 0.0.0.0:3000 -t {{ wait_service_timeout }} -- echo echo "-> Running E2E tests" - just e2e stratus {{test}} + just e2e stratus {{block-mode}} {{test}} result_code=$? echo "-> Killing Stratus"