Understanding Geth in depth

Understanding Geth in depth

Geth(Go Ethereum) is a command line interface for running Ethereum node implemented in Go Language. Using Geth you can join Ethereum network, transfer ether between accounts or even mine ethers.

Image for post

Installation:

You can download and install Go Ethereum (Geth) directly from Download Geth page.

Installation of Geth from terminal in Ubuntu:

You can install geth from Ubuntu terminal with the following commands,

sudo apt-get install software-properties-common

sudo add-apt-repository -y ppa:ethereum/ethereum

sudo apt-get update

sudo apt-get install Ethereum

Create a separate folder to store the ethereum network data. To start Ethereum node using the Geth console, you need to type the below command,

kbhargav5@ubuntu:~/Gethtut$ geth

the above command will connect to the main Ethereum network and try to download the blocks. This will consume huge amount of memory space in your hard disk in GBs.

If you think it consumes too much of disk space then you can run Ethereum in light mode using geth –light command. This will download only block headers without transaction data.

You can check the status of sync to the main network by typing command inside geth console,

> eth.syncing{currentBlock: 3165668,highestBlock: 5157070,knownStates: 3180,pulledStates: 214,startingBlock: 3165125}

Running Ethereum Node in Private Blockchain network:

You can create a private Ethereum network yourself using geth. An Ethereum network starts with its blockchain with a root block called genesis block. Genesis block doesn?t contain any transactions.

You can create Genesis block storing the below json data in a file and running the command,

geth –datadir ./data init genesis.json

Genesis.json file:

{“config”: {“chainId”: 2018,”homesteadBlock”: 0,”eip155Block”: 0,”eip158Block”: 0},”alloc” : {},”coinbase” : “0x0000000000000000000000000000000000000000″,”difficulty” : “10”,”extraData” : “”,”gasLimit” : “0x2fefd8″,”nonce” : “0x0000000000000042″,”mixhash” : “0x0000000000000000000000000000000000000000000000000000000000000000″,”parentHash” : “0x0000000000000000000000000000000000000000000000000000000000000000″,”timestamp” : “0x00”}

ChainId is the network id of Ethereum network. You can choose any value higher than 4 as chainid because 1?4 are defaults for main and test networks.

Creating account in Ethereum network,

geth –datadir ./data account new

You need to enter the password and make sure you don?t forget it.

This will create a key file in data/keystore which contains your public and private addresses. Make sure you store it somewhere safe.

To list all the accounts created. Type the command,

geth –datadir ./data account list

You can start the ethereum node by entering into the console,

geth –datadir ./data console

Running two instances of Ethereum node server leads to data corruption in Ethereum database. So you can use geth attach to connect to the same node.

For example,

C:> geth attach ipc:/home/Gethtut/data/geth.ipcWelcome to the Geth JavaScript console!instance: Geth/v1.8.1-stable-1e67410e/windows-386/go1.9.2modules: admin:1.0 debug:1.0 eth:1.0 net:1.0 personal:1.0 rpc:1.0txpool:1.0 web3:1.0

This will open Javascript console where in you can use Javascript command for interacting with Ethereum network.

To come out of the geth console you can use exit command. This will stop the Ethereum node.

You can also create a new account inside the geth console with the command,

personal.newAccount()

Now we have Ethereum accounts but no balance in the accounts. In order to get some ether we need to perform mining. Mining is a key component in Ethereum network where multiple miners compete to create a new block. The corresponding miner will be rewarded with some ether.

Lets start mining with 1 thread,

miner.start(1)

If you get any error like this,

Error: etherbase missing: Etherbase must be explicitly specified

Then you need to set the Etherbase. Etherbase, also called coinbase, is the public key of an Ether account, which is needed by the miner to receive mining reward Ether money.

You can set the Etherbase by following commands,

> base = eth.accounts[0]”0x05d4e1a499775ce9d681abd50bda655c7b5ccb90″> miner.setEtherbase(base)true> miner.start(1)null

Running ?geth? miner with 1 thread is too slow to generate new blocks. So you can run geth miner with 4 threads to make the mining process faster. Also lower the difficulty level if you need mining process faster and get more ether.

Get the balance of the account,

eth.getBalance(eth.accounts[0])

This will give the balance in Wei. To get the balance in ether type the below command,

web3.fromWei(eth.getBalance(eth.accounts[0]), ?ether?)

To send ether from one account to other, create a second account to which you want to send ether by using personal.newAccount() and send the ether using sendTransaction command.

eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value: web3.toWei(4, ?ether?)})

This will send 4 ether to the second account. This transaction will be in transaction pool until miner picks up. If you get any error like this,

Error: authentication needed: password or unlock at web3.js:3143:20 at web3.js:6347:15 at web3.js:5081:36 at <anonymous>:1:1

You can unlock the account using,

personal.unlockAccount(eth.accounts[0], ***password***)

Then start the mining process using miner.start(). Then check the balance in the second account to see if the transaction of transferring of 4 ether is processed.

eth.getBalance(eth.accounts[1])

11

No Responses

Write a response