Month: June 2018

Smart contracts using Ethereum for Impatient Part-I

Introduction:

Ethereum is to date the most advanced blockchain platform ever created. In this blog series, I will be explaining how to deploy your own ethereum private network and write smart contracts on top of it.This tutorial series is for the impatient who already know the basics of blockchain and ethereum and want to quickly try writing smart contracts. In the first part of this tutorial, we will learn how to set up private Ethereum network,
while in the second part we will see how to write, deploy and publish smart contract on private ethereum network with an example.

Note: This blog assumes that you have a basic understanding of Blockchains,  Cryptocurrencies, and Smart contracts. If you don’t, will recommend to at least watch few videos on Ethereum and Smart contracts on youtube

What is Ethereum?

Ethereum can refer to both a a framework for deploying blockchain and the ethereum network created using ethereum framework. Ethereum bockchain is capable of running programming code of any decentralized application (also known as dApps). Similar to bitcoin, miners in ethereum network work to earn Ether (a token that fuels the network). Ethereum comes with a Turing complete execution environment  “Ethereum Virtual Machine” (EVM) which is used to execute the smart contracts.

Setting up private ethereum network:

In this section we will learn how to setup your own ethereum network. Private ethereum network means setting up a new blockchain and this should not be confused with the ethereum main network. To setup private ethereum network you need to perform following steps:

  1.  Getting and installing Geth ethereum client

    sudo apt-get install software-properties-common
    sudo add-apt-repository -y ppa:ethereum/ethereum
    sudo apt-get update
    sudo apt-get install ethereum

  2. Writing the Genesis block

    A genesis block is the first block of a block chain. In order to deploy a private blockchain you need to define configuration for the a genesis block. Following is the sample genesis block

{
“nonce”: “0x0000000000000042”,
“mixhash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“difficulty”: “0x400”,
“alloc”: {},
“coinbase”: “0x0000000000000000000000000000000000000000”,
“timestamp”: “0x00”,
“parentHash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“extraData”: “0x”,
“gasLimit”: “0xffffffff”,
“config”: {
“chainId”: 59,
“homesteadBlock”: 0,
“eip155Block”: 0,
“eip158Block”: 0
}
}

Here,

  • mixhash

A 256-bit hash which proves, combined with the nonce, that a sufficient amount of computation has been carried out on this block: the Proof-of-Work (PoF).

  • nonce

A scalar value equal to the number of transactions sent by the sender.

A 64-bit hash which proves combined with the mix-hash that a sufficient amount of computation has been carried out on this block.

The nonce is the cryptographically secure mining proof-of-work that proves beyond reasonable doubt that a particular amount of computation has been expended in the determination of this token value. (Yellowpager, 11.5. Mining Proof-of-Work).

  • difficulty

A scalar value corresponding to the difficulty level applied during the nonce discovering of this block. It defines the Mining Target, which can be calculated from the previous block’s difficulty level and the timestamp. The higher the difficulty, the statistically more calculations a Miner must perform to discover a valid block. This value is used to control the Block generation time of a Blockchain, keeping the Block generation frequency within a target range. On the test network, we keep this value low to avoid waiting during tests since the discovery of a valid Block is required to execute a transaction on the Blockchain.

  • alloc

Allows to define a list of pre-filled wallets. That’s a Ethereum specific functionality to handle the “Ether pre-sale” period. Since we can mine local Ether quickly, we don’t use this option.

  • coinbase

The 160-bit address to which all rewards (in Ether) collected from the successful mining of this block have been transferred. They are a sum of the mining eward itself and the Contract transaction execution refunds.

  • timestamp

A scalar value equal to the reasonable output of Unix’ time() function at this block inception.

  • parentHash

The Keccak 256-bit hash of the entire parent block’s header (including its nonce and mixhash). Pointer to the parent block, thus effectively building the chain of blocks. In the case of the Genesis block, and only in this case, it’s 0.

  • extraData

An optional free, but max. 32 byte long space to conserve smart things for ethernity on the Blockchain.

  • gasLimit

A scalar value equal to the current chain-wide limit of Gas expenditure per block. High in our case to avoid being limited by this threshold during tests. Note: this does not indicate that we should not pay attention to the Gas consumption of our Contracts.

The detailed information about the genesis block can be found in this yellow paper

3. Starting a ethereum (miner) node

To start ethereum node you need to first initialize the blockchain using genesis block as follows:

geth –networkid 300 –identity node1 –verbosity 3 –nodiscover –nat none –datadir=./privatenet init ./custom_genesis.json

More info about the options mentioned in above command can be found here .

The above command will initialize the block chain using genesis block specified using custom_genesis.json and will use privatenet as datadirectory to store blockchain data and keystore. Once the blockchain has been initialized you need to create a etherbase which is nothing but a default address where mining reward is going/credited in. This etherbase can be created using the below command:

geth –networkid 300 –identity node1 –verbosity 3 –nodiscover –nat none –datadir=./privatenet4 account new

Now you can start your ethereum miner using below command:

geth –networkid 300 –identity node1 –verbosity 3 –nodiscover –nat none –datadir=./privatenet –mine –ipcpath ./privatenet/geth.ipc

Congratulations you have successfully deployed you own private ethereum network. In the next part we will see how to interact with this private ethereum network using Mist browser.

 

 

 

Advertisements