Bitcoin is the world’s first peer-to-peer electronic cash system. Bitcoin offers anonymity, transparency, and no third-party dependent system to exchange value such as Money.
Satoshi Nakamoto invented Bitcoin in 2009, and it’s been hailed as one of the most important inventions of the century.
Bitcoin was invented to solve trust-based dependency in the current commerce system. We as people trust banks, governments, and federal reserves to safeguard money and provide a platform for the common man to trade and exchange using paper currency.
Due to these trust-based models, the control of the economy is in the hands of a few people, who can use it for malicious purposes. I don’t want to go into detail about it because our article is not meant for bashing the banks and governments ;)
However, there was no alternative, and you had to go to the bank to handle your financial needs. Then, Bitcoin arrived as a new and better alternative.
To trade in Bitcoin, you don’t need any bank or government approval. You can send money to your peers using your computer or mobile. It’s as simple as that.
In this article, we will learn in-depth about how Bitcoin works, how each transaction is formed and added to the block, and how it has done more than just solving our money problems.
Note: I highly recommend our readers to print out the Bitcoin
whitepaper written by Satoshi and give it a thorough read. You can download the paper from https://bitcoin.org/bitcoin.pdf
What Is Bitcoin and How Does It Work?
Bitcoin is a peer-to-peer electronic payment system created in 2009 by Satoshi Nakamoto. Bitcoin allows us to send money to anyone in the world, without the need for a central authority such as banks to issue accounts or process payments.
Bitcoin was created as a solution to the current financial system, where a marginal number of large corporations and banks control the financial world and the processing of transactions across the world.
It centralizes the control of finance and commerce and forces people to trust the banks to act responsibly. And like Satoshi said in the P2P foundation.
"The central bank must be trusted not to debase the currency, but the history of fiat currencies is full of breaches of that trust"
Due to this trust issue, a major financial crisis affected the world at the end of 2007. This crisis inspired Satoshi to create Bitcoin.
Bitcoin is a computer program. You can download it precompiled, or you can download the source code from Github and compile it
yourself. Once you run the Bitcoin program on your computer, you become part of the network; you are a peer!
Once your computer becomes a part of the network, the program
will connect to other computers running the same program. Once
connected, computers will start sharing a file and try to sync the files with each other; this file is nothing but a Blockchain. Once your computer is up to date with the Blockchain files, you can start submitting the transaction.
It may raise a question: does everyone need to run the Bitcoin program to send/receive Bitcoin? Well, no. You can download a Bitcoin wallet and start making Bitcoin transactions. Those who wish to become a peer or miners in the network need to run the Bitcoin program
Once your computer is synced with the latest copy of the Blockchain, you are good to go. You can now submit transactions, mine the blocks, read the Blockchain, and do whatever you wish to do.
When a new transaction is submitted in the Blockchain, it is broadcast across the network. Every node then makes a copy of that transaction and adds it to their memory pool.
In the Bitcoin network, at around ~10 minutes of the interval, one of the miners will mine a new block and broadcast it across the network. Every node will then update its Blockchain, and the network will be up to date:
This peer-to-peer nature of the system creates a trust-free, electronic cash system that runs with a solid consensus mechanism.
Before we proceed ahead with the technical aspects of Bitcoin,
let’s understand one of the key issues that Bitcoin solved—double-spending.
What is a Double Spending Issue?
Bitcoin is a peer-to-peer system, so what if two nodes submit the same transaction at the same time? How will the network with no central authority handle such a scenario? It is a serious and groundbreaking issue; let’s learn how Bitcoin solved it.
Bitcoin solved this problem by introducing a memory pool. The
memory pool is a buffer that every node has to maintain. The memory pool contains the transactions that are later written to the Blockchain.
When a node submits a new block, it is broadcast throughout the
network:
When a new block is submitted to the network, every node has to
accept that as a "correct" transaction and remove any conflicting transaction from their memory pool.
As a result, no chance of double spending can occur because it won’t persist in the network if it’s already been added to the block.
This double-spending issue is groundbreaking and opens the way
for many other projects such as Ripple cryptocurrency, which works with the current finance system but offers a solution to the double-spending.
Now, let’s look over the technical aspects of Bitcoin.
Blocks
Block is the data structure that holds transactions in an immutable form. Bitcoin stores users’ transactions in the blocks, and later they form a chain to build a Blockchain network.
As we discussed in the previous sections, the block is generated by the miner by the process of mining. The mining process requires a consensus algorithm to reach an agreement in the network.
Block consists of the block header, which is used by the miner to generate the hash. Block header consists of the following:
- Version: The version of the block
- Previous block hash: Hash of the last identified block, thus forming a chain
- Merkle root: Hash of all of the transactions in the block
- Time: Timestamp
- Bits: Hexadecimal version of the target value
- Nonce: Is a 32-bit random whole number that is adjusted by the miners, so that it becomes a valid number to be used to provide the hash of the block that should be less than the target hash.
The first two fields are self-explanatory, and we have already discussed them in the previous sections. Let’s learn about the Merkle tree.
Merkle Tree
Merkle tree is the data structure in which every leaf node is labeled with the cryptographic hash of the data, and every non-leaf node is labeled with the hash of its child nodes, forming a tree to the root with one hash value.
As you can see in the following image, hash 0-0 and hash 0-1 is the hash value of data block L1 and L2. Later, Hash 0-0 and 0-1 will be hashed together to form Hash 0 and so on until we reach a root hash:
Using a Merkle tree, we can generate a hash of a long series of data in a compact and secure format:
As shown in the preceding screenshot, the Merkle root contains transactions that are hashed together in a way the binary tree is formed.
Merkle tree is formed by hashing the transaction IDs in a pair. Transaction IDs are used if needed, and we can perform a recheck using the transaction ID in a tree.
To generate a Merkle tree, we take a pair of transaction IDs (TXID) and hash them twice using the SHA256 algorithm. The resulting hash is then hashed with another hashed pair twice to generate a single hash. This goes on till we generate a root hash.
Merkle trees are best suited for applications such as Bitcoin, where the size of the data matters. Due to the use of the Merkle tree, the block size is retained to 1Mb max in Bitcoin.
Learn in-depth about the Merkle trees; it’s one of the most frequently asked questions on Facebook, Amazon, Apple, Netflix, Google (FAANG) companies. Start from here.
Let’s learn about the next field in the block header—Bits.
Bits
Bits is the field in the block header that stores the target value in the compact. Target is the value that is required for the miner to generate the block hash that is less than the target. We studied it in the Blockchain core concepts section.
Let’s look at an example.
Here’s what a target value looks like:
0x00000000000000000696f4000000000000000000000000000000000000000000
Storing such long values will require size, and mind you, the target will keep changing in the future.
To solve this, the target is compacted, and it looks like this in the block header field:
0x180696f4
It’s easy to store and takes less size.
Here’s how to decode a bit. It’s divided into two parts. The first two bits after 0x represent the number of 0s in the target value, and the remaining values are appended after 0. The rest of the field is then filled with 0s till it reaches 48 bit.
So, 0x180696f4 value can be written as:
0x----18 times zero---0696f4---rest zeros----
This way, we can decode the bits in the actual target value.
In the next section, we’ll look at the field, the miner used to generate the hash of the block that is lower than the target value.
Nonce
The Nonce is the field in the block header that is used by the miner to generate a hash. Bitcoin uses proof of work consensus algorithms. In proof of work, Miner has to generate a hash that is lower than the target value decided by the network.
Target value starts with several 0s, and it’s really difficult to get such a hash using the SHA256 algorithm on a random basis. So, Nonce comes into the picture, Miner uses the Nonce field to generate a hash, and if it is not lower than the target value, Miner increments the Nonce and tries again.
When the lucky number or Nonce is found, a hash is generated. Miner announces the block in the network, and every other node verifies and makes a copy of it in their Blockchain.
So, Nonce plays a crucial role in Bitcoin mining.
We have studied the block and important block header fields, so let’s move ahead and learn about Bitcoin transactions.
Transactions
When someone tries to transfer Bitcoin from one account to another, a new transaction is formed and added to the block by the miner.
The transaction contains data that is required for proper Bitcoin transfer. Transaction data consists of the amount that the user wants to send, account information such as from and to addresses, transaction information required for the network such as transaction Ids, and so on.
Transaction in Bitcoin contains the following information:
- The transaction ID is also referred widely as TXID
- Transaction data such as INPUT, OUTPUT, and so on
- Fees
- Weight
- UTXO
Let’s learn about each one of them in brief.
TXID
TXID is a 32 bytes hexadecimal number that is referred to as transaction ID and used to identify the transaction in Bitcoin.
TXID is generated by hashing the transaction data twice using the SHA256 hashing algorithm.
Satoshi Nakamoto sent the first-ever Bitcoin transaction to Hal Finney, and the ID of the transaction is this:
f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16
You can still view this transaction in the Blockchain explorer:
Let’s look over the transaction data required to form the transaction payload.
Transaction Data
Transaction data contains the information required to form INPUT and OUTPUT. Now, what are they? Well, in Bitcoin, when you send someone a few coins, you unlock a few outputs that are required and lock them again with the keys of the receiver so that only they can lock it.
Sounds confusing? Stay with me.
Let’s take an example:
Imagine you have 5 Bitcoins, and you want to send 3 bitcoins to your friend. Here, 5 Bitcoins you hold is your OUTPUT, and it is split into two OUTPUT. One worth 3 Bitcoins and another worth 2 Bitcoins. The first OUTPUT will then be locked with the receiver keys so that they can unlock it. The second OUTPUT is your money, so it is again locked with your credentials for future uses.
In a nutshell, when a new transaction is formed, the whole amount is included in the transaction payload. It’s just whatever is left is being sent back to you as an OUTPUT.
So, this approach of graph-based INPUT and OUTPUT structure provides a history of ownership of Bitcoin.
Fees
Every transaction in Bitcoin pays some amount of money in the form of Bitcoin to the Miner. The remainder of the transaction output is also associated with the fees and paid to the miner for the work they do.
Generally, by adding lucrative fees, there is a high chance that a miner will pick your transaction from the memory pool to add into the next block. However, this is completely optional. If you don’t pay the fee at the moment, it may take some time, but your transaction will be mined by some miners scattered across the world.
Here is the fee associated with the transaction. You can also check the miner award, which is 6.25 Bitcoin at the time of writing this article:
The remainder of the transaction is also paid to the Miner as a fee. Suppose you want to pay someone 5 Bitcoin, and you have around 10 Bitcoin in your wallet, if you don’t claim the remaining 5 Bitcoin in the OUTPUT section of the transaction, it will be paid to the miner. It’s not refundable because, well, Blockchain is immutable! So, double-check before forming a transaction payload programmatically.
Weight
As the name suggests, it refers to the size of the transaction, that is, its weight. After the introduction of weight, the transaction payload is formed by putting the validation part at the end of the payload rather than with each INPUT, as we did earlier.
UTXO
It’s a short version of Unspent Transaction Output. When a new transaction is formed and new OUTPUT is generated, the one which is spent or sent to other users cannot be spent again. However, the OUTPUTs left or not spent can be used in future transactions.
These unspent outputs are called UTXOs.
UTXO is used to calculate the balance of a particular user and verify whether they have the amount to spend. UTXOs are a key part of forming new transactions and preventing double-spending.
These are the key factors that are involved in the Bitcoin transaction.
I hope you understood the concepts and the reasoning behind the technical changes done to make Bitcoin and Blockchain what they are now. Let's learn and understand another important and key technical aspect of Bitcoin - Cryptography.
Addresses and Keys
In order to send and receive Bitcoins, you need an address and to be specific a secure address. Bitcoin uses pair of public and private keys. The public and private keys play a crucial role in making a Bitcoin transaction secure and anonymous. The private key is generated by using a long random number and then converted to hexadecimal format. The public key is derived by using the private key.
The compressed version of a public key is also known as an address and you can share this with anyone publicly. Anyone around the globe can send you Bitcoin to your address. In order to send Bitcoin to other people, you need their public key and your private key. Your private key is a secret key and you must never share it with anyone. Each Bitcoin transaction is signed with the sender's private key along with the receiver's public key.
For example, take a look at the private and public key pair shown below:
Private key: ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db2
Public key: 02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737
Address: 1EUXSxuUVy2PC5enGXR1a3yxbEjNWMHuem
There is no way to generate the same private key again and there is no way to re-generate the private key from the public key. This makes the private key a crucial part of the system. If you lose your private key, you lose your Bitcoins.
This article is referred from the Book called Building decentralized blockchain applications.Consider a purchase if you like our content. (PS: I am author of this book)
Top comments (1)
Thanks for sharing. This is a great article for beginners. Will definitely share to my network.