Include multiple transactions in a single block using OpenZeppelin Test Environment

:computer: Environment

My package.json looks like this:

{
  "dependencies": {
    "@openzeppelin/cli": "^2.8.2",
    "@openzeppelin/test-helpers": "^0.5.6",
    "@openzeppelin/truffle-upgrades": "^1.0.2",
    "@openzeppelin/upgrades": "^2.8.0",
    "@truffle/hdwallet-provider": "^1.0.43",
    "dotenv": "^8.2.0",
    "truffle": "^5.1.43",
    "web3": "^1.3.0"
  },
  "devDependencies": {
    "@openzeppelin/contracts": "^3.1.0",
    "@openzeppelin/test-environment": "^0.1.4",
    "chai": "^4.2.0",
    "husky": "^4.3.0",
    "mocha": "^8.1.3",
    "solhint": "^3.2.0",
    "verify-on-etherscan": "^1.2.1"
  },

:memo:Details
I am trying to test a contract where I want to make multiple transactions in the same block to setup some state of the contract. In ganache, you can stop the block mining and send the transactions, then resume mining. How do I do this with openzeppelin?

In my current tests, every transaction is incrementing the block number by 1. An example would be the following:

await this.contract.deposit(depositAmount, { from: account });
await this.contract.deposit(depositAmount, { from: account2 });

In the current system, these two transactions happen in 2 blocks. I’m trying to force them to be in the same block. Any ideas?

1 Like

Hi @juliankoh,

Welcome to the community :wave:

OpenZeppelin Test Environment spins up a local ganache powered blockchain and doesn’t expose all of the options (https://github.com/trufflesuite/ganache-cli#options) so I don’t think it is possible to mine multiple transactions in the one block in OpenZeppelin Test Environment tests currently.

You could use OpenZeppelin Test Helpers to advance time (https://docs.openzeppelin.com/test-helpers/0.5/api#time). This could allow you to do your setup (one transaction per block) and then move to a specific time.

Alternatively, if you need to do multiple transactions in one block, then you may need to use truffle tests so you can control ganache-cli.

As an aside, I noticed that you don’t appear to be using OpenZeppelin Contracts Ethereum Package (the Upgrade Safe fork of OpenZeppelin Contracts, see: The Future of contracts-ethereum-package).

Hi @juliankoh,

I wanted to check if you had any more questions?