1

I keep getting this "Error: The contract code couldn't be stored, please check your gas amount." message when I try to deploy my contract to the blockchain.

I read that is a generic error so I'd like to ask you for your help to find the problem.

Here is my contract's code, im trying to store in a byte32 array some sha256 hashes and then check if some hash already exists in the array.

pragma solidity ^0.4.0;

contract Mortal {
   address owner;

   function Mortal() public { owner = msg.sender; }

   function kill() public { if (msg.sender == owner) selfdestruct(owner); }
}

contract Log is Mortal {
    bytes32[] id;

    function Log (string data) public Mortal(){
        id.push(sha256(abi.encodePacked(data)));
    }

    function mainId() public view returns (bytes32) {
        return id[0];
    }

    function check(string data) public view returns (bool){
        bytes32 d = sha256(abi.encodePacked(data));
        uint i = 0;
        bool enc = false;
        while(!enc && i < id.length){
            if(id[i] == d){
                enc = true;
            }
            i++;
        }

        return enc;
    }
    function add(string data) public {
        id.push(sha256(abi.encodePacked(data)));
    }
}

And here is my web3deploy code:

var data = "12345";
var logContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"mainId","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"data","type":"string"}],"name":"add","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"data","type":"string"}],"name":"check","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"data","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
var log = logContract.new(
   data,
   {
     from: web3.eth.accounts[0], 
     data: '0x608060405234801561001057600080fd5b506040516106f83803806106f883398101806040528101908080518201929190505050336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060016002826040516020018082805190602001908083835b6020831015156100b0578051825260208201915060208101905060208303925061008b565b6001836020036101000a0380198251168184511680821785525050505050509050019150506040516020818303038152906040526040518082805190602001908083835b60208310151561011957805182526020820191506020810190506020830392506100f4565b6001836020036101000a0380198251168184511680821785525050505050509050019150506020604051808303816000865af115801561015d573d6000803e3d6000fd5b5050506040513d602081101561017257600080fd5b8101908080519060200190929190505050908060018154018082558091505090600182039060005260206000200160009091929091909150906000191690555050610536806101c26000396000f300608060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806314d6d2141461006757806341c0e1b51461009a578063b0c8f9dc146100b1578063b6f921ad1461011a575b600080fd5b34801561007357600080fd5b5061007c61019b565b60405180826000191660001916815260200191505060405180910390f35b3480156100a657600080fd5b506100af6101bd565b005b3480156100bd57600080fd5b50610118600480360381019080803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919291929050505061024e565b005b34801561012657600080fd5b50610181600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290505050610390565b604051808215151515815260200191505060405180910390f35b6000600160008154811015156101ad57fe5b9060005260206000200154905090565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561024c576000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b565b60016002826040516020018082805190602001908083835b60208310151561028b5780518252602082019150602081019050602083039250610266565b6001836020036101000a0380198251168184511680821785525050505050509050019150506040516020818303038152906040526040518082805190602001908083835b6020831015156102f457805182526020820191506020810190506020830392506102cf565b6001836020036101000a0380198251168184511680821785525050505050509050019150506020604051808303816000865af1158015610338573d6000803e3d6000fd5b5050506040513d602081101561034d57600080fd5b8101908080519060200190929190505050908060018154018082558091505090600182039060005260206000200160009091929091909150906000191690555050565b6000806000806002856040516020018082805190602001908083835b6020831015156103d157805182526020820191506020810190506020830392506103ac565b6001836020036101000a0380198251168184511680821785525050505050509050019150506040516020818303038152906040526040518082805190602001908083835b60208310151561043a5780518252602082019150602081019050602083039250610415565b6001836020036101000a0380198251168184511680821785525050505050509050019150506020604051808303816000865af115801561047e573d6000803e3d6000fd5b5050506040513d602081101561049357600080fd5b8101908080519060200190929190505050925060009150600090505b801580156104c1575060018054905082105b156104ff5782600019166001838154811015156104da57fe5b90600052602060002001546000191614156104f457600190505b8160010191506104af565b8093505050509190505600a165627a7a7230582010ec72b950c87e36e8b36c492e1c6087adf278c914cae12487acc2669cbf87b50029', 
     gas: '50000000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

I have enough funds in my account so I dont really know whats going on. Im using Remix to create and compile the code and Geth to deploy the contract into the blockchain. I guess the problem may be in the solidity code, but i cant see anything wrong.

Update

This is my Genesis Json, maybe I have wrong settings or something. I set up gas limit to a very high value as you can see, so I shouldnt reach the block limit with this contract.

{
    "config": {  
        "chainId": 1297827, 
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "difficulty": "0x400",
    "gasLimit": "0x8000000000",  
    "alloc": {
        "0000000000000000000000000000000000000001": {
          "balance": "1"
        },
        "0000000000000000000000000000000000000002": {
          "balance": "1"
        },
        "0000000000000000000000000000000000000003": {
          "balance": "1"
        },
        "0000000000000000000000000000000000000004": {
          "balance": "1"
        }
    }
}

Update 2.0

I find out that the problem resides in the Log constructor. If I remove the id.push(sha256(abi.encodePacked(data))); line I can actually deploy the contract into the chain, but I dont know why and how to make It work just the way it is. Definitely, the problem is in the sha256 call, but i dont know why it is failing.

Kurosh D.
  • 74
  • 7
  • As posted, the only issue is the gas amount. 50M gas is above the block gas size limit. Lower it to 5M gas. It worked for me in Truffle console after making that change. If you still see issues, try explicitly adding `gasPrice` to your transaction object. Finally, you say that you have enough funds but frequently people connect to the wrong network or are using a different address than expected. Have you confirmed the balance in the console using `web3.eth.getBalance(web3.eth.accounts[0])`? – Adam Kipnis Jul 03 '18 at 00:24
  • @AdamKipnis I already tried with 5M and adding gasPrice and still didnt work. Im using _--networkid_ flag to connect to my private network and I confirmed too that my account's balance is _565000000000000000000_. Im updating my original post with my Genesis json just in case the error is over there. – Kurosh D. Jul 03 '18 at 09:47
  • Weird problem. Is there a difference if you use `keccak256` instead of `sha256`? How about if you change your `pragma` to a later version (0.4.24)? I haven't been able to reproduce your problem, so I'm just firing off some possibilities. Hopefully this helps. – Adam Kipnis Jul 03 '18 at 15:44
  • 1
    @Adam It worked! I dont know if it was the pragma version or the keccak function, but I implemented both changes and now everything is working. – Kurosh D. Jul 03 '18 at 16:37

0 Answers0