`prefer_current_channel` overflows `u64 * 110` during invoice route-hint selection (panic in debug, wrong channel in release)

未關閉 適合新手
#4,767 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
83/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
rust
領域
backend, databases

研究方向

從 lightning/src/ln/invoice_utils.rs 中第 583–585 行附近的 prefer_current_channel 開始,接著閱讀它在建立 invoice 時的呼叫端。執行提供的重現程式 repro_prefer_current_channel_overflow,確認溢位問題。比較邏輯能處理所述的大額數值而不溢位,並選擇符合條件的最大 channel 後,即可完成;接著在 debug 和 release 模式下重新執行重現程式。

由索引模型根據 Issue 內容生成。

描述

Summary

In lightning/src/ln/invoice_utils.rs:583-585, prefer_current_channel multiplies u64 msat values by 110 and 100 without widening:

let scaled_min_inbound = min_inbound_capacity_msat.unwrap() * 110;
let current_sufficient = current_channel * 100 >= scaled_min_inbound;
let candidate_sufficient = candidate_channel * 100 >= scaled_min_inbound;

Valid BOLT 11 amounts are accepted up to MAX_VALUE_MSAT = 2_100_000_000_000_000_000 (21M BTC), yet u64::MAX / 110 = 167_697_673_397_359_560 msat (~1.677M BTC) and u64::MAX / 100 = 184_467_440_737_095_516 msat (~1.845M BTC).

When a node has 2+ eligible private channels with the same counterparty and creates a BOLT 11 invoice via ChannelManager::create_bolt11_invoice (or invoice_utils::create_phantom_invoice) with amount_msats > u64::MAX / 110:

  • Debug builds (overflow-checks = true): panics at lightning/src/ln/invoice_utils.rs:583 (attempt to multiply with overflow).
  • Release builds: scaled_min_inbound silently wraps modulo 2^64 (e.g., (u64::MAX / 110 + 1).wrapping_mul(110) == 94), so both channels compare as sufficient and LDK selects the smallest channel instead of the largest channel, inverting the documented fallback rule.
Minimal Reproducer (commit 103b762b4f7842320874d4127abec854c101caba)

Add to mod test in lightning/src/ln/invoice_utils.rs:

#[test]
fn repro_prefer_current_channel_overflow() {
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 10_000);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 500_000, 10_000);
	let largest = nodes[1].node.list_usable_channels().into_iter()
		.max_by_key(|c| c.inbound_capacity_msat).unwrap();
	let params = Bolt11InvoiceParameters {
		amount_msats: Some(u64::MAX / 110 + 1),
		..Default::default()
	};
	let inv = nodes[1].node.create_bolt11_invoice(params).unwrap();
	assert_eq!(inv.route_hints()[0].0[0].short_channel_id, largest.inbound_scid_alias.unwrap());
}
  • cargo test -p lightning --lib repro_prefer_current_channel_overflow panics with attempt to multiply with overflow at invoice_utils.rs:583:30.
  • cargo test --release -p lightning --lib repro_prefer_current_channel_overflow fails the assertion because LDK selects the ~98.3M msat channel over the ~494.3M msat channel.
Suggested Fix

Widen the three operands to u128:

let scaled_min_inbound = u128::from(min_inbound_capacity_msat.unwrap()) * 110;
let current_sufficient = u128::from(current_channel) * 100 >= scaled_min_inbound;
let candidate_sufficient = u128::from(candidate_channel) * 100 >= scaled_min_inbound;
主要語言
Rust
星號
1.4k
分支
482
PR 合併指標
30 天內沒有已合併 PR

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

lightningdevkit/rust-lightning 的其他 Issue

查看 lightningdevkit/rust-lightning 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。