Skip to main content

lightning/ln/
channelmanager.rs

1// This file is Copyright its original authors, visible in version control
2// history.
3//
4// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7// You may not use this file except in accordance with one or both of these
8// licenses.
9
10//! The top-level channel management and payment tracking stuff lives here.
11//!
12//! The [`ChannelManager`] is the main chunk of logic implementing the lightning protocol and is
13//! responsible for tracking which channels are open, HTLCs are in flight and reestablishing those
14//! upon reconnect to the relevant peer(s).
15//!
16//! It does not manage routing logic (see [`Router`] for that) nor does it manage constructing
17//! on-chain transactions (it only monitors the chain to watch for any force-closes that might
18//! imply it needs to fail HTLCs/payments/channels it manages).
19
20use bitcoin::block::Header;
21use bitcoin::constants::ChainHash;
22use bitcoin::key::constants::SECRET_KEY_SIZE;
23use bitcoin::network::Network;
24use bitcoin::transaction::Transaction;
25
26use bitcoin::hash_types::{BlockHash, Txid};
27use bitcoin::hashes::hmac::Hmac;
28use bitcoin::hashes::sha256::Hash as Sha256;
29use bitcoin::hashes::{Hash, HashEngine, HmacEngine};
30
31use bitcoin::secp256k1::Secp256k1;
32use bitcoin::secp256k1::{PublicKey, SecretKey};
33use bitcoin::{secp256k1, Sequence, SignedAmount};
34
35use crate::blinded_path::message::{
36	AsyncPaymentsContext, BlindedMessagePath, MessageForwardNode, OffersContext,
37};
38use crate::blinded_path::payment::{
39	AsyncBolt12OfferContext, Bolt12OfferContext, PaymentContext, UnauthenticatedReceiveTlvs,
40};
41use crate::blinded_path::NodeIdLookUp;
42use crate::chain;
43use crate::chain::chaininterface::{
44	BroadcasterInterface, ConfirmationTarget, FeeEstimator, LowerBoundedFeeEstimator,
45};
46use crate::chain::channelmonitor::{
47	Balance, ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, MonitorEvent,
48	WithChannelMonitor, ANTI_REORG_DELAY, CLTV_CLAIM_BUFFER, HTLC_FAIL_BACK_BUFFER,
49	LATENCY_GRACE_PERIOD_BLOCKS, MAX_BLOCKS_FOR_CONF,
50};
51use crate::chain::transaction::{OutPoint, TransactionData};
52use crate::chain::{BestBlock, ChannelMonitorUpdateStatus, Confirm, Watch};
53use crate::events::{
54	self, ClosureReason, Event, EventHandler, EventsProvider, HTLCHandlingFailureType,
55	InboundChannelFunds, PaymentFailureReason, ReplayEvent,
56};
57use crate::events::{FundingInfo, PaidBolt12Invoice};
58use crate::ln::chan_utils::selected_commitment_sat_per_1000_weight;
59#[cfg(any(test, fuzzing))]
60use crate::ln::channel::QuiescentAction;
61use crate::ln::channel::{
62	self, hold_time_since, Channel, ChannelError, ChannelUpdateStatus, DisconnectResult,
63	FundedChannel, FundingTxSigned, InboundV1Channel, OutboundV1Channel, PendingV2Channel,
64	ReconnectionMsg, ShutdownResult, SpliceFundingFailed, StfuResponse, UpdateFulfillCommitFetch,
65	WithChannelContext,
66};
67use crate::ln::channel_state::ChannelDetails;
68use crate::ln::funding::SpliceContribution;
69use crate::ln::inbound_payment;
70use crate::ln::interactivetxs::InteractiveTxMessageSend;
71use crate::ln::msgs;
72use crate::ln::msgs::{
73	BaseMessageHandler, ChannelMessageHandler, CommitmentUpdate, DecodeError, LightningError,
74	MessageSendEvent,
75};
76use crate::ln::onion_payment::{
77	check_incoming_htlc_cltv, create_fwd_pending_htlc_info, create_recv_pending_htlc_info,
78	decode_incoming_update_add_htlc_onion, invalid_payment_err_data, HopConnector, InboundHTLCErr,
79	NextPacketDetails,
80};
81use crate::ln::onion_utils::{self};
82use crate::ln::onion_utils::{
83	decode_fulfill_attribution_data, HTLCFailReason, LocalHTLCFailureReason,
84};
85use crate::ln::onion_utils::{process_fulfill_attribution_data, AttributionData};
86use crate::ln::our_peer_storage::{EncryptedOurPeerStorage, PeerStorageMonitorHolder};
87#[cfg(test)]
88use crate::ln::outbound_payment;
89use crate::ln::outbound_payment::{
90	OutboundPayments, PendingOutboundPayment, RetryableInvoiceRequest, SendAlongPathArgs,
91	StaleExpiration,
92};
93use crate::ln::types::ChannelId;
94use crate::offers::async_receive_offer_cache::AsyncReceiveOfferCache;
95use crate::offers::flow::{HeldHtlcReplyPath, InvreqResponseInstructions, OffersMessageFlow};
96use crate::offers::invoice::{
97	Bolt12Invoice, DerivedSigningPubkey, InvoiceBuilder, DEFAULT_RELATIVE_EXPIRY,
98};
99use crate::offers::invoice_error::InvoiceError;
100use crate::offers::invoice_request::InvoiceRequest;
101use crate::offers::nonce::Nonce;
102use crate::offers::offer::{Offer, OfferFromHrn};
103use crate::offers::parse::Bolt12SemanticError;
104use crate::offers::refund::Refund;
105use crate::offers::signer;
106use crate::offers::static_invoice::StaticInvoice;
107use crate::onion_message::async_payments::{
108	AsyncPaymentsMessage, AsyncPaymentsMessageHandler, HeldHtlcAvailable, OfferPaths,
109	OfferPathsRequest, ReleaseHeldHtlc, ServeStaticInvoice, StaticInvoicePersisted,
110};
111use crate::onion_message::dns_resolution::HumanReadableName;
112use crate::onion_message::messenger::{
113	MessageRouter, MessageSendInstructions, Responder, ResponseInstruction,
114};
115use crate::onion_message::offers::{OffersMessage, OffersMessageHandler};
116use crate::routing::router::{
117	BlindedTail, FixedRouter, InFlightHtlcs, Path, Payee, PaymentParameters, Route,
118	RouteParameters, RouteParametersConfig, Router,
119};
120use crate::sign::ecdsa::EcdsaChannelSigner;
121use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
122#[cfg(any(feature = "_test_utils", test))]
123use crate::types::features::Bolt11InvoiceFeatures;
124use crate::types::features::{
125	Bolt12InvoiceFeatures, ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures,
126};
127use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
128use crate::types::string::UntrustedString;
129use crate::util::config::{ChannelConfig, ChannelConfigOverrides, ChannelConfigUpdate, UserConfig};
130use crate::util::errors::APIError;
131use crate::util::logger::{Level, Logger, WithContext};
132use crate::util::scid_utils::fake_scid;
133use crate::util::ser::{
134	BigSize, FixedLengthReader, LengthReadable, MaybeReadable, Readable, ReadableArgs, VecWriter,
135	WithoutLength, Writeable, Writer,
136};
137use crate::util::wakers::{Future, Notifier};
138
139#[cfg(test)]
140use crate::blinded_path::payment::BlindedPaymentPath;
141
142#[cfg(feature = "dnssec")]
143use {
144	crate::blinded_path::message::DNSResolverContext,
145	crate::onion_message::dns_resolution::{
146		DNSResolverMessage, DNSResolverMessageHandler, DNSSECProof, DNSSECQuery,
147	},
148	crate::onion_message::messenger::Destination,
149};
150
151#[cfg(c_bindings)]
152use {
153	crate::offers::offer::OfferWithDerivedMetadataBuilder,
154	crate::offers::refund::RefundMaybeWithDerivedMetadataBuilder,
155};
156#[cfg(not(c_bindings))]
157use {
158	crate::offers::offer::{DerivedMetadata, OfferBuilder},
159	crate::offers::refund::RefundBuilder,
160	crate::onion_message::messenger::DefaultMessageRouter,
161	crate::routing::gossip::NetworkGraph,
162	crate::routing::router::DefaultRouter,
163	crate::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParameters},
164	crate::sign::KeysManager,
165};
166
167use lightning_invoice::{
168	Bolt11Invoice, Bolt11InvoiceDescription, CreationError, Currency, Description,
169	InvoiceBuilder as Bolt11InvoiceBuilder, SignOrCreationError, DEFAULT_EXPIRY_TIME,
170};
171
172use alloc::collections::{btree_map, BTreeMap};
173
174use crate::io;
175use crate::io::Read;
176use crate::prelude::*;
177use crate::sync::{Arc, FairRwLock, LockHeldState, LockTestExt, Mutex, RwLock, RwLockReadGuard};
178use bitcoin::hex::impl_fmt_traits;
179
180use core::borrow::Borrow;
181use core::cell::RefCell;
182use core::convert::Infallible;
183use core::ops::Deref;
184use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
185use core::time::Duration;
186use core::{cmp, mem};
187// Re-export this for use in the public API.
188#[cfg(any(test, feature = "_externalize_tests"))]
189pub(crate) use crate::ln::outbound_payment::PaymentSendFailure;
190pub use crate::ln::outbound_payment::{
191	Bolt11PaymentError, Bolt12PaymentError, ProbeSendFailure, RecipientOnionFields, Retry,
192	RetryableSendFailure,
193};
194use crate::ln::script::ShutdownScript;
195
196// We hold various information about HTLC relay in the HTLC objects in Channel itself:
197//
198// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
199// forward the HTLC with information it will give back to us when it does so, or if it should Fail
200// the HTLC with the relevant message for the Channel to handle giving to the remote peer.
201//
202// Once said HTLC is committed in the Channel, if the PendingHTLCStatus indicated Forward, the
203// Channel will return the PendingHTLCInfo back to us, and we will create an HTLCForwardInfo
204// with it to track where it came from (in case of onwards-forward error), waiting a random delay
205// before we forward it.
206//
207// We will then use HTLCForwardInfo's PendingHTLCInfo to construct an outbound HTLC, with a
208// relevant HTLCSource::PreviousHopData filled in to indicate where it came from (which we can use
209// to either fail-backwards or fulfill the HTLC backwards along the relevant path).
210// Alternatively, we can fill an outbound HTLC with a HTLCSource::OutboundRoute indicating this is
211// our payment, which we can use to decode errors or inform the user that the payment was sent.
212
213/// Information about where a received HTLC('s onion) has indicated the HTLC should go.
214#[derive(Clone)] // See FundedChannel::revoke_and_ack for why, tl;dr: Rust bug
215#[cfg_attr(test, derive(Debug, PartialEq))]
216pub enum PendingHTLCRouting {
217	/// An HTLC which should be forwarded on to another node.
218	Forward {
219		/// The onion which should be included in the forwarded HTLC, telling the next hop what to
220		/// do with the HTLC.
221		onion_packet: msgs::OnionPacket,
222		/// The short channel ID of the channel which we were instructed to forward this HTLC to.
223		///
224		/// This could be a real on-chain SCID, an SCID alias, or some other SCID which has meaning
225		/// to the receiving node, such as one returned from
226		/// [`ChannelManager::get_intercept_scid`] or [`ChannelManager::get_phantom_scid`].
227		short_channel_id: u64, // This should be NonZero<u64> eventually when we bump MSRV
228		/// Set if this HTLC is being forwarded within a blinded path.
229		blinded: Option<BlindedForward>,
230		/// The absolute CLTV of the inbound HTLC
231		incoming_cltv_expiry: Option<u32>,
232		/// Whether this HTLC should be held by our node until we receive a corresponding
233		/// [`ReleaseHeldHtlc`] onion message.
234		hold_htlc: Option<()>,
235	},
236	/// An HTLC which should be forwarded on to another Trampoline node.
237	TrampolineForward {
238		/// The onion shared secret we build with the sender (or the preceding Trampoline node) used
239		/// to decrypt the onion.
240		///
241		/// This is later used to encrypt failure packets in the event that the HTLC is failed.
242		incoming_shared_secret: [u8; 32],
243		/// The onion which should be included in the forwarded HTLC, telling the next hop what to
244		/// do with the HTLC.
245		onion_packet: msgs::TrampolineOnionPacket,
246		/// The node ID of the Trampoline node which we need to route this HTLC to.
247		node_id: PublicKey,
248		/// Set if this HTLC is being forwarded within a blinded path.
249		blinded: Option<BlindedForward>,
250		/// The absolute CLTV of the inbound HTLC
251		incoming_cltv_expiry: u32,
252	},
253	/// The onion indicates that this is a payment for an invoice (supposedly) generated by us.
254	///
255	/// Note that at this point, we have not checked that the invoice being paid was actually
256	/// generated by us, but rather it's claiming to pay an invoice of ours.
257	Receive {
258		/// Information about the amount the sender intended to pay and (potential) proof that this
259		/// is a payment for an invoice we generated. This proof of payment is is also used for
260		/// linking MPP parts of a larger payment.
261		payment_data: msgs::FinalOnionHopData,
262		/// Additional data which we (allegedly) instructed the sender to include in the onion.
263		///
264		/// For HTLCs received by LDK, this will ultimately be exposed in
265		/// [`Event::PaymentClaimable::onion_fields`] as
266		/// [`RecipientOnionFields::payment_metadata`].
267		payment_metadata: Option<Vec<u8>>,
268		/// The context of the payment included by the recipient in a blinded path, or `None` if a
269		/// blinded path was not used.
270		///
271		/// Used in part to determine the [`events::PaymentPurpose`].
272		payment_context: Option<PaymentContext>,
273		/// CLTV expiry of the received HTLC.
274		///
275		/// Used to track when we should expire pending HTLCs that go unclaimed.
276		incoming_cltv_expiry: u32,
277		/// If the onion had forwarding instructions to one of our phantom node SCIDs, this will
278		/// provide the onion shared secret used to decrypt the next level of forwarding
279		/// instructions.
280		phantom_shared_secret: Option<[u8; 32]>,
281		/// Custom TLVs which were set by the sender.
282		///
283		/// For HTLCs received by LDK, this will ultimately be exposed in
284		/// [`Event::PaymentClaimable::onion_fields`] as
285		/// [`RecipientOnionFields::custom_tlvs`].
286		custom_tlvs: Vec<(u64, Vec<u8>)>,
287		/// Set if this HTLC is the final hop in a multi-hop blinded path.
288		requires_blinded_error: bool,
289	},
290	/// The onion indicates that this is for payment to us but which contains the preimage for
291	/// claiming included, and is unrelated to any invoice we'd previously generated (aka a
292	/// "keysend" or "spontaneous" payment).
293	ReceiveKeysend {
294		/// Information about the amount the sender intended to pay and possibly a token to
295		/// associate MPP parts of a larger payment.
296		///
297		/// This will only be filled in if receiving MPP keysend payments is enabled, and it being
298		/// present will cause deserialization to fail on versions of LDK prior to 0.0.116.
299		payment_data: Option<msgs::FinalOnionHopData>,
300		/// Preimage for this onion payment. This preimage is provided by the sender and will be
301		/// used to settle the spontaneous payment.
302		payment_preimage: PaymentPreimage,
303		/// Additional data which we (allegedly) instructed the sender to include in the onion.
304		///
305		/// For HTLCs received by LDK, this will ultimately bubble back up as
306		/// [`RecipientOnionFields::payment_metadata`].
307		payment_metadata: Option<Vec<u8>>,
308		/// CLTV expiry of the received HTLC.
309		///
310		/// Used to track when we should expire pending HTLCs that go unclaimed.
311		incoming_cltv_expiry: u32,
312		/// Custom TLVs which were set by the sender.
313		///
314		/// For HTLCs received by LDK, these will ultimately bubble back up as
315		/// [`RecipientOnionFields::custom_tlvs`].
316		custom_tlvs: Vec<(u64, Vec<u8>)>,
317		/// Set if this HTLC is the final hop in a multi-hop blinded path.
318		requires_blinded_error: bool,
319		/// Set if we are receiving a keysend to a blinded path, meaning we created the
320		/// [`PaymentSecret`] and should verify it using our
321		/// [`NodeSigner::get_expanded_key`].
322		has_recipient_created_payment_secret: bool,
323		/// The [`InvoiceRequest`] associated with the [`Offer`] corresponding to this payment.
324		invoice_request: Option<InvoiceRequest>,
325		/// The context of the payment included by the recipient in a blinded path, or `None` if a
326		/// blinded path was not used.
327		///
328		/// Used in part to determine the [`events::PaymentPurpose`].
329		payment_context: Option<PaymentContext>,
330	},
331}
332
333/// Information used to forward or fail this HTLC that is being forwarded within a blinded path.
334#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
335pub struct BlindedForward {
336	/// The `blinding_point` that was set in the inbound [`msgs::UpdateAddHTLC`], or in the inbound
337	/// onion payload if we're the introduction node. Useful for calculating the next hop's
338	/// [`msgs::UpdateAddHTLC::blinding_point`].
339	pub inbound_blinding_point: PublicKey,
340	/// If needed, this determines how this HTLC should be failed backwards, based on whether we are
341	/// the introduction node.
342	pub failure: BlindedFailure,
343	/// Overrides the next hop's [`msgs::UpdateAddHTLC::blinding_point`]. Set if this HTLC is being
344	/// forwarded within a [`BlindedPaymentPath`] that was concatenated to another blinded path that
345	/// starts at the next hop.
346	///
347	/// [`BlindedPaymentPath`]: crate::blinded_path::payment::BlindedPaymentPath
348	pub next_blinding_override: Option<PublicKey>,
349}
350
351impl PendingHTLCRouting {
352	// Used to override the onion failure code and data if the HTLC is blinded.
353	fn blinded_failure(&self) -> Option<BlindedFailure> {
354		match self {
355			Self::Forward { blinded: Some(BlindedForward { failure, .. }), .. } => Some(*failure),
356			Self::TrampolineForward { blinded: Some(BlindedForward { failure, .. }), .. } => {
357				Some(*failure)
358			},
359			Self::Receive { requires_blinded_error: true, .. } => {
360				Some(BlindedFailure::FromBlindedNode)
361			},
362			Self::ReceiveKeysend { requires_blinded_error: true, .. } => {
363				Some(BlindedFailure::FromBlindedNode)
364			},
365			_ => None,
366		}
367	}
368
369	fn incoming_cltv_expiry(&self) -> Option<u32> {
370		match self {
371			Self::Forward { incoming_cltv_expiry, .. } => *incoming_cltv_expiry,
372			Self::TrampolineForward { incoming_cltv_expiry, .. } => Some(*incoming_cltv_expiry),
373			Self::Receive { incoming_cltv_expiry, .. } => Some(*incoming_cltv_expiry),
374			Self::ReceiveKeysend { incoming_cltv_expiry, .. } => Some(*incoming_cltv_expiry),
375		}
376	}
377
378	/// Whether this HTLC should be held by our node until we receive a corresponding
379	/// [`ReleaseHeldHtlc`] onion message.
380	pub(super) fn should_hold_htlc(&self) -> bool {
381		match self {
382			Self::Forward { hold_htlc: Some(()), .. } => true,
383			_ => false,
384		}
385	}
386}
387
388/// Information about an incoming HTLC, including the [`PendingHTLCRouting`] describing where it
389/// should go next.
390#[derive(Clone)] // See FundedChannel::revoke_and_ack for why, tl;dr: Rust bug
391#[cfg_attr(test, derive(Debug, PartialEq))]
392pub struct PendingHTLCInfo {
393	/// Further routing details based on whether the HTLC is being forwarded or received.
394	pub routing: PendingHTLCRouting,
395	/// The onion shared secret we build with the sender used to decrypt the onion.
396	///
397	/// This is later used to encrypt failure packets in the event that the HTLC is failed.
398	pub incoming_shared_secret: [u8; 32],
399	/// Hash of the payment preimage, to lock the payment until the receiver releases the preimage.
400	pub payment_hash: PaymentHash,
401	/// Amount received in the incoming HTLC.
402	///
403	/// This field was added in LDK 0.0.113 and will be `None` for objects written by prior
404	/// versions.
405	pub incoming_amt_msat: Option<u64>,
406	/// The amount the sender indicated should be forwarded on to the next hop or amount the sender
407	/// intended for us to receive for received payments.
408	///
409	/// If the received amount is less than this for received payments, an intermediary hop has
410	/// attempted to steal some of our funds and we should fail the HTLC (the sender should retry
411	/// it along another path).
412	///
413	/// Because nodes can take less than their required fees, and because senders may wish to
414	/// improve their own privacy, this amount may be less than [`Self::incoming_amt_msat`] for
415	/// received payments. In such cases, recipients must handle this HTLC as if it had received
416	/// [`Self::outgoing_amt_msat`].
417	pub outgoing_amt_msat: u64,
418	/// The CLTV the sender has indicated we should set on the forwarded HTLC (or has indicated
419	/// should have been set on the received HTLC for received payments).
420	pub outgoing_cltv_value: u32,
421	/// The fee taken for this HTLC in addition to the standard protocol HTLC fees.
422	///
423	/// If this is a payment for forwarding, this is the fee we are taking before forwarding the
424	/// HTLC.
425	///
426	/// If this is a received payment, this is the fee that our counterparty took.
427	///
428	/// This is used to allow LSPs to take fees as a part of payments, without the sender having to
429	/// shoulder them.
430	pub skimmed_fee_msat: Option<u64>,
431}
432
433#[derive(Clone)] // See FundedChannel::revoke_and_ack for why, tl;dr: Rust bug
434pub(super) enum HTLCFailureMsg {
435	Relay(msgs::UpdateFailHTLC),
436	Malformed(msgs::UpdateFailMalformedHTLC),
437}
438
439/// Stores whether we can't forward an HTLC or relevant forwarding info
440#[derive(Clone)] // See FundedChannel::revoke_and_ack for why, tl;dr: Rust bug
441pub(super) enum PendingHTLCStatus {
442	Forward(PendingHTLCInfo),
443	Fail(HTLCFailureMsg),
444}
445
446#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
447pub(super) struct PendingAddHTLCInfo {
448	pub(super) forward_info: PendingHTLCInfo,
449
450	// These fields are produced in `forward_htlcs()` and consumed in
451	// `process_pending_htlc_forwards()` for constructing the
452	// `HTLCSource::PreviousHopData` for failed and forwarded
453	// HTLCs.
454	//
455	// Note that this may be an outbound SCID alias for the associated channel.
456	prev_outbound_scid_alias: u64,
457	prev_htlc_id: u64,
458	prev_counterparty_node_id: PublicKey,
459	prev_channel_id: ChannelId,
460	prev_funding_outpoint: OutPoint,
461	prev_user_channel_id: u128,
462}
463
464impl PendingAddHTLCInfo {
465	fn htlc_previous_hop_data(&self) -> HTLCPreviousHopData {
466		let phantom_shared_secret = match self.forward_info.routing {
467			PendingHTLCRouting::Receive { phantom_shared_secret, .. } => phantom_shared_secret,
468			_ => None,
469		};
470		HTLCPreviousHopData {
471			prev_outbound_scid_alias: self.prev_outbound_scid_alias,
472			user_channel_id: Some(self.prev_user_channel_id),
473			outpoint: self.prev_funding_outpoint,
474			channel_id: self.prev_channel_id,
475			counterparty_node_id: Some(self.prev_counterparty_node_id),
476			htlc_id: self.prev_htlc_id,
477			incoming_packet_shared_secret: self.forward_info.incoming_shared_secret,
478			phantom_shared_secret,
479			blinded_failure: self.forward_info.routing.blinded_failure(),
480			cltv_expiry: self.forward_info.routing.incoming_cltv_expiry(),
481		}
482	}
483}
484
485#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
486pub(super) enum HTLCForwardInfo {
487	AddHTLC(PendingAddHTLCInfo),
488	FailHTLC { htlc_id: u64, err_packet: msgs::OnionErrorPacket },
489	FailMalformedHTLC { htlc_id: u64, failure_code: u16, sha256_of_onion: [u8; 32] },
490}
491
492/// Whether this blinded HTLC is being failed backwards by the introduction node or a blinded node,
493/// which determines the failure message that should be used.
494#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
495pub enum BlindedFailure {
496	/// This HTLC is being failed backwards by the introduction node, and thus should be failed with
497	/// [`msgs::UpdateFailHTLC`] and error code [`LocalHTLCFailureReason::InvalidOnionBlinding`].
498	FromIntroductionNode,
499	/// This HTLC is being failed backwards by a blinded node within the path, and thus should be
500	/// failed with [`msgs::UpdateFailMalformedHTLC`] and error code
501	/// [`LocalHTLCFailureReason::InvalidOnionBlinding`].
502	FromBlindedNode,
503}
504
505#[derive(PartialEq, Eq)]
506enum OnionPayload {
507	/// Indicates this incoming onion payload is for the purpose of paying an invoice.
508	Invoice {
509		/// This is only here for backwards-compatibility in serialization, in the future it can be
510		/// removed, breaking clients running 0.0.106 and earlier.
511		_legacy_hop_data: Option<msgs::FinalOnionHopData>,
512	},
513	/// Contains the payer-provided preimage.
514	Spontaneous(PaymentPreimage),
515}
516
517/// HTLCs that are to us and can be failed/claimed by the user
518#[derive(PartialEq, Eq)]
519struct ClaimableHTLC {
520	prev_hop: HTLCPreviousHopData,
521	cltv_expiry: u32,
522	/// The amount (in msats) of this MPP part
523	value: u64,
524	/// The amount (in msats) that the sender intended to be sent in this MPP
525	/// part (used for validating total MPP amount)
526	sender_intended_value: u64,
527	onion_payload: OnionPayload,
528	timer_ticks: u8,
529	/// The total value received for a payment (sum of all MPP parts if the payment is a MPP).
530	/// Gets set to the amount reported when pushing [`Event::PaymentClaimable`].
531	total_value_received: Option<u64>,
532	/// The sender intended sum total of all MPP parts specified in the onion
533	total_msat: u64,
534	/// The extra fee our counterparty skimmed off the top of this HTLC.
535	counterparty_skimmed_fee_msat: Option<u64>,
536}
537
538impl From<&ClaimableHTLC> for events::ClaimedHTLC {
539	fn from(val: &ClaimableHTLC) -> Self {
540		events::ClaimedHTLC {
541			counterparty_node_id: val.prev_hop.counterparty_node_id,
542			channel_id: val.prev_hop.channel_id,
543			user_channel_id: val.prev_hop.user_channel_id.unwrap_or(0),
544			cltv_expiry: val.cltv_expiry,
545			value_msat: val.value,
546			counterparty_skimmed_fee_msat: val.counterparty_skimmed_fee_msat.unwrap_or(0),
547		}
548	}
549}
550
551impl PartialOrd for ClaimableHTLC {
552	fn partial_cmp(&self, other: &ClaimableHTLC) -> Option<cmp::Ordering> {
553		Some(self.cmp(other))
554	}
555}
556impl Ord for ClaimableHTLC {
557	fn cmp(&self, other: &ClaimableHTLC) -> cmp::Ordering {
558		let res = (self.prev_hop.channel_id, self.prev_hop.htlc_id)
559			.cmp(&(other.prev_hop.channel_id, other.prev_hop.htlc_id));
560		if res.is_eq() {
561			debug_assert!(self == other, "ClaimableHTLCs from the same source should be identical");
562		}
563		res
564	}
565}
566
567/// A trait defining behavior for creating and verifing the HMAC for authenticating a given data.
568pub trait Verification {
569	/// Constructs an HMAC to include in [`OffersContext`] for the data along with the given
570	/// [`Nonce`].
571	fn hmac_for_offer_payment(
572		&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
573	) -> Hmac<Sha256>;
574
575	/// Authenticates the data using an HMAC and a [`Nonce`] taken from an [`OffersContext`].
576	fn verify_for_offer_payment(
577		&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
578	) -> Result<(), ()>;
579}
580
581impl Verification for UnauthenticatedReceiveTlvs {
582	fn hmac_for_offer_payment(
583		&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
584	) -> Hmac<Sha256> {
585		signer::hmac_for_payment_tlvs(self, nonce, expanded_key)
586	}
587
588	fn verify_for_offer_payment(
589		&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
590	) -> Result<(), ()> {
591		signer::verify_payment_tlvs(self, hmac, nonce, expanded_key)
592	}
593}
594
595/// A user-provided identifier in [`ChannelManager::send_payment`] used to uniquely identify
596/// a payment and ensure idempotency in LDK.
597///
598/// This is not exported to bindings users as we just use [u8; 32] directly
599#[derive(Hash, Copy, Clone, PartialEq, Eq)]
600pub struct PaymentId(pub [u8; Self::LENGTH]);
601
602impl PaymentId {
603	/// Number of bytes in the id.
604	pub const LENGTH: usize = 32;
605}
606
607impl PaymentId {
608	fn for_inbound_from_htlcs<I: Iterator<Item = (ChannelId, u64)>>(
609		key: &[u8; 32], htlcs: I,
610	) -> PaymentId {
611		let mut prev_pair = None;
612		let mut hasher = HmacEngine::new(key);
613		for (channel_id, htlc_id) in htlcs {
614			hasher.input(&channel_id.0);
615			hasher.input(&htlc_id.to_le_bytes());
616			if let Some(prev) = prev_pair {
617				debug_assert!(prev < (channel_id, htlc_id), "HTLCs should be sorted");
618			}
619			prev_pair = Some((channel_id, htlc_id));
620		}
621		PaymentId(Hmac::<Sha256>::from_engine(hasher).to_byte_array())
622	}
623}
624
625impl Borrow<[u8]> for PaymentId {
626	fn borrow(&self) -> &[u8] {
627		&self.0[..]
628	}
629}
630
631impl_fmt_traits! {
632	impl fmt_traits for PaymentId {
633		const LENGTH: usize = 32;
634	}
635}
636
637impl Writeable for PaymentId {
638	fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
639		self.0.write(w)
640	}
641}
642
643impl Readable for PaymentId {
644	fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
645		let buf: [u8; 32] = Readable::read(r)?;
646		Ok(PaymentId(buf))
647	}
648}
649
650/// An identifier used to uniquely identify an intercepted HTLC to LDK.
651///
652/// This is not exported to bindings users as we just use [u8; 32] directly
653#[derive(Hash, Copy, Clone, PartialEq, Eq)]
654pub struct InterceptId(pub [u8; 32]);
655
656impl InterceptId {
657	/// This intercept id corresponds to an HTLC that will be forwarded on
658	/// [`ChannelManager::forward_intercepted_htlc`].
659	fn from_incoming_shared_secret(ss: &[u8; 32]) -> Self {
660		Self(Sha256::hash(ss).to_byte_array())
661	}
662
663	/// This intercept id corresponds to an HTLC that will be forwarded on receipt of a
664	/// [`ReleaseHeldHtlc`] onion message.
665	fn from_htlc_id_and_chan_id(
666		htlc_id: u64, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
667	) -> Self {
668		let mut sha = Sha256::engine();
669		sha.input(&htlc_id.to_be_bytes());
670		sha.input(&channel_id.0);
671		sha.input(&counterparty_node_id.serialize());
672		Self(Sha256::from_engine(sha).to_byte_array())
673	}
674}
675
676impl Borrow<[u8]> for InterceptId {
677	fn borrow(&self) -> &[u8] {
678		&self.0[..]
679	}
680}
681impl_fmt_traits! {
682	impl fmt_traits for InterceptId {
683		const LENGTH: usize = 32;
684	}
685}
686
687impl Writeable for InterceptId {
688	fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
689		self.0.write(w)
690	}
691}
692
693impl Readable for InterceptId {
694	fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
695		let buf: [u8; 32] = Readable::read(r)?;
696		Ok(InterceptId(buf))
697	}
698}
699
700/// Optional arguments to [`ChannelManager::pay_for_offer`]
701#[cfg_attr(
702	feature = "dnssec",
703	doc = "and [`ChannelManager::pay_for_offer_from_human_readable_name`]"
704)]
705/// .
706///
707/// These fields will often not need to be set, and the provided [`Self::default`] can be used.
708pub struct OptionalOfferPaymentParams {
709	/// A note that is communicated to the recipient about this payment via
710	/// [`InvoiceRequest::payer_note`].
711	pub payer_note: Option<String>,
712	/// Pathfinding options which tweak how the path is constructed to the recipient.
713	pub route_params_config: RouteParametersConfig,
714	/// The number of tries or time during which we'll retry this payment if some paths to the
715	/// recipient fail.
716	///
717	/// Once the retry limit is reached, further path failures will not be retried and the payment
718	/// will ultimately fail once all pending paths have failed (generating an
719	/// [`Event::PaymentFailed`]).
720	pub retry_strategy: Retry,
721}
722
723impl Default for OptionalOfferPaymentParams {
724	fn default() -> Self {
725		Self {
726			payer_note: None,
727			route_params_config: Default::default(),
728			#[cfg(feature = "std")]
729			retry_strategy: Retry::Timeout(core::time::Duration::from_secs(2)),
730			#[cfg(not(feature = "std"))]
731			retry_strategy: Retry::Attempts(3),
732		}
733	}
734}
735
736#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
737/// Uniquely describes an HTLC by its source. Just the guaranteed-unique subset of [`HTLCSource`].
738pub(crate) enum SentHTLCId {
739	PreviousHopData { prev_outbound_scid_alias: u64, htlc_id: u64 },
740	OutboundRoute { session_priv: [u8; SECRET_KEY_SIZE] },
741}
742impl SentHTLCId {
743	pub(crate) fn from_source(source: &HTLCSource) -> Self {
744		match source {
745			HTLCSource::PreviousHopData(hop_data) => Self::PreviousHopData {
746				prev_outbound_scid_alias: hop_data.prev_outbound_scid_alias,
747				htlc_id: hop_data.htlc_id,
748			},
749			HTLCSource::OutboundRoute { session_priv, .. } => {
750				Self::OutboundRoute { session_priv: session_priv.secret_bytes() }
751			},
752		}
753	}
754}
755impl_writeable_tlv_based_enum!(SentHTLCId,
756	(0, PreviousHopData) => {
757		(0, prev_outbound_scid_alias, required),
758		(2, htlc_id, required),
759	},
760	(2, OutboundRoute) => {
761		(0, session_priv, required),
762	},
763);
764
765// (src_outbound_scid_alias, src_counterparty_node_id, src_funding_outpoint, src_chan_id, src_user_chan_id)
766type PerSourcePendingForward =
767	(u64, PublicKey, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>);
768
769type FailedHTLCForward = (HTLCSource, PaymentHash, HTLCFailReason, HTLCHandlingFailureType);
770
771mod fuzzy_channelmanager {
772	use super::*;
773
774	/// Tracks the inbound corresponding to an outbound HTLC
775	#[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
776	#[derive(Clone, Debug, PartialEq, Eq)]
777	pub enum HTLCSource {
778		PreviousHopData(HTLCPreviousHopData),
779		OutboundRoute {
780			path: Path,
781			session_priv: SecretKey,
782			/// Technically we can recalculate this from the route, but we cache it here to avoid
783			/// doing a double-pass on route when we get a failure back
784			first_hop_htlc_msat: u64,
785			payment_id: PaymentId,
786			/// The BOLT12 invoice associated with this payment, if any. This is stored here to ensure
787			/// we can provide proof-of-payment details in payment claim events even after a restart
788			/// with a stale ChannelManager state.
789			bolt12_invoice: Option<PaidBolt12Invoice>,
790		},
791	}
792
793	/// Tracks the inbound corresponding to an outbound HTLC
794	#[derive(Clone, Debug, Hash, PartialEq, Eq)]
795	pub struct HTLCPreviousHopData {
796		pub prev_outbound_scid_alias: u64,
797		pub user_channel_id: Option<u128>,
798		pub htlc_id: u64,
799		pub incoming_packet_shared_secret: [u8; 32],
800		pub phantom_shared_secret: Option<[u8; 32]>,
801		pub blinded_failure: Option<BlindedFailure>,
802		pub channel_id: ChannelId,
803
804		// These fields are consumed by `claim_funds_from_hop()` when updating a force-closed backwards
805		// channel with a preimage provided by the forward channel.
806		pub outpoint: OutPoint,
807		pub counterparty_node_id: Option<PublicKey>,
808		/// Used to preserve our backwards channel by failing back in case an HTLC claim in the forward
809		/// channel remains unconfirmed for too long.
810		pub cltv_expiry: Option<u32>,
811	}
812}
813#[cfg(fuzzing)]
814pub use self::fuzzy_channelmanager::*;
815#[cfg(not(fuzzing))]
816pub(crate) use self::fuzzy_channelmanager::*;
817
818#[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
819impl core::hash::Hash for HTLCSource {
820	fn hash<H: core::hash::Hasher>(&self, hasher: &mut H) {
821		match self {
822			HTLCSource::PreviousHopData(prev_hop_data) => {
823				0u8.hash(hasher);
824				prev_hop_data.hash(hasher);
825			},
826			HTLCSource::OutboundRoute {
827				path,
828				session_priv,
829				payment_id,
830				first_hop_htlc_msat,
831				bolt12_invoice,
832			} => {
833				1u8.hash(hasher);
834				path.hash(hasher);
835				session_priv[..].hash(hasher);
836				payment_id.hash(hasher);
837				first_hop_htlc_msat.hash(hasher);
838				bolt12_invoice.hash(hasher);
839			},
840		}
841	}
842}
843impl HTLCSource {
844	#[cfg(any(test, all(ldk_test_vectors, feature = "grind_signatures")))]
845	pub fn dummy() -> Self {
846		HTLCSource::OutboundRoute {
847			path: Path { hops: Vec::new(), blinded_tail: None },
848			session_priv: SecretKey::from_slice(&[1; 32]).unwrap(),
849			first_hop_htlc_msat: 0,
850			payment_id: PaymentId([2; 32]),
851			bolt12_invoice: None,
852		}
853	}
854
855	/// Checks whether this HTLCSource could possibly match the given HTLC output in a commitment
856	/// transaction. Useful to ensure different datastructures match up.
857	pub(crate) fn possibly_matches_output(
858		&self, htlc: &super::chan_utils::HTLCOutputInCommitment,
859	) -> bool {
860		if let HTLCSource::OutboundRoute { first_hop_htlc_msat, .. } = self {
861			*first_hop_htlc_msat == htlc.amount_msat
862		} else {
863			// There's nothing we can check for forwarded HTLCs
864			true
865		}
866	}
867
868	/// Returns the CLTV expiry of the inbound HTLC (i.e. the source referred to by this object),
869	/// if the source was a forwarded HTLC and the HTLC was first forwarded on LDK 0.1.1 or later.
870	pub(crate) fn inbound_htlc_expiry(&self) -> Option<u32> {
871		match self {
872			Self::PreviousHopData(HTLCPreviousHopData { cltv_expiry, .. }) => *cltv_expiry,
873			_ => None,
874		}
875	}
876
877	pub(crate) fn static_invoice(&self) -> Option<StaticInvoice> {
878		match self {
879			Self::OutboundRoute {
880				bolt12_invoice: Some(PaidBolt12Invoice::StaticInvoice(inv)),
881				..
882			} => Some(inv.clone()),
883			_ => None,
884		}
885	}
886}
887
888/// This enum is used to specify which error data to send to peers when failing back an HTLC
889/// using [`ChannelManager::fail_htlc_backwards_with_reason`].
890///
891/// For more info on failure codes, see <https://github.com/lightning/bolts/blob/master/04-onion-routing.md#failure-messages>.
892#[derive(Clone, Copy)]
893pub enum FailureCode {
894	/// We had a temporary error processing the payment. Useful if no other error codes fit
895	/// and you want to indicate that the payer may want to retry.
896	TemporaryNodeFailure,
897	/// We have a required feature which was not in this onion. For example, you may require
898	/// some additional metadata that was not provided with this payment.
899	RequiredNodeFeatureMissing,
900	/// You may wish to use this when a `payment_preimage` is unknown, or the CLTV expiry of
901	/// the HTLC is too close to the current block height for safe handling.
902	/// Using this failure code in [`ChannelManager::fail_htlc_backwards_with_reason`] is
903	/// equivalent to calling [`ChannelManager::fail_htlc_backwards`].
904	IncorrectOrUnknownPaymentDetails,
905	/// We failed to process the payload after the onion was decrypted. You may wish to
906	/// use this when receiving custom HTLC TLVs with even type numbers that you don't recognize.
907	///
908	/// If available, the tuple data may include the type number and byte offset in the
909	/// decrypted byte stream where the failure occurred.
910	InvalidOnionPayload(Option<(u64, u16)>),
911}
912
913impl Into<LocalHTLCFailureReason> for FailureCode {
914	fn into(self) -> LocalHTLCFailureReason {
915		match self {
916			FailureCode::TemporaryNodeFailure => LocalHTLCFailureReason::TemporaryNodeFailure,
917			FailureCode::RequiredNodeFeatureMissing => LocalHTLCFailureReason::RequiredNodeFeature,
918			FailureCode::IncorrectOrUnknownPaymentDetails => {
919				LocalHTLCFailureReason::IncorrectPaymentDetails
920			},
921			FailureCode::InvalidOnionPayload(_) => LocalHTLCFailureReason::InvalidOnionPayload,
922		}
923	}
924}
925
926/// Error type returned across the peer_state mutex boundary. When an Err is generated for a
927/// Channel, we generally end up with a ChannelError::Close for which we have to close the channel
928/// immediately (ie with no further calls on it made). Thus, this step happens inside a
929/// peer_state lock. We then return the set of things that need to be done outside the lock in
930/// this struct and call handle_error!() on it.
931struct MsgHandleErrInternal {
932	err: msgs::LightningError,
933	closes_channel: bool,
934	shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
935	tx_abort: Option<msgs::TxAbort>,
936}
937impl MsgHandleErrInternal {
938	fn send_err_msg_no_close(err: String, channel_id: ChannelId) -> Self {
939		Self {
940			err: LightningError {
941				err: err.clone(),
942				action: msgs::ErrorAction::SendErrorMessage {
943					msg: msgs::ErrorMessage { channel_id, data: err },
944				},
945			},
946			closes_channel: false,
947			shutdown_finish: None,
948			tx_abort: None,
949		}
950	}
951
952	fn from_no_close(err: msgs::LightningError) -> Self {
953		Self { err, closes_channel: false, shutdown_finish: None, tx_abort: None }
954	}
955
956	fn from_finish_shutdown(
957		err: String, channel_id: ChannelId, shutdown_res: ShutdownResult,
958		channel_update: Option<msgs::ChannelUpdate>,
959	) -> Self {
960		let err_msg = msgs::ErrorMessage { channel_id, data: err.clone() };
961		let action = if shutdown_res.monitor_update.is_some() {
962			// We have a closing `ChannelMonitorUpdate`, which means the channel was funded and we
963			// should disconnect our peer such that we force them to broadcast their latest
964			// commitment upon reconnecting.
965			msgs::ErrorAction::DisconnectPeer { msg: Some(err_msg) }
966		} else {
967			msgs::ErrorAction::SendErrorMessage { msg: err_msg }
968		};
969		Self {
970			err: LightningError { err, action },
971			closes_channel: true,
972			shutdown_finish: Some((shutdown_res, channel_update)),
973			tx_abort: None,
974		}
975	}
976
977	fn from_chan_no_close(err: ChannelError, channel_id: ChannelId) -> Self {
978		let tx_abort = match &err {
979			&ChannelError::Abort(reason) => Some(reason.into_tx_abort_msg(channel_id)),
980			_ => None,
981		};
982		let err = match err {
983			ChannelError::Warn(msg) => LightningError {
984				err: msg.clone(),
985				action: msgs::ErrorAction::SendWarningMessage {
986					msg: msgs::WarningMessage { channel_id, data: msg },
987					log_level: Level::Warn,
988				},
989			},
990			ChannelError::WarnAndDisconnect(msg) => LightningError {
991				err: msg.clone(),
992				action: msgs::ErrorAction::DisconnectPeerWithWarning {
993					msg: msgs::WarningMessage { channel_id, data: msg },
994				},
995			},
996			ChannelError::Ignore(msg) => {
997				LightningError { err: msg, action: msgs::ErrorAction::IgnoreError }
998			},
999			ChannelError::Abort(reason) => {
1000				LightningError { err: reason.to_string(), action: msgs::ErrorAction::IgnoreError }
1001			},
1002			ChannelError::Close((msg, _)) | ChannelError::SendError(msg) => LightningError {
1003				err: msg.clone(),
1004				action: msgs::ErrorAction::SendErrorMessage {
1005					msg: msgs::ErrorMessage { channel_id, data: msg },
1006				},
1007			},
1008		};
1009		Self { err, closes_channel: false, shutdown_finish: None, tx_abort }
1010	}
1011
1012	fn dont_send_error_message(&mut self) {
1013		match &mut self.err.action {
1014			msgs::ErrorAction::DisconnectPeer { msg } => *msg = None,
1015			msgs::ErrorAction::SendErrorMessage { msg: _ } => {
1016				self.err.action = msgs::ErrorAction::IgnoreError;
1017			},
1018			_ => {},
1019		}
1020	}
1021
1022	fn closes_channel(&self) -> bool {
1023		self.closes_channel
1024	}
1025}
1026
1027/// For events which result in both a RevokeAndACK and a CommitmentUpdate, by default they should
1028/// be sent in the order they appear in the return value, however sometimes the order needs to be
1029/// variable at runtime (eg FundedChannel::channel_reestablish needs to re-send messages in the order
1030/// they were originally sent). In those cases, this enum is also returned.
1031#[derive(Clone, PartialEq, Debug)]
1032pub(super) enum RAACommitmentOrder {
1033	/// Send the CommitmentUpdate messages first
1034	CommitmentFirst,
1035	/// Send the RevokeAndACK message first
1036	RevokeAndACKFirst,
1037}
1038
1039/// Similar to scenarios used by [`RAACommitmentOrder`], this determines whether a `channel_ready`
1040/// message should be sent first (i.e., prior to a `commitment_update`) or after the initial
1041/// `commitment_update` and `tx_signatures` for channel funding.
1042pub(super) enum ChannelReadyOrder {
1043	/// Send `channel_ready` message first.
1044	ChannelReadyFirst,
1045	/// Send initial `commitment_update` and `tx_signatures` first.
1046	SignaturesFirst,
1047}
1048
1049/// Information about a payment which is currently being claimed.
1050#[derive(Clone, Debug, PartialEq, Eq)]
1051struct ClaimingPayment {
1052	amount_msat: u64,
1053	payment_purpose: events::PaymentPurpose,
1054	receiver_node_id: PublicKey,
1055	htlcs: Vec<events::ClaimedHTLC>,
1056	sender_intended_value: Option<u64>,
1057	onion_fields: Option<RecipientOnionFields>,
1058	payment_id: Option<PaymentId>,
1059	/// When we claim and generate a [`Event::PaymentClaimed`], we want to block any
1060	/// payment-preimage-removing RAA [`ChannelMonitorUpdate`]s until the [`Event::PaymentClaimed`]
1061	/// is handled, ensuring we can regenerate the event on restart. We pick a random channel to
1062	/// block and store it here.
1063	///
1064	/// Note that once we disallow downgrades to 0.1 we should be able to simply use
1065	/// [`Self::htlcs`] to generate this rather than storing it here (as we won't need the funding
1066	/// outpoint), allowing us to remove this field.
1067	durable_preimage_channel: Option<(OutPoint, PublicKey, ChannelId)>,
1068}
1069impl_writeable_tlv_based!(ClaimingPayment, {
1070	(0, amount_msat, required),
1071	(1, durable_preimage_channel, option),
1072	(2, payment_purpose, required),
1073	(4, receiver_node_id, required),
1074	(5, htlcs, optional_vec),
1075	(7, sender_intended_value, option),
1076	(9, onion_fields, option),
1077	(11, payment_id, option),
1078});
1079
1080struct ClaimablePayment {
1081	purpose: events::PaymentPurpose,
1082	onion_fields: Option<RecipientOnionFields>,
1083	htlcs: Vec<ClaimableHTLC>,
1084}
1085
1086impl ClaimablePayment {
1087	fn inbound_payment_id(&self, secret: &[u8; 32]) -> PaymentId {
1088		PaymentId::for_inbound_from_htlcs(
1089			secret,
1090			self.htlcs.iter().map(|htlc| (htlc.prev_hop.channel_id, htlc.prev_hop.htlc_id)),
1091		)
1092	}
1093
1094	/// Returns the inbound `(channel_id, user_channel_id)` pairs for all HTLCs associated with the payment.
1095	///
1096	/// Note: The `user_channel_id` will be `None` for HTLCs created using LDK version 0.0.117 or prior.
1097	fn receiving_channel_ids(&self) -> Vec<(ChannelId, Option<u128>)> {
1098		self.htlcs
1099			.iter()
1100			.map(|htlc| (htlc.prev_hop.channel_id, htlc.prev_hop.user_channel_id))
1101			.collect()
1102	}
1103}
1104
1105/// Represent the channel funding transaction type.
1106enum FundingType {
1107	/// This variant is useful when we want LDK to validate the funding transaction and
1108	/// broadcast it automatically.
1109	///
1110	/// This is the normal flow.
1111	Checked(Transaction),
1112	/// This variant is useful when we want LDK to validate the funding transaction and
1113	/// broadcast it manually.
1114	///
1115	/// Used in LSPS2 on a client_trusts_lsp model
1116	CheckedManualBroadcast(Transaction),
1117	/// This variant is useful when we want to loosen the validation checks and allow to
1118	/// manually broadcast the funding transaction, leaving the responsibility to the caller.
1119	///
1120	/// This is useful in cases of constructing the funding transaction as part of another
1121	/// flow and the caller wants to perform the validation and broadcasting. An example of such
1122	/// scenario could be when constructing the funding transaction as part of a Payjoin
1123	/// transaction.
1124	Unchecked(OutPoint),
1125}
1126
1127impl FundingType {
1128	fn txid(&self) -> Txid {
1129		match self {
1130			FundingType::Checked(tx) => tx.compute_txid(),
1131			FundingType::CheckedManualBroadcast(tx) => tx.compute_txid(),
1132			FundingType::Unchecked(outp) => outp.txid,
1133		}
1134	}
1135
1136	fn transaction_or_dummy(&self) -> Transaction {
1137		match self {
1138			FundingType::Checked(tx) => tx.clone(),
1139			FundingType::CheckedManualBroadcast(tx) => tx.clone(),
1140			FundingType::Unchecked(_) => Transaction {
1141				version: bitcoin::transaction::Version::TWO,
1142				lock_time: bitcoin::absolute::LockTime::ZERO,
1143				input: Vec::new(),
1144				output: Vec::new(),
1145			},
1146		}
1147	}
1148
1149	fn is_manual_broadcast(&self) -> bool {
1150		match self {
1151			FundingType::Checked(_) => false,
1152			FundingType::CheckedManualBroadcast(_) => true,
1153			FundingType::Unchecked(_) => true,
1154		}
1155	}
1156}
1157
1158/// Information about claimable or being-claimed payments
1159struct ClaimablePayments {
1160	/// Map from payment hash to the payment data and any HTLCs which are to us and can be
1161	/// failed/claimed by the user.
1162	///
1163	/// Note that, no consistency guarantees are made about the channels given here actually
1164	/// existing anymore by the time you go to read them!
1165	///
1166	/// When adding to the map, [`Self::pending_claiming_payments`] must also be checked to ensure
1167	/// we don't get a duplicate payment.
1168	claimable_payments: HashMap<PaymentHash, ClaimablePayment>,
1169
1170	/// Map from payment hash to the payment data for HTLCs which we have begun claiming, but which
1171	/// are waiting on a [`ChannelMonitorUpdate`] to complete in order to be surfaced to the user
1172	/// as an [`events::Event::PaymentClaimed`].
1173	pending_claiming_payments: HashMap<PaymentHash, ClaimingPayment>,
1174}
1175
1176impl ClaimablePayments {
1177	/// Moves a payment from [`Self::claimable_payments`] to [`Self::pending_claiming_payments`].
1178	///
1179	/// If `custom_tlvs_known` is false and custom even TLVs are set by the sender, the set of
1180	/// pending HTLCs will be returned in the `Err` variant of this method. They MUST then be
1181	/// failed by the caller as they will not be in either [`Self::claimable_payments`] or
1182	/// [`Self::pending_claiming_payments`].
1183	///
1184	/// If `custom_tlvs_known` is true, and a matching payment is found, it will always be moved.
1185	///
1186	/// If no payment is found, `Err(Vec::new())` is returned.
1187	#[rustfmt::skip]
1188	fn begin_claiming_payment<L: Deref, S: Deref>(
1189		&mut self, payment_hash: PaymentHash, node_signer: &S, logger: &L,
1190		inbound_payment_id_secret: &[u8; 32], custom_tlvs_known: bool,
1191	) -> Result<(Vec<ClaimableHTLC>, ClaimingPayment), Vec<ClaimableHTLC>>
1192		where L::Target: Logger, S::Target: NodeSigner,
1193	{
1194		match self.claimable_payments.remove(&payment_hash) {
1195			Some(payment) => {
1196				let mut receiver_node_id = node_signer.get_node_id(Recipient::Node)
1197					.expect("Failed to get node_id for node recipient");
1198				for htlc in payment.htlcs.iter() {
1199					if htlc.prev_hop.phantom_shared_secret.is_some() {
1200						let phantom_pubkey = node_signer.get_node_id(Recipient::PhantomNode)
1201							.expect("Failed to get node_id for phantom node recipient");
1202						receiver_node_id = phantom_pubkey;
1203						break;
1204					}
1205				}
1206
1207				if let Some(RecipientOnionFields { custom_tlvs, .. }) = &payment.onion_fields {
1208					if !custom_tlvs_known && custom_tlvs.iter().any(|(typ, _)| typ % 2 == 0) {
1209						log_info!(logger, "Rejecting payment with payment hash {} as we cannot accept payment with unknown even TLVs: {}",
1210							&payment_hash, log_iter!(custom_tlvs.iter().map(|(typ, _)| typ).filter(|typ| *typ % 2 == 0)));
1211						return Err(payment.htlcs);
1212					}
1213				}
1214
1215				let payment_id = payment.inbound_payment_id(inbound_payment_id_secret);
1216				let claiming_payment = self.pending_claiming_payments
1217					.entry(payment_hash)
1218					.and_modify(|_| {
1219						debug_assert!(false, "Shouldn't get a duplicate pending claim event ever");
1220						log_error!(logger, "Got a duplicate pending claimable event on payment hash {}! Please report this bug",
1221							&payment_hash);
1222					})
1223					.or_insert_with(|| {
1224						let htlcs = payment.htlcs.iter().map(events::ClaimedHTLC::from).collect();
1225						let sender_intended_value = payment.htlcs.first().map(|htlc| htlc.total_msat);
1226						// Pick an "arbitrary" channel to block RAAs on until the `PaymentSent`
1227						// event is processed, specifically the last channel to get claimed.
1228						let durable_preimage_channel = payment.htlcs.last().map_or(None, |htlc| {
1229							if let Some(node_id) = htlc.prev_hop.counterparty_node_id {
1230								Some((htlc.prev_hop.outpoint, node_id, htlc.prev_hop.channel_id))
1231							} else {
1232								None
1233							}
1234						});
1235						debug_assert!(durable_preimage_channel.is_some());
1236						ClaimingPayment {
1237							amount_msat: payment.htlcs.iter().map(|source| source.value).sum(),
1238							payment_purpose: payment.purpose,
1239							receiver_node_id,
1240							htlcs,
1241							sender_intended_value,
1242							onion_fields: payment.onion_fields,
1243							payment_id: Some(payment_id),
1244							durable_preimage_channel,
1245						}
1246					}).clone();
1247
1248				Ok((payment.htlcs, claiming_payment))
1249			},
1250			None => Err(Vec::new())
1251		}
1252	}
1253}
1254
1255/// Events which we process internally but cannot be processed immediately at the generation site
1256/// usually because we're running pre-full-init. They are handled immediately once we detect we are
1257/// running normally, and specifically must be processed before any other non-background
1258/// [`ChannelMonitorUpdate`]s are applied.
1259#[derive(Debug)]
1260enum BackgroundEvent {
1261	/// Handle a ChannelMonitorUpdate which may or may not close the channel and may unblock the
1262	/// channel to continue normal operation.
1263	///
1264	/// Any such events that exist in [`ChannelManager::pending_background_events`] will *also* be
1265	/// tracked in [`PeerState::in_flight_monitor_updates`].
1266	///
1267	/// Note that any such events are lost on shutdown, so in general they must be updates which
1268	/// are regenerated on startup.
1269	MonitorUpdateRegeneratedOnStartup {
1270		counterparty_node_id: PublicKey,
1271		funding_txo: OutPoint,
1272		channel_id: ChannelId,
1273		update: ChannelMonitorUpdate,
1274	},
1275	/// Some [`ChannelMonitorUpdate`] (s) completed before we were serialized but we still have
1276	/// them marked pending, thus we need to run any [`MonitorUpdateCompletionAction`] (s) pending
1277	/// on a channel.
1278	MonitorUpdatesComplete {
1279		counterparty_node_id: PublicKey,
1280		channel_id: ChannelId,
1281		highest_update_id_completed: u64,
1282	},
1283	/// A channel had blocked monitor updates waiting on startup. If the updates were blocked on
1284	/// an MPP claim blocker not written to disk, we may be able to unblock them now.
1285	///
1286	/// This event is never written to disk.
1287	AttemptUnblockMonitorUpdates { counterparty_node_id: PublicKey, channel_id: ChannelId },
1288}
1289
1290/// A pointer to a channel that is unblocked when an event is surfaced
1291#[derive(Debug)]
1292pub(crate) struct EventUnblockedChannel {
1293	counterparty_node_id: PublicKey,
1294	funding_txo: OutPoint,
1295	channel_id: ChannelId,
1296	blocking_action: RAAMonitorUpdateBlockingAction,
1297}
1298
1299impl Writeable for EventUnblockedChannel {
1300	fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
1301		self.counterparty_node_id.write(writer)?;
1302		self.funding_txo.write(writer)?;
1303		self.channel_id.write(writer)?;
1304		self.blocking_action.write(writer)
1305	}
1306}
1307
1308impl MaybeReadable for EventUnblockedChannel {
1309	fn read<R: Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
1310		let counterparty_node_id = Readable::read(reader)?;
1311		let funding_txo = Readable::read(reader)?;
1312		let channel_id = Readable::read(reader)?;
1313		let blocking_action = match RAAMonitorUpdateBlockingAction::read(reader)? {
1314			Some(blocking_action) => blocking_action,
1315			None => return Ok(None),
1316		};
1317		Ok(Some(EventUnblockedChannel {
1318			counterparty_node_id,
1319			funding_txo,
1320			channel_id,
1321			blocking_action,
1322		}))
1323	}
1324}
1325
1326#[derive(Debug)]
1327/// Note that these run after all *non-blocked* [`ChannelMonitorUpdate`]s have been persisted.
1328/// Thus, they're primarily useful for (and currently only used for) claims, where the
1329/// [`ChannelMonitorUpdate`] we care about is a preimage update, which bypass the monitor update
1330/// blocking logic entirely and can never be blocked.
1331pub(crate) enum MonitorUpdateCompletionAction {
1332	/// Indicates that a payment ultimately destined for us was claimed and we should emit an
1333	/// [`events::Event::PaymentClaimed`] to the user if we haven't yet generated such an event for
1334	/// this payment. Note that this is only best-effort. On restart it's possible such a duplicate
1335	/// event can be generated.
1336	PaymentClaimed {
1337		payment_hash: PaymentHash,
1338		/// A pending MPP claim which hasn't yet completed.
1339		///
1340		/// Not written to disk.
1341		pending_mpp_claim: Option<(PublicKey, ChannelId, PendingMPPClaimPointer)>,
1342	},
1343	/// Indicates an [`events::Event`] should be surfaced to the user and possibly resume the
1344	/// operation of another channel.
1345	///
1346	/// This is usually generated when we've forwarded an HTLC and want to block the outbound edge
1347	/// from completing a monitor update which removes the payment preimage until the inbound edge
1348	/// completes a monitor update containing the payment preimage. In that case, after the inbound
1349	/// edge completes, we will surface an [`Event::PaymentForwarded`] as well as unblock the
1350	/// outbound edge.
1351	EmitEventAndFreeOtherChannel {
1352		event: events::Event,
1353		downstream_counterparty_and_funding_outpoint: Option<EventUnblockedChannel>,
1354	},
1355	/// Indicates we should immediately resume the operation of another channel, unless there is
1356	/// some other reason why the channel is blocked. In practice this simply means immediately
1357	/// removing the [`RAAMonitorUpdateBlockingAction`] provided from the blocking set.
1358	///
1359	/// This is usually generated when we've forwarded an HTLC and want to block the outbound edge
1360	/// from completing a monitor update which removes the payment preimage until the inbound edge
1361	/// completes a monitor update containing the payment preimage. However, we use this variant
1362	/// instead of [`Self::EmitEventAndFreeOtherChannel`] when we discover that the claim was in
1363	/// fact duplicative and we simply want to resume the outbound edge channel immediately.
1364	///
1365	/// This variant should thus never be written to disk, as it is processed inline rather than
1366	/// stored for later processing.
1367	FreeOtherChannelImmediately {
1368		downstream_counterparty_node_id: PublicKey,
1369		blocking_action: RAAMonitorUpdateBlockingAction,
1370		downstream_channel_id: ChannelId,
1371	},
1372}
1373
1374impl_writeable_tlv_based_enum_upgradable!(MonitorUpdateCompletionAction,
1375	(0, PaymentClaimed) => {
1376		(0, payment_hash, required),
1377		(9999999999, pending_mpp_claim, (static_value, None)),
1378	},
1379	// Note that FreeOtherChannelImmediately should never be written - we were supposed to free
1380	// *immediately*. However, for simplicity we implement read/write here.
1381	(1, FreeOtherChannelImmediately) => {
1382		(0, downstream_counterparty_node_id, required),
1383		(4, blocking_action, upgradable_required),
1384		(5, downstream_channel_id, required),
1385	},
1386	(2, EmitEventAndFreeOtherChannel) => {
1387		(0, event, upgradable_required),
1388		// LDK prior to 0.0.116 did not have this field as the monitor update application order was
1389		// required by clients. If we downgrade to something prior to 0.0.116 this may result in
1390		// monitor updates which aren't properly blocked or resumed, however that's fine - we don't
1391		// support async monitor updates even in LDK 0.0.116 and once we do we'll require no
1392		// downgrades to prior versions.
1393		(1, downstream_counterparty_and_funding_outpoint, upgradable_option),
1394	},
1395);
1396
1397#[derive(Clone, Debug, PartialEq, Eq)]
1398pub(crate) struct PaymentCompleteUpdate {
1399	counterparty_node_id: PublicKey,
1400	channel_funding_outpoint: OutPoint,
1401	channel_id: ChannelId,
1402	htlc_id: SentHTLCId,
1403}
1404
1405impl_writeable_tlv_based!(PaymentCompleteUpdate, {
1406	(1, channel_funding_outpoint, required),
1407	(3, counterparty_node_id, required),
1408	(5, channel_id, required),
1409	(7, htlc_id, required),
1410});
1411
1412#[derive(Clone, Debug, PartialEq, Eq)]
1413pub(crate) enum EventCompletionAction {
1414	ReleaseRAAChannelMonitorUpdate {
1415		counterparty_node_id: PublicKey,
1416		// Was required until LDK 0.2. Always filled in as `Some`.
1417		channel_funding_outpoint: Option<OutPoint>,
1418		channel_id: ChannelId,
1419	},
1420
1421	/// When a payment's resolution is communicated to the downstream logic via
1422	/// [`Event::PaymentSent`] or [`Event::PaymentFailed`] we may want to mark the payment as
1423	/// fully-resolved in the [`ChannelMonitor`], which we do via this action.
1424	/// Note that this action will be dropped on downgrade to LDK prior to 0.2!
1425	ReleasePaymentCompleteChannelMonitorUpdate(PaymentCompleteUpdate),
1426}
1427impl_writeable_tlv_based_enum!(EventCompletionAction,
1428	(0, ReleaseRAAChannelMonitorUpdate) => {
1429		(0, channel_funding_outpoint, option),
1430		(2, counterparty_node_id, required),
1431		(3, channel_id, (default_value, {
1432			if channel_funding_outpoint.is_none() {
1433				Err(DecodeError::InvalidValue)?
1434			}
1435			ChannelId::v1_from_funding_outpoint(channel_funding_outpoint.unwrap())
1436		})),
1437	}
1438	{1, ReleasePaymentCompleteChannelMonitorUpdate} => (),
1439);
1440
1441/// The source argument which is passed to [`ChannelManager::claim_mpp_part`].
1442///
1443/// This is identical to [`MPPClaimHTLCSource`] except that [`Self::counterparty_node_id`] is an
1444/// `Option`, whereas it is required in [`MPPClaimHTLCSource`]. In the future, we should ideally
1445/// drop this and merge the two, however doing so may break upgrades for nodes which have pending
1446/// forwarded payments.
1447struct HTLCClaimSource {
1448	counterparty_node_id: PublicKey,
1449	funding_txo: OutPoint,
1450	channel_id: ChannelId,
1451	htlc_id: u64,
1452}
1453
1454impl From<&MPPClaimHTLCSource> for HTLCClaimSource {
1455	fn from(o: &MPPClaimHTLCSource) -> HTLCClaimSource {
1456		HTLCClaimSource {
1457			counterparty_node_id: o.counterparty_node_id,
1458			funding_txo: o.funding_txo,
1459			channel_id: o.channel_id,
1460			htlc_id: o.htlc_id,
1461		}
1462	}
1463}
1464
1465#[derive(Debug)]
1466pub(crate) struct PendingMPPClaim {
1467	channels_without_preimage: Vec<(PublicKey, ChannelId)>,
1468	channels_with_preimage: Vec<(PublicKey, ChannelId)>,
1469}
1470
1471#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1472/// The source of an HTLC which is being claimed as a part of an incoming payment. Each part is
1473/// tracked in [`ChannelMonitor`]s, so that it can be converted to an [`HTLCClaimSource`] for claim
1474/// replays on startup.
1475struct MPPClaimHTLCSource {
1476	counterparty_node_id: PublicKey,
1477	funding_txo: OutPoint,
1478	channel_id: ChannelId,
1479	htlc_id: u64,
1480}
1481
1482impl_writeable_tlv_based!(MPPClaimHTLCSource, {
1483	(0, counterparty_node_id, required),
1484	(2, funding_txo, required),
1485	(4, channel_id, required),
1486	(6, htlc_id, required),
1487});
1488
1489#[derive(Clone, Debug, PartialEq, Eq)]
1490/// When we're claiming a(n MPP) payment, we want to store information about that payment in the
1491/// [`ChannelMonitor`] so that we can replay the claim without any information from the
1492/// [`ChannelManager`] at all. This struct stores that information with enough to replay claims
1493/// against all MPP parts as well as generate an [`Event::PaymentClaimed`].
1494pub(crate) struct PaymentClaimDetails {
1495	mpp_parts: Vec<MPPClaimHTLCSource>,
1496	/// Use [`ClaimingPayment`] as a stable source of all the fields we need to generate the
1497	/// [`Event::PaymentClaimed`].
1498	claiming_payment: ClaimingPayment,
1499}
1500
1501impl_writeable_tlv_based!(PaymentClaimDetails, {
1502	(0, mpp_parts, required_vec),
1503	(2, claiming_payment, required),
1504});
1505
1506#[derive(Clone)]
1507pub(crate) struct PendingMPPClaimPointer(Arc<Mutex<PendingMPPClaim>>);
1508
1509impl PartialEq for PendingMPPClaimPointer {
1510	fn eq(&self, o: &Self) -> bool {
1511		Arc::ptr_eq(&self.0, &o.0)
1512	}
1513}
1514impl Eq for PendingMPPClaimPointer {}
1515
1516impl core::fmt::Debug for PendingMPPClaimPointer {
1517	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
1518		self.0.lock().unwrap().fmt(f)
1519	}
1520}
1521
1522#[derive(Clone, PartialEq, Eq, Debug)]
1523/// If something is blocked on the completion of an RAA-generated [`ChannelMonitorUpdate`] we track
1524/// the blocked action here. See enum variants for more info.
1525pub(crate) enum RAAMonitorUpdateBlockingAction {
1526	/// A forwarded payment was claimed. We block the downstream channel completing its monitor
1527	/// update which removes the HTLC preimage until the upstream channel has gotten the preimage
1528	/// durably to disk.
1529	ForwardedPaymentInboundClaim {
1530		/// The upstream channel ID (i.e. the inbound edge).
1531		channel_id: ChannelId,
1532		/// The HTLC ID on the inbound edge.
1533		htlc_id: u64,
1534	},
1535	/// We claimed an MPP payment across multiple channels. We have to block removing the payment
1536	/// preimage from any monitor until the last monitor is updated to contain the payment
1537	/// preimage. Otherwise we may not be able to replay the preimage on the monitor(s) that
1538	/// weren't updated on startup.
1539	///
1540	/// This variant is *not* written to disk, instead being inferred from [`ChannelMonitor`]
1541	/// state.
1542	ClaimedMPPPayment { pending_claim: PendingMPPClaimPointer },
1543}
1544
1545impl RAAMonitorUpdateBlockingAction {
1546	fn from_prev_hop_data(prev_hop: &HTLCPreviousHopData) -> Self {
1547		Self::ForwardedPaymentInboundClaim {
1548			channel_id: prev_hop.channel_id,
1549			htlc_id: prev_hop.htlc_id,
1550		}
1551	}
1552}
1553
1554impl_writeable_tlv_based_enum_upgradable!(RAAMonitorUpdateBlockingAction,
1555	(0, ForwardedPaymentInboundClaim) => { (0, channel_id, required), (2, htlc_id, required) },
1556	unread_variants: ClaimedMPPPayment
1557);
1558
1559impl Readable for Option<RAAMonitorUpdateBlockingAction> {
1560	fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
1561		Ok(RAAMonitorUpdateBlockingAction::read(reader)?)
1562	}
1563}
1564
1565/// State we hold per-peer.
1566pub(super) struct PeerState<SP: Deref>
1567where
1568	SP::Target: SignerProvider,
1569{
1570	/// `channel_id` -> `Channel`
1571	///
1572	/// Holds all channels where the peer is the counterparty.
1573	pub(super) channel_by_id: HashMap<ChannelId, Channel<SP>>,
1574	/// `temporary_channel_id` -> `InboundChannelRequest`.
1575	///
1576	/// When manual channel acceptance is enabled, this holds all unaccepted inbound channels where
1577	/// the peer is the counterparty. If the channel is accepted, then the entry in this table is
1578	/// removed, and an InboundV1Channel is created and placed in the `inbound_v1_channel_by_id` table. If
1579	/// the channel is rejected, then the entry is simply removed.
1580	pub(super) inbound_channel_request_by_id: HashMap<ChannelId, InboundChannelRequest>,
1581	/// The latest `InitFeatures` we heard from the peer.
1582	latest_features: InitFeatures,
1583	/// Messages to send to the peer - pushed to in the same lock that they are generated in (except
1584	/// for broadcast messages, where ordering isn't as strict).
1585	pub(super) pending_msg_events: Vec<MessageSendEvent>,
1586	/// Map from Channel IDs to pending [`ChannelMonitorUpdate`]s which have been passed to the
1587	/// user but which have not yet completed. We still keep the funding outpoint around to backfill
1588	/// the legacy TLV field to support downgrading.
1589	///
1590	/// Note that the channel may no longer exist. For example if the channel was closed but we
1591	/// later needed to claim an HTLC which is pending on-chain, we may generate a monitor update
1592	/// for a missing channel.
1593	///
1594	/// Note that any pending [`BackgroundEvent::MonitorUpdateRegeneratedOnStartup`]s which are
1595	/// sitting in [`ChannelManager::pending_background_events`] will *also* be tracked here. This
1596	/// avoids a race condition during [`ChannelManager::pending_background_events`] processing
1597	/// where we complete one [`ChannelMonitorUpdate`] (but there are more pending as background
1598	/// events) but we conclude all pending [`ChannelMonitorUpdate`]s have completed and its safe
1599	/// to run post-completion actions.
1600	in_flight_monitor_updates: BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
1601	/// Map from a specific channel to some action(s) that should be taken when all pending
1602	/// [`ChannelMonitorUpdate`]s for the channel complete updating.
1603	///
1604	/// Note that because we generally only have one entry here a HashMap is pretty overkill. A
1605	/// BTreeMap currently stores more than ten elements per leaf node, so even up to a few
1606	/// channels with a peer this will just be one allocation and will amount to a linear list of
1607	/// channels to walk, avoiding the whole hashing rigmarole.
1608	///
1609	/// Note that the channel may no longer exist. For example, if a channel was closed but we
1610	/// later needed to claim an HTLC which is pending on-chain, we may generate a monitor update
1611	/// for a missing channel. While a malicious peer could construct a second channel with the
1612	/// same `temporary_channel_id` (or final `channel_id` in the case of 0conf channels or prior
1613	/// to funding appearing on-chain), the downstream `ChannelMonitor` set is required to ensure
1614	/// duplicates do not occur, so such channels should fail without a monitor update completing.
1615	///
1616	/// Note that these run after all *non-blocked* [`ChannelMonitorUpdate`]s have been persisted.
1617	/// Thus, they're primarily useful for (and currently only used for) claims, where the
1618	/// [`ChannelMonitorUpdate`] we care about is a preimage update, which bypass the monitor
1619	/// update blocking logic entirely and can never be blocked.
1620	monitor_update_blocked_actions: BTreeMap<ChannelId, Vec<MonitorUpdateCompletionAction>>,
1621	/// If another channel's [`ChannelMonitorUpdate`] needs to complete before a channel we have
1622	/// with this peer can complete an RAA [`ChannelMonitorUpdate`] (e.g. because the RAA update
1623	/// will remove a preimage that needs to be durably in an upstream channel first), we put an
1624	/// entry here to note that the channel with the key's ID is blocked on a set of actions.
1625	actions_blocking_raa_monitor_updates: BTreeMap<ChannelId, Vec<RAAMonitorUpdateBlockingAction>>,
1626	/// The latest [`ChannelMonitor::get_latest_update_id`] value for all closed channels as they
1627	/// exist on-disk/in our [`chain::Watch`].
1628	///
1629	/// If there are any updates pending in [`Self::in_flight_monitor_updates`] this will contain
1630	/// the highest `update_id` of all the pending in-flight updates (note that any pending updates
1631	/// not yet applied sitting in [`ChannelManager::pending_background_events`] will also be
1632	/// considered as they are also in [`Self::in_flight_monitor_updates`]).
1633	///
1634	/// Note that channels which were closed prior to LDK 0.1 may have a value here of `u64::MAX`.
1635	closed_channel_monitor_update_ids: BTreeMap<ChannelId, u64>,
1636	/// The peer is currently connected (i.e. we've seen a
1637	/// [`BaseMessageHandler::peer_connected`] and no corresponding
1638	/// [`BaseMessageHandler::peer_disconnected`].
1639	pub is_connected: bool,
1640	/// Holds the peer storage data for the channel partner on a per-peer basis.
1641	peer_storage: Vec<u8>,
1642}
1643
1644impl<SP: Deref> PeerState<SP>
1645where
1646	SP::Target: SignerProvider,
1647{
1648	/// Indicates that a peer meets the criteria where we're ok to remove it from our storage.
1649	/// If true is passed for `require_disconnected`, the function will return false if we haven't
1650	/// disconnected from the node already, ie. `PeerState::is_connected` is set to `true`.
1651	fn ok_to_remove(&self, require_disconnected: bool) -> bool {
1652		if require_disconnected && self.is_connected {
1653			return false;
1654		}
1655		for (_, updates) in self.in_flight_monitor_updates.values() {
1656			if !updates.is_empty() {
1657				return false;
1658			}
1659		}
1660		let chan_is_funded_or_outbound = |(_, channel): (_, &Channel<SP>)| {
1661			channel.is_funded() || channel.funding().is_outbound()
1662		};
1663		!self.channel_by_id.iter().any(chan_is_funded_or_outbound)
1664			&& self.monitor_update_blocked_actions.is_empty()
1665			&& self.closed_channel_monitor_update_ids.is_empty()
1666	}
1667
1668	// Returns a count of all channels we have with this peer, including unfunded channels.
1669	fn total_channel_count(&self) -> usize {
1670		self.channel_by_id.len() + self.inbound_channel_request_by_id.len()
1671	}
1672
1673	// Returns a bool indicating if the given `channel_id` matches a channel we have with this peer.
1674	fn has_channel(&self, channel_id: &ChannelId) -> bool {
1675		self.channel_by_id.contains_key(channel_id)
1676			|| self.inbound_channel_request_by_id.contains_key(channel_id)
1677	}
1678}
1679
1680#[derive(Clone)]
1681pub(super) enum OpenChannelMessage {
1682	V1(msgs::OpenChannel),
1683	V2(msgs::OpenChannelV2),
1684}
1685
1686pub(super) enum OpenChannelMessageRef<'a> {
1687	V1(&'a msgs::OpenChannel),
1688	V2(&'a msgs::OpenChannelV2),
1689}
1690
1691/// A not-yet-accepted inbound (from counterparty) channel. Once
1692/// accepted, the parameters will be used to construct a channel.
1693pub(super) struct InboundChannelRequest {
1694	/// The original OpenChannel message.
1695	pub open_channel_msg: OpenChannelMessage,
1696	/// The number of ticks remaining before the request expires.
1697	pub ticks_remaining: i32,
1698}
1699
1700/// The number of ticks that may elapse while we're waiting for an unaccepted inbound channel to be
1701/// accepted. An unaccepted channel that exceeds this limit will be abandoned.
1702const UNACCEPTED_INBOUND_CHANNEL_AGE_LIMIT_TICKS: i32 = 2;
1703
1704/// The number of blocks of historical feerate estimates we keep around and consider when deciding
1705/// to force-close a channel for having too-low fees. Also the number of blocks we have to see
1706/// after startup before we consider force-closing channels for having too-low fees.
1707pub(super) const FEERATE_TRACKING_BLOCKS: usize = 144;
1708
1709/// Stores a PaymentSecret and any other data we may need to validate an inbound payment is
1710/// actually ours and not some duplicate HTLC sent to us by a node along the route.
1711///
1712/// For users who don't want to bother doing their own payment preimage storage, we also store that
1713/// here.
1714///
1715/// Note that this struct will be removed entirely soon, in favor of storing no inbound payment data
1716/// and instead encoding it in the payment secret.
1717#[derive(Debug)]
1718struct PendingInboundPayment {
1719	/// The payment secret that the sender must use for us to accept this payment
1720	payment_secret: PaymentSecret,
1721	/// Time at which this HTLC expires - blocks with a header time above this value will result in
1722	/// this payment being removed.
1723	expiry_time: u64,
1724	/// Arbitrary identifier the user specifies (or not)
1725	user_payment_id: u64,
1726	// Other required attributes of the payment, optionally enforced:
1727	payment_preimage: Option<PaymentPreimage>,
1728	min_value_msat: Option<u64>,
1729}
1730
1731/// [`SimpleArcChannelManager`] is useful when you need a [`ChannelManager`] with a static lifetime, e.g.
1732/// when you're using `lightning-net-tokio` (since `tokio::spawn` requires parameters with static
1733/// lifetimes). Other times you can afford a reference, which is more efficient, in which case
1734/// [`SimpleRefChannelManager`] is the more appropriate type. Defining these type aliases prevents
1735/// issues such as overly long function definitions. Note that the `ChannelManager` can take any type
1736/// that implements [`NodeSigner`], [`EntropySource`], and [`SignerProvider`] for its keys manager,
1737/// or, respectively, [`Router`] for its router, but this type alias chooses the concrete types
1738/// of [`KeysManager`] and [`DefaultRouter`].
1739///
1740/// This is not exported to bindings users as type aliases aren't supported in most languages.
1741#[cfg(not(c_bindings))]
1742pub type SimpleArcChannelManager<M, T, F, L> = ChannelManager<
1743	Arc<M>,
1744	Arc<T>,
1745	Arc<KeysManager>,
1746	Arc<KeysManager>,
1747	Arc<KeysManager>,
1748	Arc<F>,
1749	Arc<
1750		DefaultRouter<
1751			Arc<NetworkGraph<Arc<L>>>,
1752			Arc<L>,
1753			Arc<KeysManager>,
1754			Arc<RwLock<ProbabilisticScorer<Arc<NetworkGraph<Arc<L>>>, Arc<L>>>>,
1755			ProbabilisticScoringFeeParameters,
1756			ProbabilisticScorer<Arc<NetworkGraph<Arc<L>>>, Arc<L>>,
1757		>,
1758	>,
1759	Arc<DefaultMessageRouter<Arc<NetworkGraph<Arc<L>>>, Arc<L>, Arc<KeysManager>>>,
1760	Arc<L>,
1761>;
1762
1763/// [`SimpleRefChannelManager`] is a type alias for a ChannelManager reference, and is the reference
1764/// counterpart to the [`SimpleArcChannelManager`] type alias. Use this type by default when you don't
1765/// need a ChannelManager with a static lifetime. You'll need a static lifetime in cases such as
1766/// usage of lightning-net-tokio (since `tokio::spawn` requires parameters with static lifetimes).
1767/// But if this is not necessary, using a reference is more efficient. Defining these type aliases
1768/// issues such as overly long function definitions. Note that the ChannelManager can take any type
1769/// that implements [`NodeSigner`], [`EntropySource`], and [`SignerProvider`] for its keys manager,
1770/// or, respectively, [`Router`]  for its router, but this type alias chooses the concrete types
1771/// of [`KeysManager`] and [`DefaultRouter`].
1772///
1773/// This is not exported to bindings users as type aliases aren't supported in most languages.
1774#[cfg(not(c_bindings))]
1775pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, M, T, F, L> = ChannelManager<
1776	&'a M,
1777	&'b T,
1778	&'c KeysManager,
1779	&'c KeysManager,
1780	&'c KeysManager,
1781	&'d F,
1782	&'e DefaultRouter<
1783		&'f NetworkGraph<&'g L>,
1784		&'g L,
1785		&'c KeysManager,
1786		&'h RwLock<ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>>,
1787		ProbabilisticScoringFeeParameters,
1788		ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>,
1789	>,
1790	&'i DefaultMessageRouter<&'f NetworkGraph<&'g L>, &'g L, &'c KeysManager>,
1791	&'g L,
1792>;
1793
1794/// A trivial trait which describes any [`ChannelManager`].
1795///
1796/// This is not exported to bindings users as general cover traits aren't useful in other
1797/// languages.
1798pub trait AChannelManager {
1799	/// A type implementing [`chain::Watch`].
1800	type Watch: chain::Watch<Self::Signer> + ?Sized;
1801	/// A type that may be dereferenced to [`Self::Watch`].
1802	type M: Deref<Target = Self::Watch>;
1803	/// A type implementing [`BroadcasterInterface`].
1804	type Broadcaster: BroadcasterInterface + ?Sized;
1805	/// A type that may be dereferenced to [`Self::Broadcaster`].
1806	type T: Deref<Target = Self::Broadcaster>;
1807	/// A type implementing [`EntropySource`].
1808	type EntropySource: EntropySource + ?Sized;
1809	/// A type that may be dereferenced to [`Self::EntropySource`].
1810	type ES: Deref<Target = Self::EntropySource>;
1811	/// A type implementing [`NodeSigner`].
1812	type NodeSigner: NodeSigner + ?Sized;
1813	/// A type that may be dereferenced to [`Self::NodeSigner`].
1814	type NS: Deref<Target = Self::NodeSigner>;
1815	/// A type implementing [`EcdsaChannelSigner`].
1816	type Signer: EcdsaChannelSigner + Sized;
1817	/// A type implementing [`SignerProvider`] for [`Self::Signer`].
1818	type SignerProvider: SignerProvider<EcdsaSigner = Self::Signer> + ?Sized;
1819	/// A type that may be dereferenced to [`Self::SignerProvider`].
1820	type SP: Deref<Target = Self::SignerProvider>;
1821	/// A type implementing [`FeeEstimator`].
1822	type FeeEstimator: FeeEstimator + ?Sized;
1823	/// A type that may be dereferenced to [`Self::FeeEstimator`].
1824	type F: Deref<Target = Self::FeeEstimator>;
1825	/// A type implementing [`Router`].
1826	type Router: Router + ?Sized;
1827	/// A type that may be dereferenced to [`Self::Router`].
1828	type R: Deref<Target = Self::Router>;
1829	/// A type implementing [`MessageRouter`].
1830	type MessageRouter: MessageRouter + ?Sized;
1831	/// A type that may be dereferenced to [`Self::MessageRouter`].
1832	type MR: Deref<Target = Self::MessageRouter>;
1833	/// A type implementing [`Logger`].
1834	type Logger: Logger + ?Sized;
1835	/// A type that may be dereferenced to [`Self::Logger`].
1836	type L: Deref<Target = Self::Logger>;
1837	/// Returns a reference to the actual [`ChannelManager`] object.
1838	fn get_cm(
1839		&self,
1840	) -> &ChannelManager<
1841		Self::M,
1842		Self::T,
1843		Self::ES,
1844		Self::NS,
1845		Self::SP,
1846		Self::F,
1847		Self::R,
1848		Self::MR,
1849		Self::L,
1850	>;
1851}
1852
1853impl<
1854		M: Deref,
1855		T: Deref,
1856		ES: Deref,
1857		NS: Deref,
1858		SP: Deref,
1859		F: Deref,
1860		R: Deref,
1861		MR: Deref,
1862		L: Deref,
1863	> AChannelManager for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
1864where
1865	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
1866	T::Target: BroadcasterInterface,
1867	ES::Target: EntropySource,
1868	NS::Target: NodeSigner,
1869	SP::Target: SignerProvider,
1870	F::Target: FeeEstimator,
1871	R::Target: Router,
1872	MR::Target: MessageRouter,
1873	L::Target: Logger,
1874{
1875	type Watch = M::Target;
1876	type M = M;
1877	type Broadcaster = T::Target;
1878	type T = T;
1879	type EntropySource = ES::Target;
1880	type ES = ES;
1881	type NodeSigner = NS::Target;
1882	type NS = NS;
1883	type Signer = <SP::Target as SignerProvider>::EcdsaSigner;
1884	type SignerProvider = SP::Target;
1885	type SP = SP;
1886	type FeeEstimator = F::Target;
1887	type F = F;
1888	type Router = R::Target;
1889	type R = R;
1890	type MessageRouter = MR::Target;
1891	type MR = MR;
1892	type Logger = L::Target;
1893	type L = L;
1894	fn get_cm(&self) -> &ChannelManager<M, T, ES, NS, SP, F, R, MR, L> {
1895		self
1896	}
1897}
1898
1899/// A lightning node's channel state machine and payment management logic, which facilitates
1900/// sending, forwarding, and receiving payments through lightning channels.
1901///
1902/// [`ChannelManager`] is parameterized by a number of components to achieve this.
1903/// - [`chain::Watch`] (typically [`ChainMonitor`]) for on-chain monitoring and enforcement of each
1904///   channel
1905/// - [`BroadcasterInterface`] for broadcasting transactions related to opening, funding, and
1906///   closing channels
1907/// - [`EntropySource`] for providing random data needed for cryptographic operations
1908/// - [`NodeSigner`] for cryptographic operations scoped to the node
1909/// - [`SignerProvider`] for providing signers whose operations are scoped to individual channels
1910/// - [`FeeEstimator`] to determine transaction fee rates needed to have a transaction mined in a
1911///   timely manner
1912/// - [`Router`] for finding payment paths when initiating and retrying payments
1913/// - [`MessageRouter`] for finding message paths when initiating and retrying onion messages
1914/// - [`Logger`] for logging operational information of varying degrees
1915///
1916/// Additionally, it implements the following traits:
1917/// - [`ChannelMessageHandler`] to handle off-chain channel activity from peers
1918/// - [`BaseMessageHandler`] to handle peer dis/connection and send messages to peers
1919/// - [`OffersMessageHandler`] for BOLT 12 message handling and sending
1920/// - [`EventsProvider`] to generate user-actionable [`Event`]s
1921/// - [`chain::Listen`] and [`chain::Confirm`] for notification of on-chain activity
1922///
1923/// Thus, [`ChannelManager`] is typically used to parameterize a [`MessageHandler`] and an
1924/// [`OnionMessenger`]. The latter is required to support BOLT 12 functionality.
1925///
1926/// # `ChannelManager` vs `ChannelMonitor`
1927///
1928/// It's important to distinguish between the *off-chain* management and *on-chain* enforcement of
1929/// lightning channels. [`ChannelManager`] exchanges messages with peers to manage the off-chain
1930/// state of each channel. During this process, it generates a [`ChannelMonitor`] for each channel
1931/// and a [`ChannelMonitorUpdate`] for each relevant change, notifying its parameterized
1932/// [`chain::Watch`] of them.
1933///
1934/// An implementation of [`chain::Watch`], such as [`ChainMonitor`], is responsible for aggregating
1935/// these [`ChannelMonitor`]s and applying any [`ChannelMonitorUpdate`]s to them. It then monitors
1936/// for any pertinent on-chain activity, enforcing claims as needed.
1937///
1938/// This division of off-chain management and on-chain enforcement allows for interesting node
1939/// setups. For instance, on-chain enforcement could be moved to a separate host or have added
1940/// redundancy, possibly as a watchtower. See [`chain::Watch`] for the relevant interface.
1941///
1942/// # Initialization
1943///
1944/// Use [`ChannelManager::new`] with the most recent [`BlockHash`] when creating a fresh instance.
1945/// Otherwise, if restarting, construct [`ChannelManagerReadArgs`] with the necessary parameters and
1946/// references to any deserialized [`ChannelMonitor`]s that were previously persisted. Use this to
1947/// deserialize the [`ChannelManager`] and feed it any new chain data since it was last online, as
1948/// detailed in the [`ChannelManagerReadArgs`] documentation.
1949///
1950/// ```
1951/// use bitcoin::BlockHash;
1952/// use bitcoin::network::Network;
1953/// use lightning::chain::BestBlock;
1954/// # use lightning::chain::channelmonitor::ChannelMonitor;
1955/// use lightning::ln::channelmanager::{ChainParameters, ChannelManager, ChannelManagerReadArgs};
1956/// # use lightning::routing::gossip::NetworkGraph;
1957/// use lightning::util::config::UserConfig;
1958/// use lightning::util::ser::ReadableArgs;
1959///
1960/// # fn read_channel_monitors() -> Vec<ChannelMonitor<lightning::sign::InMemorySigner>> { vec![] }
1961/// # fn example<
1962/// #     'a,
1963/// #     L: lightning::util::logger::Logger,
1964/// #     ES: lightning::sign::EntropySource,
1965/// #     S: for <'b> lightning::routing::scoring::LockableScore<'b, ScoreLookUp = SL>,
1966/// #     SL: lightning::routing::scoring::ScoreLookUp<ScoreParams = SP>,
1967/// #     SP: Sized,
1968/// #     R: lightning::io::Read,
1969/// # >(
1970/// #     fee_estimator: &dyn lightning::chain::chaininterface::FeeEstimator,
1971/// #     chain_monitor: &dyn lightning::chain::Watch<lightning::sign::InMemorySigner>,
1972/// #     tx_broadcaster: &dyn lightning::chain::chaininterface::BroadcasterInterface,
1973/// #     router: &lightning::routing::router::DefaultRouter<&NetworkGraph<&'a L>, &'a L, &ES, &S, SP, SL>,
1974/// #     message_router: &lightning::onion_message::messenger::DefaultMessageRouter<&NetworkGraph<&'a L>, &'a L, &ES>,
1975/// #     logger: &L,
1976/// #     entropy_source: &ES,
1977/// #     node_signer: &dyn lightning::sign::NodeSigner,
1978/// #     signer_provider: &lightning::sign::DynSignerProvider,
1979/// #     best_block: lightning::chain::BestBlock,
1980/// #     current_timestamp: u32,
1981/// #     mut reader: R,
1982/// # ) -> Result<(), lightning::ln::msgs::DecodeError> {
1983/// // Fresh start with no channels
1984/// let params = ChainParameters {
1985///     network: Network::Bitcoin,
1986///     best_block,
1987/// };
1988/// let config = UserConfig::default();
1989/// let channel_manager = ChannelManager::new(
1990///     fee_estimator, chain_monitor, tx_broadcaster, router, message_router, logger,
1991///     entropy_source, node_signer, signer_provider, config.clone(), params, current_timestamp,
1992/// );
1993///
1994/// // Restart from deserialized data
1995/// let mut channel_monitors = read_channel_monitors();
1996/// let args = ChannelManagerReadArgs::new(
1997///     entropy_source, node_signer, signer_provider, fee_estimator, chain_monitor, tx_broadcaster,
1998///     router, message_router, logger, config, channel_monitors.iter().collect(),
1999/// );
2000/// let (block_hash, channel_manager) =
2001///     <(BlockHash, ChannelManager<_, _, _, _, _, _, _, _, _>)>::read(&mut reader, args)?;
2002///
2003/// // Update the ChannelManager and ChannelMonitors with the latest chain data
2004/// // ...
2005///
2006/// // Move the monitors to the ChannelManager's chain::Watch parameter
2007/// for monitor in channel_monitors {
2008///     chain_monitor.watch_channel(monitor.channel_id(), monitor);
2009/// }
2010/// # Ok(())
2011/// # }
2012/// ```
2013///
2014/// # Operation
2015///
2016/// The following is required for [`ChannelManager`] to function properly:
2017/// - Handle messages from peers using its [`ChannelMessageHandler`] implementation (typically
2018///   called by [`PeerManager::read_event`] when processing network I/O)
2019/// - Process peer connections and send messages to peers obtained via its [`BaseMessageHandler`]
2020///   implementation (typically initiated when [`PeerManager::process_events`] is called)
2021/// - Feed on-chain activity using either its [`chain::Listen`] or [`chain::Confirm`] implementation
2022///   as documented by those traits
2023/// - Perform any periodic channel and payment checks by calling [`timer_tick_occurred`] roughly
2024///   every minute
2025/// - Persist to disk whenever [`get_and_clear_needs_persistence`] returns `true` using a
2026///   [`KVStoreSync`] implementation
2027/// - Handle [`Event`]s obtained via its [`EventsProvider`] implementation
2028///
2029/// The [`Future`] returned by [`get_event_or_persistence_needed_future`] is useful in determining
2030/// when the last two requirements need to be checked.
2031///
2032/// The [`lightning-block-sync`] and [`lightning-transaction-sync`] crates provide utilities that
2033/// simplify feeding in on-chain activity using the [`chain::Listen`] and [`chain::Confirm`] traits,
2034/// respectively. The remaining requirements can be met using the [`lightning-background-processor`]
2035/// crate. For languages other than Rust, the availability of similar utilities may vary.
2036///
2037/// # Channels
2038///
2039/// [`ChannelManager`]'s primary function involves managing a channel state. Without channels,
2040/// payments can't be sent. Use [`list_channels`] or [`list_usable_channels`] for a snapshot of the
2041/// currently open channels.
2042///
2043/// ```
2044/// # use lightning::ln::channelmanager::AChannelManager;
2045/// #
2046/// # fn example<T: AChannelManager>(channel_manager: T) {
2047/// # let channel_manager = channel_manager.get_cm();
2048/// let channels = channel_manager.list_usable_channels();
2049/// for details in channels {
2050///     println!("{:?}", details);
2051/// }
2052/// # }
2053/// ```
2054///
2055/// Each channel is identified using a [`ChannelId`], which will change throughout the channel's
2056/// life cycle. Additionally, channels are assigned a `user_channel_id`, which is given in
2057/// [`Event`]s associated with the channel and serves as a fixed identifier but is otherwise unused
2058/// by [`ChannelManager`].
2059///
2060/// ## Opening Channels
2061///
2062/// To an open a channel with a peer, call [`create_channel`]. This will initiate the process of
2063/// opening an outbound channel, which requires self-funding when handling
2064/// [`Event::FundingGenerationReady`].
2065///
2066/// ```
2067/// # use bitcoin::{ScriptBuf, Transaction};
2068/// # use bitcoin::secp256k1::PublicKey;
2069/// # use lightning::ln::channelmanager::AChannelManager;
2070/// # use lightning::events::{Event, EventsProvider};
2071/// #
2072/// # trait Wallet {
2073/// #     fn create_funding_transaction(
2074/// #         &self, _amount_sats: u64, _output_script: ScriptBuf
2075/// #     ) -> Transaction;
2076/// # }
2077/// #
2078/// # fn example<T: AChannelManager, W: Wallet>(channel_manager: T, wallet: W, peer_id: PublicKey) {
2079/// # let channel_manager = channel_manager.get_cm();
2080/// let value_sats = 1_000_000;
2081/// let push_msats = 10_000_000;
2082/// match channel_manager.create_channel(peer_id, value_sats, push_msats, 42, None, None) {
2083///     Ok(channel_id) => println!("Opening channel {}", channel_id),
2084///     Err(e) => println!("Error opening channel: {:?}", e),
2085/// }
2086///
2087/// // On the event processing thread once the peer has responded
2088/// channel_manager.process_pending_events(&|event| {
2089///     match event {
2090///         Event::FundingGenerationReady {
2091///             temporary_channel_id, counterparty_node_id, channel_value_satoshis, output_script,
2092///             user_channel_id, ..
2093///         } => {
2094///             assert_eq!(user_channel_id, 42);
2095///             let funding_transaction = wallet.create_funding_transaction(
2096///                 channel_value_satoshis, output_script
2097///             );
2098///             match channel_manager.funding_transaction_generated(
2099///                 temporary_channel_id, counterparty_node_id, funding_transaction
2100///             ) {
2101///                 Ok(()) => println!("Funding channel {}", temporary_channel_id),
2102///                 Err(e) => println!("Error funding channel {}: {:?}", temporary_channel_id, e),
2103///             }
2104///         },
2105///         Event::ChannelPending { channel_id, user_channel_id, former_temporary_channel_id, .. } => {
2106///             assert_eq!(user_channel_id, 42);
2107///             println!(
2108///                 "Channel {} now {} pending (funding transaction has been broadcasted)", channel_id,
2109///                 former_temporary_channel_id.unwrap()
2110///             );
2111///         },
2112///         Event::ChannelReady { channel_id, user_channel_id, .. } => {
2113///             assert_eq!(user_channel_id, 42);
2114///             println!("Channel {} ready", channel_id);
2115///         },
2116///         // ...
2117///     #     _ => {},
2118///     }
2119///     Ok(())
2120/// });
2121/// # }
2122/// ```
2123///
2124/// ## Accepting Channels
2125///
2126/// Inbound channels are initiated by peers and are automatically accepted unless [`ChannelManager`]
2127/// has [`UserConfig::manually_accept_inbound_channels`] set. In that case, the channel may be
2128/// either accepted or rejected when handling [`Event::OpenChannelRequest`].
2129///
2130/// ```
2131/// # use bitcoin::secp256k1::PublicKey;
2132/// # use lightning::ln::channelmanager::AChannelManager;
2133/// # use lightning::events::{Event, EventsProvider};
2134/// #
2135/// # fn is_trusted(counterparty_node_id: PublicKey) -> bool {
2136/// #     // ...
2137/// #     unimplemented!()
2138/// # }
2139/// #
2140/// # fn example<T: AChannelManager>(channel_manager: T) {
2141/// # let channel_manager = channel_manager.get_cm();
2142/// # let error_message = "Channel force-closed";
2143/// channel_manager.process_pending_events(&|event| {
2144///     match event {
2145///         Event::OpenChannelRequest { temporary_channel_id, counterparty_node_id, ..  } => {
2146///             if !is_trusted(counterparty_node_id) {
2147///                 match channel_manager.force_close_broadcasting_latest_txn(
2148///                     &temporary_channel_id, &counterparty_node_id, error_message.to_string()
2149///                 ) {
2150///                     Ok(()) => println!("Rejecting channel {}", temporary_channel_id),
2151///                     Err(e) => println!("Error rejecting channel {}: {:?}", temporary_channel_id, e),
2152///                 }
2153///                 return Ok(());
2154///             }
2155///
2156///             let user_channel_id = 43;
2157///             match channel_manager.accept_inbound_channel(
2158///                 &temporary_channel_id, &counterparty_node_id, user_channel_id, None
2159///             ) {
2160///                 Ok(()) => println!("Accepting channel {}", temporary_channel_id),
2161///                 Err(e) => println!("Error accepting channel {}: {:?}", temporary_channel_id, e),
2162///             }
2163///         },
2164///         // ...
2165///     #     _ => {},
2166///     }
2167///     Ok(())
2168/// });
2169/// # }
2170/// ```
2171///
2172/// ## Closing Channels
2173///
2174/// There are two ways to close a channel: either cooperatively using [`close_channel`] or
2175/// unilaterally using [`force_close_broadcasting_latest_txn`]. The former is ideal as it makes for
2176/// lower fees and immediate access to funds. However, the latter may be necessary if the
2177/// counterparty isn't behaving properly or has gone offline. [`Event::ChannelClosed`] is generated
2178/// once the channel has been closed successfully.
2179///
2180/// ```
2181/// # use bitcoin::secp256k1::PublicKey;
2182/// # use lightning::ln::types::ChannelId;
2183/// # use lightning::ln::channelmanager::AChannelManager;
2184/// # use lightning::events::{Event, EventsProvider};
2185/// #
2186/// # fn example<T: AChannelManager>(
2187/// #     channel_manager: T, channel_id: ChannelId, counterparty_node_id: PublicKey
2188/// # ) {
2189/// # let channel_manager = channel_manager.get_cm();
2190/// match channel_manager.close_channel(&channel_id, &counterparty_node_id) {
2191///     Ok(()) => println!("Closing channel {}", channel_id),
2192///     Err(e) => println!("Error closing channel {}: {:?}", channel_id, e),
2193/// }
2194///
2195/// // On the event processing thread
2196/// channel_manager.process_pending_events(&|event| {
2197///     match event {
2198///         Event::ChannelClosed { channel_id, user_channel_id, ..  } => {
2199///             assert_eq!(user_channel_id, 42);
2200///             println!("Channel {} closed", channel_id);
2201///         },
2202///         // ...
2203///     #     _ => {},
2204///     }
2205///     Ok(())
2206/// });
2207/// # }
2208/// ```
2209///
2210/// # Payments
2211///
2212/// [`ChannelManager`] is responsible for sending, forwarding, and receiving payments through its
2213/// channels. A payment is typically initiated from a [BOLT 11] invoice or a [BOLT 12] offer, though
2214/// spontaneous (i.e., keysend) payments are also possible. Incoming payments don't require
2215/// maintaining any additional state as [`ChannelManager`] can reconstruct the [`PaymentPreimage`]
2216/// from the [`PaymentSecret`]. Sending payments, however, require tracking in order to retry failed
2217/// HTLCs.
2218///
2219/// After a payment is initiated, it will appear in [`list_recent_payments`] until a short time
2220/// after either an [`Event::PaymentSent`] or [`Event::PaymentFailed`] is handled. Failed HTLCs
2221/// for a payment will be retried according to the payment's [`Retry`] strategy or until
2222/// [`abandon_payment`] is called.
2223///
2224/// ## BOLT 11 Invoices
2225///
2226/// The [`lightning-invoice`] crate is useful for creating BOLT 11 invoices. However, in order to
2227/// construct a [`Bolt11Invoice`] that is compatible with [`ChannelManager`], use
2228/// [`create_bolt11_invoice`]. This method serves as a convenience for building invoices with the
2229/// [`PaymentHash`] and [`PaymentSecret`] returned from [`create_inbound_payment`]. To provide your
2230/// own [`PaymentHash`], override the appropriate [`Bolt11InvoiceParameters`], which is equivalent
2231/// to using [`create_inbound_payment_for_hash`].
2232///
2233/// [`ChannelManager`] generates an [`Event::PaymentClaimable`] once the full payment has been
2234/// received. Call [`claim_funds`] to release the [`PaymentPreimage`], which in turn will result in
2235/// an [`Event::PaymentClaimed`].
2236///
2237/// ```
2238/// # use lightning::events::{Event, EventsProvider, PaymentPurpose};
2239/// # use lightning::ln::channelmanager::{AChannelManager, Bolt11InvoiceParameters};
2240/// #
2241/// # fn example<T: AChannelManager>(channel_manager: T) {
2242/// # let channel_manager = channel_manager.get_cm();
2243/// let params = Bolt11InvoiceParameters {
2244///     amount_msats: Some(10_000_000),
2245///     invoice_expiry_delta_secs: Some(3600),
2246///     ..Default::default()
2247/// };
2248/// let invoice = match channel_manager.create_bolt11_invoice(params) {
2249///     Ok(invoice) => {
2250///         println!("Creating invoice with payment hash {}", invoice.payment_hash());
2251///         invoice
2252///     },
2253///     Err(e) => panic!("Error creating invoice: {}", e),
2254/// };
2255///
2256/// // On the event processing thread
2257/// channel_manager.process_pending_events(&|event| {
2258///     match event {
2259///         Event::PaymentClaimable { payment_hash, purpose, .. } => match purpose {
2260///             PaymentPurpose::Bolt11InvoicePayment { payment_preimage: Some(payment_preimage), .. } => {
2261///                 assert_eq!(payment_hash.0, invoice.payment_hash().as_ref());
2262///                 println!("Claiming payment {}", payment_hash);
2263///                 channel_manager.claim_funds(payment_preimage);
2264///             },
2265///             PaymentPurpose::Bolt11InvoicePayment { payment_preimage: None, .. } => {
2266///                 println!("Unknown payment hash: {}", payment_hash);
2267///             },
2268///             PaymentPurpose::SpontaneousPayment(payment_preimage) => {
2269///                 assert_ne!(payment_hash.0, invoice.payment_hash().as_ref());
2270///                 println!("Claiming spontaneous payment {}", payment_hash);
2271///                 channel_manager.claim_funds(payment_preimage);
2272///             },
2273///             // ...
2274/// #           _ => {},
2275///         },
2276///         Event::PaymentClaimed { payment_hash, amount_msat, .. } => {
2277///             assert_eq!(payment_hash.0, invoice.payment_hash().as_ref());
2278///             println!("Claimed {} msats", amount_msat);
2279///         },
2280///         // ...
2281/// #       _ => {},
2282///     }
2283///     Ok(())
2284/// });
2285/// # }
2286/// ```
2287///
2288/// ```
2289/// # use bitcoin::hashes::Hash;
2290/// # use lightning::events::{Event, EventsProvider};
2291/// # use lightning::types::payment::PaymentHash;
2292/// # use lightning::ln::channelmanager::{AChannelManager, PaymentId, RecentPaymentDetails, Retry};
2293/// # use lightning::routing::router::RouteParametersConfig;
2294/// # use lightning_invoice::Bolt11Invoice;
2295/// #
2296/// # fn example<T: AChannelManager>(
2297/// #     channel_manager: T, invoice: &Bolt11Invoice, route_params_config: RouteParametersConfig,
2298/// #     retry: Retry
2299/// # ) {
2300/// # let channel_manager = channel_manager.get_cm();
2301/// # let payment_id = PaymentId([42; 32]);
2302/// # let payment_hash = PaymentHash((*invoice.payment_hash()).to_byte_array());
2303/// match channel_manager.pay_for_bolt11_invoice(
2304///     invoice, payment_id, None, route_params_config, retry
2305/// ) {
2306///     Ok(()) => println!("Sending payment with hash {}", payment_hash),
2307///     Err(e) => println!("Failed sending payment with hash {}: {:?}", payment_hash, e),
2308/// }
2309///
2310/// let expected_payment_id = payment_id;
2311/// let expected_payment_hash = payment_hash;
2312/// assert!(
2313///     channel_manager.list_recent_payments().iter().find(|details| matches!(
2314///         details,
2315///         RecentPaymentDetails::Pending {
2316///             payment_id: expected_payment_id,
2317///             payment_hash: expected_payment_hash,
2318///             ..
2319///         }
2320///     )).is_some()
2321/// );
2322///
2323/// // On the event processing thread
2324/// channel_manager.process_pending_events(&|event| {
2325///     match event {
2326///         Event::PaymentSent { payment_hash, .. } => println!("Paid {}", payment_hash),
2327///         Event::PaymentFailed { payment_hash: Some(payment_hash), .. } =>
2328///             println!("Failed paying {}", payment_hash),
2329///         // ...
2330///     #     _ => {},
2331///     }
2332///     Ok(())
2333/// });
2334/// # }
2335/// ```
2336///
2337/// ## BOLT 12 Offers
2338///
2339/// The [`offers`] module is useful for creating BOLT 12 offers. An [`Offer`] is a precursor to a
2340/// [`Bolt12Invoice`], which must first be requested by the payer. The interchange of these messages
2341/// as defined in the specification is handled by [`ChannelManager`] and its implementation of
2342/// [`OffersMessageHandler`]. However, this only works with an [`Offer`] created using a builder
2343/// returned by [`create_offer_builder`]. With this approach, BOLT 12 offers and invoices are
2344/// stateless just as BOLT 11 invoices are.
2345///
2346/// ```
2347/// # use lightning::events::{Event, EventsProvider, PaymentPurpose};
2348/// # use lightning::ln::channelmanager::AChannelManager;
2349/// # use lightning::offers::parse::Bolt12SemanticError;
2350/// # use lightning::routing::router::RouteParametersConfig;
2351/// #
2352/// # fn example<T: AChannelManager>(channel_manager: T) -> Result<(), Bolt12SemanticError> {
2353/// # let channel_manager = channel_manager.get_cm();
2354/// let offer = channel_manager
2355///     .create_offer_builder()?
2356/// # ;
2357/// # // Needed for compiling for c_bindings
2358/// # let builder: lightning::offers::offer::OfferBuilder<_, _> = offer.into();
2359/// # let offer = builder
2360///     .description("coffee".to_string())
2361///     .amount_msats(10_000_000)
2362///     .build()?;
2363/// let bech32_offer = offer.to_string();
2364///
2365/// // On the event processing thread
2366/// channel_manager.process_pending_events(&|event| {
2367///     match event {
2368///         Event::PaymentClaimable { payment_hash, purpose, .. } => match purpose {
2369///             PaymentPurpose::Bolt12OfferPayment { payment_preimage: Some(payment_preimage), .. } => {
2370///                 println!("Claiming payment {}", payment_hash);
2371///                 channel_manager.claim_funds(payment_preimage);
2372///             },
2373///             PaymentPurpose::Bolt12OfferPayment { payment_preimage: None, .. } => {
2374///                 println!("Unknown payment hash: {}", payment_hash);
2375///             }
2376/// #           _ => {},
2377///         },
2378///         Event::PaymentClaimed { payment_hash, amount_msat, .. } => {
2379///             println!("Claimed {} msats", amount_msat);
2380///         },
2381///         // ...
2382///     #     _ => {},
2383///     }
2384///     Ok(())
2385/// });
2386/// # Ok(())
2387/// # }
2388/// ```
2389///
2390/// Use [`pay_for_offer`] to initiated payment, which sends an [`InvoiceRequest`] for an [`Offer`]
2391/// and pays the [`Bolt12Invoice`] response.
2392///
2393/// ```
2394/// # use lightning::events::{Event, EventsProvider};
2395/// # use lightning::ln::channelmanager::{AChannelManager, PaymentId, RecentPaymentDetails};
2396/// # use lightning::offers::offer::Offer;
2397/// #
2398/// # fn example<T: AChannelManager>(
2399/// #     channel_manager: T, offer: &Offer, amount_msats: Option<u64>,
2400/// # ) {
2401/// # let channel_manager = channel_manager.get_cm();
2402/// let payment_id = PaymentId([42; 32]);
2403/// match channel_manager.pay_for_offer(
2404///     offer, amount_msats, payment_id, Default::default(),
2405/// ) {
2406///     Ok(()) => println!("Requesting invoice for offer"),
2407///     Err(e) => println!("Unable to request invoice for offer: {:?}", e),
2408/// }
2409///
2410/// // First the payment will be waiting on an invoice
2411/// let expected_payment_id = payment_id;
2412/// assert!(
2413///     channel_manager.list_recent_payments().iter().find(|details| matches!(
2414///         details,
2415///         RecentPaymentDetails::AwaitingInvoice { payment_id: expected_payment_id }
2416///     )).is_some()
2417/// );
2418///
2419/// // Once the invoice is received, a payment will be sent
2420/// assert!(
2421///     channel_manager.list_recent_payments().iter().find(|details| matches!(
2422///         details,
2423///         RecentPaymentDetails::Pending { payment_id: expected_payment_id, ..  }
2424///     )).is_some()
2425/// );
2426///
2427/// // On the event processing thread
2428/// channel_manager.process_pending_events(&|event| {
2429///     match event {
2430///         Event::PaymentSent { payment_id: Some(payment_id), .. } => println!("Paid {}", payment_id),
2431///         Event::PaymentFailed { payment_id, .. } => println!("Failed paying {}", payment_id),
2432///         // ...
2433///     #     _ => {},
2434///     }
2435///     Ok(())
2436/// });
2437/// # }
2438/// ```
2439///
2440/// ## BOLT 12 Refunds
2441///
2442/// A [`Refund`] is a request for an invoice to be paid. Like *paying* for an [`Offer`], *creating*
2443/// a [`Refund`] involves maintaining state since it represents a future outbound payment.
2444/// Therefore, use [`create_refund_builder`] when creating one, otherwise [`ChannelManager`] will
2445/// refuse to pay any corresponding [`Bolt12Invoice`] that it receives.
2446///
2447/// ```
2448/// # use core::time::Duration;
2449/// # use lightning::events::{Event, EventsProvider};
2450/// # use lightning::ln::channelmanager::{AChannelManager, PaymentId, RecentPaymentDetails, Retry};
2451/// # use lightning::offers::parse::Bolt12SemanticError;
2452/// # use lightning::routing::router::RouteParametersConfig;
2453/// #
2454/// # fn example<T: AChannelManager>(
2455/// #     channel_manager: T, amount_msats: u64, absolute_expiry: Duration, retry: Retry,
2456/// #     route_params_config: RouteParametersConfig
2457/// # ) -> Result<(), Bolt12SemanticError> {
2458/// # let channel_manager = channel_manager.get_cm();
2459/// let payment_id = PaymentId([42; 32]);
2460/// let refund = channel_manager
2461///     .create_refund_builder(
2462///         amount_msats, absolute_expiry, payment_id, retry, route_params_config
2463///     )?
2464/// # ;
2465/// # // Needed for compiling for c_bindings
2466/// # let builder: lightning::offers::refund::RefundBuilder<_> = refund.into();
2467/// # let refund = builder
2468///     .description("coffee".to_string())
2469///     .payer_note("refund for order 1234".to_string())
2470///     .build()?;
2471/// let bech32_refund = refund.to_string();
2472///
2473/// // First the payment will be waiting on an invoice
2474/// let expected_payment_id = payment_id;
2475/// assert!(
2476///     channel_manager.list_recent_payments().iter().find(|details| matches!(
2477///         details,
2478///         RecentPaymentDetails::AwaitingInvoice { payment_id: expected_payment_id }
2479///     )).is_some()
2480/// );
2481///
2482/// // Once the invoice is received, a payment will be sent
2483/// assert!(
2484///     channel_manager.list_recent_payments().iter().find(|details| matches!(
2485///         details,
2486///         RecentPaymentDetails::Pending { payment_id: expected_payment_id, ..  }
2487///     )).is_some()
2488/// );
2489///
2490/// // On the event processing thread
2491/// channel_manager.process_pending_events(&|event| {
2492///     match event {
2493///         Event::PaymentSent { payment_id: Some(payment_id), .. } => println!("Paid {}", payment_id),
2494///         Event::PaymentFailed { payment_id, .. } => println!("Failed paying {}", payment_id),
2495///         // ...
2496///     #     _ => {},
2497///     }
2498///     Ok(())
2499/// });
2500/// # Ok(())
2501/// # }
2502/// ```
2503///
2504/// Use [`request_refund_payment`] to send a [`Bolt12Invoice`] for receiving the refund. Similar to
2505/// *creating* an [`Offer`], this is stateless as it represents an inbound payment.
2506///
2507/// ```
2508/// # use lightning::events::{Event, EventsProvider, PaymentPurpose};
2509/// # use lightning::ln::channelmanager::AChannelManager;
2510/// # use lightning::offers::refund::Refund;
2511/// #
2512/// # fn example<T: AChannelManager>(channel_manager: T, refund: &Refund) {
2513/// # let channel_manager = channel_manager.get_cm();
2514/// let known_payment_hash = match channel_manager.request_refund_payment(refund) {
2515///     Ok(invoice) => {
2516///         let payment_hash = invoice.payment_hash();
2517///         println!("Requesting refund payment {}", payment_hash);
2518///         payment_hash
2519///     },
2520///     Err(e) => panic!("Unable to request payment for refund: {:?}", e),
2521/// };
2522///
2523/// // On the event processing thread
2524/// channel_manager.process_pending_events(&|event| {
2525///     match event {
2526///         Event::PaymentClaimable { payment_hash, purpose, .. } => match purpose {
2527///             PaymentPurpose::Bolt12RefundPayment { payment_preimage: Some(payment_preimage), .. } => {
2528///                 assert_eq!(payment_hash, known_payment_hash);
2529///                 println!("Claiming payment {}", payment_hash);
2530///                 channel_manager.claim_funds(payment_preimage);
2531///             },
2532///             PaymentPurpose::Bolt12RefundPayment { payment_preimage: None, .. } => {
2533///                 println!("Unknown payment hash: {}", payment_hash);
2534///             },
2535///             // ...
2536/// #           _ => {},
2537///     },
2538///     Event::PaymentClaimed { payment_hash, amount_msat, .. } => {
2539///         assert_eq!(payment_hash, known_payment_hash);
2540///         println!("Claimed {} msats", amount_msat);
2541///     },
2542///     // ...
2543/// #     _ => {},
2544///     }
2545///     Ok(())
2546/// });
2547/// # }
2548/// ```
2549///
2550/// # Persistence
2551///
2552/// Implements [`Writeable`] to write out all channel state to disk. Implies [`peer_disconnected`] for
2553/// all peers during write/read (though does not modify this instance, only the instance being
2554/// serialized). This will result in any channels which have not yet exchanged [`funding_created`] (i.e.,
2555/// called [`funding_transaction_generated`] for outbound channels) being closed.
2556///
2557/// Note that you can be a bit lazier about writing out `ChannelManager` than you can be with
2558/// [`ChannelMonitor`]. With [`ChannelMonitor`] you MUST durably write each
2559/// [`ChannelMonitorUpdate`] before returning from
2560/// [`chain::Watch::watch_channel`]/[`update_channel`] or before completing async writes. With
2561/// `ChannelManager`s, writing updates happens out-of-band (and will prevent any other
2562/// `ChannelManager` operations from occurring during the serialization process). If the
2563/// deserialized version is out-of-date compared to the [`ChannelMonitor`] passed by reference to
2564/// [`read`], those channels will be force-closed based on the `ChannelMonitor` state and no funds
2565/// will be lost (modulo on-chain transaction fees).
2566///
2567/// Note that the deserializer is only implemented for `(`[`BlockHash`]`, `[`ChannelManager`]`)`, which
2568/// tells you the last block hash which was connected. You should get the best block tip before using the manager.
2569/// See [`chain::Listen`] and [`chain::Confirm`] for more details.
2570///
2571/// # `ChannelUpdate` Messages
2572///
2573/// Note that `ChannelManager` is responsible for tracking liveness of its channels and generating
2574/// [`ChannelUpdate`] messages informing peers that the channel is temporarily disabled. To avoid
2575/// spam due to quick disconnection/reconnection, updates are not sent until the channel has been
2576/// offline for a full minute. In order to track this, you must call
2577/// [`timer_tick_occurred`] roughly once per minute, though it doesn't have to be perfect.
2578///
2579/// # DoS Mitigation
2580///
2581/// To avoid trivial DoS issues, `ChannelManager` limits the number of inbound connections and
2582/// inbound channels without confirmed funding transactions. This may result in nodes which we do
2583/// not have a channel with being unable to connect to us or open new channels with us if we have
2584/// many peers with unfunded channels.
2585///
2586/// Because it is an indication of trust, inbound channels which we've accepted as 0conf are
2587/// exempted from the count of unfunded channels. Similarly, outbound channels and connections are
2588/// never limited. Please ensure you limit the count of such channels yourself.
2589///
2590/// # Type Aliases
2591///
2592/// Rather than using a plain `ChannelManager`, it is preferable to use either a [`SimpleArcChannelManager`]
2593/// a [`SimpleRefChannelManager`], for conciseness. See their documentation for more details, but
2594/// essentially you should default to using a [`SimpleRefChannelManager`], and use a
2595/// [`SimpleArcChannelManager`] when you require a `ChannelManager` with a static lifetime, such as when
2596/// you're using lightning-net-tokio.
2597///
2598/// [`ChainMonitor`]: crate::chain::chainmonitor::ChainMonitor
2599/// [`MessageHandler`]: crate::ln::peer_handler::MessageHandler
2600/// [`OnionMessenger`]: crate::onion_message::messenger::OnionMessenger
2601/// [`PeerManager::read_event`]: crate::ln::peer_handler::PeerManager::read_event
2602/// [`PeerManager::process_events`]: crate::ln::peer_handler::PeerManager::process_events
2603/// [`timer_tick_occurred`]: Self::timer_tick_occurred
2604/// [`get_and_clear_needs_persistence`]: Self::get_and_clear_needs_persistence
2605/// [`KVStoreSync`]: crate::util::persist::KVStoreSync
2606/// [`get_event_or_persistence_needed_future`]: Self::get_event_or_persistence_needed_future
2607/// [`lightning-block-sync`]: https://docs.rs/lightning_block_sync/latest/lightning_block_sync
2608/// [`lightning-transaction-sync`]: https://docs.rs/lightning_transaction_sync/latest/lightning_transaction_sync
2609/// [`lightning-background-processor`]: https://docs.rs/lightning-background-processor/latest/lightning_background_processor
2610/// [`list_channels`]: Self::list_channels
2611/// [`list_usable_channels`]: Self::list_usable_channels
2612/// [`create_channel`]: Self::create_channel
2613/// [`close_channel`]: Self::force_close_broadcasting_latest_txn
2614/// [`force_close_broadcasting_latest_txn`]: Self::force_close_broadcasting_latest_txn
2615/// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
2616/// [BOLT 12]: https://github.com/rustyrussell/lightning-rfc/blob/guilt/offers/12-offer-encoding.md
2617/// [`list_recent_payments`]: Self::list_recent_payments
2618/// [`abandon_payment`]: Self::abandon_payment
2619/// [`lightning-invoice`]: https://docs.rs/lightning_invoice/latest/lightning_invoice
2620/// [`create_bolt11_invoice`]: Self::create_bolt11_invoice
2621/// [`create_inbound_payment`]: Self::create_inbound_payment
2622/// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
2623/// [`claim_funds`]: Self::claim_funds
2624/// [`send_payment`]: Self::send_payment
2625/// [`offers`]: crate::offers
2626/// [`create_offer_builder`]: Self::create_offer_builder
2627/// [`pay_for_offer`]: Self::pay_for_offer
2628/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
2629/// [`create_refund_builder`]: Self::create_refund_builder
2630/// [`request_refund_payment`]: Self::request_refund_payment
2631/// [`peer_disconnected`]: msgs::BaseMessageHandler::peer_disconnected
2632/// [`funding_created`]: msgs::FundingCreated
2633/// [`funding_transaction_generated`]: Self::funding_transaction_generated
2634/// [`BlockHash`]: bitcoin::hash_types::BlockHash
2635/// [`update_channel`]: chain::Watch::update_channel
2636/// [`ChannelUpdate`]: msgs::ChannelUpdate
2637/// [`read`]: ReadableArgs::read
2638//
2639// Lock order:
2640// The tree structure below illustrates the lock order requirements for the different locks of the
2641// `ChannelManager`. Locks can be held at the same time if they are on the same branch in the tree,
2642// and should then be taken in the order of the lowest to the highest level in the tree.
2643// Note that locks on different branches shall not be taken at the same time, as doing so will
2644// create a new lock order for those specific locks in the order they were taken.
2645//
2646// Lock order tree:
2647//
2648// `pending_offers_messages`
2649//
2650// `pending_async_payments_messages`
2651//
2652// `total_consistency_lock`
2653//  |
2654//  |__`forward_htlcs`
2655//  |
2656//  |__`pending_intercepted_htlcs`
2657//  |
2658//  |__`decode_update_add_htlcs`
2659//  |
2660//  |__`per_peer_state`
2661//      |
2662//      |__`claimable_payments`
2663//      |
2664//      |__`pending_outbound_payments` // This field's struct contains a map of pending outbounds
2665//         |
2666//         |__`peer_state`
2667//            |
2668//            |__`short_to_chan_info`
2669//            |
2670//            |__`outbound_scid_aliases`
2671//            |
2672//            |__`best_block`
2673//            |
2674//            |__`pending_events`
2675//               |
2676//               |__`pending_background_events`
2677//
2678pub struct ChannelManager<
2679	M: Deref,
2680	T: Deref,
2681	ES: Deref,
2682	NS: Deref,
2683	SP: Deref,
2684	F: Deref,
2685	R: Deref,
2686	MR: Deref,
2687	L: Deref,
2688> where
2689	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
2690	T::Target: BroadcasterInterface,
2691	ES::Target: EntropySource,
2692	NS::Target: NodeSigner,
2693	SP::Target: SignerProvider,
2694	F::Target: FeeEstimator,
2695	R::Target: Router,
2696	MR::Target: MessageRouter,
2697	L::Target: Logger,
2698{
2699	config: RwLock<UserConfig>,
2700	chain_hash: ChainHash,
2701	fee_estimator: LowerBoundedFeeEstimator<F>,
2702	chain_monitor: M,
2703	tx_broadcaster: T,
2704	router: R,
2705
2706	#[cfg(test)]
2707	pub(super) flow: OffersMessageFlow<MR, L>,
2708	#[cfg(not(test))]
2709	flow: OffersMessageFlow<MR, L>,
2710
2711	/// See `ChannelManager` struct-level documentation for lock order requirements.
2712	#[cfg(any(test, feature = "_test_utils"))]
2713	pub(super) best_block: RwLock<BestBlock>,
2714	#[cfg(not(any(test, feature = "_test_utils")))]
2715	/// See `ChannelManager` struct-level documentation for lock order requirements.
2716	best_block: RwLock<BestBlock>,
2717	pub(super) secp_ctx: Secp256k1<secp256k1::All>,
2718
2719	/// The session_priv bytes and retry metadata of outbound payments which are pending resolution.
2720	/// The authoritative state of these HTLCs resides either within Channels or ChannelMonitors
2721	/// (if the channel has been force-closed), however we track them here to prevent duplicative
2722	/// PaymentSent/PaymentPathFailed events. Specifically, in the case of a duplicative
2723	/// update_fulfill_htlc message after a reconnect, we may "claim" a payment twice.
2724	/// Additionally, because ChannelMonitors are often not re-serialized after connecting block(s)
2725	/// which may generate a claim event, we may receive similar duplicate claim/fail MonitorEvents
2726	/// after reloading from disk while replaying blocks against ChannelMonitors.
2727	///
2728	/// See `PendingOutboundPayment` documentation for more info.
2729	///
2730	/// See `ChannelManager` struct-level documentation for lock order requirements.
2731	pending_outbound_payments: OutboundPayments,
2732
2733	/// SCID/SCID Alias -> forward infos. Key of 0 means payments received.
2734	///
2735	/// Note that because we may have an SCID Alias as the key we can have two entries per channel,
2736	/// though in practice we probably won't be receiving HTLCs for a channel both via the alias
2737	/// and via the classic SCID.
2738	///
2739	/// Note that no consistency guarantees are made about the existence of a channel with the
2740	/// `short_channel_id` here, nor the `short_channel_id` in the `PendingHTLCInfo`!
2741	///
2742	/// See `ChannelManager` struct-level documentation for lock order requirements.
2743	#[cfg(test)]
2744	pub(super) forward_htlcs: Mutex<HashMap<u64, Vec<HTLCForwardInfo>>>,
2745	#[cfg(not(test))]
2746	forward_htlcs: Mutex<HashMap<u64, Vec<HTLCForwardInfo>>>,
2747	/// Storage for HTLCs that have been intercepted.
2748	///
2749	/// These HTLCs fall into two categories:
2750	/// 1. HTLCs that are bubbled up to the user and held until the invocation of
2751	///    [`ChannelManager::forward_intercepted_htlc`] or [`ChannelManager::fail_intercepted_htlc`]
2752	///    (or timeout)
2753	/// 2. HTLCs that are being held on behalf of an often-offline sender until receipt of a
2754	///    [`ReleaseHeldHtlc`] onion message from an often-offline recipient
2755	///
2756	/// See `ChannelManager` struct-level documentation for lock order requirements.
2757	pending_intercepted_htlcs: Mutex<HashMap<InterceptId, PendingAddHTLCInfo>>,
2758
2759	/// Outbound SCID Alias -> pending `update_add_htlc`s to decode.
2760	/// We use the scid alias because regular scids may change if a splice occurs.
2761	///
2762	/// Note that no consistency guarantees are made about the existence of a channel with the
2763	/// `short_channel_id` here, nor the `channel_id` in `UpdateAddHTLC`!
2764	///
2765	/// See `ChannelManager` struct-level documentation for lock order requirements.
2766	decode_update_add_htlcs: Mutex<HashMap<u64, Vec<msgs::UpdateAddHTLC>>>,
2767
2768	/// The sets of payments which are claimable or currently being claimed. See
2769	/// [`ClaimablePayments`]' individual field docs for more info.
2770	///
2771	/// See `ChannelManager` struct-level documentation for lock order requirements.
2772	claimable_payments: Mutex<ClaimablePayments>,
2773
2774	/// The set of outbound SCID aliases across all our channels, including unconfirmed channels
2775	/// and some closed channels which reached a usable state prior to being closed. This is used
2776	/// only to avoid duplicates, and is not persisted explicitly to disk, but rebuilt from the
2777	/// active channel list on load.
2778	///
2779	/// See `ChannelManager` struct-level documentation for lock order requirements.
2780	outbound_scid_aliases: Mutex<HashSet<u64>>,
2781
2782	/// SCIDs (and outbound SCID aliases) -> `counterparty_node_id`s and `channel_id`s.
2783	///
2784	/// Outbound SCID aliases are added here once the channel is available for normal use, with
2785	/// SCIDs being added once the funding transaction is confirmed at the channel's required
2786	/// confirmation depth.
2787	///
2788	/// Note that while this holds `counterparty_node_id`s and `channel_id`s, no consistency
2789	/// guarantees are made about the existence of a peer with the `counterparty_node_id` nor a
2790	/// channel with the `channel_id` in our other maps.
2791	///
2792	/// See `ChannelManager` struct-level documentation for lock order requirements.
2793	#[cfg(test)]
2794	pub(super) short_to_chan_info: FairRwLock<HashMap<u64, (PublicKey, ChannelId)>>,
2795	#[cfg(not(test))]
2796	short_to_chan_info: FairRwLock<HashMap<u64, (PublicKey, ChannelId)>>,
2797
2798	our_network_pubkey: PublicKey,
2799
2800	inbound_payment_key: inbound_payment::ExpandedKey,
2801
2802	/// LDK puts the [fake scids] that it generates into namespaces, to identify the type of an
2803	/// incoming payment. To make it harder for a third-party to identify the type of a payment,
2804	/// we encrypt the namespace identifier using these bytes.
2805	///
2806	/// [fake scids]: crate::util::scid_utils::fake_scid
2807	fake_scid_rand_bytes: [u8; 32],
2808
2809	/// When we send payment probes, we generate the [`PaymentHash`] based on this cookie secret
2810	/// and a random [`PaymentId`]. This allows us to discern probes from real payments, without
2811	/// keeping additional state.
2812	probing_cookie_secret: [u8; 32],
2813
2814	/// When generating [`PaymentId`]s for inbound payments, we HMAC the HTLCs with this secret.
2815	inbound_payment_id_secret: [u8; 32],
2816
2817	/// The highest block timestamp we've seen, which is usually a good guess at the current time.
2818	/// Assuming most miners are generating blocks with reasonable timestamps, this shouldn't be
2819	/// very far in the past, and can only ever be up to two hours in the future.
2820	highest_seen_timestamp: AtomicUsize,
2821
2822	/// The bulk of our storage. Currently the `per_peer_state` stores our channels on a per-peer
2823	/// basis, as well as the peer's latest features.
2824	///
2825	/// If we are connected to a peer we always at least have an entry here, even if no channels
2826	/// are currently open with that peer.
2827	///
2828	/// Because adding or removing an entry is rare, we usually take an outer read lock and then
2829	/// operate on the inner value freely. This opens up for parallel per-peer operation for
2830	/// channels.
2831	///
2832	/// Note that the same thread must never acquire two inner `PeerState` locks at the same time.
2833	///
2834	/// See `ChannelManager` struct-level documentation for lock order requirements.
2835	#[cfg(not(any(test, feature = "_test_utils")))]
2836	per_peer_state: FairRwLock<HashMap<PublicKey, Mutex<PeerState<SP>>>>,
2837	#[cfg(any(test, feature = "_test_utils"))]
2838	pub(super) per_peer_state: FairRwLock<HashMap<PublicKey, Mutex<PeerState<SP>>>>,
2839
2840	/// We only support using one of [`ChannelMonitorUpdateStatus::InProgress`] and
2841	/// [`ChannelMonitorUpdateStatus::Completed`] without restarting. Because the API does not
2842	/// otherwise directly enforce this, we enforce it in non-test builds here by storing which one
2843	/// is in use.
2844	#[cfg(not(any(test, feature = "_externalize_tests")))]
2845	monitor_update_type: AtomicUsize,
2846
2847	/// The set of events which we need to give to the user to handle. In some cases an event may
2848	/// require some further action after the user handles it (currently only blocking a monitor
2849	/// update from being handed to the user to ensure the included changes to the channel state
2850	/// are handled by the user before they're persisted durably to disk). In that case, the second
2851	/// element in the tuple is set to `Some` with further details of the action.
2852	///
2853	/// Note that events MUST NOT be removed from pending_events after deserialization, as they
2854	/// could be in the middle of being processed without the direct mutex held.
2855	///
2856	/// See `ChannelManager` struct-level documentation for lock order requirements.
2857	#[cfg(not(any(test, feature = "_test_utils")))]
2858	pending_events: Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>,
2859	#[cfg(any(test, feature = "_test_utils"))]
2860	pub(crate) pending_events: Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>,
2861
2862	/// A simple atomic flag to ensure only one task at a time can be processing events asynchronously.
2863	pending_events_processor: AtomicBool,
2864
2865	/// A simple atomic flag to ensure only one task at a time can be processing HTLC forwards via
2866	/// [`Self::process_pending_htlc_forwards`].
2867	pending_htlc_forwards_processor: AtomicBool,
2868
2869	/// If we are running during init (either directly during the deserialization method or in
2870	/// block connection methods which run after deserialization but before normal operation) we
2871	/// cannot provide the user with [`ChannelMonitorUpdate`]s through the normal update flow -
2872	/// prior to normal operation the user may not have loaded the [`ChannelMonitor`]s into their
2873	/// [`ChainMonitor`] and thus attempting to update it will fail or panic.
2874	///
2875	/// Thus, we place them here to be handled as soon as possible once we are running normally.
2876	///
2877	/// See `ChannelManager` struct-level documentation for lock order requirements.
2878	///
2879	/// [`ChainMonitor`]: crate::chain::chainmonitor::ChainMonitor
2880	pending_background_events: Mutex<Vec<BackgroundEvent>>,
2881	/// Used when we have to take a BIG lock to make sure everything is self-consistent.
2882	/// Essentially just when we're serializing ourselves out.
2883	/// Taken first everywhere where we are making changes before any other locks.
2884	/// When acquiring this lock in read mode, rather than acquiring it directly, call
2885	/// `PersistenceNotifierGuard::notify_on_drop(..)` and pass the lock to it, to ensure the
2886	/// Notifier the lock contains sends out a notification when the lock is released.
2887	total_consistency_lock: RwLock<()>,
2888	/// Tracks the progress of channels going through batch funding by whether funding_signed was
2889	/// received and the monitor has been persisted.
2890	///
2891	/// This information does not need to be persisted as funding nodes can forget
2892	/// unfunded channels upon disconnection.
2893	funding_batch_states: Mutex<BTreeMap<Txid, Vec<(ChannelId, PublicKey, bool)>>>,
2894
2895	background_events_processed_since_startup: AtomicBool,
2896
2897	event_persist_notifier: Notifier,
2898	needs_persist_flag: AtomicBool,
2899
2900	/// Tracks the message events that are to be broadcasted when we are connected to some peer.
2901	pending_broadcast_messages: Mutex<Vec<MessageSendEvent>>,
2902
2903	/// We only want to force-close our channels on peers based on stale feerates when we're
2904	/// confident the feerate on the channel is *really* stale, not just became stale recently.
2905	/// Thus, we store the fee estimates we had as of the last [`FEERATE_TRACKING_BLOCKS`] blocks
2906	/// (after startup completed) here, and only force-close when channels have a lower feerate
2907	/// than we predicted any time in the last [`FEERATE_TRACKING_BLOCKS`] blocks.
2908	///
2909	/// We only keep this in memory as we assume any feerates we receive immediately after startup
2910	/// may be bunk (as they often are if Bitcoin Core crashes) and want to delay taking any
2911	/// actions for a day anyway.
2912	///
2913	/// The first element in the pair is the
2914	/// [`ConfirmationTarget::MinAllowedAnchorChannelRemoteFee`] estimate, the second the
2915	/// [`ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee`] estimate.
2916	last_days_feerates: Mutex<VecDeque<(u32, u32)>>,
2917
2918	#[cfg(feature = "_test_utils")]
2919	/// In testing, it is useful be able to forge a name -> offer mapping so that we can pay an
2920	/// offer generated in the test.
2921	///
2922	/// This allows for doing so, validating proofs as normal, but, if they pass, replacing the
2923	/// offer they resolve to to the given one.
2924	pub testing_dnssec_proof_offer_resolution_override: Mutex<HashMap<HumanReadableName, Offer>>,
2925
2926	#[cfg(test)]
2927	pub(super) entropy_source: ES,
2928	#[cfg(not(test))]
2929	entropy_source: ES,
2930	node_signer: NS,
2931	#[cfg(test)]
2932	pub(super) signer_provider: SP,
2933	#[cfg(not(test))]
2934	signer_provider: SP,
2935
2936	logger: L,
2937}
2938
2939/// Chain-related parameters used to construct a new `ChannelManager`.
2940///
2941/// Typically, the block-specific parameters are derived from the best block hash for the network,
2942/// as a newly constructed `ChannelManager` will not have created any channels yet. These parameters
2943/// are not needed when deserializing a previously constructed `ChannelManager`.
2944#[derive(Clone, Copy, PartialEq)]
2945pub struct ChainParameters {
2946	/// The network for determining the `chain_hash` in Lightning messages.
2947	pub network: Network,
2948
2949	/// The hash and height of the latest block successfully connected.
2950	///
2951	/// Used to track on-chain channel funding outputs and send payments with reliable timelocks.
2952	pub best_block: BestBlock,
2953}
2954
2955#[derive(Copy, Clone, PartialEq)]
2956#[must_use]
2957enum NotifyOption {
2958	DoPersist,
2959	SkipPersistHandleEvents,
2960	SkipPersistNoEvents,
2961}
2962
2963/// Whenever we release the `ChannelManager`'s `total_consistency_lock`, from read mode, it is
2964/// desirable to notify any listeners on `await_persistable_update_timeout`/
2965/// `await_persistable_update` when new updates are available for persistence. Therefore, this
2966/// struct is responsible for locking the total consistency lock and, upon going out of scope,
2967/// sending the aforementioned notification (since the lock being released indicates that the
2968/// updates are ready for persistence).
2969///
2970/// We allow callers to either always notify by constructing with `notify_on_drop` or choose to
2971/// notify or not based on whether relevant changes have been made, providing a closure to
2972/// `optionally_notify` which returns a `NotifyOption`.
2973struct PersistenceNotifierGuard<'a, F: FnOnce() -> NotifyOption> {
2974	event_persist_notifier: &'a Notifier,
2975	needs_persist_flag: &'a AtomicBool,
2976	// Always `Some` once initialized, but tracked as an `Option` to obtain the closure by value in
2977	// [`PersistenceNotifierGuard::drop`].
2978	should_persist: Option<F>,
2979	// We hold onto this result so the lock doesn't get released immediately.
2980	_read_guard: RwLockReadGuard<'a, ()>,
2981}
2982
2983// We don't care what the concrete F is here, it's unused
2984impl<'a> PersistenceNotifierGuard<'a, fn() -> NotifyOption> {
2985	/// Notifies any waiters and indicates that we need to persist, in addition to possibly having
2986	/// events to handle.
2987	///
2988	/// This must always be called if the changes included a `ChannelMonitorUpdate`, as well as in
2989	/// other cases where losing the changes on restart may result in a force-close or otherwise
2990	/// isn't ideal.
2991	fn notify_on_drop<C: AChannelManager>(
2992		cm: &'a C,
2993	) -> PersistenceNotifierGuard<'a, impl FnOnce() -> NotifyOption> {
2994		Self::optionally_notify(cm, || -> NotifyOption { NotifyOption::DoPersist })
2995	}
2996
2997	fn optionally_notify<F: FnOnce() -> NotifyOption, C: AChannelManager>(
2998		cm: &'a C, persist_check: F,
2999	) -> PersistenceNotifierGuard<'a, impl FnOnce() -> NotifyOption> {
3000		let read_guard = cm.get_cm().total_consistency_lock.read().unwrap();
3001		let force_notify = cm.get_cm().process_background_events();
3002
3003		PersistenceNotifierGuard {
3004			event_persist_notifier: &cm.get_cm().event_persist_notifier,
3005			needs_persist_flag: &cm.get_cm().needs_persist_flag,
3006			should_persist: Some(move || {
3007				// Pick the "most" action between `persist_check` and the background events
3008				// processing and return that.
3009				let notify = persist_check();
3010				match (notify, force_notify) {
3011					(NotifyOption::DoPersist, _) => NotifyOption::DoPersist,
3012					(_, NotifyOption::DoPersist) => NotifyOption::DoPersist,
3013					(NotifyOption::SkipPersistHandleEvents, _) => {
3014						NotifyOption::SkipPersistHandleEvents
3015					},
3016					(_, NotifyOption::SkipPersistHandleEvents) => {
3017						NotifyOption::SkipPersistHandleEvents
3018					},
3019					_ => NotifyOption::SkipPersistNoEvents,
3020				}
3021			}),
3022			_read_guard: read_guard,
3023		}
3024	}
3025
3026	/// Note that if any [`ChannelMonitorUpdate`]s are possibly generated,
3027	/// [`ChannelManager::process_background_events`] MUST be called first (or
3028	/// [`Self::optionally_notify`] used).
3029	fn optionally_notify_skipping_background_events<F: Fn() -> NotifyOption, C: AChannelManager>(
3030		cm: &'a C, persist_check: F,
3031	) -> PersistenceNotifierGuard<'a, F> {
3032		let read_guard = cm.get_cm().total_consistency_lock.read().unwrap();
3033
3034		PersistenceNotifierGuard {
3035			event_persist_notifier: &cm.get_cm().event_persist_notifier,
3036			needs_persist_flag: &cm.get_cm().needs_persist_flag,
3037			should_persist: Some(persist_check),
3038			_read_guard: read_guard,
3039		}
3040	}
3041}
3042
3043impl<'a, F: FnOnce() -> NotifyOption> Drop for PersistenceNotifierGuard<'a, F> {
3044	fn drop(&mut self) {
3045		let should_persist = match self.should_persist.take() {
3046			Some(should_persist) => should_persist,
3047			None => {
3048				debug_assert!(false);
3049				return;
3050			},
3051		};
3052		match should_persist() {
3053			NotifyOption::DoPersist => {
3054				self.needs_persist_flag.store(true, Ordering::Release);
3055				self.event_persist_notifier.notify()
3056			},
3057			NotifyOption::SkipPersistHandleEvents => self.event_persist_notifier.notify(),
3058			NotifyOption::SkipPersistNoEvents => {},
3059		}
3060	}
3061}
3062
3063/// The amount of time in blocks we require our counterparty wait to claim their money (ie time
3064/// between when we, or our watchtower, must check for them having broadcast a theft transaction).
3065///
3066/// This can be increased (but not decreased) through [`ChannelHandshakeConfig::our_to_self_delay`]
3067///
3068/// [`ChannelHandshakeConfig::our_to_self_delay`]: crate::util::config::ChannelHandshakeConfig::our_to_self_delay
3069pub const BREAKDOWN_TIMEOUT: u16 = 6 * 24;
3070/// The amount of time in blocks we're willing to wait to claim money back to us. This matches
3071/// the maximum required amount in lnd as of March 2021.
3072pub(crate) const MAX_LOCAL_BREAKDOWN_TIMEOUT: u16 = 2 * 6 * 24 * 7;
3073
3074/// The minimum number of blocks between an inbound HTLC's CLTV and the corresponding outbound
3075/// HTLC's CLTV. The current default represents roughly eight hours of blocks at six blocks/hour.
3076///
3077/// This can be increased (but not decreased) through [`ChannelConfig::cltv_expiry_delta`]
3078///
3079/// [`ChannelConfig::cltv_expiry_delta`]: crate::util::config::ChannelConfig::cltv_expiry_delta
3080// This should always be a few blocks greater than channelmonitor::CLTV_CLAIM_BUFFER,
3081// i.e. the node we forwarded the payment on to should always have enough room to reliably time out
3082// the HTLC via a full update_fail_htlc/commitment_signed dance before we hit the
3083// CLTV_CLAIM_BUFFER point (we static assert that it's at least 3 blocks more).
3084pub const MIN_CLTV_EXPIRY_DELTA: u16 = 6 * 8;
3085// This should be long enough to allow a payment path drawn across multiple routing hops with substantial
3086// `cltv_expiry_delta`. Indeed, the length of those values is the reaction delay offered to a routing node
3087// in case of HTLC on-chain settlement. While appearing less competitive, a node operator could decide to
3088// scale them up to suit its security policy. At the network-level, we shouldn't constrain them too much,
3089// while avoiding to introduce a DoS vector. Further, a low CTLV_FAR_FAR_AWAY could be a source of
3090// routing failure for any HTLC sender picking up an LDK node among the first hops.
3091pub(crate) const CLTV_FAR_FAR_AWAY: u32 = 14 * 24 * 6;
3092
3093/// Minimum CLTV difference between the current block height and received inbound payments.
3094/// Invoices generated for payment to us must set their `min_final_cltv_expiry_delta` field to at least
3095/// this value.
3096// Note that we fail if exactly HTLC_FAIL_BACK_BUFFER + 1 was used, so we need to add one for
3097// any payments to succeed. Further, we don't want payments to fail if a block was found while
3098// a payment was being routed, so we add an extra block to be safe.
3099pub const MIN_FINAL_CLTV_EXPIRY_DELTA: u16 = HTLC_FAIL_BACK_BUFFER as u16 + 3;
3100
3101// Check that our MIN_CLTV_EXPIRY_DELTA gives us enough time to get everything on chain and locked
3102// in with enough time left to fail the corresponding HTLC back to our inbound edge before they
3103// force-close on us.
3104// In other words, if the next-hop peer fails HTLC LATENCY_GRACE_PERIOD_BLOCKS after our
3105// CLTV_CLAIM_BUFFER (because that's how many blocks we allow them after expiry), we'll still have
3106// 2*MAX_BLOCKS_FOR_CONF + ANTI_REORG_DELAY left to get two transactions on chain and the second
3107// fully locked in before the peer force-closes on us (LATENCY_GRACE_PERIOD_BLOCKS before the
3108// expiry, i.e. assuming the peer force-closes right at the expiry and we're behind by
3109// LATENCY_GRACE_PERIOD_BLOCKS).
3110const _CHECK_CLTV_EXPIRY_SANITY: () = assert!(
3111	MIN_CLTV_EXPIRY_DELTA as u32
3112		>= 2 * LATENCY_GRACE_PERIOD_BLOCKS + 2 * MAX_BLOCKS_FOR_CONF + ANTI_REORG_DELAY
3113);
3114
3115// Check that our MIN_CLTV_EXPIRY_DELTA gives us enough time to get the HTLC preimage back to our
3116// counterparty if the outbound edge gives us the preimage only one block before we'd force-close
3117// the channel.
3118// ie they provide the preimage LATENCY_GRACE_PERIOD_BLOCKS - 1 after the HTLC expires, then we
3119// pass the preimage back, which takes LATENCY_GRACE_PERIOD_BLOCKS to complete, and we want to make
3120// sure this all happens at least N blocks before the inbound HTLC expires (where N is the
3121// counterparty's CLTV_CLAIM_BUFFER or equivalent).
3122const _ASSUMED_COUNTERPARTY_CLTV_CLAIM_BUFFER: u32 = 6 * 6;
3123
3124const _CHECK_COUNTERPARTY_REALISTIC: () =
3125	assert!(_ASSUMED_COUNTERPARTY_CLTV_CLAIM_BUFFER >= CLTV_CLAIM_BUFFER);
3126
3127const _CHECK_CLTV_EXPIRY_OFFCHAIN: () = assert!(
3128	MIN_CLTV_EXPIRY_DELTA as u32
3129		>= 2 * LATENCY_GRACE_PERIOD_BLOCKS - 1 + _ASSUMED_COUNTERPARTY_CLTV_CLAIM_BUFFER
3130);
3131
3132/// The number of ticks of [`ChannelManager::timer_tick_occurred`] until expiry of incomplete MPPs
3133pub(crate) const MPP_TIMEOUT_TICKS: u8 = 3;
3134
3135/// The number of ticks of [`ChannelManager::timer_tick_occurred`] where a peer is disconnected
3136/// until we mark the channel disabled and gossip the update.
3137pub(crate) const DISABLE_GOSSIP_TICKS: u8 = 10;
3138
3139/// The number of ticks of [`ChannelManager::timer_tick_occurred`] where a peer is connected until
3140/// we mark the channel enabled and gossip the update.
3141pub(crate) const ENABLE_GOSSIP_TICKS: u8 = 5;
3142
3143/// The maximum number of unfunded channels we can have per-peer before we start rejecting new
3144/// (inbound) ones. The number of peers with unfunded channels is limited separately in
3145/// [`MAX_UNFUNDED_CHANNEL_PEERS`].
3146pub(super) const MAX_UNFUNDED_CHANS_PER_PEER: usize = 4;
3147
3148/// The maximum number of peers from which we will allow pending unfunded channels. Once we reach
3149/// this many peers we reject new (inbound) channels from peers with which we don't have a funded
3150/// channel.
3151pub(super) const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
3152
3153/// The maximum allowed size for peer storage, in bytes.
3154///
3155/// This constant defines the upper limit for the size of data
3156/// that can be stored for a peer. It is set to 1024 bytes (1 kilobyte)
3157/// to prevent excessive resource consumption.
3158#[cfg(not(test))]
3159const MAX_PEER_STORAGE_SIZE: usize = 1024;
3160
3161/// The maximum number of peers which we do not have a (funded) channel with. Once we reach this
3162/// many peers we reject new (inbound) connections.
3163const MAX_NO_CHANNEL_PEERS: usize = 250;
3164
3165/// Used by [`ChannelManager::list_recent_payments`] to express the status of recent payments.
3166/// These include payments that have yet to find a successful path, or have unresolved HTLCs.
3167#[derive(Debug, PartialEq)]
3168pub enum RecentPaymentDetails {
3169	/// When an invoice was requested and thus a payment has not yet been sent.
3170	AwaitingInvoice {
3171		/// A user-provided identifier in [`ChannelManager::pay_for_offer`] used to uniquely identify a
3172		/// payment and ensure idempotency in LDK.
3173		payment_id: PaymentId,
3174	},
3175	/// When a payment is still being sent and awaiting successful delivery.
3176	Pending {
3177		/// A user-provided identifier in [`send_payment`] or [`pay_for_offer`] used to uniquely
3178		/// identify a payment and ensure idempotency in LDK.
3179		///
3180		/// [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
3181		/// [`pay_for_offer`]: crate::ln::channelmanager::ChannelManager::pay_for_offer
3182		payment_id: PaymentId,
3183		/// Hash of the payment that is currently being sent but has yet to be fulfilled or
3184		/// abandoned.
3185		payment_hash: PaymentHash,
3186		/// Total amount (in msat, excluding fees) across all paths for this payment,
3187		/// not just the amount currently inflight.
3188		total_msat: u64,
3189	},
3190	/// When a pending payment is fulfilled, we continue tracking it until all pending HTLCs have
3191	/// been resolved. Upon receiving [`Event::PaymentSent`], we delay for a few minutes before the
3192	/// payment is removed from tracking.
3193	Fulfilled {
3194		/// A user-provided identifier in [`send_payment`] or [`pay_for_offer`] used to uniquely
3195		/// identify a payment and ensure idempotency in LDK.
3196		///
3197		/// [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
3198		/// [`pay_for_offer`]: crate::ln::channelmanager::ChannelManager::pay_for_offer
3199		payment_id: PaymentId,
3200		/// Hash of the payment that was claimed. `None` for serializations of [`ChannelManager`]
3201		/// made before LDK version 0.0.104.
3202		payment_hash: Option<PaymentHash>,
3203	},
3204	/// After a payment's retries are exhausted per the provided [`Retry`], or it is explicitly
3205	/// abandoned via [`ChannelManager::abandon_payment`], it is marked as abandoned until all
3206	/// pending HTLCs for this payment resolve and an [`Event::PaymentFailed`] is generated.
3207	Abandoned {
3208		/// A user-provided identifier in [`send_payment`] or [`pay_for_offer`] used to uniquely
3209		/// identify a payment and ensure idempotency in LDK.
3210		///
3211		/// [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
3212		/// [`pay_for_offer`]: crate::ln::channelmanager::ChannelManager::pay_for_offer
3213		payment_id: PaymentId,
3214		/// Hash of the payment that we have given up trying to send.
3215		payment_hash: PaymentHash,
3216	},
3217}
3218
3219/// Route hints used in constructing invoices for [phantom node payents].
3220///
3221/// [phantom node payments]: crate::sign::PhantomKeysManager
3222#[derive(Clone)]
3223pub struct PhantomRouteHints {
3224	/// The list of channels to be included in the invoice route hints.
3225	pub channels: Vec<ChannelDetails>,
3226	/// A fake scid used for representing the phantom node's fake channel in generating the invoice
3227	/// route hints.
3228	pub phantom_scid: u64,
3229	/// The pubkey of the real backing node that would ultimately receive the payment.
3230	pub real_node_pubkey: PublicKey,
3231}
3232
3233#[rustfmt::skip]
3234macro_rules! handle_error {
3235	($self: ident, $internal: expr, $counterparty_node_id: expr) => { {
3236		// In testing, ensure there are no deadlocks where the lock is already held upon
3237		// entering the macro.
3238		debug_assert_ne!($self.pending_events.held_by_thread(), LockHeldState::HeldByThread);
3239		debug_assert_ne!($self.per_peer_state.held_by_thread(), LockHeldState::HeldByThread);
3240
3241		match $internal {
3242			Ok(msg) => Ok(msg),
3243			Err(MsgHandleErrInternal { err, shutdown_finish, tx_abort, .. }) => {
3244				let mut msg_event = None;
3245
3246				if let Some((shutdown_res, update_option)) = shutdown_finish {
3247					let counterparty_node_id = shutdown_res.counterparty_node_id;
3248					let channel_id = shutdown_res.channel_id;
3249					let logger = WithContext::from(
3250						&$self.logger, Some(counterparty_node_id), Some(channel_id), None
3251					);
3252					log_error!(logger, "Closing channel: {}", err.err);
3253
3254					$self.finish_close_channel(shutdown_res);
3255					if let Some(update) = update_option {
3256						let mut pending_broadcast_messages = $self.pending_broadcast_messages.lock().unwrap();
3257						pending_broadcast_messages.push(MessageSendEvent::BroadcastChannelUpdate {
3258							msg: update
3259						});
3260					}
3261				} else {
3262					log_error!($self.logger, "Got non-closing error: {}", err.err);
3263				}
3264
3265				if let msgs::ErrorAction::IgnoreError = err.action {
3266					if let Some(tx_abort) = tx_abort {
3267						msg_event = Some(MessageSendEvent::SendTxAbort {
3268							node_id: $counterparty_node_id,
3269							msg: tx_abort,
3270						});
3271					}
3272				} else {
3273					msg_event = Some(MessageSendEvent::HandleError {
3274						node_id: $counterparty_node_id,
3275						action: err.action.clone()
3276					});
3277				}
3278
3279				if let Some(msg_event) = msg_event {
3280					let per_peer_state = $self.per_peer_state.read().unwrap();
3281					if let Some(peer_state_mutex) = per_peer_state.get(&$counterparty_node_id) {
3282						let mut peer_state = peer_state_mutex.lock().unwrap();
3283						if peer_state.is_connected {
3284							peer_state.pending_msg_events.push(msg_event);
3285						}
3286					}
3287				}
3288
3289				// Return error in case higher-API need one
3290				Err(err)
3291			},
3292		}
3293	} };
3294}
3295
3296/// Do not call this directly, use `convert_channel_err` instead.
3297#[rustfmt::skip]
3298macro_rules! locked_close_channel {
3299	($self: ident, $chan_context: expr, UNFUNDED) => {{
3300		$self.short_to_chan_info.write().unwrap().remove(&$chan_context.outbound_scid_alias());
3301		// If the channel was never confirmed on-chain prior to its closure, remove the
3302		// outbound SCID alias we used for it from the collision-prevention set. While we
3303		// generally want to avoid ever re-using an outbound SCID alias across all channels, we
3304		// also don't want a counterparty to be able to trivially cause a memory leak by simply
3305		// opening a million channels with us which are closed before we ever reach the funding
3306		// stage.
3307		let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$chan_context.outbound_scid_alias());
3308		debug_assert!(alias_removed);
3309	}};
3310	($self: ident, $peer_state: expr, $funded_chan: expr, $shutdown_res_mut: expr, FUNDED) => {{
3311		if let Some((_, funding_txo, _, update)) = $shutdown_res_mut.monitor_update.take() {
3312			handle_new_monitor_update!($self, funding_txo, update, $peer_state,
3313				$funded_chan.context, REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER);
3314		}
3315		// If there's a possibility that we need to generate further monitor updates for this
3316		// channel, we need to store the last update_id of it. However, we don't want to insert
3317		// into the map (which prevents the `PeerState` from being cleaned up) for channels that
3318		// never even got confirmations (which would open us up to DoS attacks).
3319		let update_id = $funded_chan.context.get_latest_monitor_update_id();
3320		if $funded_chan.funding.get_funding_tx_confirmation_height().is_some() || $funded_chan.context.minimum_depth(&$funded_chan.funding) == Some(0) || update_id > 1 {
3321			let chan_id = $funded_chan.context.channel_id();
3322			$peer_state.closed_channel_monitor_update_ids.insert(chan_id, update_id);
3323		}
3324		let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
3325		if let Some(short_id) = $funded_chan.funding.get_short_channel_id() {
3326			short_to_chan_info.remove(&short_id);
3327		} else {
3328			// If the channel was never confirmed on-chain prior to its closure, remove the
3329			// outbound SCID alias we used for it from the collision-prevention set. While we
3330			// generally want to avoid ever re-using an outbound SCID alias across all channels, we
3331			// also don't want a counterparty to be able to trivially cause a memory leak by simply
3332			// opening a million channels with us which are closed before we ever reach the funding
3333			// stage.
3334			let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$funded_chan.context.outbound_scid_alias());
3335			debug_assert!(alias_removed);
3336		}
3337		short_to_chan_info.remove(&$funded_chan.context.outbound_scid_alias());
3338		for scid in $funded_chan.context.historical_scids() {
3339			short_to_chan_info.remove(scid);
3340		}
3341	}}
3342}
3343
3344/// When a channel is removed, two things need to happen:
3345/// (a) This must be called in the same `per_peer_state` lock as the channel-closing action,
3346/// (b) [`handle_error`] needs to be called without holding any locks (except
3347///     [`ChannelManager::total_consistency_lock`]), which then calls
3348///     [`ChannelManager::finish_close_channel`].
3349///
3350/// Note that this step can be skipped if the channel was never opened (through the creation of a
3351/// [`ChannelMonitor`]/channel funding transaction) to begin with.
3352///
3353/// Returns `(boolean indicating if we should remove the Channel object from memory, a mapped
3354/// error)`, except in the `COOP_CLOSE` case, where the bool is elided (it is always implicitly
3355/// true).
3356#[rustfmt::skip]
3357macro_rules! convert_channel_err {
3358	($self: ident, $peer_state: expr, $err: expr, $chan: expr, $close: expr, $locked_close: expr, $channel_id: expr, _internal) => { {
3359		match $err {
3360			ChannelError::Warn(msg) => {
3361				(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(msg), $channel_id))
3362			},
3363			ChannelError::WarnAndDisconnect(msg) => {
3364				(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::WarnAndDisconnect(msg), $channel_id))
3365			},
3366			ChannelError::Ignore(msg) => {
3367				(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $channel_id))
3368			},
3369			ChannelError::Abort(reason) => {
3370				(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Abort(reason), $channel_id))
3371			},
3372			ChannelError::Close((msg, reason)) => {
3373				let (mut shutdown_res, chan_update) = $close(reason);
3374				let logger = WithChannelContext::from(&$self.logger, &$chan.context(), None);
3375				log_error!(logger, "Closed channel {} due to close-required error: {}", $channel_id, msg);
3376				$locked_close(&mut shutdown_res, $chan);
3377				let err =
3378					MsgHandleErrInternal::from_finish_shutdown(msg, $channel_id, shutdown_res, chan_update);
3379				(true, err)
3380			},
3381			ChannelError::SendError(msg) => {
3382				(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::SendError(msg), $channel_id))
3383			},
3384		}
3385	} };
3386	($self: ident, $peer_state: expr, $shutdown_result: expr, $funded_channel: expr, COOP_CLOSED) => { {
3387		let chan_id = $funded_channel.context.channel_id();
3388		let reason = ChannelError::Close(("Coop Closed".to_owned(), $shutdown_result.closure_reason.clone()));
3389		let do_close = |_| {
3390			(
3391				$shutdown_result,
3392				$self.get_channel_update_for_broadcast(&$funded_channel).ok(),
3393			)
3394		};
3395		let mut locked_close = |shutdown_res_mut: &mut ShutdownResult, funded_channel: &mut FundedChannel<_>| {
3396			locked_close_channel!($self, $peer_state, funded_channel, shutdown_res_mut, FUNDED);
3397		};
3398		let (close, mut err) =
3399			convert_channel_err!($self, $peer_state, reason, $funded_channel, do_close, locked_close, chan_id, _internal);
3400		err.dont_send_error_message();
3401		debug_assert!(close);
3402		err
3403	} };
3404	($self: ident, $peer_state: expr, $err: expr, $funded_channel: expr, FUNDED_CHANNEL) => { {
3405		let chan_id = $funded_channel.context.channel_id();
3406		let mut do_close = |reason| {
3407			(
3408				$funded_channel.force_shutdown(reason),
3409				$self.get_channel_update_for_broadcast(&$funded_channel).ok(),
3410			)
3411		};
3412		let mut locked_close = |shutdown_res_mut: &mut ShutdownResult, funded_channel: &mut FundedChannel<_>| {
3413			locked_close_channel!($self, $peer_state, funded_channel, shutdown_res_mut, FUNDED);
3414		};
3415		convert_channel_err!($self, $peer_state, $err, $funded_channel, do_close, locked_close, chan_id, _internal)
3416	} };
3417	($self: ident, $peer_state: expr, $err: expr, $channel: expr, UNFUNDED_CHANNEL) => { {
3418		let chan_id = $channel.context().channel_id();
3419		let mut do_close = |reason| { ($channel.force_shutdown(reason), None) };
3420		let locked_close = |_, chan: &mut Channel<_>| { locked_close_channel!($self, chan.context(), UNFUNDED); };
3421		convert_channel_err!($self, $peer_state, $err, $channel, do_close, locked_close, chan_id, _internal)
3422	} };
3423	($self: ident, $peer_state: expr, $err: expr, $channel: expr) => {
3424		match $channel.as_funded_mut() {
3425			Some(funded_channel) => {
3426				convert_channel_err!($self, $peer_state, $err, funded_channel, FUNDED_CHANNEL)
3427			},
3428			None => {
3429				convert_channel_err!($self, $peer_state, $err, $channel, UNFUNDED_CHANNEL)
3430			},
3431		}
3432	};
3433}
3434
3435macro_rules! break_channel_entry {
3436	($self: ident, $peer_state: expr, $res: expr, $entry: expr) => {
3437		match $res {
3438			Ok(res) => res,
3439			Err(e) => {
3440				let (drop, res) = convert_channel_err!($self, $peer_state, e, $entry.get_mut());
3441				if drop {
3442					$entry.remove_entry();
3443				}
3444				break Err(res);
3445			},
3446		}
3447	};
3448}
3449
3450macro_rules! try_channel_entry {
3451	($self: ident, $peer_state: expr, $res: expr, $entry: expr) => {
3452		match $res {
3453			Ok(res) => res,
3454			Err(e) => {
3455				let (drop, res) = convert_channel_err!($self, $peer_state, e, $entry.get_mut());
3456				if drop {
3457					$entry.remove_entry();
3458				}
3459				return Err(res);
3460			},
3461		}
3462	};
3463}
3464
3465macro_rules! send_channel_ready {
3466	($self: ident, $pending_msg_events: expr, $channel: expr, $channel_ready_msg: expr) => {{
3467		if $channel.context.is_connected() {
3468			$pending_msg_events.push(MessageSendEvent::SendChannelReady {
3469				node_id: $channel.context.get_counterparty_node_id(),
3470				msg: $channel_ready_msg,
3471			});
3472		}
3473		// Note that we may send a `channel_ready` multiple times for a channel if we reconnect, so
3474		// we allow collisions, but we shouldn't ever be updating the channel ID pointed to.
3475		let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
3476		let outbound_alias_insert = short_to_chan_info.insert($channel.context.outbound_scid_alias(), ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
3477		assert!(outbound_alias_insert.is_none() || outbound_alias_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
3478			"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
3479		insert_short_channel_id!(short_to_chan_info, $channel);
3480	}}
3481}
3482
3483macro_rules! insert_short_channel_id {
3484	($short_to_chan_info: ident, $channel: expr) => {{
3485		if let Some(real_scid) = $channel.funding.get_short_channel_id() {
3486			let scid_insert = $short_to_chan_info.insert(real_scid, ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
3487			assert!(scid_insert.is_none() || scid_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
3488				"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
3489		}
3490	}}
3491}
3492
3493macro_rules! emit_funding_tx_broadcast_safe_event {
3494	($locked_events: expr, $channel: expr, $funding_txo: expr) => {
3495		if !$channel.context.funding_tx_broadcast_safe_event_emitted() {
3496			$locked_events.push_back((events::Event::FundingTxBroadcastSafe {
3497				channel_id: $channel.context.channel_id(),
3498				user_channel_id: $channel.context.get_user_id(),
3499				funding_txo: $funding_txo,
3500				counterparty_node_id: $channel.context.get_counterparty_node_id(),
3501				former_temporary_channel_id: $channel.context.temporary_channel_id()
3502					.expect("Unreachable: FundingTxBroadcastSafe event feature added to channel establishment process in LDK v0.0.124 where this should never be None."),
3503			}, None));
3504			$channel.context.set_funding_tx_broadcast_safe_event_emitted();
3505		}
3506	}
3507}
3508
3509macro_rules! emit_channel_pending_event {
3510	($locked_events: expr, $channel: expr) => {
3511		if $channel.context.should_emit_channel_pending_event() {
3512			let funding_txo = $channel.funding.get_funding_txo().unwrap();
3513			let funding_redeem_script =
3514				Some($channel.funding.channel_transaction_parameters.make_funding_redeemscript());
3515			$locked_events.push_back((
3516				events::Event::ChannelPending {
3517					channel_id: $channel.context.channel_id(),
3518					former_temporary_channel_id: $channel.context.temporary_channel_id(),
3519					counterparty_node_id: $channel.context.get_counterparty_node_id(),
3520					user_channel_id: $channel.context.get_user_id(),
3521					funding_txo: funding_txo.into_bitcoin_outpoint(),
3522					channel_type: Some($channel.funding.get_channel_type().clone()),
3523					funding_redeem_script,
3524				},
3525				None,
3526			));
3527			$channel.context.set_channel_pending_event_emitted();
3528		}
3529	};
3530}
3531
3532macro_rules! emit_initial_channel_ready_event {
3533	($locked_events: expr, $channel: expr) => {
3534		if $channel.context.should_emit_initial_channel_ready_event() {
3535			debug_assert!($channel.context.channel_pending_event_emitted());
3536			$locked_events.push_back((
3537				events::Event::ChannelReady {
3538					channel_id: $channel.context.channel_id(),
3539					user_channel_id: $channel.context.get_user_id(),
3540					counterparty_node_id: $channel.context.get_counterparty_node_id(),
3541					funding_txo: $channel
3542						.funding
3543						.get_funding_txo()
3544						.map(|outpoint| outpoint.into_bitcoin_outpoint()),
3545					channel_type: $channel.funding.get_channel_type().clone(),
3546				},
3547				None,
3548			));
3549			$channel.context.set_initial_channel_ready_event_emitted();
3550		}
3551	};
3552}
3553
3554/// Handles the completion steps for when a [`ChannelMonitorUpdate`] is applied to a live channel.
3555///
3556/// You should not add new direct calls to this, generally, rather rely on
3557/// `handle_new_monitor_update` or [`ChannelManager::channel_monitor_updated`] to call it for you.
3558///
3559/// Requires that  the in-flight monitor update set for this channel is empty!
3560macro_rules! handle_monitor_update_completion {
3561	($self: ident, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr) => {{
3562		let channel_id = $chan.context.channel_id();
3563		let outbound_scid_alias = $chan.context().outbound_scid_alias();
3564		let counterparty_node_id = $chan.context.get_counterparty_node_id();
3565
3566		#[cfg(debug_assertions)]
3567		{
3568			let in_flight_updates =
3569				$peer_state.in_flight_monitor_updates.get(&channel_id);
3570			assert!(in_flight_updates.map(|(_, updates)| updates.is_empty()).unwrap_or(true));
3571			assert!($chan.is_awaiting_monitor_update());
3572		}
3573
3574		let logger = WithChannelContext::from(&$self.logger, &$chan.context, None);
3575
3576		let update_actions = $peer_state.monitor_update_blocked_actions
3577			.remove(&channel_id).unwrap_or(Vec::new());
3578
3579		if $chan.blocked_monitor_updates_pending() != 0 {
3580			mem::drop($peer_state_lock);
3581			mem::drop($per_peer_state_lock);
3582
3583			log_debug!(logger, "Channel has blocked monitor updates, completing update actions but leaving channel blocked");
3584			$self.handle_monitor_update_completion_actions(update_actions);
3585		} else {
3586			log_debug!(logger, "Channel is open and awaiting update, resuming it");
3587			let mut updates = $chan.monitor_updating_restored(
3588				&&logger,
3589				&$self.node_signer,
3590				$self.chain_hash,
3591				&*$self.config.read().unwrap(),
3592				$self.best_block.read().unwrap().height,
3593				|htlc_id| {
3594					$self.path_for_release_held_htlc(htlc_id, outbound_scid_alias, &channel_id, &counterparty_node_id)
3595				},
3596			);
3597			let channel_update = if updates.channel_ready.is_some()
3598				&& $chan.context.is_usable()
3599				&& $peer_state.is_connected
3600			{
3601				// We only send a channel_update in the case where we are just now sending a
3602				// channel_ready and the channel is in a usable state. We may re-send a
3603				// channel_update later through the announcement_signatures process for public
3604				// channels, but there's no reason not to just inform our counterparty of our fees
3605				// now.
3606				if let Ok(msg) = $self.get_channel_update_for_unicast($chan) {
3607					Some(MessageSendEvent::SendChannelUpdate { node_id: counterparty_node_id, msg })
3608				} else {
3609					None
3610				}
3611			} else {
3612				None
3613			};
3614
3615			let (htlc_forwards, decode_update_add_htlcs) = $self.handle_channel_resumption(
3616				&mut $peer_state.pending_msg_events,
3617				$chan,
3618				updates.raa,
3619				updates.commitment_update,
3620				updates.commitment_order,
3621				updates.accepted_htlcs,
3622				updates.pending_update_adds,
3623				updates.funding_broadcastable,
3624				updates.channel_ready,
3625				updates.announcement_sigs,
3626				updates.tx_signatures,
3627				None,
3628				updates.channel_ready_order,
3629			);
3630			if let Some(upd) = channel_update {
3631				$peer_state.pending_msg_events.push(upd);
3632			}
3633
3634			let unbroadcasted_batch_funding_txid =
3635				$chan.context.unbroadcasted_batch_funding_txid(&$chan.funding);
3636			core::mem::drop($peer_state_lock);
3637			core::mem::drop($per_peer_state_lock);
3638
3639			// If the channel belongs to a batch funding transaction, the progress of the batch
3640			// should be updated as we have received funding_signed and persisted the monitor.
3641			if let Some(txid) = unbroadcasted_batch_funding_txid {
3642				let mut funding_batch_states = $self.funding_batch_states.lock().unwrap();
3643				let mut batch_completed = false;
3644				if let Some(batch_state) = funding_batch_states.get_mut(&txid) {
3645					let channel_state = batch_state.iter_mut().find(|(chan_id, pubkey, _)| (
3646						*chan_id == channel_id &&
3647						*pubkey == counterparty_node_id
3648					));
3649					if let Some(channel_state) = channel_state {
3650						channel_state.2 = true;
3651					} else {
3652						debug_assert!(false, "Missing channel batch state for channel which completed initial monitor update");
3653					}
3654					batch_completed = batch_state.iter().all(|(_, _, completed)| *completed);
3655				} else {
3656					debug_assert!(false, "Missing batch state for channel which completed initial monitor update");
3657				}
3658
3659				// When all channels in a batched funding transaction have become ready, it is not necessary
3660				// to track the progress of the batch anymore and the state of the channels can be updated.
3661				if batch_completed {
3662					let removed_batch_state = funding_batch_states.remove(&txid).into_iter().flatten();
3663					let per_peer_state = $self.per_peer_state.read().unwrap();
3664					let mut batch_funding_tx = None;
3665					for (channel_id, counterparty_node_id, _) in removed_batch_state {
3666						if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
3667							let mut peer_state = peer_state_mutex.lock().unwrap();
3668							if let Some(funded_chan) = peer_state.channel_by_id
3669								.get_mut(&channel_id)
3670								.and_then(Channel::as_funded_mut)
3671							{
3672								batch_funding_tx = batch_funding_tx.or_else(|| funded_chan.context.unbroadcasted_funding(&funded_chan.funding));
3673								funded_chan.set_batch_ready();
3674								let mut pending_events = $self.pending_events.lock().unwrap();
3675								emit_channel_pending_event!(pending_events, funded_chan);
3676							}
3677						}
3678					}
3679					if let Some(tx) = batch_funding_tx {
3680						log_info!($self.logger, "Broadcasting batch funding transaction with txid {}", tx.compute_txid());
3681						$self.tx_broadcaster.broadcast_transactions(&[&tx]);
3682					}
3683				}
3684			}
3685
3686			$self.handle_monitor_update_completion_actions(update_actions);
3687
3688			if let Some(forwards) = htlc_forwards {
3689				$self.forward_htlcs(&mut [forwards][..]);
3690			}
3691			if let Some(decode) = decode_update_add_htlcs {
3692				$self.push_decode_update_add_htlcs(decode);
3693			}
3694			$self.finalize_claims(updates.finalized_claimed_htlcs);
3695			for failure in updates.failed_htlcs.drain(..) {
3696				let receiver = HTLCHandlingFailureType::Forward { node_id: Some(counterparty_node_id), channel_id };
3697				$self.fail_htlc_backwards_internal(&failure.0, &failure.1, &failure.2, receiver, None);
3698			}
3699		}
3700	}};
3701}
3702
3703macro_rules! handle_new_monitor_update {
3704	($self: ident, $update_res: expr, $logger: expr, $channel_id: expr, _internal, $completed: expr) => { {
3705		debug_assert!($self.background_events_processed_since_startup.load(Ordering::Acquire));
3706		match $update_res {
3707			ChannelMonitorUpdateStatus::UnrecoverableError => {
3708				let err_str = "ChannelMonitor[Update] persistence failed unrecoverably. This indicates we cannot continue normal operation and must shut down.";
3709				log_error!($logger, "{}", err_str);
3710				panic!("{}", err_str);
3711			},
3712			ChannelMonitorUpdateStatus::InProgress => {
3713				#[cfg(not(any(test, feature = "_externalize_tests")))]
3714				if $self.monitor_update_type.swap(1, Ordering::Relaxed) == 2 {
3715					panic!("Cannot use both ChannelMonitorUpdateStatus modes InProgress and Completed without restart");
3716				}
3717				log_debug!($logger, "ChannelMonitor update for {} in flight, holding messages until the update completes.",
3718					$channel_id);
3719				false
3720			},
3721			ChannelMonitorUpdateStatus::Completed => {
3722				#[cfg(not(any(test, feature = "_externalize_tests")))]
3723				if $self.monitor_update_type.swap(2, Ordering::Relaxed) == 1 {
3724					panic!("Cannot use both ChannelMonitorUpdateStatus modes InProgress and Completed without restart");
3725				}
3726				$completed;
3727				true
3728			},
3729		}
3730	} };
3731	($self: ident, $update_res: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr, INITIAL_MONITOR) => {
3732		let logger = WithChannelContext::from(&$self.logger, &$chan.context, None);
3733		handle_new_monitor_update!($self, $update_res, logger, $chan.context.channel_id(), _internal,
3734			handle_monitor_update_completion!($self, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan))
3735	};
3736	(
3737		$self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $logger: expr,
3738		$chan_id: expr, $counterparty_node_id: expr, $in_flight_updates: ident, $update_idx: ident,
3739		_internal_outer, $completed: expr
3740	) => { {
3741		$in_flight_updates = &mut $peer_state.in_flight_monitor_updates.entry($chan_id)
3742			.or_insert_with(|| ($funding_txo, Vec::new())).1;
3743		// During startup, we push monitor updates as background events through to here in
3744		// order to replay updates that were in-flight when we shut down. Thus, we have to
3745		// filter for uniqueness here.
3746		$update_idx = $in_flight_updates.iter().position(|upd| upd == &$update)
3747			.unwrap_or_else(|| {
3748				$in_flight_updates.push($update);
3749				$in_flight_updates.len() - 1
3750			});
3751		if $self.background_events_processed_since_startup.load(Ordering::Acquire) {
3752			let update_res = $self.chain_monitor.update_channel($chan_id, &$in_flight_updates[$update_idx]);
3753			handle_new_monitor_update!($self, update_res, $logger, $chan_id, _internal, $completed)
3754		} else {
3755			// We blindly assume that the ChannelMonitorUpdate will be regenerated on startup if we
3756			// fail to persist it. This is a fairly safe assumption, however, since anything we do
3757			// during the startup sequence should be replayed exactly if we immediately crash.
3758			let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
3759				counterparty_node_id: $counterparty_node_id,
3760				funding_txo: $funding_txo,
3761				channel_id: $chan_id,
3762				update: $in_flight_updates[$update_idx].clone(),
3763			};
3764			// We want to track the in-flight update both in `in_flight_monitor_updates` and in
3765			// `pending_background_events` to avoid a race condition during
3766			// `pending_background_events` processing where we complete one
3767			// `ChannelMonitorUpdate` (but there are more pending as background events) but we
3768			// conclude that all pending `ChannelMonitorUpdate`s have completed and its safe to
3769			// run post-completion actions.
3770			// We could work around that with some effort, but its simpler to just track updates
3771			// twice.
3772			$self.pending_background_events.lock().unwrap().push(event);
3773			false
3774		}
3775	} };
3776	(
3777		$self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $chan_context: expr,
3778		REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER
3779	) => { {
3780		let logger = WithChannelContext::from(&$self.logger, &$chan_context, None);
3781		let chan_id = $chan_context.channel_id();
3782		let counterparty_node_id = $chan_context.get_counterparty_node_id();
3783		let in_flight_updates;
3784		let idx;
3785		handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, logger, chan_id,
3786			counterparty_node_id, in_flight_updates, idx, _internal_outer,
3787			{
3788				let _ = in_flight_updates.remove(idx);
3789			})
3790	} };
3791	(
3792		$self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr,
3793		$per_peer_state_lock: expr, $counterparty_node_id: expr, $channel_id: expr, POST_CHANNEL_CLOSE
3794	) => { {
3795		let logger = WithContext::from(&$self.logger, Some($counterparty_node_id), Some($channel_id), None);
3796		let in_flight_updates;
3797		let idx;
3798		handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, logger,
3799			$channel_id, $counterparty_node_id, in_flight_updates, idx, _internal_outer,
3800			{
3801				let _ = in_flight_updates.remove(idx);
3802				if in_flight_updates.is_empty() {
3803					let update_actions = $peer_state.monitor_update_blocked_actions
3804						.remove(&$channel_id).unwrap_or(Vec::new());
3805
3806					mem::drop($peer_state_lock);
3807					mem::drop($per_peer_state_lock);
3808
3809					$self.handle_monitor_update_completion_actions(update_actions);
3810				}
3811			})
3812	} };
3813	(
3814		$self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr,
3815		$per_peer_state_lock: expr, $chan: expr
3816	) => { {
3817		let logger = WithChannelContext::from(&$self.logger, &$chan.context, None);
3818		let chan_id = $chan.context.channel_id();
3819		let counterparty_node_id = $chan.context.get_counterparty_node_id();
3820		let in_flight_updates;
3821		let idx;
3822		handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, logger, chan_id,
3823			counterparty_node_id, in_flight_updates, idx, _internal_outer,
3824			{
3825				let _ = in_flight_updates.remove(idx);
3826				if in_flight_updates.is_empty() {
3827					handle_monitor_update_completion!($self, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan);
3828				}
3829			})
3830	} };
3831}
3832
3833#[rustfmt::skip]
3834macro_rules! process_events_body {
3835	($self: expr, $event_to_handle: expr, $handle_event: expr) => {
3836		let mut handling_failed = false;
3837		let mut processed_all_events = false;
3838		while !handling_failed && !processed_all_events {
3839			if $self.pending_events_processor.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed).is_err() {
3840				return;
3841			}
3842
3843			let mut result;
3844
3845			{
3846				// We'll acquire our total consistency lock so that we can be sure no other
3847				// persists happen while processing monitor events.
3848				let _read_guard = $self.total_consistency_lock.read().unwrap();
3849
3850				// Because `handle_post_event_actions` may send `ChannelMonitorUpdate`s to the user we must
3851				// ensure any startup-generated background events are handled first.
3852				result = $self.process_background_events();
3853
3854				// TODO: This behavior should be documented. It's unintuitive that we query
3855				// ChannelMonitors when clearing other events.
3856				if $self.process_pending_monitor_events() {
3857					result = NotifyOption::DoPersist;
3858				}
3859			}
3860
3861			let pending_events = $self.pending_events.lock().unwrap().clone();
3862			if !pending_events.is_empty() {
3863				result = NotifyOption::DoPersist;
3864			}
3865
3866			let mut post_event_actions = Vec::new();
3867
3868			let mut num_handled_events = 0;
3869			for (event, action_opt) in pending_events {
3870				log_trace!($self.logger, "Handling event {:?}...", event);
3871				$event_to_handle = event;
3872				let event_handling_result = $handle_event;
3873				log_trace!($self.logger, "Done handling event, result: {:?}", event_handling_result);
3874				match event_handling_result {
3875					Ok(()) => {
3876						if let Some(action) = action_opt {
3877							post_event_actions.push(action);
3878						}
3879						num_handled_events += 1;
3880					}
3881					Err(_e) => {
3882						// If we encounter an error we stop handling events and make sure to replay
3883						// any unhandled events on the next invocation.
3884						handling_failed = true;
3885						break;
3886					}
3887				}
3888			}
3889
3890			{
3891				let mut pending_events = $self.pending_events.lock().unwrap();
3892				pending_events.drain(..num_handled_events);
3893				processed_all_events = pending_events.is_empty();
3894				// Note that `push_pending_forwards_ev` relies on `pending_events_processor` being
3895				// updated here with the `pending_events` lock acquired.
3896				$self.pending_events_processor.store(false, Ordering::Release);
3897			}
3898
3899			if !post_event_actions.is_empty() {
3900				$self.handle_post_event_actions(post_event_actions);
3901				// If we had some actions, go around again as we may have more events now
3902				processed_all_events = false;
3903			}
3904
3905			match result {
3906				NotifyOption::DoPersist => {
3907					$self.needs_persist_flag.store(true, Ordering::Release);
3908					$self.event_persist_notifier.notify();
3909				},
3910				NotifyOption::SkipPersistHandleEvents =>
3911					$self.event_persist_notifier.notify(),
3912				NotifyOption::SkipPersistNoEvents => {},
3913			}
3914		}
3915	}
3916}
3917
3918impl<
3919		M: Deref,
3920		T: Deref,
3921		ES: Deref,
3922		NS: Deref,
3923		SP: Deref,
3924		F: Deref,
3925		R: Deref,
3926		MR: Deref,
3927		L: Deref,
3928	> ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
3929where
3930	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
3931	T::Target: BroadcasterInterface,
3932	ES::Target: EntropySource,
3933	NS::Target: NodeSigner,
3934	SP::Target: SignerProvider,
3935	F::Target: FeeEstimator,
3936	R::Target: Router,
3937	MR::Target: MessageRouter,
3938	L::Target: Logger,
3939{
3940	/// Constructs a new `ChannelManager` to hold several channels and route between them.
3941	///
3942	/// The current time or latest block header time can be provided as the `current_timestamp`.
3943	///
3944	/// This is the main "logic hub" for all channel-related actions, and implements
3945	/// [`ChannelMessageHandler`].
3946	///
3947	/// Non-proportional fees are fixed according to our risk using the provided fee estimator.
3948	///
3949	/// Users need to notify the new `ChannelManager` when a new block is connected or
3950	/// disconnected using its [`block_connected`] and [`blocks_disconnected`] methods, starting
3951	/// from after [`params.best_block.block_hash`]. See [`chain::Listen`] and [`chain::Confirm`] for
3952	/// more details.
3953	///
3954	/// [`block_connected`]: chain::Listen::block_connected
3955	/// [`blocks_disconnected`]: chain::Listen::blocks_disconnected
3956	/// [`params.best_block.block_hash`]: chain::BestBlock::block_hash
3957	#[rustfmt::skip]
3958	pub fn new(
3959		fee_est: F, chain_monitor: M, tx_broadcaster: T, router: R, message_router: MR, logger: L,
3960		entropy_source: ES, node_signer: NS, signer_provider: SP, config: UserConfig,
3961		params: ChainParameters, current_timestamp: u32,
3962	) -> Self
3963	where
3964		L: Clone,
3965	{
3966		let mut secp_ctx = Secp256k1::new();
3967		secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
3968
3969		let expanded_inbound_key = node_signer.get_expanded_key();
3970		let our_network_pubkey = node_signer.get_node_id(Recipient::Node).unwrap();
3971
3972		let flow = OffersMessageFlow::new(
3973			ChainHash::using_genesis_block(params.network), params.best_block,
3974			our_network_pubkey, current_timestamp, expanded_inbound_key,
3975			node_signer.get_receive_auth_key(), secp_ctx.clone(), message_router, logger.clone(),
3976		);
3977
3978		ChannelManager {
3979			config: RwLock::new(config),
3980			chain_hash: ChainHash::using_genesis_block(params.network),
3981			fee_estimator: LowerBoundedFeeEstimator::new(fee_est),
3982			chain_monitor,
3983			tx_broadcaster,
3984			router,
3985			flow,
3986
3987			best_block: RwLock::new(params.best_block),
3988
3989			outbound_scid_aliases: Mutex::new(new_hash_set()),
3990			pending_outbound_payments: OutboundPayments::new(new_hash_map()),
3991			forward_htlcs: Mutex::new(new_hash_map()),
3992			decode_update_add_htlcs: Mutex::new(new_hash_map()),
3993			claimable_payments: Mutex::new(ClaimablePayments { claimable_payments: new_hash_map(), pending_claiming_payments: new_hash_map() }),
3994			pending_intercepted_htlcs: Mutex::new(new_hash_map()),
3995			short_to_chan_info: FairRwLock::new(new_hash_map()),
3996
3997			our_network_pubkey,
3998			secp_ctx,
3999
4000			inbound_payment_key: expanded_inbound_key,
4001			fake_scid_rand_bytes: entropy_source.get_secure_random_bytes(),
4002
4003			probing_cookie_secret: entropy_source.get_secure_random_bytes(),
4004			inbound_payment_id_secret: entropy_source.get_secure_random_bytes(),
4005
4006			highest_seen_timestamp: AtomicUsize::new(current_timestamp as usize),
4007
4008			per_peer_state: FairRwLock::new(new_hash_map()),
4009
4010			#[cfg(not(any(test, feature = "_externalize_tests")))]
4011			monitor_update_type: AtomicUsize::new(0),
4012
4013			pending_events: Mutex::new(VecDeque::new()),
4014			pending_events_processor: AtomicBool::new(false),
4015			pending_htlc_forwards_processor: AtomicBool::new(false),
4016			pending_background_events: Mutex::new(Vec::new()),
4017			total_consistency_lock: RwLock::new(()),
4018			background_events_processed_since_startup: AtomicBool::new(false),
4019			event_persist_notifier: Notifier::new(),
4020			needs_persist_flag: AtomicBool::new(false),
4021			funding_batch_states: Mutex::new(BTreeMap::new()),
4022
4023			pending_broadcast_messages: Mutex::new(Vec::new()),
4024
4025			last_days_feerates: Mutex::new(VecDeque::new()),
4026
4027			entropy_source,
4028			node_signer,
4029			signer_provider,
4030
4031			logger,
4032
4033			#[cfg(feature = "_test_utils")]
4034			testing_dnssec_proof_offer_resolution_override: Mutex::new(new_hash_map()),
4035		}
4036	}
4037
4038	/// Gets the current [`UserConfig`] which controls some global behavior and includes the
4039	/// default configuration applied to all new channels.
4040	pub fn get_current_config(&self) -> UserConfig {
4041		self.config.read().unwrap().clone()
4042	}
4043
4044	/// Updates the current [`UserConfig`] which controls some global behavior and includes the
4045	/// default configuration applied to all new channels.
4046	pub fn set_current_config(&self, new_config: UserConfig) {
4047		*self.config.write().unwrap() = new_config;
4048	}
4049
4050	#[cfg(test)]
4051	pub fn create_and_insert_outbound_scid_alias_for_test(&self) -> u64 {
4052		self.create_and_insert_outbound_scid_alias()
4053	}
4054
4055	fn create_and_insert_outbound_scid_alias(&self) -> u64 {
4056		let height = self.best_block.read().unwrap().height;
4057		let mut outbound_scid_alias = 0;
4058		let mut i = 0;
4059		loop {
4060			// fuzzing chacha20 doesn't use the key at all so we always get the same alias
4061			if cfg!(fuzzing) {
4062				outbound_scid_alias += 1;
4063			} else {
4064				outbound_scid_alias = fake_scid::Namespace::OutboundAlias.get_fake_scid(
4065					height,
4066					&self.chain_hash,
4067					&self.fake_scid_rand_bytes,
4068					&self.entropy_source,
4069				);
4070			}
4071			if outbound_scid_alias != 0
4072				&& self.outbound_scid_aliases.lock().unwrap().insert(outbound_scid_alias)
4073			{
4074				break;
4075			}
4076			i += 1;
4077			if i > 1_000_000 {
4078				panic!("Your RNG is busted or we ran out of possible outbound SCID aliases (which should never happen before we run out of memory to store channels");
4079			}
4080		}
4081		outbound_scid_alias
4082	}
4083
4084	/// Creates a new outbound channel to the given remote node and with the given value.
4085	///
4086	/// `user_channel_id` will be provided back as in
4087	/// [`Event::FundingGenerationReady::user_channel_id`] to allow tracking of which events
4088	/// correspond with which `create_channel` call. Note that the `user_channel_id` defaults to a
4089	/// randomized value for inbound channels. `user_channel_id` has no meaning inside of LDK, it
4090	/// is simply copied to events and otherwise ignored.
4091	///
4092	/// Raises [`APIError::APIMisuseError`] when `channel_value_satoshis` > 2**24 or `push_msat` is
4093	/// greater than `channel_value_satoshis * 1k` or `channel_value_satoshis < 1000`.
4094	///
4095	/// Raises [`APIError::ChannelUnavailable`] if the channel cannot be opened due to failing to
4096	/// generate a shutdown scriptpubkey or destination script set by
4097	/// [`SignerProvider::get_shutdown_scriptpubkey`] or [`SignerProvider::get_destination_script`].
4098	///
4099	/// Note that we do not check if you are currently connected to the given peer. If no
4100	/// connection is available, the outbound `open_channel` message may fail to send, resulting in
4101	/// the channel eventually being silently forgotten (dropped on reload).
4102	///
4103	/// If `temporary_channel_id` is specified, it will be used as the temporary channel ID of the
4104	/// channel. Otherwise, a random one will be generated for you.
4105	///
4106	/// Returns the new Channel's temporary `channel_id`. This ID will appear as
4107	/// [`Event::FundingGenerationReady::temporary_channel_id`] and in
4108	/// [`ChannelDetails::channel_id`] until after
4109	/// [`ChannelManager::funding_transaction_generated`] is called, swapping the Channel's ID for
4110	/// one derived from the funding transaction's TXID. If the counterparty rejects the channel
4111	/// immediately, this temporary ID will appear in [`Event::ChannelClosed::channel_id`].
4112	///
4113	/// [`Event::FundingGenerationReady::user_channel_id`]: events::Event::FundingGenerationReady::user_channel_id
4114	/// [`Event::FundingGenerationReady::temporary_channel_id`]: events::Event::FundingGenerationReady::temporary_channel_id
4115	/// [`Event::ChannelClosed::channel_id`]: events::Event::ChannelClosed::channel_id
4116	#[rustfmt::skip]
4117	pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_channel_id: u128, temporary_channel_id: Option<ChannelId>, override_config: Option<UserConfig>) -> Result<ChannelId, APIError> {
4118		if channel_value_satoshis < 1000 {
4119			return Err(APIError::APIMisuseError { err: format!("Channel value must be at least 1000 satoshis. It was {}", channel_value_satoshis) });
4120		}
4121
4122		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
4123		// We want to make sure the lock is actually acquired by PersistenceNotifierGuard.
4124		debug_assert!(&self.total_consistency_lock.try_write().is_err());
4125
4126		let per_peer_state = self.per_peer_state.read().unwrap();
4127
4128		let peer_state_mutex = per_peer_state.get(&their_network_key)
4129			.ok_or_else(|| APIError::APIMisuseError{ err: format!("Not connected to node: {}", their_network_key) })?;
4130
4131		let mut peer_state = peer_state_mutex.lock().unwrap();
4132		if !peer_state.is_connected {
4133			return Err(APIError::APIMisuseError{ err: format!("Not connected to node: {}", their_network_key) });
4134		}
4135
4136		if let Some(temporary_channel_id) = temporary_channel_id {
4137			if peer_state.channel_by_id.contains_key(&temporary_channel_id) {
4138				return Err(APIError::APIMisuseError{ err: format!("Channel with temporary channel ID {} already exists!", temporary_channel_id)});
4139			}
4140		}
4141
4142		let mut channel = {
4143			let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
4144			let their_features = &peer_state.latest_features;
4145			let config = self.config.read().unwrap();
4146			let config = if let Some(config) = &override_config {
4147				config
4148			} else {
4149				&*config
4150			};
4151			match OutboundV1Channel::new(&self.fee_estimator, &self.entropy_source, &self.signer_provider, their_network_key,
4152				their_features, channel_value_satoshis, push_msat, user_channel_id, config,
4153				self.best_block.read().unwrap().height, outbound_scid_alias, temporary_channel_id, &*self.logger)
4154			{
4155				Ok(res) => res,
4156				Err(e) => {
4157					self.outbound_scid_aliases.lock().unwrap().remove(&outbound_scid_alias);
4158					return Err(e);
4159				},
4160			}
4161		};
4162		let logger = WithChannelContext::from(&self.logger, &channel.context, None);
4163		let res = channel.get_open_channel(self.chain_hash, &&logger);
4164
4165		let temporary_channel_id = channel.context.channel_id();
4166		match peer_state.channel_by_id.entry(temporary_channel_id) {
4167			hash_map::Entry::Occupied(_) => {
4168				if cfg!(fuzzing) {
4169					return Err(APIError::APIMisuseError { err: "Fuzzy bad RNG".to_owned() });
4170				} else {
4171					panic!("RNG is bad???");
4172				}
4173			},
4174			hash_map::Entry::Vacant(entry) => { entry.insert(Channel::from(channel)); }
4175		}
4176
4177		if let Some(msg) = res {
4178			peer_state.pending_msg_events.push(MessageSendEvent::SendOpenChannel {
4179				node_id: their_network_key,
4180				msg,
4181			});
4182		}
4183		Ok(temporary_channel_id)
4184	}
4185
4186	fn list_funded_channels_with_filter<
4187		Fn: FnMut(&(&InitFeatures, &ChannelId, &Channel<SP>)) -> bool,
4188	>(
4189		&self, mut f: Fn,
4190	) -> Vec<ChannelDetails> {
4191		// Allocate our best estimate of the number of channels we have in the `res`
4192		// Vec. Sadly the `short_to_chan_info` map doesn't cover channels without
4193		// a scid or a scid alias. Therefore reallocations may still occur, but is
4194		// unlikely as the `short_to_chan_info` map often contains 2 entries for
4195		// the same channel.
4196		let mut res = Vec::with_capacity(self.short_to_chan_info.read().unwrap().len());
4197		{
4198			let best_block_height = self.best_block.read().unwrap().height;
4199			let per_peer_state = self.per_peer_state.read().unwrap();
4200			for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
4201				let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4202				let peer_state = &mut *peer_state_lock;
4203				// Only `Channels` in the `Channel::Funded` phase can be considered funded.
4204				let filtered_chan_by_id = peer_state
4205					.channel_by_id
4206					.iter()
4207					.map(|(cid, c)| (&peer_state.latest_features, cid, c))
4208					.filter(|(_, _, chan)| chan.is_funded())
4209					.filter(|v| f(v));
4210				res.extend(filtered_chan_by_id.map(|(_, _channel_id, channel)| {
4211					ChannelDetails::from_channel(
4212						channel,
4213						best_block_height,
4214						peer_state.latest_features.clone(),
4215						&self.fee_estimator,
4216					)
4217				}));
4218			}
4219		}
4220		res
4221	}
4222
4223	/// Gets the list of open channels, in random order. See [`ChannelDetails`] field documentation for
4224	/// more information.
4225	pub fn list_channels(&self) -> Vec<ChannelDetails> {
4226		// Allocate our best estimate of the number of channels we have in the `res`
4227		// Vec. Sadly the `short_to_chan_info` map doesn't cover channels without
4228		// a scid or a scid alias. Therefore reallocations may still occur, but is
4229		// unlikely as the `short_to_chan_info` map often contains 2 entries for
4230		// the same channel.
4231		let mut res = Vec::with_capacity(self.short_to_chan_info.read().unwrap().len());
4232		{
4233			let best_block_height = self.best_block.read().unwrap().height;
4234			let per_peer_state = self.per_peer_state.read().unwrap();
4235			for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
4236				let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4237				let peer_state = &mut *peer_state_lock;
4238				for (_, channel) in peer_state.channel_by_id.iter() {
4239					let details = ChannelDetails::from_channel(
4240						channel,
4241						best_block_height,
4242						peer_state.latest_features.clone(),
4243						&self.fee_estimator,
4244					);
4245					res.push(details);
4246				}
4247			}
4248		}
4249		res
4250	}
4251
4252	/// Gets the list of usable channels, in random order. Useful as an argument to
4253	/// [`Router::find_route`] to ensure non-announced channels are used.
4254	///
4255	/// These are guaranteed to have their [`ChannelDetails::is_usable`] value set to true, see the
4256	/// documentation for [`ChannelDetails::is_usable`] for more info on exactly what the criteria
4257	/// are.
4258	pub fn list_usable_channels(&self) -> Vec<ChannelDetails> {
4259		// Note we use is_live here instead of usable which leads to somewhat confused
4260		// internal/external nomenclature, but that's ok cause that's probably what the user
4261		// really wanted anyway.
4262		self.list_funded_channels_with_filter(|&(_, _, ref channel)| channel.context().is_live())
4263	}
4264
4265	/// Gets the list of channels we have with a given counterparty, in random order.
4266	pub fn list_channels_with_counterparty(
4267		&self, counterparty_node_id: &PublicKey,
4268	) -> Vec<ChannelDetails> {
4269		let best_block_height = self.best_block.read().unwrap().height;
4270		let per_peer_state = self.per_peer_state.read().unwrap();
4271
4272		if let Some(peer_state_mutex) = per_peer_state.get(counterparty_node_id) {
4273			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4274			let peer_state = &mut *peer_state_lock;
4275			let features = &peer_state.latest_features;
4276			let channel_to_details = |channel| {
4277				ChannelDetails::from_channel(
4278					channel,
4279					best_block_height,
4280					features.clone(),
4281					&self.fee_estimator,
4282				)
4283			};
4284			let chan_by_id = peer_state.channel_by_id.iter();
4285			return chan_by_id.map(|(_, chan)| chan).map(channel_to_details).collect();
4286		}
4287		vec![]
4288	}
4289
4290	/// Returns in an undefined order recent payments that -- if not fulfilled -- have yet to find a
4291	/// successful path, or have unresolved HTLCs.
4292	///
4293	/// This can be useful for payments that may have been prepared, but ultimately not sent, as a
4294	/// result of a crash. If such a payment exists, is not listed here, and an
4295	/// [`Event::PaymentSent`] has not been received, you may consider resending the payment.
4296	///
4297	/// [`Event::PaymentSent`]: events::Event::PaymentSent
4298	#[rustfmt::skip]
4299	pub fn list_recent_payments(&self) -> Vec<RecentPaymentDetails> {
4300		self.pending_outbound_payments.pending_outbound_payments.lock().unwrap().iter()
4301			.filter_map(|(payment_id, pending_outbound_payment)| match pending_outbound_payment {
4302				PendingOutboundPayment::AwaitingInvoice { .. }
4303					| PendingOutboundPayment::AwaitingOffer { .. }
4304					// InvoiceReceived is an intermediate state and doesn't need to be exposed
4305					| PendingOutboundPayment::InvoiceReceived { .. } =>
4306				{
4307					Some(RecentPaymentDetails::AwaitingInvoice { payment_id: *payment_id })
4308				},
4309				PendingOutboundPayment::StaticInvoiceReceived { .. } => {
4310					Some(RecentPaymentDetails::AwaitingInvoice { payment_id: *payment_id })
4311				},
4312				PendingOutboundPayment::Retryable { payment_hash, total_msat, .. } => {
4313					Some(RecentPaymentDetails::Pending {
4314						payment_id: *payment_id,
4315						payment_hash: *payment_hash,
4316						total_msat: *total_msat,
4317					})
4318				},
4319				PendingOutboundPayment::Abandoned { payment_hash, .. } => {
4320					Some(RecentPaymentDetails::Abandoned { payment_id: *payment_id, payment_hash: *payment_hash })
4321				},
4322				PendingOutboundPayment::Fulfilled { payment_hash, .. } => {
4323					Some(RecentPaymentDetails::Fulfilled { payment_id: *payment_id, payment_hash: *payment_hash })
4324				},
4325				PendingOutboundPayment::Legacy { .. } => None
4326			})
4327			.collect()
4328	}
4329
4330	#[rustfmt::skip]
4331	fn close_channel_internal(&self, chan_id: &ChannelId, counterparty_node_id: &PublicKey, target_feerate_sats_per_1000_weight: Option<u32>, override_shutdown_script: Option<ShutdownScript>) -> Result<(), APIError> {
4332		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
4333
4334		let mut failed_htlcs: Vec<(HTLCSource, PaymentHash)> = Vec::new();
4335		let mut shutdown_result = Ok(());
4336
4337		{
4338			let per_peer_state = self.per_peer_state.read().unwrap();
4339
4340			let peer_state_mutex = per_peer_state.get(counterparty_node_id)
4341				.ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}") })?;
4342
4343			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4344			let peer_state = &mut *peer_state_lock;
4345
4346			match peer_state.channel_by_id.entry(*chan_id) {
4347				hash_map::Entry::Occupied(mut chan_entry) => {
4348					if !chan_entry.get().context().is_connected() {
4349						return Err(APIError::ChannelUnavailable {
4350							err: "Cannot begin shutdown while peer is disconnected, maybe force-close instead?".to_owned(),
4351						});
4352					}
4353
4354					if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
4355						let funding_txo_opt = chan.funding.get_funding_txo();
4356						let their_features = &peer_state.latest_features;
4357						let (shutdown_msg, mut monitor_update_opt, htlcs) =
4358							chan.get_shutdown(&self.signer_provider, their_features, target_feerate_sats_per_1000_weight, override_shutdown_script)?;
4359						failed_htlcs = htlcs;
4360
4361						// We can send the `shutdown` message before updating the `ChannelMonitor`
4362						// here as we don't need the monitor update to complete until we send a
4363						// `shutdown_signed`, which we'll delay if we're pending a monitor update.
4364						peer_state.pending_msg_events.push(MessageSendEvent::SendShutdown {
4365							node_id: *counterparty_node_id,
4366							msg: shutdown_msg,
4367						});
4368
4369						debug_assert!(monitor_update_opt.is_none() || !chan.is_shutdown(),
4370							"We can't both complete shutdown and generate a monitor update");
4371
4372						// Update the monitor with the shutdown script if necessary.
4373						if let Some(monitor_update) = monitor_update_opt.take() {
4374							handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
4375								peer_state_lock, peer_state, per_peer_state, chan);
4376						}
4377					} else {
4378						let reason = ClosureReason::LocallyCoopClosedUnfundedChannel;
4379						let err = ChannelError::Close((reason.to_string(), reason));
4380						let mut chan = chan_entry.remove();
4381						let (_, mut e) = convert_channel_err!(self, peer_state, err, &mut chan);
4382						e.dont_send_error_message();
4383						shutdown_result = Err(e);
4384					}
4385				},
4386				hash_map::Entry::Vacant(_) => {
4387					return Err(APIError::ChannelUnavailable {
4388						err: format!(
4389							"Channel with id {} not found for the passed counterparty node_id {}",
4390							chan_id, counterparty_node_id,
4391						)
4392					});
4393				},
4394			}
4395		}
4396
4397		for htlc_source in failed_htlcs.drain(..) {
4398			let failure_reason = LocalHTLCFailureReason::ChannelClosed;
4399			let reason = HTLCFailReason::from_failure_code(failure_reason);
4400			let receiver = HTLCHandlingFailureType::Forward { node_id: Some(*counterparty_node_id), channel_id: *chan_id };
4401			let (source, hash) = htlc_source;
4402			self.fail_htlc_backwards_internal(&source, &hash, &reason, receiver, None);
4403		}
4404
4405		let _ = handle_error!(self, shutdown_result, *counterparty_node_id);
4406
4407		Ok(())
4408	}
4409
4410	/// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
4411	/// will be accepted on the given channel, and after additional timeout/the closing of all
4412	/// pending HTLCs, the channel will be closed on chain.
4413	///
4414	///  * If we are the channel initiator, we will pay between our [`ChannelCloseMinimum`] and
4415	///    [`ChannelConfig::force_close_avoidance_max_fee_satoshis`] plus our [`NonAnchorChannelFee`]
4416	///    fee estimate.
4417	///  * If our counterparty is the channel initiator, we will require a channel closing
4418	///    transaction feerate of at least our [`ChannelCloseMinimum`] feerate or the feerate which
4419	///    would appear on a force-closure transaction, whichever is lower. We will allow our
4420	///    counterparty to pay as much fee as they'd like, however.
4421	///
4422	/// May generate a [`SendShutdown`] message event on success, which should be relayed.
4423	///
4424	/// Raises [`APIError::ChannelUnavailable`] if the channel cannot be closed due to failing to
4425	/// generate a shutdown scriptpubkey or destination script set by
4426	/// [`SignerProvider::get_shutdown_scriptpubkey`]. A force-closure may be needed to close the
4427	/// channel.
4428	///
4429	/// [`ChannelConfig::force_close_avoidance_max_fee_satoshis`]: crate::util::config::ChannelConfig::force_close_avoidance_max_fee_satoshis
4430	/// [`ChannelCloseMinimum`]: crate::chain::chaininterface::ConfirmationTarget::ChannelCloseMinimum
4431	/// [`NonAnchorChannelFee`]: crate::chain::chaininterface::ConfirmationTarget::NonAnchorChannelFee
4432	/// [`SendShutdown`]: MessageSendEvent::SendShutdown
4433	pub fn close_channel(
4434		&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
4435	) -> Result<(), APIError> {
4436		self.close_channel_internal(channel_id, counterparty_node_id, None, None)
4437	}
4438
4439	/// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
4440	/// will be accepted on the given channel, and after additional timeout/the closing of all
4441	/// pending HTLCs, the channel will be closed on chain.
4442	///
4443	/// `target_feerate_sat_per_1000_weight` has different meanings depending on if we initiated
4444	/// the channel being closed or not:
4445	///  * If we are the channel initiator, we will pay at least this feerate on the closing
4446	///    transaction. The upper-bound is set by
4447	///    [`ChannelConfig::force_close_avoidance_max_fee_satoshis`] plus our [`NonAnchorChannelFee`]
4448	///    fee estimate (or `target_feerate_sat_per_1000_weight`, if it is greater).
4449	///  * If our counterparty is the channel initiator, we will refuse to accept a channel closure
4450	///    transaction feerate below `target_feerate_sat_per_1000_weight` (or the feerate which
4451	///    will appear on a force-closure transaction, whichever is lower).
4452	///
4453	/// The `shutdown_script` provided  will be used as the `scriptPubKey` for the closing transaction.
4454	/// Will fail if a shutdown script has already been set for this channel by
4455	/// [`ChannelHandshakeConfig::commit_upfront_shutdown_pubkey`]. The given shutdown script must
4456	/// also be compatible with our and the counterparty's features.
4457	///
4458	/// May generate a [`SendShutdown`] message event on success, which should be relayed.
4459	///
4460	/// Raises [`APIError::ChannelUnavailable`] if the channel cannot be closed due to failing to
4461	/// generate a shutdown scriptpubkey or destination script set by
4462	/// [`SignerProvider::get_shutdown_scriptpubkey`]. A force-closure may be needed to close the
4463	/// channel.
4464	///
4465	/// [`ChannelConfig::force_close_avoidance_max_fee_satoshis`]: crate::util::config::ChannelConfig::force_close_avoidance_max_fee_satoshis
4466	/// [`NonAnchorChannelFee`]: crate::chain::chaininterface::ConfirmationTarget::NonAnchorChannelFee
4467	/// [`ChannelHandshakeConfig::commit_upfront_shutdown_pubkey`]: crate::util::config::ChannelHandshakeConfig::commit_upfront_shutdown_pubkey
4468	/// [`SendShutdown`]: MessageSendEvent::SendShutdown
4469	pub fn close_channel_with_feerate_and_script(
4470		&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
4471		target_feerate_sats_per_1000_weight: Option<u32>, shutdown_script: Option<ShutdownScript>,
4472	) -> Result<(), APIError> {
4473		self.close_channel_internal(
4474			channel_id,
4475			counterparty_node_id,
4476			target_feerate_sats_per_1000_weight,
4477			shutdown_script,
4478		)
4479	}
4480
4481	/// Applies a [`ChannelMonitorUpdate`] which may or may not be for a channel which is closed.
4482	#[rustfmt::skip]
4483	fn apply_post_close_monitor_update(
4484		&self, counterparty_node_id: PublicKey, channel_id: ChannelId, funding_txo: OutPoint,
4485		monitor_update: ChannelMonitorUpdate,
4486	) {
4487		// Note that there may be some post-close updates which need to be well-ordered with
4488		// respect to the `update_id`, so we hold the `peer_state` lock here.
4489		let per_peer_state = self.per_peer_state.read().unwrap();
4490		let mut peer_state_lock = per_peer_state.get(&counterparty_node_id)
4491			.expect("We must always have a peer entry for a peer with which we have channels that have ChannelMonitors")
4492			.lock().unwrap();
4493		let peer_state = &mut *peer_state_lock;
4494		match peer_state.channel_by_id.entry(channel_id) {
4495			hash_map::Entry::Occupied(mut chan_entry) => {
4496				if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
4497					handle_new_monitor_update!(self, funding_txo,
4498						monitor_update, peer_state_lock, peer_state, per_peer_state, chan);
4499					return;
4500				} else {
4501					debug_assert!(false, "We shouldn't have an update for a non-funded channel");
4502				}
4503			},
4504			hash_map::Entry::Vacant(_) => {},
4505		}
4506
4507		handle_new_monitor_update!(
4508			self, funding_txo, monitor_update, peer_state_lock, peer_state, per_peer_state,
4509			counterparty_node_id, channel_id, POST_CHANNEL_CLOSE
4510		);
4511	}
4512
4513	/// When a channel is removed, two things need to happen:
4514	/// (a) [`convert_channel_err`] must be called in the same `per_peer_state` lock as the
4515	///     channel-closing action,
4516	/// (b) [`handle_error`] needs to be called without holding any locks (except
4517	///     [`ChannelManager::total_consistency_lock`]), which then calls this.
4518	#[rustfmt::skip]
4519	fn finish_close_channel(&self, mut shutdown_res: ShutdownResult) {
4520		debug_assert_ne!(self.per_peer_state.held_by_thread(), LockHeldState::HeldByThread);
4521		#[cfg(debug_assertions)]
4522		for (_, peer) in self.per_peer_state.read().unwrap().iter() {
4523			debug_assert_ne!(peer.held_by_thread(), LockHeldState::HeldByThread);
4524		}
4525
4526		let logger = WithContext::from(
4527			&self.logger, Some(shutdown_res.counterparty_node_id), Some(shutdown_res.channel_id), None
4528		);
4529
4530		log_debug!(logger, "Finishing closure of channel due to {} with {} HTLCs to fail",
4531			shutdown_res.closure_reason, shutdown_res.dropped_outbound_htlcs.len());
4532		for htlc_source in shutdown_res.dropped_outbound_htlcs.drain(..) {
4533			let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
4534			let failure_reason = LocalHTLCFailureReason::ChannelClosed;
4535			let reason = HTLCFailReason::from_failure_code(failure_reason);
4536			let receiver = HTLCHandlingFailureType::Forward { node_id: Some(counterparty_node_id), channel_id };
4537			self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver, None);
4538		}
4539		if let Some((_, funding_txo, _channel_id, monitor_update)) = shutdown_res.monitor_update {
4540			debug_assert!(false, "This should have been handled in `locked_close_channel`");
4541			self.apply_post_close_monitor_update(shutdown_res.counterparty_node_id, shutdown_res.channel_id, funding_txo, monitor_update);
4542		}
4543		if self.background_events_processed_since_startup.load(Ordering::Acquire) {
4544			// If a `ChannelMonitorUpdate` was applied (i.e. any time we have a funding txo and are
4545			// not in the startup sequence) check if we need to handle any
4546			// `MonitorUpdateCompletionAction`s.
4547			// TODO: If we do the `in_flight_monitor_updates.is_empty()` check in
4548			// `locked_close_channel` we can skip the locks here.
4549			if shutdown_res.channel_funding_txo.is_some() {
4550				self.channel_monitor_updated(&shutdown_res.channel_id, None, &shutdown_res.counterparty_node_id);
4551			}
4552		}
4553		let mut shutdown_results: Vec<(Result<Infallible, _>, _)> = Vec::new();
4554		if let Some(txid) = shutdown_res.unbroadcasted_batch_funding_txid {
4555			let mut funding_batch_states = self.funding_batch_states.lock().unwrap();
4556			let affected_channels = funding_batch_states.remove(&txid).into_iter().flatten();
4557			let per_peer_state = self.per_peer_state.read().unwrap();
4558			let mut has_uncompleted_channel = None;
4559			for (channel_id, counterparty_node_id, state) in affected_channels {
4560				if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
4561					let mut peer_state = peer_state_mutex.lock().unwrap();
4562					if let Some(mut chan) = peer_state.channel_by_id.remove(&channel_id) {
4563						let reason = ClosureReason::FundingBatchClosure;
4564						let err = ChannelError::Close((reason.to_string(), reason));
4565						let (_, e) = convert_channel_err!(self, peer_state, err, &mut chan);
4566						shutdown_results.push((Err(e), counterparty_node_id));
4567					}
4568				}
4569				has_uncompleted_channel = Some(has_uncompleted_channel.map_or(!state, |v| v || !state));
4570			}
4571			debug_assert!(
4572				has_uncompleted_channel.unwrap_or(true),
4573				"Closing a batch where all channels have completed initial monitor update",
4574			);
4575		}
4576
4577		{
4578			let mut pending_events = self.pending_events.lock().unwrap();
4579			pending_events.push_back((events::Event::ChannelClosed {
4580				channel_id: shutdown_res.channel_id,
4581				user_channel_id: shutdown_res.user_channel_id,
4582				reason: shutdown_res.closure_reason,
4583				counterparty_node_id: Some(shutdown_res.counterparty_node_id),
4584				channel_capacity_sats: Some(shutdown_res.channel_capacity_satoshis),
4585				channel_funding_txo: shutdown_res.channel_funding_txo,
4586				last_local_balance_msat: Some(shutdown_res.last_local_balance_msat),
4587			}, None));
4588
4589			if let Some(splice_funding_failed) = shutdown_res.splice_funding_failed.take() {
4590				pending_events.push_back((events::Event::SpliceFailed {
4591					channel_id: shutdown_res.channel_id,
4592					counterparty_node_id: shutdown_res.counterparty_node_id,
4593					user_channel_id: shutdown_res.user_channel_id,
4594					abandoned_funding_txo: splice_funding_failed.funding_txo,
4595					channel_type: splice_funding_failed.channel_type,
4596					contributed_inputs: splice_funding_failed.contributed_inputs,
4597					contributed_outputs: splice_funding_failed.contributed_outputs,
4598				}, None));
4599			}
4600
4601			if let Some(transaction) = shutdown_res.unbroadcasted_funding_tx {
4602				let funding_info = if shutdown_res.is_manual_broadcast {
4603					FundingInfo::OutPoint {
4604						outpoint: shutdown_res.channel_funding_txo
4605							.expect("We had an unbroadcasted funding tx, so should also have had a funding outpoint"),
4606					}
4607				} else {
4608					FundingInfo::Tx{ transaction }
4609				};
4610				pending_events.push_back((events::Event::DiscardFunding {
4611					channel_id: shutdown_res.channel_id, funding_info
4612				}, None));
4613			}
4614		}
4615		for (err, counterparty_node_id) in shutdown_results.drain(..) {
4616			let _ = handle_error!(self, err, counterparty_node_id);
4617		}
4618	}
4619
4620	/// `peer_msg` should be set when we receive a message from a peer, but not set when the
4621	/// user closes, which will be re-exposed as the `ChannelClosed` reason.
4622	#[rustfmt::skip]
4623	fn force_close_channel_with_peer(&self, channel_id: &ChannelId, peer_node_id: &PublicKey, reason: ClosureReason)
4624	-> Result<(), APIError> {
4625		let per_peer_state = self.per_peer_state.read().unwrap();
4626		let peer_state_mutex = per_peer_state.get(peer_node_id)
4627			.ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", peer_node_id) })?;
4628		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4629		let peer_state = &mut *peer_state_lock;
4630		let logger = WithContext::from(&self.logger, Some(*peer_node_id), Some(*channel_id), None);
4631
4632		let is_from_counterparty = matches!(reason, ClosureReason::CounterpartyForceClosed { .. });
4633		let message = match &reason {
4634			ClosureReason::HolderForceClosed { message, .. } => message.clone(),
4635			_ => reason.to_string(),
4636		};
4637
4638		if let Some(mut chan) = peer_state.channel_by_id.remove(channel_id) {
4639			log_error!(logger, "Force-closing channel {}", channel_id);
4640			let err = ChannelError::Close((message, reason));
4641			let (_, mut e) = convert_channel_err!(self, peer_state, err, &mut chan);
4642			mem::drop(peer_state_lock);
4643			mem::drop(per_peer_state);
4644			if is_from_counterparty {
4645				// If the peer is the one who asked us to force-close, don't reply with a fresh
4646				// error message.
4647				e.dont_send_error_message();
4648			}
4649			let _ = handle_error!(self, Err::<(), _>(e), *peer_node_id);
4650			Ok(())
4651		} else if peer_state.inbound_channel_request_by_id.remove(channel_id).is_some() {
4652			log_error!(logger, "Force-closing inbound channel request {}", &channel_id);
4653			if !is_from_counterparty && peer_state.is_connected {
4654				peer_state.pending_msg_events.push(
4655					MessageSendEvent::HandleError {
4656						node_id: *peer_node_id,
4657						action: msgs::ErrorAction::SendErrorMessage {
4658							msg: msgs::ErrorMessage { channel_id: *channel_id, data: message }
4659						},
4660					}
4661				);
4662			}
4663			// N.B. that we don't send any channel close event here: we
4664			// don't have a user_channel_id, and we never sent any opening
4665			// events anyway.
4666			Ok(())
4667		} else {
4668			Err(APIError::ChannelUnavailable{ err: format!("Channel with id {} not found for the passed counterparty node_id {}", channel_id, peer_node_id) })
4669		}
4670	}
4671
4672	#[rustfmt::skip]
4673	fn force_close_sending_error(&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, error_message: String)
4674	-> Result<(), APIError> {
4675		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
4676		log_debug!(self.logger,
4677			"Force-closing channel, The error message sent to the peer : {}", error_message);
4678		// No matter what value for `broadcast_latest_txn` we set here, `Channel` will override it
4679		// and set the appropriate value.
4680		let reason = ClosureReason::HolderForceClosed {
4681			broadcasted_latest_txn: Some(true),
4682			message: error_message,
4683		};
4684		self.force_close_channel_with_peer(channel_id, &counterparty_node_id, reason)
4685	}
4686
4687	/// Force closes a channel, immediately broadcasting the latest local transaction(s),
4688	/// rejecting new HTLCs.
4689	///
4690	/// The provided `error_message` is sent to connected peers for closing
4691	/// channels and should be a human-readable description of what went wrong.
4692	///
4693	/// Fails if `channel_id` is unknown to the manager, or if the `counterparty_node_id`
4694	/// isn't the counterparty of the corresponding channel.
4695	pub fn force_close_broadcasting_latest_txn(
4696		&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, error_message: String,
4697	) -> Result<(), APIError> {
4698		self.force_close_sending_error(channel_id, counterparty_node_id, error_message)
4699	}
4700
4701	/// Force close all channels, immediately broadcasting the latest local commitment transaction
4702	/// for each to the chain and rejecting new HTLCs on each.
4703	///
4704	/// The provided `error_message` is sent to connected peers for closing channels and should
4705	/// be a human-readable description of what went wrong.
4706	pub fn force_close_all_channels_broadcasting_latest_txn(&self, error_message: String) {
4707		for chan in self.list_channels() {
4708			let _ = self.force_close_broadcasting_latest_txn(
4709				&chan.channel_id,
4710				&chan.counterparty.node_id,
4711				error_message.clone(),
4712			);
4713		}
4714	}
4715
4716	/// Initiate a splice in order to add value to (splice-in) or remove value from (splice-out)
4717	/// the channel. This will spend the channel's funding transaction output, effectively replacing
4718	/// it with a new one.
4719	///
4720	/// # Required Feature Flags
4721	///
4722	/// Initiating a splice requires that the channel counterparty supports splicing. Any
4723	/// channel (no matter the type) can be spliced, as long as the counterparty is currently
4724	/// connected.
4725	///
4726	/// # Arguments
4727	///
4728	/// Provide a `contribution` to determine if value is spliced in or out. The splice initiator is
4729	/// responsible for paying fees for common fields, shared inputs, and shared outputs along with
4730	/// any contributed inputs and outputs. Fees are determined using `funding_feerate_per_kw` and
4731	/// must be covered by the supplied inputs for splice-in or the channel balance for splice-out.
4732	///
4733	/// An optional `locktime` for the funding transaction may be specified. If not given, the
4734	/// current best block height is used.
4735	///
4736	/// # Events
4737	///
4738	/// Once the funding transaction has been constructed, an [`Event::SplicePending`] will be
4739	/// emitted. At this point, any inputs contributed to the splice can only be re-spent if an
4740	/// [`Event::DiscardFunding`] is seen.
4741	///
4742	/// After initial signatures have been exchanged, [`Event::FundingTransactionReadyForSigning`]
4743	/// will be generated and [`ChannelManager::funding_transaction_signed`] should be called.
4744	///
4745	/// If any failures occur while negotiating the funding transaction, an [`Event::SpliceFailed`]
4746	/// will be emitted. Any contributed inputs no longer used will be included here and thus can
4747	/// be re-spent.
4748	///
4749	/// Once the splice has been locked by both counterparties, an [`Event::ChannelReady`] will be
4750	/// emitted with the new funding output. At this point, a new splice can be negotiated by
4751	/// calling `splice_channel` again on this channel.
4752	#[rustfmt::skip]
4753	pub fn splice_channel(
4754		&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
4755		contribution: SpliceContribution, funding_feerate_per_kw: u32, locktime: Option<u32>,
4756	) -> Result<(), APIError> {
4757		let mut res = Ok(());
4758		PersistenceNotifierGuard::optionally_notify(self, || {
4759			let result = self.internal_splice_channel(
4760				channel_id, counterparty_node_id, contribution, funding_feerate_per_kw, locktime
4761			);
4762			res = result;
4763			match res {
4764				Ok(_) => NotifyOption::DoPersist,
4765				Err(_) => NotifyOption::SkipPersistNoEvents,
4766			}
4767		});
4768		res
4769	}
4770
4771	fn internal_splice_channel(
4772		&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
4773		contribution: SpliceContribution, funding_feerate_per_kw: u32, locktime: Option<u32>,
4774	) -> Result<(), APIError> {
4775		let per_peer_state = self.per_peer_state.read().unwrap();
4776
4777		let peer_state_mutex = match per_peer_state.get(counterparty_node_id).ok_or_else(|| {
4778			APIError::ChannelUnavailable {
4779				err: format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"),
4780			}
4781		}) {
4782			Ok(p) => p,
4783			Err(e) => return Err(e),
4784		};
4785
4786		let mut peer_state = peer_state_mutex.lock().unwrap();
4787		if !peer_state.latest_features.supports_splicing() {
4788			return Err(APIError::ChannelUnavailable {
4789				err: "Peer does not support splicing".to_owned(),
4790			});
4791		}
4792		if !peer_state.latest_features.supports_quiescence() {
4793			return Err(APIError::ChannelUnavailable {
4794				err: "Peer does not support quiescence, a splicing prerequisite".to_owned(),
4795			});
4796		}
4797
4798		// Look for the channel
4799		match peer_state.channel_by_id.entry(*channel_id) {
4800			hash_map::Entry::Occupied(mut chan_phase_entry) => {
4801				let locktime = locktime.unwrap_or_else(|| self.current_best_block().height);
4802				if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
4803					let logger = WithChannelContext::from(&self.logger, &chan.context, None);
4804					let msg_opt = chan.splice_channel(
4805						contribution,
4806						funding_feerate_per_kw,
4807						locktime,
4808						&&logger,
4809					)?;
4810					if let Some(msg) = msg_opt {
4811						peer_state.pending_msg_events.push(MessageSendEvent::SendStfu {
4812							node_id: *counterparty_node_id,
4813							msg,
4814						});
4815					}
4816					Ok(())
4817				} else {
4818					Err(APIError::ChannelUnavailable {
4819						err: format!(
4820							"Channel with id {} is not funded, cannot splice it",
4821							channel_id
4822						),
4823					})
4824				}
4825			},
4826			hash_map::Entry::Vacant(_) => Err(APIError::ChannelUnavailable {
4827				err: format!(
4828					"Channel with id {} not found for the passed counterparty node_id {}",
4829					channel_id, counterparty_node_id,
4830				),
4831			}),
4832		}
4833	}
4834
4835	#[cfg(test)]
4836	pub(crate) fn abandon_splice(
4837		&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
4838	) -> Result<(), APIError> {
4839		let mut res = Ok(());
4840		PersistenceNotifierGuard::optionally_notify(self, || {
4841			let result = self.internal_abandon_splice(channel_id, counterparty_node_id);
4842			res = result;
4843			match res {
4844				Ok(_) => NotifyOption::SkipPersistHandleEvents,
4845				Err(_) => NotifyOption::SkipPersistNoEvents,
4846			}
4847		});
4848		res
4849	}
4850
4851	#[cfg(test)]
4852	fn internal_abandon_splice(
4853		&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
4854	) -> Result<(), APIError> {
4855		let per_peer_state = self.per_peer_state.read().unwrap();
4856
4857		let peer_state_mutex = match per_peer_state.get(counterparty_node_id).ok_or_else(|| {
4858			APIError::ChannelUnavailable {
4859				err: format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"),
4860			}
4861		}) {
4862			Ok(p) => p,
4863			Err(e) => return Err(e),
4864		};
4865
4866		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4867		let peer_state = &mut *peer_state_lock;
4868
4869		// Look for the channel
4870		match peer_state.channel_by_id.entry(*channel_id) {
4871			hash_map::Entry::Occupied(mut chan_phase_entry) => {
4872				if !chan_phase_entry.get().context().is_connected() {
4873					// TODO: We should probably support this, but right now `splice_channel` refuses when
4874					// the peer is disconnected, so we just check it here.
4875					return Err(APIError::ChannelUnavailable {
4876						err: "Cannot abandon splice while peer is disconnected".to_owned(),
4877					});
4878				}
4879
4880				if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
4881					let (tx_abort, splice_funding_failed) = chan.abandon_splice()?;
4882
4883					peer_state.pending_msg_events.push(MessageSendEvent::SendTxAbort {
4884						node_id: *counterparty_node_id,
4885						msg: tx_abort,
4886					});
4887
4888					if let Some(splice_funding_failed) = splice_funding_failed {
4889						let pending_events = &mut self.pending_events.lock().unwrap();
4890						pending_events.push_back((
4891							events::Event::SpliceFailed {
4892								channel_id: *channel_id,
4893								counterparty_node_id: *counterparty_node_id,
4894								user_channel_id: chan.context.get_user_id(),
4895								abandoned_funding_txo: splice_funding_failed.funding_txo,
4896								channel_type: splice_funding_failed.channel_type,
4897								contributed_inputs: splice_funding_failed.contributed_inputs,
4898								contributed_outputs: splice_funding_failed.contributed_outputs,
4899							},
4900							None,
4901						));
4902					}
4903
4904					Ok(())
4905				} else {
4906					Err(APIError::ChannelUnavailable {
4907						err: format!(
4908							"Channel with id {} is not funded, cannot abandon splice",
4909							channel_id
4910						),
4911					})
4912				}
4913			},
4914			hash_map::Entry::Vacant(_) => Err(APIError::ChannelUnavailable {
4915				err: format!(
4916					"Channel with id {} not found for the passed counterparty node_id {}",
4917					channel_id, counterparty_node_id,
4918				),
4919			}),
4920		}
4921	}
4922
4923	#[rustfmt::skip]
4924	fn can_forward_htlc_to_outgoing_channel(
4925		&self, chan: &mut FundedChannel<SP>, msg: &msgs::UpdateAddHTLC, next_packet: &NextPacketDetails
4926	) -> Result<(), LocalHTLCFailureReason> {
4927		if !chan.context.should_announce()
4928			&& !self.config.read().unwrap().accept_forwards_to_priv_channels
4929		{
4930			// Note that the behavior here should be identical to the above block - we
4931			// should NOT reveal the existence or non-existence of a private channel if
4932			// we don't allow forwards outbound over them.
4933			return Err(LocalHTLCFailureReason::PrivateChannelForward);
4934		}
4935		if let HopConnector::ShortChannelId(outgoing_scid) = next_packet.outgoing_connector {
4936			if chan.funding.get_channel_type().supports_scid_privacy() && outgoing_scid != chan.context.outbound_scid_alias() {
4937				// `option_scid_alias` (referred to in LDK as `scid_privacy`) means
4938				// "refuse to forward unless the SCID alias was used", so we pretend
4939				// we don't have the channel here.
4940				return Err(LocalHTLCFailureReason::RealSCIDForward);
4941			}
4942		} else {
4943			return Err(LocalHTLCFailureReason::InvalidTrampolineForward);
4944		}
4945
4946		// Note that we could technically not return an error yet here and just hope
4947		// that the connection is reestablished or monitor updated by the time we get
4948		// around to doing the actual forward, but better to fail early if we can and
4949		// hopefully an attacker trying to path-trace payments cannot make this occur
4950		// on a small/per-node/per-channel scale.
4951		if !chan.context.is_live() {
4952			if !chan.context.is_enabled() {
4953				return Err(LocalHTLCFailureReason::ChannelDisabled);
4954			} else {
4955				return Err(LocalHTLCFailureReason::ChannelNotReady);
4956			}
4957		}
4958		if next_packet.outgoing_amt_msat < chan.context.get_counterparty_htlc_minimum_msat() {
4959			return Err(LocalHTLCFailureReason::AmountBelowMinimum);
4960		}
4961		chan.htlc_satisfies_config(msg, next_packet.outgoing_amt_msat, next_packet.outgoing_cltv_value)?;
4962
4963		Ok(())
4964	}
4965
4966	/// Executes a callback `C` that returns some value `X` on the channel found with the given
4967	/// `scid`. `None` is returned when the channel is not found.
4968	fn do_funded_channel_callback<X, C: Fn(&mut FundedChannel<SP>) -> X>(
4969		&self, scid: u64, callback: C,
4970	) -> Option<X> {
4971		let (counterparty_node_id, channel_id) =
4972			match self.short_to_chan_info.read().unwrap().get(&scid).cloned() {
4973				None => return None,
4974				Some((cp_id, id)) => (cp_id, id),
4975			};
4976		let per_peer_state = self.per_peer_state.read().unwrap();
4977		let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
4978		if peer_state_mutex_opt.is_none() {
4979			return None;
4980		}
4981		let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
4982		let peer_state = &mut *peer_state_lock;
4983		match peer_state.channel_by_id.get_mut(&channel_id).and_then(Channel::as_funded_mut) {
4984			None => None,
4985			Some(chan) => Some(callback(chan)),
4986		}
4987	}
4988
4989	#[rustfmt::skip]
4990	fn can_forward_htlc(
4991		&self, msg: &msgs::UpdateAddHTLC, next_packet_details: &NextPacketDetails
4992	) -> Result<(), LocalHTLCFailureReason> {
4993		let outgoing_scid = match next_packet_details.outgoing_connector {
4994			HopConnector::ShortChannelId(scid) => scid,
4995			HopConnector::Trampoline(_) => {
4996				return Err(LocalHTLCFailureReason::InvalidTrampolineForward);
4997			}
4998		};
4999		match self.do_funded_channel_callback(outgoing_scid, |chan: &mut FundedChannel<SP>| {
5000			self.can_forward_htlc_to_outgoing_channel(chan, msg, next_packet_details)
5001		}) {
5002			Some(Ok(())) => {},
5003			Some(Err(e)) => return Err(e),
5004			None => {
5005				// If we couldn't find the channel info for the scid, it may be a phantom or
5006				// intercept forward.
5007				if (self.config.read().unwrap().accept_intercept_htlcs &&
5008					fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, outgoing_scid, &self.chain_hash)) ||
5009					fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, outgoing_scid, &self.chain_hash)
5010				{} else {
5011					return Err(LocalHTLCFailureReason::UnknownNextPeer);
5012				}
5013			}
5014		}
5015
5016		let cur_height = self.best_block.read().unwrap().height + 1;
5017		check_incoming_htlc_cltv(cur_height, next_packet_details.outgoing_cltv_value, msg.cltv_expiry)?;
5018
5019		Ok(())
5020	}
5021
5022	#[rustfmt::skip]
5023	fn htlc_failure_from_update_add_err(
5024		&self, msg: &msgs::UpdateAddHTLC, counterparty_node_id: &PublicKey,
5025		reason: LocalHTLCFailureReason, is_intro_node_blinded_forward: bool,
5026		shared_secret: &[u8; 32]
5027	) -> HTLCFailureMsg {
5028		// at capacity, we write fields `htlc_msat` and `len`
5029		let mut res = VecWriter(Vec::with_capacity(8 + 2));
5030		if reason.is_temporary() {
5031			if reason == LocalHTLCFailureReason::AmountBelowMinimum ||
5032				reason == LocalHTLCFailureReason::FeeInsufficient {
5033				msg.amount_msat.write(&mut res).expect("Writes cannot fail");
5034			}
5035			else if reason == LocalHTLCFailureReason::IncorrectCLTVExpiry {
5036				msg.cltv_expiry.write(&mut res).expect("Writes cannot fail");
5037			}
5038			else if reason == LocalHTLCFailureReason::ChannelDisabled {
5039				// TODO: underspecified, follow https://github.com/lightning/bolts/issues/791
5040				0u16.write(&mut res).expect("Writes cannot fail");
5041			}
5042			// See https://github.com/lightning/bolts/blob/247e83d/04-onion-routing.md?plain=1#L1414-L1415
5043			(0u16).write(&mut res).expect("Writes cannot fail");
5044		}
5045
5046		log_info!(
5047			WithContext::from(&self.logger, Some(*counterparty_node_id), Some(msg.channel_id), Some(msg.payment_hash)),
5048			"Failed to accept/forward incoming HTLC: {:?}", reason,
5049		);
5050		// If `msg.blinding_point` is set, we must always fail with malformed.
5051		if msg.blinding_point.is_some() {
5052			return HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
5053				channel_id: msg.channel_id,
5054				htlc_id: msg.htlc_id,
5055				sha256_of_onion: [0; 32],
5056				failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
5057			});
5058		}
5059
5060		let (reason, err_data) = if is_intro_node_blinded_forward {
5061			(LocalHTLCFailureReason::InvalidOnionBlinding, &[0; 32][..])
5062		} else {
5063			(reason, &res.0[..])
5064		};
5065		let failure = HTLCFailReason::reason(reason, err_data.to_vec())
5066		.get_encrypted_failure_packet(shared_secret, &None);
5067		HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
5068			channel_id: msg.channel_id,
5069			htlc_id: msg.htlc_id,
5070			reason: failure.data,
5071			attribution_data: failure.attribution_data,
5072		})
5073	}
5074
5075	#[rustfmt::skip]
5076	fn construct_pending_htlc_fail_msg<'a>(
5077		&self, msg: &msgs::UpdateAddHTLC, counterparty_node_id: &PublicKey,
5078		shared_secret: [u8; 32], inbound_err: InboundHTLCErr
5079	) -> HTLCFailureMsg {
5080		let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), Some(msg.channel_id), Some(msg.payment_hash));
5081		log_info!(logger, "Failed to accept/forward incoming HTLC: {}", inbound_err.msg);
5082
5083		if msg.blinding_point.is_some() {
5084			return HTLCFailureMsg::Malformed(
5085				msgs::UpdateFailMalformedHTLC {
5086					channel_id: msg.channel_id,
5087					htlc_id: msg.htlc_id,
5088					sha256_of_onion: [0; 32],
5089					failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
5090				}
5091			)
5092		}
5093
5094		let failure = HTLCFailReason::reason(inbound_err.reason, inbound_err.err_data.to_vec())
5095					.get_encrypted_failure_packet(&shared_secret, &None);
5096		return HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
5097			channel_id: msg.channel_id,
5098			htlc_id: msg.htlc_id,
5099			reason: failure.data,
5100			attribution_data: failure.attribution_data,
5101		});
5102	}
5103
5104	#[rustfmt::skip]
5105	fn get_pending_htlc_info<'a>(
5106		&self, msg: &msgs::UpdateAddHTLC, shared_secret: [u8; 32],
5107		decoded_hop: onion_utils::Hop, allow_underpay: bool,
5108		next_packet_pubkey_opt: Option<Result<PublicKey, secp256k1::Error>>,
5109	) -> Result<PendingHTLCInfo, InboundHTLCErr> {
5110		match decoded_hop {
5111			onion_utils::Hop::Receive { .. } | onion_utils::Hop::BlindedReceive { .. } |
5112			onion_utils::Hop::TrampolineReceive { .. } | onion_utils::Hop::TrampolineBlindedReceive { .. } => {
5113				// OUR PAYMENT!
5114				// Note that we could obviously respond immediately with an update_fulfill_htlc
5115				// message, however that would leak that we are the recipient of this payment, so
5116				// instead we stay symmetric with the forwarding case, only responding (after a
5117				// delay) once they've send us a commitment_signed!
5118				let current_height: u32 = self.best_block.read().unwrap().height;
5119				create_recv_pending_htlc_info(decoded_hop, shared_secret, msg.payment_hash,
5120					msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat,
5121					current_height)
5122			},
5123			onion_utils::Hop::Forward { .. } | onion_utils::Hop::BlindedForward { .. } => {
5124				create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt)
5125			},
5126			onion_utils::Hop::TrampolineForward { .. } | onion_utils::Hop::TrampolineBlindedForward { .. } => {
5127				create_fwd_pending_htlc_info(msg, decoded_hop, shared_secret, next_packet_pubkey_opt)
5128			},
5129		}
5130	}
5131
5132	/// Gets the current [`channel_update`] for the given channel. This first checks if the channel is
5133	/// public, and thus should be called whenever the result is going to be passed out in a
5134	/// [`MessageSendEvent::BroadcastChannelUpdate`] event.
5135	///
5136	/// Note that in [`internal_closing_signed`], this function is called without the `peer_state`
5137	/// corresponding to the channel's counterparty locked, as the channel been removed from the
5138	/// storage and the `peer_state` lock has been dropped.
5139	///
5140	/// [`channel_update`]: msgs::ChannelUpdate
5141	/// [`internal_closing_signed`]: Self::internal_closing_signed
5142	fn get_channel_update_for_broadcast(
5143		&self, chan: &FundedChannel<SP>,
5144	) -> Result<msgs::ChannelUpdate, LightningError> {
5145		if !chan.context.should_announce() {
5146			return Err(LightningError {
5147				err: "Cannot broadcast a channel_update for a private channel".to_owned(),
5148				action: msgs::ErrorAction::IgnoreError,
5149			});
5150		}
5151		if chan.funding.get_short_channel_id().is_none() {
5152			return Err(LightningError {
5153				err: "Channel not yet established".to_owned(),
5154				action: msgs::ErrorAction::IgnoreError,
5155			});
5156		}
5157		let logger = WithChannelContext::from(&self.logger, &chan.context, None);
5158		log_trace!(
5159			logger,
5160			"Attempting to generate broadcast channel update for channel {}",
5161			&chan.context.channel_id()
5162		);
5163		self.get_channel_update_for_unicast(chan)
5164	}
5165
5166	/// Gets the current [`channel_update`] for the given channel. This does not check if the channel
5167	/// is public (only returning an `Err` if the channel does not yet have an assigned SCID),
5168	/// and thus MUST NOT be called unless the recipient of the resulting message has already
5169	/// provided evidence that they know about the existence of the channel.
5170	///
5171	/// Note that through [`internal_closing_signed`], this function is called without the
5172	/// `peer_state`  corresponding to the channel's counterparty locked, as the channel been
5173	/// removed from the storage and the `peer_state` lock has been dropped.
5174	///
5175	/// [`channel_update`]: msgs::ChannelUpdate
5176	/// [`internal_closing_signed`]: Self::internal_closing_signed
5177	#[rustfmt::skip]
5178	fn get_channel_update_for_unicast(&self, chan: &FundedChannel<SP>) -> Result<msgs::ChannelUpdate, LightningError> {
5179		let logger = WithChannelContext::from(&self.logger, &chan.context, None);
5180		log_trace!(logger, "Attempting to generate channel update for channel {}", chan.context.channel_id());
5181		let short_channel_id = match chan.funding.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
5182			None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
5183			Some(id) => id,
5184		};
5185
5186		let logger = WithChannelContext::from(&self.logger, &chan.context, None);
5187		log_trace!(logger, "Generating channel update for channel {}", chan.context.channel_id());
5188		let were_node_one = self.our_network_pubkey.serialize()[..] < chan.context.get_counterparty_node_id().serialize()[..];
5189		let enabled = chan.context.is_enabled();
5190
5191		let unsigned = msgs::UnsignedChannelUpdate {
5192			chain_hash: self.chain_hash,
5193			short_channel_id,
5194			timestamp: chan.context.get_update_time_counter(),
5195			message_flags: 1, // Only must_be_one
5196			channel_flags: (!were_node_one) as u8 | ((!enabled as u8) << 1),
5197			cltv_expiry_delta: chan.context.get_cltv_expiry_delta(),
5198			htlc_minimum_msat: chan.context.get_counterparty_htlc_minimum_msat(),
5199			htlc_maximum_msat: chan.get_announced_htlc_max_msat(),
5200			fee_base_msat: chan.context.get_outbound_forwarding_fee_base_msat(),
5201			fee_proportional_millionths: chan.context.get_fee_proportional_millionths(),
5202			excess_data: Vec::new(),
5203		};
5204		// Panic on failure to signal LDK should be restarted to retry signing the `ChannelUpdate`.
5205		// If we returned an error and the `node_signer` cannot provide a signature for whatever
5206		// reason`, we wouldn't be able to receive inbound payments through the corresponding
5207		// channel.
5208		let sig = self.node_signer.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelUpdate(&unsigned)).unwrap();
5209
5210		Ok(msgs::ChannelUpdate {
5211			signature: sig,
5212			contents: unsigned
5213		})
5214	}
5215
5216	#[cfg(any(test, feature = "_externalize_tests"))]
5217	pub(crate) fn test_send_payment_along_path(
5218		&self, path: &Path, payment_hash: &PaymentHash, recipient_onion: RecipientOnionFields,
5219		total_value: u64, cur_height: u32, payment_id: PaymentId,
5220		keysend_preimage: &Option<PaymentPreimage>, session_priv_bytes: [u8; 32],
5221	) -> Result<(), APIError> {
5222		let _lck = self.total_consistency_lock.read().unwrap();
5223		self.send_payment_along_path(SendAlongPathArgs {
5224			path,
5225			payment_hash,
5226			recipient_onion: &recipient_onion,
5227			total_value,
5228			cur_height,
5229			payment_id,
5230			keysend_preimage,
5231			invoice_request: None,
5232			bolt12_invoice: None,
5233			session_priv_bytes,
5234			hold_htlc_at_next_hop: false,
5235		})
5236	}
5237
5238	fn send_payment_along_path(&self, args: SendAlongPathArgs) -> Result<(), APIError> {
5239		let SendAlongPathArgs {
5240			path,
5241			payment_hash,
5242			recipient_onion,
5243			total_value,
5244			cur_height,
5245			payment_id,
5246			keysend_preimage,
5247			invoice_request,
5248			bolt12_invoice,
5249			session_priv_bytes,
5250			hold_htlc_at_next_hop,
5251		} = args;
5252		// The top-level caller should hold the total_consistency_lock read lock.
5253		debug_assert!(self.total_consistency_lock.try_write().is_err());
5254		let prng_seed = self.entropy_source.get_secure_random_bytes();
5255		let session_priv = SecretKey::from_slice(&session_priv_bytes[..]).expect("RNG is busted");
5256
5257		let (onion_packet, htlc_msat, htlc_cltv) = onion_utils::create_payment_onion(
5258			&self.secp_ctx,
5259			&path,
5260			&session_priv,
5261			total_value,
5262			recipient_onion,
5263			cur_height,
5264			payment_hash,
5265			keysend_preimage,
5266			invoice_request,
5267			prng_seed,
5268		)
5269		.map_err(|e| {
5270			let first_hop_key = Some(path.hops.first().unwrap().pubkey);
5271			let logger = WithContext::from(&self.logger, first_hop_key, None, Some(*payment_hash));
5272			log_error!(logger, "Failed to build an onion for path for payment hash {payment_hash}");
5273			e
5274		})?;
5275
5276		let err: Result<(), _> = loop {
5277			let first_chan_scid = &path.hops.first().unwrap().short_channel_id;
5278			let first_chan = self.short_to_chan_info.read().unwrap().get(first_chan_scid).cloned();
5279
5280			let (counterparty_node_id, id) = match first_chan {
5281				None => {
5282					let first_hop_key = Some(path.hops.first().unwrap().pubkey);
5283					let logger =
5284						WithContext::from(&self.logger, first_hop_key, None, Some(*payment_hash));
5285					log_error!(logger, "Failed to find first-hop for payment hash {payment_hash}");
5286					return Err(APIError::ChannelUnavailable {
5287						err: "No channel available with first hop!".to_owned(),
5288					});
5289				},
5290				Some((cp_id, chan_id)) => (cp_id, chan_id),
5291			};
5292
5293			let logger = WithContext::from(
5294				&self.logger,
5295				Some(counterparty_node_id),
5296				Some(id),
5297				Some(*payment_hash),
5298			);
5299			log_trace!(
5300				logger,
5301				"Attempting to send payment with payment hash {payment_hash} along path with next hop {first_chan_scid}"
5302			);
5303
5304			let per_peer_state = self.per_peer_state.read().unwrap();
5305			let peer_state_mutex = per_peer_state.get(&counterparty_node_id).ok_or_else(|| {
5306				APIError::ChannelUnavailable {
5307					err: "No peer matching the path's first hop found!".to_owned(),
5308				}
5309			})?;
5310			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
5311			let peer_state = &mut *peer_state_lock;
5312			if let hash_map::Entry::Occupied(mut chan_entry) = peer_state.channel_by_id.entry(id) {
5313				match chan_entry.get_mut().as_funded_mut() {
5314					Some(chan) => {
5315						if !chan.context.is_live() {
5316							return Err(APIError::ChannelUnavailable {
5317								err: "Peer for first hop currently disconnected".to_owned(),
5318							});
5319						}
5320						let funding_txo = chan.funding.get_funding_txo().unwrap();
5321						let logger = WithChannelContext::from(
5322							&self.logger,
5323							&chan.context,
5324							Some(*payment_hash),
5325						);
5326						let htlc_source = HTLCSource::OutboundRoute {
5327							path: path.clone(),
5328							session_priv: session_priv.clone(),
5329							first_hop_htlc_msat: htlc_msat,
5330							payment_id,
5331							bolt12_invoice: bolt12_invoice.cloned(),
5332						};
5333						let send_res = chan.send_htlc_and_commit(
5334							htlc_msat,
5335							*payment_hash,
5336							htlc_cltv,
5337							htlc_source,
5338							onion_packet,
5339							None,
5340							hold_htlc_at_next_hop,
5341							&self.fee_estimator,
5342							&&logger,
5343						);
5344						match break_channel_entry!(self, peer_state, send_res, chan_entry) {
5345							Some(monitor_update) => {
5346								let ok = handle_new_monitor_update!(
5347									self,
5348									funding_txo,
5349									monitor_update,
5350									peer_state_lock,
5351									peer_state,
5352									per_peer_state,
5353									chan
5354								);
5355								if !ok {
5356									// Note that MonitorUpdateInProgress here indicates (per function
5357									// docs) that we will resend the commitment update once monitor
5358									// updating completes. Therefore, we must return an error
5359									// indicating that it is unsafe to retry the payment wholesale,
5360									// which we do in the send_payment check for
5361									// MonitorUpdateInProgress, below.
5362									return Err(APIError::MonitorUpdateInProgress);
5363								}
5364							},
5365							None => {},
5366						}
5367					},
5368					None => {
5369						return Err(APIError::ChannelUnavailable {
5370							err: "Channel to first hop is unfunded".to_owned(),
5371						})
5372					},
5373				};
5374			} else {
5375				// The channel was likely removed after we fetched the id from the
5376				// `short_to_chan_info` map, but before we successfully locked the
5377				// `channel_by_id` map.
5378				// This can occur as no consistency guarantees exists between the two maps.
5379				return Err(APIError::ChannelUnavailable {
5380					err: "No channel available with first hop!".to_owned(),
5381				});
5382			}
5383			return Ok(());
5384		};
5385		match handle_error!(self, err, path.hops.first().unwrap().pubkey) {
5386			Ok(_) => unreachable!(),
5387			Err(e) => Err(APIError::ChannelUnavailable { err: e.err }),
5388		}
5389	}
5390
5391	/// Sends a payment along a given route. See [`Self::send_payment`] for more info.
5392	///
5393	/// LDK will not automatically retry this payment, though it may be manually re-sent after an
5394	/// [`Event::PaymentFailed`] is generated.
5395	#[rustfmt::skip]
5396	pub fn send_payment_with_route(
5397		&self, mut route: Route, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields,
5398		payment_id: PaymentId
5399	) -> Result<(), RetryableSendFailure> {
5400		let best_block_height = self.best_block.read().unwrap().height;
5401		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
5402		let route_params = route.route_params.clone().unwrap_or_else(|| {
5403			// Create a dummy route params since they're a required parameter but unused in this case
5404			let (payee_node_id, cltv_delta) = route.paths.first()
5405				.and_then(|path| path.hops.last().map(|hop| (hop.pubkey, hop.cltv_expiry_delta as u32)))
5406				.unwrap_or_else(|| (PublicKey::from_slice(&[2; 33]).unwrap(), MIN_FINAL_CLTV_EXPIRY_DELTA as u32));
5407			let dummy_payment_params = PaymentParameters::from_node_id(payee_node_id, cltv_delta);
5408			RouteParameters::from_payment_params_and_value(dummy_payment_params, route.get_total_amount())
5409		});
5410		if route.route_params.is_none() { route.route_params = Some(route_params.clone()); }
5411		let router = FixedRouter::new(route);
5412		let logger = WithContext::from(&self.logger, None, None, Some(payment_hash));
5413		self.pending_outbound_payments
5414			.send_payment(payment_hash, recipient_onion, payment_id, Retry::Attempts(0),
5415				route_params, &&router, self.list_usable_channels(), || self.compute_inflight_htlcs(),
5416				&self.entropy_source, &self.node_signer, best_block_height,
5417				&self.pending_events, |args| self.send_payment_along_path(args), &logger)
5418	}
5419
5420	/// Sends a payment to the route found using the provided [`RouteParameters`], retrying failed
5421	/// payment paths based on the provided `Retry`.
5422	///
5423	/// You should likely prefer [`Self::pay_for_bolt11_invoice`] or [`Self::pay_for_offer`] in
5424	/// general, however this method may allow for slightly more customization.
5425	///
5426	/// May generate [`UpdateHTLCs`] message(s) event on success, which should be relayed (e.g. via
5427	/// [`PeerManager::process_events`]).
5428	///
5429	/// # Avoiding Duplicate Payments
5430	///
5431	/// If a pending payment is currently in-flight with the same [`PaymentId`] provided, this
5432	/// method will error with [`RetryableSendFailure::DuplicatePayment`]. Note, however, that once a
5433	/// payment is no longer pending (either via [`ChannelManager::abandon_payment`], or handling of
5434	/// an [`Event::PaymentSent`] or [`Event::PaymentFailed`]) LDK will not stop you from sending a
5435	/// second payment with the same [`PaymentId`].
5436	///
5437	/// Thus, in order to ensure duplicate payments are not sent, you should implement your own
5438	/// tracking of payments, including state to indicate once a payment has completed. Because you
5439	/// should also ensure that [`PaymentHash`]es are not re-used, for simplicity, you should
5440	/// consider using the [`PaymentHash`] as the key for tracking payments. In that case, the
5441	/// [`PaymentId`] should be a copy of the [`PaymentHash`] bytes.
5442	///
5443	/// Additionally, in the scenario where we begin the process of sending a payment, but crash
5444	/// before `send_payment` returns (or prior to [`ChannelMonitorUpdate`] persistence if you're
5445	/// using [`ChannelMonitorUpdateStatus::InProgress`]), the payment may be lost on restart. See
5446	/// [`ChannelManager::list_recent_payments`] for more information.
5447	///
5448	/// Routes are automatically found using the [`Router] provided on startup. To fix a route for a
5449	/// particular payment, use [`Self::send_payment_with_route`] or match the [`PaymentId`] passed to
5450	/// [`Router::find_route_with_id`].
5451	///
5452	/// [`Event::PaymentSent`]: events::Event::PaymentSent
5453	/// [`Event::PaymentFailed`]: events::Event::PaymentFailed
5454	/// [`UpdateHTLCs`]: MessageSendEvent::UpdateHTLCs
5455	/// [`PeerManager::process_events`]: crate::ln::peer_handler::PeerManager::process_events
5456	/// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress
5457	pub fn send_payment(
5458		&self, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields,
5459		payment_id: PaymentId, route_params: RouteParameters, retry_strategy: Retry,
5460	) -> Result<(), RetryableSendFailure> {
5461		let best_block_height = self.best_block.read().unwrap().height;
5462		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
5463		self.pending_outbound_payments.send_payment(
5464			payment_hash,
5465			recipient_onion,
5466			payment_id,
5467			retry_strategy,
5468			route_params,
5469			&self.router,
5470			self.list_usable_channels(),
5471			|| self.compute_inflight_htlcs(),
5472			&self.entropy_source,
5473			&self.node_signer,
5474			best_block_height,
5475			&self.pending_events,
5476			|args| self.send_payment_along_path(args),
5477			&WithContext::from(&self.logger, None, None, Some(payment_hash)),
5478		)
5479	}
5480
5481	#[cfg(any(test, feature = "_externalize_tests"))]
5482	pub(super) fn test_send_payment_internal(
5483		&self, route: &Route, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields,
5484		keysend_preimage: Option<PaymentPreimage>, payment_id: PaymentId,
5485		recv_value_msat: Option<u64>, onion_session_privs: Vec<[u8; 32]>,
5486	) -> Result<(), PaymentSendFailure> {
5487		let best_block_height = self.best_block.read().unwrap().height;
5488		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
5489		self.pending_outbound_payments.test_send_payment_internal(
5490			route,
5491			payment_hash,
5492			recipient_onion,
5493			keysend_preimage,
5494			payment_id,
5495			recv_value_msat,
5496			onion_session_privs,
5497			&self.node_signer,
5498			best_block_height,
5499			|args| self.send_payment_along_path(args),
5500		)
5501	}
5502
5503	#[cfg(any(test, feature = "_externalize_tests"))]
5504	pub(crate) fn test_add_new_pending_payment(
5505		&self, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields,
5506		payment_id: PaymentId, route: &Route,
5507	) -> Result<Vec<[u8; 32]>, PaymentSendFailure> {
5508		let best_block_height = self.best_block.read().unwrap().height;
5509		self.pending_outbound_payments.test_add_new_pending_payment(
5510			payment_hash,
5511			recipient_onion,
5512			payment_id,
5513			route,
5514			None,
5515			&self.entropy_source,
5516			best_block_height,
5517		)
5518	}
5519
5520	#[cfg(test)]
5521	pub(crate) fn test_modify_pending_payment<Fn>(&self, payment_id: &PaymentId, mut callback: Fn)
5522	where
5523		Fn: FnMut(&mut PendingOutboundPayment),
5524	{
5525		let mut outbounds =
5526			self.pending_outbound_payments.pending_outbound_payments.lock().unwrap();
5527		match outbounds.get_mut(payment_id) {
5528			Some(outb) => callback(outb),
5529			_ => panic!(),
5530		}
5531	}
5532
5533	#[cfg(test)]
5534	pub(crate) fn test_set_payment_metadata(
5535		&self, payment_id: PaymentId, new_payment_metadata: Option<Vec<u8>>,
5536	) {
5537		self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
5538	}
5539
5540	/// Pays a [`Bolt11Invoice`] associated with the `payment_id`. See [`Self::send_payment`] for more info.
5541	///
5542	/// # Payment Id
5543	/// The invoice's `payment_hash().0` serves as a reliable choice for the `payment_id`.
5544	///
5545	/// # Handling Invoice Amounts
5546	/// Some invoices include a specific amount, while others require you to specify one.
5547	/// - If the invoice **includes** an amount, user may provide an amount greater or equal to it
5548	/// to allow for overpayments.
5549	/// - If the invoice **doesn't include** an amount, you'll need to specify `amount_msats`.
5550	///
5551	/// If these conditions aren’t met, the function will return [`Bolt11PaymentError::InvalidAmount`].
5552	///
5553	/// # Custom Routing Parameters
5554	/// Users can customize routing parameters via [`RouteParametersConfig`].
5555	/// To use default settings, call the function with [`RouteParametersConfig::default`].
5556	pub fn pay_for_bolt11_invoice(
5557		&self, invoice: &Bolt11Invoice, payment_id: PaymentId, amount_msats: Option<u64>,
5558		route_params_config: RouteParametersConfig, retry_strategy: Retry,
5559	) -> Result<(), Bolt11PaymentError> {
5560		let best_block_height = self.best_block.read().unwrap().height;
5561		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
5562		let payment_hash = PaymentHash(invoice.payment_hash().to_byte_array());
5563		self.pending_outbound_payments.pay_for_bolt11_invoice(
5564			invoice,
5565			payment_id,
5566			amount_msats,
5567			route_params_config,
5568			retry_strategy,
5569			&self.router,
5570			self.list_usable_channels(),
5571			|| self.compute_inflight_htlcs(),
5572			&self.entropy_source,
5573			&self.node_signer,
5574			best_block_height,
5575			&self.pending_events,
5576			|args| self.send_payment_along_path(args),
5577			&WithContext::from(&self.logger, None, None, Some(payment_hash)),
5578		)
5579	}
5580
5581	/// Pays the [`Bolt12Invoice`] associated with the `payment_id` encoded in its `payer_metadata`.
5582	///
5583	/// The invoice's `payer_metadata` is used to authenticate that the invoice was indeed requested
5584	/// before attempting a payment. [`Bolt12PaymentError::UnexpectedInvoice`] is returned if this
5585	/// fails or if the encoded `payment_id` is not recognized. The latter may happen once the
5586	/// payment is no longer tracked because the payment was attempted after:
5587	/// - an invoice for the `payment_id` was already paid,
5588	/// - one full [timer tick] has elapsed since initially requesting the invoice when paying an
5589	///   offer, or
5590	/// - the refund corresponding to the invoice has already expired.
5591	///
5592	/// To retry the payment, request another invoice using a new `payment_id`.
5593	///
5594	/// Attempting to pay the same invoice twice while the first payment is still pending will
5595	/// result in a [`Bolt12PaymentError::DuplicateInvoice`].
5596	///
5597	/// Otherwise, either [`Event::PaymentSent`] or [`Event::PaymentFailed`] are used to indicate
5598	/// whether or not the payment was successful.
5599	///
5600	/// [timer tick]: Self::timer_tick_occurred
5601	pub fn send_payment_for_bolt12_invoice(
5602		&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
5603	) -> Result<(), Bolt12PaymentError> {
5604		match self.verify_bolt12_invoice(invoice, context) {
5605			Ok(payment_id) => self.send_payment_for_verified_bolt12_invoice(invoice, payment_id),
5606			Err(()) => Err(Bolt12PaymentError::UnexpectedInvoice),
5607		}
5608	}
5609
5610	fn verify_bolt12_invoice(
5611		&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
5612	) -> Result<PaymentId, ()> {
5613		let secp_ctx = &self.secp_ctx;
5614		let expanded_key = &self.inbound_payment_key;
5615
5616		match context {
5617			None if invoice.is_for_refund_without_paths() => {
5618				invoice.verify_using_metadata(expanded_key, secp_ctx)
5619			},
5620			Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
5621				invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
5622			},
5623			_ => Err(()),
5624		}
5625	}
5626
5627	fn send_payment_for_verified_bolt12_invoice(
5628		&self, invoice: &Bolt12Invoice, payment_id: PaymentId,
5629	) -> Result<(), Bolt12PaymentError> {
5630		let best_block_height = self.best_block.read().unwrap().height;
5631		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
5632		let features = self.bolt12_invoice_features();
5633		self.pending_outbound_payments.send_payment_for_bolt12_invoice(
5634			invoice,
5635			payment_id,
5636			&self.router,
5637			self.list_usable_channels(),
5638			features,
5639			|| self.compute_inflight_htlcs(),
5640			&self.entropy_source,
5641			&self.node_signer,
5642			&self,
5643			&self.secp_ctx,
5644			best_block_height,
5645			&self.pending_events,
5646			|args| self.send_payment_along_path(args),
5647			&WithContext::from(&self.logger, None, None, None),
5648		)
5649	}
5650
5651	fn check_refresh_async_receive_offer_cache(&self, timer_tick_occurred: bool) {
5652		let peers = self.get_peers_for_blinded_path();
5653		let channels = self.list_usable_channels();
5654		let entropy = &*self.entropy_source;
5655		let router = &*self.router;
5656		let refresh_res = self.flow.check_refresh_async_receive_offer_cache(
5657			peers,
5658			channels,
5659			entropy,
5660			router,
5661			timer_tick_occurred,
5662		);
5663		match refresh_res {
5664			Err(()) => {
5665				log_error!(
5666					self.logger,
5667					"Failed to create blinded paths when requesting async receive offer paths"
5668				);
5669			},
5670			Ok(()) => {},
5671		}
5672	}
5673
5674	#[cfg(test)]
5675	pub(crate) fn test_check_refresh_async_receive_offers(&self) {
5676		self.check_refresh_async_receive_offer_cache(false);
5677	}
5678
5679	/// Should be called after handling an [`Event::PersistStaticInvoice`], where the `Responder`
5680	/// comes from [`Event::PersistStaticInvoice::invoice_persisted_path`].
5681	pub fn static_invoice_persisted(&self, invoice_persisted_path: Responder) {
5682		self.flow.static_invoice_persisted(invoice_persisted_path);
5683	}
5684
5685	/// Forwards a [`StaticInvoice`] to a payer in response to an
5686	/// [`Event::StaticInvoiceRequested`]. Also forwards the payer's [`InvoiceRequest`] to the
5687	/// async recipient, in case the recipient is online to provide the payer with a fresh
5688	/// [`Bolt12Invoice`].
5689	pub fn respond_to_static_invoice_request(
5690		&self, invoice: StaticInvoice, responder: Responder, invoice_request: InvoiceRequest,
5691		invoice_request_path: BlindedMessagePath,
5692	) -> Result<(), Bolt12SemanticError> {
5693		self.flow.enqueue_invoice_request_to_forward(
5694			invoice_request,
5695			invoice_request_path,
5696			responder.clone(),
5697		);
5698		self.flow.enqueue_static_invoice(invoice, responder)
5699	}
5700
5701	fn initiate_async_payment(
5702		&self, invoice: &StaticInvoice, payment_id: PaymentId,
5703	) -> Result<(), Bolt12PaymentError> {
5704		let mut res = Ok(());
5705		PersistenceNotifierGuard::optionally_notify(self, || {
5706			let best_block_height = self.best_block.read().unwrap().height;
5707			let features = self.bolt12_invoice_features();
5708			let outbound_pmts_res = self.pending_outbound_payments.static_invoice_received(
5709				invoice,
5710				payment_id,
5711				features,
5712				best_block_height,
5713				self.duration_since_epoch(),
5714				&*self.entropy_source,
5715				&self.pending_events,
5716			);
5717			match outbound_pmts_res {
5718				Ok(()) => {},
5719				Err(Bolt12PaymentError::UnexpectedInvoice)
5720				| Err(Bolt12PaymentError::DuplicateInvoice) => {
5721					res = outbound_pmts_res.map(|_| ());
5722					return NotifyOption::SkipPersistNoEvents;
5723				},
5724				Err(e) => {
5725					res = Err(e);
5726					return NotifyOption::DoPersist;
5727				},
5728			};
5729
5730			// If the call to `Self::hold_htlc_channels` succeeded, then we are a private node and can
5731			// hold the HTLCs for this payment at our next-hop channel counterparty until the recipient
5732			// comes online. This allows us to go offline after locking in the HTLCs.
5733			if let Ok(channels) = self.hold_htlc_channels() {
5734				if let Err(e) =
5735					self.send_payment_for_static_invoice_no_persist(payment_id, channels, true)
5736				{
5737					log_trace!(
5738						self.logger,
5739						"Failed to send held HTLC with payment id {}: {:?}",
5740						payment_id,
5741						e
5742					);
5743				}
5744			} else {
5745				let reply_path = HeldHtlcReplyPath::ToUs {
5746					payment_id,
5747					peers: self.get_peers_for_blinded_path(),
5748				};
5749				let enqueue_held_htlc_available_res =
5750					self.flow.enqueue_held_htlc_available(invoice, reply_path);
5751				if enqueue_held_htlc_available_res.is_err() {
5752					self.abandon_payment_with_reason(
5753						payment_id,
5754						PaymentFailureReason::BlindedPathCreationFailed,
5755					);
5756					res = Err(Bolt12PaymentError::BlindedPathCreationFailed);
5757					return NotifyOption::DoPersist;
5758				};
5759			}
5760
5761			NotifyOption::DoPersist
5762		});
5763
5764		res
5765	}
5766
5767	/// Returns a list of channels where our counterparty supports
5768	/// [`InitFeatures::supports_htlc_hold`], or an error if there are none or we are configured not
5769	/// to hold HTLCs at our next-hop channel counterparty. Useful for sending async payments to
5770	/// [`StaticInvoice`]s.
5771	fn hold_htlc_channels(&self) -> Result<Vec<ChannelDetails>, ()> {
5772		let should_send_async = self.config.read().unwrap().hold_outbound_htlcs_at_next_hop;
5773		if !should_send_async {
5774			return Err(());
5775		}
5776
5777		let hold_htlc_channels =
5778			self.list_funded_channels_with_filter(|&(init_features, _, ref channel)| {
5779				init_features.supports_htlc_hold() && channel.context().is_live()
5780			});
5781
5782		if hold_htlc_channels.is_empty() {
5783			Err(())
5784		} else {
5785			Ok(hold_htlc_channels)
5786		}
5787	}
5788
5789	fn send_payment_for_static_invoice(
5790		&self, payment_id: PaymentId,
5791	) -> Result<(), Bolt12PaymentError> {
5792		let mut res = Ok(());
5793		let first_hops = self.list_usable_channels();
5794		PersistenceNotifierGuard::optionally_notify(self, || {
5795			let outbound_pmts_res =
5796				self.send_payment_for_static_invoice_no_persist(payment_id, first_hops, false);
5797			match outbound_pmts_res {
5798				Err(Bolt12PaymentError::UnexpectedInvoice)
5799				| Err(Bolt12PaymentError::DuplicateInvoice) => {
5800					res = outbound_pmts_res.map(|_| ());
5801					NotifyOption::SkipPersistNoEvents
5802				},
5803				other_res => {
5804					res = other_res;
5805					NotifyOption::DoPersist
5806				},
5807			}
5808		});
5809		res
5810	}
5811
5812	/// Useful if the caller is already triggering a persist of the `ChannelManager`.
5813	fn send_payment_for_static_invoice_no_persist(
5814		&self, payment_id: PaymentId, first_hops: Vec<ChannelDetails>, hold_htlcs_at_next_hop: bool,
5815	) -> Result<(), Bolt12PaymentError> {
5816		let best_block_height = self.best_block.read().unwrap().height;
5817		self.pending_outbound_payments.send_payment_for_static_invoice(
5818			payment_id,
5819			hold_htlcs_at_next_hop,
5820			&self.router,
5821			first_hops,
5822			|| self.compute_inflight_htlcs(),
5823			&self.entropy_source,
5824			&self.node_signer,
5825			&self,
5826			&self.secp_ctx,
5827			best_block_height,
5828			&self.pending_events,
5829			|args| self.send_payment_along_path(args),
5830			&WithContext::from(&self.logger, None, None, None),
5831		)
5832	}
5833
5834	/// If we are holding an HTLC on behalf of an often-offline sender, this method allows us to
5835	/// create a path for the sender to use as the reply path when they send the recipient a
5836	/// [`HeldHtlcAvailable`] onion message, so the recipient's [`ReleaseHeldHtlc`] response will be
5837	/// received to our node.
5838	fn path_for_release_held_htlc(
5839		&self, htlc_id: u64, prev_outbound_scid_alias: u64, channel_id: &ChannelId,
5840		counterparty_node_id: &PublicKey,
5841	) -> BlindedMessagePath {
5842		let intercept_id =
5843			InterceptId::from_htlc_id_and_chan_id(htlc_id, channel_id, counterparty_node_id);
5844		self.flow.path_for_release_held_htlc(
5845			intercept_id,
5846			prev_outbound_scid_alias,
5847			htlc_id,
5848			&*self.entropy_source,
5849		)
5850	}
5851
5852	/// Signals that no further attempts for the given payment should occur. Useful if you have a
5853	/// pending outbound payment with retries remaining, but wish to stop retrying the payment before
5854	/// retries are exhausted.
5855	///
5856	/// # Event Generation
5857	///
5858	/// If no [`Event::PaymentFailed`] event had been generated before, one will be generated as soon
5859	/// as there are no remaining pending HTLCs for this payment.
5860	///
5861	/// Note that calling this method does *not* prevent a payment from succeeding. You must still
5862	/// wait until you receive either a [`Event::PaymentFailed`] or [`Event::PaymentSent`] event to
5863	/// determine the ultimate status of a payment.
5864	///
5865	/// # Requested Invoices
5866	///
5867	/// In the case of paying a [`Bolt12Invoice`] via [`ChannelManager::pay_for_offer`], abandoning
5868	/// the payment prior to receiving the invoice will result in an [`Event::PaymentFailed`] and
5869	/// prevent any attempts at paying it once received.
5870	///
5871	/// # Restart Behavior
5872	///
5873	/// If an [`Event::PaymentFailed`] is generated and we restart without first persisting the
5874	/// [`ChannelManager`], another [`Event::PaymentFailed`] may be generated.
5875	///
5876	/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
5877	pub fn abandon_payment(&self, payment_id: PaymentId) {
5878		self.abandon_payment_with_reason(payment_id, PaymentFailureReason::UserAbandoned)
5879	}
5880
5881	fn abandon_payment_with_reason(&self, payment_id: PaymentId, reason: PaymentFailureReason) {
5882		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
5883		self.pending_outbound_payments.abandon_payment(payment_id, reason, &self.pending_events);
5884	}
5885
5886	/// Send a spontaneous payment, which is a payment that does not require the recipient to have
5887	/// generated an invoice. Optionally, you may specify the preimage. If you do choose to specify
5888	/// the preimage, it must be a cryptographically secure random value that no intermediate node
5889	/// would be able to guess -- otherwise, an intermediate node may claim the payment and it will
5890	/// never reach the recipient.
5891	///
5892	/// Similar to regular payments, you MUST NOT reuse a `payment_preimage` value. See
5893	/// [`send_payment`] for more information about the risks of duplicate preimage usage.
5894	///
5895	/// See [`send_payment`] documentation for more details on the idempotency guarantees provided by
5896	/// the [`PaymentId`] key.
5897	///
5898	/// See [`PaymentParameters::for_keysend`] for help in constructing `route_params` for spontaneous
5899	/// payments.
5900	///
5901	/// [`send_payment`]: Self::send_payment
5902	/// [`PaymentParameters::for_keysend`]: crate::routing::router::PaymentParameters::for_keysend
5903	pub fn send_spontaneous_payment(
5904		&self, payment_preimage: Option<PaymentPreimage>, recipient_onion: RecipientOnionFields,
5905		payment_id: PaymentId, route_params: RouteParameters, retry_strategy: Retry,
5906	) -> Result<PaymentHash, RetryableSendFailure> {
5907		let best_block_height = self.best_block.read().unwrap().height;
5908		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
5909		let payment_hash = payment_preimage.map(|preimage| preimage.into());
5910		self.pending_outbound_payments.send_spontaneous_payment(
5911			payment_preimage,
5912			recipient_onion,
5913			payment_id,
5914			retry_strategy,
5915			route_params,
5916			&self.router,
5917			self.list_usable_channels(),
5918			|| self.compute_inflight_htlcs(),
5919			&self.entropy_source,
5920			&self.node_signer,
5921			best_block_height,
5922			&self.pending_events,
5923			|args| self.send_payment_along_path(args),
5924			&WithContext::from(&self.logger, None, None, payment_hash),
5925		)
5926	}
5927
5928	/// Send a payment that is probing the given route for liquidity. We calculate the
5929	/// [`PaymentHash`] of probes based on a static secret and a random [`PaymentId`], which allows
5930	/// us to easily discern them from real payments.
5931	pub fn send_probe(&self, path: Path) -> Result<(PaymentHash, PaymentId), ProbeSendFailure> {
5932		let best_block_height = self.best_block.read().unwrap().height;
5933		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
5934		self.pending_outbound_payments.send_probe(
5935			path,
5936			self.probing_cookie_secret,
5937			&self.entropy_source,
5938			&self.node_signer,
5939			best_block_height,
5940			|args| self.send_payment_along_path(args),
5941		)
5942	}
5943
5944	/// Returns whether a payment with the given [`PaymentHash`] and [`PaymentId`] is, in fact, a
5945	/// payment probe.
5946	#[cfg(test)]
5947	pub(crate) fn payment_is_probe(
5948		&self, payment_hash: &PaymentHash, payment_id: &PaymentId,
5949	) -> bool {
5950		outbound_payment::payment_is_probe(payment_hash, payment_id, self.probing_cookie_secret)
5951	}
5952
5953	/// Sends payment probes over all paths of a route that would be used to pay the given
5954	/// amount to the given `node_id`.
5955	///
5956	/// See [`ChannelManager::send_preflight_probes`] for more information.
5957	pub fn send_spontaneous_preflight_probes(
5958		&self, node_id: PublicKey, amount_msat: u64, final_cltv_expiry_delta: u32,
5959		liquidity_limit_multiplier: Option<u64>,
5960	) -> Result<Vec<(PaymentHash, PaymentId)>, ProbeSendFailure> {
5961		let payment_params = PaymentParameters::from_node_id(node_id, final_cltv_expiry_delta);
5962
5963		let route_params =
5964			RouteParameters::from_payment_params_and_value(payment_params, amount_msat);
5965
5966		self.send_preflight_probes(route_params, liquidity_limit_multiplier)
5967	}
5968
5969	/// Sends payment probes over all paths of a route that would be used to pay a route found
5970	/// according to the given [`RouteParameters`].
5971	///
5972	/// This may be used to send "pre-flight" probes, i.e., to train our scorer before conducting
5973	/// the actual payment. Note this is only useful if there likely is sufficient time for the
5974	/// probe to settle before sending out the actual payment, e.g., when waiting for user
5975	/// confirmation in a wallet UI.
5976	///
5977	/// Otherwise, there is a chance the probe could take up some liquidity needed to complete the
5978	/// actual payment. Users should therefore be cautious and might avoid sending probes if
5979	/// liquidity is scarce and/or they don't expect the probe to return before they send the
5980	/// payment. To mitigate this issue, channels with available liquidity less than the required
5981	/// amount times the given `liquidity_limit_multiplier` won't be used to send pre-flight
5982	/// probes. If `None` is given as `liquidity_limit_multiplier`, it defaults to `3`.
5983	pub fn send_preflight_probes(
5984		&self, route_params: RouteParameters, liquidity_limit_multiplier: Option<u64>,
5985	) -> Result<Vec<(PaymentHash, PaymentId)>, ProbeSendFailure> {
5986		let liquidity_limit_multiplier = liquidity_limit_multiplier.unwrap_or(3);
5987
5988		let payer = self.get_our_node_id();
5989		let usable_channels = self.list_usable_channels();
5990		let first_hops = usable_channels.iter().collect::<Vec<_>>();
5991		let inflight_htlcs = self.compute_inflight_htlcs();
5992
5993		let route = self
5994			.router
5995			.find_route(&payer, &route_params, Some(&first_hops), inflight_htlcs)
5996			.map_err(|e| {
5997				log_error!(self.logger, "Failed to find path for payment probe: {:?}", e);
5998				ProbeSendFailure::RouteNotFound
5999			})?;
6000
6001		let mut used_liquidity_map = hash_map_with_capacity(first_hops.len());
6002
6003		let mut res = Vec::new();
6004
6005		for mut path in route.paths {
6006			// If the last hop is probably an unannounced channel we refrain from probing all the
6007			// way through to the end and instead probe up to the second-to-last channel.
6008			while let Some(last_path_hop) = path.hops.last() {
6009				if last_path_hop.maybe_announced_channel {
6010					// We found a potentially announced last hop.
6011					break;
6012				} else {
6013					// Drop the last hop, as it's likely unannounced.
6014					log_debug!(
6015						self.logger,
6016						"Avoided sending payment probe all the way to last hop {} as it is likely unannounced.",
6017						last_path_hop.short_channel_id
6018					);
6019					let final_value_msat = path.final_value_msat();
6020					path.hops.pop();
6021					if let Some(new_last) = path.hops.last_mut() {
6022						new_last.fee_msat += final_value_msat;
6023					}
6024				}
6025			}
6026
6027			if path.hops.len() < 2 {
6028				log_debug!(
6029					self.logger,
6030					"Skipped sending payment probe over path with less than two hops."
6031				);
6032				continue;
6033			}
6034
6035			if let Some(first_path_hop) = path.hops.first() {
6036				if let Some(first_hop) = first_hops.iter().find(|h| {
6037					h.get_outbound_payment_scid() == Some(first_path_hop.short_channel_id)
6038				}) {
6039					let path_value = path.final_value_msat() + path.fee_msat();
6040					let used_liquidity =
6041						used_liquidity_map.entry(first_path_hop.short_channel_id).or_insert(0);
6042
6043					if first_hop.next_outbound_htlc_limit_msat
6044						< (*used_liquidity + path_value) * liquidity_limit_multiplier
6045					{
6046						log_debug!(self.logger, "Skipped sending payment probe to avoid putting channel {} under the liquidity limit.", first_path_hop.short_channel_id);
6047						continue;
6048					} else {
6049						*used_liquidity += path_value;
6050					}
6051				}
6052			}
6053
6054			res.push(self.send_probe(path).map_err(|e| {
6055				log_error!(self.logger, "Failed to send pre-flight probe: {:?}", e);
6056				e
6057			})?);
6058		}
6059
6060		Ok(res)
6061	}
6062
6063	/// Handles the generation of a funding transaction, optionally (for tests) with a function
6064	/// which checks the correctness of the funding transaction given the associated channel.
6065	#[rustfmt::skip]
6066	fn funding_transaction_generated_intern<FundingOutput: FnMut(&OutboundV1Channel<SP>) -> Result<OutPoint, &'static str>>(
6067		&self, temporary_channel_id: ChannelId, counterparty_node_id: PublicKey, funding_transaction: Transaction, is_batch_funding: bool,
6068		mut find_funding_output: FundingOutput, is_manual_broadcast: bool,
6069	) -> Result<(), APIError> {
6070		let per_peer_state = self.per_peer_state.read().unwrap();
6071		let peer_state_mutex = per_peer_state.get(&counterparty_node_id)
6072			.ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}") })?;
6073
6074		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6075		let peer_state = &mut *peer_state_lock;
6076
6077		macro_rules! abandon_chan { ($err: expr, $api_err: expr, $chan: expr) => { {
6078			let counterparty;
6079			let err = if let ChannelError::Close((msg, reason)) = $err {
6080				let channel_id = $chan.context.channel_id();
6081				counterparty = $chan.context.get_counterparty_node_id();
6082				let shutdown_res = $chan.abandon_unfunded_chan(reason);
6083				MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, None)
6084			} else { unreachable!(); };
6085
6086			mem::drop(peer_state_lock);
6087			mem::drop(per_peer_state);
6088			let _: Result<(), _> = handle_error!(self, Err(err), counterparty);
6089			Err($api_err)
6090		} } }
6091
6092		let mut chan = match peer_state.channel_by_id.entry(temporary_channel_id) {
6093			hash_map::Entry::Occupied(chan) => {
6094				if !chan.get().ready_to_fund() {
6095					return Err(APIError::APIMisuseError {
6096						err: format!("Channel {temporary_channel_id} with counterparty {counterparty_node_id} is not an unfunded, outbound channel ready to fund"),
6097					});
6098				}
6099				match chan.remove().into_unfunded_outbound_v1() {
6100					Ok(chan) => chan,
6101					Err(chan) => {
6102						debug_assert!(false, "ready_to_fund guarantees into_unfunded_outbound_v1 will succeed");
6103						peer_state.channel_by_id.insert(temporary_channel_id, chan);
6104						return Err(APIError::APIMisuseError {
6105							err: "Invalid state, please report this bug".to_owned(),
6106						});
6107					},
6108				}
6109			},
6110			hash_map::Entry::Vacant(_) => {
6111				return Err(APIError::ChannelUnavailable {
6112					err: format!("Channel {temporary_channel_id} with counterparty {counterparty_node_id} not found"),
6113				});
6114			},
6115		};
6116
6117		let funding_txo = match find_funding_output(&chan) {
6118			Ok(found_funding_txo) => found_funding_txo,
6119			Err(err) => {
6120				let chan_err = ChannelError::close(err.to_owned());
6121				let api_err = APIError::APIMisuseError { err: err.to_owned() };
6122				return abandon_chan!(chan_err, api_err, chan);
6123			},
6124		};
6125
6126		let logger = WithChannelContext::from(&self.logger, &chan.context, None);
6127		let funding_res = chan.get_funding_created(funding_transaction, funding_txo, is_batch_funding, &&logger);
6128		let (mut chan, msg_opt) = match funding_res {
6129			Ok(funding_msg) => (chan, funding_msg),
6130			Err((mut chan, chan_err)) => {
6131				let api_err = APIError::ChannelUnavailable { err: "Signer refused to sign the initial commitment transaction".to_owned() };
6132				return abandon_chan!(chan_err, api_err, chan);
6133			}
6134		};
6135
6136		match peer_state.channel_by_id.entry(chan.context.channel_id()) {
6137			hash_map::Entry::Occupied(_) => {
6138				// We need to `unset_funding_info` to make sure we don't close the already open
6139				// channel and instead close the one pending.
6140				let err = format!(
6141					"An existing channel using ID {} is open with peer {}",
6142					chan.context.channel_id(), chan.context.get_counterparty_node_id(),
6143				);
6144				let chan_err = ChannelError::close(err.to_owned());
6145				let api_err = APIError::APIMisuseError { err: err.to_owned() };
6146				chan.unset_funding_info();
6147				return abandon_chan!(chan_err, api_err, chan);
6148			},
6149			hash_map::Entry::Vacant(e) => {
6150				if let Some(msg) = msg_opt {
6151					peer_state.pending_msg_events.push(MessageSendEvent::SendFundingCreated {
6152						node_id: chan.context.get_counterparty_node_id(),
6153						msg,
6154					});
6155				}
6156				if is_manual_broadcast {
6157					chan.context.set_manual_broadcast();
6158				}
6159
6160				e.insert(Channel::from(chan));
6161				Ok(())
6162			}
6163		}
6164	}
6165
6166	#[cfg(any(test, feature = "_externalize_tests"))]
6167	pub(crate) fn funding_transaction_generated_unchecked(
6168		&self, temporary_channel_id: ChannelId, counterparty_node_id: PublicKey,
6169		funding_transaction: Transaction, output_index: u16,
6170	) -> Result<(), APIError> {
6171		let txid = funding_transaction.compute_txid();
6172		self.funding_transaction_generated_intern(
6173			temporary_channel_id,
6174			counterparty_node_id,
6175			funding_transaction,
6176			false,
6177			|_| Ok(OutPoint { txid, index: output_index }),
6178			false,
6179		)
6180	}
6181
6182	/// Call this upon creation of a funding transaction for the given channel.
6183	///
6184	/// Returns an [`APIError::APIMisuseError`] if the funding_transaction spent non-SegWit outputs
6185	/// or if no output was found which matches the parameters in [`Event::FundingGenerationReady`].
6186	///
6187	/// Returns [`APIError::APIMisuseError`] if the funding transaction is not final for propagation
6188	/// across the p2p network.
6189	///
6190	/// Returns [`APIError::ChannelUnavailable`] if a funding transaction has already been provided
6191	/// for the channel or if the channel has been closed as indicated by [`Event::ChannelClosed`].
6192	///
6193	/// May panic if the output found in the funding transaction is duplicative with some other
6194	/// channel (note that this should be trivially prevented by using unique funding transaction
6195	/// keys per-channel).
6196	///
6197	/// Do NOT broadcast the funding transaction yourself. When we have safely received our
6198	/// counterparty's signature the funding transaction will automatically be broadcast via the
6199	/// [`BroadcasterInterface`] provided when this `ChannelManager` was constructed.
6200	///
6201	/// Note that this includes RBF or similar transaction replacement strategies - lightning does
6202	/// not currently support replacing a funding transaction on an existing channel. Instead,
6203	/// create a new channel with a conflicting funding transaction.
6204	///
6205	/// Note to keep the miner incentives aligned in moving the blockchain forward, we recommend
6206	/// the wallet software generating the funding transaction to apply anti-fee sniping as
6207	/// implemented by Bitcoin Core wallet. See <https://bitcoinops.org/en/topics/fee-sniping/>
6208	/// for more details.
6209	///
6210	/// [`Event::FundingGenerationReady`]: crate::events::Event::FundingGenerationReady
6211	/// [`Event::ChannelClosed`]: crate::events::Event::ChannelClosed
6212	pub fn funding_transaction_generated(
6213		&self, temporary_channel_id: ChannelId, counterparty_node_id: PublicKey,
6214		funding_transaction: Transaction,
6215	) -> Result<(), APIError> {
6216		let temporary_chan = &[(&temporary_channel_id, &counterparty_node_id)];
6217		self.batch_funding_transaction_generated(temporary_chan, funding_transaction)
6218	}
6219
6220	/// **Unsafe**: This method does not validate the spent output. It is the caller's
6221	/// responsibility to ensure the spent outputs are SegWit, as well as making sure the funding
6222	/// transaction has a final absolute locktime, i.e., its locktime is lower than the next block height.
6223	///
6224	/// For a safer method, please refer to [`ChannelManager::funding_transaction_generated`].
6225	///
6226	/// Call this in response to a [`Event::FundingGenerationReady`] event.
6227	///
6228	/// Note that if this method is called successfully, the funding transaction won't be
6229	/// broadcasted and you are expected to broadcast it manually when receiving the
6230	/// [`Event::FundingTxBroadcastSafe`] event.
6231	///
6232	/// Returns [`APIError::ChannelUnavailable`] if a funding transaction has already been provided
6233	/// for the channel or if the channel has been closed as indicated by [`Event::ChannelClosed`].
6234	///
6235	/// May panic if the funding output is duplicative with some other channel (note that this
6236	/// should be trivially prevented by using unique funding transaction keys per-channel).
6237	///
6238	/// Note to keep the miner incentives aligned in moving the blockchain forward, we recommend
6239	/// the wallet software generating the funding transaction to apply anti-fee sniping as
6240	/// implemented by Bitcoin Core wallet. See <https://bitcoinops.org/en/topics/fee-sniping/> for
6241	/// more details.
6242	///
6243	/// [`Event::FundingGenerationReady`]: crate::events::Event::FundingGenerationReady
6244	/// [`Event::FundingTxBroadcastSafe`]: crate::events::Event::FundingTxBroadcastSafe
6245	/// [`Event::ChannelClosed`]: crate::events::Event::ChannelClosed
6246	/// [`ChannelManager::funding_transaction_generated`]: crate::ln::channelmanager::ChannelManager::funding_transaction_generated
6247	pub fn unsafe_manual_funding_transaction_generated(
6248		&self, temporary_channel_id: ChannelId, counterparty_node_id: PublicKey, funding: OutPoint,
6249	) -> Result<(), APIError> {
6250		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
6251
6252		let temporary_chans = &[(&temporary_channel_id, &counterparty_node_id)];
6253		let funding_type = FundingType::Unchecked(funding);
6254		self.batch_funding_transaction_generated_intern(temporary_chans, funding_type)
6255	}
6256
6257	/// Call this upon creation of a funding transaction for the given channel.
6258	///
6259	/// This method executes the same checks as [`ChannelManager::funding_transaction_generated`],
6260	/// but it does not automatically broadcast the funding transaction.
6261	///
6262	/// Call this in response to a [`Event::FundingGenerationReady`] event, only in a context where you want to manually
6263	/// control the broadcast of the funding transaction.
6264	///
6265	/// The associated [`ChannelMonitor`] likewise avoids broadcasting holder commitment or CPFP
6266	/// transactions until the funding has been observed on chain. This
6267	/// prevents attempting to broadcast unconfirmable commitment transactions before the channel's
6268	/// funding exists in a block.
6269	///
6270	/// If HTLCs would otherwise approach timeout while the funding transaction has not yet appeared
6271	/// on chain, the monitor avoids broadcasting force-close transactions in manual-broadcast
6272	/// mode until the funding is seen. It may still close the channel off-chain (emitting a
6273	/// `ChannelClosed` event) to avoid accepting further updates. Ensure your application either
6274	/// broadcasts the funding transaction in a timely manner or avoids forwarding HTLCs that could
6275	/// approach timeout during this interim state.
6276	///
6277	/// See also [`ChannelMonitor::broadcast_latest_holder_commitment_txn`]. For channels using
6278	/// manual-broadcast, calling that method has no effect until the funding has been observed
6279	/// on-chain.
6280	///
6281	/// [`ChannelManager::funding_transaction_generated`]: crate::ln::channelmanager::ChannelManager::funding_transaction_generated
6282	/// [`Event::FundingGenerationReady`]: crate::events::Event::FundingGenerationReady
6283	pub fn funding_transaction_generated_manual_broadcast(
6284		&self, temporary_channel_id: ChannelId, counterparty_node_id: PublicKey,
6285		funding_transaction: Transaction,
6286	) -> Result<(), APIError> {
6287		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
6288		self.batch_funding_transaction_generated_intern(
6289			&[(&temporary_channel_id, &counterparty_node_id)],
6290			FundingType::CheckedManualBroadcast(funding_transaction),
6291		)
6292	}
6293
6294	/// Call this upon creation of a batch funding transaction for the given channels.
6295	///
6296	/// Return values are identical to [`Self::funding_transaction_generated`], respective to
6297	/// each individual channel and transaction output.
6298	///
6299	/// Do NOT broadcast the funding transaction yourself. This batch funding transaction
6300	/// will only be broadcast when we have safely received and persisted the counterparty's
6301	/// signature for each channel.
6302	///
6303	/// If there is an error, all channels in the batch are to be considered closed.
6304	pub fn batch_funding_transaction_generated(
6305		&self, temporary_channels: &[(&ChannelId, &PublicKey)], funding_transaction: Transaction,
6306	) -> Result<(), APIError> {
6307		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
6308		let funding_type = FundingType::Checked(funding_transaction);
6309		self.batch_funding_transaction_generated_intern(temporary_channels, funding_type)
6310	}
6311
6312	#[rustfmt::skip]
6313	fn batch_funding_transaction_generated_intern(&self, temporary_channels: &[(&ChannelId, &PublicKey)], funding: FundingType) -> Result<(), APIError> {
6314		let mut result = Ok(());
6315		if let FundingType::Checked(funding_transaction) |
6316			FundingType::CheckedManualBroadcast(funding_transaction) = &funding
6317		{
6318			if !funding_transaction.is_coinbase() {
6319				for inp in funding_transaction.input.iter() {
6320					if inp.witness.is_empty() {
6321						result = result.and(Err(APIError::APIMisuseError {
6322							err: "Funding transaction must be fully signed and spend Segwit outputs".to_owned()
6323						}));
6324					}
6325				}
6326			}
6327
6328			if funding_transaction.output.len() > u16::max_value() as usize {
6329				result = result.and(Err(APIError::APIMisuseError {
6330					err: "Transaction had more than 2^16 outputs, which is not supported".to_owned()
6331				}));
6332			}
6333			let height = self.best_block.read().unwrap().height;
6334			// Transactions are evaluated as final by network mempools if their locktime is strictly
6335			// lower than the next block height. However, the modules constituting our Lightning
6336			// node might not have perfect sync about their blockchain views. Thus, if the wallet
6337			// module is ahead of LDK, only allow one more block of headroom.
6338			if !funding_transaction.input.iter().all(|input| input.sequence == Sequence::MAX) &&
6339				funding_transaction.lock_time.is_block_height() &&
6340					funding_transaction.lock_time.to_consensus_u32() > height + 1
6341			{
6342				result = result.and(Err(APIError::APIMisuseError {
6343					err: "Funding transaction absolute timelock is non-final".to_owned()
6344				}));
6345			}
6346		}
6347
6348		let txid = funding.txid();
6349		let is_batch_funding = temporary_channels.len() > 1;
6350		let mut funding_batch_states = if is_batch_funding {
6351			Some(self.funding_batch_states.lock().unwrap())
6352		} else {
6353			None
6354		};
6355		let mut funding_batch_state = funding_batch_states.as_mut().and_then(|states| {
6356			match states.entry(txid) {
6357				btree_map::Entry::Occupied(_) => {
6358					result = result.clone().and(Err(APIError::APIMisuseError {
6359						err: "Batch funding transaction with the same txid already exists".to_owned()
6360					}));
6361					None
6362				},
6363				btree_map::Entry::Vacant(vacant) => Some(vacant.insert(Vec::new())),
6364			}
6365		});
6366		let is_manual_broadcast = funding.is_manual_broadcast();
6367		for &(temporary_channel_id, counterparty_node_id) in temporary_channels {
6368			result = result.and_then(|_| self.funding_transaction_generated_intern(
6369				*temporary_channel_id,
6370				*counterparty_node_id,
6371				funding.transaction_or_dummy(),
6372				is_batch_funding,
6373				|chan| {
6374					let mut output_index = None;
6375					let expected_spk = chan.funding.get_funding_redeemscript().to_p2wsh();
6376					let outpoint = match &funding {
6377						FundingType::Checked(tx) | FundingType::CheckedManualBroadcast(tx) => {
6378							for (idx, outp) in tx.output.iter().enumerate() {
6379								if outp.script_pubkey == expected_spk && outp.value.to_sat() == chan.funding.get_value_satoshis() {
6380									if output_index.is_some() {
6381										return Err("Multiple outputs matched the expected script and value");
6382									}
6383									output_index = Some(idx as u16);
6384								}
6385							}
6386							if output_index.is_none() {
6387								return Err("No output matched the script_pubkey and value in the FundingGenerationReady event");
6388							}
6389							OutPoint { txid, index: output_index.unwrap() }
6390						},
6391						FundingType::Unchecked(outpoint) => outpoint.clone(),
6392					};
6393					if let Some(funding_batch_state) = funding_batch_state.as_mut() {
6394						// TODO(dual_funding): We only do batch funding for V1 channels at the moment, but we'll probably
6395						// need to fix this somehow to not rely on using the outpoint for the channel ID if we
6396						// want to support V2 batching here as well.
6397						funding_batch_state.push((ChannelId::v1_from_funding_outpoint(outpoint), *counterparty_node_id, false));
6398					}
6399					Ok(outpoint)
6400				},
6401				is_manual_broadcast)
6402			);
6403		}
6404		if let Err(ref e) = result {
6405			// Remaining channels need to be removed on any error.
6406			let e = format!("Error in transaction funding: {:?}", e);
6407			let mut channels_to_remove = Vec::new();
6408			channels_to_remove.extend(funding_batch_states.as_mut()
6409				.and_then(|states| states.remove(&txid))
6410				.into_iter().flatten()
6411				.map(|(chan_id, node_id, _state)| (chan_id, node_id))
6412			);
6413			channels_to_remove.extend(temporary_channels.iter()
6414				.map(|(&chan_id, &node_id)| (chan_id, node_id))
6415			);
6416			let mut shutdown_results: Vec<(Result<Infallible, _>, _)> = Vec::new();
6417			{
6418				let per_peer_state = self.per_peer_state.read().unwrap();
6419				for (channel_id, counterparty_node_id) in channels_to_remove {
6420					per_peer_state.get(&counterparty_node_id)
6421						.map(|peer_state_mutex| peer_state_mutex.lock().unwrap())
6422						.and_then(|mut peer_state| peer_state.channel_by_id.remove(&channel_id).map(|chan| (chan, peer_state)))
6423						.map(|(mut chan, mut peer_state)| {
6424							let reason = ClosureReason::ProcessingError { err: e.clone() };
6425							let err = ChannelError::Close((e.clone(), reason));
6426							let (_, e) =
6427								convert_channel_err!(self, peer_state, err, &mut chan);
6428							shutdown_results.push((Err(e), counterparty_node_id));
6429						});
6430				}
6431			}
6432			mem::drop(funding_batch_states);
6433			for (err, counterparty_node_id) in shutdown_results {
6434				let _ = handle_error!(self, err, counterparty_node_id);
6435			}
6436		}
6437		result
6438	}
6439
6440	/// Handles a signed funding transaction generated by interactive transaction construction and
6441	/// provided by the client. Should only be called in response to a [`FundingTransactionReadyForSigning`]
6442	/// event.
6443	///
6444	/// Do NOT broadcast the funding transaction yourself. When we have safely received our
6445	/// counterparty's signature(s) the funding transaction will automatically be broadcast via the
6446	/// [`BroadcasterInterface`] provided when this `ChannelManager` was constructed.
6447	///
6448	/// `SIGHASH_ALL` MUST be used for all signatures when providing signatures, otherwise your
6449	/// funds can be held hostage!
6450	///
6451	/// LDK checks the following:
6452	///  * Each input spends an output that is one of P2WPKH, P2WSH, or P2TR.
6453	///    These were already checked by LDK when the inputs to be contributed were provided.
6454	///  * All signatures use the `SIGHASH_ALL` sighash type.
6455	///  * P2WPKH and P2TR key path spends are valid (verifies signatures)
6456	///
6457	/// NOTE:
6458	///  * When checking P2WSH spends, LDK tries to decode 70-72 byte witness elements as ECDSA
6459	///    signatures with a sighash flag. If the internal DER-decoding fails, then LDK just
6460	///    assumes it wasn't a signature and carries with checks. If the element can be decoded
6461	///    as an ECDSA signature, the the sighash flag must be `SIGHASH_ALL`.
6462	///  * When checking P2TR script-path spends, LDK assumes all elements of exactly 65 bytes
6463	///    with the last byte matching any valid sighash flag byte are schnorr signatures and checks
6464	///    that the sighash type is `SIGHASH_ALL`. If the last byte is not any valid sighash flag, the
6465	///    element is assumed not to be a signature and is ignored. Elements of 64 bytes are not
6466	///    checked because if they were schnorr signatures then they would implicitly be `SIGHASH_DEFAULT`
6467	///    which is an alias of `SIGHASH_ALL`.
6468	///
6469	/// Returns [`ChannelUnavailable`] when a channel is not found or an incorrect
6470	/// `counterparty_node_id` is provided.
6471	///
6472	/// Returns [`APIMisuseError`] when a channel is not in a state where it is expecting funding
6473	/// signatures or if any of the checks described above fail.
6474	///
6475	/// [`FundingTransactionReadyForSigning`]: events::Event::FundingTransactionReadyForSigning
6476	/// [`ChannelUnavailable`]: APIError::ChannelUnavailable
6477	/// [`APIMisuseError`]: APIError::APIMisuseError
6478	pub fn funding_transaction_signed(
6479		&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, transaction: Transaction,
6480	) -> Result<(), APIError> {
6481		let mut result = Ok(());
6482		PersistenceNotifierGuard::optionally_notify(self, || {
6483			let per_peer_state = self.per_peer_state.read().unwrap();
6484			let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
6485			if peer_state_mutex_opt.is_none() {
6486				result = Err(APIError::ChannelUnavailable {
6487					err: format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}")
6488				});
6489				return NotifyOption::SkipPersistNoEvents;
6490			}
6491
6492			let mut peer_state = peer_state_mutex_opt.unwrap().lock().unwrap();
6493
6494			match peer_state.channel_by_id.get_mut(channel_id) {
6495				Some(channel) => match channel.as_funded_mut() {
6496					Some(chan) => {
6497						let txid = transaction.compute_txid();
6498						let witnesses: Vec<_> = transaction
6499							.input
6500							.into_iter()
6501							.map(|input| input.witness)
6502							.filter(|witness| !witness.is_empty())
6503							.collect();
6504						let best_block_height = self.best_block.read().unwrap().height;
6505						match chan.funding_transaction_signed(
6506							txid,
6507							witnesses,
6508							best_block_height,
6509							&self.logger,
6510						) {
6511							Ok(FundingTxSigned {
6512								tx_signatures: Some(tx_signatures),
6513								funding_tx,
6514								splice_negotiated,
6515								splice_locked,
6516							}) => {
6517								if let Some(funding_tx) = funding_tx {
6518									self.broadcast_interactive_funding(
6519										chan,
6520										&funding_tx,
6521										&self.logger,
6522									);
6523								}
6524								if let Some(splice_negotiated) = splice_negotiated {
6525									self.pending_events.lock().unwrap().push_back((
6526										events::Event::SplicePending {
6527											channel_id: *channel_id,
6528											counterparty_node_id: *counterparty_node_id,
6529											user_channel_id: chan.context.get_user_id(),
6530											new_funding_txo: splice_negotiated.funding_txo,
6531											channel_type: splice_negotiated.channel_type,
6532											new_funding_redeem_script: splice_negotiated
6533												.funding_redeem_script,
6534										},
6535										None,
6536									));
6537								}
6538								peer_state.pending_msg_events.push(
6539									MessageSendEvent::SendTxSignatures {
6540										node_id: *counterparty_node_id,
6541										msg: tx_signatures,
6542									},
6543								);
6544								if let Some(splice_locked) = splice_locked {
6545									peer_state.pending_msg_events.push(
6546										MessageSendEvent::SendSpliceLocked {
6547											node_id: *counterparty_node_id,
6548											msg: splice_locked,
6549										},
6550									);
6551								}
6552								return NotifyOption::DoPersist;
6553							},
6554							Err(err) => {
6555								result = Err(err);
6556								return NotifyOption::SkipPersistNoEvents;
6557							},
6558							Ok(FundingTxSigned {
6559								tx_signatures: None,
6560								funding_tx,
6561								splice_negotiated,
6562								splice_locked,
6563							}) => {
6564								debug_assert!(funding_tx.is_none());
6565								debug_assert!(splice_negotiated.is_none());
6566								debug_assert!(splice_locked.is_none());
6567								return NotifyOption::SkipPersistNoEvents;
6568							},
6569						}
6570					},
6571					None => {
6572						result = Err(APIError::APIMisuseError {
6573							err: format!(
6574								"Channel with id {} not expecting funding signatures",
6575								channel_id
6576							),
6577						});
6578						return NotifyOption::SkipPersistNoEvents;
6579					},
6580				},
6581				None => {
6582					result = Err(APIError::ChannelUnavailable {
6583						err: format!(
6584							"Channel with id {} not found for the passed counterparty node_id {}",
6585							channel_id, counterparty_node_id
6586						),
6587					});
6588					return NotifyOption::SkipPersistNoEvents;
6589				},
6590			}
6591		});
6592
6593		result
6594	}
6595
6596	fn broadcast_interactive_funding(
6597		&self, channel: &mut FundedChannel<SP>, funding_tx: &Transaction, logger: &L,
6598	) {
6599		let logger = WithChannelContext::from(logger, channel.context(), None);
6600		log_info!(
6601			logger,
6602			"Broadcasting signed interactive funding transaction {}",
6603			funding_tx.compute_txid()
6604		);
6605		self.tx_broadcaster.broadcast_transactions(&[funding_tx]);
6606		{
6607			let mut pending_events = self.pending_events.lock().unwrap();
6608			emit_channel_pending_event!(pending_events, channel);
6609		}
6610	}
6611
6612	/// Atomically applies partial updates to the [`ChannelConfig`] of the given channels.
6613	///
6614	/// Once the updates are applied, each eligible channel (advertised with a known short channel
6615	/// ID and a change in [`forwarding_fee_proportional_millionths`], [`forwarding_fee_base_msat`],
6616	/// or [`cltv_expiry_delta`]) has a [`BroadcastChannelUpdate`] event message generated
6617	/// containing the new [`ChannelUpdate`] message which should be broadcast to the network.
6618	///
6619	/// Returns [`ChannelUnavailable`] when a channel is not found or an incorrect
6620	/// `counterparty_node_id` is provided.
6621	///
6622	/// Returns [`APIMisuseError`] when a [`cltv_expiry_delta`] update is to be applied with a value
6623	/// below [`MIN_CLTV_EXPIRY_DELTA`].
6624	///
6625	/// If an error is returned, none of the updates should be considered applied.
6626	///
6627	/// [`forwarding_fee_proportional_millionths`]: ChannelConfig::forwarding_fee_proportional_millionths
6628	/// [`forwarding_fee_base_msat`]: ChannelConfig::forwarding_fee_base_msat
6629	/// [`cltv_expiry_delta`]: ChannelConfig::cltv_expiry_delta
6630	/// [`BroadcastChannelUpdate`]: MessageSendEvent::BroadcastChannelUpdate
6631	/// [`ChannelUpdate`]: msgs::ChannelUpdate
6632	/// [`ChannelUnavailable`]: APIError::ChannelUnavailable
6633	/// [`APIMisuseError`]: APIError::APIMisuseError
6634	#[rustfmt::skip]
6635	pub fn update_partial_channel_config(
6636		&self, counterparty_node_id: &PublicKey, channel_ids: &[ChannelId], config_update: &ChannelConfigUpdate,
6637	) -> Result<(), APIError> {
6638		if config_update.cltv_expiry_delta.map(|delta| delta < MIN_CLTV_EXPIRY_DELTA).unwrap_or(false) {
6639			return Err(APIError::APIMisuseError {
6640				err: format!("The chosen CLTV expiry delta is below the minimum of {}", MIN_CLTV_EXPIRY_DELTA),
6641			});
6642		}
6643
6644		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
6645		let per_peer_state = self.per_peer_state.read().unwrap();
6646		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6647			.ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}") })?;
6648		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6649		let peer_state = &mut *peer_state_lock;
6650
6651		for channel_id in channel_ids {
6652			if !peer_state.has_channel(channel_id) {
6653				return Err(APIError::ChannelUnavailable {
6654					err: format!("Channel with id {} not found for the passed counterparty node_id {}", channel_id, counterparty_node_id),
6655				});
6656			};
6657		}
6658		for channel_id in channel_ids {
6659			if let Some(channel) = peer_state.channel_by_id.get_mut(channel_id) {
6660				let mut config = channel.context().config();
6661				config.apply(config_update);
6662				if !channel.context_mut().update_config(&config) {
6663					continue;
6664				}
6665				if let Some(channel) = channel.as_funded() {
6666					if let Ok(msg) = self.get_channel_update_for_broadcast(channel) {
6667						let mut pending_broadcast_messages = self.pending_broadcast_messages.lock().unwrap();
6668						pending_broadcast_messages.push(MessageSendEvent::BroadcastChannelUpdate { msg });
6669					} else if peer_state.is_connected {
6670						if let Ok(msg) = self.get_channel_update_for_unicast(channel) {
6671							peer_state.pending_msg_events.push(MessageSendEvent::SendChannelUpdate {
6672								node_id: channel.context.get_counterparty_node_id(),
6673								msg,
6674							});
6675						}
6676					}
6677				}
6678				continue;
6679			} else {
6680				// This should not be reachable as we've already checked for non-existence in the previous channel_id loop.
6681				debug_assert!(false);
6682				return Err(APIError::ChannelUnavailable {
6683					err: format!(
6684						"Channel with ID {} for passed counterparty_node_id {} disappeared after we confirmed its existence - this should not be reachable!",
6685						channel_id, counterparty_node_id),
6686				});
6687			};
6688		}
6689		Ok(())
6690	}
6691
6692	/// Atomically updates the [`ChannelConfig`] for the given channels.
6693	///
6694	/// Once the updates are applied, each eligible channel (advertised with a known short channel
6695	/// ID and a change in [`forwarding_fee_proportional_millionths`], [`forwarding_fee_base_msat`],
6696	/// or [`cltv_expiry_delta`]) has a [`BroadcastChannelUpdate`] event message generated
6697	/// containing the new [`ChannelUpdate`] message which should be broadcast to the network.
6698	///
6699	/// Returns [`ChannelUnavailable`] when a channel is not found or an incorrect
6700	/// `counterparty_node_id` is provided.
6701	///
6702	/// Returns [`APIMisuseError`] when a [`cltv_expiry_delta`] update is to be applied with a value
6703	/// below [`MIN_CLTV_EXPIRY_DELTA`].
6704	///
6705	/// If an error is returned, none of the updates should be considered applied.
6706	///
6707	/// [`forwarding_fee_proportional_millionths`]: ChannelConfig::forwarding_fee_proportional_millionths
6708	/// [`forwarding_fee_base_msat`]: ChannelConfig::forwarding_fee_base_msat
6709	/// [`cltv_expiry_delta`]: ChannelConfig::cltv_expiry_delta
6710	/// [`BroadcastChannelUpdate`]: MessageSendEvent::BroadcastChannelUpdate
6711	/// [`ChannelUpdate`]: msgs::ChannelUpdate
6712	/// [`ChannelUnavailable`]: APIError::ChannelUnavailable
6713	/// [`APIMisuseError`]: APIError::APIMisuseError
6714	pub fn update_channel_config(
6715		&self, counterparty_node_id: &PublicKey, channel_ids: &[ChannelId], config: &ChannelConfig,
6716	) -> Result<(), APIError> {
6717		self.update_partial_channel_config(counterparty_node_id, channel_ids, &(*config).into())
6718	}
6719
6720	/// Attempts to forward an intercepted HTLC over the provided channel id and with the provided
6721	/// amount to forward. Should only be called in response to an [`HTLCIntercepted`] event.
6722	///
6723	/// Intercepted HTLCs can be useful for Lightning Service Providers (LSPs) to open a just-in-time
6724	/// channel to a receiving node if the node lacks sufficient inbound liquidity.
6725	///
6726	/// To make use of intercepted HTLCs, set [`UserConfig::accept_intercept_htlcs`] and use
6727	/// [`ChannelManager::get_intercept_scid`] to generate short channel id(s) to put in the
6728	/// receiver's invoice route hints. These route hints will signal to LDK to generate an
6729	/// [`HTLCIntercepted`] event when it receives the forwarded HTLC, and this method or
6730	/// [`ChannelManager::fail_intercepted_htlc`] MUST be called in response to the event.
6731	///
6732	/// Note that LDK does not enforce fee requirements in `amt_to_forward_msat`, and will not stop
6733	/// you from forwarding more than you received. See
6734	/// [`HTLCIntercepted::expected_outbound_amount_msat`] for more on forwarding a different amount
6735	/// than expected.
6736	///
6737	/// Errors if the event was not handled in time, in which case the HTLC was automatically failed
6738	/// backwards.
6739	///
6740	/// [`UserConfig::accept_intercept_htlcs`]: crate::util::config::UserConfig::accept_intercept_htlcs
6741	/// [`HTLCIntercepted`]: events::Event::HTLCIntercepted
6742	/// [`HTLCIntercepted::expected_outbound_amount_msat`]: events::Event::HTLCIntercepted::expected_outbound_amount_msat
6743	// TODO: when we move to deciding the best outbound channel at forward time, only take
6744	// `next_node_id` and not `next_hop_channel_id`
6745	pub fn forward_intercepted_htlc(
6746		&self, intercept_id: InterceptId, next_hop_channel_id: &ChannelId, next_node_id: PublicKey,
6747		amt_to_forward_msat: u64,
6748	) -> Result<(), APIError> {
6749		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
6750
6751		let outbound_scid_alias = {
6752			let peer_state_lock = self.per_peer_state.read().unwrap();
6753			let peer_state_mutex =
6754				peer_state_lock.get(&next_node_id).ok_or_else(|| APIError::ChannelUnavailable {
6755					err: format!(
6756						"Can't find a peer matching the passed counterparty node_id {next_node_id}"
6757					),
6758				})?;
6759			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6760			let peer_state = &mut *peer_state_lock;
6761			match peer_state.channel_by_id.get(next_hop_channel_id) {
6762				Some(chan) => {
6763					if let Some(funded_chan) = chan.as_funded() {
6764						if !funded_chan.context.is_usable() {
6765							return Err(APIError::ChannelUnavailable {
6766								err: format!(
6767									"Channel with id {next_hop_channel_id} not fully established"
6768								),
6769							});
6770						}
6771						funded_chan.context.outbound_scid_alias()
6772					} else {
6773						return Err(APIError::ChannelUnavailable {
6774						err: format!(
6775							"Channel with id {next_hop_channel_id} for the passed counterparty node_id {next_node_id} is still opening."
6776						)
6777					});
6778					}
6779				},
6780				None => {
6781					let error = format!(
6782						"Channel with id {next_hop_channel_id} not found for the passed counterparty node_id {next_node_id}"
6783					);
6784					let logger = WithContext::from(
6785						&self.logger,
6786						Some(next_node_id),
6787						Some(*next_hop_channel_id),
6788						None,
6789					);
6790					log_error!(logger, "{error} when attempting to forward intercepted HTLC");
6791					return Err(APIError::ChannelUnavailable { err: error });
6792				},
6793			}
6794		};
6795
6796		let payment = self
6797			.pending_intercepted_htlcs
6798			.lock()
6799			.unwrap()
6800			.remove(&intercept_id)
6801			.ok_or_else(|| APIError::APIMisuseError {
6802				err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.0)),
6803			})?;
6804
6805		let routing = match payment.forward_info.routing {
6806			PendingHTLCRouting::Forward {
6807				onion_packet,
6808				blinded,
6809				incoming_cltv_expiry,
6810				hold_htlc,
6811				..
6812			} => {
6813				debug_assert!(hold_htlc.is_none(), "Held intercept HTLCs should not be surfaced in an event until the recipient comes online");
6814				PendingHTLCRouting::Forward {
6815					onion_packet,
6816					blinded,
6817					incoming_cltv_expiry,
6818					hold_htlc,
6819					short_channel_id: outbound_scid_alias,
6820				}
6821			},
6822			_ => unreachable!(), // Only `PendingHTLCRouting::Forward`s are intercepted
6823		};
6824		let skimmed_fee_msat =
6825			payment.forward_info.outgoing_amt_msat.saturating_sub(amt_to_forward_msat);
6826		let pending_htlc_info = PendingHTLCInfo {
6827			skimmed_fee_msat: if skimmed_fee_msat == 0 { None } else { Some(skimmed_fee_msat) },
6828			outgoing_amt_msat: amt_to_forward_msat,
6829			routing,
6830			..payment.forward_info
6831		};
6832
6833		let mut per_source_pending_forward = [(
6834			payment.prev_outbound_scid_alias,
6835			payment.prev_counterparty_node_id,
6836			payment.prev_funding_outpoint,
6837			payment.prev_channel_id,
6838			payment.prev_user_channel_id,
6839			vec![(pending_htlc_info, payment.prev_htlc_id)],
6840		)];
6841		self.forward_htlcs(&mut per_source_pending_forward);
6842		Ok(())
6843	}
6844
6845	/// Returns the amount received for an intercepted HTLC while it is pending.
6846	#[doc(hidden)]
6847	pub fn intercepted_htlc_inbound_amount_msat(&self, intercept_id: InterceptId) -> Option<u64> {
6848		self.pending_intercepted_htlcs
6849			.lock()
6850			.unwrap()
6851			.get(&intercept_id)
6852			.and_then(|payment| payment.forward_info.incoming_amt_msat)
6853	}
6854
6855	/// Fails the intercepted HTLC indicated by intercept_id. Should only be called in response to
6856	/// an [`HTLCIntercepted`] event. See [`ChannelManager::forward_intercepted_htlc`].
6857	///
6858	/// Errors if the event was not handled in time, in which case the HTLC was automatically failed
6859	/// backwards.
6860	///
6861	/// [`HTLCIntercepted`]: events::Event::HTLCIntercepted
6862	#[rustfmt::skip]
6863	pub fn fail_intercepted_htlc(&self, intercept_id: InterceptId) -> Result<(), APIError> {
6864		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
6865
6866		let payment = self.pending_intercepted_htlcs.lock().unwrap().remove(&intercept_id)
6867			.ok_or_else(|| APIError::APIMisuseError {
6868				err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.0))
6869			})?;
6870
6871		if let PendingHTLCRouting::Forward { short_channel_id, .. } = payment.forward_info.routing {
6872			let htlc_source = HTLCSource::PreviousHopData(payment.htlc_previous_hop_data());
6873			let reason = HTLCFailReason::from_failure_code(LocalHTLCFailureReason::UnknownNextPeer);
6874			let destination = HTLCHandlingFailureType::InvalidForward { requested_forward_scid: short_channel_id };
6875			let hash = payment.forward_info.payment_hash;
6876			self.fail_htlc_backwards_internal(&htlc_source, &hash, &reason, destination, None);
6877		} else { unreachable!() } // Only `PendingHTLCRouting::Forward`s are intercepted
6878
6879		Ok(())
6880	}
6881
6882	#[cfg(any(test, feature = "_test_utils"))]
6883	/// Process any pending inbound [`msgs::UpdateAddHTLC`] messages, decoding the onion and placing
6884	/// the pending HTLC in `ChannelManager::forward_htlcs` or
6885	/// `ChannelManager::pending_intercepted_htlcs` as well as generating relevant [`Event`]s.
6886	pub fn test_process_pending_update_add_htlcs(&self) -> bool {
6887		self.process_pending_update_add_htlcs()
6888	}
6889
6890	fn process_pending_update_add_htlcs(&self) -> bool {
6891		let mut should_persist = false;
6892		let mut decode_update_add_htlcs = new_hash_map();
6893		mem::swap(&mut decode_update_add_htlcs, &mut self.decode_update_add_htlcs.lock().unwrap());
6894
6895		let get_htlc_failure_type = |outgoing_scid_opt: Option<u64>, payment_hash: PaymentHash| {
6896			if let Some(outgoing_scid) = outgoing_scid_opt {
6897				match self.short_to_chan_info.read().unwrap().get(&outgoing_scid) {
6898					Some((outgoing_counterparty_node_id, outgoing_channel_id)) => {
6899						HTLCHandlingFailureType::Forward {
6900							node_id: Some(*outgoing_counterparty_node_id),
6901							channel_id: *outgoing_channel_id,
6902						}
6903					},
6904					None => HTLCHandlingFailureType::InvalidForward {
6905						requested_forward_scid: outgoing_scid,
6906					},
6907				}
6908			} else {
6909				HTLCHandlingFailureType::Receive { payment_hash }
6910			}
6911		};
6912
6913		'outer_loop: for (incoming_scid_alias, update_add_htlcs) in decode_update_add_htlcs {
6914			// If any decoded update_add_htlcs were processed, we need to persist.
6915			should_persist = true;
6916			let incoming_channel_details_opt = self.do_funded_channel_callback(
6917				incoming_scid_alias,
6918				|chan: &mut FundedChannel<SP>| {
6919					let counterparty_node_id = chan.context.get_counterparty_node_id();
6920					let channel_id = chan.context.channel_id();
6921					let funding_txo = chan.funding.get_funding_txo().unwrap();
6922					let user_channel_id = chan.context.get_user_id();
6923					let accept_underpaying_htlcs = chan.context.config().accept_underpaying_htlcs;
6924					(
6925						counterparty_node_id,
6926						channel_id,
6927						funding_txo,
6928						user_channel_id,
6929						accept_underpaying_htlcs,
6930					)
6931				},
6932			);
6933			let (
6934				incoming_counterparty_node_id,
6935				incoming_channel_id,
6936				incoming_funding_txo,
6937				incoming_user_channel_id,
6938				incoming_accept_underpaying_htlcs,
6939			) = if let Some(incoming_channel_details) = incoming_channel_details_opt {
6940				incoming_channel_details
6941			} else {
6942				// The incoming channel no longer exists, HTLCs should be resolved onchain instead.
6943				continue;
6944			};
6945
6946			let mut htlc_forwards = Vec::new();
6947			let mut htlc_fails = Vec::new();
6948			for update_add_htlc in &update_add_htlcs {
6949				let (next_hop, next_packet_details_opt) =
6950					match decode_incoming_update_add_htlc_onion(
6951						&update_add_htlc,
6952						&*self.node_signer,
6953						&*self.logger,
6954						&self.secp_ctx,
6955					) {
6956						Ok(decoded_onion) => decoded_onion,
6957
6958						Err((htlc_fail, reason)) => {
6959							let failure_type = HTLCHandlingFailureType::InvalidOnion;
6960							htlc_fails.push((htlc_fail, failure_type, reason.into()));
6961							continue;
6962						},
6963					};
6964
6965				let is_intro_node_blinded_forward = next_hop.is_intro_node_blinded_forward();
6966				let outgoing_scid_opt =
6967					next_packet_details_opt.as_ref().and_then(|d| match d.outgoing_connector {
6968						HopConnector::ShortChannelId(scid) => Some(scid),
6969						HopConnector::Trampoline(_) => None,
6970					});
6971				let shared_secret = next_hop.shared_secret().secret_bytes();
6972
6973				// Nodes shouldn't expect us to hold HTLCs for them if we don't advertise htlc_hold feature
6974				// support.
6975				//
6976				// If we wanted to pretend to be a node that didn't understand the feature at all here, the
6977				// correct behavior would've been to disconnect the sender when we first received the
6978				// update_add message. However, this would make the `UserConfig::enable_htlc_hold` option
6979				// unsafe -- if our node switched the config option from on to off just after the sender
6980				// enqueued their update_add + CS, the sender would continue retransmitting those messages
6981				// and we would keep disconnecting them until the HTLC timed out.
6982				if update_add_htlc.hold_htlc.is_some()
6983					&& !BaseMessageHandler::provided_node_features(self).supports_htlc_hold()
6984				{
6985					let reason = LocalHTLCFailureReason::TemporaryNodeFailure;
6986					let htlc_fail = self.htlc_failure_from_update_add_err(
6987						&update_add_htlc,
6988						&incoming_counterparty_node_id,
6989						reason,
6990						is_intro_node_blinded_forward,
6991						&shared_secret,
6992					);
6993					let failure_type =
6994						get_htlc_failure_type(outgoing_scid_opt, update_add_htlc.payment_hash);
6995					htlc_fails.push((htlc_fail, failure_type, reason.into()));
6996					continue;
6997				}
6998
6999				// Process the HTLC on the incoming channel.
7000				match self.do_funded_channel_callback(
7001					incoming_scid_alias,
7002					|chan: &mut FundedChannel<SP>| {
7003						let logger = WithChannelContext::from(
7004							&self.logger,
7005							&chan.context,
7006							Some(update_add_htlc.payment_hash),
7007						);
7008						chan.can_accept_incoming_htlc(&self.fee_estimator, &logger)
7009					},
7010				) {
7011					Some(Ok(_)) => {},
7012					Some(Err(reason)) => {
7013						let htlc_fail = self.htlc_failure_from_update_add_err(
7014							&update_add_htlc,
7015							&incoming_counterparty_node_id,
7016							reason,
7017							is_intro_node_blinded_forward,
7018							&shared_secret,
7019						);
7020						let failure_type =
7021							get_htlc_failure_type(outgoing_scid_opt, update_add_htlc.payment_hash);
7022						htlc_fails.push((htlc_fail, failure_type, reason.into()));
7023						continue;
7024					},
7025					// The incoming channel no longer exists, HTLCs should be resolved onchain instead.
7026					None => continue 'outer_loop,
7027				}
7028
7029				// Now process the HTLC on the outgoing channel if it's a forward.
7030				if let Some(next_packet_details) = next_packet_details_opt.as_ref() {
7031					if let Err(reason) =
7032						self.can_forward_htlc(&update_add_htlc, next_packet_details)
7033					{
7034						let htlc_fail = self.htlc_failure_from_update_add_err(
7035							&update_add_htlc,
7036							&incoming_counterparty_node_id,
7037							reason,
7038							is_intro_node_blinded_forward,
7039							&shared_secret,
7040						);
7041						let failure_type =
7042							get_htlc_failure_type(outgoing_scid_opt, update_add_htlc.payment_hash);
7043						htlc_fails.push((htlc_fail, failure_type, reason.into()));
7044						continue;
7045					}
7046				}
7047
7048				match self.get_pending_htlc_info(
7049					&update_add_htlc,
7050					shared_secret,
7051					next_hop,
7052					incoming_accept_underpaying_htlcs,
7053					next_packet_details_opt.map(|d| d.next_packet_pubkey),
7054				) {
7055					Ok(info) => htlc_forwards.push((info, update_add_htlc.htlc_id)),
7056					Err(inbound_err) => {
7057						let failure_type =
7058							get_htlc_failure_type(outgoing_scid_opt, update_add_htlc.payment_hash);
7059						let htlc_failure = inbound_err.reason.into();
7060						let htlc_fail = self.construct_pending_htlc_fail_msg(
7061							&update_add_htlc,
7062							&incoming_counterparty_node_id,
7063							shared_secret,
7064							inbound_err,
7065						);
7066						htlc_fails.push((htlc_fail, failure_type, htlc_failure));
7067					},
7068				}
7069			}
7070
7071			// Process all of the forwards and failures for the channel in which the HTLCs were
7072			// proposed to as a batch.
7073			let pending_forwards = (
7074				incoming_scid_alias,
7075				incoming_counterparty_node_id,
7076				incoming_funding_txo,
7077				incoming_channel_id,
7078				incoming_user_channel_id,
7079				htlc_forwards.drain(..).collect(),
7080			);
7081			self.forward_htlcs(&mut [pending_forwards]);
7082			for (htlc_fail, failure_type, failure_reason) in htlc_fails.drain(..) {
7083				let failure = match htlc_fail {
7084					HTLCFailureMsg::Relay(fail_htlc) => HTLCForwardInfo::FailHTLC {
7085						htlc_id: fail_htlc.htlc_id,
7086						err_packet: fail_htlc.into(),
7087					},
7088					HTLCFailureMsg::Malformed(fail_malformed_htlc) => {
7089						HTLCForwardInfo::FailMalformedHTLC {
7090							htlc_id: fail_malformed_htlc.htlc_id,
7091							sha256_of_onion: fail_malformed_htlc.sha256_of_onion,
7092							failure_code: fail_malformed_htlc.failure_code.into(),
7093						}
7094					},
7095				};
7096				self.forward_htlcs
7097					.lock()
7098					.unwrap()
7099					.entry(incoming_scid_alias)
7100					.or_default()
7101					.push(failure);
7102				self.pending_events.lock().unwrap().push_back((
7103					events::Event::HTLCHandlingFailed {
7104						prev_channel_id: incoming_channel_id,
7105						failure_type,
7106						failure_reason: Some(failure_reason),
7107					},
7108					None,
7109				));
7110			}
7111		}
7112		should_persist
7113	}
7114
7115	/// Returns whether we have pending HTLC forwards that need to be processed via
7116	/// [`Self::process_pending_htlc_forwards`].
7117	pub fn needs_pending_htlc_processing(&self) -> bool {
7118		if !self.forward_htlcs.lock().unwrap().is_empty() {
7119			return true;
7120		}
7121		if !self.decode_update_add_htlcs.lock().unwrap().is_empty() {
7122			return true;
7123		}
7124		if self.pending_outbound_payments.needs_abandon_or_retry() {
7125			return true;
7126		}
7127		false
7128	}
7129
7130	/// Processes HTLCs which are pending waiting on random forward delay.
7131	///
7132	/// Will be regularly called by LDK's background processor.
7133	///
7134	/// Users implementing their own background processing logic should call this in irregular,
7135	/// randomly-distributed intervals.
7136	pub fn process_pending_htlc_forwards(&self) {
7137		if self
7138			.pending_htlc_forwards_processor
7139			.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
7140			.is_err()
7141		{
7142			return;
7143		}
7144
7145		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
7146			self.internal_process_pending_htlc_forwards()
7147		});
7148
7149		self.pending_htlc_forwards_processor.store(false, Ordering::Release);
7150	}
7151
7152	// Returns whether or not we need to re-persist.
7153	fn internal_process_pending_htlc_forwards(&self) -> NotifyOption {
7154		let mut should_persist = NotifyOption::SkipPersistNoEvents;
7155
7156		if self.process_pending_update_add_htlcs() {
7157			should_persist = NotifyOption::DoPersist;
7158		}
7159
7160		let mut new_events = VecDeque::new();
7161		let mut failed_forwards = Vec::new();
7162		let mut phantom_receives: Vec<PerSourcePendingForward> = Vec::new();
7163		let mut forward_htlcs = new_hash_map();
7164		mem::swap(&mut forward_htlcs, &mut self.forward_htlcs.lock().unwrap());
7165
7166		for (short_chan_id, mut pending_forwards) in forward_htlcs {
7167			should_persist = NotifyOption::DoPersist;
7168			if short_chan_id != 0 {
7169				self.process_forward_htlcs(
7170					short_chan_id,
7171					&mut pending_forwards,
7172					&mut failed_forwards,
7173					&mut phantom_receives,
7174				);
7175			} else {
7176				self.process_receive_htlcs(
7177					&mut pending_forwards,
7178					&mut new_events,
7179					&mut failed_forwards,
7180				);
7181			}
7182		}
7183
7184		let best_block_height = self.best_block.read().unwrap().height;
7185		let needs_persist = self.pending_outbound_payments.check_retry_payments(
7186			&self.router,
7187			|| self.list_usable_channels(),
7188			|| self.compute_inflight_htlcs(),
7189			&self.entropy_source,
7190			&self.node_signer,
7191			best_block_height,
7192			&self.pending_events,
7193			|args| self.send_payment_along_path(args),
7194			&WithContext::from(&self.logger, None, None, None),
7195		);
7196		if needs_persist {
7197			should_persist = NotifyOption::DoPersist;
7198		}
7199
7200		for (htlc_source, payment_hash, failure_reason, destination) in failed_forwards.drain(..) {
7201			self.fail_htlc_backwards_internal(
7202				&htlc_source,
7203				&payment_hash,
7204				&failure_reason,
7205				destination,
7206				None,
7207			);
7208		}
7209		self.forward_htlcs(&mut phantom_receives);
7210
7211		// Freeing the holding cell here is relatively redundant - in practice we'll do it when we
7212		// next get a `get_and_clear_pending_msg_events` call, but some tests rely on it, and it's
7213		// nice to do the work now if we can rather than while we're trying to get messages in the
7214		// network stack.
7215		if self.check_free_holding_cells() {
7216			should_persist = NotifyOption::DoPersist;
7217		}
7218
7219		if new_events.is_empty() {
7220			return should_persist;
7221		}
7222		let mut events = self.pending_events.lock().unwrap();
7223		events.append(&mut new_events);
7224		should_persist = NotifyOption::DoPersist;
7225
7226		should_persist
7227	}
7228
7229	/// Fail the list of provided HTLC forwards because the channel they were to be forwarded over does no longer exist.
7230	fn forwarding_channel_not_found(
7231		&self, forward_infos: impl Iterator<Item = HTLCForwardInfo>, short_chan_id: u64,
7232		forwarding_counterparty: Option<PublicKey>, failed_forwards: &mut Vec<FailedHTLCForward>,
7233		phantom_receives: &mut Vec<PerSourcePendingForward>,
7234	) {
7235		for forward_info in forward_infos {
7236			match forward_info {
7237				HTLCForwardInfo::AddHTLC(payment) => {
7238					let PendingAddHTLCInfo {
7239						prev_outbound_scid_alias,
7240						prev_htlc_id,
7241						prev_channel_id,
7242						prev_funding_outpoint,
7243						prev_user_channel_id,
7244						prev_counterparty_node_id,
7245						forward_info:
7246							PendingHTLCInfo {
7247								ref routing,
7248								incoming_shared_secret,
7249								payment_hash,
7250								outgoing_amt_msat,
7251								outgoing_cltv_value,
7252								..
7253							},
7254					} = payment;
7255					let logger = WithContext::from(
7256						&self.logger,
7257						forwarding_counterparty,
7258						Some(prev_channel_id),
7259						Some(payment_hash),
7260					);
7261					let mut failure_handler =
7262						|msg, reason, err_data, phantom_ss, next_hop_unknown| {
7263							log_info!(logger, "Failed to accept/forward incoming HTLC: {}", msg);
7264
7265							let mut prev_hop = payment.htlc_previous_hop_data();
7266							// Override the phantom shared secret because it wasn't set in the originating
7267							// `PendingAddHTLCInfo` above, it was calculated below after detecting this as a
7268							// phantom payment.
7269							prev_hop.phantom_shared_secret = phantom_ss;
7270							let failure_type = if next_hop_unknown {
7271								HTLCHandlingFailureType::InvalidForward {
7272									requested_forward_scid: short_chan_id,
7273								}
7274							} else {
7275								HTLCHandlingFailureType::Receive { payment_hash }
7276							};
7277
7278							failed_forwards.push((
7279								HTLCSource::PreviousHopData(prev_hop),
7280								payment_hash,
7281								HTLCFailReason::reason(reason, err_data),
7282								failure_type,
7283							));
7284						};
7285
7286					if let PendingHTLCRouting::Forward { ref onion_packet, .. } = routing {
7287						let phantom_pubkey_res =
7288							self.node_signer.get_node_id(Recipient::PhantomNode);
7289						if phantom_pubkey_res.is_ok()
7290							&& fake_scid::is_valid_phantom(
7291								&self.fake_scid_rand_bytes,
7292								short_chan_id,
7293								&self.chain_hash,
7294							) {
7295							let decode_res = onion_utils::decode_next_payment_hop(
7296								Recipient::PhantomNode,
7297								&onion_packet.public_key.unwrap(),
7298								&onion_packet.hop_data,
7299								onion_packet.hmac,
7300								payment_hash,
7301								None,
7302								&*self.node_signer,
7303							);
7304							let next_hop = match decode_res {
7305								Ok(res) => res,
7306								Err(onion_utils::OnionDecodeErr::Malformed { err_msg, reason }) => {
7307									let sha256_of_onion =
7308										Sha256::hash(&onion_packet.hop_data).to_byte_array();
7309									// In this scenario, the phantom would have sent us an
7310									// `update_fail_malformed_htlc`, meaning here we encrypt the error as
7311									// if it came from us (the second-to-last hop) but contains the sha256
7312									// of the onion.
7313									failure_handler(
7314										err_msg,
7315										reason,
7316										sha256_of_onion.to_vec(),
7317										None,
7318										false,
7319									);
7320									continue;
7321								},
7322								Err(onion_utils::OnionDecodeErr::Relay {
7323									err_msg,
7324									reason,
7325									shared_secret,
7326									..
7327								}) => {
7328									let phantom_shared_secret = shared_secret.secret_bytes();
7329									failure_handler(
7330										err_msg,
7331										reason,
7332										Vec::new(),
7333										Some(phantom_shared_secret),
7334										false,
7335									);
7336									continue;
7337								},
7338							};
7339							let phantom_shared_secret = next_hop.shared_secret().secret_bytes();
7340							let current_height: u32 = self.best_block.read().unwrap().height;
7341							let create_res = create_recv_pending_htlc_info(
7342								next_hop,
7343								incoming_shared_secret,
7344								payment_hash,
7345								outgoing_amt_msat,
7346								outgoing_cltv_value,
7347								Some(phantom_shared_secret),
7348								false,
7349								None,
7350								current_height,
7351							);
7352							match create_res {
7353								Ok(info) => phantom_receives.push((
7354									prev_outbound_scid_alias,
7355									prev_counterparty_node_id,
7356									prev_funding_outpoint,
7357									prev_channel_id,
7358									prev_user_channel_id,
7359									vec![(info, prev_htlc_id)],
7360								)),
7361								Err(InboundHTLCErr { reason, err_data, msg }) => {
7362									failure_handler(
7363										msg,
7364										reason,
7365										err_data,
7366										Some(phantom_shared_secret),
7367										false,
7368									);
7369									continue;
7370								},
7371							}
7372						} else {
7373							let msg = format!(
7374								"Unknown short channel id {} for forward HTLC",
7375								short_chan_id
7376							);
7377							failure_handler(
7378								&msg,
7379								LocalHTLCFailureReason::UnknownNextPeer,
7380								Vec::new(),
7381								None,
7382								true,
7383							);
7384							continue;
7385						}
7386					} else {
7387						let msg =
7388							format!("Unknown short channel id {} for forward HTLC", short_chan_id);
7389						failure_handler(
7390							&msg,
7391							LocalHTLCFailureReason::UnknownNextPeer,
7392							Vec::new(),
7393							None,
7394							true,
7395						);
7396						continue;
7397					}
7398				},
7399				HTLCForwardInfo::FailHTLC { .. } | HTLCForwardInfo::FailMalformedHTLC { .. } => {
7400					// Channel went away before we could fail it. This implies
7401					// the channel is now on chain and our counterparty is
7402					// trying to broadcast the HTLC-Timeout, but that's their
7403					// problem, not ours.
7404				},
7405			}
7406		}
7407	}
7408
7409	fn process_forward_htlcs(
7410		&self, short_chan_id: u64, pending_forwards: &mut Vec<HTLCForwardInfo>,
7411		failed_forwards: &mut Vec<FailedHTLCForward>,
7412		phantom_receives: &mut Vec<PerSourcePendingForward>,
7413	) {
7414		let mut forwarding_counterparty = None;
7415
7416		let chan_info_opt = self.short_to_chan_info.read().unwrap().get(&short_chan_id).cloned();
7417		let (counterparty_node_id, forward_chan_id) = match chan_info_opt {
7418			Some((cp_id, chan_id)) => (cp_id, chan_id),
7419			None => {
7420				self.forwarding_channel_not_found(
7421					pending_forwards.drain(..),
7422					short_chan_id,
7423					forwarding_counterparty,
7424					failed_forwards,
7425					phantom_receives,
7426				);
7427				return;
7428			},
7429		};
7430		forwarding_counterparty = Some(counterparty_node_id);
7431		let per_peer_state = self.per_peer_state.read().unwrap();
7432		let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
7433		if peer_state_mutex_opt.is_none() {
7434			self.forwarding_channel_not_found(
7435				pending_forwards.drain(..),
7436				short_chan_id,
7437				forwarding_counterparty,
7438				failed_forwards,
7439				phantom_receives,
7440			);
7441			return;
7442		}
7443		let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
7444		let peer_state = &mut *peer_state_lock;
7445		let mut draining_pending_forwards = pending_forwards.drain(..);
7446		while let Some(forward_info) = draining_pending_forwards.next() {
7447			let queue_fail_htlc_res = match forward_info {
7448				HTLCForwardInfo::AddHTLC(ref payment) => {
7449					let htlc_source = HTLCSource::PreviousHopData(payment.htlc_previous_hop_data());
7450					let PendingAddHTLCInfo {
7451						prev_outbound_scid_alias,
7452						forward_info:
7453							PendingHTLCInfo {
7454								payment_hash,
7455								outgoing_amt_msat,
7456								outgoing_cltv_value,
7457								routing,
7458								skimmed_fee_msat,
7459								..
7460							},
7461						..
7462					} = payment;
7463					let (onion_packet, blinded) = match routing {
7464						PendingHTLCRouting::Forward { ref onion_packet, blinded, .. } => {
7465							(onion_packet, blinded)
7466						},
7467						_ => {
7468							panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
7469						},
7470					};
7471					let next_blinding_point = blinded.and_then(|b| {
7472						b.next_blinding_override.or_else(|| {
7473							let encrypted_tlvs_ss = self
7474								.node_signer
7475								.ecdh(Recipient::Node, &b.inbound_blinding_point, None)
7476								.unwrap()
7477								.secret_bytes();
7478							onion_utils::next_hop_pubkey(
7479								&self.secp_ctx,
7480								b.inbound_blinding_point,
7481								&encrypted_tlvs_ss,
7482							)
7483							.ok()
7484						})
7485					});
7486
7487					// Forward the HTLC over the most appropriate channel with the corresponding peer,
7488					// applying non-strict forwarding.
7489					// The channel with the least amount of outbound liquidity will be used to maximize the
7490					// probability of being able to successfully forward a subsequent HTLC.
7491					let maybe_optimal_channel = peer_state
7492						.channel_by_id
7493						.values_mut()
7494						.filter_map(Channel::as_funded_mut)
7495						.filter_map(|chan| {
7496							let balances = chan.get_available_balances(&self.fee_estimator);
7497							let is_in_range = (balances.next_outbound_htlc_minimum_msat
7498								..=balances.next_outbound_htlc_limit_msat)
7499								.contains(&outgoing_amt_msat);
7500							if is_in_range && chan.context.is_usable() {
7501								Some((chan, balances))
7502							} else {
7503								None
7504							}
7505						})
7506						.min_by_key(|(_, balances)| balances.next_outbound_htlc_limit_msat)
7507						.map(|(c, _)| c);
7508					let optimal_channel = match maybe_optimal_channel {
7509						Some(chan) => chan,
7510						None => {
7511							// Fall back to the specified channel to return an appropriate error.
7512							if let Some(chan) = peer_state
7513								.channel_by_id
7514								.get_mut(&forward_chan_id)
7515								.and_then(Channel::as_funded_mut)
7516							{
7517								chan
7518							} else {
7519								let fwd_iter =
7520									core::iter::once(forward_info).chain(draining_pending_forwards);
7521								self.forwarding_channel_not_found(
7522									fwd_iter,
7523									short_chan_id,
7524									forwarding_counterparty,
7525									failed_forwards,
7526									phantom_receives,
7527								);
7528								break;
7529							}
7530						},
7531					};
7532
7533					let logger = WithChannelContext::from(
7534						&self.logger,
7535						&optimal_channel.context,
7536						Some(*payment_hash),
7537					);
7538					let channel_description =
7539						if optimal_channel.funding.get_short_channel_id() == Some(short_chan_id) {
7540							"specified"
7541						} else {
7542							"alternate"
7543						};
7544					log_trace!(logger, "Forwarding HTLC from SCID {} with payment_hash {} and next hop SCID {} over {} channel {} with corresponding peer {}",
7545						prev_outbound_scid_alias, &payment_hash, short_chan_id, channel_description, optimal_channel.context.channel_id(), &counterparty_node_id);
7546					if let Err((reason, msg)) = optimal_channel.queue_add_htlc(
7547						*outgoing_amt_msat,
7548						*payment_hash,
7549						*outgoing_cltv_value,
7550						htlc_source.clone(),
7551						onion_packet.clone(),
7552						*skimmed_fee_msat,
7553						next_blinding_point,
7554						&self.fee_estimator,
7555						&&logger,
7556					) {
7557						log_trace!(
7558							logger,
7559							"Failed to forward HTLC with payment_hash {} to peer {}: {}",
7560							&payment_hash,
7561							&counterparty_node_id,
7562							msg
7563						);
7564
7565						if let Some(chan) = peer_state
7566							.channel_by_id
7567							.get_mut(&forward_chan_id)
7568							.and_then(Channel::as_funded_mut)
7569						{
7570							let data = self.get_htlc_inbound_temp_fail_data(reason);
7571							let failure_type = HTLCHandlingFailureType::Forward {
7572								node_id: Some(chan.context.get_counterparty_node_id()),
7573								channel_id: forward_chan_id,
7574							};
7575							failed_forwards.push((
7576								htlc_source,
7577								*payment_hash,
7578								HTLCFailReason::reason(reason, data),
7579								failure_type,
7580							));
7581						} else {
7582							self.forwarding_channel_not_found(
7583								core::iter::once(forward_info).chain(draining_pending_forwards),
7584								short_chan_id,
7585								forwarding_counterparty,
7586								failed_forwards,
7587								phantom_receives,
7588							);
7589							break;
7590						}
7591					}
7592					None
7593				},
7594				HTLCForwardInfo::FailHTLC { htlc_id, ref err_packet } => {
7595					if let Some(chan) = peer_state
7596						.channel_by_id
7597						.get_mut(&forward_chan_id)
7598						.and_then(Channel::as_funded_mut)
7599					{
7600						let logger = WithChannelContext::from(&self.logger, &chan.context, None);
7601						log_trace!(logger, "Failing HTLC back to channel with short id {} (backward HTLC ID {}) after delay", short_chan_id, htlc_id);
7602						Some((chan.queue_fail_htlc(htlc_id, err_packet.clone(), &&logger), htlc_id))
7603					} else {
7604						self.forwarding_channel_not_found(
7605							core::iter::once(forward_info).chain(draining_pending_forwards),
7606							short_chan_id,
7607							forwarding_counterparty,
7608							failed_forwards,
7609							phantom_receives,
7610						);
7611						break;
7612					}
7613				},
7614				HTLCForwardInfo::FailMalformedHTLC { htlc_id, failure_code, sha256_of_onion } => {
7615					if let Some(chan) = peer_state
7616						.channel_by_id
7617						.get_mut(&forward_chan_id)
7618						.and_then(Channel::as_funded_mut)
7619					{
7620						let logger = WithChannelContext::from(&self.logger, &chan.context, None);
7621						log_trace!(logger, "Failing malformed HTLC back to channel with short id {} (backward HTLC ID {}) after delay", short_chan_id, htlc_id);
7622						let res = chan.queue_fail_malformed_htlc(
7623							htlc_id,
7624							failure_code,
7625							sha256_of_onion,
7626							&&logger,
7627						);
7628						Some((res, htlc_id))
7629					} else {
7630						self.forwarding_channel_not_found(
7631							core::iter::once(forward_info).chain(draining_pending_forwards),
7632							short_chan_id,
7633							forwarding_counterparty,
7634							failed_forwards,
7635							phantom_receives,
7636						);
7637						break;
7638					}
7639				},
7640			};
7641			if let Some((queue_fail_htlc_res, htlc_id)) = queue_fail_htlc_res {
7642				if let Err(e) = queue_fail_htlc_res {
7643					if let ChannelError::Ignore(msg) = e {
7644						if let Some(chan) = peer_state
7645							.channel_by_id
7646							.get_mut(&forward_chan_id)
7647							.and_then(Channel::as_funded_mut)
7648						{
7649							let logger =
7650								WithChannelContext::from(&self.logger, &chan.context, None);
7651							log_trace!(
7652								logger,
7653								"Failed to fail HTLC with ID {} backwards to short_id {}: {}",
7654								htlc_id,
7655								short_chan_id,
7656								msg
7657							);
7658						}
7659					} else {
7660						panic!("Stated return value requirements in queue_fail_{{malformed_}}htlc() were not met");
7661					}
7662					// fail-backs are best-effort, we probably already have one
7663					// pending, and if not that's OK, if not, the channel is on
7664					// the chain and sending the HTLC-Timeout is their problem.
7665				}
7666			}
7667		}
7668	}
7669
7670	fn process_receive_htlcs(
7671		&self, pending_forwards: &mut Vec<HTLCForwardInfo>,
7672		new_events: &mut VecDeque<(Event, Option<EventCompletionAction>)>,
7673		failed_forwards: &mut Vec<FailedHTLCForward>,
7674	) {
7675		'next_forwardable_htlc: for forward_info in pending_forwards.drain(..) {
7676			match forward_info {
7677				HTLCForwardInfo::AddHTLC(payment) => {
7678					let prev_hop = payment.htlc_previous_hop_data();
7679					let PendingAddHTLCInfo {
7680						prev_channel_id,
7681						prev_funding_outpoint,
7682						forward_info:
7683							PendingHTLCInfo {
7684								routing,
7685								payment_hash,
7686								incoming_amt_msat,
7687								outgoing_amt_msat,
7688								skimmed_fee_msat,
7689								..
7690							},
7691						..
7692					} = payment;
7693					let blinded_failure = routing.blinded_failure();
7694					let (
7695						cltv_expiry,
7696						onion_payload,
7697						payment_data,
7698						payment_context,
7699						phantom_shared_secret,
7700						mut onion_fields,
7701						has_recipient_created_payment_secret,
7702						invoice_request_opt,
7703					) = match routing {
7704						PendingHTLCRouting::Receive {
7705							payment_data,
7706							payment_metadata,
7707							payment_context,
7708							incoming_cltv_expiry,
7709							phantom_shared_secret,
7710							custom_tlvs,
7711							requires_blinded_error: _,
7712						} => {
7713							let _legacy_hop_data = Some(payment_data.clone());
7714							let onion_fields = RecipientOnionFields {
7715								payment_secret: Some(payment_data.payment_secret),
7716								payment_metadata,
7717								custom_tlvs,
7718							};
7719							(
7720								incoming_cltv_expiry,
7721								OnionPayload::Invoice { _legacy_hop_data },
7722								Some(payment_data),
7723								payment_context,
7724								phantom_shared_secret,
7725								onion_fields,
7726								true,
7727								None,
7728							)
7729						},
7730						PendingHTLCRouting::ReceiveKeysend {
7731							payment_data,
7732							payment_preimage,
7733							payment_metadata,
7734							incoming_cltv_expiry,
7735							custom_tlvs,
7736							requires_blinded_error: _,
7737							has_recipient_created_payment_secret,
7738							payment_context,
7739							invoice_request,
7740						} => {
7741							let onion_fields = RecipientOnionFields {
7742								payment_secret: payment_data
7743									.as_ref()
7744									.map(|data| data.payment_secret),
7745								payment_metadata,
7746								custom_tlvs,
7747							};
7748							(
7749								incoming_cltv_expiry,
7750								OnionPayload::Spontaneous(payment_preimage),
7751								payment_data,
7752								payment_context,
7753								None,
7754								onion_fields,
7755								has_recipient_created_payment_secret,
7756								invoice_request,
7757							)
7758						},
7759						_ => {
7760							panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
7761						},
7762					};
7763					let claimable_htlc = ClaimableHTLC {
7764						prev_hop,
7765						// We differentiate the received value from the sender intended value
7766						// if possible so that we don't prematurely mark MPP payments complete
7767						// if routing nodes overpay
7768						value: incoming_amt_msat.unwrap_or(outgoing_amt_msat),
7769						sender_intended_value: outgoing_amt_msat,
7770						timer_ticks: 0,
7771						total_value_received: None,
7772						total_msat: if let Some(data) = &payment_data {
7773							data.total_msat
7774						} else {
7775							outgoing_amt_msat
7776						},
7777						cltv_expiry,
7778						onion_payload,
7779						counterparty_skimmed_fee_msat: skimmed_fee_msat,
7780					};
7781
7782					let mut just_added = false;
7783					let mut committed_to_claimable = false;
7784
7785					let mut claimable_payments = self.claimable_payments.lock().unwrap();
7786					macro_rules! fail_htlc {
7787						($htlc: expr, $payment_hash: expr) => {
7788							debug_assert!(!committed_to_claimable);
7789							let err_data = invalid_payment_err_data(
7790								$htlc.value,
7791								self.best_block.read().unwrap().height,
7792							);
7793							let counterparty_node_id = $htlc.prev_hop.counterparty_node_id;
7794							let incoming_packet_shared_secret =
7795								$htlc.prev_hop.incoming_packet_shared_secret;
7796							let prev_outbound_scid_alias = $htlc.prev_hop.prev_outbound_scid_alias;
7797							failed_forwards.push((
7798								HTLCSource::PreviousHopData(HTLCPreviousHopData {
7799									prev_outbound_scid_alias,
7800									user_channel_id: $htlc.prev_hop.user_channel_id,
7801									counterparty_node_id,
7802									channel_id: prev_channel_id,
7803									outpoint: prev_funding_outpoint,
7804									htlc_id: $htlc.prev_hop.htlc_id,
7805									incoming_packet_shared_secret,
7806									phantom_shared_secret,
7807									blinded_failure,
7808									cltv_expiry: Some(cltv_expiry),
7809								}),
7810								payment_hash,
7811								HTLCFailReason::reason(
7812									LocalHTLCFailureReason::IncorrectPaymentDetails,
7813									err_data,
7814								),
7815								HTLCHandlingFailureType::Receive { payment_hash: $payment_hash },
7816							));
7817							if just_added {
7818								claimable_payments.claimable_payments.remove(&payment_hash);
7819							}
7820							continue 'next_forwardable_htlc;
7821						};
7822					}
7823					let phantom_shared_secret = claimable_htlc.prev_hop.phantom_shared_secret;
7824					let mut receiver_node_id = self.our_network_pubkey;
7825					if phantom_shared_secret.is_some() {
7826						receiver_node_id = self
7827							.node_signer
7828							.get_node_id(Recipient::PhantomNode)
7829							.expect("Failed to get node_id for phantom node recipient");
7830					}
7831
7832					macro_rules! check_total_value {
7833						($purpose: expr) => {{
7834							let mut payment_claimable_generated = false;
7835							let is_keysend = $purpose.is_keysend();
7836							if claimable_payments.pending_claiming_payments.contains_key(&payment_hash) {
7837								fail_htlc!(claimable_htlc, payment_hash);
7838							}
7839							let ref mut claimable_payment = claimable_payments.claimable_payments
7840								.entry(payment_hash)
7841								// Note that if we insert here we MUST NOT fail_htlc!()
7842								.or_insert_with(|| {
7843									just_added = true;
7844									ClaimablePayment {
7845										purpose: $purpose.clone(), htlcs: Vec::new(), onion_fields: None,
7846									}
7847								});
7848							if $purpose != claimable_payment.purpose {
7849								let log_keysend = |keysend| if keysend { "keysend" } else { "non-keysend" };
7850								log_trace!(self.logger, "Failing new {} HTLC with payment_hash {} as we already had an existing {} HTLC with the same payment hash", log_keysend(is_keysend), &payment_hash, log_keysend(!is_keysend));
7851								fail_htlc!(claimable_htlc, payment_hash);
7852							}
7853							if let Some(earlier_fields) = &mut claimable_payment.onion_fields {
7854								if earlier_fields.check_merge(&mut onion_fields).is_err() {
7855									fail_htlc!(claimable_htlc, payment_hash);
7856								}
7857							} else {
7858								claimable_payment.onion_fields = Some(onion_fields);
7859							}
7860							let mut total_value = claimable_htlc.sender_intended_value;
7861							let mut earliest_expiry = claimable_htlc.cltv_expiry;
7862							for htlc in claimable_payment.htlcs.iter() {
7863								total_value += htlc.sender_intended_value;
7864								earliest_expiry = cmp::min(earliest_expiry, htlc.cltv_expiry);
7865								if htlc.total_msat != claimable_htlc.total_msat {
7866									log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the HTLCs had inconsistent total values (eg {} and {})",
7867										&payment_hash, claimable_htlc.total_msat, htlc.total_msat);
7868									total_value = msgs::MAX_VALUE_MSAT;
7869								}
7870								if total_value >= msgs::MAX_VALUE_MSAT { break; }
7871							}
7872							// The condition determining whether an MPP is complete must
7873							// match exactly the condition used in `timer_tick_occurred`
7874							if total_value >= msgs::MAX_VALUE_MSAT {
7875								fail_htlc!(claimable_htlc, payment_hash);
7876							} else if total_value - claimable_htlc.sender_intended_value >= claimable_htlc.total_msat {
7877								log_trace!(self.logger, "Failing HTLC with payment_hash {} as payment is already claimable",
7878									&payment_hash);
7879								fail_htlc!(claimable_htlc, payment_hash);
7880							} else if total_value >= claimable_htlc.total_msat {
7881								#[allow(unused_assignments)] {
7882									committed_to_claimable = true;
7883								}
7884								claimable_payment.htlcs.push(claimable_htlc);
7885								let amount_msat =
7886									claimable_payment.htlcs.iter().map(|htlc| htlc.value).sum();
7887								claimable_payment.htlcs.iter_mut()
7888									.for_each(|htlc| htlc.total_value_received = Some(amount_msat));
7889								let counterparty_skimmed_fee_msat = claimable_payment.htlcs.iter()
7890									.map(|htlc| htlc.counterparty_skimmed_fee_msat.unwrap_or(0)).sum();
7891								debug_assert!(total_value.saturating_sub(amount_msat) <=
7892									counterparty_skimmed_fee_msat);
7893								claimable_payment.htlcs.sort();
7894								let payment_id =
7895									claimable_payment.inbound_payment_id(&self.inbound_payment_id_secret);
7896								new_events.push_back((events::Event::PaymentClaimable {
7897									receiver_node_id: Some(receiver_node_id),
7898									payment_hash,
7899									purpose: $purpose,
7900									amount_msat,
7901									counterparty_skimmed_fee_msat,
7902									receiving_channel_ids: claimable_payment.receiving_channel_ids(),
7903									claim_deadline: Some(earliest_expiry - HTLC_FAIL_BACK_BUFFER),
7904									onion_fields: claimable_payment.onion_fields.clone(),
7905									payment_id: Some(payment_id),
7906								}, None));
7907								payment_claimable_generated = true;
7908							} else {
7909								// Nothing to do - we haven't reached the total
7910								// payment value yet, wait until we receive more
7911								// MPP parts.
7912								claimable_payment.htlcs.push(claimable_htlc);
7913								#[allow(unused_assignments)] {
7914									committed_to_claimable = true;
7915								}
7916							}
7917							payment_claimable_generated
7918						}}
7919					}
7920
7921					// Check that the payment hash and secret are known. Note that we
7922					// MUST take care to handle the "unknown payment hash" and
7923					// "incorrect payment secret" cases here identically or we'd expose
7924					// that we are the ultimate recipient of the given payment hash.
7925					// Further, we must not expose whether we have any other HTLCs
7926					// associated with the same payment_hash pending or not.
7927					let payment_preimage = if has_recipient_created_payment_secret {
7928						if let Some(ref payment_data) = payment_data {
7929							let verify_res = inbound_payment::verify(
7930								payment_hash,
7931								&payment_data,
7932								self.highest_seen_timestamp.load(Ordering::Acquire) as u64,
7933								&self.inbound_payment_key,
7934								&self.logger,
7935							);
7936							let (payment_preimage, min_final_cltv_expiry_delta) = match verify_res {
7937								Ok(result) => result,
7938								Err(()) => {
7939									log_trace!(self.logger, "Failing new HTLC with payment_hash {} as payment verification failed", &payment_hash);
7940									fail_htlc!(claimable_htlc, payment_hash);
7941								},
7942							};
7943							if let Some(min_final_cltv_expiry_delta) = min_final_cltv_expiry_delta {
7944								let expected_min_expiry_height = (self.current_best_block().height
7945									+ min_final_cltv_expiry_delta as u32)
7946									as u64;
7947								if (cltv_expiry as u64) < expected_min_expiry_height {
7948									log_trace!(self.logger, "Failing new HTLC with payment_hash {} as its CLTV expiry was too soon (had {}, earliest expected {})",
7949									&payment_hash, cltv_expiry, expected_min_expiry_height);
7950									fail_htlc!(claimable_htlc, payment_hash);
7951								}
7952							}
7953							payment_preimage
7954						} else {
7955							fail_htlc!(claimable_htlc, payment_hash);
7956						}
7957					} else {
7958						None
7959					};
7960					match claimable_htlc.onion_payload {
7961						OnionPayload::Invoice { .. } => {
7962							let payment_data = payment_data.unwrap();
7963							let from_parts_res = events::PaymentPurpose::from_parts(
7964								payment_preimage,
7965								payment_data.payment_secret,
7966								payment_context,
7967							);
7968							let purpose = match from_parts_res {
7969								Ok(purpose) => purpose,
7970								Err(()) => {
7971									fail_htlc!(claimable_htlc, payment_hash);
7972								},
7973							};
7974							check_total_value!(purpose);
7975						},
7976						OnionPayload::Spontaneous(keysend_preimage) => {
7977							let purpose = if let Some(PaymentContext::AsyncBolt12Offer(
7978								AsyncBolt12OfferContext { offer_nonce },
7979							)) = payment_context
7980							{
7981								let payment_data = match payment_data {
7982									Some(data) => data,
7983									None => {
7984										debug_assert!(
7985											false,
7986											"We checked that payment_data is Some above"
7987										);
7988										fail_htlc!(claimable_htlc, payment_hash);
7989									},
7990								};
7991
7992								let verify_opt = invoice_request_opt.and_then(|invreq| {
7993									invreq
7994										.verify_using_recipient_data(
7995											offer_nonce,
7996											&self.inbound_payment_key,
7997											&self.secp_ctx,
7998										)
7999										.ok()
8000								});
8001								let verified_invreq = match verify_opt {
8002									Some(verified_invreq) => {
8003										if let Some(invreq_amt_msat) =
8004											verified_invreq.amount_msats()
8005										{
8006											if payment_data.total_msat < invreq_amt_msat {
8007												fail_htlc!(claimable_htlc, payment_hash);
8008											}
8009										}
8010										verified_invreq
8011									},
8012									None => {
8013										fail_htlc!(claimable_htlc, payment_hash);
8014									},
8015								};
8016								let payment_purpose_context =
8017									PaymentContext::Bolt12Offer(Bolt12OfferContext {
8018										offer_id: verified_invreq.offer_id,
8019										invoice_request: verified_invreq.fields(),
8020									});
8021								let from_parts_res = events::PaymentPurpose::from_parts(
8022									Some(keysend_preimage),
8023									payment_data.payment_secret,
8024									Some(payment_purpose_context),
8025								);
8026								match from_parts_res {
8027									Ok(purpose) => purpose,
8028									Err(()) => {
8029										fail_htlc!(claimable_htlc, payment_hash);
8030									},
8031								}
8032							} else if payment_context.is_some() {
8033								log_trace!(self.logger, "Failing new HTLC with payment_hash {}: received a keysend payment to a non-async payments context {:#?}", payment_hash, payment_context);
8034								fail_htlc!(claimable_htlc, payment_hash);
8035							} else {
8036								events::PaymentPurpose::SpontaneousPayment(keysend_preimage)
8037							};
8038							check_total_value!(purpose);
8039						},
8040					}
8041				},
8042				HTLCForwardInfo::FailHTLC { .. } | HTLCForwardInfo::FailMalformedHTLC { .. } => {
8043					panic!("Got pending fail of our own HTLC");
8044				},
8045			}
8046		}
8047	}
8048
8049	/// Free the background events, generally called from [`PersistenceNotifierGuard`] constructors.
8050	///
8051	/// Expects the caller to have a total_consistency_lock read lock.
8052	fn process_background_events(&self) -> NotifyOption {
8053		debug_assert_ne!(
8054			self.total_consistency_lock.held_by_thread(),
8055			LockHeldState::NotHeldByThread
8056		);
8057
8058		self.background_events_processed_since_startup.store(true, Ordering::Release);
8059
8060		let mut background_events = Vec::new();
8061		mem::swap(&mut *self.pending_background_events.lock().unwrap(), &mut background_events);
8062		if background_events.is_empty() {
8063			return NotifyOption::SkipPersistNoEvents;
8064		}
8065
8066		for event in background_events.drain(..) {
8067			match event {
8068				BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
8069					counterparty_node_id,
8070					funding_txo,
8071					channel_id,
8072					update,
8073				} => {
8074					self.apply_post_close_monitor_update(
8075						counterparty_node_id,
8076						channel_id,
8077						funding_txo,
8078						update,
8079					);
8080				},
8081				BackgroundEvent::MonitorUpdatesComplete {
8082					counterparty_node_id,
8083					channel_id,
8084					highest_update_id_completed,
8085				} => {
8086					// Now that we can finally handle the background event, remove all in-flight
8087					// monitor updates for this channel that we've known to complete, as they have
8088					// already been persisted to the monitor and can be applied to our internal
8089					// state such that the channel resumes operation if no new updates have been
8090					// made since.
8091					self.channel_monitor_updated(
8092						&channel_id,
8093						Some(highest_update_id_completed),
8094						&counterparty_node_id,
8095					);
8096				},
8097				BackgroundEvent::AttemptUnblockMonitorUpdates {
8098					counterparty_node_id,
8099					channel_id,
8100				} => {
8101					self.handle_monitor_update_release(counterparty_node_id, channel_id, None);
8102				},
8103			}
8104		}
8105		NotifyOption::DoPersist
8106	}
8107
8108	#[cfg(any(test, feature = "_test_utils"))]
8109	/// Process background events, for functional testing
8110	pub fn test_process_background_events(&self) {
8111		let _lck = self.total_consistency_lock.read().unwrap();
8112		let _ = self.process_background_events();
8113	}
8114
8115	#[rustfmt::skip]
8116	fn update_channel_fee(&self, chan_id: &ChannelId, chan: &mut FundedChannel<SP>, new_feerate: u32) -> NotifyOption {
8117		if !chan.funding.is_outbound() { return NotifyOption::SkipPersistNoEvents; }
8118
8119		let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8120
8121		let current_feerate = chan.context.get_feerate_sat_per_1000_weight();
8122		let update_fee_required = match new_feerate.cmp(&current_feerate) {
8123			cmp::Ordering::Greater => true,
8124			cmp::Ordering::Equal => false,
8125			// Only bother with a fee update if feerate has decreased at least half.
8126			cmp::Ordering::Less => new_feerate * 2 <= current_feerate,
8127		};
8128		if !update_fee_required {
8129			return NotifyOption::SkipPersistNoEvents
8130		}
8131
8132		if !chan.context.is_live() {
8133			log_trace!(logger, "Channel {} does not qualify for a feerate change from {} to {} as it cannot currently be updated (probably the peer is disconnected).",
8134				chan_id, chan.context.get_feerate_sat_per_1000_weight(), new_feerate);
8135			return NotifyOption::SkipPersistNoEvents;
8136		}
8137		log_trace!(logger, "Channel {} qualifies for a feerate change from {} to {}.",
8138			&chan_id, chan.context.get_feerate_sat_per_1000_weight(), new_feerate);
8139
8140		chan.queue_update_fee(new_feerate, &self.fee_estimator, &&logger);
8141		NotifyOption::DoPersist
8142	}
8143
8144	#[cfg(any(test, fuzzing, feature = "_externalize_tests"))]
8145	/// In chanmon_consistency we want to sometimes do the channel fee updates done in
8146	/// timer_tick_occurred, but we can't generate the disabled channel updates as it considers
8147	/// these a fuzz failure (as they usually indicate a channel force-close, which is exactly what
8148	/// it wants to detect). Thus, we have a variant exposed here for its benefit.
8149	#[rustfmt::skip]
8150	pub fn maybe_update_chan_fees(&self) {
8151		PersistenceNotifierGuard::optionally_notify(self, || {
8152			let mut should_persist = NotifyOption::SkipPersistNoEvents;
8153			let mut feerate_cache = new_hash_map();
8154
8155			let per_peer_state = self.per_peer_state.read().unwrap();
8156			for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
8157				let mut peer_state_lock = peer_state_mutex.lock().unwrap();
8158				let peer_state = &mut *peer_state_lock;
8159				for (chan_id, chan) in peer_state.channel_by_id.iter_mut()
8160					.filter_map(|(chan_id, chan)| chan.as_funded_mut().map(|chan| (chan_id, chan)))
8161				{
8162					let channel_type = chan.funding.get_channel_type();
8163					let new_feerate = feerate_cache.get(channel_type).copied().or_else(|| {
8164						let feerate = selected_commitment_sat_per_1000_weight(&self.fee_estimator, &channel_type);
8165						feerate_cache.insert(channel_type.clone(), feerate);
8166						Some(feerate)
8167					}).unwrap();
8168					let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
8169					if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
8170				}
8171			}
8172
8173			should_persist
8174		});
8175	}
8176
8177	/// Performs actions which should happen on startup and roughly once per minute thereafter.
8178	///
8179	/// This currently includes:
8180	///  * Increasing or decreasing the on-chain feerate estimates for our outbound channels,
8181	///  * Broadcasting [`ChannelUpdate`] messages if we've been disconnected from our peer for more
8182	///    than a minute, informing the network that they should no longer attempt to route over
8183	///    the channel.
8184	///  * Expiring a channel's previous [`ChannelConfig`] if necessary to only allow forwarding HTLCs
8185	///    with the current [`ChannelConfig`].
8186	///  * Removing peers which have disconnected but and no longer have any channels.
8187	///  * Force-closing and removing channels which have not completed establishment in a timely manner.
8188	///  * Forgetting about stale outbound payments, either those that have already been fulfilled
8189	///    or those awaiting an invoice that hasn't been delivered in the necessary amount of time.
8190	///    The latter is determined using the system clock in `std` and the highest seen block time
8191	///    minus two hours in non-`std`.
8192	///
8193	/// Note that this may cause reentrancy through [`chain::Watch::update_channel`] calls or feerate
8194	/// estimate fetches.
8195	///
8196	/// [`ChannelUpdate`]: msgs::ChannelUpdate
8197	/// [`ChannelConfig`]: crate::util::config::ChannelConfig
8198	pub fn timer_tick_occurred(&self) {
8199		PersistenceNotifierGuard::optionally_notify(self, || {
8200			let mut should_persist = NotifyOption::SkipPersistNoEvents;
8201
8202			let mut handle_errors: Vec<(Result<(), _>, _)> = Vec::new();
8203			let mut timed_out_mpp_htlcs = Vec::new();
8204			let mut pending_peers_awaiting_removal = Vec::new();
8205			let mut feerate_cache = new_hash_map();
8206
8207			{
8208				let per_peer_state = self.per_peer_state.read().unwrap();
8209				for (counterparty_node_id, peer_state_mutex) in per_peer_state.iter() {
8210					let mut peer_state_lock = peer_state_mutex.lock().unwrap();
8211					let peer_state = &mut *peer_state_lock;
8212					let pending_msg_events = &mut peer_state.pending_msg_events;
8213					let counterparty_node_id = *counterparty_node_id;
8214					peer_state.channel_by_id.retain(|chan_id, chan| {
8215						match chan.as_funded_mut() {
8216							Some(funded_chan) => {
8217								let channel_type = funded_chan.funding.get_channel_type();
8218								let new_feerate = feerate_cache.get(channel_type).copied().or_else(|| {
8219									let feerate = selected_commitment_sat_per_1000_weight(&self.fee_estimator, &channel_type);
8220									feerate_cache.insert(channel_type.clone(), feerate);
8221									Some(feerate)
8222								}).unwrap();
8223								let chan_needs_persist = self.update_channel_fee(chan_id, funded_chan, new_feerate);
8224								if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
8225
8226								if let Err(e) = funded_chan.timer_check_closing_negotiation_progress() {
8227									let (needs_close, err) = convert_channel_err!(self, peer_state, e, funded_chan, FUNDED_CHANNEL);
8228									handle_errors.push((Err(err), counterparty_node_id));
8229									if needs_close { return false; }
8230								}
8231
8232								match funded_chan.channel_update_status() {
8233									ChannelUpdateStatus::Enabled if !funded_chan.context.is_live() => funded_chan.set_channel_update_status(ChannelUpdateStatus::DisabledStaged(0)),
8234									ChannelUpdateStatus::Disabled if funded_chan.context.is_live() => funded_chan.set_channel_update_status(ChannelUpdateStatus::EnabledStaged(0)),
8235									ChannelUpdateStatus::DisabledStaged(_) if funded_chan.context.is_live()
8236										=> funded_chan.set_channel_update_status(ChannelUpdateStatus::Enabled),
8237									ChannelUpdateStatus::EnabledStaged(_) if !funded_chan.context.is_live()
8238										=> funded_chan.set_channel_update_status(ChannelUpdateStatus::Disabled),
8239									ChannelUpdateStatus::DisabledStaged(mut n) if !funded_chan.context.is_live() => {
8240										n += 1;
8241										if n >= DISABLE_GOSSIP_TICKS {
8242											funded_chan.set_channel_update_status(ChannelUpdateStatus::Disabled);
8243											if let Ok(update) = self.get_channel_update_for_broadcast(&funded_chan) {
8244												let mut pending_broadcast_messages = self.pending_broadcast_messages.lock().unwrap();
8245												pending_broadcast_messages.push(MessageSendEvent::BroadcastChannelUpdate {
8246													msg: update
8247												});
8248											}
8249											should_persist = NotifyOption::DoPersist;
8250										} else {
8251											funded_chan.set_channel_update_status(ChannelUpdateStatus::DisabledStaged(n));
8252										}
8253									},
8254									ChannelUpdateStatus::EnabledStaged(mut n) if funded_chan.context.is_live() => {
8255										n += 1;
8256										if n >= ENABLE_GOSSIP_TICKS {
8257											funded_chan.set_channel_update_status(ChannelUpdateStatus::Enabled);
8258											if let Ok(update) = self.get_channel_update_for_broadcast(&funded_chan) {
8259												let mut pending_broadcast_messages = self.pending_broadcast_messages.lock().unwrap();
8260												pending_broadcast_messages.push(MessageSendEvent::BroadcastChannelUpdate {
8261													msg: update
8262												});
8263											}
8264											should_persist = NotifyOption::DoPersist;
8265										} else {
8266											funded_chan.set_channel_update_status(ChannelUpdateStatus::EnabledStaged(n));
8267										}
8268									},
8269									_ => {},
8270								}
8271
8272								funded_chan.context.maybe_expire_prev_config();
8273
8274								if peer_state.is_connected {
8275									if funded_chan.should_disconnect_peer_awaiting_response() {
8276										let logger = WithChannelContext::from(&self.logger, &funded_chan.context, None);
8277										log_debug!(logger, "Disconnecting peer {} due to not making any progress on channel {}",
8278												counterparty_node_id, chan_id);
8279										pending_msg_events.push(MessageSendEvent::HandleError {
8280											node_id: counterparty_node_id,
8281											action: msgs::ErrorAction::DisconnectPeerWithWarning {
8282												msg: msgs::WarningMessage {
8283													channel_id: *chan_id,
8284													data: "Disconnecting due to timeout awaiting response".to_owned(),
8285												},
8286											},
8287										});
8288									}
8289								}
8290
8291								true
8292							},
8293							None => {
8294								chan.context_mut().maybe_expire_prev_config();
8295								let unfunded_context = chan.unfunded_context_mut().expect("channel should be unfunded");
8296								if unfunded_context.should_expire_unfunded_channel() {
8297									let context = chan.context();
8298									let logger = WithChannelContext::from(&self.logger, context, None);
8299									log_error!(logger,
8300										"Force-closing pending channel with ID {} for not establishing in a timely manner",
8301										context.channel_id());
8302									let reason = ClosureReason::FundingTimedOut;
8303									let msg = "Force-closing pending channel due to timeout awaiting establishment handshake".to_owned();
8304									let err = ChannelError::Close((msg, reason));
8305									let (_, e) = convert_channel_err!(self, peer_state, err, chan);
8306									handle_errors.push((Err(e), counterparty_node_id));
8307									false
8308								} else {
8309									true
8310								}
8311							},
8312						}
8313					});
8314
8315					for (chan_id, req) in peer_state.inbound_channel_request_by_id.iter_mut() {
8316						if {
8317							req.ticks_remaining -= 1;
8318							req.ticks_remaining
8319						} <= 0
8320						{
8321							let logger = WithContext::from(
8322								&self.logger,
8323								Some(counterparty_node_id),
8324								Some(*chan_id),
8325								None,
8326							);
8327							log_error!(logger, "Force-closing unaccepted inbound channel {} for not accepting in a timely manner", &chan_id);
8328							if peer_state.is_connected {
8329								peer_state.pending_msg_events.push(MessageSendEvent::HandleError {
8330									node_id: counterparty_node_id,
8331									action: msgs::ErrorAction::SendErrorMessage {
8332										msg: msgs::ErrorMessage {
8333											channel_id: chan_id.clone(),
8334											data: "Channel force-closed".to_owned(),
8335										},
8336									},
8337								});
8338							}
8339						}
8340					}
8341					peer_state
8342						.inbound_channel_request_by_id
8343						.retain(|_, req| req.ticks_remaining > 0);
8344
8345					if peer_state.ok_to_remove(true) {
8346						pending_peers_awaiting_removal.push(counterparty_node_id);
8347					}
8348				}
8349			}
8350
8351			// When a peer disconnects but still has channels, the peer's `peer_state` entry in the
8352			// `per_peer_state` is not removed by the `peer_disconnected` function. If the channels
8353			// of to that peer is later closed while still being disconnected (i.e. force closed),
8354			// we therefore need to remove the peer from `peer_state` separately.
8355			// To avoid having to take the `per_peer_state` `write` lock once the channels are
8356			// closed, we instead remove such peers awaiting removal here on a timer, to limit the
8357			// negative effects on parallelism as much as possible.
8358			if pending_peers_awaiting_removal.len() > 0 {
8359				let mut per_peer_state = self.per_peer_state.write().unwrap();
8360				for counterparty_node_id in pending_peers_awaiting_removal {
8361					match per_peer_state.entry(counterparty_node_id) {
8362						hash_map::Entry::Occupied(entry) => {
8363							// Remove the entry if the peer is still disconnected and we still
8364							// have no channels to the peer.
8365							let remove_entry = {
8366								let peer_state = entry.get().lock().unwrap();
8367								peer_state.ok_to_remove(true)
8368							};
8369							if remove_entry {
8370								entry.remove_entry();
8371							}
8372						},
8373						hash_map::Entry::Vacant(_) => { /* The PeerState has already been removed */
8374						},
8375					}
8376				}
8377			}
8378
8379			self.claimable_payments.lock().unwrap().claimable_payments.retain(
8380				|payment_hash, payment| {
8381					if payment.htlcs.is_empty() {
8382						// This should be unreachable
8383						debug_assert!(false);
8384						return false;
8385					}
8386					// Check if we've received all the parts we need for an MPP.
8387					// This condition determining whether the MPP is complete here must match
8388					// exactly the condition used in `process_pending_htlc_forwards`.
8389					let htlc_total_msat =
8390						payment.htlcs.iter().map(|h| h.sender_intended_value).sum();
8391					if payment.htlcs[0].total_msat <= htlc_total_msat {
8392						return true;
8393					} else if payment.htlcs.iter_mut().any(|htlc| {
8394						htlc.timer_ticks += 1;
8395						return htlc.timer_ticks >= MPP_TIMEOUT_TICKS;
8396					}) {
8397						let htlcs = payment
8398							.htlcs
8399							.drain(..)
8400							.map(|htlc: ClaimableHTLC| (htlc.prev_hop, *payment_hash));
8401						timed_out_mpp_htlcs.extend(htlcs);
8402						return false;
8403					}
8404					true
8405				},
8406			);
8407
8408			for htlc_source in timed_out_mpp_htlcs.drain(..) {
8409				let source = HTLCSource::PreviousHopData(htlc_source.0.clone());
8410				let failure_reason = LocalHTLCFailureReason::MPPTimeout;
8411				let reason = HTLCFailReason::from_failure_code(failure_reason);
8412				let receiver = HTLCHandlingFailureType::Receive { payment_hash: htlc_source.1 };
8413				self.fail_htlc_backwards_internal(&source, &htlc_source.1, &reason, receiver, None);
8414			}
8415
8416			for (err, counterparty_node_id) in handle_errors {
8417				let _ = handle_error!(self, err, counterparty_node_id);
8418			}
8419
8420			#[cfg(feature = "std")]
8421			let duration_since_epoch = std::time::SystemTime::now()
8422				.duration_since(std::time::SystemTime::UNIX_EPOCH)
8423				.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
8424			#[cfg(not(feature = "std"))]
8425			let duration_since_epoch = Duration::from_secs(
8426				self.highest_seen_timestamp.load(Ordering::Acquire).saturating_sub(7200) as u64,
8427			);
8428
8429			self.pending_outbound_payments
8430				.remove_stale_payments(duration_since_epoch, &self.pending_events);
8431
8432			self.check_refresh_async_receive_offer_cache(true);
8433
8434			// Technically we don't need to do this here, but if we have holding cell entries in a
8435			// channel that need freeing, it's better to do that here and block a background task
8436			// than block the message queueing pipeline.
8437			if self.check_free_holding_cells() {
8438				should_persist = NotifyOption::DoPersist;
8439			}
8440
8441			should_persist
8442		});
8443	}
8444
8445	/// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
8446	/// after a PaymentClaimable event, failing the HTLC back to its origin and freeing resources
8447	/// along the path (including in our own channel on which we received it).
8448	///
8449	/// Note that in some cases around unclean shutdown, it is possible the payment may have
8450	/// already been claimed by you via [`ChannelManager::claim_funds`] prior to you seeing (a
8451	/// second copy of) the [`events::Event::PaymentClaimable`] event. Alternatively, the payment
8452	/// may have already been failed automatically by LDK if it was nearing its expiration time.
8453	///
8454	/// While LDK will never claim a payment automatically on your behalf (i.e. without you calling
8455	/// [`ChannelManager::claim_funds`]), you should still monitor for
8456	/// [`events::Event::PaymentClaimed`] events even for payments you intend to fail, especially on
8457	/// startup during which time claims that were in-progress at shutdown may be replayed.
8458	pub fn fail_htlc_backwards(&self, payment_hash: &PaymentHash) {
8459		let failure_code = FailureCode::IncorrectOrUnknownPaymentDetails;
8460		self.fail_htlc_backwards_with_reason(payment_hash, failure_code);
8461	}
8462
8463	/// This is a variant of [`ChannelManager::fail_htlc_backwards`] that allows you to specify the
8464	/// reason for the failure.
8465	///
8466	/// See [`FailureCode`] for valid failure codes.
8467	pub fn fail_htlc_backwards_with_reason(
8468		&self, payment_hash: &PaymentHash, failure_code: FailureCode,
8469	) {
8470		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8471
8472		let removed_source =
8473			self.claimable_payments.lock().unwrap().claimable_payments.remove(payment_hash);
8474		if let Some(payment) = removed_source {
8475			for htlc in payment.htlcs {
8476				let reason = self.get_htlc_fail_reason_from_failure_code(failure_code, &htlc);
8477				let source = HTLCSource::PreviousHopData(htlc.prev_hop);
8478				let receiver = HTLCHandlingFailureType::Receive { payment_hash: *payment_hash };
8479				self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver, None);
8480			}
8481		}
8482	}
8483
8484	/// Gets error data to form an [`HTLCFailReason`] given a [`FailureCode`] and [`ClaimableHTLC`].
8485	fn get_htlc_fail_reason_from_failure_code(
8486		&self, failure_code: FailureCode, htlc: &ClaimableHTLC,
8487	) -> HTLCFailReason {
8488		match failure_code {
8489			FailureCode::TemporaryNodeFailure => {
8490				HTLCFailReason::from_failure_code(failure_code.into())
8491			},
8492			FailureCode::RequiredNodeFeatureMissing => {
8493				HTLCFailReason::from_failure_code(failure_code.into())
8494			},
8495			FailureCode::IncorrectOrUnknownPaymentDetails => {
8496				let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();
8497				htlc_msat_height_data
8498					.extend_from_slice(&self.best_block.read().unwrap().height.to_be_bytes());
8499				HTLCFailReason::reason(failure_code.into(), htlc_msat_height_data)
8500			},
8501			FailureCode::InvalidOnionPayload(data) => {
8502				let fail_data = match data {
8503					Some((typ, offset)) => [BigSize(typ).encode(), offset.encode()].concat(),
8504					None => Vec::new(),
8505				};
8506				HTLCFailReason::reason(failure_code.into(), fail_data)
8507			},
8508		}
8509	}
8510
8511	/// Gets an HTLC onion failure code and error data for an `UPDATE` error, given the error code
8512	/// that we want to return and a channel.
8513	///
8514	/// This is for failures on the channel on which the HTLC was *received*, not failures
8515	/// forwarding
8516	fn get_htlc_inbound_temp_fail_data(&self, reason: LocalHTLCFailureReason) -> Vec<u8> {
8517		debug_assert!(reason.is_temporary());
8518		debug_assert!(reason != LocalHTLCFailureReason::AmountBelowMinimum);
8519		debug_assert!(reason != LocalHTLCFailureReason::FeeInsufficient);
8520		debug_assert!(reason != LocalHTLCFailureReason::IncorrectCLTVExpiry);
8521		// at capacity, we write fields `disabled_flags` and `len`
8522		let mut enc = VecWriter(Vec::with_capacity(4));
8523		if reason == LocalHTLCFailureReason::ChannelDisabled {
8524			// No flags for `disabled_flags` are currently defined so they're always two zero bytes.
8525			// See https://github.com/lightning/bolts/blob/341ec84/04-onion-routing.md?plain=1#L1008
8526			0u16.write(&mut enc).expect("Writes cannot fail");
8527		}
8528		// See https://github.com/lightning/bolts/blob/247e83d/04-onion-routing.md?plain=1#L1414-L1415
8529		(0u16).write(&mut enc).expect("Writes cannot fail");
8530		enc.0
8531	}
8532
8533	// Fail a list of HTLCs that were just freed from the holding cell. The HTLCs need to be
8534	// failed backwards or, if they were one of our outgoing HTLCs, then their failure needs to
8535	// be surfaced to the user.
8536	fn fail_holding_cell_htlcs(
8537		&self, mut htlcs_to_fail: Vec<(HTLCSource, PaymentHash)>, channel_id: ChannelId,
8538		counterparty_node_id: &PublicKey,
8539	) {
8540		let (failure_reason, onion_failure_data) = {
8541			let per_peer_state = self.per_peer_state.read().unwrap();
8542			if let Some(peer_state_mutex) = per_peer_state.get(counterparty_node_id) {
8543				let mut peer_state_lock = peer_state_mutex.lock().unwrap();
8544				let peer_state = &mut *peer_state_lock;
8545				match peer_state.channel_by_id.entry(channel_id) {
8546					hash_map::Entry::Occupied(chan_entry) => {
8547						if let Some(_chan) = chan_entry.get().as_funded() {
8548							let reason = LocalHTLCFailureReason::TemporaryChannelFailure;
8549							let data = self.get_htlc_inbound_temp_fail_data(reason);
8550							(reason, data)
8551						} else {
8552							// We shouldn't be trying to fail holding cell HTLCs on an unfunded channel.
8553							debug_assert!(false);
8554							(LocalHTLCFailureReason::UnknownNextPeer, Vec::new())
8555						}
8556					},
8557					hash_map::Entry::Vacant(_) => {
8558						(LocalHTLCFailureReason::UnknownNextPeer, Vec::new())
8559					},
8560				}
8561			} else {
8562				(LocalHTLCFailureReason::UnknownNextPeer, Vec::new())
8563			}
8564		};
8565
8566		for (htlc_src, payment_hash) in htlcs_to_fail.drain(..) {
8567			let reason = HTLCFailReason::reason(failure_reason, onion_failure_data.clone());
8568			let receiver = HTLCHandlingFailureType::Forward {
8569				node_id: Some(counterparty_node_id.clone()),
8570				channel_id,
8571			};
8572			self.fail_htlc_backwards_internal(&htlc_src, &payment_hash, &reason, receiver, None);
8573		}
8574	}
8575
8576	/// Fails an HTLC backwards to the sender of it to us.
8577	/// Note that we do not assume that channels corresponding to failed HTLCs are still available.
8578	fn fail_htlc_backwards_internal(
8579		&self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason,
8580		failure_type: HTLCHandlingFailureType,
8581		mut from_monitor_update_completion: Option<PaymentCompleteUpdate>,
8582	) {
8583		// Ensure that no peer state channel storage lock is held when calling this function.
8584		// This ensures that future code doesn't introduce a lock-order requirement for
8585		// `forward_htlcs` to be locked after the `per_peer_state` peer locks, which calling
8586		// this function with any `per_peer_state` peer lock acquired would.
8587		#[cfg(debug_assertions)]
8588		for (_, peer) in self.per_peer_state.read().unwrap().iter() {
8589			debug_assert_ne!(peer.held_by_thread(), LockHeldState::HeldByThread);
8590		}
8591
8592		//TODO: There is a timing attack here where if a node fails an HTLC back to us they can
8593		//identify whether we sent it or not based on the (I presume) very different runtime
8594		//between the branches here. We should make this async and move it into the forward HTLCs
8595		//timer handling.
8596
8597		// Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
8598		// from block_connected which may run during initialization prior to the chain_monitor
8599		// being fully configured. See the docs for `ChannelManagerReadArgs` for more.
8600		match source {
8601			HTLCSource::OutboundRoute { ref path, ref session_priv, ref payment_id, .. } => {
8602				let logger = WithContext::from(&self.logger, None, None, Some(*payment_hash));
8603				self.pending_outbound_payments.fail_htlc(
8604					source,
8605					payment_hash,
8606					onion_error,
8607					path,
8608					session_priv,
8609					payment_id,
8610					self.probing_cookie_secret,
8611					&self.secp_ctx,
8612					&self.pending_events,
8613					&mut from_monitor_update_completion,
8614					&logger,
8615				);
8616				if let Some(update) = from_monitor_update_completion {
8617					// If `fail_htlc` didn't `take` the post-event action, we should go ahead and
8618					// complete it here as the failure was duplicative - we've already handled it.
8619					// This can happen in rare cases where a MonitorUpdate is replayed after
8620					// restart because a ChannelMonitor wasn't persisted after it was applied (even
8621					// though the ChannelManager was).
8622					// For such cases, we also check that there's no existing pending event to
8623					// complete this action already, which we let finish instead.
8624					let action =
8625						EventCompletionAction::ReleasePaymentCompleteChannelMonitorUpdate(update);
8626					let have_action = {
8627						let pending_events = self.pending_events.lock().unwrap();
8628						pending_events.iter().any(|(_, act)| act.as_ref() == Some(&action))
8629					};
8630					if !have_action {
8631						self.handle_post_event_actions([action]);
8632					}
8633				}
8634			},
8635			HTLCSource::PreviousHopData(HTLCPreviousHopData {
8636				ref prev_outbound_scid_alias,
8637				ref htlc_id,
8638				ref incoming_packet_shared_secret,
8639				ref phantom_shared_secret,
8640				outpoint: _,
8641				ref blinded_failure,
8642				ref channel_id,
8643				..
8644			}) => {
8645				log_trace!(
8646					WithContext::from(&self.logger, None, Some(*channel_id), Some(*payment_hash)),
8647					"Failing {}HTLC with payment_hash {} backwards from us: {:?}",
8648					if blinded_failure.is_some() { "blinded " } else { "" },
8649					&payment_hash,
8650					onion_error
8651				);
8652				let failure = match blinded_failure {
8653					Some(BlindedFailure::FromIntroductionNode) => {
8654						let blinded_onion_error = HTLCFailReason::reason(
8655							LocalHTLCFailureReason::InvalidOnionBlinding,
8656							vec![0; 32],
8657						);
8658						let err_packet = blinded_onion_error.get_encrypted_failure_packet(
8659							incoming_packet_shared_secret,
8660							phantom_shared_secret,
8661						);
8662						HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet }
8663					},
8664					Some(BlindedFailure::FromBlindedNode) => HTLCForwardInfo::FailMalformedHTLC {
8665						htlc_id: *htlc_id,
8666						failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
8667						sha256_of_onion: [0; 32],
8668					},
8669					None => {
8670						let err_packet = onion_error.get_encrypted_failure_packet(
8671							incoming_packet_shared_secret,
8672							phantom_shared_secret,
8673						);
8674						HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet }
8675					},
8676				};
8677
8678				let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
8679				match forward_htlcs.entry(*prev_outbound_scid_alias) {
8680					hash_map::Entry::Occupied(mut entry) => {
8681						entry.get_mut().push(failure);
8682					},
8683					hash_map::Entry::Vacant(entry) => {
8684						entry.insert(vec![failure]);
8685					},
8686				}
8687				mem::drop(forward_htlcs);
8688				let mut pending_events = self.pending_events.lock().unwrap();
8689				pending_events.push_back((
8690					events::Event::HTLCHandlingFailed {
8691						prev_channel_id: *channel_id,
8692						failure_type,
8693						failure_reason: Some(onion_error.into()),
8694					},
8695					None,
8696				));
8697			},
8698		}
8699	}
8700
8701	/// Provides a payment preimage in response to [`Event::PaymentClaimable`], generating any
8702	/// [`MessageSendEvent`]s needed to claim the payment.
8703	///
8704	/// This method is guaranteed to ensure the payment has been claimed but only if the current
8705	/// height is strictly below [`Event::PaymentClaimable::claim_deadline`]. To avoid race
8706	/// conditions, you should wait for an [`Event::PaymentClaimed`] before considering the payment
8707	/// successful. It will generally be available in the next [`process_pending_events`] call.
8708	///
8709	/// Note that if you did not set an `amount_msat` when calling [`create_inbound_payment`] or
8710	/// [`create_inbound_payment_for_hash`] you must check that the amount in the `PaymentClaimable`
8711	/// event matches your expectation. If you fail to do so and call this method, you may provide
8712	/// the sender "proof-of-payment" when they did not fulfill the full expected payment.
8713	///
8714	/// This function will fail the payment if it has custom TLVs with even type numbers, as we
8715	/// will assume they are unknown. If you intend to accept even custom TLVs, you should use
8716	/// [`claim_funds_with_known_custom_tlvs`].
8717	///
8718	/// [`Event::PaymentClaimable`]: crate::events::Event::PaymentClaimable
8719	/// [`Event::PaymentClaimable::claim_deadline`]: crate::events::Event::PaymentClaimable::claim_deadline
8720	/// [`Event::PaymentClaimed`]: crate::events::Event::PaymentClaimed
8721	/// [`process_pending_events`]: EventsProvider::process_pending_events
8722	/// [`create_inbound_payment`]: Self::create_inbound_payment
8723	/// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
8724	/// [`claim_funds_with_known_custom_tlvs`]: Self::claim_funds_with_known_custom_tlvs
8725	pub fn claim_funds(&self, payment_preimage: PaymentPreimage) {
8726		self.claim_payment_internal(payment_preimage, false);
8727	}
8728
8729	/// This is a variant of [`claim_funds`] that allows accepting a payment with custom TLVs with
8730	/// even type numbers.
8731	///
8732	/// # Note
8733	///
8734	/// You MUST check you've understood all even TLVs before using this to
8735	/// claim, otherwise you may unintentionally agree to some protocol you do not understand.
8736	///
8737	/// [`claim_funds`]: Self::claim_funds
8738	pub fn claim_funds_with_known_custom_tlvs(&self, payment_preimage: PaymentPreimage) {
8739		self.claim_payment_internal(payment_preimage, true);
8740	}
8741
8742	fn claim_payment_internal(&self, payment_preimage: PaymentPreimage, custom_tlvs_known: bool) {
8743		let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).to_byte_array());
8744
8745		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8746
8747		let (sources, claiming_payment) = {
8748			let res = self.claimable_payments.lock().unwrap().begin_claiming_payment(
8749				payment_hash,
8750				&self.node_signer,
8751				&self.logger,
8752				&self.inbound_payment_id_secret,
8753				custom_tlvs_known,
8754			);
8755
8756			match res {
8757				Ok((htlcs, payment_info)) => (htlcs, payment_info),
8758				Err(htlcs) => {
8759					for htlc in htlcs {
8760						let reason = self.get_htlc_fail_reason_from_failure_code(
8761							FailureCode::InvalidOnionPayload(None),
8762							&htlc,
8763						);
8764						let source = HTLCSource::PreviousHopData(htlc.prev_hop);
8765						let receiver = HTLCHandlingFailureType::Receive { payment_hash };
8766						self.fail_htlc_backwards_internal(
8767							&source,
8768							&payment_hash,
8769							&reason,
8770							receiver,
8771							None,
8772						);
8773					}
8774					return;
8775				},
8776			}
8777		};
8778		debug_assert!(!sources.is_empty());
8779
8780		// Just in case one HTLC has been failed between when we generated the `PaymentClaimable`
8781		// and when we got here we need to check that the amount we're about to claim matches the
8782		// amount we told the user in the last `PaymentClaimable`. We also do a sanity-check that
8783		// the MPP parts all have the same `total_msat`.
8784		let mut claimable_amt_msat = 0;
8785		let mut prev_total_msat = None;
8786		let mut expected_amt_msat = None;
8787		let mut valid_mpp = true;
8788		let mut errs = Vec::new();
8789		let per_peer_state = self.per_peer_state.read().unwrap();
8790		for htlc in sources.iter() {
8791			if prev_total_msat.is_some() && prev_total_msat != Some(htlc.total_msat) {
8792				log_error!(self.logger, "Somehow ended up with an MPP payment with different expected total amounts - this should not be reachable!");
8793				debug_assert!(false);
8794				valid_mpp = false;
8795				break;
8796			}
8797			prev_total_msat = Some(htlc.total_msat);
8798
8799			if expected_amt_msat.is_some() && expected_amt_msat != htlc.total_value_received {
8800				log_error!(self.logger, "Somehow ended up with an MPP payment with different received total amounts - this should not be reachable!");
8801				debug_assert!(false);
8802				valid_mpp = false;
8803				break;
8804			}
8805			expected_amt_msat = htlc.total_value_received;
8806			claimable_amt_msat += htlc.value;
8807		}
8808		mem::drop(per_peer_state);
8809		if sources.is_empty() || expected_amt_msat.is_none() {
8810			self.claimable_payments.lock().unwrap().pending_claiming_payments.remove(&payment_hash);
8811			log_info!(
8812				self.logger,
8813				"Attempted to claim an incomplete payment which no longer had any available HTLCs!"
8814			);
8815			return;
8816		}
8817		if claimable_amt_msat != expected_amt_msat.unwrap() {
8818			self.claimable_payments.lock().unwrap().pending_claiming_payments.remove(&payment_hash);
8819			log_info!(self.logger, "Attempted to claim an incomplete payment, expected {} msat, had {} available to claim.",
8820				expected_amt_msat.unwrap(), claimable_amt_msat);
8821			return;
8822		}
8823		if valid_mpp {
8824			let mpp_parts: Vec<_> = sources
8825				.iter()
8826				.filter_map(|htlc| {
8827					if let Some(cp_id) = htlc.prev_hop.counterparty_node_id {
8828						Some(MPPClaimHTLCSource {
8829							counterparty_node_id: cp_id,
8830							funding_txo: htlc.prev_hop.outpoint,
8831							channel_id: htlc.prev_hop.channel_id,
8832							htlc_id: htlc.prev_hop.htlc_id,
8833						})
8834					} else {
8835						None
8836					}
8837				})
8838				.collect();
8839			let pending_mpp_claim_ptr_opt = if sources.len() > 1 {
8840				let mut channels_without_preimage = Vec::with_capacity(mpp_parts.len());
8841				for part in mpp_parts.iter() {
8842					let chan = (part.counterparty_node_id, part.channel_id);
8843					if !channels_without_preimage.contains(&chan) {
8844						channels_without_preimage.push(chan);
8845					}
8846				}
8847				Some(Arc::new(Mutex::new(PendingMPPClaim {
8848					channels_without_preimage,
8849					channels_with_preimage: Vec::new(),
8850				})))
8851			} else {
8852				None
8853			};
8854			let payment_info = Some(PaymentClaimDetails { mpp_parts, claiming_payment });
8855			for htlc in sources {
8856				let this_mpp_claim =
8857					pending_mpp_claim_ptr_opt.as_ref().map(|pending_mpp_claim| {
8858						let counterparty_id = htlc.prev_hop.counterparty_node_id;
8859						let counterparty_id = counterparty_id
8860							.expect("Prior to upgrading to LDK 0.1, all pending HTLCs forwarded by LDK 0.0.123 or before must be resolved. It appears at least one claimable payment was not resolved. Please downgrade to LDK 0.0.125 and resolve the HTLC by claiming the payment prior to upgrading.");
8861						let claim_ptr = PendingMPPClaimPointer(Arc::clone(pending_mpp_claim));
8862						(counterparty_id, htlc.prev_hop.channel_id, claim_ptr)
8863					});
8864				let raa_blocker = pending_mpp_claim_ptr_opt.as_ref().map(|pending_claim| {
8865					RAAMonitorUpdateBlockingAction::ClaimedMPPPayment {
8866						pending_claim: PendingMPPClaimPointer(Arc::clone(pending_claim)),
8867					}
8868				});
8869
8870				// Create new attribution data as the final hop. Always report a zero hold time, because reporting a
8871				// non-zero value will not make a difference in the penalty that may be applied by the sender. If there
8872				// is a phantom hop, we need to double-process.
8873				let attribution_data =
8874					if let Some(phantom_secret) = htlc.prev_hop.phantom_shared_secret {
8875						let attribution_data =
8876							process_fulfill_attribution_data(None, &phantom_secret, 0);
8877						Some(attribution_data)
8878					} else {
8879						None
8880					};
8881
8882				let attribution_data = process_fulfill_attribution_data(
8883					attribution_data,
8884					&htlc.prev_hop.incoming_packet_shared_secret,
8885					0,
8886				);
8887
8888				self.claim_funds_from_hop(
8889					htlc.prev_hop,
8890					payment_preimage,
8891					payment_info.clone(),
8892					Some(attribution_data),
8893					|_, definitely_duplicate| {
8894						debug_assert!(
8895							!definitely_duplicate,
8896							"We shouldn't claim duplicatively from a payment"
8897						);
8898						(
8899							Some(MonitorUpdateCompletionAction::PaymentClaimed {
8900								payment_hash,
8901								pending_mpp_claim: this_mpp_claim,
8902							}),
8903							raa_blocker,
8904						)
8905					},
8906				);
8907			}
8908		} else {
8909			for htlc in sources {
8910				let err_data =
8911					invalid_payment_err_data(htlc.value, self.best_block.read().unwrap().height);
8912				let source = HTLCSource::PreviousHopData(htlc.prev_hop);
8913				let reason = HTLCFailReason::reason(
8914					LocalHTLCFailureReason::IncorrectPaymentDetails,
8915					err_data,
8916				);
8917				let receiver = HTLCHandlingFailureType::Receive { payment_hash };
8918				self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver, None);
8919			}
8920			self.claimable_payments.lock().unwrap().pending_claiming_payments.remove(&payment_hash);
8921		}
8922
8923		// Now we can handle any errors which were generated.
8924		for (counterparty_node_id, err) in errs.drain(..) {
8925			let res: Result<(), _> = Err(err);
8926			let _ = handle_error!(self, res, counterparty_node_id);
8927		}
8928	}
8929
8930	fn claim_funds_from_hop<
8931		ComplFunc: FnOnce(
8932			Option<u64>,
8933			bool,
8934		) -> (Option<MonitorUpdateCompletionAction>, Option<RAAMonitorUpdateBlockingAction>),
8935	>(
8936		&self, prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage,
8937		payment_info: Option<PaymentClaimDetails>, attribution_data: Option<AttributionData>,
8938		completion_action: ComplFunc,
8939	) {
8940		let counterparty_node_id = prev_hop.counterparty_node_id.or_else(|| {
8941			let short_to_chan_info = self.short_to_chan_info.read().unwrap();
8942			short_to_chan_info.get(&prev_hop.prev_outbound_scid_alias).map(|(cp_id, _)| *cp_id)
8943		});
8944		let counterparty_node_id = if let Some(node_id) = counterparty_node_id {
8945			node_id
8946		} else {
8947			let payment_hash: PaymentHash = payment_preimage.into();
8948			panic!(
8949				"Prior to upgrading to LDK 0.1, all pending HTLCs forwarded by LDK 0.0.123 or before must be resolved. It appears at least the HTLC with payment_hash {payment_hash} (preimage {payment_preimage}) was not resolved. Please downgrade to LDK 0.0.125 and resolve the HTLC prior to upgrading.",
8950			);
8951		};
8952
8953		let htlc_source = HTLCClaimSource {
8954			counterparty_node_id,
8955			funding_txo: prev_hop.outpoint,
8956			channel_id: prev_hop.channel_id,
8957			htlc_id: prev_hop.htlc_id,
8958		};
8959		self.claim_mpp_part(
8960			htlc_source,
8961			payment_preimage,
8962			payment_info,
8963			attribution_data,
8964			completion_action,
8965		)
8966	}
8967
8968	fn claim_mpp_part<
8969		ComplFunc: FnOnce(
8970			Option<u64>,
8971			bool,
8972		) -> (Option<MonitorUpdateCompletionAction>, Option<RAAMonitorUpdateBlockingAction>),
8973	>(
8974		&self, prev_hop: HTLCClaimSource, payment_preimage: PaymentPreimage,
8975		payment_info: Option<PaymentClaimDetails>, attribution_data: Option<AttributionData>,
8976		completion_action: ComplFunc,
8977	) {
8978		//TODO: Delay the claimed_funds relaying just like we do outbound relay!
8979
8980		// If we haven't yet run background events assume we're still deserializing and shouldn't
8981		// actually pass `ChannelMonitorUpdate`s to users yet. Instead, queue them up as
8982		// `BackgroundEvent`s.
8983		let during_init = !self.background_events_processed_since_startup.load(Ordering::Acquire);
8984
8985		// As we may call handle_monitor_update_completion_actions in rather rare cases, check that
8986		// the required mutexes are not held before we start.
8987		debug_assert_ne!(self.pending_events.held_by_thread(), LockHeldState::HeldByThread);
8988		debug_assert_ne!(self.claimable_payments.held_by_thread(), LockHeldState::HeldByThread);
8989
8990		let per_peer_state = self.per_peer_state.read().unwrap();
8991		let chan_id = prev_hop.channel_id;
8992
8993		const MISSING_MON_ERROR: &'static str =
8994			"If we're going to claim an HTLC against a channel, we should always have *some* state for the channel, even if just the latest ChannelMonitor update_id. This failure indicates we need to claim an HTLC from a channel for which we did not have a ChannelMonitor at startup and didn't create one while running.";
8995
8996		let mut peer_state_lock = per_peer_state
8997			.get(&prev_hop.counterparty_node_id)
8998			.map(|peer_mutex| peer_mutex.lock().unwrap())
8999			.expect(MISSING_MON_ERROR);
9000
9001		{
9002			let peer_state = &mut *peer_state_lock;
9003			if let hash_map::Entry::Occupied(mut chan_entry) =
9004				peer_state.channel_by_id.entry(chan_id)
9005			{
9006				if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
9007					let logger = WithChannelContext::from(&self.logger, &chan.context, None);
9008					let fulfill_res = chan.get_update_fulfill_htlc_and_commit(
9009						prev_hop.htlc_id,
9010						payment_preimage,
9011						payment_info,
9012						attribution_data,
9013						&&logger,
9014					);
9015
9016					match fulfill_res {
9017						UpdateFulfillCommitFetch::NewClaim { htlc_value_msat, monitor_update } => {
9018							let (action_opt, raa_blocker_opt) =
9019								completion_action(Some(htlc_value_msat), false);
9020							if let Some(action) = action_opt {
9021								log_trace!(logger, "Tracking monitor update completion action for channel {}: {:?}",
9022									chan_id, action);
9023								peer_state
9024									.monitor_update_blocked_actions
9025									.entry(chan_id)
9026									.or_insert(Vec::new())
9027									.push(action);
9028							}
9029							if let Some(raa_blocker) = raa_blocker_opt {
9030								peer_state
9031									.actions_blocking_raa_monitor_updates
9032									.entry(chan_id)
9033									.or_insert_with(Vec::new)
9034									.push(raa_blocker);
9035							}
9036							handle_new_monitor_update!(
9037								self,
9038								prev_hop.funding_txo,
9039								monitor_update,
9040								peer_state_lock,
9041								peer_state,
9042								per_peer_state,
9043								chan
9044							);
9045						},
9046						UpdateFulfillCommitFetch::DuplicateClaim {} => {
9047							let (action_opt, raa_blocker_opt) = completion_action(None, true);
9048							if let Some(raa_blocker) = raa_blocker_opt {
9049								// If we're making a claim during startup, its a replay of a
9050								// payment claim from a `ChannelMonitor`. In some cases (MPP or
9051								// if the HTLC was only recently removed) we make such claims
9052								// after an HTLC has been removed from a channel entirely, and
9053								// thus the RAA blocker may have long since completed.
9054								//
9055								// However, its possible that the `ChannelMonitorUpdate` containing
9056								// the preimage never completed and is still pending. In that case,
9057								// we need to re-add the RAA blocker, which we do here. Handling
9058								// the post-update action, below, will remove it again.
9059								//
9060								// In any other case (i.e. not during startup), the RAA blocker
9061								// must still be present and blocking RAAs.
9062								let actions = &mut peer_state.actions_blocking_raa_monitor_updates;
9063								let actions_list = actions.entry(chan_id).or_insert_with(Vec::new);
9064								if !actions_list.contains(&raa_blocker) {
9065									debug_assert!(during_init);
9066									actions_list.push(raa_blocker);
9067								}
9068							}
9069							let action = if let Some(action) = action_opt {
9070								action
9071							} else {
9072								return;
9073							};
9074
9075							// If there are monitor updates in flight, we may be in the case
9076							// described above, replaying a claim on startup which needs an RAA
9077							// blocker to remain blocked. Thus, in such a case we simply push the
9078							// post-update action to the blocked list and move on.
9079							// In any case, we should err on the side of caution and not process
9080							// the post-update action no matter the situation.
9081							let in_flight_mons = peer_state.in_flight_monitor_updates.get(&chan_id);
9082							if in_flight_mons.map(|(_, mons)| !mons.is_empty()).unwrap_or(false) {
9083								peer_state
9084									.monitor_update_blocked_actions
9085									.entry(chan_id)
9086									.or_insert_with(Vec::new)
9087									.push(action);
9088								return;
9089							}
9090
9091							mem::drop(peer_state_lock);
9092
9093							log_trace!(logger, "Completing monitor update completion action for channel {} as claim was redundant: {:?}",
9094								chan_id, action);
9095							if let MonitorUpdateCompletionAction::FreeOtherChannelImmediately {
9096								downstream_counterparty_node_id: node_id,
9097								blocking_action: blocker,
9098								downstream_channel_id: channel_id,
9099							} = action
9100							{
9101								if let Some(peer_state_mtx) = per_peer_state.get(&node_id) {
9102									let mut peer_state = peer_state_mtx.lock().unwrap();
9103									let entry = peer_state
9104										.actions_blocking_raa_monitor_updates
9105										.entry(channel_id);
9106									if let btree_map::Entry::Occupied(mut entry) = entry {
9107										let mut found_blocker = false;
9108										entry.get_mut().retain(|iter| {
9109											// Note that we could actually be blocked, in
9110											// which case we need to only remove the one
9111											// blocker which was added duplicatively.
9112											let first_blocker = !found_blocker;
9113											if *iter == blocker {
9114												found_blocker = true;
9115											}
9116											*iter != blocker || !first_blocker
9117										});
9118										if entry.get().is_empty() {
9119											entry.remove();
9120										}
9121										debug_assert!(found_blocker);
9122									}
9123								} else {
9124									debug_assert!(false);
9125								}
9126							} else if matches!(
9127								action,
9128								MonitorUpdateCompletionAction::PaymentClaimed { .. }
9129							) {
9130								debug_assert!(during_init,
9131									"Duplicate claims should always either be for forwarded payments(freeing another channel immediately) or during init (for claim replay)");
9132								mem::drop(per_peer_state);
9133								self.handle_monitor_update_completion_actions([action]);
9134							} else {
9135								debug_assert!(false,
9136									"Duplicate claims should always either be for forwarded payments(freeing another channel immediately) or during init (for claim replay)");
9137								return;
9138							};
9139						},
9140					}
9141				}
9142				return;
9143			}
9144		}
9145
9146		let peer_state = &mut *peer_state_lock;
9147
9148		let update_id = if let Some(latest_update_id) =
9149			peer_state.closed_channel_monitor_update_ids.get_mut(&chan_id)
9150		{
9151			*latest_update_id = latest_update_id.saturating_add(1);
9152			*latest_update_id
9153		} else {
9154			let err = "We need the latest ChannelMonitorUpdate ID to build a new update.
9155This should have been checked for availability on startup but somehow it is no longer available.
9156This indicates a bug inside LDK. Please report this error at https://github.com/lightningdevkit/rust-lightning/issues/new";
9157			log_error!(self.logger, "{}", err);
9158			panic!("{}", err);
9159		};
9160
9161		let preimage_update = ChannelMonitorUpdate {
9162			update_id,
9163			updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
9164				payment_preimage,
9165				payment_info,
9166			}],
9167			channel_id: Some(prev_hop.channel_id),
9168		};
9169
9170		// We don't have any idea if this is a duplicate claim without interrogating the
9171		// `ChannelMonitor`, so we just always queue up the completion action after the
9172		// `ChannelMonitorUpdate` we're about to generate. This may result in a duplicate `Event`,
9173		// but note that `Event`s are generally always allowed to be duplicative (and it's
9174		// specifically noted in `PaymentForwarded`).
9175		let (action_opt, raa_blocker_opt) = completion_action(None, false);
9176
9177		if let Some(raa_blocker) = raa_blocker_opt {
9178			peer_state
9179				.actions_blocking_raa_monitor_updates
9180				.entry(prev_hop.channel_id)
9181				.or_default()
9182				.push(raa_blocker);
9183		}
9184
9185		// Given the fact that we're in a bit of a weird edge case, its worth hashing the preimage
9186		// to include the `payment_hash` in the log metadata here.
9187		let payment_hash = payment_preimage.into();
9188		let logger = WithContext::from(
9189			&self.logger,
9190			Some(prev_hop.counterparty_node_id),
9191			Some(chan_id),
9192			Some(payment_hash),
9193		);
9194
9195		if let Some(action) = action_opt {
9196			log_trace!(
9197				logger,
9198				"Tracking monitor update completion action for closed channel {}: {:?}",
9199				chan_id,
9200				action
9201			);
9202			peer_state
9203				.monitor_update_blocked_actions
9204				.entry(chan_id)
9205				.or_insert(Vec::new())
9206				.push(action);
9207		}
9208
9209		handle_new_monitor_update!(
9210			self,
9211			prev_hop.funding_txo,
9212			preimage_update,
9213			peer_state_lock,
9214			peer_state,
9215			per_peer_state,
9216			prev_hop.counterparty_node_id,
9217			chan_id,
9218			POST_CHANNEL_CLOSE
9219		);
9220	}
9221
9222	fn finalize_claims(&self, sources: Vec<(HTLCSource, Option<AttributionData>)>) {
9223		// Decode attribution data to hold times.
9224		let hold_times = sources.into_iter().filter_map(|(source, attribution_data)| {
9225			if let HTLCSource::OutboundRoute { ref session_priv, ref path, .. } = source {
9226				// If the path has trampoline hops, we need to hash the session private key to get the outer session key.
9227				let derived_key;
9228				let session_priv = if path.has_trampoline_hops() {
9229					let session_priv_hash =
9230						Sha256::hash(&session_priv.secret_bytes()).to_byte_array();
9231					derived_key = SecretKey::from_slice(&session_priv_hash[..]).unwrap();
9232					&derived_key
9233				} else {
9234					session_priv
9235				};
9236
9237				let hold_times = attribution_data.map_or(Vec::new(), |attribution_data| {
9238					decode_fulfill_attribution_data(
9239						&self.secp_ctx,
9240						&self.logger,
9241						path,
9242						session_priv,
9243						attribution_data,
9244					)
9245				});
9246
9247				Some((source, hold_times))
9248			} else {
9249				None
9250			}
9251		});
9252
9253		self.pending_outbound_payments.finalize_claims(hold_times, &self.pending_events);
9254	}
9255
9256	fn claim_funds_internal(
9257		&self, source: HTLCSource, payment_preimage: PaymentPreimage,
9258		forwarded_htlc_value_msat: Option<u64>, skimmed_fee_msat: Option<u64>, from_onchain: bool,
9259		next_channel_counterparty_node_id: PublicKey, next_channel_outpoint: OutPoint,
9260		next_channel_id: ChannelId, next_user_channel_id: Option<u128>,
9261		attribution_data: Option<AttributionData>, send_timestamp: Option<Duration>,
9262	) {
9263		let startup_replay =
9264			!self.background_events_processed_since_startup.load(Ordering::Acquire);
9265		let htlc_id = SentHTLCId::from_source(&source);
9266		match source {
9267			HTLCSource::OutboundRoute {
9268				session_priv, payment_id, path, bolt12_invoice, ..
9269			} => {
9270				debug_assert!(!startup_replay,
9271					"We don't support claim_htlc claims during startup - monitors may not be available yet");
9272				debug_assert_eq!(next_channel_counterparty_node_id, path.hops[0].pubkey);
9273
9274				let mut ev_completion_action = if from_onchain {
9275					let release = PaymentCompleteUpdate {
9276						counterparty_node_id: next_channel_counterparty_node_id,
9277						channel_funding_outpoint: next_channel_outpoint,
9278						channel_id: next_channel_id,
9279						htlc_id,
9280					};
9281					Some(EventCompletionAction::ReleasePaymentCompleteChannelMonitorUpdate(release))
9282				} else {
9283					Some(EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
9284						channel_funding_outpoint: Some(next_channel_outpoint),
9285						channel_id: next_channel_id,
9286						counterparty_node_id: path.hops[0].pubkey,
9287					})
9288				};
9289				self.pending_outbound_payments.claim_htlc(
9290					payment_id,
9291					payment_preimage,
9292					bolt12_invoice,
9293					session_priv,
9294					path,
9295					from_onchain,
9296					&mut ev_completion_action,
9297					&self.pending_events,
9298					&WithContext::from(&self.logger, None, None, Some(payment_preimage.into())),
9299				);
9300				// If an event was generated, `claim_htlc` set `ev_completion_action` to None, if
9301				// not, we should go ahead and run it now (as the claim was duplicative), at least
9302				// if a PaymentClaimed event with the same action isn't already pending.
9303				let have_action = if ev_completion_action.is_some() {
9304					let pending_events = self.pending_events.lock().unwrap();
9305					pending_events.iter().any(|(_, act)| *act == ev_completion_action)
9306				} else {
9307					false
9308				};
9309				if !have_action {
9310					self.handle_post_event_actions(ev_completion_action);
9311				}
9312			},
9313			HTLCSource::PreviousHopData(hop_data) => {
9314				let prev_channel_id = hop_data.channel_id;
9315				let prev_user_channel_id = hop_data.user_channel_id;
9316				let prev_node_id = hop_data.counterparty_node_id;
9317				let completed_blocker =
9318					RAAMonitorUpdateBlockingAction::from_prev_hop_data(&hop_data);
9319
9320				// Obtain hold time, if available.
9321				let hold_time = hold_time_since(send_timestamp).unwrap_or(0);
9322
9323				// If attribution data was received from downstream, we shift it and get it ready for adding our hold
9324				// time. Note that fulfilled HTLCs take a fast path to the incoming side. We don't need to wait for RAA
9325				// to record the hold time like we do for failed HTLCs.
9326				let attribution_data = process_fulfill_attribution_data(
9327					attribution_data,
9328					&hop_data.incoming_packet_shared_secret,
9329					hold_time,
9330				);
9331
9332				#[cfg(test)]
9333				let claiming_chan_funding_outpoint = hop_data.outpoint;
9334				self.claim_funds_from_hop(
9335					hop_data,
9336					payment_preimage,
9337					None,
9338					Some(attribution_data),
9339					|htlc_claim_value_msat, definitely_duplicate| {
9340						let chan_to_release = Some(EventUnblockedChannel {
9341							counterparty_node_id: next_channel_counterparty_node_id,
9342							funding_txo: next_channel_outpoint,
9343							channel_id: next_channel_id,
9344							blocking_action: completed_blocker,
9345						});
9346
9347						if definitely_duplicate && startup_replay {
9348							// On startup we may get redundant claims which are related to
9349							// monitor updates still in flight. In that case, we shouldn't
9350							// immediately free, but instead let that monitor update complete
9351							// in the background.
9352							#[cfg(test)]
9353							{
9354								let per_peer_state = self.per_peer_state.deadlocking_read();
9355								// The channel we'd unblock should already be closed, or...
9356								let channel_closed = per_peer_state
9357									.get(&next_channel_counterparty_node_id)
9358									.map(|lck| lck.deadlocking_lock())
9359									.map(|peer| !peer.channel_by_id.contains_key(&next_channel_id))
9360									.unwrap_or(true);
9361								let background_events =
9362									self.pending_background_events.lock().unwrap();
9363								// there should be a `BackgroundEvent` pending...
9364								let matching_bg_event =
9365									background_events.iter().any(|ev| {
9366										match ev {
9367											// to apply a monitor update that blocked the claiming channel,
9368											BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
9369												funding_txo, update, ..
9370											} => {
9371												if *funding_txo == claiming_chan_funding_outpoint {
9372													assert!(update.updates.iter().any(|upd|
9373														if let ChannelMonitorUpdateStep::PaymentPreimage {
9374															payment_preimage: update_preimage, ..
9375														} = upd {
9376															payment_preimage == *update_preimage
9377														} else { false }
9378													), "{:?}", update);
9379													true
9380												} else { false }
9381											},
9382											// or the monitor update has completed and will unblock
9383											// immediately once we get going.
9384											BackgroundEvent::MonitorUpdatesComplete {
9385												channel_id, ..
9386											} =>
9387												*channel_id == prev_channel_id,
9388											BackgroundEvent::AttemptUnblockMonitorUpdates { .. } => false,
9389										}
9390									});
9391								assert!(
9392									channel_closed || matching_bg_event,
9393									"{:?}",
9394									*background_events
9395								);
9396							}
9397							(None, None)
9398						} else if definitely_duplicate {
9399							if let Some(other_chan) = chan_to_release {
9400								(Some(MonitorUpdateCompletionAction::FreeOtherChannelImmediately {
9401									downstream_counterparty_node_id: other_chan.counterparty_node_id,
9402									downstream_channel_id: other_chan.channel_id,
9403									blocking_action: other_chan.blocking_action,
9404								}), None)
9405							} else {
9406								(None, None)
9407							}
9408						} else {
9409							let total_fee_earned_msat =
9410								if let Some(forwarded_htlc_value) = forwarded_htlc_value_msat {
9411									if let Some(claimed_htlc_value) = htlc_claim_value_msat {
9412										Some(claimed_htlc_value - forwarded_htlc_value)
9413									} else {
9414										None
9415									}
9416								} else {
9417									None
9418								};
9419							debug_assert!(
9420								skimmed_fee_msat <= total_fee_earned_msat,
9421								"skimmed_fee_msat must always be included in total_fee_earned_msat"
9422							);
9423							(
9424								Some(MonitorUpdateCompletionAction::EmitEventAndFreeOtherChannel {
9425									event: events::Event::PaymentForwarded {
9426										prev_channel_id: Some(prev_channel_id),
9427										next_channel_id: Some(next_channel_id),
9428										prev_user_channel_id,
9429										next_user_channel_id,
9430										prev_node_id,
9431										next_node_id: Some(next_channel_counterparty_node_id),
9432										total_fee_earned_msat,
9433										skimmed_fee_msat,
9434										claim_from_onchain_tx: from_onchain,
9435										outbound_amount_forwarded_msat: forwarded_htlc_value_msat,
9436									},
9437									downstream_counterparty_and_funding_outpoint: chan_to_release,
9438								}),
9439								None,
9440							)
9441						}
9442					},
9443				);
9444			},
9445		}
9446	}
9447
9448	/// Gets the node_id held by this ChannelManager
9449	pub fn get_our_node_id(&self) -> PublicKey {
9450		self.our_network_pubkey
9451	}
9452
9453	#[rustfmt::skip]
9454	fn handle_monitor_update_completion_actions<I: IntoIterator<Item=MonitorUpdateCompletionAction>>(&self, actions: I) {
9455		debug_assert_ne!(self.pending_events.held_by_thread(), LockHeldState::HeldByThread);
9456		debug_assert_ne!(self.claimable_payments.held_by_thread(), LockHeldState::HeldByThread);
9457		debug_assert_ne!(self.per_peer_state.held_by_thread(), LockHeldState::HeldByThread);
9458
9459		let mut freed_channels = Vec::new();
9460
9461		for action in actions.into_iter() {
9462			match action {
9463				MonitorUpdateCompletionAction::PaymentClaimed { payment_hash, pending_mpp_claim } => {
9464					if let Some((counterparty_node_id, chan_id, claim_ptr)) = pending_mpp_claim {
9465						let per_peer_state = self.per_peer_state.read().unwrap();
9466						per_peer_state.get(&counterparty_node_id).map(|peer_state_mutex| {
9467							let mut peer_state = peer_state_mutex.lock().unwrap();
9468							let blockers_entry = peer_state.actions_blocking_raa_monitor_updates.entry(chan_id);
9469							if let btree_map::Entry::Occupied(mut blockers) = blockers_entry {
9470								blockers.get_mut().retain(|blocker|
9471									if let &RAAMonitorUpdateBlockingAction::ClaimedMPPPayment { pending_claim } = &blocker {
9472										if *pending_claim == claim_ptr {
9473											let mut pending_claim_state_lock = pending_claim.0.lock().unwrap();
9474											let pending_claim_state = &mut *pending_claim_state_lock;
9475											pending_claim_state.channels_without_preimage.retain(|(cp, cid)| {
9476												let this_claim =
9477													*cp == counterparty_node_id && *cid == chan_id;
9478												if this_claim {
9479													pending_claim_state.channels_with_preimage.push((*cp, *cid));
9480													false
9481												} else { true }
9482											});
9483											if pending_claim_state.channels_without_preimage.is_empty() {
9484												for (cp, cid) in pending_claim_state.channels_with_preimage.iter() {
9485													let freed_chan = (*cp, *cid, blocker.clone());
9486													freed_channels.push(freed_chan);
9487												}
9488											}
9489											!pending_claim_state.channels_without_preimage.is_empty()
9490										} else { true }
9491									} else { true }
9492								);
9493								if blockers.get().is_empty() {
9494									blockers.remove();
9495								}
9496							}
9497						});
9498					}
9499
9500					let payment = self.claimable_payments.lock().unwrap().pending_claiming_payments.remove(&payment_hash);
9501					if let Some(ClaimingPayment {
9502						amount_msat,
9503						payment_purpose: purpose,
9504						receiver_node_id,
9505						htlcs,
9506						sender_intended_value: sender_intended_total_msat,
9507						onion_fields,
9508						payment_id,
9509						durable_preimage_channel,
9510					}) = payment {
9511						let event = events::Event::PaymentClaimed {
9512							payment_hash,
9513							purpose,
9514							amount_msat,
9515							receiver_node_id: Some(receiver_node_id),
9516							htlcs,
9517							sender_intended_total_msat,
9518							onion_fields,
9519							payment_id,
9520						};
9521						let action = if let Some((outpoint, counterparty_node_id, channel_id))
9522							= durable_preimage_channel
9523						{
9524							Some(EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
9525								channel_funding_outpoint: Some(outpoint),
9526								counterparty_node_id,
9527								channel_id,
9528							})
9529						} else {
9530							None
9531						};
9532						let event_action = (event, action);
9533						let mut pending_events = self.pending_events.lock().unwrap();
9534						// If we're replaying a claim on startup we may end up duplicating an event
9535						// that's already in our queue, so check before we push another one. The
9536						// `payment_id` should suffice to ensure we never spuriously drop a second
9537						// event for a duplicate payment.
9538						if !pending_events.contains(&event_action) {
9539							pending_events.push_back(event_action);
9540						}
9541					}
9542				},
9543				MonitorUpdateCompletionAction::EmitEventAndFreeOtherChannel {
9544					event, downstream_counterparty_and_funding_outpoint
9545				} => {
9546					self.pending_events.lock().unwrap().push_back((event, None));
9547					if let Some(unblocked) = downstream_counterparty_and_funding_outpoint {
9548						self.handle_monitor_update_release(
9549							unblocked.counterparty_node_id,
9550							unblocked.channel_id,
9551							Some(unblocked.blocking_action),
9552						);
9553					}
9554				},
9555				MonitorUpdateCompletionAction::FreeOtherChannelImmediately {
9556					downstream_counterparty_node_id, downstream_channel_id, blocking_action,
9557				} => {
9558					self.handle_monitor_update_release(
9559						downstream_counterparty_node_id,
9560						downstream_channel_id,
9561						Some(blocking_action),
9562					);
9563				},
9564			}
9565		}
9566
9567		for (node_id, channel_id, blocker) in freed_channels {
9568			self.handle_monitor_update_release(node_id, channel_id, Some(blocker));
9569		}
9570	}
9571
9572	/// Handles a channel reentering a functional state, either due to reconnect or a monitor
9573	/// update completion.
9574	#[rustfmt::skip]
9575	fn handle_channel_resumption(&self, pending_msg_events: &mut Vec<MessageSendEvent>,
9576		channel: &mut FundedChannel<SP>, raa: Option<msgs::RevokeAndACK>,
9577		commitment_update: Option<msgs::CommitmentUpdate>, commitment_order: RAACommitmentOrder,
9578		pending_forwards: Vec<(PendingHTLCInfo, u64)>, pending_update_adds: Vec<msgs::UpdateAddHTLC>,
9579		funding_broadcastable: Option<Transaction>,
9580		channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>,
9581		tx_signatures: Option<msgs::TxSignatures>, tx_abort: Option<msgs::TxAbort>,
9582		channel_ready_order: ChannelReadyOrder,
9583	) -> (Option<(u64, PublicKey, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
9584		let logger = WithChannelContext::from(&self.logger, &channel.context, None);
9585		log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {} pending update_add_htlcs, {}broadcasting funding, {} channel ready, {} announcement, {} tx_signatures, {} tx_abort",
9586			&channel.context.channel_id(),
9587			if raa.is_some() { "an" } else { "no" },
9588			if commitment_update.is_some() { "a" } else { "no" },
9589			pending_forwards.len(), pending_update_adds.len(),
9590			if funding_broadcastable.is_some() { "" } else { "not " },
9591			if channel_ready.is_some() { "sending" } else { "without" },
9592			if announcement_sigs.is_some() { "sending" } else { "without" },
9593			if tx_signatures.is_some() { "sending" } else { "without" },
9594			if tx_abort.is_some() { "sending" } else { "without" },
9595		);
9596
9597		let counterparty_node_id = channel.context.get_counterparty_node_id();
9598		let outbound_scid_alias = channel.context.outbound_scid_alias();
9599
9600		let mut htlc_forwards = None;
9601		if !pending_forwards.is_empty() {
9602			htlc_forwards = Some((
9603				outbound_scid_alias, channel.context.get_counterparty_node_id(),
9604				channel.funding.get_funding_txo().unwrap(), channel.context.channel_id(),
9605				channel.context.get_user_id(), pending_forwards
9606			));
9607		}
9608		let mut decode_update_add_htlcs = None;
9609		if !pending_update_adds.is_empty() {
9610			decode_update_add_htlcs = Some((outbound_scid_alias, pending_update_adds));
9611		}
9612
9613		if channel.context.is_connected() {
9614			if let ChannelReadyOrder::ChannelReadyFirst = channel_ready_order {
9615				if let Some(msg) = &channel_ready {
9616					send_channel_ready!(self, pending_msg_events, channel, msg.clone());
9617				}
9618
9619				if let Some(msg) = &announcement_sigs {
9620					pending_msg_events.push(MessageSendEvent::SendAnnouncementSignatures {
9621						node_id: counterparty_node_id,
9622						msg: msg.clone(),
9623					});
9624				}
9625			}
9626
9627			macro_rules! handle_cs { () => {
9628				if let Some(update) = commitment_update {
9629					pending_msg_events.push(MessageSendEvent::UpdateHTLCs {
9630						node_id: counterparty_node_id,
9631						channel_id: channel.context.channel_id(),
9632						updates: update,
9633					});
9634				}
9635			} }
9636			macro_rules! handle_raa { () => {
9637				if let Some(revoke_and_ack) = raa {
9638					pending_msg_events.push(MessageSendEvent::SendRevokeAndACK {
9639						node_id: counterparty_node_id,
9640						msg: revoke_and_ack,
9641					});
9642				}
9643			} }
9644			match commitment_order {
9645				RAACommitmentOrder::CommitmentFirst => {
9646					handle_cs!();
9647					handle_raa!();
9648				},
9649				RAACommitmentOrder::RevokeAndACKFirst => {
9650					handle_raa!();
9651					handle_cs!();
9652				},
9653			}
9654
9655			// TODO(dual_funding): For async signing support we need to hold back `tx_signatures` until the `commitment_signed` is ready.
9656			if let Some(msg) = tx_signatures {
9657				pending_msg_events.push(MessageSendEvent::SendTxSignatures {
9658					node_id: counterparty_node_id,
9659					msg,
9660				});
9661			}
9662			if let Some(msg) = tx_abort {
9663				pending_msg_events.push(MessageSendEvent::SendTxAbort {
9664					node_id: counterparty_node_id,
9665					msg,
9666				});
9667			}
9668
9669			if let ChannelReadyOrder::SignaturesFirst = channel_ready_order {
9670				if let Some(msg) = channel_ready {
9671					send_channel_ready!(self, pending_msg_events, channel, msg);
9672				}
9673
9674				if let Some(msg) = announcement_sigs {
9675					pending_msg_events.push(MessageSendEvent::SendAnnouncementSignatures {
9676						node_id: counterparty_node_id,
9677						msg,
9678					});
9679				}
9680			}
9681		} else if let Some(msg) = channel_ready {
9682			send_channel_ready!(self, pending_msg_events, channel, msg);
9683		}
9684
9685		if let Some(tx) = funding_broadcastable {
9686			if channel.context.is_manual_broadcast() {
9687				log_info!(logger, "Not broadcasting funding transaction with txid {} as it is manually managed", tx.compute_txid());
9688				let mut pending_events = self.pending_events.lock().unwrap();
9689				match channel.funding.get_funding_txo() {
9690					Some(funding_txo) => {
9691						emit_funding_tx_broadcast_safe_event!(pending_events, channel, funding_txo.into_bitcoin_outpoint())
9692					},
9693					None => {
9694						debug_assert!(false, "Channel resumed without a funding txo, this should never happen!");
9695						return (htlc_forwards, decode_update_add_htlcs);
9696					}
9697				};
9698			} else {
9699				log_info!(logger, "Broadcasting funding transaction with txid {}", tx.compute_txid());
9700				self.tx_broadcaster.broadcast_transactions(&[&tx]);
9701			}
9702		}
9703
9704		if let Some(signing_session) = (!channel.is_awaiting_monitor_update())
9705			.then(|| ())
9706			.and_then(|_| channel.context.interactive_tx_signing_session.as_mut())
9707			.filter(|signing_session| signing_session.has_received_commitment_signed())
9708			.filter(|signing_session| signing_session.holder_tx_signatures().is_none())
9709		{
9710			if signing_session.has_local_contribution() {
9711				let mut pending_events = self.pending_events.lock().unwrap();
9712				let unsigned_transaction = signing_session.unsigned_tx().tx().clone();
9713				let event_action = (
9714					Event::FundingTransactionReadyForSigning {
9715						unsigned_transaction,
9716						counterparty_node_id,
9717						channel_id: channel.context.channel_id(),
9718						user_channel_id: channel.context.get_user_id(),
9719					},
9720					None,
9721				);
9722
9723				if !pending_events.contains(&event_action) {
9724					pending_events.push_back(event_action);
9725				}
9726			} else {
9727				let txid = signing_session.unsigned_tx().compute_txid();
9728				let best_block_height = self.best_block.read().unwrap().height;
9729				match channel.funding_transaction_signed(txid, vec![], best_block_height, &self.logger) {
9730					Ok(FundingTxSigned {
9731						tx_signatures: Some(tx_signatures),
9732						funding_tx,
9733						splice_negotiated,
9734						splice_locked,
9735					}) => {
9736						if let Some(funding_tx) = funding_tx {
9737							self.broadcast_interactive_funding(channel, &funding_tx, &self.logger);
9738						}
9739
9740						if let Some(splice_negotiated) = splice_negotiated {
9741							self.pending_events.lock().unwrap().push_back((
9742								events::Event::SplicePending {
9743									channel_id: channel.context.channel_id(),
9744									counterparty_node_id,
9745									user_channel_id: channel.context.get_user_id(),
9746									new_funding_txo: splice_negotiated.funding_txo,
9747									channel_type: splice_negotiated.channel_type,
9748									new_funding_redeem_script: splice_negotiated.funding_redeem_script,
9749								},
9750								None,
9751							));
9752						}
9753
9754						if channel.context.is_connected() {
9755							pending_msg_events.push(MessageSendEvent::SendTxSignatures {
9756								node_id: counterparty_node_id,
9757								msg: tx_signatures,
9758							});
9759							if let Some(splice_locked) = splice_locked {
9760								pending_msg_events.push(MessageSendEvent::SendSpliceLocked {
9761									node_id: counterparty_node_id,
9762									msg: splice_locked,
9763								});
9764							}
9765						}
9766					},
9767					Ok(FundingTxSigned { tx_signatures: None, .. }) => {
9768						debug_assert!(false, "If our tx_signatures is empty, then we should send it first!");
9769					},
9770					Err(err) => {
9771						log_warn!(logger, "Failed signing interactive funding transaction: {err:?}");
9772					},
9773				}
9774			}
9775		}
9776
9777		{
9778			let mut pending_events = self.pending_events.lock().unwrap();
9779			emit_channel_pending_event!(pending_events, channel);
9780			emit_initial_channel_ready_event!(pending_events, channel);
9781		}
9782
9783		(htlc_forwards, decode_update_add_htlcs)
9784	}
9785
9786	#[rustfmt::skip]
9787	fn channel_monitor_updated(&self, channel_id: &ChannelId, highest_applied_update_id: Option<u64>, counterparty_node_id: &PublicKey) {
9788		debug_assert!(self.total_consistency_lock.try_write().is_err()); // Caller holds read lock
9789
9790		let per_peer_state = self.per_peer_state.read().unwrap();
9791		let mut peer_state_lock;
9792		let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
9793		if peer_state_mutex_opt.is_none() { return }
9794		peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
9795		let peer_state = &mut *peer_state_lock;
9796
9797		let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), Some(*channel_id), None);
9798		let remaining_in_flight =
9799			if let Some((_, pending)) = peer_state.in_flight_monitor_updates.get_mut(channel_id) {
9800				if let Some(highest_applied_update_id) = highest_applied_update_id {
9801					pending.retain(|upd| upd.update_id > highest_applied_update_id);
9802					log_trace!(
9803						logger,
9804						"ChannelMonitor updated to {highest_applied_update_id}. {} pending in-flight updates.",
9805						pending.len()
9806					);
9807				} else if let Some(update) = pending.get(0) {
9808					log_trace!(
9809						logger,
9810						"ChannelMonitor updated to {}. {} pending in-flight updates.",
9811						update.update_id - 1,
9812						pending.len()
9813					);
9814				} else {
9815					log_trace!(
9816						logger,
9817						"ChannelMonitor updated. {} pending in-flight updates.",
9818						pending.len()
9819					);
9820				}
9821				pending.len()
9822			} else { 0 };
9823
9824		if remaining_in_flight != 0 {
9825			return;
9826		}
9827
9828		if let Some(chan) = peer_state.channel_by_id
9829			.get_mut(channel_id)
9830			.and_then(Channel::as_funded_mut)
9831		{
9832			if chan.is_awaiting_monitor_update() {
9833				handle_monitor_update_completion!(self, peer_state_lock, peer_state, per_peer_state, chan);
9834			} else {
9835				log_trace!(logger, "Channel is open but not awaiting update");
9836			}
9837		} else {
9838			let update_actions = peer_state.monitor_update_blocked_actions
9839				.remove(channel_id).unwrap_or(Vec::new());
9840			log_trace!(logger, "Channel is closed, applying {} post-update actions", update_actions.len());
9841			mem::drop(peer_state_lock);
9842			mem::drop(per_peer_state);
9843			self.handle_monitor_update_completion_actions(update_actions);
9844		}
9845	}
9846
9847	/// Accepts a request to open a channel after a [`Event::OpenChannelRequest`].
9848	///
9849	/// The `temporary_channel_id` parameter indicates which inbound channel should be accepted,
9850	/// and the `counterparty_node_id` parameter is the id of the peer which has requested to open
9851	/// the channel.
9852	///
9853	/// The `user_channel_id` parameter will be provided back in
9854	/// [`Event::ChannelClosed::user_channel_id`] to allow tracking of which events correspond
9855	/// with which `accept_inbound_channel`/`accept_inbound_channel_from_trusted_peer_0conf` call.
9856	///
9857	/// Note that this method will return an error and reject the channel, if it requires support
9858	/// for zero confirmations. Instead, `accept_inbound_channel_from_trusted_peer_0conf` must be
9859	/// used to accept such channels.
9860	///
9861	/// NOTE: LDK makes no attempt to prevent the counterparty from using non-standard inputs which
9862	/// will prevent the funding transaction from being relayed on the bitcoin network and hence being
9863	/// confirmed.
9864	///
9865	/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
9866	/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
9867	pub fn accept_inbound_channel(
9868		&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey,
9869		user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>,
9870	) -> Result<(), APIError> {
9871		self.do_accept_inbound_channel(
9872			temporary_channel_id,
9873			counterparty_node_id,
9874			false,
9875			user_channel_id,
9876			config_overrides,
9877		)
9878	}
9879
9880	/// Accepts a request to open a channel after a [`events::Event::OpenChannelRequest`], treating
9881	/// it as confirmed immediately.
9882	///
9883	/// The `user_channel_id` parameter will be provided back in
9884	/// [`Event::ChannelClosed::user_channel_id`] to allow tracking of which events correspond
9885	/// with which `accept_inbound_channel`/`accept_inbound_channel_from_trusted_peer_0conf` call.
9886	///
9887	/// Unlike [`ChannelManager::accept_inbound_channel`], this method accepts the incoming channel
9888	/// and (if the counterparty agrees), enables forwarding of payments immediately.
9889	///
9890	/// This fully trusts that the counterparty has honestly and correctly constructed the funding
9891	/// transaction and blindly assumes that it will eventually confirm.
9892	///
9893	/// If it does not confirm before we decide to close the channel, or if the funding transaction
9894	/// does not pay to the correct script the correct amount, *you will lose funds*.
9895	///
9896	/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
9897	/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
9898	pub fn accept_inbound_channel_from_trusted_peer_0conf(
9899		&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey,
9900		user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>,
9901	) -> Result<(), APIError> {
9902		self.do_accept_inbound_channel(
9903			temporary_channel_id,
9904			counterparty_node_id,
9905			true,
9906			user_channel_id,
9907			config_overrides,
9908		)
9909	}
9910
9911	/// TODO(dual_funding): Allow contributions, pass intended amount and inputs
9912	#[rustfmt::skip]
9913	fn do_accept_inbound_channel(
9914		&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, accept_0conf: bool,
9915		user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>
9916	) -> Result<(), APIError> {
9917
9918		let mut config = self.config.read().unwrap().clone();
9919
9920		// Apply configuration overrides.
9921		if let Some(overrides) = config_overrides {
9922			config.apply(&overrides);
9923		};
9924
9925		let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), Some(*temporary_channel_id), None);
9926		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9927
9928		let peers_without_funded_channels =
9929			self.peers_without_funded_channels(|peer| { peer.total_channel_count() > 0 });
9930		let per_peer_state = self.per_peer_state.read().unwrap();
9931		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
9932		.ok_or_else(|| {
9933			let err_str = format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}");
9934			log_error!(logger, "{}", err_str);
9935
9936			APIError::ChannelUnavailable { err: err_str }
9937		})?;
9938		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
9939		let peer_state = &mut *peer_state_lock;
9940		let peer_lacks_funded_channels =
9941			Self::unfunded_channel_count(peer_state, self.best_block.read().unwrap().height)
9942				== peer_state.total_channel_count();
9943
9944		// Find (and remove) the channel in the unaccepted table. If it's not there, something weird is
9945		// happening and return an error. N.B. that we create channel with an outbound SCID of zero so
9946		// that we can delay allocating the SCID until after we're sure that the checks below will
9947		// succeed.
9948		let res = match peer_state.inbound_channel_request_by_id.remove(temporary_channel_id) {
9949			Some(unaccepted_channel) => {
9950				let best_block_height = self.best_block.read().unwrap().height;
9951				match unaccepted_channel.open_channel_msg {
9952					OpenChannelMessage::V1(open_channel_msg) => {
9953						InboundV1Channel::new(
9954							&self.fee_estimator, &self.entropy_source, &self.signer_provider, *counterparty_node_id,
9955							&self.channel_type_features(), &peer_state.latest_features, &open_channel_msg,
9956							user_channel_id, &config, best_block_height, &self.logger, accept_0conf
9957						).map_err(|err| MsgHandleErrInternal::from_chan_no_close(err, *temporary_channel_id)
9958						).map(|mut channel| {
9959							let logger = WithChannelContext::from(&self.logger, &channel.context, None);
9960							let message_send_event = channel.accept_inbound_channel(&&logger).map(|msg| {
9961								MessageSendEvent::SendAcceptChannel {
9962									node_id: *counterparty_node_id,
9963									msg,
9964								}
9965							});
9966							(*temporary_channel_id, Channel::from(channel), message_send_event)
9967						})
9968					},
9969					OpenChannelMessage::V2(open_channel_msg) => {
9970						PendingV2Channel::new_inbound(
9971							&self.fee_estimator, &self.entropy_source, &self.signer_provider,
9972							self.get_our_node_id(), *counterparty_node_id,
9973							&self.channel_type_features(), &peer_state.latest_features,
9974							&open_channel_msg,
9975							user_channel_id, &config, best_block_height,
9976							&self.logger,
9977						).map_err(|e| {
9978							let channel_id = open_channel_msg.common_fields.temporary_channel_id;
9979							MsgHandleErrInternal::from_chan_no_close(e, channel_id)
9980						}).map(|channel| {
9981							let message_send_event =  MessageSendEvent::SendAcceptChannelV2 {
9982								node_id: channel.context.get_counterparty_node_id(),
9983								msg: channel.accept_inbound_dual_funded_channel()
9984							};
9985							(channel.context.channel_id(), Channel::from(channel), Some(message_send_event))
9986						})
9987					},
9988				}
9989			},
9990			None => {
9991				let err_str = "No such channel awaiting to be accepted.".to_owned();
9992				log_error!(logger, "{}", err_str);
9993
9994				return Err(APIError::APIMisuseError { err: err_str });
9995			}
9996		};
9997
9998		// We have to match below instead of map_err on the above as in the map_err closure the borrow checker
9999		// would consider peer_state moved even though we would bail out with the `?` operator.
10000		let (channel_id, mut channel, message_send_event) = match res {
10001			Ok(res) => res,
10002			Err(err) => {
10003				mem::drop(peer_state_lock);
10004				mem::drop(per_peer_state);
10005				// TODO(dunxen): Find/make less icky way to do this.
10006				match handle_error!(self, Result::<(), MsgHandleErrInternal>::Err(err), *counterparty_node_id) {
10007					Ok(_) => unreachable!("`handle_error` only returns Err as we've passed in an Err"),
10008					Err(e) => {
10009						return Err(APIError::ChannelUnavailable { err: e.err });
10010					},
10011				}
10012			}
10013		};
10014
10015		if accept_0conf {
10016			// This should have been correctly configured by the call to Inbound(V1/V2)Channel::new.
10017			debug_assert!(channel.minimum_depth().unwrap() == 0);
10018		} else if channel.funding().get_channel_type().requires_zero_conf() {
10019			let send_msg_err_event = MessageSendEvent::HandleError {
10020				node_id: channel.context().get_counterparty_node_id(),
10021				action: msgs::ErrorAction::SendErrorMessage{
10022					msg: msgs::ErrorMessage { channel_id: *temporary_channel_id, data: "No zero confirmation channels accepted".to_owned(), }
10023				}
10024			};
10025			debug_assert!(peer_state.is_connected);
10026			peer_state.pending_msg_events.push(send_msg_err_event);
10027			let err_str = "Please use accept_inbound_channel_from_trusted_peer_0conf to accept channels with zero confirmations.".to_owned();
10028			log_error!(logger, "{}", err_str);
10029
10030			return Err(APIError::APIMisuseError { err: err_str });
10031		} else {
10032			// If this peer already has a funded channel with us, accepting another channel won't
10033			// increase the number of unfunded channels. Otherwise, make sure we don't end up with
10034			// too many peers with unfunded channels afterwards.
10035			if peer_lacks_funded_channels
10036				&& peers_without_funded_channels > MAX_UNFUNDED_CHANNEL_PEERS
10037			{
10038				let send_msg_err_event = MessageSendEvent::HandleError {
10039					node_id: channel.context().get_counterparty_node_id(),
10040					action: msgs::ErrorAction::SendErrorMessage{
10041						msg: msgs::ErrorMessage { channel_id: *temporary_channel_id, data: "Have too many peers with unfunded channels, not accepting new ones".to_owned(), }
10042					}
10043				};
10044				debug_assert!(peer_state.is_connected);
10045				peer_state.pending_msg_events.push(send_msg_err_event);
10046				let err_str = "Too many peers with unfunded channels, refusing to accept new ones".to_owned();
10047				log_error!(logger, "{}", err_str);
10048
10049				return Err(APIError::APIMisuseError { err: err_str });
10050			}
10051		}
10052
10053		// Now that we know we have a channel, assign an outbound SCID alias.
10054		let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
10055		channel.context_mut().set_outbound_scid_alias(outbound_scid_alias);
10056
10057		if let Some(message_send_event) = message_send_event {
10058			debug_assert!(peer_state.is_connected);
10059			peer_state.pending_msg_events.push(message_send_event);
10060		}
10061		peer_state.channel_by_id.insert(channel_id, channel);
10062
10063		Ok(())
10064	}
10065
10066	/// Gets the number of peers which match the given filter and do not have any funded, outbound,
10067	/// or 0-conf channels.
10068	///
10069	/// The filter is called for each peer and provided with the number of unfunded, inbound, and
10070	/// non-0-conf channels we have with the peer.
10071	fn peers_without_funded_channels<Filter>(&self, maybe_count_peer: Filter) -> usize
10072	where
10073		Filter: Fn(&PeerState<SP>) -> bool,
10074	{
10075		let mut peers_without_funded_channels = 0;
10076		let best_block_height = self.best_block.read().unwrap().height;
10077		{
10078			let peer_state_lock = self.per_peer_state.read().unwrap();
10079			for (_, peer_mtx) in peer_state_lock.iter() {
10080				let peer = peer_mtx.lock().unwrap();
10081				if !maybe_count_peer(&*peer) {
10082					continue;
10083				}
10084				let num_unfunded_channels = Self::unfunded_channel_count(&peer, best_block_height);
10085				if num_unfunded_channels == peer.total_channel_count() {
10086					peers_without_funded_channels += 1;
10087				}
10088			}
10089		}
10090		return peers_without_funded_channels;
10091	}
10092
10093	#[rustfmt::skip]
10094	fn unfunded_channel_count(
10095		peer: &PeerState<SP>, best_block_height: u32
10096	) -> usize {
10097		let mut num_unfunded_channels = 0;
10098		for (_, chan) in peer.channel_by_id.iter() {
10099			match chan.as_funded() {
10100				Some(funded_chan) => {
10101					// This covers non-zero-conf inbound `Channel`s that we are currently monitoring, but those
10102					// which have not yet had any confirmations on-chain.
10103					if !funded_chan.funding.is_outbound() && chan.minimum_depth().unwrap_or(1) != 0 &&
10104						funded_chan.funding.get_funding_tx_confirmations(best_block_height) == 0
10105					{
10106						num_unfunded_channels += 1;
10107					}
10108				},
10109				None => {
10110					// Outbound channels don't contribute to the unfunded count in the DoS context.
10111					if chan.funding().is_outbound() {
10112						continue;
10113					}
10114
10115					// 0conf channels are not considered unfunded.
10116					if chan.minimum_depth().unwrap_or(1) == 0 {
10117						continue;
10118					}
10119
10120					// Inbound V2 channels with contributed inputs are not considered unfunded.
10121					if let Some(unfunded_chan) = chan.as_unfunded_v2() {
10122						if unfunded_chan.funding_negotiation_context.our_funding_contribution > SignedAmount::ZERO {
10123							continue;
10124						}
10125					}
10126
10127					num_unfunded_channels += 1;
10128				},
10129			}
10130		}
10131		num_unfunded_channels + peer.inbound_channel_request_by_id.len()
10132	}
10133
10134	#[rustfmt::skip]
10135	fn internal_open_channel(&self, counterparty_node_id: &PublicKey, msg: OpenChannelMessageRef<'_>) -> Result<(), MsgHandleErrInternal> {
10136		let common_fields = match msg {
10137			OpenChannelMessageRef::V1(msg) => &msg.common_fields,
10138			OpenChannelMessageRef::V2(msg) => &msg.common_fields,
10139		};
10140
10141		// Do common open_channel(2) checks
10142
10143		// Note that the ChannelManager is NOT re-persisted on disk after this, so any changes are
10144		// likely to be lost on restart!
10145		if common_fields.chain_hash != self.chain_hash {
10146			return Err(MsgHandleErrInternal::send_err_msg_no_close("Unknown genesis block hash".to_owned(),
10147				 common_fields.temporary_channel_id));
10148		}
10149
10150		if !self.config.read().unwrap().accept_inbound_channels {
10151			return Err(MsgHandleErrInternal::send_err_msg_no_close("No inbound channels accepted".to_owned(),
10152				 common_fields.temporary_channel_id));
10153		}
10154
10155		// Get the number of peers with channels, but without funded ones. We don't care too much
10156		// about peers that never open a channel, so we filter by peers that have at least one
10157		// channel, and then limit the number of those with unfunded channels.
10158		let channeled_peers_without_funding =
10159			self.peers_without_funded_channels(|node| node.total_channel_count() > 0);
10160
10161		let per_peer_state = self.per_peer_state.read().unwrap();
10162		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
10163		    .ok_or_else(|| {
10164				debug_assert!(false);
10165				MsgHandleErrInternal::send_err_msg_no_close(
10166					format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"),
10167					common_fields.temporary_channel_id)
10168			})?;
10169		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10170		let peer_state = &mut *peer_state_lock;
10171
10172		// If this peer already has some channels, a new channel won't increase our number of peers
10173		// with unfunded channels, so as long as we aren't over the maximum number of unfunded
10174		// channels per-peer we can accept channels from a peer with existing ones.
10175		if peer_state.total_channel_count() == 0 &&
10176			channeled_peers_without_funding >= MAX_UNFUNDED_CHANNEL_PEERS &&
10177			!self.config.read().unwrap().manually_accept_inbound_channels
10178		{
10179			return Err(MsgHandleErrInternal::send_err_msg_no_close(
10180				"Have too many peers with unfunded channels, not accepting new ones".to_owned(),
10181				common_fields.temporary_channel_id));
10182		}
10183
10184		let best_block_height = self.best_block.read().unwrap().height;
10185		if Self::unfunded_channel_count(peer_state, best_block_height) >= MAX_UNFUNDED_CHANS_PER_PEER {
10186			return Err(MsgHandleErrInternal::send_err_msg_no_close(
10187				format!("Refusing more than {} unfunded channels.", MAX_UNFUNDED_CHANS_PER_PEER),
10188				common_fields.temporary_channel_id));
10189		}
10190
10191		let channel_id = common_fields.temporary_channel_id;
10192		let channel_exists = peer_state.has_channel(&channel_id);
10193		if channel_exists {
10194			return Err(MsgHandleErrInternal::send_err_msg_no_close(
10195				"temporary_channel_id collision for the same peer!".to_owned(),
10196				common_fields.temporary_channel_id));
10197		}
10198
10199		// We can get the channel type at this point already as we'll need it immediately in both the
10200		// manual and the automatic acceptance cases.
10201		let channel_type = channel::channel_type_from_open_channel(
10202			common_fields, &self.channel_type_features()
10203		).map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, common_fields.temporary_channel_id))?;
10204
10205		// If we're doing manual acceptance checks on the channel, then defer creation until we're sure we want to accept.
10206		if self.config.read().unwrap().manually_accept_inbound_channels {
10207			let mut pending_events = self.pending_events.lock().unwrap();
10208			let is_announced = (common_fields.channel_flags & 1) == 1;
10209			pending_events.push_back((events::Event::OpenChannelRequest {
10210				temporary_channel_id: common_fields.temporary_channel_id,
10211				counterparty_node_id: *counterparty_node_id,
10212				funding_satoshis: common_fields.funding_satoshis,
10213				channel_negotiation_type: match msg {
10214					OpenChannelMessageRef::V1(msg) => InboundChannelFunds::PushMsat(msg.push_msat),
10215					OpenChannelMessageRef::V2(_) => InboundChannelFunds::DualFunded,
10216				},
10217				channel_type,
10218				is_announced,
10219				params: common_fields.channel_parameters(),
10220			}, None));
10221			peer_state.inbound_channel_request_by_id.insert(channel_id, InboundChannelRequest {
10222				open_channel_msg: match msg {
10223					OpenChannelMessageRef::V1(msg) => OpenChannelMessage::V1(msg.clone()),
10224					OpenChannelMessageRef::V2(msg) => OpenChannelMessage::V2(msg.clone()),
10225				},
10226				ticks_remaining: UNACCEPTED_INBOUND_CHANNEL_AGE_LIMIT_TICKS,
10227			});
10228			return Ok(());
10229		}
10230
10231		// Otherwise create the channel right now.
10232		let mut random_bytes = [0u8; 16];
10233		random_bytes.copy_from_slice(&self.entropy_source.get_secure_random_bytes()[..16]);
10234		let user_channel_id = u128::from_be_bytes(random_bytes);
10235
10236		if channel_type.requires_zero_conf() {
10237			return Err(MsgHandleErrInternal::send_err_msg_no_close("No zero confirmation channels accepted".to_owned(), common_fields.temporary_channel_id));
10238		}
10239		if channel_type.requires_anchors_zero_fee_htlc_tx() || channel_type.requires_anchor_zero_fee_commitments() {
10240			return Err(MsgHandleErrInternal::send_err_msg_no_close("No channels with anchor outputs accepted".to_owned(), common_fields.temporary_channel_id));
10241		}
10242
10243		let (mut channel, message_send_event) = match msg {
10244			OpenChannelMessageRef::V1(msg) => {
10245				let mut channel = InboundV1Channel::new(
10246					&self.fee_estimator, &self.entropy_source, &self.signer_provider, *counterparty_node_id,
10247					&self.channel_type_features(), &peer_state.latest_features, msg, user_channel_id,
10248					&self.config.read().unwrap(), best_block_height, &self.logger, /*is_0conf=*/false
10249				).map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, msg.common_fields.temporary_channel_id))?;
10250				let logger = WithChannelContext::from(&self.logger, &channel.context, None);
10251				let message_send_event = channel.accept_inbound_channel(&&logger).map(|msg| {
10252					MessageSendEvent::SendAcceptChannel {
10253						node_id: *counterparty_node_id,
10254						msg,
10255					}
10256				});
10257				(Channel::from(channel), message_send_event)
10258			},
10259			OpenChannelMessageRef::V2(msg) => {
10260				let channel = PendingV2Channel::new_inbound(
10261					&self.fee_estimator, &self.entropy_source, &self.signer_provider,
10262					self.get_our_node_id(), *counterparty_node_id, &self.channel_type_features(),
10263					&peer_state.latest_features, msg, user_channel_id,
10264					&self.config.read().unwrap(), best_block_height, &self.logger,
10265				).map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, msg.common_fields.temporary_channel_id))?;
10266				let message_send_event = MessageSendEvent::SendAcceptChannelV2 {
10267					node_id: *counterparty_node_id,
10268					msg: channel.accept_inbound_dual_funded_channel(),
10269				};
10270				(Channel::from(channel), Some(message_send_event))
10271			},
10272		};
10273
10274		let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
10275		channel.context_mut().set_outbound_scid_alias(outbound_scid_alias);
10276
10277		if let Some(message_send_event) = message_send_event {
10278			peer_state.pending_msg_events.push(message_send_event);
10279		}
10280		peer_state.channel_by_id.insert(channel.context().channel_id(), channel);
10281
10282		Ok(())
10283	}
10284
10285	#[rustfmt::skip]
10286	fn internal_accept_channel(&self, counterparty_node_id: &PublicKey, msg: &msgs::AcceptChannel) -> Result<(), MsgHandleErrInternal> {
10287		// Note that the ChannelManager is NOT re-persisted on disk after this, so any changes are
10288		// likely to be lost on restart!
10289		let (value, output_script, user_id) = {
10290			let per_peer_state = self.per_peer_state.read().unwrap();
10291			let peer_state_mutex = per_peer_state.get(counterparty_node_id)
10292				.ok_or_else(|| {
10293					debug_assert!(false);
10294					MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), msg.common_fields.temporary_channel_id)
10295				})?;
10296			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10297			let peer_state = &mut *peer_state_lock;
10298			match peer_state.channel_by_id.entry(msg.common_fields.temporary_channel_id) {
10299				hash_map::Entry::Occupied(mut chan) => {
10300					match chan.get_mut().as_unfunded_outbound_v1_mut() {
10301						Some(unfunded_chan) => {
10302							let res = unfunded_chan.accept_channel(
10303								msg,
10304								&self.config.read().unwrap().channel_handshake_limits,
10305								&peer_state.latest_features,
10306							);
10307							try_channel_entry!(self, peer_state, res, chan);
10308							(unfunded_chan.funding.get_value_satoshis(), unfunded_chan.funding.get_funding_redeemscript().to_p2wsh(), unfunded_chan.context.get_user_id())
10309						},
10310						None => {
10311							return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got an unexpected accept_channel message from peer with counterparty_node_id {}", counterparty_node_id), msg.common_fields.temporary_channel_id));
10312						}
10313					}
10314				},
10315				hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.common_fields.temporary_channel_id))
10316			}
10317		};
10318		let mut pending_events = self.pending_events.lock().unwrap();
10319		pending_events.push_back((events::Event::FundingGenerationReady {
10320			temporary_channel_id: msg.common_fields.temporary_channel_id,
10321			counterparty_node_id: *counterparty_node_id,
10322			channel_value_satoshis: value,
10323			output_script,
10324			user_channel_id: user_id,
10325		}, None));
10326		Ok(())
10327	}
10328
10329	#[rustfmt::skip]
10330	fn internal_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
10331		let best_block = *self.best_block.read().unwrap();
10332
10333		let per_peer_state = self.per_peer_state.read().unwrap();
10334		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
10335			.ok_or_else(|| {
10336				debug_assert!(false);
10337				MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), msg.temporary_channel_id)
10338			})?;
10339
10340		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10341		let peer_state = &mut *peer_state_lock;
10342		let (mut chan, funding_msg_opt, monitor) =
10343			match peer_state.channel_by_id.remove(&msg.temporary_channel_id)
10344				.map(Channel::into_unfunded_inbound_v1)
10345			{
10346				Some(Ok(inbound_chan)) => {
10347					let logger = WithChannelContext::from(&self.logger, &inbound_chan.context, None);
10348					match inbound_chan.funding_created(msg, best_block, &self.signer_provider, &&logger) {
10349						Ok(res) => res,
10350						Err((inbound_chan, err)) => {
10351							// We've already removed this inbound channel from the map in `PeerState`
10352							// above so at this point we just need to clean up any lingering entries
10353							// concerning this channel as it is safe to do so.
10354							debug_assert!(matches!(err, ChannelError::Close(_)));
10355							let mut chan = Channel::from(inbound_chan);
10356							return Err(convert_channel_err!(self, peer_state, err, &mut chan).1);
10357						},
10358					}
10359				},
10360				Some(Err(mut chan)) => {
10361					let err_msg = format!("Got an unexpected funding_created message from peer with counterparty_node_id {}", counterparty_node_id);
10362					let err = ChannelError::close(err_msg);
10363					return Err(convert_channel_err!(self, peer_state, err, &mut chan).1);
10364				},
10365				None => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.temporary_channel_id))
10366			};
10367
10368		let funded_channel_id = chan.context.channel_id();
10369
10370		macro_rules! fail_chan { ($err: expr) => { {
10371			// Note that at this point we've filled in the funding outpoint on our
10372			// channel, but its actually in conflict with another channel. Thus, if
10373			// we call `convert_channel_err` immediately (thus calling
10374			// `locked_close_channel`), we'll remove the existing channel from `outpoint_to_peer`.
10375			// Thus, we must first unset the funding outpoint on the channel.
10376			let err = ChannelError::close($err.to_owned());
10377			chan.unset_funding_info();
10378			let mut chan = Channel::from(chan);
10379			return Err(convert_channel_err!(self, peer_state, err, &mut chan, UNFUNDED_CHANNEL).1);
10380		} } }
10381
10382		match peer_state.channel_by_id.entry(funded_channel_id) {
10383			hash_map::Entry::Occupied(_) => {
10384				fail_chan!("Already had channel with the new channel_id");
10385			},
10386			hash_map::Entry::Vacant(e) => {
10387				let monitor_res = self.chain_monitor.watch_channel(monitor.channel_id(), monitor);
10388				if let Ok(persist_state) = monitor_res {
10389					// There's no problem signing a counterparty's funding transaction if our monitor
10390					// hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
10391					// accepted payment from yet. We do, however, need to wait to send our channel_ready
10392					// until we have persisted our monitor.
10393					if let Some(msg) = funding_msg_opt {
10394						peer_state.pending_msg_events.push(MessageSendEvent::SendFundingSigned {
10395							node_id: *counterparty_node_id,
10396							msg,
10397						});
10398					}
10399
10400					if let Some(funded_chan) = e.insert(Channel::from(chan)).as_funded_mut() {
10401						handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
10402							per_peer_state, funded_chan, INITIAL_MONITOR);
10403					} else {
10404						unreachable!("This must be a funded channel as we just inserted it.");
10405					}
10406					Ok(())
10407				} else {
10408					let logger = WithChannelContext::from(&self.logger, &chan.context, None);
10409					log_error!(logger, "Persisting initial ChannelMonitor failed, implying the channel ID was duplicated");
10410					fail_chan!("Duplicate channel ID");
10411				}
10412			}
10413		}
10414	}
10415
10416	fn internal_peer_storage_retrieval(
10417		&self, peer_node_id: PublicKey, msg: msgs::PeerStorageRetrieval,
10418	) -> Result<(), MsgHandleErrInternal> {
10419		// TODO: Check if have any stale or missing ChannelMonitor.
10420		let logger = WithContext::from(&self.logger, Some(peer_node_id), None, None);
10421		let err = || {
10422			MsgHandleErrInternal::from_chan_no_close(
10423				ChannelError::Ignore("Invalid PeerStorageRetrieval message received.".into()),
10424				ChannelId([0; 32]),
10425			)
10426		};
10427
10428		let encrypted_ops = match EncryptedOurPeerStorage::new(msg.data) {
10429			Ok(encrypted_ops) => encrypted_ops,
10430			Err(()) => {
10431				log_debug!(logger, "Received a peer backup which wasn't long enough to be valid");
10432				return Err(err());
10433			},
10434		};
10435
10436		let decrypted = match encrypted_ops.decrypt(&self.node_signer.get_peer_storage_key()) {
10437			Ok(decrypted_ops) => decrypted_ops.into_vec(),
10438			Err(()) => {
10439				log_debug!(logger, "Received a peer backup which was corrupted");
10440				return Err(err());
10441			},
10442		};
10443
10444		log_trace!(logger, "Got valid {}-byte peer backup from {}", decrypted.len(), peer_node_id);
10445		let per_peer_state = self.per_peer_state.read().unwrap();
10446
10447		let mut cursor = io::Cursor::new(decrypted);
10448		let mon_list = <Vec<PeerStorageMonitorHolder> as Readable>::read(&mut cursor)
10449			.unwrap_or_else(|e| {
10450				// This should NEVER happen.
10451				debug_assert!(false);
10452				log_debug!(self.logger, "Unable to unpack the retrieved peer storage {:?}", e);
10453				Vec::new()
10454			});
10455
10456		for mon_holder in mon_list.iter() {
10457			let peer_state_mutex = match per_peer_state.get(&mon_holder.counterparty_node_id) {
10458				Some(mutex) => mutex,
10459				None => {
10460					log_debug!(
10461						logger,
10462						"Not able to find peer_state for the counterparty {}, channel_id {}",
10463						log_pubkey!(mon_holder.counterparty_node_id),
10464						mon_holder.channel_id
10465					);
10466					continue;
10467				},
10468			};
10469
10470			let peer_state_lock = peer_state_mutex.lock().unwrap();
10471			let peer_state = &*peer_state_lock;
10472
10473			match peer_state.channel_by_id.get(&mon_holder.channel_id) {
10474				Some(chan) => {
10475					if let Some(funded_chan) = chan.as_funded() {
10476						if funded_chan.get_revoked_counterparty_commitment_transaction_number()
10477							> mon_holder.min_seen_secret
10478						{
10479							panic!(
10480								"Lost channel state for channel {}.\n\
10481								Received peer storage with a more recent state than what our node had.\n\
10482								Use the FundRecoverer to initiate a force close and sweep the funds.",
10483								&mon_holder.channel_id
10484							);
10485						}
10486					}
10487				},
10488				None => {
10489					log_debug!(logger, "Found an unknown channel {}", &mon_holder.channel_id);
10490				},
10491			}
10492		}
10493		Ok(())
10494	}
10495
10496	#[rustfmt::skip]
10497	fn internal_peer_storage(&self, counterparty_node_id: PublicKey, msg: msgs::PeerStorage) -> Result<(), MsgHandleErrInternal> {
10498		let per_peer_state = self.per_peer_state.read().unwrap();
10499		let peer_state_mutex = per_peer_state.get(&counterparty_node_id)
10500			.ok_or_else(|| {
10501				debug_assert!(false);
10502				MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), ChannelId([0; 32]))
10503			})?;
10504
10505		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10506		let peer_state = &mut *peer_state_lock;
10507		let logger = WithContext::from(&self.logger, Some(counterparty_node_id), None, None);
10508
10509		// Check if we have any channels with the peer (Currently we only provide the service to peers we have a channel with).
10510		if !peer_state.channel_by_id.values().any(|phase| phase.is_funded()) {
10511			log_debug!(logger, "Ignoring peer storage request from {} as we don't have any funded channels with them.", log_pubkey!(counterparty_node_id));
10512			return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(
10513				"Ignoring peer_storage message, as peer storage is currently supported only for \
10514				peers with an active funded channel.".into(),
10515			), ChannelId([0; 32])));
10516		}
10517
10518		#[cfg(not(test))]
10519		if msg.data.len() > MAX_PEER_STORAGE_SIZE {
10520			log_debug!(logger, "Sending warning to peer and ignoring peer storage request from {} as its over 1KiB", log_pubkey!(counterparty_node_id));
10521
10522			return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(
10523				format!("Supports only data up to {} bytes in peer storage.", MAX_PEER_STORAGE_SIZE)
10524			), ChannelId([0; 32])));
10525		}
10526
10527		log_trace!(logger, "Received peer_storage from {}", log_pubkey!(counterparty_node_id));
10528		peer_state.peer_storage = msg.data;
10529
10530		Ok(())
10531	}
10532
10533	#[rustfmt::skip]
10534	fn internal_funding_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), MsgHandleErrInternal> {
10535		let best_block = *self.best_block.read().unwrap();
10536		let per_peer_state = self.per_peer_state.read().unwrap();
10537		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
10538			.ok_or_else(|| {
10539				debug_assert!(false);
10540				MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), msg.channel_id)
10541			})?;
10542
10543		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10544		let peer_state = &mut *peer_state_lock;
10545		match peer_state.channel_by_id.entry(msg.channel_id) {
10546			hash_map::Entry::Occupied(mut chan_entry) => {
10547				let chan = chan_entry.get_mut();
10548				match chan
10549					.funding_signed(&msg, best_block, &self.signer_provider, &self.logger)
10550					.and_then(|(funded_chan, monitor)| {
10551						self.chain_monitor
10552							.watch_channel(funded_chan.context.channel_id(), monitor)
10553							.map_err(|()| {
10554								// We weren't able to watch the channel to begin with, so no
10555								// updates should be made on it. Previously, full_stack_target
10556								// found an (unreachable) panic when the monitor update contained
10557								// within `shutdown_finish` was applied.
10558								funded_chan.unset_funding_info();
10559								ChannelError::close("Channel ID was a duplicate".to_owned())
10560							})
10561							.map(|persist_status| (funded_chan, persist_status))
10562					})
10563				{
10564					Ok((funded_chan, persist_status)) => {
10565						handle_new_monitor_update!(self, persist_status, peer_state_lock, peer_state, per_peer_state, funded_chan, INITIAL_MONITOR);
10566						Ok(())
10567					},
10568					Err(e) => try_channel_entry!(self, peer_state, Err(e), chan_entry),
10569				}
10570			},
10571			hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
10572		}
10573	}
10574
10575	fn internal_tx_msg<
10576		HandleTxMsgFn: Fn(
10577			&mut Channel<SP>,
10578		) -> Result<InteractiveTxMessageSend, (ChannelError, Option<SpliceFundingFailed>)>,
10579	>(
10580		&self, counterparty_node_id: &PublicKey, channel_id: ChannelId,
10581		tx_msg_handler: HandleTxMsgFn,
10582	) -> Result<NotifyOption, MsgHandleErrInternal> {
10583		let per_peer_state = self.per_peer_state.read().unwrap();
10584		let peer_state_mutex = per_peer_state.get(counterparty_node_id).ok_or_else(|| {
10585			debug_assert!(false);
10586			MsgHandleErrInternal::send_err_msg_no_close(
10587				format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"),
10588				channel_id,
10589			)
10590		})?;
10591		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10592		let peer_state = &mut *peer_state_lock;
10593		match peer_state.channel_by_id.entry(channel_id) {
10594			hash_map::Entry::Occupied(mut chan_entry) => {
10595				let channel = chan_entry.get_mut();
10596				match tx_msg_handler(channel) {
10597					Ok(msg_send) => {
10598						let msg_send_event = msg_send.into_msg_send_event(*counterparty_node_id);
10599						peer_state.pending_msg_events.push(msg_send_event);
10600						Ok(NotifyOption::SkipPersistHandleEvents)
10601					},
10602					Err((error, splice_funding_failed)) => {
10603						if let Some(splice_funding_failed) = splice_funding_failed {
10604							let pending_events = &mut self.pending_events.lock().unwrap();
10605							pending_events.push_back((events::Event::SpliceFailed {
10606								channel_id,
10607								counterparty_node_id: *counterparty_node_id,
10608								user_channel_id: channel.context().get_user_id(),
10609								abandoned_funding_txo: splice_funding_failed.funding_txo,
10610								channel_type: splice_funding_failed.channel_type.clone(),
10611								contributed_inputs: splice_funding_failed.contributed_inputs,
10612								contributed_outputs: splice_funding_failed.contributed_outputs,
10613							}, None));
10614						}
10615						Err(MsgHandleErrInternal::from_chan_no_close(error, channel_id))
10616					},
10617				}
10618			},
10619			hash_map::Entry::Vacant(_) => {
10620				Err(MsgHandleErrInternal::send_err_msg_no_close(format!(
10621					"Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}",
10622					counterparty_node_id), channel_id)
10623				)
10624			}
10625		}
10626	}
10627
10628	fn internal_tx_add_input(
10629		&self, counterparty_node_id: PublicKey, msg: &msgs::TxAddInput,
10630	) -> Result<NotifyOption, MsgHandleErrInternal> {
10631		self.internal_tx_msg(&counterparty_node_id, msg.channel_id, |channel: &mut Channel<SP>| {
10632			channel.tx_add_input(msg, &self.logger)
10633		})
10634	}
10635
10636	fn internal_tx_add_output(
10637		&self, counterparty_node_id: PublicKey, msg: &msgs::TxAddOutput,
10638	) -> Result<NotifyOption, MsgHandleErrInternal> {
10639		self.internal_tx_msg(&counterparty_node_id, msg.channel_id, |channel: &mut Channel<SP>| {
10640			channel.tx_add_output(msg, &self.logger)
10641		})
10642	}
10643
10644	fn internal_tx_remove_input(
10645		&self, counterparty_node_id: PublicKey, msg: &msgs::TxRemoveInput,
10646	) -> Result<NotifyOption, MsgHandleErrInternal> {
10647		self.internal_tx_msg(&counterparty_node_id, msg.channel_id, |channel: &mut Channel<SP>| {
10648			channel.tx_remove_input(msg, &self.logger)
10649		})
10650	}
10651
10652	fn internal_tx_remove_output(
10653		&self, counterparty_node_id: PublicKey, msg: &msgs::TxRemoveOutput,
10654	) -> Result<NotifyOption, MsgHandleErrInternal> {
10655		self.internal_tx_msg(&counterparty_node_id, msg.channel_id, |channel: &mut Channel<SP>| {
10656			channel.tx_remove_output(msg, &self.logger)
10657		})
10658	}
10659
10660	#[rustfmt::skip]
10661	fn internal_tx_complete(&self, counterparty_node_id: PublicKey, msg: &msgs::TxComplete) -> Result<NotifyOption, MsgHandleErrInternal> {
10662		let per_peer_state = self.per_peer_state.read().unwrap();
10663		let peer_state_mutex = per_peer_state.get(&counterparty_node_id)
10664			.ok_or_else(|| {
10665				debug_assert!(false);
10666				MsgHandleErrInternal::send_err_msg_no_close(
10667					format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"),
10668					msg.channel_id)
10669			})?;
10670		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10671		let peer_state = &mut *peer_state_lock;
10672		match peer_state.channel_by_id.entry(msg.channel_id) {
10673			hash_map::Entry::Occupied(mut chan_entry) => {
10674				let chan = chan_entry.get_mut();
10675				match chan.tx_complete(msg, &self.logger) {
10676					Ok((interactive_tx_msg_send, commitment_signed)) => {
10677						let persist = if interactive_tx_msg_send.is_some() || commitment_signed.is_some() {
10678							NotifyOption::SkipPersistHandleEvents
10679						} else {
10680							NotifyOption::SkipPersistNoEvents
10681						};
10682						if let Some(interactive_tx_msg_send) = interactive_tx_msg_send {
10683							let msg_send_event = interactive_tx_msg_send.into_msg_send_event(counterparty_node_id);
10684							peer_state.pending_msg_events.push(msg_send_event);
10685						};
10686						if let Some(commitment_signed) = commitment_signed {
10687							peer_state.pending_msg_events.push(MessageSendEvent::UpdateHTLCs {
10688								node_id: counterparty_node_id,
10689								channel_id: msg.channel_id,
10690								updates: CommitmentUpdate {
10691									commitment_signed: vec![commitment_signed],
10692									update_add_htlcs: vec![],
10693									update_fulfill_htlcs: vec![],
10694									update_fail_htlcs: vec![],
10695									update_fail_malformed_htlcs: vec![],
10696									update_fee: None,
10697								},
10698							});
10699						}
10700						Ok(persist)
10701					},
10702					Err((error, splice_funding_failed)) => {
10703						if let Some(splice_funding_failed) = splice_funding_failed {
10704							let pending_events = &mut self.pending_events.lock().unwrap();
10705							pending_events.push_back((events::Event::SpliceFailed {
10706								channel_id: msg.channel_id,
10707								counterparty_node_id,
10708								user_channel_id: chan.context().get_user_id(),
10709								abandoned_funding_txo: splice_funding_failed.funding_txo,
10710								channel_type: splice_funding_failed.channel_type.clone(),
10711								contributed_inputs: splice_funding_failed.contributed_inputs,
10712								contributed_outputs: splice_funding_failed.contributed_outputs,
10713							}, None));
10714						}
10715						Err(MsgHandleErrInternal::from_chan_no_close(error, msg.channel_id))
10716					},
10717				}
10718			},
10719			hash_map::Entry::Vacant(_) => {
10720				Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
10721			}
10722		}
10723	}
10724
10725	#[rustfmt::skip]
10726	fn internal_tx_signatures(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxSignatures)
10727	-> Result<(), MsgHandleErrInternal> {
10728		let per_peer_state = self.per_peer_state.read().unwrap();
10729		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
10730			.ok_or_else(|| {
10731				debug_assert!(false);
10732				MsgHandleErrInternal::send_err_msg_no_close(
10733					format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"),
10734					msg.channel_id)
10735			})?;
10736		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10737		let peer_state = &mut *peer_state_lock;
10738		match peer_state.channel_by_id.entry(msg.channel_id) {
10739			hash_map::Entry::Occupied(mut chan_entry) => {
10740				match chan_entry.get_mut().as_funded_mut() {
10741					Some(chan) => {
10742						let best_block_height = self.best_block.read().unwrap().height;
10743						let FundingTxSigned {
10744							tx_signatures,
10745							funding_tx,
10746							splice_negotiated,
10747							splice_locked,
10748						} = try_channel_entry!(
10749							self,
10750							peer_state,
10751							chan.tx_signatures(msg, best_block_height, &self.logger),
10752							chan_entry
10753						);
10754						if let Some(tx_signatures) = tx_signatures {
10755							peer_state.pending_msg_events.push(MessageSendEvent::SendTxSignatures {
10756								node_id: *counterparty_node_id,
10757								msg: tx_signatures,
10758							});
10759						}
10760						if let Some(splice_locked) = splice_locked {
10761							peer_state.pending_msg_events.push(MessageSendEvent::SendSpliceLocked {
10762								node_id: *counterparty_node_id,
10763								msg: splice_locked,
10764							});
10765						}
10766						if let Some(ref funding_tx) = funding_tx {
10767							self.broadcast_interactive_funding(chan, funding_tx, &self.logger);
10768						}
10769						if let Some(splice_negotiated) = splice_negotiated {
10770							self.pending_events.lock().unwrap().push_back((
10771								events::Event::SplicePending {
10772									channel_id: msg.channel_id,
10773									counterparty_node_id: *counterparty_node_id,
10774									user_channel_id: chan.context.get_user_id(),
10775									new_funding_txo: splice_negotiated.funding_txo,
10776									channel_type: splice_negotiated.channel_type,
10777									new_funding_redeem_script: splice_negotiated.funding_redeem_script,
10778								},
10779								None,
10780							));
10781						}
10782					},
10783					None => {
10784						let msg = "Got an unexpected tx_signatures message";
10785						let reason = ClosureReason::ProcessingError { err: msg.to_owned() };
10786						let err = ChannelError::Close((msg.to_owned(), reason));
10787						try_channel_entry!(self, peer_state, Err(err), chan_entry)
10788					},
10789				}
10790				Ok(())
10791			},
10792			hash_map::Entry::Vacant(_) => {
10793				Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
10794			}
10795		}
10796	}
10797
10798	#[rustfmt::skip]
10799	fn internal_tx_abort(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxAbort)
10800	-> Result<NotifyOption, MsgHandleErrInternal> {
10801		let per_peer_state = self.per_peer_state.read().unwrap();
10802		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
10803			.ok_or_else(|| {
10804				debug_assert!(false);
10805				MsgHandleErrInternal::send_err_msg_no_close(
10806					format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"),
10807					msg.channel_id)
10808			})?;
10809		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10810		let peer_state = &mut *peer_state_lock;
10811		match peer_state.channel_by_id.entry(msg.channel_id) {
10812			hash_map::Entry::Occupied(mut chan_entry) => {
10813				let res = chan_entry.get_mut().tx_abort(msg, &self.logger);
10814				let (tx_abort, splice_failed) = try_channel_entry!(self, peer_state, res, chan_entry);
10815
10816				let persist = if tx_abort.is_some() || splice_failed.is_some() {
10817					NotifyOption::DoPersist
10818				} else {
10819					NotifyOption::SkipPersistNoEvents
10820				};
10821
10822				if let Some(tx_abort_msg) = tx_abort {
10823					peer_state.pending_msg_events.push(MessageSendEvent::SendTxAbort {
10824						node_id: *counterparty_node_id,
10825						msg: tx_abort_msg,
10826					});
10827				}
10828
10829				if let Some(splice_funding_failed) = splice_failed {
10830					let pending_events = &mut self.pending_events.lock().unwrap();
10831					pending_events.push_back((events::Event::SpliceFailed {
10832						channel_id: msg.channel_id,
10833						counterparty_node_id: *counterparty_node_id,
10834						user_channel_id: chan_entry.get().context().get_user_id(),
10835						abandoned_funding_txo: splice_funding_failed.funding_txo,
10836						channel_type: splice_funding_failed.channel_type,
10837						contributed_inputs: splice_funding_failed.contributed_inputs,
10838						contributed_outputs: splice_funding_failed.contributed_outputs,
10839					}, None));
10840				}
10841
10842				Ok(persist)
10843			},
10844			hash_map::Entry::Vacant(_) => {
10845				Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
10846			}
10847		}
10848	}
10849
10850	#[rustfmt::skip]
10851	fn internal_channel_ready(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReady) -> Result<(), MsgHandleErrInternal> {
10852		// Note that the ChannelManager is NOT re-persisted on disk after this (unless we error
10853		// closing a channel), so any changes are likely to be lost on restart!
10854		let per_peer_state = self.per_peer_state.read().unwrap();
10855		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
10856			.ok_or_else(|| {
10857				debug_assert!(false);
10858				MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), msg.channel_id)
10859			})?;
10860		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10861		let peer_state = &mut *peer_state_lock;
10862		match peer_state.channel_by_id.entry(msg.channel_id) {
10863			hash_map::Entry::Occupied(mut chan_entry) => {
10864				if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
10865					let logger = WithChannelContext::from(&self.logger, &chan.context, None);
10866					let res = chan.channel_ready(
10867						&msg,
10868						&self.node_signer,
10869						self.chain_hash,
10870						&self.config.read().unwrap(),
10871						&self.best_block.read().unwrap(),
10872						&&logger
10873					);
10874					let announcement_sigs_opt =
10875						try_channel_entry!(self, peer_state, res, chan_entry);
10876					if let Some(announcement_sigs) = announcement_sigs_opt {
10877						log_trace!(logger, "Sending announcement_signatures for channel {}", chan.context.channel_id());
10878						peer_state.pending_msg_events.push(MessageSendEvent::SendAnnouncementSignatures {
10879							node_id: counterparty_node_id.clone(),
10880							msg: announcement_sigs,
10881						});
10882					} else if chan.context.is_usable() {
10883						// If we're sending an announcement_signatures, we'll send the (public)
10884						// channel_update after sending a channel_announcement when we receive our
10885						// counterparty's announcement_signatures. Thus, we only bother to send a
10886						// channel_update here if the channel is not public, i.e. we're not sending an
10887						// announcement_signatures.
10888						log_trace!(logger, "Sending private initial channel_update for our counterparty on channel {}", chan.context.channel_id());
10889						if let Ok(msg) = self.get_channel_update_for_unicast(chan) {
10890							peer_state.pending_msg_events.push(MessageSendEvent::SendChannelUpdate {
10891								node_id: counterparty_node_id.clone(),
10892								msg,
10893							});
10894						}
10895					}
10896
10897					{
10898						let mut pending_events = self.pending_events.lock().unwrap();
10899						emit_initial_channel_ready_event!(pending_events, chan);
10900					}
10901
10902					Ok(())
10903				} else {
10904					try_channel_entry!(self, peer_state, Err(ChannelError::close(
10905						"Got a channel_ready message for an unfunded channel!".into())), chan_entry)
10906				}
10907			},
10908			hash_map::Entry::Vacant(_) => {
10909				Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
10910			}
10911		}
10912	}
10913
10914	fn internal_shutdown(
10915		&self, counterparty_node_id: &PublicKey, msg: &msgs::Shutdown,
10916	) -> Result<(), MsgHandleErrInternal> {
10917		let mut dropped_htlcs: Vec<(HTLCSource, PaymentHash)>;
10918		{
10919			let per_peer_state = self.per_peer_state.read().unwrap();
10920			let peer_state_mutex = per_peer_state.get(counterparty_node_id).ok_or_else(|| {
10921				debug_assert!(false);
10922				MsgHandleErrInternal::send_err_msg_no_close(
10923					format!(
10924						"Can't find a peer matching the passed counterparty node_id {}",
10925						counterparty_node_id
10926					),
10927					msg.channel_id,
10928				)
10929			})?;
10930			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10931			let peer_state = &mut *peer_state_lock;
10932			if let hash_map::Entry::Occupied(mut chan_entry) =
10933				peer_state.channel_by_id.entry(msg.channel_id.clone())
10934			{
10935				match chan_entry.get_mut().as_funded_mut() {
10936					Some(chan) => {
10937						if !chan.received_shutdown() {
10938							let logger =
10939								WithChannelContext::from(&self.logger, &chan.context, None);
10940							log_info!(logger, "Received a shutdown message from our counterparty for channel {}{}.",
10941								msg.channel_id,
10942								if chan.sent_shutdown() { " after we initiated shutdown" } else { "" });
10943						}
10944
10945						let funding_txo_opt = chan.funding.get_funding_txo();
10946						let (shutdown, monitor_update_opt, htlcs) = try_channel_entry!(
10947							self,
10948							peer_state,
10949							chan.shutdown(&self.signer_provider, &peer_state.latest_features, &msg),
10950							chan_entry
10951						);
10952						dropped_htlcs = htlcs;
10953
10954						if let Some(msg) = shutdown {
10955							// We can send the `shutdown` message before updating the `ChannelMonitor`
10956							// here as we don't need the monitor update to complete until we send a
10957							// `shutdown_signed`, which we'll delay if we're pending a monitor update.
10958							peer_state.pending_msg_events.push(MessageSendEvent::SendShutdown {
10959								node_id: *counterparty_node_id,
10960								msg,
10961							});
10962						}
10963						// Update the monitor with the shutdown script if necessary.
10964						if let Some(monitor_update) = monitor_update_opt {
10965							handle_new_monitor_update!(
10966								self,
10967								funding_txo_opt.unwrap(),
10968								monitor_update,
10969								peer_state_lock,
10970								peer_state,
10971								per_peer_state,
10972								chan
10973							);
10974						}
10975					},
10976					None => {
10977						let logger = WithChannelContext::from(
10978							&self.logger,
10979							chan_entry.get().context(),
10980							None,
10981						);
10982						log_error!(logger, "Immediately closing unfunded channel {} as peer asked to cooperatively shut it down (which is unnecessary)", &msg.channel_id);
10983						let reason = ClosureReason::CounterpartyCoopClosedUnfundedChannel;
10984						let err = ChannelError::Close((reason.to_string(), reason));
10985						let mut chan = chan_entry.remove();
10986						let (_, mut e) = convert_channel_err!(self, peer_state, err, &mut chan);
10987						e.dont_send_error_message();
10988						return Err(e);
10989					},
10990				}
10991			} else {
10992				return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id));
10993			}
10994		}
10995		for htlc_source in dropped_htlcs.drain(..) {
10996			let receiver = HTLCHandlingFailureType::Forward {
10997				node_id: Some(counterparty_node_id.clone()),
10998				channel_id: msg.channel_id,
10999			};
11000			let reason = HTLCFailReason::from_failure_code(LocalHTLCFailureReason::ChannelClosed);
11001			let (source, hash) = htlc_source;
11002			self.fail_htlc_backwards_internal(&source, &hash, &reason, receiver, None);
11003		}
11004
11005		Ok(())
11006	}
11007
11008	fn internal_closing_signed(
11009		&self, counterparty_node_id: &PublicKey, msg: &msgs::ClosingSigned,
11010	) -> Result<(), MsgHandleErrInternal> {
11011		let per_peer_state = self.per_peer_state.read().unwrap();
11012		let peer_state_mutex = per_peer_state.get(counterparty_node_id).ok_or_else(|| {
11013			debug_assert!(false);
11014			MsgHandleErrInternal::send_err_msg_no_close(
11015				format!(
11016					"Can't find a peer matching the passed counterparty node_id {}",
11017					counterparty_node_id
11018				),
11019				msg.channel_id,
11020			)
11021		})?;
11022		let logger;
11023		let tx_err: Option<(_, Result<Infallible, _>)> = {
11024			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11025			let peer_state = &mut *peer_state_lock;
11026			match peer_state.channel_by_id.entry(msg.channel_id.clone()) {
11027				hash_map::Entry::Occupied(mut chan_entry) => {
11028					if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
11029						logger = WithChannelContext::from(&self.logger, &chan.context, None);
11030						let res = chan.closing_signed(&self.fee_estimator, &msg, &&logger);
11031						let (closing_signed, tx_shutdown_result) =
11032							try_channel_entry!(self, peer_state, res, chan_entry);
11033						debug_assert_eq!(tx_shutdown_result.is_some(), chan.is_shutdown());
11034						if let Some(msg) = closing_signed {
11035							peer_state.pending_msg_events.push(MessageSendEvent::SendClosingSigned {
11036								node_id: counterparty_node_id.clone(),
11037								msg,
11038							});
11039						}
11040						if let Some((tx, close_res)) = tx_shutdown_result {
11041							// We're done with this channel, we've got a signed closing transaction and
11042							// will send the closing_signed back to the remote peer upon return. This
11043							// also implies there are no pending HTLCs left on the channel, so we can
11044							// fully delete it from tracking (the channel monitor is still around to
11045							// watch for old state broadcasts)!
11046							let err = convert_channel_err!(self, peer_state, close_res, chan, COOP_CLOSED);
11047							chan_entry.remove();
11048							Some((tx, Err(err)))
11049						} else {
11050							None
11051						}
11052					} else {
11053						return try_channel_entry!(self, peer_state, Err(ChannelError::close(
11054							"Got a closing_signed message for an unfunded channel!".into())), chan_entry);
11055					}
11056				},
11057				hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
11058			}
11059		};
11060		mem::drop(per_peer_state);
11061		if let Some((broadcast_tx, err)) = tx_err {
11062			log_info!(logger, "Broadcasting {}", log_tx!(broadcast_tx));
11063			self.tx_broadcaster.broadcast_transactions(&[&broadcast_tx]);
11064			let _ = handle_error!(self, err, *counterparty_node_id);
11065		}
11066		Ok(())
11067	}
11068
11069	#[cfg(simple_close)]
11070	fn internal_closing_complete(
11071		&self, _counterparty_node_id: PublicKey, _msg: msgs::ClosingComplete,
11072	) -> Result<(), MsgHandleErrInternal> {
11073		unimplemented!("Handling ClosingComplete is not implemented");
11074	}
11075
11076	#[cfg(simple_close)]
11077	fn internal_closing_sig(
11078		&self, _counterparty_node_id: PublicKey, _msg: msgs::ClosingSig,
11079	) -> Result<(), MsgHandleErrInternal> {
11080		unimplemented!("Handling ClosingSig is not implemented");
11081	}
11082
11083	#[rustfmt::skip]
11084	fn internal_update_add_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) -> Result<(), MsgHandleErrInternal> {
11085		//TODO: BOLT 4 points out a specific attack where a peer may re-send an onion packet and
11086		//determine the state of the payment based on our response/if we forward anything/the time
11087		//we take to respond. We should take care to avoid allowing such an attack.
11088		//
11089		//TODO: There exists a further attack where a node may garble the onion data, forward it to
11090		//us repeatedly garbled in different ways, and compare our error messages, which are
11091		//encrypted with the same key. It's not immediately obvious how to usefully exploit that,
11092		//but we should prevent it anyway.
11093
11094		// Note that the ChannelManager is NOT re-persisted on disk after this (unless we error
11095		// closing a channel), so any changes are likely to be lost on restart!
11096
11097		let per_peer_state = self.per_peer_state.read().unwrap();
11098		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
11099			.ok_or_else(|| {
11100				debug_assert!(false);
11101				MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), msg.channel_id)
11102			})?;
11103		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11104		let peer_state = &mut *peer_state_lock;
11105		match peer_state.channel_by_id.entry(msg.channel_id) {
11106			hash_map::Entry::Occupied(mut chan_entry) => {
11107				if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
11108					try_channel_entry!(self, peer_state, chan.update_add_htlc(&msg, &self.fee_estimator), chan_entry);
11109				} else {
11110					return try_channel_entry!(self, peer_state, Err(ChannelError::close(
11111						"Got an update_add_htlc message for an unfunded channel!".into())), chan_entry);
11112				}
11113			},
11114			hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
11115		}
11116		Ok(())
11117	}
11118
11119	fn internal_update_fulfill_htlc(
11120		&self, counterparty_node_id: &PublicKey, msg: msgs::UpdateFulfillHTLC,
11121	) -> Result<(), MsgHandleErrInternal> {
11122		let funding_txo;
11123		let next_user_channel_id;
11124		let (htlc_source, forwarded_htlc_value, skimmed_fee_msat, send_timestamp) = {
11125			let per_peer_state = self.per_peer_state.read().unwrap();
11126			let peer_state_mutex = per_peer_state.get(counterparty_node_id).ok_or_else(|| {
11127				debug_assert!(false);
11128				MsgHandleErrInternal::send_err_msg_no_close(
11129					format!(
11130						"Can't find a peer matching the passed counterparty node_id {}",
11131						counterparty_node_id
11132					),
11133					msg.channel_id,
11134				)
11135			})?;
11136			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11137			let peer_state = &mut *peer_state_lock;
11138			match peer_state.channel_by_id.entry(msg.channel_id) {
11139				hash_map::Entry::Occupied(mut chan_entry) => {
11140					if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
11141						let res = try_channel_entry!(self, peer_state, chan.update_fulfill_htlc(&msg), chan_entry);
11142						if let HTLCSource::PreviousHopData(prev_hop) = &res.0 {
11143							let logger = WithChannelContext::from(&self.logger, &chan.context, None);
11144							log_trace!(logger,
11145								"Holding the next revoke_and_ack from {} until the preimage is durably persisted in the inbound edge's ChannelMonitor",
11146								msg.channel_id);
11147							peer_state.actions_blocking_raa_monitor_updates.entry(msg.channel_id)
11148								.or_insert_with(Vec::new)
11149								.push(RAAMonitorUpdateBlockingAction::from_prev_hop_data(&prev_hop));
11150						}
11151						// Note that we do not need to push an `actions_blocking_raa_monitor_updates`
11152						// entry here, even though we *do* need to block the next RAA monitor update.
11153						// We do this instead in the `claim_funds_internal` by attaching a
11154						// `ReleaseRAAChannelMonitorUpdate` action to the event generated when the
11155						// outbound HTLC is claimed. This is guaranteed to all complete before we
11156						// process the RAA as messages are processed from single peers serially.
11157						funding_txo = chan.funding.get_funding_txo().expect("We won't accept a fulfill until funded");
11158						next_user_channel_id = chan.context.get_user_id();
11159						res
11160					} else {
11161						return try_channel_entry!(self, peer_state, Err(ChannelError::close(
11162							"Got an update_fulfill_htlc message for an unfunded channel!".into())), chan_entry);
11163					}
11164				},
11165				hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
11166			}
11167		};
11168		self.claim_funds_internal(
11169			htlc_source,
11170			msg.payment_preimage.clone(),
11171			Some(forwarded_htlc_value),
11172			skimmed_fee_msat,
11173			false,
11174			*counterparty_node_id,
11175			funding_txo,
11176			msg.channel_id,
11177			Some(next_user_channel_id),
11178			msg.attribution_data,
11179			send_timestamp,
11180		);
11181
11182		Ok(())
11183	}
11184
11185	#[rustfmt::skip]
11186	fn internal_update_fail_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) -> Result<(), MsgHandleErrInternal> {
11187		// Note that the ChannelManager is NOT re-persisted on disk after this (unless we error
11188		// closing a channel), so any changes are likely to be lost on restart!
11189		let per_peer_state = self.per_peer_state.read().unwrap();
11190		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
11191			.ok_or_else(|| {
11192				debug_assert!(false);
11193				MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), msg.channel_id)
11194			})?;
11195		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11196		let peer_state = &mut *peer_state_lock;
11197		match peer_state.channel_by_id.entry(msg.channel_id) {
11198			hash_map::Entry::Occupied(mut chan_entry) => {
11199				if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
11200					try_channel_entry!(self, peer_state, chan.update_fail_htlc(&msg, HTLCFailReason::from_msg(msg)), chan_entry);
11201				} else {
11202					return try_channel_entry!(self, peer_state, Err(ChannelError::close(
11203						"Got an update_fail_htlc message for an unfunded channel!".into())), chan_entry);
11204				}
11205			},
11206			hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
11207		}
11208		Ok(())
11209	}
11210
11211	#[rustfmt::skip]
11212	fn internal_update_fail_malformed_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), MsgHandleErrInternal> {
11213		// Note that the ChannelManager is NOT re-persisted on disk after this (unless we error
11214		// closing a channel), so any changes are likely to be lost on restart!
11215		let per_peer_state = self.per_peer_state.read().unwrap();
11216		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
11217			.ok_or_else(|| {
11218				debug_assert!(false);
11219				MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), msg.channel_id)
11220			})?;
11221		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11222		let peer_state = &mut *peer_state_lock;
11223		match peer_state.channel_by_id.entry(msg.channel_id) {
11224			hash_map::Entry::Occupied(mut chan_entry) => {
11225				if (msg.failure_code & 0x8000) == 0 {
11226					let chan_err = ChannelError::close("Got update_fail_malformed_htlc with BADONION not set".to_owned());
11227					try_channel_entry!(self, peer_state, Err(chan_err), chan_entry);
11228				}
11229				if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
11230					try_channel_entry!(self, peer_state, chan.update_fail_malformed_htlc(&msg, HTLCFailReason::reason(msg.failure_code.into(), msg.sha256_of_onion.to_vec())), chan_entry);
11231				} else {
11232					return try_channel_entry!(self, peer_state, Err(ChannelError::close(
11233						"Got an update_fail_malformed_htlc message for an unfunded channel!".into())), chan_entry);
11234				}
11235				Ok(())
11236			},
11237			hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
11238		}
11239	}
11240
11241	#[rustfmt::skip]
11242	fn internal_commitment_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::CommitmentSigned) -> Result<(), MsgHandleErrInternal> {
11243		let best_block = *self.best_block.read().unwrap();
11244		let per_peer_state = self.per_peer_state.read().unwrap();
11245		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
11246			.ok_or_else(|| {
11247				debug_assert!(false);
11248				MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), msg.channel_id)
11249			})?;
11250		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11251		let peer_state = &mut *peer_state_lock;
11252		match peer_state.channel_by_id.entry(msg.channel_id) {
11253			hash_map::Entry::Occupied(mut chan_entry) => {
11254				let chan = chan_entry.get_mut();
11255				let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
11256				let funding_txo = chan.funding().get_funding_txo();
11257				let (monitor_opt, monitor_update_opt) = try_channel_entry!(
11258					self, peer_state, chan.commitment_signed(msg, best_block, &self.signer_provider, &self.fee_estimator, &&logger),
11259					chan_entry);
11260
11261				if let Some(chan) = chan.as_funded_mut() {
11262					if let Some(monitor) = monitor_opt {
11263						let monitor_res = self.chain_monitor.watch_channel(monitor.channel_id(), monitor);
11264						if let Ok(persist_state) = monitor_res {
11265							handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
11266								per_peer_state, chan, INITIAL_MONITOR);
11267						} else {
11268							let logger = WithChannelContext::from(&self.logger, &chan.context, None);
11269							log_error!(logger, "Persisting initial ChannelMonitor failed, implying the channel ID was duplicated");
11270							let msg = "Channel ID was a duplicate";
11271							let reason = ClosureReason::ProcessingError { err: msg.to_owned() };
11272							let err = ChannelError::Close((msg.to_owned(), reason));
11273							try_channel_entry!(self, peer_state, Err(err), chan_entry)
11274						}
11275					} else if let Some(monitor_update) = monitor_update_opt {
11276						handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update, peer_state_lock,
11277							peer_state, per_peer_state, chan);
11278					}
11279				}
11280				Ok(())
11281			},
11282			hash_map::Entry::Vacant(_) => Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
11283		}
11284	}
11285
11286	#[rustfmt::skip]
11287	fn internal_commitment_signed_batch(&self, counterparty_node_id: &PublicKey, channel_id: ChannelId, batch: Vec<msgs::CommitmentSigned>) -> Result<(), MsgHandleErrInternal> {
11288		let per_peer_state = self.per_peer_state.read().unwrap();
11289		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
11290			.ok_or_else(|| {
11291				debug_assert!(false);
11292				MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), channel_id)
11293			})?;
11294		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11295		let peer_state = &mut *peer_state_lock;
11296		match peer_state.channel_by_id.entry(channel_id) {
11297			hash_map::Entry::Occupied(mut chan_entry) => {
11298				let chan = chan_entry.get_mut();
11299				let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
11300				let funding_txo = chan.funding().get_funding_txo();
11301				if let Some(chan) = chan.as_funded_mut() {
11302					let monitor_update_opt = try_channel_entry!(
11303						self, peer_state, chan.commitment_signed_batch(batch, &self.fee_estimator, &&logger), chan_entry
11304					);
11305
11306					if let Some(monitor_update) = monitor_update_opt {
11307						handle_new_monitor_update!(
11308							self, funding_txo.unwrap(), monitor_update, peer_state_lock, peer_state,
11309							per_peer_state, chan
11310						);
11311					}
11312				}
11313				Ok(())
11314			},
11315			hash_map::Entry::Vacant(_) => Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), channel_id))
11316		}
11317	}
11318
11319	fn push_decode_update_add_htlcs(&self, mut update_add_htlcs: (u64, Vec<msgs::UpdateAddHTLC>)) {
11320		let mut decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
11321		let src_outbound_scid_alias = update_add_htlcs.0;
11322		match decode_update_add_htlcs.entry(src_outbound_scid_alias) {
11323			hash_map::Entry::Occupied(mut e) => {
11324				e.get_mut().append(&mut update_add_htlcs.1);
11325			},
11326			hash_map::Entry::Vacant(e) => {
11327				e.insert(update_add_htlcs.1);
11328			},
11329		}
11330	}
11331
11332	#[inline]
11333	fn forward_htlcs(&self, per_source_pending_forwards: &mut [PerSourcePendingForward]) {
11334		for &mut (
11335			prev_outbound_scid_alias,
11336			prev_counterparty_node_id,
11337			prev_funding_outpoint,
11338			prev_channel_id,
11339			prev_user_channel_id,
11340			ref mut pending_forwards,
11341		) in per_source_pending_forwards
11342		{
11343			let mut new_intercept_events = VecDeque::new();
11344			let mut failed_intercept_forwards = Vec::new();
11345			if !pending_forwards.is_empty() {
11346				for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
11347					let scid = match forward_info.routing {
11348						PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
11349						PendingHTLCRouting::TrampolineForward { .. } => 0,
11350						PendingHTLCRouting::Receive { .. } => 0,
11351						PendingHTLCRouting::ReceiveKeysend { .. } => 0,
11352					};
11353					// Pull this now to avoid introducing a lock order with `forward_htlcs`.
11354					let is_our_scid = self.short_to_chan_info.read().unwrap().contains_key(&scid);
11355
11356					let payment_hash = forward_info.payment_hash;
11357					let logger = WithContext::from(
11358						&self.logger,
11359						None,
11360						Some(prev_channel_id),
11361						Some(payment_hash),
11362					);
11363					let pending_add = PendingAddHTLCInfo {
11364						prev_outbound_scid_alias,
11365						prev_counterparty_node_id,
11366						prev_funding_outpoint,
11367						prev_channel_id,
11368						prev_htlc_id,
11369						prev_user_channel_id,
11370						forward_info,
11371					};
11372					let mut fail_intercepted_htlc = |pending_add: PendingAddHTLCInfo| {
11373						let htlc_source =
11374							HTLCSource::PreviousHopData(pending_add.htlc_previous_hop_data());
11375						let reason = HTLCFailReason::from_failure_code(
11376							LocalHTLCFailureReason::UnknownNextPeer,
11377						);
11378						let failure_type = HTLCHandlingFailureType::InvalidForward {
11379							requested_forward_scid: scid,
11380						};
11381						failed_intercept_forwards.push((
11382							htlc_source,
11383							payment_hash,
11384							reason,
11385							failure_type,
11386						));
11387					};
11388
11389					// In the case that we have an HTLC that we're supposed to hold onto until the
11390					// recipient comes online *and* the outbound scid is encoded as
11391					// `fake_scid::is_valid_intercept`, we should first wait for the recipient to come
11392					// online before generating an `HTLCIntercepted` event, since the event cannot be
11393					// acted on until the recipient is online to cooperatively open the JIT channel. Once
11394					// we receive the `ReleaseHeldHtlc` message from the recipient, we will circle back
11395					// here and resume generating the event below.
11396					if pending_add.forward_info.routing.should_hold_htlc() {
11397						let intercept_id = InterceptId::from_htlc_id_and_chan_id(
11398							prev_htlc_id,
11399							&prev_channel_id,
11400							&prev_counterparty_node_id,
11401						);
11402						let mut held_htlcs = self.pending_intercepted_htlcs.lock().unwrap();
11403						match held_htlcs.entry(intercept_id) {
11404							hash_map::Entry::Vacant(entry) => {
11405								log_trace!(
11406									logger,
11407									"Intercepted held HTLC with id {}, holding until the recipient is online",
11408									intercept_id
11409								);
11410								entry.insert(pending_add);
11411							},
11412							hash_map::Entry::Occupied(_) => {
11413								debug_assert!(false, "Should never have two HTLCs with the same channel id and htlc id");
11414								fail_intercepted_htlc(pending_add);
11415							},
11416						}
11417					} else if !is_our_scid
11418						&& pending_add.forward_info.incoming_amt_msat.is_some()
11419						&& fake_scid::is_valid_intercept(
11420							&self.fake_scid_rand_bytes,
11421							scid,
11422							&self.chain_hash,
11423						) {
11424						let intercept_id = InterceptId::from_incoming_shared_secret(
11425							&pending_add.forward_info.incoming_shared_secret,
11426						);
11427						let mut pending_intercepts = self.pending_intercepted_htlcs.lock().unwrap();
11428						match pending_intercepts.entry(intercept_id) {
11429							hash_map::Entry::Vacant(entry) => {
11430								new_intercept_events.push_back((
11431									events::Event::HTLCIntercepted {
11432										requested_next_hop_scid: scid,
11433										payment_hash,
11434										inbound_amount_msat: pending_add
11435											.forward_info
11436											.incoming_amt_msat
11437											.unwrap(),
11438										expected_outbound_amount_msat: pending_add
11439											.forward_info
11440											.outgoing_amt_msat,
11441										intercept_id,
11442									},
11443									None,
11444								));
11445								entry.insert(pending_add);
11446							},
11447							hash_map::Entry::Occupied(_) => {
11448								log_info!(
11449											logger,
11450											"Failed to forward incoming HTLC: detected duplicate intercepted payment over short channel id {}",
11451											scid
11452										);
11453								fail_intercepted_htlc(pending_add);
11454							},
11455						}
11456					} else {
11457						match self.forward_htlcs.lock().unwrap().entry(scid) {
11458							hash_map::Entry::Occupied(mut entry) => {
11459								entry.get_mut().push(HTLCForwardInfo::AddHTLC(pending_add));
11460							},
11461							hash_map::Entry::Vacant(entry) => {
11462								entry.insert(vec![HTLCForwardInfo::AddHTLC(pending_add)]);
11463							},
11464						}
11465					}
11466				}
11467			}
11468
11469			for (htlc_source, payment_hash, failure_reason, destination) in
11470				failed_intercept_forwards.drain(..)
11471			{
11472				self.fail_htlc_backwards_internal(
11473					&htlc_source,
11474					&payment_hash,
11475					&failure_reason,
11476					destination,
11477					None,
11478				);
11479			}
11480
11481			if !new_intercept_events.is_empty() {
11482				let mut events = self.pending_events.lock().unwrap();
11483				events.append(&mut new_intercept_events);
11484			}
11485		}
11486	}
11487
11488	/// Checks whether [`ChannelMonitorUpdate`]s generated by the receipt of a remote
11489	/// [`msgs::RevokeAndACK`] should be held for the given channel until some other action
11490	/// completes. Note that this needs to happen in the same [`PeerState`] mutex as any release of
11491	/// the [`ChannelMonitorUpdate`] in question.
11492	#[rustfmt::skip]
11493	fn raa_monitor_updates_held(&self,
11494		actions_blocking_raa_monitor_updates: &BTreeMap<ChannelId, Vec<RAAMonitorUpdateBlockingAction>>,
11495		channel_id: ChannelId, counterparty_node_id: PublicKey,
11496	) -> bool {
11497		actions_blocking_raa_monitor_updates
11498			.get(&channel_id).map(|v| !v.is_empty()).unwrap_or(false)
11499		|| self.pending_events.lock().unwrap().iter().any(|(_, action)| {
11500			if let Some(EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
11501				channel_funding_outpoint: _,
11502				channel_id: ev_channel_id,
11503				counterparty_node_id: ev_counterparty_node_id
11504			}) = action {
11505				*ev_channel_id == channel_id && *ev_counterparty_node_id == counterparty_node_id
11506			} else {
11507				false
11508			}
11509		})
11510	}
11511
11512	#[cfg(any(test, feature = "_test_utils"))]
11513	pub(crate) fn test_raa_monitor_updates_held(
11514		&self, counterparty_node_id: PublicKey, channel_id: ChannelId,
11515	) -> bool {
11516		let per_peer_state = self.per_peer_state.read().unwrap();
11517		if let Some(peer_state_mtx) = per_peer_state.get(&counterparty_node_id) {
11518			let mut peer_state_lck = peer_state_mtx.lock().unwrap();
11519			let peer_state = &mut *peer_state_lck;
11520
11521			assert!(peer_state.channel_by_id.contains_key(&channel_id));
11522			return self.raa_monitor_updates_held(
11523				&peer_state.actions_blocking_raa_monitor_updates,
11524				channel_id,
11525				counterparty_node_id,
11526			);
11527		}
11528		false
11529	}
11530
11531	#[rustfmt::skip]
11532	fn internal_revoke_and_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), MsgHandleErrInternal> {
11533		let (htlcs_to_fail, static_invoices) = {
11534			let per_peer_state = self.per_peer_state.read().unwrap();
11535			let mut peer_state_lock = per_peer_state.get(counterparty_node_id)
11536				.ok_or_else(|| {
11537					debug_assert!(false);
11538					MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), msg.channel_id)
11539				}).map(|mtx| mtx.lock().unwrap())?;
11540			let peer_state = &mut *peer_state_lock;
11541			match peer_state.channel_by_id.entry(msg.channel_id) {
11542				hash_map::Entry::Occupied(mut chan_entry) => {
11543					if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
11544						let logger = WithChannelContext::from(&self.logger, &chan.context, None);
11545						let funding_txo_opt = chan.funding.get_funding_txo();
11546						let mon_update_blocked = self.raa_monitor_updates_held(
11547							&peer_state.actions_blocking_raa_monitor_updates, msg.channel_id,
11548							*counterparty_node_id);
11549						let (htlcs_to_fail, static_invoices, monitor_update_opt) = try_channel_entry!(self, peer_state,
11550							chan.revoke_and_ack(&msg, &self.fee_estimator, &&logger, mon_update_blocked), chan_entry);
11551						if let Some(monitor_update) = monitor_update_opt {
11552							let funding_txo = funding_txo_opt
11553								.expect("Funding outpoint must have been set for RAA handling to succeed");
11554							handle_new_monitor_update!(self, funding_txo, monitor_update,
11555								peer_state_lock, peer_state, per_peer_state, chan);
11556						}
11557						(htlcs_to_fail, static_invoices)
11558					} else {
11559						return try_channel_entry!(self, peer_state, Err(ChannelError::close(
11560							"Got a revoke_and_ack message for an unfunded channel!".into())), chan_entry);
11561					}
11562				},
11563				hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
11564			}
11565		};
11566		self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id, counterparty_node_id);
11567		for (static_invoice, reply_path) in static_invoices {
11568			let res = self.flow.enqueue_held_htlc_available(&static_invoice, HeldHtlcReplyPath::ToCounterparty { path: reply_path });
11569			debug_assert!(res.is_ok(), "enqueue_held_htlc_available can only fail for non-async senders");
11570		}
11571		Ok(())
11572	}
11573
11574	#[rustfmt::skip]
11575	fn internal_update_fee(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFee) -> Result<(), MsgHandleErrInternal> {
11576		let per_peer_state = self.per_peer_state.read().unwrap();
11577		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
11578			.ok_or_else(|| {
11579				debug_assert!(false);
11580				MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), msg.channel_id)
11581			})?;
11582		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11583		let peer_state = &mut *peer_state_lock;
11584		match peer_state.channel_by_id.entry(msg.channel_id) {
11585			hash_map::Entry::Occupied(mut chan_entry) => {
11586				if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
11587					let logger = WithChannelContext::from(&self.logger, &chan.context, None);
11588					try_channel_entry!(self, peer_state, chan.update_fee(&self.fee_estimator, &msg, &&logger), chan_entry);
11589				} else {
11590					return try_channel_entry!(self, peer_state, Err(ChannelError::close(
11591						"Got an update_fee message for an unfunded channel!".into())), chan_entry);
11592				}
11593			},
11594			hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
11595		}
11596		Ok(())
11597	}
11598
11599	#[rustfmt::skip]
11600	fn internal_stfu(&self, counterparty_node_id: &PublicKey, msg: &msgs::Stfu) -> Result<bool, MsgHandleErrInternal> {
11601		let per_peer_state = self.per_peer_state.read().unwrap();
11602		let peer_state_mutex = per_peer_state.get(counterparty_node_id).ok_or_else(|| {
11603			debug_assert!(false);
11604			MsgHandleErrInternal::send_err_msg_no_close(
11605				format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"),
11606				msg.channel_id
11607			)
11608		})?;
11609		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11610		let peer_state = &mut *peer_state_lock;
11611
11612		if !self.init_features().supports_quiescence() {
11613			return Err(MsgHandleErrInternal::from_chan_no_close(
11614				ChannelError::Warn("Quiescense not supported".to_string()), msg.channel_id
11615			));
11616		}
11617
11618		match peer_state.channel_by_id.entry(msg.channel_id) {
11619			hash_map::Entry::Occupied(mut chan_entry) => {
11620				if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
11621					let logger = WithContext::from(
11622						&self.logger, Some(*counterparty_node_id), Some(msg.channel_id), None
11623					);
11624
11625					let res = chan.stfu(&msg, &&logger);
11626					let resp = try_channel_entry!(self, peer_state, res, chan_entry);
11627					match resp {
11628						None => Ok(false),
11629						Some(StfuResponse::Stfu(msg)) => {
11630							peer_state.pending_msg_events.push(MessageSendEvent::SendStfu {
11631								node_id: *counterparty_node_id,
11632								msg,
11633							});
11634							Ok(true)
11635						},
11636						Some(StfuResponse::SpliceInit(msg)) => {
11637							peer_state.pending_msg_events.push(MessageSendEvent::SendSpliceInit {
11638								node_id: *counterparty_node_id,
11639								msg,
11640							});
11641							Ok(true)
11642						},
11643					}
11644				} else {
11645					let msg = "Peer sent `stfu` for an unfunded channel";
11646					let err = Err(ChannelError::Close(
11647						(msg.into(), ClosureReason::ProcessingError { err: msg.into() })
11648					));
11649					return try_channel_entry!(self, peer_state, err, chan_entry);
11650				}
11651			},
11652			hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(
11653				format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id),
11654				msg.channel_id
11655			))
11656		}
11657	}
11658
11659	#[rustfmt::skip]
11660	fn internal_announcement_signatures(&self, counterparty_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) -> Result<(), MsgHandleErrInternal> {
11661		let per_peer_state = self.per_peer_state.read().unwrap();
11662		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
11663			.ok_or_else(|| {
11664				debug_assert!(false);
11665				MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), msg.channel_id)
11666			})?;
11667		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11668		let peer_state = &mut *peer_state_lock;
11669		match peer_state.channel_by_id.entry(msg.channel_id) {
11670			hash_map::Entry::Occupied(mut chan_entry) => {
11671				if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
11672					if !chan.context.is_usable() {
11673						return Err(MsgHandleErrInternal::from_no_close(LightningError{err: "Got an announcement_signatures before we were ready for it".to_owned(), action: msgs::ErrorAction::IgnoreError}));
11674					}
11675
11676					let cur_height = self.best_block.read().unwrap().height;
11677					let res = chan.announcement_signatures(
11678						&self.node_signer,
11679						self.chain_hash,
11680						cur_height,
11681						msg,
11682						&self.config.read().unwrap(),
11683					);
11684					peer_state.pending_msg_events.push(MessageSendEvent::BroadcastChannelAnnouncement {
11685						msg: try_channel_entry!(self, peer_state, res, chan_entry),
11686						// Note that announcement_signatures fails if the channel cannot be announced,
11687						// so get_channel_update_for_broadcast will never fail by the time we get here.
11688						update_msg: Some(self.get_channel_update_for_broadcast(chan).unwrap()),
11689					});
11690				} else {
11691					return try_channel_entry!(self, peer_state, Err(ChannelError::close(
11692						"Got an announcement_signatures message for an unfunded channel!".into())), chan_entry);
11693				}
11694			},
11695			hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
11696		}
11697		Ok(())
11698	}
11699
11700	/// Returns DoPersist if anything changed, otherwise either SkipPersistNoEvents or an Err.
11701	#[rustfmt::skip]
11702	fn internal_channel_update(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelUpdate) -> Result<NotifyOption, MsgHandleErrInternal> {
11703		let (chan_counterparty_node_id, chan_id) = match self.short_to_chan_info.read().unwrap().get(&msg.contents.short_channel_id) {
11704			Some((cp_id, chan_id)) => (cp_id.clone(), chan_id.clone()),
11705			None => {
11706				// It's not a local channel
11707				return Ok(NotifyOption::SkipPersistNoEvents)
11708			}
11709		};
11710		let per_peer_state = self.per_peer_state.read().unwrap();
11711		let peer_state_mutex_opt = per_peer_state.get(&chan_counterparty_node_id);
11712		if peer_state_mutex_opt.is_none() {
11713			return Ok(NotifyOption::SkipPersistNoEvents)
11714		}
11715		let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
11716		let peer_state = &mut *peer_state_lock;
11717		match peer_state.channel_by_id.entry(chan_id) {
11718			hash_map::Entry::Occupied(mut chan_entry) => {
11719				if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
11720					if chan.context.get_counterparty_node_id() != *counterparty_node_id {
11721						if chan.context.should_announce() {
11722							// If the announcement is about a channel of ours which is public, some
11723							// other peer may simply be forwarding all its gossip to us. Don't provide
11724							// a scary-looking error message and return Ok instead.
11725							return Ok(NotifyOption::SkipPersistNoEvents);
11726						}
11727						return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a channel_update for a channel from the wrong node - it shouldn't know about our private channels!".to_owned(), chan_id));
11728					}
11729					let were_node_one = self.get_our_node_id().serialize()[..] < chan.context.get_counterparty_node_id().serialize()[..];
11730					let msg_from_node_one = msg.contents.channel_flags & 1 == 0;
11731					if were_node_one == msg_from_node_one {
11732						return Ok(NotifyOption::SkipPersistNoEvents);
11733					} else {
11734						let logger = WithChannelContext::from(&self.logger, &chan.context, None);
11735						log_debug!(logger, "Received channel_update {:?} for channel {}.", msg, chan_id);
11736						let did_change = try_channel_entry!(self, peer_state, chan.channel_update(&msg), chan_entry);
11737						// If nothing changed after applying their update, we don't need to bother
11738						// persisting.
11739						if !did_change {
11740							return Ok(NotifyOption::SkipPersistNoEvents);
11741						}
11742					}
11743				} else {
11744					return try_channel_entry!(self, peer_state, Err(ChannelError::close(
11745						"Got a channel_update for an unfunded channel!".into())), chan_entry);
11746				}
11747			},
11748			hash_map::Entry::Vacant(_) => return Ok(NotifyOption::SkipPersistNoEvents)
11749		}
11750		Ok(NotifyOption::DoPersist)
11751	}
11752
11753	#[rustfmt::skip]
11754	fn internal_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(), MsgHandleErrInternal> {
11755		let (inferred_splice_locked, need_lnd_workaround) = {
11756			let per_peer_state = self.per_peer_state.read().unwrap();
11757
11758			let peer_state_mutex = per_peer_state.get(counterparty_node_id)
11759				.ok_or_else(|| {
11760					debug_assert!(false);
11761					MsgHandleErrInternal::send_err_msg_no_close(
11762						format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"),
11763						msg.channel_id
11764					)
11765				})?;
11766			let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), Some(msg.channel_id), None);
11767			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11768			let peer_state = &mut *peer_state_lock;
11769			match peer_state.channel_by_id.entry(msg.channel_id) {
11770				hash_map::Entry::Occupied(mut chan_entry) => {
11771					if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
11772						// Currently, we expect all holding cell update_adds to be dropped on peer
11773						// disconnect, so Channel's reestablish will never hand us any holding cell
11774						// freed HTLCs to fail backwards. If in the future we no longer drop pending
11775						// add-HTLCs on disconnect, we may be handed HTLCs to fail backwards here.
11776						let outbound_scid_alias = chan.context.outbound_scid_alias();
11777						let res = chan.channel_reestablish(
11778							msg,
11779							&&logger,
11780							&self.node_signer,
11781							self.chain_hash,
11782							&self.config.read().unwrap(),
11783							&*self.best_block.read().unwrap(),
11784							|htlc_id| self.path_for_release_held_htlc(htlc_id, outbound_scid_alias, &msg.channel_id, counterparty_node_id)
11785						);
11786						let responses = try_channel_entry!(self, peer_state, res, chan_entry);
11787						let mut channel_update = None;
11788						if let Some(msg) = responses.shutdown_msg {
11789							peer_state.pending_msg_events.push(MessageSendEvent::SendShutdown {
11790								node_id: counterparty_node_id.clone(),
11791								msg,
11792							});
11793						} else if chan.context.is_usable() {
11794							// If the channel is in a usable state (ie the channel is not being shut
11795							// down), send a unicast channel_update to our counterparty to make sure
11796							// they have the latest channel parameters.
11797							if let Ok(msg) = self.get_channel_update_for_unicast(chan) {
11798								channel_update = Some(MessageSendEvent::SendChannelUpdate {
11799									node_id: chan.context.get_counterparty_node_id(),
11800									msg,
11801								});
11802							}
11803						}
11804						let need_lnd_workaround = chan.context.workaround_lnd_bug_4006.take();
11805						let (htlc_forwards, decode_update_add_htlcs) = self.handle_channel_resumption(
11806							&mut peer_state.pending_msg_events, chan, responses.raa, responses.commitment_update, responses.commitment_order,
11807							Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs,
11808							responses.tx_signatures, responses.tx_abort, responses.channel_ready_order,
11809						);
11810						debug_assert!(htlc_forwards.is_none());
11811						debug_assert!(decode_update_add_htlcs.is_none());
11812						if let Some(upd) = channel_update {
11813							peer_state.pending_msg_events.push(upd);
11814						}
11815
11816						(responses.inferred_splice_locked, need_lnd_workaround)
11817					} else {
11818						return try_channel_entry!(self, peer_state, Err(ChannelError::close(
11819							"Got a channel_reestablish message for an unfunded channel!".into())), chan_entry);
11820					}
11821				},
11822				hash_map::Entry::Vacant(_) => {
11823					log_debug!(logger, "Sending bogus ChannelReestablish for unknown channel {} to force channel closure",
11824						msg.channel_id);
11825					// Unfortunately, lnd doesn't force close on errors
11826					// (https://github.com/lightningnetwork/lnd/blob/abb1e3463f3a83bbb843d5c399869dbe930ad94f/htlcswitch/link.go#L2119).
11827					// One of the few ways to get an lnd counterparty to force close is by
11828					// replicating what they do when restoring static channel backups (SCBs). They
11829					// send an invalid `ChannelReestablish` with `0` commitment numbers and an
11830					// invalid `your_last_per_commitment_secret`.
11831					//
11832					// Since we received a `ChannelReestablish` for a channel that doesn't exist, we
11833					// can assume it's likely the channel closed from our point of view, but it
11834					// remains open on the counterparty's side. By sending this bogus
11835					// `ChannelReestablish` message now as a response to theirs, we trigger them to
11836					// force close broadcasting their latest state. If the closing transaction from
11837					// our point of view remains unconfirmed, it'll enter a race with the
11838					// counterparty's to-be-broadcast latest commitment transaction.
11839					peer_state.pending_msg_events.push(MessageSendEvent::SendChannelReestablish {
11840						node_id: *counterparty_node_id,
11841						msg: msgs::ChannelReestablish {
11842							channel_id: msg.channel_id,
11843							next_local_commitment_number: 0,
11844							next_remote_commitment_number: 0,
11845							your_last_per_commitment_secret: [1u8; 32],
11846							my_current_per_commitment_point: PublicKey::from_slice(&[2u8; 33]).unwrap(),
11847							next_funding: None,
11848							my_current_funding_locked: None,
11849						},
11850					});
11851					return Err(MsgHandleErrInternal::send_err_msg_no_close(
11852						format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}",
11853							counterparty_node_id), msg.channel_id)
11854					)
11855				}
11856			}
11857		};
11858
11859		if let Some(channel_ready_msg) = need_lnd_workaround {
11860			self.internal_channel_ready(counterparty_node_id, &channel_ready_msg)?;
11861		}
11862
11863		if let Some(splice_locked) = inferred_splice_locked {
11864			self.internal_splice_locked(counterparty_node_id, &splice_locked)?;
11865		}
11866
11867		Ok(())
11868	}
11869
11870	/// Handle incoming splice request, transition channel to splice-pending (unless some check fails).
11871	#[rustfmt::skip]
11872	fn internal_splice_init(&self, counterparty_node_id: &PublicKey, msg: &msgs::SpliceInit) -> Result<(), MsgHandleErrInternal> {
11873		let per_peer_state = self.per_peer_state.read().unwrap();
11874		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
11875			.ok_or_else(|| {
11876				debug_assert!(false);
11877				MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), msg.channel_id)
11878			})?;
11879		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11880		let peer_state = &mut *peer_state_lock;
11881
11882		// TODO(splicing): Currently not possible to contribute on the splicing-acceptor side
11883		let our_funding_contribution = 0i64;
11884
11885		// Look for the channel
11886		match peer_state.channel_by_id.entry(msg.channel_id) {
11887			hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!(
11888					"Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}, channel_id {}",
11889					counterparty_node_id, msg.channel_id,
11890				), msg.channel_id)),
11891			hash_map::Entry::Occupied(mut chan_entry) => {
11892				if self.config.read().unwrap().reject_inbound_splices {
11893					let err = ChannelError::WarnAndDisconnect(
11894						"Inbound channel splices are currently not allowed".to_owned()
11895					);
11896					return Err(MsgHandleErrInternal::from_chan_no_close(err, msg.channel_id));
11897				}
11898
11899				if let Some(ref mut funded_channel) = chan_entry.get_mut().as_funded_mut() {
11900					let init_res = funded_channel.splice_init(
11901						msg, our_funding_contribution, &self.signer_provider, &self.entropy_source,
11902						&self.get_our_node_id(), &self.logger
11903					);
11904					let splice_ack_msg = try_channel_entry!(self, peer_state, init_res, chan_entry);
11905					peer_state.pending_msg_events.push(MessageSendEvent::SendSpliceAck {
11906						node_id: *counterparty_node_id,
11907						msg: splice_ack_msg,
11908					});
11909					Ok(())
11910				} else {
11911					try_channel_entry!(self, peer_state, Err(ChannelError::close("Channel is not funded, cannot be spliced".into())), chan_entry)
11912				}
11913			},
11914		}
11915	}
11916
11917	/// Handle incoming splice request ack, transition channel to splice-pending (unless some check fails).
11918	#[rustfmt::skip]
11919	fn internal_splice_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::SpliceAck) -> Result<(), MsgHandleErrInternal> {
11920		let per_peer_state = self.per_peer_state.read().unwrap();
11921		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
11922			.ok_or_else(|| {
11923				debug_assert!(false);
11924				MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}"), msg.channel_id)
11925			})?;
11926		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11927		let peer_state = &mut *peer_state_lock;
11928
11929		// Look for the channel
11930		match peer_state.channel_by_id.entry(msg.channel_id) {
11931			hash_map::Entry::Vacant(_) => Err(MsgHandleErrInternal::send_err_msg_no_close(format!(
11932					"Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}",
11933					counterparty_node_id
11934				), msg.channel_id)),
11935			hash_map::Entry::Occupied(mut chan_entry) => {
11936				if let Some(ref mut funded_channel) = chan_entry.get_mut().as_funded_mut() {
11937					let splice_ack_res = funded_channel.splice_ack(
11938						msg, &self.signer_provider, &self.entropy_source,
11939						&self.get_our_node_id(), &self.logger
11940					);
11941					let tx_msg_opt = try_channel_entry!(self, peer_state, splice_ack_res, chan_entry);
11942					if let Some(tx_msg) = tx_msg_opt {
11943						peer_state.pending_msg_events.push(tx_msg.into_msg_send_event(counterparty_node_id.clone()));
11944					}
11945					Ok(())
11946				} else {
11947					try_channel_entry!(self, peer_state, Err(ChannelError::close("Channel is not funded, cannot be spliced".into())), chan_entry)
11948				}
11949			},
11950		}
11951	}
11952
11953	fn internal_splice_locked(
11954		&self, counterparty_node_id: &PublicKey, msg: &msgs::SpliceLocked,
11955	) -> Result<(), MsgHandleErrInternal> {
11956		let per_peer_state = self.per_peer_state.read().unwrap();
11957		let peer_state_mutex = per_peer_state.get(counterparty_node_id).ok_or_else(|| {
11958			debug_assert!(false);
11959			MsgHandleErrInternal::send_err_msg_no_close(
11960				format!(
11961					"Can't find a peer matching the passed counterparty node_id {}",
11962					counterparty_node_id
11963				),
11964				msg.channel_id,
11965			)
11966		})?;
11967		let mut peer_state_lock = peer_state_mutex.lock().unwrap();
11968		let peer_state = &mut *peer_state_lock;
11969
11970		// Look for the channel
11971		match peer_state.channel_by_id.entry(msg.channel_id) {
11972			hash_map::Entry::Vacant(_) => {
11973				let err = format!(
11974					"Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}",
11975					counterparty_node_id,
11976				);
11977				return Err(MsgHandleErrInternal::send_err_msg_no_close(err, msg.channel_id));
11978			},
11979			hash_map::Entry::Occupied(mut chan_entry) => {
11980				if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
11981					let logger = WithChannelContext::from(&self.logger, &chan.context, None);
11982					let result = chan.splice_locked(
11983						msg,
11984						&self.node_signer,
11985						self.chain_hash,
11986						&self.config.read().unwrap(),
11987						self.best_block.read().unwrap().height,
11988						&&logger,
11989					);
11990					let splice_promotion = try_channel_entry!(self, peer_state, result, chan_entry);
11991					if let Some(splice_promotion) = splice_promotion {
11992						{
11993							let mut short_to_chan_info = self.short_to_chan_info.write().unwrap();
11994							insert_short_channel_id!(short_to_chan_info, chan);
11995						}
11996
11997						{
11998							let mut pending_events = self.pending_events.lock().unwrap();
11999							pending_events.push_back((
12000								events::Event::ChannelReady {
12001									channel_id: chan.context.channel_id(),
12002									user_channel_id: chan.context.get_user_id(),
12003									counterparty_node_id: chan.context.get_counterparty_node_id(),
12004									funding_txo: Some(
12005										splice_promotion.funding_txo.into_bitcoin_outpoint(),
12006									),
12007									channel_type: chan.funding.get_channel_type().clone(),
12008								},
12009								None,
12010							));
12011							splice_promotion.discarded_funding.into_iter().for_each(
12012								|funding_info| {
12013									let event = Event::DiscardFunding {
12014										channel_id: chan.context.channel_id(),
12015										funding_info,
12016									};
12017									pending_events.push_back((event, None));
12018								},
12019							);
12020						}
12021
12022						if let Some(announcement_sigs) = splice_promotion.announcement_sigs {
12023							log_trace!(
12024								logger,
12025								"Sending announcement_signatures for channel {}",
12026								chan.context.channel_id()
12027							);
12028							peer_state.pending_msg_events.push(
12029								MessageSendEvent::SendAnnouncementSignatures {
12030									node_id: counterparty_node_id.clone(),
12031									msg: announcement_sigs,
12032								},
12033							);
12034						}
12035
12036						if let Some(monitor_update) = splice_promotion.monitor_update {
12037							handle_new_monitor_update!(
12038								self,
12039								splice_promotion.funding_txo,
12040								monitor_update,
12041								peer_state_lock,
12042								peer_state,
12043								per_peer_state,
12044								chan
12045							);
12046						}
12047					}
12048				} else {
12049					return Err(MsgHandleErrInternal::send_err_msg_no_close(
12050						"Channel is not funded, cannot splice".to_owned(),
12051						msg.channel_id,
12052					));
12053				}
12054			},
12055		};
12056
12057		Ok(())
12058	}
12059
12060	/// Process pending events from the [`chain::Watch`], returning whether any events were processed.
12061	fn process_pending_monitor_events(&self) -> bool {
12062		debug_assert!(self.total_consistency_lock.try_write().is_err()); // Caller holds read lock
12063
12064		let mut failed_channels: Vec<(Result<Infallible, _>, _)> = Vec::new();
12065		let mut pending_monitor_events = self.chain_monitor.release_pending_monitor_events();
12066		let has_pending_monitor_events = !pending_monitor_events.is_empty();
12067		for (funding_outpoint, channel_id, mut monitor_events, counterparty_node_id) in
12068			pending_monitor_events.drain(..)
12069		{
12070			for monitor_event in monitor_events.drain(..) {
12071				match monitor_event {
12072					MonitorEvent::HTLCEvent(htlc_update) => {
12073						let logger = WithContext::from(
12074							&self.logger,
12075							Some(counterparty_node_id),
12076							Some(channel_id),
12077							Some(htlc_update.payment_hash),
12078						);
12079						if let Some(preimage) = htlc_update.payment_preimage {
12080							log_trace!(
12081								logger,
12082								"Claiming HTLC with preimage {} from our monitor",
12083								preimage
12084							);
12085							// Claim the funds from the previous hop, if there is one. Because this is in response to a
12086							// chain event, no attribution data is available.
12087							self.claim_funds_internal(
12088								htlc_update.source,
12089								preimage,
12090								htlc_update.htlc_value_satoshis.map(|v| v * 1000),
12091								None,
12092								true,
12093								counterparty_node_id,
12094								funding_outpoint,
12095								channel_id,
12096								None,
12097								None,
12098								None,
12099							);
12100						} else {
12101							log_trace!(
12102								logger,
12103								"Failing HTLC with hash {} from our monitor",
12104								&htlc_update.payment_hash
12105							);
12106							let failure_reason = LocalHTLCFailureReason::OnChainTimeout;
12107							let receiver = HTLCHandlingFailureType::Forward {
12108								node_id: Some(counterparty_node_id),
12109								channel_id,
12110							};
12111							let reason = HTLCFailReason::from_failure_code(failure_reason);
12112							let completion_update = Some(PaymentCompleteUpdate {
12113								counterparty_node_id,
12114								channel_funding_outpoint: funding_outpoint,
12115								channel_id,
12116								htlc_id: SentHTLCId::from_source(&htlc_update.source),
12117							});
12118							self.fail_htlc_backwards_internal(
12119								&htlc_update.source,
12120								&htlc_update.payment_hash,
12121								&reason,
12122								receiver,
12123								completion_update,
12124							);
12125						}
12126					},
12127					MonitorEvent::HolderForceClosed(_)
12128					| MonitorEvent::HolderForceClosedWithInfo { .. } => {
12129						let per_peer_state = self.per_peer_state.read().unwrap();
12130						if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
12131							let mut peer_state_lock = peer_state_mutex.lock().unwrap();
12132							let peer_state = &mut *peer_state_lock;
12133							if let hash_map::Entry::Occupied(chan_entry) =
12134								peer_state.channel_by_id.entry(channel_id)
12135							{
12136								let reason = if let MonitorEvent::HolderForceClosedWithInfo {
12137									reason,
12138									..
12139								} = monitor_event
12140								{
12141									reason
12142								} else {
12143									ClosureReason::HolderForceClosed {
12144										broadcasted_latest_txn: Some(true),
12145										message: "Legacy ChannelMonitor closure".to_owned(),
12146									}
12147								};
12148								let err = ChannelError::Close((reason.to_string(), reason));
12149								let mut chan = chan_entry.remove();
12150								let (_, e) = convert_channel_err!(self, peer_state, err, &mut chan);
12151								failed_channels.push((Err(e), counterparty_node_id));
12152							}
12153						}
12154					},
12155					MonitorEvent::CommitmentTxConfirmed(_) => {
12156						let per_peer_state = self.per_peer_state.read().unwrap();
12157						if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
12158							let mut peer_state_lock = peer_state_mutex.lock().unwrap();
12159							let peer_state = &mut *peer_state_lock;
12160							if let hash_map::Entry::Occupied(chan_entry) =
12161								peer_state.channel_by_id.entry(channel_id)
12162							{
12163								let reason = ClosureReason::CommitmentTxConfirmed;
12164								let err = ChannelError::Close((reason.to_string(), reason));
12165								let mut chan = chan_entry.remove();
12166								let (_, e) = convert_channel_err!(self, peer_state, err, &mut chan);
12167								failed_channels.push((Err(e), counterparty_node_id));
12168							}
12169						}
12170					},
12171					MonitorEvent::Completed { channel_id, monitor_update_id, .. } => {
12172						self.channel_monitor_updated(
12173							&channel_id,
12174							Some(monitor_update_id),
12175							&counterparty_node_id,
12176						);
12177					},
12178				}
12179			}
12180		}
12181
12182		for (err, counterparty_node_id) in failed_channels {
12183			let _ = handle_error!(self, err, counterparty_node_id);
12184		}
12185
12186		has_pending_monitor_events
12187	}
12188
12189	/// Check the holding cell in each channel and free any pending HTLCs in them if possible.
12190	/// Returns whether there were any updates such as if pending HTLCs were freed or a monitor
12191	/// update was applied.
12192	fn check_free_holding_cells(&self) -> bool {
12193		let mut has_monitor_update = false;
12194		let mut failed_htlcs = Vec::new();
12195
12196		// Walk our list of channels and find any that need to update. Note that when we do find an
12197		// update, if it includes actions that must be taken afterwards, we have to drop the
12198		// per-peer state lock as well as the top level per_peer_state lock. Thus, we loop until we
12199		// manage to go through all our peers without finding a single channel to update.
12200		'peer_loop: loop {
12201			let per_peer_state = self.per_peer_state.read().unwrap();
12202			for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
12203				'chan_loop: loop {
12204					let mut peer_state_lock = peer_state_mutex.lock().unwrap();
12205					let peer_state: &mut PeerState<_> = &mut *peer_state_lock;
12206					for (channel_id, chan) in
12207						peer_state.channel_by_id.iter_mut().filter_map(|(chan_id, chan)| {
12208							chan.as_funded_mut().map(|chan| (chan_id, chan))
12209						}) {
12210						let counterparty_node_id = chan.context.get_counterparty_node_id();
12211						let funding_txo = chan.funding.get_funding_txo();
12212						let (monitor_opt, holding_cell_failed_htlcs) = chan
12213							.maybe_free_holding_cell_htlcs(
12214								&self.fee_estimator,
12215								&&WithChannelContext::from(&self.logger, &chan.context, None),
12216							);
12217						if !holding_cell_failed_htlcs.is_empty() {
12218							failed_htlcs.push((
12219								holding_cell_failed_htlcs,
12220								*channel_id,
12221								counterparty_node_id,
12222							));
12223						}
12224						if let Some(monitor_update) = monitor_opt {
12225							has_monitor_update = true;
12226
12227							handle_new_monitor_update!(
12228								self,
12229								funding_txo.unwrap(),
12230								monitor_update,
12231								peer_state_lock,
12232								peer_state,
12233								per_peer_state,
12234								chan
12235							);
12236							continue 'peer_loop;
12237						}
12238					}
12239					break 'chan_loop;
12240				}
12241			}
12242			break 'peer_loop;
12243		}
12244
12245		let has_update = has_monitor_update || !failed_htlcs.is_empty();
12246		for (failures, channel_id, counterparty_node_id) in failed_htlcs.drain(..) {
12247			self.fail_holding_cell_htlcs(failures, channel_id, &counterparty_node_id);
12248		}
12249
12250		has_update
12251	}
12252
12253	/// When a call to a [`ChannelSigner`] method returns an error, this indicates that the signer
12254	/// is (temporarily) unavailable, and the operation should be retried later.
12255	///
12256	/// This method allows for that retry - either checking for any signer-pending messages to be
12257	/// attempted in every channel, or in the specifically provided channel.
12258	///
12259	/// [`ChannelSigner`]: crate::sign::ChannelSigner
12260	#[rustfmt::skip]
12261	pub fn signer_unblocked(&self, channel_opt: Option<(PublicKey, ChannelId)>) {
12262		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
12263
12264		// Returns whether we should remove this channel as it's just been closed.
12265		let unblock_chan = |chan: &mut Channel<SP>, pending_msg_events: &mut Vec<MessageSendEvent>| -> Option<ShutdownResult> {
12266			let channel_id = chan.context().channel_id();
12267			let outbound_scid_alias = chan.context().outbound_scid_alias();
12268			let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
12269			let node_id = chan.context().get_counterparty_node_id();
12270			if let Some(msgs) = chan.signer_maybe_unblocked(
12271				self.chain_hash, &&logger,
12272				|htlc_id| self.path_for_release_held_htlc(htlc_id, outbound_scid_alias, &channel_id, &node_id)
12273			) {
12274				if chan.context().is_connected() {
12275					if let Some(msg) = msgs.open_channel {
12276						pending_msg_events.push(MessageSendEvent::SendOpenChannel {
12277							node_id,
12278							msg,
12279						});
12280					}
12281					if let Some(msg) = msgs.funding_created {
12282						pending_msg_events.push(MessageSendEvent::SendFundingCreated {
12283							node_id,
12284							msg,
12285						});
12286					}
12287					if let Some(msg) = msgs.accept_channel {
12288						pending_msg_events.push(MessageSendEvent::SendAcceptChannel {
12289							node_id,
12290							msg,
12291						});
12292					}
12293					let cu_msg = msgs.commitment_update.map(|updates| MessageSendEvent::UpdateHTLCs {
12294						node_id,
12295						channel_id,
12296						updates,
12297					});
12298					let raa_msg = msgs.revoke_and_ack.map(|msg| MessageSendEvent::SendRevokeAndACK {
12299						node_id,
12300						msg,
12301					});
12302					match (cu_msg, raa_msg) {
12303						(Some(cu), Some(raa)) if msgs.order == RAACommitmentOrder::CommitmentFirst => {
12304							pending_msg_events.push(cu);
12305							pending_msg_events.push(raa);
12306						},
12307						(Some(cu), Some(raa)) if msgs.order == RAACommitmentOrder::RevokeAndACKFirst => {
12308							pending_msg_events.push(raa);
12309							pending_msg_events.push(cu);
12310						},
12311						(Some(cu), _) => pending_msg_events.push(cu),
12312						(_, Some(raa)) => pending_msg_events.push(raa),
12313						(_, _) => {},
12314					}
12315					if let Some(msg) = msgs.funding_signed {
12316						pending_msg_events.push(MessageSendEvent::SendFundingSigned {
12317							node_id,
12318							msg,
12319						});
12320					}
12321					if let Some(msg) = msgs.closing_signed {
12322						pending_msg_events.push(MessageSendEvent::SendClosingSigned {
12323							node_id,
12324							msg,
12325						});
12326					}
12327				}
12328				if let Some(funded_chan) = chan.as_funded() {
12329					if let Some(msg) = msgs.channel_ready {
12330						send_channel_ready!(self, pending_msg_events, funded_chan, msg);
12331					}
12332					if let Some(broadcast_tx) = msgs.signed_closing_tx {
12333						log_info!(logger, "Broadcasting closing tx {}", log_tx!(broadcast_tx));
12334						self.tx_broadcaster.broadcast_transactions(&[&broadcast_tx]);
12335					}
12336				} else {
12337					// We don't know how to handle a channel_ready or signed_closing_tx for a
12338					// non-funded channel.
12339					debug_assert!(msgs.channel_ready.is_none());
12340					debug_assert!(msgs.signed_closing_tx.is_none());
12341				}
12342				msgs.shutdown_result
12343			} else {
12344				None
12345			}
12346		};
12347
12348		let mut shutdown_results: Vec<(Result<Infallible, _>, _)> = Vec::new();
12349		let per_peer_state = self.per_peer_state.read().unwrap();
12350		let per_peer_state_iter = per_peer_state.iter().filter(|(cp_id, _)| {
12351			if let Some((counterparty_node_id, _)) = channel_opt {
12352				**cp_id == counterparty_node_id
12353			} else { true }
12354		});
12355		for (cp_id, peer_state_mutex) in per_peer_state_iter {
12356			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
12357			let peer_state = &mut *peer_state_lock;
12358			peer_state.channel_by_id.retain(|_, chan| {
12359				let shutdown_result = match channel_opt {
12360					Some((_, channel_id)) if chan.context().channel_id() != channel_id => None,
12361					_ => unblock_chan(chan, &mut peer_state.pending_msg_events),
12362				};
12363				if let Some(shutdown) = shutdown_result {
12364					let context = chan.context();
12365					let logger = WithChannelContext::from(&self.logger, context, None);
12366					let chan_id = context.channel_id();
12367					log_trace!(logger, "Removing channel {} now that the signer is unblocked", chan_id);
12368					let (remove, err) = if let Some(funded) = chan.as_funded_mut() {
12369						let err =
12370							convert_channel_err!(self, peer_state, shutdown, funded, COOP_CLOSED);
12371						(true, err)
12372					} else {
12373						debug_assert!(false);
12374						let reason = shutdown.closure_reason.clone();
12375						let err = ChannelError::Close((reason.to_string(), reason));
12376						convert_channel_err!(self, peer_state, err, chan, UNFUNDED_CHANNEL)
12377					};
12378					debug_assert!(remove);
12379					shutdown_results.push((Err(err), *cp_id));
12380					false
12381				} else {
12382					true
12383				}
12384			});
12385		}
12386		drop(per_peer_state);
12387		for (err, counterparty_node_id) in shutdown_results {
12388			let _ = handle_error!(self, err, counterparty_node_id);
12389		}
12390	}
12391
12392	/// Check whether any channels have finished removing all pending updates after a shutdown
12393	/// exchange and can now send a closing_signed.
12394	/// Returns whether any closing_signed messages were generated.
12395	#[rustfmt::skip]
12396	fn maybe_generate_initial_closing_signed(&self) -> bool {
12397		let mut handle_errors: Vec<(PublicKey, Result<(), _>)> = Vec::new();
12398		let mut has_update = false;
12399		{
12400			let per_peer_state = self.per_peer_state.read().unwrap();
12401
12402			for (cp_id, peer_state_mutex) in per_peer_state.iter() {
12403				let mut peer_state_lock = peer_state_mutex.lock().unwrap();
12404				let peer_state = &mut *peer_state_lock;
12405				let pending_msg_events = &mut peer_state.pending_msg_events;
12406				peer_state.channel_by_id.retain(|_, chan| {
12407					if !chan.context().is_connected() {
12408						return true;
12409					}
12410					match chan.as_funded_mut() {
12411						Some(funded_chan) => {
12412							let logger = WithChannelContext::from(&self.logger, &funded_chan.context, None);
12413							match funded_chan.maybe_propose_closing_signed(&self.fee_estimator, &&logger) {
12414								Ok((msg_opt, tx_shutdown_result_opt)) => {
12415									if let Some(msg) = msg_opt {
12416										has_update = true;
12417										pending_msg_events.push(MessageSendEvent::SendClosingSigned {
12418											node_id: funded_chan.context.get_counterparty_node_id(), msg,
12419										});
12420									}
12421									debug_assert_eq!(tx_shutdown_result_opt.is_some(), funded_chan.is_shutdown());
12422									if let Some((tx, shutdown_res)) = tx_shutdown_result_opt {
12423										// We're done with this channel. We got a closing_signed and sent back
12424										// a closing_signed with a closing transaction to broadcast.
12425										let err = convert_channel_err!(self, peer_state, shutdown_res, funded_chan, COOP_CLOSED);
12426										handle_errors.push((*cp_id, Err(err)));
12427
12428										log_info!(logger, "Broadcasting {}", log_tx!(tx));
12429										self.tx_broadcaster.broadcast_transactions(&[&tx]);
12430										false
12431									} else { true }
12432								},
12433								Err(e) => {
12434									has_update = true;
12435									let (close_channel, res) = convert_channel_err!(self, peer_state, e, funded_chan, FUNDED_CHANNEL);
12436									handle_errors.push((funded_chan.context.get_counterparty_node_id(), Err(res)));
12437									!close_channel
12438								}
12439							}
12440						},
12441						None => true, // Retain unfunded channels if present.
12442					}
12443				});
12444			}
12445		}
12446
12447		for (counterparty_node_id, err) in handle_errors {
12448			let _ = handle_error!(self, err, counterparty_node_id);
12449		}
12450
12451		has_update
12452	}
12453
12454	#[rustfmt::skip]
12455	fn maybe_send_stfu(&self) {
12456		let per_peer_state = self.per_peer_state.read().unwrap();
12457		for (counterparty_node_id, peer_state_mutex) in per_peer_state.iter() {
12458			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
12459			let peer_state = &mut *peer_state_lock;
12460			let pending_msg_events = &mut peer_state.pending_msg_events;
12461			for (channel_id, chan) in &mut peer_state.channel_by_id {
12462				if let Some(funded_chan) = chan.as_funded_mut() {
12463					let logger = WithContext::from(
12464						&self.logger, Some(*counterparty_node_id), Some(*channel_id), None
12465					);
12466					match funded_chan.try_send_stfu(&&logger) {
12467						Ok(None) => {},
12468						Ok(Some(stfu)) => {
12469							pending_msg_events.push(MessageSendEvent::SendStfu {
12470								node_id: chan.context().get_counterparty_node_id(),
12471								msg: stfu,
12472							});
12473						},
12474						Err(e) => {
12475							log_debug!(logger, "Could not advance quiescence handshake: {}", e);
12476						}
12477					}
12478				}
12479			}
12480		}
12481	}
12482
12483	#[cfg(any(test, fuzzing))]
12484	#[rustfmt::skip]
12485	pub fn maybe_propose_quiescence(&self, counterparty_node_id: &PublicKey, channel_id: &ChannelId) -> Result<(), APIError> {
12486		let mut result = Ok(());
12487		PersistenceNotifierGuard::optionally_notify(self, || {
12488			let mut notify = NotifyOption::SkipPersistNoEvents;
12489
12490			let per_peer_state = self.per_peer_state.read().unwrap();
12491			let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
12492			if peer_state_mutex_opt.is_none() {
12493				result = Err(APIError::ChannelUnavailable {
12494					err: format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}")
12495				});
12496				return notify;
12497			}
12498
12499			let mut peer_state = peer_state_mutex_opt.unwrap().lock().unwrap();
12500			if !peer_state.latest_features.supports_quiescence() {
12501				result = Err(APIError::ChannelUnavailable { err: "Peer does not support quiescence".to_owned() });
12502				return notify;
12503			}
12504
12505			match peer_state.channel_by_id.entry(channel_id.clone()) {
12506				hash_map::Entry::Occupied(mut chan_entry) => {
12507					if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
12508						let logger = WithContext::from(
12509							&self.logger, Some(*counterparty_node_id), Some(*channel_id), None
12510						);
12511
12512						match chan.propose_quiescence(&&logger, QuiescentAction::DoNothing) {
12513							Ok(None) => {},
12514							Ok(Some(stfu)) => {
12515								peer_state.pending_msg_events.push(MessageSendEvent::SendStfu {
12516									node_id: *counterparty_node_id, msg: stfu
12517								});
12518								notify = NotifyOption::SkipPersistHandleEvents;
12519							},
12520							Err(msg) => log_trace!(logger, "{}", msg),
12521						}
12522					} else {
12523						result = Err(APIError::APIMisuseError {
12524							err: format!("Unfunded channel {} cannot be quiescent", channel_id),
12525						});
12526					}
12527				},
12528				hash_map::Entry::Vacant(_) => {
12529					result = Err(APIError::ChannelUnavailable {
12530						err: format!("Channel with id {} not found for the passed counterparty node_id {}",
12531							channel_id, counterparty_node_id),
12532					});
12533				},
12534			}
12535
12536			notify
12537		});
12538
12539		result
12540	}
12541
12542	#[cfg(any(test, fuzzing))]
12543	#[rustfmt::skip]
12544	pub fn exit_quiescence(&self, counterparty_node_id: &PublicKey, channel_id: &ChannelId) -> Result<bool, APIError> {
12545		let per_peer_state = self.per_peer_state.read().unwrap();
12546		let peer_state_mutex = per_peer_state.get(counterparty_node_id)
12547			.ok_or_else(|| APIError::ChannelUnavailable {
12548				err: format!("Can't find a peer matching the passed counterparty node_id {counterparty_node_id}")
12549			})?;
12550		let mut peer_state = peer_state_mutex.lock().unwrap();
12551		let initiator = match peer_state.channel_by_id.entry(*channel_id) {
12552			hash_map::Entry::Occupied(mut chan_entry) => {
12553				if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
12554					chan.exit_quiescence()
12555				} else {
12556					return Err(APIError::APIMisuseError {
12557						err: format!("Unfunded channel {} cannot be quiescent", channel_id),
12558					})
12559				}
12560			},
12561			hash_map::Entry::Vacant(_) => return Err(APIError::ChannelUnavailable {
12562				err: format!("Channel with id {} not found for the passed counterparty node_id {}",
12563					channel_id, counterparty_node_id),
12564			}),
12565		};
12566		Ok(initiator)
12567	}
12568
12569	/// Utility for creating a BOLT11 invoice that can be verified by [`ChannelManager`] without
12570	/// storing any additional state. It achieves this by including a [`PaymentSecret`] in the
12571	/// invoice which it uses to verify that the invoice has not expired and the payment amount is
12572	/// sufficient, reproducing the [`PaymentPreimage`] if applicable.
12573	#[rustfmt::skip]
12574	pub fn create_bolt11_invoice(
12575		&self, params: Bolt11InvoiceParameters,
12576	) -> Result<Bolt11Invoice, SignOrCreationError<()>> {
12577		let Bolt11InvoiceParameters {
12578			amount_msats, description, invoice_expiry_delta_secs, min_final_cltv_expiry_delta,
12579			payment_hash,
12580		} = params;
12581
12582		let currency =
12583			Network::from_chain_hash(self.chain_hash).map(Into::into).unwrap_or(Currency::Bitcoin);
12584
12585		#[cfg(feature = "std")]
12586		let duration_since_epoch = {
12587			use std::time::SystemTime;
12588			SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
12589				.expect("SystemTime::now() should be after SystemTime::UNIX_EPOCH")
12590		};
12591
12592		// This may be up to 2 hours in the future because of bitcoin's block time rule or about
12593		// 10-30 minutes in the past if a block hasn't been found recently. This should be fine as
12594		// the default invoice expiration is 2 hours, though shorter expirations may be problematic.
12595		#[cfg(not(feature = "std"))]
12596		let duration_since_epoch =
12597			Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64);
12598
12599		if let Some(min_final_cltv_expiry_delta) = min_final_cltv_expiry_delta {
12600			if min_final_cltv_expiry_delta.saturating_add(3) < MIN_FINAL_CLTV_EXPIRY_DELTA {
12601				return Err(SignOrCreationError::CreationError(CreationError::MinFinalCltvExpiryDeltaTooShort));
12602			}
12603		}
12604
12605		let (payment_hash, payment_secret) = match payment_hash {
12606			Some(payment_hash) => {
12607				let payment_secret = self
12608					.create_inbound_payment_for_hash(
12609						payment_hash, amount_msats,
12610						invoice_expiry_delta_secs.unwrap_or(DEFAULT_EXPIRY_TIME as u32),
12611						min_final_cltv_expiry_delta,
12612					)
12613					.map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
12614				(payment_hash, payment_secret)
12615			},
12616			None => {
12617				self
12618					.create_inbound_payment(
12619						amount_msats, invoice_expiry_delta_secs.unwrap_or(DEFAULT_EXPIRY_TIME as u32),
12620						min_final_cltv_expiry_delta,
12621					)
12622					.map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?
12623			},
12624		};
12625
12626		log_trace!(self.logger, "Creating invoice with payment hash {}", &payment_hash);
12627
12628		let invoice = Bolt11InvoiceBuilder::new(currency);
12629		let invoice = match description {
12630			Bolt11InvoiceDescription::Direct(description) => invoice.description(description.into_inner().0),
12631			Bolt11InvoiceDescription::Hash(hash) => invoice.description_hash(hash.0),
12632		};
12633
12634		let mut invoice = invoice
12635			.duration_since_epoch(duration_since_epoch)
12636			.payee_pub_key(self.get_our_node_id())
12637			.payment_hash(Hash::from_slice(&payment_hash.0).unwrap())
12638			.payment_secret(payment_secret)
12639			.basic_mpp()
12640			.min_final_cltv_expiry_delta(
12641				// Add a buffer of 3 to the delta if present, otherwise use LDK's minimum.
12642				min_final_cltv_expiry_delta.map(|x| x.saturating_add(3)).unwrap_or(MIN_FINAL_CLTV_EXPIRY_DELTA).into()
12643			);
12644
12645		if let Some(invoice_expiry_delta_secs) = invoice_expiry_delta_secs{
12646			invoice = invoice.expiry_time(Duration::from_secs(invoice_expiry_delta_secs.into()));
12647		}
12648
12649		if let Some(amount_msats) = amount_msats {
12650			invoice = invoice.amount_milli_satoshis(amount_msats);
12651		}
12652
12653		let channels = self.list_channels();
12654		let route_hints = super::invoice_utils::sort_and_filter_channels(channels, amount_msats, &self.logger);
12655		for hint in route_hints {
12656			invoice = invoice.private_route(hint);
12657		}
12658
12659		let raw_invoice = invoice.build_raw().map_err(|e| SignOrCreationError::CreationError(e))?;
12660		let signature = self.node_signer.sign_invoice(&raw_invoice, Recipient::Node);
12661
12662		raw_invoice
12663			.sign(|_| signature)
12664			.map(|invoice| Bolt11Invoice::from_signed(invoice).unwrap())
12665			.map_err(|e| SignOrCreationError::SignError(e))
12666	}
12667}
12668
12669/// Parameters used with [`create_bolt11_invoice`].
12670///
12671/// [`create_bolt11_invoice`]: ChannelManager::create_bolt11_invoice
12672pub struct Bolt11InvoiceParameters {
12673	/// The amount for the invoice, if any.
12674	pub amount_msats: Option<u64>,
12675
12676	/// The description for what the invoice is for, or hash of such description.
12677	pub description: Bolt11InvoiceDescription,
12678
12679	/// The invoice expiration relative to its creation time. If not set, the invoice will expire in
12680	/// [`DEFAULT_EXPIRY_TIME`] by default.
12681	///
12682	/// The creation time used is the duration since the Unix epoch for `std` builds. For non-`std`
12683	/// builds, the highest block timestamp seen is used instead. In the latter case, use a long
12684	/// enough expiry to account for the average block time.
12685	pub invoice_expiry_delta_secs: Option<u32>,
12686
12687	/// The minimum `cltv_expiry` for the last HTLC in the route. If not set, will use
12688	/// [`MIN_FINAL_CLTV_EXPIRY_DELTA`].
12689	///
12690	/// If set, must be at least [`MIN_FINAL_CLTV_EXPIRY_DELTA`], and a three-block buffer will be
12691	/// added as well to allow for up to a few new block confirmations during routing.
12692	pub min_final_cltv_expiry_delta: Option<u16>,
12693
12694	/// The payment hash used in the invoice. If not set, a payment hash will be generated using a
12695	/// preimage that can be reproduced by [`ChannelManager`] without storing any state.
12696	///
12697	/// Uses the payment hash if set. This may be useful if you're building an on-chain swap or
12698	/// involving another protocol where the payment hash is also involved outside the scope of
12699	/// lightning.
12700	pub payment_hash: Option<PaymentHash>,
12701}
12702
12703impl Default for Bolt11InvoiceParameters {
12704	fn default() -> Self {
12705		Self {
12706			amount_msats: None,
12707			description: Bolt11InvoiceDescription::Direct(Description::empty()),
12708			invoice_expiry_delta_secs: None,
12709			min_final_cltv_expiry_delta: None,
12710			payment_hash: None,
12711		}
12712	}
12713}
12714
12715macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
12716	/// Creates an [`OfferBuilder`] such that the [`Offer`] it builds is recognized by the
12717	/// [`ChannelManager`] when handling [`InvoiceRequest`] messages for the offer. The offer's
12718	/// expiration will be `absolute_expiry` if `Some`, otherwise it will not expire.
12719	///
12720	/// # Privacy
12721	///
12722	/// Uses [`MessageRouter`] provided at construction to construct a [`BlindedMessagePath`] for
12723	/// the offer. See the documentation of the selected [`MessageRouter`] for details on how it
12724	/// selects blinded paths including privacy implications and reliability tradeoffs.
12725	///
12726	/// Also, uses a derived signing pubkey in the offer for recipient privacy.
12727	///
12728	/// # Limitations
12729	///
12730	/// See [`OffersMessageFlow::create_offer_builder`] for limitations on the offer builder.
12731	///
12732	/// # Errors
12733	///
12734	/// Errors if the parameterized [`MessageRouter`] is unable to create a blinded path for the offer.
12735	///
12736	/// [`BlindedMessagePath`]: crate::blinded_path::message::BlindedMessagePath
12737	/// [`Offer`]: crate::offers::offer::Offer
12738	/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
12739	pub fn create_offer_builder(&$self) -> Result<$builder, Bolt12SemanticError> {
12740		let builder = $self.flow.create_offer_builder(
12741			&*$self.entropy_source, $self.get_peers_for_blinded_path()
12742		)?;
12743
12744		Ok(builder.into())
12745	}
12746
12747	/// Same as [`Self::create_offer_builder`], but allows specifying a custom [`MessageRouter`]
12748	/// instead of using the [`MessageRouter`] provided to the [`ChannelManager`] at construction.
12749	///
12750	/// This gives users full control over how the [`BlindedMessagePath`] is constructed,
12751	/// including the option to omit it entirely.
12752	///
12753	/// See [`Self::create_offer_builder`] for more details on usage.
12754	///
12755	/// [`BlindedMessagePath`]: crate::blinded_path::message::BlindedMessagePath
12756	/// [`Offer`]: crate::offers::offer::Offer
12757	/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
12758	pub fn create_offer_builder_using_router<ME: Deref>(
12759		&$self,
12760		router: ME,
12761	) -> Result<$builder, Bolt12SemanticError>
12762	where
12763		ME::Target: MessageRouter,
12764	{
12765		let builder = $self.flow.create_offer_builder_using_router(
12766			router, &*$self.entropy_source, $self.get_peers_for_blinded_path()
12767		)?;
12768
12769		Ok(builder.into())
12770	}
12771} }
12772
12773macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
12774	/// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
12775	/// [`ChannelManager`] when handling [`Bolt12Invoice`] messages for the refund.
12776	///
12777	/// # Payment
12778	///
12779	/// The provided `payment_id` is used to ensure that only one invoice is paid for the refund.
12780	/// See [Avoiding Duplicate Payments] for other requirements once the payment has been sent.
12781	///
12782	/// The builder will have the provided expiration set. Any changes to the expiration on the
12783	/// returned builder will not be honored by [`ChannelManager`]. For non-`std`, the highest seen
12784	/// block time minus two hours is used for the current time when determining if the refund has
12785	/// expired.
12786	///
12787	/// To revoke the refund, use [`ChannelManager::abandon_payment`] prior to receiving the
12788	/// invoice. If abandoned, or an invoice isn't received before expiration, the payment will fail
12789	/// with an [`Event::PaymentFailed`].
12790	///
12791	/// If `max_total_routing_fee_msat` is not specified, The default from
12792	/// [`RouteParameters::from_payment_params_and_value`] is applied.
12793	///
12794	/// # Privacy
12795	///
12796	/// Uses [`MessageRouter`] provided at construction to construct a [`BlindedMessagePath`] for
12797	/// the refund. See the documentation of the selected [`MessageRouter`] for details on how it
12798	/// selects blinded paths including privacy implications and reliability tradeoffs.
12799	///
12800	/// Also, uses a derived payer id in the refund for payer privacy.
12801	///
12802	/// # Errors
12803	///
12804	/// Errors if:
12805	/// - a duplicate `payment_id` is provided given the caveats in the aforementioned link,
12806	/// - `amount_msats` is invalid, or
12807	/// - the parameterized [`Router`] is unable to create a blinded path for the refund.
12808	///
12809	/// [`Refund`]: crate::offers::refund::Refund
12810	/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
12811	/// [`Bolt12Invoice::payment_paths`]: crate::offers::invoice::Bolt12Invoice::payment_paths
12812	/// [`BlindedMessagePath`]: crate::blinded_path::message::BlindedMessagePath
12813	/// [Avoiding Duplicate Payments]: #avoiding-duplicate-payments
12814	pub fn create_refund_builder(
12815		&$self, amount_msats: u64, absolute_expiry: Duration, payment_id: PaymentId,
12816		retry_strategy: Retry, route_params_config: RouteParametersConfig
12817	) -> Result<$builder, Bolt12SemanticError> {
12818		let entropy = &*$self.entropy_source;
12819
12820		let builder = $self.flow.create_refund_builder(
12821			entropy, amount_msats, absolute_expiry,
12822			payment_id, $self.get_peers_for_blinded_path()
12823		)?;
12824
12825		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop($self);
12826
12827		let expiration = StaleExpiration::AbsoluteTimeout(absolute_expiry);
12828		$self.pending_outbound_payments
12829			.add_new_awaiting_invoice(
12830				payment_id, expiration, retry_strategy, route_params_config, None,
12831			)
12832			.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
12833
12834		Ok(builder.into())
12835	}
12836
12837	/// Same as [`Self::create_refund_builder`], but allows specifying a custom [`MessageRouter`]
12838	/// instead of using the one provided during [`ChannelManager`] construction for
12839	/// [`BlindedMessagePath`] creation.
12840	///
12841	/// This gives users full control over how the [`BlindedMessagePath`] is constructed for the
12842	/// refund, including the option to omit it entirely. This is useful for testing or when
12843	/// alternative privacy strategies are needed.
12844	///
12845	/// See [`Self::create_refund_builder`] for more details on usage.
12846	///
12847	/// # Errors
12848	///
12849	/// In addition to the errors in [`Self::create_refund_builder`], this returns an error if
12850	/// the provided [`MessageRouter`] fails to construct a valid [`BlindedMessagePath`] for the refund.
12851	///
12852	/// [`Refund`]: crate::offers::refund::Refund
12853	/// [`BlindedMessagePath`]: crate::blinded_path::message::BlindedMessagePath
12854	/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
12855	pub fn create_refund_builder_using_router<ME: Deref>(
12856		&$self, router: ME, amount_msats: u64, absolute_expiry: Duration, payment_id: PaymentId,
12857		retry_strategy: Retry, route_params_config: RouteParametersConfig
12858	) -> Result<$builder, Bolt12SemanticError>
12859	where
12860		ME::Target: MessageRouter,
12861	{
12862		let entropy = &*$self.entropy_source;
12863
12864		let builder = $self.flow.create_refund_builder_using_router(
12865			router, entropy, amount_msats, absolute_expiry,
12866			payment_id, $self.get_peers_for_blinded_path()
12867		)?;
12868
12869		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop($self);
12870
12871		let expiration = StaleExpiration::AbsoluteTimeout(absolute_expiry);
12872		$self.pending_outbound_payments
12873			.add_new_awaiting_invoice(
12874				payment_id, expiration, retry_strategy, route_params_config, None,
12875			)
12876			.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
12877
12878		Ok(builder.into())
12879	}
12880} }
12881
12882impl<
12883		M: Deref,
12884		T: Deref,
12885		ES: Deref,
12886		NS: Deref,
12887		SP: Deref,
12888		F: Deref,
12889		R: Deref,
12890		MR: Deref,
12891		L: Deref,
12892	> ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
12893where
12894	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
12895	T::Target: BroadcasterInterface,
12896	ES::Target: EntropySource,
12897	NS::Target: NodeSigner,
12898	SP::Target: SignerProvider,
12899	F::Target: FeeEstimator,
12900	R::Target: Router,
12901	MR::Target: MessageRouter,
12902	L::Target: Logger,
12903{
12904	#[cfg(not(c_bindings))]
12905	create_offer_builder!(self, OfferBuilder<'_, DerivedMetadata, secp256k1::All>);
12906	#[cfg(not(c_bindings))]
12907	create_refund_builder!(self, RefundBuilder<'_, secp256k1::All>);
12908
12909	#[cfg(c_bindings)]
12910	create_offer_builder!(self, OfferWithDerivedMetadataBuilder);
12911	#[cfg(c_bindings)]
12912	create_refund_builder!(self, RefundMaybeWithDerivedMetadataBuilder);
12913
12914	/// Retrieve an [`Offer`] for receiving async payments as an often-offline recipient. Will only
12915	/// return an offer if [`Self::set_paths_to_static_invoice_server`] was called and we succeeded in
12916	/// interactively building a [`StaticInvoice`] with the static invoice server.
12917	///
12918	/// Useful for posting offers to receive payments later, such as posting an offer on a website.
12919	pub fn get_async_receive_offer(&self) -> Result<Offer, ()> {
12920		let (offer, needs_persist) = self.flow.get_async_receive_offer()?;
12921		if needs_persist {
12922			// We need to re-persist the cache if a fresh offer was just marked as used to ensure we
12923			// continue to keep this offer's invoice updated and don't replace it with the server.
12924			let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
12925		}
12926		Ok(offer)
12927	}
12928
12929	/// Sets the [`BlindedMessagePath`]s that we will use as an async recipient to interactively build
12930	/// [`Offer`]s with a static invoice server, so the server can serve [`StaticInvoice`]s to payers
12931	/// on our behalf when we're offline.
12932	///
12933	/// This method only needs to be called once when the server first takes on the recipient as a
12934	/// client, or when the paths change, e.g. if the paths are set to expire at a particular time.
12935	pub fn set_paths_to_static_invoice_server(
12936		&self, paths_to_static_invoice_server: Vec<BlindedMessagePath>,
12937	) -> Result<(), ()> {
12938		let peers = self.get_peers_for_blinded_path();
12939		self.flow.set_paths_to_static_invoice_server(paths_to_static_invoice_server, peers)?;
12940
12941		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
12942		Ok(())
12943	}
12944
12945	/// Pays for an [`Offer`] using the given parameters by creating an [`InvoiceRequest`] and
12946	/// enqueuing it to be sent via an onion message. [`ChannelManager`] will pay the actual
12947	/// [`Bolt12Invoice`] once it is received.
12948	///
12949	/// Uses [`InvoiceRequestBuilder`] such that the [`InvoiceRequest`] it builds is recognized by
12950	/// the [`ChannelManager`] when handling a [`Bolt12Invoice`] message in response to the request.
12951	///
12952	/// `amount_msats` allows you to overpay what is required to satisfy the offer, or may be
12953	/// required if the offer does not require a specific amount.
12954	///
12955	/// If the [`Offer`] was built from a human readable name resolved using BIP 353, you *must*
12956	/// instead call [`Self::pay_for_offer_from_hrn`].
12957	///
12958	/// # Payment
12959	///
12960	/// The provided `payment_id` is used to ensure that only one invoice is paid for the request
12961	/// when received. See [Avoiding Duplicate Payments] for other requirements once the payment has
12962	/// been sent.
12963	///
12964	/// To revoke the request, use [`ChannelManager::abandon_payment`] prior to receiving the
12965	/// invoice. If abandoned, or an invoice isn't received in a reasonable amount of time, the
12966	/// payment will fail with an [`Event::PaymentFailed`].
12967	///
12968	/// # Privacy
12969	///
12970	/// For payer privacy, uses a derived payer id and uses [`MessageRouter::create_blinded_paths`]
12971	/// to construct a [`BlindedMessagePath`] for the reply path.
12972	///
12973	/// # Note
12974	///
12975	/// If the offer resolves to an async payment, and the HTLC is neither claimed nor failed by
12976	/// our next-hop peer, we will not force-close the channel to resolve the payment for 4
12977	/// weeks. This avoids an issue for often-offline nodes where channels are force-closed on
12978	/// startup during chain sync prior to connecting to peers. If you want to resolve such a
12979	/// timed-out payment more urgently, you can manually force-close the channel which will,
12980	/// after some transaction confirmation(s), result in an [`Event::PaymentFailed`].
12981	///
12982	/// # Errors
12983	///
12984	/// Errors if:
12985	/// - a duplicate `payment_id` is provided given the caveats in the aforementioned link,
12986	/// - the provided parameters are invalid for the offer,
12987	/// - the offer is for an unsupported chain, or
12988	/// - the parameterized [`Router`] is unable to create a blinded reply path for the invoice
12989	///   request.
12990	///
12991	/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
12992	/// [`InvoiceRequestBuilder`]: crate::offers::invoice_request::InvoiceRequestBuilder
12993	/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
12994	/// [`BlindedMessagePath`]: crate::blinded_path::message::BlindedMessagePath
12995	/// [`Bolt12Invoice::payment_paths`]: crate::offers::invoice::Bolt12Invoice::payment_paths
12996	/// [Avoiding Duplicate Payments]: #avoiding-duplicate-payments
12997	pub fn pay_for_offer(
12998		&self, offer: &Offer, amount_msats: Option<u64>, payment_id: PaymentId,
12999		optional_params: OptionalOfferPaymentParams,
13000	) -> Result<(), Bolt12SemanticError> {
13001		let create_pending_payment_fn = |retryable_invoice_request: RetryableInvoiceRequest| {
13002			self.pending_outbound_payments
13003				.add_new_awaiting_invoice(
13004					payment_id,
13005					StaleExpiration::TimerTicks(1),
13006					optional_params.retry_strategy,
13007					optional_params.route_params_config,
13008					Some(retryable_invoice_request),
13009				)
13010				.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)
13011		};
13012
13013		self.pay_for_offer_intern(
13014			offer,
13015			if offer.expects_quantity() { Some(1) } else { None },
13016			amount_msats,
13017			optional_params.payer_note,
13018			payment_id,
13019			None,
13020			create_pending_payment_fn,
13021		)
13022	}
13023
13024	/// Pays for an [`Offer`] which was built by resolving a human readable name. It is otherwise
13025	/// identical to [`Self::pay_for_offer`].
13026	pub fn pay_for_offer_from_hrn(
13027		&self, offer: &OfferFromHrn, amount_msats: u64, payment_id: PaymentId,
13028		optional_params: OptionalOfferPaymentParams,
13029	) -> Result<(), Bolt12SemanticError> {
13030		let create_pending_payment_fn = |retryable_invoice_request: RetryableInvoiceRequest| {
13031			self.pending_outbound_payments
13032				.add_new_awaiting_invoice(
13033					payment_id,
13034					StaleExpiration::TimerTicks(1),
13035					optional_params.retry_strategy,
13036					optional_params.route_params_config,
13037					Some(retryable_invoice_request),
13038				)
13039				.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)
13040		};
13041
13042		self.pay_for_offer_intern(
13043			&offer.offer,
13044			if offer.offer.expects_quantity() { Some(1) } else { None },
13045			Some(amount_msats),
13046			optional_params.payer_note,
13047			payment_id,
13048			Some(offer.hrn),
13049			create_pending_payment_fn,
13050		)
13051	}
13052
13053	/// Pays for an [`Offer`] using the given parameters, including a `quantity`, by creating an
13054	/// [`InvoiceRequest`] and enqueuing it to be sent via an onion message. [`ChannelManager`] will
13055	/// pay the actual [`Bolt12Invoice`] once it is received.
13056	///
13057	/// This method is identical to [`Self::pay_for_offer`] with the one exception that it allows
13058	/// you to specify the [`InvoiceRequest::quantity`]. We expect this to be rather seldomly used,
13059	/// as the "quantity" feature of offers doesn't line up with common payment flows today.
13060	///
13061	/// This method is otherwise identical to [`Self::pay_for_offer`] but will additionally fail if
13062	/// the provided `quantity` does not meet the requirements described by
13063	/// [`Offer::supported_quantity`].
13064	///
13065	/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
13066	/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
13067	/// [`InvoiceRequest::quantity`]: crate::offers::invoice_request::InvoiceRequest::quantity
13068	pub fn pay_for_offer_with_quantity(
13069		&self, offer: &Offer, amount_msats: Option<u64>, payment_id: PaymentId,
13070		optional_params: OptionalOfferPaymentParams, quantity: u64,
13071	) -> Result<(), Bolt12SemanticError> {
13072		let create_pending_payment_fn = |retryable_invoice_request: RetryableInvoiceRequest| {
13073			self.pending_outbound_payments
13074				.add_new_awaiting_invoice(
13075					payment_id,
13076					StaleExpiration::TimerTicks(1),
13077					optional_params.retry_strategy,
13078					optional_params.route_params_config,
13079					Some(retryable_invoice_request),
13080				)
13081				.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)
13082		};
13083
13084		self.pay_for_offer_intern(
13085			offer,
13086			Some(quantity),
13087			amount_msats,
13088			optional_params.payer_note,
13089			payment_id,
13090			None,
13091			create_pending_payment_fn,
13092		)
13093	}
13094
13095	#[rustfmt::skip]
13096	fn pay_for_offer_intern<CPP: FnOnce(RetryableInvoiceRequest) -> Result<(), Bolt12SemanticError>>(
13097		&self, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
13098		payer_note: Option<String>, payment_id: PaymentId,
13099		human_readable_name: Option<HumanReadableName>, create_pending_payment: CPP,
13100	) -> Result<(), Bolt12SemanticError> {
13101		let entropy = &*self.entropy_source;
13102		let nonce = Nonce::from_entropy_source(entropy);
13103
13104		let builder = self.flow.create_invoice_request_builder(
13105			offer, nonce, payment_id,
13106		)?;
13107
13108		let builder = match quantity {
13109			None => builder,
13110			Some(quantity) => builder.quantity(quantity)?,
13111		};
13112		let builder = match amount_msats {
13113			None => builder,
13114			Some(amount_msats) => builder.amount_msats(amount_msats)?,
13115		};
13116		let builder = match payer_note {
13117			None => builder,
13118			Some(payer_note) => builder.payer_note(payer_note),
13119		};
13120		let builder = match human_readable_name {
13121			None => builder,
13122			Some(hrn) => builder.sourced_from_human_readable_name(hrn),
13123		};
13124
13125		let invoice_request = builder.build_and_sign()?;
13126		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
13127
13128		self.flow.enqueue_invoice_request(
13129			invoice_request.clone(), payment_id, nonce,
13130			self.get_peers_for_blinded_path()
13131		)?;
13132
13133		let retryable_invoice_request = RetryableInvoiceRequest {
13134			invoice_request: invoice_request.clone(),
13135			nonce,
13136			needs_retry: true,
13137		};
13138
13139		create_pending_payment(retryable_invoice_request)
13140	}
13141
13142	/// Creates a [`Bolt12Invoice`] for a [`Refund`] and enqueues it to be sent via an onion
13143	/// message.
13144	///
13145	/// The resulting invoice uses a [`PaymentHash`] recognized by the [`ChannelManager`] and a
13146	/// [`BlindedPaymentPath`] containing the [`PaymentSecret`] needed to reconstruct the
13147	/// corresponding [`PaymentPreimage`]. It is returned purely for informational purposes.
13148	///
13149	/// # Limitations
13150	///
13151	/// Requires a direct connection to an introduction node in [`Refund::paths`] or to
13152	/// [`Refund::payer_signing_pubkey`], if empty. This request is best effort; an invoice will be
13153	/// sent to each node meeting the aforementioned criteria, but there's no guarantee that they
13154	/// will be received and no retries will be made.
13155	///
13156	/// # Errors
13157	///
13158	/// Errors if:
13159	/// - the refund is for an unsupported chain, or
13160	/// - the parameterized [`Router`] is unable to create a blinded payment path or reply path for
13161	///   the invoice.
13162	///
13163	/// [`BlindedPaymentPath`]: crate::blinded_path::payment::BlindedPaymentPath
13164	/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
13165	#[rustfmt::skip]
13166	pub fn request_refund_payment(
13167		&self, refund: &Refund
13168	) -> Result<Bolt12Invoice, Bolt12SemanticError> {
13169		let secp_ctx = &self.secp_ctx;
13170
13171		let amount_msats = refund.amount_msats();
13172		let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
13173
13174		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
13175
13176		match self.create_inbound_payment(Some(amount_msats), relative_expiry, None) {
13177			Ok((payment_hash, payment_secret)) => {
13178				let entropy = &*self.entropy_source;
13179				let builder = self.flow.create_invoice_builder_from_refund(
13180					&self.router, entropy, refund, payment_hash,
13181					payment_secret, self.list_usable_channels()
13182				)?;
13183
13184				let invoice = builder.allow_mpp().build_and_sign(secp_ctx)?;
13185
13186				self.flow.enqueue_invoice(invoice.clone(), refund, self.get_peers_for_blinded_path())?;
13187
13188				Ok(invoice)
13189			},
13190			Err(()) => Err(Bolt12SemanticError::InvalidAmount),
13191		}
13192	}
13193
13194	/// Pays for an [`Offer`] looked up using [BIP 353] Human Readable Names resolved by the DNS
13195	/// resolver(s) at `dns_resolvers` which resolve names according to [bLIP 32].
13196	///
13197	/// Because most wallets support on-chain or other payment schemes beyond only offers, this is
13198	/// deprecated in favor of the [`bitcoin-payment-instructions`] crate, which can be used to
13199	/// build an [`OfferFromHrn`] and call [`Self::pay_for_offer_from_hrn`]. Thus, this method is
13200	/// deprecated.
13201	///
13202	/// # Payment
13203	///
13204	/// The provided `payment_id` is used to ensure that only one invoice is paid for the request
13205	/// when received. See [Avoiding Duplicate Payments] for other requirements once the payment has
13206	/// been sent.
13207	///
13208	/// To revoke the request, use [`ChannelManager::abandon_payment`] prior to receiving the
13209	/// invoice. If abandoned, or an invoice isn't received in a reasonable amount of time, the
13210	/// payment will fail with an [`PaymentFailureReason::UserAbandoned`] or
13211	/// [`PaymentFailureReason::InvoiceRequestExpired`], respectively.
13212	///
13213	/// # Privacy
13214	///
13215	/// For payer privacy, uses a derived payer id and uses [`MessageRouter::create_blinded_paths`]
13216	/// to construct a [`BlindedMessagePath`] for the reply path.
13217	///
13218	/// # Errors
13219	///
13220	/// Errors if a duplicate `payment_id` is provided given the caveats in the aforementioned link.
13221	///
13222	/// [BIP 353]: https://github.com/bitcoin/bips/blob/master/bip-0353.mediawiki
13223	/// [bLIP 32]: https://github.com/lightning/blips/blob/master/blip-0032.md
13224	/// [`OMNameResolver::resolve_name`]: crate::onion_message::dns_resolution::OMNameResolver::resolve_name
13225	/// [`OMNameResolver::handle_dnssec_proof_for_uri`]: crate::onion_message::dns_resolution::OMNameResolver::handle_dnssec_proof_for_uri
13226	/// [`bitcoin-payment-instructions`]: https://docs.rs/bitcoin-payment-instructions/
13227	/// [Avoiding Duplicate Payments]: #avoiding-duplicate-payments
13228	/// [`BlindedMessagePath`]: crate::blinded_path::message::BlindedMessagePath
13229	/// [`PaymentFailureReason::UserAbandoned`]: crate::events::PaymentFailureReason::UserAbandoned
13230	/// [`PaymentFailureReason::InvoiceRequestRejected`]: crate::events::PaymentFailureReason::InvoiceRequestRejected
13231	#[cfg(feature = "dnssec")]
13232	#[deprecated(note = "Use bitcoin-payment-instructions and pay_for_offer_from_hrn instead")]
13233	pub fn pay_for_offer_from_human_readable_name(
13234		&self, name: HumanReadableName, amount_msats: u64, payment_id: PaymentId,
13235		optional_params: OptionalOfferPaymentParams, dns_resolvers: Vec<Destination>,
13236	) -> Result<(), ()> {
13237		let (onion_message, context) =
13238			self.flow.hrn_resolver.resolve_name(payment_id, name, &*self.entropy_source)?;
13239
13240		let expiration = StaleExpiration::TimerTicks(1);
13241		self.pending_outbound_payments.add_new_awaiting_offer(
13242			payment_id,
13243			expiration,
13244			optional_params.retry_strategy,
13245			optional_params.route_params_config,
13246			amount_msats,
13247			optional_params.payer_note,
13248		)?;
13249
13250		self.flow
13251			.enqueue_dns_onion_message(
13252				onion_message,
13253				context,
13254				dns_resolvers,
13255				self.get_peers_for_blinded_path(),
13256			)
13257			.map_err(|_| ())
13258	}
13259
13260	/// Gets a payment secret and payment hash for use in an invoice given to a third party wishing
13261	/// to pay us.
13262	///
13263	/// This differs from [`create_inbound_payment_for_hash`] only in that it generates the
13264	/// [`PaymentHash`] and [`PaymentPreimage`] for you.
13265	///
13266	/// The [`PaymentPreimage`] will ultimately be returned to you in the [`PaymentClaimable`] event, which
13267	/// will have the [`PaymentClaimable::purpose`] return `Some` for [`PaymentPurpose::preimage`]. That
13268	/// should then be passed directly to [`claim_funds`].
13269	///
13270	/// See [`create_inbound_payment_for_hash`] for detailed documentation on behavior and requirements.
13271	///
13272	/// Note that a malicious eavesdropper can intuit whether an inbound payment was created by
13273	/// `create_inbound_payment` or `create_inbound_payment_for_hash` based on runtime.
13274	///
13275	/// # Note
13276	///
13277	/// If you register an inbound payment with this method, then serialize the `ChannelManager`, then
13278	/// deserialize it with a node running 0.0.103 and earlier, the payment will fail to be received.
13279	///
13280	/// Errors if `min_value_msat` is greater than total bitcoin supply.
13281	///
13282	/// If `min_final_cltv_expiry_delta` is set to some value, then the payment will not be receivable
13283	/// on versions of LDK prior to 0.0.114.
13284	///
13285	/// [`claim_funds`]: Self::claim_funds
13286	/// [`PaymentClaimable`]: events::Event::PaymentClaimable
13287	/// [`PaymentClaimable::purpose`]: events::Event::PaymentClaimable::purpose
13288	/// [`PaymentPurpose::preimage`]: events::PaymentPurpose::preimage
13289	/// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
13290	pub fn create_inbound_payment(
13291		&self, min_value_msat: Option<u64>, invoice_expiry_delta_secs: u32,
13292		min_final_cltv_expiry_delta: Option<u16>,
13293	) -> Result<(PaymentHash, PaymentSecret), ()> {
13294		inbound_payment::create(
13295			&self.inbound_payment_key,
13296			min_value_msat,
13297			invoice_expiry_delta_secs,
13298			&self.entropy_source,
13299			self.highest_seen_timestamp.load(Ordering::Acquire) as u64,
13300			min_final_cltv_expiry_delta,
13301		)
13302	}
13303
13304	/// Gets a [`PaymentSecret`] for a given [`PaymentHash`], for which the payment preimage is
13305	/// stored external to LDK.
13306	///
13307	/// A [`PaymentClaimable`] event will only be generated if the [`PaymentSecret`] matches a
13308	/// payment secret fetched via this method or [`create_inbound_payment`], and which is at least
13309	/// the `min_value_msat` provided here, if one is provided.
13310	///
13311	/// The [`PaymentHash`] (and corresponding [`PaymentPreimage`]) should be globally unique, though
13312	/// note that LDK will not stop you from registering duplicate payment hashes for inbound
13313	/// payments.
13314	///
13315	/// `min_value_msat` should be set if the invoice being generated contains a value. Any payment
13316	/// received for the returned [`PaymentHash`] will be required to be at least `min_value_msat`
13317	/// before a [`PaymentClaimable`] event will be generated, ensuring that we do not provide the
13318	/// sender "proof-of-payment" unless they have paid the required amount.
13319	///
13320	/// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
13321	/// in excess of the current time. This should roughly match the expiry time set in the invoice.
13322	/// After this many seconds, we will remove the inbound payment, resulting in any attempts to
13323	/// pay the invoice failing. The BOLT spec suggests 3,600 secs as a default validity time for
13324	/// invoices when no timeout is set.
13325	///
13326	/// Note that we use block header time to time-out pending inbound payments (with some margin
13327	/// to compensate for the inaccuracy of block header timestamps). Thus, in practice we will
13328	/// accept a payment and generate a [`PaymentClaimable`] event for some time after the expiry.
13329	/// If you need exact expiry semantics, you should enforce them upon receipt of
13330	/// [`PaymentClaimable`].
13331	///
13332	/// Note that invoices generated for inbound payments should have their `min_final_cltv_expiry_delta`
13333	/// set to at least [`MIN_FINAL_CLTV_EXPIRY_DELTA`].
13334	///
13335	/// Note that a malicious eavesdropper can intuit whether an inbound payment was created by
13336	/// `create_inbound_payment` or `create_inbound_payment_for_hash` based on runtime.
13337	///
13338	/// # Note
13339	///
13340	/// If you register an inbound payment with this method, then serialize the `ChannelManager`, then
13341	/// deserialize it with a node running 0.0.103 and earlier, the payment will fail to be received.
13342	///
13343	/// Errors if `min_value_msat` is greater than total bitcoin supply.
13344	///
13345	/// If `min_final_cltv_expiry_delta` is set to some value, then the payment will not be receivable
13346	/// on versions of LDK prior to 0.0.114.
13347	///
13348	/// [`create_inbound_payment`]: Self::create_inbound_payment
13349	/// [`PaymentClaimable`]: events::Event::PaymentClaimable
13350	pub fn create_inbound_payment_for_hash(
13351		&self, payment_hash: PaymentHash, min_value_msat: Option<u64>,
13352		invoice_expiry_delta_secs: u32, min_final_cltv_expiry: Option<u16>,
13353	) -> Result<PaymentSecret, ()> {
13354		inbound_payment::create_from_hash(
13355			&self.inbound_payment_key,
13356			min_value_msat,
13357			payment_hash,
13358			invoice_expiry_delta_secs,
13359			self.highest_seen_timestamp.load(Ordering::Acquire) as u64,
13360			min_final_cltv_expiry,
13361		)
13362	}
13363
13364	/// Gets an LDK-generated payment preimage from a payment hash and payment secret that were
13365	/// previously returned from [`create_inbound_payment`].
13366	///
13367	/// [`create_inbound_payment`]: Self::create_inbound_payment
13368	pub fn get_payment_preimage(
13369		&self, payment_hash: PaymentHash, payment_secret: PaymentSecret,
13370	) -> Result<PaymentPreimage, APIError> {
13371		let expanded_key = &self.inbound_payment_key;
13372		inbound_payment::get_payment_preimage(payment_hash, payment_secret, expanded_key)
13373	}
13374
13375	/// [`BlindedMessagePath`]s for an async recipient to communicate with this node and interactively
13376	/// build [`Offer`]s and [`StaticInvoice`]s for receiving async payments.
13377	///
13378	/// ## Usage
13379	/// 1. Static invoice server calls [`Self::blinded_paths_for_async_recipient`]
13380	/// 2. Static invoice server communicates the resulting paths out-of-band to the async recipient,
13381	///    who calls [`Self::set_paths_to_static_invoice_server`] to configure themselves with these
13382	///    paths
13383	/// 3. Async recipient automatically sends [`OfferPathsRequest`]s over the configured paths, and
13384	///    uses the resulting paths from the server's [`OfferPaths`] response to build their async
13385	///    receive offer
13386	///
13387	/// If `relative_expiry` is unset, the [`BlindedMessagePath`]s will never expire.
13388	///
13389	/// Returns the paths that the recipient should be configured with via
13390	/// [`Self::set_paths_to_static_invoice_server`].
13391	///
13392	/// The provided `recipient_id` must uniquely identify the recipient, and will be surfaced later
13393	/// when the recipient provides us with a static invoice to persist and serve to payers on their
13394	/// behalf.
13395	pub fn blinded_paths_for_async_recipient(
13396		&self, recipient_id: Vec<u8>, relative_expiry: Option<Duration>,
13397	) -> Result<Vec<BlindedMessagePath>, ()> {
13398		let peers = self.get_peers_for_blinded_path();
13399		self.flow.blinded_paths_for_async_recipient(recipient_id, relative_expiry, peers)
13400	}
13401
13402	pub(super) fn duration_since_epoch(&self) -> Duration {
13403		#[cfg(not(feature = "std"))]
13404		let now = Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64);
13405		#[cfg(feature = "std")]
13406		let now = std::time::SystemTime::now()
13407			.duration_since(std::time::SystemTime::UNIX_EPOCH)
13408			.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
13409
13410		now
13411	}
13412
13413	fn get_peers_for_blinded_path(&self) -> Vec<MessageForwardNode> {
13414		let per_peer_state = self.per_peer_state.read().unwrap();
13415		per_peer_state
13416			.iter()
13417			.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
13418			.filter(|(_, peer)| peer.is_connected)
13419			.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
13420			.map(|(node_id, peer)| MessageForwardNode {
13421				node_id: *node_id,
13422				short_channel_id: peer
13423					.channel_by_id
13424					.iter()
13425					.filter(|(_, channel)| channel.context().is_usable())
13426					.filter_map(|(_, channel)| channel.as_funded())
13427					.min_by_key(|funded_channel| funded_channel.context.channel_creation_height)
13428					.and_then(|funded_channel| funded_channel.get_inbound_scid()),
13429			})
13430			.collect::<Vec<_>>()
13431	}
13432
13433	#[cfg(test)]
13434	pub(super) fn test_get_peers_for_blinded_path(&self) -> Vec<MessageForwardNode> {
13435		self.get_peers_for_blinded_path()
13436	}
13437
13438	#[cfg(test)]
13439	/// Creates multi-hop blinded payment paths for the given `amount_msats` by delegating to
13440	/// [`Router::create_blinded_payment_paths`].
13441	pub(super) fn test_create_blinded_payment_paths(
13442		&self, amount_msats: Option<u64>, payment_secret: PaymentSecret,
13443		payment_context: PaymentContext, relative_expiry_seconds: u32,
13444	) -> Result<Vec<BlindedPaymentPath>, ()> {
13445		let entropy = &*self.entropy_source;
13446
13447		self.flow.test_create_blinded_payment_paths(
13448			&self.router,
13449			entropy,
13450			self.list_usable_channels(),
13451			amount_msats,
13452			payment_secret,
13453			payment_context,
13454			relative_expiry_seconds,
13455		)
13456	}
13457
13458	/// Gets a fake short channel id for use in receiving [phantom node payments]. These fake scids
13459	/// are used when constructing the phantom invoice's route hints.
13460	///
13461	/// [phantom node payments]: crate::sign::PhantomKeysManager
13462	pub fn get_phantom_scid(&self) -> u64 {
13463		let best_block_height = self.best_block.read().unwrap().height;
13464		let short_to_chan_info = self.short_to_chan_info.read().unwrap();
13465		loop {
13466			let scid_candidate = fake_scid::Namespace::Phantom.get_fake_scid(
13467				best_block_height,
13468				&self.chain_hash,
13469				&self.fake_scid_rand_bytes,
13470				&self.entropy_source,
13471			);
13472			// Ensure the generated scid doesn't conflict with a real channel.
13473			match short_to_chan_info.get(&scid_candidate) {
13474				Some(_) => continue,
13475				None => return scid_candidate,
13476			}
13477		}
13478	}
13479
13480	/// Gets route hints for use in receiving [phantom node payments].
13481	///
13482	/// [phantom node payments]: crate::sign::PhantomKeysManager
13483	pub fn get_phantom_route_hints(&self) -> PhantomRouteHints {
13484		PhantomRouteHints {
13485			channels: self.list_usable_channels(),
13486			phantom_scid: self.get_phantom_scid(),
13487			real_node_pubkey: self.get_our_node_id(),
13488		}
13489	}
13490
13491	/// Gets a fake short channel id for use in receiving intercepted payments. These fake scids are
13492	/// used when constructing the route hints for HTLCs intended to be intercepted. See
13493	/// [`ChannelManager::forward_intercepted_htlc`].
13494	///
13495	/// Note that this method is not guaranteed to return unique values, you may need to call it a few
13496	/// times to get a unique scid.
13497	pub fn get_intercept_scid(&self) -> u64 {
13498		let best_block_height = self.best_block.read().unwrap().height;
13499		let short_to_chan_info = self.short_to_chan_info.read().unwrap();
13500		loop {
13501			let scid_candidate = fake_scid::Namespace::Intercept.get_fake_scid(
13502				best_block_height,
13503				&self.chain_hash,
13504				&self.fake_scid_rand_bytes,
13505				&self.entropy_source,
13506			);
13507			// Ensure the generated scid doesn't conflict with a real channel.
13508			if short_to_chan_info.contains_key(&scid_candidate) {
13509				continue;
13510			}
13511			return scid_candidate;
13512		}
13513	}
13514
13515	/// Gets inflight HTLC information by processing pending outbound payments that are in
13516	/// our channels. May be used during pathfinding to account for in-use channel liquidity.
13517	pub fn compute_inflight_htlcs(&self) -> InFlightHtlcs {
13518		let mut inflight_htlcs = InFlightHtlcs::new();
13519
13520		let per_peer_state = self.per_peer_state.read().unwrap();
13521		for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
13522			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
13523			let peer_state = &mut *peer_state_lock;
13524			for chan in peer_state.channel_by_id.values().filter_map(Channel::as_funded) {
13525				for (htlc_source, _) in chan.inflight_htlc_sources() {
13526					if let HTLCSource::OutboundRoute { path, .. } = htlc_source {
13527						inflight_htlcs.process_path(path, self.get_our_node_id());
13528					}
13529				}
13530			}
13531		}
13532
13533		inflight_htlcs
13534	}
13535
13536	#[cfg(any(test, feature = "_test_utils"))]
13537	pub fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
13538		let events = core::cell::RefCell::new(Vec::new());
13539		let event_handler = |event: events::Event| Ok(events.borrow_mut().push(event));
13540		self.process_pending_events(&event_handler);
13541		let collected_events = events.into_inner();
13542
13543		// To expand the coverage and make sure all events are properly serialised and deserialised,
13544		// we test all generated events round-trip:
13545		for event in &collected_events {
13546			let ser = event.encode();
13547			if let Some(deser) =
13548				events::Event::read(&mut &ser[..]).expect("event should deserialize")
13549			{
13550				assert_eq!(&deser, event, "event should roundtrip correctly");
13551			}
13552		}
13553
13554		collected_events
13555	}
13556
13557	#[cfg(feature = "_test_utils")]
13558	pub fn push_pending_event(&self, event: events::Event) {
13559		let mut events = self.pending_events.lock().unwrap();
13560		events.push_back((event, None));
13561	}
13562
13563	#[cfg(test)]
13564	pub fn pop_pending_event(&self) -> Option<events::Event> {
13565		let mut events = self.pending_events.lock().unwrap();
13566		events.pop_front().map(|(e, _)| e)
13567	}
13568
13569	#[cfg(test)]
13570	pub fn has_pending_payments(&self) -> bool {
13571		self.pending_outbound_payments.has_pending_payments()
13572	}
13573
13574	#[cfg(test)]
13575	pub fn clear_pending_payments(&self) {
13576		self.pending_outbound_payments.clear_pending_payments()
13577	}
13578
13579	#[cfg(any(test, feature = "_test_utils"))]
13580	pub(crate) fn get_and_clear_pending_raa_blockers(
13581		&self,
13582	) -> Vec<(ChannelId, Vec<RAAMonitorUpdateBlockingAction>)> {
13583		let per_peer_state = self.per_peer_state.read().unwrap();
13584		let mut pending_blockers = Vec::new();
13585
13586		for (_peer_pubkey, peer_state_mutex) in per_peer_state.iter() {
13587			let mut peer_state = peer_state_mutex.lock().unwrap();
13588
13589			for (chan_id, actions) in peer_state.actions_blocking_raa_monitor_updates.iter() {
13590				// Only collect the non-empty actions into `pending_blockers`.
13591				if !actions.is_empty() {
13592					pending_blockers.push((chan_id.clone(), actions.clone()));
13593				}
13594			}
13595
13596			peer_state.actions_blocking_raa_monitor_updates.clear();
13597		}
13598
13599		pending_blockers
13600	}
13601
13602	/// When something which was blocking a channel from updating its [`ChannelMonitor`] (e.g. an
13603	/// [`Event`] being handled) completes, this should be called to restore the channel to normal
13604	/// operation. It will double-check that nothing *else* is also blocking the same channel from
13605	/// making progress and then let any blocked [`ChannelMonitorUpdate`]s fly.
13606	#[rustfmt::skip]
13607	fn handle_monitor_update_release(
13608		&self, counterparty_node_id: PublicKey, channel_id: ChannelId,
13609		mut completed_blocker: Option<RAAMonitorUpdateBlockingAction>,
13610	) {
13611		let logger = WithContext::from(
13612			&self.logger, Some(counterparty_node_id), Some(channel_id), None
13613		);
13614		loop {
13615			let per_peer_state = self.per_peer_state.read().unwrap();
13616			if let Some(peer_state_mtx) = per_peer_state.get(&counterparty_node_id) {
13617				let mut peer_state_lck = peer_state_mtx.lock().unwrap();
13618				let peer_state = &mut *peer_state_lck;
13619				if let Some(blocker) = completed_blocker.take() {
13620					// Only do this on the first iteration of the loop.
13621					let entry = peer_state.actions_blocking_raa_monitor_updates.entry(channel_id);
13622					if let btree_map::Entry::Occupied(mut entry) = entry {
13623						entry.get_mut().retain(|iter| iter != &blocker);
13624						if entry.get().is_empty() {
13625							entry.remove();
13626						}
13627					}
13628				}
13629
13630				if self.raa_monitor_updates_held(&peer_state.actions_blocking_raa_monitor_updates,
13631					channel_id, counterparty_node_id) {
13632					// Check that, while holding the peer lock, we don't have anything else
13633					// blocking monitor updates for this channel. If we do, release the monitor
13634					// update(s) when those blockers complete.
13635					log_trace!(logger, "Delaying monitor unlock for channel {} as another channel's mon update needs to complete first",
13636						&channel_id);
13637					break;
13638				}
13639
13640				if let hash_map::Entry::Occupied(mut chan_entry) = peer_state.channel_by_id.entry(
13641					channel_id) {
13642					if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
13643						let channel_funding_outpoint = chan.funding_outpoint();
13644						if let Some((monitor_update, further_update_exists)) = chan.unblock_next_blocked_monitor_update() {
13645							log_debug!(logger, "Unlocking monitor updating for channel {} and updating monitor",
13646								channel_id);
13647							handle_new_monitor_update!(self, channel_funding_outpoint, monitor_update,
13648								peer_state_lck, peer_state, per_peer_state, chan);
13649							if further_update_exists {
13650								// If there are more `ChannelMonitorUpdate`s to process, restart at the
13651								// top of the loop.
13652								continue;
13653							}
13654						} else {
13655							log_trace!(logger, "Unlocked monitor updating for channel {} without monitors to update",
13656								channel_id);
13657						}
13658					}
13659				}
13660			} else {
13661				log_debug!(logger,
13662					"Got a release post-RAA monitor update for peer {} but the channel is gone",
13663					log_pubkey!(counterparty_node_id));
13664			}
13665			break;
13666		}
13667	}
13668
13669	fn handle_post_event_actions<I: IntoIterator<Item = EventCompletionAction>>(&self, actions: I) {
13670		for action in actions.into_iter() {
13671			match action {
13672				EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
13673					channel_funding_outpoint: _,
13674					channel_id,
13675					counterparty_node_id,
13676				} => {
13677					let startup_complete =
13678						self.background_events_processed_since_startup.load(Ordering::Acquire);
13679					debug_assert!(startup_complete);
13680					self.handle_monitor_update_release(counterparty_node_id, channel_id, None);
13681				},
13682				EventCompletionAction::ReleasePaymentCompleteChannelMonitorUpdate(
13683					PaymentCompleteUpdate {
13684						counterparty_node_id,
13685						channel_funding_outpoint,
13686						channel_id,
13687						htlc_id,
13688					},
13689				) => {
13690					let per_peer_state = self.per_peer_state.read().unwrap();
13691					let mut peer_state = per_peer_state
13692						.get(&counterparty_node_id)
13693						.map(|state| state.lock().unwrap())
13694						.expect("Channels originating a payment resolution must have peer state");
13695					let update_id = peer_state
13696						.closed_channel_monitor_update_ids
13697						.get_mut(&channel_id)
13698						.expect("Channels originating a payment resolution must have a monitor");
13699					// Note that for channels closed pre-0.1, the latest update_id is `u64::MAX`.
13700					*update_id = update_id.saturating_add(1);
13701
13702					let update = ChannelMonitorUpdate {
13703						update_id: *update_id,
13704						channel_id: Some(channel_id),
13705						updates: vec![ChannelMonitorUpdateStep::ReleasePaymentComplete {
13706							htlc: htlc_id,
13707						}],
13708					};
13709
13710					let during_startup =
13711						!self.background_events_processed_since_startup.load(Ordering::Acquire);
13712					if during_startup {
13713						let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
13714							counterparty_node_id,
13715							funding_txo: channel_funding_outpoint,
13716							channel_id,
13717							update,
13718						};
13719						self.pending_background_events.lock().unwrap().push(event);
13720					} else {
13721						handle_new_monitor_update!(
13722							self,
13723							channel_funding_outpoint,
13724							update,
13725							peer_state,
13726							peer_state,
13727							per_peer_state,
13728							counterparty_node_id,
13729							channel_id,
13730							POST_CHANNEL_CLOSE
13731						);
13732					}
13733				},
13734			}
13735		}
13736	}
13737
13738	/// Processes any events asynchronously in the order they were generated since the last call
13739	/// using the given event handler.
13740	///
13741	/// See the trait-level documentation of [`EventsProvider`] for requirements.
13742	pub async fn process_pending_events_async<
13743		Future: core::future::Future<Output = Result<(), ReplayEvent>>,
13744		H: Fn(Event) -> Future,
13745	>(
13746		&self, handler: H,
13747	) {
13748		let mut ev;
13749		process_events_body!(self, ev, { handler(ev).await });
13750	}
13751}
13752
13753impl<
13754		M: Deref,
13755		T: Deref,
13756		ES: Deref,
13757		NS: Deref,
13758		SP: Deref,
13759		F: Deref,
13760		R: Deref,
13761		MR: Deref,
13762		L: Deref,
13763	> BaseMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
13764where
13765	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
13766	T::Target: BroadcasterInterface,
13767	ES::Target: EntropySource,
13768	NS::Target: NodeSigner,
13769	SP::Target: SignerProvider,
13770	F::Target: FeeEstimator,
13771	R::Target: Router,
13772	MR::Target: MessageRouter,
13773	L::Target: Logger,
13774{
13775	fn provided_node_features(&self) -> NodeFeatures {
13776		provided_node_features(&self.config.read().unwrap())
13777	}
13778
13779	fn provided_init_features(&self, _their_init_features: PublicKey) -> InitFeatures {
13780		provided_init_features(&self.config.read().unwrap())
13781	}
13782
13783	#[rustfmt::skip]
13784	fn peer_disconnected(&self, counterparty_node_id: PublicKey) {
13785		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
13786			let mut splice_failed_events = Vec::new();
13787			let mut failed_channels: Vec<(Result<Infallible, _>, _)> = Vec::new();
13788			let mut per_peer_state = self.per_peer_state.write().unwrap();
13789			let remove_peer = {
13790				log_debug!(
13791					WithContext::from(&self.logger, Some(counterparty_node_id), None, None),
13792					"Marking channels with {} disconnected and generating channel_updates.",
13793					log_pubkey!(counterparty_node_id)
13794				);
13795				if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
13796					let mut peer_state_lock = peer_state_mutex.lock().unwrap();
13797					let peer_state = &mut *peer_state_lock;
13798					let pending_msg_events = &mut peer_state.pending_msg_events;
13799					peer_state.channel_by_id.retain(|_, chan| {
13800						let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
13801						let DisconnectResult { is_resumable, splice_funding_failed } =
13802							chan.peer_disconnected_is_resumable(&&logger);
13803
13804						if let Some(splice_funding_failed) = splice_funding_failed {
13805							splice_failed_events.push(events::Event::SpliceFailed {
13806								channel_id: chan.context().channel_id(),
13807								counterparty_node_id,
13808								user_channel_id: chan.context().get_user_id(),
13809								abandoned_funding_txo: splice_funding_failed.funding_txo,
13810								channel_type: splice_funding_failed.channel_type,
13811								contributed_inputs: splice_funding_failed.contributed_inputs,
13812								contributed_outputs: splice_funding_failed.contributed_outputs,
13813							});
13814						}
13815
13816						if is_resumable {
13817							return true;
13818						}
13819
13820						// Clean up for removal.
13821						let reason = ClosureReason::DisconnectedPeer;
13822						let err = ChannelError::Close((reason.to_string(), reason));
13823						let (_, e) = convert_channel_err!(self, peer_state, err, chan);
13824						failed_channels.push((Err(e), counterparty_node_id));
13825						false
13826					});
13827					// Note that we don't bother generating any events for pre-accept channels -
13828					// they're not considered "channels" yet from the PoV of our events interface.
13829					peer_state.inbound_channel_request_by_id.clear();
13830					pending_msg_events.retain(|msg| {
13831						match msg {
13832							// V1 Channel Establishment
13833							&MessageSendEvent::SendAcceptChannel { .. } => false,
13834							&MessageSendEvent::SendOpenChannel { .. } => false,
13835							&MessageSendEvent::SendFundingCreated { .. } => false,
13836							&MessageSendEvent::SendFundingSigned { .. } => false,
13837							// V2 Channel Establishment
13838							&MessageSendEvent::SendAcceptChannelV2 { .. } => false,
13839							&MessageSendEvent::SendOpenChannelV2 { .. } => false,
13840							// Common Channel Establishment
13841							&MessageSendEvent::SendChannelReady { .. } => false,
13842							&MessageSendEvent::SendAnnouncementSignatures { .. } => false,
13843							// Quiescence
13844							&MessageSendEvent::SendStfu { .. } => false,
13845							// Splicing
13846							&MessageSendEvent::SendSpliceInit { .. } => false,
13847							&MessageSendEvent::SendSpliceAck { .. } => false,
13848							&MessageSendEvent::SendSpliceLocked { .. } => false,
13849							// Interactive Transaction Construction
13850							&MessageSendEvent::SendTxAddInput { .. } => false,
13851							&MessageSendEvent::SendTxAddOutput { .. } => false,
13852							&MessageSendEvent::SendTxRemoveInput { .. } => false,
13853							&MessageSendEvent::SendTxRemoveOutput { .. } => false,
13854							&MessageSendEvent::SendTxComplete { .. } => false,
13855							&MessageSendEvent::SendTxSignatures { .. } => false,
13856							&MessageSendEvent::SendTxInitRbf { .. } => false,
13857							&MessageSendEvent::SendTxAckRbf { .. } => false,
13858							&MessageSendEvent::SendTxAbort { .. } => false,
13859							// Channel Operations
13860							&MessageSendEvent::UpdateHTLCs { .. } => false,
13861							&MessageSendEvent::SendRevokeAndACK { .. } => false,
13862							&MessageSendEvent::SendClosingSigned { .. } => false,
13863							&MessageSendEvent::SendClosingComplete { .. } => false,
13864							&MessageSendEvent::SendClosingSig { .. } => false,
13865							&MessageSendEvent::SendShutdown { .. } => false,
13866							&MessageSendEvent::SendChannelReestablish { .. } => false,
13867							&MessageSendEvent::HandleError { .. } => false,
13868							// Gossip
13869							&MessageSendEvent::SendChannelAnnouncement { .. } => false,
13870							&MessageSendEvent::BroadcastChannelAnnouncement { .. } => true,
13871							// [`ChannelManager::pending_broadcast_events`] holds the [`BroadcastChannelUpdate`]
13872							// This check here is to ensure exhaustivity.
13873							&MessageSendEvent::BroadcastChannelUpdate { .. } => {
13874								debug_assert!(false, "This event shouldn't have been here");
13875								false
13876							},
13877							&MessageSendEvent::BroadcastNodeAnnouncement { .. } => true,
13878							&MessageSendEvent::SendChannelUpdate { .. } => false,
13879							&MessageSendEvent::SendChannelRangeQuery { .. } => false,
13880							&MessageSendEvent::SendShortIdsQuery { .. } => false,
13881							&MessageSendEvent::SendReplyChannelRange { .. } => false,
13882							&MessageSendEvent::SendGossipTimestampFilter { .. } => false,
13883
13884							// Peer Storage
13885							&MessageSendEvent::SendPeerStorage { .. } => false,
13886							&MessageSendEvent::SendPeerStorageRetrieval { .. } => false,
13887						}
13888					});
13889					debug_assert!(peer_state.is_connected, "A disconnected peer cannot disconnect");
13890					peer_state.is_connected = false;
13891					peer_state.ok_to_remove(true)
13892				} else { debug_assert!(false, "Unconnected peer disconnected"); true }
13893			};
13894			if remove_peer {
13895				per_peer_state.remove(&counterparty_node_id);
13896			}
13897			mem::drop(per_peer_state);
13898
13899			let persist = if splice_failed_events.is_empty() {
13900				NotifyOption::SkipPersistHandleEvents
13901			} else {
13902				let mut pending_events = self.pending_events.lock().unwrap();
13903				for event in splice_failed_events {
13904					pending_events.push_back((event, None));
13905				}
13906				NotifyOption::DoPersist
13907			};
13908
13909			for (err, counterparty_node_id) in failed_channels.drain(..) {
13910				let _ = handle_error!(self, err, counterparty_node_id);
13911			}
13912
13913			persist
13914		});
13915	}
13916
13917	#[rustfmt::skip]
13918	fn peer_connected(&self, counterparty_node_id: PublicKey, init_msg: &msgs::Init, inbound: bool) -> Result<(), ()> {
13919		let logger = WithContext::from(&self.logger, Some(counterparty_node_id), None, None);
13920		if !init_msg.features.supports_static_remote_key() {
13921			log_debug!(logger, "Peer {} does not support static remote key, disconnecting", log_pubkey!(counterparty_node_id));
13922			return Err(());
13923		}
13924
13925		let mut res = Ok(());
13926
13927		PersistenceNotifierGuard::optionally_notify(self, || {
13928			// If we have too many peers connected which don't have funded channels, disconnect the
13929			// peer immediately (as long as it doesn't have funded channels). If we have a bunch of
13930			// unfunded channels taking up space in memory for disconnected peers, we still let new
13931			// peers connect, but we'll reject new channels from them.
13932			let connected_peers_without_funded_channels = self.peers_without_funded_channels(|node| node.is_connected);
13933			let inbound_peer_limited = inbound && connected_peers_without_funded_channels >= MAX_NO_CHANNEL_PEERS;
13934
13935			{
13936				let mut peer_state_lock = self.per_peer_state.write().unwrap();
13937				match peer_state_lock.entry(counterparty_node_id) {
13938					hash_map::Entry::Vacant(e) => {
13939						if inbound_peer_limited {
13940							res = Err(());
13941							return NotifyOption::SkipPersistNoEvents;
13942						}
13943						e.insert(Mutex::new(PeerState {
13944							channel_by_id: new_hash_map(),
13945							inbound_channel_request_by_id: new_hash_map(),
13946							latest_features: init_msg.features.clone(),
13947							pending_msg_events: Vec::new(),
13948							in_flight_monitor_updates: BTreeMap::new(),
13949							monitor_update_blocked_actions: BTreeMap::new(),
13950							actions_blocking_raa_monitor_updates: BTreeMap::new(),
13951							closed_channel_monitor_update_ids: BTreeMap::new(),
13952							is_connected: true,
13953							peer_storage: Vec::new(),
13954						}));
13955					},
13956					hash_map::Entry::Occupied(e) => {
13957						let mut peer_state = e.get().lock().unwrap();
13958						peer_state.latest_features = init_msg.features.clone();
13959
13960						let best_block_height = self.best_block.read().unwrap().height;
13961						if inbound_peer_limited &&
13962							Self::unfunded_channel_count(&*peer_state, best_block_height) ==
13963							peer_state.channel_by_id.len()
13964						{
13965							res = Err(());
13966							return NotifyOption::SkipPersistNoEvents;
13967						}
13968
13969						debug_assert!(peer_state.pending_msg_events.is_empty());
13970						peer_state.pending_msg_events.clear();
13971
13972						debug_assert!(!peer_state.is_connected, "A peer shouldn't be connected twice");
13973						peer_state.is_connected = true;
13974					},
13975				}
13976			}
13977
13978			log_debug!(logger, "Generating channel_reestablish events for {}", log_pubkey!(counterparty_node_id));
13979
13980			let per_peer_state = self.per_peer_state.read().unwrap();
13981			if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
13982				let mut peer_state_lock = peer_state_mutex.lock().unwrap();
13983				let peer_state = &mut *peer_state_lock;
13984				let pending_msg_events = &mut peer_state.pending_msg_events;
13985
13986				if !peer_state.peer_storage.is_empty() {
13987					pending_msg_events.push(MessageSendEvent::SendPeerStorageRetrieval {
13988						node_id: counterparty_node_id.clone(),
13989						msg: msgs::PeerStorageRetrieval {
13990							data: peer_state.peer_storage.clone()
13991						},
13992					});
13993				}
13994
13995				for (_, chan) in peer_state.channel_by_id.iter_mut() {
13996					let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
13997					match chan.peer_connected_get_handshake(self.chain_hash, &&logger) {
13998						ReconnectionMsg::Reestablish(msg) =>
13999							pending_msg_events.push(MessageSendEvent::SendChannelReestablish {
14000								node_id: chan.context().get_counterparty_node_id(),
14001								msg,
14002							}),
14003						ReconnectionMsg::Open(OpenChannelMessage::V1(msg)) =>
14004							pending_msg_events.push(MessageSendEvent::SendOpenChannel {
14005								node_id: chan.context().get_counterparty_node_id(),
14006								msg,
14007							}),
14008						ReconnectionMsg::Open(OpenChannelMessage::V2(msg)) =>
14009							pending_msg_events.push(MessageSendEvent::SendOpenChannelV2 {
14010								node_id: chan.context().get_counterparty_node_id(),
14011								msg,
14012							}),
14013						ReconnectionMsg::None => {},
14014					}
14015				}
14016			}
14017
14018			return NotifyOption::SkipPersistHandleEvents;
14019			//TODO: Also re-broadcast announcement_signatures
14020		});
14021
14022		// While we usually refresh the AsyncReceiveOfferCache on a timer, we also want to start
14023		// interactively building offers as soon as we can after startup. We can't start building offers
14024		// until we have some peer connection(s) to receive onion messages over, so as a minor optimization
14025		// refresh the cache when a peer connects.
14026		self.check_refresh_async_receive_offer_cache(false);
14027		res
14028	}
14029
14030	/// Returns `MessageSendEvent`s strictly ordered per-peer, in the order they were generated.
14031	/// The returned array will contain `MessageSendEvent`s for different peers if
14032	/// `MessageSendEvent`s to more than one peer exists, but `MessageSendEvent`s to the same peer
14033	/// is always placed next to each other.
14034	///
14035	/// Note that that while `MessageSendEvent`s are strictly ordered per-peer, the peer order for
14036	/// the chunks of `MessageSendEvent`s for different peers is random. I.e. if the array contains
14037	/// `MessageSendEvent`s  for both `node_a` and `node_b`, the `MessageSendEvent`s for `node_a`
14038	/// will randomly be placed first or last in the returned array.
14039	///
14040	/// Note that even though `BroadcastChannelAnnouncement` and `BroadcastChannelUpdate`
14041	/// `MessageSendEvent`s are intended to be broadcasted to all peers, they will be placed among
14042	/// the `MessageSendEvent`s to the specific peer they were generated under.
14043	fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
14044		let events = RefCell::new(Vec::new());
14045		PersistenceNotifierGuard::optionally_notify(self, || {
14046			let mut result = NotifyOption::SkipPersistNoEvents;
14047
14048			// TODO: This behavior should be documented. It's unintuitive that we query
14049			// ChannelMonitors when clearing other events.
14050			if self.process_pending_monitor_events() {
14051				result = NotifyOption::DoPersist;
14052			}
14053
14054			if self.check_free_holding_cells() {
14055				result = NotifyOption::DoPersist;
14056			}
14057			if self.maybe_generate_initial_closing_signed() {
14058				result = NotifyOption::DoPersist;
14059			}
14060
14061			// Quiescence is an in-memory protocol, so we don't have to persist because of it.
14062			self.maybe_send_stfu();
14063
14064			let mut is_any_peer_connected = false;
14065			let mut pending_events = Vec::new();
14066			let per_peer_state = self.per_peer_state.read().unwrap();
14067			for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
14068				let mut peer_state_lock = peer_state_mutex.lock().unwrap();
14069				let peer_state = &mut *peer_state_lock;
14070				if peer_state.pending_msg_events.len() > 0 {
14071					pending_events.append(&mut peer_state.pending_msg_events);
14072				}
14073				if peer_state.is_connected {
14074					is_any_peer_connected = true
14075				}
14076			}
14077
14078			// Ensure that we are connected to some peers before getting broadcast messages.
14079			if is_any_peer_connected {
14080				let mut broadcast_msgs = self.pending_broadcast_messages.lock().unwrap();
14081				pending_events.append(&mut broadcast_msgs);
14082			}
14083
14084			if !pending_events.is_empty() {
14085				events.replace(pending_events);
14086			}
14087
14088			result
14089		});
14090		events.into_inner()
14091	}
14092}
14093
14094impl<
14095		M: Deref,
14096		T: Deref,
14097		ES: Deref,
14098		NS: Deref,
14099		SP: Deref,
14100		F: Deref,
14101		R: Deref,
14102		MR: Deref,
14103		L: Deref,
14104	> EventsProvider for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
14105where
14106	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
14107	T::Target: BroadcasterInterface,
14108	ES::Target: EntropySource,
14109	NS::Target: NodeSigner,
14110	SP::Target: SignerProvider,
14111	F::Target: FeeEstimator,
14112	R::Target: Router,
14113	MR::Target: MessageRouter,
14114	L::Target: Logger,
14115{
14116	/// Processes events that must be periodically handled.
14117	///
14118	/// An [`EventHandler`] may safely call back to the provider in order to handle an event.
14119	/// However, it must not call [`Writeable::write`] as doing so would result in a deadlock.
14120	fn process_pending_events<H: Deref>(&self, handler: H)
14121	where
14122		H::Target: EventHandler,
14123	{
14124		let mut ev;
14125		process_events_body!(self, ev, handler.handle_event(ev));
14126	}
14127}
14128
14129impl<
14130		M: Deref,
14131		T: Deref,
14132		ES: Deref,
14133		NS: Deref,
14134		SP: Deref,
14135		F: Deref,
14136		R: Deref,
14137		MR: Deref,
14138		L: Deref,
14139	> chain::Listen for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
14140where
14141	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
14142	T::Target: BroadcasterInterface,
14143	ES::Target: EntropySource,
14144	NS::Target: NodeSigner,
14145	SP::Target: SignerProvider,
14146	F::Target: FeeEstimator,
14147	R::Target: Router,
14148	MR::Target: MessageRouter,
14149	L::Target: Logger,
14150{
14151	fn filtered_block_connected(&self, header: &Header, txdata: &TransactionData, height: u32) {
14152		let is_rescan = {
14153			let best_block = self.best_block.read().unwrap();
14154			let is_rescan =
14155				best_block.block_hash == header.block_hash() && best_block.height == height;
14156			if !is_rescan {
14157				assert_eq!(best_block.block_hash, header.prev_blockhash,
14158					"Blocks must be connected in chain-order - the connected header must build on the last connected header");
14159				assert_eq!(best_block.height, height - 1,
14160					"Blocks must be connected in chain-order - the connected block height must be one greater than the previous height");
14161			}
14162			is_rescan
14163		};
14164
14165		self.transactions_confirmed(header, txdata, height);
14166		if !is_rescan {
14167			self.best_block_updated(header, height);
14168		}
14169	}
14170
14171	fn blocks_disconnected(&self, fork_point: BestBlock) {
14172		let _persistence_guard =
14173			PersistenceNotifierGuard::optionally_notify_skipping_background_events(
14174				self,
14175				|| -> NotifyOption { NotifyOption::DoPersist },
14176			);
14177		{
14178			let mut best_block = self.best_block.write().unwrap();
14179			assert!(best_block.height > fork_point.height,
14180				"Blocks disconnected must indicate disconnection from the current best height, i.e. the new chain tip must be lower than the previous best height");
14181			*best_block = fork_point;
14182		}
14183
14184		self.do_chain_event(Some(fork_point.height), |channel| {
14185			channel.best_block_updated(
14186				fork_point.height,
14187				None,
14188				self.chain_hash,
14189				&self.node_signer,
14190				&self.config.read().unwrap(),
14191				&&WithChannelContext::from(&self.logger, &channel.context, None),
14192			)
14193		});
14194	}
14195}
14196
14197impl<
14198		M: Deref,
14199		T: Deref,
14200		ES: Deref,
14201		NS: Deref,
14202		SP: Deref,
14203		F: Deref,
14204		R: Deref,
14205		MR: Deref,
14206		L: Deref,
14207	> chain::Confirm for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
14208where
14209	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
14210	T::Target: BroadcasterInterface,
14211	ES::Target: EntropySource,
14212	NS::Target: NodeSigner,
14213	SP::Target: SignerProvider,
14214	F::Target: FeeEstimator,
14215	R::Target: Router,
14216	MR::Target: MessageRouter,
14217	L::Target: Logger,
14218{
14219	#[rustfmt::skip]
14220	fn transactions_confirmed(&self, header: &Header, txdata: &TransactionData, height: u32) {
14221		// Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
14222		// during initialization prior to the chain_monitor being fully configured in some cases.
14223		// See the docs for `ChannelManagerReadArgs` for more.
14224
14225		let block_hash = header.block_hash();
14226		log_trace!(self.logger, "{} transactions included in block {} at height {} provided", txdata.len(), block_hash, height);
14227
14228		let _persistence_guard =
14229			PersistenceNotifierGuard::optionally_notify_skipping_background_events(
14230				self, || -> NotifyOption { NotifyOption::DoPersist });
14231		self.do_chain_event(Some(height), |channel| channel.transactions_confirmed(&block_hash, height, txdata, self.chain_hash, &self.node_signer, &self.config.read().unwrap(), &&WithChannelContext::from(&self.logger, &channel.context, None))
14232			.map(|(a, b)| (a, Vec::new(), b)));
14233
14234		let last_best_block_height = self.best_block.read().unwrap().height;
14235		if height < last_best_block_height {
14236			let timestamp = self.highest_seen_timestamp.load(Ordering::Acquire);
14237			let do_update = |channel: &mut FundedChannel<SP>| {
14238				channel.best_block_updated(
14239					last_best_block_height,
14240					Some(timestamp as u32),
14241					self.chain_hash,
14242					&self.node_signer,
14243					&self.config.read().unwrap(),
14244					&&WithChannelContext::from(&self.logger, &channel.context, None),
14245				)
14246			};
14247			self.do_chain_event(Some(last_best_block_height), do_update);
14248		}
14249	}
14250
14251	#[rustfmt::skip]
14252	fn best_block_updated(&self, header: &Header, height: u32) {
14253		// Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
14254		// during initialization prior to the chain_monitor being fully configured in some cases.
14255		// See the docs for `ChannelManagerReadArgs` for more.
14256
14257		let block_hash = header.block_hash();
14258		log_trace!(self.logger, "New best block: {} at height {}", block_hash, height);
14259
14260		let _persistence_guard =
14261			PersistenceNotifierGuard::optionally_notify_skipping_background_events(
14262				self, || -> NotifyOption { NotifyOption::DoPersist });
14263		*self.best_block.write().unwrap() = BestBlock::new(block_hash, height);
14264
14265		let mut min_anchor_feerate = None;
14266		let mut min_non_anchor_feerate = None;
14267		if self.background_events_processed_since_startup.load(Ordering::Relaxed) {
14268			// If we're past the startup phase, update our feerate cache
14269			let mut last_days_feerates = self.last_days_feerates.lock().unwrap();
14270			if last_days_feerates.len() >= FEERATE_TRACKING_BLOCKS {
14271				last_days_feerates.pop_front();
14272			}
14273			let anchor_feerate = self.fee_estimator
14274				.bounded_sat_per_1000_weight(ConfirmationTarget::MinAllowedAnchorChannelRemoteFee);
14275			let non_anchor_feerate = self.fee_estimator
14276				.bounded_sat_per_1000_weight(ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee);
14277			last_days_feerates.push_back((anchor_feerate, non_anchor_feerate));
14278			if last_days_feerates.len() >= FEERATE_TRACKING_BLOCKS {
14279				min_anchor_feerate = last_days_feerates.iter().map(|(f, _)| f).min().copied();
14280				min_non_anchor_feerate = last_days_feerates.iter().map(|(_, f)| f).min().copied();
14281			}
14282		}
14283
14284		self.do_chain_event(Some(height), |channel| {
14285			let logger = WithChannelContext::from(&self.logger, &channel.context, None);
14286			if channel.funding.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
14287				if let Some(feerate) = min_anchor_feerate {
14288					channel.check_for_stale_feerate(&logger, feerate)?;
14289				}
14290			} else {
14291				if let Some(feerate) = min_non_anchor_feerate {
14292					channel.check_for_stale_feerate(&logger, feerate)?;
14293				}
14294			}
14295
14296			// Remove any SCIDs used by older funding transactions
14297			{
14298				let legacy_scids = channel.remove_legacy_scids_before_block(height);
14299				if !legacy_scids.as_slice().is_empty() {
14300					let mut short_to_chan_info = self.short_to_chan_info.write().unwrap();
14301					for scid in legacy_scids {
14302						short_to_chan_info.remove(&scid);
14303					}
14304				}
14305			}
14306
14307			channel.best_block_updated(
14308				height,
14309				Some(header.time),
14310				self.chain_hash,
14311				&self.node_signer,
14312				&self.config.read().unwrap(),
14313				&&WithChannelContext::from(&self.logger, &channel.context, None),
14314			)
14315		});
14316
14317		macro_rules! max_time {
14318			($timestamp: expr) => {
14319				loop {
14320					// Update $timestamp to be the max of its current value and the block
14321					// timestamp. This should keep us close to the current time without relying on
14322					// having an explicit local time source.
14323					// Just in case we end up in a race, we loop until we either successfully
14324					// update $timestamp or decide we don't need to.
14325					let old_serial = $timestamp.load(Ordering::Acquire);
14326					if old_serial >= header.time as usize { break; }
14327					if $timestamp.compare_exchange(old_serial, header.time as usize, Ordering::AcqRel, Ordering::Relaxed).is_ok() {
14328						break;
14329					}
14330				}
14331			}
14332		}
14333		max_time!(self.highest_seen_timestamp);
14334
14335		self.flow.best_block_updated(header, height);
14336	}
14337
14338	fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)> {
14339		let mut res = Vec::with_capacity(self.short_to_chan_info.read().unwrap().len());
14340		for (_cp_id, peer_state_mutex) in self.per_peer_state.read().unwrap().iter() {
14341			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
14342			let peer_state = &mut *peer_state_lock;
14343			for chan in peer_state.channel_by_id.values().filter_map(Channel::as_funded) {
14344				for (funding_txid, conf_height, block_hash) in chan.get_relevant_txids() {
14345					res.push((funding_txid, conf_height, block_hash));
14346				}
14347			}
14348		}
14349		res
14350	}
14351
14352	fn transaction_unconfirmed(&self, txid: &Txid) {
14353		let _persistence_guard =
14354			PersistenceNotifierGuard::optionally_notify_skipping_background_events(
14355				self,
14356				|| -> NotifyOption { NotifyOption::DoPersist },
14357			);
14358		self.do_chain_event(None, |channel| {
14359			let logger = WithChannelContext::from(&self.logger, &channel.context, None);
14360			channel.transaction_unconfirmed(txid, &&logger).map(|()| (None, Vec::new(), None))
14361		});
14362	}
14363}
14364
14365pub(super) enum FundingConfirmedMessage {
14366	Establishment(msgs::ChannelReady),
14367	Splice(msgs::SpliceLocked, Option<OutPoint>, Option<ChannelMonitorUpdate>, Vec<FundingInfo>),
14368}
14369
14370impl<
14371		M: Deref,
14372		T: Deref,
14373		ES: Deref,
14374		NS: Deref,
14375		SP: Deref,
14376		F: Deref,
14377		R: Deref,
14378		MR: Deref,
14379		L: Deref,
14380	> ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
14381where
14382	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
14383	T::Target: BroadcasterInterface,
14384	ES::Target: EntropySource,
14385	NS::Target: NodeSigner,
14386	SP::Target: SignerProvider,
14387	F::Target: FeeEstimator,
14388	R::Target: Router,
14389	MR::Target: MessageRouter,
14390	L::Target: Logger,
14391{
14392	/// Calls a function which handles an on-chain event (blocks dis/connected, transactions
14393	/// un/confirmed, etc) on each channel, handling any resulting errors or messages generated by
14394	/// the function.
14395	#[rustfmt::skip]
14396	fn do_chain_event<FN: Fn(&mut FundedChannel<SP>) -> Result<(Option<FundingConfirmedMessage>, Vec<(HTLCSource, PaymentHash)>, Option<msgs::AnnouncementSignatures>), ClosureReason>>
14397			(&self, height_opt: Option<u32>, f: FN) {
14398		// Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
14399		// during initialization prior to the chain_monitor being fully configured in some cases.
14400		// See the docs for `ChannelManagerReadArgs` for more.
14401
14402		let mut failed_channels: Vec<(Result<Infallible, _>, _)> = Vec::new();
14403		let mut timed_out_htlcs = Vec::new();
14404		let mut to_process_monitor_update_actions = Vec::new();
14405		{
14406			let per_peer_state = self.per_peer_state.read().unwrap();
14407			for (counterparty_node_id, peer_state_mutex) in per_peer_state.iter() {
14408				let mut peer_state_lock = peer_state_mutex.lock().unwrap();
14409				let peer_state = &mut *peer_state_lock;
14410				let pending_msg_events = &mut peer_state.pending_msg_events;
14411
14412				peer_state.channel_by_id.retain(|channel_id, chan| {
14413					match chan.as_funded_mut() {
14414						// Retain unfunded channels.
14415						None => true,
14416						Some(funded_channel) => {
14417							let res = f(funded_channel);
14418							if let Ok((funding_confirmed_opt, mut timed_out_pending_htlcs, announcement_sigs)) = res {
14419								for (source, payment_hash) in timed_out_pending_htlcs.drain(..) {
14420									let reason = LocalHTLCFailureReason::CLTVExpiryTooSoon;
14421									let data = self.get_htlc_inbound_temp_fail_data(reason);
14422									timed_out_htlcs.push((source, payment_hash, HTLCFailReason::reason(reason, data),
14423										HTLCHandlingFailureType::Forward { node_id: Some(funded_channel.context.get_counterparty_node_id()), channel_id: *channel_id }));
14424								}
14425								let logger = WithChannelContext::from(&self.logger, &funded_channel.context, None);
14426								match funding_confirmed_opt {
14427									Some(FundingConfirmedMessage::Establishment(channel_ready)) => {
14428										send_channel_ready!(self, pending_msg_events, funded_channel, channel_ready);
14429										if funded_channel.context.is_usable() && peer_state.is_connected {
14430											log_trace!(logger, "Sending channel_ready with private initial channel_update for our counterparty on channel {}", channel_id);
14431											if let Ok(msg) = self.get_channel_update_for_unicast(funded_channel) {
14432												pending_msg_events.push(MessageSendEvent::SendChannelUpdate {
14433													node_id: funded_channel.context.get_counterparty_node_id(),
14434													msg,
14435												});
14436											}
14437										} else {
14438											log_trace!(logger, "Sending channel_ready WITHOUT channel_update for {}", channel_id);
14439										}
14440									},
14441									Some(FundingConfirmedMessage::Splice(splice_locked, funding_txo, monitor_update_opt, discarded_funding)) => {
14442										let counterparty_node_id = funded_channel.context.get_counterparty_node_id();
14443										let channel_id = funded_channel.context.channel_id();
14444
14445										if let Some(funding_txo) = funding_txo {
14446											let mut short_to_chan_info = self.short_to_chan_info.write().unwrap();
14447											insert_short_channel_id!(short_to_chan_info, funded_channel);
14448
14449											if let Some(monitor_update) = monitor_update_opt {
14450												handle_new_monitor_update!(
14451													self,
14452													funding_txo,
14453													monitor_update,
14454													peer_state,
14455													funded_channel.context,
14456													REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER
14457												);
14458												to_process_monitor_update_actions.push((
14459													counterparty_node_id, channel_id
14460												));
14461											}
14462
14463											let mut pending_events = self.pending_events.lock().unwrap();
14464											pending_events.push_back((events::Event::ChannelReady {
14465												channel_id,
14466												user_channel_id: funded_channel.context.get_user_id(),
14467												counterparty_node_id,
14468												funding_txo: Some(funding_txo.into_bitcoin_outpoint()),
14469												channel_type: funded_channel.funding.get_channel_type().clone(),
14470											}, None));
14471											discarded_funding.into_iter().for_each(|funding_info| {
14472												let event = Event::DiscardFunding {
14473													channel_id: funded_channel.context.channel_id(),
14474													funding_info,
14475												};
14476												pending_events.push_back((event, None));
14477											});
14478										}
14479
14480										if funded_channel.context.is_connected() {
14481											pending_msg_events.push(MessageSendEvent::SendSpliceLocked {
14482												node_id: counterparty_node_id,
14483												msg: splice_locked,
14484											});
14485										}
14486									},
14487									None => {},
14488								}
14489
14490								{
14491									let mut pending_events = self.pending_events.lock().unwrap();
14492									emit_initial_channel_ready_event!(pending_events, funded_channel);
14493								}
14494
14495								if let Some(height) = height_opt {
14496									// (re-)broadcast signed `channel_announcement`s and
14497									// `channel_update`s for any channels less than a week old.
14498									let funding_conf_height =
14499										funded_channel.funding.get_funding_tx_confirmation_height().unwrap_or(height);
14500									// To avoid broadcast storms after each block, only
14501									// re-broadcast every hour (6 blocks) after the initial
14502									// broadcast, or if this is the first time we're ready to
14503									// broadcast this channel.
14504									let rebroadcast_announcement = funding_conf_height < height + 1008
14505										&& funding_conf_height % 6 == height % 6;
14506									#[allow(unused_mut, unused_assignments)]
14507									let mut should_announce = announcement_sigs.is_some() || rebroadcast_announcement;
14508									// Most of our tests were written when we only broadcasted
14509									// `channel_announcement`s once and then never re-broadcasted
14510									// them again, so disable the re-broadcasting entirely in tests
14511									#[cfg(any(test, feature = "_test_utils"))]
14512									{
14513										should_announce = announcement_sigs.is_some();
14514									}
14515									if should_announce {
14516										if let Some(announcement) = funded_channel.get_signed_channel_announcement(
14517											&self.node_signer, self.chain_hash, height, &self.config.read().unwrap(),
14518										) {
14519											pending_msg_events.push(MessageSendEvent::BroadcastChannelAnnouncement {
14520												msg: announcement,
14521												// Note that get_signed_channel_announcement fails
14522												// if the channel cannot be announced, so
14523												// get_channel_update_for_broadcast will never fail
14524												// by the time we get here.
14525												update_msg: Some(self.get_channel_update_for_broadcast(funded_channel).unwrap()),
14526											});
14527										}
14528									}
14529								}
14530								if let Some(announcement_sigs) = announcement_sigs {
14531									if peer_state.is_connected {
14532										log_trace!(logger, "Sending announcement_signatures for channel {}", funded_channel.context.channel_id());
14533										pending_msg_events.push(MessageSendEvent::SendAnnouncementSignatures {
14534											node_id: funded_channel.context.get_counterparty_node_id(),
14535											msg: announcement_sigs,
14536										});
14537									}
14538								}
14539								if funded_channel.is_our_channel_ready() {
14540									if let Some(real_scid) = funded_channel.funding.get_short_channel_id() {
14541										// If we sent a 0conf channel_ready, and now have an SCID, we add it
14542										// to the short_to_chan_info map here. Note that we check whether we
14543										// can relay using the real SCID at relay-time (i.e.
14544										// enforce option_scid_alias then), and if the funding tx is ever
14545										// un-confirmed we force-close the channel, ensuring short_to_chan_info
14546										// is always consistent.
14547										let mut short_to_chan_info = self.short_to_chan_info.write().unwrap();
14548										let scid_insert = short_to_chan_info.insert(real_scid, (funded_channel.context.get_counterparty_node_id(), *channel_id));
14549										assert!(scid_insert.is_none() || scid_insert.unwrap() == (funded_channel.context.get_counterparty_node_id(), *channel_id),
14550											"SCIDs should never collide - ensure you weren't behind by a full {} blocks when creating channels",
14551											fake_scid::MAX_SCID_BLOCKS_FROM_NOW);
14552									}
14553								}
14554							} else if let Err(reason) = res {
14555								// It looks like our counterparty went on-chain or funding transaction was
14556								// reorged out of the main chain. Close the channel.
14557								let err = ChannelError::Close((reason.to_string(), reason));
14558								let (_, e) = convert_channel_err!(
14559									self,
14560									peer_state,
14561									err,
14562									funded_channel,
14563									FUNDED_CHANNEL
14564								);
14565								failed_channels.push((Err(e), *counterparty_node_id));
14566								return false;
14567							}
14568							true
14569						}
14570					}
14571				});
14572			}
14573		}
14574
14575		for (counterparty_node_id, channel_id) in to_process_monitor_update_actions {
14576			self.channel_monitor_updated(&channel_id, None, &counterparty_node_id);
14577		}
14578
14579		if let Some(height) = height_opt {
14580			self.claimable_payments.lock().unwrap().claimable_payments.retain(|payment_hash, payment| {
14581				payment.htlcs.retain(|htlc| {
14582					// If height is approaching the number of blocks we think it takes us to get
14583					// our commitment transaction confirmed before the HTLC expires, plus the
14584					// number of blocks we generally consider it to take to do a commitment update,
14585					// just give up on it and fail the HTLC.
14586					if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
14587						let reason = LocalHTLCFailureReason::PaymentClaimBuffer;
14588						timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(),
14589							HTLCFailReason::reason(reason, invalid_payment_err_data(htlc.value, height)),
14590							HTLCHandlingFailureType::Receive { payment_hash: payment_hash.clone() }));
14591						false
14592					} else { true }
14593				});
14594				!payment.htlcs.is_empty() // Only retain this entry if htlcs has at least one entry.
14595			});
14596
14597			let mut intercepted_htlcs = self.pending_intercepted_htlcs.lock().unwrap();
14598			intercepted_htlcs.retain(|_, htlc| {
14599				if height >= htlc.forward_info.outgoing_cltv_value - HTLC_FAIL_BACK_BUFFER {
14600					let prev_hop_data = HTLCSource::PreviousHopData(htlc.htlc_previous_hop_data());
14601					let requested_forward_scid /* intercept scid */ = match htlc.forward_info.routing {
14602						PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
14603						_ => unreachable!(),
14604					};
14605					timed_out_htlcs.push((prev_hop_data, htlc.forward_info.payment_hash,
14606							HTLCFailReason::from_failure_code(LocalHTLCFailureReason::ForwardExpiryBuffer),
14607							HTLCHandlingFailureType::InvalidForward { requested_forward_scid }));
14608					let logger = WithContext::from(
14609						&self.logger, None, Some(htlc.prev_channel_id), Some(htlc.forward_info.payment_hash)
14610					);
14611					log_trace!(logger, "Timing out intercepted HTLC with requested forward scid {}", requested_forward_scid);
14612					false
14613				} else { true }
14614			});
14615		}
14616
14617		for (failure, counterparty_node_id) in failed_channels {
14618			let _ = handle_error!(self, failure, counterparty_node_id);
14619		}
14620
14621		for (source, payment_hash, reason, destination) in timed_out_htlcs.drain(..) {
14622			self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, destination, None);
14623		}
14624	}
14625
14626	/// Gets a [`Future`] that completes when this [`ChannelManager`] may need to be persisted or
14627	/// may have events that need processing.
14628	///
14629	/// In order to check if this [`ChannelManager`] needs persisting, call
14630	/// [`Self::get_and_clear_needs_persistence`].
14631	///
14632	/// Note that callbacks registered on the [`Future`] MUST NOT call back into this
14633	/// [`ChannelManager`] and should instead register actions to be taken later.
14634	pub fn get_event_or_persistence_needed_future(&self) -> Future {
14635		self.event_persist_notifier.get_future()
14636	}
14637
14638	/// Returns true if this [`ChannelManager`] needs to be persisted.
14639	///
14640	/// See [`Self::get_event_or_persistence_needed_future`] for retrieving a [`Future`] that
14641	/// indicates this should be checked.
14642	pub fn get_and_clear_needs_persistence(&self) -> bool {
14643		self.needs_persist_flag.swap(false, Ordering::AcqRel)
14644	}
14645
14646	#[cfg(any(test, feature = "_test_utils"))]
14647	pub fn get_event_or_persist_condvar_value(&self) -> bool {
14648		self.event_persist_notifier.notify_pending()
14649	}
14650
14651	/// Gets the latest best block which was connected either via the [`chain::Listen`] or
14652	/// [`chain::Confirm`] interfaces.
14653	pub fn current_best_block(&self) -> BestBlock {
14654		self.best_block.read().unwrap().clone()
14655	}
14656
14657	/// Fetches the set of [`NodeFeatures`] flags that are provided by or required by
14658	/// [`ChannelManager`].
14659	pub fn node_features(&self) -> NodeFeatures {
14660		provided_node_features(&self.config.read().unwrap())
14661	}
14662
14663	/// Fetches the set of [`Bolt11InvoiceFeatures`] flags that are provided by or required by
14664	/// [`ChannelManager`].
14665	///
14666	/// Note that the invoice feature flags can vary depending on if the invoice is a "phantom invoice"
14667	/// or not. Thus, this method is not public.
14668	#[cfg(any(feature = "_test_utils", test))]
14669	pub fn bolt11_invoice_features(&self) -> Bolt11InvoiceFeatures {
14670		provided_bolt11_invoice_features(&self.config.read().unwrap())
14671	}
14672
14673	/// Fetches the set of [`Bolt12InvoiceFeatures`] flags that are provided by or required by
14674	/// [`ChannelManager`].
14675	fn bolt12_invoice_features(&self) -> Bolt12InvoiceFeatures {
14676		provided_bolt12_invoice_features(&self.config.read().unwrap())
14677	}
14678
14679	/// Fetches the set of [`ChannelFeatures`] flags that are provided by or required by
14680	/// [`ChannelManager`].
14681	pub fn channel_features(&self) -> ChannelFeatures {
14682		provided_channel_features(&self.config.read().unwrap())
14683	}
14684
14685	/// Fetches the set of [`ChannelTypeFeatures`] flags that are provided by or required by
14686	/// [`ChannelManager`].
14687	pub fn channel_type_features(&self) -> ChannelTypeFeatures {
14688		provided_channel_type_features(&self.config.read().unwrap())
14689	}
14690
14691	/// Fetches the set of [`InitFeatures`] flags that are provided by or required by
14692	/// [`ChannelManager`].
14693	pub fn init_features(&self) -> InitFeatures {
14694		provided_init_features(&self.config.read().unwrap())
14695	}
14696}
14697
14698impl<
14699		M: Deref,
14700		T: Deref,
14701		ES: Deref,
14702		NS: Deref,
14703		SP: Deref,
14704		F: Deref,
14705		R: Deref,
14706		MR: Deref,
14707		L: Deref,
14708	> ChannelMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
14709where
14710	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
14711	T::Target: BroadcasterInterface,
14712	ES::Target: EntropySource,
14713	NS::Target: NodeSigner,
14714	SP::Target: SignerProvider,
14715	F::Target: FeeEstimator,
14716	R::Target: Router,
14717	MR::Target: MessageRouter,
14718	L::Target: Logger,
14719{
14720	fn handle_open_channel(&self, counterparty_node_id: PublicKey, message: &msgs::OpenChannel) {
14721		// Note that we never need to persist the updated ChannelManager for an inbound
14722		// open_channel message - pre-funded channels are never written so there should be no
14723		// change to the contents.
14724		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
14725			let msg = OpenChannelMessageRef::V1(message);
14726			let res = self.internal_open_channel(&counterparty_node_id, msg);
14727			let persist = match &res {
14728				Err(e) if e.closes_channel() => {
14729					debug_assert!(false, "We shouldn't close a new channel");
14730					NotifyOption::DoPersist
14731				},
14732				_ => NotifyOption::SkipPersistHandleEvents,
14733			};
14734			let _ = handle_error!(self, res, counterparty_node_id);
14735			persist
14736		});
14737	}
14738
14739	#[rustfmt::skip]
14740	fn handle_open_channel_v2(&self, counterparty_node_id: PublicKey, msg: &msgs::OpenChannelV2) {
14741		if !self.init_features().supports_dual_fund() {
14742			let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
14743				"Dual-funded channels not supported".to_owned(),
14744				msg.common_fields.temporary_channel_id.clone())), counterparty_node_id);
14745			return;
14746		}
14747		// Note that we never need to persist the updated ChannelManager for an inbound
14748		// open_channel message - pre-funded channels are never written so there should be no
14749		// change to the contents.
14750		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
14751			let res = self.internal_open_channel(&counterparty_node_id, OpenChannelMessageRef::V2(msg));
14752			let persist = match &res {
14753				Err(e) if e.closes_channel() => {
14754					debug_assert!(false, "We shouldn't close a new channel");
14755					NotifyOption::DoPersist
14756				},
14757				_ => NotifyOption::SkipPersistHandleEvents,
14758			};
14759			let _ = handle_error!(self, res, counterparty_node_id);
14760			persist
14761		});
14762	}
14763
14764	fn handle_accept_channel(&self, counterparty_node_id: PublicKey, msg: &msgs::AcceptChannel) {
14765		// Note that we never need to persist the updated ChannelManager for an inbound
14766		// accept_channel message - pre-funded channels are never written so there should be no
14767		// change to the contents.
14768		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
14769			let res = self.internal_accept_channel(&counterparty_node_id, msg);
14770			let _ = handle_error!(self, res, counterparty_node_id);
14771			NotifyOption::SkipPersistHandleEvents
14772		});
14773	}
14774
14775	fn handle_accept_channel_v2(
14776		&self, counterparty_node_id: PublicKey, msg: &msgs::AcceptChannelV2,
14777	) {
14778		let err = Err(MsgHandleErrInternal::send_err_msg_no_close(
14779			"Dual-funded channels not supported".to_owned(),
14780			msg.common_fields.temporary_channel_id.clone(),
14781		));
14782		let _: Result<(), _> = handle_error!(self, err, counterparty_node_id);
14783	}
14784
14785	fn handle_funding_created(&self, counterparty_node_id: PublicKey, msg: &msgs::FundingCreated) {
14786		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
14787		let res = self.internal_funding_created(&counterparty_node_id, msg);
14788		let _ = handle_error!(self, res, counterparty_node_id);
14789	}
14790
14791	fn handle_funding_signed(&self, counterparty_node_id: PublicKey, msg: &msgs::FundingSigned) {
14792		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
14793		let res = self.internal_funding_signed(&counterparty_node_id, msg);
14794		let _ = handle_error!(self, res, counterparty_node_id);
14795	}
14796
14797	fn handle_peer_storage(&self, counterparty_node_id: PublicKey, msg: msgs::PeerStorage) {
14798		let _persistence_guard =
14799			PersistenceNotifierGuard::optionally_notify(self, || NotifyOption::SkipPersistNoEvents);
14800		let res = self.internal_peer_storage(counterparty_node_id, msg);
14801		let _ = handle_error!(self, res, counterparty_node_id);
14802	}
14803
14804	fn handle_peer_storage_retrieval(
14805		&self, counterparty_node_id: PublicKey, msg: msgs::PeerStorageRetrieval,
14806	) {
14807		let _persistence_guard =
14808			PersistenceNotifierGuard::optionally_notify(self, || NotifyOption::SkipPersistNoEvents);
14809		let res = self.internal_peer_storage_retrieval(counterparty_node_id, msg);
14810		let _ = handle_error!(self, res, counterparty_node_id);
14811	}
14812
14813	fn handle_channel_ready(&self, counterparty_node_id: PublicKey, msg: &msgs::ChannelReady) {
14814		// Note that we never need to persist the updated ChannelManager for an inbound
14815		// channel_ready message - while the channel's state will change, any channel_ready message
14816		// will ultimately be re-sent on startup and the `ChannelMonitor` won't be updated so we
14817		// will not force-close the channel on startup.
14818		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
14819			let res = self.internal_channel_ready(&counterparty_node_id, msg);
14820			let persist = match &res {
14821				Err(e) if e.closes_channel() => NotifyOption::DoPersist,
14822				_ => NotifyOption::SkipPersistHandleEvents,
14823			};
14824			let _ = handle_error!(self, res, counterparty_node_id);
14825			persist
14826		});
14827	}
14828
14829	fn handle_stfu(&self, counterparty_node_id: PublicKey, msg: &msgs::Stfu) {
14830		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
14831			let res = self.internal_stfu(&counterparty_node_id, msg);
14832			let persist = match &res {
14833				Err(e) if e.closes_channel() => NotifyOption::DoPersist,
14834				Err(_) => NotifyOption::SkipPersistHandleEvents,
14835				Ok(responded) => {
14836					if *responded {
14837						NotifyOption::SkipPersistHandleEvents
14838					} else {
14839						NotifyOption::SkipPersistNoEvents
14840					}
14841				},
14842			};
14843			let _ = handle_error!(self, res, counterparty_node_id);
14844			persist
14845		});
14846	}
14847
14848	fn handle_splice_init(&self, counterparty_node_id: PublicKey, msg: &msgs::SpliceInit) {
14849		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
14850			let res = self.internal_splice_init(&counterparty_node_id, msg);
14851			let persist = match &res {
14852				Err(e) if e.closes_channel() => NotifyOption::DoPersist,
14853				Err(_) => NotifyOption::SkipPersistHandleEvents,
14854				Ok(()) => NotifyOption::SkipPersistHandleEvents,
14855			};
14856			let _ = handle_error!(self, res, counterparty_node_id);
14857			persist
14858		});
14859	}
14860
14861	fn handle_splice_ack(&self, counterparty_node_id: PublicKey, msg: &msgs::SpliceAck) {
14862		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
14863			let res = self.internal_splice_ack(&counterparty_node_id, msg);
14864			let persist = match &res {
14865				Err(e) if e.closes_channel() => NotifyOption::DoPersist,
14866				Err(_) => NotifyOption::SkipPersistHandleEvents,
14867				Ok(()) => NotifyOption::SkipPersistHandleEvents,
14868			};
14869			let _ = handle_error!(self, res, counterparty_node_id);
14870			persist
14871		});
14872	}
14873
14874	#[rustfmt::skip]
14875	fn handle_splice_locked(&self, counterparty_node_id: PublicKey, msg: &msgs::SpliceLocked) {
14876		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
14877			let res = self.internal_splice_locked(&counterparty_node_id, msg);
14878			let persist = match &res {
14879				Err(e) if e.closes_channel() => NotifyOption::DoPersist,
14880				Err(_) => NotifyOption::SkipPersistHandleEvents,
14881				Ok(()) => NotifyOption::DoPersist,
14882			};
14883			let _ = handle_error!(self, res, counterparty_node_id);
14884			persist
14885		});
14886	}
14887
14888	fn handle_shutdown(&self, counterparty_node_id: PublicKey, msg: &msgs::Shutdown) {
14889		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
14890		let res = self.internal_shutdown(&counterparty_node_id, msg);
14891		let _ = handle_error!(self, res, counterparty_node_id);
14892	}
14893
14894	fn handle_closing_signed(&self, counterparty_node_id: PublicKey, msg: &msgs::ClosingSigned) {
14895		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
14896		let res = self.internal_closing_signed(&counterparty_node_id, msg);
14897		let _ = handle_error!(self, res, counterparty_node_id);
14898	}
14899
14900	#[cfg(simple_close)]
14901	fn handle_closing_complete(&self, counterparty_node_id: PublicKey, msg: msgs::ClosingComplete) {
14902		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
14903		let res = self.internal_closing_complete(counterparty_node_id, msg);
14904		let _ = handle_error!(self, res, counterparty_node_id);
14905	}
14906
14907	#[cfg(simple_close)]
14908	fn handle_closing_sig(&self, counterparty_node_id: PublicKey, msg: msgs::ClosingSig) {
14909		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
14910		let res = self.internal_closing_sig(counterparty_node_id, msg);
14911		let _ = handle_error!(self, res, counterparty_node_id);
14912	}
14913
14914	fn handle_update_add_htlc(&self, counterparty_node_id: PublicKey, msg: &msgs::UpdateAddHTLC) {
14915		// Note that we never need to persist the updated ChannelManager for an inbound
14916		// update_add_htlc message - the message itself doesn't change our channel state only the
14917		// `commitment_signed` message afterwards will.
14918		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
14919			let res = self.internal_update_add_htlc(&counterparty_node_id, msg);
14920			let persist = match &res {
14921				Err(e) if e.closes_channel() => NotifyOption::DoPersist,
14922				Err(_) => NotifyOption::SkipPersistHandleEvents,
14923				Ok(()) => NotifyOption::SkipPersistNoEvents,
14924			};
14925			let _ = handle_error!(self, res, counterparty_node_id);
14926			persist
14927		});
14928	}
14929
14930	fn handle_update_fulfill_htlc(
14931		&self, counterparty_node_id: PublicKey, msg: msgs::UpdateFulfillHTLC,
14932	) {
14933		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
14934		let res = self.internal_update_fulfill_htlc(&counterparty_node_id, msg);
14935		let _ = handle_error!(self, res, counterparty_node_id);
14936	}
14937
14938	fn handle_update_fail_htlc(&self, counterparty_node_id: PublicKey, msg: &msgs::UpdateFailHTLC) {
14939		// Note that we never need to persist the updated ChannelManager for an inbound
14940		// update_fail_htlc message - the message itself doesn't change our channel state only the
14941		// `commitment_signed` message afterwards will.
14942		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
14943			let res = self.internal_update_fail_htlc(&counterparty_node_id, msg);
14944			let persist = match &res {
14945				Err(e) if e.closes_channel() => NotifyOption::DoPersist,
14946				Err(_) => NotifyOption::SkipPersistHandleEvents,
14947				Ok(()) => NotifyOption::SkipPersistNoEvents,
14948			};
14949			let _ = handle_error!(self, res, counterparty_node_id);
14950			persist
14951		});
14952	}
14953
14954	fn handle_update_fail_malformed_htlc(
14955		&self, counterparty_node_id: PublicKey, msg: &msgs::UpdateFailMalformedHTLC,
14956	) {
14957		// Note that we never need to persist the updated ChannelManager for an inbound
14958		// update_fail_malformed_htlc message - the message itself doesn't change our channel state
14959		// only the `commitment_signed` message afterwards will.
14960		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
14961			let res = self.internal_update_fail_malformed_htlc(&counterparty_node_id, msg);
14962			let persist = match &res {
14963				Err(e) if e.closes_channel() => NotifyOption::DoPersist,
14964				Err(_) => NotifyOption::SkipPersistHandleEvents,
14965				Ok(()) => NotifyOption::SkipPersistNoEvents,
14966			};
14967			let _ = handle_error!(self, res, counterparty_node_id);
14968			persist
14969		});
14970	}
14971
14972	fn handle_commitment_signed(
14973		&self, counterparty_node_id: PublicKey, msg: &msgs::CommitmentSigned,
14974	) {
14975		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
14976		let res = self.internal_commitment_signed(&counterparty_node_id, msg);
14977		let _ = handle_error!(self, res, counterparty_node_id);
14978	}
14979
14980	fn handle_commitment_signed_batch(
14981		&self, counterparty_node_id: PublicKey, channel_id: ChannelId,
14982		batch: Vec<msgs::CommitmentSigned>,
14983	) {
14984		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
14985		let res = self.internal_commitment_signed_batch(&counterparty_node_id, channel_id, batch);
14986		let _ = handle_error!(self, res, counterparty_node_id);
14987	}
14988
14989	fn handle_revoke_and_ack(&self, counterparty_node_id: PublicKey, msg: &msgs::RevokeAndACK) {
14990		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
14991		let res = self.internal_revoke_and_ack(&counterparty_node_id, msg);
14992		let _ = handle_error!(self, res, counterparty_node_id);
14993	}
14994
14995	fn handle_update_fee(&self, counterparty_node_id: PublicKey, msg: &msgs::UpdateFee) {
14996		// Note that we never need to persist the updated ChannelManager for an inbound
14997		// update_fee message - the message itself doesn't change our channel state only the
14998		// `commitment_signed` message afterwards will.
14999		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
15000			let res = self.internal_update_fee(&counterparty_node_id, msg);
15001			let persist = match &res {
15002				Err(e) if e.closes_channel() => NotifyOption::DoPersist,
15003				Err(_) => NotifyOption::SkipPersistHandleEvents,
15004				Ok(()) => NotifyOption::SkipPersistNoEvents,
15005			};
15006			let _ = handle_error!(self, res, counterparty_node_id);
15007			persist
15008		});
15009	}
15010
15011	fn handle_announcement_signatures(
15012		&self, counterparty_node_id: PublicKey, msg: &msgs::AnnouncementSignatures,
15013	) {
15014		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
15015		let res = self.internal_announcement_signatures(&counterparty_node_id, msg);
15016		let _ = handle_error!(self, res, counterparty_node_id);
15017	}
15018
15019	fn handle_channel_update(&self, counterparty_node_id: PublicKey, msg: &msgs::ChannelUpdate) {
15020		PersistenceNotifierGuard::optionally_notify(self, || {
15021			let res = self.internal_channel_update(&counterparty_node_id, msg);
15022			if let Ok(persist) = handle_error!(self, res, counterparty_node_id) {
15023				persist
15024			} else {
15025				NotifyOption::DoPersist
15026			}
15027		});
15028	}
15029
15030	fn handle_channel_reestablish(
15031		&self, counterparty_node_id: PublicKey, msg: &msgs::ChannelReestablish,
15032	) {
15033		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
15034		let res = self.internal_channel_reestablish(&counterparty_node_id, msg);
15035		let _ = handle_error!(self, res, counterparty_node_id);
15036	}
15037
15038	#[rustfmt::skip]
15039	fn handle_error(&self, counterparty_node_id: PublicKey, msg: &msgs::ErrorMessage) {
15040		match &msg.data as &str {
15041			"cannot co-op close channel w/ active htlcs"|
15042			"link failed to shutdown" =>
15043			{
15044				// LND hasn't properly handled shutdown messages ever, and force-closes any time we
15045				// send one while HTLCs are still present. The issue is tracked at
15046				// https://github.com/lightningnetwork/lnd/issues/6039 and has had multiple patches
15047				// to fix it but none so far have managed to land upstream. The issue appears to be
15048				// very low priority for the LND team despite being marked "P1".
15049				// We're not going to bother handling this in a sensible way, instead simply
15050				// repeating the Shutdown message on repeat until morale improves.
15051				if !msg.channel_id.is_zero() {
15052					PersistenceNotifierGuard::optionally_notify(
15053						self,
15054						|| -> NotifyOption {
15055							let per_peer_state = self.per_peer_state.read().unwrap();
15056							let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
15057							if peer_state_mutex_opt.is_none() { return NotifyOption::SkipPersistNoEvents; }
15058							let mut peer_state = peer_state_mutex_opt.unwrap().lock().unwrap();
15059							if let Some(chan) = peer_state.channel_by_id
15060								.get(&msg.channel_id)
15061								.and_then(Channel::as_funded)
15062							{
15063								if let Some(msg) = chan.get_outbound_shutdown() {
15064									peer_state.pending_msg_events.push(MessageSendEvent::SendShutdown {
15065										node_id: counterparty_node_id,
15066										msg,
15067									});
15068								}
15069								peer_state.pending_msg_events.push(MessageSendEvent::HandleError {
15070									node_id: counterparty_node_id,
15071									action: msgs::ErrorAction::SendWarningMessage {
15072										msg: msgs::WarningMessage {
15073											channel_id: msg.channel_id,
15074											data: "You appear to be exhibiting LND bug 6039, we'll keep sending you shutdown messages until you handle them correctly".to_owned()
15075										},
15076										log_level: Level::Trace,
15077									}
15078								});
15079								// This can happen in a fairly tight loop, so we absolutely cannot trigger
15080								// a `ChannelManager` write here.
15081								return NotifyOption::SkipPersistHandleEvents;
15082							}
15083							NotifyOption::SkipPersistNoEvents
15084						}
15085					);
15086				}
15087				return;
15088			}
15089			_ => {}
15090		}
15091
15092		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
15093
15094		let peer_msg = UntrustedString(msg.data.clone());
15095		let reason = ClosureReason::CounterpartyForceClosed { peer_msg };
15096
15097		if msg.channel_id.is_zero() {
15098			let channel_ids: Vec<ChannelId> = {
15099				let per_peer_state = self.per_peer_state.read().unwrap();
15100				let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
15101				if peer_state_mutex_opt.is_none() { return; }
15102				let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
15103				let peer_state = &mut *peer_state_lock;
15104				// Note that we don't bother generating any events for pre-accept channels -
15105				// they're not considered "channels" yet from the PoV of our events interface.
15106				peer_state.inbound_channel_request_by_id.clear();
15107				peer_state.channel_by_id.keys().cloned().collect()
15108			};
15109			for channel_id in channel_ids {
15110				// Untrusted messages from peer, we throw away the error if id points to a non-existent channel
15111				let _ = self.force_close_channel_with_peer(&channel_id, &counterparty_node_id, reason.clone());
15112			}
15113		} else {
15114			{
15115				// First check if we can advance the channel type and try again.
15116				let per_peer_state = self.per_peer_state.read().unwrap();
15117				let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
15118				if peer_state_mutex_opt.is_none() { return; }
15119				let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
15120				let peer_state = &mut *peer_state_lock;
15121				match peer_state.channel_by_id.get_mut(&msg.channel_id) {
15122					Some(chan) => match chan.maybe_handle_error_without_close(
15123						self.chain_hash, &self.fee_estimator, &self.logger,
15124						&self.config.read().unwrap(), &peer_state.latest_features,
15125					) {
15126						Ok(Some(OpenChannelMessage::V1(msg))) => {
15127							peer_state.pending_msg_events.push(MessageSendEvent::SendOpenChannel {
15128								node_id: counterparty_node_id,
15129								msg,
15130							});
15131							return;
15132						},
15133						Ok(Some(OpenChannelMessage::V2(msg))) => {
15134							peer_state.pending_msg_events.push(MessageSendEvent::SendOpenChannelV2 {
15135								node_id: counterparty_node_id,
15136								msg,
15137							});
15138							return;
15139						},
15140						Ok(None) | Err(()) => {},
15141					},
15142					None => {},
15143				}
15144			}
15145
15146			// Untrusted messages from peer, we throw away the error if id points to a non-existent channel
15147			let _ = self.force_close_channel_with_peer(&msg.channel_id, &counterparty_node_id, reason);
15148		}
15149	}
15150
15151	fn get_chain_hashes(&self) -> Option<Vec<ChainHash>> {
15152		Some(vec![self.chain_hash])
15153	}
15154
15155	fn handle_tx_add_input(&self, counterparty_node_id: PublicKey, msg: &msgs::TxAddInput) {
15156		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
15157			let res = self.internal_tx_add_input(counterparty_node_id, msg);
15158			let persist = match &res {
15159				Err(_) => NotifyOption::DoPersist,
15160				Ok(persist) => *persist,
15161			};
15162			let _ = handle_error!(self, res, counterparty_node_id);
15163			persist
15164		});
15165	}
15166
15167	fn handle_tx_add_output(&self, counterparty_node_id: PublicKey, msg: &msgs::TxAddOutput) {
15168		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
15169			let res = self.internal_tx_add_output(counterparty_node_id, msg);
15170			let persist = match &res {
15171				Err(_) => NotifyOption::DoPersist,
15172				Ok(persist) => *persist,
15173			};
15174			let _ = handle_error!(self, res, counterparty_node_id);
15175			persist
15176		});
15177	}
15178
15179	fn handle_tx_remove_input(&self, counterparty_node_id: PublicKey, msg: &msgs::TxRemoveInput) {
15180		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
15181			let res = self.internal_tx_remove_input(counterparty_node_id, msg);
15182			let persist = match &res {
15183				Err(_) => NotifyOption::DoPersist,
15184				Ok(persist) => *persist,
15185			};
15186			let _ = handle_error!(self, res, counterparty_node_id);
15187			persist
15188		});
15189	}
15190
15191	fn handle_tx_remove_output(&self, counterparty_node_id: PublicKey, msg: &msgs::TxRemoveOutput) {
15192		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
15193			let res = self.internal_tx_remove_output(counterparty_node_id, msg);
15194			let persist = match &res {
15195				Err(_) => NotifyOption::DoPersist,
15196				Ok(persist) => *persist,
15197			};
15198			let _ = handle_error!(self, res, counterparty_node_id);
15199			persist
15200		});
15201	}
15202
15203	fn handle_tx_complete(&self, counterparty_node_id: PublicKey, msg: &msgs::TxComplete) {
15204		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
15205			let res = self.internal_tx_complete(counterparty_node_id, msg);
15206			let persist = match &res {
15207				Err(_) => NotifyOption::DoPersist,
15208				Ok(persist) => *persist,
15209			};
15210			let _ = handle_error!(self, res, counterparty_node_id);
15211			persist
15212		});
15213	}
15214
15215	fn handle_tx_signatures(&self, counterparty_node_id: PublicKey, msg: &msgs::TxSignatures) {
15216		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
15217		let res = self.internal_tx_signatures(&counterparty_node_id, msg);
15218		let _ = handle_error!(self, res, counterparty_node_id);
15219	}
15220
15221	fn handle_tx_init_rbf(&self, counterparty_node_id: PublicKey, msg: &msgs::TxInitRbf) {
15222		let err = Err(MsgHandleErrInternal::send_err_msg_no_close(
15223			"Dual-funded channels not supported".to_owned(),
15224			msg.channel_id.clone(),
15225		));
15226		let _: Result<(), _> = handle_error!(self, err, counterparty_node_id);
15227	}
15228
15229	fn handle_tx_ack_rbf(&self, counterparty_node_id: PublicKey, msg: &msgs::TxAckRbf) {
15230		let err = Err(MsgHandleErrInternal::send_err_msg_no_close(
15231			"Dual-funded channels not supported".to_owned(),
15232			msg.channel_id.clone(),
15233		));
15234		let _: Result<(), _> = handle_error!(self, err, counterparty_node_id);
15235	}
15236
15237	fn handle_tx_abort(&self, counterparty_node_id: PublicKey, msg: &msgs::TxAbort) {
15238		// Note that we never need to persist the updated ChannelManager for an inbound
15239		// tx_abort message - interactive transaction construction does not need to
15240		// be persisted before any signatures are exchanged.
15241		let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
15242			let res = self.internal_tx_abort(&counterparty_node_id, msg);
15243			let persist = match &res {
15244				Err(e) if e.closes_channel() => NotifyOption::DoPersist,
15245				Err(_) => NotifyOption::SkipPersistHandleEvents,
15246				Ok(persist) => *persist,
15247			};
15248			let _ = handle_error!(self, res, counterparty_node_id);
15249			persist
15250		});
15251	}
15252
15253	fn message_received(&self) {
15254		for (payment_id, retryable_invoice_request) in
15255			self.pending_outbound_payments.release_invoice_requests_awaiting_invoice()
15256		{
15257			let RetryableInvoiceRequest { invoice_request, nonce, .. } = retryable_invoice_request;
15258
15259			let peers = self.get_peers_for_blinded_path();
15260			let enqueue_invreq_res =
15261				self.flow.enqueue_invoice_request(invoice_request, payment_id, nonce, peers);
15262			if enqueue_invreq_res.is_err() {
15263				log_warn!(
15264					self.logger,
15265					"Retry failed for invoice request with payment_id {}",
15266					payment_id
15267				);
15268			}
15269		}
15270	}
15271}
15272
15273impl<
15274		M: Deref,
15275		T: Deref,
15276		ES: Deref,
15277		NS: Deref,
15278		SP: Deref,
15279		F: Deref,
15280		R: Deref,
15281		MR: Deref,
15282		L: Deref,
15283	> OffersMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
15284where
15285	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
15286	T::Target: BroadcasterInterface,
15287	ES::Target: EntropySource,
15288	NS::Target: NodeSigner,
15289	SP::Target: SignerProvider,
15290	F::Target: FeeEstimator,
15291	R::Target: Router,
15292	MR::Target: MessageRouter,
15293	L::Target: Logger,
15294{
15295	#[rustfmt::skip]
15296	fn handle_message(
15297		&self, message: OffersMessage, context: Option<OffersContext>, responder: Option<Responder>,
15298	) -> Option<(OffersMessage, ResponseInstruction)> {
15299		macro_rules! handle_pay_invoice_res {
15300			($res: expr, $invoice: expr, $logger: expr) => {{
15301				let error = match $res {
15302					Err(Bolt12PaymentError::UnknownRequiredFeatures) => {
15303						log_trace!(
15304							$logger, "Invoice requires unknown features: {:?}",
15305							$invoice.invoice_features()
15306						);
15307						InvoiceError::from(Bolt12SemanticError::UnknownRequiredFeatures)
15308					},
15309					Err(Bolt12PaymentError::SendingFailed(e)) => {
15310						log_trace!($logger, "Failed paying invoice: {:?}", e);
15311						InvoiceError::from_string(format!("{:?}", e))
15312					},
15313					Err(Bolt12PaymentError::BlindedPathCreationFailed) => {
15314						let err_msg = "Failed to create a blinded path back to ourselves";
15315						log_trace!($logger, "{}", err_msg);
15316						InvoiceError::from_string(err_msg.to_string())
15317					},
15318					Err(Bolt12PaymentError::UnexpectedInvoice)
15319						| Err(Bolt12PaymentError::DuplicateInvoice)
15320						| Ok(()) => return None,
15321				};
15322
15323				match responder {
15324					Some(responder) => return Some((OffersMessage::InvoiceError(error), responder.respond())),
15325					None => {
15326						log_trace!($logger, "No reply path to send error: {:?}", error);
15327						return None
15328					},
15329				}
15330			}}
15331		}
15332
15333		match message {
15334			OffersMessage::InvoiceRequest(invoice_request) => {
15335				let responder = match responder {
15336					Some(responder) => responder,
15337					None => return None,
15338				};
15339
15340				let invoice_request = match self.flow.verify_invoice_request(invoice_request, context) {
15341					Ok(InvreqResponseInstructions::SendInvoice(invoice_request)) => invoice_request,
15342					Ok(InvreqResponseInstructions::SendStaticInvoice { recipient_id, invoice_slot, invoice_request }) => {
15343						self.pending_events.lock().unwrap().push_back((Event::StaticInvoiceRequested {
15344							recipient_id, invoice_slot, reply_path: responder, invoice_request,
15345						}, None));
15346
15347						return None
15348					},
15349					Err(_) => return None,
15350				};
15351
15352				let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats(
15353					&invoice_request.inner
15354				) {
15355					Ok(amount_msats) => amount_msats,
15356					Err(error) => return Some((OffersMessage::InvoiceError(error.into()), responder.respond())),
15357				};
15358
15359				let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
15360				let (payment_hash, payment_secret) = match self.create_inbound_payment(
15361					Some(amount_msats), relative_expiry, None
15362				) {
15363					Ok((payment_hash, payment_secret)) => (payment_hash, payment_secret),
15364					Err(()) => {
15365						let error = Bolt12SemanticError::InvalidAmount;
15366						return Some((OffersMessage::InvoiceError(error.into()), responder.respond()));
15367					},
15368				};
15369
15370				let entropy = &*self.entropy_source;
15371				let (response, context) = self.flow.create_response_for_invoice_request(
15372					&self.node_signer, &self.router, entropy, invoice_request, amount_msats,
15373					payment_hash, payment_secret, self.list_usable_channels()
15374				);
15375
15376				match context {
15377					Some(context) => Some((response, responder.respond_with_reply_path(context))),
15378					None => Some((response, responder.respond()))
15379				}
15380			},
15381			OffersMessage::Invoice(invoice) => {
15382				let payment_id = match self.flow.verify_bolt12_invoice(&invoice, context.as_ref()) {
15383					Ok(payment_id) => payment_id,
15384					Err(()) => return None,
15385				};
15386
15387				let logger = WithContext::from(
15388					&self.logger, None, None, Some(invoice.payment_hash()),
15389				);
15390
15391				if self.config.read().unwrap().manually_handle_bolt12_invoices {
15392					// Update the corresponding entry in `PendingOutboundPayment` for this invoice.
15393					// This ensures that event generation remains idempotent in case we receive
15394					// the same invoice multiple times.
15395					self.pending_outbound_payments.mark_invoice_received(&invoice, payment_id).ok()?;
15396
15397					let event = Event::InvoiceReceived {
15398						payment_id, invoice, context, responder,
15399					};
15400					self.pending_events.lock().unwrap().push_back((event, None));
15401					return None;
15402				}
15403
15404				let res = self.send_payment_for_verified_bolt12_invoice(&invoice, payment_id);
15405				handle_pay_invoice_res!(res, invoice, logger);
15406			},
15407			OffersMessage::StaticInvoice(invoice) => {
15408				let payment_id = match context {
15409					Some(OffersContext::OutboundPayment { payment_id, .. }) => payment_id,
15410					_ => return None
15411				};
15412				let res = self.initiate_async_payment(&invoice, payment_id);
15413				handle_pay_invoice_res!(res, invoice, self.logger);
15414			},
15415			OffersMessage::InvoiceError(invoice_error) => {
15416				let payment_hash = match context {
15417					Some(OffersContext::InboundPayment { payment_hash }) => Some(payment_hash),
15418					_ => None,
15419				};
15420
15421				let logger = WithContext::from(&self.logger, None, None, payment_hash);
15422				log_trace!(logger, "Received invoice_error: {}", invoice_error);
15423
15424				match context {
15425					Some(OffersContext::OutboundPayment { payment_id, .. }) => {
15426						self.abandon_payment_with_reason(
15427							payment_id, PaymentFailureReason::InvoiceRequestRejected,
15428						);
15429					},
15430					_ => {},
15431				}
15432
15433				None
15434			},
15435		}
15436	}
15437
15438	fn release_pending_messages(&self) -> Vec<(OffersMessage, MessageSendInstructions)> {
15439		self.flow.release_pending_offers_messages()
15440	}
15441}
15442
15443impl<
15444		M: Deref,
15445		T: Deref,
15446		ES: Deref,
15447		NS: Deref,
15448		SP: Deref,
15449		F: Deref,
15450		R: Deref,
15451		MR: Deref,
15452		L: Deref,
15453	> AsyncPaymentsMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
15454where
15455	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
15456	T::Target: BroadcasterInterface,
15457	ES::Target: EntropySource,
15458	NS::Target: NodeSigner,
15459	SP::Target: SignerProvider,
15460	F::Target: FeeEstimator,
15461	R::Target: Router,
15462	MR::Target: MessageRouter,
15463	L::Target: Logger,
15464{
15465	fn handle_offer_paths_request(
15466		&self, message: OfferPathsRequest, context: AsyncPaymentsContext,
15467		responder: Option<Responder>,
15468	) -> Option<(OfferPaths, ResponseInstruction)> {
15469		let peers = self.get_peers_for_blinded_path();
15470		let (message, reply_path_context) =
15471			match self.flow.handle_offer_paths_request(&message, context, peers) {
15472				Some(msg) => msg,
15473				None => return None,
15474			};
15475		responder.map(|resp| (message, resp.respond_with_reply_path(reply_path_context)))
15476	}
15477
15478	fn handle_offer_paths(
15479		&self, message: OfferPaths, context: AsyncPaymentsContext, responder: Option<Responder>,
15480	) -> Option<(ServeStaticInvoice, ResponseInstruction)> {
15481		let responder = match responder {
15482			Some(responder) => responder,
15483			None => return None,
15484		};
15485		let (serve_static_invoice, reply_context) = match self.flow.handle_offer_paths(
15486			message,
15487			context,
15488			responder.clone(),
15489			self.get_peers_for_blinded_path(),
15490			self.list_usable_channels(),
15491			&*self.entropy_source,
15492			&*self.router,
15493		) {
15494			Some((msg, ctx)) => (msg, ctx),
15495			None => return None,
15496		};
15497
15498		// We cached a new pending offer, so persist the cache.
15499		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
15500
15501		let response_instructions = responder.respond_with_reply_path(reply_context);
15502		return Some((serve_static_invoice, response_instructions));
15503	}
15504
15505	fn handle_serve_static_invoice(
15506		&self, message: ServeStaticInvoice, context: AsyncPaymentsContext,
15507		responder: Option<Responder>,
15508	) {
15509		let responder = match responder {
15510			Some(resp) => resp,
15511			None => return,
15512		};
15513
15514		let (recipient_id, invoice_slot) =
15515			match self.flow.verify_serve_static_invoice_message(&message, context) {
15516				Ok((recipient_id, inv_slot)) => (recipient_id, inv_slot),
15517				Err(()) => return,
15518			};
15519
15520		let mut pending_events = self.pending_events.lock().unwrap();
15521		pending_events.push_back((
15522			Event::PersistStaticInvoice {
15523				invoice: message.invoice,
15524				invoice_request_path: message.forward_invoice_request_path,
15525				invoice_slot,
15526				recipient_id,
15527				invoice_persisted_path: responder,
15528			},
15529			None,
15530		));
15531	}
15532
15533	fn handle_static_invoice_persisted(
15534		&self, _message: StaticInvoicePersisted, context: AsyncPaymentsContext,
15535	) {
15536		let should_persist = self.flow.handle_static_invoice_persisted(context);
15537		if should_persist {
15538			let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
15539		}
15540	}
15541
15542	fn handle_held_htlc_available(
15543		&self, _message: HeldHtlcAvailable, context: AsyncPaymentsContext,
15544		responder: Option<Responder>,
15545	) -> Option<(ReleaseHeldHtlc, ResponseInstruction)> {
15546		self.flow.verify_inbound_async_payment_context(context).ok()?;
15547		return responder.map(|responder| (ReleaseHeldHtlc {}, responder.respond()));
15548	}
15549
15550	fn handle_release_held_htlc(&self, _message: ReleaseHeldHtlc, context: AsyncPaymentsContext) {
15551		match context {
15552			AsyncPaymentsContext::OutboundPayment { payment_id } => {
15553				if let Err(e) = self.send_payment_for_static_invoice(payment_id) {
15554					log_trace!(
15555						self.logger,
15556						"Failed to release held HTLC with payment id {}: {:?}",
15557						payment_id,
15558						e
15559					);
15560				}
15561			},
15562			AsyncPaymentsContext::ReleaseHeldHtlc {
15563				intercept_id,
15564				prev_outbound_scid_alias,
15565				htlc_id,
15566			} => {
15567				// It's possible the release_held_htlc message raced ahead of us transitioning the pending
15568				// update_add to `Self::pending_intercept_htlcs`. If that's the case, update the pending
15569				// update_add to indicate that the HTLC should be released immediately.
15570				//
15571				// Check for the HTLC here before checking `pending_intercept_htlcs` to avoid a different
15572				// race where the HTLC gets transitioned to `pending_intercept_htlcs` after we drop that
15573				// map's lock but before acquiring the `decode_update_add_htlcs` lock.
15574				let mut decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
15575				if let Some(htlcs) = decode_update_add_htlcs.get_mut(&prev_outbound_scid_alias) {
15576					for update_add in htlcs.iter_mut() {
15577						if update_add.htlc_id == htlc_id {
15578							log_trace!(
15579								self.logger,
15580								"Marking held htlc with intercept_id {} as ready to release",
15581								intercept_id
15582							);
15583							update_add.hold_htlc.take();
15584							return;
15585						}
15586					}
15587				}
15588				core::mem::drop(decode_update_add_htlcs);
15589
15590				let mut htlc = {
15591					let mut pending_intercept_htlcs =
15592						self.pending_intercepted_htlcs.lock().unwrap();
15593					match pending_intercept_htlcs.remove(&intercept_id) {
15594						Some(htlc) => htlc,
15595						None => {
15596							log_trace!(
15597								self.logger,
15598								"Failed to release HTLC with intercept_id {}: HTLC not found",
15599								intercept_id
15600							);
15601							return;
15602						},
15603					}
15604				};
15605				match htlc.forward_info.routing {
15606					PendingHTLCRouting::Forward { ref mut hold_htlc, .. } => {
15607						debug_assert!(hold_htlc.is_some());
15608						*hold_htlc = None;
15609					},
15610					_ => {
15611						debug_assert!(false, "HTLC intercepts can only be forwards");
15612						return;
15613					},
15614				}
15615
15616				let logger = WithContext::from(
15617					&self.logger,
15618					Some(htlc.prev_counterparty_node_id),
15619					Some(htlc.prev_channel_id),
15620					Some(htlc.forward_info.payment_hash),
15621				);
15622				log_trace!(logger, "Releasing held htlc with intercept_id {}", intercept_id);
15623
15624				let mut per_source_pending_forward = [(
15625					htlc.prev_outbound_scid_alias,
15626					htlc.prev_counterparty_node_id,
15627					htlc.prev_funding_outpoint,
15628					htlc.prev_channel_id,
15629					htlc.prev_user_channel_id,
15630					vec![(htlc.forward_info, htlc.prev_htlc_id)],
15631				)];
15632				self.forward_htlcs(&mut per_source_pending_forward);
15633				PersistenceNotifierGuard::notify_on_drop(self);
15634			},
15635			_ => return,
15636		}
15637	}
15638
15639	fn release_pending_messages(&self) -> Vec<(AsyncPaymentsMessage, MessageSendInstructions)> {
15640		self.flow.release_pending_async_messages()
15641	}
15642}
15643
15644#[cfg(feature = "dnssec")]
15645impl<
15646		M: Deref,
15647		T: Deref,
15648		ES: Deref,
15649		NS: Deref,
15650		SP: Deref,
15651		F: Deref,
15652		R: Deref,
15653		MR: Deref,
15654		L: Deref,
15655	> DNSResolverMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
15656where
15657	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
15658	T::Target: BroadcasterInterface,
15659	ES::Target: EntropySource,
15660	NS::Target: NodeSigner,
15661	SP::Target: SignerProvider,
15662	F::Target: FeeEstimator,
15663	R::Target: Router,
15664	MR::Target: MessageRouter,
15665	L::Target: Logger,
15666{
15667	fn handle_dnssec_query(
15668		&self, _message: DNSSECQuery, _responder: Option<Responder>,
15669	) -> Option<(DNSResolverMessage, ResponseInstruction)> {
15670		None
15671	}
15672
15673	#[rustfmt::skip]
15674	fn handle_dnssec_proof(&self, message: DNSSECProof, context: DNSResolverContext) {
15675		let offer_opt = self.flow.hrn_resolver.handle_dnssec_proof_for_offer(message, context);
15676		#[cfg_attr(not(feature = "_test_utils"), allow(unused_mut))]
15677		if let Some((completed_requests, mut offer)) = offer_opt {
15678			for (name, payment_id) in completed_requests {
15679				#[cfg(feature = "_test_utils")]
15680				if let Some(replacement_offer) = self.testing_dnssec_proof_offer_resolution_override.lock().unwrap().remove(&name) {
15681					// If we have multiple pending requests we may end up over-using the override
15682					// offer, but tests can deal with that.
15683					offer = replacement_offer;
15684				}
15685				if let Ok((amt_msats, payer_note)) = self.pending_outbound_payments.params_for_payment_awaiting_offer(payment_id) {
15686					let offer_pay_res =
15687						self.pay_for_offer_intern(&offer, None, Some(amt_msats), payer_note, payment_id, Some(name),
15688							|retryable_invoice_request| {
15689								self.pending_outbound_payments
15690									.received_offer(payment_id, Some(retryable_invoice_request))
15691									.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)
15692						});
15693					if offer_pay_res.is_err() {
15694						// The offer we tried to pay is the canonical current offer for the name we
15695						// wanted to pay. If we can't pay it, there's no way to recover so fail the
15696						// payment.
15697						// Note that the PaymentFailureReason should be ignored for an
15698						// AwaitingInvoice payment.
15699						self.pending_outbound_payments.abandon_payment(
15700							payment_id, PaymentFailureReason::RouteNotFound, &self.pending_events,
15701						);
15702					}
15703				}
15704			}
15705		}
15706	}
15707
15708	fn release_pending_messages(&self) -> Vec<(DNSResolverMessage, MessageSendInstructions)> {
15709		self.flow.release_pending_dns_messages()
15710	}
15711}
15712
15713impl<
15714		M: Deref,
15715		T: Deref,
15716		ES: Deref,
15717		NS: Deref,
15718		SP: Deref,
15719		F: Deref,
15720		R: Deref,
15721		MR: Deref,
15722		L: Deref,
15723	> NodeIdLookUp for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
15724where
15725	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
15726	T::Target: BroadcasterInterface,
15727	ES::Target: EntropySource,
15728	NS::Target: NodeSigner,
15729	SP::Target: SignerProvider,
15730	F::Target: FeeEstimator,
15731	R::Target: Router,
15732	MR::Target: MessageRouter,
15733	L::Target: Logger,
15734{
15735	fn next_node_id(&self, short_channel_id: u64) -> Option<PublicKey> {
15736		self.short_to_chan_info.read().unwrap().get(&short_channel_id).map(|(pubkey, _)| *pubkey)
15737	}
15738}
15739
15740/// Fetches the set of [`NodeFeatures`] flags that are provided by or required by
15741/// [`ChannelManager`].
15742pub(crate) fn provided_node_features(config: &UserConfig) -> NodeFeatures {
15743	let mut node_features = provided_init_features(config).to_context();
15744	node_features.set_keysend_optional();
15745	node_features
15746}
15747
15748/// Fetches the set of [`Bolt11InvoiceFeatures`] flags that are provided by or required by
15749/// [`ChannelManager`].
15750///
15751/// Note that the invoice feature flags can vary depending on if the invoice is a "phantom invoice"
15752/// or not. Thus, this method is not public.
15753#[cfg(any(feature = "_test_utils", test))]
15754pub(crate) fn provided_bolt11_invoice_features(config: &UserConfig) -> Bolt11InvoiceFeatures {
15755	provided_init_features(config).to_context()
15756}
15757
15758/// Fetches the set of [`Bolt12InvoiceFeatures`] flags that are provided by or required by
15759/// [`ChannelManager`].
15760pub(crate) fn provided_bolt12_invoice_features(config: &UserConfig) -> Bolt12InvoiceFeatures {
15761	provided_init_features(config).to_context()
15762}
15763
15764/// Fetches the set of [`ChannelFeatures`] flags that are provided by or required by
15765/// [`ChannelManager`].
15766pub(crate) fn provided_channel_features(config: &UserConfig) -> ChannelFeatures {
15767	provided_init_features(config).to_context()
15768}
15769
15770/// Fetches the set of [`ChannelTypeFeatures`] flags that are provided by or required by
15771/// [`ChannelManager`].
15772pub(crate) fn provided_channel_type_features(config: &UserConfig) -> ChannelTypeFeatures {
15773	ChannelTypeFeatures::from_init(&provided_init_features(config))
15774}
15775
15776/// Fetches the set of [`InitFeatures`] flags that are provided by or required by
15777/// [`ChannelManager`].
15778pub fn provided_init_features(config: &UserConfig) -> InitFeatures {
15779	// Note that if new features are added here which other peers may (eventually) require, we
15780	// should also add the corresponding (optional) bit to the [`BaseMessageHandler`] impl for
15781	// [`ErroringMessageHandler`].
15782	let mut features = InitFeatures::empty();
15783	features.set_data_loss_protect_required();
15784	features.set_upfront_shutdown_script_optional();
15785	features.set_variable_length_onion_required();
15786	features.set_static_remote_key_required();
15787	features.set_payment_secret_required();
15788	features.set_basic_mpp_optional();
15789	features.set_wumbo_optional();
15790	features.set_shutdown_any_segwit_optional();
15791	features.set_channel_type_required();
15792	features.set_scid_privacy_optional();
15793	features.set_zero_conf_optional();
15794	features.set_route_blinding_optional();
15795	features.set_provide_storage_optional();
15796	#[cfg(simple_close)]
15797	features.set_simple_close_optional();
15798	features.set_quiescence_optional();
15799	features.set_splicing_optional();
15800
15801	if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
15802		features.set_anchors_zero_fee_htlc_tx_optional();
15803	}
15804	#[cfg(dual_funding)]
15805	if config.enable_dual_funded_channels {
15806		features.set_dual_fund_optional();
15807	}
15808
15809	if config.channel_handshake_config.negotiate_anchor_zero_fee_commitments {
15810		features.set_anchor_zero_fee_commitments_optional();
15811	}
15812
15813	if config.enable_htlc_hold {
15814		features.set_htlc_hold_optional();
15815	}
15816
15817	features
15818}
15819
15820const SERIALIZATION_VERSION: u8 = 1;
15821const MIN_SERIALIZATION_VERSION: u8 = 1;
15822
15823impl_writeable_tlv_based!(PhantomRouteHints, {
15824	(2, channels, required_vec),
15825	(4, phantom_scid, required),
15826	(6, real_node_pubkey, required),
15827});
15828
15829impl_writeable_tlv_based!(BlindedForward, {
15830	(0, inbound_blinding_point, required),
15831	(1, failure, (default_value, BlindedFailure::FromIntroductionNode)),
15832	(3, next_blinding_override, option),
15833});
15834
15835impl_writeable_tlv_based_enum!(PendingHTLCRouting,
15836	(0, Forward) => {
15837		(0, onion_packet, required),
15838		(1, blinded, option),
15839		(2, short_channel_id, required),
15840		(3, incoming_cltv_expiry, option),
15841		(4, hold_htlc, option),
15842	},
15843	(1, Receive) => {
15844		(0, payment_data, required),
15845		(1, phantom_shared_secret, option),
15846		(2, incoming_cltv_expiry, required),
15847		(3, payment_metadata, option),
15848		(5, custom_tlvs, optional_vec),
15849		(7, requires_blinded_error, (default_value, false)),
15850		(9, payment_context, option),
15851	},
15852	(2, ReceiveKeysend) => {
15853		(0, payment_preimage, required),
15854		(1, requires_blinded_error, (default_value, false)),
15855		(2, incoming_cltv_expiry, required),
15856		(3, payment_metadata, option),
15857		(4, payment_data, option), // Added in 0.0.116
15858		(5, custom_tlvs, optional_vec),
15859		(7, has_recipient_created_payment_secret, (default_value, false)),
15860		(9, payment_context, option),
15861		(11, invoice_request, option),
15862	},
15863	(3, TrampolineForward) => {
15864		(0, incoming_shared_secret, required),
15865		(2, onion_packet, required),
15866		(4, blinded, option),
15867		(6, node_id, required),
15868		(8, incoming_cltv_expiry, required),
15869	}
15870);
15871
15872impl_writeable_tlv_based!(PendingHTLCInfo, {
15873	(0, routing, required),
15874	(2, incoming_shared_secret, required),
15875	(4, payment_hash, required),
15876	(6, outgoing_amt_msat, required),
15877	(8, outgoing_cltv_value, required),
15878	(9, incoming_amt_msat, option),
15879	(10, skimmed_fee_msat, option),
15880});
15881
15882impl Writeable for HTLCFailureMsg {
15883	#[rustfmt::skip]
15884	fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
15885		match self {
15886			HTLCFailureMsg::Relay(msgs::UpdateFailHTLC { channel_id, htlc_id, reason, attribution_data }) => {
15887				0u8.write(writer)?;
15888				channel_id.write(writer)?;
15889				htlc_id.write(writer)?;
15890				reason.write(writer)?;
15891
15892				// This code will only ever be hit for legacy data that is re-serialized. It isn't necessary to try
15893				// writing out attribution data, because it can never be present.
15894				debug_assert!(attribution_data.is_none());
15895			},
15896			HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
15897				channel_id, htlc_id, sha256_of_onion, failure_code
15898			}) => {
15899				1u8.write(writer)?;
15900				channel_id.write(writer)?;
15901				htlc_id.write(writer)?;
15902				sha256_of_onion.write(writer)?;
15903				failure_code.write(writer)?;
15904			},
15905		}
15906		Ok(())
15907	}
15908}
15909
15910impl Readable for HTLCFailureMsg {
15911	#[rustfmt::skip]
15912	fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
15913		let id: u8 = Readable::read(reader)?;
15914		match id {
15915			0 => {
15916				Ok(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
15917					channel_id: Readable::read(reader)?,
15918					htlc_id: Readable::read(reader)?,
15919					reason: Readable::read(reader)?,
15920					attribution_data: None,
15921				}))
15922			},
15923			1 => {
15924				Ok(HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
15925					channel_id: Readable::read(reader)?,
15926					htlc_id: Readable::read(reader)?,
15927					sha256_of_onion: Readable::read(reader)?,
15928					failure_code: Readable::read(reader)?,
15929				}))
15930			},
15931			// In versions prior to 0.0.101, HTLCFailureMsg objects were written with type 0 or 1 but
15932			// weren't length-prefixed and thus didn't support reading the TLV stream suffix of the network
15933			// messages contained in the variants.
15934			// In version 0.0.101, support for reading the variants with these types was added, and
15935			// we should migrate to writing these variants when UpdateFailHTLC or
15936			// UpdateFailMalformedHTLC get TLV fields.
15937			2 => {
15938				let length: BigSize = Readable::read(reader)?;
15939				let mut s = FixedLengthReader::new(reader, length.0);
15940				let res = LengthReadable::read_from_fixed_length_buffer(&mut s)?;
15941				s.eat_remaining()?; // Return ShortRead if there's actually not enough bytes
15942				Ok(HTLCFailureMsg::Relay(res))
15943			},
15944			3 => {
15945				let length: BigSize = Readable::read(reader)?;
15946				let mut s = FixedLengthReader::new(reader, length.0);
15947				let res = LengthReadable::read_from_fixed_length_buffer(&mut s)?;
15948				s.eat_remaining()?; // Return ShortRead if there's actually not enough bytes
15949				Ok(HTLCFailureMsg::Malformed(res))
15950			},
15951			_ => Err(DecodeError::UnknownRequiredFeature),
15952		}
15953	}
15954}
15955
15956impl_writeable_tlv_based_enum_legacy!(PendingHTLCStatus, ;
15957	(0, Forward),
15958	(1, Fail),
15959);
15960
15961impl_writeable_tlv_based_enum!(BlindedFailure,
15962	(0, FromIntroductionNode) => {},
15963	(2, FromBlindedNode) => {},
15964);
15965
15966impl_writeable_tlv_based!(HTLCPreviousHopData, {
15967	(0, prev_outbound_scid_alias, required),
15968	(1, phantom_shared_secret, option),
15969	(2, outpoint, required),
15970	(3, blinded_failure, option),
15971	(4, htlc_id, required),
15972	(5, cltv_expiry, option),
15973	(6, incoming_packet_shared_secret, required),
15974	(7, user_channel_id, option),
15975	// Note that by the time we get past the required read for type 2 above, outpoint will be
15976	// filled in, so we can safely unwrap it here.
15977	(9, channel_id, (default_value, ChannelId::v1_from_funding_outpoint(outpoint.0.unwrap()))),
15978	(11, counterparty_node_id, option),
15979});
15980
15981impl Writeable for ClaimableHTLC {
15982	fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
15983		let (payment_data, keysend_preimage) = match &self.onion_payload {
15984			OnionPayload::Invoice { _legacy_hop_data } => (_legacy_hop_data.as_ref(), None),
15985			OnionPayload::Spontaneous(preimage) => (None, Some(preimage)),
15986		};
15987		write_tlv_fields!(writer, {
15988			(0, self.prev_hop, required),
15989			(1, self.total_msat, required),
15990			(2, self.value, required),
15991			(3, self.sender_intended_value, required),
15992			(4, payment_data, option),
15993			(5, self.total_value_received, option),
15994			(6, self.cltv_expiry, required),
15995			(8, keysend_preimage, option),
15996			(10, self.counterparty_skimmed_fee_msat, option),
15997		});
15998		Ok(())
15999	}
16000}
16001
16002impl Readable for ClaimableHTLC {
16003	#[rustfmt::skip]
16004	fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
16005		_init_and_read_len_prefixed_tlv_fields!(reader, {
16006			(0, prev_hop, required),
16007			(1, total_msat, option),
16008			(2, value_ser, required),
16009			(3, sender_intended_value, option),
16010			(4, payment_data_opt, option),
16011			(5, total_value_received, option),
16012			(6, cltv_expiry, required),
16013			(8, keysend_preimage, option),
16014			(10, counterparty_skimmed_fee_msat, option),
16015		});
16016		let payment_data: Option<msgs::FinalOnionHopData> = payment_data_opt;
16017		let value = value_ser.0.unwrap();
16018		let onion_payload = match keysend_preimage {
16019			Some(p) => {
16020				if payment_data.is_some() {
16021					return Err(DecodeError::InvalidValue)
16022				}
16023				if total_msat.is_none() {
16024					total_msat = Some(value);
16025				}
16026				OnionPayload::Spontaneous(p)
16027			},
16028			None => {
16029				if total_msat.is_none() {
16030					if payment_data.is_none() {
16031						return Err(DecodeError::InvalidValue)
16032					}
16033					total_msat = Some(payment_data.as_ref().unwrap().total_msat);
16034				}
16035				OnionPayload::Invoice { _legacy_hop_data: payment_data }
16036			},
16037		};
16038		Ok(Self {
16039			prev_hop: prev_hop.0.unwrap(),
16040			timer_ticks: 0,
16041			value,
16042			sender_intended_value: sender_intended_value.unwrap_or(value),
16043			total_value_received,
16044			total_msat: total_msat.unwrap(),
16045			onion_payload,
16046			cltv_expiry: cltv_expiry.0.unwrap(),
16047			counterparty_skimmed_fee_msat,
16048		})
16049	}
16050}
16051
16052impl Readable for HTLCSource {
16053	#[rustfmt::skip]
16054	fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
16055		let id: u8 = Readable::read(reader)?;
16056		match id {
16057			0 => {
16058				let mut session_priv: crate::util::ser::RequiredWrapper<SecretKey> = crate::util::ser::RequiredWrapper(None);
16059				let mut first_hop_htlc_msat: u64 = 0;
16060				let mut path_hops = Vec::new();
16061				let mut payment_id = None;
16062				let mut payment_params: Option<PaymentParameters> = None;
16063				let mut blinded_tail: Option<BlindedTail> = None;
16064				let mut bolt12_invoice: Option<PaidBolt12Invoice> = None;
16065				read_tlv_fields!(reader, {
16066					(0, session_priv, required),
16067					(1, payment_id, option),
16068					(2, first_hop_htlc_msat, required),
16069					(4, path_hops, required_vec),
16070					(5, payment_params, (option: ReadableArgs, 0)),
16071					(6, blinded_tail, option),
16072					(7, bolt12_invoice, option),
16073				});
16074				if payment_id.is_none() {
16075					// For backwards compat, if there was no payment_id written, use the session_priv bytes
16076					// instead.
16077					payment_id = Some(PaymentId(*session_priv.0.unwrap().as_ref()));
16078				}
16079				let path = Path { hops: path_hops, blinded_tail };
16080				if path.hops.len() == 0 {
16081					return Err(DecodeError::InvalidValue);
16082				}
16083				if let Some(params) = payment_params.as_mut() {
16084					if let Payee::Clear { ref mut final_cltv_expiry_delta, .. } = params.payee {
16085						if final_cltv_expiry_delta == &0 {
16086							*final_cltv_expiry_delta = path.final_cltv_expiry_delta().ok_or(DecodeError::InvalidValue)?;
16087						}
16088					}
16089				}
16090				Ok(HTLCSource::OutboundRoute {
16091					session_priv: session_priv.0.unwrap(),
16092					first_hop_htlc_msat,
16093					path,
16094					payment_id: payment_id.unwrap(),
16095					bolt12_invoice,
16096				})
16097			}
16098			1 => Ok(HTLCSource::PreviousHopData(Readable::read(reader)?)),
16099			_ => Err(DecodeError::UnknownRequiredFeature),
16100		}
16101	}
16102}
16103
16104impl Writeable for HTLCSource {
16105	fn write<W: Writer>(&self, writer: &mut W) -> Result<(), crate::io::Error> {
16106		match self {
16107			HTLCSource::OutboundRoute {
16108				ref session_priv,
16109				ref first_hop_htlc_msat,
16110				ref path,
16111				payment_id,
16112				bolt12_invoice,
16113			} => {
16114				0u8.write(writer)?;
16115				let payment_id_opt = Some(payment_id);
16116				write_tlv_fields!(writer, {
16117				   (0, session_priv, required),
16118				   (1, payment_id_opt, option),
16119				   (2, first_hop_htlc_msat, required),
16120				   // 3 was previously used to write a PaymentSecret for the payment.
16121				   (4, path.hops, required_vec),
16122				   (5, None::<PaymentParameters>, option), // payment_params in LDK versions prior to 0.0.115
16123				   (6, path.blinded_tail, option),
16124				   (7, bolt12_invoice, option),
16125				});
16126			},
16127			HTLCSource::PreviousHopData(ref field) => {
16128				1u8.write(writer)?;
16129				field.write(writer)?;
16130			},
16131		}
16132		Ok(())
16133	}
16134}
16135
16136impl_writeable_tlv_based!(PendingAddHTLCInfo, {
16137	(0, forward_info, required),
16138	(1, prev_user_channel_id, (default_value, 0)),
16139	(2, prev_outbound_scid_alias, required),
16140	(4, prev_htlc_id, required),
16141	(6, prev_funding_outpoint, required),
16142	// Note that by the time we get past the required read for type 6 above, prev_funding_outpoint will be
16143	// filled in, so we can safely unwrap it here.
16144	(7, prev_channel_id, (default_value, ChannelId::v1_from_funding_outpoint(prev_funding_outpoint.0.unwrap()))),
16145	(9, prev_counterparty_node_id, required),
16146});
16147
16148impl Writeable for HTLCForwardInfo {
16149	fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
16150		const FAIL_HTLC_VARIANT_ID: u8 = 1;
16151		match self {
16152			Self::AddHTLC(info) => {
16153				0u8.write(w)?;
16154				info.write(w)?;
16155			},
16156			Self::FailHTLC { htlc_id, err_packet } => {
16157				FAIL_HTLC_VARIANT_ID.write(w)?;
16158				write_tlv_fields!(w, {
16159					(0, htlc_id, required),
16160					(2, err_packet.data, required),
16161					(5, err_packet.attribution_data, option),
16162				});
16163			},
16164			Self::FailMalformedHTLC { htlc_id, failure_code, sha256_of_onion } => {
16165				// Since this variant was added in 0.0.119, write this as `::FailHTLC` with an empty error
16166				// packet so older versions have something to fail back with, but serialize the real data as
16167				// optional TLVs for the benefit of newer versions.
16168				FAIL_HTLC_VARIANT_ID.write(w)?;
16169				write_tlv_fields!(w, {
16170					(0, htlc_id, required),
16171					(1, failure_code, required),
16172					(2, Vec::<u8>::new(), required),
16173					(3, sha256_of_onion, required),
16174				});
16175			},
16176		}
16177		Ok(())
16178	}
16179}
16180
16181impl Readable for HTLCForwardInfo {
16182	#[rustfmt::skip]
16183	fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
16184		let id: u8 = Readable::read(r)?;
16185		Ok(match id {
16186			0 => Self::AddHTLC(Readable::read(r)?),
16187			1 => {
16188				_init_and_read_len_prefixed_tlv_fields!(r, {
16189					(0, htlc_id, required),
16190					(1, malformed_htlc_failure_code, option),
16191					(2, err_packet, required),
16192					(3, sha256_of_onion, option),
16193					(5, attribution_data, option),
16194				});
16195				if let Some(failure_code) = malformed_htlc_failure_code {
16196					if attribution_data.is_some() {
16197						return Err(DecodeError::InvalidValue);
16198					}
16199					Self::FailMalformedHTLC {
16200						htlc_id: _init_tlv_based_struct_field!(htlc_id, required),
16201						failure_code,
16202						sha256_of_onion: sha256_of_onion.ok_or(DecodeError::InvalidValue)?,
16203					}
16204				} else {
16205					Self::FailHTLC {
16206						htlc_id: _init_tlv_based_struct_field!(htlc_id, required),
16207						err_packet: crate::ln::msgs::OnionErrorPacket {
16208							data: _init_tlv_based_struct_field!(err_packet, required),
16209							attribution_data: _init_tlv_based_struct_field!(attribution_data, option),
16210						},
16211					}
16212				}
16213			},
16214			_ => return Err(DecodeError::InvalidValue),
16215		})
16216	}
16217}
16218
16219impl_writeable_tlv_based!(PendingInboundPayment, {
16220	(0, payment_secret, required),
16221	(2, expiry_time, required),
16222	(4, user_payment_id, required),
16223	(6, payment_preimage, required),
16224	(8, min_value_msat, required),
16225});
16226
16227impl<
16228		M: Deref,
16229		T: Deref,
16230		ES: Deref,
16231		NS: Deref,
16232		SP: Deref,
16233		F: Deref,
16234		R: Deref,
16235		MR: Deref,
16236		L: Deref,
16237	> Writeable for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
16238where
16239	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
16240	T::Target: BroadcasterInterface,
16241	ES::Target: EntropySource,
16242	NS::Target: NodeSigner,
16243	SP::Target: SignerProvider,
16244	F::Target: FeeEstimator,
16245	R::Target: Router,
16246	MR::Target: MessageRouter,
16247	L::Target: Logger,
16248{
16249	#[rustfmt::skip]
16250	fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
16251		let _consistency_lock = self.total_consistency_lock.write().unwrap();
16252
16253		write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
16254
16255		self.chain_hash.write(writer)?;
16256		{
16257			let best_block = self.best_block.read().unwrap();
16258			best_block.height.write(writer)?;
16259			best_block.block_hash.write(writer)?;
16260		}
16261
16262		let per_peer_state = self.per_peer_state.write().unwrap();
16263
16264		let mut serializable_peer_count: u64 = 0;
16265		{
16266			let mut number_of_funded_channels = 0;
16267			for (_, peer_state_mutex) in per_peer_state.iter() {
16268				let mut peer_state_lock = peer_state_mutex.lock().unwrap();
16269				let peer_state = &mut *peer_state_lock;
16270				if !peer_state.ok_to_remove(false) {
16271					serializable_peer_count += 1;
16272				}
16273
16274				number_of_funded_channels += peer_state.channel_by_id
16275					.values()
16276					.filter_map(Channel::as_funded)
16277					.filter(|chan| chan.context.can_resume_on_restart())
16278					.count();
16279			}
16280
16281			(number_of_funded_channels as u64).write(writer)?;
16282
16283			for (_, peer_state_mutex) in per_peer_state.iter() {
16284				let mut peer_state_lock = peer_state_mutex.lock().unwrap();
16285				let peer_state = &mut *peer_state_lock;
16286				for channel in peer_state.channel_by_id
16287					.values()
16288					.filter_map(Channel::as_funded)
16289					.filter(|channel| channel.context.can_resume_on_restart())
16290				{
16291					channel.write(writer)?;
16292				}
16293			}
16294		}
16295
16296		{
16297			let forward_htlcs = self.forward_htlcs.lock().unwrap();
16298			(forward_htlcs.len() as u64).write(writer)?;
16299			for (short_channel_id, pending_forwards) in forward_htlcs.iter() {
16300				short_channel_id.write(writer)?;
16301				(pending_forwards.len() as u64).write(writer)?;
16302				for forward in pending_forwards {
16303					forward.write(writer)?;
16304				}
16305			}
16306		}
16307
16308		let mut decode_update_add_htlcs_opt = None;
16309		let decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
16310		if !decode_update_add_htlcs.is_empty() {
16311			decode_update_add_htlcs_opt = Some(decode_update_add_htlcs);
16312		}
16313
16314		let claimable_payments = self.claimable_payments.lock().unwrap();
16315		let pending_outbound_payments = self.pending_outbound_payments.pending_outbound_payments.lock().unwrap();
16316
16317		let mut htlc_purposes: Vec<&events::PaymentPurpose> = Vec::new();
16318		let mut htlc_onion_fields: Vec<&_> = Vec::new();
16319		(claimable_payments.claimable_payments.len() as u64).write(writer)?;
16320		for (payment_hash, payment) in claimable_payments.claimable_payments.iter() {
16321			payment_hash.write(writer)?;
16322			(payment.htlcs.len() as u64).write(writer)?;
16323			for htlc in payment.htlcs.iter() {
16324				htlc.write(writer)?;
16325			}
16326			htlc_purposes.push(&payment.purpose);
16327			htlc_onion_fields.push(&payment.onion_fields);
16328		}
16329
16330		let mut monitor_update_blocked_actions_per_peer = None;
16331		let mut peer_states = Vec::new();
16332		for (_, peer_state_mutex) in per_peer_state.iter() {
16333			// Because we're holding the owning `per_peer_state` write lock here there's no chance
16334			// of a lockorder violation deadlock - no other thread can be holding any
16335			// per_peer_state lock at all.
16336			peer_states.push(peer_state_mutex.unsafe_well_ordered_double_lock_self());
16337		}
16338
16339		let mut peer_storage_dir: Vec<(&PublicKey, &Vec<u8>)> = Vec::new();
16340
16341		(serializable_peer_count).write(writer)?;
16342		for ((peer_pubkey, _), peer_state) in per_peer_state.iter().zip(peer_states.iter()) {
16343			// Peers which we have no channels to should be dropped once disconnected. As we
16344			// disconnect all peers when shutting down and serializing the ChannelManager, we
16345			// consider all peers as disconnected here. There's therefore no need write peers with
16346			// no channels.
16347			if !peer_state.ok_to_remove(false) {
16348				peer_pubkey.write(writer)?;
16349				peer_state.latest_features.write(writer)?;
16350				peer_storage_dir.push((peer_pubkey, &peer_state.peer_storage));
16351
16352				if !peer_state.monitor_update_blocked_actions.is_empty() {
16353					monitor_update_blocked_actions_per_peer
16354						.get_or_insert_with(Vec::new)
16355						.push((*peer_pubkey, &peer_state.monitor_update_blocked_actions));
16356				}
16357			}
16358		}
16359
16360
16361		// Since some FundingNegotiation variants are not persisted, any splice in such state must
16362		// be failed upon reload. However, as the necessary information for the SpliceFailed event
16363		// is not persisted, the event itself needs to be persisted even though it hasn't been
16364		// emitted yet.
16365		// They are written alongside the pending events below but never added to them, as writing
16366		// may fail.
16367		let mut splice_failed_events: Vec<(Event, Option<EventCompletionAction>)> = Vec::new();
16368		for peer_state in peer_states.iter() {
16369			for chan in peer_state.channel_by_id.values().filter_map(Channel::as_funded) {
16370				if let Some(splice_funding_failed) = chan.maybe_splice_funding_failed() {
16371					splice_failed_events.push((
16372						events::Event::SpliceFailed {
16373							channel_id: chan.context.channel_id(),
16374							counterparty_node_id: chan.context.get_counterparty_node_id(),
16375							user_channel_id: chan.context.get_user_id(),
16376							abandoned_funding_txo: splice_funding_failed.funding_txo,
16377							channel_type: splice_funding_failed.channel_type,
16378							contributed_inputs: splice_funding_failed.contributed_inputs,
16379							contributed_outputs: splice_funding_failed.contributed_outputs,
16380						},
16381						None,
16382					));
16383				}
16384			}
16385		}
16386		let events = self.pending_events.lock().unwrap();
16387
16388		// LDK versions prior to 0.0.115 don't support post-event actions, thus if there's no
16389		// actions at all, skip writing the required TLV. Otherwise, pre-0.0.115 versions will
16390		// refuse to read the new ChannelManager.
16391		let events_not_backwards_compatible = events.iter().any(|(_, action)| action.is_some());
16392		if events_not_backwards_compatible {
16393			// If we're gonna write a even TLV that will overwrite our events anyway we might as
16394			// well save the space and not write any events here.
16395			0u64.write(writer)?;
16396		} else {
16397			((events.len() + splice_failed_events.len()) as u64).write(writer)?;
16398			for (event, _) in events.iter().chain(splice_failed_events.iter()) {
16399				event.write(writer)?;
16400			}
16401		}
16402
16403		// LDK versions prior to 0.0.116 wrote the `pending_background_events`
16404		// `MonitorUpdateRegeneratedOnStartup`s here, however there was never a reason to do so -
16405		// the closing monitor updates were always effectively replayed on startup (either directly
16406		// by calling `broadcast_latest_holder_commitment_txn` on a `ChannelMonitor` during
16407		// deserialization or, in 0.0.115, by regenerating the monitor update itself).
16408		0u64.write(writer)?;
16409
16410		// Prior to 0.0.111 we tracked node_announcement serials here, however that now happens in
16411		// `PeerManager`, and thus we simply write the `highest_seen_timestamp` twice, which is
16412		// likely to be identical.
16413		(self.highest_seen_timestamp.load(Ordering::Acquire) as u32).write(writer)?;
16414		(self.highest_seen_timestamp.load(Ordering::Acquire) as u32).write(writer)?;
16415
16416		// LDK versions prior to 0.0.104 wrote `pending_inbound_payments` here, with deprecated support
16417		// for stateful inbound payments maintained until 0.0.116, after which no further inbound
16418		// payments could have been written here.
16419		(0 as u64).write(writer)?;
16420
16421		// For backwards compat, write the session privs and their total length.
16422		let mut num_pending_outbounds_compat: u64 = 0;
16423		for (_, outbound) in pending_outbound_payments.iter() {
16424			if !outbound.is_fulfilled() && !outbound.abandoned() {
16425				num_pending_outbounds_compat += outbound.remaining_parts() as u64;
16426			}
16427		}
16428		num_pending_outbounds_compat.write(writer)?;
16429		for (_, outbound) in pending_outbound_payments.iter() {
16430			match outbound {
16431				PendingOutboundPayment::Legacy { session_privs } |
16432				PendingOutboundPayment::Retryable { session_privs, .. } => {
16433					for session_priv in session_privs.iter() {
16434						session_priv.write(writer)?;
16435					}
16436				}
16437				PendingOutboundPayment::AwaitingInvoice { .. } => {},
16438				PendingOutboundPayment::AwaitingOffer { .. } => {},
16439				PendingOutboundPayment::InvoiceReceived { .. } => {},
16440				PendingOutboundPayment::StaticInvoiceReceived { .. } => {},
16441				PendingOutboundPayment::Fulfilled { .. } => {},
16442				PendingOutboundPayment::Abandoned { .. } => {},
16443			}
16444		}
16445
16446		// Encode without retry info for 0.0.101 compatibility.
16447		let mut pending_outbound_payments_no_retry: HashMap<PaymentId, HashSet<[u8; 32]>> = new_hash_map();
16448		for (id, outbound) in pending_outbound_payments.iter() {
16449			match outbound {
16450				PendingOutboundPayment::Legacy { session_privs } |
16451				PendingOutboundPayment::Retryable { session_privs, .. } => {
16452					pending_outbound_payments_no_retry.insert(*id, session_privs.clone());
16453				},
16454				_ => {},
16455			}
16456		}
16457
16458		let mut pending_intercepted_htlcs = None;
16459		let our_pending_intercepts = self.pending_intercepted_htlcs.lock().unwrap();
16460		if our_pending_intercepts.len() != 0 {
16461			pending_intercepted_htlcs = Some(our_pending_intercepts);
16462		}
16463
16464		let mut pending_claiming_payments = Some(&claimable_payments.pending_claiming_payments);
16465		if pending_claiming_payments.as_ref().unwrap().is_empty() {
16466			// LDK versions prior to 0.0.113 do not know how to read the pending claimed payments
16467			// map. Thus, if there are no entries we skip writing a TLV for it.
16468			pending_claiming_payments = None;
16469		}
16470
16471		let mut legacy_in_flight_monitor_updates: Option<HashMap<(&PublicKey, &OutPoint), &Vec<ChannelMonitorUpdate>>> = None;
16472		let mut in_flight_monitor_updates: Option<HashMap<(&PublicKey, &ChannelId), &Vec<ChannelMonitorUpdate>>> = None;
16473		for ((counterparty_id, _), peer_state) in per_peer_state.iter().zip(peer_states.iter()) {
16474			for (channel_id, (funding_txo, updates)) in peer_state.in_flight_monitor_updates.iter() {
16475				if !updates.is_empty() {
16476					legacy_in_flight_monitor_updates.get_or_insert_with(|| new_hash_map())
16477						.insert((counterparty_id, funding_txo), updates);
16478					in_flight_monitor_updates.get_or_insert_with(|| new_hash_map())
16479						.insert((counterparty_id, channel_id), updates);
16480				}
16481			}
16482		}
16483
16484		let pending_events_writer = PendingEventsWriter {
16485			pending_events: &*events,
16486			splice_failed_events: &splice_failed_events,
16487		};
16488
16489		write_tlv_fields!(writer, {
16490			(1, pending_outbound_payments_no_retry, required),
16491			(2, pending_intercepted_htlcs, option),
16492			(3, pending_outbound_payments, required),
16493			(4, pending_claiming_payments, option),
16494			(5, self.our_network_pubkey, required),
16495			(6, monitor_update_blocked_actions_per_peer, option),
16496			(7, self.fake_scid_rand_bytes, required),
16497			(8, if events_not_backwards_compatible { Some(&pending_events_writer) } else { None }, option),
16498			(9, htlc_purposes, required_vec),
16499			(10, legacy_in_flight_monitor_updates, option),
16500			(11, self.probing_cookie_secret, required),
16501			(13, htlc_onion_fields, optional_vec),
16502			(14, decode_update_add_htlcs_opt, option),
16503			(15, self.inbound_payment_id_secret, required),
16504			(17, in_flight_monitor_updates, option),
16505			(19, peer_storage_dir, optional_vec),
16506			(21, WithoutLength(&self.flow.writeable_async_receive_offer_cache()), required),
16507		});
16508
16509		Ok(())
16510	}
16511}
16512
16513/// Writes the pending events of a [`ChannelManager`] chained with any events that are only
16514/// generated at serialization time, as if they were a single sequence of events.
16515struct PendingEventsWriter<'a> {
16516	pending_events: &'a VecDeque<(Event, Option<EventCompletionAction>)>,
16517	splice_failed_events: &'a [(Event, Option<EventCompletionAction>)],
16518}
16519
16520impl Writeable for PendingEventsWriter<'_> {
16521	fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
16522		let Self { pending_events, splice_failed_events } = self;
16523		((pending_events.len() + splice_failed_events.len()) as u64).write(w)?;
16524		for (event, action) in pending_events.iter().chain(splice_failed_events.iter()) {
16525			event.write(w)?;
16526			action.write(w)?;
16527			#[cfg(debug_assertions)]
16528			{
16529				// Events are MaybeReadable, in some cases indicating that they shouldn't actually
16530				// be persisted and are regenerated on restart. However, if such an event has a
16531				// post-event-handling action we'll write nothing for the event and would have to
16532				// either forget the action or fail on deserialization (which we do below). Thus,
16533				// check that the event is sane here.
16534				let event_encoded = event.encode();
16535				let event_read: Option<Event> =
16536					MaybeReadable::read(&mut &event_encoded[..]).unwrap();
16537				if action.is_some() {
16538					assert!(event_read.is_some());
16539				}
16540			}
16541		}
16542		Ok(())
16543	}
16544}
16545impl Readable for VecDeque<(Event, Option<EventCompletionAction>)> {
16546	fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
16547		let len: u64 = Readable::read(reader)?;
16548		const MAX_ALLOC_SIZE: u64 = 1024 * 16;
16549		let event_size = mem::size_of::<(events::Event, Option<EventCompletionAction>)>();
16550		let mut events: Self =
16551			VecDeque::with_capacity(cmp::min(MAX_ALLOC_SIZE / event_size as u64, len) as usize);
16552		for _ in 0..len {
16553			let ev_opt = MaybeReadable::read(reader)?;
16554			let action = Readable::read(reader)?;
16555			if let Some(ev) = ev_opt {
16556				events.push_back((ev, action));
16557			} else if action.is_some() {
16558				return Err(DecodeError::InvalidValue);
16559			}
16560		}
16561		Ok(events)
16562	}
16563}
16564
16565/// Arguments for the creation of a ChannelManager that are not deserialized.
16566///
16567/// At a high-level, the process for deserializing a ChannelManager and resuming normal operation
16568/// is:
16569/// 1) Deserialize all stored [`ChannelMonitor`]s.
16570/// 2) Deserialize the [`ChannelManager`] by filling in this struct and calling:
16571///    `<(BlockHash, ChannelManager)>::read(reader, args)`
16572///    This may result in closing some channels if the [`ChannelMonitor`] is newer than the stored
16573///    [`ChannelManager`] state to ensure no loss of funds. Thus, transactions may be broadcasted.
16574/// 3) If you are not fetching full blocks, register all relevant [`ChannelMonitor`] outpoints the
16575///    same way you would handle a [`chain::Filter`] call using
16576///    [`ChannelMonitor::get_outputs_to_watch`] and [`ChannelMonitor::get_funding_txo`].
16577/// 4) Disconnect/connect blocks on your [`ChannelMonitor`]s to get them in sync with the chain.
16578/// 5) Disconnect/connect blocks on the [`ChannelManager`] to get it in sync with the chain.
16579/// 6) Optionally re-persist the [`ChannelMonitor`]s to ensure the latest state is on disk.
16580///    This is important if you have replayed a nontrivial number of blocks in step (4), allowing
16581///    you to avoid having to replay the same blocks if you shut down quickly after startup. It is
16582///    otherwise not required.
16583///
16584///    Note that if you're using a [`ChainMonitor`] for your [`chain::Watch`] implementation, you
16585///    will likely accomplish this as a side-effect of calling [`chain::Watch::watch_channel`] in
16586///    the next step.
16587///
16588///    If you wish to avoid this for performance reasons, use
16589///    [`ChainMonitor::load_existing_monitor`].
16590/// 7) Move the [`ChannelMonitor`]s into your local [`chain::Watch`]. If you're using a
16591///    [`ChainMonitor`], this is done by calling [`chain::Watch::watch_channel`].
16592///
16593/// Note that the ordering of #4-7 is not of importance, however all four must occur before you
16594/// call any other methods on the newly-deserialized [`ChannelManager`].
16595///
16596/// Note that because some channels may be closed during deserialization, it is critical that you
16597/// always deserialize only the latest version of a ChannelManager and ChannelMonitors available to
16598/// you. If you deserialize an old ChannelManager (during which force-closure transactions may be
16599/// broadcast), and then later deserialize a newer version of the same ChannelManager (which will
16600/// not force-close the same channels but consider them live), you may end up revoking a state for
16601/// which you've already broadcasted the transaction.
16602///
16603/// [`ChainMonitor`]: crate::chain::chainmonitor::ChainMonitor
16604/// [`ChainMonitor::load_existing_monitor`]: crate::chain::chainmonitor::ChainMonitor::load_existing_monitor
16605pub struct ChannelManagerReadArgs<
16606	'a,
16607	M: Deref,
16608	T: Deref,
16609	ES: Deref,
16610	NS: Deref,
16611	SP: Deref,
16612	F: Deref,
16613	R: Deref,
16614	MR: Deref,
16615	L: Deref + Clone,
16616> where
16617	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
16618	T::Target: BroadcasterInterface,
16619	ES::Target: EntropySource,
16620	NS::Target: NodeSigner,
16621	SP::Target: SignerProvider,
16622	F::Target: FeeEstimator,
16623	R::Target: Router,
16624	MR::Target: MessageRouter,
16625	L::Target: Logger,
16626{
16627	/// A cryptographically secure source of entropy.
16628	pub entropy_source: ES,
16629
16630	/// A signer that is able to perform node-scoped cryptographic operations.
16631	pub node_signer: NS,
16632
16633	/// The keys provider which will give us relevant keys. Some keys will be loaded during
16634	/// deserialization and [`SignerProvider::derive_channel_signer`] will be used to derive
16635	/// per-Channel signing data.
16636	pub signer_provider: SP,
16637
16638	/// The fee_estimator for use in the ChannelManager in the future.
16639	///
16640	/// No calls to the FeeEstimator will be made during deserialization.
16641	pub fee_estimator: F,
16642	/// The chain::Watch for use in the ChannelManager in the future.
16643	///
16644	/// No calls to the chain::Watch will be made during deserialization. It is assumed that
16645	/// you have deserialized ChannelMonitors separately and will add them to your
16646	/// chain::Watch after deserializing this ChannelManager.
16647	pub chain_monitor: M,
16648
16649	/// The BroadcasterInterface which will be used in the ChannelManager in the future and may be
16650	/// used to broadcast the latest local commitment transactions of channels which must be
16651	/// force-closed during deserialization.
16652	pub tx_broadcaster: T,
16653	/// The router which will be used in the ChannelManager in the future for finding routes
16654	/// on-the-fly for trampoline payments. Absent in private nodes that don't support forwarding.
16655	///
16656	/// No calls to the router will be made during deserialization.
16657	pub router: R,
16658	/// The [`MessageRouter`] used for constructing [`BlindedMessagePath`]s for [`Offer`]s,
16659	/// [`Refund`]s, and any reply paths.
16660	///
16661	/// [`BlindedMessagePath`]: crate::blinded_path::message::BlindedMessagePath
16662	pub message_router: MR,
16663	/// The Logger for use in the ChannelManager and which may be used to log information during
16664	/// deserialization.
16665	pub logger: L,
16666	/// Default settings used for new channels. Any existing channels will continue to use the
16667	/// runtime settings which were stored when the ChannelManager was serialized.
16668	pub config: UserConfig,
16669
16670	/// A map from channel IDs to ChannelMonitors for those channels.
16671	///
16672	/// If a monitor is inconsistent with the channel state during deserialization the channel will
16673	/// be force-closed using the data in the ChannelMonitor and the channel will be dropped. This
16674	/// is true for missing channels as well. If there is a monitor missing for which we find
16675	/// channel data Err(DecodeError::InvalidValue) will be returned.
16676	///
16677	/// In such cases the latest local transactions will be sent to the tx_broadcaster included in
16678	/// this struct.
16679	///
16680	/// This is not exported to bindings users because we have no HashMap bindings
16681	pub channel_monitors:
16682		HashMap<ChannelId, &'a ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>>,
16683}
16684
16685impl<
16686		'a,
16687		M: Deref,
16688		T: Deref,
16689		ES: Deref,
16690		NS: Deref,
16691		SP: Deref,
16692		F: Deref,
16693		R: Deref,
16694		MR: Deref,
16695		L: Deref + Clone,
16696	> ChannelManagerReadArgs<'a, M, T, ES, NS, SP, F, R, MR, L>
16697where
16698	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
16699	T::Target: BroadcasterInterface,
16700	ES::Target: EntropySource,
16701	NS::Target: NodeSigner,
16702	SP::Target: SignerProvider,
16703	F::Target: FeeEstimator,
16704	R::Target: Router,
16705	MR::Target: MessageRouter,
16706	L::Target: Logger,
16707{
16708	/// Simple utility function to create a ChannelManagerReadArgs which creates the monitor
16709	/// HashMap for you. This is primarily useful for C bindings where it is not practical to
16710	/// populate a HashMap directly from C.
16711	pub fn new(
16712		entropy_source: ES, node_signer: NS, signer_provider: SP, fee_estimator: F,
16713		chain_monitor: M, tx_broadcaster: T, router: R, message_router: MR, logger: L,
16714		config: UserConfig,
16715		mut channel_monitors: Vec<&'a ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>>,
16716	) -> Self {
16717		Self {
16718			entropy_source,
16719			node_signer,
16720			signer_provider,
16721			fee_estimator,
16722			chain_monitor,
16723			tx_broadcaster,
16724			router,
16725			message_router,
16726			logger,
16727			config,
16728			channel_monitors: hash_map_from_iter(
16729				channel_monitors.drain(..).map(|monitor| (monitor.channel_id(), monitor)),
16730			),
16731		}
16732	}
16733}
16734
16735// Implement ReadableArgs for an Arc'd ChannelManager to make it a bit easier to work with the
16736// SipmleArcChannelManager type:
16737impl<
16738		'a,
16739		M: Deref,
16740		T: Deref,
16741		ES: Deref,
16742		NS: Deref,
16743		SP: Deref,
16744		F: Deref,
16745		R: Deref,
16746		MR: Deref,
16747		L: Deref + Clone,
16748	> ReadableArgs<ChannelManagerReadArgs<'a, M, T, ES, NS, SP, F, R, MR, L>>
16749	for (BlockHash, Arc<ChannelManager<M, T, ES, NS, SP, F, R, MR, L>>)
16750where
16751	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
16752	T::Target: BroadcasterInterface,
16753	ES::Target: EntropySource,
16754	NS::Target: NodeSigner,
16755	SP::Target: SignerProvider,
16756	F::Target: FeeEstimator,
16757	R::Target: Router,
16758	MR::Target: MessageRouter,
16759	L::Target: Logger,
16760{
16761	fn read<Reader: io::Read>(
16762		reader: &mut Reader, args: ChannelManagerReadArgs<'a, M, T, ES, NS, SP, F, R, MR, L>,
16763	) -> Result<Self, DecodeError> {
16764		let (blockhash, chan_manager) =
16765			<(BlockHash, ChannelManager<M, T, ES, NS, SP, F, R, MR, L>)>::read(reader, args)?;
16766		Ok((blockhash, Arc::new(chan_manager)))
16767	}
16768}
16769
16770impl<
16771		'a,
16772		M: Deref,
16773		T: Deref,
16774		ES: Deref,
16775		NS: Deref,
16776		SP: Deref,
16777		F: Deref,
16778		R: Deref,
16779		MR: Deref,
16780		L: Deref + Clone,
16781	> ReadableArgs<ChannelManagerReadArgs<'a, M, T, ES, NS, SP, F, R, MR, L>>
16782	for (BlockHash, ChannelManager<M, T, ES, NS, SP, F, R, MR, L>)
16783where
16784	M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
16785	T::Target: BroadcasterInterface,
16786	ES::Target: EntropySource,
16787	NS::Target: NodeSigner,
16788	SP::Target: SignerProvider,
16789	F::Target: FeeEstimator,
16790	R::Target: Router,
16791	MR::Target: MessageRouter,
16792	L::Target: Logger,
16793{
16794	fn read<Reader: io::Read>(
16795		reader: &mut Reader, mut args: ChannelManagerReadArgs<'a, M, T, ES, NS, SP, F, R, MR, L>,
16796	) -> Result<Self, DecodeError> {
16797		let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
16798
16799		let chain_hash: ChainHash = Readable::read(reader)?;
16800		let best_block_height: u32 = Readable::read(reader)?;
16801		let best_block_hash: BlockHash = Readable::read(reader)?;
16802
16803		let empty_peer_state = || PeerState {
16804			channel_by_id: new_hash_map(),
16805			inbound_channel_request_by_id: new_hash_map(),
16806			latest_features: InitFeatures::empty(),
16807			pending_msg_events: Vec::new(),
16808			in_flight_monitor_updates: BTreeMap::new(),
16809			monitor_update_blocked_actions: BTreeMap::new(),
16810			actions_blocking_raa_monitor_updates: BTreeMap::new(),
16811			closed_channel_monitor_update_ids: BTreeMap::new(),
16812			peer_storage: Vec::new(),
16813			is_connected: false,
16814		};
16815
16816		let mut failed_htlcs = Vec::new();
16817		let channel_count: u64 = Readable::read(reader)?;
16818		let mut channel_id_set = hash_set_with_capacity(cmp::min(channel_count as usize, 128));
16819		let mut per_peer_state = hash_map_with_capacity(cmp::min(
16820			channel_count as usize,
16821			MAX_ALLOC_SIZE / mem::size_of::<(PublicKey, Mutex<PeerState<SP>>)>(),
16822		));
16823		let mut short_to_chan_info = hash_map_with_capacity(cmp::min(channel_count as usize, 128));
16824		let mut channel_closures = VecDeque::new();
16825		let mut close_background_events = Vec::new();
16826		for _ in 0..channel_count {
16827			let mut channel: FundedChannel<SP> = FundedChannel::read(
16828				reader,
16829				(
16830					&args.entropy_source,
16831					&args.signer_provider,
16832					&provided_channel_type_features(&args.config),
16833				),
16834			)?;
16835			let logger = WithChannelContext::from(&args.logger, &channel.context, None);
16836			let channel_id = channel.context.channel_id();
16837			channel_id_set.insert(channel_id);
16838			if let Some(ref mut monitor) = args.channel_monitors.get_mut(&channel_id) {
16839				// Blocked updates the `ChannelMonitor` has since applied must be dropped before
16840				// checking whether we're stale, as force-closing a stale channel fails back the
16841				// HTLCs added in its blocked updates, which the counterparty is committed to if
16842				// the update was applied.
16843				channel.on_startup_drop_completed_blocked_mon_updates_through(
16844					&logger,
16845					monitor.get_latest_update_id(),
16846				);
16847				if channel.get_cur_holder_commitment_transaction_number()
16848					> monitor.get_cur_holder_commitment_number()
16849					|| channel.get_revoked_counterparty_commitment_transaction_number()
16850						> monitor.get_min_seen_secret()
16851					|| channel.get_cur_counterparty_commitment_transaction_number()
16852						> monitor.get_cur_counterparty_commitment_number()
16853					|| channel.context.get_latest_monitor_update_id()
16854						< monitor.get_latest_update_id()
16855				{
16856					// But if the channel is behind of the monitor, close the channel:
16857					log_error!(
16858						logger,
16859						"A ChannelManager is stale compared to the current ChannelMonitor!"
16860					);
16861					log_error!(logger, " The channel will be force-closed and the latest commitment transaction from the ChannelMonitor broadcast.");
16862					if channel.context.get_latest_monitor_update_id()
16863						< monitor.get_latest_update_id()
16864					{
16865						log_error!(logger, " The ChannelMonitor for channel {} is at update_id {} but the ChannelManager is at update_id {}.",
16866							&channel.context.channel_id(), monitor.get_latest_update_id(), channel.context.get_latest_monitor_update_id());
16867					}
16868					if channel.get_cur_holder_commitment_transaction_number()
16869						> monitor.get_cur_holder_commitment_number()
16870					{
16871						log_error!(logger, " The ChannelMonitor for channel {} is at holder commitment number {} but the ChannelManager is at holder commitment number {}.",
16872							&channel.context.channel_id(), monitor.get_cur_holder_commitment_number(), channel.get_cur_holder_commitment_transaction_number());
16873					}
16874					if channel.get_revoked_counterparty_commitment_transaction_number()
16875						> monitor.get_min_seen_secret()
16876					{
16877						log_error!(logger, " The ChannelMonitor for channel {} is at revoked counterparty transaction number {} but the ChannelManager is at revoked counterparty transaction number {}.",
16878							&channel.context.channel_id(), monitor.get_min_seen_secret(), channel.get_revoked_counterparty_commitment_transaction_number());
16879					}
16880					if channel.get_cur_counterparty_commitment_transaction_number()
16881						> monitor.get_cur_counterparty_commitment_number()
16882					{
16883						log_error!(logger, " The ChannelMonitor for channel {} is at counterparty commitment transaction number {} but the ChannelManager is at counterparty commitment transaction number {}.",
16884							&channel.context.channel_id(), monitor.get_cur_counterparty_commitment_number(), channel.get_cur_counterparty_commitment_transaction_number());
16885					}
16886					let shutdown_result =
16887						channel.force_shutdown(ClosureReason::OutdatedChannelManager);
16888					if shutdown_result.unbroadcasted_batch_funding_txid.is_some() {
16889						return Err(DecodeError::InvalidValue);
16890					}
16891					if let Some((counterparty_node_id, funding_txo, channel_id, mut update)) =
16892						shutdown_result.monitor_update
16893					{
16894						// Our channel information is out of sync with the `ChannelMonitor`, so
16895						// force the update to use the `ChannelMonitor`'s update_id for the close
16896						// update.
16897						let latest_update_id = monitor.get_latest_update_id().saturating_add(1);
16898						update.update_id = latest_update_id;
16899						per_peer_state
16900							.entry(counterparty_node_id)
16901							.or_insert_with(|| Mutex::new(empty_peer_state()))
16902							.lock()
16903							.unwrap()
16904							.closed_channel_monitor_update_ids
16905							.entry(channel_id)
16906							.and_modify(|v| *v = cmp::max(latest_update_id, *v))
16907							.or_insert(latest_update_id);
16908
16909						close_background_events.push(
16910							BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
16911								counterparty_node_id,
16912								funding_txo,
16913								channel_id,
16914								update,
16915							},
16916						);
16917					}
16918					for (source, hash, cp_id, chan_id) in shutdown_result.dropped_outbound_htlcs {
16919						let reason = LocalHTLCFailureReason::ChannelClosed;
16920						failed_htlcs.push((source, hash, cp_id, chan_id, reason, None));
16921					}
16922					channel_closures.push_back((
16923						events::Event::ChannelClosed {
16924							channel_id: channel.context.channel_id(),
16925							user_channel_id: channel.context.get_user_id(),
16926							reason: ClosureReason::OutdatedChannelManager,
16927							counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
16928							channel_capacity_sats: Some(channel.funding.get_value_satoshis()),
16929							channel_funding_txo: channel.funding.get_funding_txo(),
16930							last_local_balance_msat: Some(channel.funding.get_value_to_self_msat()),
16931						},
16932						None,
16933					));
16934					for (channel_htlc_source, payment_hash) in channel.inflight_htlc_sources() {
16935						let mut found_htlc = false;
16936						for (monitor_htlc_source, _) in monitor.get_all_current_outbound_htlcs() {
16937							if *channel_htlc_source == monitor_htlc_source {
16938								found_htlc = true;
16939								break;
16940							}
16941						}
16942						if !found_htlc {
16943							// If we have some HTLCs in the channel which are not present in the newer
16944							// ChannelMonitor, they have been removed and should be failed back to
16945							// ensure we don't forget them entirely. Note that if the missing HTLC(s)
16946							// were actually claimed we'd have generated and ensured the previous-hop
16947							// claim update ChannelMonitor updates were persisted prior to persising
16948							// the ChannelMonitor update for the forward leg, so attempting to fail the
16949							// backwards leg of the HTLC will simply be rejected.
16950							let logger = WithChannelContext::from(
16951								&args.logger,
16952								&channel.context,
16953								Some(*payment_hash),
16954							);
16955							log_info!(logger,
16956								"Failing HTLC with hash {} as it is missing in the ChannelMonitor for channel {} but was present in the (stale) ChannelManager",
16957								&channel.context.channel_id(), &payment_hash);
16958							failed_htlcs.push((
16959								channel_htlc_source.clone(),
16960								*payment_hash,
16961								channel.context.get_counterparty_node_id(),
16962								channel.context.channel_id(),
16963								LocalHTLCFailureReason::ChannelClosed,
16964								None,
16965							));
16966						}
16967					}
16968				} else {
16969					log_info!(logger, "Successfully loaded channel {} at update_id {} against monitor at update id {} with {} blocked updates",
16970						&channel.context.channel_id(), channel.context.get_latest_monitor_update_id(),
16971						monitor.get_latest_update_id(), channel.blocked_monitor_updates_pending());
16972					if let Some(short_channel_id) = channel.funding.get_short_channel_id() {
16973						short_to_chan_info.insert(
16974							short_channel_id,
16975							(
16976								channel.context.get_counterparty_node_id(),
16977								channel.context.channel_id(),
16978							),
16979						);
16980					}
16981
16982					for short_channel_id in channel.context.historical_scids() {
16983						let cp_id = channel.context.get_counterparty_node_id();
16984						let chan_id = channel.context.channel_id();
16985						short_to_chan_info.insert(*short_channel_id, (cp_id, chan_id));
16986					}
16987
16988					per_peer_state
16989						.entry(channel.context.get_counterparty_node_id())
16990						.or_insert_with(|| Mutex::new(empty_peer_state()))
16991						.get_mut()
16992						.unwrap()
16993						.channel_by_id
16994						.insert(channel.context.channel_id(), Channel::from(channel));
16995				}
16996			} else if channel.is_awaiting_initial_mon_persist() {
16997				// If we were persisted and shut down while the initial ChannelMonitor persistence
16998				// was in-progress, we never broadcasted the funding transaction and can still
16999				// safely discard the channel.
17000				channel_closures.push_back((
17001					events::Event::ChannelClosed {
17002						channel_id: channel.context.channel_id(),
17003						user_channel_id: channel.context.get_user_id(),
17004						reason: ClosureReason::DisconnectedPeer,
17005						counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
17006						channel_capacity_sats: Some(channel.funding.get_value_satoshis()),
17007						channel_funding_txo: channel.funding.get_funding_txo(),
17008						last_local_balance_msat: Some(channel.funding.get_value_to_self_msat()),
17009					},
17010					None,
17011				));
17012			} else {
17013				log_error!(
17014					logger,
17015					"Missing ChannelMonitor for channel {} needed by ChannelManager.",
17016					&channel.context.channel_id()
17017				);
17018				log_error!(logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
17019				log_error!(logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
17020				log_error!(
17021					logger,
17022					" Without the ChannelMonitor we cannot continue without risking funds."
17023				);
17024				log_error!(logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
17025				return Err(DecodeError::InvalidValue);
17026			}
17027		}
17028
17029		for (channel_id, monitor) in args.channel_monitors.iter() {
17030			if !channel_id_set.contains(channel_id) {
17031				let mut should_queue_fc_update = false;
17032				let counterparty_node_id = monitor.get_counterparty_node_id();
17033
17034				// If the ChannelMonitor had any updates, we may need to update it further and
17035				// thus track it in `closed_channel_monitor_update_ids`. If the channel never
17036				// had any updates at all, there can't be any HTLCs pending which we need to
17037				// claim.
17038				// Note that a `ChannelMonitor` is created with `update_id` 0 and after we
17039				// provide it with a closure update its `update_id` will be at 1.
17040				if !monitor.no_further_updates_allowed() || monitor.get_latest_update_id() > 1 {
17041					should_queue_fc_update = !monitor.no_further_updates_allowed();
17042					let mut latest_update_id = monitor.get_latest_update_id();
17043					if should_queue_fc_update {
17044						// Note that for channels closed pre-0.1, the latest update_id is
17045						// `u64::MAX`.
17046						latest_update_id = latest_update_id.saturating_add(1);
17047					}
17048					per_peer_state
17049						.entry(counterparty_node_id)
17050						.or_insert_with(|| Mutex::new(empty_peer_state()))
17051						.lock()
17052						.unwrap()
17053						.closed_channel_monitor_update_ids
17054						.entry(monitor.channel_id())
17055						.and_modify(|v| *v = cmp::max(latest_update_id, *v))
17056						.or_insert(latest_update_id);
17057				}
17058
17059				if !should_queue_fc_update {
17060					continue;
17061				}
17062
17063				let logger = WithChannelMonitor::from(&args.logger, monitor, None);
17064				let channel_id = monitor.channel_id();
17065				log_info!(
17066					logger,
17067					"Queueing monitor update to ensure missing channel {} is force closed",
17068					&channel_id
17069				);
17070				let monitor_update = ChannelMonitorUpdate {
17071					update_id: monitor.get_latest_update_id().saturating_add(1),
17072					updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed {
17073						should_broadcast: true,
17074						counterparty_failed_htlcs: Vec::new(),
17075					}],
17076					channel_id: Some(monitor.channel_id()),
17077				};
17078				let funding_txo = monitor.get_funding_txo();
17079				let update = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
17080					counterparty_node_id,
17081					funding_txo,
17082					channel_id,
17083					update: monitor_update,
17084				};
17085				close_background_events.push(update);
17086			}
17087		}
17088
17089		const MAX_ALLOC_SIZE: usize = 1024 * 64;
17090		let forward_htlcs_count: u64 = Readable::read(reader)?;
17091		let mut forward_htlcs = hash_map_with_capacity(cmp::min(forward_htlcs_count as usize, 128));
17092		for _ in 0..forward_htlcs_count {
17093			let short_channel_id = Readable::read(reader)?;
17094			let pending_forwards_count: u64 = Readable::read(reader)?;
17095			let mut pending_forwards = Vec::with_capacity(cmp::min(
17096				pending_forwards_count as usize,
17097				MAX_ALLOC_SIZE / mem::size_of::<HTLCForwardInfo>(),
17098			));
17099			for _ in 0..pending_forwards_count {
17100				pending_forwards.push(Readable::read(reader)?);
17101			}
17102			forward_htlcs.insert(short_channel_id, pending_forwards);
17103		}
17104
17105		let claimable_htlcs_count: u64 = Readable::read(reader)?;
17106		let mut claimable_htlcs_list =
17107			Vec::with_capacity(cmp::min(claimable_htlcs_count as usize, 128));
17108		for _ in 0..claimable_htlcs_count {
17109			let payment_hash = Readable::read(reader)?;
17110			let previous_hops_len: u64 = Readable::read(reader)?;
17111			let mut previous_hops = Vec::with_capacity(cmp::min(
17112				previous_hops_len as usize,
17113				MAX_ALLOC_SIZE / mem::size_of::<ClaimableHTLC>(),
17114			));
17115			for _ in 0..previous_hops_len {
17116				previous_hops.push(<ClaimableHTLC as Readable>::read(reader)?);
17117			}
17118			claimable_htlcs_list.push((payment_hash, previous_hops));
17119		}
17120
17121		let peer_count: u64 = Readable::read(reader)?;
17122		for _ in 0..peer_count {
17123			let peer_pubkey: PublicKey = Readable::read(reader)?;
17124			let latest_features = Readable::read(reader)?;
17125			if let Some(peer_state) = per_peer_state.get_mut(&peer_pubkey) {
17126				peer_state.get_mut().unwrap().latest_features = latest_features;
17127			}
17128		}
17129
17130		let event_count: u64 = Readable::read(reader)?;
17131		let mut pending_events_read: VecDeque<(events::Event, Option<EventCompletionAction>)> =
17132			VecDeque::with_capacity(cmp::min(
17133				event_count as usize,
17134				MAX_ALLOC_SIZE / mem::size_of::<(events::Event, Option<EventCompletionAction>)>(),
17135			));
17136		for _ in 0..event_count {
17137			match MaybeReadable::read(reader)? {
17138				Some(event) => pending_events_read.push_back((event, None)),
17139				None => continue,
17140			}
17141		}
17142
17143		let background_event_count: u64 = Readable::read(reader)?;
17144		for _ in 0..background_event_count {
17145			match <u8 as Readable>::read(reader)? {
17146				0 => {
17147					// LDK versions prior to 0.0.116 wrote pending `MonitorUpdateRegeneratedOnStartup`s here,
17148					// however we really don't (and never did) need them - we regenerate all
17149					// on-startup monitor updates.
17150					let _: OutPoint = Readable::read(reader)?;
17151					let _: ChannelMonitorUpdate = Readable::read(reader)?;
17152				},
17153				_ => return Err(DecodeError::InvalidValue),
17154			}
17155		}
17156
17157		let _last_node_announcement_serial: u32 = Readable::read(reader)?; // Only used < 0.0.111
17158		let highest_seen_timestamp: u32 = Readable::read(reader)?;
17159
17160		// The last version where a pending inbound payment may have been added was 0.0.116.
17161		let pending_inbound_payment_count: u64 = Readable::read(reader)?;
17162		for _ in 0..pending_inbound_payment_count {
17163			let payment_hash: PaymentHash = Readable::read(reader)?;
17164			let logger = WithContext::from(&args.logger, None, None, Some(payment_hash));
17165			let inbound: PendingInboundPayment = Readable::read(reader)?;
17166			log_warn!(
17167				logger,
17168				"Ignoring deprecated pending inbound payment with payment hash {}: {:?}",
17169				payment_hash,
17170				inbound
17171			);
17172		}
17173
17174		let pending_outbound_payments_count_compat: u64 = Readable::read(reader)?;
17175		let mut pending_outbound_payments_compat: HashMap<PaymentId, PendingOutboundPayment> =
17176			hash_map_with_capacity(cmp::min(
17177				pending_outbound_payments_count_compat as usize,
17178				MAX_ALLOC_SIZE / 32,
17179			));
17180		for _ in 0..pending_outbound_payments_count_compat {
17181			let session_priv = Readable::read(reader)?;
17182			let payment = PendingOutboundPayment::Legacy {
17183				session_privs: hash_set_from_iter([session_priv]),
17184			};
17185			if pending_outbound_payments_compat.insert(PaymentId(session_priv), payment).is_some() {
17186				return Err(DecodeError::InvalidValue);
17187			};
17188		}
17189
17190		// pending_outbound_payments_no_retry is for compatibility with 0.0.101 clients.
17191		let mut pending_outbound_payments_no_retry: Option<HashMap<PaymentId, HashSet<[u8; 32]>>> =
17192			None;
17193		let mut pending_outbound_payments = None;
17194		let mut pending_intercepted_htlcs: Option<HashMap<InterceptId, PendingAddHTLCInfo>> =
17195			Some(new_hash_map());
17196		let mut received_network_pubkey: Option<PublicKey> = None;
17197		let mut fake_scid_rand_bytes: Option<[u8; 32]> = None;
17198		let mut probing_cookie_secret: Option<[u8; 32]> = None;
17199		let mut claimable_htlc_purposes = None;
17200		let mut claimable_htlc_onion_fields = None;
17201		let mut pending_claiming_payments = Some(new_hash_map());
17202		let mut monitor_update_blocked_actions_per_peer: Option<Vec<(_, BTreeMap<_, Vec<_>>)>> =
17203			Some(Vec::new());
17204		let mut events_override = None;
17205		let mut legacy_in_flight_monitor_updates: Option<
17206			HashMap<(PublicKey, OutPoint), Vec<ChannelMonitorUpdate>>,
17207		> = None;
17208		// We use this one over the legacy since they represent the same data, just with a different
17209		// key. We still need to read the legacy one as it's an even TLV.
17210		let mut in_flight_monitor_updates: Option<
17211			HashMap<(PublicKey, ChannelId), Vec<ChannelMonitorUpdate>>,
17212		> = None;
17213		let mut decode_update_add_htlcs: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> = None;
17214		let mut inbound_payment_id_secret = None;
17215		let mut peer_storage_dir: Option<Vec<(PublicKey, Vec<u8>)>> = None;
17216		let mut async_receive_offer_cache: AsyncReceiveOfferCache = AsyncReceiveOfferCache::new();
17217		read_tlv_fields!(reader, {
17218			(1, pending_outbound_payments_no_retry, option),
17219			(2, pending_intercepted_htlcs, option),
17220			(3, pending_outbound_payments, option),
17221			(4, pending_claiming_payments, option),
17222			(5, received_network_pubkey, option),
17223			(6, monitor_update_blocked_actions_per_peer, option),
17224			(7, fake_scid_rand_bytes, option),
17225			(8, events_override, option),
17226			(9, claimable_htlc_purposes, optional_vec),
17227			(10, legacy_in_flight_monitor_updates, option),
17228			(11, probing_cookie_secret, option),
17229			(13, claimable_htlc_onion_fields, optional_vec),
17230			(14, decode_update_add_htlcs, option),
17231			(15, inbound_payment_id_secret, option),
17232			(17, in_flight_monitor_updates, option),
17233			(19, peer_storage_dir, optional_vec),
17234			(21, async_receive_offer_cache, (default_value, async_receive_offer_cache)),
17235		});
17236		let mut decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or_else(|| new_hash_map());
17237		let peer_storage_dir: Vec<(PublicKey, Vec<u8>)> = peer_storage_dir.unwrap_or_else(Vec::new);
17238		if fake_scid_rand_bytes.is_none() {
17239			fake_scid_rand_bytes = Some(args.entropy_source.get_secure_random_bytes());
17240		}
17241
17242		if probing_cookie_secret.is_none() {
17243			probing_cookie_secret = Some(args.entropy_source.get_secure_random_bytes());
17244		}
17245
17246		if inbound_payment_id_secret.is_none() {
17247			inbound_payment_id_secret = Some(args.entropy_source.get_secure_random_bytes());
17248		}
17249
17250		if let Some(events) = events_override {
17251			pending_events_read = events;
17252		}
17253
17254		if !channel_closures.is_empty() {
17255			pending_events_read.append(&mut channel_closures);
17256		}
17257
17258		if pending_outbound_payments.is_none() && pending_outbound_payments_no_retry.is_none() {
17259			pending_outbound_payments = Some(pending_outbound_payments_compat);
17260		} else if pending_outbound_payments.is_none() {
17261			let mut outbounds = new_hash_map();
17262			for (id, session_privs) in pending_outbound_payments_no_retry.unwrap().drain() {
17263				outbounds.insert(id, PendingOutboundPayment::Legacy { session_privs });
17264			}
17265			pending_outbound_payments = Some(outbounds);
17266		}
17267		let pending_outbounds = OutboundPayments::new(pending_outbound_payments.unwrap());
17268
17269		for (peer_pubkey, peer_storage) in peer_storage_dir {
17270			if let Some(peer_state) = per_peer_state.get_mut(&peer_pubkey) {
17271				peer_state.get_mut().unwrap().peer_storage = peer_storage;
17272			}
17273		}
17274
17275		// Handle transitioning from the legacy TLV to the new one on upgrades.
17276		if let Some(legacy_in_flight_upds) = legacy_in_flight_monitor_updates {
17277			// We should never serialize an empty map.
17278			if legacy_in_flight_upds.is_empty() {
17279				return Err(DecodeError::InvalidValue);
17280			}
17281			if in_flight_monitor_updates.is_none() {
17282				let in_flight_upds =
17283					in_flight_monitor_updates.get_or_insert_with(|| new_hash_map());
17284				for ((counterparty_node_id, funding_txo), updates) in legacy_in_flight_upds {
17285					// All channels with legacy in flight monitor updates are v1 channels.
17286					let channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
17287					in_flight_upds.insert((counterparty_node_id, channel_id), updates);
17288				}
17289			} else {
17290				// We should never serialize an empty map.
17291				if in_flight_monitor_updates.as_ref().unwrap().is_empty() {
17292					return Err(DecodeError::InvalidValue);
17293				}
17294			}
17295		}
17296
17297		// We have to replay (or skip, if they were completed after we wrote the `ChannelManager`)
17298		// each `ChannelMonitorUpdate` in `in_flight_monitor_updates`. After doing so, we have to
17299		// check that each channel we have isn't newer than the latest `ChannelMonitorUpdate`(s) we
17300		// replayed, and for each monitor update we have to replay we have to ensure there's a
17301		// `ChannelMonitor` for it.
17302		//
17303		// In order to do so we first walk all of our live channels (so that we can check their
17304		// state immediately after doing the update replays, when we have the `update_id`s
17305		// available) and then walk any remaining in-flight updates.
17306		//
17307		// Because the actual handling of the in-flight updates is the same, it's macro'ized here:
17308		let mut pending_background_events = Vec::new();
17309		macro_rules! handle_in_flight_updates {
17310			($counterparty_node_id: expr, $chan_in_flight_upds: expr, $monitor: expr,
17311			 $peer_state: expr, $logger: expr, $channel_info_log: expr
17312			) => { {
17313				// When all in-flight updates have completed after we were last serialized, we
17314				// need to remove them. However, we can't guarantee that the next serialization
17315				// will have happened after processing the
17316				// `BackgroundEvent::MonitorUpdatesComplete`, so removing them now could lead to the
17317				// channel never being resumed as the event would not be regenerated after another
17318				// reload. At the same time, we don't want to resume the channel now because there
17319				// may be post-update actions to handle. Therefore, we're forced to keep tracking
17320				// the completed in-flight updates (but only when they have all completed) until we
17321				// are processing the `BackgroundEvent::MonitorUpdatesComplete`.
17322				let mut max_in_flight_update_id = 0;
17323				let num_updates_completed = $chan_in_flight_upds
17324					.iter()
17325					.filter(|update| {
17326						max_in_flight_update_id = cmp::max(max_in_flight_update_id, update.update_id);
17327						update.update_id <= $monitor.get_latest_update_id()
17328					})
17329					.count();
17330				if num_updates_completed > 0 {
17331					log_debug!(
17332						$logger,
17333						"{} ChannelMonitorUpdates completed after ChannelManager was last serialized",
17334						num_updates_completed,
17335					);
17336				}
17337				let all_updates_completed = num_updates_completed == $chan_in_flight_upds.len();
17338
17339				let funding_txo = $monitor.get_funding_txo();
17340				if all_updates_completed {
17341					log_debug!($logger, "All monitor updates completed since the ChannelManager was last serialized");
17342					pending_background_events.push(
17343						BackgroundEvent::MonitorUpdatesComplete {
17344							counterparty_node_id: $counterparty_node_id,
17345							channel_id: $monitor.channel_id(),
17346							highest_update_id_completed: max_in_flight_update_id,
17347						});
17348				} else {
17349					$chan_in_flight_upds.retain(|update| {
17350						let replay = update.update_id > $monitor.get_latest_update_id();
17351						if replay {
17352							log_debug!($logger, "Replaying ChannelMonitorUpdate {} for {}channel {}",
17353								update.update_id, $channel_info_log, &$monitor.channel_id());
17354							pending_background_events.push(
17355								BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
17356									counterparty_node_id: $counterparty_node_id,
17357									funding_txo: funding_txo,
17358									channel_id: $monitor.channel_id(),
17359									update: update.clone(),
17360								}
17361							);
17362						}
17363						replay
17364					});
17365					$peer_state.closed_channel_monitor_update_ids.entry($monitor.channel_id())
17366						.and_modify(|v| *v = cmp::max(max_in_flight_update_id, *v))
17367						.or_insert(max_in_flight_update_id);
17368				}
17369				if $peer_state.in_flight_monitor_updates.insert($monitor.channel_id(), (funding_txo, $chan_in_flight_upds)).is_some() {
17370					log_error!($logger, "Duplicate in-flight monitor update set for the same channel!");
17371					return Err(DecodeError::InvalidValue);
17372				}
17373				max_in_flight_update_id
17374			} }
17375		}
17376
17377		for (counterparty_id, peer_state_mtx) in per_peer_state.iter_mut() {
17378			let mut peer_state_lock = peer_state_mtx.lock().unwrap();
17379			let peer_state = &mut *peer_state_lock;
17380			for (chan_id, chan) in peer_state.channel_by_id.iter() {
17381				if let Some(funded_chan) = chan.as_funded() {
17382					let logger = WithChannelContext::from(&args.logger, &funded_chan.context, None);
17383
17384					// Channels that were persisted have to be funded, otherwise they should have been
17385					// discarded.
17386					let monitor = args
17387						.channel_monitors
17388						.get(chan_id)
17389						.expect("We already checked for monitor presence when loading channels");
17390					let mut max_in_flight_update_id = monitor.get_latest_update_id();
17391					if let Some(in_flight_upds) = &mut in_flight_monitor_updates {
17392						if let Some(mut chan_in_flight_upds) =
17393							in_flight_upds.remove(&(*counterparty_id, *chan_id))
17394						{
17395							max_in_flight_update_id = cmp::max(
17396								max_in_flight_update_id,
17397								handle_in_flight_updates!(
17398									*counterparty_id,
17399									chan_in_flight_upds,
17400									monitor,
17401									peer_state,
17402									logger,
17403									""
17404								),
17405							);
17406						}
17407					}
17408					if funded_chan.get_latest_unblocked_monitor_update_id()
17409						> max_in_flight_update_id
17410					{
17411						// If the channel is ahead of the monitor, return DangerousValue:
17412						log_error!(logger, "A ChannelMonitor is stale compared to the current ChannelManager! This indicates a potentially-critical violation of the chain::Watch API!");
17413						log_error!(logger, " The ChannelMonitor for channel {} is at update_id {} with update_id through {} in-flight",
17414							chan_id, monitor.get_latest_update_id(), max_in_flight_update_id);
17415						log_error!(
17416							logger,
17417							" but the ChannelManager is at update_id {}.",
17418							funded_chan.get_latest_unblocked_monitor_update_id()
17419						);
17420						log_error!(logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
17421						log_error!(logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
17422						log_error!(logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");
17423						log_error!(logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
17424						return Err(DecodeError::DangerousValue);
17425					}
17426					if funded_chan.blocked_monitor_updates_pending() > 0 {
17427						pending_background_events.push(
17428							BackgroundEvent::AttemptUnblockMonitorUpdates {
17429								counterparty_node_id: *counterparty_id,
17430								channel_id: *chan_id,
17431							},
17432						);
17433					}
17434				} else {
17435					// We shouldn't have persisted (or read) any unfunded channel types so none should have been
17436					// created in this `channel_by_id` map.
17437					debug_assert!(false);
17438					return Err(DecodeError::InvalidValue);
17439				}
17440			}
17441		}
17442
17443		if let Some(in_flight_upds) = in_flight_monitor_updates {
17444			for ((counterparty_id, channel_id), mut chan_in_flight_updates) in in_flight_upds {
17445				let logger =
17446					WithContext::from(&args.logger, Some(counterparty_id), Some(channel_id), None);
17447				if let Some(monitor) = args.channel_monitors.get(&channel_id) {
17448					// Now that we've removed all the in-flight monitor updates for channels that are
17449					// still open, we need to replay any monitor updates that are for closed channels,
17450					// creating the neccessary peer_state entries as we go.
17451					let peer_state_mutex = per_peer_state
17452						.entry(counterparty_id)
17453						.or_insert_with(|| Mutex::new(empty_peer_state()));
17454					let mut peer_state = peer_state_mutex.lock().unwrap();
17455					handle_in_flight_updates!(
17456						counterparty_id,
17457						chan_in_flight_updates,
17458						monitor,
17459						peer_state,
17460						logger,
17461						"closed "
17462					);
17463				} else {
17464					log_error!(logger, "A ChannelMonitor is missing even though we have in-flight updates for it! This indicates a potentially-critical violation of the chain::Watch API!");
17465					log_error!(
17466						logger,
17467						" The ChannelMonitor for channel {} is missing.",
17468						channel_id
17469					);
17470					log_error!(logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
17471					log_error!(logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
17472					log_error!(logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");
17473					log_error!(logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
17474					log_error!(
17475						logger,
17476						" Pending in-flight updates are: {:?}",
17477						chan_in_flight_updates
17478					);
17479					return Err(DecodeError::InvalidValue);
17480				}
17481			}
17482		}
17483
17484		// The newly generated `close_background_events` have to be added after any updates that
17485		// were already in-flight on shutdown, so we append them here.
17486		pending_background_events.reserve(close_background_events.len());
17487		'each_bg_event: for mut new_event in close_background_events {
17488			if let BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
17489				counterparty_node_id,
17490				funding_txo,
17491				channel_id,
17492				update,
17493			} = &mut new_event
17494			{
17495				debug_assert_eq!(update.updates.len(), 1);
17496				debug_assert!(matches!(
17497					update.updates[0],
17498					ChannelMonitorUpdateStep::ChannelForceClosed { .. }
17499				));
17500				let mut updated_id = false;
17501				for pending_event in pending_background_events.iter() {
17502					if let BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
17503						counterparty_node_id: pending_cp,
17504						funding_txo: pending_funding,
17505						channel_id: pending_chan_id,
17506						update: pending_update,
17507					} = pending_event
17508					{
17509						let for_same_channel = counterparty_node_id == pending_cp
17510							&& funding_txo == pending_funding
17511							&& channel_id == pending_chan_id;
17512						if for_same_channel {
17513							debug_assert!(update.update_id >= pending_update.update_id);
17514							if pending_update.updates.iter().any(|upd| {
17515								matches!(upd, ChannelMonitorUpdateStep::ChannelForceClosed { .. })
17516							}) {
17517								// If the background event we're looking at is just
17518								// force-closing the channel which already has a pending
17519								// force-close update, no need to duplicate it.
17520								continue 'each_bg_event;
17521							}
17522							update.update_id = pending_update.update_id.saturating_add(1);
17523							updated_id = true;
17524						}
17525					}
17526				}
17527				let mut per_peer_state = per_peer_state
17528					.get(counterparty_node_id)
17529					.expect("If we have pending updates for a channel it must have an entry")
17530					.lock()
17531					.unwrap();
17532				if updated_id {
17533					per_peer_state
17534						.closed_channel_monitor_update_ids
17535						.entry(*channel_id)
17536						.and_modify(|v| *v = cmp::max(update.update_id, *v))
17537						.or_insert(update.update_id);
17538				}
17539				let in_flight_updates = &mut per_peer_state
17540					.in_flight_monitor_updates
17541					.entry(*channel_id)
17542					.or_insert_with(|| (*funding_txo, Vec::new()))
17543					.1;
17544				debug_assert!(!in_flight_updates.iter().any(|upd| upd == update));
17545				in_flight_updates.push(update.clone());
17546			}
17547			pending_background_events.push(new_event);
17548		}
17549
17550		// If there's any preimages for forwarded HTLCs hanging around in ChannelMonitors we
17551		// should ensure we try them again on the inbound edge. We put them here and do so after we
17552		// have a fully-constructed `ChannelManager` at the end.
17553		let mut pending_claims_to_replay = Vec::new();
17554
17555		{
17556			// If we're tracking pending payments, ensure we haven't lost any by looking at the
17557			// ChannelMonitor data for any channels for which we do not have authorative state
17558			// (i.e. those for which we just force-closed above or we otherwise don't have a
17559			// corresponding `Channel` at all).
17560			// This avoids several edge-cases where we would otherwise "forget" about pending
17561			// payments which are still in-flight via their on-chain state.
17562			// We only rebuild the pending payments map if we were most recently serialized by
17563			// 0.0.102+
17564			//
17565			// First we rebuild all pending payments, then separately re-claim and re-fail pending
17566			// payments. This avoids edge-cases around MPP payments resulting in redundant actions.
17567			for (channel_id, monitor) in args.channel_monitors.iter() {
17568				let mut is_channel_closed = true;
17569				let counterparty_node_id = monitor.get_counterparty_node_id();
17570				if let Some(peer_state_mtx) = per_peer_state.get(&counterparty_node_id) {
17571					let mut peer_state_lock = peer_state_mtx.lock().unwrap();
17572					let peer_state = &mut *peer_state_lock;
17573					is_channel_closed = !peer_state.channel_by_id.contains_key(channel_id);
17574				}
17575
17576				if is_channel_closed {
17577					for (htlc_source, (htlc, _)) in monitor.get_all_current_outbound_htlcs() {
17578						let logger = WithChannelMonitor::from(
17579							&args.logger,
17580							monitor,
17581							Some(htlc.payment_hash),
17582						);
17583						if let HTLCSource::OutboundRoute {
17584							payment_id, session_priv, path, ..
17585						} = htlc_source
17586						{
17587							if path.hops.is_empty() {
17588								log_error!(logger, "Got an empty path for a pending payment");
17589								return Err(DecodeError::InvalidValue);
17590							}
17591
17592							let mut session_priv_bytes = [0; 32];
17593							session_priv_bytes[..].copy_from_slice(&session_priv[..]);
17594							pending_outbounds.insert_from_monitor_on_startup(
17595								payment_id,
17596								htlc.payment_hash,
17597								session_priv_bytes,
17598								&path,
17599								best_block_height,
17600								&logger,
17601							);
17602						}
17603					}
17604				}
17605			}
17606			for (channel_id, monitor) in args.channel_monitors.iter() {
17607				let mut is_channel_closed = true;
17608				let counterparty_node_id = monitor.get_counterparty_node_id();
17609				if let Some(peer_state_mtx) = per_peer_state.get(&counterparty_node_id) {
17610					let mut peer_state_lock = peer_state_mtx.lock().unwrap();
17611					let peer_state = &mut *peer_state_lock;
17612					is_channel_closed = !peer_state.channel_by_id.contains_key(channel_id);
17613				}
17614
17615				if is_channel_closed {
17616					for (htlc_source, (htlc, preimage_opt)) in
17617						monitor.get_all_current_outbound_htlcs()
17618					{
17619						let logger = WithChannelMonitor::from(
17620							&args.logger,
17621							monitor,
17622							Some(htlc.payment_hash),
17623						);
17624						let htlc_id = SentHTLCId::from_source(&htlc_source);
17625						match htlc_source {
17626							HTLCSource::PreviousHopData(prev_hop_data) => {
17627								let pending_forward_matches_htlc = |info: &PendingAddHTLCInfo| {
17628									info.prev_funding_outpoint == prev_hop_data.outpoint
17629										&& info.prev_htlc_id == prev_hop_data.htlc_id
17630								};
17631								// The ChannelMonitor is now responsible for this HTLC's
17632								// failure/success and will let us know what its outcome is. If we
17633								// still have an entry for this HTLC in `forward_htlcs` or
17634								// `pending_intercepted_htlcs`, we were apparently not persisted after
17635								// the monitor was when forwarding the payment.
17636								decode_update_add_htlcs.retain(|src_outb_alias, update_add_htlcs| {
17637									update_add_htlcs.retain(|update_add_htlc| {
17638										let matches = *src_outb_alias == prev_hop_data.prev_outbound_scid_alias &&
17639											update_add_htlc.htlc_id == prev_hop_data.htlc_id;
17640										if matches {
17641											log_info!(logger, "Removing pending to-decode HTLC with hash {} as it was forwarded to the closed channel {}",
17642												&htlc.payment_hash, &monitor.channel_id());
17643										}
17644										!matches
17645									});
17646									!update_add_htlcs.is_empty()
17647								});
17648								forward_htlcs.retain(|_, forwards| {
17649									forwards.retain(|forward| {
17650										if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
17651											if pending_forward_matches_htlc(&htlc_info) {
17652												log_info!(logger, "Removing pending to-forward HTLC with hash {} as it was forwarded to the closed channel {}",
17653													&htlc.payment_hash, &monitor.channel_id());
17654												false
17655											} else { true }
17656										} else { true }
17657									});
17658									!forwards.is_empty()
17659								});
17660								pending_intercepted_htlcs.as_mut().unwrap().retain(|intercepted_id, htlc_info| {
17661									if pending_forward_matches_htlc(&htlc_info) {
17662										log_info!(logger, "Removing pending intercepted HTLC with hash {} as it was forwarded to the closed channel {}",
17663											&htlc.payment_hash, &monitor.channel_id());
17664										pending_events_read.retain(|(event, _)| {
17665											if let Event::HTLCIntercepted { intercept_id: ev_id, .. } = event {
17666												intercepted_id != ev_id
17667											} else { true }
17668										});
17669										false
17670									} else { true }
17671								});
17672							},
17673							HTLCSource::OutboundRoute {
17674								payment_id,
17675								session_priv,
17676								path,
17677								bolt12_invoice,
17678								..
17679							} => {
17680								if let Some(preimage) = preimage_opt {
17681									let pending_events = Mutex::new(pending_events_read);
17682									let update = PaymentCompleteUpdate {
17683										counterparty_node_id: monitor.get_counterparty_node_id(),
17684										channel_funding_outpoint: monitor.get_funding_txo(),
17685										channel_id: monitor.channel_id(),
17686										htlc_id,
17687									};
17688									let mut compl_action = Some(
17689										EventCompletionAction::ReleasePaymentCompleteChannelMonitorUpdate(update)
17690									);
17691									pending_outbounds.claim_htlc(
17692										payment_id,
17693										preimage,
17694										bolt12_invoice,
17695										session_priv,
17696										path,
17697										true,
17698										&mut compl_action,
17699										&pending_events,
17700										&logger,
17701									);
17702									// If the completion action was not consumed, then there was no
17703									// payment to claim, and we need to tell the `ChannelMonitor`
17704									// we don't need to hear about the HTLC again, at least as long
17705									// as the PaymentSent event isn't still sitting around in our
17706									// event queue.
17707									let have_action = if compl_action.is_some() {
17708										let pending_events = pending_events.lock().unwrap();
17709										pending_events.iter().any(|(_, act)| *act == compl_action)
17710									} else {
17711										false
17712									};
17713									if !have_action && compl_action.is_some() {
17714										let mut peer_state = per_peer_state
17715											.get(&counterparty_node_id)
17716											.map(|state| state.lock().unwrap())
17717											.expect("Channels originating a preimage must have peer state");
17718										let update_id = peer_state
17719											.closed_channel_monitor_update_ids
17720											.get_mut(channel_id)
17721											.expect("Channels originating a preimage must have a monitor");
17722										// Note that for channels closed pre-0.1, the latest
17723										// update_id is `u64::MAX`.
17724										*update_id = update_id.saturating_add(1);
17725
17726										pending_background_events.push(BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
17727											counterparty_node_id: monitor.get_counterparty_node_id(),
17728											funding_txo: monitor.get_funding_txo(),
17729											channel_id: monitor.channel_id(),
17730											update: ChannelMonitorUpdate {
17731												update_id: *update_id,
17732												channel_id: Some(monitor.channel_id()),
17733												updates: vec![ChannelMonitorUpdateStep::ReleasePaymentComplete {
17734													htlc: htlc_id,
17735												}],
17736											},
17737										});
17738									}
17739									pending_events_read = pending_events.into_inner().unwrap();
17740								}
17741							},
17742						}
17743					}
17744					for (htlc_source, payment_hash) in monitor.get_onchain_failed_outbound_htlcs() {
17745						let logger =
17746							WithChannelMonitor::from(&args.logger, monitor, Some(payment_hash));
17747						log_info!(
17748							logger,
17749							"Failing HTLC with payment hash {} as it was resolved on-chain.",
17750							payment_hash
17751						);
17752						let completion_action = Some(PaymentCompleteUpdate {
17753							counterparty_node_id: monitor.get_counterparty_node_id(),
17754							channel_funding_outpoint: monitor.get_funding_txo(),
17755							channel_id: monitor.channel_id(),
17756							htlc_id: SentHTLCId::from_source(&htlc_source),
17757						});
17758
17759						failed_htlcs.push((
17760							htlc_source,
17761							payment_hash,
17762							monitor.get_counterparty_node_id(),
17763							monitor.channel_id(),
17764							LocalHTLCFailureReason::OnChainTimeout,
17765							completion_action,
17766						));
17767					}
17768				}
17769
17770				// Whether the downstream channel was closed or not, try to re-apply any payment
17771				// preimages from it which may be needed in upstream channels for forwarded
17772				// payments.
17773				let mut fail_read = false;
17774				let outbound_claimed_htlcs_iter = monitor.get_all_current_outbound_htlcs()
17775					.into_iter()
17776					.filter_map(|(htlc_source, (htlc, preimage_opt))| {
17777						if let HTLCSource::PreviousHopData(prev_hop) = &htlc_source {
17778							if let Some(payment_preimage) = preimage_opt {
17779								let inbound_edge_monitor = args.channel_monitors.get(&prev_hop.channel_id);
17780								// Note that for channels which have gone to chain,
17781								// `get_all_current_outbound_htlcs` is never pruned and always returns
17782								// a constant set until the monitor is removed/archived. Thus, we
17783								// want to skip replaying claims that have definitely been resolved
17784								// on-chain.
17785
17786								// If the inbound monitor is not present, we assume it was fully
17787								// resolved and properly archived, implying this payment had plenty
17788								// of time to get claimed and we can safely skip any further
17789								// attempts to claim it (they wouldn't succeed anyway as we don't
17790								// have a monitor against which to do so).
17791								let inbound_edge_monitor = if let Some(monitor) = inbound_edge_monitor {
17792									monitor
17793								} else {
17794									return None;
17795								};
17796								// Second, if the inbound edge of the payment's monitor has been
17797								// fully claimed we've had at least `ANTI_REORG_DELAY` blocks to
17798								// get any PaymentForwarded event(s) to the user and assume that
17799								// there's no need to try to replay the claim just for that.
17800								let inbound_edge_balances = inbound_edge_monitor.get_claimable_balances();
17801								if inbound_edge_balances.is_empty() {
17802									return None;
17803								}
17804
17805								if prev_hop.counterparty_node_id.is_none() {
17806									// We no longer support claiming an HTLC where we don't have
17807									// the counterparty_node_id available if the claim has to go to
17808									// a closed channel. Its possible we can get away with it if
17809									// the channel is not yet closed, but its by no means a
17810									// guarantee.
17811
17812									// Thus, in this case we are a bit more aggressive with our
17813									// pruning - if we have no use for the claim (because the
17814									// inbound edge of the payment's monitor has already claimed
17815									// the HTLC) we skip trying to replay the claim.
17816									let htlc_payment_hash: PaymentHash = payment_preimage.into();
17817									let logger = WithChannelMonitor::from(
17818										&args.logger,
17819										monitor,
17820										Some(htlc_payment_hash),
17821									);
17822									let balance_could_incl_htlc = |bal| match bal {
17823										&Balance::ClaimableOnChannelClose { .. } => {
17824											// The channel is still open, assume we can still
17825											// claim against it
17826											true
17827										},
17828										&Balance::MaybePreimageClaimableHTLC { payment_hash, .. } => {
17829											payment_hash == htlc_payment_hash
17830										},
17831										_ => false,
17832									};
17833									let htlc_may_be_in_balances =
17834										inbound_edge_balances.iter().any(balance_could_incl_htlc);
17835									if !htlc_may_be_in_balances {
17836										return None;
17837									}
17838
17839									// First check if we're absolutely going to fail - if we need
17840									// to replay this claim to get the preimage into the inbound
17841									// edge monitor but the channel is closed (and thus we'll
17842									// immediately panic if we call claim_funds_from_hop).
17843									if short_to_chan_info.get(&prev_hop.prev_outbound_scid_alias).is_none() {
17844										log_error!(logger,
17845											"We need to replay the HTLC claim for payment_hash {} (preimage {}) but cannot do so as the HTLC was forwarded prior to LDK 0.0.124.\
17846											All HTLCs that were forwarded by LDK 0.0.123 and prior must be resolved prior to upgrading to LDK 0.1",
17847											htlc_payment_hash,
17848											payment_preimage,
17849										);
17850										fail_read = true;
17851									}
17852
17853									// At this point we're confident we need the claim, but the
17854									// inbound edge channel is still live. As long as this remains
17855									// the case, we can conceivably proceed, but we run some risk
17856									// of panicking at runtime. The user ideally should have read
17857									// the release notes and we wouldn't be here, but we go ahead
17858									// and let things run in the hope that it'll all just work out.
17859									log_error!(logger,
17860										"We need to replay the HTLC claim for payment_hash {} (preimage {}) but don't have all the required information to do so reliably.\
17861										As long as the channel for the inbound edge of the forward remains open, this may work okay, but we may panic at runtime!\
17862										All HTLCs that were forwarded by LDK 0.0.123 and prior must be resolved prior to upgrading to LDK 0.1\
17863										Continuing anyway, though panics may occur!",
17864										htlc_payment_hash,
17865										payment_preimage,
17866									);
17867								}
17868
17869								Some((htlc_source, payment_preimage, htlc.amount_msat,
17870									is_channel_closed, monitor.get_counterparty_node_id(),
17871									monitor.get_funding_txo(), monitor.channel_id()))
17872							} else { None }
17873						} else {
17874							// If it was an outbound payment, we've handled it above - if a preimage
17875							// came in and we persisted the `ChannelManager` we either handled it and
17876							// are good to go or the channel force-closed - we don't have to handle the
17877							// channel still live case here.
17878							None
17879						}
17880					});
17881				for tuple in outbound_claimed_htlcs_iter {
17882					pending_claims_to_replay.push(tuple);
17883				}
17884				if fail_read {
17885					return Err(DecodeError::InvalidValue);
17886				}
17887			}
17888		}
17889
17890		let expanded_inbound_key = args.node_signer.get_expanded_key();
17891
17892		let mut claimable_payments = hash_map_with_capacity(claimable_htlcs_list.len());
17893		if let Some(purposes) = claimable_htlc_purposes {
17894			if purposes.len() != claimable_htlcs_list.len() {
17895				return Err(DecodeError::InvalidValue);
17896			}
17897			if let Some(onion_fields) = claimable_htlc_onion_fields {
17898				if onion_fields.len() != claimable_htlcs_list.len() {
17899					return Err(DecodeError::InvalidValue);
17900				}
17901				for (purpose, (onion, (payment_hash, htlcs))) in purposes
17902					.into_iter()
17903					.zip(onion_fields.into_iter().zip(claimable_htlcs_list.into_iter()))
17904				{
17905					if htlcs.is_empty() {
17906						continue;
17907					}
17908					let claimable = ClaimablePayment { purpose, htlcs, onion_fields: onion };
17909					let existing_payment = claimable_payments.insert(payment_hash, claimable);
17910					if existing_payment.is_some() {
17911						return Err(DecodeError::InvalidValue);
17912					}
17913				}
17914			} else {
17915				for (purpose, (payment_hash, htlcs)) in
17916					purposes.into_iter().zip(claimable_htlcs_list.into_iter())
17917				{
17918					if htlcs.is_empty() {
17919						continue;
17920					}
17921					let claimable = ClaimablePayment { purpose, htlcs, onion_fields: None };
17922					let existing_payment = claimable_payments.insert(payment_hash, claimable);
17923					if existing_payment.is_some() {
17924						return Err(DecodeError::InvalidValue);
17925					}
17926				}
17927			}
17928		} else {
17929			// LDK versions prior to 0.0.107 did not write a `pending_htlc_purposes`, but do
17930			// include a `_legacy_hop_data` in the `OnionPayload`.
17931			for (payment_hash, htlcs) in claimable_htlcs_list.drain(..) {
17932				if htlcs.is_empty() {
17933					return Err(DecodeError::InvalidValue);
17934				}
17935				let purpose = match &htlcs[0].onion_payload {
17936					OnionPayload::Invoice { _legacy_hop_data } => {
17937						if let Some(hop_data) = _legacy_hop_data {
17938							events::PaymentPurpose::Bolt11InvoicePayment {
17939								payment_preimage: match inbound_payment::verify(
17940									payment_hash,
17941									&hop_data,
17942									0,
17943									&expanded_inbound_key,
17944									&args.logger,
17945								) {
17946									Ok((payment_preimage, _)) => payment_preimage,
17947									Err(()) => {
17948										log_error!(args.logger, "Failed to read claimable payment data for HTLC with payment hash {} - was not a pending inbound payment and didn't match our payment key", &payment_hash);
17949										return Err(DecodeError::InvalidValue);
17950									},
17951								},
17952								payment_secret: hop_data.payment_secret,
17953							}
17954						} else {
17955							return Err(DecodeError::InvalidValue);
17956						}
17957					},
17958					OnionPayload::Spontaneous(payment_preimage) => {
17959						events::PaymentPurpose::SpontaneousPayment(*payment_preimage)
17960					},
17961				};
17962				claimable_payments
17963					.insert(payment_hash, ClaimablePayment { purpose, htlcs, onion_fields: None });
17964			}
17965		}
17966
17967		// Similar to the above cases for forwarded payments, if we have any pending inbound HTLCs
17968		// which haven't yet been claimed, we may be missing counterparty_node_id info and would
17969		// panic if we attempted to claim them at this point.
17970		for (payment_hash, payment) in claimable_payments.iter() {
17971			for htlc in payment.htlcs.iter() {
17972				if htlc.prev_hop.counterparty_node_id.is_some() {
17973					continue;
17974				}
17975				if short_to_chan_info.get(&htlc.prev_hop.prev_outbound_scid_alias).is_some() {
17976					log_error!(args.logger,
17977						"We do not have the required information to claim a pending payment with payment hash {} reliably.\
17978						As long as the channel for the inbound edge of the forward remains open, this may work okay, but we may panic at runtime!\
17979						All HTLCs that were received by LDK 0.0.123 and prior must be resolved prior to upgrading to LDK 0.1\
17980						Continuing anyway, though panics may occur!",
17981						payment_hash,
17982					);
17983				} else {
17984					log_error!(args.logger,
17985						"We do not have the required information to claim a pending payment with payment hash {}.\
17986						All HTLCs that were received by LDK 0.0.123 and prior must be resolved prior to upgrading to LDK 0.1",
17987						payment_hash,
17988					);
17989					return Err(DecodeError::InvalidValue);
17990				}
17991			}
17992		}
17993
17994		let mut secp_ctx = Secp256k1::new();
17995		secp_ctx.seeded_randomize(&args.entropy_source.get_secure_random_bytes());
17996
17997		let our_network_pubkey = match args.node_signer.get_node_id(Recipient::Node) {
17998			Ok(key) => key,
17999			Err(()) => return Err(DecodeError::InvalidValue),
18000		};
18001		if let Some(network_pubkey) = received_network_pubkey {
18002			if network_pubkey != our_network_pubkey {
18003				log_error!(args.logger, "Key that was generated does not match the existing key.");
18004				return Err(DecodeError::InvalidValue);
18005			}
18006		}
18007
18008		let mut outbound_scid_aliases = new_hash_set();
18009		for (_peer_node_id, peer_state_mutex) in per_peer_state.iter_mut() {
18010			let mut peer_state_lock = peer_state_mutex.lock().unwrap();
18011			let peer_state = &mut *peer_state_lock;
18012			for (chan_id, chan) in peer_state.channel_by_id.iter_mut() {
18013				if let Some(funded_chan) = chan.as_funded_mut() {
18014					let logger = WithChannelContext::from(&args.logger, &funded_chan.context, None);
18015					if funded_chan.context.outbound_scid_alias() == 0 {
18016						let mut outbound_scid_alias;
18017						loop {
18018							outbound_scid_alias = fake_scid::Namespace::OutboundAlias
18019								.get_fake_scid(
18020									best_block_height,
18021									&chain_hash,
18022									fake_scid_rand_bytes.as_ref().unwrap(),
18023									&args.entropy_source,
18024								);
18025							if outbound_scid_aliases.insert(outbound_scid_alias) {
18026								break;
18027							}
18028						}
18029						funded_chan.context.set_outbound_scid_alias(outbound_scid_alias);
18030					} else if !outbound_scid_aliases
18031						.insert(funded_chan.context.outbound_scid_alias())
18032					{
18033						// Note that in rare cases its possible to hit this while reading an older
18034						// channel if we just happened to pick a colliding outbound alias above.
18035						log_error!(
18036							logger,
18037							"Got duplicate outbound SCID alias; {}",
18038							funded_chan.context.outbound_scid_alias()
18039						);
18040						return Err(DecodeError::InvalidValue);
18041					}
18042					if funded_chan.context.is_usable() {
18043						let alias = funded_chan.context.outbound_scid_alias();
18044						let cp_id = funded_chan.context.get_counterparty_node_id();
18045						if short_to_chan_info.insert(alias, (cp_id, *chan_id)).is_some() {
18046							// Note that in rare cases its possible to hit this while reading an older
18047							// channel if we just happened to pick a colliding outbound alias above.
18048							log_error!(
18049								logger,
18050								"Got duplicate outbound SCID alias; {}",
18051								funded_chan.context.outbound_scid_alias()
18052							);
18053							return Err(DecodeError::InvalidValue);
18054						}
18055					}
18056				} else {
18057					// We shouldn't have persisted (or read) any unfunded channel types so none should have been
18058					// created in this `channel_by_id` map.
18059					debug_assert!(false);
18060					return Err(DecodeError::InvalidValue);
18061				}
18062			}
18063		}
18064
18065		let bounded_fee_estimator = LowerBoundedFeeEstimator::new(args.fee_estimator);
18066
18067		for (node_id, monitor_update_blocked_actions) in
18068			monitor_update_blocked_actions_per_peer.unwrap()
18069		{
18070			if let Some(peer_state) = per_peer_state.get(&node_id) {
18071				for (channel_id, actions) in monitor_update_blocked_actions.iter() {
18072					let logger =
18073						WithContext::from(&args.logger, Some(node_id), Some(*channel_id), None);
18074					for action in actions.iter() {
18075						if let MonitorUpdateCompletionAction::EmitEventAndFreeOtherChannel {
18076							downstream_counterparty_and_funding_outpoint:
18077								Some(EventUnblockedChannel {
18078									counterparty_node_id: blocked_node_id,
18079									funding_txo: _,
18080									channel_id: blocked_channel_id,
18081									blocking_action,
18082								}),
18083							..
18084						} = action
18085						{
18086							if let Some(blocked_peer_state) = per_peer_state.get(blocked_node_id) {
18087								log_trace!(logger,
18088									"Holding the next revoke_and_ack from {} until the preimage is durably persisted in the inbound edge's ChannelMonitor",
18089									blocked_channel_id);
18090								blocked_peer_state
18091									.lock()
18092									.unwrap()
18093									.actions_blocking_raa_monitor_updates
18094									.entry(*blocked_channel_id)
18095									.or_insert_with(Vec::new)
18096									.push(blocking_action.clone());
18097							} else {
18098								// If the channel we were blocking has closed, we don't need to
18099								// worry about it - the blocked monitor update should never have
18100								// been released from the `Channel` object so it can't have
18101								// completed, and if the channel closed there's no reason to bother
18102								// anymore.
18103							}
18104						}
18105						if let MonitorUpdateCompletionAction::FreeOtherChannelImmediately {
18106							..
18107						} = action
18108						{
18109							debug_assert!(false, "Non-event-generating channel freeing should not appear in our queue");
18110						}
18111					}
18112					// Note that we may have a post-update action for a channel that has no pending
18113					// `ChannelMonitorUpdate`s, but unlike the no-peer-state case, it may simply be
18114					// because we had a `ChannelMonitorUpdate` complete after the last time this
18115					// `ChannelManager` was serialized. In that case, we'll run the post-update
18116					// actions as soon as we get going.
18117				}
18118				peer_state.lock().unwrap().monitor_update_blocked_actions =
18119					monitor_update_blocked_actions;
18120			} else {
18121				for actions in monitor_update_blocked_actions.values() {
18122					for action in actions.iter() {
18123						if matches!(action, MonitorUpdateCompletionAction::PaymentClaimed { .. }) {
18124							// If there are no state for this channel but we have pending
18125							// post-update actions, its possible that one was left over from pre-0.1
18126							// payment claims where MPP claims led to a channel blocked on itself
18127							// and later `ChannelMonitorUpdate`s didn't get their post-update
18128							// actions run.
18129							// This should only have happened for `PaymentClaimed` post-update actions,
18130							// which we ignore here.
18131						} else {
18132							let logger = WithContext::from(&args.logger, Some(node_id), None, None);
18133							log_error!(
18134								logger,
18135								"Got blocked actions {:?} without a per-peer-state for {}",
18136								monitor_update_blocked_actions,
18137								node_id
18138							);
18139							return Err(DecodeError::InvalidValue);
18140						}
18141					}
18142				}
18143			}
18144		}
18145
18146		let best_block = BestBlock::new(best_block_hash, best_block_height);
18147		let flow = OffersMessageFlow::new(
18148			chain_hash,
18149			best_block,
18150			our_network_pubkey,
18151			highest_seen_timestamp,
18152			expanded_inbound_key,
18153			args.node_signer.get_receive_auth_key(),
18154			secp_ctx.clone(),
18155			args.message_router,
18156			args.logger.clone(),
18157		)
18158		.with_async_payments_offers_cache(async_receive_offer_cache);
18159
18160		let channel_manager = ChannelManager {
18161			chain_hash,
18162			fee_estimator: bounded_fee_estimator,
18163			chain_monitor: args.chain_monitor,
18164			tx_broadcaster: args.tx_broadcaster,
18165			router: args.router,
18166			flow,
18167
18168			best_block: RwLock::new(best_block),
18169
18170			inbound_payment_key: expanded_inbound_key,
18171			pending_outbound_payments: pending_outbounds,
18172			pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()),
18173
18174			forward_htlcs: Mutex::new(forward_htlcs),
18175			decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs),
18176			claimable_payments: Mutex::new(ClaimablePayments {
18177				claimable_payments,
18178				pending_claiming_payments: pending_claiming_payments.unwrap(),
18179			}),
18180			outbound_scid_aliases: Mutex::new(outbound_scid_aliases),
18181			short_to_chan_info: FairRwLock::new(short_to_chan_info),
18182			fake_scid_rand_bytes: fake_scid_rand_bytes.unwrap(),
18183
18184			probing_cookie_secret: probing_cookie_secret.unwrap(),
18185			inbound_payment_id_secret: inbound_payment_id_secret.unwrap(),
18186
18187			our_network_pubkey,
18188			secp_ctx,
18189
18190			highest_seen_timestamp: AtomicUsize::new(highest_seen_timestamp as usize),
18191
18192			per_peer_state: FairRwLock::new(per_peer_state),
18193
18194			#[cfg(not(any(test, feature = "_externalize_tests")))]
18195			monitor_update_type: AtomicUsize::new(0),
18196
18197			pending_events: Mutex::new(pending_events_read),
18198			pending_events_processor: AtomicBool::new(false),
18199			pending_htlc_forwards_processor: AtomicBool::new(false),
18200			pending_background_events: Mutex::new(pending_background_events),
18201			total_consistency_lock: RwLock::new(()),
18202			background_events_processed_since_startup: AtomicBool::new(false),
18203
18204			event_persist_notifier: Notifier::new(),
18205			needs_persist_flag: AtomicBool::new(false),
18206
18207			funding_batch_states: Mutex::new(BTreeMap::new()),
18208
18209			pending_broadcast_messages: Mutex::new(Vec::new()),
18210
18211			entropy_source: args.entropy_source,
18212			node_signer: args.node_signer,
18213			signer_provider: args.signer_provider,
18214
18215			last_days_feerates: Mutex::new(VecDeque::new()),
18216
18217			logger: args.logger,
18218			config: RwLock::new(args.config),
18219
18220			#[cfg(feature = "_test_utils")]
18221			testing_dnssec_proof_offer_resolution_override: Mutex::new(new_hash_map()),
18222		};
18223
18224		let mut processed_claims: HashSet<Vec<MPPClaimHTLCSource>> = new_hash_set();
18225		for (_, monitor) in args.channel_monitors.iter() {
18226			for (payment_hash, (payment_preimage, payment_claims)) in monitor.get_stored_preimages()
18227			{
18228				if !payment_claims.is_empty() {
18229					for payment_claim in payment_claims {
18230						if processed_claims.contains(&payment_claim.mpp_parts) {
18231							// We might get the same payment a few times from different channels
18232							// that the MPP payment was received using. There's no point in trying
18233							// to claim the same payment again and again, so we check if the HTLCs
18234							// are the same and skip the payment here.
18235							continue;
18236						}
18237						if payment_claim.mpp_parts.is_empty() {
18238							return Err(DecodeError::InvalidValue);
18239						}
18240						{
18241							let payments = channel_manager.claimable_payments.lock().unwrap();
18242							if !payments.claimable_payments.contains_key(&payment_hash) {
18243								if let Some(payment) =
18244									payments.pending_claiming_payments.get(&payment_hash)
18245								{
18246									if payment.payment_id
18247										== payment_claim.claiming_payment.payment_id
18248									{
18249										// If this payment already exists and was marked as
18250										// being-claimed then the serialized state must contain all
18251										// of the pending `ChannelMonitorUpdate`s required to get
18252										// the preimage on disk in all MPP parts. Thus we can skip
18253										// the replay below.
18254										continue;
18255									}
18256								}
18257							}
18258						}
18259
18260						let mut channels_without_preimage = payment_claim
18261							.mpp_parts
18262							.iter()
18263							.map(|htlc_info| (htlc_info.counterparty_node_id, htlc_info.channel_id))
18264							.collect::<Vec<_>>();
18265						// If we have multiple MPP parts which were received over the same channel,
18266						// we only track it once as once we get a preimage durably in the
18267						// `ChannelMonitor` it will be used for all HTLCs with a matching hash.
18268						channels_without_preimage.sort_unstable();
18269						channels_without_preimage.dedup();
18270						let pending_claims = PendingMPPClaim {
18271							channels_without_preimage,
18272							channels_with_preimage: Vec::new(),
18273						};
18274						let pending_claim_ptr_opt = Some(Arc::new(Mutex::new(pending_claims)));
18275
18276						// While it may be duplicative to generate a PaymentClaimed here, trying to
18277						// figure out if the user definitely saw it before shutdown would require some
18278						// nontrivial logic and may break as we move away from regularly persisting
18279						// ChannelManager. Instead, we rely on the users' event handler being
18280						// idempotent and just blindly generate one no matter what, letting the
18281						// preimages eventually timing out from ChannelMonitors to prevent us from
18282						// doing so forever.
18283
18284						let claim_found = channel_manager
18285							.claimable_payments
18286							.lock()
18287							.unwrap()
18288							.begin_claiming_payment(
18289								payment_hash,
18290								&channel_manager.node_signer,
18291								&channel_manager.logger,
18292								&channel_manager.inbound_payment_id_secret,
18293								true,
18294							);
18295						if claim_found.is_err() {
18296							let mut claimable_payments =
18297								channel_manager.claimable_payments.lock().unwrap();
18298							match claimable_payments.pending_claiming_payments.entry(payment_hash) {
18299								hash_map::Entry::Occupied(_) => {
18300									debug_assert!(
18301										false,
18302										"Entry was added in begin_claiming_payment"
18303									);
18304									return Err(DecodeError::InvalidValue);
18305								},
18306								hash_map::Entry::Vacant(entry) => {
18307									entry.insert(payment_claim.claiming_payment);
18308								},
18309							}
18310						}
18311
18312						for part in payment_claim.mpp_parts.iter() {
18313							let pending_mpp_claim = pending_claim_ptr_opt.as_ref().map(|ptr| {
18314								(
18315									part.counterparty_node_id,
18316									part.channel_id,
18317									PendingMPPClaimPointer(Arc::clone(&ptr)),
18318								)
18319							});
18320							let pending_claim_ptr = pending_claim_ptr_opt.as_ref().map(|ptr| {
18321								RAAMonitorUpdateBlockingAction::ClaimedMPPPayment {
18322									pending_claim: PendingMPPClaimPointer(Arc::clone(&ptr)),
18323								}
18324							});
18325							// Note that we don't need to pass the `payment_info` here - its
18326							// already (clearly) durably on disk in the `ChannelMonitor` so there's
18327							// no need to worry about getting it into others.
18328							//
18329							// We don't encode any attribution data, because the required onion shared secret isn't
18330							// available here.
18331							channel_manager.claim_mpp_part(
18332								part.into(),
18333								payment_preimage,
18334								None,
18335								None,
18336								|_, _| {
18337									(
18338										Some(MonitorUpdateCompletionAction::PaymentClaimed {
18339											payment_hash,
18340											pending_mpp_claim,
18341										}),
18342										pending_claim_ptr,
18343									)
18344								},
18345							);
18346						}
18347						processed_claims.insert(payment_claim.mpp_parts);
18348					}
18349				} else {
18350					let per_peer_state = channel_manager.per_peer_state.read().unwrap();
18351					let mut claimable_payments = channel_manager.claimable_payments.lock().unwrap();
18352					let payment = claimable_payments.claimable_payments.remove(&payment_hash);
18353					mem::drop(claimable_payments);
18354					if let Some(payment) = payment {
18355						log_info!(channel_manager.logger, "Re-claiming HTLCs with payment hash {} as we've released the preimage to a ChannelMonitor!", &payment_hash);
18356						let mut claimable_amt_msat = 0;
18357						let mut receiver_node_id = Some(our_network_pubkey);
18358						let phantom_shared_secret = payment.htlcs[0].prev_hop.phantom_shared_secret;
18359						if phantom_shared_secret.is_some() {
18360							let phantom_pubkey = channel_manager
18361								.node_signer
18362								.get_node_id(Recipient::PhantomNode)
18363								.expect("Failed to get node_id for phantom node recipient");
18364							receiver_node_id = Some(phantom_pubkey)
18365						}
18366						for claimable_htlc in &payment.htlcs {
18367							claimable_amt_msat += claimable_htlc.value;
18368
18369							// Add a holding-cell claim of the payment to the Channel, which should be
18370							// applied ~immediately on peer reconnection. Because it won't generate a
18371							// new commitment transaction we can just provide the payment preimage to
18372							// the corresponding ChannelMonitor and nothing else.
18373							//
18374							// We do so directly instead of via the normal ChannelMonitor update
18375							// procedure as the ChainMonitor hasn't yet been initialized, implying
18376							// we're not allowed to call it directly yet. Further, we do the update
18377							// without incrementing the ChannelMonitor update ID as there isn't any
18378							// reason to.
18379							// If we were to generate a new ChannelMonitor update ID here and then
18380							// crash before the user finishes block connect we'd end up force-closing
18381							// this channel as well. On the flip side, there's no harm in restarting
18382							// without the new monitor persisted - we'll end up right back here on
18383							// restart.
18384							let previous_channel_id = claimable_htlc.prev_hop.channel_id;
18385							let peer_node_id = monitor.get_counterparty_node_id();
18386							{
18387								let peer_state_mutex = per_peer_state.get(&peer_node_id).unwrap();
18388								let mut peer_state_lock = peer_state_mutex.lock().unwrap();
18389								let peer_state = &mut *peer_state_lock;
18390								if let Some(channel) = peer_state
18391									.channel_by_id
18392									.get_mut(&previous_channel_id)
18393									.and_then(Channel::as_funded_mut)
18394								{
18395									let logger = WithChannelContext::from(
18396										&channel_manager.logger,
18397										&channel.context,
18398										Some(payment_hash),
18399									);
18400									channel
18401										.claim_htlc_while_disconnected_dropping_mon_update_legacy(
18402											claimable_htlc.prev_hop.htlc_id,
18403											payment_preimage,
18404											&&logger,
18405										);
18406								}
18407							}
18408							if let Some(previous_hop_monitor) =
18409								args.channel_monitors.get(&claimable_htlc.prev_hop.channel_id)
18410							{
18411								// Note that this is unsafe as we no longer require the
18412								// `ChannelMonitor`s to be re-persisted prior to this
18413								// `ChannelManager` being persisted after we get started running.
18414								// If this `ChannelManager` gets persisted first then we crash, we
18415								// won't have the `claimable_payments` entry we need to re-enter
18416								// this code block, causing us to not re-apply the preimage to this
18417								// `ChannelMonitor`.
18418								//
18419								// We should never be here with modern payment claims, however, as
18420								// they should always include the HTLC list. Instead, this is only
18421								// for nodes during upgrade, and we explicitly require the old
18422								// persistence semantics on upgrade in the release notes.
18423								previous_hop_monitor.provide_payment_preimage_unsafe_legacy(
18424									&payment_hash,
18425									&payment_preimage,
18426									&channel_manager.tx_broadcaster,
18427									&channel_manager.fee_estimator,
18428									&channel_manager.logger,
18429								);
18430							}
18431						}
18432						let mut pending_events = channel_manager.pending_events.lock().unwrap();
18433						let payment_id =
18434							payment.inbound_payment_id(&inbound_payment_id_secret.unwrap());
18435						let htlcs = payment.htlcs.iter().map(events::ClaimedHTLC::from).collect();
18436						let sender_intended_total_msat =
18437							payment.htlcs.first().map(|htlc| htlc.total_msat);
18438						pending_events.push_back((
18439							events::Event::PaymentClaimed {
18440								receiver_node_id,
18441								payment_hash,
18442								purpose: payment.purpose,
18443								amount_msat: claimable_amt_msat,
18444								htlcs,
18445								sender_intended_total_msat,
18446								onion_fields: payment.onion_fields,
18447								payment_id: Some(payment_id),
18448							},
18449							// Note that we don't bother adding a EventCompletionAction here to
18450							// ensure the `PaymentClaimed` event is durable processed as this
18451							// should only be hit for particularly old channels and we don't have
18452							// enough information to generate such an action.
18453							None,
18454						));
18455					}
18456				}
18457			}
18458		}
18459
18460		for htlc_source in failed_htlcs {
18461			let (source, hash, counterparty_id, channel_id, failure_reason, ev_action) =
18462				htlc_source;
18463			let receiver =
18464				HTLCHandlingFailureType::Forward { node_id: Some(counterparty_id), channel_id };
18465			let reason = HTLCFailReason::from_failure_code(failure_reason);
18466			channel_manager
18467				.fail_htlc_backwards_internal(&source, &hash, &reason, receiver, ev_action);
18468		}
18469
18470		for (
18471			source,
18472			preimage,
18473			downstream_value,
18474			downstream_closed,
18475			downstream_node_id,
18476			downstream_funding,
18477			downstream_channel_id,
18478		) in pending_claims_to_replay
18479		{
18480			// We use `downstream_closed` in place of `from_onchain` here just as a guess - we
18481			// don't remember in the `ChannelMonitor` where we got a preimage from, but if the
18482			// channel is closed we just assume that it probably came from an on-chain claim.
18483			// The same holds for attribution data. We don't have any, so we pass an empty one.
18484			channel_manager.claim_funds_internal(
18485				source,
18486				preimage,
18487				Some(downstream_value),
18488				None,
18489				downstream_closed,
18490				downstream_node_id,
18491				downstream_funding,
18492				downstream_channel_id,
18493				None,
18494				None,
18495				None,
18496			);
18497		}
18498
18499		//TODO: Broadcast channel update for closed channels, but only after we've made a
18500		//connection or two.
18501
18502		Ok((best_block_hash.clone(), channel_manager))
18503	}
18504}
18505
18506#[cfg(test)]
18507mod tests {
18508	use crate::events::{ClosureReason, Event, HTLCHandlingFailureType};
18509	use crate::ln::channelmanager::{
18510		create_recv_pending_htlc_info, inbound_payment, HTLCForwardInfo, InterceptId, PaymentId,
18511		RecipientOnionFields,
18512	};
18513	use crate::ln::functional_test_utils::*;
18514	use crate::ln::msgs::{self, BaseMessageHandler, ChannelMessageHandler, MessageSendEvent};
18515	use crate::ln::onion_utils::AttributionData;
18516	use crate::ln::onion_utils::{self, LocalHTLCFailureReason};
18517	use crate::ln::outbound_payment::Retry;
18518	use crate::ln::types::ChannelId;
18519	use crate::prelude::*;
18520	use crate::routing::router::{find_route, PaymentParameters, RouteParameters};
18521	use crate::sign::EntropySource;
18522	use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
18523	use crate::util::config::{ChannelConfig, ChannelConfigUpdate};
18524	use crate::util::errors::APIError;
18525	use crate::util::ser::Writeable;
18526	use crate::util::test_utils;
18527	use bitcoin::secp256k1::ecdh::SharedSecret;
18528	use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
18529	use core::sync::atomic::Ordering;
18530
18531	#[test]
18532	#[rustfmt::skip]
18533	fn test_notify_limits() {
18534		// Check that a few cases which don't require the persistence of a new ChannelManager,
18535		// indeed, do not cause the persistence of a new ChannelManager.
18536		let chanmon_cfgs = create_chanmon_cfgs(3);
18537		let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
18538		let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
18539		let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
18540
18541		// All nodes start with a persistable update pending as `create_network` connects each node
18542		// with all other nodes to make most tests simpler.
18543		assert!(nodes[0].node.get_event_or_persistence_needed_future().poll_is_complete());
18544		assert!(nodes[1].node.get_event_or_persistence_needed_future().poll_is_complete());
18545		assert!(nodes[2].node.get_event_or_persistence_needed_future().poll_is_complete());
18546
18547		let mut chan = create_announced_chan_between_nodes(&nodes, 0, 1);
18548
18549		// We check that the channel info nodes have doesn't change too early, even though we try
18550		// to connect messages with new values
18551		chan.0.contents.fee_base_msat *= 2;
18552		chan.1.contents.fee_base_msat *= 2;
18553		let node_a_chan_info = nodes[0].node.list_channels_with_counterparty(
18554			&nodes[1].node.get_our_node_id()).pop().unwrap();
18555		let node_b_chan_info = nodes[1].node.list_channels_with_counterparty(
18556			&nodes[0].node.get_our_node_id()).pop().unwrap();
18557
18558		// The first two nodes (which opened a channel) should now require fresh persistence
18559		assert!(nodes[0].node.get_event_or_persistence_needed_future().poll_is_complete());
18560		assert!(nodes[1].node.get_event_or_persistence_needed_future().poll_is_complete());
18561		// ... but the last node should not.
18562		assert!(!nodes[2].node.get_event_or_persistence_needed_future().poll_is_complete());
18563		// After persisting the first two nodes they should no longer need fresh persistence.
18564		assert!(!nodes[0].node.get_event_or_persistence_needed_future().poll_is_complete());
18565		assert!(!nodes[1].node.get_event_or_persistence_needed_future().poll_is_complete());
18566
18567		// Node 3, unrelated to the only channel, shouldn't care if it receives a channel_update
18568		// about the channel.
18569		nodes[2].node.handle_channel_update(nodes[1].node.get_our_node_id(), &chan.0);
18570		nodes[2].node.handle_channel_update(nodes[1].node.get_our_node_id(), &chan.1);
18571		assert!(!nodes[2].node.get_event_or_persistence_needed_future().poll_is_complete());
18572
18573		// The nodes which are a party to the channel should also ignore messages from unrelated
18574		// parties.
18575		nodes[0].node.handle_channel_update(nodes[2].node.get_our_node_id(), &chan.0);
18576		nodes[0].node.handle_channel_update(nodes[2].node.get_our_node_id(), &chan.1);
18577		nodes[1].node.handle_channel_update(nodes[2].node.get_our_node_id(), &chan.0);
18578		nodes[1].node.handle_channel_update(nodes[2].node.get_our_node_id(), &chan.1);
18579		assert!(!nodes[0].node.get_event_or_persistence_needed_future().poll_is_complete());
18580		assert!(!nodes[1].node.get_event_or_persistence_needed_future().poll_is_complete());
18581
18582		// At this point the channel info given by peers should still be the same.
18583		assert_eq!(nodes[0].node.list_channels()[0], node_a_chan_info);
18584		assert_eq!(nodes[1].node.list_channels()[0], node_b_chan_info);
18585
18586		// An earlier version of handle_channel_update didn't check the directionality of the
18587		// update message and would always update the local fee info, even if our peer was
18588		// (spuriously) forwarding us our own channel_update.
18589		let as_node_one = nodes[0].node.get_our_node_id().serialize()[..] < nodes[1].node.get_our_node_id().serialize()[..];
18590		let as_update = if as_node_one == (chan.0.contents.channel_flags & 1 == 0 /* chan.0 is from node one */) { &chan.0 } else { &chan.1 };
18591		let bs_update = if as_node_one == (chan.0.contents.channel_flags & 1 == 0 /* chan.0 is from node one */) { &chan.1 } else { &chan.0 };
18592
18593		// First deliver each peers' own message, checking that the node doesn't need to be
18594		// persisted and that its channel info remains the same.
18595		nodes[0].node.handle_channel_update(nodes[1].node.get_our_node_id(), &as_update);
18596		nodes[1].node.handle_channel_update(nodes[0].node.get_our_node_id(), &bs_update);
18597		assert!(!nodes[0].node.get_event_or_persistence_needed_future().poll_is_complete());
18598		assert!(!nodes[1].node.get_event_or_persistence_needed_future().poll_is_complete());
18599		assert_eq!(nodes[0].node.list_channels()[0], node_a_chan_info);
18600		assert_eq!(nodes[1].node.list_channels()[0], node_b_chan_info);
18601
18602		// Finally, deliver the other peers' message, ensuring each node needs to be persisted and
18603		// the channel info has updated.
18604		nodes[0].node.handle_channel_update(nodes[1].node.get_our_node_id(), &bs_update);
18605		nodes[1].node.handle_channel_update(nodes[0].node.get_our_node_id(), &as_update);
18606		assert!(nodes[0].node.get_event_or_persistence_needed_future().poll_is_complete());
18607		assert!(nodes[1].node.get_event_or_persistence_needed_future().poll_is_complete());
18608		assert_ne!(nodes[0].node.list_channels()[0], node_a_chan_info);
18609		assert_ne!(nodes[1].node.list_channels()[0], node_b_chan_info);
18610	}
18611
18612	#[test]
18613	#[rustfmt::skip]
18614	fn test_keysend_dup_hash_partial_mpp() {
18615		// Test that a keysend payment with a duplicate hash to an existing partial MPP payment fails as
18616		// expected.
18617		let chanmon_cfgs = create_chanmon_cfgs(2);
18618		let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
18619		let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
18620		let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
18621		create_announced_chan_between_nodes(&nodes, 0, 1);
18622
18623		// First, send a partial MPP payment.
18624		let (route, our_payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], 100_000);
18625		let mut mpp_route = route.clone();
18626		mpp_route.paths.push(mpp_route.paths[0].clone());
18627
18628		let payment_id = PaymentId([42; 32]);
18629		// Use the utility function send_payment_along_path to send the payment with MPP data which
18630		// indicates there are more HTLCs coming.
18631		let cur_height = CHAN_CONFIRM_DEPTH + 1; // route_payment calls send_payment, which adds 1 to the current height. So we do the same here to match.
18632		let session_privs = nodes[0].node.test_add_new_pending_payment(our_payment_hash,
18633			RecipientOnionFields::secret_only(payment_secret), payment_id, &mpp_route).unwrap();
18634		nodes[0].node.test_send_payment_along_path(&mpp_route.paths[0], &our_payment_hash,
18635			RecipientOnionFields::secret_only(payment_secret), 200_000, cur_height, payment_id, &None, session_privs[0]).unwrap();
18636		check_added_monitors!(nodes[0], 1);
18637		let mut events = nodes[0].node.get_and_clear_pending_msg_events();
18638		assert_eq!(events.len(), 1);
18639		pass_along_path(&nodes[0], &[&nodes[1]], 200_000, our_payment_hash, Some(payment_secret), events.drain(..).next().unwrap(), false, None);
18640
18641		// Next, send a keysend payment with the same payment_hash and make sure it fails.
18642		nodes[0].node.send_spontaneous_payment(
18643			Some(payment_preimage), RecipientOnionFields::spontaneous_empty(),
18644			PaymentId(payment_preimage.0), route.route_params.clone().unwrap(), Retry::Attempts(0)
18645		).unwrap();
18646		check_added_monitors!(nodes[0], 1);
18647		let mut events = nodes[0].node.get_and_clear_pending_msg_events();
18648		assert_eq!(events.len(), 1);
18649		let ev = events.drain(..).next().unwrap();
18650		let payment_event = SendEvent::from_event(ev);
18651		nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
18652		check_added_monitors!(nodes[1], 0);
18653		commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
18654		expect_and_process_pending_htlcs(&nodes[1], true);
18655		let events = nodes[1].node.get_and_clear_pending_events();
18656		let fail = HTLCHandlingFailureType::Receive { payment_hash: our_payment_hash };
18657		expect_htlc_failure_conditions(events, &[fail]);
18658		check_added_monitors!(nodes[1], 1);
18659		let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
18660		assert!(updates.update_add_htlcs.is_empty());
18661		assert!(updates.update_fulfill_htlcs.is_empty());
18662		assert_eq!(updates.update_fail_htlcs.len(), 1);
18663		assert!(updates.update_fail_malformed_htlcs.is_empty());
18664		assert!(updates.update_fee.is_none());
18665		nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
18666		commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
18667		expect_payment_failed!(nodes[0], our_payment_hash, true);
18668
18669		// Send the second half of the original MPP payment.
18670		nodes[0].node.test_send_payment_along_path(&mpp_route.paths[1], &our_payment_hash,
18671			RecipientOnionFields::secret_only(payment_secret), 200_000, cur_height, payment_id, &None, session_privs[1]).unwrap();
18672		check_added_monitors!(nodes[0], 1);
18673		let mut events = nodes[0].node.get_and_clear_pending_msg_events();
18674		assert_eq!(events.len(), 1);
18675		pass_along_path(&nodes[0], &[&nodes[1]], 200_000, our_payment_hash, Some(payment_secret), events.drain(..).next().unwrap(), true, None);
18676
18677		// Claim the full MPP payment. Note that we can't use a test utility like
18678		// claim_funds_along_route because the ordering of the messages causes the second half of the
18679		// payment to be put in the holding cell, which confuses the test utilities. So we exchange the
18680		// lightning messages manually.
18681		nodes[1].node.claim_funds(payment_preimage);
18682		expect_payment_claimed!(nodes[1], our_payment_hash, 200_000);
18683		check_added_monitors!(nodes[1], 2);
18684
18685		let mut bs_1st_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
18686		nodes[0].node.handle_update_fulfill_htlc(nodes[1].node.get_our_node_id(), bs_1st_updates.update_fulfill_htlcs.remove(0));
18687		expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
18688		nodes[0].node.handle_commitment_signed_batch_test(nodes[1].node.get_our_node_id(), &bs_1st_updates.commitment_signed);
18689		check_added_monitors!(nodes[0], 1);
18690		let (as_first_raa, as_first_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
18691		nodes[1].node.handle_revoke_and_ack(nodes[0].node.get_our_node_id(), &as_first_raa);
18692		check_added_monitors!(nodes[1], 1);
18693		let mut bs_2nd_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
18694		nodes[1].node.handle_commitment_signed_batch_test(nodes[0].node.get_our_node_id(), &as_first_cs);
18695		check_added_monitors!(nodes[1], 1);
18696		let bs_first_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
18697		nodes[0].node.handle_update_fulfill_htlc(nodes[1].node.get_our_node_id(), bs_2nd_updates.update_fulfill_htlcs.remove(0));
18698		nodes[0].node.handle_commitment_signed_batch_test(nodes[1].node.get_our_node_id(), &bs_2nd_updates.commitment_signed);
18699		check_added_monitors!(nodes[0], 1);
18700		let as_second_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
18701		nodes[0].node.handle_revoke_and_ack(nodes[1].node.get_our_node_id(), &bs_first_raa);
18702		let as_second_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
18703		check_added_monitors!(nodes[0], 1);
18704		nodes[1].node.handle_revoke_and_ack(nodes[0].node.get_our_node_id(), &as_second_raa);
18705		check_added_monitors!(nodes[1], 1);
18706		nodes[1].node.handle_commitment_signed_batch_test(nodes[0].node.get_our_node_id(), &as_second_updates.commitment_signed);
18707		check_added_monitors!(nodes[1], 1);
18708		let bs_third_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
18709		nodes[0].node.handle_revoke_and_ack(nodes[1].node.get_our_node_id(), &bs_third_raa);
18710		check_added_monitors!(nodes[0], 1);
18711
18712		// Note that successful MPP payments will generate a single PaymentSent event upon the first
18713		// path's success and a PaymentPathSuccessful event for each path's success.
18714		let events = nodes[0].node.get_and_clear_pending_events();
18715		assert_eq!(events.len(), 2);
18716		match events[0] {
18717			Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path, .. } => {
18718				assert_eq!(payment_id, *actual_payment_id);
18719				assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
18720				assert_eq!(route.paths[0], *path);
18721			},
18722			_ => panic!("Unexpected event"),
18723		}
18724		match events[1] {
18725			Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path, ..} => {
18726				assert_eq!(payment_id, *actual_payment_id);
18727				assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
18728				assert_eq!(route.paths[0], *path);
18729			},
18730			_ => panic!("Unexpected event"),
18731		}
18732	}
18733
18734	#[test]
18735	#[rustfmt::skip]
18736	fn test_keysend_dup_payment_hash() {
18737		// (1): Test that a keysend payment with a duplicate payment hash to an existing pending
18738		//      outbound regular payment fails as expected.
18739		// (2): Test that a regular payment with a duplicate payment hash to an existing keysend payment
18740		//      fails as expected.
18741		// (3): Test that a keysend payment with a duplicate payment hash to an existing keysend
18742		//      payment fails as expected. We only accept MPP keysends with payment secrets and reject
18743		//      otherwise.
18744		let chanmon_cfgs = create_chanmon_cfgs(2);
18745		let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
18746		let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
18747		let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
18748		create_announced_chan_between_nodes(&nodes, 0, 1);
18749		let scorer = test_utils::TestScorer::new();
18750		let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
18751
18752		// To start (1), send a regular payment but don't claim it.
18753		let expected_route = [&nodes[1]];
18754		let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &expected_route, 100_000);
18755
18756		// Next, attempt a keysend payment and make sure it fails.
18757		let route_params = RouteParameters::from_payment_params_and_value(
18758			PaymentParameters::for_keysend(expected_route.last().unwrap().node.get_our_node_id(),
18759			TEST_FINAL_CLTV, false), 100_000);
18760		nodes[0].node.send_spontaneous_payment(
18761			Some(payment_preimage), RecipientOnionFields::spontaneous_empty(),
18762			PaymentId(payment_preimage.0), route_params.clone(), Retry::Attempts(0)
18763		).unwrap();
18764		check_added_monitors!(nodes[0], 1);
18765		let mut events = nodes[0].node.get_and_clear_pending_msg_events();
18766		assert_eq!(events.len(), 1);
18767		let ev = events.drain(..).next().unwrap();
18768		let payment_event = SendEvent::from_event(ev);
18769		nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
18770		check_added_monitors!(nodes[1], 0);
18771		commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
18772		// We have to forward pending HTLCs twice - once tries to forward the payment forward (and
18773		// fails), the second will process the resulting failure and fail the HTLC backward
18774		expect_and_process_pending_htlcs(&nodes[1], true);
18775		let events = nodes[1].node.get_and_clear_pending_events();
18776		let fail = HTLCHandlingFailureType::Receive { payment_hash };
18777		expect_htlc_failure_conditions(events, &[fail]);
18778		check_added_monitors!(nodes[1], 1);
18779		let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
18780		assert!(updates.update_add_htlcs.is_empty());
18781		assert!(updates.update_fulfill_htlcs.is_empty());
18782		assert_eq!(updates.update_fail_htlcs.len(), 1);
18783		assert!(updates.update_fail_malformed_htlcs.is_empty());
18784		assert!(updates.update_fee.is_none());
18785		nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
18786		commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
18787		expect_payment_failed!(nodes[0], payment_hash, true);
18788
18789		// Finally, claim the original payment.
18790		claim_payment(&nodes[0], &expected_route, payment_preimage);
18791
18792		// To start (2), send a keysend payment but don't claim it.
18793		let payment_preimage = PaymentPreimage([42; 32]);
18794		let route = find_route(
18795			&nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph,
18796			None, nodes[0].logger, &scorer, &Default::default(), &random_seed_bytes
18797		).unwrap();
18798		let payment_hash = nodes[0].node.send_spontaneous_payment(
18799			Some(payment_preimage), RecipientOnionFields::spontaneous_empty(),
18800			PaymentId(payment_preimage.0), route.route_params.clone().unwrap(), Retry::Attempts(0)
18801		).unwrap();
18802		check_added_monitors!(nodes[0], 1);
18803		let mut events = nodes[0].node.get_and_clear_pending_msg_events();
18804		assert_eq!(events.len(), 1);
18805		let event = events.pop().unwrap();
18806		let path = vec![&nodes[1]];
18807		pass_along_path(&nodes[0], &path, 100_000, payment_hash, None, event, true, Some(payment_preimage));
18808
18809		// Next, attempt a regular payment and make sure it fails.
18810		let payment_secret = PaymentSecret([43; 32]);
18811		nodes[0].node.send_payment_with_route(route.clone(), payment_hash,
18812			RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
18813		check_added_monitors!(nodes[0], 1);
18814		let mut events = nodes[0].node.get_and_clear_pending_msg_events();
18815		assert_eq!(events.len(), 1);
18816		let ev = events.drain(..).next().unwrap();
18817		let payment_event = SendEvent::from_event(ev);
18818		nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
18819		check_added_monitors!(nodes[1], 0);
18820		commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
18821		expect_and_process_pending_htlcs(&nodes[1], true);
18822		let events = nodes[1].node.get_and_clear_pending_events();
18823		let fail = HTLCHandlingFailureType::Receive { payment_hash };
18824		expect_htlc_failure_conditions(events, &[fail]);
18825		check_added_monitors!(nodes[1], 1);
18826		let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
18827		assert!(updates.update_add_htlcs.is_empty());
18828		assert!(updates.update_fulfill_htlcs.is_empty());
18829		assert_eq!(updates.update_fail_htlcs.len(), 1);
18830		assert!(updates.update_fail_malformed_htlcs.is_empty());
18831		assert!(updates.update_fee.is_none());
18832		nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
18833		commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
18834		expect_payment_failed!(nodes[0], payment_hash, true);
18835
18836		// Finally, succeed the keysend payment.
18837		claim_payment(&nodes[0], &expected_route, payment_preimage);
18838
18839		// To start (3), send a keysend payment but don't claim it.
18840		let payment_id_1 = PaymentId([44; 32]);
18841		let payment_hash = nodes[0].node.send_spontaneous_payment(
18842			Some(payment_preimage), RecipientOnionFields::spontaneous_empty(), payment_id_1,
18843			route.route_params.clone().unwrap(), Retry::Attempts(0)
18844		).unwrap();
18845		check_added_monitors!(nodes[0], 1);
18846		let mut events = nodes[0].node.get_and_clear_pending_msg_events();
18847		assert_eq!(events.len(), 1);
18848		let event = events.pop().unwrap();
18849		let path = vec![&nodes[1]];
18850		pass_along_path(&nodes[0], &path, 100_000, payment_hash, None, event, true, Some(payment_preimage));
18851
18852		// Next, attempt a keysend payment and make sure it fails.
18853		let route_params = RouteParameters::from_payment_params_and_value(
18854			PaymentParameters::for_keysend(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV, false),
18855			100_000
18856		);
18857		let payment_id_2 = PaymentId([45; 32]);
18858		nodes[0].node.send_spontaneous_payment(
18859			Some(payment_preimage), RecipientOnionFields::spontaneous_empty(), payment_id_2, route_params,
18860			Retry::Attempts(0)
18861		).unwrap();
18862		check_added_monitors!(nodes[0], 1);
18863		let mut events = nodes[0].node.get_and_clear_pending_msg_events();
18864		assert_eq!(events.len(), 1);
18865		let ev = events.drain(..).next().unwrap();
18866		let payment_event = SendEvent::from_event(ev);
18867		nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
18868		check_added_monitors!(nodes[1], 0);
18869		commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
18870		expect_and_process_pending_htlcs(&nodes[1], true);
18871		let events = nodes[1].node.get_and_clear_pending_events();
18872		let fail = HTLCHandlingFailureType::Receive { payment_hash };
18873		expect_htlc_failure_conditions(events, &[fail]);
18874		check_added_monitors!(nodes[1], 1);
18875		let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
18876		assert!(updates.update_add_htlcs.is_empty());
18877		assert!(updates.update_fulfill_htlcs.is_empty());
18878		assert_eq!(updates.update_fail_htlcs.len(), 1);
18879		assert!(updates.update_fail_malformed_htlcs.is_empty());
18880		assert!(updates.update_fee.is_none());
18881		nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
18882		commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
18883		expect_payment_failed!(nodes[0], payment_hash, true);
18884
18885		// Finally, claim the original payment.
18886		claim_payment(&nodes[0], &expected_route, payment_preimage);
18887	}
18888
18889	#[test]
18890	#[rustfmt::skip]
18891	fn test_keysend_hash_mismatch() {
18892		// Test that if we receive a keysend `update_add_htlc` msg, we fail as expected if the keysend
18893		// preimage doesn't match the msg's payment hash.
18894		let chanmon_cfgs = create_chanmon_cfgs(2);
18895		let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
18896		let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
18897		let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
18898
18899		let payer_pubkey = nodes[0].node.get_our_node_id();
18900		let payee_pubkey = nodes[1].node.get_our_node_id();
18901
18902		let _chan = create_chan_between_nodes(&nodes[0], &nodes[1]);
18903		let route_params = RouteParameters::from_payment_params_and_value(
18904			PaymentParameters::for_keysend(payee_pubkey, 40, false), 10_000);
18905		let network_graph = nodes[0].network_graph;
18906		let first_hops = nodes[0].node.list_usable_channels();
18907		let scorer = test_utils::TestScorer::new();
18908		let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
18909		let route = find_route(
18910			&payer_pubkey, &route_params, &network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
18911			nodes[0].logger, &scorer, &Default::default(), &random_seed_bytes
18912		).unwrap();
18913
18914		let test_preimage = PaymentPreimage([42; 32]);
18915		let mismatch_payment_hash = PaymentHash([43; 32]);
18916		let session_privs = nodes[0].node.test_add_new_pending_payment(mismatch_payment_hash,
18917			RecipientOnionFields::spontaneous_empty(), PaymentId(mismatch_payment_hash.0), &route).unwrap();
18918		nodes[0].node.test_send_payment_internal(&route, mismatch_payment_hash,
18919			RecipientOnionFields::spontaneous_empty(), Some(test_preimage), PaymentId(mismatch_payment_hash.0), None, session_privs).unwrap();
18920		check_added_monitors!(nodes[0], 1);
18921
18922		let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
18923		assert_eq!(updates.update_add_htlcs.len(), 1);
18924		assert!(updates.update_fulfill_htlcs.is_empty());
18925		assert!(updates.update_fail_htlcs.is_empty());
18926		assert!(updates.update_fail_malformed_htlcs.is_empty());
18927		assert!(updates.update_fee.is_none());
18928		nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
18929		commitment_signed_dance!(nodes[1], nodes[0], &updates.commitment_signed, false);
18930		expect_and_process_pending_htlcs(&nodes[1], false);
18931		expect_htlc_handling_failed_destinations!(nodes[1].node.get_and_clear_pending_events(), &[HTLCHandlingFailureType::Receive { payment_hash: mismatch_payment_hash }]);
18932		check_added_monitors(&nodes[1], 1);
18933		let _ = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
18934
18935		nodes[1].logger.assert_log_contains("lightning::ln::channelmanager", "Payment preimage didn't match payment hash", 1);
18936	}
18937
18938	#[test]
18939	#[rustfmt::skip]
18940	fn test_multi_hop_missing_secret() {
18941		let chanmon_cfgs = create_chanmon_cfgs(4);
18942		let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
18943		let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
18944		let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
18945
18946		let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
18947		let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2).0.contents.short_channel_id;
18948		let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents.short_channel_id;
18949		let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3).0.contents.short_channel_id;
18950
18951		// Marshall an MPP route.
18952		let (mut route, payment_hash, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[3], 100000);
18953		let path = route.paths[0].clone();
18954		route.paths.push(path);
18955		route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
18956		route.paths[0].hops[0].short_channel_id = chan_1_id;
18957		route.paths[0].hops[1].short_channel_id = chan_3_id;
18958		route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
18959		route.paths[1].hops[0].short_channel_id = chan_2_id;
18960		route.paths[1].hops[1].short_channel_id = chan_4_id;
18961
18962		nodes[0].node.send_payment_with_route(route, payment_hash,
18963			RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0)).unwrap();
18964		let events = nodes[0].node.get_and_clear_pending_events();
18965		assert_eq!(events.len(), 1);
18966		match events[0] {
18967			Event::PaymentFailed { reason, .. } => {
18968				assert_eq!(reason.unwrap(), crate::events::PaymentFailureReason::UnexpectedError);
18969			}
18970			_ => panic!()
18971		}
18972		nodes[0].logger.assert_log_contains("lightning::ln::outbound_payment", "Payment secret is required for multi-path payments", 2);
18973		assert!(nodes[0].node.list_recent_payments().is_empty());
18974	}
18975
18976	#[test]
18977	#[rustfmt::skip]
18978	fn test_channel_update_cached() {
18979		let chanmon_cfgs = create_chanmon_cfgs(3);
18980		let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
18981		let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
18982		let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
18983
18984		let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
18985
18986		let message = "Channel force-closed".to_owned();
18987		nodes[0].node.force_close_broadcasting_latest_txn(&chan.2, &nodes[1].node.get_our_node_id(), message.clone()).unwrap();
18988		check_added_monitors!(nodes[0], 1);
18989		let reason = ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true), message };
18990		check_closed_event!(nodes[0], 1, reason, [nodes[1].node.get_our_node_id()], 100000);
18991
18992		// Confirm that the channel_update was not sent immediately to node[1] but was cached.
18993		let node_1_events = nodes[1].node.get_and_clear_pending_msg_events();
18994		assert_eq!(node_1_events.len(), 0);
18995
18996		{
18997			// Assert that ChannelUpdate message has been added to node[0] pending broadcast messages
18998			let pending_broadcast_messages= nodes[0].node.pending_broadcast_messages.lock().unwrap();
18999			assert_eq!(pending_broadcast_messages.len(), 1);
19000		}
19001
19002		// Test that we do not retrieve the pending broadcast messages when we are not connected to any peer
19003		nodes[0].node.peer_disconnected(nodes[1].node.get_our_node_id());
19004		nodes[1].node.peer_disconnected(nodes[0].node.get_our_node_id());
19005
19006		nodes[0].node.peer_disconnected(nodes[2].node.get_our_node_id());
19007		nodes[2].node.peer_disconnected(nodes[0].node.get_our_node_id());
19008
19009		let node_0_events = nodes[0].node.get_and_clear_pending_msg_events();
19010		assert_eq!(node_0_events.len(), 0);
19011
19012		// Now we reconnect to a peer
19013		nodes[0].node.peer_connected(nodes[2].node.get_our_node_id(), &msgs::Init {
19014			features: nodes[2].node.init_features(), networks: None, remote_network_address: None
19015		}, true).unwrap();
19016		nodes[2].node.peer_connected(nodes[0].node.get_our_node_id(), &msgs::Init {
19017			features: nodes[0].node.init_features(), networks: None, remote_network_address: None
19018		}, false).unwrap();
19019
19020		// Confirm that get_and_clear_pending_msg_events correctly captures pending broadcast messages
19021		let node_0_events = nodes[0].node.get_and_clear_pending_msg_events();
19022		assert_eq!(node_0_events.len(), 1);
19023		match &node_0_events[0] {
19024			MessageSendEvent::BroadcastChannelUpdate { .. } => (),
19025			_ => panic!("Unexpected event"),
19026		}
19027		{
19028			// Assert that ChannelUpdate message has been cleared from nodes[0] pending broadcast messages
19029			let pending_broadcast_messages= nodes[0].node.pending_broadcast_messages.lock().unwrap();
19030			assert_eq!(pending_broadcast_messages.len(), 0);
19031		}
19032	}
19033
19034	#[test]
19035	#[rustfmt::skip]
19036	fn test_drop_disconnected_peers_when_removing_channels() {
19037		let chanmon_cfgs = create_chanmon_cfgs(2);
19038		let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
19039		let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
19040		let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
19041
19042		create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
19043
19044		nodes[0].node.peer_disconnected(nodes[1].node.get_our_node_id());
19045		nodes[1].node.peer_disconnected(nodes[0].node.get_our_node_id());
19046		let chan_id = nodes[0].node.list_channels()[0].channel_id;
19047		let message = "Channel force-closed".to_owned();
19048		nodes[0]
19049			.node
19050			.force_close_broadcasting_latest_txn(&chan_id, &nodes[1].node.get_our_node_id(), message.clone())
19051			.unwrap();
19052		check_added_monitors!(nodes[0], 1);
19053		let reason = ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true), message };
19054		check_closed_event!(nodes[0], 1, reason, [nodes[1].node.get_our_node_id()], 1_000_000);
19055
19056		{
19057			// Assert that nodes[1] is awaiting removal for nodes[0] once nodes[1] has been
19058			// disconnected and the channel between has been force closed.
19059			let nodes_0_per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
19060			// Assert that nodes[1] isn't removed before `timer_tick_occurred` has been executed.
19061			assert_eq!(nodes_0_per_peer_state.len(), 1);
19062			assert!(nodes_0_per_peer_state.get(&nodes[1].node.get_our_node_id()).is_some());
19063		}
19064
19065		nodes[0].node.timer_tick_occurred();
19066
19067		{
19068			// Assert that nodes[1] has now been removed.
19069			assert_eq!(nodes[0].node.per_peer_state.read().unwrap().len(), 0);
19070		}
19071	}
19072
19073	#[test]
19074	#[rustfmt::skip]
19075	fn test_drop_peers_when_removing_unfunded_channels() {
19076		let chanmon_cfgs = create_chanmon_cfgs(2);
19077		let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
19078		let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
19079		let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
19080
19081		exchange_open_accept_chan(&nodes[0], &nodes[1], 1_000_000, 0);
19082		let events = nodes[0].node.get_and_clear_pending_events();
19083		assert_eq!(events.len(), 1, "Unexpected events {:?}", events);
19084		match events[0] {
19085			Event::FundingGenerationReady { .. } => {}
19086			_ => panic!("Unexpected event {:?}", events),
19087		}
19088
19089		nodes[0].node.peer_disconnected(nodes[1].node.get_our_node_id());
19090		nodes[1].node.peer_disconnected(nodes[0].node.get_our_node_id());
19091		check_closed_event!(nodes[0], 1, ClosureReason::DisconnectedPeer, [nodes[1].node.get_our_node_id()], 1_000_000);
19092		check_closed_event!(nodes[1], 1, ClosureReason::DisconnectedPeer, [nodes[0].node.get_our_node_id()], 1_000_000);
19093
19094		// At this point the state for the peers should have been removed.
19095		assert_eq!(nodes[0].node.per_peer_state.read().unwrap().len(), 0);
19096		assert_eq!(nodes[1].node.per_peer_state.read().unwrap().len(), 0);
19097	}
19098
19099	#[test]
19100	#[rustfmt::skip]
19101	fn bad_inbound_payment_hash() {
19102		// Add coverage for checking that a user-provided payment hash matches the payment secret.
19103		let chanmon_cfgs = create_chanmon_cfgs(2);
19104		let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
19105		let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
19106		let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
19107
19108		let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(&nodes[0]);
19109		let payment_data = msgs::FinalOnionHopData {
19110			payment_secret,
19111			total_msat: 100_000,
19112		};
19113
19114		// Ensure that if the payment hash given to `inbound_payment::verify` differs from the original,
19115		// payment verification fails as expected.
19116		let mut bad_payment_hash = payment_hash.clone();
19117		bad_payment_hash.0[0] += 1;
19118		match inbound_payment::verify(bad_payment_hash, &payment_data, nodes[0].node.highest_seen_timestamp.load(Ordering::Acquire) as u64, &nodes[0].node.inbound_payment_key, &nodes[0].logger) {
19119			Ok(_) => panic!("Unexpected ok"),
19120			Err(()) => {
19121				nodes[0].logger.assert_log_contains("lightning::ln::inbound_payment", "Failing HTLC with user-generated payment_hash", 1);
19122			}
19123		}
19124
19125		// Check that using the original payment hash succeeds.
19126		assert!(inbound_payment::verify(payment_hash, &payment_data, nodes[0].node.highest_seen_timestamp.load(Ordering::Acquire) as u64, &nodes[0].node.inbound_payment_key, &nodes[0].logger).is_ok());
19127	}
19128
19129	fn check_not_connected_to_peer_error<T>(
19130		res_err: Result<T, APIError>, expected_public_key: PublicKey,
19131	) {
19132		let expected_message = format!("Not connected to node: {}", expected_public_key);
19133		check_api_error_message(expected_message, res_err)
19134	}
19135
19136	#[rustfmt::skip]
19137	fn check_unkown_peer_error<T>(res_err: Result<T, APIError>, expected_public_key: PublicKey) {
19138		let expected_message = format!("Can't find a peer matching the passed counterparty node_id {}", expected_public_key);
19139		check_api_error_message(expected_message, res_err)
19140	}
19141
19142	#[rustfmt::skip]
19143	fn check_channel_unavailable_error<T>(res_err: Result<T, APIError>, expected_channel_id: ChannelId, peer_node_id: PublicKey) {
19144		let expected_message = format!("Channel with id {} not found for the passed counterparty node_id {}", expected_channel_id, peer_node_id);
19145		check_api_error_message(expected_message, res_err)
19146	}
19147
19148	fn check_api_misuse_error<T>(res_err: Result<T, APIError>) {
19149		let expected_message = "No such channel awaiting to be accepted.".to_string();
19150		check_api_error_message(expected_message, res_err)
19151	}
19152
19153	fn check_api_error_message<T>(expected_err_message: String, res_err: Result<T, APIError>) {
19154		match res_err {
19155			Err(APIError::APIMisuseError { err }) => {
19156				assert_eq!(err, expected_err_message);
19157			},
19158			Err(APIError::ChannelUnavailable { err }) => {
19159				assert_eq!(err, expected_err_message);
19160			},
19161			Ok(_) => panic!("Unexpected Ok"),
19162			Err(_) => panic!("Unexpected Error"),
19163		}
19164	}
19165
19166	#[test]
19167	#[rustfmt::skip]
19168	fn test_api_calls_with_unkown_counterparty_node() {
19169		// Tests that our API functions that expects a `counterparty_node_id` as input, behaves as
19170		// expected if the `counterparty_node_id` is an unkown peer in the
19171		// `ChannelManager::per_peer_state` map.
19172		let chanmon_cfg = create_chanmon_cfgs(2);
19173		let node_cfg = create_node_cfgs(2, &chanmon_cfg);
19174		let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[None, None]);
19175		let nodes = create_network(2, &node_cfg, &node_chanmgr);
19176
19177		// Dummy values
19178		let channel_id = ChannelId::from_bytes([4; 32]);
19179		let unkown_public_key = PublicKey::from_secret_key(&Secp256k1::signing_only(), &SecretKey::from_slice(&[42; 32]).unwrap());
19180		let intercept_id = InterceptId([0; 32]);
19181		let error_message = "Channel force-closed";
19182
19183		// Test the API functions.
19184		check_not_connected_to_peer_error(nodes[0].node.create_channel(unkown_public_key, 1_000_000, 500_000_000, 42, None, None), unkown_public_key);
19185
19186		check_unkown_peer_error(nodes[0].node.accept_inbound_channel(&channel_id, &unkown_public_key, 42, None), unkown_public_key);
19187
19188		check_unkown_peer_error(nodes[0].node.close_channel(&channel_id, &unkown_public_key), unkown_public_key);
19189
19190		check_unkown_peer_error(nodes[0].node.force_close_broadcasting_latest_txn(&channel_id, &unkown_public_key, error_message.to_string()), unkown_public_key);
19191
19192		check_unkown_peer_error(nodes[0].node.forward_intercepted_htlc(intercept_id, &channel_id, unkown_public_key, 1_000_000), unkown_public_key);
19193
19194		check_unkown_peer_error(nodes[0].node.update_channel_config(&unkown_public_key, &[channel_id], &ChannelConfig::default()), unkown_public_key);
19195	}
19196
19197	#[test]
19198	#[rustfmt::skip]
19199	fn test_api_calls_with_unavailable_channel() {
19200		// Tests that our API functions that expects a `counterparty_node_id` and a `channel_id`
19201		// as input, behaves as expected if the `counterparty_node_id` is a known peer in the
19202		// `ChannelManager::per_peer_state` map, but the peer state doesn't contain a channel with
19203		// the given `channel_id`.
19204		let chanmon_cfg = create_chanmon_cfgs(2);
19205		let node_cfg = create_node_cfgs(2, &chanmon_cfg);
19206		let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[None, None]);
19207		let nodes = create_network(2, &node_cfg, &node_chanmgr);
19208
19209		let counterparty_node_id = nodes[1].node.get_our_node_id();
19210
19211		// Dummy values
19212		let channel_id = ChannelId::from_bytes([4; 32]);
19213		let error_message = "Channel force-closed";
19214
19215		// Test the API functions.
19216		check_api_misuse_error(nodes[0].node.accept_inbound_channel(&channel_id, &counterparty_node_id, 42, None));
19217
19218		check_channel_unavailable_error(nodes[0].node.close_channel(&channel_id, &counterparty_node_id), channel_id, counterparty_node_id);
19219
19220		check_channel_unavailable_error(nodes[0].node.force_close_broadcasting_latest_txn(&channel_id, &counterparty_node_id, error_message.to_string()), channel_id, counterparty_node_id);
19221
19222		check_channel_unavailable_error(nodes[0].node.forward_intercepted_htlc(InterceptId([0; 32]), &channel_id, counterparty_node_id, 1_000_000), channel_id, counterparty_node_id);
19223
19224		check_channel_unavailable_error(nodes[0].node.update_channel_config(&counterparty_node_id, &[channel_id], &ChannelConfig::default()), channel_id, counterparty_node_id);
19225	}
19226
19227	#[test]
19228	#[rustfmt::skip]
19229	fn test_connection_limiting() {
19230		// Test that we limit un-channel'd peers and un-funded channels properly.
19231		let chanmon_cfgs = create_chanmon_cfgs(2);
19232		let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
19233		let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
19234		let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
19235
19236		// Note that create_network connects the nodes together for us
19237
19238		nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None, None).unwrap();
19239		let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
19240
19241		let mut funding_tx = None;
19242		for idx in 0..super::MAX_UNFUNDED_CHANS_PER_PEER {
19243			nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
19244			let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
19245
19246			if idx == 0 {
19247				nodes[0].node.handle_accept_channel(nodes[1].node.get_our_node_id(), &accept_channel);
19248				let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100_000, 42);
19249				funding_tx = Some(tx.clone());
19250				nodes[0].node.funding_transaction_generated(temporary_channel_id, nodes[1].node.get_our_node_id(), tx).unwrap();
19251				let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
19252
19253				nodes[1].node.handle_funding_created(nodes[0].node.get_our_node_id(), &funding_created_msg);
19254				check_added_monitors!(nodes[1], 1);
19255				expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
19256
19257				let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
19258
19259				nodes[0].node.handle_funding_signed(nodes[1].node.get_our_node_id(), &funding_signed);
19260				check_added_monitors!(nodes[0], 1);
19261				expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
19262			}
19263			open_channel_msg.common_fields.temporary_channel_id = ChannelId::temporary_from_entropy_source(&nodes[0].keys_manager);
19264		}
19265
19266		// A MAX_UNFUNDED_CHANS_PER_PEER + 1 channel will be summarily rejected
19267		open_channel_msg.common_fields.temporary_channel_id = ChannelId::temporary_from_entropy_source(
19268			&nodes[0].keys_manager);
19269		nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
19270		assert_eq!(get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id()).channel_id,
19271			open_channel_msg.common_fields.temporary_channel_id);
19272
19273		// Further, because all of our channels with nodes[0] are inbound, and none of them funded,
19274		// it doesn't count as a "protected" peer, i.e. it counts towards the MAX_NO_CHANNEL_PEERS
19275		// limit.
19276		let mut peer_pks = Vec::with_capacity(super::MAX_NO_CHANNEL_PEERS);
19277		for _ in 1..super::MAX_NO_CHANNEL_PEERS {
19278			let random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
19279				&SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
19280			peer_pks.push(random_pk);
19281			nodes[1].node.peer_connected(random_pk, &msgs::Init {
19282				features: nodes[0].node.init_features(), networks: None, remote_network_address: None
19283			}, true).unwrap();
19284		}
19285		let last_random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
19286			&SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
19287		nodes[1].node.peer_connected(last_random_pk, &msgs::Init {
19288			features: nodes[0].node.init_features(), networks: None, remote_network_address: None
19289		}, true).unwrap_err();
19290
19291		// Also importantly, because nodes[0] isn't "protected", we will refuse a reconnection from
19292		// them if we have too many un-channel'd peers.
19293		nodes[1].node.peer_disconnected(nodes[0].node.get_our_node_id());
19294		let chan_closed_events = nodes[1].node.get_and_clear_pending_events();
19295		assert_eq!(chan_closed_events.len(), super::MAX_UNFUNDED_CHANS_PER_PEER - 1);
19296		for ev in chan_closed_events {
19297			if let Event::ChannelClosed { .. } = ev { } else { panic!(); }
19298		}
19299		nodes[1].node.peer_connected(last_random_pk, &msgs::Init {
19300			features: nodes[0].node.init_features(), networks: None, remote_network_address: None
19301		}, true).unwrap();
19302		nodes[1].node.peer_connected(nodes[0].node.get_our_node_id(), &msgs::Init {
19303			features: nodes[0].node.init_features(), networks: None, remote_network_address: None
19304		}, true).unwrap_err();
19305
19306		// but of course if the connection is outbound its allowed...
19307		nodes[1].node.peer_connected(nodes[0].node.get_our_node_id(), &msgs::Init {
19308			features: nodes[0].node.init_features(), networks: None, remote_network_address: None
19309		}, false).unwrap();
19310		nodes[1].node.peer_disconnected(nodes[0].node.get_our_node_id());
19311
19312		// Now nodes[0] is disconnected but still has a pending, un-funded channel lying around.
19313		// Even though we accept one more connection from new peers, we won't actually let them
19314		// open channels.
19315		assert!(peer_pks.len() > super::MAX_UNFUNDED_CHANNEL_PEERS - 1);
19316		for i in 0..super::MAX_UNFUNDED_CHANNEL_PEERS - 1 {
19317			nodes[1].node.handle_open_channel(peer_pks[i], &open_channel_msg);
19318			get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, peer_pks[i]);
19319			open_channel_msg.common_fields.temporary_channel_id = ChannelId::temporary_from_entropy_source(&nodes[0].keys_manager);
19320		}
19321		nodes[1].node.handle_open_channel(last_random_pk, &open_channel_msg);
19322		assert_eq!(get_err_msg(&nodes[1], &last_random_pk).channel_id,
19323			open_channel_msg.common_fields.temporary_channel_id);
19324
19325		// Of course, however, outbound channels are always allowed
19326		nodes[1].node.create_channel(last_random_pk, 100_000, 0, 42, None, None).unwrap();
19327		get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, last_random_pk);
19328
19329		// If we fund the first channel, nodes[0] has a live on-chain channel with us, it is now
19330		// "protected" and can connect again.
19331		mine_transaction(&nodes[1], funding_tx.as_ref().unwrap());
19332		nodes[1].node.peer_connected(nodes[0].node.get_our_node_id(), &msgs::Init {
19333			features: nodes[0].node.init_features(), networks: None, remote_network_address: None
19334		}, true).unwrap();
19335		get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
19336
19337		// Further, because the first channel was funded, we can open another channel with
19338		// last_random_pk.
19339		nodes[1].node.handle_open_channel(last_random_pk, &open_channel_msg);
19340		get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, last_random_pk);
19341	}
19342
19343	#[test]
19344	#[rustfmt::skip]
19345	fn reject_excessively_underpaying_htlcs() {
19346		let chanmon_cfg = create_chanmon_cfgs(1);
19347		let node_cfg = create_node_cfgs(1, &chanmon_cfg);
19348		let node_chanmgr = create_node_chanmgrs(1, &node_cfg, &[None]);
19349		let node = create_network(1, &node_cfg, &node_chanmgr);
19350		let sender_intended_amt_msat = 100;
19351		let extra_fee_msat = 10;
19352		let hop_data = onion_utils::Hop::Receive {
19353			hop_data: msgs::InboundOnionReceivePayload {
19354				sender_intended_htlc_amt_msat: 100,
19355				cltv_expiry_height: 42,
19356				payment_metadata: None,
19357				keysend_preimage: None,
19358				payment_data: Some(msgs::FinalOnionHopData {
19359					payment_secret: PaymentSecret([0; 32]),
19360					total_msat: sender_intended_amt_msat,
19361				}),
19362				custom_tlvs: Vec::new(),
19363			},
19364			shared_secret: SharedSecret::from_bytes([0; 32]),
19365		};
19366		// Check that if the amount we received + the penultimate hop extra fee is less than the sender
19367		// intended amount, we fail the payment.
19368		let current_height: u32 = node[0].node.best_block.read().unwrap().height;
19369		if let Err(crate::ln::channelmanager::InboundHTLCErr { reason, .. }) =
19370			create_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
19371				sender_intended_amt_msat - extra_fee_msat - 1, 42, None, true, Some(extra_fee_msat),
19372				current_height)
19373		{
19374			assert_eq!(reason, LocalHTLCFailureReason::FinalIncorrectHTLCAmount);
19375		} else { panic!(); }
19376
19377		// If amt_received + extra_fee is equal to the sender intended amount, we're fine.
19378		let hop_data = onion_utils::Hop::Receive {
19379			hop_data: msgs::InboundOnionReceivePayload { // This is the same payload as above, InboundOnionPayload doesn't implement Clone
19380				sender_intended_htlc_amt_msat: 100,
19381				cltv_expiry_height: 42,
19382				payment_metadata: None,
19383				keysend_preimage: None,
19384				payment_data: Some(msgs::FinalOnionHopData {
19385					payment_secret: PaymentSecret([0; 32]),
19386					total_msat: sender_intended_amt_msat,
19387				}),
19388				custom_tlvs: Vec::new(),
19389			},
19390			shared_secret: SharedSecret::from_bytes([0; 32]),
19391		};
19392		let current_height: u32 = node[0].node.best_block.read().unwrap().height;
19393		assert!(create_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
19394			sender_intended_amt_msat - extra_fee_msat, 42, None, true, Some(extra_fee_msat),
19395			current_height).is_ok());
19396	}
19397
19398	#[test]
19399	#[rustfmt::skip]
19400	fn test_final_incorrect_cltv(){
19401		let chanmon_cfg = create_chanmon_cfgs(1);
19402		let node_cfg = create_node_cfgs(1, &chanmon_cfg);
19403		let node_chanmgr = create_node_chanmgrs(1, &node_cfg, &[None]);
19404		let node = create_network(1, &node_cfg, &node_chanmgr);
19405
19406		let current_height: u32 = node[0].node.best_block.read().unwrap().height;
19407		let result = create_recv_pending_htlc_info(onion_utils::Hop::Receive {
19408			hop_data: msgs::InboundOnionReceivePayload {
19409				sender_intended_htlc_amt_msat: 100,
19410				cltv_expiry_height: TEST_FINAL_CLTV,
19411				payment_metadata: None,
19412				keysend_preimage: None,
19413				payment_data: Some(msgs::FinalOnionHopData {
19414					payment_secret: PaymentSecret([0; 32]),
19415					total_msat: 100,
19416				}),
19417				custom_tlvs: Vec::new(),
19418			},
19419			shared_secret: SharedSecret::from_bytes([0; 32]),
19420		}, [0; 32], PaymentHash([0; 32]), 100, TEST_FINAL_CLTV + 1, None, true, None, current_height);
19421
19422		// Should not return an error as this condition:
19423		// https://github.com/lightning/bolts/blob/4dcc377209509b13cf89a4b91fde7d478f5b46d8/04-onion-routing.md?plain=1#L334
19424		// is not satisfied.
19425		assert!(result.is_ok());
19426	}
19427
19428	#[test]
19429	#[rustfmt::skip]
19430	fn test_update_channel_config() {
19431		let chanmon_cfg = create_chanmon_cfgs(2);
19432		let node_cfg = create_node_cfgs(2, &chanmon_cfg);
19433		let mut user_config = test_default_channel_config();
19434		let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[Some(user_config.clone()), Some(user_config.clone())]);
19435		let nodes = create_network(2, &node_cfg, &node_chanmgr);
19436		let _ = create_announced_chan_between_nodes(&nodes, 0, 1);
19437		let channel = &nodes[0].node.list_channels()[0];
19438
19439		nodes[0].node.update_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &user_config.channel_config).unwrap();
19440		let events = nodes[0].node.get_and_clear_pending_msg_events();
19441		assert_eq!(events.len(), 0);
19442
19443		user_config.channel_config.forwarding_fee_base_msat += 10;
19444		nodes[0].node.update_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &user_config.channel_config).unwrap();
19445		assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_base_msat, user_config.channel_config.forwarding_fee_base_msat);
19446		let events = nodes[0].node.get_and_clear_pending_msg_events();
19447		assert_eq!(events.len(), 1);
19448		match &events[0] {
19449			MessageSendEvent::BroadcastChannelUpdate { .. } => {},
19450			_ => panic!("expected BroadcastChannelUpdate event"),
19451		}
19452
19453		nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &ChannelConfigUpdate::default()).unwrap();
19454		let events = nodes[0].node.get_and_clear_pending_msg_events();
19455		assert_eq!(events.len(), 0);
19456
19457		let new_cltv_expiry_delta = user_config.channel_config.cltv_expiry_delta + 6;
19458		nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &ChannelConfigUpdate {
19459			cltv_expiry_delta: Some(new_cltv_expiry_delta),
19460			..Default::default()
19461		}).unwrap();
19462		assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().cltv_expiry_delta, new_cltv_expiry_delta);
19463		let events = nodes[0].node.get_and_clear_pending_msg_events();
19464		assert_eq!(events.len(), 1);
19465		match &events[0] {
19466			MessageSendEvent::BroadcastChannelUpdate { .. } => {},
19467			_ => panic!("expected BroadcastChannelUpdate event"),
19468		}
19469
19470		let new_fee = user_config.channel_config.forwarding_fee_proportional_millionths + 100;
19471		nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &ChannelConfigUpdate {
19472			forwarding_fee_proportional_millionths: Some(new_fee),
19473			accept_underpaying_htlcs: Some(true),
19474			..Default::default()
19475		}).unwrap();
19476		assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().cltv_expiry_delta, new_cltv_expiry_delta);
19477		assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths, new_fee);
19478		assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().accept_underpaying_htlcs, true);
19479		let events = nodes[0].node.get_and_clear_pending_msg_events();
19480		assert_eq!(events.len(), 1);
19481		match &events[0] {
19482			MessageSendEvent::BroadcastChannelUpdate { .. } => {},
19483			_ => panic!("expected BroadcastChannelUpdate event"),
19484		}
19485
19486		// If we provide a channel_id not associated with the peer, we should get an error and no updates
19487		// should be applied to ensure update atomicity as specified in the API docs.
19488		let bad_channel_id = ChannelId::v1_from_funding_txid(&[10; 32], 10);
19489		let current_fee = nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths;
19490		let new_fee = current_fee + 100;
19491		assert!(
19492			matches!(
19493				nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id, bad_channel_id], &ChannelConfigUpdate {
19494					forwarding_fee_proportional_millionths: Some(new_fee),
19495					..Default::default()
19496				}),
19497				Err(APIError::ChannelUnavailable { err: _ }),
19498			)
19499		);
19500		// Check that the fee hasn't changed for the channel that exists.
19501		assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths, current_fee);
19502		let events = nodes[0].node.get_and_clear_pending_msg_events();
19503		assert_eq!(events.len(), 0);
19504	}
19505
19506	#[test]
19507	#[rustfmt::skip]
19508	fn test_payment_display() {
19509		let payment_id = PaymentId([42; 32]);
19510		assert_eq!(format!("{}", &payment_id), "2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a");
19511		let payment_hash = PaymentHash([42; 32]);
19512		assert_eq!(format!("{}", &payment_hash), "2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a");
19513		let payment_preimage = PaymentPreimage([42; 32]);
19514		assert_eq!(format!("{}", &payment_preimage), "2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a");
19515	}
19516
19517	#[test]
19518	#[rustfmt::skip]
19519	fn test_trigger_lnd_force_close() {
19520		let chanmon_cfg = create_chanmon_cfgs(2);
19521		let node_cfg = create_node_cfgs(2, &chanmon_cfg);
19522		let user_config = test_default_channel_config();
19523		let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[Some(user_config.clone()), Some(user_config)]);
19524		let nodes = create_network(2, &node_cfg, &node_chanmgr);
19525		let message = "Channel force-closed".to_owned();
19526
19527		// Open a channel, immediately disconnect each other, and broadcast Alice's latest state.
19528		let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1);
19529		nodes[0].node.peer_disconnected(nodes[1].node.get_our_node_id());
19530		nodes[1].node.peer_disconnected(nodes[0].node.get_our_node_id());
19531		nodes[0]
19532			.node
19533			.force_close_broadcasting_latest_txn(&chan_id, &nodes[1].node.get_our_node_id(), message.clone())
19534			.unwrap();
19535		check_closed_broadcast(&nodes[0], 1, false);
19536		check_added_monitors(&nodes[0], 1);
19537		let reason = ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true), message };
19538		check_closed_event!(nodes[0], 1, reason, [nodes[1].node.get_our_node_id()], 100000);
19539		{
19540			let txn = nodes[0].tx_broadcaster.txn_broadcast();
19541			assert_eq!(txn.len(), 1);
19542			check_spends!(txn[0], funding_tx);
19543		}
19544
19545		// Since they're disconnected, Bob won't receive Alice's `Error` message. Reconnect them
19546		// such that Bob sends a `ChannelReestablish` to Alice since the channel is still open from
19547		// their side.
19548		nodes[0].node.peer_connected(nodes[1].node.get_our_node_id(), &msgs::Init {
19549			features: nodes[1].node.init_features(), networks: None, remote_network_address: None
19550		}, true).unwrap();
19551		nodes[1].node.peer_connected(nodes[0].node.get_our_node_id(), &msgs::Init {
19552			features: nodes[0].node.init_features(), networks: None, remote_network_address: None
19553		}, false).unwrap();
19554		assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
19555		let channel_reestablish = get_event_msg!(
19556			nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id()
19557		);
19558		nodes[0].node.handle_channel_reestablish(nodes[1].node.get_our_node_id(), &channel_reestablish);
19559
19560		// Alice should respond with an error since the channel isn't known, but a bogus
19561		// `ChannelReestablish` should be sent first, such that we actually trigger Bob to force
19562		// close even if it was an lnd node.
19563		let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
19564		assert_eq!(msg_events.len(), 2);
19565		if let MessageSendEvent::SendChannelReestablish { node_id, msg } = &msg_events[0] {
19566			assert_eq!(*node_id, nodes[1].node.get_our_node_id());
19567			assert_eq!(msg.next_local_commitment_number, 0);
19568			assert_eq!(msg.next_remote_commitment_number, 0);
19569			nodes[1].node.handle_channel_reestablish(nodes[0].node.get_our_node_id(), &msg);
19570		} else { panic!() };
19571		check_closed_broadcast(&nodes[1], 1, true);
19572		check_added_monitors(&nodes[1], 1);
19573		let expected_close_reason = ClosureReason::ProcessingError {
19574			err: "Peer sent an invalid channel_reestablish to force close in a non-standard way".to_string()
19575		};
19576		check_closed_event!(nodes[1], 1, expected_close_reason, [nodes[0].node.get_our_node_id()], 100000);
19577		{
19578			let txn = nodes[1].tx_broadcaster.txn_broadcast();
19579			assert_eq!(txn.len(), 1);
19580			check_spends!(txn[0], funding_tx);
19581		}
19582	}
19583
19584	#[test]
19585	#[rustfmt::skip]
19586	fn test_malformed_forward_htlcs_ser() {
19587		// Ensure that `HTLCForwardInfo::FailMalformedHTLC`s are (de)serialized properly.
19588		let chanmon_cfg = create_chanmon_cfgs(1);
19589		let node_cfg = create_node_cfgs(1, &chanmon_cfg);
19590		let persister;
19591		let chain_monitor;
19592		let chanmgrs = create_node_chanmgrs(1, &node_cfg, &[None]);
19593		let deserialized_chanmgr;
19594		let mut nodes = create_network(1, &node_cfg, &chanmgrs);
19595
19596		let dummy_failed_htlc = |htlc_id| {
19597			HTLCForwardInfo::FailHTLC { htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42], attribution_data: Some(AttributionData::new()) } }
19598		};
19599		let dummy_malformed_htlc = |htlc_id| {
19600			HTLCForwardInfo::FailMalformedHTLC {
19601				htlc_id,
19602				failure_code: LocalHTLCFailureReason::InvalidOnionPayload.failure_code(),
19603				sha256_of_onion: [0; 32],
19604			}
19605		};
19606
19607		let dummy_htlcs_1: Vec<HTLCForwardInfo> = (1..10).map(|htlc_id| {
19608			if htlc_id % 2 == 0 {
19609				dummy_failed_htlc(htlc_id)
19610			} else {
19611				dummy_malformed_htlc(htlc_id)
19612			}
19613		}).collect();
19614
19615		let dummy_htlcs_2: Vec<HTLCForwardInfo> = (1..10).map(|htlc_id| {
19616			if htlc_id % 2 == 1 {
19617				dummy_failed_htlc(htlc_id)
19618			} else {
19619				dummy_malformed_htlc(htlc_id)
19620			}
19621		}).collect();
19622
19623
19624		let (scid_1, scid_2) = (42, 43);
19625		let mut forward_htlcs = new_hash_map();
19626		forward_htlcs.insert(scid_1, dummy_htlcs_1.clone());
19627		forward_htlcs.insert(scid_2, dummy_htlcs_2.clone());
19628
19629		let mut chanmgr_fwd_htlcs = nodes[0].node.forward_htlcs.lock().unwrap();
19630		*chanmgr_fwd_htlcs = forward_htlcs.clone();
19631		core::mem::drop(chanmgr_fwd_htlcs);
19632
19633		reload_node!(nodes[0], nodes[0].node.encode(), &[], persister, chain_monitor, deserialized_chanmgr);
19634
19635		let mut deserialized_fwd_htlcs = nodes[0].node.forward_htlcs.lock().unwrap();
19636		for scid in [scid_1, scid_2].iter() {
19637			let deserialized_htlcs = deserialized_fwd_htlcs.remove(scid).unwrap();
19638			assert_eq!(forward_htlcs.remove(scid).unwrap(), deserialized_htlcs);
19639		}
19640		assert!(deserialized_fwd_htlcs.is_empty());
19641		core::mem::drop(deserialized_fwd_htlcs);
19642	}
19643}
19644
19645#[cfg(ldk_bench)]
19646pub mod bench {
19647	use crate::chain::chainmonitor::{ChainMonitor, Persist};
19648	use crate::chain::Listen;
19649	use crate::events::Event;
19650	use crate::ln::channelmanager::{
19651		BestBlock, ChainParameters, ChannelManager, PaymentHash, PaymentId, PaymentPreimage,
19652		RecipientOnionFields, Retry,
19653	};
19654	use crate::ln::functional_test_utils::*;
19655	use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, Init, MessageSendEvent};
19656	use crate::routing::gossip::NetworkGraph;
19657	use crate::routing::router::{PaymentParameters, RouteParameters};
19658	use crate::sign::{InMemorySigner, KeysManager, NodeSigner};
19659	use crate::util::config::{MaxDustHTLCExposure, UserConfig};
19660	use crate::util::test_utils;
19661
19662	use bitcoin::amount::Amount;
19663	use bitcoin::hashes::sha256::Hash as Sha256;
19664	use bitcoin::hashes::Hash;
19665	use bitcoin::locktime::absolute::LockTime;
19666	use bitcoin::transaction::Version;
19667	use bitcoin::{Transaction, TxOut};
19668
19669	use crate::sync::{Arc, RwLock};
19670
19671	use criterion::Criterion;
19672
19673	type Manager<'a, P> = ChannelManager<
19674		&'a ChainMonitor<
19675			InMemorySigner,
19676			&'a test_utils::TestChainSource,
19677			&'a test_utils::TestBroadcaster,
19678			&'a test_utils::TestFeeEstimator,
19679			&'a test_utils::TestLogger,
19680			&'a P,
19681			&'a KeysManager,
19682		>,
19683		&'a test_utils::TestBroadcaster,
19684		&'a KeysManager,
19685		&'a KeysManager,
19686		&'a KeysManager,
19687		&'a test_utils::TestFeeEstimator,
19688		&'a test_utils::TestRouter<'a>,
19689		&'a test_utils::TestMessageRouter<'a>,
19690		&'a test_utils::TestLogger,
19691	>;
19692
19693	struct ANodeHolder<'node_cfg, 'chan_mon_cfg: 'node_cfg, P: Persist<InMemorySigner>> {
19694		node: &'node_cfg Manager<'chan_mon_cfg, P>,
19695	}
19696	impl<'node_cfg, 'chan_mon_cfg: 'node_cfg, P: Persist<InMemorySigner>> NodeHolder
19697		for ANodeHolder<'node_cfg, 'chan_mon_cfg, P>
19698	{
19699		type CM = Manager<'chan_mon_cfg, P>;
19700		#[inline]
19701		#[rustfmt::skip]
19702		fn node(&self) -> &Manager<'chan_mon_cfg, P> { self.node }
19703		#[inline]
19704		#[rustfmt::skip]
19705		fn chain_monitor(&self) -> Option<&test_utils::TestChainMonitor> { None }
19706	}
19707
19708	#[rustfmt::skip]
19709	pub fn bench_sends(bench: &mut Criterion) {
19710		bench_two_sends(bench, "bench_sends", test_utils::TestPersister::new(), test_utils::TestPersister::new());
19711	}
19712
19713	#[rustfmt::skip]
19714	pub fn bench_two_sends<P: Persist<InMemorySigner>>(bench: &mut Criterion, bench_name: &str, persister_a: P, persister_b: P) {
19715		// Do a simple benchmark of sending a payment back and forth between two nodes.
19716		// Note that this is unrealistic as each payment send will require at least two fsync
19717		// calls per node.
19718		let network = bitcoin::Network::Testnet;
19719		let genesis_block = bitcoin::constants::genesis_block(network);
19720
19721		let tx_broadcaster = test_utils::TestBroadcaster::new(network);
19722		let fee_estimator = test_utils::TestFeeEstimator::new(253);
19723		let logger_a = test_utils::TestLogger::with_id("node a".to_owned());
19724		let scorer = RwLock::new(test_utils::TestScorer::new());
19725		let entropy = test_utils::TestKeysInterface::new(&[0u8; 32], network);
19726		let router = test_utils::TestRouter::new(Arc::new(NetworkGraph::new(network, &logger_a)), &logger_a, &scorer);
19727		let message_router = test_utils::TestMessageRouter::new_default(Arc::new(NetworkGraph::new(network, &logger_a)), &entropy);
19728
19729		let mut config: UserConfig = Default::default();
19730		config.channel_config.max_dust_htlc_exposure = MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253);
19731		config.channel_handshake_config.minimum_depth = 1;
19732
19733		let seed_a = [1u8; 32];
19734		let keys_manager_a = KeysManager::new(&seed_a, 42, 42, true);
19735		let chain_monitor_a = ChainMonitor::new(None, &tx_broadcaster, &logger_a, &fee_estimator, &persister_a, &keys_manager_a, keys_manager_a.get_peer_storage_key());
19736		let node_a = ChannelManager::new(&fee_estimator, &chain_monitor_a, &tx_broadcaster, &router, &message_router, &logger_a, &keys_manager_a, &keys_manager_a, &keys_manager_a, config.clone(), ChainParameters {
19737			network,
19738			best_block: BestBlock::from_network(network),
19739		}, genesis_block.header.time);
19740		let node_a_holder = ANodeHolder { node: &node_a };
19741
19742		let logger_b = test_utils::TestLogger::with_id("node a".to_owned());
19743		let seed_b = [2u8; 32];
19744		let keys_manager_b = KeysManager::new(&seed_b, 42, 42, true);
19745		let chain_monitor_b = ChainMonitor::new(None, &tx_broadcaster, &logger_a, &fee_estimator, &persister_b, &keys_manager_b, keys_manager_b.get_peer_storage_key());
19746		let node_b = ChannelManager::new(&fee_estimator, &chain_monitor_b, &tx_broadcaster, &router, &message_router, &logger_b, &keys_manager_b, &keys_manager_b, &keys_manager_b, config.clone(), ChainParameters {
19747			network,
19748			best_block: BestBlock::from_network(network),
19749		}, genesis_block.header.time);
19750		let node_b_holder = ANodeHolder { node: &node_b };
19751
19752		node_a.peer_connected(node_b.get_our_node_id(), &Init {
19753			features: node_b.init_features(), networks: None, remote_network_address: None
19754		}, true).unwrap();
19755		node_b.peer_connected(node_a.get_our_node_id(), &Init {
19756			features: node_a.init_features(), networks: None, remote_network_address: None
19757		}, false).unwrap();
19758		node_a.create_channel(node_b.get_our_node_id(), 8_000_000, 100_000_000, 42, None, None).unwrap();
19759		node_b.handle_open_channel(node_a.get_our_node_id(), &get_event_msg!(node_a_holder, MessageSendEvent::SendOpenChannel, node_b.get_our_node_id()));
19760		node_a.handle_accept_channel(node_b.get_our_node_id(), &get_event_msg!(node_b_holder, MessageSendEvent::SendAcceptChannel, node_a.get_our_node_id()));
19761
19762		let tx;
19763		if let Event::FundingGenerationReady { temporary_channel_id, output_script, .. } = get_event!(node_a_holder, Event::FundingGenerationReady) {
19764			tx = Transaction { version: Version::TWO, lock_time: LockTime::ZERO, input: Vec::new(), output: vec![TxOut {
19765				value: Amount::from_sat(8_000_000), script_pubkey: output_script,
19766			}]};
19767			node_a.funding_transaction_generated(temporary_channel_id, node_b.get_our_node_id(), tx.clone()).unwrap();
19768		} else { panic!(); }
19769
19770		node_b.handle_funding_created(node_a.get_our_node_id(), &get_event_msg!(node_a_holder, MessageSendEvent::SendFundingCreated, node_b.get_our_node_id()));
19771		let events_b = node_b.get_and_clear_pending_events();
19772		assert_eq!(events_b.len(), 1);
19773		match events_b[0] {
19774			Event::ChannelPending{ ref counterparty_node_id, .. } => {
19775				assert_eq!(*counterparty_node_id, node_a.get_our_node_id());
19776			},
19777			_ => panic!("Unexpected event"),
19778		}
19779
19780		node_a.handle_funding_signed(node_b.get_our_node_id(), &get_event_msg!(node_b_holder, MessageSendEvent::SendFundingSigned, node_a.get_our_node_id()));
19781		let events_a = node_a.get_and_clear_pending_events();
19782		assert_eq!(events_a.len(), 1);
19783		match events_a[0] {
19784			Event::ChannelPending{ ref counterparty_node_id, .. } => {
19785				assert_eq!(*counterparty_node_id, node_b.get_our_node_id());
19786			},
19787			_ => panic!("Unexpected event"),
19788		}
19789
19790		assert_eq!(&tx_broadcaster.txn_broadcasted.lock().unwrap()[..], &[tx.clone()]);
19791
19792		let block = create_dummy_block(BestBlock::from_network(network).block_hash, 42, vec![tx]);
19793		Listen::block_connected(&node_a, &block, 1);
19794		Listen::block_connected(&node_b, &block, 1);
19795
19796		node_a.handle_channel_ready(node_b.get_our_node_id(), &get_event_msg!(node_b_holder, MessageSendEvent::SendChannelReady, node_a.get_our_node_id()));
19797		let msg_events = node_a.get_and_clear_pending_msg_events();
19798		assert_eq!(msg_events.len(), 2);
19799		match msg_events[0] {
19800			MessageSendEvent::SendChannelReady { ref msg, .. } => {
19801				node_b.handle_channel_ready(node_a.get_our_node_id(), msg);
19802				get_event_msg!(node_b_holder, MessageSendEvent::SendChannelUpdate, node_a.get_our_node_id());
19803			},
19804			_ => panic!(),
19805		}
19806		match msg_events[1] {
19807			MessageSendEvent::SendChannelUpdate { .. } => {},
19808			_ => panic!(),
19809		}
19810
19811		let events_a = node_a.get_and_clear_pending_events();
19812		assert_eq!(events_a.len(), 1);
19813		match events_a[0] {
19814			Event::ChannelReady{ ref counterparty_node_id, .. } => {
19815				assert_eq!(*counterparty_node_id, node_b.get_our_node_id());
19816			},
19817			_ => panic!("Unexpected event"),
19818		}
19819
19820		let events_b = node_b.get_and_clear_pending_events();
19821		assert_eq!(events_b.len(), 1);
19822		match events_b[0] {
19823			Event::ChannelReady{ ref counterparty_node_id, .. } => {
19824				assert_eq!(*counterparty_node_id, node_a.get_our_node_id());
19825			},
19826			_ => panic!("Unexpected event"),
19827		}
19828
19829		let mut payment_count: u64 = 0;
19830		macro_rules! send_payment {
19831			($node_a: expr, $node_b: expr) => {
19832				let payment_params = PaymentParameters::from_node_id($node_b.get_our_node_id(), TEST_FINAL_CLTV)
19833					.with_bolt11_features($node_b.bolt11_invoice_features()).unwrap();
19834				let mut payment_preimage = PaymentPreimage([0; 32]);
19835				payment_preimage.0[0..8].copy_from_slice(&payment_count.to_le_bytes());
19836				payment_count += 1;
19837				let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).to_byte_array());
19838				let payment_secret = $node_b.create_inbound_payment_for_hash(payment_hash, None, 7200, None).unwrap();
19839
19840				$node_a.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
19841					PaymentId(payment_hash.0),
19842					RouteParameters::from_payment_params_and_value(payment_params, 10_000),
19843					Retry::Attempts(0)).unwrap();
19844				let payment_event = SendEvent::from_event($node_a.get_and_clear_pending_msg_events().pop().unwrap());
19845				$node_b.handle_update_add_htlc($node_a.get_our_node_id(), &payment_event.msgs[0]);
19846				$node_b.handle_commitment_signed_batch_test($node_a.get_our_node_id(), &payment_event.commitment_msg);
19847				let (raa, cs) = get_revoke_commit_msgs(&ANodeHolder { node: &$node_b }, &$node_a.get_our_node_id());
19848				$node_a.handle_revoke_and_ack($node_b.get_our_node_id(), &raa);
19849				$node_a.handle_commitment_signed_batch_test($node_b.get_our_node_id(), &cs);
19850				$node_b.handle_revoke_and_ack($node_a.get_our_node_id(), &get_event_msg!(ANodeHolder { node: &$node_a }, MessageSendEvent::SendRevokeAndACK, $node_b.get_our_node_id()));
19851
19852				$node_b.process_pending_htlc_forwards();
19853				expect_payment_claimable!(ANodeHolder { node: &$node_b }, payment_hash, payment_secret, 10_000);
19854				$node_b.claim_funds(payment_preimage);
19855				expect_payment_claimed!(ANodeHolder { node: &$node_b }, payment_hash, 10_000);
19856
19857				match $node_b.get_and_clear_pending_msg_events().pop().unwrap() {
19858					MessageSendEvent::UpdateHTLCs { node_id, mut updates, .. } => {
19859						assert_eq!(node_id, $node_a.get_our_node_id());
19860						let fulfill = updates.update_fulfill_htlcs.remove(0);
19861						$node_a.handle_update_fulfill_htlc($node_b.get_our_node_id(), fulfill);
19862						$node_a.handle_commitment_signed_batch_test($node_b.get_our_node_id(), &updates.commitment_signed);
19863					},
19864					_ => panic!("Failed to generate claim event"),
19865				}
19866
19867				let (raa, cs) = get_revoke_commit_msgs(&ANodeHolder { node: &$node_a }, &$node_b.get_our_node_id());
19868				$node_b.handle_revoke_and_ack($node_a.get_our_node_id(), &raa);
19869				$node_b.handle_commitment_signed_batch_test($node_a.get_our_node_id(), &cs);
19870				$node_a.handle_revoke_and_ack($node_b.get_our_node_id(), &get_event_msg!(ANodeHolder { node: &$node_b }, MessageSendEvent::SendRevokeAndACK, $node_a.get_our_node_id()));
19871
19872				expect_payment_sent!(ANodeHolder { node: &$node_a }, payment_preimage);
19873			}
19874		}
19875
19876		bench.bench_function(bench_name, |b| b.iter(|| {
19877			send_payment!(node_a, node_b);
19878			send_payment!(node_b, node_a);
19879		}));
19880	}
19881}