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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.