Skip to content

Commit e224263

Browse files
committed
translate some articles
1 parent f36c6fa commit e224263

File tree

12 files changed

+878
-1
lines changed

12 files changed

+878
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,86 @@
11
---
22
sidebar_position: 5
33
---
4+
45
# Bits
6+
7+
Introducing the difficulty bits representing the difficulty target of a block.
8+
9+
## What are Difficulty Bits (Bits)?
10+
11+
Difficulty bits (Bits) are a field in the Bitcoin block header that represent the current mining difficulty. It is a
12+
compact form of the target threshold, and miners must generate a block hash that is less than this target value for it
13+
to be accepted by the network. The difficulty bits are encoded in a compact way to reduce data storage and transmission
14+
overhead.
15+
16+
## The Role of Difficulty Bits
17+
18+
### 1. Controlling Block Generation Time
19+
20+
Difficulty bits directly affect the mining difficulty, thereby controlling the block generation time. The goal of the
21+
Bitcoin network is to generate a new block every 10 minutes. By adjusting the difficulty bits, the network ensures that
22+
block generation time stays within the target range despite changes in miner hash power.
23+
24+
### 2. Ensuring Network Security
25+
26+
By adjusting mining difficulty, the Bitcoin network can prevent malicious actors from easily generating new blocks. As
27+
miner hash power increases, the network automatically raises the mining difficulty, increasing the cost and difficulty
28+
of attacks, thereby ensuring the security and stability of the network.
29+
30+
## Calculating Difficulty Bits
31+
32+
The calculation process of difficulty bits is as follows:
33+
34+
1. **Target Threshold**: Represent the target threshold as a 256-bit large integer.
35+
2. **Compact Representation**: Convert the target threshold into a compact form, represented as the difficulty bits
36+
field.
37+
38+
### Example
39+
40+
Assuming the target threshold is `0x1d00ffff`, the corresponding difficulty bits representation is:
41+
42+
```
43+
0x1d00ffff = 0x00ffff * 2^(8*(0x1d - 3))
44+
```
45+
46+
## Difficulty Adjustment Mechanism
47+
48+
The Bitcoin network adjusts the difficulty every 2016 blocks (approximately every two weeks). Based on the actual
49+
generation time of the past 2016 blocks, the network adjusts the difficulty bits so that the next 2016 blocks'
50+
generation time approaches the target time (two weeks).
51+
52+
### Adjustment Formula
53+
54+
New Difficulty = Old Difficulty * (Actual Generation Time / Target Generation Time)
55+
56+
## Obtaining Difficulty Bits Information
57+
58+
Using the Bitcoin Core client, you can obtain the difficulty bits information of the current block through the command
59+
line interface (CLI):
60+
61+
### Obtaining Latest Block Information
62+
63+
```bash
64+
mvc-cli getblockchaininfo
65+
```
66+
67+
### Obtaining Specific Block Information
68+
69+
First, get the block hash:
70+
71+
```bash
72+
mvc-cli getblockhash <height>
73+
```
74+
75+
Then, use the block hash to get detailed block information, including the difficulty bits field:
76+
77+
```bash
78+
mvc-cli getblock <blockhash>
79+
```
80+
81+
## Summary
82+
83+
Difficulty bits (Bits) are an important field in the Bitcoin blockchain, representing mining difficulty, controlling
84+
block generation time, and ensuring network security. Understanding the principles and calculation methods of difficulty
85+
bits helps to gain a deeper understanding of the Bitcoin network's operation mechanism and its application in
86+
decentralized systems.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,84 @@
11
---
22
sidebar_position: 7
33
---
4+
45
# blk.dat
6+
7+
Introducing the storage format of Bitcoin block files blk.dat.
8+
9+
## What is blk.dat File
10+
11+
Bitcoin nodes use blk.dat files to store blockchain data. Each blk.dat file contains a series of Bitcoin blocks stored
12+
in a compact binary format. These files are located in the Bitcoin data directory, usually under the `blocks/`
13+
subdirectory.
14+
15+
## Structure of blk.dat File
16+
17+
### Block File Header
18+
19+
Each blk.dat file begins with a 4-byte magic number, which is used to mark the start of a block. In Bitcoin, the magic
20+
number is `0xD9B4BEF9`.
21+
22+
### Block Data
23+
24+
Following the magic number is the block data. The block data includes:
25+
26+
1. **Block Size**: A 4-byte integer representing the size of the block (in bytes).
27+
2. **Block Content**: The actual block data, including the block header and block body (transaction data).
28+
29+
The order of the block data is: magic number -> block size -> block content. Multiple blocks are stored consecutively in
30+
a single blk.dat file.
31+
32+
### File Rotation
33+
34+
When a blk.dat file reaches its maximum size (default is 2GB), the Bitcoin node creates a new blk.dat file, with the
35+
filenames incrementing sequentially, such as `blk00000.dat`, `blk00001.dat`, and so on.
36+
37+
## How to Read blk.dat File
38+
39+
Reading a blk.dat file can be done using the Bitcoin Core client or custom parsing tools. The parsing process involves
40+
the following steps:
41+
42+
1. **Open File**: Open the blk.dat file in binary mode.
43+
2. **Read Magic Number**: Verify if it is a valid block start.
44+
3. **Read Block Size**: Get the block size information.
45+
4. **Read Block Content**: Read the complete block data based on the block size.
46+
5. **Repeat Steps**: Continue reading the next block until the end of the file.
47+
48+
### Example Code
49+
50+
Here is a simple Python example code to parse a blk.dat file:
51+
52+
```python
53+
import struct
54+
55+
def read_block(file):
56+
magic = file.read(4)
57+
if not magic:
58+
return None
59+
size = struct.unpack("<I", file.read(4))[0]
60+
block_data = file.read(size)
61+
return block_data
62+
63+
with open('blk00000.dat', 'rb') as f:
64+
while True:
65+
block = read_block(f)
66+
if block is None:
67+
break
68+
print(f"Read block of size: {len(block)}")
69+
```
70+
71+
## Importance of blk.dat Files
72+
73+
1. **Blockchain Storage**: blk.dat files are the foundation for Bitcoin nodes to store the complete blockchain data,
74+
ensuring all transactions and block records are fully preserved.
75+
2. **Node Synchronization**: When new nodes join the network, they can quickly synchronize blockchain data by
76+
downloading and parsing blk.dat files.
77+
3. **Data Recovery**: In case of data corruption or node restart, blk.dat files provide a reliable way to recover
78+
blockchain data.
79+
80+
## Summary
81+
82+
blk.dat files are crucial components for storing blockchain data in Bitcoin nodes. By storing block data in a compact
83+
binary format, they ensure the integrity and reliability of the blockchain. Understanding the structure and reading
84+
methods of blk.dat files helps to deeply grasp the operation mechanism of Bitcoin nodes and blockchain data management.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,79 @@
11
---
22
sidebar_position: 6
33
---
4+
45
# Block Hash
6+
7+
Introducing how the block ID is calculated.
8+
9+
## What is a Block Hash
10+
11+
A block hash is a unique identifier in the blockchain, generated by performing a double SHA-256 hash on the block
12+
header. The block hash is used to identify and reference specific blocks, ensuring the security and integrity of the
13+
blockchain.
14+
15+
## Block Hash Generation Process
16+
17+
### 1. Constructing the Block Header
18+
19+
The block header contains the following fields:
20+
21+
- Version ([Version](version.md))
22+
- Previous Block Hash ([Previous Block Hash](previous-block.md))
23+
- Merkle Root ([Merkle Root](merkle-root.md))
24+
- Timestamp ([Timestamp](time.md))
25+
- Difficulty Target ([Bits](bits.md))
26+
- Nonce ([Nonce](nonce.md))
27+
28+
### 2. Hashing Process
29+
30+
1. Convert the block header's content into a byte sequence.
31+
2. Perform the first SHA-256 hash on the byte sequence to get Hash 1.
32+
3. Perform the second SHA-256 hash on Hash 1 to get the final block hash.
33+
34+
### Example Code
35+
36+
Here is an example code to generate a block hash:
37+
38+
```ruby
39+
require 'digest'
40+
41+
# Block header fields
42+
version = "01000000"
43+
previousblock = "0000000000000000000000000000000000000000000000000000000000000000"
44+
merkleroot = "3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a"
45+
time = "29ab5f49"
46+
bits = "ffff001d"
47+
nonce = "1dac2b7c"
48+
49+
blockheader = version + previousblock + merkleroot + time + bits + nonce
50+
51+
# Convert to byte sequence
52+
bytes = [blockheader].pack("H*")
53+
54+
# First SHA-256 hash
55+
hash1 = Digest::SHA256.digest(bytes)
56+
57+
# Second SHA-256 hash
58+
hash2 = Digest::SHA256.digest(hash1)
59+
60+
# Convert result to hexadecimal string
61+
blockhash = hash2.unpack("H*")[0]
62+
63+
puts blockhash
64+
```
65+
66+
## Functions of a Block Hash
67+
68+
1. **Block Identification**: The block hash serves as the unique identifier for a block, used to quickly locate and
69+
reference specific blocks in the blockchain.
70+
2. **Ensuring Integrity**: By hashing the block header, the block hash ensures the integrity and immutability of the
71+
block data.
72+
3. **Consensus Mechanism**: Miners must find a nonce that makes the block hash less than the target value when
73+
generating new blocks, ensuring the proof-of-work mechanism of the blockchain.
74+
75+
## Summary
76+
77+
The block hash is generated by performing a double SHA-256 hash on the block header and is a key element in identifying
78+
and verifying blocks in the blockchain. Understanding the block hash generation process and its functions helps to grasp
79+
the security and operation mechanisms of the blockchain.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,65 @@
11
---
22
sidebar_position: 3
33
---
4+
45
# Merkle Root
6+
7+
Introducing the concept of the Merkle root.
8+
9+
## What is a Merkle Root
10+
11+
The Merkle root is a key component in the blockchain, used to verify the integrity and consistency of all transactions
12+
within a block. It is a single hash value obtained by recursively hashing all transaction hashes (TXIDs) in the block.
13+
The Merkle root is included in the block header to ensure the immutability of the transaction data within the block.
14+
15+
## Generation of the Merkle Root
16+
17+
The process of generating the Merkle root is as follows:
18+
19+
1. **Hash Transactions**: Hash all transaction hashes (TXIDs) within the block.
20+
2. **Pairwise Hashing**: Combine the hashed transactions in pairs and hash them again. If the number of transactions is
21+
odd, the last transaction hash is paired with itself and hashed.
22+
3. **Recursive Hashing**: Repeat the above steps until only one hash value remains, which is the Merkle root.
23+
24+
### Example
25+
26+
Suppose there are four transaction TXIDs: A, B, C, D.
27+
28+
1. **Initial Hashing**:
29+
- Hash A, B, C, D to get Ha, Hb, Hc, Hd.
30+
2. **Pairwise Hashing**:
31+
- Combine Ha with Hb, and Hc with Hd, to get Ha+b, Hc+d.
32+
3. **Final Hashing**:
33+
- Combine Ha+b with Hc+d to get the Merkle root Hroot.
34+
35+
## Functions of the Merkle Root
36+
37+
### 1. Verifying Transaction Integrity
38+
39+
The Merkle root provides an efficient way to verify the integrity of all transactions within a block. By checking the
40+
Merkle root, it can be determined whether the transactions in the block have been tampered with.
41+
42+
### 2. Improving Data Processing Efficiency
43+
44+
The Merkle root allows lightweight nodes (SPV nodes) to verify the presence of specific transactions in a block by
45+
downloading only the block header rather than the entire block, thereby improving data processing efficiency and
46+
bandwidth utilization.
47+
48+
### 3. Ensuring Data Security
49+
50+
Through the Merkle root, the integrity of blockchain data can be verified, preventing malicious nodes from tampering
51+
with transaction records and ensuring the security and reliability of the blockchain.
52+
53+
## Merkle Tree and Lightweight Wallets
54+
55+
The Merkle tree structure not only enhances blockchain security but also supports the implementation of lightweight
56+
wallets. Lightweight wallets do not need to download the entire blockchain; they only need to download the block headers
57+
and corresponding Merkle proofs to verify the existence and validity of transactions. This significantly reduces the
58+
need for storage and computational resources, making blockchain technology more accessible.
59+
60+
## Summary
61+
62+
The Merkle root is an important component of blockchain technology, generated through recursive hashing to verify the
63+
integrity and consistency of transactions within a block. It not only enhances blockchain security but also supports the
64+
efficient implementation of lightweight wallets, optimizing data processing and verification efficiency. Understanding
65+
how the Merkle root works helps to deeply grasp blockchain technology and its application in decentralized systems.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,77 @@
11
---
22
sidebar_position: 6
33
---
4+
45
# Nonce
6+
7+
Introducing the nonce field in the block header.
8+
9+
## What is a Nonce
10+
11+
Nonce is a 32-bit field in the block header, representing a random number used only once. In the Bitcoin mining process,
12+
miners continuously change the nonce value and calculate the hash of the block header until they find a hash that meets
13+
specific conditions (i.e., less than the current network difficulty target).
14+
15+
## Position of Nonce in the Block Header
16+
17+
The block header contains the following fields:
18+
19+
- Version
20+
- Previous Block Hash
21+
- Merkle Root
22+
- Timestamp
23+
- Bits (Difficulty Target)
24+
- Nonce
25+
26+
## Function of Nonce
27+
28+
### 1. Mining Process
29+
30+
In the Bitcoin mining process, miners continuously try different nonce values to find a valid block where the hash of
31+
the block header is less than the current difficulty target. When such a nonce is found, the block is considered valid
32+
and can be added to the blockchain.
33+
34+
### 2. Ensuring Fair Competition
35+
36+
By using the nonce, miners can compete fairly, ensuring that block generation is random and not monopolized by a
37+
specific miner.
38+
39+
### 3. Preventing Reuse
40+
41+
The uniqueness and randomness of the nonce ensure the uniqueness of each block, preventing block reuse and duplicate
42+
transaction confirmations.
43+
44+
## Nonce Calculation
45+
46+
When miners generate a new block, they continuously change the nonce value and perform a double SHA-256 hash on the
47+
block header until they find a hash that meets the required conditions.
48+
49+
### Example
50+
51+
Here is a simple pseudo-code example demonstrating how to mine by changing the nonce value:
52+
53+
```python
54+
import hashlib
55+
56+
def mine_block(block_header, difficulty_target):
57+
nonce = 0
58+
while True:
59+
block_header_with_nonce = block_header + str(nonce)
60+
hash_result = hashlib.sha256(hashlib.sha256(block_header_with_nonce.encode()).digest()).hexdigest()
61+
if int(hash_result, 16) < difficulty_target:
62+
return nonce, hash_result
63+
nonce += 1
64+
65+
block_header = "example_block_header"
66+
difficulty_target = 0x00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
67+
68+
nonce, valid_hash = mine_block(block_header, difficulty_target)
69+
print(f"Valid Nonce: {nonce}, Valid Hash: {valid_hash}")
70+
```
71+
72+
## Summary
73+
74+
Nonce is a crucial field in the Bitcoin block header. By continuously changing the nonce value and performing hash
75+
calculations, miners can find a valid block that meets the specific difficulty target, ensuring the security and
76+
fairness of the blockchain. Understanding the function and calculation process of the nonce helps to deeply understand
77+
the Bitcoin mining mechanism and its application in decentralized systems.

0 commit comments

Comments
 (0)