Skip to content

Commit 2b0fbd5

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 df3c8fa commit 2b0fbd5

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 who's 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.
@@ -2314,10 +2340,17 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
23142340
} else {
23152341
let mut claimable_inbound_htlc_value_sat = 0;
23162342
let mut nondust_htlc_count = 0;
2343+
let mut outbound_payment_htlc_rounded_msat = 0;
2344+
let mut outbound_forwarded_htlc_rounded_msat = 0;
2345+
let mut inbound_claiming_htlc_rounded_msat = 0;
2346+
let mut inbound_htlc_rounded_msat = 0;
23172347
for (htlc, _, source) in us.current_holder_commitment_tx.htlc_outputs.iter() {
23182348
if htlc.transaction_output_index.is_some() {
23192349
nondust_htlc_count += 1;
2320-
} else { continue; }
2350+
}
2351+
let rounded_value_msat = if htlc.transaction_output_index.is_none() {
2352+
htlc.amount_msat
2353+
} else { htlc.amount_msat % 1000 };
23212354
if htlc.offered {
23222355
let outbound_payment = match source {
23232356
None => {
@@ -2327,22 +2360,35 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
23272360
Some(HTLCSource::PreviousHopData(_)) => false,
23282361
Some(HTLCSource::OutboundRoute { .. }) => true,
23292362
};
2330-
res.push(Balance::MaybeTimeoutClaimableHTLC {
2331-
amount_satoshis: htlc.amount_msat / 1000,
2332-
claimable_height: htlc.cltv_expiry,
2333-
payment_hash: htlc.payment_hash,
2334-
outbound_payment,
2335-
});
2363+
if outbound_payment {
2364+
outbound_payment_htlc_rounded_msat += rounded_value_msat;
2365+
} else {
2366+
outbound_forwarded_htlc_rounded_msat += rounded_value_msat;
2367+
}
2368+
if htlc.transaction_output_index.is_some() {
2369+
res.push(Balance::MaybeTimeoutClaimableHTLC {
2370+
amount_satoshis: htlc.amount_msat / 1000,
2371+
claimable_height: htlc.cltv_expiry,
2372+
payment_hash: htlc.payment_hash,
2373+
outbound_payment,
2374+
});
2375+
}
23362376
} else if us.payment_preimages.get(&htlc.payment_hash).is_some() {
2337-
claimable_inbound_htlc_value_sat += htlc.amount_msat / 1000;
2377+
inbound_claiming_htlc_rounded_msat += rounded_value_msat;
2378+
if htlc.transaction_output_index.is_some() {
2379+
claimable_inbound_htlc_value_sat += htlc.amount_msat / 1000;
2380+
}
23382381
} else {
2339-
// As long as the HTLC is still in our latest commitment state, treat
2340-
// it as potentially claimable, even if it has long-since expired.
2341-
res.push(Balance::MaybePreimageClaimableHTLC {
2342-
amount_satoshis: htlc.amount_msat / 1000,
2343-
expiry_height: htlc.cltv_expiry,
2344-
payment_hash: htlc.payment_hash,
2345-
});
2382+
inbound_htlc_rounded_msat += rounded_value_msat;
2383+
if htlc.transaction_output_index.is_some() {
2384+
// As long as the HTLC is still in our latest commitment state, treat
2385+
// it as potentially claimable, even if it has long-since expired.
2386+
res.push(Balance::MaybePreimageClaimableHTLC {
2387+
amount_satoshis: htlc.amount_msat / 1000,
2388+
expiry_height: htlc.cltv_expiry,
2389+
payment_hash: htlc.payment_hash,
2390+
});
2391+
}
23462392
}
23472393
}
23482394
res.push(Balance::ClaimableOnChannelClose {
@@ -2352,6 +2398,10 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
23522398
us.current_holder_commitment_tx.feerate_per_kw, nondust_htlc_count,
23532399
us.onchain_tx_handler.channel_type_features())
23542400
} else { 0 },
2401+
outbound_payment_htlc_rounded_msat,
2402+
outbound_forwarded_htlc_rounded_msat,
2403+
inbound_claiming_htlc_rounded_msat,
2404+
inbound_htlc_rounded_msat,
23552405
});
23562406
}
23572407

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)