Skip to content

Commit 3de6928

Browse files
committed
Use real on-chain value where possible in get_claimable_balance
This uses the new `onchain_value_satoshis` fields in `OnchainEvent::HTLCSpendConfirmation` and `OnchainEvent::HTLCUpdate` to report the post-HTLC-transaction value of HTLC claims, instead of the original HTLC's value.
1 parent 0c3381b commit 3de6928

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,16 +1436,20 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
14361436
let mut delayed_output_pending = None;
14371437
for event in us.onchain_events_awaiting_threshold_conf.iter() {
14381438
match event.event {
1439-
OnchainEvent::HTLCUpdate { commitment_tx_output_idx, htlc_value_satoshis, .. }
1439+
OnchainEvent::HTLCUpdate {
1440+
commitment_tx_output_idx, onchain_value_satoshis, htlc_value_satoshis, .. }
14401441
if commitment_tx_output_idx == Some(htlc_commitment_tx_output_idx) => {
14411442
debug_assert!(htlc_update_pending.is_none());
1442-
htlc_update_pending =
1443-
Some((htlc_value_satoshis.unwrap(), event.confirmation_threshold()));
1443+
htlc_update_pending = Some((
1444+
onchain_value_satoshis.unwrap_or(htlc_value_satoshis.unwrap()),
1445+
event.confirmation_threshold()));
14441446
},
1445-
OnchainEvent::HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. }
1447+
OnchainEvent::HTLCSpendConfirmation {
1448+
commitment_tx_output_idx, preimage, onchain_value_satoshis, .. }
14461449
if commitment_tx_output_idx == htlc_commitment_tx_output_idx => {
14471450
debug_assert!(htlc_spend_pending.is_none());
1448-
htlc_spend_pending = Some((event.confirmation_threshold(), preimage.is_some()));
1451+
htlc_spend_pending = Some((event.confirmation_threshold(),
1452+
preimage.is_some(), onchain_value_satoshis));
14491453
},
14501454
OnchainEvent::MaturingOutput {
14511455
descriptor: SpendableOutputDescriptor::DelayedPaymentOutput(ref descriptor) }
@@ -1496,9 +1500,9 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
14961500
// preimage, we lost funds to our counterparty! We will then continue
14971501
// to show it as ContentiousClaimable until ANTI_REORG_DELAY.
14981502
debug_assert!(htlc_update_pending.is_none());
1499-
if let Some((conf_thresh, true)) = htlc_spend_pending {
1503+
if let Some((conf_thresh, true, value)) = htlc_spend_pending {
15001504
res.push(Balance::ClaimableAwaitingConfirmations {
1501-
claimable_amount_satoshis: htlc.amount_msat / 1000,
1505+
claimable_amount_satoshis: value.unwrap_or(htlc.amount_msat / 1000),
15021506
confirmation_height: conf_thresh,
15031507
});
15041508
} else {

lightning/src/ln/monitor_tests.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,17 @@ fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
228228
v
229229
}
230230

231+
/// Asserts that `a` and `b` are close, but maybe off by up to 5.
232+
/// This is useful when checking fees and weights on transactions as things may vary by a few based
233+
/// on signature size and signature size estimation being non-exact.
234+
fn fuzzy_assert_eq<V: core::convert::TryInto<u64>>(a: V, b: V) {
235+
let a_u64 = a.try_into().map_err(|_| ()).unwrap();
236+
let b_u64 = b.try_into().map_err(|_| ()).unwrap();
237+
eprintln!("Checking {} and {} for fuzzy equality", a_u64, b_u64);
238+
assert!(a_u64 >= b_u64 - 5);
239+
assert!(b_u64 >= a_u64 - 5);
240+
}
241+
231242
fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
232243
// Tests `get_claimable_balances` with an HTLC across a force-close.
233244
// We build a channel with an HTLC pending, then force close the channel and check that the
@@ -496,11 +507,14 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
496507
let node_b_htlc_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
497508
mine_transaction(&nodes[1], &b_broadcast_txn[0]);
498509

510+
fuzzy_assert_eq(b_broadcast_txn[0].output[0].value,
511+
3_000 - chan_feerate * b_broadcast_txn[0].weight() as u64 / 1_000);
512+
499513
assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
500514
claimable_amount_satoshis: 1_000,
501515
confirmation_height: node_b_commitment_claimable,
502516
}, Balance::ClaimableAwaitingConfirmations {
503-
claimable_amount_satoshis: 3_000,
517+
claimable_amount_satoshis: b_broadcast_txn[0].output[0].value,
504518
confirmation_height: node_b_htlc_claimable,
505519
}, Balance::ContentiousClaimable {
506520
claimable_amount_satoshis: 4_000,
@@ -514,7 +528,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
514528
test_spendable_output(&nodes[1], &remote_txn[0]);
515529

516530
assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
517-
claimable_amount_satoshis: 3_000,
531+
claimable_amount_satoshis: b_broadcast_txn[0].output[0].value,
518532
confirmation_height: node_b_htlc_claimable,
519533
}, Balance::ContentiousClaimable {
520534
claimable_amount_satoshis: 4_000,

0 commit comments

Comments
 (0)