Truffle: Pet Shop Test: contract code couldn't be stored

Created on 11 Jan 2018  路  7Comments  路  Source: trufflesuite/truffle

Issue

Following the pet shop tutorial, I ran into a gas limit issue running on my private blockchain. I asked on Gitter but could not find an answer about setting the right gas amount for running the tests.
Apparently, there is enough gas to store the contract as I see it deployed on the blockchain (INFO [01-11|16:03:06] Submitted contract creation) but not enough to run the code.
Truffle.js is supposed to have enough gas.

Steps to Reproduce

My blockchain is running with:

geth --datadir ./pethshop-chaindata --rpc --rpcaddr "localhost" --rpcport 8545 --rpcapi eth,web3,personal,net --rpccorsdomain "*" --nodiscover --unlock 0 --mine 1

A very minimal test file example that fails:

pragma solidity ^0.4.17;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/Adoption.sol";

contract TestAdoption {
  Adoption adoption = Adoption(DeployedAddresses.Adoption());

  function testUserCanAdoptPet() public {
    uint returnedId = adoption.adopt(8);

    uint expected = 8;

    Assert.equal(returnedId, expected, "Adoption of pet ID 8 should be recorded.");
  }
}

My two migration files are the following:

_migrations/1_initial_migration.js_

var Migrations = artifacts.require("./Migrations.sol");

module.exports = function(deployer) {
  deployer.deploy(Migrations,  {gas: 10000});
};

_migrations/2_deploy_contracts.js_

var Adoption = artifacts.require("Adoption");

module.exports = function(deployer) {
  deployer.deploy(Adoption,   {gas: 10000});
};

_truffle.js_

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "15",
      gas: 0x10000000,
      from: "0x0cddd..."
    }
  }
};

My private blockchain has the following gas limit: "gasLimit": "0x80000000"

Expected Behavior

The simple test should pass.

Actual Result

$ truffle test
  TestAdoption
    1) "before all" hook: prepare suite
  0 passing (5s)
  1 failing
  1) TestAdoption "before all" hook: prepare suite:
     Error: The contract code couldn't be stored, please check your gas amount.

Environment

  • Operating System: Ubuntu 17.04
  • Truffle version: Truffle v4.0.4 (core: 4.0.4)
  • Ethereum client: 1.7.3-stable

Most helpful comment

@MathieuMailhos Finally got this to work :) I've used geths dev mode instead of the regular mining mode because it's easier to set up for testing, comes with unlocked accounts, and is quite a bit faster. Without specifying any genesis or chain data you can just run:

geth --dev --dev.period 1 --rpc --targetgaslimit 0xfffffffff

In a separate terminal window run:

geth attach http://127.0.0.1:8545

> eth.getBlock('latest')
{
  difficulty: 2,
  extraData: "0xd883010703846765746887676f312e392e328664617277696e00000000000000fe78ce6ab95947c2b21f9811bb14798572b39f65fae94951cb3b5d6b7a6bdfa85211a36a774ff03c0dae2bec9299b99c4823b8a9ef435196b3471717ea36f4ee01",
  gasLimit: 6320079,
  gasUsed: 0,
  ... etc ..
}

Then set the gas field of the Truffle config to slightly less than the gasLimit of the most recent block:

networks: {
    geth: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*",
      gas: 6300000,
    }
  }

Then run truffle test --network geth.

Let me know if that doesn't work for you.

All 7 comments

@MathieuMailhos Hi. I've tried to reproduce this error on Pet Shop on ganache-cli with a gas limit of 0x80000000 and your truffle config.

Running:

truffle test --network development

results in Error: Error: base fee exceeds gas limit (from ganache).

However, if the migrations files are rewritten so that no gas is specified for deployment, e.g.

module.exports = function(deployer) {
  deployer.deploy(Adoption);
};

the tests succeed. It looks like the gas value specified in the config is high enough to deploy the contract, but its being overwritten by the much lower values in the migrations.

Does making this change cause it to succeed on Geth?

@MathieuMailhos I'm going to close this for house-keeping since it seems like too little gas was the problem, but if not please feel free to re-open. Thanks so much!

Thanks a lot for your answer. Unfortunately, this change did not cause it to succeed on Geth.
Updating initial post with a specified from in truffle.js also to use proper address.

Same problem for me...

Running migration: 2_deploy_contracts.js
  Deploying BlockchainvestTokenCrowdsale...
  ... 0xc46a28d51e7b3f9c39aaeaf9d5ebedf2012babb13913e4387d204ab95d1b39cd
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: The contract code couldn't be stored, please check your gas amount.

Source: https://github.com/LeRoyJulian/BlockchainvestToken

@MathieuMailhos Finally got this to work :) I've used geths dev mode instead of the regular mining mode because it's easier to set up for testing, comes with unlocked accounts, and is quite a bit faster. Without specifying any genesis or chain data you can just run:

geth --dev --dev.period 1 --rpc --targetgaslimit 0xfffffffff

In a separate terminal window run:

geth attach http://127.0.0.1:8545

> eth.getBlock('latest')
{
  difficulty: 2,
  extraData: "0xd883010703846765746887676f312e392e328664617277696e00000000000000fe78ce6ab95947c2b21f9811bb14798572b39f65fae94951cb3b5d6b7a6bdfa85211a36a774ff03c0dae2bec9299b99c4823b8a9ef435196b3471717ea36f4ee01",
  gasLimit: 6320079,
  gasUsed: 0,
  ... etc ..
}

Then set the gas field of the Truffle config to slightly less than the gasLimit of the most recent block:

networks: {
    geth: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*",
      gas: 6300000,
    }
  }

Then run truffle test --network geth.

Let me know if that doesn't work for you.

@LeRoyJulian I believe the problem you're facing is specific to deploying a Crowdsale contract, rather than Solidity testing against a non-ganache client. I'd encourage you to look in the zeppelin-solidity issues for a solution, although feel free to open a separate issue at Truffle if you discover a clearly reproducible bug.

@MathieuMailhos Closing, happy to re-visit if you continue to have problems with with.

@cgewecke Thanks so much, works like a charm!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timothywangdev picture timothywangdev  路  3Comments

oed picture oed  路  3Comments

ferittuncer picture ferittuncer  路  3Comments

bmmpxf picture bmmpxf  路  3Comments

tcurdt picture tcurdt  路  3Comments