Writing Software Contracts for Ethereum — Part 3

Updated: 29th Jan. 2018 — (Setting up our private blockchain using geth)

  • In Part 1 we talked about the basics of Blockchain.
  • In Part 2 we understood some basics of Solidity language and wrote a simple software contract for Ethereum using Remix.

To use Ethereum, you download a program (typically geth) and run it on your computer. When you boot this program, it will reach out and connect to other computers in the Ethereum network. In the same way that you run a web browser like Chrome to connect to the web, you run an Ethereum node to connect to the Ethereum network. Each node connects to a number of peers who are already on the network.

When your node joins the network, it will begin to download the entire Ethereum “database” on to your computer. This database contains the entire state of all of the active programs on Ethereum. The full database will take more than 100GB of disk space (so you might need to buy an extra hard drive). You can read data for free, because you already have it locally, but when you want to write new data, you’re asking the entire network to receive it, verify it, and store it for you. That service will cost you. This Ethereum “database” that you have on your machine is called the blockchain (or rather, it’s your copy of the blockchain).

The problem with the public blockchain is that it takes days to download and hence we will create our own private isolated test network or blockchain which helps us to play around, it will not cost you anything and you are completely separated from the public blockchain.

Creating our own private blockchain

We shall now download and install the software that we will need for our private blockchain. In order to create our own private isolated test network or blockchain we will need:

geth

Ethereum is available as a standalone client called Geth that you can install on pretty much any operating system. We will first download and install the Ethereum client (geth) which is written in the Go programming language. The version that I have downloaded for my 64-bit Windows desktop computer is 1.7.3. You should download geth for your own operating system. I installed geth and its development tools to d:Geth.

From time to time do check the website for the latest version of Geth and re-install the same on your computer.

genesis.json

We will need something called a genesis block. genesis.json is a file that is used to create our private blockchain.

Download the genesis.json file from https://gist.github.com/smtalimett/c6d31dd423ff19c8cb97c68323f3d1d4 and store it in a folder (which you should create). I have created and stored the same in the folder d:ethereum-examplesexample_1

https://medium.com/media/b7d4fc690fd4d7563593c4bf5e29133b/href

Very briefly, this file defines some initial values for our private blockchain. chainId can be any value but not 1 (which is used for the MainNet), 2 and 3 (used for TestNet). difficulty should be a low value for faster mining. gasLimit is necessary to compile and run contracts on your private blockchain.

In the folder d:ethereum-examplesexample_1 create a folder to save your chaindata. Let’s call it chaindata. Next open a new command window and go to the folder d:ethereum-examplesexample_1 where you run the following command:

geth — datadir=./chaindata init genesis.json

You should see something like this:

https://medium.com/media/06f77e5503756edfe13738936fd67ce2/href

Inside the d:ethereum-examplesexample_1chaindata folder, it has created two sub folders geth and keystore. The geth folder holds our blockchain data and the keystore folder stores our account keys once we have them.

To start our private blockchain, type:

https://medium.com/media/f3db1428248e108c4b7073ce118a900b/href

Our private blockchain is up and running.

Now, open another command window and attach it to the running blockchain by typing:

https://medium.com/media/cafb35a0f5451bcd649085bb1e442907/href

On Linux and Mac, the way to connect to the running blockchain is by typing:

https://medium.com/media/b28f13122beba5f6c6eb7246ff9f1d87/href

For a list of methods available, go to https://github.com/ethereum/go-ethereum and click on Wiki and then on Management API. If one comes out of the Management API then do check the JSON-RPC server option too. This will list out all the eth commands.

One of the first things we will need are accounts. Lets first check if we have an account by typing eth.accounts in the command window that we just opened, as shown below. You will observe that I do have some accounts already with me; you might not have any. We shall now create an account by typing personal.newAccount(); as can be seen below:

https://medium.com/media/86660d646f0e60ac8ba4875f28e2ebb9/href

The passphrase I gave was xyz123 — you can give a passphrase of your choice. Remember to use a strong password when you handle funds on a public blockchain. Notice above that it has outputted an address which is series of characters. This is a receiving address and can be shared publicly. If you check the keystore sub folder you should see this address there.

Ethereum accounts are generated from an algorithm, completely offline. You’re not hitting a remote server to generate an account. In fact, no one knows that your account address even exists until it’s part of a transaction on the network. When you create a brand new account, your balance starts at zero.

First you need to set the account where the ethers will be accumulated by typing eth.coinbase as shown below. Since I have multiple accounts, I can change the account to which I would like the ethers to accumulate by typing miner.setEtherbase(eth.accounts[1]); as shown below.

On our test network we can create some Ethers by mining. Type miner.start(1); where 1 is the number of threads. This will return null as we are not aware of what all the thread will do internally. If you see the main command window, it has started mining. Give it some time before we can earn some Ethers. After you see several statements like “Successfully sealed new block” which would have earned you some Ethers, you can stop the mining by typing miner.stop(). To check how many Ethers you have earned type eth.getBalance(eth.accounts[1]). You could see a very large number and it’s actually the number of weis (refer to Part 1 of this series) you have earned. Do note that 1 Ether = 10 raised to 18 wei. It would be much better if we can see the number of Ethers we have earned and that’s possible if we use the fromWei method available in the web3 library like: web3.fromWei(eth.getBalance(eth.accounts[1]), “ether”)

https://medium.com/media/b50a0f5103df974fe33b625767c775b0/href

Instead of opening two command windows, we can do all our work in one window also. Type the following:

https://medium.com/media/d0fe34f6615f4463ac1586989bead1c3/href

where unlock 6 is to open my 6th account number and mine 1 is to start mining with 1 thread. Immediately, you will observe that the scrolling stops as it is asking for your passphrase. Type your passphrase and hit the enter key. I typed my passphrase which was xyz123. You can minimize the command window now.


Writing Software Contracts for Ethereum — Part 3 was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.