[Ethereum] Fatal: failed to write genesis block: wrong genesis block in database

ethergenesisgo-ethereum

I was trying to set-up ethereum private test-net. I was able to create a custom Genesis block and create an account. Now I modified my customGenesis.json to preallocate ethers to my account, I was hit by:

Fatal: failed to write genesis block: wrong genesis block in database
(have 6650a0ac6c5e8054, new 423e5a9e61bce67e)

Steps to reproduce:

  1. setup Genesis state:

    geth –identity "Prashant" –fast –cache=1024 –rpc –rpcport "8013"
    –rpccorsdomain "*" –datadir "myPrivateNetwork2" –port "30312" –rpcapi "db,eth,net,web3,admin,debug,miner,personal,web3" –networkid 1902 –nat "any" init customGenesis.json console

  2. Start geth without init to create account:

    geth –identity "Prashant" –fast –cache=1024 –rpc –rpcport "8013"
    –rpccorsdomain "*" –datadir "myPrivateNetwork2" –port "30312" –rpcapi "db,eth,net,web3,admin,debug,miner,personal,web3" –networkid 1902 –nat "any" -ipcpath "/home/prsingh/.ethereum/geth.ipc" console

  3. Create an account in geth console

    personal.newAccount("my_password");

  4. Modify customeGenesis.json to preallocate ethers

    "alloc":{
         "0xe43ee2af5a41385ff951f0c3fe2f5156287757a6":
         {"balance": "20000000000000000000" }
     }
    
  5. Again run geth with init

    geth –identity "Prashant" –fast –cache=1024 –rpc –rpcport "8013"
    –rpccorsdomain "*" –datadir "myPrivateNetwork2" –port "30312" –rpcapi "db,eth,net,web3,admin,debug,miner,personal,web3" –networkid 1902 –nat "any" init customGenesis.json console

At step 5, I am getting the wrong genesis block in database error. I am quite sure this is because I have already run the geth with init param so when I am trying to run it the second time, geth gives error because these in already a genesis block.

But then the question is:
How will I pre-allocate ethers to my account?

Here is my customGenesis.json after adding funds to my account.

{
    "nonce": "0x0000000000000042",
    "timestamp": "0x0",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x00",
    "gasLimit": "0x8000000",
    "difficulty": "0x400",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x3333333333333333333333333333333333333333",
    "config": {
        "chainId": 15,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "alloc":{
    "0xe43ee2af5a41385ff951f0c3fe2f5156287757a6":
    {"balance": "20000000000000000000" }
    }
}

Best Answer

You cannot do a init again if you have already done a init on datadir. In order to achive what you want you need to first create simply create account .... once account is created use that for pre-funding, by putting it in the genesis file, now go for the init of datadir.

so steps are
1) Creation of account in datadir

geth --datadir <path_of_data_directory> account new ( I have used command line method for account creation, which does not require invocation of geth console)

2) use the account created as pre-funded account, add entry in genesis file.

3) Init of the datadirectory with the genesis file.

geth --datadir <path_of_data_directory> init <path_of_genesis_file>

I have used bare minimum parameters, it should also work fine with the parameter you are using. Main point to note you cannot re-initialize your data directory once it is initialized.

Related Topic