Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add P7 Mining Difficulty notes #34

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions BTC/mining-difficulty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# P7 Mining Difficulty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As good notes, key points should be highlighted for clarity.
Please involve more list with key words highlighted in this note.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please help me review again, thanks.


## Mining and mining difficulty

**Mining**: 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.

**Mining Difficulty**: 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

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.

## Resource

- From the statistical chart in this [URL](https://bitinfocharts.com/comparison/bitcoin-confirmationtime.html#3y), we can see that the average block time of Bitcoin has remained at around 10 minutes.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ 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)**
3. **Data Structures**
4. **Consensus Protocols**
5. **Implementation**
6. **Network**
7. **Mining Difficulty**
7. **[Mining Difficulty](./BTC/mining-difficulty.md)**
8. **Mining**
9. **Scripting**
10. **Forks**
Expand Down