DEV Community

J. Marchán
J. Marchán

Posted on • Edited on

A dummy blockchain implementation in C (I)

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

typedef int hash;
typedef char *transaction;

typedef struct Block_T {
    hash previous_block_hash;
    hash block_hash;
    transaction transactions;    
}block_t;
Enter fullscreen mode Exit fullscreen mode

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

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;
}
Enter fullscreen mode Exit fullscreen mode

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.

typedef struct {
    block_t info;
} DATA;

typedef struct node {
    DATA data;
    struct node* next;
} NODE;
Enter fullscreen mode Exit fullscreen mode

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

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);
Enter fullscreen mode Exit fullscreen mode

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.

(Original post in myram)

Top comments (0)