How Does Blockchain Work?

09.12.2018

A central part in every blockchain is the mining algorithm, 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 in machine code.

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 bitcoin.

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 number" 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!

Loading