How Does Blockchain Work?

2023-11-21

What is blockchain?

In short, a blockchain is a list of data records that works as a decentralized digital ledger. The data is organized into blocks, which are chronologically arranged and secured by cryptography. 

The earliest model of a blockchain was created in the early 1990s when computer scientist Stuart Haber and physicist W. Scott Stornetta employed cryptographic techniques in a chain of blocks as a way to secure digital documents from data tampering. 

The work of Haber and Stornetta certainly inspired the work of many other computer scientists and cryptography enthusiasts - which eventually led to the creation of Bitcoin as the first decentralized electronic cash system (or simply the first cryptocurrency).

Although blockchain technology is older than cryptocurrencies, it was only after the creation of Bitcoin in 2008 that its potential started to be recognized. Since then, the interest in blockchain technology has been growing gradually, and cryptocurrencies are now being acknowledged on a larger scale.

Blockchain technology is mostly used to record cryptocurrency transactions, but it suits many other kinds of digital data and can be applied to a wide range of use cases. The oldest, safest, and largest blockchain network is Bitcoin, which was designed with a careful and balanced combination of cryptography and game theory.

How does blockchain work?

In the context of cryptocurrencies, a blockchain consists of a stable chain of blocks, each one storing a list of previously confirmed transactions.  Since the blockchain network is maintained by a myriad of computers spread around the world, it functions as a decentralized database (or ledger). This means that each participant (node) maintains a copy of the blockchain data, and they communicate with each other to ensure that they are all on the same page (or block).

Therefore, blockchain transactions occur within a peer-to-peer global network and this is what makes Bitcoin a decentralized digital currency that is borderless, and censorship-resistant. In addition, most blockchain systems are considered trustless because they do not require any kind of trust. There is no single authority in control of Bitcoin.

A central part of almost every blockchain is the process of mining, which relies on hashing algorithms. Bitcoin uses the SHA-256 algorithm (Secure hash algorithm 256 bits). It takes an input of any length and generates an output that will always have the same length. The output produced is called a "hash" and, in this case, is always made of 64 characters (256bits).

So the same input will result in the same output, no matter how many times the process is repeated. But if a small change is made to the input, the output will change completely. As such, hash functions are deterministic, and in the cryptocurrency world, most of them are designed as a one-way hash function.

Being a one-way function means that it is almost impossible to calculate what was the input from the output. One can only guess what the input was, but the odds of guessing it right are extremely low. This is one of the reasons why Bitcoin's blockchain is secure.

Now that we know what the algorithm does, let's demonstrate how a blockchain works with a simple example of a transaction.

Imagine that we have Alice and Bob along with their Bitcoin balance. Let's say Alice owes Bob 2 Bitcoins.

For Alice to send Bob that 2 bitcoin, Alice broadcasts a message with the transaction that she wants to make to all the miners in the network.

In that transaction, Alice gives the miners Bob's address and the amount of Bitcoins she would like to send, along with a digital signature and her public key. The signature is made with Alice's private key, and the miners can validate that Alice, in fact, is the owner of those coins.

Once the miners are sure that the transaction is valid, they can put it in a block along with many other transactions and attempt to mine the block. This is done by putting the block through the SHA-256 algorithm. The output needs to start with a certain amount of 0's in order to be considered valid. The amount of 0's needed depends on what's called the "difficulty," which changes depending on how much computing power there is on the network.

In order to produce an output hash with the desired amount of 0's in the beginning, the miners add what's called a "nonce" into the block before running it through the algorithm. Since a small change to the input completely changes the output, the miners try random nonces until they find a valid output hash.

Once the block is mined, the miner broadcasts that newly mined block to all the other miners. They then check to make sure that the block is valid so that they can add it to their copy of the blockchain and the transaction is complete. But in the block, the miners also need to include the output hash from the previous block so that all blocks are tied together, hence the name blockchain. This is an important part because of the way trust works in the system.

Every miner has their own copy of the blockchain on their computer, and everyone trusts whichever blockchain that has the most computational work put into it, the longest blockchain. If a miner changes a transaction in a previous block, the output hash for that block will change, which leads to all the hashes after it changing as well due to the blocks being liked with hashes. The miner would have to redo all of the work in order to make anyone accept his blockchain as the right one. So if a miner wanted to cheat, he would need more than 50% of the network's computing power, which is very unlikely. Network attacks like this are thereby called 51% attacks.

The model of making computers work in order to produce blocks is called Proof-of-Work (PoW) there are also other models like Proof-of-Stake (PoS) which do not require as much computing power and are meant to require less electricity while being able to scale to more users.