- Understand the shortcomings of the current voting system
- Appreciate the benefits of a decentralized voting system and the problems it solves
- Deploy a smart contract using Ethereum that carries out decentralized voting
- Remix: Solidity IDE
Now that you have an idea about writing a basic smart contract using the solidity interface, let’s move on to a more useful application using Blockchain – A decentralized voting application.
Problems with the existing voting system
The traditional voting system is hosted by a centralized authority and takes place in a common centralized region where people in that region vote. In this system, there is a third party that coordinates the voting procedure. The major problems in this system of voting include people casting unauthorized votes, the same person voting multiple times, tampering of votes, and voting procedures not terminating after the allocated polling time is over. The idea here is to use blockchain technology to hold a free and fair election that addresses all the above mentioned issues in the current system. We deploy this using a blockchain to make the votes immutable and decentralized, preventing any tampering of data. So let’s get started. For simplicity, let’s start by allowing only two people to vote.
We start by setting the version of solidity that we require. This is to ensure that our program gets compiled properly even when the default version is updated in the future. The constructor function will have a structure called candidates that stores all the candidate details, name of voter and the number of votes. Then another structure called Voters will keep track of if a certain voter has already voted or not. We initialize an integer variable called weight, to ensure that only those voters who have been assigned a weight of 1 have the capability to vote. We will end the voting session only after a stipulated time as defined by auctionEnd. Even if candidates who aren’t authorized vote, their weight by default will be zero and hence it will not affect the final result. In addition, precautions are taken to ensure that each user votes only once. Copy the above code and compile it. Once compiled, move on the section which says “Run”, on the top right hand corner.
In the deploy text field, we need to type in a voting objective. For example, let’s look to settle the age long debate of Apple vs Android. You will need to type the following within the deploy textbox: “Apple vs Andorid”, 1 , “Apple”, “Android”. The first argument is the question which needs to be investigated, followed by the time for which the voting will be valid ( in this case, 1 minute), and finally the two options which a person can vote for (i.e, Apple and Android). After this is done, click on deploy and a smart contract will be created. Under deployed contracts, there is a field called authority. This is where you need to specify which users are authorized to vote. To do this, scroll to the top of the run tab and find the drop down box that says Account. The account which is there by default will be the owner as the contract was deployed with this owner selected. Copy the address of the user and paste it in the authority tab of the owner. Give authority to two more users to vote, by selecting any two users one by one, copying their respective addresses, switching the account back to the owner account, and replacing the previous address with the copied address of the other user. In the vote tab in the smart contract, you can either type in 0 ( index value of Apple) or 1 (Index value for android) to cast your vote. Each account can vote only once and you need to switch accounts to cast your next vote. After waiting for a minute (voting time), click on end to complete the voting process. Click on “details” on the command prompt to find out the winner!
There you go, you’ve created your own decentralized voting smart contract. Play around with the code and try adding more candidates and voters. In the next post, we will take a look at how to deploy a lottery system and finally an IoT system using the backbone of this technology!