# 打包

### 打包（Packing / Block Creation）是礦工將待處理的交易整理並封裝成一個新區塊的過程。這就像郵局工作人員把散落的信件整理裝箱，準備寄出——只不過這個「箱子」一旦封好，就永遠無法打開修改。

***

## 打包的流程

```
交易池（Mempool）
      ↓
   礦工挑選交易
      ↓
   組裝區塊
      ↓
   尋找有效 Nonce
      ↓
   廣播新區塊
```

***

## 打包的詳細步驟

### 1. 選取交易

礦工從交易池中挑選交易。通常優先選擇：

* 手續費較高的交易
* 體積較小的交易（可以塞更多筆）

### 2. 驗證交易

確認每筆交易都合法：

* 簽名正確
* 餘額足夠
* 沒有雙重支付

### 3. 組裝區塊

| 區塊組成        | 說明              |
| ----------- | --------------- |
| 區塊頭         | 包含前區塊哈希、時間戳、難度等 |
| 交易列表        | 所有被打包的交易        |
| Coinbase 交易 | 礦工給自己的獎勵        |

### 4. 挖礦

不斷嘗試不同的 Nonce 值，直到找到符合難度要求的哈希值。

{% hint style="info" %}
**誰決定哪些交易被打包？**

礦工有完全的自主權。理論上礦工可以只打包手續費最高的交易，甚至打包空區塊（只有 Coinbase 交易）。但大多數礦工為了最大化收益，會盡量塞滿區塊。
{% endhint %}

***

## 區塊大小限制

| 區塊鏈 | 區塊大小/限制          |
| --- | ---------------- |
| 比特幣 | 1 MB（後為 4 MB 等效） |
| 以太坊 | Gas 上限（動態調整）     |
| BCH | 32 MB            |
| BSV | 無上限              |

區塊大小限制決定了每個區塊能打包多少交易，也影響了交易確認速度。

***

## 打包與交易手續費

當交易量大、區塊塞不下時：

```
交易太多 → 區塊塞滿 → 礦工優先打包高手續費交易 → 手續費上漲
```

這就是為什麼網路壅塞時，你需要付更高手續費才能讓交易被「插隊」打包。

{% hint style="warning" %}
**交易卡住了？**

如果你的交易手續費設太低，可能會在交易池裡等很久。有些交易甚至會因為等太久而被「踢出」交易池。
{% endhint %}

***

## 打包與 MEV

MEV（Maximal Extractable Value）是礦工或驗證者透過重新排序、插入或排除交易來獲取額外收益的行為：

* **搶先交易**：看到有利可圖的交易，搶先執行
* **夾心攻擊**：在目標交易前後插入自己的交易
* **清算狙擊**：搶先執行清算獲利

{% hint style="danger" %}
**黑暗森林**

區塊鏈的交易池是公開的，這意味著你的交易意圖可能被礦工或機器人看到並利用。這也是為什麼有些 DeFi 用戶會使用私密交易通道。
{% endhint %}

***

## 延伸閱讀

* [礦工](/qu-kuai-lian-ru-he-da-cheng-gong-shi/wa/gong.md) — 負責打包的人
* [區塊高度](/qu-kuai-lian-ru-he-da-cheng-gong-shi/wa/gao-du.md) — 打包後的區塊編號
* [廣播](/qu-kuai-lian-ru-he-da-cheng-gong-shi/wa/bo.md) — 打包完成後的下一步
* [驗證](/qu-kuai-lian-ru-he-da-cheng-gong-shi/wa/undefined.md) — 其他節點如何驗證區塊
* [算力](/qu-kuai-lian-ru-he-da-cheng-gong-shi/wa/suan-li.md) — 打包需要的運算能力

***

#### 參考資料

* [How Bitcoin Mining Works](https://bitcoin.org/en/how-it-works)
* [Ethereum Block Structure](https://ethereum.org/en/developers/docs/blocks/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.0x1.academy/qu-kuai-lian-ru-he-da-cheng-gong-shi/wa/da-bao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
