Voting… on the Blockchain?

Eila Farnood
5 min readFeb 26, 2020

--

Photo by Element5 Digital on Unsplash

You’ve probably heard of the allegations made against Trump during the election. Ok, so maybe there are a ton of them, but the one I’m thinking of is the idea that the election was rigged. This may be old news, but people’s distrust in the current ballot system will not hold for long. However, thanks to technology, we might be closer to a solution than ever before.

Introducing the blockchain 🔗

The blockchain is literally a network that has its data stored in a different manner compared to other centralized networks.

When a piece of data is pushed to the blockchain, then that piece of data needs to be verified by other people on the network. In the blockchain, there are multiple nodes, each one being a separate person.

To put data on the blockchain, it needs to be verified. This makes the process more secure. Everything on the blockchain is(mostly) open-source, meaning anyone who has access to the blockchain can see what you put on it, meaning anything you put there can be seen. The platform is a public ledger that allows others to see it and for it to be validated.

The voting dApp. 🗳

So why a dApp? The voting app needs to be user-friendly, and make sure that people would actually vote on it. Ethereum is one of the only public blockchains that can support this because it has SMART contracts. SMART contracts are code that verifies and makes an online contract on Ethereum.

This dApp was built on REMIX and I used solidity

Let’s start with the source code(the contract). The smart contract is the backend to this program, meaning it runs behind the UI/Front End.

pragma solidity ^0.4.25;contract Voting {  
mapping (bytes32 => uint8) public votesReceived;
bytes32[] public candidateList; constructor(bytes32[] candidateNames) public {
candidateList = candidateNames;
}

function totalVotesFor(bytes32 candidate) view public
returns (uint8)
{
require(validCandidate(candidate));
return votesReceived[candidate];
} function voteForCandidate(bytes32 candidate) public { require(validCandidate(candidate));
votesReceived[candidate] += 1;
} function validCandidate(bytes32 candidate)
view public returns (bool) {
for(uint i = 0; i < candidateList.length; i++) {
if (candidateList[i] == candidate) {
return true;
}
}
return false;
}}

Let’s dissect this code and look more deeply into it.

contract Voting {  
mapping (bytes32 => uint8) public votesReceived;
bytes32[] public candidateList;constructor(bytes32[] candidateNames) public {
candidateList = candidateNames;
}

The first contract, Voting, first takes in a mapping value, which is a hash table.

Hash Table → A type of array that stores abstract data

Basically, mapping works in a similar manner to an array. In this mapping, we are storing the candidate names.

The constructor mainly passes the information to the blockchain once the contract is deployed. This will be apparent if this dApp was tested on a public blockchain like Ethereum or a test blockchain like Ganache.

function totalVotesFor(bytes32 candidate) view public 
require(validCandidate(candidate));
return votesReceived[candidate];
{
returns (uint8)
}

function totalVotesFor takes in the candidate and returns an integer(the number of votes). The required variables are that the candidate is valid. Meaning they have to be on the array list.

Required variables are necessary to execute certain functions because if it randomly activated, then the function would be ineffective and not serve the assigned purpose. The required variable can also serve to restrict access to certain functions so only some people or even one person can activate that function.

Then, the program returns the votes received to the front end of the program.

function voteForCandidate(bytes32 candidate) public {    require(validCandidate(candidate));    
votesReceived[candidate] += 1;
}

The function voteForCandidate takes in the candidate name as a parameter and requires, again that the candidate is valid. Then the counter for the total amount of votes is increased. This is important because this program is counting votes, and the rest is self-explanatory.

function validCandidate(bytes32 candidate) 
view public returns (bool) {
for(uint i = 0; i < candidateList.length; i++) {
if (candidateList[i] == candidate) {
return true;
}
}
return false;
}}

function ValidCandidate judges the “candidate” based on a boolean value. That means true or false. The function will not execute if the conditions are not met.

Let’s dissect that for loop, shall we?

i is the condition here. i increases by one each time, and as long as i is under the candidate list length, then the loop will keep running. In the for loop is an if statement that checks that the candidate list checks out, making sure the candidate is in the array.

Why does this all matter?

The truth about voting scandals is that they barely ever happen. In the US, there have been at most 241 fraudulent ballots in the past 14 years of voting.

So, why should we really care about this problem? If it’s so exclusive and barely occurs, why do we need to upgrade our system?

Well, democracy is built on people’s trust in the system. They believe that they elect will be a representative of the majority of their community. But there is always distrust in the system. People know that it is possible to cheat our current system of counting the ballots, and when the decision comes down to just one or two votes, it’s no surprise that people are starting to lose faith in the system. The deciding votes in those situations could be a fraudulent ballot, while also being the difference between America’s first female president or someone who thinks that climate change doesn’t even exist.

Voting fraud is still widespread. People are still being able to cast fraudulent ballots and get them through the system. The number of fraudulent ballots should be 0. People need to be able to have faith in the system, and if one or two ballots can slip by, there is no telling how many people will exploit its weakness.

Key Takeaways 🗝

  1. A voting dApp can help regulate elections so that they’re more fair and transparent.
  2. The essence of democracy is a fair vote for a leader, and by updating our program we can try to bring the number of fraudulent votes in our system to 0
  3. Smart contracts are the backbone of dApps, and functions can be implemented to control the outcomes.
  4. Almost any app can be replicated into “dApp form”.

If you enjoyed this article, please give it some claps so I know you liked it! You can also follow me on LinkedIn to see what I’m up to :)

--

--

Eila Farnood

A Canadian student who is super passionate about exponential technology!