Decentralizing Democracy

A simple Dapp for more secure voting

Democracy might be the backbone of our society, but how do we know when we go out to vote our ballots actually count?

We don’t…we place our trust in the government that these processes will be carried out appropriately.

But what happens when the people controlling these processes are corrupt?

In 1984 the Prime minster of India was assassinated, elections were urgent with the need for a new head of the state, people were forced by military and gangs to vote at literal gunpoint. The election was anything but democratic.

Even in recent North American elections, there’s been controversy over election manipulation making headlines nearly every week. People don’t have faith in the security of the countries voting systems, more than half of Americans don’t.

This is a system that we’ve trusted for hundreds of years but system whose broken our trust hundreds of times.

What about citizens living abroad or serving military service, who still want a say in how their home country should be ran. Or people who lack transportation to these voting locations or those who are afraid of voting publicly. Without having to wait in long lines and process the ballots the process could be faster and results would be in near real-time.

Online voting platforms run the risk of losing your identity one of the backbones of democratic voting is security of identity. What’s stopping a government from manipulating the online votes or these systems from getting hacked.

A single hack could change the future of a country forever.

With blockchains we could have a system that’s real-time, accessible even for those not able to reach polling stations, that’s tamper-proof and will almost never go down, while helping protect people's identities.

Image for post
Image for post

The blockchain is like a decentralize ledger that’s immutable, meaning the data can’t be tampered with once it’s on the ledger.

Most of our current systems are on centralized networks meaning there’s one node in the middle and a bunch connecting to it. These types of networks only take one point of failure in a hack for the system to become vulnerable.

A blockchain is decentralized and distributed meaning all the nodes are interconnected, every node has a copy of this ledger, this

Transactions on blockchains can be transparent while still being anonymous using public addresses which serve like pseudonyms for users.

The backend for a Dapp is a smart contract, a smart contract is self-executing code on the blockchain, once this code is deployed it can’t be changed.

Similar to a contract in the real world where multiple parties create a set of conditions, if these conditions are true then other events are activated.

Smart contracts can eliminate the need for middlemen who traditionally bridge gaps in trust. You don’t need to trust that the other party will follow through, you just need to trust the code.

The biggest platform for deploying these smart contracts is Ethereum. Lots of developers wanted to create smart contracts and build decentralized apps(DAPPS), but they’d have to build their own blockchain or go through tedious steps that created a lot of friction. Ethereum provided one ready to go blockchain structurally built for handling Dapps and smart contracts.

To deploy smart contracts we need to use gas as payment. Gas is just Ether(Ethereum’s cryptocurrency). These payments prevent malicious users from just running a never-ending loop and depleting the system of its resources, it economically disincentivizes bad behaviors. This gas payment is tiny for each interaction though, so it’s not a big barrier for us.

  • Ganache is a local blockchain instance, it gives us 10 accounts each preloaded with 100 ETH.
  • Truffle is a framework for building and testing Dapps,
  • Remix is an online IDE, for debugging contracts in solidity, we can compile and deploy them onto different blockchains too.
  • Web3 connects our client-side with the smart contract
  • Metamask chrome extension wallet for interacting with smart contract

The Dapp allows us to have multiple candidates running in an election, every address can cast a vote towards one of these candidates, the Dapp will display the vote count for each candidate in real-time.

First, we define the compiler version using pragma solidity ^

pragma solidity ^0.4.25;

Create a struct which will hold fields of the candidate

struct Candidate {

uint id;

string name;

uint voteCount;

}

We want to keep track of the accounts that have voted so we’ll create a mapping, with the address as the key and have it point towards a boolean value, true for voted and false for not.

mapping(address => bool) public voters;

We create a mapping for our candidates using their id that will point towards their candidate struct, we call this mapping candidates.

mapping(uint => Candidate) public candidates;

To keep track of how many candidates we have running in our election we create a counter called candidatesCount

uint public candidatesCount;

We create an event for voting taking in the id of the candidate.

event votedEvent (

uint indexed _candidateId

);

If we want more than 2 candidates we create a function for adding a candidate that takes in their name. We increase our candidatesCount, then add our new candidate to the candidates mapping at the current candidate count.

function addCandidate (string _name) private {

candidatesCount ++;

candidates[candidatesCount] = Candidate(candidatesCount, _name, 0);

}

We always want at least 2 candidates to vote for or else that wouldn’t be much of an election. So we use a constructer, so every time we deploy the contract it’ll default have two starting candidates.

constructor () public {

addCandidate(“Candidate 1”);

addCandidate(“Candidate 2”);

}

A function to actually vote takes in the candidates Id. But before they can vote we need to make sure they haven’t voted twice so we use a require statement that checks if the address is in the votes who already voted mapping we did before.

function vote (uint _candidateId) public {

require(!voters[msg.sender]);

We also need to make sure that their voting for a valid candidate we use another require statement that makes sure their ID isn’t greater than the number of candidates we have running in the election.

require(_candidateId > 0 && _candidateId <= candidatesCount);

If all those conditions are true then we record that the voter voted, then increase the candidate's vote count. And trigger the voted event.

voters[msg.sender] = true;

candidates[_candidateId].voteCount ++;

emit votedEvent(_candidateId);

}

Hope you learned something, feel free to reach out on LinkedIn or subscribe to my newsletter to stay connected!

Playing around with blockchain and machine learning. 17. Trying to understand my self and the world.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store