Blockchain - how it works

by Alex Roan on May 26, 2020

Blockchain has been one of the most hyped technologies of the last decade thanks to the massive profits that were made from bitcoin and other cryptocurrencies. Despite a lot of of the smoke and mirrors that surrounds blockchain it is a fascinating technology.

This post is based on a presentation I gave to my fellow international coleagues in Japan in 2019. I was motivated to create and deliver this presentation to combat a lot of the misinformation around blockchain solutions and when they should and shouldn’t be considered. To understand the benefits and use cases for blockchain it’s useful to understand how it works.

This requires a brief walk through of various concepts including mathematics and cryptography which have a big role in making blockchain possible. Bear with me as we work through some of the concepts it will hopefully all make sense by the end of the post.

The big idea

A decentralised ledger

Blockchain can be considered simply as a ledger, what makes it unique is it’s decentralised nature. Blockchain has various key properites:

Don’t worry if these don’t make sense now, we will cover them during this discussion.

A traditional ledger - bank account example

A bank account is a simple example of a ledger. However a ledger can be any book of record; shipments, patient records, insurance policies etc. Consider the properties of a traditional bank acocunt:

An illustration of how a user accesses money from a bank

Decentralisation

As with the bank account example a 3rd party acts as a central authority to control and manage the account. What if it was possible to keep a book of record without relying on a central authority, this would provid the following:

The potential benefits of these include:

Blockchain origins

In 1991 a paper entitled “How to Time-Stamp a Digital Document” presented a major landmark in cryptography and provided the concept which would allow us to send and receive documents across a public internet with trust. This provided the foundation for blockchain.

A screenshot of the 'how to time-stamp a digital document' paper

In 2001 Satoshi Nakamoto published a paper entitled, “Bitcoin: A Peer to Peer Electronic Cash System” which outlined how blockchain works. Satoshi Nakamoto is the name used by the person or group of people who developed bitcoin, but this is not a real person.

A screenshot of the 'bitcoin' paper

You could read through these two papers and peice together everything we will talk about here, however it does get rather technical and I’ve included a number of diagrams in this post that I hope will make the concepts easy to grasp.

A brief history of blockchain

No need for a trusted party

One of the most noteworthy benefits of blockchain is that no central authority is required.

An illustration of how bitcoin doesn't rely on traditional banks

This means blockchain can be used for an electronic currency; as is the case with bitcoin, without any involvement from banks, central banks, or governments etc.

Cryptocurrencies market

Blockchain has multiple applications, cryptocurrencies being the original and one of the most popular use cases. For context the cryptocurrnecy market currently stands at around US $250,000,000,000. May 2020 data from coinmarketcap.com:

A screenshot of a web page showing data on the size of the cryptocurrency market

Cryptography as an enabler of blockchain

To understand the concept of blockchain and some of the terminology involved we need to understand some number formats used in mathematics and technology.

A 256 bit number

An illustration of the size of a 256 bit number in base, binary and hex

What do the three sets of digits above have in common?

They all represent the same number, but in three different formats.

We normally count in decimal; also known as base 10. This simply refers to counting with 10 digits; 0,1,2,3,4,5,6,7,8,9. There are two other ways of counting that are important. Binary which uses 2 digits 0 and 1. And Hexadecimal which uses 16 digits; 0-9 and A-F. Binary is used by computers and hexadecimal is commonly used as a short form to record long numbers.

A 256 bit number is simply a number that when written in binary has 256 digits. These numbers are commonly used in cryptography and blockchain as they are hard to guess.

How hard is a 256 bit number to guess?

Because long numbers of this nature are very hard to guess they play a critical role in cryptography, unlike a 10 digit password they can’t easily be brute force guessed by computers (i.e. trying all combinations).

Basics of cryptography – can you de-crypt this?

In cryptography we apply a rule known as a cipher to a message (sometimes called plain text) to create a cipher text. For example:

An illustration showing an original message and a cipher based on it

A cipher was applied to a message to create the text Mjqqt, Btwqi! – can you figure out what the original message was.

In this case it’s quite simple to break. The cipher is a simple and fairly easy rule. It’s known as a Caesar Sipher; it was first used by the Roman Empire, and simply involves shifting the digits a number of spaces up or down the alphabet.

An illustration showing an original message and a cipher based on it with additional annotation of the cipher used

In this case we moved each digit 5 characters down the alphabet. This is also known as a map function.

About functions

There are two key ‘cryptographic’ functions that enable blockchain, these are:

For general purposes the term algorithm can be considered synonymous with function, however in certain areas of maths and computer science the term may be used with slightly different meanings.

Hashing functions

A hashing algorithm creates a short ‘fingerprint number’ which can represent an arbitrary large amount of information.

To provide an illustration of how a hashing function works, it does something like the following:

There are online tools that will convert any data into a hash. Here are three examples of some short, simple text fed into a SHA256 hash function:

An illustration of different messages and their correponding hash values

Elliptic curve digital signature functions

Elliptic curve functions are used for digital signatures, these are used to:

The mathematics is complex, so we will skip the mathematics on how the required numbers (known as “key pair”) are generated, but will look at how the process works in detail.

A good starting point for more detail is wikipedia:

Digital signatures

Public and private key cryptography

We can use the elliptic curve function to create two connected numbers which are known as public private key pairs and are used in cryptography.

An illustration of a user having a secret key and a public key!

Encryption for confidentiality

The following diagram shows how one party can encrypt a document, then send it across a public network, where it can be decrypted by another party. In this diagram pk represents a persons public key while sk represents their private / secret key. Each person has their own key pair that they will generate, they will only share or publish their public keys.

An illustration of the a cryptography matching the description below

This is a simple example of the basics of cryptography that allow us to send secure data across the internet.

However there are some problems with this data. How does party B know that the encrypted data they received came from party A? How do they know it wasn’t intercepted and modified.

This is where we need to take things further and look at a more complicated and complete example of the use of digital signatures.

Encryption using digital signatures

Step 1: Party A encrypts data with their private key

An illustration of the above point

Step 2: Party A combines the original data with the ‘Party A private key’ encrypted data and then encrypts this package with party Bs public key.

An illustration of the above point

Step 3: The package of encrypted data is sent over the public network.

An illustration of the above point

Step 4: Decrypt the package of encrypted data with the party Bs private key to reveal the data and the ‘party A private key’ encrypted data

An illustration of the above point

Step 5: Decrypt the ‘party A private key’ encrypted data with party As public key and then compare this data with the other data file, if they match you can trust that the encrypted data you received was sent by party A and was not tampered with or changed.

An illustration of the above point

This is a little difficult to follow, it’s worth reading through a few times, it’s quite straightforward once you get used to working with key-pairs.

Generating a key-pair

A fun way to geneate a 256-bit number is to use bit-address.org which will generate a key-pair that can be used for bitcoin.

With bitcoin your public key is your bitcoin address.

It’s critical to keep your private key secret as this will allow complete access to any bitcoin you own or in another blockchain any encrypted data etc.

A screenshot of the bit-address.org website

A screenshot of a generated addresses from bit-address.org

Digital signatures:

Building a decentralised ledger

Consider a ledger to track transactions between 4 parties:

An illustration of transactions between 4 parties with a table of example transactions with values

To manage this without a central authority there are a number of requirements:

Developing a blockchain protocol

The original bitcoin paper looked at each challenge and proposed a solution

An illustration of three challenges for developing a blockchain protocal which are described in the following section

Challenge 1 - transaction trust

What stops party B adding a transaction saying that party A owes them $20?

An illustration showing party A sends to party B

An illustration expanding on the above to show the value sent from party A to party B is £20

Traditionally we trade with cash on delivery or use payment systems with some inbuilt protection e.g. credit cards or paypal.

This is where digital signatures can help:

An illustration of a digital signature on the example transaction

We can now update the first challenge in our blockchain protocol:

An illustration updating challenge 1 as resolvable with digital signatures

Challenge 2 - ensuring settlement

What if party A racks up debt and refuses to settle?

An illustration of various transactions between parties

Instead of solving the problem of making the parties settle, the blockchain paper raises the questions, “What if we can remove the need to settle”?This can be done by preventing people from spending more than they take in.

Step 1: Start by giving all participants an opening balance

An update to the above illustration to add initial balances for each oarty

Step 2: Only allow transactions where no overspending occurs

An update to the above to show how a transaction could be invalid

We can update our blockchain protocol for the second challenge:

An illustration of how the second challenge can be resolved by not allowing overspending

An interesting note is that blockchain does not keep a running balance, each new transaction checks the complete bitcoin history.

Challenge 3 - storage and management

Without a central authority how do we manage the ledger:

Let everyone keep their own copy of the ledger, whenever someone has a new transaction they broadcast it out to the network.

An illustration of a new transaction going to multiple ledger copies

How can you be sure that each ledger picks up every transaction that is broadcast out and in the right order?

We can update the blockchain protocol:

An update to the challenges to show challenge three can be resolved by multiple copies

The final challenge we will cover deals with how the network stays aligned. For this we need to deep dive into the structure of blocks and look at how the chain is managed.

Blocks and the blockchain

Blockchain and proof of work

How do we ensure the network stays aligned?

Transactions are bundled into blocks. And those blocks are validated in a way that allows the network to reach consensus.

The method bitcoin uses to validate blocks is known as ‘proof of work’. This is made possible because of the foundational concepts discussed thus far:

How blocks are created

In the diagram I’ve included a screenshot of the bitcoin wallet mycellium as an example of how a user might interact with a blockchain. Using mycellium a user may create a transaction; a request to send bitcoing from their address to another address. This transaction then enters the ‘mempool’ which can be considered as a waiting room for the transaction to be added to the blockchain. Miners pick transactions from the mempool and create ‘blocks’ of transactions which they then compete with other miners to validate and add to the chain.

An illustration as described in the above text

Anatomy of a block

Using a slightly simplified block design for the purposes of illustration we can consider a block to be something like the below.

  1. Sequential ID: to ensure the same transaction / block cannot be copied
  2. Nonce (number used only once): a number the miner can vary to validate the block
  3. Data / message: the transactions in the block – normally hundreds or thousands, only a few shown for illustration purposes.
  4. Previous block hash
  5. Hash signature of the current block

A simplified illustration of a block

Many miners build blocks in parallel

Miners are constantly picking up transactions from the mempool and competing to make a block. This is all part of a system to ensure that blocks are created with a certainy frequency and that the network will agree on the order of the blocks and hence the transactions that are contained within.

An illustration showing how multiple blocks are being picked up by miners

By competing to make a block we refer to a competition that miners are taking part in. Understanding this competition is key to understanding how blockchain works and where it’s weaknesses lie. This competition is called proof of work.

Proof of work

Proof of work involves using a cryptographic hash function to encode the data contained in the block into a hash.

The format of the hash is unpredictable.

So blockchain set’s a competition for miners to find a hash with a certain number of preceding zeros. This is known as a difficulty threshold.

The only way to do this is by changing the nonce. The timestamp will also change as time progresses. The miner will keep generating a hash with this info. until eventually one miner finds a hash that wins.

Recall that the data we put into a hash function changes the hash output. Therefore changing the nonce or timestamp provides an opportunity to hash the same block of transactions over and over and generate different hash outputs.

If we take a look recent blockchain blocks; which we can do via a number of online explorers:

A screenshot from a blockchain explorer showing block details for one block

We can see that the winning block hash start with a certain number of leading zeros; currently 19 for blockchain.

Proof of work - step by step

An illustration of how a block is encrypted

An illustration of the hash value with leading zeros

Block demo

As we talk through this section, please expirement with the excellent online demo available on andersbrownworth.com

A screenshot of the above mentioned block demo tool

How blocks are connected

After a block is validated via proof of work, the hash of that block is then used as part of the construction of the next block

This means that any change to any previously validated block will make all blocks since then invalid.

An illustration showing how each block is connected to the previous block

The synchronisation challenge

The protocol deals with this by always trusting the longest chain:

Consider 3 nodes participating in a blockchain network at a given moment. The recent blocks may be different, but as node c is the longest it will be trusted and node a and b will eventually synchronise with node c.

An illustration showing how recent blocks may vary across different nodes

55% attacks

One of the commonly talked about weaknesses of public blockchains is the 51% attack. This is based on the computational feasibility to have a node or set of nodes that can validate new blocks on the network faster than anyone else for an extended period of time; there is no longer true consensus across the distributed network and a single party has taken control.

An illustration of a 55% attack as described

Recall that each transaction that enters the mempool is verified by digital signatures.

This means an attack cannot add false transactions.

However there is a way to fraudulently attack the network called the ‘double spend’ attack.

To execute the double spend attack, the attacker must be able to validate blocks faster than the rest of the network hence > 51% of computational power is needed. The first step is to take the latest block offline.

Step 1: The attacker takes the latest block offline

An illustration showing an offline copy

Step 2: The attacker mines faster than the network and Step 3: Spends their cryptocurrency on the main network.

An illustration of step 3

Step 4: The attacker goes online, as they have the longer chain the rest of the network will trust it and Step 5: They can then double spend.

An illustration of step 5

Step 6: Eventually the attacker will cease the attack, but will have by then double spent their funds.

An illustration of step 6

Public, private and consortium blockchains

Public

Bitcoin is a public blockchain is where the technology shines. The network is open for anyone to use, there is not central authority and proof of work is used to validate blocks.

Private (permissioned)

As people have tried to take advantage of blockchain they have developed so called private blockchains. The idea is that many organisations see some benefit to blockchain, but they don’t want it to be truly public. They want control of who can enter transactions and who can validate transactions / blocks.

Consortium

Alternatives to proof of work

Proof of work presents some issues. On the one hand it’s extremely processing intensive and has received criticism from an environmental perspective. On the other hand not every organisation is comfortable with an open model where anyone has authority to verify blocks based on processing power. Alternatives are always under investigation, some examples include:

Proof of work

Proof of stake

Proof of authority

Other topics

Cryptocurrency vs. token

Cryptocurrencies have been around since bitcoin. More recently ‘tokens’ have become a popular topic.

Ethereum smart contracts

Advantages

Disadvantages

dApps

Advantages

Disadvantages

Final thoughts

There are a number of questions to ask when considering blockchain as a solution. From my perspective if we are considering a truly public application where we have solid requirements such as:

Then blockchain makes sense.

For other cases where we are considering a non public network where we might restrict who can add transactions and how blocks are verified then it might be better to consider traditional database. Keep in mind blockchain comes with a number of disadvantages:

Questions to consider:

While we considered the concepts of blockchain and the history up to early 2019, recent advancements and use cases are not discussed here. What are your thoughts on blockchain in 2020?

References

I found the following resources extremely helpful when first using blockchain. You will find the inspiration for some of my diagrams here: