[Ethereum] Is it possible to preload contracts in the genesis block

contract-debuggingcontract-deploymentgenesisgo-ethereumtestnets

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.

Best Answer

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