Getting started with Go Ethereum
By: Roberto Infante
Tags: Ethereum Blockchain, Go Ethereum, Ethereum, Web3, Geth

Following my previous articles “Introduction to the Ethereum blockchain” and “Smart contracts: the brain of Dapps”, in which I introduced Ethereum from a high level point of view, we’ll start to get more hands by playing with one of the most popular Ethereum client: Go Ethereum, also known as geth.

The download page for the Go Ethereum website shows releases for all major operating systems: Android, iOS, Linux, MacOS, and Windows. Various installation formats are available for some of the supported operating systems. For example, for Windows, you can choose between the 32- and 64-bit version and whether to download only the executable (Archive option) or the full installer (Installer option). At the time of writing of this article the latest version is 1.8.23. For this article I will assume you have picked the 64-bit Geth & Tools 1.8.23 Windows archive. If you pick the archive associated with another operative system don’t worry: you will be able to follow everything perhaps with minor tweaks.

Once you’ve installed or uncompressed the relevant file, you can run the geth executable. If you have the Ethereum wallet open, close it before starting geth, because they use the same port number! If you run geth with no parameters, it’ll start to synchronize in Full mode with the public production network, also known as MAINNET, as shown in the screenshot in figure 1.

Figure 1. geth synchronizing with the MAINNET network at startup

You can interact with geth in two ways:

1. Through the user-friendly geth interactive JavaScript console

2. Through low-level JSON-RPC calls over HTTP

In this article I will present you the JavaScript console. In the next article I will explain how JSON-RPC calls work.

A first look at the geth interactive console

Let’s get started with the interactive JavaScript console! geth comes with an interactive console that accepts JavaScript instructions. It implicitly references Web3.js, a JavaScript implementation of Web3, the official high-level library for interacting with Ethereum clients. This console is similar in purpose to read-eval-print loop (REPL) consoles for programming language IDEs. I’ll cover Web3 extensively in the next few chapters. For the moment, let’s get a quick feel for it.

First, open the interactive console. You’ve got two possible ways to start it up:

1. If no geth clients are running on the same machine, run the geth executable with the console command:

C:\program files\geth> geth console

2. If a geth client is already running on the machine, attach to a running geth process with the attach command:

C:\program files\geth> geth attach ipc:\\.\pipe\geth.ipc

You have a running geth process, so you’ll go for the second option.

NOTE: In this article, I’m assuming you use Windows, as over 60% of the nodes of the Ethereum network run on this operating system. Consequently, I’ll show shell commands with a Windows command prompt format. Also note that after successful execution of instructions in the console, you might see undefined right before or after the correct results. You can ignore it.

Displaying version information

First of all, you can query the console for version information. A Web3 object named web3 is implicitly instantiated when opening the console, so you can access version information in this way by typing

> web3.version

and you’ll see something like

{ api: “0.20.1”, ethereum: “0x3f”, network: “1”, node: “Geth/v1.8.13-stable-225171a4/windows-amd64/go1.10.3”, …

As you can see, the version property is an object that contains many subproperties. If you want, you can be specific and query an individual property of the version object:

> web3.version.api

and you’ll see something like

“0.20.1”

Because the console accepts JavaScript instructions, it’s possible to assign the values of Web3 properties and subproperties to variables and then display them through the console object:

> var apiVersion = web3.version.api > var nodeVersion = web3.version.node > console.log(‘Api version: ‘ + apiVersion) > console.log(‘Node version: ‘ + nodeVersion)

Checking connectivity

You can get some client connectivity information from the web3.net object. (You can omit the web3 namespace because it’s implicitly referenced.) If you type

> net

you’ll see something like

{ listening: true, peerCount: 2, version: “3”, getListening: function(callback), getPeerCount: function(callback), getVersion: function(callback) }

As for the version object, you can directly access individual properties of the net object as follows:

> console.log(‘this geth instance is listening for network connections : ‘ + web3.net.listening) > console.log(‘number of peers connected to this geth instance: ‘ + web3.net.peerCount)

If you want to get more detailed information about your node, you can use the web3.admin object and call

> admin.nodeInfo

and you’ll see something like

{ enode: “enode://90946319e42ef4d4670c1d7…, id: “90946319e42ef4d467…, ip: “::”, listenAddr: “[::]:30303”, name: “Geth/v1.8.13-stable-225171a4/windows-amd64/go1.10.3”, …

The peers property gives you detailed information about the peers you’re connected to:

> admin.peers

will yield something like

[{ caps: [“eth/62”, “eth/63”], id: “0b64924d478abaf6900ffe…, name: “Geth/v1.6.1-stable-021c3c28/linux-amd64/go1.8.1”, network: { localAddress: “192.168.1.555:53557”, remoteAddress: “136.144.129.555:30303” }, …

Accessing the blockchain

The web3.eth object retrieves real-time information about the client and the blockchain. Have a look at the console of the geth process currently running. You should see how new blocks are getting appended to the blockchain in real time. Take a note of the block number of a recently appended block, then switch back to the JavaScript console.

You can get the most recent block number through the eth.blockNumber property:

> var latestBlockNum = eth.blockNumber > console.log(‘Latest block #: ‘ + latestBlockNumber)

Then you can display summary information about this block by calling

> eth.getBlock(latestBlockNum)

and you’ll see something like

{ difficulty: 64344784, extraData: “0xd7830106078467657…, gasLimit: 4723091, gasUsed: 262264, hash: “0x8196edb66315b460f0bd4b9bdfa884…, nonce: “0x2ac78a350ec95787”, number: 1732206, …

You also can drill down at transaction level. You can get the first transaction stored in the latest block in this way:

> eth.getTransactionFromBlock(latestBlockNum, 0)

and you’ll see something like

{ blockHash: “0x8196edb66315b460f0bd4b9bdfa88…, blockNumber: 1732206, from: “0x392fd4954de442bb6c4d57f1923b4708642d3408”, gas: 210000, gasPrice: 120000000000, hash: “0x4eb5ae8d7b7919f92d1dd02fcc407d6…”, …

I encourage you to have a look at the content of the whole eth object:

> eth

Performing Ether conversions

The Web3 API offers some useful functions to convert any Ether denomination from/to Wei. You can convert a Wei amount to a specific denomination:

> var amountInWei = 12000000 > var amountInSzabo = web3.fromWei(amountInWei, ‘szabo’) > console.log(amountInWei + ‘ Wei is equivalent to ‘ + amountInSzabo + ‘ szabo’) 12000000 Wei is equivalent to 0.000012 szabo

And you can convert a specific denomination into Wei:

> var amountInEther = 12 > var amountInWei = web3.toWei(amountInEther, ‘Ether’) > console.log(amountInEther + ‘ Ether is equivalent to ‘ + amountInWei + ‘ Wei’) 12 Ether is equivalent to 12000000000000000000 Wei

I hope you have enjoyed this introduction on the Go Ethereum client. If you would like to learn more, please check out my next article, “Managing accounts with Go Ethereum” or have a look at my book: Building Ethereum Dapps.