高級
通道狀態機
理解閃電網路通道的完整生命週期,從開啟到關閉的所有狀態轉換。
15 分鐘
通道生命週期概覽
閃電網路通道是一個有限狀態機,從建立到關閉經歷多個狀態。 理解這些狀態對於開發和調試閃電網路應用至關重要。
Channel State Overview:
[Initializing]
|
| open_channel / accept_channel
v
[Funding Pending]
|
| funding_locked (now channel_ready)
v
[Normal Operation] <-- payments, state updates
|
| shutdown / unilateral close / penalty
v
[Closing]
|
v
[Closed] 階段一:通道建立
開啟流程
Alice (initiator) Bob (acceptor)
| |
|---- open_channel --------------->|
| chain_hash |
| funding_satoshis |
| push_msat |
| channel_reserve |
| htlc_minimum |
| feerate_per_kw |
| funding_pubkey |
| ...various basepoints |
| |
|<--- accept_channel --------------|
| minimum_depth |
| channel_reserve |
| funding_pubkey |
| ...various basepoints |
| | 資金交易交換
Alice Bob
| |
| (Alice creates funding tx) |
| |
|---- funding_created ------------>|
| funding_txid |
| funding_output_index |
| signature (for Bob's |
| commitment tx) |
| |
|<--- funding_signed --------------|
| signature (for Alice's |
| commitment tx) |
| |
| (Alice broadcasts funding tx) |
| | 等待確認
Alice Bob
| |
| (wait for minimum_depth conf) |
| |
|---- channel_ready -------------->|
|<--- channel_ready ---------------|
| |
| Channel open! Can start paying |
| | 階段二:正常運作
添加 HTLC
Alice Bob
| |
|---- update_add_htlc ------------>|
| channel_id |
| id (htlc_id) |
| amount_msat |
| payment_hash |
| cltv_expiry |
| onion_routing_packet |
| |
State: HTLC proposed but not confirmed 提交新狀態
Alice Bob
| |
|---- commitment_signed ---------->|
| signature |
| htlc_signatures[] |
| |
|<--- revoke_and_ack --------------|
| per_commitment_secret |
| next_per_commitment_point |
| |
|<--- commitment_signed -----------|
| |
|---- revoke_and_ack ------------->|
| |
Now: both have new commitment tx, old state revoked 解決 HTLC
Three resolution methods:
1. Success (fulfill)
<- update_fulfill_htlc
payment_preimage
2. Failure (fail)
<- update_fail_htlc
reason
3. Malformed
<- update_fail_malformed_htlc
failure_code
After resolution: need commitment_signed / revoke_and_ack again 階段三:通道關閉
協作關閉
Alice Bob
| |
|---- shutdown ------------------->|
| scriptpubkey (close address) |
| |
|<--- shutdown --------------------|
| scriptpubkey |
| |
| (no more new HTLCs accepted) |
| (wait for all HTLCs to resolve) |
| |
|---- closing_signed ------------->|
| fee_satoshis |
| signature |
| |
|<--- closing_signed --------------|
| fee_satoshis (may differ) |
| signature |
| |
| (negotiate fee until agreed) |
| (broadcast closing tx) |
| | 協作關閉
雙方同意,創建簡單的 closing tx,無時間鎖。最快最便宜。
單方關閉
廣播最新承諾交易。發起方需等待 to_self_delay,對方可立即取款。
懲罰關閉
對方廣播舊狀態,使用撤銷密鑰取走全部資金。
錯誤處理
Error Messages: |---- error --------------------------->| | channel_id (or 0 for all) | | data (human-readable message) | Warning message (BOLT 1.1+): |---- warning ------------------------->| | channel_id | | data | error causes channel close warning is just advisory, channel continues
通道狀態完整圖
┌─────────────────┐
│ DISCONNECTED │
└────────┬────────┘
│ connect
▼
┌─────────────────┐
│ CONNECTED │
└────────┬────────┘
│ open_channel
▼
┌───────────────────────────────────────────────────────┐
│ CHANNEL OPENING │
├───────────────────────────────────────────────────────┤
│ SENT_OPEN → RCVD_ACCEPT → SENT_FUNDING_CREATED → │
│ RCVD_FUNDING_SIGNED → FUNDING_SPEND_SEEN → │
│ AWAITING_CHANNEL_READY │
└───────────────────────────┬───────────────────────────┘
│ channel_ready (雙方)
▼
┌─────────────────┐
│ CHANNELD │←──┐
│ (正常) │ │ 更新狀態
└────────┬────────┘───┘
│ shutdown
▼
┌─────────────────┐
│ SHUTTING_DOWN │
└────────┬────────┘
│ closing_signed
▼
┌─────────────────┐
│ CLOSINGD │
└────────┬────────┘
│
▼
┌─────────────────┐
│ CLOSED │
└─────────────────┘ 重新連接
當節點斷開後重新連接,需要同步狀態:
Reconnection Flow:
Alice Bob
| |
|<--> channel_reestablish -------->|
| next_commitment_number |
| next_revocation_number |
| your_last_per_commitment |
| _secret |
| |
Compare states:
• If synced -> continue normal operation
• If out of sync -> retransmit missing messages
• If cannot recover -> force close 常見狀態問題
Stuck Channel
通道卡在某個狀態無法前進。常見原因:對方離線、網路問題、軟體 bug。 解決方案:等待重連或強制關閉。
Pending HTLC
HTLC 長時間未解決。可能是路由問題或下游節點問題。 最終會超時返還,但會佔用流動性。
State Mismatch
雙方狀態不一致。通常是備份恢復後發生。 可能導致資金損失,需要謹慎處理。
下一步: 了解 BOLT 規範 的完整協議定義。
已複製連結