Skip to main content

Ethereum Blockchain Platform - How to create a Local Private Network, Design, Develop and Deploy a Smart Contract

Purpose

The goal of this post is to take a simple business use case and develop a blockchain application that is cryptographically secured, decentralized and tamper-proof. Also understand the key components of the Ethereum blockchain application stack, create a local private network, learn to write smart contract in solidity using Remix IDE, and leverage Ethereum Wallet to deploy and execute the smart contract for transferring the digital tokens.

Business Use Case

A fictitious epidemic code named "Chaindemic" is spreading like wildfire and the Center of Disease Control and Prevention (CDC) has found a particular strain of vaccine that is more effective compared to other variants of the strain available in the market. The procedure to administer the vaccine is very specialized and requires a control environment and training. CDC has chosen three providers who are trained and equipped to administer the vaccine. To encourage higher public participation in the vaccination drive, CDC will be offering monetary incentive.

CDC would like to leverage blockchain technology in a private network by offering digital tokens as an incentive to both individuals and authorized providers for their services.

Project Requirements
  • The application should be cryptographically secured, decentralized and tamper-proof
  • Individuals and providers will be tracked through their digital identities created on the CDC private network
  • CDC will be responsible for designating authorized providers
  • Each authorized providers will be paid 1000 digital tokens as an initial funding
  • Authorized provider will be paid 10 digital tokens for vaccinating an individual
  • Each individual will be paid 5 digital tokens and an individual should be vaccinated only once
  • The vaccination status and the tokens earned by providers and individuals is public and hence any one can query the data
Blockchain Platform

For a quick high-level review of the popular blockchain platforms, please refer to my earlier post Blockchain Frameworks for Enterprise Application Development. In this post we will be exploring the Ethereum development platform for the CDC project by creating a local private blockchain network, developing and deploying a solidity smart contract, and transferring the digital tokens to the participant members of the network.

Ethereum Platform

Ethereum decentralized platform enables developers to create markets in many vertical industries, store registries of debts or promises, move funds and many other things that have not been invented yet, all without a middleman or counterparty risk. The platform provides features to create a tradeable crypto tokens that can be used as a currency, a crowdfund to pre-sell a product, a crowdsale to sell virtual shares in an organization, a democratic autonomous organization which can be run by humans or robots.

Ethereum Decentralized Application Stack

Ethereum protocol consists of multiple distributed components each playing a specialized role and interfacing with others to provide a programmable blockchain platform. Figure 1 depicts a high-level view of the application stack.

Figure 1.

Key Elements of Ethereum Application Stack
  • Ethereum node - a physical machine, a virtual machine, a docker container, a mobile phone, or an IoT device that can run an Ethereum client
  • Ethereum client - exposes a common client interface and communicates with other clients through a P2P protocol. The key components of a client are: a low-level JSON-RPC interfaces, an Ethereum runtime environment and a blockchain database. Ethereum protocol is implemented in multiple programming languages including Go, C++, Python, JavaScript, Java, Ruby, etc.
  • Ethereum client libraries - simplifies the JSON-RPC communication with the Ethereum clients. The popular libraries include web3.js(JavaScript), web3j(Java), Nethereum(C# .NET) and ethereum-ruby (Ruby)
  • End User Interface Apps - a desktop, mobile, or IoT application that communicates with an Ethereum client leveraging a client library such as web3.js
End User Interface Apps and Development Environments in the Ethereum Ecosystem
  • MetaMask - It is a browser (available for Chrome and Firefox) plugin that allows end users to interact with blockchain accounts and smart contracts on an Ethereum network. The plugin communicates with the blockchain network over the web by injecting Web3.js library thus allowing to carry out transactions through a regular web page in a browser. It can interface with private, test and live Ethereum networks
  • Ethereum Mist Browser - It is an end-user interface for Ethereum platform. It's the tool for browsing and using Decentralized Apps (DApps) and designed for both developers and non-technical users.
  • Ethereum Wallet - It is a gateway to Decentralized Apps (DApps) on the Ethereum blockchain  network. It enables to hold and secure ether (Ethereum cryptocurrency) and other crypto tokens, as well as write, deploy, and use smart contracts
  • Custom Web Apps - A standard web app, generally developed as a Single Page Application (SPA) using frameworks such as AngularJS, ReactJS or straight HTML and JavaScript. The SPA communicates directly with a Ethereum node on the network through a client-side JavaScript library such as web3.js. The SPA can be deployed to a conventional web server and accessed through a standard web browser
  • Ethereum Remix - It is an IDE for the Ethereum smart contract programming language Solidity and has an integrated debugger and testing environment. It is available as a web application.
  • Truffle - It is a popular environment for developing DApps. It provides smart contract compilation, linking, deployment, testing and binary management. It also includes "Ganache", a personal Ethereum blockchain to run tests.
In the rest of the post, I will cover how to create a local private Ethereum blockchain network, develop a smart contract with Remix IDE, and deploy the smart contract to the local network using Ethereum wallet.

Key Steps Covered:
  • Setting up a local private blockchain network with primary account and ether
  • Connect Ethereum Wallet to a local private blockchain network and send ether from primary to other accounts
  • Develop and debug a smart contract using Ethereum Remix Solidity IDE
  • Deploy and test the smart contract using Ethereum Wallet
Setting Up A Local Private Blockchain Network With Primary Account And Ether

The first step in creating a blockchain network is creating a "Genesis block".

Genesis file -  defines a genesis block which is the start of the blockchain. The first block is the only block that does not point to a predecessor block. A typical genesis file looks as follows:



One of the key element in the genesis file is the "difficulty", its value defines the mining difficulty of the blockchain. For a private network this value can be set to low which speeds up the mining of the blocks thus increasing the performance and scalability of the platform.

To create a blockchain network, download an Ethereum client. For this post, I have downloaded and installed "go-ethereum" which is commonly referred to as "geth". After geth is installed, copy the genesis file to a directory and run the following commands from the command line:




The execution of the above commands creates a local Ethereum blockchain network, initializes, starts up the local database and the Ethereum protocol. To verify the running of the local network, open another console window and run the following command which will respond with a welcome message as shown in the console. We will use this console to create a primary account, generate ether through mining, etc.



Currently the local blockchain network doesn't have any accounts or ether. Run the following commands from the console, which creates a primary account and generates enough ether for executing transactions.



Connect Ethereum Wallet To A Local Private Blockchain Network And Send Ether From Primary Account To Other Accounts

Download and install the Ethereum Wallet on your local desktop. The latest release can be found at  https://github.com/ethereum/mist/releases. From another command window run the following command to connect the Ethereum Wallet to local private network.



The above command starts of Ethereum Wallet and connects to local blockchain network. The dashboard of Ethereum Wallet looks as shown in Figure 2. The key areas on the screen are: 
  • PRIVATE-NET - the network Ethereum Wallet is connected, in this case local network
  • Send - to send ether to other accounts
  • Balance - ether balance in the network generated earlier
  • Contracts - to deploy new smart contracts, to watch and execute deployed contracts 
  • Account Overview - which displays the accounts in the blockchain network. You should see the primary account (changed the label to "Center of Disease Control") we created earlier
  • Wallet Contracts - These contracts are stored on the blockchain and can hold and secure ether
  • Latest Transactions - List of transactions submitted and committed to the blockchain network


Figure 2.

To deploy and test CDC smart contract application in the Ethereum Wallet, we will create three provider accounts, three user accounts and transfer 50 ether to each of the provider accounts from the primary account. To create new accounts, click on "File/New Account" and follow the flow provided by the Ethereum Wallet. To send ether, click on the "Send" menu and follow the screen flow. To commit the transactions, from the console window run the miner for few seconds, transactions will be verified and committed to the local blockchain network. The Figure 3. shows the updated view with additional accounts and three "ether transfer" transactions created and committed.

Figure 3.

Develop And Debug A Smart Contract Using Ethereum Remix Solidity IDE

Remix Solidity IDE can be accessed by going to http://remix.ethereum.org in a web browser. It provides code intellisense, compiler and a test environment for the smart contracts with built in JavaScript VM or Injected Web3 or Web3 provider. 

Smart Contract 

To support the CDC project requirements, a smart contract named "EpidemicPreventionContract" (shown in Figure 4.) is implemented in the Remix IDE. The smart contract logic consist of:
  • a "constructor" invoked during the deployment of the smart contract by CDC
  • a public method "vaccinate" called by the providers
  • an internal method "payTokens" called by the "vaccinate" method
  • a public "getTokenCount" method which can be called by any member of the blockchain network

You can compile, debug and verify the logic using Remix Solidity IDE built-in JavaScript VM environment.

Note: The logic is mainly to show how to code a smart contract for implementing CDC fictitious blockchain project and hence does not include all the validations, security, and access controls needed for an enterprise application. In the future post, the contract will be refactored to support ERC20 token specification so the tokens can be exchanged with ether.

Figure 4.

Deploying And Using The Smart Contract In Ethereum Wallet

To deploy the smart contract to Ethereum Wallet click "Contracts" menu on the top right corner of the screen and then click "Deploy New Contract" button. In the view shown in Figure 5., choose the account "Center of Disease Control" as the account from which the contract will be deployed, paste the smart contract logic from Remix IDE in the area "Solidity Contract Source Code". The code is automatically compiled and if there are no compilations errors, the constructor with input parameters is displayed under "Select Contract to Deploy". Enter the provider account addresses and 1000 for the seed token value. Currently in Ethereum Wallet, entering the values is a manual process, which can be automated through a custom web or mobile application. After entering the parameter values, click "Deploy" button. To commit the smart contract to blockchain, run the miner for few seconds from the command console.

  Figure 5

To run the smart contract as a provider, click "Contracts" menu and then click on the "Epidemic Prevention Contract" . The contract screen displays all the getter and setters of the contract and allows the execution of the contract functions from a member account. From the drop down "Select function" select the "Vaccinate" method which will display the input parameters of the function. Enter an user address for "patient -address", select "Provider 1" from the dropdown "Execute from" and then click "Execute" button. Figure 6. depicts a smart contract detail screen. Run the miner from the command console to commit the transactions to the blockchain.

Figure 6. 


After the transaction is committed, you can verify the tokens earned by user and the provider by entering their respective addresses in the "Get token count" textbox. Figure 7. displays the tokens (1010 = initial funding + service fee) earned by a provider, and Figure 8. displays the tokens earned by the user (5 tokens). 


Figure 7.

Figure 8.

To view the latest transactions, go to "Latest Transaction" section in the dashboard which displays all the transactions including submitted and committed to the blockchain database. Figure 9. shows the list of transactions recently executed - the transfer of ether to providers, deployment of smart contract and invocation of smart contract by Provider 1.

Figure 9.

In the future articles we will explore Truffle (a popular Ethereum development environment) and other open source blockchain application frameworks such as Hyperledger Fabric, Hyperledger Sawtooth, etc.

Comments

Popular posts from this blog

Building a Genome Next Generation Sequencing (NGS) Data Pipeline in Azure

1)               Introduction Next Generation Sequencing (NGS) also known as Deep Sequencing or Massive Parallel Sequencing or Second and Third Generation Sequencing is a technique that offers unprecedented detail in the genomic, transcriptomic, and epigenomic patterns associated with cellular processes.  A medium size lab (10-15 scientists) could easily generate multiple terabytes of data during a NGS end-to-end process. Hence building a scalable, cost effective, and secured data pipelines is a lifeblood for life sciences industry and in particular Genomics domain. 2)                    Key Terms Genomics: It is study of genomes (complete set of genetic material within an organism). Genomics involves the mapping, sequencing and analysis of genomes. It includes structure, function, comparison, and evolution ...

Blockchain Frameworks for Enterprise Application Development

Introduction In the last few years there have been a number of articles and blogs around blockchain concepts, benefits of Distributed Ledger Technology (DLT), and real-world blockchain application use cases in industry verticals such as supply chain, finance, healthcare, etc. According to many industry pundits, 2018 will be the year of blockchain applications going mainstream. The goal of this post is to discuss the current popular open source blockchain application frameworks, their key features and which framework is suited for a business use case. Blockchain Application Framework The top two open source blockchain projects are Hyperledger and Ethereum. Hyperledger is a suite of many blockchain frameworks consisting of Hyperledger Sawtooth, Hyperledger Iroha, Hyperledger Fabric, Hyperledger Burrow, and Hyperledger Indy. This post will cover the three popular frameworks:         Hyperledger Sawtooth         Hyperledger Fabric ...