Forward the Original Title ‘白话区块链三:揭开挖矿神秘的面纱——挖矿是什么?为什么要挖矿?谁是我们买不起显卡的罪魁祸首?’
One of the most familiar concepts related to blockchain for everyone is probably mining. Many people may have heard about mining numerous times but are not quite sure what it actually entails. In this section, we will interrupt our regular programming to first introduce “mining.”
A quick recap of the content from “Plain Blockchain 1: Introduction to Bitcoin (The Pioneer of Blockchain Revolution and the Challenger of Sovereign Currency),” where Satoshi Nakamoto designed the Bitcoin monetary system to link the rewards for bookkeeping with currency issuance. This approach allows the currency issuance to be automated through the bookkeeping process, solving the currency issuance problem while also encouraging bookkeepers to actively participate.
In the real world, mining involves miners using various tools to dig out precious metals hidden in rocks from mines. Miners extract value from these rare metals hidden in nature through physical labor.
For blockchain projects with rewards for bookkeeping (usually digital currency projects), network nodes expend computational resources to solve problems. Once they find the solution, they package the block and receive a reward in the form of newly issued currency. This process is analogous to extracting precious metals like gold from ore, hence it is commonly referred to as “mining.” Here, the bookkeepers are the miners, who compete to package blocks using hardware and electricity, resulting in the creation of new currency.
Mining serves two main purposes: validating recent transactions by packaging them into blocks and linking them to the blockchain, and issuing new currency by rewarding bookkeepers. Below are the detailed functions of mining:
Miners are incentivized to participate in mining because they receive substantial rewards for successfully packaging a block onto the blockchain. These rewards come from two main sources:
Over time, the block rewards will decrease (for example, due to Bitcoin’s halving mechanism, where the reward is halved approximately every four years). Eventually, after all 21 million Bitcoins have been mined, expected around the year 2141, the automatic block reward will cease to exist, and transaction fees will become the primary source of income for miners.
Bitcoin generates a new block approximately every 10 minutes. Initially, each new block produced 50 Bitcoins. This reward undergoes halving every four years, and to date, there have been four such halving events. Currently, each block generates 3.125 Bitcoins, with the most recent halving occurring in April 2024. The next halving is anticipated around 2028. By around 2141, Bitcoin will reach its issuance limit.
The transaction fee is influenced not only by supply and demand but also by the size of the transaction, as fees are proportional to the space the transaction occupies in the block.
For miners, participating in mining is straightforward. Download a digital currency wallet client, and within the client, click the mining button to start mining.
For blockchain projects with block rewards, the mining algorithm is typically written into a script integrated into the wallet. Miners simply need to click a button to start the script.
The mining algorithm is deterministic, meaning that as long as miners continuously run the algorithm, they will eventually get a result. However, due to varying computational resources, the time required to calculate this result can differ among miners. Once a node calculates the target value, other miners’ efforts during that period are rendered futile, resulting in negative returns due to the expended physical resources.
To avoid wasted effort, miners often pool their computational resources by joining a mining pool node. This node operates like any other node but has significantly more computational power. When the pool successfully mines a block, the rewards are distributed among the participating miners based on their contributed computational resources. It’s important to note that on the blockchain, the miner recorded as packaging the block is the mining pool node, while individual miners receive their share of the rewards allocated by the pool, not directly from the currency system.
Bitmain, a company that manufactures specialized high-efficiency mining chips, has solidified the role of mining pools, contributing to the centralization of bookkeeping power.
Different blockchain projects may have varying mining puzzles and levels of difficulty. Here, we take Bitcoin as an example. The mining algorithm used by Bitcoin is called Proof of Work (PoW). The essence of this algorithm is that to obtain a result, a certain amount of work must be expended to prove it.
The mining puzzle is not a traditional mathematical problem but involves finding a random number, known as a nonce. This nonce, when combined with the data in the block and processed through a hash function, must produce a hash value that meets a specific condition. Typically, this condition requires the hash value to be less than a target value (or, equivalently, that the first n bits of the hash value are zero). The equation is as follows: hash(nonce+block_data)≤target\text{hash}(\text{nonce} + \text{block_data}) \leq \text{target}hash(nonce+block_data)≤target
Bitcoin uses the SHA-256 hash function, which converts any length of input into a fixed-length output of 256 bits (equivalent to 64 hexadecimal digits or 32 bytes). The output is nearly random but guaranteed to be the same for the same input. Mining involves continuously changing the nonce and hashing the block header data using SHA-256 until a hash value that meets the target condition is found.
For example, a SHA-256 hash value with the first 30 bits being zeros: 000000000000000000000000000000111111010000011011000100100110111011000110100010011011000110100010110110101010011101011010100100011011010001111101001111110101001101111101011110011100011110011110000111000100110000001011011010001110011100110010111010010010010001101010110010110000000000000000000000000000000111111010000011011000100100110111011000110100010011011000110100010110110101010011101011010100100011011010001111101001111110101001101111101011110011100011110011110000111000100110000001011011010001110011100110010111010010010010001101010110010110000000000000000000000000000000111111010000011011000100100110111011000110100010011011000110100010110110101010011101011010100100011011010001111101001111110101001101111101011110011100011110011110000111000100110000001011011010001110011100110010111010010010010001101010110010110
Converting this to hexadecimal results in a value starting with seven zeros: 00000003f41b126ec689b1a2da9d5d46d13d0fd1bece47983d59c5d32eb4ac9000000003f41b126ec689b1a2da9d5d46d13d0fd1bece47983d59c5d32eb4ac9000000003f41b126ec689b1a2da9d5d46d13d0fd1bece47983d59c5d32eb4ac90
To simplify, the probability of finding a hash with the first n bits being zero is 12n\frac{1}{2^n}2n1. The larger the n, the more bits are zero, and the lower the probability:
With n up to 256, finding such a hash value with current human computational power, excluding quantum computers, is virtually impossible before the Earth ceases to exist.
Due to the properties of SHA-256, the only method is brute force, which involves continuously trying different nonces until the condition is met. This necessity for high-performance computing equipment is why mining requires such devices.
Since you cannot predict the hash value generated by adding the nonce to the block data and passing it through SHA-256, the process is entirely random. For instance, if the target hash value is 10,000, you have no way of knowing which nonce combined with the block data will yield a hash value less than 10,000. This unpredictable and random nature means miners must keep enumerating possibilities until the condition is satisfied. If multiple values meet the condition, the smallest hash value is chosen since a smaller hash value indicates higher difficulty and lower probability of occurrence.
Verifying if a generated hash value meets the requirement is easy, requiring just one comparison operation. However, finding a hash value less than or equal to the target value can only be achieved through brute force enumeration. This characteristic, where verifying a result is easy but finding the result is hard, is known as computational asymmetry.
The following code simulates the mining process. It starts with a block header data string “geekbang” and incrementally searches from a nonce value of 10,000 until it finds a nonce that meets the specified condition.
Here’s a Python script that demonstrates the mining process:
import hashlib
def main():
base_string = "geekbang"
nonce = 10000
count = 0
while True:
target_string = base_string + str(nonce)
pow_hash = hashlib.sha256(target_string.encode()).hexdigest()
count += 1
if pow_hash.startswith("0000"): # First 4 hex digits are 0, equivalent to the first 16 bits being 0
print("Hash:", pow_hash)
print("Nonce:", nonce, "Scan times:", count)
break
nonce += 1
if __name__ == '__main__':
main()
When the requirement is that the first 4 hexadecimal digits of the hash result are zero (equivalent to the first 16 bits being zero), the number of calculations is approximately 58,000. If the requirement is increased to the first 5 digits being zero, the number of calculations increases to 1.23 million. When the requirement is the first 7 digits being zero, the number of calculations reaches 160 million. This illustrates that each additional zero in the hash prefix increases the computation by approximately 16 times.
Here are the results for different target conditions:
First 4 hex digits are zero (16 bits):
import hashlib
def main():
base_string = "geekbang"
nonce = 10000
count = 0
while True:
target_string = base_string + str(nonce)
pow_hash = hashlib.sha256(target_string).hexdigest()
count = count + 1
if pow_hash.startswith("0000"): # 前4个16进制位是0,相当于前16个比特位是0
print pow_hash
print "nonce: %s scan times: %s" % (nonce, count)
break
nonce = nonce + 1
if name == ‘main‘:
main()
As seen, increasing the required number of leading zeroes in the hash significantly increases the number of attempts needed, following an exponential growth pattern. This demonstrates the computational difficulty and resources needed for mining, underscoring the need for high-performance computing devices in the mining process.
To ensure that a block is generated approximately every 10 minutes, the Bitcoin system adjusts the mining difficulty by changing the number of leading zeros required in the hash value. This difficulty adjustment mechanism is a crucial part of the Bitcoin system. It evaluates the time taken to generate the last 2016 blocks, which should ideally be about two weeks. If these blocks were mined in less than two weeks, the difficulty increases by adding more leading zeros (lowering the target value). Conversely, if it took longer than two weeks, the difficulty decreases by reducing the number of leading zeros (raising the target value). This mechanism ensures that the time to generate a block remains around 10 minutes, even as the network’s total hash rate fluctuates.
This difficulty adjustment mechanism, designed by Bitcoin’s creator Satoshi Nakamoto, was planned from the beginning to automatically adapt to the total computational power of the network. It maintains a steady mining pace and prevents the currency from being issued too quickly, which could disrupt the market economy.
Different blockchain projects may have varying mining algorithms. Here, we describe the most famous one: the Bitcoin mining algorithm.
The execution process of Bitcoin’s underlying mining algorithm is as follows:
The size of each field in the block header is as follows:
The Merkle tree structure is as follows:
The main controversy surrounding mining involves the Proof of Work (PoW) algorithms used by Bitcoin and its derivatives. These algorithms require miners to allocate substantial computational resources to solve problems that the network agrees upon. Many people are working on these computations simultaneously, but only the first to solve the problem gets the reward. The other participants’ computational and electrical resources are effectively wasted, producing no additional value.
There are two primary avenues for optimizing this resource waste:
What is the value of mining? It seems like a waste of resources and meaningless to society.
Revisiting the “Why is Mining Necessary?” section of this article, the direct significance of mining is that it supports the issuance of digital currencies. The mining rewards incentivize more people to participate in maintaining digital currencies, making them more stable. Additionally, mining algorithms uphold transaction consistency within digital currency systems, making them more robust and less susceptible to internal and external attacks.
The indirect significance of mining is derived from digital currencies themselves. Digital currencies create a global, trustless monetary system where secure transactions can occur without the need for centralized institutions, avoiding the drawbacks of centralization. Their global nature also enables efficient and low-cost cross-border transactions.
Will mining returns decrease to the point where they fall below costs, causing miners to stop mining and transactions to cease?
It’s a common concern that mining returns are solely dependent on system rewards, which diminish over time. However, mining also generates significant income from transaction fees. As digital currencies gain wider acceptance, the market for digital currency transactions will expand. This increased transaction volume will intensify competition for block inclusion, leading to higher fees that users are willing to pay to prioritize their transactions. Consequently, miners can continue to earn substantial transaction fees, ensuring the ongoing viability of transaction processing.
Will transaction fees compensate for mining costs?
Yes, transaction fees can help compensate for the costs of mining. As block rewards decrease over time, transaction fees become a crucial source of income for miners. This ensures that even if the electricity costs are high, miners can still sustain their operations through the fees paid by users for transaction processing.
Do multiple miners split the mining rewards if they simultaneously broadcast their new blocks?
No, if multiple miners solve the puzzle and broadcast their new blocks at the same time, it creates a temporary fork in the blockchain. Here’s how it works:
Example:
What is a 51% attack and why don’t super nodes cheat?
A 51% attack occurs when a single entity or group controls more than 50% of the network’s computational power. Here’s why super nodes don’t usually cheat:
How does blockchain prevent intentional disruptions and fraudulent transactions?
Preventing Inconsistent Block Distribution by Miners
Definition and Function: In a blockchain network, all broadcast transactions that have not yet been included in a block are temporarily stored in the memory pool (mempool) of network nodes. Miners monitor their mempool to select transactions for constructing new blocks.
Transaction Selection Strategy: Miners can choose transactions from the mempool based on their strategy. Generally, they prioritize transactions with higher fees, but some may also consider other factors, such as the transaction’s age (the time it has spent in the mempool).
Who Participates and How They Earn: Apart from users, the three main participants in the Bitcoin system are miners, developers, and node operators.
Increase in GPU Prices: The high demand for GPUs by miners for mining purposes has led to a supply shortage, causing prices to increase.
Mining GPUs: Mining GPUs are those previously used for cryptocurrency mining. These GPUs undergo intensive computation, leading to significant wear and tear. Due to their diminished performance, most people prefer not to buy used mining GPUs.
Full Redundant Storage Requirement: Yes, miners need to store the entire blockchain ledger to verify if a token has been previously spent and to ensure the security and integrity of the ledger. However, for very early blocks, miners only need to store the block headers, not the full block contents. This helps maintain the blockchain’s efficiency while preserving its security.
No Automatic Rewards Beyond 21 Million Bitcoins: Once the total supply of Bitcoin reaches its limit of 21 million, the system will no longer issue new bitcoins as rewards to miners. If users are also unwilling to pay transaction fees, theoretically, the transaction system could face operational challenges. However, it is likely that users will be willing to pay transaction fees to have their transactions processed, ensuring the continuation of the system.
Block Height Will Continue to Increase: Even after reaching the maximum supply of 21 million bitcoins, the block height (the total number of blocks) will continue to increase as long as miners are willing to mine and package transactions into new blocks. The block height is not limited by the bitcoin supply cap.
Potential for Consensus Changes: The community could propose changes to the consensus rules, such as increasing the total bitcoin supply, to allow for new coin generation if needed.
Coinbase Transaction: In the Bitcoin blockchain, miner rewards are distributed through a special transaction known as the “coinbase transaction” or “block reward.” This transaction has unique characteristics:
Revenue Calculation per Block: Miner revenue is calculated each time a block is successfully mined and confirmed by the network’s consensus mechanism. The miner (or mining pool) that mines the block receives the block reward and the cumulative transaction fees from all transactions included in the block.
Process of Receiving Rewards: Once a miner or mining pool successfully mines a block and it is confirmed by the network, they receive the block reward and transaction fees. This is executed through the coinbase transaction, which converts the fees and rewards into unspent transaction outputs (UTXOs) credited to the miner’s account.
Sequential Creation: Blocks are created sequentially in the blockchain. A new block can only be created after the previous block has been successfully mined and added to the chain.
Multiple Transactions per Block: Blocks typically contain multiple transactions. Miners include as many transactions as possible to maximize their transaction fee earnings, rather than packaging transactions individually.
Partially Filled Blocks: Most blocks are not fully utilized. Miners start working on the next block as soon as they find a valid hash that meets the difficulty requirement, regardless of whether the block is completely full.
Empty Blocks: Blocks can be mined even if there are no transactions to include. These blocks, called empty blocks, still provide a system reward to the miner.
Longest Chain Rule: When a fork occurs, the network follows the chain with the most accumulated proof of work, which is usually the longest chain. Nodes switch to the longer chain once it becomes evident which one has more work put into it.
Double-Spend Resolution:
Validation Process:
Transaction Fee Calculation: Users can control the transaction fee to expedite the confirmation of their transactions. The fee is determined by the following formula:
Transaction Fee=Total Input−Total Output−Change
Users set the transaction fee by adjusting the input amount, the output amount, and the change amount.
Earnings Distribution: When mining in a pool, the pool node receives the block reward. The pool then distributes the earnings to its members based on their contributed computational power. This distribution is executed through transfers to the miners’ wallets.
Selfish Mining: Selfish mining is a strategy where a miner (or mining pool) finds a new block but does not immediately broadcast it. Instead, they continue mining privately on their chain (hidden chain). Once they find additional blocks, they broadcast them all at once. This can invalidate other miners’ work and tilt computational power and rewards towards the selfish miner. Bitcoin can mitigate the impact of selfish mining by improving the broadcasting protocol.
Transaction Validation: When a transaction is broadcast to a node, it undergoes several checks to determine if it should be added to the mempool:
Block Validation: When a miner node receives a new block, it performs extensive checks on the block and its transactions:
[1] Lecture 12 | In-depth Blockchain Technology (4): PoW Consensus-In-depth explanation of Blockchain-Geek Time:https://time.geekbang.org/column/article/5963
Forward the Original Title ‘白话区块链三:揭开挖矿神秘的面纱——挖矿是什么?为什么要挖矿?谁是我们买不起显卡的罪魁祸首?’
One of the most familiar concepts related to blockchain for everyone is probably mining. Many people may have heard about mining numerous times but are not quite sure what it actually entails. In this section, we will interrupt our regular programming to first introduce “mining.”
A quick recap of the content from “Plain Blockchain 1: Introduction to Bitcoin (The Pioneer of Blockchain Revolution and the Challenger of Sovereign Currency),” where Satoshi Nakamoto designed the Bitcoin monetary system to link the rewards for bookkeeping with currency issuance. This approach allows the currency issuance to be automated through the bookkeeping process, solving the currency issuance problem while also encouraging bookkeepers to actively participate.
In the real world, mining involves miners using various tools to dig out precious metals hidden in rocks from mines. Miners extract value from these rare metals hidden in nature through physical labor.
For blockchain projects with rewards for bookkeeping (usually digital currency projects), network nodes expend computational resources to solve problems. Once they find the solution, they package the block and receive a reward in the form of newly issued currency. This process is analogous to extracting precious metals like gold from ore, hence it is commonly referred to as “mining.” Here, the bookkeepers are the miners, who compete to package blocks using hardware and electricity, resulting in the creation of new currency.
Mining serves two main purposes: validating recent transactions by packaging them into blocks and linking them to the blockchain, and issuing new currency by rewarding bookkeepers. Below are the detailed functions of mining:
Miners are incentivized to participate in mining because they receive substantial rewards for successfully packaging a block onto the blockchain. These rewards come from two main sources:
Over time, the block rewards will decrease (for example, due to Bitcoin’s halving mechanism, where the reward is halved approximately every four years). Eventually, after all 21 million Bitcoins have been mined, expected around the year 2141, the automatic block reward will cease to exist, and transaction fees will become the primary source of income for miners.
Bitcoin generates a new block approximately every 10 minutes. Initially, each new block produced 50 Bitcoins. This reward undergoes halving every four years, and to date, there have been four such halving events. Currently, each block generates 3.125 Bitcoins, with the most recent halving occurring in April 2024. The next halving is anticipated around 2028. By around 2141, Bitcoin will reach its issuance limit.
The transaction fee is influenced not only by supply and demand but also by the size of the transaction, as fees are proportional to the space the transaction occupies in the block.
For miners, participating in mining is straightforward. Download a digital currency wallet client, and within the client, click the mining button to start mining.
For blockchain projects with block rewards, the mining algorithm is typically written into a script integrated into the wallet. Miners simply need to click a button to start the script.
The mining algorithm is deterministic, meaning that as long as miners continuously run the algorithm, they will eventually get a result. However, due to varying computational resources, the time required to calculate this result can differ among miners. Once a node calculates the target value, other miners’ efforts during that period are rendered futile, resulting in negative returns due to the expended physical resources.
To avoid wasted effort, miners often pool their computational resources by joining a mining pool node. This node operates like any other node but has significantly more computational power. When the pool successfully mines a block, the rewards are distributed among the participating miners based on their contributed computational resources. It’s important to note that on the blockchain, the miner recorded as packaging the block is the mining pool node, while individual miners receive their share of the rewards allocated by the pool, not directly from the currency system.
Bitmain, a company that manufactures specialized high-efficiency mining chips, has solidified the role of mining pools, contributing to the centralization of bookkeeping power.
Different blockchain projects may have varying mining puzzles and levels of difficulty. Here, we take Bitcoin as an example. The mining algorithm used by Bitcoin is called Proof of Work (PoW). The essence of this algorithm is that to obtain a result, a certain amount of work must be expended to prove it.
The mining puzzle is not a traditional mathematical problem but involves finding a random number, known as a nonce. This nonce, when combined with the data in the block and processed through a hash function, must produce a hash value that meets a specific condition. Typically, this condition requires the hash value to be less than a target value (or, equivalently, that the first n bits of the hash value are zero). The equation is as follows: hash(nonce+block_data)≤target\text{hash}(\text{nonce} + \text{block_data}) \leq \text{target}hash(nonce+block_data)≤target
Bitcoin uses the SHA-256 hash function, which converts any length of input into a fixed-length output of 256 bits (equivalent to 64 hexadecimal digits or 32 bytes). The output is nearly random but guaranteed to be the same for the same input. Mining involves continuously changing the nonce and hashing the block header data using SHA-256 until a hash value that meets the target condition is found.
For example, a SHA-256 hash value with the first 30 bits being zeros: 000000000000000000000000000000111111010000011011000100100110111011000110100010011011000110100010110110101010011101011010100100011011010001111101001111110101001101111101011110011100011110011110000111000100110000001011011010001110011100110010111010010010010001101010110010110000000000000000000000000000000111111010000011011000100100110111011000110100010011011000110100010110110101010011101011010100100011011010001111101001111110101001101111101011110011100011110011110000111000100110000001011011010001110011100110010111010010010010001101010110010110000000000000000000000000000000111111010000011011000100100110111011000110100010011011000110100010110110101010011101011010100100011011010001111101001111110101001101111101011110011100011110011110000111000100110000001011011010001110011100110010111010010010010001101010110010110
Converting this to hexadecimal results in a value starting with seven zeros: 00000003f41b126ec689b1a2da9d5d46d13d0fd1bece47983d59c5d32eb4ac9000000003f41b126ec689b1a2da9d5d46d13d0fd1bece47983d59c5d32eb4ac9000000003f41b126ec689b1a2da9d5d46d13d0fd1bece47983d59c5d32eb4ac90
To simplify, the probability of finding a hash with the first n bits being zero is 12n\frac{1}{2^n}2n1. The larger the n, the more bits are zero, and the lower the probability:
With n up to 256, finding such a hash value with current human computational power, excluding quantum computers, is virtually impossible before the Earth ceases to exist.
Due to the properties of SHA-256, the only method is brute force, which involves continuously trying different nonces until the condition is met. This necessity for high-performance computing equipment is why mining requires such devices.
Since you cannot predict the hash value generated by adding the nonce to the block data and passing it through SHA-256, the process is entirely random. For instance, if the target hash value is 10,000, you have no way of knowing which nonce combined with the block data will yield a hash value less than 10,000. This unpredictable and random nature means miners must keep enumerating possibilities until the condition is satisfied. If multiple values meet the condition, the smallest hash value is chosen since a smaller hash value indicates higher difficulty and lower probability of occurrence.
Verifying if a generated hash value meets the requirement is easy, requiring just one comparison operation. However, finding a hash value less than or equal to the target value can only be achieved through brute force enumeration. This characteristic, where verifying a result is easy but finding the result is hard, is known as computational asymmetry.
The following code simulates the mining process. It starts with a block header data string “geekbang” and incrementally searches from a nonce value of 10,000 until it finds a nonce that meets the specified condition.
Here’s a Python script that demonstrates the mining process:
import hashlib
def main():
base_string = "geekbang"
nonce = 10000
count = 0
while True:
target_string = base_string + str(nonce)
pow_hash = hashlib.sha256(target_string.encode()).hexdigest()
count += 1
if pow_hash.startswith("0000"): # First 4 hex digits are 0, equivalent to the first 16 bits being 0
print("Hash:", pow_hash)
print("Nonce:", nonce, "Scan times:", count)
break
nonce += 1
if __name__ == '__main__':
main()
When the requirement is that the first 4 hexadecimal digits of the hash result are zero (equivalent to the first 16 bits being zero), the number of calculations is approximately 58,000. If the requirement is increased to the first 5 digits being zero, the number of calculations increases to 1.23 million. When the requirement is the first 7 digits being zero, the number of calculations reaches 160 million. This illustrates that each additional zero in the hash prefix increases the computation by approximately 16 times.
Here are the results for different target conditions:
First 4 hex digits are zero (16 bits):
import hashlib
def main():
base_string = "geekbang"
nonce = 10000
count = 0
while True:
target_string = base_string + str(nonce)
pow_hash = hashlib.sha256(target_string).hexdigest()
count = count + 1
if pow_hash.startswith("0000"): # 前4个16进制位是0,相当于前16个比特位是0
print pow_hash
print "nonce: %s scan times: %s" % (nonce, count)
break
nonce = nonce + 1
if name == ‘main‘:
main()
As seen, increasing the required number of leading zeroes in the hash significantly increases the number of attempts needed, following an exponential growth pattern. This demonstrates the computational difficulty and resources needed for mining, underscoring the need for high-performance computing devices in the mining process.
To ensure that a block is generated approximately every 10 minutes, the Bitcoin system adjusts the mining difficulty by changing the number of leading zeros required in the hash value. This difficulty adjustment mechanism is a crucial part of the Bitcoin system. It evaluates the time taken to generate the last 2016 blocks, which should ideally be about two weeks. If these blocks were mined in less than two weeks, the difficulty increases by adding more leading zeros (lowering the target value). Conversely, if it took longer than two weeks, the difficulty decreases by reducing the number of leading zeros (raising the target value). This mechanism ensures that the time to generate a block remains around 10 minutes, even as the network’s total hash rate fluctuates.
This difficulty adjustment mechanism, designed by Bitcoin’s creator Satoshi Nakamoto, was planned from the beginning to automatically adapt to the total computational power of the network. It maintains a steady mining pace and prevents the currency from being issued too quickly, which could disrupt the market economy.
Different blockchain projects may have varying mining algorithms. Here, we describe the most famous one: the Bitcoin mining algorithm.
The execution process of Bitcoin’s underlying mining algorithm is as follows:
The size of each field in the block header is as follows:
The Merkle tree structure is as follows:
The main controversy surrounding mining involves the Proof of Work (PoW) algorithms used by Bitcoin and its derivatives. These algorithms require miners to allocate substantial computational resources to solve problems that the network agrees upon. Many people are working on these computations simultaneously, but only the first to solve the problem gets the reward. The other participants’ computational and electrical resources are effectively wasted, producing no additional value.
There are two primary avenues for optimizing this resource waste:
What is the value of mining? It seems like a waste of resources and meaningless to society.
Revisiting the “Why is Mining Necessary?” section of this article, the direct significance of mining is that it supports the issuance of digital currencies. The mining rewards incentivize more people to participate in maintaining digital currencies, making them more stable. Additionally, mining algorithms uphold transaction consistency within digital currency systems, making them more robust and less susceptible to internal and external attacks.
The indirect significance of mining is derived from digital currencies themselves. Digital currencies create a global, trustless monetary system where secure transactions can occur without the need for centralized institutions, avoiding the drawbacks of centralization. Their global nature also enables efficient and low-cost cross-border transactions.
Will mining returns decrease to the point where they fall below costs, causing miners to stop mining and transactions to cease?
It’s a common concern that mining returns are solely dependent on system rewards, which diminish over time. However, mining also generates significant income from transaction fees. As digital currencies gain wider acceptance, the market for digital currency transactions will expand. This increased transaction volume will intensify competition for block inclusion, leading to higher fees that users are willing to pay to prioritize their transactions. Consequently, miners can continue to earn substantial transaction fees, ensuring the ongoing viability of transaction processing.
Will transaction fees compensate for mining costs?
Yes, transaction fees can help compensate for the costs of mining. As block rewards decrease over time, transaction fees become a crucial source of income for miners. This ensures that even if the electricity costs are high, miners can still sustain their operations through the fees paid by users for transaction processing.
Do multiple miners split the mining rewards if they simultaneously broadcast their new blocks?
No, if multiple miners solve the puzzle and broadcast their new blocks at the same time, it creates a temporary fork in the blockchain. Here’s how it works:
Example:
What is a 51% attack and why don’t super nodes cheat?
A 51% attack occurs when a single entity or group controls more than 50% of the network’s computational power. Here’s why super nodes don’t usually cheat:
How does blockchain prevent intentional disruptions and fraudulent transactions?
Preventing Inconsistent Block Distribution by Miners
Definition and Function: In a blockchain network, all broadcast transactions that have not yet been included in a block are temporarily stored in the memory pool (mempool) of network nodes. Miners monitor their mempool to select transactions for constructing new blocks.
Transaction Selection Strategy: Miners can choose transactions from the mempool based on their strategy. Generally, they prioritize transactions with higher fees, but some may also consider other factors, such as the transaction’s age (the time it has spent in the mempool).
Who Participates and How They Earn: Apart from users, the three main participants in the Bitcoin system are miners, developers, and node operators.
Increase in GPU Prices: The high demand for GPUs by miners for mining purposes has led to a supply shortage, causing prices to increase.
Mining GPUs: Mining GPUs are those previously used for cryptocurrency mining. These GPUs undergo intensive computation, leading to significant wear and tear. Due to their diminished performance, most people prefer not to buy used mining GPUs.
Full Redundant Storage Requirement: Yes, miners need to store the entire blockchain ledger to verify if a token has been previously spent and to ensure the security and integrity of the ledger. However, for very early blocks, miners only need to store the block headers, not the full block contents. This helps maintain the blockchain’s efficiency while preserving its security.
No Automatic Rewards Beyond 21 Million Bitcoins: Once the total supply of Bitcoin reaches its limit of 21 million, the system will no longer issue new bitcoins as rewards to miners. If users are also unwilling to pay transaction fees, theoretically, the transaction system could face operational challenges. However, it is likely that users will be willing to pay transaction fees to have their transactions processed, ensuring the continuation of the system.
Block Height Will Continue to Increase: Even after reaching the maximum supply of 21 million bitcoins, the block height (the total number of blocks) will continue to increase as long as miners are willing to mine and package transactions into new blocks. The block height is not limited by the bitcoin supply cap.
Potential for Consensus Changes: The community could propose changes to the consensus rules, such as increasing the total bitcoin supply, to allow for new coin generation if needed.
Coinbase Transaction: In the Bitcoin blockchain, miner rewards are distributed through a special transaction known as the “coinbase transaction” or “block reward.” This transaction has unique characteristics:
Revenue Calculation per Block: Miner revenue is calculated each time a block is successfully mined and confirmed by the network’s consensus mechanism. The miner (or mining pool) that mines the block receives the block reward and the cumulative transaction fees from all transactions included in the block.
Process of Receiving Rewards: Once a miner or mining pool successfully mines a block and it is confirmed by the network, they receive the block reward and transaction fees. This is executed through the coinbase transaction, which converts the fees and rewards into unspent transaction outputs (UTXOs) credited to the miner’s account.
Sequential Creation: Blocks are created sequentially in the blockchain. A new block can only be created after the previous block has been successfully mined and added to the chain.
Multiple Transactions per Block: Blocks typically contain multiple transactions. Miners include as many transactions as possible to maximize their transaction fee earnings, rather than packaging transactions individually.
Partially Filled Blocks: Most blocks are not fully utilized. Miners start working on the next block as soon as they find a valid hash that meets the difficulty requirement, regardless of whether the block is completely full.
Empty Blocks: Blocks can be mined even if there are no transactions to include. These blocks, called empty blocks, still provide a system reward to the miner.
Longest Chain Rule: When a fork occurs, the network follows the chain with the most accumulated proof of work, which is usually the longest chain. Nodes switch to the longer chain once it becomes evident which one has more work put into it.
Double-Spend Resolution:
Validation Process:
Transaction Fee Calculation: Users can control the transaction fee to expedite the confirmation of their transactions. The fee is determined by the following formula:
Transaction Fee=Total Input−Total Output−Change
Users set the transaction fee by adjusting the input amount, the output amount, and the change amount.
Earnings Distribution: When mining in a pool, the pool node receives the block reward. The pool then distributes the earnings to its members based on their contributed computational power. This distribution is executed through transfers to the miners’ wallets.
Selfish Mining: Selfish mining is a strategy where a miner (or mining pool) finds a new block but does not immediately broadcast it. Instead, they continue mining privately on their chain (hidden chain). Once they find additional blocks, they broadcast them all at once. This can invalidate other miners’ work and tilt computational power and rewards towards the selfish miner. Bitcoin can mitigate the impact of selfish mining by improving the broadcasting protocol.
Transaction Validation: When a transaction is broadcast to a node, it undergoes several checks to determine if it should be added to the mempool:
Block Validation: When a miner node receives a new block, it performs extensive checks on the block and its transactions:
[1] Lecture 12 | In-depth Blockchain Technology (4): PoW Consensus-In-depth explanation of Blockchain-Geek Time:https://time.geekbang.org/column/article/5963