Voting with Blockchain

Hundreds of thousands of articles have been written about blockchain and how it is going to change the world. I am not to talk why I don’t understand how this can be possible. Neither Bitcoin, which (from my misinformed point of view) it is just a way to people trying to earn dollars: the ultimate goal of bitcoiners is wait enough time and change them to dollars (sincerely, where is the revolution?).

But this is not the topic. This is a description step by step of a blockchain algorithm implementation in C. And in order to vary a bit, instead a money transactions it is going to emulate a voting system.

Firstly, it is necessary to understand the basic concepts of blockchain. If you still don’t know it I recommend you this, this and this 5 minutes introduction video.

Now all we know what a transaction, block and hash are we are ready: Blockchain is very easy in its core. It is formed by a well-known data structure in computer science: Linked list.

A linked list is a sequence of nodes. Each node contains a bulk of data and a link to the next node of the list.

1. The block

It is declared a block with the hash of the previous block, the hash of the current block and a transaction. Instead of a set of transaction, the block has just one transaction: a single string of characters.

2. The hash

This function, copied from somewhere, take a string of characters and return an integer which is the unique hash.

3. The list

I will not explain either how a list woks since it is a very basic data structure and it is easy to find resources on Internet. However, it was not so easy for me to find a clear, clean and simply implementation of lists in C. So I take one and adapted it.

The data of the nodes of our list is the block_t structure defined previously.

And the main functions of the list is add, in order to insert new nodes at the end, and print, which is going to show the node data information, which is the block.

Those are the main ingredients. In the next post I am going to put everything together to see a blockchain running.


Disclaimer: This article is full of simplifications and abstractions in order to get the fundamental. Although one of the key points of blockchain is that database isn’t stored in any single location but it is shared and transactions are spreading to a P2P network of nodes, here is obviated all the network part.