Ganache-cli: Error: VM Exception while processing transaction: invalid opcode when testing on different machines

Created on 8 Oct 2017  ·  33Comments  ·  Source: trufflesuite/ganache-cli


I have TestRPC setup on my local machine and another test environment on Travis-CI (which is a continuous integration service). The test passes fine on Travis, but does gives me Error: VM Exception while processing transaction: invalid opcode on my local machine. The only difference I can tell with the environment is mine is Mac OSX and Travis is Ubuntu.

Expected Behavior

Tests should pass on local machine.

Current Behavior

Test does not pass on local machine, but passes on Travis-CI remote machine.

Possible Solution

Not sure.

Steps to Reproduce (for bugs)

  1. Start test on local machine
  2. Start test on Travis-CI
  3. Results differ.

Context

This is making it hard to trust the testing process in general. I would like to have matching results when testing my contracts.

Your Environment

Local Machine

  • Node: 8.6.0
  • Solidity: 0.4.16+commit.d7661dd9.Emscripten.clang
  • TestRPC: EthereumJS TestRPC v4.1.3 (ganache-core: 1.1.3)
  • Truffle: 3.4.11
  • Operating System and version: Mac OSX 10.12.6

Travis-CI

  • Node: v8.6.0
  • Solidity: 0.4.16+commit.d7661dd9.Emscripten.clang
  • Truffle: 3.4.11
  • TestRPC: EthereumJS TestRPC v4.1.3 (ganache-core: 1.1.3)
  • Operating System and version: Ubuntu 14.04.5 LTS
    commands used in build:

    • npm install -g ethereumjs-testrpc

    • npm install -g truffle

    • npm install bluebird

    • npm install chai

    • testrpc & truffle test

TestRPC logs

Listening on localhost:8545
net_version
eth_accounts
eth_accounts
eth_accounts
net_version
net_version
eth_sendTransaction

  Transaction: 0xf0b4502c8092b8a8d7ba34c6c40758c8930b2c97807f64e6eba766b13adf0222
  Contract created: 0x332db8d402aacb428b81a43f9ef2f3897732f8f6
  Gas usage: 201556
  Block Number: 1
  Block Time: Sun Oct 08 2017 13:45:49 GMT+0700 (+07)

eth_newBlockFilter
eth_getFilterChanges
eth_getTransactionReceipt
eth_getCode
eth_uninstallFilter
eth_sendTransaction

  Transaction: 0xcb51aea82bcdbf0af7c3c479eb029eeb41ed743eaefa594f252e86d484c23290
  Gas usage: 41965
  Block Number: 2
  Block Time: Sun Oct 08 2017 13:45:49 GMT+0700 (+07)

eth_getTransactionReceipt
eth_accounts
net_version
net_version
eth_sendTransaction

  Transaction: 0x9614ff52b77bb5df420b0efa0ad2134dc381f42fe9e508d32278a31b0bcc0839
  Contract created: 0xce7880cdbdf19bde9b4fcd9c1cb68faa36eb38bd
  Gas usage: 500820
  Block Number: 3
  Block Time: Sun Oct 08 2017 13:45:50 GMT+0700 (+07)

eth_newBlockFilter
eth_getFilterChanges
eth_getTransactionReceipt
eth_getCode
eth_uninstallFilter
net_version
net_version
eth_sendTransaction

  Transaction: 0x89230fe3e909768a3121074d1a72540f153ee1f6c4e493dfe01f3e9cf1158dc5
  Contract created: 0xc0e49f512de2ff823a5ff5bbcedd421a23a9dcee
  Gas usage: 68990
  Block Number: 4
  Block Time: Sun Oct 08 2017 13:45:50 GMT+0700 (+07)

eth_newBlockFilter
eth_getFilterChanges
eth_getTransactionReceipt
eth_getCode
eth_uninstallFilter
net_version
net_version
eth_sendTransaction

  Transaction: 0x742e9fb077b78990851bcecd890eb7c37a68b80cd06242343db32b38364673b8
  Contract created: 0xae553b01715d564ecfd3b3624f65da12d8691cfc
  Gas usage: 1080704
  Block Number: 5
  Block Time: Sun Oct 08 2017 13:45:50 GMT+0700 (+07)

eth_newBlockFilter
eth_getFilterChanges
eth_getTransactionReceipt
eth_getCode
eth_uninstallFilter
eth_sendTransaction

  Transaction: 0x9da29ec48e4626ef9a786e71282251f346a13000c381eb647b99b24ba77187be
  Gas usage: 26965
  Block Number: 6
  Block Time: Sun Oct 08 2017 13:45:50 GMT+0700 (+07)

eth_getTransactionReceipt
evm_snapshot
Saved snapshot #1
eth_blockNumber
evm_snapshot
Saved snapshot #2
net_version
eth_sendTransaction

  Transaction: 0xac2805988d39e5104b12a976916f10a37f2e6da3115e92648a30e78337376183
  Contract created: 0x181cebc385d36bcf90bcd050e45428be8490e0fc
  Gas usage: 4712388
  Block Number: 7
  Block Time: Sun Oct 08 2017 13:45:50 GMT+0700 (+07)
  Runtime Error: invalid opcode

evm_revert
Reverting to snapshot #2
evm_snapshot
Saved snapshot #2

Most helpful comment

@petermunnings How did you do that? I mean I have "Runtime Error: revert" without any details. How to understand what exactly goes wrong ?

All 33 comments

I have just confirmed that the tests pass on a colleague's machine and Travis-CI machine, but does not on mine.

I have completely uninstalled all traces of Node, NPM, Truffle, and TestRPC. Reinstalled everything from scratch and still nothing.

I am having exactly the same problem

  Transaction: 0x72bcdd35b07495f3a75778cc99b3934d9ec0ef0e7ecb148d6ee5fa063de49052
  Gas usage: 4712388
  Block Number: 13
  Block Time: Tue Oct 31 2017 13:41:52 GMT+0200 (SAST)
  Runtime Error: invalid opcode

EthereumJS TestRPC v4.1.3 (ganache-core: 1.1.3)

Could you post your contracts?

I discovered it was a problem in the contract itself. Testrpc is fine

On 03 Nov 2017 8:41 PM, "Abraham Elmahrek" notifications@github.com wrote:

Could you post your contracts?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/ethereumjs/testrpc/issues/397#issuecomment-341792540,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AB6hnUmVwMnUT2WjXbbQeIZYQDrGPXa6ks5sy15tgaJpZM4PxnmX
.

Awesome. Just for history, was it an error in the constructor of a contract? If not, where was it?

No in one of the methods. Not the constructor.

On 03 Nov 2017 9:50 PM, "Abraham Elmahrek" notifications@github.com wrote:

Awesome. Just for history, was it an error in the constructor of a
contract? If not, where was it?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/ethereumjs/testrpc/issues/397#issuecomment-341808658,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AB6hnU6bbyjjuXKFBOlXBtsFel-359zVks5sy256gaJpZM4PxnmX
.

Awesome. FWIW I had a similar issue where I had a require statement in the constructor. It would be cool if there were some compiler checks for this kind of thing.

@petermunnings How did you do that? I mean I have "Runtime Error: revert" without any details. How to understand what exactly goes wrong ?

@dwalintukan are you still experiencing this problem on TestRPC v6.0.1? What version of the solidity compiler are you using? My suspicion is that the invalid opcode message was correct, expected behaviour. As of solc 0.4.12, they started using the REVERT opcode, which was unknown to older versions of the EVM. TestRPC updated to a REVERT aware version during the v5.0.1 beta. However there were some small problems in 5.0.1, including one where we had misnamed a field in one of our RPC responses, so 6.0.1 is recommended now.

Hello @abec @petermunnings
I am also facing the same issue. I am using open-zeppelin contracts and imported those contracts to create my contract and override createTokenContract method to create my token.
I am using EthereumJS TestRPC v4.1.3 (ganache-core: 1.1.3), Ubuntu 16.04.
As @abec suggested to check require inside the constructor, the open-zeppelin contracts has these require statements, so I guess this should not be a problem.
Where else should I look for the solution?

@parth5049 can you give the latest TestRPC a try? It should be v6.0.3 as of today.

@parth5049 also what version of the solidity compiler are you using?

Do all of your tests pass?
As my contracts compile and migrate successfully, all tests pass successfully however, Truffle fails with the error:

truffle(develop)> /usr/local/lib/node_modules/truffle/build/cli.bundled.js:320098
        throw reason;
        ^

Error: VM Exception while processing transaction: invalid opcode

After the tests have ran.

I found that the reason this was happening on mine, was because I throw an exception when a non-owner of the contract and tries to call a function which requires onlyOwner. This causes Truffle to throw the invalid opcode exception.

http://truffleframework.com/tutorials/testing-for-throws-in-solidity-tests

Here is a link which describes how to run tests with a ThrowProxy contract if anyone else has this issue.

I believe I fixed this issue by deleting my build folder and recompiling again. It was so long ago so I am not absolutely sure.

Hello @benjamincburns
I am using following versions
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
solc Version: 0.4.18+commit.9cf6e910.Linux.g++

For those still experiencing this issue, are you using truffle test to run your tests? If so, are you certain that you've configured Truffle to connect to testrpc/ganache? Are you seeing console logs from testrpc or ganache-cli indicating that truffle is hitting it correctly?

I ask because Truffle 4 has a feature which will use an embedded version of ganache-core when you run truffle test without a default network configured. Until Truffle 4.0.3 was released (yesterday, I think?), this embedded version of ganache-core was a bit outdated, and didn't include the Byzantium hardfork changes, which would cause this invalid opcode error.

@parth5049 for you specifically, can you please either npm install -g truffle@latest or npm install --saveDev truffle@latest (depending on how you've installed truffle) and confirm whether this problem still persists?

Probably not be the issue for the other users, but, may help some...

I have been working with a hardcoded contract address.

I hadn't yet setup a contract (or a contract address to interact with) on the new environment using testrpc or ganache-cli.

Hello there,

I'm experiencing the same problem here. I'm trying to compile the Minimum Viable Token Contract.

pragma solidity ^0.4.17;

contract MyToken {
  /* This creates an array with all balances */
  mapping (address => uint256) public balanceOf;

  /* Initializes contract with initial supply tokens to the creator of the contract */
  function MyToken(uint256 initialSupply) public {
    balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens
  }

  /* Send coins */
  function transfer(address _to, uint256 _value) public {
    require(balanceOf[msg.sender] >= _value);           // Check if the sender has enough
    require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
    balanceOf[msg.sender] -= _value;                    // Subtract from the sender
    balanceOf[_to] += _value;                           // Add the same to the recipient
  }
}

Here's my test, which usually passes:

var MyToken = artifacts.require('MyToken');

contract('MyToken', function(accounts) {
  it('transfer tokens correctly', function() {
    var contract = null;

    var accountOne = accounts[0];
    var accountTwo = accounts[1];

    var accountOneStartBalance = web3.fromWei(web3.eth.getBalance(accountOne), 'ether').toNumber();
    var accountTwoStartBalance = web3.fromWei(web3.eth.getBalance(accountTwo), 'ether').toNumber();
    var accountOneEndingBalance = null;
    var accountTwoEndingBalance = null;

    var amount = 10;

    MyToken.deployed().then(function(instance) {
      contract = instance;
      return contract;
    })
    .then(function(contract) {
      contract.transfer(accountTwo, amount, { from: accountOne });
    })
    .then(function() {
      var accountOneEndingBalance = web3.fromWei(web3.eth.getBalance(accountOne), 'ether').toNumber();
      var accountTwoEndingBalance = web3.fromWei(web3.eth.getBalance(accountTwo), 'ether').toNumber();
    })
    .then(function() {
      assert.equal(accountOneEndingBalance, accountOneStartBalance - amount, "Amount wasn't correctly taken from the sender");
      assert.equal(accountTwoEndingBalance, accountTwoStartBalance + amount, "Amount wasn't correctly sent to the receiver");
    });
  });
});

Compiling ./contracts/MyToken.sol...

Contract: MyToken
✓ transfer tokens correctly (189ms)

1 passing (396ms)

But it later fails when the contract is invoked from truffle console:

MyToken $ truffle console
truffle(development)> var myTokenContract = null;
undefined
truffle(development)> MyToken.deployed().then(function(instance) { myTokenContract = instance; });
undefined
truffle(development)> var account1 = web3.eth.accounts[2];
undefined
truffle(development)> var account2 = web3.eth.accounts[3];
undefined
truffle(development)> myTokenContract.transfer(account2, 5, { from: account1 });
Error: VM Exception while processing transaction: revert
at Object.InvalidResponse (/Users/jose/.nvm/versions/node/v8.9.3/lib/node_modules/truffle/build/cli.bundled.js:41484:16)
at /Users/jose/.nvm/versions/node/v8.9.3/lib/node_modules/truffle/build/cli.bundled.js:329530:36
at /Users/jose/.nvm/versions/node/v8.9.3/lib/node_modules/truffle/build/cli.bundled.js:325200:9
at XMLHttpRequest.request.onreadystatechange (/Users/jose/.nvm/versions/node/v8.9.3/lib/node_modules/truffle/build/cli.bundled.js:328229:7)
at XMLHttpRequestEventTarget.dispatchEvent (/Users/jose/.nvm/versions/node/v8.9.3/lib/node_modules/truffle/build/cli.bundled.js:176415:18)
at XMLHttpRequest._setReadyState (/Users/jose/.nvm/versions/node/v8.9.3/lib/node_modules/truffle/build/cli.bundled.js:176705:12)
at XMLHttpRequest._onHttpResponseEnd (/Users/jose/.nvm/versions/node/v8.9.3/lib/node_modules/truffle/build/cli.bundled.js:176860:12)
at IncomingMessage. (/Users/jose/.nvm/versions/node/v8.9.3/lib/node_modules/truffle/build/cli.bundled.js:176820:24)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)

TestRPC Output for that Transaction is as follows:

eth_sendTransaction

Transaction: 0x7cb18101e89840f9c90303910e7704aa339096b07ad47c3c29771ec2d7b1b42c
Gas usage: 23339
Block Number: 30
Block Time: Sat Dec 30 2017 21:40:07 GMT-0500 (-05)
Runtime Error: revert

If I remove the require statements from the Contract's transfer() function then it works, so I guess this is a Bug?

I'm using the Latest Truffle 4.0.4.

@jlstr please give truffle debug a try. This will help you to trace through what failed and why.

truffle debug doesn't work well with inheritance

I'm closing this issue because it's no longer tracking the original problem (that the original submitter was seeing different results on different machines). To those finding this issue after the fact, please see my advice in this comment, and/or open a new issue to discuss your problem.

👍 Yes, my original post was related to the build folder having to be deleted, which seems to be fixed in the latest version.

Just for information, I just had the same error (inconsistency between Travis and my local tests). I updated testrpc and the latest truffle version on both and I still had the same error.
I solved the issue by installing the ganache-cli package instead of ethereumjs-testrpc.

I updated/installed again truffle, testrpc and ganache-cli and this resolved my Error: VM Exception while processing transaction: invalid opcode issue :

npm install -g truffle@latest
npm install -g ethereumjs-testrpc@latest
npm install -g ganache-cli@latest

Same issue here.

macOS Mojave 10.14.2
Truffle v5.0.22 (core: 5.0.22)
Solidity v0.5.0 (solc-js)
Node v10.15.0
Web3.js v1.0.0-beta.37

In window my code runs OK.

@Chococoin, can you try updating truffle to latest (v5.0.26) and let me know if you still have issues?

@Chococoin, can you try updating truffle to latest (v5.0.26) and let me know if you still have issues?

@davidmurdoch, I'm sorry about that, but I've upgraded and the same problem arises with the latest wich is Truffle v5.0.28 already.

@Chococoin can you file a new Issue with steps to reproduce?

Having the same issue. Please link if you file a new issue. Thanks.

@Chococoin can you file a new Issue with steps to reproduce?

As soon as I get some time I'll do.

I have the same issue, caused by smart contract, you can use my smart contract. It will work
pragma solidity ^0.5.8;
contract Fcontracts {
mapping (address => uint) fcontracts;
function updateFcontracts(uint fcontract) public {
fcontracts[msg.sender] = getFcontracts(msg.sender) + fcontract;
}
function getFcontracts(address addr) public view returns(uint) {
return fcontracts[addr];
}
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ralph-pichler picture ralph-pichler  ·  6Comments

Tectract picture Tectract  ·  3Comments

gskerry picture gskerry  ·  3Comments

xavierlepretre picture xavierlepretre  ·  5Comments

SCBuergel picture SCBuergel  ·  5Comments