In this article I will explain how a blockchain (also spelled block chain, but since I’m Swiss I like to put my words together) works and what it accomplishes with regard to cryptocurrencies. My goal is to explain the concepts in simple terms that should be understandable without a technical background. Where I feel that technical details might be of interest I added special Detail sections that dig deeper. It is not necessary to read those for a general understanding of blockchain.
The concept of blockchains was first introduced in 2008 by Satoshi Nakamoto for use in the cryptocurrency Bitcoin. The original purpose of a blockchain was to facilitate the direct sending of payments from one person to another without the need for a financial institution. To make this possible Satoshi Nakamoto proposed the use of a decentralized blockchain that would use cryptography to store transactions. The word decentralized implies that there is no need for a trusted third party.
To understand how a blockchain functions we first take a step back and look at how these “trusted third parties” – most often banks – store which customer has how much money in which account.
Banks use databases. You can imagine a set of tables such as this:
Updating a Database
Imagine that you want to send some money to a friend, pay a bill or withdraw some money from the ATM. The database would have to get updated as it doesn’t reflect the current state correctly anymore.
First we need to send the new information to the database. This is usually done by some client (software) contacting a secure backend (another software) which in turn sends the corresponding commands to the database.
One needs to ensure that a single record (a row in the database) is not updated simultaneously by two different transactions. This process is called locking. So to update a record in the database it is first locked, then updated and finally unlocked. This is done so that no change accidentally overrides the previous change. Imagine the two transactions to account 123. If the record would not be locked the following scenario could happen:
- The first transaction reads the balance of account 123 as 100.
- The second transaction reads the balance of account 123 as 100.
- The second transaction (being faster than the first) updates the balance to 10’100.
- The first transaction updates the balance to 1’100.
In this scenario suddenly 10’000 of whatever currency just vanished into thin air. With locking the following would happen at the bank.
- The first transaction gets the lock for the record with ID 1 in Table Accounts.
- The second transaction attempts to get the lock the record with ID1 in Table Accounts, but it is already taken.
- The first transaction reads the balance as 100 and updates it to 1’100.
- The first transaction releases the lock.
- The second transaction gets the lock for the record with ID 1 in Table Accounts.
- The second transaction reads the balance as 1’100 and updates it to 11’100.
- The second transaction releases the lock.
As you can see there are many more steps involved to achieve a secure protocol which guarantees atomicity (meaning that records are read and updated in the same step).
There are several points we’d like to mention regarding the way banks store information.
- It is necessary to trust the bank to correctly store information.
- It is possible (although unlikely) for the database to get hacked.
- Only 1 process can change a particular row in the database at the same time.
- Transactions aren’t final. Once a transaction appears in an account there are processes that could reverse the transaction. If you pay for goods using your credit card it is possible to get your money back in some cases through mediation. This results in work for the bank / credit card provider which in turn leads to higher transaction fees.
Blockchains are decentralized. They replace the need for trust with math or more exactly cryptographic proofs. They also require no locking as anyone can write data to the blockchain once certain conditions are met. Of course some order has to be established and rules exist.
What is a Blockchain?
A Blockchain stores transactions and is regularly updated by many different people called miners. These transactions are stored in blocks which are then connected to each other. The result looks something like this.
So you see where the term blockchain comes from, we have a chain of blocks.
A block consists of transactions, the hash of the previous block and some additional technical information.
- Each transaction consists of the sender data (input(s) to the transaction), the receiver data (output(s) of the transaction) and the amount that was sent.
- The hash of the previous block is what creates the chain aspect of the blockchain. It is also an important security feature of the blockchain.
- The additional information consists of various data about the block itself such as its size or the current time and special information that is relevant for mining.
Since each block contains a hash of the previous block which again contains a hash of the previous block every new block basically has a hash of the whole history of the blockchain up to this point! This makes it incredibly difficult to hack.
If you would like to look at all the information in a block yourself you can use a block chain explorer. This is a tool that lets you look at blocks in the blockchain and translates the information into human readable format. For example you can have a look at the first ever Bitcoin block that was mined which is also known as the genesis block. If you would like an explanation for the various numbers an excellent one can be found here.
You might note that the blockchain does not keep an account of which user has how much money in which account like we have seen with the database that bank is using. Don’t worry. This information can be inferred from the transactions (this would also be possible in a database, but would take much more time due to the way the data is stored).
So we now know how the blockchain looks and what information is stored inside, but how is new information added?
How does the Blockchain get Updated?
Depending on the specification of a blockchain a new block is added at regular intervals by a miner. For Bitcoin this happens roughly every 10 minutes. Miners have incentive to add new blocks because they get a reward for doing so. Before a miner can add a new block he has to perform some very difficult calculations and find a special number called a nonce that matches certain criteria.
But from where does the miner know which transactions to include?
This information comes from a structure in the Bitcoin network, the peer-to-peer network (P2P network). Every time someone wants to send some bitcoin to someone else he needs to connect to the P2P network and broadcast the transaction. Let’s break this down into less technical terms:
- Alice wants to sent 1 bitcoin to Bob.
- Alice uses her wallet software to achieve this.
- The wallet software (which can also be referred to as a client) searches other clients on the P2P network and connects to them. This means it can now send and receive information from all connected clients.
- Alice confirms that she wants to send 1 bitcoin to Bob.
- Her client then informs all connected clients of this transaction.
- All connected clients inform all of their other connected clients of this transaction and so forth. This is called a broadcast.
The miners are also part of the P2P network and listen for new transactions. Once they have heard of enough new transactions they will attempt to create a new block. If they successfully find a suitable nonce, a “golden” nonce, they will then broadcast this block to the network. The other nodes in the network will accept the block if it is valid. Then they will start to create the next block using a hash of the block they just received. In this way the blockchain gets extended.
It is entirely possible that two miners find two different blocks at the same time. They will then both broadcast the blocks to the network. Since honest miners always extend the longest chain one of the two blocks will eventually be orphaned. Only the miner who found the block on the longest chain will be rewarded. Therefore honest miners have no incentive to not extend the longest chain as they will not get any money.
We conclude the following:
- There is always only 1 path going back to the genesis block (the first ever created block). In the above image it is the path of black blocks.
- There might be a fork in the road when walking through the chain from the genesis block towards the end. One part of the fork will always eventually be part of the main chain, the other part(s) will become orphaned.
This image sums up the different terminology for blocks in the blockchain.
We now know how a traditional bank stores and updates its information and how a blockchain achieves the same thing. A few details remain unclear however. If there are only transactions stored in the blocks how is it possible to efficiently find certain information without having to go through every block in the whole history of the blockchain?
How can we follow the history of a coin?
To make it easier to navigate through the blockchain additional links are inserted between the output of one transaction and the input of the transaction it originated from. So imagine that Bob were to send 1 bitcoin to Alice. Afterwards Alice sends 0.5 bitcoin to Eve. We would have the following transactions:
We notice something curious in these transactions. Why is there a transaction of 0.5 bitcoin from Alice to Alice? This is because it is always necessary to spend the entire input of a transaction by design. We can also see that both transactions from Alice point to the output of the first transaction. This way it is really easy to follow the history of a coin in the blockchain. When using a blockchain explorer you can use this to easily see where one of your coins came from back to the moment when it was first created.
How exactly are bitcoins created? Each coin originates in a so-called coinbase transaction. Remember how we mentioned that the miner who adds a block to the longest chain of the blockchain gets rewarded? To collect this reward the miner simply adds a transaction that will send a specified amount of bitcoin to himself. This amount of bitcoin is predetermined. It started at 50 bitcoin in 2008 and is set to half every 210’000 blocks. We are currently (end of February 2018) around block 511’000, so the reward has been halved twice and is at 12.5 bitcoin. At some point in the future the reward will drop to 0 bitcoin. The basic idea behind the halving events is that it decreases the amount of new currency being created over time.
So basically new bitcoins are generated roughly every 10 minutes when a new block is added to the blockchain. In addition to the coinbase transaction miners will also be able to collect all fees of the transactions contained in the block. This combined payment is referred to as a block reward. You might think that 12.5 bitcoin plus transaction fees is a lot of money, but miners have to make huge investments in order to be competitive. It would not be a lucrative business for anyone without specialized hardware, expert knowledge and access to cheap power.
Another aspect which we haven’t completely answered is the following.
How does the blockchain know who has how much money in which account?
With our knowledge so far it would absolutely be necessary to search through the entire blockchain and create the balances based on all the transactions in order. Luckily this isn’t necessary.
To make it easier to find out who owns how much money in which account there is a special UTXO pool. This is a pool containing all unspent transactions or more technically all transactions that have an output which has not yet been the input of another transaction. To find out how much money Alice owns in her account it is only necessary to go through all UTXOs and add the amounts of all transactions where Alice’s account was in the output. Let’s make an example.
We have the following UTXO pool:
In this case we know that Alice has 1.75 bitcoin in her possession. You might think that Alice only has 0.5 bitcoin since she received 1.75 bitcoin and spent 1.25 bitcoin, but this is not the case. Just remember that all transactions in the UTXO pool are unspent. So Alice does have the 1.75 bitcoin. At some point she potentially had more bitcoin since she was able to send 1.25 bitcoin to other people.
The UTXO pool also makes it much easier to verify whether a new transaction is actually valid. One can quickly check whether the person spending money is actually in possession of this money. So everyone that is interested in knowing who owns how much money or needs to verify transactions needs to have their own UTXO pool. This pool is stored in a traditional database. So every miner and most wallet providers would have their own UTXO pool. If you would want to create your own UTXO pool you would have to go through all blocks from the beginning. For each block you would need to look at all transactions. For each transaction you would have to remove each input from the UTXO pool and add each output to the UTXO pool.
You should now have a good understanding of what a Blockchain is and how it works. We haven’t yet covered the cryptographic principles that make the blockchain secure, but for most everyday purposes you should have a solid foundation.