A dummy blockchain implementation in C (I)
Tags: blockchain, c
Published: 2018-10-22 16:46:55

---
title: A dummy blockchain implementation in C (I)
published: true
description: A dummy blockchain implementation in C
tags: blockchain, c
---
##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](https://blockgeeks.com/guides/what-is-blockchain-technology/), [this](https://medium.com/the-mission/a-simple-explanation-on-how-blockchain-works-e52f75da6e9a) and [this 5 minutes introduction video](https://www.youtube.com/watch?v=SSo_EIwHSd4&feature=youtu.be).

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.](https://i0.wp.com/myram.xyz/wp-content/uploads/2018/09/408px-Singly-linked-list.svg_.png?w=408&ssl=1)

### 1. The block
```c
typedef int hash;
typedef char *transaction;
 
typedef struct Block_T {
    hash previous_block_hash;
    hash block_hash;
    transaction transactions;    
}block_t;
```
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
```c
int string_hash(void *string)
{
	/* This is the djb2 string hash function */
	int result = 5381;
	unsigned char *p;
	p = (unsigned char *) string;
 
	while (*p != '\0') {
		result = (result << 5) + result + *p;
		++p;
	}
 
	return result;
}
```
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.
```c 
typedef struct {
    block_t info;
} DATA;
 
typedef struct node {
    DATA data;
    struct node* next;
} NODE;
```
The data of the nodes of our list is the block_t structure defined previously.
```c
NODE* add(NODE* node, DATA data);
void print_list(NODE* head);
NODE * reverse(NODE * node);
void get_list_transactions(NODE* head,unsigned char *list_trans);
```

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](https://dev.to/devmarchan/a-dummy-blockchain-implementation-in-c-ii-36eh) I am going to put everything together to see a blockchain running.

(Original post in [myram](https://myram.xyz/c-blockchain-implementation-1/))