Blockchain – How it works?
Blockchain can be explained as a chain of digital blocks linked sequentially by a cryptographic hash. Each block contains records of immutable transactions. Each transaction is digitally signed by a private key of the sender and addressed to the receiver’s public key. An identical copy of whole blockchain is shared by all the participating nodes.
Blockchain can be defined as a distributed, immutable, public/private digital ledger consisting of blocks that are sequentially linked by cryptographic hashes having each block containing a set of digitally signed transactions.
Public blockchain: Any one can participate as a node or can transact in the blockchain. They are also called permission-less blockchains.
Private blockchain: A membership is required to participate. They are also called permissioned blockchains.
Hash: A cryptographic hash function, such as SHA-256 used in bitcoin (BTC) blockchain, takes any size of input data and outputs a string of 256 bits or 64 hex characters. The length of SHA-256 hash value is always 64 characters, whether the input was a single word or a whole book. Even a single character change in input causes a significant change in the hash. Hashing is one way - when a hash is computed with one input, that same input cannot be derived back from that hash. This one-way nature of hashing makes the message/input secure from tampering.
Public/private key pair: In asymmetric cryptography, a pair of private/public keys is used to encrypt and decrypt messages. Blockchain uses the asymmetric cryptography for securing the transaction. Private key is securely stored in the crypto wallet and public key is shared with transacting parties. Private key is used to digitally sign a transaction before sending it. Public key is used as the owner’s wallet address for receiving transactions. Digitally signing the transaction makes it verifiable.
Double Spending: This is a problem of party spending the same digital currency twice, similar to the problem of counterfeiting currency notes. For example, party A has a balance of 70 BTC and sends 50 to B and 30 to C. In blockchain, one of these transactions would be invalidated by the mining nodes and would not be included in the block. Even if a faulty/fraudulent node includes it, other participating nodes would invalidate the block and would not accept it.
Transaction: Transaction includes the sender and receiver addresses and the amount in BTC to transfer. This data is used as the input to compute the transaction hash. Below is a summary of a transaction.
Screenshot Source: https://www.blockchain.com/btc/tx/8efc8d2bbcc0cb35ea63cc8b8c0bac7229b885418221af6aaa37e2645638ce3a
Merkle Root: Merkle root is a binary tree where each non-leaf node is a hash of its children. All the transaction hashes are kept at the bottom with same depth and a cumulative hash is computed up to the top. This top node hash is the Merkle root. This method ensures that any minor change in the leaf(transaction) results a change in the Merkle root. Since Merkle root is one of the inputs to the hash function, any change in the Merkle root changes the block hash.
Creating a new Block: The first block in a blockchain is called genesis block which has a height as zero. Height of a block simply denotes its sequential position from the genesis block. Each block is linked to the block before and after using block hash which is computed by SHA-256 algorithm.
When a transaction is digitally signed and sent over the network, it is added to the transaction pool. Miners verify transactions in the pool to include them in a new block. The verification process involves validation of the digital signature and confirmation against double spending. Only verified transactions are included in the block. Once the transaction is included and is confirmed, the receiver can spend the amount. Each block consists of a block header and a set of transactions. Block header is meta data of the block. Block hash is derived by SHA-256 hash function passing it inputs such as Block version, Previous block hash, Merkle root, Timestamp, Bits, Nonce. Below is the simplified linear representation of the nonce finding problem.
Targeted Hash = SHA-256 Function (Block version, Previous block hash, Merkle Root, Timestamp, Bits, Nonce)
Given other inputs, miners needs to find a nonce?(a number) such that the output hash value does not exceed the target. In simple words or rudimentary way, this indicates that the computed hash should have certain number of leading zeros so that its value remains below the target value.?The mining nodes use lot of processing power to run millions of computations to find a nonce that can output the hash below the given target. This is referred as Proof of Work (PoW) consensus mechanism. PoW makes it difficult for the malicious nodes or attackers to game the system and take the control of the network.
Bitcoin blockchain is set up to keep the duration between two blocks at 10 minutes. To achieve this goal, the system adjusts the target periodically with a varying level of difficulty. As difficulty level goes up, the target bar slides down and the odds of finding the nonce below that target reduces. Due to this, more work and time is required to solve the nonce problem. The difficulty is adjusted to lower or higher level by the network so that the duration between block creation averages to 10 minutes. This adjustment occurs exactly after 2016 blocks, roughly every two weeks.
Once a miner successfully solves the nonce problem, they create the block and broadcast it to the network. All the nodes then verify the transactions and accept the newly created valid block by consensus using the gossip protocol. At all times, nodes tend to maintain the longest chain. When a node receives information/gossip that a longer chain exists, it updates its copy with the history of the longest chain. This keeps the whole network in sync.?
Mining: Miner node creates and broadcasts the block over the network to add to the chain. Miners get the block reward which is 6.25 BTC as of now. When the Bitcoin was launched, the reward was 50 BTC for each block created. Satoshi Nakamoto (the creator of Bitcoin) has set a cap of 21 million BTC supply through mining reward over the time. To limit the supply over time (for deflationary reasons), the block mining reward amount halves after every 210,000 blocks or roughly around 4 years. It is estimated that it would take up to year 2140 (not a typo) until the final coin is mined. So far, roughly 18.85 million coins are mined which is close to 90% of the cap of 21 million. Miners also get the transaction fees for each transaction included in the block. Thus, miners get block reward and fees for working for the network. Miners can spend this amount after the block they mined becomes 100 blocks deep in the chain. This incentivizes the miner to keep adding the block to the longest chain and maintain the network.
Longest Chain: All the nodes always adopt to the longest chain and thereby all nodes share the same view of the chain history. Most of the times it is the one with highest number of blocks. There can be scenarios when the chain with highest number of blocks is not the longest chain, this is because the longest chain is actually determined on the basis of chainwork. Longest chain is the one which has the most work or the biggest chainwork value.
When more than one miner solves the nonce problem simultaneously and mine a block, the network is in disagreement about which block should be accepted. This gets resolved when the next block is linked to one of these blocks and becomes the longest chain, and adopted by the network. What happens to the blocks that are not in the longest chain? They are considered as orphaned blocks and the transactions re-added to the pool so that they can be included in a new block.
Benefits of Blockchain:
Enterprise Architect @ Zensar Technologies | Driving Innovation| |Front End Engineering Practice Head | AI-First Practitioner | IIM Ahmedabad - RPG Leadership Development Programme II
3 年Nicely explained... agreed informative...
--
3 年Very informative.