Ganache problems not completely resetting contracts ... But they were not caused by ganache

Hi,

I’ like to share a short learning experiance from “beginners learning”.

Topic : Restart does not reset the ganache network

A Problem which is easy to understand, but at beginners learning drive you nuts. Something that feels like a ganache problem

Problem:

  • You get error messages from variables or function, that does not exist anymore in your contract
  • You change something in deployment or smart contract. And then nothing changes
  • You get strange error messages that does not relate to what you changed
  • You change something in you deployment script and things running more and more strange
  • Occurs if you run ganache or ganache-cli for a very long time, e.g. multiple suspends of the working environment or virtual instance
  • And your code/code snipped seems to be absolute ok

So this seems to be often an environment problem when working with ganache and truffle

Explantion:

I was not the first ran into this… from github project

Source: https://github.com/trufflesuite/ganache/issues/296

….

Ah! I finally figured out what this is all about. This isn’t a bug with Ganache or even really Truffle, but this is why this is being observed:

Get a fresh Truffle project with your contracts

Compile the contracts, this makes your build/contracts/contract.json artifact files

When you do truffle migrate, it checks the artifact file and sees that it hasn’t been deployed to the network your interested in (say your development network in truffle-config.js), so it deploys a new contract

Truffle takes the address of the new deployed instance and writes it in your artifact file

You want to change some stuff, you reset Ganache

Ganache does in fact start a new chain, so the previously deployed version of your contract is now wiped

You run truffle migrate again, but Truffle sees your artifact file has an existing address, so Truffle attempts to do a migration rather than just a deployment

Truffle does like a getCode (or similar check) on this address of a previously deployed instance, and sees there is no deployed contract there, and Truffle errors saying that it’s already deployed, but can’t find the contract at the address or something along those lines

This behavior is by design with Truffle Migrations. To prevent Truffle from looking at the existing address, you can specify the --reset option to do a new deployment. You can read more in the migration docs

If you believe there should be some sort of smarter logic to check whether or not the contract should get deployed if the existing address can’t be found, please feel free to open an issue on Truffle’s repo

Thanks for reporting this issue! Let me know if you have any questions!

Solution:

It’s not a ganache problem cause this behavior. It was the migration process that got tired.

Delete all jour json files in build/contracts/* and recompile

Well even it drive me almost crazy, but sometime you just search on the wrong side :wink: Writing smart contracs needs a broughter view when getting into troubles!

2 Likes

I run Ganache with Workspace mode which maintains the all transactions(contracts). Then I guess no problem in migration process or something like that. Of course, sometimes, need to reset all things. :smile:

1 Like

Hi @blexx,

Nice tip, sorry you had to go through that.

I have gotten into the (bad) habit of deleting my build directory during development. (I haven’t memorized the appropriate reset command).

I tend not to restart ganache-cli and is one of the very few things I have installed globally (Installing packages locally rather than globally (npx))

Thank you for the feedback.

Ganache is not the software that caused the problem. Ganache is fine.

The strange thing is, that there are objevous troubles in the truffle mirgation process.

So I assume. During learning you go through 3 steps

  • truffle compile
  • truflle migrate
  • truffle test
    As a beginner you might go through this process every time you change something. Then you learn, that typing truffle test is good engough, because it triggers a truffle complile. - But this seems to trigger the problem, especialy when using a “learning virtual machine” that keeps the state of your ganache instance alive.

In some cases a truffle test does not delete the json-API of the contract. So truffle migrate or truffle test just uses the old json-API of the former contract version. And then a really ugly play of error messages start, that takes time to identify this as a wrong json API.

So deleting manually the json-API of the contract. Then never run truffle compile maunal again. Instead run directly truffle test. So this will delay or avoid this problem in the future.

I posted this, because if you start learning openZeppelin with the SimpleContract.sol in the docs everything get’s fine. But sometimes when testing, you get strange errors from a definitly correct smart contract. Then you may go back to a contract that is absolutly ok, for example the content SimpleContract. Coyp&Paste and the problem is still there. That’s where i digged into that problem.

Workarounds:

  • renaming the contract -> works, not really a solution
  • creating a new truffle project -> works, not really a solution
  • reboot the machine -> does not help

The only way to do it right:

  • deleting the json-API manually -> the solution

I can’t judge how often I did one of the useless work arounds this topic during my process of learning. But I hope others will short cut this time when reading this thread.

thx,

Stefan

1 Like

The evil thing is, truflle migrate --reset does not solve the problem. It will reset ganache or ganache cli. I also did this.

You can imagine how strange it is to find error messages of variables, functions or migration errors (request for arguments) that have bin deleted in you contract source code.

The reason for this is, ganache got reset, but the old json-API is loaded by the migration process. And the API is used to check for errors. So you have happy days then

2 Likes

Thanks, very helpful.

1 Like