How Bitcoin Blockchain really secures transactions

Fig. 1 — Bitcoin

Cryptocurrency — De-centralised — No intermediaries — P2P network are some of the buzzwords which you might have heard when it comes to the amazing Blockchain technology! But, how is it useful to us and how does it even play a crucial role in securing our day-to-day transactions?

This article explains the underlying logic and mechanisms of Blockchain in general and specifically, how the Bitcoin Blockchain operates. Here’s a nice article which I recommend if you’d like to know more about the history and evolution of Blockchain.

Conventional Scenario

Assume there are five persons, say — A, B, C, D & E, exchanging transactions among one another and one person noting down the transactions along with the corresponding balances in chronological order. Let that person be C.

Fig. 2— Conventional Transactions

Sense the problem?

What if person C, noting down the transactions, tries to manipulate them to his own advantage by altering the order or the person? Traditional banks were similar to C in noting down the transactions in chronological order. Due to an alarming increase in the volume of transactions, such a manual approach was error-prone. A solution was required to scale these humongous transactions without relying on a central entity. And, the solution to this problem was, of course, found to be Blockchain.

The Basics of Bitcoin

Ledger

Let’s now assume Fig. 2 to be distributed with many people on the network, like Google Sheets, so that everyone would be able to transparently see what’s happening with the transactions and it’ll always be updated. This is now termed as a Ledger in Blockchain. Typically, this is how a ledger looks like.

Fig. 3 — Blockchain Ledger

Block

A block is a data structure containing the necessary metadata about the block itself which in-turn comprises of header and the body (transactions). The first block is called the Genesis block.

Fig. 4— Bitcoin Blocks

Don’t worry about the rest of the terms! We’ll be coming to them soon. Just notice how the transactions are linked to the block header and how each block is linked to the preceding block in the form of a linked list. In the end, Blockchain, in a nutshell, is a chain of blocks linked securely.

Mining

Mining is the process of solving a complex mathematical puzzle so as to yield a new block. The mathematical puzzle is so complex (more on that soon) that it takes tons of processor energy out of your system to solve it and the caveat here is that it should be solved within a short amount of time. Imagine the hardware requirements of such a system and this is the reason why mining a block is not for everyone! In fact, there are companies that manufacture mining ASICs that are specifically for cryptocurrency mining.

A quick analogy to this mathematical puzzle — Consider you have a 256-bit number. Can you guess the probability of finding a number that starts with specific number of zeros, say 10? If you got the answer, great! You’d understand how the answer is just mind-boggling! Read on to find the answer.

The person who mines a block (in other words — forms a new block) of transactions is the miner. So, in a blockchain network, there are miners who mine the blocks and participants who initiate and receive the transactions. There are also a few other groups out there but let’s not concern about them since their role is minor.

Consensus

Now that you have formed a new block by solving such a complex mathematical puzzle, how should everyone trust that this is a valid block and not something manipulated? After all, you’re not the only person involved in this. Hence, a consensus with other miners in the blockchain network is required. There are various processes to achieve this and a common one in Bitcoin Blockchain is Proof-of-Work (PoW).

Bitcoin Transactions in Action!

Fig. 5— Bitcoin Blockchain simplified

Let’s now deep-dive into how the Bitcoin Blockchain works. Assume, Bill transfers 10$ to Jenny. How do we ensure that it’s indeed Bill who transferred the money? This is where the first aspect of security comes into play — Digital Signature. Bill signs (hashes — produces a message digest) his message (transaction) with the private key which is unique and known only to him. The public key is known to everyone and, of course, to the recipient as well.

Sign (Message Digest, Key) = Digital Signature

Verify (Message, Signature, Public Key) = True/False

If the second equation yields true, we get back the original message untampered and if it’s false, we can be sure that there was some foul play.

Bitcoin Blockchain uses SHA256 algorithm to produce a digital signature and it’s extremely difficult (almost impossible!) to retrieve the private key from the signature. Such functions are called trap functions. To get back the private key, you need to go through 2²⁵⁶ combinations since the signature is a 256-bit number.

Once the sender has signed his transaction, the transaction is broadcasted by the wallet (an application to manage your bitcoins and keys — similar to a third party wallet) to the Blockchain network. Each transaction is associated with a unique ID (Txhash) so that the same transactions can’t be copied again into a ledger. After all, since everything is digital, isn’t there a possibility to forge?

It then hovers in mempool, a pool of unconfirmed transactions, until it’s picked up by a miner. Multiple miners can collect multiple such transactions from the pool for mining. Yes, it does overlap but the algorithm is smart enough to overcome this issue.

After collecting the transactions from the pool and before beginning to mine, how does the miner determine if the sender has sufficient funds to pay? The miner analyses the wallet history of the sender. Remember, the ledger is an updated list of all the transactions in the Blockchain and a copy of this resides with all the nodes in the network. If the sender has sufficient funds, the miner includes the transaction, provided there’s a good transaction fee associated, with the block. Else, it’s rejected.

But, what’s in a Block?

Fig. 6— A Block in Blockchain

Irrespective of Bitcoin/Ethereum/Hyperledger Blockchain, a block mainly consists of the following fields.

Block Header

  1. Transaction hash — A digital hash, also called Merkle root, which is a summary of all the transactions present in the block’s body.
  2. Difficulty — How difficult the mathematical puzzle would be to solve.

Example: To find a hash that starts with 7 zeros. This is not a constant value and keeps varying every two weeks.

  1. Previous block hash — Remember, Blockchain in the end is a linked list!
  2. Timestamp — The date & time when the block was mined.

There are certain other fields that differ by the type of Blockchain in use but the above are the ones which you need to mainly know.

The process of mining the blocks is adaptive in nature! The difficulty is adjusted every two weeks so that the miners do not construct a block < 10 minutes or > 10 minutes. It’s always ~10 minutes. Now, the reason why it’s 10 minutes is because, any new block added to the Blockchain takes atleast 10 minutes to globally propagate and sync with the local copies present with the nodes.

Nonce — A random number, specifically in Bitcoin Blockchain, used as an aid to solve the complex mathematical puzzle.

Block Body

A list of transactions arranged in the form of Merkle tree. A Merkle tree is a tree data structure that is used in optimally searching for a transaction. It’s a great topic in itself and I’d consider it writing in a separate post. Here’s a visualization of the same. The top hash is what is present as metadata in the block header.

Fig. 7— Merkle Tree

The Mathematical Puzzle

Now, this is the core of the whole Bitcoin Blockchain! Because this is what yields a block in the end. Before we deep-dive into this, remember the fact that a block is formed only after the miner solves this puzzle. And the result of this puzzle is a block hash.

Puzzle — The block hash should begin with ‘x’ amount of zeros within 10 minutes of time.

You might be wondering how difficult or easy it is to solve this puzzle. Bitcoin Blockchain again uses SHA256 to generate a block hash. Remember, I had asked a question before about the probability of finding a number that starts with a specific number of zeros, say 10?

Ans — The probability is 1/2²⁵⁶.

Now imagine how difficult it is to find such a number within just 10 minutes of time. It’s definitely impossible by an ordinary computer having just GBs of processing capability. If the puzzle is successfully solved, it surely means that the miner has spent a significant amount of energy through the system. To appreciate the efforts, the miner is rewarded bitcoins — 12.5 BTC in Bitcoin Blockchain — which just originates out of thin air.

There can only be 21 million BTCs in total and the reason to this is an educated limit set by the founder of bitcoin, Satoshi Nakamoto. Currently, ~18 million BTCs are in existence. So, in the future when the limit is reached, the only way to probably earn BTCs is through the transaction fee.

After the block has been mined, it’s broadcasted out to other miners in the network to validate the solution. Other miners confirm if the generated hash really solves the mathematical puzzle. Only if all the miners agree, we say that consensus is reached. The signature is the Proof of Work (PoW) done.

Apart from the reward, the miner also collects transaction fee from the sender to include the transaction as soon as possible in the block. Higher the transaction fee, more is the probability of transaction being included in the block. This is the reason why it sometimes takes longer for bitcoin transactions to complete since it has a lower processing fee.

The first miner to mine the block is declared the winner and if another miner broadcasts his solution at the same time, a race condition occurs. This is resolved using the longest chain rule which I’ll discuss in a future post. The latter miner’s transactions are returned to the mempool to be processed again.

Once the consensus is reached, the block is added to the Blockchain. Bitcoins are deducted from the sender’s wallet and get added to the recipient’s wallet. The block is now permanent and all the nodes in the network update their copies. That’s the entire process in the end!

How difficult it is to alter a block

Now that you have a fair understanding of Blockchain, let’s analyze what it takes to tamper with a block, or in other words, alter the transaction.

Let’s say there are 500 blocks present in the Blockchain totally and the attacker tries to modify the 400th block. Remember that all the succeeding blocks in the Blockchain are linked using the predecessor‘s hash.

Fig. 8— Linked Blocks

If any transaction in the 400th block is altered, the Merkle root’s hash will change and consequently, the entire block hash will be different because, in the end, it’s the block header which is hashed along with the nonce. Hence, the succeeding block’s hash would also change because of the change in the preceding block’s hash.

This has a cascading effect till the 500th block!

Caveats

  1. A hacker who alters a single block now has to alter all the 100 blocks within 10 minutes of time! And we know that it’s possible to mine only a single block in 10 minutes of time.
  2. If the hacker takes more time or even if he finishes mining them in 10 minutes, his solution will still be invalidated by others because the proposed solution will be different than the already existing copies present with the nodes.

Hence, a foul-play is suspected and the miner will be removed from the network. This doesn’t mean that the probability to alter a block is completely ruled out. It can still be possible, although extremely difficult, using the 51% attack.

Can it be possible with a quantum computer? I’d love to hear your thoughts about that.

In the end, there will always be challenges present in every technology!

Useful Links

So, hopefully, you got a clear idea of how Bitcoin Blockchain works under the hood. It’s an emerging technology and the time has never been better to start learning about it.

Thanks for reading till the end of the post! I love sharing information in the hope that it would help people out there. I’d be writing more and your support would mean a lot! If you liked my writing, just hit the follow button on Medium. Connect with me on LinkedIn, Facebook or Twitter for any guidance or discussion on technology or help.

Thanks again for reading and have a wonderful day!

--

--

--

Quality Engineer @ Extreme Networks

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The LUX path, or how to conquer No Man’s Land.

What is Blockchain 4.0?

DAML is designed for distributed business workflows: why is this big news?

Welcome to Analog Swap — BSC

Aurora Monthly Update | March 1–31, 2020

1. Innovative Finance Introduction

ShuttleOne, a DeFI blockchain company, funds the first commercial electric vehicles in Singapore

Enlisting Bison Trails To Power Calaxy Validator Nodes on Flow

Abishaik Mohan

Abishaik Mohan

Quality Engineer @ Extreme Networks

More from Medium

Types of Blockchains inside the Bitcoin Network

Blockchain Technology: A Complete Guide to Blockchain Marketplaces.

header image | tedbree.com | Blockchain Technology: A Complete Guide to Blockchain Marketplaces.|

Celsius Just Reminded Everyone: Not Your Keys, Not Your Crypto