Discover Ethereum, a Blockchain Based Computing Platform
Dive into the world of blockchains, and use Ethereum to host a private network and deploy a smart contract.
Blockchain, as you might already know, is a tamperresistant, append-only, distributed ledger. It is expected to be the next disruptive technology, although some argue that it is more of a foundational technology than something disruptive. Bitcoin was the first large scale adoption of a blockchain based platform, but the blockchain technology is capable of supporting much more than just cryptocurrencies. Ethereum provides an open source blockchain based distributed computing platform on which we can deploy apps to do much more than just financial transactions or money exchange.
I’ve chosen Ethereum because it is very popular, and currently ranks second behind bitcoin, in terms of market capitalisation. Besides, it is open source, provides a Turing complete virtual machine – the Ethereum Virtual Machine (EVM) — to deploy the applications, and has an active community to support it.
In this article, we will discuss how to set up a private network that will host the Ethereum platform, including the blockchain. We will also learn to deploy a very basic application and interact with it. First, let us discuss some key terms that will help us understand the later sections.
Smart contract: This is a piece of code that is deployed in a distributed manner across the nodes hosting the network. It administers laws and rules, determines penalties and enforces them across the network. It contains states that are stored on the blockchain.
Consensus: This is the process by which all the different nodes agree about a state change. It ensures that even if malicious parties behave arbitrarily, the honest parties would still be able to reach an agreement.
Transactions: Any change of state is proposed as a transaction. The transactions (individually or collectively) are consented upon and then written into the blockchain.
Queries: Reading states from a current blockchain is known as a query. It does not require consensus, and any node can perform a query by just reading the latest states on the blockchain.
Mining (in Proof of Work systems): A hard cryptographic puzzle needs to be solved before a node can take multiple transactions and form a block. This process is called mining and the nodes which perform this are called miners. Because this is a tough job, to incentivise miners, a mining reward is given to the winner who successfully writes on the blockchain.
Genesis: Every blockchain starts with a genesis block containing the basic rules of that chain. It mainly contains the system parameters.
Prerequisites for installation
We first need a few tools to help us through the installation and deployment. Let’s assume you have Ubuntu 16.04 running on your system, although the tools and methods are quite generic and can be easily ported to other distributions or operating systems too.
Installing Git and Go: There are a few official implementations of the Ethereum protocol. We will build the Golang implementation from source and to check out the latest build, we will need Git. Also, the Go version present in the Ubuntu repositories is old and hence we will use the gophers PPA for the latest version.
$ sudo add-apt-repository ppa:gophers/archive $ sudo apt-get update
$ sudo apt-get install golang-1.10-go git
Setting up the environment variables: We need to set up some environment variables for Go to function correctly. So let us first create a folder which we will use as our workspace, and set up a GOPATH to that folder. Next, we will update our PATH variable so that the system recognises our compiled binaries and also the previously installed Golang.
$ cd ~
$ mkdir workspace
$ echo “export GOPATH=$HOME/workspace” >> ~/.bashrc
$ echo “export PATH=$PATH:$HOME/workspace/bin:/usr/local/go/ bin:/usr/lib/go-1.10/bin” >> ~/.bashrc
$ source ~/.bashrc
Installing and running Ethereum
Let us first check out the latest version of geth (go-ethereum) with Git, and then build it from the source.
$ cd ~/workspace
$ git clone https://github.com/ethereum/go-ethereum.git $ cd go-ethereum/
$ make geth
Upon successful completion of the last command, it will display a path to the geth binary. We don’t want to write the entire path again and again, so let’s add that to the PATH variable:
$ echo “export PATH=$PATH:$HOME/workspace/go-ethereum/build/ bin” >> ~/.bashrc
$ source ~/.bashrc
Next, we need to create a genesis.json file that will contain the blockchain parameters. It will be used to initialise the nodes. All the nodes need to have the same genesis.json file. Here is a sample of a genesis.json file that we will use. It is saved in our workspace directory.
$ cd ~/workspace/
$ cat genesis.json
{
“config”: {
“chainId”: 1907, “homesteadBlock”: 0, “eip155Block”: 0, “eip158Block”: 0, “ByzantiumBlock”: 0
},
“difficulty”: “10”, “gasLimit”: “900000000000”, “alloc”: {}
}
Now, open a new terminal (say Terminal 1) and run our first Ethereum node on it. To enable that, first create a work folder for the node and a new account. Make sure you don’t forget the passphrase because that can be fatal on the main Ethereum network. In our case, because we are just creating our own private network, this is not such a serious concern. Then we supply a network ID:
cd ~/workspace/
$ mkdir node1
$ geth account new --datadir node1
$ geth init genesis.json --datadir node1
$ geth console --datadir node1 --networkid 1729
We should now be inside the Geth console. You can play around before proceeding but for our purpose, let us head on to creating a second node. To do that, look at the enode details of our current node. On the Geth console, execute the following commands:
> admin.nodeInfo
{
enode: “enode://c2b8714eca73d7a5a4264fa60641a8791ff8d33e47 dbb51f8b590594eb48e2aba9f360f340f358700e41e9d8415d7ca f70c67d12a66096053989c3824f7f64c3@[::]:30303”,
………