Group Services: Technology Consulting
phone +91-9999-283-283/9540-283-283
email info@sisoft.in

Introduction to Blockchain

Blockchain is a distributed network of virtual blocks on the internet that keeps growing as people keep participating in the network and transactions keep happening with the network. You can think of blockchain as a huge excel sheet or a huge record keeping diary which is used by people to make entries in it.

All the participants are free to go through these records. This feature of blockchain provides transparency to all the participants in the network and makes blockchain a viable platform for conducting activities that require transparency. When we say transparency, it does not mean that participants can not have privacy while using the blockchain.

What it actually means is that they can agree upon policies among themselves - policies that will promote transparency in their day-to-day transactions while maintaining privacy too. This aspect of Blockchain is actually very much what we would like to see in any transparent and efficient system. E.g. - if we both agree to work on a project together , we both will be contributing to the project during our work time. However, what you or i do during our free time is a matter of privacy. This is an ideal workplace and this is what Blockchain provides.

To use Blockchain ,there are a few platforms that are available. We are going to use Ethereum BlockChain platform. Any program/app that you make on a blockchain network is called dApp - Decentralized Application. It is called so because this application is not stored or hosted by any centralized server.

Like we said earlier, blockchain is used to register transactions that are available for everyone to see. Every transaction is called a block and all these transactions are linked to each other - hence the name - blockchain.

Now before we proceed ahead, let us have a look at basics of Ethereum Blockchain network that one needs to know.

*Contract is a generic term, Smart Contract is a contract written in Solidity.

You need to learn basics of Solidity in case you not in touch with any programming. However, if you code regularly you may continue.

Making of the App -

Blockchain being an open source concept and with the constant need of bringing transparency to the systems , has been gaining a lot of traction among people. Let’s make a dApp here.

Executing an Election Contract in Ethereum Blockchain.

In this example, we will be executing a contract to hold free and fair elections. We will be replicating a real world election and hence consider all the cases. Below are the summarized requirements of the election :

  1. Must have secret ballot. No one knows who voted for whom.
  2. Must be able to give the total count of votes and provide the results.
  3. Must be timed i.e. you can’t vote after the end-time and you can’t end the election before the end-time.

How to create a Contract(Smart Contract) for this ?

A smart contract will be created to carry out this activity. Below are the required components of the contract-

  1. We will be using a data type - struct - to capture the details of a candidate appearing in the election.



  2. We also need to create another struct that captures the details of the Voter.



  3. An address data type to store the address of the contract owner.



  4. A string data type to store the name of the election being held.



  5. A mapping data type to keep a track of all the voter’s addresses.



  6. An array to hold the candidates contesting in the election.

As we have taken care of the participants in the election , we need to now look at conducting the election. For the same we need below variables -

  1. An event to declare the election end. This event will declare the number of votes each candidate secured.



  2. A constructor to create an instance of the Election contract. The instance should have the name of the election , duration of the election , names of the candidates.



  3. A function to authorize a voter to cast their vote. In this function we accept address of the voter as the input argument and check for 2 requirements - the authorizer has to be the contract owner and also the voter has not yet voted in the election.



  4. A function to let the voter finally cast their vote. In this function we will be taking the vote as input argument, checking if the election is still going on , the voter has not yet voted, mark the voter as ‘has voted’, incrementing the vote count of the voted candidate.



  5. A function to end the election .Although the result is declared by the event - electionResult but this event is triggered by the function end()

Complete code for the contract.

Below is the complete code for the Election Contract -

Executing the Contract.

Let us now run this contract on remix IDE using JavaScriptVM option. Below are the screenshots of the contract execution -

  1. You get to use 5 accounts from JavaScriptVM option to test.
  2. All the accounts are initialized to 100 ether.
  3. Provide the input arguments to the constructor and click ‘create’ button.

We have the following inputs:

  1. Name of the election - Demo Election
  2. Duration of the election - 3 minutes
  3. Name of Candidate 1 - Candidate1
  4. Name of Candidate 2 - Candidate2
not available333 not available333 not available333 not available333 not available333 not available333

As we saw in this example, we can create an election contract to conduct voting and declare result. A few points to highlight are :

  1. In case the ‘end’ button is clicked before the decided time, the system(EVM) does not accept it and reverts to the initial state.
  2. In case an account which is not authorized tries to cast a vote, the EVM does not throw an exception or does not revert. However, the vote is not counted.
  3. If an account tries to vote after the election decided time, the system(EVM) gives a VM error and reverts the state to the initial state.