Hands-on with blockchain - Ethereum
What is Blockchain?
The blockchain is a distributed ledger of economic transactions, which can be programmed to record not only crypto-currency transactions but anything having value. There are many implementation technologies of blockchain but I am going to provide detail on Ethereum on this brief hands-on post. The chain word in blockchain makes it’s definition more clear where one block of transactions connects with another block using some part of signature of previous block. The blockchain will start from first block, which is called genesis block and from that block onwards chain of block starts to create whole blockchain network, you can refer below image explaining same concepts.
So the blockchain has to be started by someone using genesis block and we will discuss further how genesis block can be configured using genesis.json file when we start our private blockchain locally. The application which runs on blockchain is called DApp (Decentralized Application); whichever current ICO (Initial Coin Offering) startups essentially are DApp offering their own tokens. When we talk about Ethereum, it is one of the blockchain networks with its own protocol. In blockchain, each block is time bound and miners will mine each block of transactions using Proof of Work algorithm to verify authenticity. The current mining is based on Proof of Work (PoW), which requires very high computing power to solve particular mathematical formula/puzzle and whoever first mines the block is the winner while there exists another approach called Proof of Stake (PoS). In PoS, the creator of block is chosen deterministic way so that it is mined by one entity rather than multiple miners are competing with each other to solve the puzzle. The determination of miner can be based on its wealth or stake and that is the reason it is called Proof of Stake and future of blockchain would be based on this approach because it is very cost effective. In this post, we would be using Ethereum as a blockchain platform to run our first simple smart contract; which uses PoW to mine the block.
Development Environment for Ethereum
- Operating System: Ubuntu 16.04 LTS (I have used Windows 10 as well for the same app and it works exactly the same but this post is about Linux)
- Geth: Using geth, you can run full Ethereum node. It is written in GoLang and it is also known as go-ethereum, you can download it from here.
- Mist: It is a kind of blockchain browser where you can browse the DApp, deploy your smart contracts, open Remix (it is a Solidity editor using which you can write smart contract), manage your accounts etc. You can download it from here, go ahead and select Mist-linux32-0-9-3.deb or Mist-linux64-0-9-3.deb based on your OS architecture.
You do not need any other tools for this hands-on demo but for your real world use cases, you may need other tools.
Installing Geth on Ubuntu
You can use below commands to install Geth on ubuntu and for Windows, there is an exe on download page mentioned above; which installs required dependency and prepare command line tool to work with Geth.
Installing Mist on Ubuntu
It is a .deb file and you can double click on it to install Mist on your Ubuntu or you can use dpkg to install downloaded .deb file of Mist.
Running full Ethereum node locally - Private Network
Before we start running different commands, you need to create one data directory where you want your private blockchain network to store data. As we are going to create first blockchain node, we need to start it as a genesis block and for that we need to prepare genesis.json file as mentioned below.
The content of your genesis file should be something similar to above and file should be stored in data directory you created previously. I am going to explain only few properties of genesis file here and for rest you can look online. The ‘chainId’ is the id you want to give to your block/network, it can be anything above 3 as 1 to 3 are reserved by blockchain for different purpose. The ‘difficulty’ indicates the effort required to discover valid block, statistically more calculations a Miner must perform to discover a valid block. We should keep ‘difficulty’ level as low as possible in test network so that our transactions get completed faster. The ‘alloc’ is the place where we will put default accounts with ether balance when we start our private network. You need to change both account ids as per generated by your geth, below is the command to create new account before starting the node.
You need to provide password to account and note the address(id)/password for the account, go ahead and repeat this one more time to generate another account. Once you have two accounts, you can replace two ids in genesis file with these two accounts you generated.
Initializing genesis Node
We have now genesis file under data folder and we need to tell Geth about it so next command is to initialize genesis node with genesis file.
The above command is the one time initialization of your genesis node, from where your blockchain can be started.
Running private Node/Network
In above command, you are noticing ipcpath option, which tells geth to put IPC file to specific location. The geth will start with above command and you are running your private blockchain network locally.
Using Mist with Private Network
The Mist comes with its own private blockchain node so when you open Mist, it will start the private node by itself. In our case we want Mist to use our private network rather than starting new one so the default location from where Mist looks for geth.ipc file is ~/.ethereum/geth.ipc. So, now you understand that why we started our geth with ipcpath command line option. Once Mist is opened, you will find that two accounts in Wallet tab: (1) Main Account and (2): Account1. These accounts were created by us initially before we initialized our Geth. We now need to create our first smart contract using Remix - a Solidity editor.
Creating first Smart Contract
Once Mist is open, click on ‘Open Remix IDE’ under Develop menu from main toolbar. The Remix editor will open with default contract, which you can override with below contract. The sample contract is for E-Voting where owner of the contract can add candidate and others can vote for the chosen candidate, we can also find total votes of specific candidate by supplying name of the candidate.
Following are the important functions of above smart contract
Note: We are using Byte32 instead of string data type for candidate names because as of this writing Solidity does not support dynamic data type in mapping where we wanted to map candidate and number of votes.
- addCandidate - is the function using which contract owner can add new candidate, here we are making sure that only contract owner can add candidate
- voteForCandidate - is the function to vote for a specific candidate by supplying candidate name
- validCandidate - is the function to check whether candidate is valid candidate by looking at the candidate list
- totalVotesFor - it returns number of votes for a specific candidate
You can also notice that read functions have ‘view’ in its definition and other functions are write functions. The write function will need ether to carry out transaction in blockchain, we will look into it once we deploy the contract. Also Remix editor comes with its own debugger and you can run full contract within javascript based virtual blockchain node but I will not cover that part in this post instead we will deploy and run contract in private blockchain network.
Deploying Smart Contract to private network using Mist
Now we have our smart contract ready and we want to deploy it to our network but before doing that we want to make sure that miner is started on our private network. To start the miner, you need to open another terminal window and run below command.
The above command will open interactive JavaScript console to interact with running blockchain. Once you are in console, you will need to type below command and press enter.
The number in start method indicates number of threads so you can put any number above 0 based on your processor architecture. There is also stop method as mentioned below to stop the miner.
Now again you can switch back to Mist and find the ‘CONTRACTS’ tab and click on it. Once you are in ‘CONTRACTS’ tab you will find ‘DEPLOY NEW CONTRACT’ button, which you can click. You can select Main account in ‘FROM’ field and copy paste the smart contract we developed in to ‘SOLIDITY CONTRACT SOURCE CODE’ field. Once the contract is pasted, from right hand side dropdown select ‘EVoting’ as contract under ‘SELECT CONTRACT TO DEPLOY’ panel. Now you can click on ‘DEPLOY’ button, which will open up a dialog box where you need to provide transaction password for main account you created using ‘Geth Account new’ command initially.
Running Smart Contract using Mist
Once the contract is deployed, you can verify that your contract is deployed successfully from ‘WALLETS’ tab by looking at ‘LATEST TRANSACTIONS’. You need to allow contract execution to complete with all confirmations and then you can select your contract from ‘CONTRACTS’ tab. The contract will open up and now you can interact with your contract. You will find two sections there (1) READ FROM CONTRACT and (2) WRITE TO CONTRACT. The second option is where you will be interacting with your contract, you can select ‘ADD CANDIDATE’ to add new candidate and ‘VOTE FOR CANDIDATE’ to vote specific candidate. Here we need to remember that the name we need to supply in byte32 encoding, you can use Hex Converter to covert string to byte32. You can use read methods similar ways to know total number of votes for a specific candidate. When you execute write method, you will have to provide transaction password to carry out that specific transaction and in real world there would be a transaction charge in terms of ether for different types of transactions.
I have not attached all screenshots for the all above steps to keep post length minimum but if you need screenshots of all above steps, please write it in comments and I will send you across.