Skip to content

Commit f5a614e

Browse files
TheBlueMattdunxen
authored andcommitted
Include rounded msat balances in Balance::ClaimableOnChannelClose
If we're gonna push users towards using `Balance` to determine their current balances, we really need to provide more information, including msat balances. Here we add rounded-out msat balances to the pre-close balance information
1 parent a39357e commit f5a614e

File tree

2 files changed

+108
-16
lines changed

2 files changed

+108
-16
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,32 @@ pub enum Balance {
629629
/// transaction fee) this value will be zero. For [`ChannelMonitor`]s created prior to LDK
630630
/// 0.0.124, the channel is always treated as outbound (and thus this value is never zero).
631631
transaction_fee_satoshis: u64,
632+
/// The amount of millisatoshis which has been burned to fees from HTLCs which are outbound
633+
/// from us and are related to a payment which was sent by us. This is the sum of the
634+
/// millisatoshis part of all HTLCs which are otherwise represented by
635+
/// [`Balance::MaybeTimeoutClaimableHTLC`] with their
636+
/// [`Balance::MaybeTimeoutClaimableHTLC::outbound_payment`] flag set, as well as any dust
637+
/// HTLCs which would otherwise be represented the same.
638+
outbound_payment_htlc_rounded_msat: u64,
639+
/// The amount of millisatoshis which has been burned to fees from HTLCs which are outbound
640+
/// from us and are related to a forwarded HTLC. This is the sum of the millisatoshis part
641+
/// of all HTLCs which are otherwise represented by [`Balance::MaybeTimeoutClaimableHTLC`]
642+
/// with their [`Balance::MaybeTimeoutClaimableHTLC::outbound_payment`] flag *not* set, as
643+
/// well as any dust HTLCs which would otherwise be represented the same.
644+
outbound_forwarded_htlc_rounded_msat: u64,
645+
/// The amount of millisatoshis which has been burned to fees from HTLCs which are inbound
646+
/// to us and for which we know the preimage. This is the sum of the millisatoshis part of
647+
/// all HTLCs which would be represented by [`Balance::ContentiousClaimable`] on channel
648+
/// close, but whose current value is included in
649+
/// [`Balance::ClaimableOnChannelClose::amount_satoshis`], as well as any dust HTLCs which
650+
/// would otherwise be represented the same.
651+
inbound_claiming_htlc_rounded_msat: u64,
652+
/// The amount of millisatoshis which has been burned to fees from HTLCs which are inbound
653+
/// to us and for which we do not know the preimage. This is the sum of the millisatoshis
654+
/// part of all HTLCs which would be represented by [`Balance::MaybePreimageClaimableHTLC`]
655+
/// on channel close, as well as any dust HTLCs which would otherwise be represented the
656+
/// same.
657+
inbound_htlc_rounded_msat: u64,
632658
},
633659
/// The channel has been closed, and the given balance is ours but awaiting confirmations until
634660
/// we consider it spendable.
@@ -2320,10 +2346,17 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
23202346
} else {
23212347
let mut claimable_inbound_htlc_value_sat = 0;
23222348
let mut nondust_htlc_count = 0;
2349+
let mut outbound_payment_htlc_rounded_msat = 0;
2350+
let mut outbound_forwarded_htlc_rounded_msat = 0;
2351+
let mut inbound_claiming_htlc_rounded_msat = 0;
2352+
let mut inbound_htlc_rounded_msat = 0;
23232353
for (htlc, _, source) in us.current_holder_commitment_tx.htlc_outputs.iter() {
23242354
if htlc.transaction_output_index.is_some() {
23252355
nondust_htlc_count += 1;
2326-
} else { continue; }
2356+
}
2357+
let rounded_value_msat = if htlc.transaction_output_index.is_none() {
2358+
htlc.amount_msat
2359+
} else { htlc.amount_msat % 1000 };
23272360
if htlc.offered {
23282361
let outbound_payment = match source {
23292362
None => {
@@ -2333,22 +2366,35 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
23332366
Some(HTLCSource::PreviousHopData(_)) => false,
23342367
Some(HTLCSource::OutboundRoute { .. }) => true,
23352368
};
2336-
res.push(Balance::MaybeTimeoutClaimableHTLC {
2337-
amount_satoshis: htlc.amount_msat / 1000,
2338-
claimable_height: htlc.cltv_expiry,
2339-
payment_hash: htlc.payment_hash,
2340-
outbound_payment,
2341-
});
2369+
if outbound_payment {
2370+
outbound_payment_htlc_rounded_msat += rounded_value_msat;
2371+
} else {
2372+
outbound_forwarded_htlc_rounded_msat += rounded_value_msat;
2373+
}
2374+
if htlc.transaction_output_index.is_some() {
2375+
res.push(Balance::MaybeTimeoutClaimableHTLC {
2376+
amount_satoshis: htlc.amount_msat / 1000,
2377+
claimable_height: htlc.cltv_expiry,
2378+
payment_hash: htlc.payment_hash,
2379+
outbound_payment,
2380+
});
2381+
}
23422382
} else if us.payment_preimages.get(&htlc.payment_hash).is_some() {
2343-
claimable_inbound_htlc_value_sat += htlc.amount_msat / 1000;
2383+
inbound_claiming_htlc_rounded_msat += rounded_value_msat;
2384+
if htlc.transaction_output_index.is_some() {
2385+
claimable_inbound_htlc_value_sat += htlc.amount_msat / 1000;
2386+
}
23442387
} else {
2345-
// As long as the HTLC is still in our latest commitment state, treat
2346-
// it as potentially claimable, even if it has long-since expired.
2347-
res.push(Balance::MaybePreimageClaimableHTLC {
2348-
amount_satoshis: htlc.amount_msat / 1000,
2349-
expiry_height: htlc.cltv_expiry,
2350-
payment_hash: htlc.payment_hash,
2351-
});
2388+
inbound_htlc_rounded_msat += rounded_value_msat;
2389+
if htlc.transaction_output_index.is_some() {
2390+
// As long as the HTLC is still in our latest commitment state, treat
2391+
// it as potentially claimable, even if it has long-since expired.
2392+
res.push(Balance::MaybePreimageClaimableHTLC {
2393+
amount_satoshis: htlc.amount_msat / 1000,
2394+
expiry_height: htlc.cltv_expiry,
2395+
payment_hash: htlc.payment_hash,
2396+
});
2397+
}
23522398
}
23532399
}
23542400
res.push(Balance::ClaimableOnChannelClose {
@@ -2358,6 +2404,10 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
23582404
us.current_holder_commitment_tx.feerate_per_kw, nondust_htlc_count,
23592405
us.onchain_tx_handler.channel_type_features())
23602406
} else { 0 },
2407+
outbound_payment_htlc_rounded_msat,
2408+
outbound_forwarded_htlc_rounded_msat,
2409+
inbound_claiming_htlc_rounded_msat,
2410+
inbound_htlc_rounded_msat,
23612411
});
23622412
}
23632413

lightning/src/ln/monitor_tests.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,19 @@ fn do_chanmon_claim_value_coop_close(anchors: bool) {
242242
assert_eq!(vec![Balance::ClaimableOnChannelClose {
243243
amount_satoshis: 1_000_000 - 1_000 - commitment_tx_fee - anchor_outputs_value,
244244
transaction_fee_satoshis: commitment_tx_fee,
245+
outbound_payment_htlc_rounded_msat: 0,
246+
outbound_forwarded_htlc_rounded_msat: 0,
247+
inbound_claiming_htlc_rounded_msat: 0,
248+
inbound_htlc_rounded_msat: 0,
245249
}],
246250
nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
247-
assert_eq!(vec![Balance::ClaimableOnChannelClose { amount_satoshis: 1_000, transaction_fee_satoshis: 0 }],
251+
assert_eq!(vec![Balance::ClaimableOnChannelClose {
252+
amount_satoshis: 1_000, transaction_fee_satoshis: 0,
253+
outbound_payment_htlc_rounded_msat: 0,
254+
outbound_forwarded_htlc_rounded_msat: 0,
255+
inbound_claiming_htlc_rounded_msat: 0,
256+
inbound_htlc_rounded_msat: 0,
257+
}],
248258
nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
249259

250260
nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
@@ -443,11 +453,19 @@ fn do_test_claim_value_force_close(anchors: bool, prev_commitment_tx: bool) {
443453
assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
444454
amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - commitment_tx_fee - anchor_outputs_value,
445455
transaction_fee_satoshis: commitment_tx_fee,
456+
outbound_payment_htlc_rounded_msat: 3000,
457+
outbound_forwarded_htlc_rounded_msat: 0,
458+
inbound_claiming_htlc_rounded_msat: 0,
459+
inbound_htlc_rounded_msat: 0,
446460
}, sent_htlc_balance.clone(), sent_htlc_timeout_balance.clone()]),
447461
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
448462
assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
449463
amount_satoshis: 1_000,
450464
transaction_fee_satoshis: 0,
465+
outbound_payment_htlc_rounded_msat: 0,
466+
outbound_forwarded_htlc_rounded_msat: 0,
467+
inbound_claiming_htlc_rounded_msat: 0,
468+
inbound_htlc_rounded_msat: 3000,
451469
}, received_htlc_balance.clone(), received_htlc_timeout_balance.clone()]),
452470
sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
453471

@@ -495,6 +513,10 @@ fn do_test_claim_value_force_close(anchors: bool, prev_commitment_tx: bool) {
495513
commitment_tx_fee - // The commitment transaction fee with two HTLC outputs
496514
anchor_outputs_value, // The anchor outputs value in satoshis
497515
transaction_fee_satoshis: commitment_tx_fee,
516+
outbound_payment_htlc_rounded_msat: 3000,
517+
outbound_forwarded_htlc_rounded_msat: 0,
518+
inbound_claiming_htlc_rounded_msat: 0,
519+
inbound_htlc_rounded_msat: 0,
498520
}, sent_htlc_timeout_balance.clone()];
499521
if !prev_commitment_tx {
500522
a_expected_balances.push(sent_htlc_balance.clone());
@@ -504,6 +526,10 @@ fn do_test_claim_value_force_close(anchors: bool, prev_commitment_tx: bool) {
504526
assert_eq!(vec![Balance::ClaimableOnChannelClose {
505527
amount_satoshis: 1_000 + 3_000 + 4_000,
506528
transaction_fee_satoshis: 0,
529+
outbound_payment_htlc_rounded_msat: 0,
530+
outbound_forwarded_htlc_rounded_msat: 0,
531+
inbound_claiming_htlc_rounded_msat: 3000,
532+
inbound_htlc_rounded_msat: 0,
507533
}],
508534
nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
509535

@@ -987,12 +1013,20 @@ fn test_no_preimage_inbound_htlc_balances() {
9871013
assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
9881014
amount_satoshis: 1_000_000 - 500_000 - 10_000 - commitment_tx_fee,
9891015
transaction_fee_satoshis: commitment_tx_fee,
1016+
outbound_payment_htlc_rounded_msat: 0,
1017+
outbound_forwarded_htlc_rounded_msat: 0,
1018+
inbound_claiming_htlc_rounded_msat: 0,
1019+
inbound_htlc_rounded_msat: 0,
9901020
}, a_received_htlc_balance.clone(), a_sent_htlc_balance.clone()]),
9911021
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
9921022

9931023
assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
9941024
amount_satoshis: 500_000 - 20_000,
9951025
transaction_fee_satoshis: 0,
1026+
outbound_payment_htlc_rounded_msat: 0,
1027+
outbound_forwarded_htlc_rounded_msat: 0,
1028+
inbound_claiming_htlc_rounded_msat: 0,
1029+
inbound_htlc_rounded_msat: 0,
9961030
}, b_received_htlc_balance.clone(), b_sent_htlc_balance.clone()]),
9971031
sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
9981032

@@ -1272,6 +1306,10 @@ fn do_test_revoked_counterparty_commitment_balances(anchors: bool, confirm_htlc_
12721306
assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
12731307
amount_satoshis: 100_000 - 5_000 - 4_000 - 3 - 2_000 + 3_000,
12741308
transaction_fee_satoshis: 0,
1309+
outbound_payment_htlc_rounded_msat: 3000,
1310+
outbound_forwarded_htlc_rounded_msat: 0,
1311+
inbound_claiming_htlc_rounded_msat: 0,
1312+
inbound_htlc_rounded_msat: 0,
12751313
}, Balance::MaybeTimeoutClaimableHTLC {
12761314
amount_satoshis: 2_000,
12771315
claimable_height: missing_htlc_cltv_timeout,
@@ -1826,6 +1864,10 @@ fn do_test_revoked_counterparty_aggregated_claims(anchors: bool) {
18261864
assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
18271865
amount_satoshis: 100_000 - 4_000 - 3_000,
18281866
transaction_fee_satoshis: 0,
1867+
outbound_payment_htlc_rounded_msat: 0,
1868+
outbound_forwarded_htlc_rounded_msat: 0,
1869+
inbound_claiming_htlc_rounded_msat: 0,
1870+
inbound_htlc_rounded_msat: 0,
18291871
}, Balance::MaybeTimeoutClaimableHTLC {
18301872
amount_satoshis: 4_000,
18311873
claimable_height: htlc_cltv_timeout,

0 commit comments

Comments
 (0)