Practical steps for go-ethereum setup

This tutorial cover installation, setup and basic commands of go-ethereum. After completing this tutorial, you can setup your own semi public ethereum network.

Prerequisites

OS: Ubuntu 16.04 ( 64 bit )
RAM: 2 GB

If you are using Virtual Machine, bridge the network adapter as below.

Network setting in virtual box

Geth Installation

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt update
sudo apt install ethereum

Check geth command

geth --help
Testing geth

Project directory setup

mkdir ethProject
cd ethProject

Create a directory node_app to add script files containing geth commands.

mkdir node_app

Creating a new account

Inside directory node_app , create a file account.sh .

cd node_app
nano account.sh

Put the below command inside account.sh file.


geth --datadir ../eth_node account new

press Ctrl + x and y and enter to save file.

Make the file executable.

sudo chmod +x account.sh

Run script.


./account.sh

The directory structure become like below.

ethProject
|- eth_node
|- keystore
|- account-file
|- node_app
|- account.sh

Blockchain initialization

Create a file genesis.json

nano genesis.json

Put the below contents

{
"config": {
"chainId": 88888,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x1",
"extraData" : "0x00",
"gasLimit" : "0xfffffffffffffff",
"nonce" : "0x0000000000000042",
"timestamp" : "0x00",
"alloc" :{
"account-address": {"balance": "0xffffffffffffffffffffffff"}
}
}
Notes: "alloc" properties will fill your account(s) with ether. You need to fill account-address with the previously created account. When you setup geth on another computer, you need to use the same 'genesis.json' file in order to make successful connections between nodes.
genesis.json

Create a file init.sh .

nano init.sh

Put the below command.

geth --datadir ../eth_node init genesis.json

Make the file executable as above.

Run script.

./init.sh

Running geth and interacting with javascript console

Create a file console.sh putting the below command and make it executable.

geth --datadir ../eth_node --nodiscover console

Run script

./console.sh

You can test basic commands valid inside JavaScript console.

eth.accounts // list accounts
eth.getBalance(eth.accounts[0]) //check balance of account 0

more commands

Start mining

miner.start(1)

Let’s Create another account to transfer ether.

Open another terminal and Inside node_app directory, run ./account.sh .

You will see two accounts when running eth.accounts inside console.

Check the balance of new account.

Transferring ether between accounts

Unlock account to send transaction.

personal.unlockAccount(eth.accounts[0])

Transfer ether

eth.sendTransaction({from:eth.account[0],to:eth.account[1], value:10000000000})
Transferring ether

The above transaction is mined by geth as below.

Make sure geth node is mining by checking a boolean property,

eth.mining

Check balances again

Balances after transaction

Now, geth is running as a single node with own genesis block and mining transactions successfully. Let’s create a blockchain network.

Geth multi-nodes setup

Create a file static-nodes.json

Inside console, print enode of geth node

admin.nodeInfo.enode

Copy result into static-nodes.json file and insert ip address as below

static-nodes.json

Copy node_app folder to another computer.

Run scripts inside node_app folder.

./account.sh
./init.sh

Copy static-nodes.json into eth_node directory

cp static-nodes.json ../eth_node

Start geth

./console.sh

When geth run, it will detect static-nodes.json and connect to the node(s) using enode(s).

Check peer count

net.peerCount

Check balance of account

Transfer ether from account of first node to account of second node.

Check balance again.

Now, you have successfully created blockchain network using geth.

You can add as many nodes as you want by adding enodes inside static-nodes.json file.