BUILDING THE CHAIN OF SECURITY: UNDERSTANDING THE PROCESS OF BITCOIN MINING AND ITS ROLE IN THE BLOCKCHAIN
Chapter 8
??UNCOVERING THE GEMS OF THE BLOCKCHAIN: MEET THE MINERS AND THE MINING PROCESS
Miners act as the bookkeepers of the Bitcoin network. In centralized systems, accountants are compensated by companies to maintain the accuracy and integrity of financial records.
Similarly, miners receive bitcoin for verifying and adding transactions to the blockchain, contributing to network security and stability. Mining entails using computational power and specialized hardware to add new blocks to the blockchain, ensuring its security, decentralization, and long-term viability.
The blockchain serves as a ledger of past transactions, consisting of a string of interconnected blocks. This chain validates transactions across the network, distinguishing genuine Bitcoin transactions from attempts to double-spend coins.
Miners aggregate unconfirmed transactions into blocks and then search for the elusive "valid block hash" that secures the block's position in the blockchain. This hash, among billions of others, can only be unlocked by a specific key set by the network.
Picture a giant haystack filled with millions of keys, each representing a unique block hash. The network designates a specific key to unlock a valuable prize. Miners sift through the haystack, testing each key in the lock, with only one miner ultimately discovering the correct match.
The first miner to find the correct block hash broadcasts it to the network along with the block of transactions. Other miners verify the solution, ensuring its accuracy before adding the block to the blockchain, thereby establishing a secure and public ledger.
Miners are rewarded for their efforts through block rewards and transaction fees. Block rewards consist of newly generated bitcoin released into circulation with each block addition. Transaction fees, on the other hand, are small amounts paid by users to expedite transaction processing and prioritize their inclusion in mined blocks. Miners have the autonomy to select which transactions to include, often favoring those with higher transaction fees.
??THE DYNAMIC REWARDS SYSTEM OF BITCOIN MINING: BLOCK REWARDS, TRANSACTION FEES, AND HALVINGS
Satoshi Nakamoto, the creator of Bitcoin, devised a clever solution for distributing new bitcoin through a decentralized block reward system.
The bitcoin supply schedule outlines the creation and release of new bitcoin into circulation, aiming to maintain the cryptocurrency's scarcity over time.
In Bitcoin's early days, miners received 50 bitcoin as a reward for each block they successfully mined. This block reward serves as a financial incentive for miners to invest in powerful hardware and electricity for their mining operations.
However, to regulate the supply of new bitcoin and ensure network stability, the block reward is halved approximately every 210,000 blocks. This process, known as "halving," decreases the amount of new bitcoin entering circulation, continuing to motivate miners to secure the network and uphold its decentralization.
To illustrate, imagine you have a jar that can hold 1000 pieces of candy. Each day, you can add 10 pieces of candy to the jar. This mirrors the creation of new bitcoin through mining. Yet, every four years, the number of candy pieces you can add is halved.
Similarly, Bitcoin's halving slows down the rate at which new bitcoin is created, with the ultimate goal of maintaining scarcity and limiting the total supply to 21 million units, akin to the limited capacity of the candy jar.
This halving process resembles how gold mines eventually deplete and become harder to locate. Presently, the Bitcoin protocol releases around 3.125 new BTC to miners roughly every 10 minutes when a block is mined.
The table displays details of upcoming halving events for Bitcoin, indicating the percentage of the total supply mined by the date, the expected date of the next halving event, and the block number for the event.
Circulating Supply refers to the amount of bitcoin currently in circulation and available for trading, excluding locked or lost coins. As more bitcoin is mined, this figure and the percentage of the total supply mined will increase until reaching the 21 million total supply.
The Inflation Rate measures the rate at which a cryptocurrency's circulating supply increases over time, expressed as a percentage of the total supply. It's calculated as the difference between the circulating supply and the total max supply (21 million), divided by the total max supply, then multiplied by 100.
During each halving event, the block reward for miners decreases, lowering the issuance rate of new bitcoin and reducing its inflation rate over time. This reduced supply, coupled with rising demand, can drive up the price of bitcoin, benefiting early adopters and incentivizing miners to continue securing the network. And remember Miners are still collecting
TX fees.
??THE VITAL TASK OF BITCOIN MINING: SECURING THE BLOCKCHAIN
What is a valid block hash in Blockchain?
A block hash serves as a unique identifier for each block in the blockchain, aiding in detecting attempts to alter past transactions. Blocks store transactions and form a chain, creating a transparent record of all transactions from the genesis block to the latest one. The block hash links each block to the previous one, facilitating viewing transaction history and ensuring data accuracy and security, much like how a fingerprint identifies an individual.
While a block hash can verify the integrity of block data, it doesn't reveal all information within the block, which requires cryptographic keys for access. The block hash merely verifies that the block's data remains unaltered.
The Race to Mine a Block:
Miners compete to discover the block hash aligning with the target set by the network. The successful miner adds the block to the blockchain, validating its authenticity. Mining resembles a race where miners aim to reach the finish line quickly, with the difficulty target adjusting to accommodate increasing miner participation.
Imagine the network's target is 1000. Miners search for a block hash lower than 1000, with the first miner to find such a hash adding a group of transactions to the blockchain and earning bitcoin rewards.
Difficulty level measures the challenge of finding a valid block hash meeting the network's target, adjusting periodically for consistent block addition rates. As difficulty rises, so does the complexity of finding a valid block hash.
For instance:
Hash 1: 0000A1mINgF0RbL0cK5wItHth3hAy5tAcK (Difficulty level = 1)
Hash 2: 00000000A1mINgF0RbL0cK5wItHth3hAy5tAcK (Difficulty level = 2)
In this example, Hash 2 has a higher difficulty level, requiring more leading zeros, indicating a more challenging target.
Finding a valid block hash demands substantial computational effort. By finding one, miners demonstrate the work needed to add a new block, earning bitcoin rewards. Proof of Work (PoW) validates transactions and adds blocks to the blockchain, safeguarding it against malicious control. The target aims for one block to be mined every 10 minutes, enhancing network security as the target becomes harder to attain.
For instance, the target set by the network might be:
00000000A1mINgF0RbL0cK5wItHth3hAy5tAcK
Miners strive to find the hash with 8 leading zeros to add the block to the blockchain and receive bitcoin compensation.
??The Roles of Miners:
Miners verify transactions and add new blocks by collecting unconfirmed transactions into their mempool, selecting some for inclusion in their candidate block, and searching for the block hash. Multiple miners may concurrently work on new blocks, with the first to discover a valid block hash announcing it to the network. Other miners verify the block's transactions, and if valid, the block is added to the blockchain, promoting consensus and preventing double-spending. A candidate block is a collection of transactions under consideration for blockchain inclusion but hasn't been added yet.
Dissecting The Block
A blockchain consists of blocks, akin to pages in a ledger, storing new transactions. Each block contains a header summarizing the data, a link to the previous block, a unique nonce (number used once), and other specifics. Miners are tasked with accurately completing header information when crafting candidate blocks.
Organization of Transactions:
Miners must organize transactions within their candidate blocks following a specific format, with only partial data included in the header. Transactions serve as the backbone of the Bitcoin Network, efficiently and securely organized using Merkle Trees. These trees condense vast amounts of data into a compact form, enhancing network security and efficiency.
The Merkle Root Hash, part of the header data, acts as a digital fingerprint for all block transactions. It enables efficient transaction verification without scrutinizing each one individually, crucial for network security and scalability. If any transaction data changes, the resulting code differs, facilitating the detection of malicious alterations and safeguarding transaction privacy and integrity.
A Coinbase Transaction:
A Coinbase transaction, included in every block, rewards successful miners and designates an address for receiving transaction fees. This transaction is also part of the Merkle Tree but differs from others as it generates new coins instead of having an input.
Understanding Block Headers:
A block header, akin to a book cover, summarizes a block's content and key details. It includes:
Block Hash: A unique code verifying block information consistency.
Version: Software version used by the block creator.
Previous Block Hash: Ensures block sequence integrity.
Merkle Root Hash: Updates specific Unspent Transaction Outputs (UTXOs).
Time: Timestamp of block creation.
Bits: Difficulty level of block creation.
Nonce: Unique number miners adjust to create a valid block hash.
The Nonce Quest:
Miners search for the nonce that produces a hash value meeting the network's target, ensuring block validity. By attempting various nonce values, miners eventually find one creating a valid block hash.
Nonce's Role:
The nonce is a unique number added to the block header during mining. Miners modify the nonce to create different block hashes, aiming for a value meeting the network's target. The nonce is critical for verifying blocks and safeguarding network security, preventing manipulation. Bitcoin uses a 32-bit (4-byte) field as a nonce in the proof-of-work mining process. Miners repeatedly increment the nonce until finding a valid hash.
In this example:
00000000A1mINgF0RbL0cK5wItHth3hAy5tAcK (Difficulty level = 2)
Miners adjust the nonce value until discovering a hash matching the network's target, enabling block addition to the blockchain. By modifying the nonce, miners create distinct block hashes, seeking a value aligning with the target and ensuring network security. This highlights the nonce's importance in maintaining network integrity.
??REHASHING THE HASHES
How Long Does it Take Miners to Discover a Valid Hash? And How Quickly Can They Modify the Nonce Values During Their Calculation Process?
In the example below, it took a miner 1,619,820,810 iterations to find a hash value with the required number of zeros. The miner who succeeded in finding the right nonce value added the block to the blockchain, forming a secure and unalterable part of the chain.
The Hash Rate
The hash rate is a measure of the computational power of the network and the speed at which miners can make nonce calculations to find the correct block hash. The more computational power a miner has, the faster they can make these calculations, giving them an advantage in the mining process. However, as the network’s hash rate increases, the difficulty of mining new Bitcoin also increases, making it more challenging for all miners to find the correct block hash.
领英推荐
Just like athletes upgrade their equipment to run faster, Bitcoin miners invest in specialized computer hardware to increase their hash rate and mine blocks more efficiently. The more resources they invest in, the better their chances of reaching the finish line first.
Hash Rate and Mining Difficulty
Hash rate can be compared to the speed of a runner. The more powerful the miner’s machine, the higher the hash rate, and the faster they can mine. However, just like in a race, having a high speed does not guarantee a win if the difficulty target has been adjusted to a higher level. Miners must constantly upgrade their equipment and improve their hash rate to stay ahead of the competition and have a chance of mining a block and winning the race.
Mining Process
The process of finding the correct hash value by changing the nonce is called mining. The block hash can be referred to as the proof of work. In summary, every 10 minutes or so, miners enter a race to find the valid block hash. They begin by taking all the data from their candidate blocks (which is summarized neatly in the block headers), double-hashing it into a single hash, and comparing the output to a target hash value set by the network. If the block hash produced is too high, the miner adjusts the nonce and tries again, repeating this process trillions of times per second until one lucky miner finally finds a hash that meets the network’s target.
No matter how little or how much mining hash power is applied, the average block is mined every 10 minutes.
Difficulty Adjustments
When the total hash rate decreases, the difficulty decreases to make it easier for miners to mine new Bitcoin. This helps to keep steady the rate at which new Bitcoin is mined. The difficulty adjustments are done by mining the previous 2016 blocks. If the average time to mine 2016 blocks is less than 14 days, the difficulty level is increased. If the average time to mine 2016 blocks is more than 14 days, the difficulty level is decreased.
Issuance Rate
The issuance rate refers to the rate at which new coins are being added to the circulating supply, often through mining. This rate can be impacted by various factors, including changes in network hash rate, the number of blocks mined, and halving events, which reduce the number of coins that can be mined per block.
??THE STEP-BY-STEP PROCESS OF MINING A BLOCK
Mining a block in the Bitcoin Network involves several steps:
New Transactions: New transactions are broadcasted to the network and then verified by nodes. Transactions are collected from unconfirmed transactions in the mempool. Transactions with higher fees are prioritized.
Merkle Tree: These transactions are then organized in a Merkle Tree and included in a candidate block, along with the previous block’s hash, a timestamp, and a nonce. Miners compete to solve a mathematical puzzle based on the information in the block, including the transactions and a random number.
Mathematical Puzzle: The puzzle involves finding a specific number (block hash) that, when combined with the block data, results in a value that is less than a target number. The miner who first finds the correct hash broadcasts it to the network and the other miners check the solution to verify that it is indeed correct.
Adding to Blockchain: If the solution is verified, the block is added to the blockchain. The miner who successfully mined the block is rewarded with newly minted Bitcoin from the coinbase transaction and the transaction fees from the included transactions.
Immutable Record: The new block becomes part of the immutable and transparent record of all transactions on the blockchain. The block hash and the information in it are used to update the blockchain, and the process starts again with the next block.
In other words, miners use proof-of-work to adjust the nonce value until a hash is obtained that meets the specified difficulty requirements.
??Summary of the Transaction Start to Finish
A user wants to send some Bitcoin to another user.
They create a transaction with the details of the send, including the amount of Bitcoin being sent, the sender’s address, and the receiver’s address.
The user then uses their private key to encrypt the transaction. This private key is like a secret code that only the user knows and it is used to prove that the user is who they say they are. The encrypted transaction is broadcasted to the network of Bitcoin nodes.
The nodes verify the transaction using the sender’s public key, which is available on the blockchain. They check that the signature is valid and that the sender has enough Bitcoin to complete the transaction.
The node then groups the verified transactions into a block. The block is then broadcasted to the network of Bitcoin miners. Miners use a complex mathematical algorithm to solve a puzzle, which is called “mining.” Once the puzzle is solved, it is added to the blockchain and the block is added to the chain.
Once the block is added to the blockchain, the transaction is considered complete and the receiver can access the Bitcoin using their own private key.
In summary, the sender creates and encrypts the transaction with their private key, the nodes verify the transaction UTXOs using the sender’s public key, and the miners add the verified transaction to the blockchain. The receiver can then access the Bitcoin using their private key. Once a block is mined, all the transactions included in it are considered confirmed, and the UTXOs used as inputs in these transactions are considered spent and will not be used again.
??DON’T TRUST, VERIFY
In the world of cryptocurrencies, the phrase “Don’t trust, verify” is a reminder to always verify transactions yourself rather than relying on others, such as a centralized authority or intermediary. The Bitcoin network is made up of a decentralized network of nodes, which allows users to verify transactions on their own.
However, there are scenarios that can cause transactions to be reversed, such as double-spending, orphaned blocks, and reorganization. To increase the security of transactions, it is recommended to wait for 6 confirmations, or 6 blocks containing the specific transaction, before considering it final. The more confirmations a transaction has, the more secure it becomes as the likelihood of it being reversed decreases. The number of confirmations may vary depending on the use case and desired level of security.
Double-Spending
In a double-spend attack, a malicious actor attempts to spend the same Bitcoin twice by manipulating the network to accept their second spending of the same Bitcoin as valid. If a miner or group of miners controlling more than 50% of the network’s hashing power (known as a 51% attack) confirm a double-spend transaction, it could be added to a block and considered valid, effectively reversing the original transaction.
Orphaned Blocks
When two miners find the same block at the same time, the network may temporarily accept both. When one of the blocks is later extended by additional blocks, the network will recognize this chain as the main chain and the other block will become an orphan, no longer part of the main blockchain. The transactions included in the orphaned block are not lost and will be included in a later block if they remain valid.
Reorganization
This could theoretically happen if a new block were added to the blockchain and it causes the existing chain to be replaced by a different one. If a transaction was included in a block that is no longer on the main chain, it would be considered invalid and the transaction would be reversed.
______________________________________________
Class Exercise: Transactions with UTXOs
TRANSACTIONS WITH UTXOs
Understand Your Role: You have been assigned one of the following roles: sender, receiver, node, or miner.
Sender: Responsible for creating and broadcasting transactions.
Receiver: Responsible for receiving and verifying transactions.
Node: Responsible for validating transactions and following the rules.
Miner: Responsible for verifying, adding transactions to the blockchain, and collecting rewards for their work.
If You Are the Sender, Create a Transaction:
Take a transaction form and fill in the following fields:
Input UTXO: 20 BTC
Output UTXO: 10 BTC to the receiver’s address
Output UTXO: 1 BTC to the miner’s address
Change UTXO: 9 BTC to your address
Signature: Your signature simulating a private key
Pass the transaction form and the corresponding number of coins to the receiver.
If You Are the Receiver, Verify Transactions:
Check the transaction form to ensure that the correct number of coins and the receiver’s name or initials are written.
Count the coins you received and compare them to the number of coins written on the transaction form.
If the coins match, check the approval box on the UTXO chart that is shared and accessible to everyone in the class.
If the coins do not match or you have doubts, reject the transaction and write the reason on the UTXO chart.
If You Are a Node, Validate Transactions:
Verify that the transaction is valid according to the rules of the protocol and the consensus mechanism.
Check that the sender’s address and receiver’s address are valid.
Ensure that the sender has enough funds to complete the transaction by verifying that the UTXO used as input in the transaction exists and has not been spent before, using the UTXO chart.
Check that the transaction does not double-spend any coins by examining the UTXO chart.
If You Are a Miner, Add Transactions to the Blockchain:
Check the transactions that have been approved by the receivers and validated by the nodes.
Roll the die and compare the numbers with the other miners. The miner with the smaller roll number (under 25) will add the transaction to the blockchain. For your time and energy, you will receive a reward of 1 BTC.
Once a transaction is added to the blockchain, it cannot be changed or reversed.
Keep Track of Your Coin Balance:
Throughout the activity, keep track of your coin balance by counting the coins in your digital wallet.
Discuss Key Concepts:
Discuss with your classmates and teacher the key concepts learned.