From 8a89ed2e7d5b4b842253ca0a0fc308fcdfb50045 Mon Sep 17 00:00:00 2001 From: Howie Zhao Date: Sun, 1 Sep 2024 15:13:51 +0000 Subject: [PATCH 1/2] feat: add p7 notes --- BTC/mining-difficulty.md | 25 +++++++++++++++++++++++++ README.md | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 BTC/mining-difficulty.md diff --git a/BTC/mining-difficulty.md b/BTC/mining-difficulty.md new file mode 100644 index 0000000..cc3a6d0 --- /dev/null +++ b/BTC/mining-difficulty.md @@ -0,0 +1,25 @@ +# P7 Mining Difficulty + +## Mining and mining difficulty + +Mining is to continuously try the `nonce` value in the block header so that the hash value of the entire block header is less than or equal to the given target threshold. That is, $H(block \; header) \leq target$. The smaller the `target`, the greater the mining difficulty. + +The mining difficulty is inversely proportional to the `target`, that is, $difficulty=\frac{difficulty\_1\_target}{target}$, where `difficulty_1_target` refers to the `target` corresponding to the mining difficulty of 1. The minimum mining difficulty is 1. + +## Adjust mining difficulty + +The mining difficulty is adjusted to keep the average block time of BTC stable. That is, a new block is generated every **10** minutes. + +If the block time cannot remain stable, then as the computing power increases, the block time may become shorter and shorter, which will cause a large number of forks. The more forks there are, the less beneficial it is for the system to reach consensus. This is because a large number of forks disperse the system's computing power. + +## Specific implementation of BTC + +The mining difficulty is adjusted every **2016** blocks. The specific adjustment method is: $target = target \times \frac{actual \; time}{expected \; time}$. + +The *expected time* represents the time required to generate 2016 blocks under ideal circumstances, that is, $2016 \times 10$ minutes (about two weeks). The *actual time* is the time spent mining the last 2016 blocks. + +That is to say, if the time required to generate 2016 blocks is more than two weeks, it means that the mining difficulty is too high. According to the above formula, the `target` will become larger and the mining difficulty will be reduced. Vice versa. + +The above formula is defined in the code of the Bitcoin node, prompting each Bitcoin node to comply with this rule. + +If a Bitcoin node maliciously modifies the code to reduce the difficulty of mining, the blocks it issues will not be recognized by the vast majority of other honest nodes. diff --git a/README.md b/README.md index 35a5e90..7484fd5 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ This course focuses on the fundamental concepts and implementation principles of ## Course Outline ### Course Introduction -- **Introduction** +1. **Introduction** ### Bitcoin 2. **[Cryptographic Principles](./BTC/cryptographic-principle.md)** @@ -22,7 +22,7 @@ This course focuses on the fundamental concepts and implementation principles of 4. **Consensus Protocols** 5. **Implementation** 6. **Network** -7. **Mining Difficulty** +7. **[Mining Difficulty](./BTC/mining-difficulty.md)** 8. **Mining** 9. **Scripting** 10. **Forks** From ce7f0e3aea7210aef4c409f53963bbf1130b8180 Mon Sep 17 00:00:00 2001 From: Howie Zhao Date: Sun, 1 Sep 2024 23:52:07 +0800 Subject: [PATCH 2/2] fix: fix latex warning --- BTC/mining-difficulty.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BTC/mining-difficulty.md b/BTC/mining-difficulty.md index cc3a6d0..97aa1be 100644 --- a/BTC/mining-difficulty.md +++ b/BTC/mining-difficulty.md @@ -4,7 +4,7 @@ Mining is to continuously try the `nonce` value in the block header so that the hash value of the entire block header is less than or equal to the given target threshold. That is, $H(block \; header) \leq target$. The smaller the `target`, the greater the mining difficulty. -The mining difficulty is inversely proportional to the `target`, that is, $difficulty=\frac{difficulty\_1\_target}{target}$, where `difficulty_1_target` refers to the `target` corresponding to the mining difficulty of 1. The minimum mining difficulty is 1. +The mining difficulty is inversely proportional to the `target`, that is, $difficulty=\frac{difficulty1target}{target}$, where `difficulty_1_target` refers to the `target` corresponding to the mining difficulty of 1. The minimum mining difficulty is 1. ## Adjust mining difficulty