Blockchain 101: Part 2
In my previous article Blockchain 101: Part 1 I introduced some of the fundamental concepts of bitcoin. I ended by promising a follow up post where I would come back and explain proof of work, and this is it! To recap, I am focusing on Bitcoin in this post to keep things simple. Proof of work is the way that Bitcoin does things but other blockchain use other systems such as proof of stake or proof of authority.
Let’s start by reminding ourselves why we need proof of work. The first transaction in each block is a special “coinbase” transaction that the creator can use to assign themselves a certain number of Bitcoins out of thin air. You can also have “normal” transactions that record you transferring bitcoins to someone else. The problem is, what’s to stop someone creating lots of blocks and getting all the coins from the coinbase transactions? Or creating multiple blocks containing transactions that spend the same Bitcoins (known as the double spend problem)? The answer is rules! These rules are known as “Proof of Work”.
The first idea in proof of work is to make creating blocks a computationally expensive problem that takes time to complete. To explain how this is achieved we are going to take another dive into the world of cryptography and have a look at cryptographic hashes. Cryptographic hashes are simpler than the digital signatures (that I introduced in the previous article) as there is only one function and it has a single input and single output. The input is any piece of data (e.g. a transaction or a block) and the output is a fixed length piece of data known as a “hash”.
The function has three important properties:
- The same input data will always result in the same output hash.
- Different input data will always result in a different pseudo-random output hash; slightly different input will also result in a very different output hash.
- Given a hash it is (almost) impossible to reverse engineer the original data.
In the rules of Bitcoin every block must have the property that when it is hashed (i.e. fed in to the hashing function as data) the output hash must match a certain pattern. To create a valid block the person creating the block, known as the “miner”, repeats the following process until they find a valid block:
- Make a slight modification to the block.
- Hash the modified block.
- Check if the resulting hash matches the required pattern.
The way that the miner modifies the block is by appending a numeric field, known as a “cryptographic nonce”, to the block’s data. This way they can easily modify the block slightly by adding 1 to the nonce. This system has the property that finding a valid block takes a lot of effort as it requires computing thousands of hashes but verifying a block is quick as only one hash needs to be computed.
This is only part way to solving the problem though, it does not prevent miners creating multiple blocks in parallel that contain transactions spending the same bitcoins. To prevent this, the proof of work rules require that all valid blocks form a “chain”, starting with the genesis block, where each subsequent block must include a hash of the previous one like so:
But there is still a problem, what if the following situation arises where there are two different valid blocks created which both reference the previous block, known as a “fork”:
The two blocks could contain completely different sets of transactions but still be valid so long as they include a hash of the previous block and their hashes match the required pattern. The proof of work rules resolve this by stipulating that the longest chain is the valid one. This means that, so long as 51% or all miners are honest and thus working on the same chain, it will grow the longest and be accepted as the valid. To illustrate: if we assume that “v1” is the valid block above, it is very unlikely that the malicious miner will be able to create a block following on from v2 and continue to mine blocks faster than all the honest miners who will be working on blocks succeeding v1.
And that is it! These are the fundamental concepts that make Bitcoin work, to recap:
- At its most basic, Bitcoin is a set of rules that lots of people follow independently at the same time.
- Every participant has a public key and a corresponding private key that they use to prove ownership of Bitcoins.
- Creators of blocks are permitted to include a “coinbase transaction” that assigns their keys some bitcoins out of thin air.
- Subsequently, these Bitcoins are reassigned to someone else’s keys by creating a “normal” transaction. These transactions are digitally signed to simultaneously prove ownership of the Bitcoins being transferred and authorize the transfer.
- The creation of blocks is made time consuming by requiring that the hash of every block matches a certain pattern.
- Every block must also include a hash of the previous block and the longest “chain” is the one that is accepted.
I have obviously glossed over a lot of the details to keep this post readable but these are the core principles of Bitcoin and therefore the foundations of all blockchain based systems. Understanding them will stand you in good stead for getting in on the blockchain revolution!
I hope you have enjoyed these articles and you have found them useful. Please do get in touch with me at firstname.lastname@example.org with any questions, if you have any suggestions for improving the articles or to let me know what you think I should write about next!