3

I'm trying to test a solidity function that only allows it to be called once per block. E.g. (my desired functionality):

  1. Tx1 calls Fn during block #100, and when successful sets a boolean flag for block #100 to true, then:

  2. Tx2 calls Fn during block #100, but gets reverted because the flag was true.

My problem is, as I can see from the logs, the transactions are mined in subsequent blocks.

Code example:

  contract.fn({ from: player1 }) // mined in block 100
  await contract.fn({ from: player2 }) // mined in block 101

Using ganache-cli with --blockTime=10 and ganache UI w/o automine this still happens.

How do I get the transactions out one after the other, but to be mined in the same block?

  • 1
    Since it's for testing purposes, you can implement a helper contract which inherits (or contains) your contract, and in that helper contract, implement a function which calls Fn twice. Then, in your Truffle test, call this function and make sure that you get a revert exception. – goodvibration Dec 21 '18 at 5:19
  • One possibility is to call miner_stop before sending transactions and call evm_mine after. You might need to use .sendTransaction to prevent truffle from waiting for the transaction to be mined. – Ismael Dec 27 '18 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.