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.
Solidity is a contract - oriented programming language developed by ethereum foundation. This language has been developed to create codes to make use of the platform.
Contract(Smart Contract*) - A contract/smart contract is a computer protocol intended to facilitate, verify, or enforce the negotiation or performance of an activity. This activity is designed to carry out a task continuously or for limited time. Contract is like an object which is used in Object Oriented languages as a basic unit.
e.g. - if we want to carry out an activity wherein the participants can contribute in writing a blog and earn credits for the contribution, we will write a code(smart contract) which keeps a track of the blog contribution made by a participant and also the credits s/he carries with them. This contract will keep a track of all the contributions that keep coming in and any edits made to the blog. Basically , a smart contract is a code written keeping in mind the task to be completed and various possibilities around it. It can be argued that a smart contract is nothing but a general code. However, we would not like to encourage this argument. When you create an object in object oriented programming , you can use several features in Object oriented programming to solve problems. Similarly, when you create a Contract(Smart Contract) , you are provided with several features which can help you execute a contract and make it useful to carry out a task.
It’s like comparing apples and oranges.
*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 :
- Must have secret ballot. No one knows who voted for whom.
- Must be able to give the total count of votes and provide the results.
- 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-
- We will be using a data type - struct - to capture the details of a candidate appearing in the election.
- We also need to create another struct that captures the details of the Voter.
- An address data type to store the address of the contract owner.
- A string data type to store the name of the election being held.
- A mapping data type to keep a track of all the voter’s addresses.
- 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 -
- An event to declare the election end. This event will declare the number of votes each candidate secured.
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.
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.
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.
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.
- All the accounts are initialized to 100 ether.
- Provide the input arguments to the constructor and click ‘create’ button.
We have the following inputs:
- Name of the election - Demo Election
Duration of the election - 3 minutes
Name of Candidate 1 - Candidate1
Name of Candidate 2 - Candidate2
As we saw in this example, we can create an election contract to conduct voting and declare result. A few points to highlight are :
In case the ‘end’ button is clicked before the decided time, the system(EVM) does not accept it and reverts to the initial state.
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.
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.