Understanding Blockchains

A Cryptography Coding Example in R

Written by Johannes on 11 January, 2018

Share On:

Everybody is talking about cryptocurrencies now – thanks to the hype about BitCoin. Much more interesting than whether there is a BitCoin bubble or not is the question how this technology actually works and what it is capable of. I first got introduced to blockchains, which is the underlying technology of BitCoin, at a meeting of the Blockchain Society at Oxford. Everybody there seemed very excited about the potential of this new technology. And to be fair, it does sound intriguing: A decentralised, incorruptible database of monetary transactions, contracts or whatever you like (e.g. un-hackable voting machines). The promise of the blockchain to the crypto community is that it will disrupt entire industries by revolutionising trust in a way that we won't need any third parties like banks or lawyers anymore – we just need the power of cryptography.

While the basic idea is quite intuitive, the question how blockchains actually work on a technical level is a bit harder to understand. Last week I came across this article on R bloggers, where BigData Dog builds a blockchain entirely in R. A blockchain built in R might not be the most efficient and practical thing in the world but it is a great way to understand the programming and crypto principles behind it.
I wanted to understand it as well and that’s why I implemented a smaller version of the blockchain in R: "If you can code it, you certainly understand it".

What is a blockchain?

Let's say our goal is to store some data in a secure way. To this end we first store the data in a container – which we call a block. In the case of BitCoin each block contains several financial transactions. When there are new transactions (or there is new data) a new block will be created and will be added together with previous blocks to form a chain – the blockchain. Let's take a look at how blockchains are making use of cryptography to become virtually un-hackable.

1. Blocks

How does a block look like? At the very least we need some data and a timestamp. We will also add an index and a self-identifying hash (more on this in a second). We will just store it in a basic list for the moment.

block_example

Before we can start building the blockchain – a.k.a. chaining different containers with data together – we need to get to know two more concepts: Hashing and Proof-of-Work-Algorithms.

2. Hash

A hash helps to ensure the integrity of a block by connecting it to the other blocks in the chain. A hash function takes something as an input and gives us a unique, encrypted output. An example would be the following: You give your friend a puzzle "What is the best statistics program in the world?" and you give her the hash of the correct solution: "71ec0b920622cf4358bbc21d6a8b41f903584808db53ec07a8aa79119304ce86". She can know check on her own if she has the correct answer by just inputting her answer into the Hash-function (in our case the SHA256 algorithm):

How does this help us? In our case we not only input information about the block (index, timestamp, data) to the hash function, but also the hash of the previous block. This means we can only calculate the valid hash if we know the hash of the previous block, which is created using the hash of the block before and so on and so forth. This provides us with an immutable, sequential chain of blocks. If we would alter one block afterwards we would have to calculate all the hashes for the sequential blocks again.

#Function that creates a hashed "block"
hash_block

3. Proof-of-Work

If there is a lot of information that must be stored in the blockchain we will need to create a lot of new blocks. In many cases we want to control how many new blocks are created. In the case of cryptocurrencies for example coins would lose their value if there is an infinite amount of coins that can be created every second.

Therefore, we add a so-called “Proof-of-Work” (PoW) algorithm which controls the difficulty of creating a new block. "Proof" means that the computer has performed a certain amount of work. In practice the goal is to create an item that is hard to create but easy to verify. I will use the following “task” as a PoW: find the next number that is divisible by 99 and divisable by the proof-number of the last block.

For blockchains like BitCoin or Ethereum the job of creating new blocks is done by so-called miners. When a new block has to be created, a computational problem is sent out to the network. The miner which solves the PoW problem first creates the new block and is rewarded in bitcoins (this is the way new BitCoins are actually created). This "lottery" of finding the new correct proof ensures that the power of creating new blocks is decentralised. When a new block is mined it is sent out to everybody so that every node in the network has a copy of the latest blockchain. The idea that the longest blockchain in the network (the one which "the most work was put into") is the valid version of the blockchain is called "decentralised consensus".

In the case of BitCoin the PoW problem involves the problem of finding numbers that generate hashes with a certain amount of leading zeros (the best explanation I found is this video from Savjee) and is designed to take about 10 minutes to solve.

4. Adding new blocks

Now we know how a block looks like, how blocks are chained together using hashes and how the pace of creating new blocks is being regulated by PoWs. So let's put it together in a function

#A function that takes the previous block and optionally some data (in our case just a string indicating which block in the chain it is)
gen_new_block

Before we start building our blockchain we need to start the chain somewhere. This is done using a so-called Genesis Block. It contains no data and arbitrary values for proof and previous hash (as there is no previous block).

If we would add more blocks, we would see that the first blocks are created quite quickly, but as the proof of work gets much harder for every new block, it takes increasingly more time. For BitCoin the PoW is also increasingly becoming harder over the years as the mining power increases. This is done to hold the time to mine one block more or less constant.

In this little blog post we created the tiniest blockchain. The main goal was to introduce how a blockchain looks like and introduce some of the core concepts behind it. Understanding the genius application of cryptography really shows why people get so excited about the possibilities of the blockchain.

To put a blockchain into production much more work is needed: Set up an API, create wallets, digital signatures using public-private-key-pairs etc. I also only scrateched the surface on the concept of decentralised consensus which is at the heart of the blockchain network

This little introduction draws on the blog post of Gerald Nash who implemented the "tiniest blockchain" in Python. I implemented it in R and added the PoW implementation from Daniel van Flymen (https://hackernoon.com/learn-blockchains-by-building-one-117428612f46).

To dive deeper into the topic of blockchains I recommend the brilliant, orginial whitepaper on BitCoin and the blog post from BigData Dog.