Ethereum : How to setup a local test node with initial ether balance using geth

I had to spend unnecessary amount of time to setup an Ethereum local test node/network with initial ether balance (ie without mining) in few accounts. Even though the information is available on multiple sources, couldn’t find everything in one place.

Scope

Here I would be explaining how to set up a private Ethereum test network on a Mac with initial ether balance in 2 accounts and then connect a Mist wallet with that local private network.

  • Ethereum node — Using geth (Version: 1.7.3)
  • Environment — MacOS Sierra (Version10.12.3)
  • Ethereum wallet/client -Mist (Version 0.9.2)

Prerequisites

  • Geth should be installed
  • Mist should be installed
  • Create folder “mychain” in your home directory (all the data related to the new node will be stored in this folder). Create “data” and “logs” subfolder inside mychain folder. inside logs folder, create an empty log file call “00.log
mkdir ~/mychain
cd ~/mychain
mkdir data
mkdir logs
cd logs
touch 00.log

Verify environment

  1. If geth is installed successfully you should see the following output for ‘geth version’ from your command prompt.
XXXMBP:~ xxxx$ geth version
Geth
Version: 1.7.3-stable
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9.2
Operating System: darwin
GOPATH=
GOROOT=/usr/local/Cellar/go/1.9.2/libexec

2. If Mist is installed successfully, you should be able to launch it as an app

3. Folder/Files should looks like below

Folder/File structure

Setting up process

Provided you have the all the prerequisites in place, following would be the steps you have to follow.

Step 1 — Create an account

Create test accounts

input:
geth --datadir ~/mychain/data account new
output:
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase:
Repeat passphrase:
Address: {7a5ef9a69d1748bb9ece0ef8ad94e63a7e76d10d}
Remember the Passphrase and save the address in a place you can retrieve when needed in a later step

Step 2 — Create additional accounts

Repeat Step 1 many times as you want, to create multiple accounts and do not forget to remember the passphrase and save the address.

If you check “~/mychain/data/keystore” folder, you should see private key file for each account you created as below

Private keys in keystore for newly created accounts

Step 3 — Create CustomGenesis.json

Create a CustomGenesis.json in ~/mychain folder which defines your local Ethereum network. Following is a sample.

{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"nonce": "0x0000000000000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x8000000",
"difficulty": "0x400",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {
"0x01c9bad367636cf1583e4130ab9f99b59f204c25": {
"balance": "0x1337000000000000000000"},
"0x350032e948159e9d11bec9f4ad8ed88b4f100a03": {
"balance": "0x2337000000000000000000"}
}
}

Addresses mentioned in alloc section should be replaced with the addresses you got from account new command earlier in Step 1 & 2. Please note, addresses should prefixed 0x when specify in the CustomGenesis.json. Balance would be the account balance of these accounts and numbers are in hex in this file. You would see the balance as a decimal number in an Ethereum client/wallet

Step 4 — Initialise local private node

Initialise your local private network with following command. Please note, we have given the path to the CustomGenesis.json file created in previous step

geth --identity “LocalTestNode” --rpc --rpcport 8080 --rpccorsdomain “*” --datadir ~/mychain/data/ --port 30303 --nodiscover --rpcapi db,eth,net,web3,personal --networkid 1999 --maxpeers 0 --verbosity 6 init ~/mychain/CustomGenesis.json 2>> ~/mychain/logs/00.log

Please refer this link for detail explanation about parameters used in this command. Most important part of this command is the init which specifies the CustomGenesis.json we created which contains the accounts and their balances along with other important configurations of the local private network. As per the command above this file should be in ~/mychain/data folder.

Step 5 — Start the node with JavaScript console

Run the following command

geth --identity “LocalTestNode” --rpc --rpcport 8080 --rpccorsdomain “*” --datadir ~/mychain/data/ --port 30303 --nodiscover --rpcapi db,eth,net,web3,personal --networkid 1999 --maxpeers 0 console
In Steps 1,4 &5, it is required to use the same value for datadir parameter which is in this case “~/mychain/data

If above command run successfully, it would end up in the JavaScript console similar to one below.

Step 6 — Check account balances (from JS console)

On JavaScript Console (with the prompt > ) run the following command

>eth.getBalance(eth.accounts[0]);
2.3229320729235784806170624e+25
> eth.getBalance(eth.accounts[1]);
4.257213384306985160146944e+25

Step 7 — Check account balances (from Mist)

Run following command to launch the Mist and connect to the local private network you created.

/Applications/Mist.app/Contents/MacOS/Mist --rpc http://localhost:8080 --swarmurl “null”

You will prompt with the message to indicate it is a “Insecure RPC Connection”, click OK to continue and connect. Then you will be prompt with a progress window. From that click on “LAUNCH APPLICATION” to view the Ethereum Wallet/Client.

Mist Wallet/Client with accounts showing initial balances
It is important to have the same port in Step 5 (rpcport 8080) and in this step (rpc http://localhost:8080) are the same.