question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pet Shop Test: contract code couldn't be stored

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
cgeweckecommented, Jan 25, 2018

@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.

0reactions
mmailhoscommented, Feb 3, 2018

@cgewecke Thanks so much, works like a charm!

Read more comments on GitHub >

github_iconTop Results From Across the Web

The contract code couldn't be stored, please check your gas ...
Network state unknown. Review successful transactions manually. Error: The contract code couldn't be stored, please check your gas amount. test ...
Read more >
Truffle - Pet Shop - Errors on All Tests (.sol) - Stack Overflow
Js) to ensure no typos but they are compiling so I don't think that's it. But here is the TestAdoption.sol code (in the...
Read more >
ConsenSys/truffle - Gitter
Hello, how can I overcome Error: The contract code couldn't be stored, ... hello, I'm trying pet-shop tutorial with geth client (private network)....
Read more >
Truffle Migrations Explained - SitePoint
Commands: Compile: truffle compile Migrate: truffle migrate Test contracts: truffle test. This command creates a barebones Truffle project ...
Read more >
Damages for Breach of Contract - NYU Law
•Uses different test than Hadley, but adheres to the underlying principle of ... resale price and the contract price + incidental damages –...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found