Getting started with Ethereum & Smart Contracts — Part 2

Devanshu Jain
Coinmonks
8 min readDec 13, 2018

--

Hi, Welcome Back! This is the part 2 of Getting started with Ethereum & Smart Contracts. If you somehow landed on this for the first time, Welcome! Here is the Part 1 about what is Blockchain and Ethereum?

Eth — Eth — Ethereum

The Ethereum blockchain network is simply lots of EVM (Ethereum Virtual Machines) or “nodes” connected to every other node to create a network. Each node runs a copy of the entire blockchain and competes to mine the next block to validate a transaction. Whenever a new block is added, the blockchain updates and is propagated to the entire network, such that each node is in sync. This means that blockchain’s database is maintained and updated by all these nodes that are connected to the network. Each and every node of the network runs the EVM and executes the same instructions. For this reason, Ethereum is sometimes described evocatively as a “world computer”.

To become a node in the Ethereum network, our computer will have to download and update a copy of the entire Ethereum blockchain. To achieve this, Ethereum provides tools that we can download, connect to the Ethereum network with and then interact with it (basically setting up EVM). The tools are:

Geth “……if you have experience with web development and are interested in building frontends for dapps, you should experiment with Geth.”

Eth“…If you want added security by running two different implementations in parallel or are serious about GPU mining, then the C++ “Eth” client is for you.”

Pyethapp“ …If you are a Python developer that wants to build decentralized apps or are interested in Ethereum for research or an academic purpose, this is a great client”

Each of these tools will eventually provide us access to a console. We will be using Geth as our EVM in our further tutorial to get started with our development.

Installing GETH

Geth is a command line (CLI) tool that communicates with the Ethereum Network. It acts as an interface between a computer, the hardware and rest of other Ethereum nodes in the network. If a block is mined by another node, our Geth program(i.e. our EVM) will pick it up and then pass on the new information onto CPU to update the blockchain.

Mac:

To install GETH on Mac, the easiest way is by using Homebrew. If you don’t have it, first install from here.

After installing Homebrew, just enter the following commands in the terminal:

Windows:

First download the latest 64-bit stable release of Geth for here according to your type of machine. After downloding the stable binaries, extract geth.exe from zip and execute ‘geth.exe’ or open a command terminal and type:

Linux:

For ubuntu, follow these steps:

For other linux distros, download these packages with its corresponding package managers like yum and all.

Once you have installed Geth on your respective machines, you can technically connect to the Main or public Ethereum blockchain Network and run a full ethereum node.

Creating a private Ethereum Blockchain

Geth enables us to set up a testnet(private) Ethereum blockchain. It is the best way to learn blockchain concepts that you hear and read about on the internet. You can build smart contracts, make transactions an even distributed apps — without needing real ether. You can actually CREATE your own fake ether, preassign ether to your account and then use it to make transactions, transfers or deploy smart contracts.

Blockchain and its Genesis Block

Every blockchain starts with a Genesis Block, the very first block in the chain, block ZERO — the only block that does not have a predecessor.

To create our private blockchain, we will create a genesis block. To do this, we will create a custom Genesis file, and ask Geth to use that genesis file to create our own genesis block , which in turn will be the start of our custom private blockchain. Our genesis file looks like this:

chainId: Identifies our blockchain and will set it to a unique value for our private chain. The main Ethereum chain has its own ID.

homesteadBlock: Homestead is the second major version of the Ethereum platform and is the first production release of Ethereum. It includes several protocol changes. Since we are already on homestead version, this attribute is 0.

eip155Block/eip158Block: Homestead version was released with a few backward-incompatible protocol changes, and therefore requires a hard fork. These protocol changes/improvements proposed through a process Ethereum Improvement Proposals (EIP). Our chain however won’t be hard-forking for these changes, so 0.

difficulty: This value is used to control the mining time of the blocks.

gasLimit: Gas is Ethereum’s fuel(crypto-fuel) that is spent during transactions. We will mark this value high enough in our case to avoid being limited during tests. This gasLimit value specifies the maximum limit of Gas expenditure that our block could use.

alloc: This is where we can create our wallet and prefill it with fake ethers. But here we will mine our ethers locally quickly, so leaving it as blank.

Start by making two empty folders named EthGenesis and EthData in your system. These folders would help us to store chain data and the chain’s corresponding genesis file.

Creating private blockchain

Create a file as CustomGenesis.json in EthGenesis folder and paste the above genesis file code into that. Now open terminal and execute the following command: # geth --identity "1" init EthGenesis/CustomGenesis.json --datadir EthData/ACPrivateChain

This snippet instructs Geth to use the CustomGenesis.json file you created as to be the first block of your custom blockchain. Then, we also specify a data directory where our private chain data will be stored. Geth will create the data directory for you. Once you run this snippet on your terminal window, you should see Geth connect to the genesis file and provide confirmation of the same.

Next, to start our private network so we can mine new blocks execute the following command: # geth —-datadir EthData/ACPrivateChain/ --networkid 999 —-port 30303 —-rpc —-rpccorsdomain “*” —-rpcport 8545

My own private network

At this point, we can see that our private network of Ethereum is created and our blockchain is initialized with our genesis block.

Now open new tab in same terminal and to connect this new tab to the one running Ethereum private network, type: # geth attach EthData/ACPrivateChain/geth.ipc

Apparently on Windows we have to specify the IPC path. So for windows users, instead of executing the above, type the following in your cmd to have console

By doing so, we can see that we now have the console in our terminal. That means we are now successfully connected to our private chain.

To create an account in our private chain, go with this in the console

By this we can have our account address. Save this account address for future use.

At this point of time, we have initialised our chain with genesis file parameters and our chain is running on our private network with network id 999. We also have an account with Zero balance as of now. To check an account balance

Mining some ether

To get some fake ethers, we need to start mining the blocks. Start the mining and let it go for 30–40 seconds to get some balance. After it stop the mining.

As soon as we execute the command miner.start(), we can see mining description in our primary tab. After stopping mining, check again with the balance and we can see the balance in ether(wei).

Congratulations! We have created our own private(testnet) ethereum backed blockchain with an account having fake ethers.

Peer Connection

By far, we have started with our own chain on our private network. But how can we connect peers to our chain?

To add peers we have to follow the same procedure, to initialise the chain with the same genesis file and creating a network with same network id. Go with the same flow with the new machine that we want to connect to our chain. We can also open a new terminal instead of having another machine and connect that terminal to our chain.

Open terminal(in the new machine/or in the same machine) and execute these commands as we have done previously. Remember, Geth needs to be installed in the new machine.

You would have noticed the following differences:

identity: This is our identity of initialising the chain with genesis block. It needs to be different

networkid: It needs to be the same as we want to connect to the same network

port: If we are connecting a new terminal in the same machine, then port needs to be different. Otherwise it will give an error of ‘Port already in use’. If we are trying to connect another machine then port can be anything that is not in use.

rpcport: Same reason as above

Open a new tab and connect it with the running chain by: # geth attach Eth1Data/ACPrivateChain/geth.ipc

Add an account onto this network and mine some new ethers by the same procedure as above.

Now to connect this node to our existing chain, we need to know the enode address of this new node. To get enode of this new node, go with this and save the address of this new machine(terminal)

Come back to our primary machine(or terminal) and check how many peers are connected to it right now by

This means, no peer is connected to it yet. To connect the machine to our primary network we need to manually add it from our main machine.

And now if we check by admin.peers .We can now see that array is not blank.

To connect more peers follow the same procedure.

Yayy! Big Win! We have successfully created our own private network with the blockchain running on it and have one peer connected to it.

Get Best Software Deals Directly In Your Inbox

Click to read today’s top story

--

--

Devanshu Jain
Coinmonks

Dreamer | Builder | Want to be a part of Nation Building!