---
title: "Ethereum 分片：區塊鏈分片入門"
description: "什麼是區塊鏈分片？它與 Ethereum 有什麼關係？"
---

## **什麼是區塊鏈分片（sharding）？**

Ethereum Sharding 已被 [danksharding 取代——支援 layer 2 擴容，而非直接擴展 mainnet 本身](https://etherscan.io/chartsync/chainarchive)。

**注意**：以下文章概述了原本針對 Ethereum mainnet 分片的計畫，該計畫已被放棄。部分內容可能已過時，請參考上方連結的 danksharding 文章，以取得 Ethereum 擴容路線圖的最新資訊。

多年來，區塊鏈的可擴展性一直是開發者社群中的討論議題。像 Ethereum 這類公有區塊鏈網路需要多個節點驗證交易，這限制了它們的擴展能力。

以 Ethereum 為例，其每秒約可處理 10-13 筆交易。相較於 VISA 等中心化系統可處理高達 24,000 TPS，這個數字相去甚遠。

若要讓區塊鏈——以及運行於其上的去中心化應用——獲得大規模採用，就需要能支撐龐大用戶量的可擴展性。

除了 layer 2 區塊鏈之外，分片（sharding）也是一種被提出用來擴展 Ethereum、支援更多用戶的方案。分片的概念是將主鏈拆分成多個獨立區段，讓節點只需驗證其中一部分交易。

當節點以平行方式驗證交易時，網路吞吐量得以提升，[應用](https://www.alchemy.com/dapps/top/defi-dapps)也能隨用戶量成長而擴展。

<EmbeddedTable
  table={{
    columns: [
      { key: "1", width: 200, title: "Payment System / Cryptocurrency", dataType: "object" },
      { key: "2", width: 200, title: "Transactions per Second", dataType: "object" },
    ],
    data: [
      {
        "1": { title: "<p>Visa</p>", tooltip: "", icon: "" },
        "2": { title: "<p>24,000</p>", tooltip: "", icon: "" },
        id: 0,
      },
      {
        "1": { title: "<p>Ripple</p>", tooltip: "", icon: "" },
        "2": { title: "<p>1,500</p>", tooltip: "", icon: "" },
        id: 1,
      },
      {
        "1": { title: "<p>PayPal</p>", tooltip: "", icon: "" },
        "2": { title: "<p>193</p>", tooltip: "", icon: "" },
        id: 2,
      },
      {
        "1": { title: "<p>Bitcoin Cash</p>", tooltip: "", icon: "" },
        "2": { title: "<p>60</p>", tooltip: "", icon: "" },
        id: 3,
      },
      {
        "1": { title: "<p>Litecoin</p>", tooltip: "", icon: "" },
        "2": { title: "<p>56</p>", tooltip: "", icon: "" },
        id: 4,
      },
      {
        "1": { title: "<p>Dash</p>", tooltip: "", icon: "" },
        "2": { title: "<p>48</p>", tooltip: "", icon: "" },
        id: 5,
      },
      {
        "1": { title: "<p>Ethereum</p>", tooltip: "", icon: "" },
        "2": { title: "<p>20</p>", tooltip: "", icon: "" },
        id: 6,
      },
      {
        "1": { title: "<p>Bitcoin</p>", tooltip: "", icon: "" },
        "2": { title: "<p>7</p>", tooltip: "", icon: "" },
        id: 7,
      },
    ],
  }}
/>

## **什麼是資料庫分片（database sharding）？**

資料庫分片是中心化資料庫管理中常見的技術，指將一個大型資料庫切分成較小的區塊（「shard」），透過將資料庫平行分散到多台機器上，以提升效率與應用程式的可擴展性。

隨著使用軟體的用戶數或執行的操作數增加，儲存在資料庫中的資料量也會隨之增加。超載的資料庫會影響應用效能並損害使用體驗。因此，分片有其必要，可以減輕資料庫負擔並改善載入時間。

### **資料庫分片範例**

假設有一個資料庫，儲存了某城市 100,000 位居民的個人紀錄。

要找出某個人的資訊，需要處理約 100,000 筆交易——這是一項成本高昂且耗時的工作。

但如果我們把這個大型資料庫拆分成多個較小的資料庫，會發生什麼事？

例如，把姓氏開頭為特定字母的所有市民歸類到同一台獨立伺服器上，尋找資訊所需的運算資源就會減少，任務完成所需時間縮短，資料庫也變得更容易管理。

以下是另一個資料庫分片的示意範例：

<EmbeddedTable
  table={{
    columns: [
      { key: "1", width: 200, title: "Product", dataType: "object" },
      { key: "2", width: 200, title: "Price", dataType: "object" },
      { key: "3", width: 200, title: "Price Range", dataType: "object" },
    ],
    data: [
      {
        "1": { title: "<p>WIDGET</p>", tooltip: "", icon: "" },
        "2": { title: "<p>$118</p>", tooltip: "", icon: "" },
        "3": { title: "<p>$100+</p>", tooltip: "", icon: "" },
        id: 0,
      },
      {
        "1": { title: "<p>GIZMO</p>", tooltip: "", icon: "" },
        "2": { title: "<p>$88</p>", tooltip: "", icon: "" },
        "3": { title: "<p>$50-$99.99</p>", tooltip: "", icon: "" },
        id: 1,
      },
      {
        "1": { title: "<p>TRINKET</p>", tooltip: "", icon: "" },
        "2": { title: "<p>$37</p>", tooltip: "", icon: "" },
        "3": { title: "<p>$0-$49.99</p>", tooltip: "", icon: "" },
        id: 2,
      },
      {
        "1": { title: "<p>THINGAMAJIG</p>", tooltip: "", icon: "" },
        "2": { title: "<p>$18</p>", tooltip: "", icon: "" },
        "3": { title: "<p>$0-$49.99</p>", tooltip: "", icon: "" },
        id: 3,
      },
      {
        "1": { title: "<p>DOODAD</p>", tooltip: "", icon: "" },
        "2": { title: "<p>$60</p>", tooltip: "", icon: "" },
        "3": { title: "<p>$50-$99.99</p>", tooltip: "", icon: "" },
        id: 4,
      },
      {
        "1": { title: "<p>TCHOTCHKE</p>", tooltip: "", icon: "" },
        "2": { title: "<p>$999</p>", tooltip: "", icon: "" },
        "3": { title: "<p>$100+</p>", tooltip: "", icon: "" },
        id: 5,
      },
    ],
  }}
/>

### 什麼是 shard？

「shard」意指「整體中的一小部分」。在資料庫管理中，shard 是託管在獨立伺服器上的大型資料庫子集。每個 shard 雖各自包含部分資料，但它們共同組成一個邏輯上完整的資料集。

延續前面的例子，我們可以將姓氏開頭為「A」的市民歸為「Shard 1」，姓氏開頭為「B」的市民歸為「Shard 2」，依此類推。

若將這些邏輯 shard 全部合併，就能得到涵蓋所有市民紀錄的單一資料集。

## **區塊鏈網路中的分片**

區塊鏈網路的分片與中心化資料庫的做法相同：區塊鏈網路可被「分片」，拆分成不同區段，每個 shard 儲存區塊鏈部分資料，並處理一組獨立的交易。

透過分片，區塊鏈網路可以改善網路延遲與可擴展性。

### **區塊鏈分片試圖解決什麼問題？**

由於所有節點都必須就交易有效性達成共識（即取得一致意見），區塊鏈網路同時間只能處理少量交易。

一般而言，每個節點都會儲存區塊鏈的完整歷史紀錄，並處理每一筆交易。這正是 Ethereum、Bitcoin 等區塊鏈網路能稱為「去中心化」的原因。

由於每個完整節點都擁有網路完整歷史紀錄的副本，惡意行為者要劫持網路、進而回滾或竄改交易，會變得更加困難。

不過，確保區塊鏈的去中心化與安全性是以犧牲可擴展性為代價。

分片區塊鏈讓節點無需下載區塊鏈的完整歷史紀錄，也無需驗證流經網路的每一筆交易，藉此提升網路效率，讓區塊鏈能夠擴展以支援更大的用戶需求。

<ImageBlock
  src="https://media.alchemy.com/1703255000-ethereum-sharding-diagram.jpeg"
  alt="ethereum 分片示意圖"
  width={792}
  height={379}
  caption="來源：Genesis Block"
/>

### **什麼是 shard chain？**

在區塊鏈網路的脈絡下，shard chain 會包含一部分資料，並負責一部分交易處理工作。

shard chain 類似一組獨立運作的迷你區塊鏈，為了維護安全性，每條 shard chain 會透過 Validator Manager Contract（VMC）定期向主鏈（Beacon Chain）提交交易紀錄。

由於每條 shard chain 都有各自獨立的交易歷史，以及一組驗證新交易的節點，多條 shard chain 可以同時運行，透過平行處理來提升網路延遲表現與吞吐量。

<ImageBlock
  src="https://media.alchemy.com/1703771502-shard-chains.jpeg"
  alt="分片鏈將交易紀錄提交至 Ethereum Beacon Chain 的示意圖"
  width={1542}
  height={687}
  caption="分片鏈"
/>

## **Ethereum 的分片是什麼？**

Ethereum 原計畫在完成[ Ethereum PoS 升級](https://www.alchemy.com/overviews/ethereum-2-0-your-guide-for-2022)——一系列旨在改善 Ethereum 1.0 功能的升級——後，採用分片作為擴容方案。

### **為什麼需要分片？**

Ethereum 需要分片的主要原因有兩個：支撐用戶數呈指數成長的能力，以及在規模擴大的同時維持去中心化。

#### **1. 支撐持續增加的用戶數**

Ethereum 目前的架構無法應對使用量呈指數成長的情況。

目前，所有 Ethereum 節點都儲存著[ Ethereum Virtual Machine（EVM）](https://www.alchemy.com/overviews/what-is-the-ethereum-virtual-machine-evm)的完整狀態，包括智能合約程式碼與帳戶餘額。

此外，交易是以線性方式執行，且需要整個網路的確認。

線性方式處理交易，加上節點需要管理龐大的資料集，會拖慢網路速度。

#### **2. 在規模擴大時維持去中心化**

要求節點保有區塊鏈完整副本，也會帶來中心化的問題。目前 Ethereum 帳本[已占用超過 10 terabytes 的儲存空間](https://etherscan.io/chartsync/chainarchive)，是一般電腦可容納容量的 10 倍。

隨著 Ethereum 區塊鏈持續成長，運行 Ethereum 節點可能會變得困難，導致只剩下少數節點負責維護網路安全。這會重新引入 Ethereum 原本設計要解決的中心化與單點故障問題，削弱其價值。

#### **分片可以同時解決這兩個問題。**

分片能促進更好的可擴展性，因為節點可以同時驗證不同的交易；而將交易資料切分成較小的區塊，也讓運行完整節點變得更容易，降低中心化的風險。

## **Ethereum 分片術語**

在說明分片的運作方式之前，先介紹一些重要定義：

#### **State（狀態）**

State 指的是系統在任一時間點的資訊。在 Ethereum 中，state 是特定時間點的網路描述——包括合約程式碼、帳戶、地址餘額等。每一筆新交易都會改變 Ethereum 的 state。

#### **Merkle tree（默克爾樹）**

Merkle tree 或 Merkle root 是一種透過雜湊值儲存大量資訊的加密機制。Merkle tree/root 對 Ethereum 的安全性至關重要，因為它們讓節點能快速驗證某筆資料是否屬於更大結構的一部分。

#### **Collation**

Collation 是在 shard chain 上執行的一組交易，類似於 proof-of-work（PoW）中的區塊。Collation 會被提交至主鏈，並串接起來形成區塊鏈。

#### **Collation header**

Collation header 類似於 proof-of-work 共識中的區塊標頭（block header）。Collation header 包含關於 collation 內部資訊的中繼資料，例如：

- 該 collation 所屬的單一 shard 
- 父 collation 的 root hash 
- collation 中所有交易的 Merkle root 
- pre-state root 與 post-state root 
- notaries 的簽章

<EmbeddedTable
  table={{
    columns: [
      { key: "1", width: 200, title: "Property", dataType: "object" },
      { key: "2", width: 200, title: "Value", dataType: "object" },
      { key: "3", width: 200, title: "Signature 1", dataType: "object" },
      { key: "4", width: 200, title: "Signature 2", dataType: "object" },
    ],
    data: [
      {
        "1": { title: "<p><strong>Shard ID:</strong></p>", tooltip: "", icon: "" },
        "2": { title: "<p>43</p>", tooltip: "", icon: "" },
        "3": { title: "<p>&lt;sig #1284&gt;</p>", tooltip: "", icon: "" },
        "4": { title: "<p>&lt;sig #2543&gt;</p>", tooltip: "", icon: "" },
        id: 0,
      },
      {
        "1": { title: "<p><strong>Pre state:</strong></p>", tooltip: "", icon: "" },
        "2": { title: "<p>a138b3ff</p>", tooltip: "", icon: "" },
        "3": { title: "<p>&lt;sig #7821&gt;</p>", tooltip: "", icon: "" },
        "4": { title: "<p>&lt;sig #6118&gt;</p>", tooltip: "", icon: "" },
        id: 1,
      },
      {
        "1": { title: "<p><strong>Post state:</strong></p>", tooltip: "", icon: "" },
        "2": { title: "<p>835680cc</p>", tooltip: "", icon: "" },
        "3": { title: "<p>&lt;sig #9053&gt;</p>", tooltip: "", icon: "" },
        "4": { title: "<p>&lt;sig #4337&gt;</p>", tooltip: "", icon: "" },
        id: 2,
      },
      {
        "1": { title: "<p><strong>Receipt root:</strong></p>", tooltip: "", icon: "" },
        "2": { title: "<p>fa3819d4</p>", tooltip: "", icon: "" },
        "3": { title: "<p>&lt;sig #1662&gt;</p>", tooltip: "", icon: "" },
        "4": { title: "<p>&lt;sig #4785&gt;</p>", tooltip: "", icon: "" },
        id: 3,
      },
      {
        "1": { title: "<p>Tx a142</p>", tooltip: "", icon: "" },
        "2": { title: "<p>Tx a558</p>", tooltip: "", icon: "" },
        "3": { title: "<p>Tx eca6</p>", tooltip: "", icon: "" },
        "4": { title: "<p></p>", tooltip: "", icon: "" },
        id: 4,
      },
      {
        "1": { title: "<p>Tx a35f</p>", tooltip: "", icon: "" },
        "2": { title: "<p>Tx e25a</p>", tooltip: "", icon: "" },
        "3": { title: "<p>Tx 34ac</p>", tooltip: "", icon: "" },
        "4": { title: "<p></p>", tooltip: "", icon: "" },
        id: 5,
      },
      {
        "1": { title: "<p>Tx 2308</p>", tooltip: "", icon: "" },
        "2": { title: "<p>Tx 6987</p>", tooltip: "", icon: "" },
        "3": { title: "<p>Tx f260</p>", tooltip: "", icon: "" },
        "4": { title: "<p></p>", tooltip: "", icon: "" },
        id: 6,
      },
      {
        "1": { title: "<p>Tx 9f14</p>", tooltip: "", icon: "" },
        "2": { title: "<p>Tx ec30</p>", tooltip: "", icon: "" },
        "3": { title: "<p>Tx 5fc3</p>", tooltip: "", icon: "" },
        "4": { title: "<p></p>", tooltip: "", icon: "" },
        id: 7,
      },
    ],
  }}
/>

#### **Notaries**

Notaries 是被隨機指派到某個 shard chain、負責對提出的 collation 投票的驗證者。這些投票稱為「attestation」，用以證明 collation 的有效性。每個 collation 都需要至少三分之二的 collator 簽署認可，才能被加入共識鏈。

#### **Proposers**

Proposer 是被選定來建立 collation 並提交驗證的 collator（或稱驗證者）。Proposer 的職責與 PoW 區塊鏈中的礦工相同。

#### **Committees**

Committee 是一組負責驗證 shard 區塊有效性的驗證者或 notary。這些 committee 會定期隨機重新洗牌，因此驗證者無法預測自己會被分配到哪個（些）committee。

## **Ethereum 分片如何運作？**

由於路線圖持續延遲，Ethereum 社群放棄了原本的分片計畫，轉向以 layer 2 為核心的路線圖。這讓核心開發者得以推出稱為「The Merge」的共識升級，將網路共識機制改為 Proof-of-Stake。

原本 Ethereum 分片升級計畫將 Ethereum 區塊鏈拆分成 64 條 shard chain。每條 shard chain 都會有獨立的 state，也就是說，節點只會儲存部分帳戶餘額與智能合約程式碼，並處理總交易量中的一部分。

#### **Ethereum PoS 分片在實務上可能如何運作？**

假設 Ethereum 有 10,000 個驗證者與 100 條 shard chain。

透過一種偽隨機協定，已在 Validator Manager Contract（VMC）中存入 ETH、具備資格的驗證者會被分配到 shard 1-100。

在 Shard 1 中，會選出一位驗證者（proposer）將新交易組成一個 collation。

其他驗證者（notary）會下載該 collation，並驗證其中交易的有效性。

若三分之二的 notary 對該 collation 提出 attestation，該 collation 便會透過 VMC 提交至主鏈。

值得注意的是，並非整個 collation 都會被加入 Beacon Chain——若要驗證每個 shard 的 collation，會既困難又浪費時間。

相反地，主鏈上的[驗證者節點](https://www.alchemy.com/docs)只需檢查每個 collation 的 attestation（簽章），即可判斷其有效性。

<ImageBlock
  src="https://media.alchemy.com/1703771811-how-ethereum-pos-sharding-works.png"
  alt="Ethereum PoS 分片示意圖：驗證者對提交至主鏈的分片區塊進行認證"
  width={860}
  height={683}
  caption="Ethereum PoS 分片如何運作"
/>

透過 collation header，任何人都能驗證各個 shard 上的活動。

Collation header 的作用相當於「cross-link」，描述不同 shard 的 state 與交易資訊。因此，跨 shard 通訊讓人得以在不參與每個 shard 的情況下，取得 Ethereum 網路的整體視角。

## **分片可能帶來的缺點**

雖然 Ethereum 分片承諾帶來許多好處，但也引入了一系列新問題：

- 每個 shard 運行的節點數量減少，51% 攻擊等惡意行為變得更容易發生。
- 程式碼變得更複雜，[智能合約安全漏洞](https://alchemy.com/overviews/smart-contract-security-best-practices)風險提高。
- Committee 成員可能串謀，將惡意交易提交至主鏈。 

<ImageBlock
  src="https://media.alchemy.com/1703771962-smart-contract-vulnerabilities-via-sharding.jpeg"
  alt="分片帶來的智能合約安全漏洞"
  width={741}
  height={343}
  caption="分片帶來的智能合約安全漏洞"
/>

## **Ethereum 分片：時間軸與分階段推出**

Ethereum 社群至少從 2013 年起就一直在討論分片，但開發者一直延後實作——這是有原因的。分片極為複雜，會帶來新的風險，因此需要嚴謹的測試才能解決各種問題。

根據 Ethereum.org 的說法，分片將在「The Merge」完成後才會在 Ethereum 上推出。所謂 Merge，指的是 PoW 的 Ethereum 主網路（mainnet）與 Beacon Chain（PoS）整合的事件。

[Beacon Chain](https://www.alchemy.com/dapps/the-beacon) 是 Casper proof-of-stake 系統的一種實作，能產生建構可運作分片系統所需的隨機性。該鏈已於 2020 年 12 月 1 日上線。

在下一節中，我們將簡要概述 Ethereum 分片的實作情況：

## **【已放棄】Ethereum 分片：時間軸與分階段推出**

Ethereum 社群自 2013 年起便一直在討論分片，但由於分片極為複雜且會帶來新風險，需要嚴謹測試才能成功上線，因此 Ethereum 開發者一直延後實作。

以下簡要概述 Ethereum 分片的時間軸：

### **ETH 2.0 分片的時間軸為何？**

根據 Ethereum.org 的說法，分片將在「The Merge」——即 PoW 的 Ethereum 主網路（mainnet）與 Beacon Chain 完成整合——之後部署於 Ethereum。

#### **分片第一階段**

根據[ Ethereum 的升級時程規劃](https://ethereum.org/en/upgrades/shard-chains/)，此階段可能於 2023 年展開。不過，目前尚未有明確的分片時間表。

以下概述 Ethereum 分片第一階段可能的樣貌：

- Validator Manager Contract（VMC）託管於 Beacon Chain 上，負責協調分片流程
- 有意成為 ETH2 驗證者的節點，需先將 32 ETH 鎖入智能合約，才能被加入合格驗證者池
- VMC 會定期將驗證者分配至各 shard，負責驗證與處理交易 collation 並提交至共識鏈
- shard 僅作為「資料倉儲」，用以提升 Ethereum 網路的資料處理能力

#### 分片第二階段

ETH PoS 分片升級的第二階段定義較不明確，開發者仍在討論部分細節。不過，我們可以預期 Ethereum 分片第二階段在實務上大致如下：

- shard 從資料層轉變為程式碼執行層——每個 shard 都擁有獨立的「state」（即一組獨立的智能合約、帳戶餘額與地址）
- 每個 shard 的運作方式類似 [Ethereum Mainnet](https://www.alchemy.com/rpc/ethereum)，完整支援智能合約與 dApp
- 跨 shard 通訊讓不同 shard chain 上的用戶能夠交換價值
- 運行於不同 shard chain 上的應用可透過跨 shard 通訊「溝通」並互動，改善 Ethereum 的可擴展性

## **Ethereum 分片——結語**

當多條 shard chain 同時運行時，節點可以提升交易處理能力，並能處理更大量的鏈上資料。

延續前面的例子，若 100 條 shard chain 各自每秒處理 100 筆交易，那麼 Ethereum 2.0 便能達到 10,000 TPS。

從各 shard 發布到基礎層鏈上的資訊只有 collation header——即有效性的加密證明——因此驗證者節點確認交易並提交至共識層鏈會變得更容易。

結果是交易[最終確定性（finality）](https://medium.com/@icebearhww/ethereum-sharding-and-finality-65248951f649)更快，網路延遲表現也隨之提升。

雖然各方估計不一，但分片的導入預期能讓 Ethereum 擴展至每秒處理數十萬筆交易的水準。

隨著 TPS 提升，Ethereum 能提供應用所需的可擴展性，以應對使用量激增與數十億用戶的需求。
