How does the current banking system work ?
We have accounts in the banks where we deposit our salaries. We have credit/debit cards and a banking app to spend the money in our bank accounts.
The banks maintain a ledger where they record account balances of all the individuals. They update the ledger with new account balances when two individuals conduct a transaction.
We trust the banks that:
- Bank will honestly maintain and update the account balance whenever a transaction occurs.
- Bank will allow us to transact with each other.
This does not seem like a big deal till the time the system functions smoothly.
However, imagine waking up one day and realizing that your bank account has been frozen because you participated in a protest that government did not like. This is what happened with thousands of truckers protesting in Canada in 2022.
Bitcoin fixes this !
At its core, Bitcoin is nothing but a distributed ledger system.
It replaces the centralized ledger being maintained by the banks with the distributed ledger being maintained by thousands of nodes spread across the earth.
This takes away power from any one individual to tamper with the money ledger.
What is Bitcoin at its core ?
Bitcoin ledger is nothing more than a record of addresses and their balances. Noone said that these balances mean something.
The reason these balances have value is because someone is willing to trade their services and goods in exchange for a higher number next to their account addresses.
How does Bitcoin achieves this ?
Distributing the ledger solves the problem of any one powerful party controlling the money ledger but creates several new problems:
- How does everyone maintain the same copy of Bitcoin ledger?
- What stops someone from spending more Bitcoin than they have?
- What stops someone from spending someone else Bitcoin?
- What stops someone from spending the same Bitcoin twice?
- Who gets to update the Bitcoin ledger?
Bitcoin was the first cryptocurrency to solve these problems through some clever engineering.
Digital signatures ensure that:
- People are only able to spend the balance they have.
- People do not spend the same Bitcoin twice(double spend).
Every new user in the Bitcoin network generates a unique secret and public key. Every user has a secret and public key that is unique to him and does not match with any other user on the network.
These keys are the identity of a user on the Bitcoin network.
Sender uses his private key to sign a transaction just like we make transactions using cheque.
However unlike physical signatures, digital signatures depend on the contents of the message. Every message will have a different signature.
Signature = Sign( Message, Secret Key)
This ensures that no one can just copy the signature from one transaction to another.
Each transaction is unique as it has to reference a previous transaction on the Bitcoin ledger in which the sender’s address received the Bitcoin that he is going to spend. This makes every transaction unique.
Miners maintain a separate database of all the addresses with unspent transactions called UTXO so that they can quickly check if the sender has sufficient balance to send funds.
Hence receiver can not just add the same transaction in Bitcoin ledger to receive Bitcoin more than once.
Sender broadcasts the transaction after signing the transaction with his secret key. Sender also includes a tip for anyone(miner) who helps in adding the transaction to the Bitcoin ledger.
Bitcoin miners verify that the sender has sufficient balance to spend and validity of the signatures using the public key.
Verify(Transaction, Signature, Public Key) = True/False
Transactions wait in Mem Pool till the time they are included in the Bitcoin ledger.
Miners sort the transactions in the Mem Pool on basis of tips and include them in the next blocks.
Bitcoin stores the transactions in the blockchain. Blockchain is a data structure made of blocks that are added every 10 mins(approx).
Every block is linked to the previous block(How will be explained subsequently).
This avoids anyone from tampering with the history once a transaction has been added to the Bitcoin blockchain.
Miners are computers that pick up transactions from Mem Pool and add them to blocks prioritizing the transactions with most tips because they are greedy.
They broadcast the block to the Bitcoin network once the block is mined.
They receive the mined blocks from Miners check their validity and add them to the Bitcoin blockchain.
Anyone is free to join the network as a Bitcoin node and store the state of the Bitcoin Blockchain.
Proof of work consenses mechanism - Hash function
- How can thousands of participants in the Bitcoin network agree on a common history when every miner is broadcasting its own block?
- Which block should the node add when it is being bombarded with blocks from many miners?
In short, how do thousands of nodes, miners and users come to a common consensus regarding the state of Bitcoin Blockchain?
Bitcoin uses Hash function as a race among miners to decide which miner’s block will be added to the blockchain.
The fastest miner is able to solve the Hash function puzzle the fastest and
- Gets to add its block to the Bitcoin blockchain
- Earns miner tip and block reward for being first to mine the next block.
Hash function is a mathematical function whose input can be any string of arbitrary length, however the output is always a random-looking string of number with fix length called hash.
Every input has a different hash unique to the particular input. The hash of the input changes even if we change a single byte of the input. Hence hash is like fingerprint, every input has a different unique hash.
Hash function is a one-way function. It is not possible to calculate the input given an output except by trying random inputs till it gives the desired output.
Proof of work - Minning
Every Block in the Bitcoin blockchain is 10 MB. Miners pick the transactions from the Mempool and fill the block.
Then they try to find a number(called nonce) such that the hash of the block and nonce has certain number of leading digits(decided by difficulty adjustment) as 0s.
The only way to find nonce is to randomly keep trying different numbers till the time you find a number that satisfies the output criteria.
This hard work is also called Bitcoin minning and the computers trying to find the nonce are called miners.
However once nonce is found it is very easy for other nodes to verify that it is the correct solution ie large amount of work was done to find the nonce.
Just as a transaction is considered valid if it has a valid signature, a block is considered valid if it has proof of work.
Miner broadcasts the block once it finds the nonce. Nodes verify that the nonce is correct and add the block to the blockchain.
The block of the first miner who is able to find the nonce gets added to the blockchain.
Why do miners do hardwork to find nonce - Miner reward
In addition to the tips paid out to miners, the miners also receive Bitcoins in form of block reward.
Miner reward = Tips + Block reward
Miner reward is the reward for the miner for being first to successfully mine the block. This is how all the Bitcoin ever created came into existence.
In 2009, the block reward for minning a new block was 50 Bitcoin. The block reward gets cut in half every 4 years.
There will ever be only 21 million Bitcoin and the last Bitcoin will be mined somewhere in the year 2140.
As the number of miners guessing the nonce increase the blocks are found faster and if miners leave the network the number of computers guessing the number reduces resulting in blocks being found in a longer time frame.
The leading number of 0s is changed periodically so that the average block is formed in 10 mins.
The zeros are increased(difficulty is increased) in case the blocks are getting formed earlier than 10 mins.
And the leading 0s are reduced if it is taking longer than 10 mins to find a new block.
The difficulty is readjusted by taking the average of blocks formed every 2 weeks.
How are blocks interlinked - Why is it called a Blockchain ?
In addition to the transactions and nonce, the block also contains hash of the previous block.
That is how each block is linked to the previous block.
This prevents anyone from changing state of any previous block. Because tampering with any block will change its hash which in turn affects the next block, which will, in turn, affect the next block, and so on.
Because all the blocks are interlinked and cannot be changed once added, the data structure is called Blockchain.
How is conflict resolved ?
Bitcoin network participants choose the longest Blockchain ie the blockchain with the most amount of work in case it has to decide between two different blockchains.
This is because longer blockchain signifies that more miners are working on that particular Blockchain and you would want to go with the most popular blockchain being used by the majority of participants.
If there is a tie wait for some time and check again which chain has grown longer that is the most accepted blockchain.
When is a transaction considered final ?
It is a good practice to wait for 2 to 3 blocks after your transaction has been added to the blockchain.
Because after 2 to 3 blocks one can be pretty sure that his transaction has been added to the blockchain on which all the network participants are building.
It becomes harder and harder to change the transaction as more blocks with proof of work are added on top of the block containing the transaction.