This is the second article in the blogger’s mining science album . It follows the previous article “The Process of Bitcoin Transactions” and continues to analyze the entire process of Bitcoin transactions from initiation to confirmation to give a glimpse of the overall picture of Bitcoin mining. The process of Bitcoin transactions is essentially a process of consuming and generating a bunch of UTXOs. These processes are constructed by the transaction initiator in accordance with the method specified by the Bitcoin protocol, and recorded and confirmed by the new blocks generated by the Bitcoin network. Once the transaction information is recorded and confirmed by the Bitcoin block, the transaction is completed , and the Bitcoin network realizes the transfer of value. In the article "The Process of Bitcoin Transactions", we already know how Bitcoin transaction information is constructed, but how does this information enter the new Bitcoin block? Next, the blogger will use the process of generating new Bitcoin blocks to explain in detail how Bitcoin blocks record and confirm transaction information. Here are some knowledge points to be introduced: The transaction pool, also known as the memory pool, is a place to store pending transactions. Each Bitcoin mining node has its own independent transaction pool. Due to the size of the transaction pool, the minimum transaction fee ratio ( the transaction fee ratio referred to in this article is the handling fee included in the unit volume transaction, the unit is Sat/B, that is, the handling fee for each B-byte transaction is x satoshis Bitcoin, the same below), etc., the transaction pool of each node is also different. When constructing a preliminary block, miners (mining pools) need to select transactions to be packaged from the transaction pool. Since the transaction pool is often called, its data is stored in the RAM of the node server, which means that the size of the transaction pool will not be too large. Mining nodes , in the Bitcoin network, are nodes that store Bitcoin data and participate in recording and verifying Bitcoin transactions and blocks. Some of these nodes not only participate in recording and verifying, but also participate in the creation of new Bitcoin blocks. They construct new blocks and compete for the right to record through PoW proof of work, and then obtain the right to create new blocks. These nodes are mining nodes . Early mining nodes included miners and mining pools, but currently, due to the high difficulty of Bitcoin mining, it is difficult for a single miner to win the right to record and create new blocks. The main Bitcoin mining nodes are various mining pools, such as F2Pool, Poolin, BTC.com, Antpool, Slushpool, etc. UTXO library , the UTXO cluster constructed by the Bitcoin node by scanning all transaction information of the node. It contains all unconsumed UTXO. Whenever a new block is generated, the UTXO library will delete the UTXO consumed in the new block from its own list and add the newly generated UTXO to its own list. Coinbase reward , also known as coin creation transaction. The Bitcoin protocol stipulates that for each new Bitcoin block, the Bitcoin network will generate N Bitcoins, which will be paid to the miner who created this block as a reward for maintaining the Bitcoin network. At the same time, all transaction fees included in other transactions in this block other than the Coinbase reward will also be combined in the Coinbase reward and paid to the miner who created this block. Among them, the value of N was 50 when Bitcoin was born, and it has been halved approximately every 4 years since then. It is currently 6.25 . The Bitcoin network controls the total amount of Bitcoin in this halving method. The Coinbase reward is the first transaction recorded in each block .Transactions to be confirmed will first enter the transaction pool When we want to initiate a Bitcoin transaction, the transaction initiator constructs the transaction information. At this time, the transaction information is a transaction to be confirmed , which contains transaction input information (unused UTXO and correct private key signature) and transaction output information (unconfirmed UTXO locked in the new wallet address). After verification, the pending transaction is broadcasted to the Bitcoin network by the transaction initiator. All nodes in the Bitcoin network can verify and record the broadcasted information. Among them, the mining node will verify the pending transaction information after receiving the broadcast. After the verification is passed, the mining node will add the pending transaction to its own transaction pool. Figure 1: Transactions to be confirmed enter the transaction pool The transaction information that needs to be verified includes: Whether the transaction contains valid input and output wallet addresses; Is the transaction size smaller than the maximum size of the block (the maximum size of a Bitcoin block is currently 1M)? Whether the input UTXO is legal (compared with the node's UTXO library, the input UTXO has not been used); Whether the total transaction input and output amounts are reasonable (total input amount ≥ total output amount); Determine whether the transaction input has a reward from Coinbase. The coin corresponding to the reward must have at least 100 block confirmations before it can be used; Confirm that there are no duplicate transactions in the transaction pool; The transaction fee set by the transaction is higher than the transaction fee ratio (Sat/B) limit of the mempool, as well as other verifications (such as verification and tracking of orphaned transactions, etc.) Mining nodes select transactions from the transaction pool and construct preliminary blocks When a mining node is about to construct a reserve block and prepare to generate a new block, it will sort by priority and take pending transactions from the transaction pool. The reserve block usually reserves a certain amount of space for high-priority transactions, and the remaining space will be filled up in descending order according to the transaction fee ratio (Sat/B) until the block is full or the transactions in the transaction pool are used up. But Bitcoin blocks contain more than just pending transactions from the transaction pool. According to the Bitcoin protocol, a Bitcoin block mainly consists of five parts: magic number, block size, block header, transaction counter and transaction information. As shown below: Figure 2 The structure of a Bitcoin block Among them, "magic number" is a constant with a value of 0xD9B4BEF9; "block volume" is the total volume of all data in this block; "block header" can be regarded as an abbreviated information of the entire block. The block information used for mining is the block header; "transaction counter" is used to record the number of transactions in the block; "transaction data" is all transaction information contained in the block, including the Coinbase reward part. Generally speaking, this part of the data occupies most of the space of the entire block. In a Bitcoin block, the block header is the most critical information. It contains all the characteristic information of the entire block:- Block version number . The version information of the Bitcoin node that created the block, used to track the upgrade and update of the Bitcoin protocol;
- The hash value of the previous block . Also called the parent block hash, it is used to locate the previous block. Each block contains the hash value of its previous block. Any slight change to any block will cause a huge change in the hash value of the subsequent blocks. This ensures that all Bitcoin blocks form a single chain structure, which can effectively prevent malicious tampering of Bitcoin block data.
- MerkleRoot hash . In the transaction data list of the block, take the hash value of all transaction data to build a Merkle tree. The root hash value of this Merkle tree is the MerkleRoot hash (as shown below). Due to the sensitivity of the hash algorithm, any slight change in any transaction data in the Merkle tree of the entire transaction will have a linkage effect, resulting in a huge change in the root hash value of the Merkle tree. Therefore, the Merkle root hash value (MerkleRoot Hash) of the transaction data can be regarded as the fingerprint of the entire transaction, which is used to refer to the transaction data in the block.
Figure 3 Merkle tree structure of transaction data- Timestamp . The time when the reserve block was created.
- The current target hash value . The Bitcoin protocol stipulates that a block is valid only when the hash value of the prepared block created by the miner is less than the target hash value. The target hash value is determined by the mining difficulty. When the mining difficulty increases, the target hash value becomes smaller, and it becomes more difficult for miners to find a hash value that meets the requirements of the Bitcoin network. According to the current mining difficulty, to find a hash value lower than the target hash value, theoretically, an S17 mining machine needs to work continuously for 42 years . Therefore, there is basically no situation where individuals build their own nodes to mine Bitcoin.
- Random number . Also called Nonce. We can find that in the block header information, the block version number, the hash value of the previous block, the MerkleRoot hash value, the timestamp, and the current target hash are all known information, which are relatively fixed and difficult to change at will. Therefore, if you want to adjust the hash value of the reserve block, you need to introduce a variable data - a random number. By modifying the random number, you can adjust the hash value of the reserve block.
After the mining node builds the preliminary block, it will send the block header information to the miners. The miners change the hash value of the preliminary block by continuously adjusting the random number in the block header. When the hash value of the preliminary block is lower than the current target hash value of the Bitcoin network, this block is a legal new block. The mining node will promptly broadcast the new block to the Bitcoin network. After receiving the broadcast information, other Bitcoin nodes in the Bitcoin network will verify the new block. After the verification is passed, the new block will be added to the local. At this point, the new block is created and confirmed, and the corresponding transaction is also completed. References: 1. Bitcoin source code analysis: https://blog.csdn.net/ztemt_sw2/article/details/80958087?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase 2. Bitcoin block header structure: https://www.investopedia.com/terms/b/block-header-cryptocurrency.asp 3. Bitcoin target hash value: https://en.bitcoin.it/wiki/Target 4. Bitcoin block structure: https://en.bitcoin.it/wiki/Block#Block_structure 5. Mastering Bitcoin: https://wizardforcel.gitbooks.io/masterbitcoin2cn/content/ch09.html |