hardhat icon indicating copy to clipboard operation
hardhat copied to clipboard

Random "contract call run out of gas and made the transaction revert" errors while running tests

Open ppedziwiatr opened this issue 3 years ago • 4 comments

Hi,

we're having really strange issues with running tests. There are about 100 of them, and on each run some of them (usually 2 or 3) fail with the error from title, e.g.:

4) Smart loan
       A loan with debt and repayment
         should repay funds:
     Error: Transaction reverted: contract call run out of gas and made the transaction revert
      at CompoundingIndex.rayPow (contracts/WadRayMath.sol:83)
      at CompoundingIndex.getCompoundedFactor (contracts/CompoundingIndex.sol:112)
      at CompoundingIndex.getIndex (contracts/CompoundingIndex.sol:75)
      at CompoundingIndex.updateIndex (contracts/CompoundingIndex.sol:102)
      at CompoundingIndex.setRate (contracts/CompoundingIndex.sol:45)
      at Pool.updateRates (contracts/Pool.sol:200)
      at Pool.repay (contracts/Pool.sol:150)
      at SmartLoan.repay (contracts/SmartLoan.sol:109)
      at processTicksAndRejections (internal/process/task_queues.js:95:5)
      at HardhatNode._mineBlockWithPendingTxs (node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:1575:23)

What is really odd is that It happens (every time!) only on computers with intel-based cpu-s (on both MacOS on Linux, also in github hook - you can see it here: https://github.com/jakub-wojciechowski/avaloan/pull/4/checks )

However - it has never happened on my M1 Macbook.

I've already tried different settings described in this issue: https://github.com/nomiclabs/hardhat/issues/660 - but none of them seems to help.

Current configuration: https://github.com/jakub-wojciechowski/avaloan/blob/dcf754acb37d82b03256b219e612c8f77d1950c1/hardhat.config.ts

What may be causing these issues?

My only idea is that it is because of frequent contracts deploys (in "beforeEach") - for example https://github.com/jakub-wojciechowski/avaloan/blob/dcf754acb37d82b03256b219e612c8f77d1950c1/test/pool-fixed-rates-deposits_1.test.ts

But I really would not like to switch to deploying contracts in "before" - as it would introduce some nasty dependencies between tests.

And still - I can't understand why it isn't an issue on Apple Silicon.

Any help would be appreciated :-)

Using hardhat 2.5.0.

ppedziwiatr avatar Jul 23 '21 17:07 ppedziwiatr

Hey @ppedziwiatr, any chance you could convert this into a minimal reproducible example? In your case, it probably means removing all the stuff you can (plugins, other contracts, other tests) until you have the minimal amount of code that still reproduces this. If you can do that, we'll be able to look into this sooner.

fvictorio avatar Jul 26 '21 12:07 fvictorio

Hey @ppedziwiatr, any chance you could convert this into a minimal reproducible example? In your case, it probably means removing all the stuff you can (plugins, other contracts, other tests) until you have the minimal amount of code that still reproduces this. If you can do that, we'll be able to look into this sooner.

Here's branch with sth. that I believe is MRE - https://github.com/jakub-wojciechowski/avaloan/tree/ppe/hardhat-gas-issues This is the minimum amount of the tests that are required to make the tests fail everytime on my old Intel-based CPU laptop (10 fails for 10 runs). Same issues on Windows and Intel-based macbooks. I've also removed all non-related source code and left only the required set of depedencies.

Example errors on Intel: image

It works without any issues (10 runs, no errors) on my Apple Silicon: image

@fvictorio , please let me know if this version is giving you errors - we've tested it on a yet another Intel machine and it failed 3 times on 5 runs...

ppedziwiatr avatar Jul 27 '21 11:07 ppedziwiatr

I've been looking into this issue and I'm not sure if the gas parameter from the hre (Hardhat runtime environment) is passed correctly, especially if there are multiple plugins attached (both hardhat-ethers and hardhat-wallet). After playing with that "gas" parameter, I haven't noticed any changes spying the hardhat-ethers. I've also noticed that the gas is estimated even when the "gas" parameter is set (is this expected behaviour)? Anyway, I implemented a simple wrapper around the signers - to set the gas limit at a transaction level:

export const getFixedGasSigners = async function(gasLimit:number) {
  const signers : SignerWithAddress[] = await ethers.getSigners();
  signers.forEach(signer => {
    let orig = signer.sendTransaction;
    signer.sendTransaction = function(transaction) {
      transaction.gasLimit = BigNumber.from(gasLimit.toString());
      return orig.apply(signer, [transaction]);
    }
  });
  return signers;
};

It helped to fix all the issues with tests and they are executed roughly 2 times faster, due to skipping the gas estimation step.

I hope this will help you to track down the problem or have a workaround for anyone facing the same issue until it's properly fixed.

jakub-wojciechowski avatar Jul 30 '21 13:07 jakub-wojciechowski

This issue was marked as stale because it didn't have any activity in the last 30 days. If you think it's still relevant, please leave a comment indicating so. Otherwise, it will be closed in 7 days.

github-actions[bot] avatar Aug 08 '22 10:08 github-actions[bot]

I'm revisiting this issue and, as part of that, I removed the ethereum-waffle usages of the reproduction repo and replaced them with deployments using hardhat-ethers. For some reason, after that the problem went away.

So it's very likely that the problem is related to Waffle's deployContract. Sadly, that's out of scope for me, so I'm going to tentatively close this issue. If someone has a similar problem and can provide minimal reproduction steps, please open a new issue.

fvictorio avatar Dec 29 '22 10:12 fvictorio