Skip to content

Commit 3698f7b

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 f1ac752 commit 3698f7b

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
@@ -1441,16 +1441,20 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
14411441
let mut delayed_output_pending = None;
14421442
for event in us.onchain_events_awaiting_threshold_conf.iter() {
14431443
match event.event {
1444-
OnchainEvent::HTLCUpdate { commitment_tx_output_idx, htlc_value_satoshis, .. }
1444+
OnchainEvent::HTLCUpdate {
1445+
commitment_tx_output_idx, onchain_value_satoshis, htlc_value_satoshis, .. }
14451446
if commitment_tx_output_idx == Some(htlc_commitment_tx_output_idx) => {
14461447
debug_assert!(htlc_update_pending.is_none());
1447-
htlc_update_pending =
1448-
Some((htlc_value_satoshis.unwrap(), event.confirmation_threshold()));
1448+
htlc_update_pending = Some((
1449+
onchain_value_satoshis.unwrap_or(htlc_value_satoshis.unwrap()),
1450+
event.confirmation_threshold()));
14491451
},
1450-
OnchainEvent::HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. }
1452+
OnchainEvent::HTLCSpendConfirmation {
1453+
commitment_tx_output_idx, preimage, onchain_value_satoshis, .. }
14511454
if commitment_tx_output_idx == htlc_commitment_tx_output_idx => {
14521455
debug_assert!(htlc_spend_pending.is_none());
1453-
htlc_spend_pending = Some((event.confirmation_threshold(), preimage.is_some()));
1456+
htlc_spend_pending = Some((event.confirmation_threshold(),
1457+
preimage.is_some(), onchain_value_satoshis));
14541458
},
14551459
OnchainEvent::MaturingOutput {
14561460
descriptor: SpendableOutputDescriptor::DelayedPaymentOutput(ref descriptor) }
@@ -1501,9 +1505,9 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
15011505
// preimage, we lost funds to our counterparty! We will then continue
15021506
// to show it as ContentiousClaimable until ANTI_REORG_DELAY.
15031507
debug_assert!(htlc_update_pending.is_none());
1504-
if let Some((conf_thresh, true)) = htlc_spend_pending {
1508+
if let Some((conf_thresh, true, value)) = htlc_spend_pending {
15051509
res.push(Balance::ClaimableAwaitingConfirmations {
1506-
claimable_amount_satoshis: htlc.amount_msat / 1000,
1510+
claimable_amount_satoshis: value.unwrap_or(htlc.amount_msat / 1000),
15071511
confirmation_height: conf_thresh,
15081512
});
15091513
} 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
@@ -501,11 +512,14 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
501512
let node_b_htlc_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
502513
mine_transaction(&nodes[1], &b_broadcast_txn[0]);
503514

515+
fuzzy_assert_eq(b_broadcast_txn[0].output[0].value,
516+
3_000 - chan_feerate * b_broadcast_txn[0].weight() as u64 / 1_000);
517+
504518
assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
505519
claimable_amount_satoshis: 1_000,
506520
confirmation_height: node_b_commitment_claimable,
507521
}, Balance::ClaimableAwaitingConfirmations {
508-
claimable_amount_satoshis: 3_000,
522+
claimable_amount_satoshis: b_broadcast_txn[0].output[0].value,
509523
confirmation_height: node_b_htlc_claimable,
510524
}, Balance::ContentiousClaimable {
511525
claimable_amount_satoshis: 4_000,
@@ -519,7 +533,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
519533
test_spendable_output(&nodes[1], &remote_txn[0]);
520534

521535
assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
522-
claimable_amount_satoshis: 3_000,
536+
claimable_amount_satoshis: b_broadcast_txn[0].output[0].value,
523537
confirmation_height: node_b_htlc_claimable,
524538
}, Balance::ContentiousClaimable {
525539
claimable_amount_satoshis: 4_000,

0 commit comments

Comments
 (0)