13

Here's the setup I'm trying to use:

A genesis.json with the alloc field set to include code.

"alloc": {
    "0000000000000000000000000000000000000001": {
      "code": "6060604052608f8060106000396000f360606040526000357c0100000000000000000000000000000000000000000000000000000000900480634e70b1dc14604157806360fe47b114606257603f565b005b604c60048050506078565b6040518082815260200191505060405180910390f35b607660048080359060200190919050506081565b005b60006000505481565b806000600050819055505b5056"
    }
}

Then running geth and entering (with a default account set, unlocked, and loaded):

var getAndSetContract = eth.contract([{"constant":true,"inputs":[],"name":"num","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"val","type":"uint256"}],"name":"set","outputs":[],"type":"function"}])
var getAndSet = getAndSetContract.at("0000000000000000000000000000000000000001")
getAndSet.set(2, {gas: 1000000})
miner.start()
// a few blocks quickly roll in given this is on a private testing blockchain
miner.stop()
getAndSet.num()
Results in a final returned value of
3.054357634959941643970700719428062453954541146150392020568168920432276498019e+75
versus the desired
2

Here's the contract source:

contract getAndSet {
    uint public num;
    function set(uint val){
        num = val;
    }
}

This whole adventure started after coming across this line in the geth source: https://github.com/ethereum/go-ethereum/blob/290e851f57f5d27a1d5f0f7ad784c836e017c337/core/genesis.go#L56 I think I saw something about preloading contracts in the past, though I haven't been able to dig anything up. AFAIK I could be on a wild goose chase trying to make use of a half-implemented feature.

3
  • Have you tried with a higher address? Address 1 is for ecrecover (ethereum.stackexchange.com/questions/440/…) and you might be invoking it instead of your contract.
    – eth
    Commented Aug 3, 2016 at 10:38
  • I tried changing it to "1000000000000000000000000000000000000002", still doesn't work though the returned value changed to 4.3592315083973277133168013697365335412971280313784556186150828777023154946048e+76 Commented Aug 3, 2016 at 12:55
  • web3.js usually needs '0x' and it might need to be added to getAndSetContract.at("0x...
    – eth
    Commented Aug 3, 2016 at 19:23

3 Answers 3

5

How did you build yor contract ? If you want to add a contract to genesis block you need to build it with solc --bin-runtime.

By default solc and ide like remix build return code that is used for creating the contract. This code returns the actual code that is stored in the blockchain

2

Your method does not return any value. Calling set() will return the transaction hash of the calling transaction. You also need to specify the account from which you are deploying the transaction:

getAndSet.set(2, {eth.accounts[0], gas: 1000000});

(make sure you have enough balance on that account).

7
  • I'm calling the generated constant function num() which returns the variable by the same name: solidity.readthedocs.io/en/latest/… Commented Aug 3, 2016 at 12:37
  • Oh, I see. Have you tried specifying the account from which the contract deployment transaction is made?
    – Sebi
    Commented Aug 3, 2016 at 12:45
  • I have the default account set, the from field in the transaction object defaults to it Commented Aug 3, 2016 at 12:53
  • Not always. I have run into this a few days ago. It's much safer to explicitly specify all fields.
    – Sebi
    Commented Aug 3, 2016 at 12:59
  • 1
    @JeffreyB.Petersen did you find a solution ?? Commented Dec 19, 2017 at 9:52
0

I came across this old question about preloading a contract into the genesis file, so the answer is yes and for people who don't want to use solc (--bin-runtime) you can:

  • Deploy your contract into your network

  • get back the bytecode deployed by using (after attaching to geth)

    web3.eth.getCode("0xEc76BC7094f17fE3C987d3eb574C765219B7F1bF") "0x608060405260043610610112576000357c0100000000000000000000000000000000000000000000000000000000900463fffffff.."

NB: the bytecode returned byremix doesn't work

  • copy the result into your genesis file with an address of your choice as following

    "8c7aD47113641cA92b138470620A5A39b2f7B9e8": { "code":"0x608060405260043610610112576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff...."

  • restart the network

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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