[Ethereum] Simple contract on private network is never mined

contract-debugginggo-ethereumminingprivate-blockchain

I'm trying to run the crowdsale example from the Frontier docs on a private network and local blockchain. I can create the contract, but do not seem to be able to mine it.

You can see a gist of the code I'm running in the geth console here.

In particular, the crowdsale example involves two contracts: one called token and one called Crowdsale. I can compile these fine with solc; given the contracts' source in crowdSrc and an unlocked account named 'foo', the following code creates the token contract as expected:

var crowdCompiled = eth.compile.solidity(crowdSrc);

var tokenContract = eth.contract(crowdCompiled.token.info.abiDefinition);

var token = tokenContract.new(
    10000
  , { from: foo
    , data: crowdCompiled.token.code
    , gas:  300000
    }
  , function(e, contract) {
      if(!e) {
        if(!contract.address) {
        console.log(
          "Contract transaction send: TransactionHash: " +
          contract.transactionHash + " waiting to be mined...");
      } else {
        console.log("Contract mined! Address: " + contract.address);
      }
    }
  })

This is mined almost instantaneously when I start mining with a couple of threads via miner.start(2):

Contract mined! Address: 0xaacc2f82f12a54172a0eb6b499a91578a5b7e0b5

and I can check its address:

> token.address
"0xaacc2f82f12a54172a0eb6b499a91578a5b7e0b5"

Now. crowdCompiled also contains the compiled code for the Crowdsale contract, so I can create it from the unlocked account 'foo' like so:

var crowdContract = eth.contract(crowdCompiled.Crowdsale.info.abiDefinition);

var _beneficiary = foo;
var _fundingGoal = parseInt(web3.toWei(100, 'ether'));
var _duration    = 5;
var _price       = parseInt(web3.toWei(0.02, 'ether'));
var _reward      = token.address;

var crowdsale = crowdContract.new(
    _beneficiary
  , _fundingGoal
  , _duration
  , _price
  , _reward
  , { from: foo
    , data: crowdCompiled.Crowdsale.code
    , gas:  300000
    }
  , function(e, contract) {
      if (!e) {
        if (!contract.address) {
          console.log(
            "contract transaction send: hash " + contract.transactionHash +
            " waiting to be mined.");
          } else {
            console.log("contract mined, address " + contract.address);
            }
        }});

Again it is created fine:

contract transaction send: hash 0xc813953780a364f91f339a711a301188f9c546a29915ae19fd1722e335076ad8 waiting to be mined.

But it seemingly takes far longer than expected to finish mining this contract, if it indeed finishes at all. I've kicked off miner.start(2) and waited for over fifty blocks to go through numerous times, but I've never actually managed to mine the contract.

When the miner is active I can confirm that crowdsale._eth.hashrate is a valid number (e.g. 139791, 206544, etc.). Checking crowdsale.address yields undefined.

Why is this simple contract taking so long to mine? Have I missed something, or is this behaviour possibly due to a bug? Are there any useful web3 methods or other tips I'm missing to help diagnose the problem?

Best Answer

One possibility is that you are running out of gas (i.e. deploying the contract requires more computation than you allowed). Please raise the gas limit to 3 million and try again to see if tis is the issue.

More generally, if you see issues like this you might want to increase logging to see if some error occurs and what that is exactly. One useful way would be --vmdebug which creates huge stack dumps of exactly what the EVM is doing, which if terminates with OUT OF GAS, then you immediately have your answer :)

Related Topic