7

When I compiled following code at "https://ethereum.github.io/browser-solidity/" it gives me two Web3 deploy code for:

var libraryContract  = web3.eth.contract(...); //Web3 deploy code
var contractContract = web3.eth.contract(...); //Web3 deploy code

Then I have copy paste Web3 deploy code for (libraryContract, contractContract) into geth in order to mine my contracts.

My code (I have trimmed most of it for simplicity):

library Library {
    function func () constant returns (uint8) {
        return 5;
    }
}

contract Contract {
    uint8 value;
    function call_library_function () {
        value = Library.func();
    }
}

"Library"'s Web3 deploy code mines perfectly fine, but when I try to mine "Contract"'s Web3 deploy code, I get the following error:

> Error: The contract code couldn't be stored, please check your gas amount. undefined

If I remove the following line from my Contract Link code, newly generated "Contract"'s Web3 deploy code mines! without any problem:

value = Library.func();

[Q] My question is, how could I fix this problem I am having? What should I do to call my function inside a Library from a Contract, which will allow me to mine it?

How I did setup my private Ethereum Blockchain: Please see on the following link: How could I update default homesteadBlock value on my private Ethereum blockchain?

Thank you for your valuable help and time.

alper
  • 8,395
  • 11
  • 63
  • 152

1 Answers1

5

From Library contract method failing #2831:

You probably run geth with the --dev option to create a private network? In that case use the init subcommand to create a private chain. Instructions can be found in the wiki. Or use the testnet.

The problem is that this code uses the delegatecall which is added in the homestead release. The --dev flag uses the homestead block number of the mainnet which is 1150000. Any transaction before this block will encounter an invalid opcode, consumes all gas and all state changes are reverted.

To work around this issue:

...

The genesis config file supports a config section. Currently the only thing you can configure is the homestead block number. If you don't specify it, it will be defaulted to the mainnet number which is 1150000. So basically your contract will work after this block.

Try the following genesis file:

{
  "config":{
     "homesteadBlock":"5"
  },
  "nonce":"0x0000000000000042",
  "timestamp":"0x0",
  "parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
  "extraData":"0x0",
  "gasLimit":"0x8000000",
  "difficulty":"0x400",
  "mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase":"0x3333333333333333333333333333333333333333",
  "alloc":{

  }

}

If the contract is deployed in block <5 it will give the gas limit problem.

Deployed in block >= 5 it should work as intended.

Further information on using the genesis file can be found at geth init, what are the arguments?.



Update 10:33 Oct 14 2016

I have set up my private blockchain using the following procedures:


1. Setup genesis.json

Wocket:ESE bok$ more genesis.json 
{
    "config": {
            "homesteadBlock": 10
    },
    "nonce": "0",
    "difficulty": "0x400",
    "mixhash": "0x00000000000000000000000000000000000000647572616c65787365646c6578",
    "coinbase": "0x0000000000000000000000000000000000000000",
    "timestamp": "0x00",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x",
    "gasLimit": "0x3B4A1B44",
    "alloc": {}
}


2. Password file

I created a password file testpassword containing my account password


3. Blockchain Initialisation Script

I created a script to initialise my blockchain:

Wocket:ESE bok$ more 02_runGethGenesisInit 
#!/bin/sh

geth --datadir /Users/bok/ESE/gethgenesis init /Users/bok/ESE/genesis.json
geth --datadir /Users/bok/ESE/gethgenesis --password testpassword account new

And I used chmod 700 02_runGethGenesisInit to set the executable bit.


4. Private Node Script

I created a script to start my private geth node in mining mode:

Wocket:ESE bok$ more 02_runGethGenesis 
#!/bin/sh

geth --datadir /Users/bok/ESE/gethgenesis --unlock 0 --password /Users/bok/ESE/testpassword --rpc --rpccorsdomain '*' --mine --minerthreads 1 console

And I used chmod 700 02_runGethGenesis to set the executable bit.


5. Browser Solidity

I downloaded Browser Solidity from https://github.com/ethereum/browser-solidity/tree/gh-pages.

I unzipped the files and loaded index.html into my web browser.


6. Start it all

I initialised my blockchain data using the following command:

Wocket:ESE bok$ ./02_runGethGenesisInit 

I started my node using the following command, and have been running it for a while:

Wocket:ESE bok$ ./02_runGethGenesis
...
I1015 09:56:14.364062 miner/worker.go:342]   Mined block (#5210 / f881b1be). Wait 5 blocks for confirmation
I1015 09:56:14.364333 miner/worker.go:539] commit new work on block 5211 with 0 txs & 0 uncles. Took 281.474µs
I1015 09:56:14.364360 miner/worker.go:435]    Mined 5 blocks back: block #5205
I1015 09:56:14.364492 miner/worker.go:539] commit new work on block 5211 with 0 txs & 0 uncles. Took 114.592µs
I1015 09:56:16.394886 miner/worker.go:342]   Mined block (#5211 / 420c7edc). Wait 5 blocks for confirmation
I1015 09:56:16.395189 miner/worker.go:539] commit new work on block 5212 with 0 txs & 0 uncles. Took 262.993µs
I1015 09:56:16.395222 miner/worker.go:435]    Mined 5 blocks back: block #5206


7. Testing For The Homestead Mode

I loaded the test script by @TjadenHess from How could I update default homesteadBlock value on my private Ethereum blockchain? into Solidity Browser:

contract TestHomestead{
    function test () returns(bool){
        return address(4).delegatecall(1);
    }
}

I connected Solidity Browser to my local private node by clicking on the block icon and selecting Web3 Provider. I then clicked on Create to deploy the contract above to the blockchain. I clicked test and the message Transaction cost: 21657 gas. shows that my blockchain is in Homestead mode.

enter image description here

enter image description here

Note: If I alter the function to make it a constant function test () constant returns(bool){, I get the following results for a Homestead mode blockchain - true:

enter image description here

See section 9. below for the message displayed when the blockchain is in non-Homestead mode.


8. Testing The Library Call Source Code

I've modified the library call source code slightly to make the value variable public, and loaded this into Browser Solidity :

library Library {
    function func () constant returns (uint8) {
        return 5;
    }
}

contract Contract {
    uint8 public value;
    function call_library_function () {
        value = Library.func();
    }
}

I clicked Create to deploy the code, call_library_function to execute the method, and value to view the library call function result:

enter image description here

enter image description here

Success

The message Transaction cost: 41914 gas. and the value of value shows that the library function call succeeded.


9. What Happens When My Node Is In Non-Homestead Mode

I connected Browser Solidity with a geth --dev blockchain:

geth --dev --datadir /Users/bok/ESE/gethdata --unlock 0 --password /Users/bok/ESE/testpassword --rpc --rpccorsdomain '*' --mine --minerthreads 1 console

I ran the same Homestead test code to produce the message Gas required exceeds limit: 50000000 showing that this --dev blockchain is in non-Homestead mode:

enter image description here

Note: If I alter the function to make it a constant function test () constant returns(bool){, I get the following results for a non-Homestead mode blockchain - false:

enter image description here

BokkyPooBah
  • 40,274
  • 14
  • 123
  • 193
  • Thank you sir. Did it worked with you when gas Limit is updated as 5000000? – alper Oct 11 '16 at 12:40
  • Sorry. My Browser Solidity is offline at the moment. Rebuilding my notebook as an software upgrade caused it to stop working. Deploy your contract without the list.append("abc","111"); and check how much gas it takes. Then add 10% more and see if this solves your problem.

    I note that you are writing your code and then deploying into geth directly. You should be able to connect Browser Solidity to your local geth instance by selecting the Web3 Provider in the third icon from the left - refer to the screen image in http://ethereum.stackexchange.com/a/8970/1268.

    – BokkyPooBah Oct 11 '16 at 12:44
  • Ok I am trying your suggestion. I am running geth on my private ethereum blockchain. I have forced to run geth -targetgaslimit "994712388". My gas limit increasing from 470000, right now at 500000. Solidity browser always give 470000 for the gas usage, so I am updating it manually the "gas: " value for the contract. Hopefully I will find a gas that the contract will be mined. – alper Oct 11 '16 at 12:56
  • I you want to deploy via geth, see the step-by-step guide http://ethereum.stackexchange.com/questions/2751/deploying-the-greeter-contract-via-the-geth-cli-is-not-registering-in-my-private for an example on how to specify gas for the contract deployment. – BokkyPooBah Oct 11 '16 at 13:42
  • I updated gas limit up to "15529600", still same error. – alper Oct 11 '16 at 18:34
  • I have done suggestion but again it seems like it won't work. I am still working on it. Thanks for your help. – alper Oct 12 '16 at 14:16
  • 1
    I have updated my answer with a link for using the genesis file. – BokkyPooBah Oct 12 '16 at 14:36
  • Thank you sir, based on your guidance I have updated my question how I created my private Ethereum blockchain. But still it gives me the same error. Is there anything wrong about how I created my private ethereum blockchain? – alper Oct 12 '16 at 18:22
  • 1
    I've updated my answer with a step-by-step guide to deploying the library function call successfully. – BokkyPooBah Oct 14 '16 at 23:18
  • 1
    Thank you very much :) I appreciate your valuable help. I am working on it to make it work on my side. – alper Oct 15 '16 at 03:32
  • 1
    Well it works!! – alper Oct 15 '16 at 09:03
  • Is it possible to do your answer also using solc? @BokkyPooBah – alper Jan 02 '17 at 17:16
  • 1
    @Avatar, see http://ethereum.stackexchange.com/a/11031/1268 – BokkyPooBah Jan 02 '17 at 22:19
  • Would it be possible to update your answer with new solidity browser? Solidity-browser's GUI has been changed and I am not able to deploy my contract through it :( @BokkyPooBah – alper Nov 25 '17 at 10:37