How to build a dapp on a private Ethereum network: Part — 1
I received requests from many fellow developers to share the document of how I developed a decentralized application (dapp) on a network of 2 private Ethereum nodes without using any third-party APIs or apps (such as Infura, MetaMask, etc.). Since my work is a part of a research project at CSIR-CEERI and is not yet published, I cannot explicitly share its code.
However, I too have learnt a lot from online resources and I wanted to contribute to the development of the community. Hence, I decided to publish the essential parts of the development process.
In this series of 5 articles, I have explained how to build a simple HelloWorld dapp on the same distributed infrastructure as mentioned above. It will perform the following operations :
- Input a name (string) from the user and store it in a private Ethereum blockchain using a smart contract
- The dapp will be able to read the entered name value from the chain and display it to the user
Recently released— “Part 5 : Section VIII-IX”
Table of Contents
Here, you can find a list of the sections and subsections that I have divided this tutorial into :
→ I. Creating the project
→ II. Installing prerequisites
→ III. Configuring the network
IV. Designing the smart contract
V. Setting-up the Truffle project
VI. Launching the network
VII. Building the web app
VIII. Testing the dapp
IX. Modifying the dapp
If you want to skip to a particular section, you can scroll down to the All series links section at the end of this article.
In part-1 of this series, I will cover sections I-III. This shows how to install the packages (with specific versions) required to successfully build a dapp.
Pro-tip : I would suggest that you type the code yourself and avoid the copy-paste scam. It’s a better way to learn.
I. Creating the Project
To create and move into a dedicated repository for the dapp, run in a new terminal window :
$ mkdir TruffleDapp
$ cd ./TruffleDapp
PLEASE READ THIS :
- All commands and steps mentioned henceforth should be executed within the
TruffleDapp
repository, unless mentioned otherwise - I am working on Ubuntu 16.04 and the web browser used is Chromium
II. Installing Prerequisites
This section details the steps to set up the prerequisites for developing the dapp. The versions required are listed below :
- NVM v8.11.3
- NPM v5.6.0
- GoLang (aka go) v1.10.3
- Geth v1.8.27-stable
a) Installing Golang
- Download
go1.10.3.linux-amd64.tar.gz
package into~/Downloads
from here. - Extract the downloaded package with :
$ tar xzvf ~/Downloads/go1.10.3.linux-amd64.tar.gz
- Add to
~/.profile
and~/.bashrc
file :
# set GO variables to config go env
export GOROOT=$HOME/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
- To compile
~/.profile
and~/.bashrc
, run in terminal :
$ source ~/.profile && source ~/.bashrc
- Close current temrinal window. To verify installation, open new terminal and run
$ go version
b) Installing geth
- Scroll down to the bottom of this page. Under “Assets” section, click on “Source code (tar.gz)” to download
go-ethereum-1.8.27.tar.gz
into~/Downloads
- Extract the downloaded package using :
$ tar xzvf ~/Downloads/go-ethereum-1.8.27.tar.gz
NOTE : If make and other required packages aren’t installed, run in terminal :
$ sudo apt install make
$ sudo apt install make-guile
$ sudo apt-get install -y build-essential
- To install geth code :
$ cd go-ethereum-1.8.27
$ make geth
- Add to
~/.bashrc
(thePATH
maybe different for your system, copy the path from Properties for location ofgeth
executable) :
# set path for geth permanently
export PATH="/home/ethereum/go-ethereum-1.8.27/build/bin/:$PATH"
- To compile this file, run in a terminal :
$ source ~/.bashrc
- To verify, open a new terminal window and run :
$ geth version
c) Installing NodeJS
Here, NodeJS will be used for developing the back-end component of the dapp. Follow the steps given below :
(i) To install the nvm
package :
- To get the software packages from Ubuntu repositories that will allow building source packages. Run in a terminal open in
TruffleDapp
(T1) :
$ sudo apt-get update
$ sudo apt-get install build-essential libssl-dev
- Once the prerequisite packages are installed, the
nvm
installation script can be pulled from the project’s GitHub page.
$ curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh -o install_nvm.sh
- Run the script with bash :
$ bash install_nvm.sh
- Then, either log out of the Ubuntu system and login again or run in T1 :
$ source ~/.profile
- To verify the installation, open a new terminal in
TruffleDapp
(T2) and run :$ nvm --version
(ii) To install node v8.11.3
and npm v5.6.0
:
- Run in a terminal open in
TruffleDapp
(T1) :
$ nvm install 8.11.3
- To use the above installed
node
andnpm
packages, run in T1 :
$ nvm use 8.11.3
$ nvm alias default 8.11.3
- To verify the installation, open a new terminal in
TruffleDapp
(T2) and run :
$ node -v
$ npm -v
d) Installing Truffle
It is a development framework for Ethereum. It provides you with a project structure, files, and directories that make deployment and testing much easier and allows scriptable deployment and migrations.
I used Truffle v4.x and not the newly released v5.x due to the availability of online information (blogs, articles, tutorials) for the former, especially related to linking the truffle-deployed contract to the private Ethereum network. Open a terminal window and run :
$ npm install -g truffle@4.1.16
You’ve taken the first step (well, almost). Don’t you slack off now! Be smart. You wouldn’t want to get thrown out of a window now, would you?
III. Configuring the Network
The project requires using a private Ethereum blockchain. To do so, “nodes” need to be set up which will run the EVM and store the blockchain data. This is done with the help of the go-ethereum
package that was installed earlier by using the geth
keyword.
A genesis block is the first block of a blockchain. The genesis block is almost always hardcoded into the software. It is a special case in that it does not reference a previous block.
A customGenesis.json
file defines various parameters for the genesis block of the Ethereum network that nodes will be a part of. Open customGenesis.json
in an editor and type :
{
"nonce": "0x0000000000000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x00",
"gasLimit": "0x8000000",
"difficulty": "0x400",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": { },
"config": {
"chainId": 111,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
}
}
Some important terms have been explained below. Detailed information can be found here under the section “Explanation of genesis file”.
- gasLimit : Establishes an upper limit for executing smart contracts
- difficulty : controls the complexity of the mining puzzle and a lower value enables quicker mining
- alloc : allows allocation of Ethers to a specific address
- chainId : A unique identifier of the new private blockchain
- homesteadBlock : Homestead is the first production release of Ethereum and since the developers are already using this version the value of this parameter can be left as ‘0’
- eip155Block/eip158Block : EIP stands for “Ethereum Improvement Proposals”, these were implemented to release Homestead. In a private blockchain development, hard forks aren’t needed, hence the parameter value should be left as ‘0’
This concludes section I-III of this tutorial. In the next article (part-2), I will cover section IV. Click on this link to continue BUIDL-ing : How to build a dapp on a private Ethereum network : Part — 2
P.S. — Clap 10 times if you liked the article! Comment below to let me know your thoughts or if you want to share some hacks.
I will be publishing more such interesting articles shortly. Follow me on Medium or stalk me on Twitter to stay tuned.