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.
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)
- 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”
- If geth is installed successfully you should see the following output for ‘geth version’ from your command prompt.
XXXMBP:~ xxxx$ geth version
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9.2
Operating System: darwin
2. If Mist is installed successfully, you should be able to launch it as an app
3. Folder/Files should looks like below
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
geth --datadir ~/mychain/data account new
Your new account is locked with a password. Please give a password. Do not forget this password.
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
Step 3 — Create CustomGenesis.json
Create a CustomGenesis.json in ~/mychain folder which defines your local Ethereum network. Following is a sample.
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.
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”
Step 6 — Check account balances (from JS console)
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.
It is important to have the same port in Step 5 (rpcport 8080) and in this step (rpc http://localhost:8080) are the same.