Skip to content

Commit 3e355b3

Browse files
committed
Doc the on-upgrade ChannelMonitor startup persistence semantics
Because the new startup `ChannelMonitor` persistence semantics rely on new information stored in `ChannelMonitor` only for claims made in the upgraded code, users upgrading from previous version of LDK must apply the old `ChannelMonitor` persistence semantics at least once (as the old code will be used to handle partial claims).
1 parent 467f253 commit 3e355b3

8 files changed

+59
-15
lines changed

lightning/src/chain/channelmonitor.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,15 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
15011501

15021502
/// This is used to provide payment preimage(s) out-of-band during startup without updating the
15031503
/// off-chain state with a new commitment transaction.
1504-
pub(crate) fn provide_payment_preimage<B: Deref, F: Deref, L: Deref>(
1504+
///
1505+
/// It is used only for legacy (created prior to LDK 0.1) pending payments on upgrade, and the
1506+
/// flow that uses it assumes that this [`ChannelMonitor`] is persisted prior to the
1507+
/// [`ChannelManager`] being persisted (as the state necessary to call this method again is
1508+
/// removed from the [`ChannelManager`] and thus a persistence inversion would imply we do not
1509+
/// get the preimage back into this [`ChannelMonitor`] on startup).
1510+
///
1511+
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
1512+
pub(crate) fn provide_payment_preimage_unsafe_legacy<B: Deref, F: Deref, L: Deref>(
15051513
&self,
15061514
payment_hash: &PaymentHash,
15071515
payment_preimage: &PaymentPreimage,
@@ -5279,7 +5287,9 @@ mod tests {
52795287
preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key, &logger);
52805288
for &(ref preimage, ref hash) in preimages.iter() {
52815289
let bounded_fee_estimator = LowerBoundedFeeEstimator::new(&fee_estimator);
5282-
monitor.provide_payment_preimage(hash, preimage, &broadcaster, &bounded_fee_estimator, &logger);
5290+
monitor.provide_payment_preimage_unsafe_legacy(
5291+
hash, preimage, &broadcaster, &bounded_fee_estimator, &logger
5292+
);
52835293
}
52845294

52855295
// Now provide a secret, pruning preimages 10-15

lightning/src/ln/channel.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4027,12 +4027,14 @@ impl<SP: Deref> Channel<SP> where
40274027
/// Claims an HTLC while we're disconnected from a peer, dropping the [`ChannelMonitorUpdate`]
40284028
/// entirely.
40294029
///
4030+
/// This is only used for payments received prior to LDK 0.1.
4031+
///
40304032
/// The [`ChannelMonitor`] for this channel MUST be updated out-of-band with the preimage
40314033
/// provided (i.e. without calling [`crate::chain::Watch::update_channel`]).
40324034
///
40334035
/// The HTLC claim will end up in the holding cell (because the caller must ensure the peer is
40344036
/// disconnected).
4035-
pub fn claim_htlc_while_disconnected_dropping_mon_update<L: Deref>
4037+
pub fn claim_htlc_while_disconnected_dropping_mon_update_legacy<L: Deref>
40364038
(&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage, logger: &L)
40374039
where L::Target: Logger {
40384040
// Assert that we'll add the HTLC claim to the holding cell in `get_update_fulfill_htlc`

lightning/src/ln/channelmanager.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -13284,11 +13284,28 @@ where
1328413284
let peer_state = &mut *peer_state_lock;
1328513285
if let Some(ChannelPhase::Funded(channel)) = peer_state.channel_by_id.get_mut(&previous_channel_id) {
1328613286
let logger = WithChannelContext::from(&channel_manager.logger, &channel.context, Some(payment_hash));
13287-
channel.claim_htlc_while_disconnected_dropping_mon_update(claimable_htlc.prev_hop.htlc_id, payment_preimage, &&logger);
13287+
channel.claim_htlc_while_disconnected_dropping_mon_update_legacy(
13288+
claimable_htlc.prev_hop.htlc_id, payment_preimage, &&logger
13289+
);
1328813290
}
1328913291
}
1329013292
if let Some(previous_hop_monitor) = args.channel_monitors.get(&claimable_htlc.prev_hop.outpoint) {
13291-
previous_hop_monitor.provide_payment_preimage(&payment_hash, &payment_preimage, &channel_manager.tx_broadcaster, &channel_manager.fee_estimator, &channel_manager.logger);
13293+
// Note that this is unsafe as we no longer require the
13294+
// `ChannelMonitor`s to be re-persisted prior to this
13295+
// `ChannelManager` being persisted after we get started running.
13296+
// If this `ChannelManager` gets persisted first then we crash, we
13297+
// won't have the `claimable_payments` entry we need to re-enter
13298+
// this code block, causing us to not re-apply the preimage to this
13299+
// `ChannelMonitor`.
13300+
//
13301+
// We should never be here with modern payment claims, however, as
13302+
// they should always include the HTLC list. Instead, this is only
13303+
// for nodes during upgrade, and we explicitly require the old
13304+
// persistence semantics on upgrade in the release notes.
13305+
previous_hop_monitor.provide_payment_preimage_unsafe_legacy(
13306+
&payment_hash, &payment_preimage, &channel_manager.tx_broadcaster,
13307+
&channel_manager.fee_estimator, &channel_manager.logger
13308+
);
1329213309
}
1329313310
}
1329413311
let mut pending_events = channel_manager.pending_events.lock().unwrap();

lightning/src/ln/functional_tests.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -3702,7 +3702,10 @@ fn test_force_close_fail_back() {
37023702
// Now check that if we add the preimage to ChannelMonitor it broadcasts our HTLC-Success..
37033703
{
37043704
get_monitor!(nodes[2], payment_event.commitment_msg.channel_id)
3705-
.provide_payment_preimage(&our_payment_hash, &our_payment_preimage, &node_cfgs[2].tx_broadcaster, &LowerBoundedFeeEstimator::new(node_cfgs[2].fee_estimator), &node_cfgs[2].logger);
3705+
.provide_payment_preimage_unsafe_legacy(
3706+
&our_payment_hash, &our_payment_preimage, &node_cfgs[2].tx_broadcaster,
3707+
&LowerBoundedFeeEstimator::new(node_cfgs[2].fee_estimator), &node_cfgs[2].logger
3708+
);
37063709
}
37073710
mine_transaction(&nodes[2], &commitment_tx);
37083711
let mut node_txn = nodes[2].tx_broadcaster.txn_broadcast();

lightning/src/ln/monitor_tests.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1883,8 +1883,10 @@ fn do_test_revoked_counterparty_aggregated_claims(anchors: bool) {
18831883

18841884
// Cheat by giving A's ChannelMonitor the preimage to the to-be-claimed HTLC so that we have an
18851885
// HTLC-claim transaction on the to-be-revoked state.
1886-
get_monitor!(nodes[0], chan_id).provide_payment_preimage(&claimed_payment_hash, &claimed_payment_preimage,
1887-
&node_cfgs[0].tx_broadcaster, &LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger);
1886+
get_monitor!(nodes[0], chan_id).provide_payment_preimage_unsafe_legacy(
1887+
&claimed_payment_hash, &claimed_payment_preimage, &node_cfgs[0].tx_broadcaster,
1888+
&LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger
1889+
);
18881890

18891891
// Now get the latest commitment transaction from A and then update the fee to revoke it
18901892
let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
@@ -2507,11 +2509,11 @@ fn do_test_yield_anchors_events(have_htlcs: bool) {
25072509
}
25082510

25092511
if have_htlcs {
2510-
get_monitor!(nodes[0], chan_id).provide_payment_preimage(
2512+
get_monitor!(nodes[0], chan_id).provide_payment_preimage_unsafe_legacy(
25112513
&payment_hash_2.unwrap(), &payment_preimage_2.unwrap(), &node_cfgs[0].tx_broadcaster,
25122514
&LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger
25132515
);
2514-
get_monitor!(nodes[1], chan_id).provide_payment_preimage(
2516+
get_monitor!(nodes[1], chan_id).provide_payment_preimage_unsafe_legacy(
25152517
&payment_hash_1.unwrap(), &payment_preimage_1.unwrap(), &node_cfgs[1].tx_broadcaster,
25162518
&LowerBoundedFeeEstimator::new(node_cfgs[1].fee_estimator), &nodes[1].logger
25172519
);
@@ -2706,7 +2708,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
27062708
for chan_id in [chan_a.2, chan_b.2].iter() {
27072709
let monitor = get_monitor!(nodes[1], chan_id);
27082710
for payment in [payment_a, payment_b, payment_c, payment_d].iter() {
2709-
monitor.provide_payment_preimage(
2711+
monitor.provide_payment_preimage_unsafe_legacy(
27102712
&payment.1, &payment.0, &node_cfgs[1].tx_broadcaster,
27112713
&LowerBoundedFeeEstimator::new(node_cfgs[1].fee_estimator), &nodes[1].logger
27122714
);

lightning/src/ln/reload_tests.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,10 @@ fn do_forwarded_payment_no_manager_persistence(use_cs_commitment: bool, claim_ht
10411041
check_added_monitors!(nodes[2], 1);
10421042

10431043
if claim_htlc {
1044-
get_monitor!(nodes[2], chan_id_2).provide_payment_preimage(&payment_hash, &payment_preimage,
1045-
&nodes[2].tx_broadcaster, &LowerBoundedFeeEstimator(nodes[2].fee_estimator), &nodes[2].logger);
1044+
get_monitor!(nodes[2], chan_id_2).provide_payment_preimage_unsafe_legacy(
1045+
&payment_hash, &payment_preimage, &nodes[2].tx_broadcaster,
1046+
&LowerBoundedFeeEstimator(nodes[2].fee_estimator), &nodes[2].logger
1047+
);
10461048
}
10471049
assert!(nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
10481050

lightning/src/ln/reorg_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ fn test_htlc_preimage_claim_holder_commitment_after_counterparty_commitment_reor
674674

675675
// Provide the preimage now, such that we only claim from the holder commitment (since it's
676676
// currently confirmed) and not the counterparty's.
677-
get_monitor!(nodes[1], chan_id).provide_payment_preimage(
677+
get_monitor!(nodes[1], chan_id).provide_payment_preimage_unsafe_legacy(
678678
&payment_hash, &payment_preimage, &nodes[1].tx_broadcaster,
679679
&LowerBoundedFeeEstimator(nodes[1].fee_estimator), &nodes[1].logger
680680
);
@@ -749,7 +749,7 @@ fn test_htlc_preimage_claim_prev_counterparty_commitment_after_current_counterpa
749749

750750
// Provide the preimage now, such that we only claim from the previous commitment (since it's
751751
// currently confirmed) and not the latest.
752-
get_monitor!(nodes[1], chan_id).provide_payment_preimage(
752+
get_monitor!(nodes[1], chan_id).provide_payment_preimage_unsafe_legacy(
753753
&payment_hash, &payment_preimage, &nodes[1].tx_broadcaster,
754754
&LowerBoundedFeeEstimator(nodes[1].fee_estimator), &nodes[1].logger
755755
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Backwards Compatibility
2+
* The `ChannelManager` deserialization semantics no longer require that
3+
`ChannelMonitor`s be re-persisted after `(BlockHash, ChannelManager)::read`
4+
is called prior to normal node operation. This applies to upgraded nodes
5+
only *after* a startup with the old semantics completes at least once. IOW,
6+
you must deserialize the `ChannelManager` with upgraded LDK, persist the
7+
`ChannelMonitor`s then continue to normal startup once, and thereafter you
8+
may skip the `ChannelMonitor` persistence step.

0 commit comments

Comments
 (0)