Articles Partners

My journey towards understanding cryptocurrencies - Episode 2 - The Blockchain

My journey towards understanding cryptocurrencies - Episode 2 - The Blockchain

Part two: What is a blockchain

Before we start getting any deeper into cryptocurrency details, there are a few basic ideas that we should take a closer look at.

We already learned that bitcoin balances and transactions are stored on a virtual piece of paper called the blockchain. The blockchain, as the name suggests, is a chain of blocks where one block is connected to the former one through something that is called a hash.

All double dutch to you? Ok...here a bit more visually.

We will create a very simple blockchain ourselves.

Let’s say our non-existing guy Satoshi starts a new cryptocurrency. He defines the rules of the game and he mines the first block, the genesis block.

He defines the riddle that one needs to solve to mine a block as follows:

“Take the hash of the former block. Add to it the sum of all transactions in the current block. Take the result, divide it by 17 and get the remaining part of that operation (a so called modulo). That is the hash of your next block.”

The genesis block looks as follows.

No alt text provided for this image

Now what does it tell us.

1.    This is Block Number 1 (called the genesis block or block 0)

2.    This first block has one transaction on it, namely the first 50 coins that our guy Satoshi has gotten, for mining this block (the reward)

3.    And then there is the number in the upper right corner, which is the solution to the riddle. As this is the first block there is no hash to the former block. The sum of all transactions is 50. The remaining part of 50 divided by 17 is 16 (2x17 = 34 + 16 = 50)

Done…our blockchain has come to life with the first mined block. Now that is not the end. Look at the following second block:

No alt text provided for this image

On this block, mined by Olivia, who was rewarded 50 coins, we have three additional transactions. Now let’s take a look at our riddle.

This time the hash of the previous block was 16. We also have 4 transactions of 16, 5, 29 and 50 coins, summing up to a total of 100 coins. We add the 16 from the previous block, divide this by 17 and get the remaining part, which is 14 (6x17 = 102 + 14 = 116).

It is important to note, that the hash of this block takes the hash of the previous block into consideration. Let us take a look at a third block before we draw some conclusions.

No alt text provided for this image

Let’s keep it shorter this time. The hash of the previous block was 14, the sum of our transactions here is 82. This sums up to 96. 96 divided by 17 gives us a rest of 11 (5x17 = 85 + 11 = 96).

Ok…I think now it is clear why the blockchain is called the blockchain. If a person goes through the chain starting from the genesis block, that person can verify the integrity of all the blocks by looking at the solutions to the riddles (the block hashes). If a bad guy comes around and modifies one block in the middle, that modification would change all the solutions to all the upcoming riddles and therefore that altered block can be easily identified.

Now that we understand the basic concept let’s take a look at a block on the bitcoin blockchain.

On the bitcoin blockchain a block is a bit more complex than in our example above. But not so much.

A block is split into two pieces:

1.    The block header

2.    The transactions

Let us take a look at the transactions first.

A transaction is composed of one or multiple inputs, that is addresses from which bitcoins are sent, and one or multiple outputs, that is addresses to which bitcoins are sent. In addition to the output addresses a transaction stores the amount of coin to be transferred to a specific output address.

We will take a look into addresses later in this series. For now it is sufficient to understand that an address is comparable to an account in the real world, on which a balance of bitcoin can be held.

Bitcoin transactions have one special thing to mention. The coins that are currently held on a given input address are always fully spent. If the total amount of coin on all input addresses is greater than the total amount of coin on all output addresses, an additional output will be generated that transfers the remaining part back to one of the input addresses (or to a new one that also belongs to the spender).

And, and that is something many people don’t know, transferring bitcoin is not for free. Usually there is a small gap left between the sum of all inputs and the sum of all outputs. That is the transaction fee that is for the miner of the next block to claim. Miners can decide which transactions they include into their mined blocks and a transaction is not executed until it was included into a block by a miner. Miners usually give those transactions a higher priority that have a higher fee attached to them.

Ok…so a miner includes as many transactions as he wishes into the new block. In theory a new block could contain zero transactions, however the more transactions a miner is able to include, the more fees he can claim.

All transactions to be included in the new block will be stored into something called a merkle tree. The root node of such a merkle tree is called the merkle root and is hashed and included in the header of the new block.

Now let’s take a look at the header.

 The header of a block on the bitcoin blockchain contains the following information

-      The previous block’s hash (this is a chain of blocks)

-      The hash of the merkle root of the transactions in the block (which changes when transactions are added or removed)

-      The timestamp when this block was mined

-      The current difficulty of the riddle to be solved

-      The nonce…this is the number that miners search for in order to solve the riddle

 And now to the most important thing, the riddle.

Bitcoin uses something called SHA256 hashing as part of the riddle.

SHA stands for Secure Hash Algorithm and 256 means, that the output is a 256bit value (a big, big, big number).

We already had a look at hashing in our simple example before. Hashing a value means that you execute some operation on a given input and you get a given output, which is always the same if you hash the same input again and again. A hash function is therefore deterministic. Also, it is close to impossible to reverse the hashing operation. In our example riddle above the inputs 18, 35, 52 would all produce the hash of 1, but we never know which input was given when we look at the resulting hash. It could be any of the three or a totally different one.

In our example however it is easy to predict what the hash will be if we change the input. If our input is 18 the hash will be 1, if our input is 19 the hash will be 2. Easy to predict. For a SHA256 it is not possible to predict what the output will be if you make a small change to the input.

For example, the text “hello” produces the hash

2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

And the text “hello.” With just an added dot at the end produces the hash

1589999b0ca6ef8814283026a9f166d51c70a910671c3d44049755f07f2eb910

Bummer…

It is now up to the miners to build a block, by playing with the transactions, the timestamp (within given boundaries) and the nonce so that the hash that results from the block is smaller than a given number. Visually this can be expressed as “the hash has to start with x zeros”. Currently that is 19 zeros at the beginning of the hash, i.e., something like

000000000000000000083026a9f166d51c70a910671c3d44049755f07f2eb910

And doing this is not difficult, but time consuming (and therefore energy consuming).

Now you can imagine that the solution will be found much faster if more people participate and split the work in a clever way. And that is exactly what happens in real world. More and more people participate in mining and the solutions will therefore be found faster and faster. And how does the bitcoin network react? It increases the difficulty on a regular basis. And also, this is part of the rules given by Satoshi. Every 2016 blocks the difficulty is adjusted. The goal is to keep the time it takes to mine a block at roughly 10 minutes. If more people mine blocks will be mined faster, the difficulty goes up and if less people mine it will take longer, the difficulty goes down.

And, of course, there is one thing to be mentioned before I close this episode…there is not just the fee that a miner claims when he has successfully mined the next block. There is also the big price. New coins are generated with each new block. Currently 6.25 BTC, which is a fairly decent reward given the current value of 1 BTC being 50K USD.

Ok…now this was a very very dry session and I hope you still stick with me. Because from now on we have most of the basic knowledge to get a bit deeper into the mining.

 The hardware has already been ordered and, by now, was assembled…

A post by
Christian Reichel
Christian Reichel