進階
Gossip Protocol 八卦協議
了解閃電網路的 Gossip 協議如何讓節點發現彼此、廣播通道資訊,並構建用於路由的網路圖譜。
12 分鐘
什麼是 Gossip 協議?
Gossip 協議(BOLT #7)定義了閃電網路節點如何發現彼此、如何廣播通道資訊, 以及如何維護用於支付路由的網路圖譜。這個「八卦」機制讓每個節點都能了解 整個網路的拓撲結構。
核心功能: Gossip 讓節點能夠發現不直接連接的遠程節點和通道,這是源路由(source routing) 的基礎——發送方需要知道整個路徑才能構建洋蔥封包。
Gossip 訊息類型
256
channel_announcement
宣告一個新的公開通道。包含通道的兩個節點公鑰和 funding 交易資訊。
258
channel_update
更新通道的路由參數,如費率、時間鎖、啟用/禁用狀態等。每個方向獨立更新。
257
node_announcement
宣告節點的元數據,如別名、顏色、網路地址、支持的功能等。
Channel Announcement
channel_announcement structure:
Fields:
node_signature_1: node 1 signature
node_signature_2: node 2 signature
bitcoin_signature_1: node 1 Bitcoin key signature
bitcoin_signature_2: node 2 Bitcoin key signature
features: channel feature bits
chain_hash: blockchain identifier (mainnet/testnet)
short_channel_id: 8 byte channel identifier
block height (3 bytes)
tx index (3 bytes)
output index (2 bytes)
node_id_1: node 1 pubkey (lexicographically smaller)
node_id_2: node 2 pubkey (lexicographically larger)
bitcoin_key_1: node 1 Bitcoin pubkey
bitcoin_key_2: node 2 Bitcoin pubkey
Validation steps:
1. Check UTXO exists for short_channel_id
2. UTXO is P2WSH, locked to 2-of-2 multisig
3. Verify all 4 signatures
4. Confirm chain_hash matches Channel Update
channel_update structure:
Fields:
signature: publisher's signature
chain_hash: blockchain identifier
short_channel_id: channel identifier
timestamp: Unix timestamp (update time)
message_flags:
bit 0: must_be_one
channel_flags:
bit 0: direction (0=node1->node2, 1=node2->node1)
bit 1: disabled (is channel disabled)
cltv_expiry_delta: HTLC timelock delta
htlc_minimum_msat: minimum HTLC amount
fee_base_msat: base routing fee
fee_proportional_millionths: proportional routing fee
htlc_maximum_msat: maximum HTLC amount (optional)
Fee calculation:
fee = fee_base_msat + (amount * fee_proportional_millionths / 1000000)
Update rules:
• timestamp must be greater than previous
• Each direction updates independently
• Can disable/enable channel Node Announcement
node_announcement structure:
Fields:
signature: node signature
features: node feature bits
timestamp: update time
node_id: node public key
rgb_color: node color (3 bytes)
alias: node alias (max 32 bytes UTF-8)
addresses: network address list
type 1: IPv4 (6 bytes)
type 2: IPv6 (18 bytes)
type 3: Tor v2 (12 bytes) [deprecated]
type 4: Tor v3 (37 bytes)
type 5: DNS hostname
type 6: WebSocket
Prerequisites:
• Node must have at least one public channel
• Otherwise node_announcement is ignored
• Prevents spam node information Gossip 同步
Gossip Query Mechanism:
Initial sync:
New Node Old Node
| |
|---- query_channel_range ---------->|
| (request channels in block range)
| |
|<--- reply_channel_range -----------|
| (returns short_channel_ids) |
| |
|---- query_short_channel_ids ------>|
| (request full channel info) |
| |
|<--- channel_announcement ----------|
|<--- channel_update ----------------|
|<--- node_announcement -------------|
| |
v v
Incremental sync:
• gossip_timestamp_filter: only receive updates after specific time
• Reduces duplicate message transmission Gossip 驗證
有效性檢查
- • 驗證所有簽名
- • 確認 UTXO 存在且未花費
- • 檢查 timestamp 遞增
- • 驗證 chain_hash 匹配
防止垃圾
- • 通道必須有鏈上 UTXO
- • 節點必須有公開通道
- • 限制更新頻率
- • 限制訊息大小
私有通道
Private Channels (Unannounced):
Private channels:
• Do not broadcast channel_announcement
• Not in public graph
• Only known to channel parties
Use cases:
• Mobile wallets connecting to LSP (privacy)
• Merchants not exposing payment channels
• Reducing network graph size
Route Hints in Invoices:
Invoice:
route_hints:
- pubkey: 02abc...
short_channel_id: 123x456x0
fee_base_msat: 1000
fee_proportional: 100
cltv_expiry_delta: 40
This lets senders route to recipients with private channels,
without exposing the channel to the entire network. 圖譜維護
通道過期
如果通道超過 2 週沒有 channel_update,節點可能會認為它不可用並從圖譜中移除。
UTXO 監控
節點應該監控區塊鏈,當 funding UTXO 被花費時,從圖譜中移除對應通道。
存儲需求
完整的網路圖譜可能需要 50-100 MB 存儲空間,這對移動設備是個挑戰。
Gossip v1.5 / v2
社群正在研究改進 Gossip 協議以減少頻寬和存儲需求:
Minisketch
使用集合對帳技術,只同步差異而非完整數據,大幅減少頻寬。
Taproot Gossip
Taproot 通道的 gossip 格式會更緊湊,使用 MuSig2 簽名減少數據量。
相關資源
下一步: 了解 通道備份 如何保護你的閃電網路資金安全。
已複製連結