ganache icon indicating copy to clipboard operation
ganache copied to clipboard

Automatic nonce generation race condition

Open theblock-dev opened this issue 3 years ago • 14 comments

Discussed in https://github.com/trufflesuite/truffle/discussions/4774

Originally posted by theblock-dev February 24, 2022 Hi All While running the truffle migrate --reset command, I am getting into nonce issue. The migration code seems to be fine and I can not seem to find any issue with it. Here is the error I am getting

image

Is there a way to reset the nonce for the truffle?

Thanks

theblock-dev avatar Feb 24 '22 13:02 theblock-dev

Hey @theblock-dev, can you give me a little more information about what you are doing? For example, can you tell me what version of Truffle you are using, what network you are migrating to, and what provider you are furnishing? If you have your truffle-config.js and wouldn't mind sharing it that would also be helpful. Let me know and I'll try and help troubleshoot!

eggplantzzz avatar Feb 24 '22 15:02 eggplantzzz

Hi Tyler Thanks for reaching out, much appreciated. Here are the version details

image

I am migrating it to the local ganache cli. Attached are the truffle-config.js and 2_deploy_contracts.js files ( Pls extract the zip )

config.zip

Thanks

theblock-dev avatar Feb 25 '22 04:02 theblock-dev

Sure! Can you try upgrading Truffle to the latest version? Also, if you are using ganache-cli, upgrade to the latest Ganache as well if you can. It has been renamed just ganache. You will have to uninstall ganache-cli first.

So npm uninstall -g ganache-cli && npm install -g ganache should install v7 (the latest) for you. You then run it with ganache.

Is your project on Github?

eggplantzzz avatar Feb 25 '22 18:02 eggplantzzz

I am using the latest ganache 7.0 image

Also update truffle to the latest version of 5.5.2, however I still have the same issue while running the migration.

theblock-dev avatar Feb 26 '22 17:02 theblock-dev

I downgraded the truffle version to 5.4.23, and the issue got resolved automatically. I have not idea why the higher version gave this error. However, now I have a different issue. Truffle now gives out of gas while running migration.

theblock-dev avatar Feb 28 '22 13:02 theblock-dev

Actually the latest Ganaceh is 7.0.2 I believe. Is your project on GitHub so I can take a look and try to figure out what might be happening? If there is a problem with the latest version of Truffle then I would like to try and document it so we can fix it. Let me know if you have a code sample and steps for reliably reproducing this error.

eggplantzzz avatar Feb 28 '22 17:02 eggplantzzz

Hi Tyler Here is the link to the project - https://github.com/theblock-dev/ERC20Dex

Here are the version details image

Steps to replicate -

  1. Go to the root of the project
  2. open truffle develop environment - truffle develop
  3. migrate the project - migrate --reset

Thanks

theblock-dev avatar Mar 01 '22 05:03 theblock-dev

Thanks @theblock-dev! I'll go ahead and see if I can reproduce the problem you are having and hopefully have some advice for you.

eggplantzzz avatar Mar 01 '22 15:03 eggplantzzz

So I have reproduced your error and found the issue. The problem is with how you are asynchronously creating transactions with transferSeedToken. You are using map to create a bunch of transactions but they are not executing in the order of the nonces, I think this has to do with how Ganache's "instamine" mode works as it tries to process each transaction as it receives it. When Truffle creates these transactions it gives them each nonces (in order of course), but when Ganache goes to actually try and execute the transactions they are out of order.

If I run Ganache separately and set a block time (which causes instamine mode to be off) it seems to get past the area causing problems. When I run your example with a plain old for loop everything seems to run smoothly because we can ensure that the transactions are being processed in the order of the nonces.

I think this will not cause a problem when using a real network like Rinkeby as it will just throw each into the pool and wait until each's turn comes up. But as a workaround you could just remove the map and await each call to transferSeedToken. Let me chat with some of the Ganache team to see if I can get some more info about this behavior.

eggplantzzz avatar Mar 01 '22 16:03 eggplantzzz

So after a bit of troubleshooting with the Ganache team, it looks like there is an issue in Ganache. I'm going to transfer this issue there so we can get it triaged and fixed. Thanks for reporting this @theblock-dev!

eggplantzzz avatar Mar 01 '22 17:03 eggplantzzz

Just chiming in to say I am having the exact same issue.

I don't run into the issue when using ganache-cli (v6.12.2), only when I upgrade to to ganache v7.0.2

Juan-de-Costa-Rica avatar Mar 01 '22 21:03 Juan-de-Costa-Rica

@eggplantzzz - thanks a lot for your help and discussing with the ganache team.

theblock-dev avatar Mar 02 '22 06:03 theblock-dev

Has this been fixed in the new ganache version 7.0.3? Thanks

theblock-dev avatar Mar 13 '22 12:03 theblock-dev

@theblock-dev no, it's not fixed yet. This issue is actively being worked on though.

davidmurdoch avatar Mar 14 '22 02:03 davidmurdoch