How Does Blockchain Work?
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 lead to the creation of Bitcoin, as the first decentralized electronic cash system (or simply the first cryptocurrency).
Although the 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 the one of 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, 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. Let's take Bitcoin's algorithm as an example. It's called SHA-256 short for "Secure hash algorithm 256 bits" It takes an input which can be anything, text, numbers, or even a computer file of any length. The output produced is called a "hash" and will be the same length every time, 256bits (or 64 characters).
The same input will give the same output every time, it's not random. But if you make a small change to the input the output will change completely.
It's also what's called a one-way function meaning that if you only have the output you can't calculate what the input was. You can only guess what the input was, and the odds of guessing it right is 1 in 2^256 which is pretty much impossible, in other words, it's secure.
Now that we know what the algorithm does, let's demonstrate how a blockchain works with a simple example of a transaction.
Here 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 broadcast, Alice gives the miners Bobs public address, the amount of bitcoin that 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 the bitcoin and that she wants to make the transaction.
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 on 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 needs to include the output hash from the previous block so that all blocks are tied together, hence the name block-CHAIN. 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 the have to redo all of the work in order to make anyone accept he's blockchain as the right one. So if a miner wanted to cheat he would need more than 50% of the networks 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 does not require as much computing power and is meant to require less electricity while being able to scale to more users.
And that's the basics of how a blockchain works!