hardhat icon indicating copy to clipboard operation
hardhat copied to clipboard

Hardhat crashes when bit left shifting

Open trojanMcAfee opened this issue 3 years ago • 6 comments

Hardhat crashes with the error Error: Transaction reverted and Hardhat couldn't infer the reason. Please report this to help us improve Hardhat. 100% of the times when attempting a left bit shift operation in this code:

function _toggleBit(uint bitmap_, uint index_) internal {
        s.bitLocks[bitmap_] ^= (1 << index_);
    }

If I put a console.log before the operation, it crashes around 30% of the times but it still crashes when it shouldn't:

function _toggleBit(uint bitmap_, uint index_) internal {
        console.log(' ');
        s.bitLocks[bitmap_] ^= (1 << index_);
    }

Full error:

Error: Transaction reverted and Hardhat couldn't infer the reason. Please report this to help us improve Hardhat.
      at OZLFacet._toggleBit (contracts/arbitrum/Bits.sol:19)
      at OZLFacet.noReentrancy (contracts/Modifiers.sol:20)
      at OZLFacet.exchangeToUserToken (contracts/arbitrum/facets/OZLFacet.sol:42)
      at Diamond.<fallback> (contracts/arbitrum/Diamond.sol:71)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
      at HardhatNode._mineBlockWithPendingTxs (node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:1582:23)
      at HardhatNode.mineBlock (node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:435:16)
      at EthModule._sendTransactionAndReturnHash (node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:1494:18)
      at HardhatNetworkProvider.request (node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:108:18)
      at EthersProviderWrapper.send (node_modules/@nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:13:20)

All of OZLFacet methods and Diamond are mine.

Sol v0.8.14

trojanMcAfee avatar Aug 31 '22 11:08 trojanMcAfee

This issue is also being tracked on Linear.

We use Linear to manage our development process, but we keep the conversations on Github.

LINEAR-ID: b185f987-3692-448b-94aa-f388d59bfcef

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

@cdgmachado0 can you provide a minimal reproducible example?

I tried this:

contract Foo {
  uint public x = 1;

  function f(uint index) public {
    x = x << index;
  }
}

with values for index of 0, 1, 2, 100 and 1000 and they all worked.

fvictorio avatar Aug 31 '22 11:08 fvictorio

I've tried also with a minimum example and it works every time like yours.

It fails only when running tests on a fork with my whole system deployed, and the only way to go around that error is by adding that console.log (I know, extremely weird).

See the screenshot of my terminal (right side test passing with the console.log and left side test failing without it) @fvictorio .

Bit bug

trojanMcAfee avatar Aug 31 '22 12:08 trojanMcAfee

I see, but without a way to reproduce it there's not much I can do, sorry.

If your repo is public, sharing it and the minimal steps to reproduce it (for example, removing/skipping tests that are not relevant) would be enough.

If you can't share your repo, then what you can do is to start removing code until you have something simple enough that reproduces the problem and can be turned into an MRE. In fact, this would be even better than the whole project.

fvictorio avatar Aug 31 '22 14:08 fvictorio

I tried to reproduce the issue in the following repository without success. Even when using forked networks, the test fails. Could you kindly help with a minimal reproducible example @cdgmachado0 ? https://github.com/sisco0/hardhat-test-3108

sisco0 avatar Sep 19 '22 06:09 sisco0

If the test fails, you're reproducing the issue @sisco0 . The test is designed to pass (test/arb-tests/ozel-index.js) without the console.log().

I'm not really sure -without any ts knowledge- how to build a minimal reproducible example since it's only one test that does 18 txs, and the bug can come in tx Num 4 or 11 or 14 or not come at all.

It's an on/off behavior that doesn't happen all the time, so I'm doubting of having a piece of code -from my end- that could always reproduce it. If you tried it out using my repo and the test succeeded (meaning, the issue wasn't thrown), try running it several times to see if you catch the doggy behavior.

I'm currently finishing up with the development of a part of the larger project. Once I finish it, I'll try to bring a minimum example.

trojanMcAfee avatar Sep 20 '22 18:09 trojanMcAfee

I'm going to close this one for bookkeeping reasons. If someone can provide a reproduction example, I will re-open it.

fvictorio avatar Oct 28 '22 13:10 fvictorio