Go-ethereum: how to initialize a account in genesis.json file and fund it some money before mining?thank you.

Created on 19 Jul 2017  ·  11Comments  ·  Source: ethereum/go-ethereum

in ethereum page of github,we can see something about genesis file below:
Defining the private genesis state

First, you'll need to create the genesis state of your networks, which all nodes need to be aware of and agree upon. This consists of a small JSON file (e.g. call it genesis.json):

{
"config": {
"chainId": 0,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
The above fields should be fine for most purposes, although we'd recommend changing the nonce to some random value so you prevent unknown remote nodes from being able to connect to you. If you'd like to pre-fund some accounts for easier testing, you can populate the alloc field with account configs:

"alloc": {
"0x0000000000000000000000000000000000000001": {"balance": "111111111"},
"0x0000000000000000000000000000000000000002": {"balance": "222222222"}
}

I define "alloc": {
"0x0000000000000000000000000000000000000001": {"balance": "111111111"}
} in my genesis.json file, then I run command below:

./geth --datadir ethereum init genesis.json

but in geth console, after I run eth.accounts, it returns null

instance: Geth/helloworld/v1.6.6-unstable/linux-amd64/go1.8.3
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

> eth.accounts
[]
>

how to initialize a account in genesis.json file and fund it some money before mining?thank you.

inactive

Most helpful comment

i think you can do it easier than this. before you run init, and with an empty datadir run

here is part of a script i am developing to set up a private node

    # wipe the old blockchain and wallet/keystore
    rm -rf datadir 
    mkdir datadir

    # first we create some accounts
    geth --datadir=./datadir --password ./password.txt account new > account1.txt
    geth --datadir=./datadir --password ./password.txt account new > account2.txt

    # update genesis json to use the addresses from one of the new accounts
    <script here modifies genesis.json to replace the account ids with the just generated ones>

    # create the blockchain with the fund allocations
    geth --datadir ./datadir init genisis.json

All 11 comments

You need to import you private key file, just put the private key file under yourdatadir/keystore/

thank you luren5. I do the setps below:

# Create a text file containing your private key
$ more privatekey
3a1076bf45ab87712ad64ccb3b10217737f7faacbf2872e88fdd9a537d8fe266
# Execute geth to import your private key
$ geth account import privatekey 
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase: 
Repeat passphrase: 
Address: {c2d7cf95645d33006175b78989035c7c9061d3f9}

then I paste c2d7cf95645d33006175b78989035c7c9061d3f9 into genesis.json file such as:

{
    "config": {
        "chainId": 88888,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "coinbase" : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x1",
    "extraData" : "0x00",
    "gasLimit" : "0x47e7c5",
    "nonce" : "0x0000000000000042",
    "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp" : "0x00",
    "alloc" : {
        "c2d7cf95645d33006175b78989035c7c9061d3f9": {"balance": "888888888888888888888888"}
    }
}

then

 ./geth  --datadir  ethereum  init genesis.json
WARN [07-20|11:09:21] No etherbase set and no accounts found as default
INFO [07-20|11:09:21] Allocated cache and file handles         database=/home/superman/data/src/github.com/ethereum/go-ethereum/ethereum/geth/chaindata cache=16 handles=16
Fatal: Failed to write genesis block: database already contains an incompatible genesis block (have a08f593807ac9e62, new aea5d3587a98bfcd)

so what's wrong with my setps? how to solve the problem? thanks

As the Fatal says, you have inited, so can't init again, try as following
1、start your geth and create a new account
2、write down the address, and copy the corresponding private key file under datadir/keystore/privatekeyfile. The private key file name like this UTC--2017-07-10T16-55-52.479148210Z--6e423da3705daaa16a3cdef560293139bb277a3e
3、remove the data dir
4、modify your genesis.json, replace the account and then init again
5、move the private key file to the newcreating datadir/keystore/

thank you very much.

i think you can do it easier than this. before you run init, and with an empty datadir run

here is part of a script i am developing to set up a private node

    # wipe the old blockchain and wallet/keystore
    rm -rf datadir 
    mkdir datadir

    # first we create some accounts
    geth --datadir=./datadir --password ./password.txt account new > account1.txt
    geth --datadir=./datadir --password ./password.txt account new > account2.txt

    # update genesis json to use the addresses from one of the new accounts
    <script here modifies genesis.json to replace the account ids with the just generated ones>

    # create the blockchain with the fund allocations
    geth --datadir ./datadir init genisis.json

I'm having a similar problem ( #14875 )
I'm not mining but I do know that using more cache makes things faster
geth --cache=4096
is what I use and it seems to work better than less cache

we made it work using luren5's recommendations with a minor change in the execution order

As the Fatal says, you have inited, so can't init again, try as following
1 start your geth and create a new account
2、write down the address, and copy the corresponding private key file under datadir/keystore/privatekeyfile. The private key file name like this UTC--2017-07-10T16-55-52.479148210Z--6e423da3705daaa16a3cdef560293139bb277a3e
3、remove the data dir
4、modify your genesis.json,
5、copy again the private key file to the newcreating datadir/keystore/

6 init again

I couldn't initialize my genesis.json file.
Could you guys tell me the way!!!!

The answers above does work. What problem did you run into? @jiebanghan

@jiebanghan use this one modified from @carchrae

Remove everything, start from scratch:
# wipe the old blockchain and wallet/keystore
rm -rf datadir
mkdir datadir

# first we create some accounts
geth --datadir=./datadir account new
    follow from the prompt, type and retype a passphrase, you will receive a result address, e.g. 
      address: {e44f98b12f1460bfde940f3b1bf95b537dbc4106}

# use any text editor to save the text below and save as genesis.json. Note the account address returned above is put in alloc:

{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"gasLimit": "210000",
"difficulty": "20000",
"alloc": {
"e44f98b12f1460bfde940f3b1bf95b537dbc4106": {
"balance": "10000"
}
}
}
# create the blockchain with the fund allocations
geth --datadir=./datadir init genesis.json

    # start the console
geth --datadir=./datadir console

   # check the account

eth.accounts
["0xe44f98b12f1460bfde940f3b1bf95b537dbc4106"]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

362228416 picture 362228416  ·  3Comments

vogelito picture vogelito  ·  3Comments

cheershendtco picture cheershendtco  ·  3Comments

VenusHu picture VenusHu  ·  3Comments

JMaxU picture JMaxU  ·  3Comments