9620e793ec4a549f8aaebe14479330c7f14dce35
[rust-lightning] / lightning / src / 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
20 use bitcoin::blockdata::block::BlockHeader;
21 use bitcoin::blockdata::transaction::Transaction;
22 use bitcoin::blockdata::constants::ChainHash;
23 use bitcoin::network::constants::Network;
24
25 use bitcoin::hashes::Hash;
26 use bitcoin::hashes::sha256::Hash as Sha256;
27 use bitcoin::hash_types::{BlockHash, Txid};
28
29 use bitcoin::secp256k1::{SecretKey,PublicKey};
30 use bitcoin::secp256k1::Secp256k1;
31 use bitcoin::{LockTime, secp256k1, Sequence};
32
33 use crate::blinded_path::BlindedPath;
34 use crate::chain;
35 use crate::chain::{Confirm, ChannelMonitorUpdateStatus, Watch, BestBlock};
36 use crate::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator, LowerBoundedFeeEstimator};
37 use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent, CLOSED_CHANNEL_UPDATE_ID};
38 use crate::chain::transaction::{OutPoint, TransactionData};
39 use crate::events;
40 use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination, PaymentFailureReason};
41 // Since this struct is returned in `list_channels` methods, expose it here in case users want to
42 // construct one themselves.
43 use crate::ln::{inbound_payment, ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
44 use crate::ln::channel::{Channel, ChannelPhase, ChannelContext, ChannelError, ChannelUpdateStatus, ShutdownResult, UnfundedChannelContext, UpdateFulfillCommitFetch, OutboundV1Channel, InboundV1Channel};
45 use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
46 #[cfg(any(feature = "_test_utils", test))]
47 use crate::ln::features::Bolt11InvoiceFeatures;
48 use crate::routing::gossip::NetworkGraph;
49 use crate::routing::router::{BlindedTail, DefaultRouter, InFlightHtlcs, Path, Payee, PaymentParameters, Route, RouteParameters, Router};
50 use crate::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParameters};
51 use crate::ln::msgs;
52 use crate::ln::onion_utils;
53 use crate::ln::onion_utils::HTLCFailReason;
54 use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError};
55 #[cfg(test)]
56 use crate::ln::outbound_payment;
57 use crate::ln::outbound_payment::{OutboundPayments, PaymentAttempts, PendingOutboundPayment, SendAlongPathArgs};
58 use crate::ln::wire::Encode;
59 use crate::offers::offer::{DerivedMetadata, OfferBuilder};
60 use crate::offers::parse::Bolt12SemanticError;
61 use crate::offers::refund::RefundBuilder;
62 use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider, WriteableEcdsaChannelSigner};
63 use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
64 use crate::util::wakers::{Future, Notifier};
65 use crate::util::scid_utils::fake_scid;
66 use crate::util::string::UntrustedString;
67 use crate::util::ser::{BigSize, FixedLengthReader, Readable, ReadableArgs, MaybeReadable, Writeable, Writer, VecWriter};
68 use crate::util::logger::{Level, Logger};
69 use crate::util::errors::APIError;
70
71 use alloc::collections::{btree_map, BTreeMap};
72
73 use crate::io;
74 use crate::prelude::*;
75 use core::{cmp, mem};
76 use core::cell::RefCell;
77 use crate::io::Read;
78 use crate::sync::{Arc, Mutex, RwLock, RwLockReadGuard, FairRwLock, LockTestExt, LockHeldState};
79 use core::sync::atomic::{AtomicUsize, AtomicBool, Ordering};
80 use core::time::Duration;
81 use core::ops::Deref;
82
83 // Re-export this for use in the public API.
84 pub use crate::ln::outbound_payment::{PaymentSendFailure, ProbeSendFailure, Retry, RetryableSendFailure, RecipientOnionFields};
85 use crate::ln::script::ShutdownScript;
86
87 // We hold various information about HTLC relay in the HTLC objects in Channel itself:
88 //
89 // Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
90 // forward the HTLC with information it will give back to us when it does so, or if it should Fail
91 // the HTLC with the relevant message for the Channel to handle giving to the remote peer.
92 //
93 // Once said HTLC is committed in the Channel, if the PendingHTLCStatus indicated Forward, the
94 // Channel will return the PendingHTLCInfo back to us, and we will create an HTLCForwardInfo
95 // with it to track where it came from (in case of onwards-forward error), waiting a random delay
96 // before we forward it.
97 //
98 // We will then use HTLCForwardInfo's PendingHTLCInfo to construct an outbound HTLC, with a
99 // relevant HTLCSource::PreviousHopData filled in to indicate where it came from (which we can use
100 // to either fail-backwards or fulfill the HTLC backwards along the relevant path).
101 // Alternatively, we can fill an outbound HTLC with a HTLCSource::OutboundRoute indicating this is
102 // our payment, which we can use to decode errors or inform the user that the payment was sent.
103
104 #[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
105 pub(super) enum PendingHTLCRouting {
106         Forward {
107                 onion_packet: msgs::OnionPacket,
108                 /// The SCID from the onion that we should forward to. This could be a real SCID or a fake one
109                 /// generated using `get_fake_scid` from the scid_utils::fake_scid module.
110                 short_channel_id: u64, // This should be NonZero<u64> eventually when we bump MSRV
111         },
112         Receive {
113                 payment_data: msgs::FinalOnionHopData,
114                 payment_metadata: Option<Vec<u8>>,
115                 incoming_cltv_expiry: u32, // Used to track when we should expire pending HTLCs that go unclaimed
116                 phantom_shared_secret: Option<[u8; 32]>,
117                 /// See [`RecipientOnionFields::custom_tlvs`] for more info.
118                 custom_tlvs: Vec<(u64, Vec<u8>)>,
119         },
120         ReceiveKeysend {
121                 /// This was added in 0.0.116 and will break deserialization on downgrades.
122                 payment_data: Option<msgs::FinalOnionHopData>,
123                 payment_preimage: PaymentPreimage,
124                 payment_metadata: Option<Vec<u8>>,
125                 incoming_cltv_expiry: u32, // Used to track when we should expire pending HTLCs that go unclaimed
126                 /// See [`RecipientOnionFields::custom_tlvs`] for more info.
127                 custom_tlvs: Vec<(u64, Vec<u8>)>,
128         },
129 }
130
131 #[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
132 pub(super) struct PendingHTLCInfo {
133         pub(super) routing: PendingHTLCRouting,
134         pub(super) incoming_shared_secret: [u8; 32],
135         payment_hash: PaymentHash,
136         /// Amount received
137         pub(super) incoming_amt_msat: Option<u64>, // Added in 0.0.113
138         /// Sender intended amount to forward or receive (actual amount received
139         /// may overshoot this in either case)
140         pub(super) outgoing_amt_msat: u64,
141         pub(super) outgoing_cltv_value: u32,
142         /// The fee being skimmed off the top of this HTLC. If this is a forward, it'll be the fee we are
143         /// skimming. If we're receiving this HTLC, it's the fee that our counterparty skimmed.
144         pub(super) skimmed_fee_msat: Option<u64>,
145 }
146
147 #[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
148 pub(super) enum HTLCFailureMsg {
149         Relay(msgs::UpdateFailHTLC),
150         Malformed(msgs::UpdateFailMalformedHTLC),
151 }
152
153 /// Stores whether we can't forward an HTLC or relevant forwarding info
154 #[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
155 pub(super) enum PendingHTLCStatus {
156         Forward(PendingHTLCInfo),
157         Fail(HTLCFailureMsg),
158 }
159
160 pub(super) struct PendingAddHTLCInfo {
161         pub(super) forward_info: PendingHTLCInfo,
162
163         // These fields are produced in `forward_htlcs()` and consumed in
164         // `process_pending_htlc_forwards()` for constructing the
165         // `HTLCSource::PreviousHopData` for failed and forwarded
166         // HTLCs.
167         //
168         // Note that this may be an outbound SCID alias for the associated channel.
169         prev_short_channel_id: u64,
170         prev_htlc_id: u64,
171         prev_funding_outpoint: OutPoint,
172         prev_user_channel_id: u128,
173 }
174
175 pub(super) enum HTLCForwardInfo {
176         AddHTLC(PendingAddHTLCInfo),
177         FailHTLC {
178                 htlc_id: u64,
179                 err_packet: msgs::OnionErrorPacket,
180         },
181 }
182
183 /// Tracks the inbound corresponding to an outbound HTLC
184 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
185 pub(crate) struct HTLCPreviousHopData {
186         // Note that this may be an outbound SCID alias for the associated channel.
187         short_channel_id: u64,
188         user_channel_id: Option<u128>,
189         htlc_id: u64,
190         incoming_packet_shared_secret: [u8; 32],
191         phantom_shared_secret: Option<[u8; 32]>,
192
193         // This field is consumed by `claim_funds_from_hop()` when updating a force-closed backwards
194         // channel with a preimage provided by the forward channel.
195         outpoint: OutPoint,
196 }
197
198 enum OnionPayload {
199         /// Indicates this incoming onion payload is for the purpose of paying an invoice.
200         Invoice {
201                 /// This is only here for backwards-compatibility in serialization, in the future it can be
202                 /// removed, breaking clients running 0.0.106 and earlier.
203                 _legacy_hop_data: Option<msgs::FinalOnionHopData>,
204         },
205         /// Contains the payer-provided preimage.
206         Spontaneous(PaymentPreimage),
207 }
208
209 /// HTLCs that are to us and can be failed/claimed by the user
210 struct ClaimableHTLC {
211         prev_hop: HTLCPreviousHopData,
212         cltv_expiry: u32,
213         /// The amount (in msats) of this MPP part
214         value: u64,
215         /// The amount (in msats) that the sender intended to be sent in this MPP
216         /// part (used for validating total MPP amount)
217         sender_intended_value: u64,
218         onion_payload: OnionPayload,
219         timer_ticks: u8,
220         /// The total value received for a payment (sum of all MPP parts if the payment is a MPP).
221         /// Gets set to the amount reported when pushing [`Event::PaymentClaimable`].
222         total_value_received: Option<u64>,
223         /// The sender intended sum total of all MPP parts specified in the onion
224         total_msat: u64,
225         /// The extra fee our counterparty skimmed off the top of this HTLC.
226         counterparty_skimmed_fee_msat: Option<u64>,
227 }
228
229 impl From<&ClaimableHTLC> for events::ClaimedHTLC {
230         fn from(val: &ClaimableHTLC) -> Self {
231                 events::ClaimedHTLC {
232                         channel_id: val.prev_hop.outpoint.to_channel_id(),
233                         user_channel_id: val.prev_hop.user_channel_id.unwrap_or(0),
234                         cltv_expiry: val.cltv_expiry,
235                         value_msat: val.value,
236                 }
237         }
238 }
239
240 /// A user-provided identifier in [`ChannelManager::send_payment`] used to uniquely identify
241 /// a payment and ensure idempotency in LDK.
242 ///
243 /// This is not exported to bindings users as we just use [u8; 32] directly
244 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
245 pub struct PaymentId(pub [u8; Self::LENGTH]);
246
247 impl PaymentId {
248         /// Number of bytes in the id.
249         pub const LENGTH: usize = 32;
250 }
251
252 impl Writeable for PaymentId {
253         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
254                 self.0.write(w)
255         }
256 }
257
258 impl Readable for PaymentId {
259         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
260                 let buf: [u8; 32] = Readable::read(r)?;
261                 Ok(PaymentId(buf))
262         }
263 }
264
265 impl core::fmt::Display for PaymentId {
266         fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
267                 crate::util::logger::DebugBytes(&self.0).fmt(f)
268         }
269 }
270
271 /// An identifier used to uniquely identify an intercepted HTLC to LDK.
272 ///
273 /// This is not exported to bindings users as we just use [u8; 32] directly
274 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
275 pub struct InterceptId(pub [u8; 32]);
276
277 impl Writeable for InterceptId {
278         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
279                 self.0.write(w)
280         }
281 }
282
283 impl Readable for InterceptId {
284         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
285                 let buf: [u8; 32] = Readable::read(r)?;
286                 Ok(InterceptId(buf))
287         }
288 }
289
290 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
291 /// Uniquely describes an HTLC by its source. Just the guaranteed-unique subset of [`HTLCSource`].
292 pub(crate) enum SentHTLCId {
293         PreviousHopData { short_channel_id: u64, htlc_id: u64 },
294         OutboundRoute { session_priv: SecretKey },
295 }
296 impl SentHTLCId {
297         pub(crate) fn from_source(source: &HTLCSource) -> Self {
298                 match source {
299                         HTLCSource::PreviousHopData(hop_data) => Self::PreviousHopData {
300                                 short_channel_id: hop_data.short_channel_id,
301                                 htlc_id: hop_data.htlc_id,
302                         },
303                         HTLCSource::OutboundRoute { session_priv, .. } =>
304                                 Self::OutboundRoute { session_priv: *session_priv },
305                 }
306         }
307 }
308 impl_writeable_tlv_based_enum!(SentHTLCId,
309         (0, PreviousHopData) => {
310                 (0, short_channel_id, required),
311                 (2, htlc_id, required),
312         },
313         (2, OutboundRoute) => {
314                 (0, session_priv, required),
315         };
316 );
317
318
319 /// Tracks the inbound corresponding to an outbound HTLC
320 #[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
321 #[derive(Clone, Debug, PartialEq, Eq)]
322 pub(crate) enum HTLCSource {
323         PreviousHopData(HTLCPreviousHopData),
324         OutboundRoute {
325                 path: Path,
326                 session_priv: SecretKey,
327                 /// Technically we can recalculate this from the route, but we cache it here to avoid
328                 /// doing a double-pass on route when we get a failure back
329                 first_hop_htlc_msat: u64,
330                 payment_id: PaymentId,
331         },
332 }
333 #[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
334 impl core::hash::Hash for HTLCSource {
335         fn hash<H: core::hash::Hasher>(&self, hasher: &mut H) {
336                 match self {
337                         HTLCSource::PreviousHopData(prev_hop_data) => {
338                                 0u8.hash(hasher);
339                                 prev_hop_data.hash(hasher);
340                         },
341                         HTLCSource::OutboundRoute { path, session_priv, payment_id, first_hop_htlc_msat } => {
342                                 1u8.hash(hasher);
343                                 path.hash(hasher);
344                                 session_priv[..].hash(hasher);
345                                 payment_id.hash(hasher);
346                                 first_hop_htlc_msat.hash(hasher);
347                         },
348                 }
349         }
350 }
351 impl HTLCSource {
352         #[cfg(all(feature = "_test_vectors", not(feature = "grind_signatures")))]
353         #[cfg(test)]
354         pub fn dummy() -> Self {
355                 HTLCSource::OutboundRoute {
356                         path: Path { hops: Vec::new(), blinded_tail: None },
357                         session_priv: SecretKey::from_slice(&[1; 32]).unwrap(),
358                         first_hop_htlc_msat: 0,
359                         payment_id: PaymentId([2; 32]),
360                 }
361         }
362
363         #[cfg(debug_assertions)]
364         /// Checks whether this HTLCSource could possibly match the given HTLC output in a commitment
365         /// transaction. Useful to ensure different datastructures match up.
366         pub(crate) fn possibly_matches_output(&self, htlc: &super::chan_utils::HTLCOutputInCommitment) -> bool {
367                 if let HTLCSource::OutboundRoute { first_hop_htlc_msat, .. } = self {
368                         *first_hop_htlc_msat == htlc.amount_msat
369                 } else {
370                         // There's nothing we can check for forwarded HTLCs
371                         true
372                 }
373         }
374 }
375
376 struct InboundOnionErr {
377         err_code: u16,
378         err_data: Vec<u8>,
379         msg: &'static str,
380 }
381
382 /// This enum is used to specify which error data to send to peers when failing back an HTLC
383 /// using [`ChannelManager::fail_htlc_backwards_with_reason`].
384 ///
385 /// For more info on failure codes, see <https://github.com/lightning/bolts/blob/master/04-onion-routing.md#failure-messages>.
386 #[derive(Clone, Copy)]
387 pub enum FailureCode {
388         /// We had a temporary error processing the payment. Useful if no other error codes fit
389         /// and you want to indicate that the payer may want to retry.
390         TemporaryNodeFailure,
391         /// We have a required feature which was not in this onion. For example, you may require
392         /// some additional metadata that was not provided with this payment.
393         RequiredNodeFeatureMissing,
394         /// You may wish to use this when a `payment_preimage` is unknown, or the CLTV expiry of
395         /// the HTLC is too close to the current block height for safe handling.
396         /// Using this failure code in [`ChannelManager::fail_htlc_backwards_with_reason`] is
397         /// equivalent to calling [`ChannelManager::fail_htlc_backwards`].
398         IncorrectOrUnknownPaymentDetails,
399         /// We failed to process the payload after the onion was decrypted. You may wish to
400         /// use this when receiving custom HTLC TLVs with even type numbers that you don't recognize.
401         ///
402         /// If available, the tuple data may include the type number and byte offset in the
403         /// decrypted byte stream where the failure occurred.
404         InvalidOnionPayload(Option<(u64, u16)>),
405 }
406
407 impl Into<u16> for FailureCode {
408     fn into(self) -> u16 {
409                 match self {
410                         FailureCode::TemporaryNodeFailure => 0x2000 | 2,
411                         FailureCode::RequiredNodeFeatureMissing => 0x4000 | 0x2000 | 3,
412                         FailureCode::IncorrectOrUnknownPaymentDetails => 0x4000 | 15,
413                         FailureCode::InvalidOnionPayload(_) => 0x4000 | 22,
414                 }
415         }
416 }
417
418 /// Error type returned across the peer_state mutex boundary. When an Err is generated for a
419 /// Channel, we generally end up with a ChannelError::Close for which we have to close the channel
420 /// immediately (ie with no further calls on it made). Thus, this step happens inside a
421 /// peer_state lock. We then return the set of things that need to be done outside the lock in
422 /// this struct and call handle_error!() on it.
423
424 struct MsgHandleErrInternal {
425         err: msgs::LightningError,
426         chan_id: Option<(ChannelId, u128)>, // If Some a channel of ours has been closed
427         shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
428         channel_capacity: Option<u64>,
429 }
430 impl MsgHandleErrInternal {
431         #[inline]
432         fn send_err_msg_no_close(err: String, channel_id: ChannelId) -> Self {
433                 Self {
434                         err: LightningError {
435                                 err: err.clone(),
436                                 action: msgs::ErrorAction::SendErrorMessage {
437                                         msg: msgs::ErrorMessage {
438                                                 channel_id,
439                                                 data: err
440                                         },
441                                 },
442                         },
443                         chan_id: None,
444                         shutdown_finish: None,
445                         channel_capacity: None,
446                 }
447         }
448         #[inline]
449         fn from_no_close(err: msgs::LightningError) -> Self {
450                 Self { err, chan_id: None, shutdown_finish: None, channel_capacity: None }
451         }
452         #[inline]
453         fn from_finish_shutdown(err: String, channel_id: ChannelId, user_channel_id: u128, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>, channel_capacity: u64) -> Self {
454                 let err_msg = msgs::ErrorMessage { channel_id, data: err.clone() };
455                 let action = if shutdown_res.monitor_update.is_some() {
456                         // We have a closing `ChannelMonitorUpdate`, which means the channel was funded and we
457                         // should disconnect our peer such that we force them to broadcast their latest
458                         // commitment upon reconnecting.
459                         msgs::ErrorAction::DisconnectPeer { msg: Some(err_msg) }
460                 } else {
461                         msgs::ErrorAction::SendErrorMessage { msg: err_msg }
462                 };
463                 Self {
464                         err: LightningError { err, action },
465                         chan_id: Some((channel_id, user_channel_id)),
466                         shutdown_finish: Some((shutdown_res, channel_update)),
467                         channel_capacity: Some(channel_capacity)
468                 }
469         }
470         #[inline]
471         fn from_chan_no_close(err: ChannelError, channel_id: ChannelId) -> Self {
472                 Self {
473                         err: match err {
474                                 ChannelError::Warn(msg) =>  LightningError {
475                                         err: msg.clone(),
476                                         action: msgs::ErrorAction::SendWarningMessage {
477                                                 msg: msgs::WarningMessage {
478                                                         channel_id,
479                                                         data: msg
480                                                 },
481                                                 log_level: Level::Warn,
482                                         },
483                                 },
484                                 ChannelError::Ignore(msg) => LightningError {
485                                         err: msg,
486                                         action: msgs::ErrorAction::IgnoreError,
487                                 },
488                                 ChannelError::Close(msg) => LightningError {
489                                         err: msg.clone(),
490                                         action: msgs::ErrorAction::SendErrorMessage {
491                                                 msg: msgs::ErrorMessage {
492                                                         channel_id,
493                                                         data: msg
494                                                 },
495                                         },
496                                 },
497                         },
498                         chan_id: None,
499                         shutdown_finish: None,
500                         channel_capacity: None,
501                 }
502         }
503
504         fn closes_channel(&self) -> bool {
505                 self.chan_id.is_some()
506         }
507 }
508
509 /// We hold back HTLCs we intend to relay for a random interval greater than this (see
510 /// Event::PendingHTLCsForwardable for the API guidelines indicating how long should be waited).
511 /// This provides some limited amount of privacy. Ideally this would range from somewhere like one
512 /// second to 30 seconds, but people expect lightning to be, you know, kinda fast, sadly.
513 pub(super) const MIN_HTLC_RELAY_HOLDING_CELL_MILLIS: u64 = 100;
514
515 /// For events which result in both a RevokeAndACK and a CommitmentUpdate, by default they should
516 /// be sent in the order they appear in the return value, however sometimes the order needs to be
517 /// variable at runtime (eg Channel::channel_reestablish needs to re-send messages in the order
518 /// they were originally sent). In those cases, this enum is also returned.
519 #[derive(Clone, PartialEq)]
520 pub(super) enum RAACommitmentOrder {
521         /// Send the CommitmentUpdate messages first
522         CommitmentFirst,
523         /// Send the RevokeAndACK message first
524         RevokeAndACKFirst,
525 }
526
527 /// Information about a payment which is currently being claimed.
528 struct ClaimingPayment {
529         amount_msat: u64,
530         payment_purpose: events::PaymentPurpose,
531         receiver_node_id: PublicKey,
532         htlcs: Vec<events::ClaimedHTLC>,
533         sender_intended_value: Option<u64>,
534 }
535 impl_writeable_tlv_based!(ClaimingPayment, {
536         (0, amount_msat, required),
537         (2, payment_purpose, required),
538         (4, receiver_node_id, required),
539         (5, htlcs, optional_vec),
540         (7, sender_intended_value, option),
541 });
542
543 struct ClaimablePayment {
544         purpose: events::PaymentPurpose,
545         onion_fields: Option<RecipientOnionFields>,
546         htlcs: Vec<ClaimableHTLC>,
547 }
548
549 /// Information about claimable or being-claimed payments
550 struct ClaimablePayments {
551         /// Map from payment hash to the payment data and any HTLCs which are to us and can be
552         /// failed/claimed by the user.
553         ///
554         /// Note that, no consistency guarantees are made about the channels given here actually
555         /// existing anymore by the time you go to read them!
556         ///
557         /// When adding to the map, [`Self::pending_claiming_payments`] must also be checked to ensure
558         /// we don't get a duplicate payment.
559         claimable_payments: HashMap<PaymentHash, ClaimablePayment>,
560
561         /// Map from payment hash to the payment data for HTLCs which we have begun claiming, but which
562         /// are waiting on a [`ChannelMonitorUpdate`] to complete in order to be surfaced to the user
563         /// as an [`events::Event::PaymentClaimed`].
564         pending_claiming_payments: HashMap<PaymentHash, ClaimingPayment>,
565 }
566
567 /// Events which we process internally but cannot be processed immediately at the generation site
568 /// usually because we're running pre-full-init. They are handled immediately once we detect we are
569 /// running normally, and specifically must be processed before any other non-background
570 /// [`ChannelMonitorUpdate`]s are applied.
571 enum BackgroundEvent {
572         /// Handle a ChannelMonitorUpdate which closes the channel or for an already-closed channel.
573         /// This is only separated from [`Self::MonitorUpdateRegeneratedOnStartup`] as the
574         /// maybe-non-closing variant needs a public key to handle channel resumption, whereas if the
575         /// channel has been force-closed we do not need the counterparty node_id.
576         ///
577         /// Note that any such events are lost on shutdown, so in general they must be updates which
578         /// are regenerated on startup.
579         ClosedMonitorUpdateRegeneratedOnStartup((OutPoint, ChannelMonitorUpdate)),
580         /// Handle a ChannelMonitorUpdate which may or may not close the channel and may unblock the
581         /// channel to continue normal operation.
582         ///
583         /// In general this should be used rather than
584         /// [`Self::ClosedMonitorUpdateRegeneratedOnStartup`], however in cases where the
585         /// `counterparty_node_id` is not available as the channel has closed from a [`ChannelMonitor`]
586         /// error the other variant is acceptable.
587         ///
588         /// Note that any such events are lost on shutdown, so in general they must be updates which
589         /// are regenerated on startup.
590         MonitorUpdateRegeneratedOnStartup {
591                 counterparty_node_id: PublicKey,
592                 funding_txo: OutPoint,
593                 update: ChannelMonitorUpdate
594         },
595         /// Some [`ChannelMonitorUpdate`] (s) completed before we were serialized but we still have
596         /// them marked pending, thus we need to run any [`MonitorUpdateCompletionAction`] (s) pending
597         /// on a channel.
598         MonitorUpdatesComplete {
599                 counterparty_node_id: PublicKey,
600                 channel_id: ChannelId,
601         },
602 }
603
604 #[derive(Debug)]
605 pub(crate) enum MonitorUpdateCompletionAction {
606         /// Indicates that a payment ultimately destined for us was claimed and we should emit an
607         /// [`events::Event::PaymentClaimed`] to the user if we haven't yet generated such an event for
608         /// this payment. Note that this is only best-effort. On restart it's possible such a duplicate
609         /// event can be generated.
610         PaymentClaimed { payment_hash: PaymentHash },
611         /// Indicates an [`events::Event`] should be surfaced to the user and possibly resume the
612         /// operation of another channel.
613         ///
614         /// This is usually generated when we've forwarded an HTLC and want to block the outbound edge
615         /// from completing a monitor update which removes the payment preimage until the inbound edge
616         /// completes a monitor update containing the payment preimage. In that case, after the inbound
617         /// edge completes, we will surface an [`Event::PaymentForwarded`] as well as unblock the
618         /// outbound edge.
619         EmitEventAndFreeOtherChannel {
620                 event: events::Event,
621                 downstream_counterparty_and_funding_outpoint: Option<(PublicKey, OutPoint, RAAMonitorUpdateBlockingAction)>,
622         },
623 }
624
625 impl_writeable_tlv_based_enum_upgradable!(MonitorUpdateCompletionAction,
626         (0, PaymentClaimed) => { (0, payment_hash, required) },
627         (2, EmitEventAndFreeOtherChannel) => {
628                 (0, event, upgradable_required),
629                 // LDK prior to 0.0.116 did not have this field as the monitor update application order was
630                 // required by clients. If we downgrade to something prior to 0.0.116 this may result in
631                 // monitor updates which aren't properly blocked or resumed, however that's fine - we don't
632                 // support async monitor updates even in LDK 0.0.116 and once we do we'll require no
633                 // downgrades to prior versions.
634                 (1, downstream_counterparty_and_funding_outpoint, option),
635         },
636 );
637
638 #[derive(Clone, Debug, PartialEq, Eq)]
639 pub(crate) enum EventCompletionAction {
640         ReleaseRAAChannelMonitorUpdate {
641                 counterparty_node_id: PublicKey,
642                 channel_funding_outpoint: OutPoint,
643         },
644 }
645 impl_writeable_tlv_based_enum!(EventCompletionAction,
646         (0, ReleaseRAAChannelMonitorUpdate) => {
647                 (0, channel_funding_outpoint, required),
648                 (2, counterparty_node_id, required),
649         };
650 );
651
652 #[derive(Clone, PartialEq, Eq, Debug)]
653 /// If something is blocked on the completion of an RAA-generated [`ChannelMonitorUpdate`] we track
654 /// the blocked action here. See enum variants for more info.
655 pub(crate) enum RAAMonitorUpdateBlockingAction {
656         /// A forwarded payment was claimed. We block the downstream channel completing its monitor
657         /// update which removes the HTLC preimage until the upstream channel has gotten the preimage
658         /// durably to disk.
659         ForwardedPaymentInboundClaim {
660                 /// The upstream channel ID (i.e. the inbound edge).
661                 channel_id: ChannelId,
662                 /// The HTLC ID on the inbound edge.
663                 htlc_id: u64,
664         },
665 }
666
667 impl RAAMonitorUpdateBlockingAction {
668         fn from_prev_hop_data(prev_hop: &HTLCPreviousHopData) -> Self {
669                 Self::ForwardedPaymentInboundClaim {
670                         channel_id: prev_hop.outpoint.to_channel_id(),
671                         htlc_id: prev_hop.htlc_id,
672                 }
673         }
674 }
675
676 impl_writeable_tlv_based_enum!(RAAMonitorUpdateBlockingAction,
677         (0, ForwardedPaymentInboundClaim) => { (0, channel_id, required), (2, htlc_id, required) }
678 ;);
679
680
681 /// State we hold per-peer.
682 pub(super) struct PeerState<SP: Deref> where SP::Target: SignerProvider {
683         /// `channel_id` -> `ChannelPhase`
684         ///
685         /// Holds all channels within corresponding `ChannelPhase`s where the peer is the counterparty.
686         pub(super) channel_by_id: HashMap<ChannelId, ChannelPhase<SP>>,
687         /// `temporary_channel_id` -> `InboundChannelRequest`.
688         ///
689         /// When manual channel acceptance is enabled, this holds all unaccepted inbound channels where
690         /// the peer is the counterparty. If the channel is accepted, then the entry in this table is
691         /// removed, and an InboundV1Channel is created and placed in the `inbound_v1_channel_by_id` table. If
692         /// the channel is rejected, then the entry is simply removed.
693         pub(super) inbound_channel_request_by_id: HashMap<ChannelId, InboundChannelRequest>,
694         /// The latest `InitFeatures` we heard from the peer.
695         latest_features: InitFeatures,
696         /// Messages to send to the peer - pushed to in the same lock that they are generated in (except
697         /// for broadcast messages, where ordering isn't as strict).
698         pub(super) pending_msg_events: Vec<MessageSendEvent>,
699         /// Map from Channel IDs to pending [`ChannelMonitorUpdate`]s which have been passed to the
700         /// user but which have not yet completed.
701         ///
702         /// Note that the channel may no longer exist. For example if the channel was closed but we
703         /// later needed to claim an HTLC which is pending on-chain, we may generate a monitor update
704         /// for a missing channel.
705         in_flight_monitor_updates: BTreeMap<OutPoint, Vec<ChannelMonitorUpdate>>,
706         /// Map from a specific channel to some action(s) that should be taken when all pending
707         /// [`ChannelMonitorUpdate`]s for the channel complete updating.
708         ///
709         /// Note that because we generally only have one entry here a HashMap is pretty overkill. A
710         /// BTreeMap currently stores more than ten elements per leaf node, so even up to a few
711         /// channels with a peer this will just be one allocation and will amount to a linear list of
712         /// channels to walk, avoiding the whole hashing rigmarole.
713         ///
714         /// Note that the channel may no longer exist. For example, if a channel was closed but we
715         /// later needed to claim an HTLC which is pending on-chain, we may generate a monitor update
716         /// for a missing channel. While a malicious peer could construct a second channel with the
717         /// same `temporary_channel_id` (or final `channel_id` in the case of 0conf channels or prior
718         /// to funding appearing on-chain), the downstream `ChannelMonitor` set is required to ensure
719         /// duplicates do not occur, so such channels should fail without a monitor update completing.
720         monitor_update_blocked_actions: BTreeMap<ChannelId, Vec<MonitorUpdateCompletionAction>>,
721         /// If another channel's [`ChannelMonitorUpdate`] needs to complete before a channel we have
722         /// with this peer can complete an RAA [`ChannelMonitorUpdate`] (e.g. because the RAA update
723         /// will remove a preimage that needs to be durably in an upstream channel first), we put an
724         /// entry here to note that the channel with the key's ID is blocked on a set of actions.
725         actions_blocking_raa_monitor_updates: BTreeMap<ChannelId, Vec<RAAMonitorUpdateBlockingAction>>,
726         /// The peer is currently connected (i.e. we've seen a
727         /// [`ChannelMessageHandler::peer_connected`] and no corresponding
728         /// [`ChannelMessageHandler::peer_disconnected`].
729         is_connected: bool,
730 }
731
732 impl <SP: Deref> PeerState<SP> where SP::Target: SignerProvider {
733         /// Indicates that a peer meets the criteria where we're ok to remove it from our storage.
734         /// If true is passed for `require_disconnected`, the function will return false if we haven't
735         /// disconnected from the node already, ie. `PeerState::is_connected` is set to `true`.
736         fn ok_to_remove(&self, require_disconnected: bool) -> bool {
737                 if require_disconnected && self.is_connected {
738                         return false
739                 }
740                 self.channel_by_id.iter().filter(|(_, phase)| matches!(phase, ChannelPhase::Funded(_))).count() == 0
741                         && self.monitor_update_blocked_actions.is_empty()
742                         && self.in_flight_monitor_updates.is_empty()
743         }
744
745         // Returns a count of all channels we have with this peer, including unfunded channels.
746         fn total_channel_count(&self) -> usize {
747                 self.channel_by_id.len() + self.inbound_channel_request_by_id.len()
748         }
749
750         // Returns a bool indicating if the given `channel_id` matches a channel we have with this peer.
751         fn has_channel(&self, channel_id: &ChannelId) -> bool {
752                 self.channel_by_id.contains_key(channel_id) ||
753                         self.inbound_channel_request_by_id.contains_key(channel_id)
754         }
755 }
756
757 /// A not-yet-accepted inbound (from counterparty) channel. Once
758 /// accepted, the parameters will be used to construct a channel.
759 pub(super) struct InboundChannelRequest {
760         /// The original OpenChannel message.
761         pub open_channel_msg: msgs::OpenChannel,
762         /// The number of ticks remaining before the request expires.
763         pub ticks_remaining: i32,
764 }
765
766 /// The number of ticks that may elapse while we're waiting for an unaccepted inbound channel to be
767 /// accepted. An unaccepted channel that exceeds this limit will be abandoned.
768 const UNACCEPTED_INBOUND_CHANNEL_AGE_LIMIT_TICKS: i32 = 2;
769
770 /// Stores a PaymentSecret and any other data we may need to validate an inbound payment is
771 /// actually ours and not some duplicate HTLC sent to us by a node along the route.
772 ///
773 /// For users who don't want to bother doing their own payment preimage storage, we also store that
774 /// here.
775 ///
776 /// Note that this struct will be removed entirely soon, in favor of storing no inbound payment data
777 /// and instead encoding it in the payment secret.
778 struct PendingInboundPayment {
779         /// The payment secret that the sender must use for us to accept this payment
780         payment_secret: PaymentSecret,
781         /// Time at which this HTLC expires - blocks with a header time above this value will result in
782         /// this payment being removed.
783         expiry_time: u64,
784         /// Arbitrary identifier the user specifies (or not)
785         user_payment_id: u64,
786         // Other required attributes of the payment, optionally enforced:
787         payment_preimage: Option<PaymentPreimage>,
788         min_value_msat: Option<u64>,
789 }
790
791 /// [`SimpleArcChannelManager`] is useful when you need a [`ChannelManager`] with a static lifetime, e.g.
792 /// when you're using `lightning-net-tokio` (since `tokio::spawn` requires parameters with static
793 /// lifetimes). Other times you can afford a reference, which is more efficient, in which case
794 /// [`SimpleRefChannelManager`] is the more appropriate type. Defining these type aliases prevents
795 /// issues such as overly long function definitions. Note that the `ChannelManager` can take any type
796 /// that implements [`NodeSigner`], [`EntropySource`], and [`SignerProvider`] for its keys manager,
797 /// or, respectively, [`Router`] for its router, but this type alias chooses the concrete types
798 /// of [`KeysManager`] and [`DefaultRouter`].
799 ///
800 /// This is not exported to bindings users as Arcs don't make sense in bindings
801 pub type SimpleArcChannelManager<M, T, F, L> = ChannelManager<
802         Arc<M>,
803         Arc<T>,
804         Arc<KeysManager>,
805         Arc<KeysManager>,
806         Arc<KeysManager>,
807         Arc<F>,
808         Arc<DefaultRouter<
809                 Arc<NetworkGraph<Arc<L>>>,
810                 Arc<L>,
811                 Arc<RwLock<ProbabilisticScorer<Arc<NetworkGraph<Arc<L>>>, Arc<L>>>>,
812                 ProbabilisticScoringFeeParameters,
813                 ProbabilisticScorer<Arc<NetworkGraph<Arc<L>>>, Arc<L>>,
814         >>,
815         Arc<L>
816 >;
817
818 /// [`SimpleRefChannelManager`] is a type alias for a ChannelManager reference, and is the reference
819 /// counterpart to the [`SimpleArcChannelManager`] type alias. Use this type by default when you don't
820 /// need a ChannelManager with a static lifetime. You'll need a static lifetime in cases such as
821 /// usage of lightning-net-tokio (since `tokio::spawn` requires parameters with static lifetimes).
822 /// But if this is not necessary, using a reference is more efficient. Defining these type aliases
823 /// issues such as overly long function definitions. Note that the ChannelManager can take any type
824 /// that implements [`NodeSigner`], [`EntropySource`], and [`SignerProvider`] for its keys manager,
825 /// or, respectively, [`Router`]  for its router, but this type alias chooses the concrete types
826 /// of [`KeysManager`] and [`DefaultRouter`].
827 ///
828 /// This is not exported to bindings users as Arcs don't make sense in bindings
829 pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L> =
830         ChannelManager<
831                 &'a M,
832                 &'b T,
833                 &'c KeysManager,
834                 &'c KeysManager,
835                 &'c KeysManager,
836                 &'d F,
837                 &'e DefaultRouter<
838                         &'f NetworkGraph<&'g L>,
839                         &'g L,
840                         &'h RwLock<ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>>,
841                         ProbabilisticScoringFeeParameters,
842                         ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>
843                 >,
844                 &'g L
845         >;
846
847 /// A trivial trait which describes any [`ChannelManager`].
848 ///
849 /// This is not exported to bindings users as general cover traits aren't useful in other
850 /// languages.
851 pub trait AChannelManager {
852         /// A type implementing [`chain::Watch`].
853         type Watch: chain::Watch<Self::Signer> + ?Sized;
854         /// A type that may be dereferenced to [`Self::Watch`].
855         type M: Deref<Target = Self::Watch>;
856         /// A type implementing [`BroadcasterInterface`].
857         type Broadcaster: BroadcasterInterface + ?Sized;
858         /// A type that may be dereferenced to [`Self::Broadcaster`].
859         type T: Deref<Target = Self::Broadcaster>;
860         /// A type implementing [`EntropySource`].
861         type EntropySource: EntropySource + ?Sized;
862         /// A type that may be dereferenced to [`Self::EntropySource`].
863         type ES: Deref<Target = Self::EntropySource>;
864         /// A type implementing [`NodeSigner`].
865         type NodeSigner: NodeSigner + ?Sized;
866         /// A type that may be dereferenced to [`Self::NodeSigner`].
867         type NS: Deref<Target = Self::NodeSigner>;
868         /// A type implementing [`WriteableEcdsaChannelSigner`].
869         type Signer: WriteableEcdsaChannelSigner + Sized;
870         /// A type implementing [`SignerProvider`] for [`Self::Signer`].
871         type SignerProvider: SignerProvider<Signer = Self::Signer> + ?Sized;
872         /// A type that may be dereferenced to [`Self::SignerProvider`].
873         type SP: Deref<Target = Self::SignerProvider>;
874         /// A type implementing [`FeeEstimator`].
875         type FeeEstimator: FeeEstimator + ?Sized;
876         /// A type that may be dereferenced to [`Self::FeeEstimator`].
877         type F: Deref<Target = Self::FeeEstimator>;
878         /// A type implementing [`Router`].
879         type Router: Router + ?Sized;
880         /// A type that may be dereferenced to [`Self::Router`].
881         type R: Deref<Target = Self::Router>;
882         /// A type implementing [`Logger`].
883         type Logger: Logger + ?Sized;
884         /// A type that may be dereferenced to [`Self::Logger`].
885         type L: Deref<Target = Self::Logger>;
886         /// Returns a reference to the actual [`ChannelManager`] object.
887         fn get_cm(&self) -> &ChannelManager<Self::M, Self::T, Self::ES, Self::NS, Self::SP, Self::F, Self::R, Self::L>;
888 }
889
890 impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> AChannelManager
891 for ChannelManager<M, T, ES, NS, SP, F, R, L>
892 where
893         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
894         T::Target: BroadcasterInterface,
895         ES::Target: EntropySource,
896         NS::Target: NodeSigner,
897         SP::Target: SignerProvider,
898         F::Target: FeeEstimator,
899         R::Target: Router,
900         L::Target: Logger,
901 {
902         type Watch = M::Target;
903         type M = M;
904         type Broadcaster = T::Target;
905         type T = T;
906         type EntropySource = ES::Target;
907         type ES = ES;
908         type NodeSigner = NS::Target;
909         type NS = NS;
910         type Signer = <SP::Target as SignerProvider>::Signer;
911         type SignerProvider = SP::Target;
912         type SP = SP;
913         type FeeEstimator = F::Target;
914         type F = F;
915         type Router = R::Target;
916         type R = R;
917         type Logger = L::Target;
918         type L = L;
919         fn get_cm(&self) -> &ChannelManager<M, T, ES, NS, SP, F, R, L> { self }
920 }
921
922 /// Manager which keeps track of a number of channels and sends messages to the appropriate
923 /// channel, also tracking HTLC preimages and forwarding onion packets appropriately.
924 ///
925 /// Implements [`ChannelMessageHandler`], handling the multi-channel parts and passing things through
926 /// to individual Channels.
927 ///
928 /// Implements [`Writeable`] to write out all channel state to disk. Implies [`peer_disconnected`] for
929 /// all peers during write/read (though does not modify this instance, only the instance being
930 /// serialized). This will result in any channels which have not yet exchanged [`funding_created`] (i.e.,
931 /// called [`funding_transaction_generated`] for outbound channels) being closed.
932 ///
933 /// Note that you can be a bit lazier about writing out `ChannelManager` than you can be with
934 /// [`ChannelMonitor`]. With [`ChannelMonitor`] you MUST durably write each
935 /// [`ChannelMonitorUpdate`] before returning from
936 /// [`chain::Watch::watch_channel`]/[`update_channel`] or before completing async writes. With
937 /// `ChannelManager`s, writing updates happens out-of-band (and will prevent any other
938 /// `ChannelManager` operations from occurring during the serialization process). If the
939 /// deserialized version is out-of-date compared to the [`ChannelMonitor`] passed by reference to
940 /// [`read`], those channels will be force-closed based on the `ChannelMonitor` state and no funds
941 /// will be lost (modulo on-chain transaction fees).
942 ///
943 /// Note that the deserializer is only implemented for `(`[`BlockHash`]`, `[`ChannelManager`]`)`, which
944 /// tells you the last block hash which was connected. You should get the best block tip before using the manager.
945 /// See [`chain::Listen`] and [`chain::Confirm`] for more details.
946 ///
947 /// Note that `ChannelManager` is responsible for tracking liveness of its channels and generating
948 /// [`ChannelUpdate`] messages informing peers that the channel is temporarily disabled. To avoid
949 /// spam due to quick disconnection/reconnection, updates are not sent until the channel has been
950 /// offline for a full minute. In order to track this, you must call
951 /// [`timer_tick_occurred`] roughly once per minute, though it doesn't have to be perfect.
952 ///
953 /// To avoid trivial DoS issues, `ChannelManager` limits the number of inbound connections and
954 /// inbound channels without confirmed funding transactions. This may result in nodes which we do
955 /// not have a channel with being unable to connect to us or open new channels with us if we have
956 /// many peers with unfunded channels.
957 ///
958 /// Because it is an indication of trust, inbound channels which we've accepted as 0conf are
959 /// exempted from the count of unfunded channels. Similarly, outbound channels and connections are
960 /// never limited. Please ensure you limit the count of such channels yourself.
961 ///
962 /// Rather than using a plain `ChannelManager`, it is preferable to use either a [`SimpleArcChannelManager`]
963 /// a [`SimpleRefChannelManager`], for conciseness. See their documentation for more details, but
964 /// essentially you should default to using a [`SimpleRefChannelManager`], and use a
965 /// [`SimpleArcChannelManager`] when you require a `ChannelManager` with a static lifetime, such as when
966 /// you're using lightning-net-tokio.
967 ///
968 /// [`peer_disconnected`]: msgs::ChannelMessageHandler::peer_disconnected
969 /// [`funding_created`]: msgs::FundingCreated
970 /// [`funding_transaction_generated`]: Self::funding_transaction_generated
971 /// [`BlockHash`]: bitcoin::hash_types::BlockHash
972 /// [`update_channel`]: chain::Watch::update_channel
973 /// [`ChannelUpdate`]: msgs::ChannelUpdate
974 /// [`timer_tick_occurred`]: Self::timer_tick_occurred
975 /// [`read`]: ReadableArgs::read
976 //
977 // Lock order:
978 // The tree structure below illustrates the lock order requirements for the different locks of the
979 // `ChannelManager`. Locks can be held at the same time if they are on the same branch in the tree,
980 // and should then be taken in the order of the lowest to the highest level in the tree.
981 // Note that locks on different branches shall not be taken at the same time, as doing so will
982 // create a new lock order for those specific locks in the order they were taken.
983 //
984 // Lock order tree:
985 //
986 // `total_consistency_lock`
987 //  |
988 //  |__`forward_htlcs`
989 //  |   |
990 //  |   |__`pending_intercepted_htlcs`
991 //  |
992 //  |__`per_peer_state`
993 //  |   |
994 //  |   |__`pending_inbound_payments`
995 //  |       |
996 //  |       |__`claimable_payments`
997 //  |       |
998 //  |       |__`pending_outbound_payments` // This field's struct contains a map of pending outbounds
999 //  |           |
1000 //  |           |__`peer_state`
1001 //  |               |
1002 //  |               |__`id_to_peer`
1003 //  |               |
1004 //  |               |__`short_to_chan_info`
1005 //  |               |
1006 //  |               |__`outbound_scid_aliases`
1007 //  |               |
1008 //  |               |__`best_block`
1009 //  |               |
1010 //  |               |__`pending_events`
1011 //  |                   |
1012 //  |                   |__`pending_background_events`
1013 //
1014 pub struct ChannelManager<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>
1015 where
1016         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
1017         T::Target: BroadcasterInterface,
1018         ES::Target: EntropySource,
1019         NS::Target: NodeSigner,
1020         SP::Target: SignerProvider,
1021         F::Target: FeeEstimator,
1022         R::Target: Router,
1023         L::Target: Logger,
1024 {
1025         default_configuration: UserConfig,
1026         chain_hash: ChainHash,
1027         fee_estimator: LowerBoundedFeeEstimator<F>,
1028         chain_monitor: M,
1029         tx_broadcaster: T,
1030         #[allow(unused)]
1031         router: R,
1032
1033         /// See `ChannelManager` struct-level documentation for lock order requirements.
1034         #[cfg(test)]
1035         pub(super) best_block: RwLock<BestBlock>,
1036         #[cfg(not(test))]
1037         best_block: RwLock<BestBlock>,
1038         secp_ctx: Secp256k1<secp256k1::All>,
1039
1040         /// Storage for PaymentSecrets and any requirements on future inbound payments before we will
1041         /// expose them to users via a PaymentClaimable event. HTLCs which do not meet the requirements
1042         /// here are failed when we process them as pending-forwardable-HTLCs, and entries are removed
1043         /// after we generate a PaymentClaimable upon receipt of all MPP parts or when they time out.
1044         ///
1045         /// See `ChannelManager` struct-level documentation for lock order requirements.
1046         pending_inbound_payments: Mutex<HashMap<PaymentHash, PendingInboundPayment>>,
1047
1048         /// The session_priv bytes and retry metadata of outbound payments which are pending resolution.
1049         /// The authoritative state of these HTLCs resides either within Channels or ChannelMonitors
1050         /// (if the channel has been force-closed), however we track them here to prevent duplicative
1051         /// PaymentSent/PaymentPathFailed events. Specifically, in the case of a duplicative
1052         /// update_fulfill_htlc message after a reconnect, we may "claim" a payment twice.
1053         /// Additionally, because ChannelMonitors are often not re-serialized after connecting block(s)
1054         /// which may generate a claim event, we may receive similar duplicate claim/fail MonitorEvents
1055         /// after reloading from disk while replaying blocks against ChannelMonitors.
1056         ///
1057         /// See `PendingOutboundPayment` documentation for more info.
1058         ///
1059         /// See `ChannelManager` struct-level documentation for lock order requirements.
1060         pending_outbound_payments: OutboundPayments,
1061
1062         /// SCID/SCID Alias -> forward infos. Key of 0 means payments received.
1063         ///
1064         /// Note that because we may have an SCID Alias as the key we can have two entries per channel,
1065         /// though in practice we probably won't be receiving HTLCs for a channel both via the alias
1066         /// and via the classic SCID.
1067         ///
1068         /// Note that no consistency guarantees are made about the existence of a channel with the
1069         /// `short_channel_id` here, nor the `short_channel_id` in the `PendingHTLCInfo`!
1070         ///
1071         /// See `ChannelManager` struct-level documentation for lock order requirements.
1072         #[cfg(test)]
1073         pub(super) forward_htlcs: Mutex<HashMap<u64, Vec<HTLCForwardInfo>>>,
1074         #[cfg(not(test))]
1075         forward_htlcs: Mutex<HashMap<u64, Vec<HTLCForwardInfo>>>,
1076         /// Storage for HTLCs that have been intercepted and bubbled up to the user. We hold them here
1077         /// until the user tells us what we should do with them.
1078         ///
1079         /// See `ChannelManager` struct-level documentation for lock order requirements.
1080         pending_intercepted_htlcs: Mutex<HashMap<InterceptId, PendingAddHTLCInfo>>,
1081
1082         /// The sets of payments which are claimable or currently being claimed. See
1083         /// [`ClaimablePayments`]' individual field docs for more info.
1084         ///
1085         /// See `ChannelManager` struct-level documentation for lock order requirements.
1086         claimable_payments: Mutex<ClaimablePayments>,
1087
1088         /// The set of outbound SCID aliases across all our channels, including unconfirmed channels
1089         /// and some closed channels which reached a usable state prior to being closed. This is used
1090         /// only to avoid duplicates, and is not persisted explicitly to disk, but rebuilt from the
1091         /// active channel list on load.
1092         ///
1093         /// See `ChannelManager` struct-level documentation for lock order requirements.
1094         outbound_scid_aliases: Mutex<HashSet<u64>>,
1095
1096         /// `channel_id` -> `counterparty_node_id`.
1097         ///
1098         /// Only `channel_id`s are allowed as keys in this map, and not `temporary_channel_id`s. As
1099         /// multiple channels with the same `temporary_channel_id` to different peers can exist,
1100         /// allowing `temporary_channel_id`s in this map would cause collisions for such channels.
1101         ///
1102         /// Note that this map should only be used for `MonitorEvent` handling, to be able to access
1103         /// the corresponding channel for the event, as we only have access to the `channel_id` during
1104         /// the handling of the events.
1105         ///
1106         /// Note that no consistency guarantees are made about the existence of a peer with the
1107         /// `counterparty_node_id` in our other maps.
1108         ///
1109         /// TODO:
1110         /// The `counterparty_node_id` isn't passed with `MonitorEvent`s currently. To pass it, we need
1111         /// to make `counterparty_node_id`'s a required field in `ChannelMonitor`s, which unfortunately
1112         /// would break backwards compatability.
1113         /// We should add `counterparty_node_id`s to `MonitorEvent`s, and eventually rely on it in the
1114         /// future. That would make this map redundant, as only the `ChannelManager::per_peer_state` is
1115         /// required to access the channel with the `counterparty_node_id`.
1116         ///
1117         /// See `ChannelManager` struct-level documentation for lock order requirements.
1118         id_to_peer: Mutex<HashMap<ChannelId, PublicKey>>,
1119
1120         /// SCIDs (and outbound SCID aliases) -> `counterparty_node_id`s and `channel_id`s.
1121         ///
1122         /// Outbound SCID aliases are added here once the channel is available for normal use, with
1123         /// SCIDs being added once the funding transaction is confirmed at the channel's required
1124         /// confirmation depth.
1125         ///
1126         /// Note that while this holds `counterparty_node_id`s and `channel_id`s, no consistency
1127         /// guarantees are made about the existence of a peer with the `counterparty_node_id` nor a
1128         /// channel with the `channel_id` in our other maps.
1129         ///
1130         /// See `ChannelManager` struct-level documentation for lock order requirements.
1131         #[cfg(test)]
1132         pub(super) short_to_chan_info: FairRwLock<HashMap<u64, (PublicKey, ChannelId)>>,
1133         #[cfg(not(test))]
1134         short_to_chan_info: FairRwLock<HashMap<u64, (PublicKey, ChannelId)>>,
1135
1136         our_network_pubkey: PublicKey,
1137
1138         inbound_payment_key: inbound_payment::ExpandedKey,
1139
1140         /// LDK puts the [fake scids] that it generates into namespaces, to identify the type of an
1141         /// incoming payment. To make it harder for a third-party to identify the type of a payment,
1142         /// we encrypt the namespace identifier using these bytes.
1143         ///
1144         /// [fake scids]: crate::util::scid_utils::fake_scid
1145         fake_scid_rand_bytes: [u8; 32],
1146
1147         /// When we send payment probes, we generate the [`PaymentHash`] based on this cookie secret
1148         /// and a random [`PaymentId`]. This allows us to discern probes from real payments, without
1149         /// keeping additional state.
1150         probing_cookie_secret: [u8; 32],
1151
1152         /// The highest block timestamp we've seen, which is usually a good guess at the current time.
1153         /// Assuming most miners are generating blocks with reasonable timestamps, this shouldn't be
1154         /// very far in the past, and can only ever be up to two hours in the future.
1155         highest_seen_timestamp: AtomicUsize,
1156
1157         /// The bulk of our storage. Currently the `per_peer_state` stores our channels on a per-peer
1158         /// basis, as well as the peer's latest features.
1159         ///
1160         /// If we are connected to a peer we always at least have an entry here, even if no channels
1161         /// are currently open with that peer.
1162         ///
1163         /// Because adding or removing an entry is rare, we usually take an outer read lock and then
1164         /// operate on the inner value freely. This opens up for parallel per-peer operation for
1165         /// channels.
1166         ///
1167         /// Note that the same thread must never acquire two inner `PeerState` locks at the same time.
1168         ///
1169         /// See `ChannelManager` struct-level documentation for lock order requirements.
1170         #[cfg(not(any(test, feature = "_test_utils")))]
1171         per_peer_state: FairRwLock<HashMap<PublicKey, Mutex<PeerState<SP>>>>,
1172         #[cfg(any(test, feature = "_test_utils"))]
1173         pub(super) per_peer_state: FairRwLock<HashMap<PublicKey, Mutex<PeerState<SP>>>>,
1174
1175         /// The set of events which we need to give to the user to handle. In some cases an event may
1176         /// require some further action after the user handles it (currently only blocking a monitor
1177         /// update from being handed to the user to ensure the included changes to the channel state
1178         /// are handled by the user before they're persisted durably to disk). In that case, the second
1179         /// element in the tuple is set to `Some` with further details of the action.
1180         ///
1181         /// Note that events MUST NOT be removed from pending_events after deserialization, as they
1182         /// could be in the middle of being processed without the direct mutex held.
1183         ///
1184         /// See `ChannelManager` struct-level documentation for lock order requirements.
1185         #[cfg(not(any(test, feature = "_test_utils")))]
1186         pending_events: Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>,
1187         #[cfg(any(test, feature = "_test_utils"))]
1188         pub(crate) pending_events: Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>,
1189
1190         /// A simple atomic flag to ensure only one task at a time can be processing events asynchronously.
1191         pending_events_processor: AtomicBool,
1192
1193         /// If we are running during init (either directly during the deserialization method or in
1194         /// block connection methods which run after deserialization but before normal operation) we
1195         /// cannot provide the user with [`ChannelMonitorUpdate`]s through the normal update flow -
1196         /// prior to normal operation the user may not have loaded the [`ChannelMonitor`]s into their
1197         /// [`ChainMonitor`] and thus attempting to update it will fail or panic.
1198         ///
1199         /// Thus, we place them here to be handled as soon as possible once we are running normally.
1200         ///
1201         /// See `ChannelManager` struct-level documentation for lock order requirements.
1202         ///
1203         /// [`ChainMonitor`]: crate::chain::chainmonitor::ChainMonitor
1204         pending_background_events: Mutex<Vec<BackgroundEvent>>,
1205         /// Used when we have to take a BIG lock to make sure everything is self-consistent.
1206         /// Essentially just when we're serializing ourselves out.
1207         /// Taken first everywhere where we are making changes before any other locks.
1208         /// When acquiring this lock in read mode, rather than acquiring it directly, call
1209         /// `PersistenceNotifierGuard::notify_on_drop(..)` and pass the lock to it, to ensure the
1210         /// Notifier the lock contains sends out a notification when the lock is released.
1211         total_consistency_lock: RwLock<()>,
1212         /// Tracks the progress of channels going through batch funding by whether funding_signed was
1213         /// received and the monitor has been persisted.
1214         ///
1215         /// This information does not need to be persisted as funding nodes can forget
1216         /// unfunded channels upon disconnection.
1217         funding_batch_states: Mutex<BTreeMap<Txid, Vec<(ChannelId, PublicKey, bool)>>>,
1218
1219         background_events_processed_since_startup: AtomicBool,
1220
1221         event_persist_notifier: Notifier,
1222         needs_persist_flag: AtomicBool,
1223
1224         entropy_source: ES,
1225         node_signer: NS,
1226         signer_provider: SP,
1227
1228         logger: L,
1229 }
1230
1231 /// Chain-related parameters used to construct a new `ChannelManager`.
1232 ///
1233 /// Typically, the block-specific parameters are derived from the best block hash for the network,
1234 /// as a newly constructed `ChannelManager` will not have created any channels yet. These parameters
1235 /// are not needed when deserializing a previously constructed `ChannelManager`.
1236 #[derive(Clone, Copy, PartialEq)]
1237 pub struct ChainParameters {
1238         /// The network for determining the `chain_hash` in Lightning messages.
1239         pub network: Network,
1240
1241         /// The hash and height of the latest block successfully connected.
1242         ///
1243         /// Used to track on-chain channel funding outputs and send payments with reliable timelocks.
1244         pub best_block: BestBlock,
1245 }
1246
1247 #[derive(Copy, Clone, PartialEq)]
1248 #[must_use]
1249 enum NotifyOption {
1250         DoPersist,
1251         SkipPersistHandleEvents,
1252         SkipPersistNoEvents,
1253 }
1254
1255 /// Whenever we release the `ChannelManager`'s `total_consistency_lock`, from read mode, it is
1256 /// desirable to notify any listeners on `await_persistable_update_timeout`/
1257 /// `await_persistable_update` when new updates are available for persistence. Therefore, this
1258 /// struct is responsible for locking the total consistency lock and, upon going out of scope,
1259 /// sending the aforementioned notification (since the lock being released indicates that the
1260 /// updates are ready for persistence).
1261 ///
1262 /// We allow callers to either always notify by constructing with `notify_on_drop` or choose to
1263 /// notify or not based on whether relevant changes have been made, providing a closure to
1264 /// `optionally_notify` which returns a `NotifyOption`.
1265 struct PersistenceNotifierGuard<'a, F: FnMut() -> NotifyOption> {
1266         event_persist_notifier: &'a Notifier,
1267         needs_persist_flag: &'a AtomicBool,
1268         should_persist: F,
1269         // We hold onto this result so the lock doesn't get released immediately.
1270         _read_guard: RwLockReadGuard<'a, ()>,
1271 }
1272
1273 impl<'a> PersistenceNotifierGuard<'a, fn() -> NotifyOption> { // We don't care what the concrete F is here, it's unused
1274         /// Notifies any waiters and indicates that we need to persist, in addition to possibly having
1275         /// events to handle.
1276         ///
1277         /// This must always be called if the changes included a `ChannelMonitorUpdate`, as well as in
1278         /// other cases where losing the changes on restart may result in a force-close or otherwise
1279         /// isn't ideal.
1280         fn notify_on_drop<C: AChannelManager>(cm: &'a C) -> PersistenceNotifierGuard<'a, impl FnMut() -> NotifyOption> {
1281                 Self::optionally_notify(cm, || -> NotifyOption { NotifyOption::DoPersist })
1282         }
1283
1284         fn optionally_notify<F: FnMut() -> NotifyOption, C: AChannelManager>(cm: &'a C, mut persist_check: F)
1285         -> PersistenceNotifierGuard<'a, impl FnMut() -> NotifyOption> {
1286                 let read_guard = cm.get_cm().total_consistency_lock.read().unwrap();
1287                 let force_notify = cm.get_cm().process_background_events();
1288
1289                 PersistenceNotifierGuard {
1290                         event_persist_notifier: &cm.get_cm().event_persist_notifier,
1291                         needs_persist_flag: &cm.get_cm().needs_persist_flag,
1292                         should_persist: move || {
1293                                 // Pick the "most" action between `persist_check` and the background events
1294                                 // processing and return that.
1295                                 let notify = persist_check();
1296                                 match (notify, force_notify) {
1297                                         (NotifyOption::DoPersist, _) => NotifyOption::DoPersist,
1298                                         (_, NotifyOption::DoPersist) => NotifyOption::DoPersist,
1299                                         (NotifyOption::SkipPersistHandleEvents, _) => NotifyOption::SkipPersistHandleEvents,
1300                                         (_, NotifyOption::SkipPersistHandleEvents) => NotifyOption::SkipPersistHandleEvents,
1301                                         _ => NotifyOption::SkipPersistNoEvents,
1302                                 }
1303                         },
1304                         _read_guard: read_guard,
1305                 }
1306         }
1307
1308         /// Note that if any [`ChannelMonitorUpdate`]s are possibly generated,
1309         /// [`ChannelManager::process_background_events`] MUST be called first (or
1310         /// [`Self::optionally_notify`] used).
1311         fn optionally_notify_skipping_background_events<F: Fn() -> NotifyOption, C: AChannelManager>
1312         (cm: &'a C, persist_check: F) -> PersistenceNotifierGuard<'a, F> {
1313                 let read_guard = cm.get_cm().total_consistency_lock.read().unwrap();
1314
1315                 PersistenceNotifierGuard {
1316                         event_persist_notifier: &cm.get_cm().event_persist_notifier,
1317                         needs_persist_flag: &cm.get_cm().needs_persist_flag,
1318                         should_persist: persist_check,
1319                         _read_guard: read_guard,
1320                 }
1321         }
1322 }
1323
1324 impl<'a, F: FnMut() -> NotifyOption> Drop for PersistenceNotifierGuard<'a, F> {
1325         fn drop(&mut self) {
1326                 match (self.should_persist)() {
1327                         NotifyOption::DoPersist => {
1328                                 self.needs_persist_flag.store(true, Ordering::Release);
1329                                 self.event_persist_notifier.notify()
1330                         },
1331                         NotifyOption::SkipPersistHandleEvents =>
1332                                 self.event_persist_notifier.notify(),
1333                         NotifyOption::SkipPersistNoEvents => {},
1334                 }
1335         }
1336 }
1337
1338 /// The amount of time in blocks we require our counterparty wait to claim their money (ie time
1339 /// between when we, or our watchtower, must check for them having broadcast a theft transaction).
1340 ///
1341 /// This can be increased (but not decreased) through [`ChannelHandshakeConfig::our_to_self_delay`]
1342 ///
1343 /// [`ChannelHandshakeConfig::our_to_self_delay`]: crate::util::config::ChannelHandshakeConfig::our_to_self_delay
1344 pub const BREAKDOWN_TIMEOUT: u16 = 6 * 24;
1345 /// The amount of time in blocks we're willing to wait to claim money back to us. This matches
1346 /// the maximum required amount in lnd as of March 2021.
1347 pub(crate) const MAX_LOCAL_BREAKDOWN_TIMEOUT: u16 = 2 * 6 * 24 * 7;
1348
1349 /// The minimum number of blocks between an inbound HTLC's CLTV and the corresponding outbound
1350 /// HTLC's CLTV. The current default represents roughly seven hours of blocks at six blocks/hour.
1351 ///
1352 /// This can be increased (but not decreased) through [`ChannelConfig::cltv_expiry_delta`]
1353 ///
1354 /// [`ChannelConfig::cltv_expiry_delta`]: crate::util::config::ChannelConfig::cltv_expiry_delta
1355 // This should always be a few blocks greater than channelmonitor::CLTV_CLAIM_BUFFER,
1356 // i.e. the node we forwarded the payment on to should always have enough room to reliably time out
1357 // the HTLC via a full update_fail_htlc/commitment_signed dance before we hit the
1358 // CLTV_CLAIM_BUFFER point (we static assert that it's at least 3 blocks more).
1359 pub const MIN_CLTV_EXPIRY_DELTA: u16 = 6*7;
1360 // This should be long enough to allow a payment path drawn across multiple routing hops with substantial
1361 // `cltv_expiry_delta`. Indeed, the length of those values is the reaction delay offered to a routing node
1362 // in case of HTLC on-chain settlement. While appearing less competitive, a node operator could decide to
1363 // scale them up to suit its security policy. At the network-level, we shouldn't constrain them too much,
1364 // while avoiding to introduce a DoS vector. Further, a low CTLV_FAR_FAR_AWAY could be a source of
1365 // routing failure for any HTLC sender picking up an LDK node among the first hops.
1366 pub(super) const CLTV_FAR_FAR_AWAY: u32 = 14 * 24 * 6;
1367
1368 /// Minimum CLTV difference between the current block height and received inbound payments.
1369 /// Invoices generated for payment to us must set their `min_final_cltv_expiry_delta` field to at least
1370 /// this value.
1371 // Note that we fail if exactly HTLC_FAIL_BACK_BUFFER + 1 was used, so we need to add one for
1372 // any payments to succeed. Further, we don't want payments to fail if a block was found while
1373 // a payment was being routed, so we add an extra block to be safe.
1374 pub const MIN_FINAL_CLTV_EXPIRY_DELTA: u16 = HTLC_FAIL_BACK_BUFFER as u16 + 3;
1375
1376 // Check that our CLTV_EXPIRY is at least CLTV_CLAIM_BUFFER + ANTI_REORG_DELAY + LATENCY_GRACE_PERIOD_BLOCKS,
1377 // ie that if the next-hop peer fails the HTLC within
1378 // LATENCY_GRACE_PERIOD_BLOCKS then we'll still have CLTV_CLAIM_BUFFER left to timeout it onchain,
1379 // then waiting ANTI_REORG_DELAY to be reorg-safe on the outbound HLTC and
1380 // failing the corresponding htlc backward, and us now seeing the last block of ANTI_REORG_DELAY before
1381 // LATENCY_GRACE_PERIOD_BLOCKS.
1382 #[deny(const_err)]
1383 #[allow(dead_code)]
1384 const CHECK_CLTV_EXPIRY_SANITY: u32 = MIN_CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - CLTV_CLAIM_BUFFER - ANTI_REORG_DELAY - LATENCY_GRACE_PERIOD_BLOCKS;
1385
1386 // Check for ability of an attacker to make us fail on-chain by delaying an HTLC claim. See
1387 // ChannelMonitor::should_broadcast_holder_commitment_txn for a description of why this is needed.
1388 #[deny(const_err)]
1389 #[allow(dead_code)]
1390 const CHECK_CLTV_EXPIRY_SANITY_2: u32 = MIN_CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - 2*CLTV_CLAIM_BUFFER;
1391
1392 /// The number of ticks of [`ChannelManager::timer_tick_occurred`] until expiry of incomplete MPPs
1393 pub(crate) const MPP_TIMEOUT_TICKS: u8 = 3;
1394
1395 /// The number of ticks of [`ChannelManager::timer_tick_occurred`] where a peer is disconnected
1396 /// until we mark the channel disabled and gossip the update.
1397 pub(crate) const DISABLE_GOSSIP_TICKS: u8 = 10;
1398
1399 /// The number of ticks of [`ChannelManager::timer_tick_occurred`] where a peer is connected until
1400 /// we mark the channel enabled and gossip the update.
1401 pub(crate) const ENABLE_GOSSIP_TICKS: u8 = 5;
1402
1403 /// The maximum number of unfunded channels we can have per-peer before we start rejecting new
1404 /// (inbound) ones. The number of peers with unfunded channels is limited separately in
1405 /// [`MAX_UNFUNDED_CHANNEL_PEERS`].
1406 const MAX_UNFUNDED_CHANS_PER_PEER: usize = 4;
1407
1408 /// The maximum number of peers from which we will allow pending unfunded channels. Once we reach
1409 /// this many peers we reject new (inbound) channels from peers with which we don't have a channel.
1410 const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
1411
1412 /// The maximum number of peers which we do not have a (funded) channel with. Once we reach this
1413 /// many peers we reject new (inbound) connections.
1414 const MAX_NO_CHANNEL_PEERS: usize = 250;
1415
1416 /// Information needed for constructing an invoice route hint for this channel.
1417 #[derive(Clone, Debug, PartialEq)]
1418 pub struct CounterpartyForwardingInfo {
1419         /// Base routing fee in millisatoshis.
1420         pub fee_base_msat: u32,
1421         /// Amount in millionths of a satoshi the channel will charge per transferred satoshi.
1422         pub fee_proportional_millionths: u32,
1423         /// The minimum difference in cltv_expiry between an ingoing HTLC and its outgoing counterpart,
1424         /// such that the outgoing HTLC is forwardable to this counterparty. See `msgs::ChannelUpdate`'s
1425         /// `cltv_expiry_delta` for more details.
1426         pub cltv_expiry_delta: u16,
1427 }
1428
1429 /// Channel parameters which apply to our counterparty. These are split out from [`ChannelDetails`]
1430 /// to better separate parameters.
1431 #[derive(Clone, Debug, PartialEq)]
1432 pub struct ChannelCounterparty {
1433         /// The node_id of our counterparty
1434         pub node_id: PublicKey,
1435         /// The Features the channel counterparty provided upon last connection.
1436         /// Useful for routing as it is the most up-to-date copy of the counterparty's features and
1437         /// many routing-relevant features are present in the init context.
1438         pub features: InitFeatures,
1439         /// The value, in satoshis, that must always be held in the channel for our counterparty. This
1440         /// value ensures that if our counterparty broadcasts a revoked state, we can punish them by
1441         /// claiming at least this value on chain.
1442         ///
1443         /// This value is not included in [`inbound_capacity_msat`] as it can never be spent.
1444         ///
1445         /// [`inbound_capacity_msat`]: ChannelDetails::inbound_capacity_msat
1446         pub unspendable_punishment_reserve: u64,
1447         /// Information on the fees and requirements that the counterparty requires when forwarding
1448         /// payments to us through this channel.
1449         pub forwarding_info: Option<CounterpartyForwardingInfo>,
1450         /// The smallest value HTLC (in msat) the remote peer will accept, for this channel. This field
1451         /// is only `None` before we have received either the `OpenChannel` or `AcceptChannel` message
1452         /// from the remote peer, or for `ChannelCounterparty` objects serialized prior to LDK 0.0.107.
1453         pub outbound_htlc_minimum_msat: Option<u64>,
1454         /// The largest value HTLC (in msat) the remote peer currently will accept, for this channel.
1455         pub outbound_htlc_maximum_msat: Option<u64>,
1456 }
1457
1458 /// Details of a channel, as returned by [`ChannelManager::list_channels`] and [`ChannelManager::list_usable_channels`]
1459 #[derive(Clone, Debug, PartialEq)]
1460 pub struct ChannelDetails {
1461         /// The channel's ID (prior to funding transaction generation, this is a random 32 bytes,
1462         /// thereafter this is the txid of the funding transaction xor the funding transaction output).
1463         /// Note that this means this value is *not* persistent - it can change once during the
1464         /// lifetime of the channel.
1465         pub channel_id: ChannelId,
1466         /// Parameters which apply to our counterparty. See individual fields for more information.
1467         pub counterparty: ChannelCounterparty,
1468         /// The Channel's funding transaction output, if we've negotiated the funding transaction with
1469         /// our counterparty already.
1470         ///
1471         /// Note that, if this has been set, `channel_id` will be equivalent to
1472         /// `funding_txo.unwrap().to_channel_id()`.
1473         pub funding_txo: Option<OutPoint>,
1474         /// The features which this channel operates with. See individual features for more info.
1475         ///
1476         /// `None` until negotiation completes and the channel type is finalized.
1477         pub channel_type: Option<ChannelTypeFeatures>,
1478         /// The position of the funding transaction in the chain. None if the funding transaction has
1479         /// not yet been confirmed and the channel fully opened.
1480         ///
1481         /// Note that if [`inbound_scid_alias`] is set, it must be used for invoices and inbound
1482         /// payments instead of this. See [`get_inbound_payment_scid`].
1483         ///
1484         /// For channels with [`confirmations_required`] set to `Some(0)`, [`outbound_scid_alias`] may
1485         /// be used in place of this in outbound routes. See [`get_outbound_payment_scid`].
1486         ///
1487         /// [`inbound_scid_alias`]: Self::inbound_scid_alias
1488         /// [`outbound_scid_alias`]: Self::outbound_scid_alias
1489         /// [`get_inbound_payment_scid`]: Self::get_inbound_payment_scid
1490         /// [`get_outbound_payment_scid`]: Self::get_outbound_payment_scid
1491         /// [`confirmations_required`]: Self::confirmations_required
1492         pub short_channel_id: Option<u64>,
1493         /// An optional [`short_channel_id`] alias for this channel, randomly generated by us and
1494         /// usable in place of [`short_channel_id`] to reference the channel in outbound routes when
1495         /// the channel has not yet been confirmed (as long as [`confirmations_required`] is
1496         /// `Some(0)`).
1497         ///
1498         /// This will be `None` as long as the channel is not available for routing outbound payments.
1499         ///
1500         /// [`short_channel_id`]: Self::short_channel_id
1501         /// [`confirmations_required`]: Self::confirmations_required
1502         pub outbound_scid_alias: Option<u64>,
1503         /// An optional [`short_channel_id`] alias for this channel, randomly generated by our
1504         /// counterparty and usable in place of [`short_channel_id`] in invoice route hints. Our
1505         /// counterparty will recognize the alias provided here in place of the [`short_channel_id`]
1506         /// when they see a payment to be routed to us.
1507         ///
1508         /// Our counterparty may choose to rotate this value at any time, though will always recognize
1509         /// previous values for inbound payment forwarding.
1510         ///
1511         /// [`short_channel_id`]: Self::short_channel_id
1512         pub inbound_scid_alias: Option<u64>,
1513         /// The value, in satoshis, of this channel as appears in the funding output
1514         pub channel_value_satoshis: u64,
1515         /// The value, in satoshis, that must always be held in the channel for us. This value ensures
1516         /// that if we broadcast a revoked state, our counterparty can punish us by claiming at least
1517         /// this value on chain.
1518         ///
1519         /// This value is not included in [`outbound_capacity_msat`] as it can never be spent.
1520         ///
1521         /// This value will be `None` for outbound channels until the counterparty accepts the channel.
1522         ///
1523         /// [`outbound_capacity_msat`]: ChannelDetails::outbound_capacity_msat
1524         pub unspendable_punishment_reserve: Option<u64>,
1525         /// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
1526         /// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
1527         /// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
1528         /// `user_channel_id` will be randomized for an inbound channel.  This may be zero for objects
1529         /// serialized with LDK versions prior to 0.0.113.
1530         ///
1531         /// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
1532         /// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
1533         /// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
1534         pub user_channel_id: u128,
1535         /// The currently negotiated fee rate denominated in satoshi per 1000 weight units,
1536         /// which is applied to commitment and HTLC transactions.
1537         ///
1538         /// This value will be `None` for objects serialized with LDK versions prior to 0.0.115.
1539         pub feerate_sat_per_1000_weight: Option<u32>,
1540         /// Our total balance.  This is the amount we would get if we close the channel.
1541         /// This value is not exact. Due to various in-flight changes and feerate changes, exactly this
1542         /// amount is not likely to be recoverable on close.
1543         ///
1544         /// This does not include any pending HTLCs which are not yet fully resolved (and, thus, whose
1545         /// balance is not available for inclusion in new outbound HTLCs). This further does not include
1546         /// any pending outgoing HTLCs which are awaiting some other resolution to be sent.
1547         /// This does not consider any on-chain fees.
1548         ///
1549         /// See also [`ChannelDetails::outbound_capacity_msat`]
1550         pub balance_msat: u64,
1551         /// The available outbound capacity for sending HTLCs to the remote peer. This does not include
1552         /// any pending HTLCs which are not yet fully resolved (and, thus, whose balance is not
1553         /// available for inclusion in new outbound HTLCs). This further does not include any pending
1554         /// outgoing HTLCs which are awaiting some other resolution to be sent.
1555         ///
1556         /// See also [`ChannelDetails::balance_msat`]
1557         ///
1558         /// This value is not exact. Due to various in-flight changes, feerate changes, and our
1559         /// conflict-avoidance policy, exactly this amount is not likely to be spendable. However, we
1560         /// should be able to spend nearly this amount.
1561         pub outbound_capacity_msat: u64,
1562         /// The available outbound capacity for sending a single HTLC to the remote peer. This is
1563         /// similar to [`ChannelDetails::outbound_capacity_msat`] but it may be further restricted by
1564         /// the current state and per-HTLC limit(s). This is intended for use when routing, allowing us
1565         /// to use a limit as close as possible to the HTLC limit we can currently send.
1566         ///
1567         /// See also [`ChannelDetails::next_outbound_htlc_minimum_msat`],
1568         /// [`ChannelDetails::balance_msat`], and [`ChannelDetails::outbound_capacity_msat`].
1569         pub next_outbound_htlc_limit_msat: u64,
1570         /// The minimum value for sending a single HTLC to the remote peer. This is the equivalent of
1571         /// [`ChannelDetails::next_outbound_htlc_limit_msat`] but represents a lower-bound, rather than
1572         /// an upper-bound. This is intended for use when routing, allowing us to ensure we pick a
1573         /// route which is valid.
1574         pub next_outbound_htlc_minimum_msat: u64,
1575         /// The available inbound capacity for the remote peer to send HTLCs to us. This does not
1576         /// include any pending HTLCs which are not yet fully resolved (and, thus, whose balance is not
1577         /// available for inclusion in new inbound HTLCs).
1578         /// Note that there are some corner cases not fully handled here, so the actual available
1579         /// inbound capacity may be slightly higher than this.
1580         ///
1581         /// This value is not exact. Due to various in-flight changes, feerate changes, and our
1582         /// counterparty's conflict-avoidance policy, exactly this amount is not likely to be spendable.
1583         /// However, our counterparty should be able to spend nearly this amount.
1584         pub inbound_capacity_msat: u64,
1585         /// The number of required confirmations on the funding transaction before the funding will be
1586         /// considered "locked". This number is selected by the channel fundee (i.e. us if
1587         /// [`is_outbound`] is *not* set), and can be selected for inbound channels with
1588         /// [`ChannelHandshakeConfig::minimum_depth`] or limited for outbound channels with
1589         /// [`ChannelHandshakeLimits::max_minimum_depth`].
1590         ///
1591         /// This value will be `None` for outbound channels until the counterparty accepts the channel.
1592         ///
1593         /// [`is_outbound`]: ChannelDetails::is_outbound
1594         /// [`ChannelHandshakeConfig::minimum_depth`]: crate::util::config::ChannelHandshakeConfig::minimum_depth
1595         /// [`ChannelHandshakeLimits::max_minimum_depth`]: crate::util::config::ChannelHandshakeLimits::max_minimum_depth
1596         pub confirmations_required: Option<u32>,
1597         /// The current number of confirmations on the funding transaction.
1598         ///
1599         /// This value will be `None` for objects serialized with LDK versions prior to 0.0.113.
1600         pub confirmations: Option<u32>,
1601         /// The number of blocks (after our commitment transaction confirms) that we will need to wait
1602         /// until we can claim our funds after we force-close the channel. During this time our
1603         /// counterparty is allowed to punish us if we broadcasted a stale state. If our counterparty
1604         /// force-closes the channel and broadcasts a commitment transaction we do not have to wait any
1605         /// time to claim our non-HTLC-encumbered funds.
1606         ///
1607         /// This value will be `None` for outbound channels until the counterparty accepts the channel.
1608         pub force_close_spend_delay: Option<u16>,
1609         /// True if the channel was initiated (and thus funded) by us.
1610         pub is_outbound: bool,
1611         /// True if the channel is confirmed, channel_ready messages have been exchanged, and the
1612         /// channel is not currently being shut down. `channel_ready` message exchange implies the
1613         /// required confirmation count has been reached (and we were connected to the peer at some
1614         /// point after the funding transaction received enough confirmations). The required
1615         /// confirmation count is provided in [`confirmations_required`].
1616         ///
1617         /// [`confirmations_required`]: ChannelDetails::confirmations_required
1618         pub is_channel_ready: bool,
1619         /// The stage of the channel's shutdown.
1620         /// `None` for `ChannelDetails` serialized on LDK versions prior to 0.0.116.
1621         pub channel_shutdown_state: Option<ChannelShutdownState>,
1622         /// True if the channel is (a) confirmed and channel_ready messages have been exchanged, (b)
1623         /// the peer is connected, and (c) the channel is not currently negotiating a shutdown.
1624         ///
1625         /// This is a strict superset of `is_channel_ready`.
1626         pub is_usable: bool,
1627         /// True if this channel is (or will be) publicly-announced.
1628         pub is_public: bool,
1629         /// The smallest value HTLC (in msat) we will accept, for this channel. This field
1630         /// is only `None` for `ChannelDetails` objects serialized prior to LDK 0.0.107
1631         pub inbound_htlc_minimum_msat: Option<u64>,
1632         /// The largest value HTLC (in msat) we currently will accept, for this channel.
1633         pub inbound_htlc_maximum_msat: Option<u64>,
1634         /// Set of configurable parameters that affect channel operation.
1635         ///
1636         /// This field is only `None` for `ChannelDetails` objects serialized prior to LDK 0.0.109.
1637         pub config: Option<ChannelConfig>,
1638 }
1639
1640 impl ChannelDetails {
1641         /// Gets the current SCID which should be used to identify this channel for inbound payments.
1642         /// This should be used for providing invoice hints or in any other context where our
1643         /// counterparty will forward a payment to us.
1644         ///
1645         /// This is either the [`ChannelDetails::inbound_scid_alias`], if set, or the
1646         /// [`ChannelDetails::short_channel_id`]. See those for more information.
1647         pub fn get_inbound_payment_scid(&self) -> Option<u64> {
1648                 self.inbound_scid_alias.or(self.short_channel_id)
1649         }
1650
1651         /// Gets the current SCID which should be used to identify this channel for outbound payments.
1652         /// This should be used in [`Route`]s to describe the first hop or in other contexts where
1653         /// we're sending or forwarding a payment outbound over this channel.
1654         ///
1655         /// This is either the [`ChannelDetails::short_channel_id`], if set, or the
1656         /// [`ChannelDetails::outbound_scid_alias`]. See those for more information.
1657         pub fn get_outbound_payment_scid(&self) -> Option<u64> {
1658                 self.short_channel_id.or(self.outbound_scid_alias)
1659         }
1660
1661         fn from_channel_context<SP: Deref, F: Deref>(
1662                 context: &ChannelContext<SP>, best_block_height: u32, latest_features: InitFeatures,
1663                 fee_estimator: &LowerBoundedFeeEstimator<F>
1664         ) -> Self
1665         where
1666                 SP::Target: SignerProvider,
1667                 F::Target: FeeEstimator
1668         {
1669                 let balance = context.get_available_balances(fee_estimator);
1670                 let (to_remote_reserve_satoshis, to_self_reserve_satoshis) =
1671                         context.get_holder_counterparty_selected_channel_reserve_satoshis();
1672                 ChannelDetails {
1673                         channel_id: context.channel_id(),
1674                         counterparty: ChannelCounterparty {
1675                                 node_id: context.get_counterparty_node_id(),
1676                                 features: latest_features,
1677                                 unspendable_punishment_reserve: to_remote_reserve_satoshis,
1678                                 forwarding_info: context.counterparty_forwarding_info(),
1679                                 // Ensures that we have actually received the `htlc_minimum_msat` value
1680                                 // from the counterparty through the `OpenChannel` or `AcceptChannel`
1681                                 // message (as they are always the first message from the counterparty).
1682                                 // Else `Channel::get_counterparty_htlc_minimum_msat` could return the
1683                                 // default `0` value set by `Channel::new_outbound`.
1684                                 outbound_htlc_minimum_msat: if context.have_received_message() {
1685                                         Some(context.get_counterparty_htlc_minimum_msat()) } else { None },
1686                                 outbound_htlc_maximum_msat: context.get_counterparty_htlc_maximum_msat(),
1687                         },
1688                         funding_txo: context.get_funding_txo(),
1689                         // Note that accept_channel (or open_channel) is always the first message, so
1690                         // `have_received_message` indicates that type negotiation has completed.
1691                         channel_type: if context.have_received_message() { Some(context.get_channel_type().clone()) } else { None },
1692                         short_channel_id: context.get_short_channel_id(),
1693                         outbound_scid_alias: if context.is_usable() { Some(context.outbound_scid_alias()) } else { None },
1694                         inbound_scid_alias: context.latest_inbound_scid_alias(),
1695                         channel_value_satoshis: context.get_value_satoshis(),
1696                         feerate_sat_per_1000_weight: Some(context.get_feerate_sat_per_1000_weight()),
1697                         unspendable_punishment_reserve: to_self_reserve_satoshis,
1698                         balance_msat: balance.balance_msat,
1699                         inbound_capacity_msat: balance.inbound_capacity_msat,
1700                         outbound_capacity_msat: balance.outbound_capacity_msat,
1701                         next_outbound_htlc_limit_msat: balance.next_outbound_htlc_limit_msat,
1702                         next_outbound_htlc_minimum_msat: balance.next_outbound_htlc_minimum_msat,
1703                         user_channel_id: context.get_user_id(),
1704                         confirmations_required: context.minimum_depth(),
1705                         confirmations: Some(context.get_funding_tx_confirmations(best_block_height)),
1706                         force_close_spend_delay: context.get_counterparty_selected_contest_delay(),
1707                         is_outbound: context.is_outbound(),
1708                         is_channel_ready: context.is_usable(),
1709                         is_usable: context.is_live(),
1710                         is_public: context.should_announce(),
1711                         inbound_htlc_minimum_msat: Some(context.get_holder_htlc_minimum_msat()),
1712                         inbound_htlc_maximum_msat: context.get_holder_htlc_maximum_msat(),
1713                         config: Some(context.config()),
1714                         channel_shutdown_state: Some(context.shutdown_state()),
1715                 }
1716         }
1717 }
1718
1719 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
1720 /// Further information on the details of the channel shutdown.
1721 /// Upon channels being forced closed (i.e. commitment transaction confirmation detected
1722 /// by `ChainMonitor`), ChannelShutdownState will be set to `ShutdownComplete` or
1723 /// the channel will be removed shortly.
1724 /// Also note, that in normal operation, peers could disconnect at any of these states
1725 /// and require peer re-connection before making progress onto other states
1726 pub enum ChannelShutdownState {
1727         /// Channel has not sent or received a shutdown message.
1728         NotShuttingDown,
1729         /// Local node has sent a shutdown message for this channel.
1730         ShutdownInitiated,
1731         /// Shutdown message exchanges have concluded and the channels are in the midst of
1732         /// resolving all existing open HTLCs before closing can continue.
1733         ResolvingHTLCs,
1734         /// All HTLCs have been resolved, nodes are currently negotiating channel close onchain fee rates.
1735         NegotiatingClosingFee,
1736         /// We've successfully negotiated a closing_signed dance. At this point `ChannelManager` is about
1737         /// to drop the channel.
1738         ShutdownComplete,
1739 }
1740
1741 /// Used by [`ChannelManager::list_recent_payments`] to express the status of recent payments.
1742 /// These include payments that have yet to find a successful path, or have unresolved HTLCs.
1743 #[derive(Debug, PartialEq)]
1744 pub enum RecentPaymentDetails {
1745         /// When an invoice was requested and thus a payment has not yet been sent.
1746         AwaitingInvoice {
1747                 /// A user-provided identifier in [`ChannelManager::send_payment`] used to uniquely identify
1748                 /// a payment and ensure idempotency in LDK.
1749                 payment_id: PaymentId,
1750         },
1751         /// When a payment is still being sent and awaiting successful delivery.
1752         Pending {
1753                 /// A user-provided identifier in [`ChannelManager::send_payment`] used to uniquely identify
1754                 /// a payment and ensure idempotency in LDK.
1755                 payment_id: PaymentId,
1756                 /// Hash of the payment that is currently being sent but has yet to be fulfilled or
1757                 /// abandoned.
1758                 payment_hash: PaymentHash,
1759                 /// Total amount (in msat, excluding fees) across all paths for this payment,
1760                 /// not just the amount currently inflight.
1761                 total_msat: u64,
1762         },
1763         /// When a pending payment is fulfilled, we continue tracking it until all pending HTLCs have
1764         /// been resolved. Upon receiving [`Event::PaymentSent`], we delay for a few minutes before the
1765         /// payment is removed from tracking.
1766         Fulfilled {
1767                 /// A user-provided identifier in [`ChannelManager::send_payment`] used to uniquely identify
1768                 /// a payment and ensure idempotency in LDK.
1769                 payment_id: PaymentId,
1770                 /// Hash of the payment that was claimed. `None` for serializations of [`ChannelManager`]
1771                 /// made before LDK version 0.0.104.
1772                 payment_hash: Option<PaymentHash>,
1773         },
1774         /// After a payment's retries are exhausted per the provided [`Retry`], or it is explicitly
1775         /// abandoned via [`ChannelManager::abandon_payment`], it is marked as abandoned until all
1776         /// pending HTLCs for this payment resolve and an [`Event::PaymentFailed`] is generated.
1777         Abandoned {
1778                 /// A user-provided identifier in [`ChannelManager::send_payment`] used to uniquely identify
1779                 /// a payment and ensure idempotency in LDK.
1780                 payment_id: PaymentId,
1781                 /// Hash of the payment that we have given up trying to send.
1782                 payment_hash: PaymentHash,
1783         },
1784 }
1785
1786 /// Route hints used in constructing invoices for [phantom node payents].
1787 ///
1788 /// [phantom node payments]: crate::sign::PhantomKeysManager
1789 #[derive(Clone)]
1790 pub struct PhantomRouteHints {
1791         /// The list of channels to be included in the invoice route hints.
1792         pub channels: Vec<ChannelDetails>,
1793         /// A fake scid used for representing the phantom node's fake channel in generating the invoice
1794         /// route hints.
1795         pub phantom_scid: u64,
1796         /// The pubkey of the real backing node that would ultimately receive the payment.
1797         pub real_node_pubkey: PublicKey,
1798 }
1799
1800 macro_rules! handle_error {
1801         ($self: ident, $internal: expr, $counterparty_node_id: expr) => { {
1802                 // In testing, ensure there are no deadlocks where the lock is already held upon
1803                 // entering the macro.
1804                 debug_assert_ne!($self.pending_events.held_by_thread(), LockHeldState::HeldByThread);
1805                 debug_assert_ne!($self.per_peer_state.held_by_thread(), LockHeldState::HeldByThread);
1806
1807                 match $internal {
1808                         Ok(msg) => Ok(msg),
1809                         Err(MsgHandleErrInternal { err, chan_id, shutdown_finish, channel_capacity }) => {
1810                                 let mut msg_events = Vec::with_capacity(2);
1811
1812                                 if let Some((shutdown_res, update_option)) = shutdown_finish {
1813                                         $self.finish_close_channel(shutdown_res);
1814                                         if let Some(update) = update_option {
1815                                                 msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
1816                                                         msg: update
1817                                                 });
1818                                         }
1819                                         if let Some((channel_id, user_channel_id)) = chan_id {
1820                                                 $self.pending_events.lock().unwrap().push_back((events::Event::ChannelClosed {
1821                                                         channel_id, user_channel_id,
1822                                                         reason: ClosureReason::ProcessingError { err: err.err.clone() },
1823                                                         counterparty_node_id: Some($counterparty_node_id),
1824                                                         channel_capacity_sats: channel_capacity,
1825                                                 }, None));
1826                                         }
1827                                 }
1828
1829                                 log_error!($self.logger, "{}", err.err);
1830                                 if let msgs::ErrorAction::IgnoreError = err.action {
1831                                 } else {
1832                                         msg_events.push(events::MessageSendEvent::HandleError {
1833                                                 node_id: $counterparty_node_id,
1834                                                 action: err.action.clone()
1835                                         });
1836                                 }
1837
1838                                 if !msg_events.is_empty() {
1839                                         let per_peer_state = $self.per_peer_state.read().unwrap();
1840                                         if let Some(peer_state_mutex) = per_peer_state.get(&$counterparty_node_id) {
1841                                                 let mut peer_state = peer_state_mutex.lock().unwrap();
1842                                                 peer_state.pending_msg_events.append(&mut msg_events);
1843                                         }
1844                                 }
1845
1846                                 // Return error in case higher-API need one
1847                                 Err(err)
1848                         },
1849                 }
1850         } };
1851         ($self: ident, $internal: expr) => {
1852                 match $internal {
1853                         Ok(res) => Ok(res),
1854                         Err((chan, msg_handle_err)) => {
1855                                 let counterparty_node_id = chan.get_counterparty_node_id();
1856                                 handle_error!($self, Err(msg_handle_err), counterparty_node_id).map_err(|err| (chan, err))
1857                         },
1858                 }
1859         };
1860 }
1861
1862 macro_rules! update_maps_on_chan_removal {
1863         ($self: expr, $channel_context: expr) => {{
1864                 $self.id_to_peer.lock().unwrap().remove(&$channel_context.channel_id());
1865                 let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
1866                 if let Some(short_id) = $channel_context.get_short_channel_id() {
1867                         short_to_chan_info.remove(&short_id);
1868                 } else {
1869                         // If the channel was never confirmed on-chain prior to its closure, remove the
1870                         // outbound SCID alias we used for it from the collision-prevention set. While we
1871                         // generally want to avoid ever re-using an outbound SCID alias across all channels, we
1872                         // also don't want a counterparty to be able to trivially cause a memory leak by simply
1873                         // opening a million channels with us which are closed before we ever reach the funding
1874                         // stage.
1875                         let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$channel_context.outbound_scid_alias());
1876                         debug_assert!(alias_removed);
1877                 }
1878                 short_to_chan_info.remove(&$channel_context.outbound_scid_alias());
1879         }}
1880 }
1881
1882 /// Returns (boolean indicating if we should remove the Channel object from memory, a mapped error)
1883 macro_rules! convert_chan_phase_err {
1884         ($self: ident, $err: expr, $channel: expr, $channel_id: expr, MANUAL_CHANNEL_UPDATE, $channel_update: expr) => {
1885                 match $err {
1886                         ChannelError::Warn(msg) => {
1887                                 (false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(msg), *$channel_id))
1888                         },
1889                         ChannelError::Ignore(msg) => {
1890                                 (false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), *$channel_id))
1891                         },
1892                         ChannelError::Close(msg) => {
1893                                 log_error!($self.logger, "Closing channel {} due to close-required error: {}", $channel_id, msg);
1894                                 update_maps_on_chan_removal!($self, $channel.context);
1895                                 let shutdown_res = $channel.context.force_shutdown(true);
1896                                 let user_id = $channel.context.get_user_id();
1897                                 let channel_capacity_satoshis = $channel.context.get_value_satoshis();
1898
1899                                 (true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, user_id,
1900                                         shutdown_res, $channel_update, channel_capacity_satoshis))
1901                         },
1902                 }
1903         };
1904         ($self: ident, $err: expr, $channel: expr, $channel_id: expr, FUNDED_CHANNEL) => {
1905                 convert_chan_phase_err!($self, $err, $channel, $channel_id, MANUAL_CHANNEL_UPDATE, { $self.get_channel_update_for_broadcast($channel).ok() })
1906         };
1907         ($self: ident, $err: expr, $channel: expr, $channel_id: expr, UNFUNDED_CHANNEL) => {
1908                 convert_chan_phase_err!($self, $err, $channel, $channel_id, MANUAL_CHANNEL_UPDATE, None)
1909         };
1910         ($self: ident, $err: expr, $channel_phase: expr, $channel_id: expr) => {
1911                 match $channel_phase {
1912                         ChannelPhase::Funded(channel) => {
1913                                 convert_chan_phase_err!($self, $err, channel, $channel_id, FUNDED_CHANNEL)
1914                         },
1915                         ChannelPhase::UnfundedOutboundV1(channel) => {
1916                                 convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
1917                         },
1918                         ChannelPhase::UnfundedInboundV1(channel) => {
1919                                 convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
1920                         },
1921                 }
1922         };
1923 }
1924
1925 macro_rules! break_chan_phase_entry {
1926         ($self: ident, $res: expr, $entry: expr) => {
1927                 match $res {
1928                         Ok(res) => res,
1929                         Err(e) => {
1930                                 let key = *$entry.key();
1931                                 let (drop, res) = convert_chan_phase_err!($self, e, $entry.get_mut(), &key);
1932                                 if drop {
1933                                         $entry.remove_entry();
1934                                 }
1935                                 break Err(res);
1936                         }
1937                 }
1938         }
1939 }
1940
1941 macro_rules! try_chan_phase_entry {
1942         ($self: ident, $res: expr, $entry: expr) => {
1943                 match $res {
1944                         Ok(res) => res,
1945                         Err(e) => {
1946                                 let key = *$entry.key();
1947                                 let (drop, res) = convert_chan_phase_err!($self, e, $entry.get_mut(), &key);
1948                                 if drop {
1949                                         $entry.remove_entry();
1950                                 }
1951                                 return Err(res);
1952                         }
1953                 }
1954         }
1955 }
1956
1957 macro_rules! remove_channel_phase {
1958         ($self: expr, $entry: expr) => {
1959                 {
1960                         let channel = $entry.remove_entry().1;
1961                         update_maps_on_chan_removal!($self, &channel.context());
1962                         channel
1963                 }
1964         }
1965 }
1966
1967 macro_rules! send_channel_ready {
1968         ($self: ident, $pending_msg_events: expr, $channel: expr, $channel_ready_msg: expr) => {{
1969                 $pending_msg_events.push(events::MessageSendEvent::SendChannelReady {
1970                         node_id: $channel.context.get_counterparty_node_id(),
1971                         msg: $channel_ready_msg,
1972                 });
1973                 // Note that we may send a `channel_ready` multiple times for a channel if we reconnect, so
1974                 // we allow collisions, but we shouldn't ever be updating the channel ID pointed to.
1975                 let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
1976                 let outbound_alias_insert = short_to_chan_info.insert($channel.context.outbound_scid_alias(), ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
1977                 assert!(outbound_alias_insert.is_none() || outbound_alias_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
1978                         "SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
1979                 if let Some(real_scid) = $channel.context.get_short_channel_id() {
1980                         let scid_insert = short_to_chan_info.insert(real_scid, ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
1981                         assert!(scid_insert.is_none() || scid_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
1982                                 "SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
1983                 }
1984         }}
1985 }
1986
1987 macro_rules! emit_channel_pending_event {
1988         ($locked_events: expr, $channel: expr) => {
1989                 if $channel.context.should_emit_channel_pending_event() {
1990                         $locked_events.push_back((events::Event::ChannelPending {
1991                                 channel_id: $channel.context.channel_id(),
1992                                 former_temporary_channel_id: $channel.context.temporary_channel_id(),
1993                                 counterparty_node_id: $channel.context.get_counterparty_node_id(),
1994                                 user_channel_id: $channel.context.get_user_id(),
1995                                 funding_txo: $channel.context.get_funding_txo().unwrap().into_bitcoin_outpoint(),
1996                         }, None));
1997                         $channel.context.set_channel_pending_event_emitted();
1998                 }
1999         }
2000 }
2001
2002 macro_rules! emit_channel_ready_event {
2003         ($locked_events: expr, $channel: expr) => {
2004                 if $channel.context.should_emit_channel_ready_event() {
2005                         debug_assert!($channel.context.channel_pending_event_emitted());
2006                         $locked_events.push_back((events::Event::ChannelReady {
2007                                 channel_id: $channel.context.channel_id(),
2008                                 user_channel_id: $channel.context.get_user_id(),
2009                                 counterparty_node_id: $channel.context.get_counterparty_node_id(),
2010                                 channel_type: $channel.context.get_channel_type().clone(),
2011                         }, None));
2012                         $channel.context.set_channel_ready_event_emitted();
2013                 }
2014         }
2015 }
2016
2017 macro_rules! handle_monitor_update_completion {
2018         ($self: ident, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr) => { {
2019                 let mut updates = $chan.monitor_updating_restored(&$self.logger,
2020                         &$self.node_signer, $self.chain_hash, &$self.default_configuration,
2021                         $self.best_block.read().unwrap().height());
2022                 let counterparty_node_id = $chan.context.get_counterparty_node_id();
2023                 let channel_update = if updates.channel_ready.is_some() && $chan.context.is_usable() {
2024                         // We only send a channel_update in the case where we are just now sending a
2025                         // channel_ready and the channel is in a usable state. We may re-send a
2026                         // channel_update later through the announcement_signatures process for public
2027                         // channels, but there's no reason not to just inform our counterparty of our fees
2028                         // now.
2029                         if let Ok(msg) = $self.get_channel_update_for_unicast($chan) {
2030                                 Some(events::MessageSendEvent::SendChannelUpdate {
2031                                         node_id: counterparty_node_id,
2032                                         msg,
2033                                 })
2034                         } else { None }
2035                 } else { None };
2036
2037                 let update_actions = $peer_state.monitor_update_blocked_actions
2038                         .remove(&$chan.context.channel_id()).unwrap_or(Vec::new());
2039
2040                 let htlc_forwards = $self.handle_channel_resumption(
2041                         &mut $peer_state.pending_msg_events, $chan, updates.raa,
2042                         updates.commitment_update, updates.order, updates.accepted_htlcs,
2043                         updates.funding_broadcastable, updates.channel_ready,
2044                         updates.announcement_sigs);
2045                 if let Some(upd) = channel_update {
2046                         $peer_state.pending_msg_events.push(upd);
2047                 }
2048
2049                 let channel_id = $chan.context.channel_id();
2050                 let unbroadcasted_batch_funding_txid = $chan.context.unbroadcasted_batch_funding_txid();
2051                 core::mem::drop($peer_state_lock);
2052                 core::mem::drop($per_peer_state_lock);
2053
2054                 // If the channel belongs to a batch funding transaction, the progress of the batch
2055                 // should be updated as we have received funding_signed and persisted the monitor.
2056                 if let Some(txid) = unbroadcasted_batch_funding_txid {
2057                         let mut funding_batch_states = $self.funding_batch_states.lock().unwrap();
2058                         let mut batch_completed = false;
2059                         if let Some(batch_state) = funding_batch_states.get_mut(&txid) {
2060                                 let channel_state = batch_state.iter_mut().find(|(chan_id, pubkey, _)| (
2061                                         *chan_id == channel_id &&
2062                                         *pubkey == counterparty_node_id
2063                                 ));
2064                                 if let Some(channel_state) = channel_state {
2065                                         channel_state.2 = true;
2066                                 } else {
2067                                         debug_assert!(false, "Missing channel batch state for channel which completed initial monitor update");
2068                                 }
2069                                 batch_completed = batch_state.iter().all(|(_, _, completed)| *completed);
2070                         } else {
2071                                 debug_assert!(false, "Missing batch state for channel which completed initial monitor update");
2072                         }
2073
2074                         // When all channels in a batched funding transaction have become ready, it is not necessary
2075                         // to track the progress of the batch anymore and the state of the channels can be updated.
2076                         if batch_completed {
2077                                 let removed_batch_state = funding_batch_states.remove(&txid).into_iter().flatten();
2078                                 let per_peer_state = $self.per_peer_state.read().unwrap();
2079                                 let mut batch_funding_tx = None;
2080                                 for (channel_id, counterparty_node_id, _) in removed_batch_state {
2081                                         if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
2082                                                 let mut peer_state = peer_state_mutex.lock().unwrap();
2083                                                 if let Some(ChannelPhase::Funded(chan)) = peer_state.channel_by_id.get_mut(&channel_id) {
2084                                                         batch_funding_tx = batch_funding_tx.or_else(|| chan.context.unbroadcasted_funding());
2085                                                         chan.set_batch_ready();
2086                                                         let mut pending_events = $self.pending_events.lock().unwrap();
2087                                                         emit_channel_pending_event!(pending_events, chan);
2088                                                 }
2089                                         }
2090                                 }
2091                                 if let Some(tx) = batch_funding_tx {
2092                                         log_info!($self.logger, "Broadcasting batch funding transaction with txid {}", tx.txid());
2093                                         $self.tx_broadcaster.broadcast_transactions(&[&tx]);
2094                                 }
2095                         }
2096                 }
2097
2098                 $self.handle_monitor_update_completion_actions(update_actions);
2099
2100                 if let Some(forwards) = htlc_forwards {
2101                         $self.forward_htlcs(&mut [forwards][..]);
2102                 }
2103                 $self.finalize_claims(updates.finalized_claimed_htlcs);
2104                 for failure in updates.failed_htlcs.drain(..) {
2105                         let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
2106                         $self.fail_htlc_backwards_internal(&failure.0, &failure.1, &failure.2, receiver);
2107                 }
2108         } }
2109 }
2110
2111 macro_rules! handle_new_monitor_update {
2112         ($self: ident, $update_res: expr, $chan: expr, _internal, $completed: expr) => { {
2113                 debug_assert!($self.background_events_processed_since_startup.load(Ordering::Acquire));
2114                 match $update_res {
2115                         ChannelMonitorUpdateStatus::UnrecoverableError => {
2116                                 let err_str = "ChannelMonitor[Update] persistence failed unrecoverably. This indicates we cannot continue normal operation and must shut down.";
2117                                 log_error!($self.logger, "{}", err_str);
2118                                 panic!("{}", err_str);
2119                         },
2120                         ChannelMonitorUpdateStatus::InProgress => {
2121                                 log_debug!($self.logger, "ChannelMonitor update for {} in flight, holding messages until the update completes.",
2122                                         &$chan.context.channel_id());
2123                                 false
2124                         },
2125                         ChannelMonitorUpdateStatus::Completed => {
2126                                 $completed;
2127                                 true
2128                         },
2129                 }
2130         } };
2131         ($self: ident, $update_res: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr, INITIAL_MONITOR) => {
2132                 handle_new_monitor_update!($self, $update_res, $chan, _internal,
2133                         handle_monitor_update_completion!($self, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan))
2134         };
2135         ($self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr) => { {
2136                 let in_flight_updates = $peer_state.in_flight_monitor_updates.entry($funding_txo)
2137                         .or_insert_with(Vec::new);
2138                 // During startup, we push monitor updates as background events through to here in
2139                 // order to replay updates that were in-flight when we shut down. Thus, we have to
2140                 // filter for uniqueness here.
2141                 let idx = in_flight_updates.iter().position(|upd| upd == &$update)
2142                         .unwrap_or_else(|| {
2143                                 in_flight_updates.push($update);
2144                                 in_flight_updates.len() - 1
2145                         });
2146                 let update_res = $self.chain_monitor.update_channel($funding_txo, &in_flight_updates[idx]);
2147                 handle_new_monitor_update!($self, update_res, $chan, _internal,
2148                         {
2149                                 let _ = in_flight_updates.remove(idx);
2150                                 if in_flight_updates.is_empty() && $chan.blocked_monitor_updates_pending() == 0 {
2151                                         handle_monitor_update_completion!($self, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan);
2152                                 }
2153                         })
2154         } };
2155 }
2156
2157 macro_rules! process_events_body {
2158         ($self: expr, $event_to_handle: expr, $handle_event: expr) => {
2159                 let mut processed_all_events = false;
2160                 while !processed_all_events {
2161                         if $self.pending_events_processor.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed).is_err() {
2162                                 return;
2163                         }
2164
2165                         let mut result;
2166
2167                         {
2168                                 // We'll acquire our total consistency lock so that we can be sure no other
2169                                 // persists happen while processing monitor events.
2170                                 let _read_guard = $self.total_consistency_lock.read().unwrap();
2171
2172                                 // Because `handle_post_event_actions` may send `ChannelMonitorUpdate`s to the user we must
2173                                 // ensure any startup-generated background events are handled first.
2174                                 result = $self.process_background_events();
2175
2176                                 // TODO: This behavior should be documented. It's unintuitive that we query
2177                                 // ChannelMonitors when clearing other events.
2178                                 if $self.process_pending_monitor_events() {
2179                                         result = NotifyOption::DoPersist;
2180                                 }
2181                         }
2182
2183                         let pending_events = $self.pending_events.lock().unwrap().clone();
2184                         let num_events = pending_events.len();
2185                         if !pending_events.is_empty() {
2186                                 result = NotifyOption::DoPersist;
2187                         }
2188
2189                         let mut post_event_actions = Vec::new();
2190
2191                         for (event, action_opt) in pending_events {
2192                                 $event_to_handle = event;
2193                                 $handle_event;
2194                                 if let Some(action) = action_opt {
2195                                         post_event_actions.push(action);
2196                                 }
2197                         }
2198
2199                         {
2200                                 let mut pending_events = $self.pending_events.lock().unwrap();
2201                                 pending_events.drain(..num_events);
2202                                 processed_all_events = pending_events.is_empty();
2203                                 // Note that `push_pending_forwards_ev` relies on `pending_events_processor` being
2204                                 // updated here with the `pending_events` lock acquired.
2205                                 $self.pending_events_processor.store(false, Ordering::Release);
2206                         }
2207
2208                         if !post_event_actions.is_empty() {
2209                                 $self.handle_post_event_actions(post_event_actions);
2210                                 // If we had some actions, go around again as we may have more events now
2211                                 processed_all_events = false;
2212                         }
2213
2214                         match result {
2215                                 NotifyOption::DoPersist => {
2216                                         $self.needs_persist_flag.store(true, Ordering::Release);
2217                                         $self.event_persist_notifier.notify();
2218                                 },
2219                                 NotifyOption::SkipPersistHandleEvents =>
2220                                         $self.event_persist_notifier.notify(),
2221                                 NotifyOption::SkipPersistNoEvents => {},
2222                         }
2223                 }
2224         }
2225 }
2226
2227 impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> ChannelManager<M, T, ES, NS, SP, F, R, L>
2228 where
2229         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
2230         T::Target: BroadcasterInterface,
2231         ES::Target: EntropySource,
2232         NS::Target: NodeSigner,
2233         SP::Target: SignerProvider,
2234         F::Target: FeeEstimator,
2235         R::Target: Router,
2236         L::Target: Logger,
2237 {
2238         /// Constructs a new `ChannelManager` to hold several channels and route between them.
2239         ///
2240         /// The current time or latest block header time can be provided as the `current_timestamp`.
2241         ///
2242         /// This is the main "logic hub" for all channel-related actions, and implements
2243         /// [`ChannelMessageHandler`].
2244         ///
2245         /// Non-proportional fees are fixed according to our risk using the provided fee estimator.
2246         ///
2247         /// Users need to notify the new `ChannelManager` when a new block is connected or
2248         /// disconnected using its [`block_connected`] and [`block_disconnected`] methods, starting
2249         /// from after [`params.best_block.block_hash`]. See [`chain::Listen`] and [`chain::Confirm`] for
2250         /// more details.
2251         ///
2252         /// [`block_connected`]: chain::Listen::block_connected
2253         /// [`block_disconnected`]: chain::Listen::block_disconnected
2254         /// [`params.best_block.block_hash`]: chain::BestBlock::block_hash
2255         pub fn new(
2256                 fee_est: F, chain_monitor: M, tx_broadcaster: T, router: R, logger: L, entropy_source: ES,
2257                 node_signer: NS, signer_provider: SP, config: UserConfig, params: ChainParameters,
2258                 current_timestamp: u32,
2259         ) -> Self {
2260                 let mut secp_ctx = Secp256k1::new();
2261                 secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
2262                 let inbound_pmt_key_material = node_signer.get_inbound_payment_key_material();
2263                 let expanded_inbound_key = inbound_payment::ExpandedKey::new(&inbound_pmt_key_material);
2264                 ChannelManager {
2265                         default_configuration: config.clone(),
2266                         chain_hash: ChainHash::using_genesis_block(params.network),
2267                         fee_estimator: LowerBoundedFeeEstimator::new(fee_est),
2268                         chain_monitor,
2269                         tx_broadcaster,
2270                         router,
2271
2272                         best_block: RwLock::new(params.best_block),
2273
2274                         outbound_scid_aliases: Mutex::new(HashSet::new()),
2275                         pending_inbound_payments: Mutex::new(HashMap::new()),
2276                         pending_outbound_payments: OutboundPayments::new(),
2277                         forward_htlcs: Mutex::new(HashMap::new()),
2278                         claimable_payments: Mutex::new(ClaimablePayments { claimable_payments: HashMap::new(), pending_claiming_payments: HashMap::new() }),
2279                         pending_intercepted_htlcs: Mutex::new(HashMap::new()),
2280                         id_to_peer: Mutex::new(HashMap::new()),
2281                         short_to_chan_info: FairRwLock::new(HashMap::new()),
2282
2283                         our_network_pubkey: node_signer.get_node_id(Recipient::Node).unwrap(),
2284                         secp_ctx,
2285
2286                         inbound_payment_key: expanded_inbound_key,
2287                         fake_scid_rand_bytes: entropy_source.get_secure_random_bytes(),
2288
2289                         probing_cookie_secret: entropy_source.get_secure_random_bytes(),
2290
2291                         highest_seen_timestamp: AtomicUsize::new(current_timestamp as usize),
2292
2293                         per_peer_state: FairRwLock::new(HashMap::new()),
2294
2295                         pending_events: Mutex::new(VecDeque::new()),
2296                         pending_events_processor: AtomicBool::new(false),
2297                         pending_background_events: Mutex::new(Vec::new()),
2298                         total_consistency_lock: RwLock::new(()),
2299                         background_events_processed_since_startup: AtomicBool::new(false),
2300                         event_persist_notifier: Notifier::new(),
2301                         needs_persist_flag: AtomicBool::new(false),
2302                         funding_batch_states: Mutex::new(BTreeMap::new()),
2303
2304                         entropy_source,
2305                         node_signer,
2306                         signer_provider,
2307
2308                         logger,
2309                 }
2310         }
2311
2312         /// Gets the current configuration applied to all new channels.
2313         pub fn get_current_default_configuration(&self) -> &UserConfig {
2314                 &self.default_configuration
2315         }
2316
2317         fn create_and_insert_outbound_scid_alias(&self) -> u64 {
2318                 let height = self.best_block.read().unwrap().height();
2319                 let mut outbound_scid_alias = 0;
2320                 let mut i = 0;
2321                 loop {
2322                         if cfg!(fuzzing) { // fuzzing chacha20 doesn't use the key at all so we always get the same alias
2323                                 outbound_scid_alias += 1;
2324                         } else {
2325                                 outbound_scid_alias = fake_scid::Namespace::OutboundAlias.get_fake_scid(height, &self.chain_hash, &self.fake_scid_rand_bytes, &self.entropy_source);
2326                         }
2327                         if outbound_scid_alias != 0 && self.outbound_scid_aliases.lock().unwrap().insert(outbound_scid_alias) {
2328                                 break;
2329                         }
2330                         i += 1;
2331                         if i > 1_000_000 { 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"); }
2332                 }
2333                 outbound_scid_alias
2334         }
2335
2336         /// Creates a new outbound channel to the given remote node and with the given value.
2337         ///
2338         /// `user_channel_id` will be provided back as in
2339         /// [`Event::FundingGenerationReady::user_channel_id`] to allow tracking of which events
2340         /// correspond with which `create_channel` call. Note that the `user_channel_id` defaults to a
2341         /// randomized value for inbound channels. `user_channel_id` has no meaning inside of LDK, it
2342         /// is simply copied to events and otherwise ignored.
2343         ///
2344         /// Raises [`APIError::APIMisuseError`] when `channel_value_satoshis` > 2**24 or `push_msat` is
2345         /// greater than `channel_value_satoshis * 1k` or `channel_value_satoshis < 1000`.
2346         ///
2347         /// Raises [`APIError::ChannelUnavailable`] if the channel cannot be opened due to failing to
2348         /// generate a shutdown scriptpubkey or destination script set by
2349         /// [`SignerProvider::get_shutdown_scriptpubkey`] or [`SignerProvider::get_destination_script`].
2350         ///
2351         /// Note that we do not check if you are currently connected to the given peer. If no
2352         /// connection is available, the outbound `open_channel` message may fail to send, resulting in
2353         /// the channel eventually being silently forgotten (dropped on reload).
2354         ///
2355         /// Returns the new Channel's temporary `channel_id`. This ID will appear as
2356         /// [`Event::FundingGenerationReady::temporary_channel_id`] and in
2357         /// [`ChannelDetails::channel_id`] until after
2358         /// [`ChannelManager::funding_transaction_generated`] is called, swapping the Channel's ID for
2359         /// one derived from the funding transaction's TXID. If the counterparty rejects the channel
2360         /// immediately, this temporary ID will appear in [`Event::ChannelClosed::channel_id`].
2361         ///
2362         /// [`Event::FundingGenerationReady::user_channel_id`]: events::Event::FundingGenerationReady::user_channel_id
2363         /// [`Event::FundingGenerationReady::temporary_channel_id`]: events::Event::FundingGenerationReady::temporary_channel_id
2364         /// [`Event::ChannelClosed::channel_id`]: events::Event::ChannelClosed::channel_id
2365         pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_channel_id: u128, override_config: Option<UserConfig>) -> Result<ChannelId, APIError> {
2366                 if channel_value_satoshis < 1000 {
2367                         return Err(APIError::APIMisuseError { err: format!("Channel value must be at least 1000 satoshis. It was {}", channel_value_satoshis) });
2368                 }
2369
2370                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
2371                 // We want to make sure the lock is actually acquired by PersistenceNotifierGuard.
2372                 debug_assert!(&self.total_consistency_lock.try_write().is_err());
2373
2374                 let per_peer_state = self.per_peer_state.read().unwrap();
2375
2376                 let peer_state_mutex = per_peer_state.get(&their_network_key)
2377                         .ok_or_else(|| APIError::APIMisuseError{ err: format!("Not connected to node: {}", their_network_key) })?;
2378
2379                 let mut peer_state = peer_state_mutex.lock().unwrap();
2380                 let channel = {
2381                         let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
2382                         let their_features = &peer_state.latest_features;
2383                         let config = if override_config.is_some() { override_config.as_ref().unwrap() } else { &self.default_configuration };
2384                         match OutboundV1Channel::new(&self.fee_estimator, &self.entropy_source, &self.signer_provider, their_network_key,
2385                                 their_features, channel_value_satoshis, push_msat, user_channel_id, config,
2386                                 self.best_block.read().unwrap().height(), outbound_scid_alias)
2387                         {
2388                                 Ok(res) => res,
2389                                 Err(e) => {
2390                                         self.outbound_scid_aliases.lock().unwrap().remove(&outbound_scid_alias);
2391                                         return Err(e);
2392                                 },
2393                         }
2394                 };
2395                 let res = channel.get_open_channel(self.chain_hash);
2396
2397                 let temporary_channel_id = channel.context.channel_id();
2398                 match peer_state.channel_by_id.entry(temporary_channel_id) {
2399                         hash_map::Entry::Occupied(_) => {
2400                                 if cfg!(fuzzing) {
2401                                         return Err(APIError::APIMisuseError { err: "Fuzzy bad RNG".to_owned() });
2402                                 } else {
2403                                         panic!("RNG is bad???");
2404                                 }
2405                         },
2406                         hash_map::Entry::Vacant(entry) => { entry.insert(ChannelPhase::UnfundedOutboundV1(channel)); }
2407                 }
2408
2409                 peer_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
2410                         node_id: their_network_key,
2411                         msg: res,
2412                 });
2413                 Ok(temporary_channel_id)
2414         }
2415
2416         fn list_funded_channels_with_filter<Fn: FnMut(&(&ChannelId, &Channel<SP>)) -> bool + Copy>(&self, f: Fn) -> Vec<ChannelDetails> {
2417                 // Allocate our best estimate of the number of channels we have in the `res`
2418                 // Vec. Sadly the `short_to_chan_info` map doesn't cover channels without
2419                 // a scid or a scid alias, and the `id_to_peer` shouldn't be used outside
2420                 // of the ChannelMonitor handling. Therefore reallocations may still occur, but is
2421                 // unlikely as the `short_to_chan_info` map often contains 2 entries for
2422                 // the same channel.
2423                 let mut res = Vec::with_capacity(self.short_to_chan_info.read().unwrap().len());
2424                 {
2425                         let best_block_height = self.best_block.read().unwrap().height();
2426                         let per_peer_state = self.per_peer_state.read().unwrap();
2427                         for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
2428                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
2429                                 let peer_state = &mut *peer_state_lock;
2430                                 res.extend(peer_state.channel_by_id.iter()
2431                                         .filter_map(|(chan_id, phase)| match phase {
2432                                                 // Only `Channels` in the `ChannelPhase::Funded` phase can be considered funded.
2433                                                 ChannelPhase::Funded(chan) => Some((chan_id, chan)),
2434                                                 _ => None,
2435                                         })
2436                                         .filter(f)
2437                                         .map(|(_channel_id, channel)| {
2438                                                 ChannelDetails::from_channel_context(&channel.context, best_block_height,
2439                                                         peer_state.latest_features.clone(), &self.fee_estimator)
2440                                         })
2441                                 );
2442                         }
2443                 }
2444                 res
2445         }
2446
2447         /// Gets the list of open channels, in random order. See [`ChannelDetails`] field documentation for
2448         /// more information.
2449         pub fn list_channels(&self) -> Vec<ChannelDetails> {
2450                 // Allocate our best estimate of the number of channels we have in the `res`
2451                 // Vec. Sadly the `short_to_chan_info` map doesn't cover channels without
2452                 // a scid or a scid alias, and the `id_to_peer` shouldn't be used outside
2453                 // of the ChannelMonitor handling. Therefore reallocations may still occur, but is
2454                 // unlikely as the `short_to_chan_info` map often contains 2 entries for
2455                 // the same channel.
2456                 let mut res = Vec::with_capacity(self.short_to_chan_info.read().unwrap().len());
2457                 {
2458                         let best_block_height = self.best_block.read().unwrap().height();
2459                         let per_peer_state = self.per_peer_state.read().unwrap();
2460                         for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
2461                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
2462                                 let peer_state = &mut *peer_state_lock;
2463                                 for context in peer_state.channel_by_id.iter().map(|(_, phase)| phase.context()) {
2464                                         let details = ChannelDetails::from_channel_context(context, best_block_height,
2465                                                 peer_state.latest_features.clone(), &self.fee_estimator);
2466                                         res.push(details);
2467                                 }
2468                         }
2469                 }
2470                 res
2471         }
2472
2473         /// Gets the list of usable channels, in random order. Useful as an argument to
2474         /// [`Router::find_route`] to ensure non-announced channels are used.
2475         ///
2476         /// These are guaranteed to have their [`ChannelDetails::is_usable`] value set to true, see the
2477         /// documentation for [`ChannelDetails::is_usable`] for more info on exactly what the criteria
2478         /// are.
2479         pub fn list_usable_channels(&self) -> Vec<ChannelDetails> {
2480                 // Note we use is_live here instead of usable which leads to somewhat confused
2481                 // internal/external nomenclature, but that's ok cause that's probably what the user
2482                 // really wanted anyway.
2483                 self.list_funded_channels_with_filter(|&(_, ref channel)| channel.context.is_live())
2484         }
2485
2486         /// Gets the list of channels we have with a given counterparty, in random order.
2487         pub fn list_channels_with_counterparty(&self, counterparty_node_id: &PublicKey) -> Vec<ChannelDetails> {
2488                 let best_block_height = self.best_block.read().unwrap().height();
2489                 let per_peer_state = self.per_peer_state.read().unwrap();
2490
2491                 if let Some(peer_state_mutex) = per_peer_state.get(counterparty_node_id) {
2492                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
2493                         let peer_state = &mut *peer_state_lock;
2494                         let features = &peer_state.latest_features;
2495                         let context_to_details = |context| {
2496                                 ChannelDetails::from_channel_context(context, best_block_height, features.clone(), &self.fee_estimator)
2497                         };
2498                         return peer_state.channel_by_id
2499                                 .iter()
2500                                 .map(|(_, phase)| phase.context())
2501                                 .map(context_to_details)
2502                                 .collect();
2503                 }
2504                 vec![]
2505         }
2506
2507         /// Returns in an undefined order recent payments that -- if not fulfilled -- have yet to find a
2508         /// successful path, or have unresolved HTLCs.
2509         ///
2510         /// This can be useful for payments that may have been prepared, but ultimately not sent, as a
2511         /// result of a crash. If such a payment exists, is not listed here, and an
2512         /// [`Event::PaymentSent`] has not been received, you may consider resending the payment.
2513         ///
2514         /// [`Event::PaymentSent`]: events::Event::PaymentSent
2515         pub fn list_recent_payments(&self) -> Vec<RecentPaymentDetails> {
2516                 self.pending_outbound_payments.pending_outbound_payments.lock().unwrap().iter()
2517                         .filter_map(|(payment_id, pending_outbound_payment)| match pending_outbound_payment {
2518                                 PendingOutboundPayment::AwaitingInvoice { .. } => {
2519                                         Some(RecentPaymentDetails::AwaitingInvoice { payment_id: *payment_id })
2520                                 },
2521                                 // InvoiceReceived is an intermediate state and doesn't need to be exposed
2522                                 PendingOutboundPayment::InvoiceReceived { .. } => {
2523                                         Some(RecentPaymentDetails::AwaitingInvoice { payment_id: *payment_id })
2524                                 },
2525                                 PendingOutboundPayment::Retryable { payment_hash, total_msat, .. } => {
2526                                         Some(RecentPaymentDetails::Pending {
2527                                                 payment_id: *payment_id,
2528                                                 payment_hash: *payment_hash,
2529                                                 total_msat: *total_msat,
2530                                         })
2531                                 },
2532                                 PendingOutboundPayment::Abandoned { payment_hash, .. } => {
2533                                         Some(RecentPaymentDetails::Abandoned { payment_id: *payment_id, payment_hash: *payment_hash })
2534                                 },
2535                                 PendingOutboundPayment::Fulfilled { payment_hash, .. } => {
2536                                         Some(RecentPaymentDetails::Fulfilled { payment_id: *payment_id, payment_hash: *payment_hash })
2537                                 },
2538                                 PendingOutboundPayment::Legacy { .. } => None
2539                         })
2540                         .collect()
2541         }
2542
2543         /// Helper function that issues the channel close events
2544         fn issue_channel_close_events(&self, context: &ChannelContext<SP>, closure_reason: ClosureReason) {
2545                 let mut pending_events_lock = self.pending_events.lock().unwrap();
2546                 match context.unbroadcasted_funding() {
2547                         Some(transaction) => {
2548                                 pending_events_lock.push_back((events::Event::DiscardFunding {
2549                                         channel_id: context.channel_id(), transaction
2550                                 }, None));
2551                         },
2552                         None => {},
2553                 }
2554                 pending_events_lock.push_back((events::Event::ChannelClosed {
2555                         channel_id: context.channel_id(),
2556                         user_channel_id: context.get_user_id(),
2557                         reason: closure_reason,
2558                         counterparty_node_id: Some(context.get_counterparty_node_id()),
2559                         channel_capacity_sats: Some(context.get_value_satoshis()),
2560                 }, None));
2561         }
2562
2563         fn close_channel_internal(&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, target_feerate_sats_per_1000_weight: Option<u32>, override_shutdown_script: Option<ShutdownScript>) -> Result<(), APIError> {
2564                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
2565
2566                 let mut failed_htlcs: Vec<(HTLCSource, PaymentHash)>;
2567                 let shutdown_result;
2568                 loop {
2569                         let per_peer_state = self.per_peer_state.read().unwrap();
2570
2571                         let peer_state_mutex = per_peer_state.get(counterparty_node_id)
2572                                 .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
2573
2574                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
2575                         let peer_state = &mut *peer_state_lock;
2576
2577                         match peer_state.channel_by_id.entry(channel_id.clone()) {
2578                                 hash_map::Entry::Occupied(mut chan_phase_entry) => {
2579                                         if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
2580                                                 let funding_txo_opt = chan.context.get_funding_txo();
2581                                                 let their_features = &peer_state.latest_features;
2582                                                 let (shutdown_msg, mut monitor_update_opt, htlcs, local_shutdown_result) =
2583                                                         chan.get_shutdown(&self.signer_provider, their_features, target_feerate_sats_per_1000_weight, override_shutdown_script)?;
2584                                                 failed_htlcs = htlcs;
2585                                                 shutdown_result = local_shutdown_result;
2586                                                 debug_assert_eq!(shutdown_result.is_some(), chan.is_shutdown());
2587
2588                                                 // We can send the `shutdown` message before updating the `ChannelMonitor`
2589                                                 // here as we don't need the monitor update to complete until we send a
2590                                                 // `shutdown_signed`, which we'll delay if we're pending a monitor update.
2591                                                 peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
2592                                                         node_id: *counterparty_node_id,
2593                                                         msg: shutdown_msg,
2594                                                 });
2595
2596                                                 debug_assert!(monitor_update_opt.is_none() || !chan.is_shutdown(),
2597                                                         "We can't both complete shutdown and generate a monitor update");
2598
2599                                                 // Update the monitor with the shutdown script if necessary.
2600                                                 if let Some(monitor_update) = monitor_update_opt.take() {
2601                                                         handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
2602                                                                 peer_state_lock, peer_state, per_peer_state, chan);
2603                                                         break;
2604                                                 }
2605
2606                                                 if chan.is_shutdown() {
2607                                                         if let ChannelPhase::Funded(chan) = remove_channel_phase!(self, chan_phase_entry) {
2608                                                                 if let Ok(channel_update) = self.get_channel_update_for_broadcast(&chan) {
2609                                                                         peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2610                                                                                 msg: channel_update
2611                                                                         });
2612                                                                 }
2613                                                                 self.issue_channel_close_events(&chan.context, ClosureReason::HolderForceClosed);
2614                                                         }
2615                                                 }
2616                                                 break;
2617                                         }
2618                                 },
2619                                 hash_map::Entry::Vacant(_) => {
2620                                         // If we reach this point, it means that the channel_id either refers to an unfunded channel or
2621                                         // it does not exist for this peer. Either way, we can attempt to force-close it.
2622                                         //
2623                                         // An appropriate error will be returned for non-existence of the channel if that's the case.
2624                                         mem::drop(peer_state_lock);
2625                                         mem::drop(per_peer_state);
2626                                         return self.force_close_channel_with_peer(&channel_id, counterparty_node_id, None, false).map(|_| ())
2627                                 },
2628                         }
2629                 }
2630
2631                 for htlc_source in failed_htlcs.drain(..) {
2632                         let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
2633                         let receiver = HTLCDestination::NextHopChannel { node_id: Some(*counterparty_node_id), channel_id: *channel_id };
2634                         self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
2635                 }
2636
2637                 if let Some(shutdown_result) = shutdown_result {
2638                         self.finish_close_channel(shutdown_result);
2639                 }
2640
2641                 Ok(())
2642         }
2643
2644         /// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
2645         /// will be accepted on the given channel, and after additional timeout/the closing of all
2646         /// pending HTLCs, the channel will be closed on chain.
2647         ///
2648         ///  * If we are the channel initiator, we will pay between our [`Background`] and
2649         ///    [`ChannelConfig::force_close_avoidance_max_fee_satoshis`] plus our [`Normal`] fee
2650         ///    estimate.
2651         ///  * If our counterparty is the channel initiator, we will require a channel closing
2652         ///    transaction feerate of at least our [`Background`] feerate or the feerate which
2653         ///    would appear on a force-closure transaction, whichever is lower. We will allow our
2654         ///    counterparty to pay as much fee as they'd like, however.
2655         ///
2656         /// May generate a [`SendShutdown`] message event on success, which should be relayed.
2657         ///
2658         /// Raises [`APIError::ChannelUnavailable`] if the channel cannot be closed due to failing to
2659         /// generate a shutdown scriptpubkey or destination script set by
2660         /// [`SignerProvider::get_shutdown_scriptpubkey`]. A force-closure may be needed to close the
2661         /// channel.
2662         ///
2663         /// [`ChannelConfig::force_close_avoidance_max_fee_satoshis`]: crate::util::config::ChannelConfig::force_close_avoidance_max_fee_satoshis
2664         /// [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background
2665         /// [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal
2666         /// [`SendShutdown`]: crate::events::MessageSendEvent::SendShutdown
2667         pub fn close_channel(&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey) -> Result<(), APIError> {
2668                 self.close_channel_internal(channel_id, counterparty_node_id, None, None)
2669         }
2670
2671         /// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
2672         /// will be accepted on the given channel, and after additional timeout/the closing of all
2673         /// pending HTLCs, the channel will be closed on chain.
2674         ///
2675         /// `target_feerate_sat_per_1000_weight` has different meanings depending on if we initiated
2676         /// the channel being closed or not:
2677         ///  * If we are the channel initiator, we will pay at least this feerate on the closing
2678         ///    transaction. The upper-bound is set by
2679         ///    [`ChannelConfig::force_close_avoidance_max_fee_satoshis`] plus our [`Normal`] fee
2680         ///    estimate (or `target_feerate_sat_per_1000_weight`, if it is greater).
2681         ///  * If our counterparty is the channel initiator, we will refuse to accept a channel closure
2682         ///    transaction feerate below `target_feerate_sat_per_1000_weight` (or the feerate which
2683         ///    will appear on a force-closure transaction, whichever is lower).
2684         ///
2685         /// The `shutdown_script` provided  will be used as the `scriptPubKey` for the closing transaction.
2686         /// Will fail if a shutdown script has already been set for this channel by
2687         /// ['ChannelHandshakeConfig::commit_upfront_shutdown_pubkey`]. The given shutdown script must
2688         /// also be compatible with our and the counterparty's features.
2689         ///
2690         /// May generate a [`SendShutdown`] message event on success, which should be relayed.
2691         ///
2692         /// Raises [`APIError::ChannelUnavailable`] if the channel cannot be closed due to failing to
2693         /// generate a shutdown scriptpubkey or destination script set by
2694         /// [`SignerProvider::get_shutdown_scriptpubkey`]. A force-closure may be needed to close the
2695         /// channel.
2696         ///
2697         /// [`ChannelConfig::force_close_avoidance_max_fee_satoshis`]: crate::util::config::ChannelConfig::force_close_avoidance_max_fee_satoshis
2698         /// [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background
2699         /// [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal
2700         /// [`SendShutdown`]: crate::events::MessageSendEvent::SendShutdown
2701         pub fn close_channel_with_feerate_and_script(&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, target_feerate_sats_per_1000_weight: Option<u32>, shutdown_script: Option<ShutdownScript>) -> Result<(), APIError> {
2702                 self.close_channel_internal(channel_id, counterparty_node_id, target_feerate_sats_per_1000_weight, shutdown_script)
2703         }
2704
2705         fn finish_close_channel(&self, mut shutdown_res: ShutdownResult) {
2706                 debug_assert_ne!(self.per_peer_state.held_by_thread(), LockHeldState::HeldByThread);
2707                 #[cfg(debug_assertions)]
2708                 for (_, peer) in self.per_peer_state.read().unwrap().iter() {
2709                         debug_assert_ne!(peer.held_by_thread(), LockHeldState::HeldByThread);
2710                 }
2711
2712                 log_debug!(self.logger, "Finishing closure of channel with {} HTLCs to fail", shutdown_res.dropped_outbound_htlcs.len());
2713                 for htlc_source in shutdown_res.dropped_outbound_htlcs.drain(..) {
2714                         let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
2715                         let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
2716                         let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
2717                         self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
2718                 }
2719                 if let Some((_, funding_txo, monitor_update)) = shutdown_res.monitor_update {
2720                         // There isn't anything we can do if we get an update failure - we're already
2721                         // force-closing. The monitor update on the required in-memory copy should broadcast
2722                         // the latest local state, which is the best we can do anyway. Thus, it is safe to
2723                         // ignore the result here.
2724                         let _ = self.chain_monitor.update_channel(funding_txo, &monitor_update);
2725                 }
2726                 let mut shutdown_results = Vec::new();
2727                 if let Some(txid) = shutdown_res.unbroadcasted_batch_funding_txid {
2728                         let mut funding_batch_states = self.funding_batch_states.lock().unwrap();
2729                         let affected_channels = funding_batch_states.remove(&txid).into_iter().flatten();
2730                         let per_peer_state = self.per_peer_state.read().unwrap();
2731                         let mut has_uncompleted_channel = None;
2732                         for (channel_id, counterparty_node_id, state) in affected_channels {
2733                                 if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
2734                                         let mut peer_state = peer_state_mutex.lock().unwrap();
2735                                         if let Some(mut chan) = peer_state.channel_by_id.remove(&channel_id) {
2736                                                 update_maps_on_chan_removal!(self, &chan.context());
2737                                                 self.issue_channel_close_events(&chan.context(), ClosureReason::FundingBatchClosure);
2738                                                 shutdown_results.push(chan.context_mut().force_shutdown(false));
2739                                         }
2740                                 }
2741                                 has_uncompleted_channel = Some(has_uncompleted_channel.map_or(!state, |v| v || !state));
2742                         }
2743                         debug_assert!(
2744                                 has_uncompleted_channel.unwrap_or(true),
2745                                 "Closing a batch where all channels have completed initial monitor update",
2746                         );
2747                 }
2748                 for shutdown_result in shutdown_results.drain(..) {
2749                         self.finish_close_channel(shutdown_result);
2750                 }
2751         }
2752
2753         /// `peer_msg` should be set when we receive a message from a peer, but not set when the
2754         /// user closes, which will be re-exposed as the `ChannelClosed` reason.
2755         fn force_close_channel_with_peer(&self, channel_id: &ChannelId, peer_node_id: &PublicKey, peer_msg: Option<&String>, broadcast: bool)
2756         -> Result<PublicKey, APIError> {
2757                 let per_peer_state = self.per_peer_state.read().unwrap();
2758                 let peer_state_mutex = per_peer_state.get(peer_node_id)
2759                         .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", peer_node_id) })?;
2760                 let (update_opt, counterparty_node_id) = {
2761                         let mut peer_state = peer_state_mutex.lock().unwrap();
2762                         let closure_reason = if let Some(peer_msg) = peer_msg {
2763                                 ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(peer_msg.to_string()) }
2764                         } else {
2765                                 ClosureReason::HolderForceClosed
2766                         };
2767                         if let hash_map::Entry::Occupied(chan_phase_entry) = peer_state.channel_by_id.entry(channel_id.clone()) {
2768                                 log_error!(self.logger, "Force-closing channel {}", channel_id);
2769                                 self.issue_channel_close_events(&chan_phase_entry.get().context(), closure_reason);
2770                                 let mut chan_phase = remove_channel_phase!(self, chan_phase_entry);
2771                                 mem::drop(peer_state);
2772                                 mem::drop(per_peer_state);
2773                                 match chan_phase {
2774                                         ChannelPhase::Funded(mut chan) => {
2775                                                 self.finish_close_channel(chan.context.force_shutdown(broadcast));
2776                                                 (self.get_channel_update_for_broadcast(&chan).ok(), chan.context.get_counterparty_node_id())
2777                                         },
2778                                         ChannelPhase::UnfundedOutboundV1(_) | ChannelPhase::UnfundedInboundV1(_) => {
2779                                                 self.finish_close_channel(chan_phase.context_mut().force_shutdown(false));
2780                                                 // Unfunded channel has no update
2781                                                 (None, chan_phase.context().get_counterparty_node_id())
2782                                         },
2783                                 }
2784                         } else if peer_state.inbound_channel_request_by_id.remove(channel_id).is_some() {
2785                                 log_error!(self.logger, "Force-closing channel {}", &channel_id);
2786                                 // N.B. that we don't send any channel close event here: we
2787                                 // don't have a user_channel_id, and we never sent any opening
2788                                 // events anyway.
2789                                 (None, *peer_node_id)
2790                         } else {
2791                                 return Err(APIError::ChannelUnavailable{ err: format!("Channel with id {} not found for the passed counterparty node_id {}", channel_id, peer_node_id) });
2792                         }
2793                 };
2794                 if let Some(update) = update_opt {
2795                         // Try to send the `BroadcastChannelUpdate` to the peer we just force-closed on, but if
2796                         // not try to broadcast it via whatever peer we have.
2797                         let per_peer_state = self.per_peer_state.read().unwrap();
2798                         let a_peer_state_opt = per_peer_state.get(peer_node_id)
2799                                 .ok_or(per_peer_state.values().next());
2800                         if let Ok(a_peer_state_mutex) = a_peer_state_opt {
2801                                 let mut a_peer_state = a_peer_state_mutex.lock().unwrap();
2802                                 a_peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2803                                         msg: update
2804                                 });
2805                         }
2806                 }
2807
2808                 Ok(counterparty_node_id)
2809         }
2810
2811         fn force_close_sending_error(&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, broadcast: bool) -> Result<(), APIError> {
2812                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
2813                 match self.force_close_channel_with_peer(channel_id, counterparty_node_id, None, broadcast) {
2814                         Ok(counterparty_node_id) => {
2815                                 let per_peer_state = self.per_peer_state.read().unwrap();
2816                                 if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
2817                                         let mut peer_state = peer_state_mutex.lock().unwrap();
2818                                         peer_state.pending_msg_events.push(
2819                                                 events::MessageSendEvent::HandleError {
2820                                                         node_id: counterparty_node_id,
2821                                                         action: msgs::ErrorAction::DisconnectPeer {
2822                                                                 msg: Some(msgs::ErrorMessage { channel_id: *channel_id, data: "Channel force-closed".to_owned() })
2823                                                         },
2824                                                 }
2825                                         );
2826                                 }
2827                                 Ok(())
2828                         },
2829                         Err(e) => Err(e)
2830                 }
2831         }
2832
2833         /// Force closes a channel, immediately broadcasting the latest local transaction(s) and
2834         /// rejecting new HTLCs on the given channel. Fails if `channel_id` is unknown to
2835         /// the manager, or if the `counterparty_node_id` isn't the counterparty of the corresponding
2836         /// channel.
2837         pub fn force_close_broadcasting_latest_txn(&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey)
2838         -> Result<(), APIError> {
2839                 self.force_close_sending_error(channel_id, counterparty_node_id, true)
2840         }
2841
2842         /// Force closes a channel, rejecting new HTLCs on the given channel but skips broadcasting
2843         /// the latest local transaction(s). Fails if `channel_id` is unknown to the manager, or if the
2844         /// `counterparty_node_id` isn't the counterparty of the corresponding channel.
2845         ///
2846         /// You can always get the latest local transaction(s) to broadcast from
2847         /// [`ChannelMonitor::get_latest_holder_commitment_txn`].
2848         pub fn force_close_without_broadcasting_txn(&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey)
2849         -> Result<(), APIError> {
2850                 self.force_close_sending_error(channel_id, counterparty_node_id, false)
2851         }
2852
2853         /// Force close all channels, immediately broadcasting the latest local commitment transaction
2854         /// for each to the chain and rejecting new HTLCs on each.
2855         pub fn force_close_all_channels_broadcasting_latest_txn(&self) {
2856                 for chan in self.list_channels() {
2857                         let _ = self.force_close_broadcasting_latest_txn(&chan.channel_id, &chan.counterparty.node_id);
2858                 }
2859         }
2860
2861         /// Force close all channels rejecting new HTLCs on each but without broadcasting the latest
2862         /// local transaction(s).
2863         pub fn force_close_all_channels_without_broadcasting_txn(&self) {
2864                 for chan in self.list_channels() {
2865                         let _ = self.force_close_without_broadcasting_txn(&chan.channel_id, &chan.counterparty.node_id);
2866                 }
2867         }
2868
2869         fn construct_fwd_pending_htlc_info(
2870                 &self, msg: &msgs::UpdateAddHTLC, hop_data: msgs::InboundOnionPayload, hop_hmac: [u8; 32],
2871                 new_packet_bytes: [u8; onion_utils::ONION_DATA_LEN], shared_secret: [u8; 32],
2872                 next_packet_pubkey_opt: Option<Result<PublicKey, secp256k1::Error>>
2873         ) -> Result<PendingHTLCInfo, InboundOnionErr> {
2874                 debug_assert!(next_packet_pubkey_opt.is_some());
2875                 let outgoing_packet = msgs::OnionPacket {
2876                         version: 0,
2877                         public_key: next_packet_pubkey_opt.unwrap_or(Err(secp256k1::Error::InvalidPublicKey)),
2878                         hop_data: new_packet_bytes,
2879                         hmac: hop_hmac,
2880                 };
2881
2882                 let (short_channel_id, amt_to_forward, outgoing_cltv_value) = match hop_data {
2883                         msgs::InboundOnionPayload::Forward { short_channel_id, amt_to_forward, outgoing_cltv_value } =>
2884                                 (short_channel_id, amt_to_forward, outgoing_cltv_value),
2885                         msgs::InboundOnionPayload::Receive { .. } | msgs::InboundOnionPayload::BlindedReceive { .. } =>
2886                                 return Err(InboundOnionErr {
2887                                         msg: "Final Node OnionHopData provided for us as an intermediary node",
2888                                         err_code: 0x4000 | 22,
2889                                         err_data: Vec::new(),
2890                                 }),
2891                 };
2892
2893                 Ok(PendingHTLCInfo {
2894                         routing: PendingHTLCRouting::Forward {
2895                                 onion_packet: outgoing_packet,
2896                                 short_channel_id,
2897                         },
2898                         payment_hash: msg.payment_hash,
2899                         incoming_shared_secret: shared_secret,
2900                         incoming_amt_msat: Some(msg.amount_msat),
2901                         outgoing_amt_msat: amt_to_forward,
2902                         outgoing_cltv_value,
2903                         skimmed_fee_msat: None,
2904                 })
2905         }
2906
2907         fn construct_recv_pending_htlc_info(
2908                 &self, hop_data: msgs::InboundOnionPayload, shared_secret: [u8; 32], payment_hash: PaymentHash,
2909                 amt_msat: u64, cltv_expiry: u32, phantom_shared_secret: Option<[u8; 32]>, allow_underpay: bool,
2910                 counterparty_skimmed_fee_msat: Option<u64>,
2911         ) -> Result<PendingHTLCInfo, InboundOnionErr> {
2912                 let (payment_data, keysend_preimage, custom_tlvs, onion_amt_msat, outgoing_cltv_value, payment_metadata) = match hop_data {
2913                         msgs::InboundOnionPayload::Receive {
2914                                 payment_data, keysend_preimage, custom_tlvs, amt_msat, outgoing_cltv_value, payment_metadata, ..
2915                         } =>
2916                                 (payment_data, keysend_preimage, custom_tlvs, amt_msat, outgoing_cltv_value, payment_metadata),
2917                         msgs::InboundOnionPayload::BlindedReceive {
2918                                 amt_msat, total_msat, outgoing_cltv_value, payment_secret, ..
2919                         } => {
2920                                 let payment_data = msgs::FinalOnionHopData { payment_secret, total_msat };
2921                                 (Some(payment_data), None, Vec::new(), amt_msat, outgoing_cltv_value, None)
2922                         }
2923                         msgs::InboundOnionPayload::Forward { .. } => {
2924                                 return Err(InboundOnionErr {
2925                                         err_code: 0x4000|22,
2926                                         err_data: Vec::new(),
2927                                         msg: "Got non final data with an HMAC of 0",
2928                                 })
2929                         },
2930                 };
2931                 // final_incorrect_cltv_expiry
2932                 if outgoing_cltv_value > cltv_expiry {
2933                         return Err(InboundOnionErr {
2934                                 msg: "Upstream node set CLTV to less than the CLTV set by the sender",
2935                                 err_code: 18,
2936                                 err_data: cltv_expiry.to_be_bytes().to_vec()
2937                         })
2938                 }
2939                 // final_expiry_too_soon
2940                 // We have to have some headroom to broadcast on chain if we have the preimage, so make sure
2941                 // we have at least HTLC_FAIL_BACK_BUFFER blocks to go.
2942                 //
2943                 // Also, ensure that, in the case of an unknown preimage for the received payment hash, our
2944                 // payment logic has enough time to fail the HTLC backward before our onchain logic triggers a
2945                 // channel closure (see HTLC_FAIL_BACK_BUFFER rationale).
2946                 let current_height: u32 = self.best_block.read().unwrap().height();
2947                 if cltv_expiry <= current_height + HTLC_FAIL_BACK_BUFFER + 1 {
2948                         let mut err_data = Vec::with_capacity(12);
2949                         err_data.extend_from_slice(&amt_msat.to_be_bytes());
2950                         err_data.extend_from_slice(&current_height.to_be_bytes());
2951                         return Err(InboundOnionErr {
2952                                 err_code: 0x4000 | 15, err_data,
2953                                 msg: "The final CLTV expiry is too soon to handle",
2954                         });
2955                 }
2956                 if (!allow_underpay && onion_amt_msat > amt_msat) ||
2957                         (allow_underpay && onion_amt_msat >
2958                          amt_msat.saturating_add(counterparty_skimmed_fee_msat.unwrap_or(0)))
2959                 {
2960                         return Err(InboundOnionErr {
2961                                 err_code: 19,
2962                                 err_data: amt_msat.to_be_bytes().to_vec(),
2963                                 msg: "Upstream node sent less than we were supposed to receive in payment",
2964                         });
2965                 }
2966
2967                 let routing = if let Some(payment_preimage) = keysend_preimage {
2968                         // We need to check that the sender knows the keysend preimage before processing this
2969                         // payment further. Otherwise, an intermediary routing hop forwarding non-keysend-HTLC X
2970                         // could discover the final destination of X, by probing the adjacent nodes on the route
2971                         // with a keysend payment of identical payment hash to X and observing the processing
2972                         // time discrepancies due to a hash collision with X.
2973                         let hashed_preimage = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
2974                         if hashed_preimage != payment_hash {
2975                                 return Err(InboundOnionErr {
2976                                         err_code: 0x4000|22,
2977                                         err_data: Vec::new(),
2978                                         msg: "Payment preimage didn't match payment hash",
2979                                 });
2980                         }
2981                         if !self.default_configuration.accept_mpp_keysend && payment_data.is_some() {
2982                                 return Err(InboundOnionErr {
2983                                         err_code: 0x4000|22,
2984                                         err_data: Vec::new(),
2985                                         msg: "We don't support MPP keysend payments",
2986                                 });
2987                         }
2988                         PendingHTLCRouting::ReceiveKeysend {
2989                                 payment_data,
2990                                 payment_preimage,
2991                                 payment_metadata,
2992                                 incoming_cltv_expiry: outgoing_cltv_value,
2993                                 custom_tlvs,
2994                         }
2995                 } else if let Some(data) = payment_data {
2996                         PendingHTLCRouting::Receive {
2997                                 payment_data: data,
2998                                 payment_metadata,
2999                                 incoming_cltv_expiry: outgoing_cltv_value,
3000                                 phantom_shared_secret,
3001                                 custom_tlvs,
3002                         }
3003                 } else {
3004                         return Err(InboundOnionErr {
3005                                 err_code: 0x4000|0x2000|3,
3006                                 err_data: Vec::new(),
3007                                 msg: "We require payment_secrets",
3008                         });
3009                 };
3010                 Ok(PendingHTLCInfo {
3011                         routing,
3012                         payment_hash,
3013                         incoming_shared_secret: shared_secret,
3014                         incoming_amt_msat: Some(amt_msat),
3015                         outgoing_amt_msat: onion_amt_msat,
3016                         outgoing_cltv_value,
3017                         skimmed_fee_msat: counterparty_skimmed_fee_msat,
3018                 })
3019         }
3020
3021         fn decode_update_add_htlc_onion(
3022                 &self, msg: &msgs::UpdateAddHTLC
3023         ) -> Result<(onion_utils::Hop, [u8; 32], Option<Result<PublicKey, secp256k1::Error>>), HTLCFailureMsg> {
3024                 macro_rules! return_malformed_err {
3025                         ($msg: expr, $err_code: expr) => {
3026                                 {
3027                                         log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
3028                                         return Err(HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
3029                                                 channel_id: msg.channel_id,
3030                                                 htlc_id: msg.htlc_id,
3031                                                 sha256_of_onion: Sha256::hash(&msg.onion_routing_packet.hop_data).into_inner(),
3032                                                 failure_code: $err_code,
3033                                         }));
3034                                 }
3035                         }
3036                 }
3037
3038                 if let Err(_) = msg.onion_routing_packet.public_key {
3039                         return_malformed_err!("invalid ephemeral pubkey", 0x8000 | 0x4000 | 6);
3040                 }
3041
3042                 let shared_secret = self.node_signer.ecdh(
3043                         Recipient::Node, &msg.onion_routing_packet.public_key.unwrap(), None
3044                 ).unwrap().secret_bytes();
3045
3046                 if msg.onion_routing_packet.version != 0 {
3047                         //TODO: Spec doesn't indicate if we should only hash hop_data here (and in other
3048                         //sha256_of_onion error data packets), or the entire onion_routing_packet. Either way,
3049                         //the hash doesn't really serve any purpose - in the case of hashing all data, the
3050                         //receiving node would have to brute force to figure out which version was put in the
3051                         //packet by the node that send us the message, in the case of hashing the hop_data, the
3052                         //node knows the HMAC matched, so they already know what is there...
3053                         return_malformed_err!("Unknown onion packet version", 0x8000 | 0x4000 | 4);
3054                 }
3055                 macro_rules! return_err {
3056                         ($msg: expr, $err_code: expr, $data: expr) => {
3057                                 {
3058                                         log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
3059                                         return Err(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
3060                                                 channel_id: msg.channel_id,
3061                                                 htlc_id: msg.htlc_id,
3062                                                 reason: HTLCFailReason::reason($err_code, $data.to_vec())
3063                                                         .get_encrypted_failure_packet(&shared_secret, &None),
3064                                         }));
3065                                 }
3066                         }
3067                 }
3068
3069                 let next_hop = match onion_utils::decode_next_payment_hop(
3070                         shared_secret, &msg.onion_routing_packet.hop_data[..], msg.onion_routing_packet.hmac,
3071                         msg.payment_hash, &self.node_signer
3072                 ) {
3073                         Ok(res) => res,
3074                         Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code }) => {
3075                                 return_malformed_err!(err_msg, err_code);
3076                         },
3077                         Err(onion_utils::OnionDecodeErr::Relay { err_msg, err_code }) => {
3078                                 return_err!(err_msg, err_code, &[0; 0]);
3079                         },
3080                 };
3081                 let (outgoing_scid, outgoing_amt_msat, outgoing_cltv_value, next_packet_pk_opt) = match next_hop {
3082                         onion_utils::Hop::Forward {
3083                                 next_hop_data: msgs::InboundOnionPayload::Forward {
3084                                         short_channel_id, amt_to_forward, outgoing_cltv_value
3085                                 }, ..
3086                         } => {
3087                                 let next_packet_pk = onion_utils::next_hop_pubkey(&self.secp_ctx,
3088                                         msg.onion_routing_packet.public_key.unwrap(), &shared_secret);
3089                                 (short_channel_id, amt_to_forward, outgoing_cltv_value, Some(next_packet_pk))
3090                         },
3091                         // We'll do receive checks in [`Self::construct_pending_htlc_info`] so we have access to the
3092                         // inbound channel's state.
3093                         onion_utils::Hop::Receive { .. } => return Ok((next_hop, shared_secret, None)),
3094                         onion_utils::Hop::Forward { next_hop_data: msgs::InboundOnionPayload::Receive { .. }, .. } |
3095                                 onion_utils::Hop::Forward { next_hop_data: msgs::InboundOnionPayload::BlindedReceive { .. }, .. } =>
3096                         {
3097                                 return_err!("Final Node OnionHopData provided for us as an intermediary node", 0x4000 | 22, &[0; 0]);
3098                         }
3099                 };
3100
3101                 // Perform outbound checks here instead of in [`Self::construct_pending_htlc_info`] because we
3102                 // can't hold the outbound peer state lock at the same time as the inbound peer state lock.
3103                 if let Some((err, mut code, chan_update)) = loop {
3104                         let id_option = self.short_to_chan_info.read().unwrap().get(&outgoing_scid).cloned();
3105                         let forwarding_chan_info_opt = match id_option {
3106                                 None => { // unknown_next_peer
3107                                         // Note that this is likely a timing oracle for detecting whether an scid is a
3108                                         // phantom or an intercept.
3109                                         if (self.default_configuration.accept_intercept_htlcs &&
3110                                                 fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, outgoing_scid, &self.chain_hash)) ||
3111                                                 fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, outgoing_scid, &self.chain_hash)
3112                                         {
3113                                                 None
3114                                         } else {
3115                                                 break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
3116                                         }
3117                                 },
3118                                 Some((cp_id, id)) => Some((cp_id.clone(), id.clone())),
3119                         };
3120                         let chan_update_opt = if let Some((counterparty_node_id, forwarding_id)) = forwarding_chan_info_opt {
3121                                 let per_peer_state = self.per_peer_state.read().unwrap();
3122                                 let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
3123                                 if peer_state_mutex_opt.is_none() {
3124                                         break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
3125                                 }
3126                                 let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
3127                                 let peer_state = &mut *peer_state_lock;
3128                                 let chan = match peer_state.channel_by_id.get_mut(&forwarding_id).map(
3129                                         |chan_phase| if let ChannelPhase::Funded(chan) = chan_phase { Some(chan) } else { None }
3130                                 ).flatten() {
3131                                         None => {
3132                                                 // Channel was removed. The short_to_chan_info and channel_by_id maps
3133                                                 // have no consistency guarantees.
3134                                                 break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
3135                                         },
3136                                         Some(chan) => chan
3137                                 };
3138                                 if !chan.context.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
3139                                         // Note that the behavior here should be identical to the above block - we
3140                                         // should NOT reveal the existence or non-existence of a private channel if
3141                                         // we don't allow forwards outbound over them.
3142                                         break Some(("Refusing to forward to a private channel based on our config.", 0x4000 | 10, None));
3143                                 }
3144                                 if chan.context.get_channel_type().supports_scid_privacy() && outgoing_scid != chan.context.outbound_scid_alias() {
3145                                         // `option_scid_alias` (referred to in LDK as `scid_privacy`) means
3146                                         // "refuse to forward unless the SCID alias was used", so we pretend
3147                                         // we don't have the channel here.
3148                                         break Some(("Refusing to forward over real channel SCID as our counterparty requested.", 0x4000 | 10, None));
3149                                 }
3150                                 let chan_update_opt = self.get_channel_update_for_onion(outgoing_scid, chan).ok();
3151
3152                                 // Note that we could technically not return an error yet here and just hope
3153                                 // that the connection is reestablished or monitor updated by the time we get
3154                                 // around to doing the actual forward, but better to fail early if we can and
3155                                 // hopefully an attacker trying to path-trace payments cannot make this occur
3156                                 // on a small/per-node/per-channel scale.
3157                                 if !chan.context.is_live() { // channel_disabled
3158                                         // If the channel_update we're going to return is disabled (i.e. the
3159                                         // peer has been disabled for some time), return `channel_disabled`,
3160                                         // otherwise return `temporary_channel_failure`.
3161                                         if chan_update_opt.as_ref().map(|u| u.contents.flags & 2 == 2).unwrap_or(false) {
3162                                                 break Some(("Forwarding channel has been disconnected for some time.", 0x1000 | 20, chan_update_opt));
3163                                         } else {
3164                                                 break Some(("Forwarding channel is not in a ready state.", 0x1000 | 7, chan_update_opt));
3165                                         }
3166                                 }
3167                                 if outgoing_amt_msat < chan.context.get_counterparty_htlc_minimum_msat() { // amount_below_minimum
3168                                         break Some(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11, chan_update_opt));
3169                                 }
3170                                 if let Err((err, code)) = chan.htlc_satisfies_config(&msg, outgoing_amt_msat, outgoing_cltv_value) {
3171                                         break Some((err, code, chan_update_opt));
3172                                 }
3173                                 chan_update_opt
3174                         } else {
3175                                 if (msg.cltv_expiry as u64) < (outgoing_cltv_value) as u64 + MIN_CLTV_EXPIRY_DELTA as u64 {
3176                                         // We really should set `incorrect_cltv_expiry` here but as we're not
3177                                         // forwarding over a real channel we can't generate a channel_update
3178                                         // for it. Instead we just return a generic temporary_node_failure.
3179                                         break Some((
3180                                                         "Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta",
3181                                                         0x2000 | 2, None,
3182                                         ));
3183                                 }
3184                                 None
3185                         };
3186
3187                         let cur_height = self.best_block.read().unwrap().height() + 1;
3188                         // Theoretically, channel counterparty shouldn't send us a HTLC expiring now,
3189                         // but we want to be robust wrt to counterparty packet sanitization (see
3190                         // HTLC_FAIL_BACK_BUFFER rationale).
3191                         if msg.cltv_expiry <= cur_height + HTLC_FAIL_BACK_BUFFER as u32 { // expiry_too_soon
3192                                 break Some(("CLTV expiry is too close", 0x1000 | 14, chan_update_opt));
3193                         }
3194                         if msg.cltv_expiry > cur_height + CLTV_FAR_FAR_AWAY as u32 { // expiry_too_far
3195                                 break Some(("CLTV expiry is too far in the future", 21, None));
3196                         }
3197                         // If the HTLC expires ~now, don't bother trying to forward it to our
3198                         // counterparty. They should fail it anyway, but we don't want to bother with
3199                         // the round-trips or risk them deciding they definitely want the HTLC and
3200                         // force-closing to ensure they get it if we're offline.
3201                         // We previously had a much more aggressive check here which tried to ensure
3202                         // our counterparty receives an HTLC which has *our* risk threshold met on it,
3203                         // but there is no need to do that, and since we're a bit conservative with our
3204                         // risk threshold it just results in failing to forward payments.
3205                         if (outgoing_cltv_value) as u64 <= (cur_height + LATENCY_GRACE_PERIOD_BLOCKS) as u64 {
3206                                 break Some(("Outgoing CLTV value is too soon", 0x1000 | 14, chan_update_opt));
3207                         }
3208
3209                         break None;
3210                 }
3211                 {
3212                         let mut res = VecWriter(Vec::with_capacity(chan_update.serialized_length() + 2 + 8 + 2));
3213                         if let Some(chan_update) = chan_update {
3214                                 if code == 0x1000 | 11 || code == 0x1000 | 12 {
3215                                         msg.amount_msat.write(&mut res).expect("Writes cannot fail");
3216                                 }
3217                                 else if code == 0x1000 | 13 {
3218                                         msg.cltv_expiry.write(&mut res).expect("Writes cannot fail");
3219                                 }
3220                                 else if code == 0x1000 | 20 {
3221                                         // TODO: underspecified, follow https://github.com/lightning/bolts/issues/791
3222                                         0u16.write(&mut res).expect("Writes cannot fail");
3223                                 }
3224                                 (chan_update.serialized_length() as u16 + 2).write(&mut res).expect("Writes cannot fail");
3225                                 msgs::ChannelUpdate::TYPE.write(&mut res).expect("Writes cannot fail");
3226                                 chan_update.write(&mut res).expect("Writes cannot fail");
3227                         } else if code & 0x1000 == 0x1000 {
3228                                 // If we're trying to return an error that requires a `channel_update` but
3229                                 // we're forwarding to a phantom or intercept "channel" (i.e. cannot
3230                                 // generate an update), just use the generic "temporary_node_failure"
3231                                 // instead.
3232                                 code = 0x2000 | 2;
3233                         }
3234                         return_err!(err, code, &res.0[..]);
3235                 }
3236                 Ok((next_hop, shared_secret, next_packet_pk_opt))
3237         }
3238
3239         fn construct_pending_htlc_status<'a>(
3240                 &self, msg: &msgs::UpdateAddHTLC, shared_secret: [u8; 32], decoded_hop: onion_utils::Hop,
3241                 allow_underpay: bool, next_packet_pubkey_opt: Option<Result<PublicKey, secp256k1::Error>>
3242         ) -> PendingHTLCStatus {
3243                 macro_rules! return_err {
3244                         ($msg: expr, $err_code: expr, $data: expr) => {
3245                                 {
3246                                         log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
3247                                         return PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
3248                                                 channel_id: msg.channel_id,
3249                                                 htlc_id: msg.htlc_id,
3250                                                 reason: HTLCFailReason::reason($err_code, $data.to_vec())
3251                                                         .get_encrypted_failure_packet(&shared_secret, &None),
3252                                         }));
3253                                 }
3254                         }
3255                 }
3256                 match decoded_hop {
3257                         onion_utils::Hop::Receive(next_hop_data) => {
3258                                 // OUR PAYMENT!
3259                                 match self.construct_recv_pending_htlc_info(next_hop_data, shared_secret, msg.payment_hash,
3260                                         msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat)
3261                                 {
3262                                         Ok(info) => {
3263                                                 // Note that we could obviously respond immediately with an update_fulfill_htlc
3264                                                 // message, however that would leak that we are the recipient of this payment, so
3265                                                 // instead we stay symmetric with the forwarding case, only responding (after a
3266                                                 // delay) once they've send us a commitment_signed!
3267                                                 PendingHTLCStatus::Forward(info)
3268                                         },
3269                                         Err(InboundOnionErr { err_code, err_data, msg }) => return_err!(msg, err_code, &err_data)
3270                                 }
3271                         },
3272                         onion_utils::Hop::Forward { next_hop_data, next_hop_hmac, new_packet_bytes } => {
3273                                 match self.construct_fwd_pending_htlc_info(msg, next_hop_data, next_hop_hmac,
3274                                         new_packet_bytes, shared_secret, next_packet_pubkey_opt) {
3275                                         Ok(info) => PendingHTLCStatus::Forward(info),
3276                                         Err(InboundOnionErr { err_code, err_data, msg }) => return_err!(msg, err_code, &err_data)
3277                                 }
3278                         }
3279                 }
3280         }
3281
3282         /// Gets the current [`channel_update`] for the given channel. This first checks if the channel is
3283         /// public, and thus should be called whenever the result is going to be passed out in a
3284         /// [`MessageSendEvent::BroadcastChannelUpdate`] event.
3285         ///
3286         /// Note that in [`internal_closing_signed`], this function is called without the `peer_state`
3287         /// corresponding to the channel's counterparty locked, as the channel been removed from the
3288         /// storage and the `peer_state` lock has been dropped.
3289         ///
3290         /// [`channel_update`]: msgs::ChannelUpdate
3291         /// [`internal_closing_signed`]: Self::internal_closing_signed
3292         fn get_channel_update_for_broadcast(&self, chan: &Channel<SP>) -> Result<msgs::ChannelUpdate, LightningError> {
3293                 if !chan.context.should_announce() {
3294                         return Err(LightningError {
3295                                 err: "Cannot broadcast a channel_update for a private channel".to_owned(),
3296                                 action: msgs::ErrorAction::IgnoreError
3297                         });
3298                 }
3299                 if chan.context.get_short_channel_id().is_none() {
3300                         return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError});
3301                 }
3302                 log_trace!(self.logger, "Attempting to generate broadcast channel update for channel {}", &chan.context.channel_id());
3303                 self.get_channel_update_for_unicast(chan)
3304         }
3305
3306         /// Gets the current [`channel_update`] for the given channel. This does not check if the channel
3307         /// is public (only returning an `Err` if the channel does not yet have an assigned SCID),
3308         /// and thus MUST NOT be called unless the recipient of the resulting message has already
3309         /// provided evidence that they know about the existence of the channel.
3310         ///
3311         /// Note that through [`internal_closing_signed`], this function is called without the
3312         /// `peer_state`  corresponding to the channel's counterparty locked, as the channel been
3313         /// removed from the storage and the `peer_state` lock has been dropped.
3314         ///
3315         /// [`channel_update`]: msgs::ChannelUpdate
3316         /// [`internal_closing_signed`]: Self::internal_closing_signed
3317         fn get_channel_update_for_unicast(&self, chan: &Channel<SP>) -> Result<msgs::ChannelUpdate, LightningError> {
3318                 log_trace!(self.logger, "Attempting to generate channel update for channel {}", &chan.context.channel_id());
3319                 let short_channel_id = match chan.context.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
3320                         None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
3321                         Some(id) => id,
3322                 };
3323
3324                 self.get_channel_update_for_onion(short_channel_id, chan)
3325         }
3326
3327         fn get_channel_update_for_onion(&self, short_channel_id: u64, chan: &Channel<SP>) -> Result<msgs::ChannelUpdate, LightningError> {
3328                 log_trace!(self.logger, "Generating channel update for channel {}", &chan.context.channel_id());
3329                 let were_node_one = self.our_network_pubkey.serialize()[..] < chan.context.get_counterparty_node_id().serialize()[..];
3330
3331                 let enabled = chan.context.is_usable() && match chan.channel_update_status() {
3332                         ChannelUpdateStatus::Enabled => true,
3333                         ChannelUpdateStatus::DisabledStaged(_) => true,
3334                         ChannelUpdateStatus::Disabled => false,
3335                         ChannelUpdateStatus::EnabledStaged(_) => false,
3336                 };
3337
3338                 let unsigned = msgs::UnsignedChannelUpdate {
3339                         chain_hash: self.chain_hash,
3340                         short_channel_id,
3341                         timestamp: chan.context.get_update_time_counter(),
3342                         flags: (!were_node_one) as u8 | ((!enabled as u8) << 1),
3343                         cltv_expiry_delta: chan.context.get_cltv_expiry_delta(),
3344                         htlc_minimum_msat: chan.context.get_counterparty_htlc_minimum_msat(),
3345                         htlc_maximum_msat: chan.context.get_announced_htlc_max_msat(),
3346                         fee_base_msat: chan.context.get_outbound_forwarding_fee_base_msat(),
3347                         fee_proportional_millionths: chan.context.get_fee_proportional_millionths(),
3348                         excess_data: Vec::new(),
3349                 };
3350                 // Panic on failure to signal LDK should be restarted to retry signing the `ChannelUpdate`.
3351                 // If we returned an error and the `node_signer` cannot provide a signature for whatever
3352                 // reason`, we wouldn't be able to receive inbound payments through the corresponding
3353                 // channel.
3354                 let sig = self.node_signer.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelUpdate(&unsigned)).unwrap();
3355
3356                 Ok(msgs::ChannelUpdate {
3357                         signature: sig,
3358                         contents: unsigned
3359                 })
3360         }
3361
3362         #[cfg(test)]
3363         pub(crate) fn test_send_payment_along_path(&self, path: &Path, payment_hash: &PaymentHash, recipient_onion: RecipientOnionFields, total_value: u64, cur_height: u32, payment_id: PaymentId, keysend_preimage: &Option<PaymentPreimage>, session_priv_bytes: [u8; 32]) -> Result<(), APIError> {
3364                 let _lck = self.total_consistency_lock.read().unwrap();
3365                 self.send_payment_along_path(SendAlongPathArgs {
3366                         path, payment_hash, recipient_onion, total_value, cur_height, payment_id, keysend_preimage,
3367                         session_priv_bytes
3368                 })
3369         }
3370
3371         fn send_payment_along_path(&self, args: SendAlongPathArgs) -> Result<(), APIError> {
3372                 let SendAlongPathArgs {
3373                         path, payment_hash, recipient_onion, total_value, cur_height, payment_id, keysend_preimage,
3374                         session_priv_bytes
3375                 } = args;
3376                 // The top-level caller should hold the total_consistency_lock read lock.
3377                 debug_assert!(self.total_consistency_lock.try_write().is_err());
3378
3379                 log_trace!(self.logger,
3380                         "Attempting to send payment with payment hash {} along path with next hop {}",
3381                         payment_hash, path.hops.first().unwrap().short_channel_id);
3382                 let prng_seed = self.entropy_source.get_secure_random_bytes();
3383                 let session_priv = SecretKey::from_slice(&session_priv_bytes[..]).expect("RNG is busted");
3384
3385                 let onion_keys = onion_utils::construct_onion_keys(&self.secp_ctx, &path, &session_priv)
3386                         .map_err(|_| APIError::InvalidRoute{err: "Pubkey along hop was maliciously selected".to_owned()})?;
3387                 let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(path, total_value, recipient_onion, cur_height, keysend_preimage)?;
3388
3389                 let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, prng_seed, payment_hash)
3390                         .map_err(|_| APIError::InvalidRoute { err: "Route size too large considering onion data".to_owned()})?;
3391
3392                 let err: Result<(), _> = loop {
3393                         let (counterparty_node_id, id) = match self.short_to_chan_info.read().unwrap().get(&path.hops.first().unwrap().short_channel_id) {
3394                                 None => return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()}),
3395                                 Some((cp_id, chan_id)) => (cp_id.clone(), chan_id.clone()),
3396                         };
3397
3398                         let per_peer_state = self.per_peer_state.read().unwrap();
3399                         let peer_state_mutex = per_peer_state.get(&counterparty_node_id)
3400                                 .ok_or_else(|| APIError::ChannelUnavailable{err: "No peer matching the path's first hop found!".to_owned() })?;
3401                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
3402                         let peer_state = &mut *peer_state_lock;
3403                         if let hash_map::Entry::Occupied(mut chan_phase_entry) = peer_state.channel_by_id.entry(id) {
3404                                 match chan_phase_entry.get_mut() {
3405                                         ChannelPhase::Funded(chan) => {
3406                                                 if !chan.context.is_live() {
3407                                                         return Err(APIError::ChannelUnavailable{err: "Peer for first hop currently disconnected".to_owned()});
3408                                                 }
3409                                                 let funding_txo = chan.context.get_funding_txo().unwrap();
3410                                                 let send_res = chan.send_htlc_and_commit(htlc_msat, payment_hash.clone(),
3411                                                         htlc_cltv, HTLCSource::OutboundRoute {
3412                                                                 path: path.clone(),
3413                                                                 session_priv: session_priv.clone(),
3414                                                                 first_hop_htlc_msat: htlc_msat,
3415                                                                 payment_id,
3416                                                         }, onion_packet, None, &self.fee_estimator, &self.logger);
3417                                                 match break_chan_phase_entry!(self, send_res, chan_phase_entry) {
3418                                                         Some(monitor_update) => {
3419                                                                 match handle_new_monitor_update!(self, funding_txo, monitor_update, peer_state_lock, peer_state, per_peer_state, chan) {
3420                                                                         false => {
3421                                                                                 // Note that MonitorUpdateInProgress here indicates (per function
3422                                                                                 // docs) that we will resend the commitment update once monitor
3423                                                                                 // updating completes. Therefore, we must return an error
3424                                                                                 // indicating that it is unsafe to retry the payment wholesale,
3425                                                                                 // which we do in the send_payment check for
3426                                                                                 // MonitorUpdateInProgress, below.
3427                                                                                 return Err(APIError::MonitorUpdateInProgress);
3428                                                                         },
3429                                                                         true => {},
3430                                                                 }
3431                                                         },
3432                                                         None => {},
3433                                                 }
3434                                         },
3435                                         _ => return Err(APIError::ChannelUnavailable{err: "Channel to first hop is unfunded".to_owned()}),
3436                                 };
3437                         } else {
3438                                 // The channel was likely removed after we fetched the id from the
3439                                 // `short_to_chan_info` map, but before we successfully locked the
3440                                 // `channel_by_id` map.
3441                                 // This can occur as no consistency guarantees exists between the two maps.
3442                                 return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()});
3443                         }
3444                         return Ok(());
3445                 };
3446
3447                 match handle_error!(self, err, path.hops.first().unwrap().pubkey) {
3448                         Ok(_) => unreachable!(),
3449                         Err(e) => {
3450                                 Err(APIError::ChannelUnavailable { err: e.err })
3451                         },
3452                 }
3453         }
3454
3455         /// Sends a payment along a given route.
3456         ///
3457         /// Value parameters are provided via the last hop in route, see documentation for [`RouteHop`]
3458         /// fields for more info.
3459         ///
3460         /// May generate [`UpdateHTLCs`] message(s) event on success, which should be relayed (e.g. via
3461         /// [`PeerManager::process_events`]).
3462         ///
3463         /// # Avoiding Duplicate Payments
3464         ///
3465         /// If a pending payment is currently in-flight with the same [`PaymentId`] provided, this
3466         /// method will error with an [`APIError::InvalidRoute`]. Note, however, that once a payment
3467         /// is no longer pending (either via [`ChannelManager::abandon_payment`], or handling of an
3468         /// [`Event::PaymentSent`] or [`Event::PaymentFailed`]) LDK will not stop you from sending a
3469         /// second payment with the same [`PaymentId`].
3470         ///
3471         /// Thus, in order to ensure duplicate payments are not sent, you should implement your own
3472         /// tracking of payments, including state to indicate once a payment has completed. Because you
3473         /// should also ensure that [`PaymentHash`]es are not re-used, for simplicity, you should
3474         /// consider using the [`PaymentHash`] as the key for tracking payments. In that case, the
3475         /// [`PaymentId`] should be a copy of the [`PaymentHash`] bytes.
3476         ///
3477         /// Additionally, in the scenario where we begin the process of sending a payment, but crash
3478         /// before `send_payment` returns (or prior to [`ChannelMonitorUpdate`] persistence if you're
3479         /// using [`ChannelMonitorUpdateStatus::InProgress`]), the payment may be lost on restart. See
3480         /// [`ChannelManager::list_recent_payments`] for more information.
3481         ///
3482         /// # Possible Error States on [`PaymentSendFailure`]
3483         ///
3484         /// Each path may have a different return value, and [`PaymentSendFailure`] may return a `Vec` with
3485         /// each entry matching the corresponding-index entry in the route paths, see
3486         /// [`PaymentSendFailure`] for more info.
3487         ///
3488         /// In general, a path may raise:
3489         ///  * [`APIError::InvalidRoute`] when an invalid route or forwarding parameter (cltv_delta, fee,
3490         ///    node public key) is specified.
3491         ///  * [`APIError::ChannelUnavailable`] if the next-hop channel is not available as it has been
3492         ///    closed, doesn't exist, or the peer is currently disconnected.
3493         ///  * [`APIError::MonitorUpdateInProgress`] if a new monitor update failure prevented sending the
3494         ///    relevant updates.
3495         ///
3496         /// Note that depending on the type of the [`PaymentSendFailure`] the HTLC may have been
3497         /// irrevocably committed to on our end. In such a case, do NOT retry the payment with a
3498         /// different route unless you intend to pay twice!
3499         ///
3500         /// [`RouteHop`]: crate::routing::router::RouteHop
3501         /// [`Event::PaymentSent`]: events::Event::PaymentSent
3502         /// [`Event::PaymentFailed`]: events::Event::PaymentFailed
3503         /// [`UpdateHTLCs`]: events::MessageSendEvent::UpdateHTLCs
3504         /// [`PeerManager::process_events`]: crate::ln::peer_handler::PeerManager::process_events
3505         /// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress
3506         pub fn send_payment_with_route(&self, route: &Route, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, payment_id: PaymentId) -> Result<(), PaymentSendFailure> {
3507                 let best_block_height = self.best_block.read().unwrap().height();
3508                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
3509                 self.pending_outbound_payments
3510                         .send_payment_with_route(route, payment_hash, recipient_onion, payment_id,
3511                                 &self.entropy_source, &self.node_signer, best_block_height,
3512                                 |args| self.send_payment_along_path(args))
3513         }
3514
3515         /// Similar to [`ChannelManager::send_payment_with_route`], but will automatically find a route based on
3516         /// `route_params` and retry failed payment paths based on `retry_strategy`.
3517         pub fn send_payment(&self, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, payment_id: PaymentId, route_params: RouteParameters, retry_strategy: Retry) -> Result<(), RetryableSendFailure> {
3518                 let best_block_height = self.best_block.read().unwrap().height();
3519                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
3520                 self.pending_outbound_payments
3521                         .send_payment(payment_hash, recipient_onion, payment_id, retry_strategy, route_params,
3522                                 &self.router, self.list_usable_channels(), || self.compute_inflight_htlcs(),
3523                                 &self.entropy_source, &self.node_signer, best_block_height, &self.logger,
3524                                 &self.pending_events, |args| self.send_payment_along_path(args))
3525         }
3526
3527         #[cfg(test)]
3528         pub(super) fn test_send_payment_internal(&self, route: &Route, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, keysend_preimage: Option<PaymentPreimage>, payment_id: PaymentId, recv_value_msat: Option<u64>, onion_session_privs: Vec<[u8; 32]>) -> Result<(), PaymentSendFailure> {
3529                 let best_block_height = self.best_block.read().unwrap().height();
3530                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
3531                 self.pending_outbound_payments.test_send_payment_internal(route, payment_hash, recipient_onion,
3532                         keysend_preimage, payment_id, recv_value_msat, onion_session_privs, &self.node_signer,
3533                         best_block_height, |args| self.send_payment_along_path(args))
3534         }
3535
3536         #[cfg(test)]
3537         pub(crate) fn test_add_new_pending_payment(&self, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, payment_id: PaymentId, route: &Route) -> Result<Vec<[u8; 32]>, PaymentSendFailure> {
3538                 let best_block_height = self.best_block.read().unwrap().height();
3539                 self.pending_outbound_payments.test_add_new_pending_payment(payment_hash, recipient_onion, payment_id, route, None, &self.entropy_source, best_block_height)
3540         }
3541
3542         #[cfg(test)]
3543         pub(crate) fn test_set_payment_metadata(&self, payment_id: PaymentId, new_payment_metadata: Option<Vec<u8>>) {
3544                 self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
3545         }
3546
3547
3548         /// Signals that no further attempts for the given payment should occur. Useful if you have a
3549         /// pending outbound payment with retries remaining, but wish to stop retrying the payment before
3550         /// retries are exhausted.
3551         ///
3552         /// # Event Generation
3553         ///
3554         /// If no [`Event::PaymentFailed`] event had been generated before, one will be generated as soon
3555         /// as there are no remaining pending HTLCs for this payment.
3556         ///
3557         /// Note that calling this method does *not* prevent a payment from succeeding. You must still
3558         /// wait until you receive either a [`Event::PaymentFailed`] or [`Event::PaymentSent`] event to
3559         /// determine the ultimate status of a payment.
3560         ///
3561         /// # Restart Behavior
3562         ///
3563         /// If an [`Event::PaymentFailed`] is generated and we restart without first persisting the
3564         /// [`ChannelManager`], another [`Event::PaymentFailed`] may be generated.
3565         pub fn abandon_payment(&self, payment_id: PaymentId) {
3566                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
3567                 self.pending_outbound_payments.abandon_payment(payment_id, PaymentFailureReason::UserAbandoned, &self.pending_events);
3568         }
3569
3570         /// Send a spontaneous payment, which is a payment that does not require the recipient to have
3571         /// generated an invoice. Optionally, you may specify the preimage. If you do choose to specify
3572         /// the preimage, it must be a cryptographically secure random value that no intermediate node
3573         /// would be able to guess -- otherwise, an intermediate node may claim the payment and it will
3574         /// never reach the recipient.
3575         ///
3576         /// See [`send_payment`] documentation for more details on the return value of this function
3577         /// and idempotency guarantees provided by the [`PaymentId`] key.
3578         ///
3579         /// Similar to regular payments, you MUST NOT reuse a `payment_preimage` value. See
3580         /// [`send_payment`] for more information about the risks of duplicate preimage usage.
3581         ///
3582         /// [`send_payment`]: Self::send_payment
3583         pub fn send_spontaneous_payment(&self, route: &Route, payment_preimage: Option<PaymentPreimage>, recipient_onion: RecipientOnionFields, payment_id: PaymentId) -> Result<PaymentHash, PaymentSendFailure> {
3584                 let best_block_height = self.best_block.read().unwrap().height();
3585                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
3586                 self.pending_outbound_payments.send_spontaneous_payment_with_route(
3587                         route, payment_preimage, recipient_onion, payment_id, &self.entropy_source,
3588                         &self.node_signer, best_block_height, |args| self.send_payment_along_path(args))
3589         }
3590
3591         /// Similar to [`ChannelManager::send_spontaneous_payment`], but will automatically find a route
3592         /// based on `route_params` and retry failed payment paths based on `retry_strategy`.
3593         ///
3594         /// See [`PaymentParameters::for_keysend`] for help in constructing `route_params` for spontaneous
3595         /// payments.
3596         ///
3597         /// [`PaymentParameters::for_keysend`]: crate::routing::router::PaymentParameters::for_keysend
3598         pub fn send_spontaneous_payment_with_retry(&self, payment_preimage: Option<PaymentPreimage>, recipient_onion: RecipientOnionFields, payment_id: PaymentId, route_params: RouteParameters, retry_strategy: Retry) -> Result<PaymentHash, RetryableSendFailure> {
3599                 let best_block_height = self.best_block.read().unwrap().height();
3600                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
3601                 self.pending_outbound_payments.send_spontaneous_payment(payment_preimage, recipient_onion,
3602                         payment_id, retry_strategy, route_params, &self.router, self.list_usable_channels(),
3603                         || self.compute_inflight_htlcs(),  &self.entropy_source, &self.node_signer, best_block_height,
3604                         &self.logger, &self.pending_events, |args| self.send_payment_along_path(args))
3605         }
3606
3607         /// Send a payment that is probing the given route for liquidity. We calculate the
3608         /// [`PaymentHash`] of probes based on a static secret and a random [`PaymentId`], which allows
3609         /// us to easily discern them from real payments.
3610         pub fn send_probe(&self, path: Path) -> Result<(PaymentHash, PaymentId), PaymentSendFailure> {
3611                 let best_block_height = self.best_block.read().unwrap().height();
3612                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
3613                 self.pending_outbound_payments.send_probe(path, self.probing_cookie_secret,
3614                         &self.entropy_source, &self.node_signer, best_block_height,
3615                         |args| self.send_payment_along_path(args))
3616         }
3617
3618         /// Returns whether a payment with the given [`PaymentHash`] and [`PaymentId`] is, in fact, a
3619         /// payment probe.
3620         #[cfg(test)]
3621         pub(crate) fn payment_is_probe(&self, payment_hash: &PaymentHash, payment_id: &PaymentId) -> bool {
3622                 outbound_payment::payment_is_probe(payment_hash, payment_id, self.probing_cookie_secret)
3623         }
3624
3625         /// Sends payment probes over all paths of a route that would be used to pay the given
3626         /// amount to the given `node_id`.
3627         ///
3628         /// See [`ChannelManager::send_preflight_probes`] for more information.
3629         pub fn send_spontaneous_preflight_probes(
3630                 &self, node_id: PublicKey, amount_msat: u64, final_cltv_expiry_delta: u32,
3631                 liquidity_limit_multiplier: Option<u64>,
3632         ) -> Result<Vec<(PaymentHash, PaymentId)>, ProbeSendFailure> {
3633                 let payment_params =
3634                         PaymentParameters::from_node_id(node_id, final_cltv_expiry_delta);
3635
3636                 let route_params = RouteParameters::from_payment_params_and_value(payment_params, amount_msat);
3637
3638                 self.send_preflight_probes(route_params, liquidity_limit_multiplier)
3639         }
3640
3641         /// Sends payment probes over all paths of a route that would be used to pay a route found
3642         /// according to the given [`RouteParameters`].
3643         ///
3644         /// This may be used to send "pre-flight" probes, i.e., to train our scorer before conducting
3645         /// the actual payment. Note this is only useful if there likely is sufficient time for the
3646         /// probe to settle before sending out the actual payment, e.g., when waiting for user
3647         /// confirmation in a wallet UI.
3648         ///
3649         /// Otherwise, there is a chance the probe could take up some liquidity needed to complete the
3650         /// actual payment. Users should therefore be cautious and might avoid sending probes if
3651         /// liquidity is scarce and/or they don't expect the probe to return before they send the
3652         /// payment. To mitigate this issue, channels with available liquidity less than the required
3653         /// amount times the given `liquidity_limit_multiplier` won't be used to send pre-flight
3654         /// probes. If `None` is given as `liquidity_limit_multiplier`, it defaults to `3`.
3655         pub fn send_preflight_probes(
3656                 &self, route_params: RouteParameters, liquidity_limit_multiplier: Option<u64>,
3657         ) -> Result<Vec<(PaymentHash, PaymentId)>, ProbeSendFailure> {
3658                 let liquidity_limit_multiplier = liquidity_limit_multiplier.unwrap_or(3);
3659
3660                 let payer = self.get_our_node_id();
3661                 let usable_channels = self.list_usable_channels();
3662                 let first_hops = usable_channels.iter().collect::<Vec<_>>();
3663                 let inflight_htlcs = self.compute_inflight_htlcs();
3664
3665                 let route = self
3666                         .router
3667                         .find_route(&payer, &route_params, Some(&first_hops), inflight_htlcs)
3668                         .map_err(|e| {
3669                                 log_error!(self.logger, "Failed to find path for payment probe: {:?}", e);
3670                                 ProbeSendFailure::RouteNotFound
3671                         })?;
3672
3673                 let mut used_liquidity_map = HashMap::with_capacity(first_hops.len());
3674
3675                 let mut res = Vec::new();
3676
3677                 for mut path in route.paths {
3678                         // If the last hop is probably an unannounced channel we refrain from probing all the
3679                         // way through to the end and instead probe up to the second-to-last channel.
3680                         while let Some(last_path_hop) = path.hops.last() {
3681                                 if last_path_hop.maybe_announced_channel {
3682                                         // We found a potentially announced last hop.
3683                                         break;
3684                                 } else {
3685                                         // Drop the last hop, as it's likely unannounced.
3686                                         log_debug!(
3687                                                 self.logger,
3688                                                 "Avoided sending payment probe all the way to last hop {} as it is likely unannounced.",
3689                                                 last_path_hop.short_channel_id
3690                                         );
3691                                         let final_value_msat = path.final_value_msat();
3692                                         path.hops.pop();
3693                                         if let Some(new_last) = path.hops.last_mut() {
3694                                                 new_last.fee_msat += final_value_msat;
3695                                         }
3696                                 }
3697                         }
3698
3699                         if path.hops.len() < 2 {
3700                                 log_debug!(
3701                                         self.logger,
3702                                         "Skipped sending payment probe over path with less than two hops."
3703                                 );
3704                                 continue;
3705                         }
3706
3707                         if let Some(first_path_hop) = path.hops.first() {
3708                                 if let Some(first_hop) = first_hops.iter().find(|h| {
3709                                         h.get_outbound_payment_scid() == Some(first_path_hop.short_channel_id)
3710                                 }) {
3711                                         let path_value = path.final_value_msat() + path.fee_msat();
3712                                         let used_liquidity =
3713                                                 used_liquidity_map.entry(first_path_hop.short_channel_id).or_insert(0);
3714
3715                                         if first_hop.next_outbound_htlc_limit_msat
3716                                                 < (*used_liquidity + path_value) * liquidity_limit_multiplier
3717                                         {
3718                                                 log_debug!(self.logger, "Skipped sending payment probe to avoid putting channel {} under the liquidity limit.", first_path_hop.short_channel_id);
3719                                                 continue;
3720                                         } else {
3721                                                 *used_liquidity += path_value;
3722                                         }
3723                                 }
3724                         }
3725
3726                         res.push(self.send_probe(path).map_err(|e| {
3727                                 log_error!(self.logger, "Failed to send pre-flight probe: {:?}", e);
3728                                 ProbeSendFailure::SendingFailed(e)
3729                         })?);
3730                 }
3731
3732                 Ok(res)
3733         }
3734
3735         /// Handles the generation of a funding transaction, optionally (for tests) with a function
3736         /// which checks the correctness of the funding transaction given the associated channel.
3737         fn funding_transaction_generated_intern<FundingOutput: FnMut(&OutboundV1Channel<SP>, &Transaction) -> Result<OutPoint, APIError>>(
3738                 &self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, funding_transaction: Transaction, is_batch_funding: bool,
3739                 mut find_funding_output: FundingOutput,
3740         ) -> Result<(), APIError> {
3741                 let per_peer_state = self.per_peer_state.read().unwrap();
3742                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
3743                         .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
3744
3745                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
3746                 let peer_state = &mut *peer_state_lock;
3747                 let (chan, msg) = match peer_state.channel_by_id.remove(temporary_channel_id) {
3748                         Some(ChannelPhase::UnfundedOutboundV1(chan)) => {
3749                                 let funding_txo = find_funding_output(&chan, &funding_transaction)?;
3750
3751                                 let funding_res = chan.get_funding_created(funding_transaction, funding_txo, is_batch_funding, &self.logger)
3752                                         .map_err(|(mut chan, e)| if let ChannelError::Close(msg) = e {
3753                                                 let channel_id = chan.context.channel_id();
3754                                                 let user_id = chan.context.get_user_id();
3755                                                 let shutdown_res = chan.context.force_shutdown(false);
3756                                                 let channel_capacity = chan.context.get_value_satoshis();
3757                                                 (chan, MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, user_id, shutdown_res, None, channel_capacity))
3758                                         } else { unreachable!(); });
3759                                 match funding_res {
3760                                         Ok((chan, funding_msg)) => (chan, funding_msg),
3761                                         Err((chan, err)) => {
3762                                                 mem::drop(peer_state_lock);
3763                                                 mem::drop(per_peer_state);
3764
3765                                                 let _: Result<(), _> = handle_error!(self, Err(err), chan.context.get_counterparty_node_id());
3766                                                 return Err(APIError::ChannelUnavailable {
3767                                                         err: "Signer refused to sign the initial commitment transaction".to_owned()
3768                                                 });
3769                                         },
3770                                 }
3771                         },
3772                         Some(phase) => {
3773                                 peer_state.channel_by_id.insert(*temporary_channel_id, phase);
3774                                 return Err(APIError::APIMisuseError {
3775                                         err: format!(
3776                                                 "Channel with id {} for the passed counterparty node_id {} is not an unfunded, outbound V1 channel",
3777                                                 temporary_channel_id, counterparty_node_id),
3778                                 })
3779                         },
3780                         None => return Err(APIError::ChannelUnavailable {err: format!(
3781                                 "Channel with id {} not found for the passed counterparty node_id {}",
3782                                 temporary_channel_id, counterparty_node_id),
3783                                 }),
3784                 };
3785
3786                 peer_state.pending_msg_events.push(events::MessageSendEvent::SendFundingCreated {
3787                         node_id: chan.context.get_counterparty_node_id(),
3788                         msg,
3789                 });
3790                 match peer_state.channel_by_id.entry(chan.context.channel_id()) {
3791                         hash_map::Entry::Occupied(_) => {
3792                                 panic!("Generated duplicate funding txid?");
3793                         },
3794                         hash_map::Entry::Vacant(e) => {
3795                                 let mut id_to_peer = self.id_to_peer.lock().unwrap();
3796                                 if id_to_peer.insert(chan.context.channel_id(), chan.context.get_counterparty_node_id()).is_some() {
3797                                         panic!("id_to_peer map already contained funding txid, which shouldn't be possible");
3798                                 }
3799                                 e.insert(ChannelPhase::Funded(chan));
3800                         }
3801                 }
3802                 Ok(())
3803         }
3804
3805         #[cfg(test)]
3806         pub(crate) fn funding_transaction_generated_unchecked(&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, funding_transaction: Transaction, output_index: u16) -> Result<(), APIError> {
3807                 self.funding_transaction_generated_intern(temporary_channel_id, counterparty_node_id, funding_transaction, false, |_, tx| {
3808                         Ok(OutPoint { txid: tx.txid(), index: output_index })
3809                 })
3810         }
3811
3812         /// Call this upon creation of a funding transaction for the given channel.
3813         ///
3814         /// Returns an [`APIError::APIMisuseError`] if the funding_transaction spent non-SegWit outputs
3815         /// or if no output was found which matches the parameters in [`Event::FundingGenerationReady`].
3816         ///
3817         /// Returns [`APIError::APIMisuseError`] if the funding transaction is not final for propagation
3818         /// across the p2p network.
3819         ///
3820         /// Returns [`APIError::ChannelUnavailable`] if a funding transaction has already been provided
3821         /// for the channel or if the channel has been closed as indicated by [`Event::ChannelClosed`].
3822         ///
3823         /// May panic if the output found in the funding transaction is duplicative with some other
3824         /// channel (note that this should be trivially prevented by using unique funding transaction
3825         /// keys per-channel).
3826         ///
3827         /// Do NOT broadcast the funding transaction yourself. When we have safely received our
3828         /// counterparty's signature the funding transaction will automatically be broadcast via the
3829         /// [`BroadcasterInterface`] provided when this `ChannelManager` was constructed.
3830         ///
3831         /// Note that this includes RBF or similar transaction replacement strategies - lightning does
3832         /// not currently support replacing a funding transaction on an existing channel. Instead,
3833         /// create a new channel with a conflicting funding transaction.
3834         ///
3835         /// Note to keep the miner incentives aligned in moving the blockchain forward, we recommend
3836         /// the wallet software generating the funding transaction to apply anti-fee sniping as
3837         /// implemented by Bitcoin Core wallet. See <https://bitcoinops.org/en/topics/fee-sniping/>
3838         /// for more details.
3839         ///
3840         /// [`Event::FundingGenerationReady`]: crate::events::Event::FundingGenerationReady
3841         /// [`Event::ChannelClosed`]: crate::events::Event::ChannelClosed
3842         pub fn funding_transaction_generated(&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, funding_transaction: Transaction) -> Result<(), APIError> {
3843                 self.batch_funding_transaction_generated(&[(temporary_channel_id, counterparty_node_id)], funding_transaction)
3844         }
3845
3846         /// Call this upon creation of a batch funding transaction for the given channels.
3847         ///
3848         /// Return values are identical to [`Self::funding_transaction_generated`], respective to
3849         /// each individual channel and transaction output.
3850         ///
3851         /// Do NOT broadcast the funding transaction yourself. This batch funding transaction
3852         /// will only be broadcast when we have safely received and persisted the counterparty's
3853         /// signature for each channel.
3854         ///
3855         /// If there is an error, all channels in the batch are to be considered closed.
3856         pub fn batch_funding_transaction_generated(&self, temporary_channels: &[(&ChannelId, &PublicKey)], funding_transaction: Transaction) -> Result<(), APIError> {
3857                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
3858                 let mut result = Ok(());
3859
3860                 if !funding_transaction.is_coin_base() {
3861                         for inp in funding_transaction.input.iter() {
3862                                 if inp.witness.is_empty() {
3863                                         result = result.and(Err(APIError::APIMisuseError {
3864                                                 err: "Funding transaction must be fully signed and spend Segwit outputs".to_owned()
3865                                         }));
3866                                 }
3867                         }
3868                 }
3869                 if funding_transaction.output.len() > u16::max_value() as usize {
3870                         result = result.and(Err(APIError::APIMisuseError {
3871                                 err: "Transaction had more than 2^16 outputs, which is not supported".to_owned()
3872                         }));
3873                 }
3874                 {
3875                         let height = self.best_block.read().unwrap().height();
3876                         // Transactions are evaluated as final by network mempools if their locktime is strictly
3877                         // lower than the next block height. However, the modules constituting our Lightning
3878                         // node might not have perfect sync about their blockchain views. Thus, if the wallet
3879                         // module is ahead of LDK, only allow one more block of headroom.
3880                         if !funding_transaction.input.iter().all(|input| input.sequence == Sequence::MAX) && LockTime::from(funding_transaction.lock_time).is_block_height() && funding_transaction.lock_time.0 > height + 1 {
3881                                 result = result.and(Err(APIError::APIMisuseError {
3882                                         err: "Funding transaction absolute timelock is non-final".to_owned()
3883                                 }));
3884                         }
3885                 }
3886
3887                 let txid = funding_transaction.txid();
3888                 let is_batch_funding = temporary_channels.len() > 1;
3889                 let mut funding_batch_states = if is_batch_funding {
3890                         Some(self.funding_batch_states.lock().unwrap())
3891                 } else {
3892                         None
3893                 };
3894                 let mut funding_batch_state = funding_batch_states.as_mut().and_then(|states| {
3895                         match states.entry(txid) {
3896                                 btree_map::Entry::Occupied(_) => {
3897                                         result = result.clone().and(Err(APIError::APIMisuseError {
3898                                                 err: "Batch funding transaction with the same txid already exists".to_owned()
3899                                         }));
3900                                         None
3901                                 },
3902                                 btree_map::Entry::Vacant(vacant) => Some(vacant.insert(Vec::new())),
3903                         }
3904                 });
3905                 for &(temporary_channel_id, counterparty_node_id) in temporary_channels {
3906                         result = result.and_then(|_| self.funding_transaction_generated_intern(
3907                                 temporary_channel_id,
3908                                 counterparty_node_id,
3909                                 funding_transaction.clone(),
3910                                 is_batch_funding,
3911                                 |chan, tx| {
3912                                         let mut output_index = None;
3913                                         let expected_spk = chan.context.get_funding_redeemscript().to_v0_p2wsh();
3914                                         for (idx, outp) in tx.output.iter().enumerate() {
3915                                                 if outp.script_pubkey == expected_spk && outp.value == chan.context.get_value_satoshis() {
3916                                                         if output_index.is_some() {
3917                                                                 return Err(APIError::APIMisuseError {
3918                                                                         err: "Multiple outputs matched the expected script and value".to_owned()
3919                                                                 });
3920                                                         }
3921                                                         output_index = Some(idx as u16);
3922                                                 }
3923                                         }
3924                                         if output_index.is_none() {
3925                                                 return Err(APIError::APIMisuseError {
3926                                                         err: "No output matched the script_pubkey and value in the FundingGenerationReady event".to_owned()
3927                                                 });
3928                                         }
3929                                         let outpoint = OutPoint { txid: tx.txid(), index: output_index.unwrap() };
3930                                         if let Some(funding_batch_state) = funding_batch_state.as_mut() {
3931                                                 funding_batch_state.push((outpoint.to_channel_id(), *counterparty_node_id, false));
3932                                         }
3933                                         Ok(outpoint)
3934                                 })
3935                         );
3936                 }
3937                 if let Err(ref e) = result {
3938                         // Remaining channels need to be removed on any error.
3939                         let e = format!("Error in transaction funding: {:?}", e);
3940                         let mut channels_to_remove = Vec::new();
3941                         channels_to_remove.extend(funding_batch_states.as_mut()
3942                                 .and_then(|states| states.remove(&txid))
3943                                 .into_iter().flatten()
3944                                 .map(|(chan_id, node_id, _state)| (chan_id, node_id))
3945                         );
3946                         channels_to_remove.extend(temporary_channels.iter()
3947                                 .map(|(&chan_id, &node_id)| (chan_id, node_id))
3948                         );
3949                         let mut shutdown_results = Vec::new();
3950                         {
3951                                 let per_peer_state = self.per_peer_state.read().unwrap();
3952                                 for (channel_id, counterparty_node_id) in channels_to_remove {
3953                                         per_peer_state.get(&counterparty_node_id)
3954                                                 .map(|peer_state_mutex| peer_state_mutex.lock().unwrap())
3955                                                 .and_then(|mut peer_state| peer_state.channel_by_id.remove(&channel_id))
3956                                                 .map(|mut chan| {
3957                                                         update_maps_on_chan_removal!(self, &chan.context());
3958                                                         self.issue_channel_close_events(&chan.context(), ClosureReason::ProcessingError { err: e.clone() });
3959                                                         shutdown_results.push(chan.context_mut().force_shutdown(false));
3960                                                 });
3961                                 }
3962                         }
3963                         for shutdown_result in shutdown_results.drain(..) {
3964                                 self.finish_close_channel(shutdown_result);
3965                         }
3966                 }
3967                 result
3968         }
3969
3970         /// Atomically applies partial updates to the [`ChannelConfig`] of the given channels.
3971         ///
3972         /// Once the updates are applied, each eligible channel (advertised with a known short channel
3973         /// ID and a change in [`forwarding_fee_proportional_millionths`], [`forwarding_fee_base_msat`],
3974         /// or [`cltv_expiry_delta`]) has a [`BroadcastChannelUpdate`] event message generated
3975         /// containing the new [`ChannelUpdate`] message which should be broadcast to the network.
3976         ///
3977         /// Returns [`ChannelUnavailable`] when a channel is not found or an incorrect
3978         /// `counterparty_node_id` is provided.
3979         ///
3980         /// Returns [`APIMisuseError`] when a [`cltv_expiry_delta`] update is to be applied with a value
3981         /// below [`MIN_CLTV_EXPIRY_DELTA`].
3982         ///
3983         /// If an error is returned, none of the updates should be considered applied.
3984         ///
3985         /// [`forwarding_fee_proportional_millionths`]: ChannelConfig::forwarding_fee_proportional_millionths
3986         /// [`forwarding_fee_base_msat`]: ChannelConfig::forwarding_fee_base_msat
3987         /// [`cltv_expiry_delta`]: ChannelConfig::cltv_expiry_delta
3988         /// [`BroadcastChannelUpdate`]: events::MessageSendEvent::BroadcastChannelUpdate
3989         /// [`ChannelUpdate`]: msgs::ChannelUpdate
3990         /// [`ChannelUnavailable`]: APIError::ChannelUnavailable
3991         /// [`APIMisuseError`]: APIError::APIMisuseError
3992         pub fn update_partial_channel_config(
3993                 &self, counterparty_node_id: &PublicKey, channel_ids: &[ChannelId], config_update: &ChannelConfigUpdate,
3994         ) -> Result<(), APIError> {
3995                 if config_update.cltv_expiry_delta.map(|delta| delta < MIN_CLTV_EXPIRY_DELTA).unwrap_or(false) {
3996                         return Err(APIError::APIMisuseError {
3997                                 err: format!("The chosen CLTV expiry delta is below the minimum of {}", MIN_CLTV_EXPIRY_DELTA),
3998                         });
3999                 }
4000
4001                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
4002                 let per_peer_state = self.per_peer_state.read().unwrap();
4003                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
4004                         .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
4005                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4006                 let peer_state = &mut *peer_state_lock;
4007                 for channel_id in channel_ids {
4008                         if !peer_state.has_channel(channel_id) {
4009                                 return Err(APIError::ChannelUnavailable {
4010                                         err: format!("Channel with id {} not found for the passed counterparty node_id {}", channel_id, counterparty_node_id),
4011                                 });
4012                         };
4013                 }
4014                 for channel_id in channel_ids {
4015                         if let Some(channel_phase) = peer_state.channel_by_id.get_mut(channel_id) {
4016                                 let mut config = channel_phase.context().config();
4017                                 config.apply(config_update);
4018                                 if !channel_phase.context_mut().update_config(&config) {
4019                                         continue;
4020                                 }
4021                                 if let ChannelPhase::Funded(channel) = channel_phase {
4022                                         if let Ok(msg) = self.get_channel_update_for_broadcast(channel) {
4023                                                 peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate { msg });
4024                                         } else if let Ok(msg) = self.get_channel_update_for_unicast(channel) {
4025                                                 peer_state.pending_msg_events.push(events::MessageSendEvent::SendChannelUpdate {
4026                                                         node_id: channel.context.get_counterparty_node_id(),
4027                                                         msg,
4028                                                 });
4029                                         }
4030                                 }
4031                                 continue;
4032                         } else {
4033                                 // This should not be reachable as we've already checked for non-existence in the previous channel_id loop.
4034                                 debug_assert!(false);
4035                                 return Err(APIError::ChannelUnavailable {
4036                                         err: format!(
4037                                                 "Channel with ID {} for passed counterparty_node_id {} disappeared after we confirmed its existence - this should not be reachable!",
4038                                                 channel_id, counterparty_node_id),
4039                                 });
4040                         };
4041                 }
4042                 Ok(())
4043         }
4044
4045         /// Atomically updates the [`ChannelConfig`] for the given channels.
4046         ///
4047         /// Once the updates are applied, each eligible channel (advertised with a known short channel
4048         /// ID and a change in [`forwarding_fee_proportional_millionths`], [`forwarding_fee_base_msat`],
4049         /// or [`cltv_expiry_delta`]) has a [`BroadcastChannelUpdate`] event message generated
4050         /// containing the new [`ChannelUpdate`] message which should be broadcast to the network.
4051         ///
4052         /// Returns [`ChannelUnavailable`] when a channel is not found or an incorrect
4053         /// `counterparty_node_id` is provided.
4054         ///
4055         /// Returns [`APIMisuseError`] when a [`cltv_expiry_delta`] update is to be applied with a value
4056         /// below [`MIN_CLTV_EXPIRY_DELTA`].
4057         ///
4058         /// If an error is returned, none of the updates should be considered applied.
4059         ///
4060         /// [`forwarding_fee_proportional_millionths`]: ChannelConfig::forwarding_fee_proportional_millionths
4061         /// [`forwarding_fee_base_msat`]: ChannelConfig::forwarding_fee_base_msat
4062         /// [`cltv_expiry_delta`]: ChannelConfig::cltv_expiry_delta
4063         /// [`BroadcastChannelUpdate`]: events::MessageSendEvent::BroadcastChannelUpdate
4064         /// [`ChannelUpdate`]: msgs::ChannelUpdate
4065         /// [`ChannelUnavailable`]: APIError::ChannelUnavailable
4066         /// [`APIMisuseError`]: APIError::APIMisuseError
4067         pub fn update_channel_config(
4068                 &self, counterparty_node_id: &PublicKey, channel_ids: &[ChannelId], config: &ChannelConfig,
4069         ) -> Result<(), APIError> {
4070                 return self.update_partial_channel_config(counterparty_node_id, channel_ids, &(*config).into());
4071         }
4072
4073         /// Attempts to forward an intercepted HTLC over the provided channel id and with the provided
4074         /// amount to forward. Should only be called in response to an [`HTLCIntercepted`] event.
4075         ///
4076         /// Intercepted HTLCs can be useful for Lightning Service Providers (LSPs) to open a just-in-time
4077         /// channel to a receiving node if the node lacks sufficient inbound liquidity.
4078         ///
4079         /// To make use of intercepted HTLCs, set [`UserConfig::accept_intercept_htlcs`] and use
4080         /// [`ChannelManager::get_intercept_scid`] to generate short channel id(s) to put in the
4081         /// receiver's invoice route hints. These route hints will signal to LDK to generate an
4082         /// [`HTLCIntercepted`] event when it receives the forwarded HTLC, and this method or
4083         /// [`ChannelManager::fail_intercepted_htlc`] MUST be called in response to the event.
4084         ///
4085         /// Note that LDK does not enforce fee requirements in `amt_to_forward_msat`, and will not stop
4086         /// you from forwarding more than you received. See
4087         /// [`HTLCIntercepted::expected_outbound_amount_msat`] for more on forwarding a different amount
4088         /// than expected.
4089         ///
4090         /// Errors if the event was not handled in time, in which case the HTLC was automatically failed
4091         /// backwards.
4092         ///
4093         /// [`UserConfig::accept_intercept_htlcs`]: crate::util::config::UserConfig::accept_intercept_htlcs
4094         /// [`HTLCIntercepted`]: events::Event::HTLCIntercepted
4095         /// [`HTLCIntercepted::expected_outbound_amount_msat`]: events::Event::HTLCIntercepted::expected_outbound_amount_msat
4096         // TODO: when we move to deciding the best outbound channel at forward time, only take
4097         // `next_node_id` and not `next_hop_channel_id`
4098         pub fn forward_intercepted_htlc(&self, intercept_id: InterceptId, next_hop_channel_id: &ChannelId, next_node_id: PublicKey, amt_to_forward_msat: u64) -> Result<(), APIError> {
4099                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
4100
4101                 let next_hop_scid = {
4102                         let peer_state_lock = self.per_peer_state.read().unwrap();
4103                         let peer_state_mutex = peer_state_lock.get(&next_node_id)
4104                                 .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", next_node_id) })?;
4105                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4106                         let peer_state = &mut *peer_state_lock;
4107                         match peer_state.channel_by_id.get(next_hop_channel_id) {
4108                                 Some(ChannelPhase::Funded(chan)) => {
4109                                         if !chan.context.is_usable() {
4110                                                 return Err(APIError::ChannelUnavailable {
4111                                                         err: format!("Channel with id {} not fully established", next_hop_channel_id)
4112                                                 })
4113                                         }
4114                                         chan.context.get_short_channel_id().unwrap_or(chan.context.outbound_scid_alias())
4115                                 },
4116                                 Some(_) => return Err(APIError::ChannelUnavailable {
4117                                         err: format!("Channel with id {} for the passed counterparty node_id {} is still opening.",
4118                                                 next_hop_channel_id, next_node_id)
4119                                 }),
4120                                 None => return Err(APIError::ChannelUnavailable {
4121                                         err: format!("Channel with id {} not found for the passed counterparty node_id {}",
4122                                                 next_hop_channel_id, next_node_id)
4123                                 })
4124                         }
4125                 };
4126
4127                 let payment = self.pending_intercepted_htlcs.lock().unwrap().remove(&intercept_id)
4128                         .ok_or_else(|| APIError::APIMisuseError {
4129                                 err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.0))
4130                         })?;
4131
4132                 let routing = match payment.forward_info.routing {
4133                         PendingHTLCRouting::Forward { onion_packet, .. } => {
4134                                 PendingHTLCRouting::Forward { onion_packet, short_channel_id: next_hop_scid }
4135                         },
4136                         _ => unreachable!() // Only `PendingHTLCRouting::Forward`s are intercepted
4137                 };
4138                 let skimmed_fee_msat =
4139                         payment.forward_info.outgoing_amt_msat.saturating_sub(amt_to_forward_msat);
4140                 let pending_htlc_info = PendingHTLCInfo {
4141                         skimmed_fee_msat: if skimmed_fee_msat == 0 { None } else { Some(skimmed_fee_msat) },
4142                         outgoing_amt_msat: amt_to_forward_msat, routing, ..payment.forward_info
4143                 };
4144
4145                 let mut per_source_pending_forward = [(
4146                         payment.prev_short_channel_id,
4147                         payment.prev_funding_outpoint,
4148                         payment.prev_user_channel_id,
4149                         vec![(pending_htlc_info, payment.prev_htlc_id)]
4150                 )];
4151                 self.forward_htlcs(&mut per_source_pending_forward);
4152                 Ok(())
4153         }
4154
4155         /// Fails the intercepted HTLC indicated by intercept_id. Should only be called in response to
4156         /// an [`HTLCIntercepted`] event. See [`ChannelManager::forward_intercepted_htlc`].
4157         ///
4158         /// Errors if the event was not handled in time, in which case the HTLC was automatically failed
4159         /// backwards.
4160         ///
4161         /// [`HTLCIntercepted`]: events::Event::HTLCIntercepted
4162         pub fn fail_intercepted_htlc(&self, intercept_id: InterceptId) -> Result<(), APIError> {
4163                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
4164
4165                 let payment = self.pending_intercepted_htlcs.lock().unwrap().remove(&intercept_id)
4166                         .ok_or_else(|| APIError::APIMisuseError {
4167                                 err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.0))
4168                         })?;
4169
4170                 if let PendingHTLCRouting::Forward { short_channel_id, .. } = payment.forward_info.routing {
4171                         let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
4172                                 short_channel_id: payment.prev_short_channel_id,
4173                                 user_channel_id: Some(payment.prev_user_channel_id),
4174                                 outpoint: payment.prev_funding_outpoint,
4175                                 htlc_id: payment.prev_htlc_id,
4176                                 incoming_packet_shared_secret: payment.forward_info.incoming_shared_secret,
4177                                 phantom_shared_secret: None,
4178                         });
4179
4180                         let failure_reason = HTLCFailReason::from_failure_code(0x4000 | 10);
4181                         let destination = HTLCDestination::UnknownNextHop { requested_forward_scid: short_channel_id };
4182                         self.fail_htlc_backwards_internal(&htlc_source, &payment.forward_info.payment_hash, &failure_reason, destination);
4183                 } else { unreachable!() } // Only `PendingHTLCRouting::Forward`s are intercepted
4184
4185                 Ok(())
4186         }
4187
4188         /// Processes HTLCs which are pending waiting on random forward delay.
4189         ///
4190         /// Should only really ever be called in response to a PendingHTLCsForwardable event.
4191         /// Will likely generate further events.
4192         pub fn process_pending_htlc_forwards(&self) {
4193                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
4194
4195                 let mut new_events = VecDeque::new();
4196                 let mut failed_forwards = Vec::new();
4197                 let mut phantom_receives: Vec<(u64, OutPoint, u128, Vec<(PendingHTLCInfo, u64)>)> = Vec::new();
4198                 {
4199                         let mut forward_htlcs = HashMap::new();
4200                         mem::swap(&mut forward_htlcs, &mut self.forward_htlcs.lock().unwrap());
4201
4202                         for (short_chan_id, mut pending_forwards) in forward_htlcs {
4203                                 if short_chan_id != 0 {
4204                                         macro_rules! forwarding_channel_not_found {
4205                                                 () => {
4206                                                         for forward_info in pending_forwards.drain(..) {
4207                                                                 match forward_info {
4208                                                                         HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
4209                                                                                 prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, prev_user_channel_id,
4210                                                                                 forward_info: PendingHTLCInfo {
4211                                                                                         routing, incoming_shared_secret, payment_hash, outgoing_amt_msat,
4212                                                                                         outgoing_cltv_value, ..
4213                                                                                 }
4214                                                                         }) => {
4215                                                                                 macro_rules! failure_handler {
4216                                                                                         ($msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr, $next_hop_unknown: expr) => {
4217                                                                                                 log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
4218
4219                                                                                                 let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
4220                                                                                                         short_channel_id: prev_short_channel_id,
4221                                                                                                         user_channel_id: Some(prev_user_channel_id),
4222                                                                                                         outpoint: prev_funding_outpoint,
4223                                                                                                         htlc_id: prev_htlc_id,
4224                                                                                                         incoming_packet_shared_secret: incoming_shared_secret,
4225                                                                                                         phantom_shared_secret: $phantom_ss,
4226                                                                                                 });
4227
4228                                                                                                 let reason = if $next_hop_unknown {
4229                                                                                                         HTLCDestination::UnknownNextHop { requested_forward_scid: short_chan_id }
4230                                                                                                 } else {
4231                                                                                                         HTLCDestination::FailedPayment{ payment_hash }
4232                                                                                                 };
4233
4234                                                                                                 failed_forwards.push((htlc_source, payment_hash,
4235                                                                                                         HTLCFailReason::reason($err_code, $err_data),
4236                                                                                                         reason
4237                                                                                                 ));
4238                                                                                                 continue;
4239                                                                                         }
4240                                                                                 }
4241                                                                                 macro_rules! fail_forward {
4242                                                                                         ($msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr) => {
4243                                                                                                 {
4244                                                                                                         failure_handler!($msg, $err_code, $err_data, $phantom_ss, true);
4245                                                                                                 }
4246                                                                                         }
4247                                                                                 }
4248                                                                                 macro_rules! failed_payment {
4249                                                                                         ($msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr) => {
4250                                                                                                 {
4251                                                                                                         failure_handler!($msg, $err_code, $err_data, $phantom_ss, false);
4252                                                                                                 }
4253                                                                                         }
4254                                                                                 }
4255                                                                                 if let PendingHTLCRouting::Forward { onion_packet, .. } = routing {
4256                                                                                         let phantom_pubkey_res = self.node_signer.get_node_id(Recipient::PhantomNode);
4257                                                                                         if phantom_pubkey_res.is_ok() && fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, short_chan_id, &self.chain_hash) {
4258                                                                                                 let phantom_shared_secret = self.node_signer.ecdh(Recipient::PhantomNode, &onion_packet.public_key.unwrap(), None).unwrap().secret_bytes();
4259                                                                                                 let next_hop = match onion_utils::decode_next_payment_hop(
4260                                                                                                         phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac,
4261                                                                                                         payment_hash, &self.node_signer
4262                                                                                                 ) {
4263                                                                                                         Ok(res) => res,
4264                                                                                                         Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code }) => {
4265                                                                                                                 let sha256_of_onion = Sha256::hash(&onion_packet.hop_data).into_inner();
4266                                                                                                                 // In this scenario, the phantom would have sent us an
4267                                                                                                                 // `update_fail_malformed_htlc`, meaning here we encrypt the error as
4268                                                                                                                 // if it came from us (the second-to-last hop) but contains the sha256
4269                                                                                                                 // of the onion.
4270                                                                                                                 failed_payment!(err_msg, err_code, sha256_of_onion.to_vec(), None);
4271                                                                                                         },
4272                                                                                                         Err(onion_utils::OnionDecodeErr::Relay { err_msg, err_code }) => {
4273                                                                                                                 failed_payment!(err_msg, err_code, Vec::new(), Some(phantom_shared_secret));
4274                                                                                                         },
4275                                                                                                 };
4276                                                                                                 match next_hop {
4277                                                                                                         onion_utils::Hop::Receive(hop_data) => {
4278                                                                                                                 match self.construct_recv_pending_htlc_info(hop_data,
4279                                                                                                                         incoming_shared_secret, payment_hash, outgoing_amt_msat,
4280                                                                                                                         outgoing_cltv_value, Some(phantom_shared_secret), false, None)
4281                                                                                                                 {
4282                                                                                                                         Ok(info) => phantom_receives.push((prev_short_channel_id, prev_funding_outpoint, prev_user_channel_id, vec![(info, prev_htlc_id)])),
4283                                                                                                                         Err(InboundOnionErr { err_code, err_data, msg }) => failed_payment!(msg, err_code, err_data, Some(phantom_shared_secret))
4284                                                                                                                 }
4285                                                                                                         },
4286                                                                                                         _ => panic!(),
4287                                                                                                 }
4288                                                                                         } else {
4289                                                                                                 fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id), 0x4000 | 10, Vec::new(), None);
4290                                                                                         }
4291                                                                                 } else {
4292                                                                                         fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id), 0x4000 | 10, Vec::new(), None);
4293                                                                                 }
4294                                                                         },
4295                                                                         HTLCForwardInfo::FailHTLC { .. } => {
4296                                                                                 // Channel went away before we could fail it. This implies
4297                                                                                 // the channel is now on chain and our counterparty is
4298                                                                                 // trying to broadcast the HTLC-Timeout, but that's their
4299                                                                                 // problem, not ours.
4300                                                                         }
4301                                                                 }
4302                                                         }
4303                                                 }
4304                                         }
4305                                         let chan_info_opt = self.short_to_chan_info.read().unwrap().get(&short_chan_id).cloned();
4306                                         let (counterparty_node_id, forward_chan_id) = match chan_info_opt {
4307                                                 Some((cp_id, chan_id)) => (cp_id, chan_id),
4308                                                 None => {
4309                                                         forwarding_channel_not_found!();
4310                                                         continue;
4311                                                 }
4312                                         };
4313                                         let per_peer_state = self.per_peer_state.read().unwrap();
4314                                         let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
4315                                         if peer_state_mutex_opt.is_none() {
4316                                                 forwarding_channel_not_found!();
4317                                                 continue;
4318                                         }
4319                                         let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
4320                                         let peer_state = &mut *peer_state_lock;
4321                                         if let Some(ChannelPhase::Funded(ref mut chan)) = peer_state.channel_by_id.get_mut(&forward_chan_id) {
4322                                                 for forward_info in pending_forwards.drain(..) {
4323                                                         match forward_info {
4324                                                                 HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
4325                                                                         prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, prev_user_channel_id,
4326                                                                         forward_info: PendingHTLCInfo {
4327                                                                                 incoming_shared_secret, payment_hash, outgoing_amt_msat, outgoing_cltv_value,
4328                                                                                 routing: PendingHTLCRouting::Forward { onion_packet, .. }, skimmed_fee_msat, ..
4329                                                                         },
4330                                                                 }) => {
4331                                                                         log_trace!(self.logger, "Adding HTLC from short id {} with payment_hash {} to channel with short id {} after delay", prev_short_channel_id, &payment_hash, short_chan_id);
4332                                                                         let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
4333                                                                                 short_channel_id: prev_short_channel_id,
4334                                                                                 user_channel_id: Some(prev_user_channel_id),
4335                                                                                 outpoint: prev_funding_outpoint,
4336                                                                                 htlc_id: prev_htlc_id,
4337                                                                                 incoming_packet_shared_secret: incoming_shared_secret,
4338                                                                                 // Phantom payments are only PendingHTLCRouting::Receive.
4339                                                                                 phantom_shared_secret: None,
4340                                                                         });
4341                                                                         if let Err(e) = chan.queue_add_htlc(outgoing_amt_msat,
4342                                                                                 payment_hash, outgoing_cltv_value, htlc_source.clone(),
4343                                                                                 onion_packet, skimmed_fee_msat, &self.fee_estimator,
4344                                                                                 &self.logger)
4345                                                                         {
4346                                                                                 if let ChannelError::Ignore(msg) = e {
4347                                                                                         log_trace!(self.logger, "Failed to forward HTLC with payment_hash {}: {}", &payment_hash, msg);
4348                                                                                 } else {
4349                                                                                         panic!("Stated return value requirements in send_htlc() were not met");
4350                                                                                 }
4351                                                                                 let (failure_code, data) = self.get_htlc_temp_fail_err_and_data(0x1000|7, short_chan_id, chan);
4352                                                                                 failed_forwards.push((htlc_source, payment_hash,
4353                                                                                         HTLCFailReason::reason(failure_code, data),
4354                                                                                         HTLCDestination::NextHopChannel { node_id: Some(chan.context.get_counterparty_node_id()), channel_id: forward_chan_id }
4355                                                                                 ));
4356                                                                                 continue;
4357                                                                         }
4358                                                                 },
4359                                                                 HTLCForwardInfo::AddHTLC { .. } => {
4360                                                                         panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
4361                                                                 },
4362                                                                 HTLCForwardInfo::FailHTLC { htlc_id, err_packet } => {
4363                                                                         log_trace!(self.logger, "Failing HTLC back to channel with short id {} (backward HTLC ID {}) after delay", short_chan_id, htlc_id);
4364                                                                         if let Err(e) = chan.queue_fail_htlc(
4365                                                                                 htlc_id, err_packet, &self.logger
4366                                                                         ) {
4367                                                                                 if let ChannelError::Ignore(msg) = e {
4368                                                                                         log_trace!(self.logger, "Failed to fail HTLC with ID {} backwards to short_id {}: {}", htlc_id, short_chan_id, msg);
4369                                                                                 } else {
4370                                                                                         panic!("Stated return value requirements in queue_fail_htlc() were not met");
4371                                                                                 }
4372                                                                                 // fail-backs are best-effort, we probably already have one
4373                                                                                 // pending, and if not that's OK, if not, the channel is on
4374                                                                                 // the chain and sending the HTLC-Timeout is their problem.
4375                                                                                 continue;
4376                                                                         }
4377                                                                 },
4378                                                         }
4379                                                 }
4380                                         } else {
4381                                                 forwarding_channel_not_found!();
4382                                                 continue;
4383                                         }
4384                                 } else {
4385                                         'next_forwardable_htlc: for forward_info in pending_forwards.drain(..) {
4386                                                 match forward_info {
4387                                                         HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
4388                                                                 prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, prev_user_channel_id,
4389                                                                 forward_info: PendingHTLCInfo {
4390                                                                         routing, incoming_shared_secret, payment_hash, incoming_amt_msat, outgoing_amt_msat,
4391                                                                         skimmed_fee_msat, ..
4392                                                                 }
4393                                                         }) => {
4394                                                                 let (cltv_expiry, onion_payload, payment_data, phantom_shared_secret, mut onion_fields) = match routing {
4395                                                                         PendingHTLCRouting::Receive { payment_data, payment_metadata, incoming_cltv_expiry, phantom_shared_secret, custom_tlvs } => {
4396                                                                                 let _legacy_hop_data = Some(payment_data.clone());
4397                                                                                 let onion_fields = RecipientOnionFields { payment_secret: Some(payment_data.payment_secret),
4398                                                                                                 payment_metadata, custom_tlvs };
4399                                                                                 (incoming_cltv_expiry, OnionPayload::Invoice { _legacy_hop_data },
4400                                                                                         Some(payment_data), phantom_shared_secret, onion_fields)
4401                                                                         },
4402                                                                         PendingHTLCRouting::ReceiveKeysend { payment_data, payment_preimage, payment_metadata, incoming_cltv_expiry, custom_tlvs } => {
4403                                                                                 let onion_fields = RecipientOnionFields {
4404                                                                                         payment_secret: payment_data.as_ref().map(|data| data.payment_secret),
4405                                                                                         payment_metadata,
4406                                                                                         custom_tlvs,
4407                                                                                 };
4408                                                                                 (incoming_cltv_expiry, OnionPayload::Spontaneous(payment_preimage),
4409                                                                                         payment_data, None, onion_fields)
4410                                                                         },
4411                                                                         _ => {
4412                                                                                 panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
4413                                                                         }
4414                                                                 };
4415                                                                 let claimable_htlc = ClaimableHTLC {
4416                                                                         prev_hop: HTLCPreviousHopData {
4417                                                                                 short_channel_id: prev_short_channel_id,
4418                                                                                 user_channel_id: Some(prev_user_channel_id),
4419                                                                                 outpoint: prev_funding_outpoint,
4420                                                                                 htlc_id: prev_htlc_id,
4421                                                                                 incoming_packet_shared_secret: incoming_shared_secret,
4422                                                                                 phantom_shared_secret,
4423                                                                         },
4424                                                                         // We differentiate the received value from the sender intended value
4425                                                                         // if possible so that we don't prematurely mark MPP payments complete
4426                                                                         // if routing nodes overpay
4427                                                                         value: incoming_amt_msat.unwrap_or(outgoing_amt_msat),
4428                                                                         sender_intended_value: outgoing_amt_msat,
4429                                                                         timer_ticks: 0,
4430                                                                         total_value_received: None,
4431                                                                         total_msat: if let Some(data) = &payment_data { data.total_msat } else { outgoing_amt_msat },
4432                                                                         cltv_expiry,
4433                                                                         onion_payload,
4434                                                                         counterparty_skimmed_fee_msat: skimmed_fee_msat,
4435                                                                 };
4436
4437                                                                 let mut committed_to_claimable = false;
4438
4439                                                                 macro_rules! fail_htlc {
4440                                                                         ($htlc: expr, $payment_hash: expr) => {
4441                                                                                 debug_assert!(!committed_to_claimable);
4442                                                                                 let mut htlc_msat_height_data = $htlc.value.to_be_bytes().to_vec();
4443                                                                                 htlc_msat_height_data.extend_from_slice(
4444                                                                                         &self.best_block.read().unwrap().height().to_be_bytes(),
4445                                                                                 );
4446                                                                                 failed_forwards.push((HTLCSource::PreviousHopData(HTLCPreviousHopData {
4447                                                                                                 short_channel_id: $htlc.prev_hop.short_channel_id,
4448                                                                                                 user_channel_id: $htlc.prev_hop.user_channel_id,
4449                                                                                                 outpoint: prev_funding_outpoint,
4450                                                                                                 htlc_id: $htlc.prev_hop.htlc_id,
4451                                                                                                 incoming_packet_shared_secret: $htlc.prev_hop.incoming_packet_shared_secret,
4452                                                                                                 phantom_shared_secret,
4453                                                                                         }), payment_hash,
4454                                                                                         HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
4455                                                                                         HTLCDestination::FailedPayment { payment_hash: $payment_hash },
4456                                                                                 ));
4457                                                                                 continue 'next_forwardable_htlc;
4458                                                                         }
4459                                                                 }
4460                                                                 let phantom_shared_secret = claimable_htlc.prev_hop.phantom_shared_secret;
4461                                                                 let mut receiver_node_id = self.our_network_pubkey;
4462                                                                 if phantom_shared_secret.is_some() {
4463                                                                         receiver_node_id = self.node_signer.get_node_id(Recipient::PhantomNode)
4464                                                                                 .expect("Failed to get node_id for phantom node recipient");
4465                                                                 }
4466
4467                                                                 macro_rules! check_total_value {
4468                                                                         ($purpose: expr) => {{
4469                                                                                 let mut payment_claimable_generated = false;
4470                                                                                 let is_keysend = match $purpose {
4471                                                                                         events::PaymentPurpose::SpontaneousPayment(_) => true,
4472                                                                                         events::PaymentPurpose::InvoicePayment { .. } => false,
4473                                                                                 };
4474                                                                                 let mut claimable_payments = self.claimable_payments.lock().unwrap();
4475                                                                                 if claimable_payments.pending_claiming_payments.contains_key(&payment_hash) {
4476                                                                                         fail_htlc!(claimable_htlc, payment_hash);
4477                                                                                 }
4478                                                                                 let ref mut claimable_payment = claimable_payments.claimable_payments
4479                                                                                         .entry(payment_hash)
4480                                                                                         // Note that if we insert here we MUST NOT fail_htlc!()
4481                                                                                         .or_insert_with(|| {
4482                                                                                                 committed_to_claimable = true;
4483                                                                                                 ClaimablePayment {
4484                                                                                                         purpose: $purpose.clone(), htlcs: Vec::new(), onion_fields: None,
4485                                                                                                 }
4486                                                                                         });
4487                                                                                 if $purpose != claimable_payment.purpose {
4488                                                                                         let log_keysend = |keysend| if keysend { "keysend" } else { "non-keysend" };
4489                                                                                         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));
4490                                                                                         fail_htlc!(claimable_htlc, payment_hash);
4491                                                                                 }
4492                                                                                 if !self.default_configuration.accept_mpp_keysend && is_keysend && !claimable_payment.htlcs.is_empty() {
4493                                                                                         log_trace!(self.logger, "Failing new keysend HTLC with payment_hash {} as we already had an existing keysend HTLC with the same payment hash and our config states we don't accept MPP keysend", &payment_hash);
4494                                                                                         fail_htlc!(claimable_htlc, payment_hash);
4495                                                                                 }
4496                                                                                 if let Some(earlier_fields) = &mut claimable_payment.onion_fields {
4497                                                                                         if earlier_fields.check_merge(&mut onion_fields).is_err() {
4498                                                                                                 fail_htlc!(claimable_htlc, payment_hash);
4499                                                                                         }
4500                                                                                 } else {
4501                                                                                         claimable_payment.onion_fields = Some(onion_fields);
4502                                                                                 }
4503                                                                                 let ref mut htlcs = &mut claimable_payment.htlcs;
4504                                                                                 let mut total_value = claimable_htlc.sender_intended_value;
4505                                                                                 let mut earliest_expiry = claimable_htlc.cltv_expiry;
4506                                                                                 for htlc in htlcs.iter() {
4507                                                                                         total_value += htlc.sender_intended_value;
4508                                                                                         earliest_expiry = cmp::min(earliest_expiry, htlc.cltv_expiry);
4509                                                                                         if htlc.total_msat != claimable_htlc.total_msat {
4510                                                                                                 log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the HTLCs had inconsistent total values (eg {} and {})",
4511                                                                                                         &payment_hash, claimable_htlc.total_msat, htlc.total_msat);
4512                                                                                                 total_value = msgs::MAX_VALUE_MSAT;
4513                                                                                         }
4514                                                                                         if total_value >= msgs::MAX_VALUE_MSAT { break; }
4515                                                                                 }
4516                                                                                 // The condition determining whether an MPP is complete must
4517                                                                                 // match exactly the condition used in `timer_tick_occurred`
4518                                                                                 if total_value >= msgs::MAX_VALUE_MSAT {
4519                                                                                         fail_htlc!(claimable_htlc, payment_hash);
4520                                                                                 } else if total_value - claimable_htlc.sender_intended_value >= claimable_htlc.total_msat {
4521                                                                                         log_trace!(self.logger, "Failing HTLC with payment_hash {} as payment is already claimable",
4522                                                                                                 &payment_hash);
4523                                                                                         fail_htlc!(claimable_htlc, payment_hash);
4524                                                                                 } else if total_value >= claimable_htlc.total_msat {
4525                                                                                         #[allow(unused_assignments)] {
4526                                                                                                 committed_to_claimable = true;
4527                                                                                         }
4528                                                                                         let prev_channel_id = prev_funding_outpoint.to_channel_id();
4529                                                                                         htlcs.push(claimable_htlc);
4530                                                                                         let amount_msat = htlcs.iter().map(|htlc| htlc.value).sum();
4531                                                                                         htlcs.iter_mut().for_each(|htlc| htlc.total_value_received = Some(amount_msat));
4532                                                                                         let counterparty_skimmed_fee_msat = htlcs.iter()
4533                                                                                                 .map(|htlc| htlc.counterparty_skimmed_fee_msat.unwrap_or(0)).sum();
4534                                                                                         debug_assert!(total_value.saturating_sub(amount_msat) <=
4535                                                                                                 counterparty_skimmed_fee_msat);
4536                                                                                         new_events.push_back((events::Event::PaymentClaimable {
4537                                                                                                 receiver_node_id: Some(receiver_node_id),
4538                                                                                                 payment_hash,
4539                                                                                                 purpose: $purpose,
4540                                                                                                 amount_msat,
4541                                                                                                 counterparty_skimmed_fee_msat,
4542                                                                                                 via_channel_id: Some(prev_channel_id),
4543                                                                                                 via_user_channel_id: Some(prev_user_channel_id),
4544                                                                                                 claim_deadline: Some(earliest_expiry - HTLC_FAIL_BACK_BUFFER),
4545                                                                                                 onion_fields: claimable_payment.onion_fields.clone(),
4546                                                                                         }, None));
4547                                                                                         payment_claimable_generated = true;
4548                                                                                 } else {
4549                                                                                         // Nothing to do - we haven't reached the total
4550                                                                                         // payment value yet, wait until we receive more
4551                                                                                         // MPP parts.
4552                                                                                         htlcs.push(claimable_htlc);
4553                                                                                         #[allow(unused_assignments)] {
4554                                                                                                 committed_to_claimable = true;
4555                                                                                         }
4556                                                                                 }
4557                                                                                 payment_claimable_generated
4558                                                                         }}
4559                                                                 }
4560
4561                                                                 // Check that the payment hash and secret are known. Note that we
4562                                                                 // MUST take care to handle the "unknown payment hash" and
4563                                                                 // "incorrect payment secret" cases here identically or we'd expose
4564                                                                 // that we are the ultimate recipient of the given payment hash.
4565                                                                 // Further, we must not expose whether we have any other HTLCs
4566                                                                 // associated with the same payment_hash pending or not.
4567                                                                 let mut payment_secrets = self.pending_inbound_payments.lock().unwrap();
4568                                                                 match payment_secrets.entry(payment_hash) {
4569                                                                         hash_map::Entry::Vacant(_) => {
4570                                                                                 match claimable_htlc.onion_payload {
4571                                                                                         OnionPayload::Invoice { .. } => {
4572                                                                                                 let payment_data = payment_data.unwrap();
4573                                                                                                 let (payment_preimage, min_final_cltv_expiry_delta) = match inbound_payment::verify(payment_hash, &payment_data, self.highest_seen_timestamp.load(Ordering::Acquire) as u64, &self.inbound_payment_key, &self.logger) {
4574                                                                                                         Ok(result) => result,
4575                                                                                                         Err(()) => {
4576                                                                                                                 log_trace!(self.logger, "Failing new HTLC with payment_hash {} as payment verification failed", &payment_hash);
4577                                                                                                                 fail_htlc!(claimable_htlc, payment_hash);
4578                                                                                                         }
4579                                                                                                 };
4580                                                                                                 if let Some(min_final_cltv_expiry_delta) = min_final_cltv_expiry_delta {
4581                                                                                                         let expected_min_expiry_height = (self.current_best_block().height() + min_final_cltv_expiry_delta as u32) as u64;
4582                                                                                                         if (cltv_expiry as u64) < expected_min_expiry_height {
4583                                                                                                                 log_trace!(self.logger, "Failing new HTLC with payment_hash {} as its CLTV expiry was too soon (had {}, earliest expected {})",
4584                                                                                                                         &payment_hash, cltv_expiry, expected_min_expiry_height);
4585                                                                                                                 fail_htlc!(claimable_htlc, payment_hash);
4586                                                                                                         }
4587                                                                                                 }
4588                                                                                                 let purpose = events::PaymentPurpose::InvoicePayment {
4589                                                                                                         payment_preimage: payment_preimage.clone(),
4590                                                                                                         payment_secret: payment_data.payment_secret,
4591                                                                                                 };
4592                                                                                                 check_total_value!(purpose);
4593                                                                                         },
4594                                                                                         OnionPayload::Spontaneous(preimage) => {
4595                                                                                                 let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
4596                                                                                                 check_total_value!(purpose);
4597                                                                                         }
4598                                                                                 }
4599                                                                         },
4600                                                                         hash_map::Entry::Occupied(inbound_payment) => {
4601                                                                                 if let OnionPayload::Spontaneous(_) = claimable_htlc.onion_payload {
4602                                                                                         log_trace!(self.logger, "Failing new keysend HTLC with payment_hash {} because we already have an inbound payment with the same payment hash", &payment_hash);
4603                                                                                         fail_htlc!(claimable_htlc, payment_hash);
4604                                                                                 }
4605                                                                                 let payment_data = payment_data.unwrap();
4606                                                                                 if inbound_payment.get().payment_secret != payment_data.payment_secret {
4607                                                                                         log_trace!(self.logger, "Failing new HTLC with payment_hash {} as it didn't match our expected payment secret.", &payment_hash);
4608                                                                                         fail_htlc!(claimable_htlc, payment_hash);
4609                                                                                 } else if inbound_payment.get().min_value_msat.is_some() && payment_data.total_msat < inbound_payment.get().min_value_msat.unwrap() {
4610                                                                                         log_trace!(self.logger, "Failing new HTLC with payment_hash {} as it didn't match our minimum value (had {}, needed {}).",
4611                                                                                                 &payment_hash, payment_data.total_msat, inbound_payment.get().min_value_msat.unwrap());
4612                                                                                         fail_htlc!(claimable_htlc, payment_hash);
4613                                                                                 } else {
4614                                                                                         let purpose = events::PaymentPurpose::InvoicePayment {
4615                                                                                                 payment_preimage: inbound_payment.get().payment_preimage,
4616                                                                                                 payment_secret: payment_data.payment_secret,
4617                                                                                         };
4618                                                                                         let payment_claimable_generated = check_total_value!(purpose);
4619                                                                                         if payment_claimable_generated {
4620                                                                                                 inbound_payment.remove_entry();
4621                                                                                         }
4622                                                                                 }
4623                                                                         },
4624                                                                 };
4625                                                         },
4626                                                         HTLCForwardInfo::FailHTLC { .. } => {
4627                                                                 panic!("Got pending fail of our own HTLC");
4628                                                         }
4629                                                 }
4630                                         }
4631                                 }
4632                         }
4633                 }
4634
4635                 let best_block_height = self.best_block.read().unwrap().height();
4636                 self.pending_outbound_payments.check_retry_payments(&self.router, || self.list_usable_channels(),
4637                         || self.compute_inflight_htlcs(), &self.entropy_source, &self.node_signer, best_block_height,
4638                         &self.pending_events, &self.logger, |args| self.send_payment_along_path(args));
4639
4640                 for (htlc_source, payment_hash, failure_reason, destination) in failed_forwards.drain(..) {
4641                         self.fail_htlc_backwards_internal(&htlc_source, &payment_hash, &failure_reason, destination);
4642                 }
4643                 self.forward_htlcs(&mut phantom_receives);
4644
4645                 // Freeing the holding cell here is relatively redundant - in practice we'll do it when we
4646                 // next get a `get_and_clear_pending_msg_events` call, but some tests rely on it, and it's
4647                 // nice to do the work now if we can rather than while we're trying to get messages in the
4648                 // network stack.
4649                 self.check_free_holding_cells();
4650
4651                 if new_events.is_empty() { return }
4652                 let mut events = self.pending_events.lock().unwrap();
4653                 events.append(&mut new_events);
4654         }
4655
4656         /// Free the background events, generally called from [`PersistenceNotifierGuard`] constructors.
4657         ///
4658         /// Expects the caller to have a total_consistency_lock read lock.
4659         fn process_background_events(&self) -> NotifyOption {
4660                 debug_assert_ne!(self.total_consistency_lock.held_by_thread(), LockHeldState::NotHeldByThread);
4661
4662                 self.background_events_processed_since_startup.store(true, Ordering::Release);
4663
4664                 let mut background_events = Vec::new();
4665                 mem::swap(&mut *self.pending_background_events.lock().unwrap(), &mut background_events);
4666                 if background_events.is_empty() {
4667                         return NotifyOption::SkipPersistNoEvents;
4668                 }
4669
4670                 for event in background_events.drain(..) {
4671                         match event {
4672                                 BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup((funding_txo, update)) => {
4673                                         // The channel has already been closed, so no use bothering to care about the
4674                                         // monitor updating completing.
4675                                         let _ = self.chain_monitor.update_channel(funding_txo, &update);
4676                                 },
4677                                 BackgroundEvent::MonitorUpdateRegeneratedOnStartup { counterparty_node_id, funding_txo, update } => {
4678                                         let mut updated_chan = false;
4679                                         {
4680                                                 let per_peer_state = self.per_peer_state.read().unwrap();
4681                                                 if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
4682                                                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4683                                                         let peer_state = &mut *peer_state_lock;
4684                                                         match peer_state.channel_by_id.entry(funding_txo.to_channel_id()) {
4685                                                                 hash_map::Entry::Occupied(mut chan_phase) => {
4686                                                                         if let ChannelPhase::Funded(chan) = chan_phase.get_mut() {
4687                                                                                 updated_chan = true;
4688                                                                                 handle_new_monitor_update!(self, funding_txo, update.clone(),
4689                                                                                         peer_state_lock, peer_state, per_peer_state, chan);
4690                                                                         } else {
4691                                                                                 debug_assert!(false, "We shouldn't have an update for a non-funded channel");
4692                                                                         }
4693                                                                 },
4694                                                                 hash_map::Entry::Vacant(_) => {},
4695                                                         }
4696                                                 }
4697                                         }
4698                                         if !updated_chan {
4699                                                 // TODO: Track this as in-flight even though the channel is closed.
4700                                                 let _ = self.chain_monitor.update_channel(funding_txo, &update);
4701                                         }
4702                                 },
4703                                 BackgroundEvent::MonitorUpdatesComplete { counterparty_node_id, channel_id } => {
4704                                         let per_peer_state = self.per_peer_state.read().unwrap();
4705                                         if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
4706                                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4707                                                 let peer_state = &mut *peer_state_lock;
4708                                                 if let Some(ChannelPhase::Funded(chan)) = peer_state.channel_by_id.get_mut(&channel_id) {
4709                                                         handle_monitor_update_completion!(self, peer_state_lock, peer_state, per_peer_state, chan);
4710                                                 } else {
4711                                                         let update_actions = peer_state.monitor_update_blocked_actions
4712                                                                 .remove(&channel_id).unwrap_or(Vec::new());
4713                                                         mem::drop(peer_state_lock);
4714                                                         mem::drop(per_peer_state);
4715                                                         self.handle_monitor_update_completion_actions(update_actions);
4716                                                 }
4717                                         }
4718                                 },
4719                         }
4720                 }
4721                 NotifyOption::DoPersist
4722         }
4723
4724         #[cfg(any(test, feature = "_test_utils"))]
4725         /// Process background events, for functional testing
4726         pub fn test_process_background_events(&self) {
4727                 let _lck = self.total_consistency_lock.read().unwrap();
4728                 let _ = self.process_background_events();
4729         }
4730
4731         fn update_channel_fee(&self, chan_id: &ChannelId, chan: &mut Channel<SP>, new_feerate: u32) -> NotifyOption {
4732                 if !chan.context.is_outbound() { return NotifyOption::SkipPersistNoEvents; }
4733                 // If the feerate has decreased by less than half, don't bother
4734                 if new_feerate <= chan.context.get_feerate_sat_per_1000_weight() && new_feerate * 2 > chan.context.get_feerate_sat_per_1000_weight() {
4735                         if new_feerate != chan.context.get_feerate_sat_per_1000_weight() {
4736                                 log_trace!(self.logger, "Channel {} does not qualify for a feerate change from {} to {}.",
4737                                 chan_id, chan.context.get_feerate_sat_per_1000_weight(), new_feerate);
4738                         }
4739                         return NotifyOption::SkipPersistNoEvents;
4740                 }
4741                 if !chan.context.is_live() {
4742                         log_trace!(self.logger, "Channel {} does not qualify for a feerate change from {} to {} as it cannot currently be updated (probably the peer is disconnected).",
4743                                 chan_id, chan.context.get_feerate_sat_per_1000_weight(), new_feerate);
4744                         return NotifyOption::SkipPersistNoEvents;
4745                 }
4746                 log_trace!(self.logger, "Channel {} qualifies for a feerate change from {} to {}.",
4747                         &chan_id, chan.context.get_feerate_sat_per_1000_weight(), new_feerate);
4748
4749                 chan.queue_update_fee(new_feerate, &self.fee_estimator, &self.logger);
4750                 NotifyOption::DoPersist
4751         }
4752
4753         #[cfg(fuzzing)]
4754         /// In chanmon_consistency we want to sometimes do the channel fee updates done in
4755         /// timer_tick_occurred, but we can't generate the disabled channel updates as it considers
4756         /// these a fuzz failure (as they usually indicate a channel force-close, which is exactly what
4757         /// it wants to detect). Thus, we have a variant exposed here for its benefit.
4758         pub fn maybe_update_chan_fees(&self) {
4759                 PersistenceNotifierGuard::optionally_notify(self, || {
4760                         let mut should_persist = NotifyOption::SkipPersistNoEvents;
4761
4762                         let normal_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
4763                         let min_mempool_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::MempoolMinimum);
4764
4765                         let per_peer_state = self.per_peer_state.read().unwrap();
4766                         for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
4767                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4768                                 let peer_state = &mut *peer_state_lock;
4769                                 for (chan_id, chan) in peer_state.channel_by_id.iter_mut().filter_map(
4770                                         |(chan_id, phase)| if let ChannelPhase::Funded(chan) = phase { Some((chan_id, chan)) } else { None }
4771                                 ) {
4772                                         let new_feerate = if chan.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
4773                                                 min_mempool_feerate
4774                                         } else {
4775                                                 normal_feerate
4776                                         };
4777                                         let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
4778                                         if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
4779                                 }
4780                         }
4781
4782                         should_persist
4783                 });
4784         }
4785
4786         /// Performs actions which should happen on startup and roughly once per minute thereafter.
4787         ///
4788         /// This currently includes:
4789         ///  * Increasing or decreasing the on-chain feerate estimates for our outbound channels,
4790         ///  * Broadcasting [`ChannelUpdate`] messages if we've been disconnected from our peer for more
4791         ///    than a minute, informing the network that they should no longer attempt to route over
4792         ///    the channel.
4793         ///  * Expiring a channel's previous [`ChannelConfig`] if necessary to only allow forwarding HTLCs
4794         ///    with the current [`ChannelConfig`].
4795         ///  * Removing peers which have disconnected but and no longer have any channels.
4796         ///  * Force-closing and removing channels which have not completed establishment in a timely manner.
4797         ///  * Forgetting about stale outbound payments, either those that have already been fulfilled
4798         ///    or those awaiting an invoice that hasn't been delivered in the necessary amount of time.
4799         ///    The latter is determined using the system clock in `std` and the block time minus two
4800         ///    hours in `no-std`.
4801         ///
4802         /// Note that this may cause reentrancy through [`chain::Watch::update_channel`] calls or feerate
4803         /// estimate fetches.
4804         ///
4805         /// [`ChannelUpdate`]: msgs::ChannelUpdate
4806         /// [`ChannelConfig`]: crate::util::config::ChannelConfig
4807         pub fn timer_tick_occurred(&self) {
4808                 PersistenceNotifierGuard::optionally_notify(self, || {
4809                         let mut should_persist = NotifyOption::SkipPersistNoEvents;
4810
4811                         let normal_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
4812                         let min_mempool_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::MempoolMinimum);
4813
4814                         let mut handle_errors: Vec<(Result<(), _>, _)> = Vec::new();
4815                         let mut timed_out_mpp_htlcs = Vec::new();
4816                         let mut pending_peers_awaiting_removal = Vec::new();
4817                         let mut shutdown_channels = Vec::new();
4818
4819                         let mut process_unfunded_channel_tick = |
4820                                 chan_id: &ChannelId,
4821                                 context: &mut ChannelContext<SP>,
4822                                 unfunded_context: &mut UnfundedChannelContext,
4823                                 pending_msg_events: &mut Vec<MessageSendEvent>,
4824                                 counterparty_node_id: PublicKey,
4825                         | {
4826                                 context.maybe_expire_prev_config();
4827                                 if unfunded_context.should_expire_unfunded_channel() {
4828                                         log_error!(self.logger,
4829                                                 "Force-closing pending channel with ID {} for not establishing in a timely manner", chan_id);
4830                                         update_maps_on_chan_removal!(self, &context);
4831                                         self.issue_channel_close_events(&context, ClosureReason::HolderForceClosed);
4832                                         shutdown_channels.push(context.force_shutdown(false));
4833                                         pending_msg_events.push(MessageSendEvent::HandleError {
4834                                                 node_id: counterparty_node_id,
4835                                                 action: msgs::ErrorAction::SendErrorMessage {
4836                                                         msg: msgs::ErrorMessage {
4837                                                                 channel_id: *chan_id,
4838                                                                 data: "Force-closing pending channel due to timeout awaiting establishment handshake".to_owned(),
4839                                                         },
4840                                                 },
4841                                         });
4842                                         false
4843                                 } else {
4844                                         true
4845                                 }
4846                         };
4847
4848                         {
4849                                 let per_peer_state = self.per_peer_state.read().unwrap();
4850                                 for (counterparty_node_id, peer_state_mutex) in per_peer_state.iter() {
4851                                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4852                                         let peer_state = &mut *peer_state_lock;
4853                                         let pending_msg_events = &mut peer_state.pending_msg_events;
4854                                         let counterparty_node_id = *counterparty_node_id;
4855                                         peer_state.channel_by_id.retain(|chan_id, phase| {
4856                                                 match phase {
4857                                                         ChannelPhase::Funded(chan) => {
4858                                                                 let new_feerate = if chan.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
4859                                                                         min_mempool_feerate
4860                                                                 } else {
4861                                                                         normal_feerate
4862                                                                 };
4863                                                                 let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
4864                                                                 if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
4865
4866                                                                 if let Err(e) = chan.timer_check_closing_negotiation_progress() {
4867                                                                         let (needs_close, err) = convert_chan_phase_err!(self, e, chan, chan_id, FUNDED_CHANNEL);
4868                                                                         handle_errors.push((Err(err), counterparty_node_id));
4869                                                                         if needs_close { return false; }
4870                                                                 }
4871
4872                                                                 match chan.channel_update_status() {
4873                                                                         ChannelUpdateStatus::Enabled if !chan.context.is_live() => chan.set_channel_update_status(ChannelUpdateStatus::DisabledStaged(0)),
4874                                                                         ChannelUpdateStatus::Disabled if chan.context.is_live() => chan.set_channel_update_status(ChannelUpdateStatus::EnabledStaged(0)),
4875                                                                         ChannelUpdateStatus::DisabledStaged(_) if chan.context.is_live()
4876                                                                                 => chan.set_channel_update_status(ChannelUpdateStatus::Enabled),
4877                                                                         ChannelUpdateStatus::EnabledStaged(_) if !chan.context.is_live()
4878                                                                                 => chan.set_channel_update_status(ChannelUpdateStatus::Disabled),
4879                                                                         ChannelUpdateStatus::DisabledStaged(mut n) if !chan.context.is_live() => {
4880                                                                                 n += 1;
4881                                                                                 if n >= DISABLE_GOSSIP_TICKS {
4882                                                                                         chan.set_channel_update_status(ChannelUpdateStatus::Disabled);
4883                                                                                         if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
4884                                                                                                 pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
4885                                                                                                         msg: update
4886                                                                                                 });
4887                                                                                         }
4888                                                                                         should_persist = NotifyOption::DoPersist;
4889                                                                                 } else {
4890                                                                                         chan.set_channel_update_status(ChannelUpdateStatus::DisabledStaged(n));
4891                                                                                 }
4892                                                                         },
4893                                                                         ChannelUpdateStatus::EnabledStaged(mut n) if chan.context.is_live() => {
4894                                                                                 n += 1;
4895                                                                                 if n >= ENABLE_GOSSIP_TICKS {
4896                                                                                         chan.set_channel_update_status(ChannelUpdateStatus::Enabled);
4897                                                                                         if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
4898                                                                                                 pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
4899                                                                                                         msg: update
4900                                                                                                 });
4901                                                                                         }
4902                                                                                         should_persist = NotifyOption::DoPersist;
4903                                                                                 } else {
4904                                                                                         chan.set_channel_update_status(ChannelUpdateStatus::EnabledStaged(n));
4905                                                                                 }
4906                                                                         },
4907                                                                         _ => {},
4908                                                                 }
4909
4910                                                                 chan.context.maybe_expire_prev_config();
4911
4912                                                                 if chan.should_disconnect_peer_awaiting_response() {
4913                                                                         log_debug!(self.logger, "Disconnecting peer {} due to not making any progress on channel {}",
4914                                                                                         counterparty_node_id, chan_id);
4915                                                                         pending_msg_events.push(MessageSendEvent::HandleError {
4916                                                                                 node_id: counterparty_node_id,
4917                                                                                 action: msgs::ErrorAction::DisconnectPeerWithWarning {
4918                                                                                         msg: msgs::WarningMessage {
4919                                                                                                 channel_id: *chan_id,
4920                                                                                                 data: "Disconnecting due to timeout awaiting response".to_owned(),
4921                                                                                         },
4922                                                                                 },
4923                                                                         });
4924                                                                 }
4925
4926                                                                 true
4927                                                         },
4928                                                         ChannelPhase::UnfundedInboundV1(chan) => {
4929                                                                 process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
4930                                                                         pending_msg_events, counterparty_node_id)
4931                                                         },
4932                                                         ChannelPhase::UnfundedOutboundV1(chan) => {
4933                                                                 process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
4934                                                                         pending_msg_events, counterparty_node_id)
4935                                                         },
4936                                                 }
4937                                         });
4938
4939                                         for (chan_id, req) in peer_state.inbound_channel_request_by_id.iter_mut() {
4940                                                 if { req.ticks_remaining -= 1 ; req.ticks_remaining } <= 0 {
4941                                                         log_error!(self.logger, "Force-closing unaccepted inbound channel {} for not accepting in a timely manner", &chan_id);
4942                                                         peer_state.pending_msg_events.push(
4943                                                                 events::MessageSendEvent::HandleError {
4944                                                                         node_id: counterparty_node_id,
4945                                                                         action: msgs::ErrorAction::SendErrorMessage {
4946                                                                                 msg: msgs::ErrorMessage { channel_id: chan_id.clone(), data: "Channel force-closed".to_owned() }
4947                                                                         },
4948                                                                 }
4949                                                         );
4950                                                 }
4951                                         }
4952                                         peer_state.inbound_channel_request_by_id.retain(|_, req| req.ticks_remaining > 0);
4953
4954                                         if peer_state.ok_to_remove(true) {
4955                                                 pending_peers_awaiting_removal.push(counterparty_node_id);
4956                                         }
4957                                 }
4958                         }
4959
4960                         // When a peer disconnects but still has channels, the peer's `peer_state` entry in the
4961                         // `per_peer_state` is not removed by the `peer_disconnected` function. If the channels
4962                         // of to that peer is later closed while still being disconnected (i.e. force closed),
4963                         // we therefore need to remove the peer from `peer_state` separately.
4964                         // To avoid having to take the `per_peer_state` `write` lock once the channels are
4965                         // closed, we instead remove such peers awaiting removal here on a timer, to limit the
4966                         // negative effects on parallelism as much as possible.
4967                         if pending_peers_awaiting_removal.len() > 0 {
4968                                 let mut per_peer_state = self.per_peer_state.write().unwrap();
4969                                 for counterparty_node_id in pending_peers_awaiting_removal {
4970                                         match per_peer_state.entry(counterparty_node_id) {
4971                                                 hash_map::Entry::Occupied(entry) => {
4972                                                         // Remove the entry if the peer is still disconnected and we still
4973                                                         // have no channels to the peer.
4974                                                         let remove_entry = {
4975                                                                 let peer_state = entry.get().lock().unwrap();
4976                                                                 peer_state.ok_to_remove(true)
4977                                                         };
4978                                                         if remove_entry {
4979                                                                 entry.remove_entry();
4980                                                         }
4981                                                 },
4982                                                 hash_map::Entry::Vacant(_) => { /* The PeerState has already been removed */ }
4983                                         }
4984                                 }
4985                         }
4986
4987                         self.claimable_payments.lock().unwrap().claimable_payments.retain(|payment_hash, payment| {
4988                                 if payment.htlcs.is_empty() {
4989                                         // This should be unreachable
4990                                         debug_assert!(false);
4991                                         return false;
4992                                 }
4993                                 if let OnionPayload::Invoice { .. } = payment.htlcs[0].onion_payload {
4994                                         // Check if we've received all the parts we need for an MPP (the value of the parts adds to total_msat).
4995                                         // In this case we're not going to handle any timeouts of the parts here.
4996                                         // This condition determining whether the MPP is complete here must match
4997                                         // exactly the condition used in `process_pending_htlc_forwards`.
4998                                         if payment.htlcs[0].total_msat <= payment.htlcs.iter()
4999                                                 .fold(0, |total, htlc| total + htlc.sender_intended_value)
5000                                         {
5001                                                 return true;
5002                                         } else if payment.htlcs.iter_mut().any(|htlc| {
5003                                                 htlc.timer_ticks += 1;
5004                                                 return htlc.timer_ticks >= MPP_TIMEOUT_TICKS
5005                                         }) {
5006                                                 timed_out_mpp_htlcs.extend(payment.htlcs.drain(..)
5007                                                         .map(|htlc: ClaimableHTLC| (htlc.prev_hop, *payment_hash)));
5008                                                 return false;
5009                                         }
5010                                 }
5011                                 true
5012                         });
5013
5014                         for htlc_source in timed_out_mpp_htlcs.drain(..) {
5015                                 let source = HTLCSource::PreviousHopData(htlc_source.0.clone());
5016                                 let reason = HTLCFailReason::from_failure_code(23);
5017                                 let receiver = HTLCDestination::FailedPayment { payment_hash: htlc_source.1 };
5018                                 self.fail_htlc_backwards_internal(&source, &htlc_source.1, &reason, receiver);
5019                         }
5020
5021                         for (err, counterparty_node_id) in handle_errors.drain(..) {
5022                                 let _ = handle_error!(self, err, counterparty_node_id);
5023                         }
5024
5025                         for shutdown_res in shutdown_channels {
5026                                 self.finish_close_channel(shutdown_res);
5027                         }
5028
5029                         #[cfg(feature = "std")]
5030                         let duration_since_epoch = std::time::SystemTime::now()
5031                                 .duration_since(std::time::SystemTime::UNIX_EPOCH)
5032                                 .expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
5033                         #[cfg(not(feature = "std"))]
5034                         let duration_since_epoch = Duration::from_secs(
5035                                 self.highest_seen_timestamp.load(Ordering::Acquire).saturating_sub(7200) as u64
5036                         );
5037
5038                         self.pending_outbound_payments.remove_stale_payments(
5039                                 duration_since_epoch, &self.pending_events
5040                         );
5041
5042                         // Technically we don't need to do this here, but if we have holding cell entries in a
5043                         // channel that need freeing, it's better to do that here and block a background task
5044                         // than block the message queueing pipeline.
5045                         if self.check_free_holding_cells() {
5046                                 should_persist = NotifyOption::DoPersist;
5047                         }
5048
5049                         should_persist
5050                 });
5051         }
5052
5053         /// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
5054         /// after a PaymentClaimable event, failing the HTLC back to its origin and freeing resources
5055         /// along the path (including in our own channel on which we received it).
5056         ///
5057         /// Note that in some cases around unclean shutdown, it is possible the payment may have
5058         /// already been claimed by you via [`ChannelManager::claim_funds`] prior to you seeing (a
5059         /// second copy of) the [`events::Event::PaymentClaimable`] event. Alternatively, the payment
5060         /// may have already been failed automatically by LDK if it was nearing its expiration time.
5061         ///
5062         /// While LDK will never claim a payment automatically on your behalf (i.e. without you calling
5063         /// [`ChannelManager::claim_funds`]), you should still monitor for
5064         /// [`events::Event::PaymentClaimed`] events even for payments you intend to fail, especially on
5065         /// startup during which time claims that were in-progress at shutdown may be replayed.
5066         pub fn fail_htlc_backwards(&self, payment_hash: &PaymentHash) {
5067                 self.fail_htlc_backwards_with_reason(payment_hash, FailureCode::IncorrectOrUnknownPaymentDetails);
5068         }
5069
5070         /// This is a variant of [`ChannelManager::fail_htlc_backwards`] that allows you to specify the
5071         /// reason for the failure.
5072         ///
5073         /// See [`FailureCode`] for valid failure codes.
5074         pub fn fail_htlc_backwards_with_reason(&self, payment_hash: &PaymentHash, failure_code: FailureCode) {
5075                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
5076
5077                 let removed_source = self.claimable_payments.lock().unwrap().claimable_payments.remove(payment_hash);
5078                 if let Some(payment) = removed_source {
5079                         for htlc in payment.htlcs {
5080                                 let reason = self.get_htlc_fail_reason_from_failure_code(failure_code, &htlc);
5081                                 let source = HTLCSource::PreviousHopData(htlc.prev_hop);
5082                                 let receiver = HTLCDestination::FailedPayment { payment_hash: *payment_hash };
5083                                 self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
5084                         }
5085                 }
5086         }
5087
5088         /// Gets error data to form an [`HTLCFailReason`] given a [`FailureCode`] and [`ClaimableHTLC`].
5089         fn get_htlc_fail_reason_from_failure_code(&self, failure_code: FailureCode, htlc: &ClaimableHTLC) -> HTLCFailReason {
5090                 match failure_code {
5091                         FailureCode::TemporaryNodeFailure => HTLCFailReason::from_failure_code(failure_code.into()),
5092                         FailureCode::RequiredNodeFeatureMissing => HTLCFailReason::from_failure_code(failure_code.into()),
5093                         FailureCode::IncorrectOrUnknownPaymentDetails => {
5094                                 let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();
5095                                 htlc_msat_height_data.extend_from_slice(&self.best_block.read().unwrap().height().to_be_bytes());
5096                                 HTLCFailReason::reason(failure_code.into(), htlc_msat_height_data)
5097                         },
5098                         FailureCode::InvalidOnionPayload(data) => {
5099                                 let fail_data = match data {
5100                                         Some((typ, offset)) => [BigSize(typ).encode(), offset.encode()].concat(),
5101                                         None => Vec::new(),
5102                                 };
5103                                 HTLCFailReason::reason(failure_code.into(), fail_data)
5104                         }
5105                 }
5106         }
5107
5108         /// Gets an HTLC onion failure code and error data for an `UPDATE` error, given the error code
5109         /// that we want to return and a channel.
5110         ///
5111         /// This is for failures on the channel on which the HTLC was *received*, not failures
5112         /// forwarding
5113         fn get_htlc_inbound_temp_fail_err_and_data(&self, desired_err_code: u16, chan: &Channel<SP>) -> (u16, Vec<u8>) {
5114                 // We can't be sure what SCID was used when relaying inbound towards us, so we have to
5115                 // guess somewhat. If its a public channel, we figure best to just use the real SCID (as
5116                 // we're not leaking that we have a channel with the counterparty), otherwise we try to use
5117                 // an inbound SCID alias before the real SCID.
5118                 let scid_pref = if chan.context.should_announce() {
5119                         chan.context.get_short_channel_id().or(chan.context.latest_inbound_scid_alias())
5120                 } else {
5121                         chan.context.latest_inbound_scid_alias().or(chan.context.get_short_channel_id())
5122                 };
5123                 if let Some(scid) = scid_pref {
5124                         self.get_htlc_temp_fail_err_and_data(desired_err_code, scid, chan)
5125                 } else {
5126                         (0x4000|10, Vec::new())
5127                 }
5128         }
5129
5130
5131         /// Gets an HTLC onion failure code and error data for an `UPDATE` error, given the error code
5132         /// that we want to return and a channel.
5133         fn get_htlc_temp_fail_err_and_data(&self, desired_err_code: u16, scid: u64, chan: &Channel<SP>) -> (u16, Vec<u8>) {
5134                 debug_assert_eq!(desired_err_code & 0x1000, 0x1000);
5135                 if let Ok(upd) = self.get_channel_update_for_onion(scid, chan) {
5136                         let mut enc = VecWriter(Vec::with_capacity(upd.serialized_length() + 6));
5137                         if desired_err_code == 0x1000 | 20 {
5138                                 // No flags for `disabled_flags` are currently defined so they're always two zero bytes.
5139                                 // See https://github.com/lightning/bolts/blob/341ec84/04-onion-routing.md?plain=1#L1008
5140                                 0u16.write(&mut enc).expect("Writes cannot fail");
5141                         }
5142                         (upd.serialized_length() as u16 + 2).write(&mut enc).expect("Writes cannot fail");
5143                         msgs::ChannelUpdate::TYPE.write(&mut enc).expect("Writes cannot fail");
5144                         upd.write(&mut enc).expect("Writes cannot fail");
5145                         (desired_err_code, enc.0)
5146                 } else {
5147                         // If we fail to get a unicast channel_update, it implies we don't yet have an SCID,
5148                         // which means we really shouldn't have gotten a payment to be forwarded over this
5149                         // channel yet, or if we did it's from a route hint. Either way, returning an error of
5150                         // PERM|no_such_channel should be fine.
5151                         (0x4000|10, Vec::new())
5152                 }
5153         }
5154
5155         // Fail a list of HTLCs that were just freed from the holding cell. The HTLCs need to be
5156         // failed backwards or, if they were one of our outgoing HTLCs, then their failure needs to
5157         // be surfaced to the user.
5158         fn fail_holding_cell_htlcs(
5159                 &self, mut htlcs_to_fail: Vec<(HTLCSource, PaymentHash)>, channel_id: ChannelId,
5160                 counterparty_node_id: &PublicKey
5161         ) {
5162                 let (failure_code, onion_failure_data) = {
5163                         let per_peer_state = self.per_peer_state.read().unwrap();
5164                         if let Some(peer_state_mutex) = per_peer_state.get(counterparty_node_id) {
5165                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
5166                                 let peer_state = &mut *peer_state_lock;
5167                                 match peer_state.channel_by_id.entry(channel_id) {
5168                                         hash_map::Entry::Occupied(chan_phase_entry) => {
5169                                                 if let ChannelPhase::Funded(chan) = chan_phase_entry.get() {
5170                                                         self.get_htlc_inbound_temp_fail_err_and_data(0x1000|7, &chan)
5171                                                 } else {
5172                                                         // We shouldn't be trying to fail holding cell HTLCs on an unfunded channel.
5173                                                         debug_assert!(false);
5174                                                         (0x4000|10, Vec::new())
5175                                                 }
5176                                         },
5177                                         hash_map::Entry::Vacant(_) => (0x4000|10, Vec::new())
5178                                 }
5179                         } else { (0x4000|10, Vec::new()) }
5180                 };
5181
5182                 for (htlc_src, payment_hash) in htlcs_to_fail.drain(..) {
5183                         let reason = HTLCFailReason::reason(failure_code, onion_failure_data.clone());
5184                         let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id };
5185                         self.fail_htlc_backwards_internal(&htlc_src, &payment_hash, &reason, receiver);
5186                 }
5187         }
5188
5189         /// Fails an HTLC backwards to the sender of it to us.
5190         /// Note that we do not assume that channels corresponding to failed HTLCs are still available.
5191         fn fail_htlc_backwards_internal(&self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason, destination: HTLCDestination) {
5192                 // Ensure that no peer state channel storage lock is held when calling this function.
5193                 // This ensures that future code doesn't introduce a lock-order requirement for
5194                 // `forward_htlcs` to be locked after the `per_peer_state` peer locks, which calling
5195                 // this function with any `per_peer_state` peer lock acquired would.
5196                 #[cfg(debug_assertions)]
5197                 for (_, peer) in self.per_peer_state.read().unwrap().iter() {
5198                         debug_assert_ne!(peer.held_by_thread(), LockHeldState::HeldByThread);
5199                 }
5200
5201                 //TODO: There is a timing attack here where if a node fails an HTLC back to us they can
5202                 //identify whether we sent it or not based on the (I presume) very different runtime
5203                 //between the branches here. We should make this async and move it into the forward HTLCs
5204                 //timer handling.
5205
5206                 // Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
5207                 // from block_connected which may run during initialization prior to the chain_monitor
5208                 // being fully configured. See the docs for `ChannelManagerReadArgs` for more.
5209                 match source {
5210                         HTLCSource::OutboundRoute { ref path, ref session_priv, ref payment_id, .. } => {
5211                                 if self.pending_outbound_payments.fail_htlc(source, payment_hash, onion_error, path,
5212                                         session_priv, payment_id, self.probing_cookie_secret, &self.secp_ctx,
5213                                         &self.pending_events, &self.logger)
5214                                 { self.push_pending_forwards_ev(); }
5215                         },
5216                         HTLCSource::PreviousHopData(HTLCPreviousHopData { ref short_channel_id, ref htlc_id, ref incoming_packet_shared_secret, ref phantom_shared_secret, ref outpoint, .. }) => {
5217                                 log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards from us with {:?}", &payment_hash, onion_error);
5218                                 let err_packet = onion_error.get_encrypted_failure_packet(incoming_packet_shared_secret, phantom_shared_secret);
5219
5220                                 let mut push_forward_ev = false;
5221                                 let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
5222                                 if forward_htlcs.is_empty() {
5223                                         push_forward_ev = true;
5224                                 }
5225                                 match forward_htlcs.entry(*short_channel_id) {
5226                                         hash_map::Entry::Occupied(mut entry) => {
5227                                                 entry.get_mut().push(HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet });
5228                                         },
5229                                         hash_map::Entry::Vacant(entry) => {
5230                                                 entry.insert(vec!(HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet }));
5231                                         }
5232                                 }
5233                                 mem::drop(forward_htlcs);
5234                                 if push_forward_ev { self.push_pending_forwards_ev(); }
5235                                 let mut pending_events = self.pending_events.lock().unwrap();
5236                                 pending_events.push_back((events::Event::HTLCHandlingFailed {
5237                                         prev_channel_id: outpoint.to_channel_id(),
5238                                         failed_next_destination: destination,
5239                                 }, None));
5240                         },
5241                 }
5242         }
5243
5244         /// Provides a payment preimage in response to [`Event::PaymentClaimable`], generating any
5245         /// [`MessageSendEvent`]s needed to claim the payment.
5246         ///
5247         /// This method is guaranteed to ensure the payment has been claimed but only if the current
5248         /// height is strictly below [`Event::PaymentClaimable::claim_deadline`]. To avoid race
5249         /// conditions, you should wait for an [`Event::PaymentClaimed`] before considering the payment
5250         /// successful. It will generally be available in the next [`process_pending_events`] call.
5251         ///
5252         /// Note that if you did not set an `amount_msat` when calling [`create_inbound_payment`] or
5253         /// [`create_inbound_payment_for_hash`] you must check that the amount in the `PaymentClaimable`
5254         /// event matches your expectation. If you fail to do so and call this method, you may provide
5255         /// the sender "proof-of-payment" when they did not fulfill the full expected payment.
5256         ///
5257         /// This function will fail the payment if it has custom TLVs with even type numbers, as we
5258         /// will assume they are unknown. If you intend to accept even custom TLVs, you should use
5259         /// [`claim_funds_with_known_custom_tlvs`].
5260         ///
5261         /// [`Event::PaymentClaimable`]: crate::events::Event::PaymentClaimable
5262         /// [`Event::PaymentClaimable::claim_deadline`]: crate::events::Event::PaymentClaimable::claim_deadline
5263         /// [`Event::PaymentClaimed`]: crate::events::Event::PaymentClaimed
5264         /// [`process_pending_events`]: EventsProvider::process_pending_events
5265         /// [`create_inbound_payment`]: Self::create_inbound_payment
5266         /// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
5267         /// [`claim_funds_with_known_custom_tlvs`]: Self::claim_funds_with_known_custom_tlvs
5268         pub fn claim_funds(&self, payment_preimage: PaymentPreimage) {
5269                 self.claim_payment_internal(payment_preimage, false);
5270         }
5271
5272         /// This is a variant of [`claim_funds`] that allows accepting a payment with custom TLVs with
5273         /// even type numbers.
5274         ///
5275         /// # Note
5276         ///
5277         /// You MUST check you've understood all even TLVs before using this to
5278         /// claim, otherwise you may unintentionally agree to some protocol you do not understand.
5279         ///
5280         /// [`claim_funds`]: Self::claim_funds
5281         pub fn claim_funds_with_known_custom_tlvs(&self, payment_preimage: PaymentPreimage) {
5282                 self.claim_payment_internal(payment_preimage, true);
5283         }
5284
5285         fn claim_payment_internal(&self, payment_preimage: PaymentPreimage, custom_tlvs_known: bool) {
5286                 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
5287
5288                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
5289
5290                 let mut sources = {
5291                         let mut claimable_payments = self.claimable_payments.lock().unwrap();
5292                         if let Some(payment) = claimable_payments.claimable_payments.remove(&payment_hash) {
5293                                 let mut receiver_node_id = self.our_network_pubkey;
5294                                 for htlc in payment.htlcs.iter() {
5295                                         if htlc.prev_hop.phantom_shared_secret.is_some() {
5296                                                 let phantom_pubkey = self.node_signer.get_node_id(Recipient::PhantomNode)
5297                                                         .expect("Failed to get node_id for phantom node recipient");
5298                                                 receiver_node_id = phantom_pubkey;
5299                                                 break;
5300                                         }
5301                                 }
5302
5303                                 let htlcs = payment.htlcs.iter().map(events::ClaimedHTLC::from).collect();
5304                                 let sender_intended_value = payment.htlcs.first().map(|htlc| htlc.total_msat);
5305                                 let dup_purpose = claimable_payments.pending_claiming_payments.insert(payment_hash,
5306                                         ClaimingPayment { amount_msat: payment.htlcs.iter().map(|source| source.value).sum(),
5307                                         payment_purpose: payment.purpose, receiver_node_id, htlcs, sender_intended_value
5308                                 });
5309                                 if dup_purpose.is_some() {
5310                                         debug_assert!(false, "Shouldn't get a duplicate pending claim event ever");
5311                                         log_error!(self.logger, "Got a duplicate pending claimable event on payment hash {}! Please report this bug",
5312                                                 &payment_hash);
5313                                 }
5314
5315                                 if let Some(RecipientOnionFields { ref custom_tlvs, .. }) = payment.onion_fields {
5316                                         if !custom_tlvs_known && custom_tlvs.iter().any(|(typ, _)| typ % 2 == 0) {
5317                                                 log_info!(self.logger, "Rejecting payment with payment hash {} as we cannot accept payment with unknown even TLVs: {}",
5318                                                         &payment_hash, log_iter!(custom_tlvs.iter().map(|(typ, _)| typ).filter(|typ| *typ % 2 == 0)));
5319                                                 claimable_payments.pending_claiming_payments.remove(&payment_hash);
5320                                                 mem::drop(claimable_payments);
5321                                                 for htlc in payment.htlcs {
5322                                                         let reason = self.get_htlc_fail_reason_from_failure_code(FailureCode::InvalidOnionPayload(None), &htlc);
5323                                                         let source = HTLCSource::PreviousHopData(htlc.prev_hop);
5324                                                         let receiver = HTLCDestination::FailedPayment { payment_hash };
5325                                                         self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
5326                                                 }
5327                                                 return;
5328                                         }
5329                                 }
5330
5331                                 payment.htlcs
5332                         } else { return; }
5333                 };
5334                 debug_assert!(!sources.is_empty());
5335
5336                 // Just in case one HTLC has been failed between when we generated the `PaymentClaimable`
5337                 // and when we got here we need to check that the amount we're about to claim matches the
5338                 // amount we told the user in the last `PaymentClaimable`. We also do a sanity-check that
5339                 // the MPP parts all have the same `total_msat`.
5340                 let mut claimable_amt_msat = 0;
5341                 let mut prev_total_msat = None;
5342                 let mut expected_amt_msat = None;
5343                 let mut valid_mpp = true;
5344                 let mut errs = Vec::new();
5345                 let per_peer_state = self.per_peer_state.read().unwrap();
5346                 for htlc in sources.iter() {
5347                         if prev_total_msat.is_some() && prev_total_msat != Some(htlc.total_msat) {
5348                                 log_error!(self.logger, "Somehow ended up with an MPP payment with different expected total amounts - this should not be reachable!");
5349                                 debug_assert!(false);
5350                                 valid_mpp = false;
5351                                 break;
5352                         }
5353                         prev_total_msat = Some(htlc.total_msat);
5354
5355                         if expected_amt_msat.is_some() && expected_amt_msat != htlc.total_value_received {
5356                                 log_error!(self.logger, "Somehow ended up with an MPP payment with different received total amounts - this should not be reachable!");
5357                                 debug_assert!(false);
5358                                 valid_mpp = false;
5359                                 break;
5360                         }
5361                         expected_amt_msat = htlc.total_value_received;
5362                         claimable_amt_msat += htlc.value;
5363                 }
5364                 mem::drop(per_peer_state);
5365                 if sources.is_empty() || expected_amt_msat.is_none() {
5366                         self.claimable_payments.lock().unwrap().pending_claiming_payments.remove(&payment_hash);
5367                         log_info!(self.logger, "Attempted to claim an incomplete payment which no longer had any available HTLCs!");
5368                         return;
5369                 }
5370                 if claimable_amt_msat != expected_amt_msat.unwrap() {
5371                         self.claimable_payments.lock().unwrap().pending_claiming_payments.remove(&payment_hash);
5372                         log_info!(self.logger, "Attempted to claim an incomplete payment, expected {} msat, had {} available to claim.",
5373                                 expected_amt_msat.unwrap(), claimable_amt_msat);
5374                         return;
5375                 }
5376                 if valid_mpp {
5377                         for htlc in sources.drain(..) {
5378                                 if let Err((pk, err)) = self.claim_funds_from_hop(
5379                                         htlc.prev_hop, payment_preimage,
5380                                         |_| Some(MonitorUpdateCompletionAction::PaymentClaimed { payment_hash }))
5381                                 {
5382                                         if let msgs::ErrorAction::IgnoreError = err.err.action {
5383                                                 // We got a temporary failure updating monitor, but will claim the
5384                                                 // HTLC when the monitor updating is restored (or on chain).
5385                                                 log_error!(self.logger, "Temporary failure claiming HTLC, treating as success: {}", err.err.err);
5386                                         } else { errs.push((pk, err)); }
5387                                 }
5388                         }
5389                 }
5390                 if !valid_mpp {
5391                         for htlc in sources.drain(..) {
5392                                 let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();
5393                                 htlc_msat_height_data.extend_from_slice(&self.best_block.read().unwrap().height().to_be_bytes());
5394                                 let source = HTLCSource::PreviousHopData(htlc.prev_hop);
5395                                 let reason = HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data);
5396                                 let receiver = HTLCDestination::FailedPayment { payment_hash };
5397                                 self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
5398                         }
5399                         self.claimable_payments.lock().unwrap().pending_claiming_payments.remove(&payment_hash);
5400                 }
5401
5402                 // Now we can handle any errors which were generated.
5403                 for (counterparty_node_id, err) in errs.drain(..) {
5404                         let res: Result<(), _> = Err(err);
5405                         let _ = handle_error!(self, res, counterparty_node_id);
5406                 }
5407         }
5408
5409         fn claim_funds_from_hop<ComplFunc: FnOnce(Option<u64>) -> Option<MonitorUpdateCompletionAction>>(&self,
5410                 prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage, completion_action: ComplFunc)
5411         -> Result<(), (PublicKey, MsgHandleErrInternal)> {
5412                 //TODO: Delay the claimed_funds relaying just like we do outbound relay!
5413
5414                 // If we haven't yet run background events assume we're still deserializing and shouldn't
5415                 // actually pass `ChannelMonitorUpdate`s to users yet. Instead, queue them up as
5416                 // `BackgroundEvent`s.
5417                 let during_init = !self.background_events_processed_since_startup.load(Ordering::Acquire);
5418
5419                 {
5420                         let per_peer_state = self.per_peer_state.read().unwrap();
5421                         let chan_id = prev_hop.outpoint.to_channel_id();
5422                         let counterparty_node_id_opt = match self.short_to_chan_info.read().unwrap().get(&prev_hop.short_channel_id) {
5423                                 Some((cp_id, _dup_chan_id)) => Some(cp_id.clone()),
5424                                 None => None
5425                         };
5426
5427                         let peer_state_opt = counterparty_node_id_opt.as_ref().map(
5428                                 |counterparty_node_id| per_peer_state.get(counterparty_node_id)
5429                                         .map(|peer_mutex| peer_mutex.lock().unwrap())
5430                         ).unwrap_or(None);
5431
5432                         if peer_state_opt.is_some() {
5433                                 let mut peer_state_lock = peer_state_opt.unwrap();
5434                                 let peer_state = &mut *peer_state_lock;
5435                                 if let hash_map::Entry::Occupied(mut chan_phase_entry) = peer_state.channel_by_id.entry(chan_id) {
5436                                         if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
5437                                                 let counterparty_node_id = chan.context.get_counterparty_node_id();
5438                                                 let fulfill_res = chan.get_update_fulfill_htlc_and_commit(prev_hop.htlc_id, payment_preimage, &self.logger);
5439
5440                                                 if let UpdateFulfillCommitFetch::NewClaim { htlc_value_msat, monitor_update } = fulfill_res {
5441                                                         if let Some(action) = completion_action(Some(htlc_value_msat)) {
5442                                                                 log_trace!(self.logger, "Tracking monitor update completion action for channel {}: {:?}",
5443                                                                         chan_id, action);
5444                                                                 peer_state.monitor_update_blocked_actions.entry(chan_id).or_insert(Vec::new()).push(action);
5445                                                         }
5446                                                         if !during_init {
5447                                                                 handle_new_monitor_update!(self, prev_hop.outpoint, monitor_update, peer_state_lock,
5448                                                                         peer_state, per_peer_state, chan);
5449                                                         } else {
5450                                                                 // If we're running during init we cannot update a monitor directly -
5451                                                                 // they probably haven't actually been loaded yet. Instead, push the
5452                                                                 // monitor update as a background event.
5453                                                                 self.pending_background_events.lock().unwrap().push(
5454                                                                         BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
5455                                                                                 counterparty_node_id,
5456                                                                                 funding_txo: prev_hop.outpoint,
5457                                                                                 update: monitor_update.clone(),
5458                                                                         });
5459                                                         }
5460                                                 }
5461                                         }
5462                                         return Ok(());
5463                                 }
5464                         }
5465                 }
5466                 let preimage_update = ChannelMonitorUpdate {
5467                         update_id: CLOSED_CHANNEL_UPDATE_ID,
5468                         updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
5469                                 payment_preimage,
5470                         }],
5471                 };
5472
5473                 if !during_init {
5474                         // We update the ChannelMonitor on the backward link, after
5475                         // receiving an `update_fulfill_htlc` from the forward link.
5476                         let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, &preimage_update);
5477                         if update_res != ChannelMonitorUpdateStatus::Completed {
5478                                 // TODO: This needs to be handled somehow - if we receive a monitor update
5479                                 // with a preimage we *must* somehow manage to propagate it to the upstream
5480                                 // channel, or we must have an ability to receive the same event and try
5481                                 // again on restart.
5482                                 log_error!(self.logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}",
5483                                         payment_preimage, update_res);
5484                         }
5485                 } else {
5486                         // If we're running during init we cannot update a monitor directly - they probably
5487                         // haven't actually been loaded yet. Instead, push the monitor update as a background
5488                         // event.
5489                         // Note that while it's safe to use `ClosedMonitorUpdateRegeneratedOnStartup` here (the
5490                         // channel is already closed) we need to ultimately handle the monitor update
5491                         // completion action only after we've completed the monitor update. This is the only
5492                         // way to guarantee this update *will* be regenerated on startup (otherwise if this was
5493                         // from a forwarded HTLC the downstream preimage may be deleted before we claim
5494                         // upstream). Thus, we need to transition to some new `BackgroundEvent` type which will
5495                         // complete the monitor update completion action from `completion_action`.
5496                         self.pending_background_events.lock().unwrap().push(
5497                                 BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup((
5498                                         prev_hop.outpoint, preimage_update,
5499                                 )));
5500                 }
5501                 // Note that we do process the completion action here. This totally could be a
5502                 // duplicate claim, but we have no way of knowing without interrogating the
5503                 // `ChannelMonitor` we've provided the above update to. Instead, note that `Event`s are
5504                 // generally always allowed to be duplicative (and it's specifically noted in
5505                 // `PaymentForwarded`).
5506                 self.handle_monitor_update_completion_actions(completion_action(None));
5507                 Ok(())
5508         }
5509
5510         fn finalize_claims(&self, sources: Vec<HTLCSource>) {
5511                 self.pending_outbound_payments.finalize_claims(sources, &self.pending_events);
5512         }
5513
5514         fn claim_funds_internal(&self, source: HTLCSource, payment_preimage: PaymentPreimage,
5515                 forwarded_htlc_value_msat: Option<u64>, from_onchain: bool,
5516                 next_channel_counterparty_node_id: Option<PublicKey>, next_channel_outpoint: OutPoint
5517         ) {
5518                 match source {
5519                         HTLCSource::OutboundRoute { session_priv, payment_id, path, .. } => {
5520                                 debug_assert!(self.background_events_processed_since_startup.load(Ordering::Acquire),
5521                                         "We don't support claim_htlc claims during startup - monitors may not be available yet");
5522                                 if let Some(pubkey) = next_channel_counterparty_node_id {
5523                                         debug_assert_eq!(pubkey, path.hops[0].pubkey);
5524                                 }
5525                                 let ev_completion_action = EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
5526                                         channel_funding_outpoint: next_channel_outpoint,
5527                                         counterparty_node_id: path.hops[0].pubkey,
5528                                 };
5529                                 self.pending_outbound_payments.claim_htlc(payment_id, payment_preimage,
5530                                         session_priv, path, from_onchain, ev_completion_action, &self.pending_events,
5531                                         &self.logger);
5532                         },
5533                         HTLCSource::PreviousHopData(hop_data) => {
5534                                 let prev_outpoint = hop_data.outpoint;
5535                                 let completed_blocker = RAAMonitorUpdateBlockingAction::from_prev_hop_data(&hop_data);
5536                                 let res = self.claim_funds_from_hop(hop_data, payment_preimage,
5537                                         |htlc_claim_value_msat| {
5538                                                 if let Some(forwarded_htlc_value) = forwarded_htlc_value_msat {
5539                                                         let fee_earned_msat = if let Some(claimed_htlc_value) = htlc_claim_value_msat {
5540                                                                 Some(claimed_htlc_value - forwarded_htlc_value)
5541                                                         } else { None };
5542
5543                                                         Some(MonitorUpdateCompletionAction::EmitEventAndFreeOtherChannel {
5544                                                                 event: events::Event::PaymentForwarded {
5545                                                                         fee_earned_msat,
5546                                                                         claim_from_onchain_tx: from_onchain,
5547                                                                         prev_channel_id: Some(prev_outpoint.to_channel_id()),
5548                                                                         next_channel_id: Some(next_channel_outpoint.to_channel_id()),
5549                                                                         outbound_amount_forwarded_msat: forwarded_htlc_value_msat,
5550                                                                 },
5551                                                                 downstream_counterparty_and_funding_outpoint:
5552                                                                         if let Some(node_id) = next_channel_counterparty_node_id {
5553                                                                                 Some((node_id, next_channel_outpoint, completed_blocker))
5554                                                                         } else {
5555                                                                                 // We can only get `None` here if we are processing a
5556                                                                                 // `ChannelMonitor`-originated event, in which case we
5557                                                                                 // don't care about ensuring we wake the downstream
5558                                                                                 // channel's monitor updating - the channel is already
5559                                                                                 // closed.
5560                                                                                 None
5561                                                                         },
5562                                                         })
5563                                                 } else { None }
5564                                         });
5565                                 if let Err((pk, err)) = res {
5566                                         let result: Result<(), _> = Err(err);
5567                                         let _ = handle_error!(self, result, pk);
5568                                 }
5569                         },
5570                 }
5571         }
5572
5573         /// Gets the node_id held by this ChannelManager
5574         pub fn get_our_node_id(&self) -> PublicKey {
5575                 self.our_network_pubkey.clone()
5576         }
5577
5578         fn handle_monitor_update_completion_actions<I: IntoIterator<Item=MonitorUpdateCompletionAction>>(&self, actions: I) {
5579                 for action in actions.into_iter() {
5580                         match action {
5581                                 MonitorUpdateCompletionAction::PaymentClaimed { payment_hash } => {
5582                                         let payment = self.claimable_payments.lock().unwrap().pending_claiming_payments.remove(&payment_hash);
5583                                         if let Some(ClaimingPayment {
5584                                                 amount_msat,
5585                                                 payment_purpose: purpose,
5586                                                 receiver_node_id,
5587                                                 htlcs,
5588                                                 sender_intended_value: sender_intended_total_msat,
5589                                         }) = payment {
5590                                                 self.pending_events.lock().unwrap().push_back((events::Event::PaymentClaimed {
5591                                                         payment_hash,
5592                                                         purpose,
5593                                                         amount_msat,
5594                                                         receiver_node_id: Some(receiver_node_id),
5595                                                         htlcs,
5596                                                         sender_intended_total_msat,
5597                                                 }, None));
5598                                         }
5599                                 },
5600                                 MonitorUpdateCompletionAction::EmitEventAndFreeOtherChannel {
5601                                         event, downstream_counterparty_and_funding_outpoint
5602                                 } => {
5603                                         self.pending_events.lock().unwrap().push_back((event, None));
5604                                         if let Some((node_id, funding_outpoint, blocker)) = downstream_counterparty_and_funding_outpoint {
5605                                                 self.handle_monitor_update_release(node_id, funding_outpoint, Some(blocker));
5606                                         }
5607                                 },
5608                         }
5609                 }
5610         }
5611
5612         /// Handles a channel reentering a functional state, either due to reconnect or a monitor
5613         /// update completion.
5614         fn handle_channel_resumption(&self, pending_msg_events: &mut Vec<MessageSendEvent>,
5615                 channel: &mut Channel<SP>, raa: Option<msgs::RevokeAndACK>,
5616                 commitment_update: Option<msgs::CommitmentUpdate>, order: RAACommitmentOrder,
5617                 pending_forwards: Vec<(PendingHTLCInfo, u64)>, funding_broadcastable: Option<Transaction>,
5618                 channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>)
5619         -> Option<(u64, OutPoint, u128, Vec<(PendingHTLCInfo, u64)>)> {
5620                 log_trace!(self.logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {}broadcasting funding, {} channel ready, {} announcement",
5621                         &channel.context.channel_id(),
5622                         if raa.is_some() { "an" } else { "no" },
5623                         if commitment_update.is_some() { "a" } else { "no" }, pending_forwards.len(),
5624                         if funding_broadcastable.is_some() { "" } else { "not " },
5625                         if channel_ready.is_some() { "sending" } else { "without" },
5626                         if announcement_sigs.is_some() { "sending" } else { "without" });
5627
5628                 let mut htlc_forwards = None;
5629
5630                 let counterparty_node_id = channel.context.get_counterparty_node_id();
5631                 if !pending_forwards.is_empty() {
5632                         htlc_forwards = Some((channel.context.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias()),
5633                                 channel.context.get_funding_txo().unwrap(), channel.context.get_user_id(), pending_forwards));
5634                 }
5635
5636                 if let Some(msg) = channel_ready {
5637                         send_channel_ready!(self, pending_msg_events, channel, msg);
5638                 }
5639                 if let Some(msg) = announcement_sigs {
5640                         pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
5641                                 node_id: counterparty_node_id,
5642                                 msg,
5643                         });
5644                 }
5645
5646                 macro_rules! handle_cs { () => {
5647                         if let Some(update) = commitment_update {
5648                                 pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
5649                                         node_id: counterparty_node_id,
5650                                         updates: update,
5651                                 });
5652                         }
5653                 } }
5654                 macro_rules! handle_raa { () => {
5655                         if let Some(revoke_and_ack) = raa {
5656                                 pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
5657                                         node_id: counterparty_node_id,
5658                                         msg: revoke_and_ack,
5659                                 });
5660                         }
5661                 } }
5662                 match order {
5663                         RAACommitmentOrder::CommitmentFirst => {
5664                                 handle_cs!();
5665                                 handle_raa!();
5666                         },
5667                         RAACommitmentOrder::RevokeAndACKFirst => {
5668                                 handle_raa!();
5669                                 handle_cs!();
5670                         },
5671                 }
5672
5673                 if let Some(tx) = funding_broadcastable {
5674                         log_info!(self.logger, "Broadcasting funding transaction with txid {}", tx.txid());
5675                         self.tx_broadcaster.broadcast_transactions(&[&tx]);
5676                 }
5677
5678                 {
5679                         let mut pending_events = self.pending_events.lock().unwrap();
5680                         emit_channel_pending_event!(pending_events, channel);
5681                         emit_channel_ready_event!(pending_events, channel);
5682                 }
5683
5684                 htlc_forwards
5685         }
5686
5687         fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64, counterparty_node_id: Option<&PublicKey>) {
5688                 debug_assert!(self.total_consistency_lock.try_write().is_err()); // Caller holds read lock
5689
5690                 let counterparty_node_id = match counterparty_node_id {
5691                         Some(cp_id) => cp_id.clone(),
5692                         None => {
5693                                 // TODO: Once we can rely on the counterparty_node_id from the
5694                                 // monitor event, this and the id_to_peer map should be removed.
5695                                 let id_to_peer = self.id_to_peer.lock().unwrap();
5696                                 match id_to_peer.get(&funding_txo.to_channel_id()) {
5697                                         Some(cp_id) => cp_id.clone(),
5698                                         None => return,
5699                                 }
5700                         }
5701                 };
5702                 let per_peer_state = self.per_peer_state.read().unwrap();
5703                 let mut peer_state_lock;
5704                 let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
5705                 if peer_state_mutex_opt.is_none() { return }
5706                 peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
5707                 let peer_state = &mut *peer_state_lock;
5708                 let channel =
5709                         if let Some(ChannelPhase::Funded(chan)) = peer_state.channel_by_id.get_mut(&funding_txo.to_channel_id()) {
5710                                 chan
5711                         } else {
5712                                 let update_actions = peer_state.monitor_update_blocked_actions
5713                                         .remove(&funding_txo.to_channel_id()).unwrap_or(Vec::new());
5714                                 mem::drop(peer_state_lock);
5715                                 mem::drop(per_peer_state);
5716                                 self.handle_monitor_update_completion_actions(update_actions);
5717                                 return;
5718                         };
5719                 let remaining_in_flight =
5720                         if let Some(pending) = peer_state.in_flight_monitor_updates.get_mut(funding_txo) {
5721                                 pending.retain(|upd| upd.update_id > highest_applied_update_id);
5722                                 pending.len()
5723                         } else { 0 };
5724                 log_trace!(self.logger, "ChannelMonitor updated to {}. Current highest is {}. {} pending in-flight updates.",
5725                         highest_applied_update_id, channel.context.get_latest_monitor_update_id(),
5726                         remaining_in_flight);
5727                 if !channel.is_awaiting_monitor_update() || channel.context.get_latest_monitor_update_id() != highest_applied_update_id {
5728                         return;
5729                 }
5730                 handle_monitor_update_completion!(self, peer_state_lock, peer_state, per_peer_state, channel);
5731         }
5732
5733         /// Accepts a request to open a channel after a [`Event::OpenChannelRequest`].
5734         ///
5735         /// The `temporary_channel_id` parameter indicates which inbound channel should be accepted,
5736         /// and the `counterparty_node_id` parameter is the id of the peer which has requested to open
5737         /// the channel.
5738         ///
5739         /// The `user_channel_id` parameter will be provided back in
5740         /// [`Event::ChannelClosed::user_channel_id`] to allow tracking of which events correspond
5741         /// with which `accept_inbound_channel`/`accept_inbound_channel_from_trusted_peer_0conf` call.
5742         ///
5743         /// Note that this method will return an error and reject the channel, if it requires support
5744         /// for zero confirmations. Instead, `accept_inbound_channel_from_trusted_peer_0conf` must be
5745         /// used to accept such channels.
5746         ///
5747         /// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
5748         /// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
5749         pub fn accept_inbound_channel(&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, user_channel_id: u128) -> Result<(), APIError> {
5750                 self.do_accept_inbound_channel(temporary_channel_id, counterparty_node_id, false, user_channel_id)
5751         }
5752
5753         /// Accepts a request to open a channel after a [`events::Event::OpenChannelRequest`], treating
5754         /// it as confirmed immediately.
5755         ///
5756         /// The `user_channel_id` parameter will be provided back in
5757         /// [`Event::ChannelClosed::user_channel_id`] to allow tracking of which events correspond
5758         /// with which `accept_inbound_channel`/`accept_inbound_channel_from_trusted_peer_0conf` call.
5759         ///
5760         /// Unlike [`ChannelManager::accept_inbound_channel`], this method accepts the incoming channel
5761         /// and (if the counterparty agrees), enables forwarding of payments immediately.
5762         ///
5763         /// This fully trusts that the counterparty has honestly and correctly constructed the funding
5764         /// transaction and blindly assumes that it will eventually confirm.
5765         ///
5766         /// If it does not confirm before we decide to close the channel, or if the funding transaction
5767         /// does not pay to the correct script the correct amount, *you will lose funds*.
5768         ///
5769         /// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
5770         /// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
5771         pub fn accept_inbound_channel_from_trusted_peer_0conf(&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, user_channel_id: u128) -> Result<(), APIError> {
5772                 self.do_accept_inbound_channel(temporary_channel_id, counterparty_node_id, true, user_channel_id)
5773         }
5774
5775         fn do_accept_inbound_channel(&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, accept_0conf: bool, user_channel_id: u128) -> Result<(), APIError> {
5776                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
5777
5778                 let peers_without_funded_channels =
5779                         self.peers_without_funded_channels(|peer| { peer.total_channel_count() > 0 });
5780                 let per_peer_state = self.per_peer_state.read().unwrap();
5781                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
5782                         .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
5783                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
5784                 let peer_state = &mut *peer_state_lock;
5785                 let is_only_peer_channel = peer_state.total_channel_count() == 1;
5786
5787                 // Find (and remove) the channel in the unaccepted table. If it's not there, something weird is
5788                 // happening and return an error. N.B. that we create channel with an outbound SCID of zero so
5789                 // that we can delay allocating the SCID until after we're sure that the checks below will
5790                 // succeed.
5791                 let mut channel = match peer_state.inbound_channel_request_by_id.remove(temporary_channel_id) {
5792                         Some(unaccepted_channel) => {
5793                                 let best_block_height = self.best_block.read().unwrap().height();
5794                                 InboundV1Channel::new(&self.fee_estimator, &self.entropy_source, &self.signer_provider,
5795                                         counterparty_node_id.clone(), &self.channel_type_features(), &peer_state.latest_features,
5796                                         &unaccepted_channel.open_channel_msg, user_channel_id, &self.default_configuration, best_block_height,
5797                                         &self.logger, accept_0conf).map_err(|e| APIError::ChannelUnavailable { err: e.to_string() })
5798                         }
5799                         _ => Err(APIError::APIMisuseError { err: "No such channel awaiting to be accepted.".to_owned() })
5800                 }?;
5801
5802                 if accept_0conf {
5803                         // This should have been correctly configured by the call to InboundV1Channel::new.
5804                         debug_assert!(channel.context.minimum_depth().unwrap() == 0);
5805                 } else if channel.context.get_channel_type().requires_zero_conf() {
5806                         let send_msg_err_event = events::MessageSendEvent::HandleError {
5807                                 node_id: channel.context.get_counterparty_node_id(),
5808                                 action: msgs::ErrorAction::SendErrorMessage{
5809                                         msg: msgs::ErrorMessage { channel_id: temporary_channel_id.clone(), data: "No zero confirmation channels accepted".to_owned(), }
5810                                 }
5811                         };
5812                         peer_state.pending_msg_events.push(send_msg_err_event);
5813                         return Err(APIError::APIMisuseError { err: "Please use accept_inbound_channel_from_trusted_peer_0conf to accept channels with zero confirmations.".to_owned() });
5814                 } else {
5815                         // If this peer already has some channels, a new channel won't increase our number of peers
5816                         // with unfunded channels, so as long as we aren't over the maximum number of unfunded
5817                         // channels per-peer we can accept channels from a peer with existing ones.
5818                         if is_only_peer_channel && peers_without_funded_channels >= MAX_UNFUNDED_CHANNEL_PEERS {
5819                                 let send_msg_err_event = events::MessageSendEvent::HandleError {
5820                                         node_id: channel.context.get_counterparty_node_id(),
5821                                         action: msgs::ErrorAction::SendErrorMessage{
5822                                                 msg: msgs::ErrorMessage { channel_id: temporary_channel_id.clone(), data: "Have too many peers with unfunded channels, not accepting new ones".to_owned(), }
5823                                         }
5824                                 };
5825                                 peer_state.pending_msg_events.push(send_msg_err_event);
5826                                 return Err(APIError::APIMisuseError { err: "Too many peers with unfunded channels, refusing to accept new ones".to_owned() });
5827                         }
5828                 }
5829
5830                 // Now that we know we have a channel, assign an outbound SCID alias.
5831                 let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
5832                 channel.context.set_outbound_scid_alias(outbound_scid_alias);
5833
5834                 peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
5835                         node_id: channel.context.get_counterparty_node_id(),
5836                         msg: channel.accept_inbound_channel(),
5837                 });
5838
5839                 peer_state.channel_by_id.insert(temporary_channel_id.clone(), ChannelPhase::UnfundedInboundV1(channel));
5840
5841                 Ok(())
5842         }
5843
5844         /// Gets the number of peers which match the given filter and do not have any funded, outbound,
5845         /// or 0-conf channels.
5846         ///
5847         /// The filter is called for each peer and provided with the number of unfunded, inbound, and
5848         /// non-0-conf channels we have with the peer.
5849         fn peers_without_funded_channels<Filter>(&self, maybe_count_peer: Filter) -> usize
5850         where Filter: Fn(&PeerState<SP>) -> bool {
5851                 let mut peers_without_funded_channels = 0;
5852                 let best_block_height = self.best_block.read().unwrap().height();
5853                 {
5854                         let peer_state_lock = self.per_peer_state.read().unwrap();
5855                         for (_, peer_mtx) in peer_state_lock.iter() {
5856                                 let peer = peer_mtx.lock().unwrap();
5857                                 if !maybe_count_peer(&*peer) { continue; }
5858                                 let num_unfunded_channels = Self::unfunded_channel_count(&peer, best_block_height);
5859                                 if num_unfunded_channels == peer.total_channel_count() {
5860                                         peers_without_funded_channels += 1;
5861                                 }
5862                         }
5863                 }
5864                 return peers_without_funded_channels;
5865         }
5866
5867         fn unfunded_channel_count(
5868                 peer: &PeerState<SP>, best_block_height: u32
5869         ) -> usize {
5870                 let mut num_unfunded_channels = 0;
5871                 for (_, phase) in peer.channel_by_id.iter() {
5872                         match phase {
5873                                 ChannelPhase::Funded(chan) => {
5874                                         // This covers non-zero-conf inbound `Channel`s that we are currently monitoring, but those
5875                                         // which have not yet had any confirmations on-chain.
5876                                         if !chan.context.is_outbound() && chan.context.minimum_depth().unwrap_or(1) != 0 &&
5877                                                 chan.context.get_funding_tx_confirmations(best_block_height) == 0
5878                                         {
5879                                                 num_unfunded_channels += 1;
5880                                         }
5881                                 },
5882                                 ChannelPhase::UnfundedInboundV1(chan) => {
5883                                         if chan.context.minimum_depth().unwrap_or(1) != 0 {
5884                                                 num_unfunded_channels += 1;
5885                                         }
5886                                 },
5887                                 ChannelPhase::UnfundedOutboundV1(_) => {
5888                                         // Outbound channels don't contribute to the unfunded count in the DoS context.
5889                                         continue;
5890                                 }
5891                         }
5892                 }
5893                 num_unfunded_channels + peer.inbound_channel_request_by_id.len()
5894         }
5895
5896         fn internal_open_channel(&self, counterparty_node_id: &PublicKey, msg: &msgs::OpenChannel) -> Result<(), MsgHandleErrInternal> {
5897                 // Note that the ChannelManager is NOT re-persisted on disk after this, so any changes are
5898                 // likely to be lost on restart!
5899                 if msg.chain_hash != self.chain_hash {
5900                         return Err(MsgHandleErrInternal::send_err_msg_no_close("Unknown genesis block hash".to_owned(), msg.temporary_channel_id.clone()));
5901                 }
5902
5903                 if !self.default_configuration.accept_inbound_channels {
5904                         return Err(MsgHandleErrInternal::send_err_msg_no_close("No inbound channels accepted".to_owned(), msg.temporary_channel_id.clone()));
5905                 }
5906
5907                 // Get the number of peers with channels, but without funded ones. We don't care too much
5908                 // about peers that never open a channel, so we filter by peers that have at least one
5909                 // channel, and then limit the number of those with unfunded channels.
5910                 let channeled_peers_without_funding =
5911                         self.peers_without_funded_channels(|node| node.total_channel_count() > 0);
5912
5913                 let per_peer_state = self.per_peer_state.read().unwrap();
5914                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
5915                     .ok_or_else(|| {
5916                                 debug_assert!(false);
5917                                 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.clone())
5918                         })?;
5919                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
5920                 let peer_state = &mut *peer_state_lock;
5921
5922                 // If this peer already has some channels, a new channel won't increase our number of peers
5923                 // with unfunded channels, so as long as we aren't over the maximum number of unfunded
5924                 // channels per-peer we can accept channels from a peer with existing ones.
5925                 if peer_state.total_channel_count() == 0 &&
5926                         channeled_peers_without_funding >= MAX_UNFUNDED_CHANNEL_PEERS &&
5927                         !self.default_configuration.manually_accept_inbound_channels
5928                 {
5929                         return Err(MsgHandleErrInternal::send_err_msg_no_close(
5930                                 "Have too many peers with unfunded channels, not accepting new ones".to_owned(),
5931                                 msg.temporary_channel_id.clone()));
5932                 }
5933
5934                 let best_block_height = self.best_block.read().unwrap().height();
5935                 if Self::unfunded_channel_count(peer_state, best_block_height) >= MAX_UNFUNDED_CHANS_PER_PEER {
5936                         return Err(MsgHandleErrInternal::send_err_msg_no_close(
5937                                 format!("Refusing more than {} unfunded channels.", MAX_UNFUNDED_CHANS_PER_PEER),
5938                                 msg.temporary_channel_id.clone()));
5939                 }
5940
5941                 let channel_id = msg.temporary_channel_id;
5942                 let channel_exists = peer_state.has_channel(&channel_id);
5943                 if channel_exists {
5944                         return Err(MsgHandleErrInternal::send_err_msg_no_close("temporary_channel_id collision for the same peer!".to_owned(), msg.temporary_channel_id.clone()));
5945                 }
5946
5947                 // If we're doing manual acceptance checks on the channel, then defer creation until we're sure we want to accept.
5948                 if self.default_configuration.manually_accept_inbound_channels {
5949                         let mut pending_events = self.pending_events.lock().unwrap();
5950                         pending_events.push_back((events::Event::OpenChannelRequest {
5951                                 temporary_channel_id: msg.temporary_channel_id.clone(),
5952                                 counterparty_node_id: counterparty_node_id.clone(),
5953                                 funding_satoshis: msg.funding_satoshis,
5954                                 push_msat: msg.push_msat,
5955                                 channel_type: msg.channel_type.clone().unwrap(),
5956                         }, None));
5957                         peer_state.inbound_channel_request_by_id.insert(channel_id, InboundChannelRequest {
5958                                 open_channel_msg: msg.clone(),
5959                                 ticks_remaining: UNACCEPTED_INBOUND_CHANNEL_AGE_LIMIT_TICKS,
5960                         });
5961                         return Ok(());
5962                 }
5963
5964                 // Otherwise create the channel right now.
5965                 let mut random_bytes = [0u8; 16];
5966                 random_bytes.copy_from_slice(&self.entropy_source.get_secure_random_bytes()[..16]);
5967                 let user_channel_id = u128::from_be_bytes(random_bytes);
5968                 let mut channel = match InboundV1Channel::new(&self.fee_estimator, &self.entropy_source, &self.signer_provider,
5969                         counterparty_node_id.clone(), &self.channel_type_features(), &peer_state.latest_features, msg, user_channel_id,
5970                         &self.default_configuration, best_block_height, &self.logger, /*is_0conf=*/false)
5971                 {
5972                         Err(e) => {
5973                                 return Err(MsgHandleErrInternal::from_chan_no_close(e, msg.temporary_channel_id));
5974                         },
5975                         Ok(res) => res
5976                 };
5977
5978                 let channel_type = channel.context.get_channel_type();
5979                 if channel_type.requires_zero_conf() {
5980                         return Err(MsgHandleErrInternal::send_err_msg_no_close("No zero confirmation channels accepted".to_owned(), msg.temporary_channel_id.clone()));
5981                 }
5982                 if channel_type.requires_anchors_zero_fee_htlc_tx() {
5983                         return Err(MsgHandleErrInternal::send_err_msg_no_close("No channels with anchor outputs accepted".to_owned(), msg.temporary_channel_id.clone()));
5984                 }
5985
5986                 let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
5987                 channel.context.set_outbound_scid_alias(outbound_scid_alias);
5988
5989                 peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
5990                         node_id: counterparty_node_id.clone(),
5991                         msg: channel.accept_inbound_channel(),
5992                 });
5993                 peer_state.channel_by_id.insert(channel_id, ChannelPhase::UnfundedInboundV1(channel));
5994                 Ok(())
5995         }
5996
5997         fn internal_accept_channel(&self, counterparty_node_id: &PublicKey, msg: &msgs::AcceptChannel) -> Result<(), MsgHandleErrInternal> {
5998                 // Note that the ChannelManager is NOT re-persisted on disk after this, so any changes are
5999                 // likely to be lost on restart!
6000                 let (value, output_script, user_id) = {
6001                         let per_peer_state = self.per_peer_state.read().unwrap();
6002                         let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6003                                 .ok_or_else(|| {
6004                                         debug_assert!(false);
6005                                         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)
6006                                 })?;
6007                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6008                         let peer_state = &mut *peer_state_lock;
6009                         match peer_state.channel_by_id.entry(msg.temporary_channel_id) {
6010                                 hash_map::Entry::Occupied(mut phase) => {
6011                                         match phase.get_mut() {
6012                                                 ChannelPhase::UnfundedOutboundV1(chan) => {
6013                                                         try_chan_phase_entry!(self, chan.accept_channel(&msg, &self.default_configuration.channel_handshake_limits, &peer_state.latest_features), phase);
6014                                                         (chan.context.get_value_satoshis(), chan.context.get_funding_redeemscript().to_v0_p2wsh(), chan.context.get_user_id())
6015                                                 },
6016                                                 _ => {
6017                                                         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.temporary_channel_id));
6018                                                 }
6019                                         }
6020                                 },
6021                                 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.temporary_channel_id))
6022                         }
6023                 };
6024                 let mut pending_events = self.pending_events.lock().unwrap();
6025                 pending_events.push_back((events::Event::FundingGenerationReady {
6026                         temporary_channel_id: msg.temporary_channel_id,
6027                         counterparty_node_id: *counterparty_node_id,
6028                         channel_value_satoshis: value,
6029                         output_script,
6030                         user_channel_id: user_id,
6031                 }, None));
6032                 Ok(())
6033         }
6034
6035         fn internal_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
6036                 let best_block = *self.best_block.read().unwrap();
6037
6038                 let per_peer_state = self.per_peer_state.read().unwrap();
6039                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6040                         .ok_or_else(|| {
6041                                 debug_assert!(false);
6042                                 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)
6043                         })?;
6044
6045                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6046                 let peer_state = &mut *peer_state_lock;
6047                 let (chan, funding_msg, monitor) =
6048                         match peer_state.channel_by_id.remove(&msg.temporary_channel_id) {
6049                                 Some(ChannelPhase::UnfundedInboundV1(inbound_chan)) => {
6050                                         match inbound_chan.funding_created(msg, best_block, &self.signer_provider, &self.logger) {
6051                                                 Ok(res) => res,
6052                                                 Err((mut inbound_chan, err)) => {
6053                                                         // We've already removed this inbound channel from the map in `PeerState`
6054                                                         // above so at this point we just need to clean up any lingering entries
6055                                                         // concerning this channel as it is safe to do so.
6056                                                         update_maps_on_chan_removal!(self, &inbound_chan.context);
6057                                                         let user_id = inbound_chan.context.get_user_id();
6058                                                         let shutdown_res = inbound_chan.context.force_shutdown(false);
6059                                                         return Err(MsgHandleErrInternal::from_finish_shutdown(format!("{}", err),
6060                                                                 msg.temporary_channel_id, user_id, shutdown_res, None, inbound_chan.context.get_value_satoshis()));
6061                                                 },
6062                                         }
6063                                 },
6064                                 Some(ChannelPhase::Funded(_)) | Some(ChannelPhase::UnfundedOutboundV1(_)) => {
6065                                         return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got an unexpected funding_created message from peer with counterparty_node_id {}", counterparty_node_id), msg.temporary_channel_id));
6066                                 },
6067                                 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))
6068                         };
6069
6070                 match peer_state.channel_by_id.entry(funding_msg.channel_id) {
6071                         hash_map::Entry::Occupied(_) => {
6072                                 Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id".to_owned(), funding_msg.channel_id))
6073                         },
6074                         hash_map::Entry::Vacant(e) => {
6075                                 let mut id_to_peer_lock = self.id_to_peer.lock().unwrap();
6076                                 match id_to_peer_lock.entry(chan.context.channel_id()) {
6077                                         hash_map::Entry::Occupied(_) => {
6078                                                 return Err(MsgHandleErrInternal::send_err_msg_no_close(
6079                                                         "The funding_created message had the same funding_txid as an existing channel - funding is not possible".to_owned(),
6080                                                         funding_msg.channel_id))
6081                                         },
6082                                         hash_map::Entry::Vacant(i_e) => {
6083                                                 let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
6084                                                 if let Ok(persist_state) = monitor_res {
6085                                                         i_e.insert(chan.context.get_counterparty_node_id());
6086                                                         mem::drop(id_to_peer_lock);
6087
6088                                                         // There's no problem signing a counterparty's funding transaction if our monitor
6089                                                         // hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
6090                                                         // accepted payment from yet. We do, however, need to wait to send our channel_ready
6091                                                         // until we have persisted our monitor.
6092                                                         peer_state.pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
6093                                                                 node_id: counterparty_node_id.clone(),
6094                                                                 msg: funding_msg,
6095                                                         });
6096
6097                                                         if let ChannelPhase::Funded(chan) = e.insert(ChannelPhase::Funded(chan)) {
6098                                                                 handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
6099                                                                         per_peer_state, chan, INITIAL_MONITOR);
6100                                                         } else {
6101                                                                 unreachable!("This must be a funded channel as we just inserted it.");
6102                                                         }
6103                                                         Ok(())
6104                                                 } else {
6105                                                         log_error!(self.logger, "Persisting initial ChannelMonitor failed, implying the funding outpoint was duplicated");
6106                                                         return Err(MsgHandleErrInternal::send_err_msg_no_close(
6107                                                                 "The funding_created message had the same funding_txid as an existing channel - funding is not possible".to_owned(),
6108                                                                 funding_msg.channel_id));
6109                                                 }
6110                                         }
6111                                 }
6112                         }
6113                 }
6114         }
6115
6116         fn internal_funding_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), MsgHandleErrInternal> {
6117                 let best_block = *self.best_block.read().unwrap();
6118                 let per_peer_state = self.per_peer_state.read().unwrap();
6119                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6120                         .ok_or_else(|| {
6121                                 debug_assert!(false);
6122                                 MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
6123                         })?;
6124
6125                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6126                 let peer_state = &mut *peer_state_lock;
6127                 match peer_state.channel_by_id.entry(msg.channel_id) {
6128                         hash_map::Entry::Occupied(mut chan_phase_entry) => {
6129                                 match chan_phase_entry.get_mut() {
6130                                         ChannelPhase::Funded(ref mut chan) => {
6131                                                 let monitor = try_chan_phase_entry!(self,
6132                                                         chan.funding_signed(&msg, best_block, &self.signer_provider, &self.logger), chan_phase_entry);
6133                                                 if let Ok(persist_status) = self.chain_monitor.watch_channel(chan.context.get_funding_txo().unwrap(), monitor) {
6134                                                         handle_new_monitor_update!(self, persist_status, peer_state_lock, peer_state, per_peer_state, chan, INITIAL_MONITOR);
6135                                                         Ok(())
6136                                                 } else {
6137                                                         try_chan_phase_entry!(self, Err(ChannelError::Close("Channel funding outpoint was a duplicate".to_owned())), chan_phase_entry)
6138                                                 }
6139                                         },
6140                                         _ => {
6141                                                 return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id));
6142                                         },
6143                                 }
6144                         },
6145                         hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
6146                 }
6147         }
6148
6149         fn internal_channel_ready(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReady) -> Result<(), MsgHandleErrInternal> {
6150                 // Note that the ChannelManager is NOT re-persisted on disk after this (unless we error
6151                 // closing a channel), so any changes are likely to be lost on restart!
6152                 let per_peer_state = self.per_peer_state.read().unwrap();
6153                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6154                         .ok_or_else(|| {
6155                                 debug_assert!(false);
6156                                 MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
6157                         })?;
6158                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6159                 let peer_state = &mut *peer_state_lock;
6160                 match peer_state.channel_by_id.entry(msg.channel_id) {
6161                         hash_map::Entry::Occupied(mut chan_phase_entry) => {
6162                                 if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
6163                                         let announcement_sigs_opt = try_chan_phase_entry!(self, chan.channel_ready(&msg, &self.node_signer,
6164                                                 self.chain_hash, &self.default_configuration, &self.best_block.read().unwrap(), &self.logger), chan_phase_entry);
6165                                         if let Some(announcement_sigs) = announcement_sigs_opt {
6166                                                 log_trace!(self.logger, "Sending announcement_signatures for channel {}", chan.context.channel_id());
6167                                                 peer_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
6168                                                         node_id: counterparty_node_id.clone(),
6169                                                         msg: announcement_sigs,
6170                                                 });
6171                                         } else if chan.context.is_usable() {
6172                                                 // If we're sending an announcement_signatures, we'll send the (public)
6173                                                 // channel_update after sending a channel_announcement when we receive our
6174                                                 // counterparty's announcement_signatures. Thus, we only bother to send a
6175                                                 // channel_update here if the channel is not public, i.e. we're not sending an
6176                                                 // announcement_signatures.
6177                                                 log_trace!(self.logger, "Sending private initial channel_update for our counterparty on channel {}", chan.context.channel_id());
6178                                                 if let Ok(msg) = self.get_channel_update_for_unicast(chan) {
6179                                                         peer_state.pending_msg_events.push(events::MessageSendEvent::SendChannelUpdate {
6180                                                                 node_id: counterparty_node_id.clone(),
6181                                                                 msg,
6182                                                         });
6183                                                 }
6184                                         }
6185
6186                                         {
6187                                                 let mut pending_events = self.pending_events.lock().unwrap();
6188                                                 emit_channel_ready_event!(pending_events, chan);
6189                                         }
6190
6191                                         Ok(())
6192                                 } else {
6193                                         try_chan_phase_entry!(self, Err(ChannelError::Close(
6194                                                 "Got a channel_ready message for an unfunded channel!".into())), chan_phase_entry)
6195                                 }
6196                         },
6197                         hash_map::Entry::Vacant(_) => {
6198                                 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))
6199                         }
6200                 }
6201         }
6202
6203         fn internal_shutdown(&self, counterparty_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(), MsgHandleErrInternal> {
6204                 let mut dropped_htlcs: Vec<(HTLCSource, PaymentHash)> = Vec::new();
6205                 let mut finish_shutdown = None;
6206                 {
6207                         let per_peer_state = self.per_peer_state.read().unwrap();
6208                         let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6209                                 .ok_or_else(|| {
6210                                         debug_assert!(false);
6211                                         MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
6212                                 })?;
6213                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6214                         let peer_state = &mut *peer_state_lock;
6215                         if let hash_map::Entry::Occupied(mut chan_phase_entry) = peer_state.channel_by_id.entry(msg.channel_id.clone()) {
6216                                 let phase = chan_phase_entry.get_mut();
6217                                 match phase {
6218                                         ChannelPhase::Funded(chan) => {
6219                                                 if !chan.received_shutdown() {
6220                                                         log_info!(self.logger, "Received a shutdown message from our counterparty for channel {}{}.",
6221                                                                 msg.channel_id,
6222                                                                 if chan.sent_shutdown() { " after we initiated shutdown" } else { "" });
6223                                                 }
6224
6225                                                 let funding_txo_opt = chan.context.get_funding_txo();
6226                                                 let (shutdown, monitor_update_opt, htlcs) = try_chan_phase_entry!(self,
6227                                                         chan.shutdown(&self.signer_provider, &peer_state.latest_features, &msg), chan_phase_entry);
6228                                                 dropped_htlcs = htlcs;
6229
6230                                                 if let Some(msg) = shutdown {
6231                                                         // We can send the `shutdown` message before updating the `ChannelMonitor`
6232                                                         // here as we don't need the monitor update to complete until we send a
6233                                                         // `shutdown_signed`, which we'll delay if we're pending a monitor update.
6234                                                         peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
6235                                                                 node_id: *counterparty_node_id,
6236                                                                 msg,
6237                                                         });
6238                                                 }
6239                                                 // Update the monitor with the shutdown script if necessary.
6240                                                 if let Some(monitor_update) = monitor_update_opt {
6241                                                         handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
6242                                                                 peer_state_lock, peer_state, per_peer_state, chan);
6243                                                 }
6244                                         },
6245                                         ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::UnfundedOutboundV1(_) => {
6246                                                 let context = phase.context_mut();
6247                                                 log_error!(self.logger, "Immediately closing unfunded channel {} as peer asked to cooperatively shut it down (which is unnecessary)", &msg.channel_id);
6248                                                 self.issue_channel_close_events(&context, ClosureReason::CounterpartyCoopClosedUnfundedChannel);
6249                                                 let mut chan = remove_channel_phase!(self, chan_phase_entry);
6250                                                 finish_shutdown = Some(chan.context_mut().force_shutdown(false));
6251                                         },
6252                                 }
6253                         } else {
6254                                 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))
6255                         }
6256                 }
6257                 for htlc_source in dropped_htlcs.drain(..) {
6258                         let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id: msg.channel_id };
6259                         let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
6260                         self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
6261                 }
6262                 if let Some(shutdown_res) = finish_shutdown {
6263                         self.finish_close_channel(shutdown_res);
6264                 }
6265
6266                 Ok(())
6267         }
6268
6269         fn internal_closing_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result<(), MsgHandleErrInternal> {
6270                 let per_peer_state = self.per_peer_state.read().unwrap();
6271                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6272                         .ok_or_else(|| {
6273                                 debug_assert!(false);
6274                                 MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
6275                         })?;
6276                 let (tx, chan_option, shutdown_result) = {
6277                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6278                         let peer_state = &mut *peer_state_lock;
6279                         match peer_state.channel_by_id.entry(msg.channel_id.clone()) {
6280                                 hash_map::Entry::Occupied(mut chan_phase_entry) => {
6281                                         if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
6282                                                 let (closing_signed, tx, shutdown_result) = try_chan_phase_entry!(self, chan.closing_signed(&self.fee_estimator, &msg), chan_phase_entry);
6283                                                 debug_assert_eq!(shutdown_result.is_some(), chan.is_shutdown());
6284                                                 if let Some(msg) = closing_signed {
6285                                                         peer_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
6286                                                                 node_id: counterparty_node_id.clone(),
6287                                                                 msg,
6288                                                         });
6289                                                 }
6290                                                 if tx.is_some() {
6291                                                         // We're done with this channel, we've got a signed closing transaction and
6292                                                         // will send the closing_signed back to the remote peer upon return. This
6293                                                         // also implies there are no pending HTLCs left on the channel, so we can
6294                                                         // fully delete it from tracking (the channel monitor is still around to
6295                                                         // watch for old state broadcasts)!
6296                                                         (tx, Some(remove_channel_phase!(self, chan_phase_entry)), shutdown_result)
6297                                                 } else { (tx, None, shutdown_result) }
6298                                         } else {
6299                                                 return try_chan_phase_entry!(self, Err(ChannelError::Close(
6300                                                         "Got a closing_signed message for an unfunded channel!".into())), chan_phase_entry);
6301                                         }
6302                                 },
6303                                 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))
6304                         }
6305                 };
6306                 if let Some(broadcast_tx) = tx {
6307                         log_info!(self.logger, "Broadcasting {}", log_tx!(broadcast_tx));
6308                         self.tx_broadcaster.broadcast_transactions(&[&broadcast_tx]);
6309                 }
6310                 if let Some(ChannelPhase::Funded(chan)) = chan_option {
6311                         if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
6312                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6313                                 let peer_state = &mut *peer_state_lock;
6314                                 peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
6315                                         msg: update
6316                                 });
6317                         }
6318                         self.issue_channel_close_events(&chan.context, ClosureReason::CooperativeClosure);
6319                 }
6320                 mem::drop(per_peer_state);
6321                 if let Some(shutdown_result) = shutdown_result {
6322                         self.finish_close_channel(shutdown_result);
6323                 }
6324                 Ok(())
6325         }
6326
6327         fn internal_update_add_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) -> Result<(), MsgHandleErrInternal> {
6328                 //TODO: BOLT 4 points out a specific attack where a peer may re-send an onion packet and
6329                 //determine the state of the payment based on our response/if we forward anything/the time
6330                 //we take to respond. We should take care to avoid allowing such an attack.
6331                 //
6332                 //TODO: There exists a further attack where a node may garble the onion data, forward it to
6333                 //us repeatedly garbled in different ways, and compare our error messages, which are
6334                 //encrypted with the same key. It's not immediately obvious how to usefully exploit that,
6335                 //but we should prevent it anyway.
6336
6337                 // Note that the ChannelManager is NOT re-persisted on disk after this (unless we error
6338                 // closing a channel), so any changes are likely to be lost on restart!
6339
6340                 let decoded_hop_res = self.decode_update_add_htlc_onion(msg);
6341                 let per_peer_state = self.per_peer_state.read().unwrap();
6342                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6343                         .ok_or_else(|| {
6344                                 debug_assert!(false);
6345                                 MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
6346                         })?;
6347                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6348                 let peer_state = &mut *peer_state_lock;
6349                 match peer_state.channel_by_id.entry(msg.channel_id) {
6350                         hash_map::Entry::Occupied(mut chan_phase_entry) => {
6351                                 if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
6352                                         let pending_forward_info = match decoded_hop_res {
6353                                                 Ok((next_hop, shared_secret, next_packet_pk_opt)) =>
6354                                                         self.construct_pending_htlc_status(msg, shared_secret, next_hop,
6355                                                                 chan.context.config().accept_underpaying_htlcs, next_packet_pk_opt),
6356                                                 Err(e) => PendingHTLCStatus::Fail(e)
6357                                         };
6358                                         let create_pending_htlc_status = |chan: &Channel<SP>, pending_forward_info: PendingHTLCStatus, error_code: u16| {
6359                                                 // If the update_add is completely bogus, the call will Err and we will close,
6360                                                 // but if we've sent a shutdown and they haven't acknowledged it yet, we just
6361                                                 // want to reject the new HTLC and fail it backwards instead of forwarding.
6362                                                 match pending_forward_info {
6363                                                         PendingHTLCStatus::Forward(PendingHTLCInfo { ref incoming_shared_secret, .. }) => {
6364                                                                 let reason = if (error_code & 0x1000) != 0 {
6365                                                                         let (real_code, error_data) = self.get_htlc_inbound_temp_fail_err_and_data(error_code, chan);
6366                                                                         HTLCFailReason::reason(real_code, error_data)
6367                                                                 } else {
6368                                                                         HTLCFailReason::from_failure_code(error_code)
6369                                                                 }.get_encrypted_failure_packet(incoming_shared_secret, &None);
6370                                                                 let msg = msgs::UpdateFailHTLC {
6371                                                                         channel_id: msg.channel_id,
6372                                                                         htlc_id: msg.htlc_id,
6373                                                                         reason
6374                                                                 };
6375                                                                 PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msg))
6376                                                         },
6377                                                         _ => pending_forward_info
6378                                                 }
6379                                         };
6380                                         try_chan_phase_entry!(self, chan.update_add_htlc(&msg, pending_forward_info, create_pending_htlc_status, &self.fee_estimator, &self.logger), chan_phase_entry);
6381                                 } else {
6382                                         return try_chan_phase_entry!(self, Err(ChannelError::Close(
6383                                                 "Got an update_add_htlc message for an unfunded channel!".into())), chan_phase_entry);
6384                                 }
6385                         },
6386                         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))
6387                 }
6388                 Ok(())
6389         }
6390
6391         fn internal_update_fulfill_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) -> Result<(), MsgHandleErrInternal> {
6392                 let funding_txo;
6393                 let (htlc_source, forwarded_htlc_value) = {
6394                         let per_peer_state = self.per_peer_state.read().unwrap();
6395                         let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6396                                 .ok_or_else(|| {
6397                                         debug_assert!(false);
6398                                         MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
6399                                 })?;
6400                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6401                         let peer_state = &mut *peer_state_lock;
6402                         match peer_state.channel_by_id.entry(msg.channel_id) {
6403                                 hash_map::Entry::Occupied(mut chan_phase_entry) => {
6404                                         if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
6405                                                 let res = try_chan_phase_entry!(self, chan.update_fulfill_htlc(&msg), chan_phase_entry);
6406                                                 if let HTLCSource::PreviousHopData(prev_hop) = &res.0 {
6407                                                         peer_state.actions_blocking_raa_monitor_updates.entry(msg.channel_id)
6408                                                                 .or_insert_with(Vec::new)
6409                                                                 .push(RAAMonitorUpdateBlockingAction::from_prev_hop_data(&prev_hop));
6410                                                 }
6411                                                 // Note that we do not need to push an `actions_blocking_raa_monitor_updates`
6412                                                 // entry here, even though we *do* need to block the next RAA monitor update.
6413                                                 // We do this instead in the `claim_funds_internal` by attaching a
6414                                                 // `ReleaseRAAChannelMonitorUpdate` action to the event generated when the
6415                                                 // outbound HTLC is claimed. This is guaranteed to all complete before we
6416                                                 // process the RAA as messages are processed from single peers serially.
6417                                                 funding_txo = chan.context.get_funding_txo().expect("We won't accept a fulfill until funded");
6418                                                 res
6419                                         } else {
6420                                                 return try_chan_phase_entry!(self, Err(ChannelError::Close(
6421                                                         "Got an update_fulfill_htlc message for an unfunded channel!".into())), chan_phase_entry);
6422                                         }
6423                                 },
6424                                 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))
6425                         }
6426                 };
6427                 self.claim_funds_internal(htlc_source, msg.payment_preimage.clone(), Some(forwarded_htlc_value), false, Some(*counterparty_node_id), funding_txo);
6428                 Ok(())
6429         }
6430
6431         fn internal_update_fail_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) -> Result<(), MsgHandleErrInternal> {
6432                 // Note that the ChannelManager is NOT re-persisted on disk after this (unless we error
6433                 // closing a channel), so any changes are likely to be lost on restart!
6434                 let per_peer_state = self.per_peer_state.read().unwrap();
6435                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6436                         .ok_or_else(|| {
6437                                 debug_assert!(false);
6438                                 MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
6439                         })?;
6440                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6441                 let peer_state = &mut *peer_state_lock;
6442                 match peer_state.channel_by_id.entry(msg.channel_id) {
6443                         hash_map::Entry::Occupied(mut chan_phase_entry) => {
6444                                 if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
6445                                         try_chan_phase_entry!(self, chan.update_fail_htlc(&msg, HTLCFailReason::from_msg(msg)), chan_phase_entry);
6446                                 } else {
6447                                         return try_chan_phase_entry!(self, Err(ChannelError::Close(
6448                                                 "Got an update_fail_htlc message for an unfunded channel!".into())), chan_phase_entry);
6449                                 }
6450                         },
6451                         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))
6452                 }
6453                 Ok(())
6454         }
6455
6456         fn internal_update_fail_malformed_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), MsgHandleErrInternal> {
6457                 // Note that the ChannelManager is NOT re-persisted on disk after this (unless we error
6458                 // closing a channel), so any changes are likely to be lost on restart!
6459                 let per_peer_state = self.per_peer_state.read().unwrap();
6460                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6461                         .ok_or_else(|| {
6462                                 debug_assert!(false);
6463                                 MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
6464                         })?;
6465                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6466                 let peer_state = &mut *peer_state_lock;
6467                 match peer_state.channel_by_id.entry(msg.channel_id) {
6468                         hash_map::Entry::Occupied(mut chan_phase_entry) => {
6469                                 if (msg.failure_code & 0x8000) == 0 {
6470                                         let chan_err: ChannelError = ChannelError::Close("Got update_fail_malformed_htlc with BADONION not set".to_owned());
6471                                         try_chan_phase_entry!(self, Err(chan_err), chan_phase_entry);
6472                                 }
6473                                 if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
6474                                         try_chan_phase_entry!(self, chan.update_fail_malformed_htlc(&msg, HTLCFailReason::reason(msg.failure_code, msg.sha256_of_onion.to_vec())), chan_phase_entry);
6475                                 } else {
6476                                         return try_chan_phase_entry!(self, Err(ChannelError::Close(
6477                                                 "Got an update_fail_malformed_htlc message for an unfunded channel!".into())), chan_phase_entry);
6478                                 }
6479                                 Ok(())
6480                         },
6481                         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))
6482                 }
6483         }
6484
6485         fn internal_commitment_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::CommitmentSigned) -> Result<(), MsgHandleErrInternal> {
6486                 let per_peer_state = self.per_peer_state.read().unwrap();
6487                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6488                         .ok_or_else(|| {
6489                                 debug_assert!(false);
6490                                 MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
6491                         })?;
6492                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6493                 let peer_state = &mut *peer_state_lock;
6494                 match peer_state.channel_by_id.entry(msg.channel_id) {
6495                         hash_map::Entry::Occupied(mut chan_phase_entry) => {
6496                                 if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
6497                                         let funding_txo = chan.context.get_funding_txo();
6498                                         let monitor_update_opt = try_chan_phase_entry!(self, chan.commitment_signed(&msg, &self.logger), chan_phase_entry);
6499                                         if let Some(monitor_update) = monitor_update_opt {
6500                                                 handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update, peer_state_lock,
6501                                                         peer_state, per_peer_state, chan);
6502                                         }
6503                                         Ok(())
6504                                 } else {
6505                                         return try_chan_phase_entry!(self, Err(ChannelError::Close(
6506                                                 "Got a commitment_signed message for an unfunded channel!".into())), chan_phase_entry);
6507                                 }
6508                         },
6509                         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))
6510                 }
6511         }
6512
6513         #[inline]
6514         fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, OutPoint, u128, Vec<(PendingHTLCInfo, u64)>)]) {
6515                 for &mut (prev_short_channel_id, prev_funding_outpoint, prev_user_channel_id, ref mut pending_forwards) in per_source_pending_forwards {
6516                         let mut push_forward_event = false;
6517                         let mut new_intercept_events = VecDeque::new();
6518                         let mut failed_intercept_forwards = Vec::new();
6519                         if !pending_forwards.is_empty() {
6520                                 for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
6521                                         let scid = match forward_info.routing {
6522                                                 PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
6523                                                 PendingHTLCRouting::Receive { .. } => 0,
6524                                                 PendingHTLCRouting::ReceiveKeysend { .. } => 0,
6525                                         };
6526                                         // Pull this now to avoid introducing a lock order with `forward_htlcs`.
6527                                         let is_our_scid = self.short_to_chan_info.read().unwrap().contains_key(&scid);
6528
6529                                         let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
6530                                         let forward_htlcs_empty = forward_htlcs.is_empty();
6531                                         match forward_htlcs.entry(scid) {
6532                                                 hash_map::Entry::Occupied(mut entry) => {
6533                                                         entry.get_mut().push(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
6534                                                                 prev_short_channel_id, prev_funding_outpoint, prev_htlc_id, prev_user_channel_id, forward_info }));
6535                                                 },
6536                                                 hash_map::Entry::Vacant(entry) => {
6537                                                         if !is_our_scid && forward_info.incoming_amt_msat.is_some() &&
6538                                                            fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, scid, &self.chain_hash)
6539                                                         {
6540                                                                 let intercept_id = InterceptId(Sha256::hash(&forward_info.incoming_shared_secret).into_inner());
6541                                                                 let mut pending_intercepts = self.pending_intercepted_htlcs.lock().unwrap();
6542                                                                 match pending_intercepts.entry(intercept_id) {
6543                                                                         hash_map::Entry::Vacant(entry) => {
6544                                                                                 new_intercept_events.push_back((events::Event::HTLCIntercepted {
6545                                                                                         requested_next_hop_scid: scid,
6546                                                                                         payment_hash: forward_info.payment_hash,
6547                                                                                         inbound_amount_msat: forward_info.incoming_amt_msat.unwrap(),
6548                                                                                         expected_outbound_amount_msat: forward_info.outgoing_amt_msat,
6549                                                                                         intercept_id
6550                                                                                 }, None));
6551                                                                                 entry.insert(PendingAddHTLCInfo {
6552                                                                                         prev_short_channel_id, prev_funding_outpoint, prev_htlc_id, prev_user_channel_id, forward_info });
6553                                                                         },
6554                                                                         hash_map::Entry::Occupied(_) => {
6555                                                                                 log_info!(self.logger, "Failed to forward incoming HTLC: detected duplicate intercepted payment over short channel id {}", scid);
6556                                                                                 let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
6557                                                                                         short_channel_id: prev_short_channel_id,
6558                                                                                         user_channel_id: Some(prev_user_channel_id),
6559                                                                                         outpoint: prev_funding_outpoint,
6560                                                                                         htlc_id: prev_htlc_id,
6561                                                                                         incoming_packet_shared_secret: forward_info.incoming_shared_secret,
6562                                                                                         phantom_shared_secret: None,
6563                                                                                 });
6564
6565                                                                                 failed_intercept_forwards.push((htlc_source, forward_info.payment_hash,
6566                                                                                                 HTLCFailReason::from_failure_code(0x4000 | 10),
6567                                                                                                 HTLCDestination::InvalidForward { requested_forward_scid: scid },
6568                                                                                 ));
6569                                                                         }
6570                                                                 }
6571                                                         } else {
6572                                                                 // We don't want to generate a PendingHTLCsForwardable event if only intercepted
6573                                                                 // payments are being processed.
6574                                                                 if forward_htlcs_empty {
6575                                                                         push_forward_event = true;
6576                                                                 }
6577                                                                 entry.insert(vec!(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
6578                                                                         prev_short_channel_id, prev_funding_outpoint, prev_htlc_id, prev_user_channel_id, forward_info })));
6579                                                         }
6580                                                 }
6581                                         }
6582                                 }
6583                         }
6584
6585                         for (htlc_source, payment_hash, failure_reason, destination) in failed_intercept_forwards.drain(..) {
6586                                 self.fail_htlc_backwards_internal(&htlc_source, &payment_hash, &failure_reason, destination);
6587                         }
6588
6589                         if !new_intercept_events.is_empty() {
6590                                 let mut events = self.pending_events.lock().unwrap();
6591                                 events.append(&mut new_intercept_events);
6592                         }
6593                         if push_forward_event { self.push_pending_forwards_ev() }
6594                 }
6595         }
6596
6597         fn push_pending_forwards_ev(&self) {
6598                 let mut pending_events = self.pending_events.lock().unwrap();
6599                 let is_processing_events = self.pending_events_processor.load(Ordering::Acquire);
6600                 let num_forward_events = pending_events.iter().filter(|(ev, _)|
6601                         if let events::Event::PendingHTLCsForwardable { .. } = ev { true } else { false }
6602                 ).count();
6603                 // We only want to push a PendingHTLCsForwardable event if no others are queued. Processing
6604                 // events is done in batches and they are not removed until we're done processing each
6605                 // batch. Since handling a `PendingHTLCsForwardable` event will call back into the
6606                 // `ChannelManager`, we'll still see the original forwarding event not removed. Phantom
6607                 // payments will need an additional forwarding event before being claimed to make them look
6608                 // real by taking more time.
6609                 if (is_processing_events && num_forward_events <= 1) || num_forward_events < 1 {
6610                         pending_events.push_back((Event::PendingHTLCsForwardable {
6611                                 time_forwardable: Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS),
6612                         }, None));
6613                 }
6614         }
6615
6616         /// Checks whether [`ChannelMonitorUpdate`]s generated by the receipt of a remote
6617         /// [`msgs::RevokeAndACK`] should be held for the given channel until some other action
6618         /// completes. Note that this needs to happen in the same [`PeerState`] mutex as any release of
6619         /// the [`ChannelMonitorUpdate`] in question.
6620         fn raa_monitor_updates_held(&self,
6621                 actions_blocking_raa_monitor_updates: &BTreeMap<ChannelId, Vec<RAAMonitorUpdateBlockingAction>>,
6622                 channel_funding_outpoint: OutPoint, counterparty_node_id: PublicKey
6623         ) -> bool {
6624                 actions_blocking_raa_monitor_updates
6625                         .get(&channel_funding_outpoint.to_channel_id()).map(|v| !v.is_empty()).unwrap_or(false)
6626                 || self.pending_events.lock().unwrap().iter().any(|(_, action)| {
6627                         action == &Some(EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
6628                                 channel_funding_outpoint,
6629                                 counterparty_node_id,
6630                         })
6631                 })
6632         }
6633
6634         #[cfg(any(test, feature = "_test_utils"))]
6635         pub(crate) fn test_raa_monitor_updates_held(&self,
6636                 counterparty_node_id: PublicKey, channel_id: ChannelId
6637         ) -> bool {
6638                 let per_peer_state = self.per_peer_state.read().unwrap();
6639                 if let Some(peer_state_mtx) = per_peer_state.get(&counterparty_node_id) {
6640                         let mut peer_state_lck = peer_state_mtx.lock().unwrap();
6641                         let peer_state = &mut *peer_state_lck;
6642
6643                         if let Some(chan) = peer_state.channel_by_id.get(&channel_id) {
6644                                 return self.raa_monitor_updates_held(&peer_state.actions_blocking_raa_monitor_updates,
6645                                         chan.context().get_funding_txo().unwrap(), counterparty_node_id);
6646                         }
6647                 }
6648                 false
6649         }
6650
6651         fn internal_revoke_and_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), MsgHandleErrInternal> {
6652                 let htlcs_to_fail = {
6653                         let per_peer_state = self.per_peer_state.read().unwrap();
6654                         let mut peer_state_lock = per_peer_state.get(counterparty_node_id)
6655                                 .ok_or_else(|| {
6656                                         debug_assert!(false);
6657                                         MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
6658                                 }).map(|mtx| mtx.lock().unwrap())?;
6659                         let peer_state = &mut *peer_state_lock;
6660                         match peer_state.channel_by_id.entry(msg.channel_id) {
6661                                 hash_map::Entry::Occupied(mut chan_phase_entry) => {
6662                                         if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
6663                                                 let funding_txo_opt = chan.context.get_funding_txo();
6664                                                 let mon_update_blocked = if let Some(funding_txo) = funding_txo_opt {
6665                                                         self.raa_monitor_updates_held(
6666                                                                 &peer_state.actions_blocking_raa_monitor_updates, funding_txo,
6667                                                                 *counterparty_node_id)
6668                                                 } else { false };
6669                                                 let (htlcs_to_fail, monitor_update_opt) = try_chan_phase_entry!(self,
6670                                                         chan.revoke_and_ack(&msg, &self.fee_estimator, &self.logger, mon_update_blocked), chan_phase_entry);
6671                                                 if let Some(monitor_update) = monitor_update_opt {
6672                                                         let funding_txo = funding_txo_opt
6673                                                                 .expect("Funding outpoint must have been set for RAA handling to succeed");
6674                                                         handle_new_monitor_update!(self, funding_txo, monitor_update,
6675                                                                 peer_state_lock, peer_state, per_peer_state, chan);
6676                                                 }
6677                                                 htlcs_to_fail
6678                                         } else {
6679                                                 return try_chan_phase_entry!(self, Err(ChannelError::Close(
6680                                                         "Got a revoke_and_ack message for an unfunded channel!".into())), chan_phase_entry);
6681                                         }
6682                                 },
6683                                 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))
6684                         }
6685                 };
6686                 self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id, counterparty_node_id);
6687                 Ok(())
6688         }
6689
6690         fn internal_update_fee(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFee) -> Result<(), MsgHandleErrInternal> {
6691                 let per_peer_state = self.per_peer_state.read().unwrap();
6692                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6693                         .ok_or_else(|| {
6694                                 debug_assert!(false);
6695                                 MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
6696                         })?;
6697                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6698                 let peer_state = &mut *peer_state_lock;
6699                 match peer_state.channel_by_id.entry(msg.channel_id) {
6700                         hash_map::Entry::Occupied(mut chan_phase_entry) => {
6701                                 if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
6702                                         try_chan_phase_entry!(self, chan.update_fee(&self.fee_estimator, &msg, &self.logger), chan_phase_entry);
6703                                 } else {
6704                                         return try_chan_phase_entry!(self, Err(ChannelError::Close(
6705                                                 "Got an update_fee message for an unfunded channel!".into())), chan_phase_entry);
6706                                 }
6707                         },
6708                         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))
6709                 }
6710                 Ok(())
6711         }
6712
6713         fn internal_announcement_signatures(&self, counterparty_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) -> Result<(), MsgHandleErrInternal> {
6714                 let per_peer_state = self.per_peer_state.read().unwrap();
6715                 let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6716                         .ok_or_else(|| {
6717                                 debug_assert!(false);
6718                                 MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
6719                         })?;
6720                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6721                 let peer_state = &mut *peer_state_lock;
6722                 match peer_state.channel_by_id.entry(msg.channel_id) {
6723                         hash_map::Entry::Occupied(mut chan_phase_entry) => {
6724                                 if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
6725                                         if !chan.context.is_usable() {
6726                                                 return Err(MsgHandleErrInternal::from_no_close(LightningError{err: "Got an announcement_signatures before we were ready for it".to_owned(), action: msgs::ErrorAction::IgnoreError}));
6727                                         }
6728
6729                                         peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelAnnouncement {
6730                                                 msg: try_chan_phase_entry!(self, chan.announcement_signatures(
6731                                                         &self.node_signer, self.chain_hash, self.best_block.read().unwrap().height(),
6732                                                         msg, &self.default_configuration
6733                                                 ), chan_phase_entry),
6734                                                 // Note that announcement_signatures fails if the channel cannot be announced,
6735                                                 // so get_channel_update_for_broadcast will never fail by the time we get here.
6736                                                 update_msg: Some(self.get_channel_update_for_broadcast(chan).unwrap()),
6737                                         });
6738                                 } else {
6739                                         return try_chan_phase_entry!(self, Err(ChannelError::Close(
6740                                                 "Got an announcement_signatures message for an unfunded channel!".into())), chan_phase_entry);
6741                                 }
6742                         },
6743                         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))
6744                 }
6745                 Ok(())
6746         }
6747
6748         /// Returns DoPersist if anything changed, otherwise either SkipPersistNoEvents or an Err.
6749         fn internal_channel_update(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelUpdate) -> Result<NotifyOption, MsgHandleErrInternal> {
6750                 let (chan_counterparty_node_id, chan_id) = match self.short_to_chan_info.read().unwrap().get(&msg.contents.short_channel_id) {
6751                         Some((cp_id, chan_id)) => (cp_id.clone(), chan_id.clone()),
6752                         None => {
6753                                 // It's not a local channel
6754                                 return Ok(NotifyOption::SkipPersistNoEvents)
6755                         }
6756                 };
6757                 let per_peer_state = self.per_peer_state.read().unwrap();
6758                 let peer_state_mutex_opt = per_peer_state.get(&chan_counterparty_node_id);
6759                 if peer_state_mutex_opt.is_none() {
6760                         return Ok(NotifyOption::SkipPersistNoEvents)
6761                 }
6762                 let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
6763                 let peer_state = &mut *peer_state_lock;
6764                 match peer_state.channel_by_id.entry(chan_id) {
6765                         hash_map::Entry::Occupied(mut chan_phase_entry) => {
6766                                 if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
6767                                         if chan.context.get_counterparty_node_id() != *counterparty_node_id {
6768                                                 if chan.context.should_announce() {
6769                                                         // If the announcement is about a channel of ours which is public, some
6770                                                         // other peer may simply be forwarding all its gossip to us. Don't provide
6771                                                         // a scary-looking error message and return Ok instead.
6772                                                         return Ok(NotifyOption::SkipPersistNoEvents);
6773                                                 }
6774                                                 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));
6775                                         }
6776                                         let were_node_one = self.get_our_node_id().serialize()[..] < chan.context.get_counterparty_node_id().serialize()[..];
6777                                         let msg_from_node_one = msg.contents.flags & 1 == 0;
6778                                         if were_node_one == msg_from_node_one {
6779                                                 return Ok(NotifyOption::SkipPersistNoEvents);
6780                                         } else {
6781                                                 log_debug!(self.logger, "Received channel_update {:?} for channel {}.", msg, chan_id);
6782                                                 let did_change = try_chan_phase_entry!(self, chan.channel_update(&msg), chan_phase_entry);
6783                                                 // If nothing changed after applying their update, we don't need to bother
6784                                                 // persisting.
6785                                                 if !did_change {
6786                                                         return Ok(NotifyOption::SkipPersistNoEvents);
6787                                                 }
6788                                         }
6789                                 } else {
6790                                         return try_chan_phase_entry!(self, Err(ChannelError::Close(
6791                                                 "Got a channel_update for an unfunded channel!".into())), chan_phase_entry);
6792                                 }
6793                         },
6794                         hash_map::Entry::Vacant(_) => return Ok(NotifyOption::SkipPersistNoEvents)
6795                 }
6796                 Ok(NotifyOption::DoPersist)
6797         }
6798
6799         fn internal_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<NotifyOption, MsgHandleErrInternal> {
6800                 let htlc_forwards;
6801                 let need_lnd_workaround = {
6802                         let per_peer_state = self.per_peer_state.read().unwrap();
6803
6804                         let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6805                                 .ok_or_else(|| {
6806                                         debug_assert!(false);
6807                                         MsgHandleErrInternal::send_err_msg_no_close(
6808                                                 format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id),
6809                                                 msg.channel_id
6810                                         )
6811                                 })?;
6812                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6813                         let peer_state = &mut *peer_state_lock;
6814                         match peer_state.channel_by_id.entry(msg.channel_id) {
6815                                 hash_map::Entry::Occupied(mut chan_phase_entry) => {
6816                                         if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
6817                                                 // Currently, we expect all holding cell update_adds to be dropped on peer
6818                                                 // disconnect, so Channel's reestablish will never hand us any holding cell
6819                                                 // freed HTLCs to fail backwards. If in the future we no longer drop pending
6820                                                 // add-HTLCs on disconnect, we may be handed HTLCs to fail backwards here.
6821                                                 let responses = try_chan_phase_entry!(self, chan.channel_reestablish(
6822                                                         msg, &self.logger, &self.node_signer, self.chain_hash,
6823                                                         &self.default_configuration, &*self.best_block.read().unwrap()), chan_phase_entry);
6824                                                 let mut channel_update = None;
6825                                                 if let Some(msg) = responses.shutdown_msg {
6826                                                         peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
6827                                                                 node_id: counterparty_node_id.clone(),
6828                                                                 msg,
6829                                                         });
6830                                                 } else if chan.context.is_usable() {
6831                                                         // If the channel is in a usable state (ie the channel is not being shut
6832                                                         // down), send a unicast channel_update to our counterparty to make sure
6833                                                         // they have the latest channel parameters.
6834                                                         if let Ok(msg) = self.get_channel_update_for_unicast(chan) {
6835                                                                 channel_update = Some(events::MessageSendEvent::SendChannelUpdate {
6836                                                                         node_id: chan.context.get_counterparty_node_id(),
6837                                                                         msg,
6838                                                                 });
6839                                                         }
6840                                                 }
6841                                                 let need_lnd_workaround = chan.context.workaround_lnd_bug_4006.take();
6842                                                 htlc_forwards = self.handle_channel_resumption(
6843                                                         &mut peer_state.pending_msg_events, chan, responses.raa, responses.commitment_update, responses.order,
6844                                                         Vec::new(), None, responses.channel_ready, responses.announcement_sigs);
6845                                                 if let Some(upd) = channel_update {
6846                                                         peer_state.pending_msg_events.push(upd);
6847                                                 }
6848                                                 need_lnd_workaround
6849                                         } else {
6850                                                 return try_chan_phase_entry!(self, Err(ChannelError::Close(
6851                                                         "Got a channel_reestablish message for an unfunded channel!".into())), chan_phase_entry);
6852                                         }
6853                                 },
6854                                 hash_map::Entry::Vacant(_) => {
6855                                         log_debug!(self.logger, "Sending bogus ChannelReestablish for unknown channel {} to force channel closure",
6856                                                 log_bytes!(msg.channel_id.0));
6857                                         // Unfortunately, lnd doesn't force close on errors
6858                                         // (https://github.com/lightningnetwork/lnd/blob/abb1e3463f3a83bbb843d5c399869dbe930ad94f/htlcswitch/link.go#L2119).
6859                                         // One of the few ways to get an lnd counterparty to force close is by
6860                                         // replicating what they do when restoring static channel backups (SCBs). They
6861                                         // send an invalid `ChannelReestablish` with `0` commitment numbers and an
6862                                         // invalid `your_last_per_commitment_secret`.
6863                                         //
6864                                         // Since we received a `ChannelReestablish` for a channel that doesn't exist, we
6865                                         // can assume it's likely the channel closed from our point of view, but it
6866                                         // remains open on the counterparty's side. By sending this bogus
6867                                         // `ChannelReestablish` message now as a response to theirs, we trigger them to
6868                                         // force close broadcasting their latest state. If the closing transaction from
6869                                         // our point of view remains unconfirmed, it'll enter a race with the
6870                                         // counterparty's to-be-broadcast latest commitment transaction.
6871                                         peer_state.pending_msg_events.push(MessageSendEvent::SendChannelReestablish {
6872                                                 node_id: *counterparty_node_id,
6873                                                 msg: msgs::ChannelReestablish {
6874                                                         channel_id: msg.channel_id,
6875                                                         next_local_commitment_number: 0,
6876                                                         next_remote_commitment_number: 0,
6877                                                         your_last_per_commitment_secret: [1u8; 32],
6878                                                         my_current_per_commitment_point: PublicKey::from_slice(&[2u8; 33]).unwrap(),
6879                                                         next_funding_txid: None,
6880                                                 },
6881                                         });
6882                                         return Err(MsgHandleErrInternal::send_err_msg_no_close(
6883                                                 format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}",
6884                                                         counterparty_node_id), msg.channel_id)
6885                                         )
6886                                 }
6887                         }
6888                 };
6889
6890                 let mut persist = NotifyOption::SkipPersistHandleEvents;
6891                 if let Some(forwards) = htlc_forwards {
6892                         self.forward_htlcs(&mut [forwards][..]);
6893                         persist = NotifyOption::DoPersist;
6894                 }
6895
6896                 if let Some(channel_ready_msg) = need_lnd_workaround {
6897                         self.internal_channel_ready(counterparty_node_id, &channel_ready_msg)?;
6898                 }
6899                 Ok(persist)
6900         }
6901
6902         /// Process pending events from the [`chain::Watch`], returning whether any events were processed.
6903         fn process_pending_monitor_events(&self) -> bool {
6904                 debug_assert!(self.total_consistency_lock.try_write().is_err()); // Caller holds read lock
6905
6906                 let mut failed_channels = Vec::new();
6907                 let mut pending_monitor_events = self.chain_monitor.release_pending_monitor_events();
6908                 let has_pending_monitor_events = !pending_monitor_events.is_empty();
6909                 for (funding_outpoint, mut monitor_events, counterparty_node_id) in pending_monitor_events.drain(..) {
6910                         for monitor_event in monitor_events.drain(..) {
6911                                 match monitor_event {
6912                                         MonitorEvent::HTLCEvent(htlc_update) => {
6913                                                 if let Some(preimage) = htlc_update.payment_preimage {
6914                                                         log_trace!(self.logger, "Claiming HTLC with preimage {} from our monitor", preimage);
6915                                                         self.claim_funds_internal(htlc_update.source, preimage, htlc_update.htlc_value_satoshis.map(|v| v * 1000), true, counterparty_node_id, funding_outpoint);
6916                                                 } else {
6917                                                         log_trace!(self.logger, "Failing HTLC with hash {} from our monitor", &htlc_update.payment_hash);
6918                                                         let receiver = HTLCDestination::NextHopChannel { node_id: counterparty_node_id, channel_id: funding_outpoint.to_channel_id() };
6919                                                         let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
6920                                                         self.fail_htlc_backwards_internal(&htlc_update.source, &htlc_update.payment_hash, &reason, receiver);
6921                                                 }
6922                                         },
6923                                         MonitorEvent::HolderForceClosed(funding_outpoint) => {
6924                                                 let counterparty_node_id_opt = match counterparty_node_id {
6925                                                         Some(cp_id) => Some(cp_id),
6926                                                         None => {
6927                                                                 // TODO: Once we can rely on the counterparty_node_id from the
6928                                                                 // monitor event, this and the id_to_peer map should be removed.
6929                                                                 let id_to_peer = self.id_to_peer.lock().unwrap();
6930                                                                 id_to_peer.get(&funding_outpoint.to_channel_id()).cloned()
6931                                                         }
6932                                                 };
6933                                                 if let Some(counterparty_node_id) = counterparty_node_id_opt {
6934                                                         let per_peer_state = self.per_peer_state.read().unwrap();
6935                                                         if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
6936                                                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6937                                                                 let peer_state = &mut *peer_state_lock;
6938                                                                 let pending_msg_events = &mut peer_state.pending_msg_events;
6939                                                                 if let hash_map::Entry::Occupied(chan_phase_entry) = peer_state.channel_by_id.entry(funding_outpoint.to_channel_id()) {
6940                                                                         if let ChannelPhase::Funded(mut chan) = remove_channel_phase!(self, chan_phase_entry) {
6941                                                                                 failed_channels.push(chan.context.force_shutdown(false));
6942                                                                                 if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
6943                                                                                         pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
6944                                                                                                 msg: update
6945                                                                                         });
6946                                                                                 }
6947                                                                                 self.issue_channel_close_events(&chan.context, ClosureReason::HolderForceClosed);
6948                                                                                 pending_msg_events.push(events::MessageSendEvent::HandleError {
6949                                                                                         node_id: chan.context.get_counterparty_node_id(),
6950                                                                                         action: msgs::ErrorAction::DisconnectPeer {
6951                                                                                                 msg: Some(msgs::ErrorMessage { channel_id: chan.context.channel_id(), data: "Channel force-closed".to_owned() })
6952                                                                                         },
6953                                                                                 });
6954                                                                         }
6955                                                                 }
6956                                                         }
6957                                                 }
6958                                         },
6959                                         MonitorEvent::Completed { funding_txo, monitor_update_id } => {
6960                                                 self.channel_monitor_updated(&funding_txo, monitor_update_id, counterparty_node_id.as_ref());
6961                                         },
6962                                 }
6963                         }
6964                 }
6965
6966                 for failure in failed_channels.drain(..) {
6967                         self.finish_close_channel(failure);
6968                 }
6969
6970                 has_pending_monitor_events
6971         }
6972
6973         /// In chanmon_consistency_target, we'd like to be able to restore monitor updating without
6974         /// handling all pending events (i.e. not PendingHTLCsForwardable). Thus, we expose monitor
6975         /// update events as a separate process method here.
6976         #[cfg(fuzzing)]
6977         pub fn process_monitor_events(&self) {
6978                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
6979                 self.process_pending_monitor_events();
6980         }
6981
6982         /// Check the holding cell in each channel and free any pending HTLCs in them if possible.
6983         /// Returns whether there were any updates such as if pending HTLCs were freed or a monitor
6984         /// update was applied.
6985         fn check_free_holding_cells(&self) -> bool {
6986                 let mut has_monitor_update = false;
6987                 let mut failed_htlcs = Vec::new();
6988
6989                 // Walk our list of channels and find any that need to update. Note that when we do find an
6990                 // update, if it includes actions that must be taken afterwards, we have to drop the
6991                 // per-peer state lock as well as the top level per_peer_state lock. Thus, we loop until we
6992                 // manage to go through all our peers without finding a single channel to update.
6993                 'peer_loop: loop {
6994                         let per_peer_state = self.per_peer_state.read().unwrap();
6995                         for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
6996                                 'chan_loop: loop {
6997                                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6998                                         let peer_state: &mut PeerState<_> = &mut *peer_state_lock;
6999                                         for (channel_id, chan) in peer_state.channel_by_id.iter_mut().filter_map(
7000                                                 |(chan_id, phase)| if let ChannelPhase::Funded(chan) = phase { Some((chan_id, chan)) } else { None }
7001                                         ) {
7002                                                 let counterparty_node_id = chan.context.get_counterparty_node_id();
7003                                                 let funding_txo = chan.context.get_funding_txo();
7004                                                 let (monitor_opt, holding_cell_failed_htlcs) =
7005                                                         chan.maybe_free_holding_cell_htlcs(&self.fee_estimator, &self.logger);
7006                                                 if !holding_cell_failed_htlcs.is_empty() {
7007                                                         failed_htlcs.push((holding_cell_failed_htlcs, *channel_id, counterparty_node_id));
7008                                                 }
7009                                                 if let Some(monitor_update) = monitor_opt {
7010                                                         has_monitor_update = true;
7011
7012                                                         handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update,
7013                                                                 peer_state_lock, peer_state, per_peer_state, chan);
7014                                                         continue 'peer_loop;
7015                                                 }
7016                                         }
7017                                         break 'chan_loop;
7018                                 }
7019                         }
7020                         break 'peer_loop;
7021                 }
7022
7023                 let has_update = has_monitor_update || !failed_htlcs.is_empty();
7024                 for (failures, channel_id, counterparty_node_id) in failed_htlcs.drain(..) {
7025                         self.fail_holding_cell_htlcs(failures, channel_id, &counterparty_node_id);
7026                 }
7027
7028                 has_update
7029         }
7030
7031         /// Check whether any channels have finished removing all pending updates after a shutdown
7032         /// exchange and can now send a closing_signed.
7033         /// Returns whether any closing_signed messages were generated.
7034         fn maybe_generate_initial_closing_signed(&self) -> bool {
7035                 let mut handle_errors: Vec<(PublicKey, Result<(), _>)> = Vec::new();
7036                 let mut has_update = false;
7037                 let mut shutdown_results = Vec::new();
7038                 {
7039                         let per_peer_state = self.per_peer_state.read().unwrap();
7040
7041                         for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
7042                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
7043                                 let peer_state = &mut *peer_state_lock;
7044                                 let pending_msg_events = &mut peer_state.pending_msg_events;
7045                                 peer_state.channel_by_id.retain(|channel_id, phase| {
7046                                         match phase {
7047                                                 ChannelPhase::Funded(chan) => {
7048                                                         match chan.maybe_propose_closing_signed(&self.fee_estimator, &self.logger) {
7049                                                                 Ok((msg_opt, tx_opt, shutdown_result_opt)) => {
7050                                                                         if let Some(msg) = msg_opt {
7051                                                                                 has_update = true;
7052                                                                                 pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
7053                                                                                         node_id: chan.context.get_counterparty_node_id(), msg,
7054                                                                                 });
7055                                                                         }
7056                                                                         debug_assert_eq!(shutdown_result_opt.is_some(), chan.is_shutdown());
7057                                                                         if let Some(shutdown_result) = shutdown_result_opt {
7058                                                                                 shutdown_results.push(shutdown_result);
7059                                                                         }
7060                                                                         if let Some(tx) = tx_opt {
7061                                                                                 // We're done with this channel. We got a closing_signed and sent back
7062                                                                                 // a closing_signed with a closing transaction to broadcast.
7063                                                                                 if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
7064                                                                                         pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
7065                                                                                                 msg: update
7066                                                                                         });
7067                                                                                 }
7068
7069                                                                                 self.issue_channel_close_events(&chan.context, ClosureReason::CooperativeClosure);
7070
7071                                                                                 log_info!(self.logger, "Broadcasting {}", log_tx!(tx));
7072                                                                                 self.tx_broadcaster.broadcast_transactions(&[&tx]);
7073                                                                                 update_maps_on_chan_removal!(self, &chan.context);
7074                                                                                 false
7075                                                                         } else { true }
7076                                                                 },
7077                                                                 Err(e) => {
7078                                                                         has_update = true;
7079                                                                         let (close_channel, res) = convert_chan_phase_err!(self, e, chan, channel_id, FUNDED_CHANNEL);
7080                                                                         handle_errors.push((chan.context.get_counterparty_node_id(), Err(res)));
7081                                                                         !close_channel
7082                                                                 }
7083                                                         }
7084                                                 },
7085                                                 _ => true, // Retain unfunded channels if present.
7086                                         }
7087                                 });
7088                         }
7089                 }
7090
7091                 for (counterparty_node_id, err) in handle_errors.drain(..) {
7092                         let _ = handle_error!(self, err, counterparty_node_id);
7093                 }
7094
7095                 for shutdown_result in shutdown_results.drain(..) {
7096                         self.finish_close_channel(shutdown_result);
7097                 }
7098
7099                 has_update
7100         }
7101
7102         /// Handle a list of channel failures during a block_connected or block_disconnected call,
7103         /// pushing the channel monitor update (if any) to the background events queue and removing the
7104         /// Channel object.
7105         fn handle_init_event_channel_failures(&self, mut failed_channels: Vec<ShutdownResult>) {
7106                 for mut failure in failed_channels.drain(..) {
7107                         // Either a commitment transactions has been confirmed on-chain or
7108                         // Channel::block_disconnected detected that the funding transaction has been
7109                         // reorganized out of the main chain.
7110                         // We cannot broadcast our latest local state via monitor update (as
7111                         // Channel::force_shutdown tries to make us do) as we may still be in initialization,
7112                         // so we track the update internally and handle it when the user next calls
7113                         // timer_tick_occurred, guaranteeing we're running normally.
7114                         if let Some((counterparty_node_id, funding_txo, update)) = failure.monitor_update.take() {
7115                                 assert_eq!(update.updates.len(), 1);
7116                                 if let ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast } = update.updates[0] {
7117                                         assert!(should_broadcast);
7118                                 } else { unreachable!(); }
7119                                 self.pending_background_events.lock().unwrap().push(
7120                                         BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
7121                                                 counterparty_node_id, funding_txo, update
7122                                         });
7123                         }
7124                         self.finish_close_channel(failure);
7125                 }
7126         }
7127
7128         /// Creates an [`OfferBuilder`] such that the [`Offer`] it builds is recognized by the
7129         /// [`ChannelManager`] when handling [`InvoiceRequest`] messages for the offer. The offer will
7130         /// not have an expiration unless otherwise set on the builder.
7131         ///
7132         /// Uses a one-hop [`BlindedPath`] for the offer with [`ChannelManager::get_our_node_id`] as the
7133         /// introduction node and a derived signing pubkey for recipient privacy. As such, currently,
7134         /// the node must be announced. Otherwise, there is no way to find a path to the introduction
7135         /// node in order to send the [`InvoiceRequest`].
7136         ///
7137         /// [`Offer`]: crate::offers::offer::Offer
7138         /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
7139         pub fn create_offer_builder(
7140                 &self, description: String
7141         ) -> OfferBuilder<DerivedMetadata, secp256k1::All> {
7142                 let node_id = self.get_our_node_id();
7143                 let expanded_key = &self.inbound_payment_key;
7144                 let entropy = &*self.entropy_source;
7145                 let secp_ctx = &self.secp_ctx;
7146                 let path = self.create_one_hop_blinded_path();
7147
7148                 OfferBuilder::deriving_signing_pubkey(description, node_id, expanded_key, entropy, secp_ctx)
7149                         .chain_hash(self.chain_hash)
7150                         .path(path)
7151         }
7152
7153         /// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
7154         /// [`ChannelManager`] when handling [`Bolt12Invoice`] messages for the refund. The builder will
7155         /// have the provided expiration set. Any changes to the expiration on the returned builder will
7156         /// not be honored by [`ChannelManager`].
7157         ///
7158         /// The provided `payment_id` is used to ensure that only one invoice is paid for the refund.
7159         ///
7160         /// Uses a one-hop [`BlindedPath`] for the refund with [`ChannelManager::get_our_node_id`] as
7161         /// the introduction node and a derived payer id for sender privacy. As such, currently, the
7162         /// node must be announced. Otherwise, there is no way to find a path to the introduction node
7163         /// in order to send the [`Bolt12Invoice`].
7164         ///
7165         /// [`Refund`]: crate::offers::refund::Refund
7166         /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
7167         pub fn create_refund_builder(
7168                 &self, description: String, amount_msats: u64, absolute_expiry: Duration,
7169                 payment_id: PaymentId, retry_strategy: Retry, max_total_routing_fee_msat: Option<u64>
7170         ) -> Result<RefundBuilder<secp256k1::All>, Bolt12SemanticError> {
7171                 let node_id = self.get_our_node_id();
7172                 let expanded_key = &self.inbound_payment_key;
7173                 let entropy = &*self.entropy_source;
7174                 let secp_ctx = &self.secp_ctx;
7175                 let path = self.create_one_hop_blinded_path();
7176
7177                 let builder = RefundBuilder::deriving_payer_id(
7178                         description, node_id, expanded_key, entropy, secp_ctx, amount_msats, payment_id
7179                 )?
7180                         .chain_hash(self.chain_hash)
7181                         .absolute_expiry(absolute_expiry)
7182                         .path(path);
7183
7184                 self.pending_outbound_payments
7185                         .add_new_awaiting_invoice(
7186                                 payment_id, absolute_expiry, retry_strategy, max_total_routing_fee_msat,
7187                         )
7188                         .map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
7189
7190                 Ok(builder)
7191         }
7192
7193         /// Gets a payment secret and payment hash for use in an invoice given to a third party wishing
7194         /// to pay us.
7195         ///
7196         /// This differs from [`create_inbound_payment_for_hash`] only in that it generates the
7197         /// [`PaymentHash`] and [`PaymentPreimage`] for you.
7198         ///
7199         /// The [`PaymentPreimage`] will ultimately be returned to you in the [`PaymentClaimable`], which
7200         /// will have the [`PaymentClaimable::purpose`] be [`PaymentPurpose::InvoicePayment`] with
7201         /// its [`PaymentPurpose::InvoicePayment::payment_preimage`] field filled in. That should then be
7202         /// passed directly to [`claim_funds`].
7203         ///
7204         /// See [`create_inbound_payment_for_hash`] for detailed documentation on behavior and requirements.
7205         ///
7206         /// Note that a malicious eavesdropper can intuit whether an inbound payment was created by
7207         /// `create_inbound_payment` or `create_inbound_payment_for_hash` based on runtime.
7208         ///
7209         /// # Note
7210         ///
7211         /// If you register an inbound payment with this method, then serialize the `ChannelManager`, then
7212         /// deserialize it with a node running 0.0.103 and earlier, the payment will fail to be received.
7213         ///
7214         /// Errors if `min_value_msat` is greater than total bitcoin supply.
7215         ///
7216         /// If `min_final_cltv_expiry_delta` is set to some value, then the payment will not be receivable
7217         /// on versions of LDK prior to 0.0.114.
7218         ///
7219         /// [`claim_funds`]: Self::claim_funds
7220         /// [`PaymentClaimable`]: events::Event::PaymentClaimable
7221         /// [`PaymentClaimable::purpose`]: events::Event::PaymentClaimable::purpose
7222         /// [`PaymentPurpose::InvoicePayment`]: events::PaymentPurpose::InvoicePayment
7223         /// [`PaymentPurpose::InvoicePayment::payment_preimage`]: events::PaymentPurpose::InvoicePayment::payment_preimage
7224         /// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
7225         pub fn create_inbound_payment(&self, min_value_msat: Option<u64>, invoice_expiry_delta_secs: u32,
7226                 min_final_cltv_expiry_delta: Option<u16>) -> Result<(PaymentHash, PaymentSecret), ()> {
7227                 inbound_payment::create(&self.inbound_payment_key, min_value_msat, invoice_expiry_delta_secs,
7228                         &self.entropy_source, self.highest_seen_timestamp.load(Ordering::Acquire) as u64,
7229                         min_final_cltv_expiry_delta)
7230         }
7231
7232         /// Gets a [`PaymentSecret`] for a given [`PaymentHash`], for which the payment preimage is
7233         /// stored external to LDK.
7234         ///
7235         /// A [`PaymentClaimable`] event will only be generated if the [`PaymentSecret`] matches a
7236         /// payment secret fetched via this method or [`create_inbound_payment`], and which is at least
7237         /// the `min_value_msat` provided here, if one is provided.
7238         ///
7239         /// The [`PaymentHash`] (and corresponding [`PaymentPreimage`]) should be globally unique, though
7240         /// note that LDK will not stop you from registering duplicate payment hashes for inbound
7241         /// payments.
7242         ///
7243         /// `min_value_msat` should be set if the invoice being generated contains a value. Any payment
7244         /// received for the returned [`PaymentHash`] will be required to be at least `min_value_msat`
7245         /// before a [`PaymentClaimable`] event will be generated, ensuring that we do not provide the
7246         /// sender "proof-of-payment" unless they have paid the required amount.
7247         ///
7248         /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
7249         /// in excess of the current time. This should roughly match the expiry time set in the invoice.
7250         /// After this many seconds, we will remove the inbound payment, resulting in any attempts to
7251         /// pay the invoice failing. The BOLT spec suggests 3,600 secs as a default validity time for
7252         /// invoices when no timeout is set.
7253         ///
7254         /// Note that we use block header time to time-out pending inbound payments (with some margin
7255         /// to compensate for the inaccuracy of block header timestamps). Thus, in practice we will
7256         /// accept a payment and generate a [`PaymentClaimable`] event for some time after the expiry.
7257         /// If you need exact expiry semantics, you should enforce them upon receipt of
7258         /// [`PaymentClaimable`].
7259         ///
7260         /// Note that invoices generated for inbound payments should have their `min_final_cltv_expiry_delta`
7261         /// set to at least [`MIN_FINAL_CLTV_EXPIRY_DELTA`].
7262         ///
7263         /// Note that a malicious eavesdropper can intuit whether an inbound payment was created by
7264         /// `create_inbound_payment` or `create_inbound_payment_for_hash` based on runtime.
7265         ///
7266         /// # Note
7267         ///
7268         /// If you register an inbound payment with this method, then serialize the `ChannelManager`, then
7269         /// deserialize it with a node running 0.0.103 and earlier, the payment will fail to be received.
7270         ///
7271         /// Errors if `min_value_msat` is greater than total bitcoin supply.
7272         ///
7273         /// If `min_final_cltv_expiry_delta` is set to some value, then the payment will not be receivable
7274         /// on versions of LDK prior to 0.0.114.
7275         ///
7276         /// [`create_inbound_payment`]: Self::create_inbound_payment
7277         /// [`PaymentClaimable`]: events::Event::PaymentClaimable
7278         pub fn create_inbound_payment_for_hash(&self, payment_hash: PaymentHash, min_value_msat: Option<u64>,
7279                 invoice_expiry_delta_secs: u32, min_final_cltv_expiry: Option<u16>) -> Result<PaymentSecret, ()> {
7280                 inbound_payment::create_from_hash(&self.inbound_payment_key, min_value_msat, payment_hash,
7281                         invoice_expiry_delta_secs, self.highest_seen_timestamp.load(Ordering::Acquire) as u64,
7282                         min_final_cltv_expiry)
7283         }
7284
7285         /// Gets an LDK-generated payment preimage from a payment hash and payment secret that were
7286         /// previously returned from [`create_inbound_payment`].
7287         ///
7288         /// [`create_inbound_payment`]: Self::create_inbound_payment
7289         pub fn get_payment_preimage(&self, payment_hash: PaymentHash, payment_secret: PaymentSecret) -> Result<PaymentPreimage, APIError> {
7290                 inbound_payment::get_payment_preimage(payment_hash, payment_secret, &self.inbound_payment_key)
7291         }
7292
7293         /// Creates a one-hop blinded path with [`ChannelManager::get_our_node_id`] as the introduction
7294         /// node.
7295         fn create_one_hop_blinded_path(&self) -> BlindedPath {
7296                 let entropy_source = self.entropy_source.deref();
7297                 let secp_ctx = &self.secp_ctx;
7298                 BlindedPath::one_hop_for_message(self.get_our_node_id(), entropy_source, secp_ctx).unwrap()
7299         }
7300
7301         /// Gets a fake short channel id for use in receiving [phantom node payments]. These fake scids
7302         /// are used when constructing the phantom invoice's route hints.
7303         ///
7304         /// [phantom node payments]: crate::sign::PhantomKeysManager
7305         pub fn get_phantom_scid(&self) -> u64 {
7306                 let best_block_height = self.best_block.read().unwrap().height();
7307                 let short_to_chan_info = self.short_to_chan_info.read().unwrap();
7308                 loop {
7309                         let scid_candidate = fake_scid::Namespace::Phantom.get_fake_scid(best_block_height, &self.chain_hash, &self.fake_scid_rand_bytes, &self.entropy_source);
7310                         // Ensure the generated scid doesn't conflict with a real channel.
7311                         match short_to_chan_info.get(&scid_candidate) {
7312                                 Some(_) => continue,
7313                                 None => return scid_candidate
7314                         }
7315                 }
7316         }
7317
7318         /// Gets route hints for use in receiving [phantom node payments].
7319         ///
7320         /// [phantom node payments]: crate::sign::PhantomKeysManager
7321         pub fn get_phantom_route_hints(&self) -> PhantomRouteHints {
7322                 PhantomRouteHints {
7323                         channels: self.list_usable_channels(),
7324                         phantom_scid: self.get_phantom_scid(),
7325                         real_node_pubkey: self.get_our_node_id(),
7326                 }
7327         }
7328
7329         /// Gets a fake short channel id for use in receiving intercepted payments. These fake scids are
7330         /// used when constructing the route hints for HTLCs intended to be intercepted. See
7331         /// [`ChannelManager::forward_intercepted_htlc`].
7332         ///
7333         /// Note that this method is not guaranteed to return unique values, you may need to call it a few
7334         /// times to get a unique scid.
7335         pub fn get_intercept_scid(&self) -> u64 {
7336                 let best_block_height = self.best_block.read().unwrap().height();
7337                 let short_to_chan_info = self.short_to_chan_info.read().unwrap();
7338                 loop {
7339                         let scid_candidate = fake_scid::Namespace::Intercept.get_fake_scid(best_block_height, &self.chain_hash, &self.fake_scid_rand_bytes, &self.entropy_source);
7340                         // Ensure the generated scid doesn't conflict with a real channel.
7341                         if short_to_chan_info.contains_key(&scid_candidate) { continue }
7342                         return scid_candidate
7343                 }
7344         }
7345
7346         /// Gets inflight HTLC information by processing pending outbound payments that are in
7347         /// our channels. May be used during pathfinding to account for in-use channel liquidity.
7348         pub fn compute_inflight_htlcs(&self) -> InFlightHtlcs {
7349                 let mut inflight_htlcs = InFlightHtlcs::new();
7350
7351                 let per_peer_state = self.per_peer_state.read().unwrap();
7352                 for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
7353                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
7354                         let peer_state = &mut *peer_state_lock;
7355                         for chan in peer_state.channel_by_id.values().filter_map(
7356                                 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
7357                         ) {
7358                                 for (htlc_source, _) in chan.inflight_htlc_sources() {
7359                                         if let HTLCSource::OutboundRoute { path, .. } = htlc_source {
7360                                                 inflight_htlcs.process_path(path, self.get_our_node_id());
7361                                         }
7362                                 }
7363                         }
7364                 }
7365
7366                 inflight_htlcs
7367         }
7368
7369         #[cfg(any(test, feature = "_test_utils"))]
7370         pub fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
7371                 let events = core::cell::RefCell::new(Vec::new());
7372                 let event_handler = |event: events::Event| events.borrow_mut().push(event);
7373                 self.process_pending_events(&event_handler);
7374                 events.into_inner()
7375         }
7376
7377         #[cfg(feature = "_test_utils")]
7378         pub fn push_pending_event(&self, event: events::Event) {
7379                 let mut events = self.pending_events.lock().unwrap();
7380                 events.push_back((event, None));
7381         }
7382
7383         #[cfg(test)]
7384         pub fn pop_pending_event(&self) -> Option<events::Event> {
7385                 let mut events = self.pending_events.lock().unwrap();
7386                 events.pop_front().map(|(e, _)| e)
7387         }
7388
7389         #[cfg(test)]
7390         pub fn has_pending_payments(&self) -> bool {
7391                 self.pending_outbound_payments.has_pending_payments()
7392         }
7393
7394         #[cfg(test)]
7395         pub fn clear_pending_payments(&self) {
7396                 self.pending_outbound_payments.clear_pending_payments()
7397         }
7398
7399         /// When something which was blocking a channel from updating its [`ChannelMonitor`] (e.g. an
7400         /// [`Event`] being handled) completes, this should be called to restore the channel to normal
7401         /// operation. It will double-check that nothing *else* is also blocking the same channel from
7402         /// making progress and then let any blocked [`ChannelMonitorUpdate`]s fly.
7403         fn handle_monitor_update_release(&self, counterparty_node_id: PublicKey, channel_funding_outpoint: OutPoint, mut completed_blocker: Option<RAAMonitorUpdateBlockingAction>) {
7404                 loop {
7405                         let per_peer_state = self.per_peer_state.read().unwrap();
7406                         if let Some(peer_state_mtx) = per_peer_state.get(&counterparty_node_id) {
7407                                 let mut peer_state_lck = peer_state_mtx.lock().unwrap();
7408                                 let peer_state = &mut *peer_state_lck;
7409
7410                                 if let Some(blocker) = completed_blocker.take() {
7411                                         // Only do this on the first iteration of the loop.
7412                                         if let Some(blockers) = peer_state.actions_blocking_raa_monitor_updates
7413                                                 .get_mut(&channel_funding_outpoint.to_channel_id())
7414                                         {
7415                                                 blockers.retain(|iter| iter != &blocker);
7416                                         }
7417                                 }
7418
7419                                 if self.raa_monitor_updates_held(&peer_state.actions_blocking_raa_monitor_updates,
7420                                         channel_funding_outpoint, counterparty_node_id) {
7421                                         // Check that, while holding the peer lock, we don't have anything else
7422                                         // blocking monitor updates for this channel. If we do, release the monitor
7423                                         // update(s) when those blockers complete.
7424                                         log_trace!(self.logger, "Delaying monitor unlock for channel {} as another channel's mon update needs to complete first",
7425                                                 &channel_funding_outpoint.to_channel_id());
7426                                         break;
7427                                 }
7428
7429                                 if let hash_map::Entry::Occupied(mut chan_phase_entry) = peer_state.channel_by_id.entry(channel_funding_outpoint.to_channel_id()) {
7430                                         if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
7431                                                 debug_assert_eq!(chan.context.get_funding_txo().unwrap(), channel_funding_outpoint);
7432                                                 if let Some((monitor_update, further_update_exists)) = chan.unblock_next_blocked_monitor_update() {
7433                                                         log_debug!(self.logger, "Unlocking monitor updating for channel {} and updating monitor",
7434                                                                 channel_funding_outpoint.to_channel_id());
7435                                                         handle_new_monitor_update!(self, channel_funding_outpoint, monitor_update,
7436                                                                 peer_state_lck, peer_state, per_peer_state, chan);
7437                                                         if further_update_exists {
7438                                                                 // If there are more `ChannelMonitorUpdate`s to process, restart at the
7439                                                                 // top of the loop.
7440                                                                 continue;
7441                                                         }
7442                                                 } else {
7443                                                         log_trace!(self.logger, "Unlocked monitor updating for channel {} without monitors to update",
7444                                                                 channel_funding_outpoint.to_channel_id());
7445                                                 }
7446                                         }
7447                                 }
7448                         } else {
7449                                 log_debug!(self.logger,
7450                                         "Got a release post-RAA monitor update for peer {} but the channel is gone",
7451                                         log_pubkey!(counterparty_node_id));
7452                         }
7453                         break;
7454                 }
7455         }
7456
7457         fn handle_post_event_actions(&self, actions: Vec<EventCompletionAction>) {
7458                 for action in actions {
7459                         match action {
7460                                 EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
7461                                         channel_funding_outpoint, counterparty_node_id
7462                                 } => {
7463                                         self.handle_monitor_update_release(counterparty_node_id, channel_funding_outpoint, None);
7464                                 }
7465                         }
7466                 }
7467         }
7468
7469         /// Processes any events asynchronously in the order they were generated since the last call
7470         /// using the given event handler.
7471         ///
7472         /// See the trait-level documentation of [`EventsProvider`] for requirements.
7473         pub async fn process_pending_events_async<Future: core::future::Future, H: Fn(Event) -> Future>(
7474                 &self, handler: H
7475         ) {
7476                 let mut ev;
7477                 process_events_body!(self, ev, { handler(ev).await });
7478         }
7479 }
7480
7481 impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> MessageSendEventsProvider for ChannelManager<M, T, ES, NS, SP, F, R, L>
7482 where
7483         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
7484         T::Target: BroadcasterInterface,
7485         ES::Target: EntropySource,
7486         NS::Target: NodeSigner,
7487         SP::Target: SignerProvider,
7488         F::Target: FeeEstimator,
7489         R::Target: Router,
7490         L::Target: Logger,
7491 {
7492         /// Returns `MessageSendEvent`s strictly ordered per-peer, in the order they were generated.
7493         /// The returned array will contain `MessageSendEvent`s for different peers if
7494         /// `MessageSendEvent`s to more than one peer exists, but `MessageSendEvent`s to the same peer
7495         /// is always placed next to each other.
7496         ///
7497         /// Note that that while `MessageSendEvent`s are strictly ordered per-peer, the peer order for
7498         /// the chunks of `MessageSendEvent`s for different peers is random. I.e. if the array contains
7499         /// `MessageSendEvent`s  for both `node_a` and `node_b`, the `MessageSendEvent`s for `node_a`
7500         /// will randomly be placed first or last in the returned array.
7501         ///
7502         /// Note that even though `BroadcastChannelAnnouncement` and `BroadcastChannelUpdate`
7503         /// `MessageSendEvent`s are intended to be broadcasted to all peers, they will be pleaced among
7504         /// the `MessageSendEvent`s to the specific peer they were generated under.
7505         fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
7506                 let events = RefCell::new(Vec::new());
7507                 PersistenceNotifierGuard::optionally_notify(self, || {
7508                         let mut result = NotifyOption::SkipPersistNoEvents;
7509
7510                         // TODO: This behavior should be documented. It's unintuitive that we query
7511                         // ChannelMonitors when clearing other events.
7512                         if self.process_pending_monitor_events() {
7513                                 result = NotifyOption::DoPersist;
7514                         }
7515
7516                         if self.check_free_holding_cells() {
7517                                 result = NotifyOption::DoPersist;
7518                         }
7519                         if self.maybe_generate_initial_closing_signed() {
7520                                 result = NotifyOption::DoPersist;
7521                         }
7522
7523                         let mut pending_events = Vec::new();
7524                         let per_peer_state = self.per_peer_state.read().unwrap();
7525                         for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
7526                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
7527                                 let peer_state = &mut *peer_state_lock;
7528                                 if peer_state.pending_msg_events.len() > 0 {
7529                                         pending_events.append(&mut peer_state.pending_msg_events);
7530                                 }
7531                         }
7532
7533                         if !pending_events.is_empty() {
7534                                 events.replace(pending_events);
7535                         }
7536
7537                         result
7538                 });
7539                 events.into_inner()
7540         }
7541 }
7542
7543 impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> EventsProvider for ChannelManager<M, T, ES, NS, SP, F, R, L>
7544 where
7545         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
7546         T::Target: BroadcasterInterface,
7547         ES::Target: EntropySource,
7548         NS::Target: NodeSigner,
7549         SP::Target: SignerProvider,
7550         F::Target: FeeEstimator,
7551         R::Target: Router,
7552         L::Target: Logger,
7553 {
7554         /// Processes events that must be periodically handled.
7555         ///
7556         /// An [`EventHandler`] may safely call back to the provider in order to handle an event.
7557         /// However, it must not call [`Writeable::write`] as doing so would result in a deadlock.
7558         fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler {
7559                 let mut ev;
7560                 process_events_body!(self, ev, handler.handle_event(ev));
7561         }
7562 }
7563
7564 impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> chain::Listen for ChannelManager<M, T, ES, NS, SP, F, R, L>
7565 where
7566         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
7567         T::Target: BroadcasterInterface,
7568         ES::Target: EntropySource,
7569         NS::Target: NodeSigner,
7570         SP::Target: SignerProvider,
7571         F::Target: FeeEstimator,
7572         R::Target: Router,
7573         L::Target: Logger,
7574 {
7575         fn filtered_block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
7576                 {
7577                         let best_block = self.best_block.read().unwrap();
7578                         assert_eq!(best_block.block_hash(), header.prev_blockhash,
7579                                 "Blocks must be connected in chain-order - the connected header must build on the last connected header");
7580                         assert_eq!(best_block.height(), height - 1,
7581                                 "Blocks must be connected in chain-order - the connected block height must be one greater than the previous height");
7582                 }
7583
7584                 self.transactions_confirmed(header, txdata, height);
7585                 self.best_block_updated(header, height);
7586         }
7587
7588         fn block_disconnected(&self, header: &BlockHeader, height: u32) {
7589                 let _persistence_guard =
7590                         PersistenceNotifierGuard::optionally_notify_skipping_background_events(
7591                                 self, || -> NotifyOption { NotifyOption::DoPersist });
7592                 let new_height = height - 1;
7593                 {
7594                         let mut best_block = self.best_block.write().unwrap();
7595                         assert_eq!(best_block.block_hash(), header.block_hash(),
7596                                 "Blocks must be disconnected in chain-order - the disconnected header must be the last connected header");
7597                         assert_eq!(best_block.height(), height,
7598                                 "Blocks must be disconnected in chain-order - the disconnected block must have the correct height");
7599                         *best_block = BestBlock::new(header.prev_blockhash, new_height)
7600                 }
7601
7602                 self.do_chain_event(Some(new_height), |channel| channel.best_block_updated(new_height, header.time, self.chain_hash, &self.node_signer, &self.default_configuration, &self.logger));
7603         }
7604 }
7605
7606 impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> chain::Confirm for ChannelManager<M, T, ES, NS, SP, F, R, L>
7607 where
7608         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
7609         T::Target: BroadcasterInterface,
7610         ES::Target: EntropySource,
7611         NS::Target: NodeSigner,
7612         SP::Target: SignerProvider,
7613         F::Target: FeeEstimator,
7614         R::Target: Router,
7615         L::Target: Logger,
7616 {
7617         fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
7618                 // Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
7619                 // during initialization prior to the chain_monitor being fully configured in some cases.
7620                 // See the docs for `ChannelManagerReadArgs` for more.
7621
7622                 let block_hash = header.block_hash();
7623                 log_trace!(self.logger, "{} transactions included in block {} at height {} provided", txdata.len(), block_hash, height);
7624
7625                 let _persistence_guard =
7626                         PersistenceNotifierGuard::optionally_notify_skipping_background_events(
7627                                 self, || -> NotifyOption { NotifyOption::DoPersist });
7628                 self.do_chain_event(Some(height), |channel| channel.transactions_confirmed(&block_hash, height, txdata, self.chain_hash, &self.node_signer, &self.default_configuration, &self.logger)
7629                         .map(|(a, b)| (a, Vec::new(), b)));
7630
7631                 let last_best_block_height = self.best_block.read().unwrap().height();
7632                 if height < last_best_block_height {
7633                         let timestamp = self.highest_seen_timestamp.load(Ordering::Acquire);
7634                         self.do_chain_event(Some(last_best_block_height), |channel| channel.best_block_updated(last_best_block_height, timestamp as u32, self.chain_hash, &self.node_signer, &self.default_configuration, &self.logger));
7635                 }
7636         }
7637
7638         fn best_block_updated(&self, header: &BlockHeader, height: u32) {
7639                 // Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
7640                 // during initialization prior to the chain_monitor being fully configured in some cases.
7641                 // See the docs for `ChannelManagerReadArgs` for more.
7642
7643                 let block_hash = header.block_hash();
7644                 log_trace!(self.logger, "New best block: {} at height {}", block_hash, height);
7645
7646                 let _persistence_guard =
7647                         PersistenceNotifierGuard::optionally_notify_skipping_background_events(
7648                                 self, || -> NotifyOption { NotifyOption::DoPersist });
7649                 *self.best_block.write().unwrap() = BestBlock::new(block_hash, height);
7650
7651                 self.do_chain_event(Some(height), |channel| channel.best_block_updated(height, header.time, self.chain_hash, &self.node_signer, &self.default_configuration, &self.logger));
7652
7653                 macro_rules! max_time {
7654                         ($timestamp: expr) => {
7655                                 loop {
7656                                         // Update $timestamp to be the max of its current value and the block
7657                                         // timestamp. This should keep us close to the current time without relying on
7658                                         // having an explicit local time source.
7659                                         // Just in case we end up in a race, we loop until we either successfully
7660                                         // update $timestamp or decide we don't need to.
7661                                         let old_serial = $timestamp.load(Ordering::Acquire);
7662                                         if old_serial >= header.time as usize { break; }
7663                                         if $timestamp.compare_exchange(old_serial, header.time as usize, Ordering::AcqRel, Ordering::Relaxed).is_ok() {
7664                                                 break;
7665                                         }
7666                                 }
7667                         }
7668                 }
7669                 max_time!(self.highest_seen_timestamp);
7670                 let mut payment_secrets = self.pending_inbound_payments.lock().unwrap();
7671                 payment_secrets.retain(|_, inbound_payment| {
7672                         inbound_payment.expiry_time > header.time as u64
7673                 });
7674         }
7675
7676         fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
7677                 let mut res = Vec::with_capacity(self.short_to_chan_info.read().unwrap().len());
7678                 for (_cp_id, peer_state_mutex) in self.per_peer_state.read().unwrap().iter() {
7679                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
7680                         let peer_state = &mut *peer_state_lock;
7681                         for chan in peer_state.channel_by_id.values().filter_map(|phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }) {
7682                                 if let (Some(funding_txo), Some(block_hash)) = (chan.context.get_funding_txo(), chan.context.get_funding_tx_confirmed_in()) {
7683                                         res.push((funding_txo.txid, Some(block_hash)));
7684                                 }
7685                         }
7686                 }
7687                 res
7688         }
7689
7690         fn transaction_unconfirmed(&self, txid: &Txid) {
7691                 let _persistence_guard =
7692                         PersistenceNotifierGuard::optionally_notify_skipping_background_events(
7693                                 self, || -> NotifyOption { NotifyOption::DoPersist });
7694                 self.do_chain_event(None, |channel| {
7695                         if let Some(funding_txo) = channel.context.get_funding_txo() {
7696                                 if funding_txo.txid == *txid {
7697                                         channel.funding_transaction_unconfirmed(&self.logger).map(|()| (None, Vec::new(), None))
7698                                 } else { Ok((None, Vec::new(), None)) }
7699                         } else { Ok((None, Vec::new(), None)) }
7700                 });
7701         }
7702 }
7703
7704 impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> ChannelManager<M, T, ES, NS, SP, F, R, L>
7705 where
7706         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
7707         T::Target: BroadcasterInterface,
7708         ES::Target: EntropySource,
7709         NS::Target: NodeSigner,
7710         SP::Target: SignerProvider,
7711         F::Target: FeeEstimator,
7712         R::Target: Router,
7713         L::Target: Logger,
7714 {
7715         /// Calls a function which handles an on-chain event (blocks dis/connected, transactions
7716         /// un/confirmed, etc) on each channel, handling any resulting errors or messages generated by
7717         /// the function.
7718         fn do_chain_event<FN: Fn(&mut Channel<SP>) -> Result<(Option<msgs::ChannelReady>, Vec<(HTLCSource, PaymentHash)>, Option<msgs::AnnouncementSignatures>), ClosureReason>>
7719                         (&self, height_opt: Option<u32>, f: FN) {
7720                 // Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
7721                 // during initialization prior to the chain_monitor being fully configured in some cases.
7722                 // See the docs for `ChannelManagerReadArgs` for more.
7723
7724                 let mut failed_channels = Vec::new();
7725                 let mut timed_out_htlcs = Vec::new();
7726                 {
7727                         let per_peer_state = self.per_peer_state.read().unwrap();
7728                         for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
7729                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
7730                                 let peer_state = &mut *peer_state_lock;
7731                                 let pending_msg_events = &mut peer_state.pending_msg_events;
7732                                 peer_state.channel_by_id.retain(|_, phase| {
7733                                         match phase {
7734                                                 // Retain unfunded channels.
7735                                                 ChannelPhase::UnfundedOutboundV1(_) | ChannelPhase::UnfundedInboundV1(_) => true,
7736                                                 ChannelPhase::Funded(channel) => {
7737                                                         let res = f(channel);
7738                                                         if let Ok((channel_ready_opt, mut timed_out_pending_htlcs, announcement_sigs)) = res {
7739                                                                 for (source, payment_hash) in timed_out_pending_htlcs.drain(..) {
7740                                                                         let (failure_code, data) = self.get_htlc_inbound_temp_fail_err_and_data(0x1000|14 /* expiry_too_soon */, &channel);
7741                                                                         timed_out_htlcs.push((source, payment_hash, HTLCFailReason::reason(failure_code, data),
7742                                                                                 HTLCDestination::NextHopChannel { node_id: Some(channel.context.get_counterparty_node_id()), channel_id: channel.context.channel_id() }));
7743                                                                 }
7744                                                                 if let Some(channel_ready) = channel_ready_opt {
7745                                                                         send_channel_ready!(self, pending_msg_events, channel, channel_ready);
7746                                                                         if channel.context.is_usable() {
7747                                                                                 log_trace!(self.logger, "Sending channel_ready with private initial channel_update for our counterparty on channel {}", channel.context.channel_id());
7748                                                                                 if let Ok(msg) = self.get_channel_update_for_unicast(channel) {
7749                                                                                         pending_msg_events.push(events::MessageSendEvent::SendChannelUpdate {
7750                                                                                                 node_id: channel.context.get_counterparty_node_id(),
7751                                                                                                 msg,
7752                                                                                         });
7753                                                                                 }
7754                                                                         } else {
7755                                                                                 log_trace!(self.logger, "Sending channel_ready WITHOUT channel_update for {}", channel.context.channel_id());
7756                                                                         }
7757                                                                 }
7758
7759                                                                 {
7760                                                                         let mut pending_events = self.pending_events.lock().unwrap();
7761                                                                         emit_channel_ready_event!(pending_events, channel);
7762                                                                 }
7763
7764                                                                 if let Some(announcement_sigs) = announcement_sigs {
7765                                                                         log_trace!(self.logger, "Sending announcement_signatures for channel {}", channel.context.channel_id());
7766                                                                         pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
7767                                                                                 node_id: channel.context.get_counterparty_node_id(),
7768                                                                                 msg: announcement_sigs,
7769                                                                         });
7770                                                                         if let Some(height) = height_opt {
7771                                                                                 if let Some(announcement) = channel.get_signed_channel_announcement(&self.node_signer, self.chain_hash, height, &self.default_configuration) {
7772                                                                                         pending_msg_events.push(events::MessageSendEvent::BroadcastChannelAnnouncement {
7773                                                                                                 msg: announcement,
7774                                                                                                 // Note that announcement_signatures fails if the channel cannot be announced,
7775                                                                                                 // so get_channel_update_for_broadcast will never fail by the time we get here.
7776                                                                                                 update_msg: Some(self.get_channel_update_for_broadcast(channel).unwrap()),
7777                                                                                         });
7778                                                                                 }
7779                                                                         }
7780                                                                 }
7781                                                                 if channel.is_our_channel_ready() {
7782                                                                         if let Some(real_scid) = channel.context.get_short_channel_id() {
7783                                                                                 // If we sent a 0conf channel_ready, and now have an SCID, we add it
7784                                                                                 // to the short_to_chan_info map here. Note that we check whether we
7785                                                                                 // can relay using the real SCID at relay-time (i.e.
7786                                                                                 // enforce option_scid_alias then), and if the funding tx is ever
7787                                                                                 // un-confirmed we force-close the channel, ensuring short_to_chan_info
7788                                                                                 // is always consistent.
7789                                                                                 let mut short_to_chan_info = self.short_to_chan_info.write().unwrap();
7790                                                                                 let scid_insert = short_to_chan_info.insert(real_scid, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
7791                                                                                 assert!(scid_insert.is_none() || scid_insert.unwrap() == (channel.context.get_counterparty_node_id(), channel.context.channel_id()),
7792                                                                                         "SCIDs should never collide - ensure you weren't behind by a full {} blocks when creating channels",
7793                                                                                         fake_scid::MAX_SCID_BLOCKS_FROM_NOW);
7794                                                                         }
7795                                                                 }
7796                                                         } else if let Err(reason) = res {
7797                                                                 update_maps_on_chan_removal!(self, &channel.context);
7798                                                                 // It looks like our counterparty went on-chain or funding transaction was
7799                                                                 // reorged out of the main chain. Close the channel.
7800                                                                 failed_channels.push(channel.context.force_shutdown(true));
7801                                                                 if let Ok(update) = self.get_channel_update_for_broadcast(&channel) {
7802                                                                         pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
7803                                                                                 msg: update
7804                                                                         });
7805                                                                 }
7806                                                                 let reason_message = format!("{}", reason);
7807                                                                 self.issue_channel_close_events(&channel.context, reason);
7808                                                                 pending_msg_events.push(events::MessageSendEvent::HandleError {
7809                                                                         node_id: channel.context.get_counterparty_node_id(),
7810                                                                         action: msgs::ErrorAction::DisconnectPeer {
7811                                                                                 msg: Some(msgs::ErrorMessage {
7812                                                                                         channel_id: channel.context.channel_id(),
7813                                                                                         data: reason_message,
7814                                                                                 })
7815                                                                         },
7816                                                                 });
7817                                                                 return false;
7818                                                         }
7819                                                         true
7820                                                 }
7821                                         }
7822                                 });
7823                         }
7824                 }
7825
7826                 if let Some(height) = height_opt {
7827                         self.claimable_payments.lock().unwrap().claimable_payments.retain(|payment_hash, payment| {
7828                                 payment.htlcs.retain(|htlc| {
7829                                         // If height is approaching the number of blocks we think it takes us to get
7830                                         // our commitment transaction confirmed before the HTLC expires, plus the
7831                                         // number of blocks we generally consider it to take to do a commitment update,
7832                                         // just give up on it and fail the HTLC.
7833                                         if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
7834                                                 let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();
7835                                                 htlc_msat_height_data.extend_from_slice(&height.to_be_bytes());
7836
7837                                                 timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(),
7838                                                         HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
7839                                                         HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }));
7840                                                 false
7841                                         } else { true }
7842                                 });
7843                                 !payment.htlcs.is_empty() // Only retain this entry if htlcs has at least one entry.
7844                         });
7845
7846                         let mut intercepted_htlcs = self.pending_intercepted_htlcs.lock().unwrap();
7847                         intercepted_htlcs.retain(|_, htlc| {
7848                                 if height >= htlc.forward_info.outgoing_cltv_value - HTLC_FAIL_BACK_BUFFER {
7849                                         let prev_hop_data = HTLCSource::PreviousHopData(HTLCPreviousHopData {
7850                                                 short_channel_id: htlc.prev_short_channel_id,
7851                                                 user_channel_id: Some(htlc.prev_user_channel_id),
7852                                                 htlc_id: htlc.prev_htlc_id,
7853                                                 incoming_packet_shared_secret: htlc.forward_info.incoming_shared_secret,
7854                                                 phantom_shared_secret: None,
7855                                                 outpoint: htlc.prev_funding_outpoint,
7856                                         });
7857
7858                                         let requested_forward_scid /* intercept scid */ = match htlc.forward_info.routing {
7859                                                 PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
7860                                                 _ => unreachable!(),
7861                                         };
7862                                         timed_out_htlcs.push((prev_hop_data, htlc.forward_info.payment_hash,
7863                                                         HTLCFailReason::from_failure_code(0x2000 | 2),
7864                                                         HTLCDestination::InvalidForward { requested_forward_scid }));
7865                                         log_trace!(self.logger, "Timing out intercepted HTLC with requested forward scid {}", requested_forward_scid);
7866                                         false
7867                                 } else { true }
7868                         });
7869                 }
7870
7871                 self.handle_init_event_channel_failures(failed_channels);
7872
7873                 for (source, payment_hash, reason, destination) in timed_out_htlcs.drain(..) {
7874                         self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, destination);
7875                 }
7876         }
7877
7878         /// Gets a [`Future`] that completes when this [`ChannelManager`] may need to be persisted or
7879         /// may have events that need processing.
7880         ///
7881         /// In order to check if this [`ChannelManager`] needs persisting, call
7882         /// [`Self::get_and_clear_needs_persistence`].
7883         ///
7884         /// Note that callbacks registered on the [`Future`] MUST NOT call back into this
7885         /// [`ChannelManager`] and should instead register actions to be taken later.
7886         pub fn get_event_or_persistence_needed_future(&self) -> Future {
7887                 self.event_persist_notifier.get_future()
7888         }
7889
7890         /// Returns true if this [`ChannelManager`] needs to be persisted.
7891         pub fn get_and_clear_needs_persistence(&self) -> bool {
7892                 self.needs_persist_flag.swap(false, Ordering::AcqRel)
7893         }
7894
7895         #[cfg(any(test, feature = "_test_utils"))]
7896         pub fn get_event_or_persist_condvar_value(&self) -> bool {
7897                 self.event_persist_notifier.notify_pending()
7898         }
7899
7900         /// Gets the latest best block which was connected either via the [`chain::Listen`] or
7901         /// [`chain::Confirm`] interfaces.
7902         pub fn current_best_block(&self) -> BestBlock {
7903                 self.best_block.read().unwrap().clone()
7904         }
7905
7906         /// Fetches the set of [`NodeFeatures`] flags which are provided by or required by
7907         /// [`ChannelManager`].
7908         pub fn node_features(&self) -> NodeFeatures {
7909                 provided_node_features(&self.default_configuration)
7910         }
7911
7912         /// Fetches the set of [`Bolt11InvoiceFeatures`] flags which are provided by or required by
7913         /// [`ChannelManager`].
7914         ///
7915         /// Note that the invoice feature flags can vary depending on if the invoice is a "phantom invoice"
7916         /// or not. Thus, this method is not public.
7917         #[cfg(any(feature = "_test_utils", test))]
7918         pub fn invoice_features(&self) -> Bolt11InvoiceFeatures {
7919                 provided_invoice_features(&self.default_configuration)
7920         }
7921
7922         /// Fetches the set of [`ChannelFeatures`] flags which are provided by or required by
7923         /// [`ChannelManager`].
7924         pub fn channel_features(&self) -> ChannelFeatures {
7925                 provided_channel_features(&self.default_configuration)
7926         }
7927
7928         /// Fetches the set of [`ChannelTypeFeatures`] flags which are provided by or required by
7929         /// [`ChannelManager`].
7930         pub fn channel_type_features(&self) -> ChannelTypeFeatures {
7931                 provided_channel_type_features(&self.default_configuration)
7932         }
7933
7934         /// Fetches the set of [`InitFeatures`] flags which are provided by or required by
7935         /// [`ChannelManager`].
7936         pub fn init_features(&self) -> InitFeatures {
7937                 provided_init_features(&self.default_configuration)
7938         }
7939 }
7940
7941 impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>
7942         ChannelMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, L>
7943 where
7944         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
7945         T::Target: BroadcasterInterface,
7946         ES::Target: EntropySource,
7947         NS::Target: NodeSigner,
7948         SP::Target: SignerProvider,
7949         F::Target: FeeEstimator,
7950         R::Target: Router,
7951         L::Target: Logger,
7952 {
7953         fn handle_open_channel(&self, counterparty_node_id: &PublicKey, msg: &msgs::OpenChannel) {
7954                 // Note that we never need to persist the updated ChannelManager for an inbound
7955                 // open_channel message - pre-funded channels are never written so there should be no
7956                 // change to the contents.
7957                 let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
7958                         let res = self.internal_open_channel(counterparty_node_id, msg);
7959                         let persist = match &res {
7960                                 Err(e) if e.closes_channel() => {
7961                                         debug_assert!(false, "We shouldn't close a new channel");
7962                                         NotifyOption::DoPersist
7963                                 },
7964                                 _ => NotifyOption::SkipPersistHandleEvents,
7965                         };
7966                         let _ = handle_error!(self, res, *counterparty_node_id);
7967                         persist
7968                 });
7969         }
7970
7971         fn handle_open_channel_v2(&self, counterparty_node_id: &PublicKey, msg: &msgs::OpenChannelV2) {
7972                 let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
7973                         "Dual-funded channels not supported".to_owned(),
7974                          msg.temporary_channel_id.clone())), *counterparty_node_id);
7975         }
7976
7977         fn handle_accept_channel(&self, counterparty_node_id: &PublicKey, msg: &msgs::AcceptChannel) {
7978                 // Note that we never need to persist the updated ChannelManager for an inbound
7979                 // accept_channel message - pre-funded channels are never written so there should be no
7980                 // change to the contents.
7981                 let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
7982                         let _ = handle_error!(self, self.internal_accept_channel(counterparty_node_id, msg), *counterparty_node_id);
7983                         NotifyOption::SkipPersistHandleEvents
7984                 });
7985         }
7986
7987         fn handle_accept_channel_v2(&self, counterparty_node_id: &PublicKey, msg: &msgs::AcceptChannelV2) {
7988                 let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
7989                         "Dual-funded channels not supported".to_owned(),
7990                          msg.temporary_channel_id.clone())), *counterparty_node_id);
7991         }
7992
7993         fn handle_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) {
7994                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
7995                 let _ = handle_error!(self, self.internal_funding_created(counterparty_node_id, msg), *counterparty_node_id);
7996         }
7997
7998         fn handle_funding_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingSigned) {
7999                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8000                 let _ = handle_error!(self, self.internal_funding_signed(counterparty_node_id, msg), *counterparty_node_id);
8001         }
8002
8003         fn handle_channel_ready(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReady) {
8004                 // Note that we never need to persist the updated ChannelManager for an inbound
8005                 // channel_ready message - while the channel's state will change, any channel_ready message
8006                 // will ultimately be re-sent on startup and the `ChannelMonitor` won't be updated so we
8007                 // will not force-close the channel on startup.
8008                 let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
8009                         let res = self.internal_channel_ready(counterparty_node_id, msg);
8010                         let persist = match &res {
8011                                 Err(e) if e.closes_channel() => NotifyOption::DoPersist,
8012                                 _ => NotifyOption::SkipPersistHandleEvents,
8013                         };
8014                         let _ = handle_error!(self, res, *counterparty_node_id);
8015                         persist
8016                 });
8017         }
8018
8019         fn handle_shutdown(&self, counterparty_node_id: &PublicKey, msg: &msgs::Shutdown) {
8020                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8021                 let _ = handle_error!(self, self.internal_shutdown(counterparty_node_id, msg), *counterparty_node_id);
8022         }
8023
8024         fn handle_closing_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::ClosingSigned) {
8025                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8026                 let _ = handle_error!(self, self.internal_closing_signed(counterparty_node_id, msg), *counterparty_node_id);
8027         }
8028
8029         fn handle_update_add_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) {
8030                 // Note that we never need to persist the updated ChannelManager for an inbound
8031                 // update_add_htlc message - the message itself doesn't change our channel state only the
8032                 // `commitment_signed` message afterwards will.
8033                 let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
8034                         let res = self.internal_update_add_htlc(counterparty_node_id, msg);
8035                         let persist = match &res {
8036                                 Err(e) if e.closes_channel() => NotifyOption::DoPersist,
8037                                 Err(_) => NotifyOption::SkipPersistHandleEvents,
8038                                 Ok(()) => NotifyOption::SkipPersistNoEvents,
8039                         };
8040                         let _ = handle_error!(self, res, *counterparty_node_id);
8041                         persist
8042                 });
8043         }
8044
8045         fn handle_update_fulfill_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) {
8046                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8047                 let _ = handle_error!(self, self.internal_update_fulfill_htlc(counterparty_node_id, msg), *counterparty_node_id);
8048         }
8049
8050         fn handle_update_fail_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) {
8051                 // Note that we never need to persist the updated ChannelManager for an inbound
8052                 // update_fail_htlc message - the message itself doesn't change our channel state only the
8053                 // `commitment_signed` message afterwards will.
8054                 let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
8055                         let res = self.internal_update_fail_htlc(counterparty_node_id, msg);
8056                         let persist = match &res {
8057                                 Err(e) if e.closes_channel() => NotifyOption::DoPersist,
8058                                 Err(_) => NotifyOption::SkipPersistHandleEvents,
8059                                 Ok(()) => NotifyOption::SkipPersistNoEvents,
8060                         };
8061                         let _ = handle_error!(self, res, *counterparty_node_id);
8062                         persist
8063                 });
8064         }
8065
8066         fn handle_update_fail_malformed_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) {
8067                 // Note that we never need to persist the updated ChannelManager for an inbound
8068                 // update_fail_malformed_htlc message - the message itself doesn't change our channel state
8069                 // only the `commitment_signed` message afterwards will.
8070                 let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
8071                         let res = self.internal_update_fail_malformed_htlc(counterparty_node_id, msg);
8072                         let persist = match &res {
8073                                 Err(e) if e.closes_channel() => NotifyOption::DoPersist,
8074                                 Err(_) => NotifyOption::SkipPersistHandleEvents,
8075                                 Ok(()) => NotifyOption::SkipPersistNoEvents,
8076                         };
8077                         let _ = handle_error!(self, res, *counterparty_node_id);
8078                         persist
8079                 });
8080         }
8081
8082         fn handle_commitment_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::CommitmentSigned) {
8083                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8084                 let _ = handle_error!(self, self.internal_commitment_signed(counterparty_node_id, msg), *counterparty_node_id);
8085         }
8086
8087         fn handle_revoke_and_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::RevokeAndACK) {
8088                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8089                 let _ = handle_error!(self, self.internal_revoke_and_ack(counterparty_node_id, msg), *counterparty_node_id);
8090         }
8091
8092         fn handle_update_fee(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFee) {
8093                 // Note that we never need to persist the updated ChannelManager for an inbound
8094                 // update_fee message - the message itself doesn't change our channel state only the
8095                 // `commitment_signed` message afterwards will.
8096                 let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
8097                         let res = self.internal_update_fee(counterparty_node_id, msg);
8098                         let persist = match &res {
8099                                 Err(e) if e.closes_channel() => NotifyOption::DoPersist,
8100                                 Err(_) => NotifyOption::SkipPersistHandleEvents,
8101                                 Ok(()) => NotifyOption::SkipPersistNoEvents,
8102                         };
8103                         let _ = handle_error!(self, res, *counterparty_node_id);
8104                         persist
8105                 });
8106         }
8107
8108         fn handle_announcement_signatures(&self, counterparty_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) {
8109                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8110                 let _ = handle_error!(self, self.internal_announcement_signatures(counterparty_node_id, msg), *counterparty_node_id);
8111         }
8112
8113         fn handle_channel_update(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelUpdate) {
8114                 PersistenceNotifierGuard::optionally_notify(self, || {
8115                         if let Ok(persist) = handle_error!(self, self.internal_channel_update(counterparty_node_id, msg), *counterparty_node_id) {
8116                                 persist
8117                         } else {
8118                                 NotifyOption::DoPersist
8119                         }
8120                 });
8121         }
8122
8123         fn handle_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) {
8124                 let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
8125                         let res = self.internal_channel_reestablish(counterparty_node_id, msg);
8126                         let persist = match &res {
8127                                 Err(e) if e.closes_channel() => NotifyOption::DoPersist,
8128                                 Err(_) => NotifyOption::SkipPersistHandleEvents,
8129                                 Ok(persist) => *persist,
8130                         };
8131                         let _ = handle_error!(self, res, *counterparty_node_id);
8132                         persist
8133                 });
8134         }
8135
8136         fn peer_disconnected(&self, counterparty_node_id: &PublicKey) {
8137                 let _persistence_guard = PersistenceNotifierGuard::optionally_notify(
8138                         self, || NotifyOption::SkipPersistHandleEvents);
8139                 let mut failed_channels = Vec::new();
8140                 let mut per_peer_state = self.per_peer_state.write().unwrap();
8141                 let remove_peer = {
8142                         log_debug!(self.logger, "Marking channels with {} disconnected and generating channel_updates.",
8143                                 log_pubkey!(counterparty_node_id));
8144                         if let Some(peer_state_mutex) = per_peer_state.get(counterparty_node_id) {
8145                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
8146                                 let peer_state = &mut *peer_state_lock;
8147                                 let pending_msg_events = &mut peer_state.pending_msg_events;
8148                                 peer_state.channel_by_id.retain(|_, phase| {
8149                                         let context = match phase {
8150                                                 ChannelPhase::Funded(chan) => {
8151                                                         if chan.remove_uncommitted_htlcs_and_mark_paused(&self.logger).is_ok() {
8152                                                                 // We only retain funded channels that are not shutdown.
8153                                                                 return true;
8154                                                         }
8155                                                         &mut chan.context
8156                                                 },
8157                                                 // Unfunded channels will always be removed.
8158                                                 ChannelPhase::UnfundedOutboundV1(chan) => {
8159                                                         &mut chan.context
8160                                                 },
8161                                                 ChannelPhase::UnfundedInboundV1(chan) => {
8162                                                         &mut chan.context
8163                                                 },
8164                                         };
8165                                         // Clean up for removal.
8166                                         update_maps_on_chan_removal!(self, &context);
8167                                         self.issue_channel_close_events(&context, ClosureReason::DisconnectedPeer);
8168                                         failed_channels.push(context.force_shutdown(false));
8169                                         false
8170                                 });
8171                                 // Note that we don't bother generating any events for pre-accept channels -
8172                                 // they're not considered "channels" yet from the PoV of our events interface.
8173                                 peer_state.inbound_channel_request_by_id.clear();
8174                                 pending_msg_events.retain(|msg| {
8175                                         match msg {
8176                                                 // V1 Channel Establishment
8177                                                 &events::MessageSendEvent::SendAcceptChannel { .. } => false,
8178                                                 &events::MessageSendEvent::SendOpenChannel { .. } => false,
8179                                                 &events::MessageSendEvent::SendFundingCreated { .. } => false,
8180                                                 &events::MessageSendEvent::SendFundingSigned { .. } => false,
8181                                                 // V2 Channel Establishment
8182                                                 &events::MessageSendEvent::SendAcceptChannelV2 { .. } => false,
8183                                                 &events::MessageSendEvent::SendOpenChannelV2 { .. } => false,
8184                                                 // Common Channel Establishment
8185                                                 &events::MessageSendEvent::SendChannelReady { .. } => false,
8186                                                 &events::MessageSendEvent::SendAnnouncementSignatures { .. } => false,
8187                                                 // Interactive Transaction Construction
8188                                                 &events::MessageSendEvent::SendTxAddInput { .. } => false,
8189                                                 &events::MessageSendEvent::SendTxAddOutput { .. } => false,
8190                                                 &events::MessageSendEvent::SendTxRemoveInput { .. } => false,
8191                                                 &events::MessageSendEvent::SendTxRemoveOutput { .. } => false,
8192                                                 &events::MessageSendEvent::SendTxComplete { .. } => false,
8193                                                 &events::MessageSendEvent::SendTxSignatures { .. } => false,
8194                                                 &events::MessageSendEvent::SendTxInitRbf { .. } => false,
8195                                                 &events::MessageSendEvent::SendTxAckRbf { .. } => false,
8196                                                 &events::MessageSendEvent::SendTxAbort { .. } => false,
8197                                                 // Channel Operations
8198                                                 &events::MessageSendEvent::UpdateHTLCs { .. } => false,
8199                                                 &events::MessageSendEvent::SendRevokeAndACK { .. } => false,
8200                                                 &events::MessageSendEvent::SendClosingSigned { .. } => false,
8201                                                 &events::MessageSendEvent::SendShutdown { .. } => false,
8202                                                 &events::MessageSendEvent::SendChannelReestablish { .. } => false,
8203                                                 &events::MessageSendEvent::HandleError { .. } => false,
8204                                                 // Gossip
8205                                                 &events::MessageSendEvent::SendChannelAnnouncement { .. } => false,
8206                                                 &events::MessageSendEvent::BroadcastChannelAnnouncement { .. } => true,
8207                                                 &events::MessageSendEvent::BroadcastChannelUpdate { .. } => true,
8208                                                 &events::MessageSendEvent::BroadcastNodeAnnouncement { .. } => true,
8209                                                 &events::MessageSendEvent::SendChannelUpdate { .. } => false,
8210                                                 &events::MessageSendEvent::SendChannelRangeQuery { .. } => false,
8211                                                 &events::MessageSendEvent::SendShortIdsQuery { .. } => false,
8212                                                 &events::MessageSendEvent::SendReplyChannelRange { .. } => false,
8213                                                 &events::MessageSendEvent::SendGossipTimestampFilter { .. } => false,
8214                                         }
8215                                 });
8216                                 debug_assert!(peer_state.is_connected, "A disconnected peer cannot disconnect");
8217                                 peer_state.is_connected = false;
8218                                 peer_state.ok_to_remove(true)
8219                         } else { debug_assert!(false, "Unconnected peer disconnected"); true }
8220                 };
8221                 if remove_peer {
8222                         per_peer_state.remove(counterparty_node_id);
8223                 }
8224                 mem::drop(per_peer_state);
8225
8226                 for failure in failed_channels.drain(..) {
8227                         self.finish_close_channel(failure);
8228                 }
8229         }
8230
8231         fn peer_connected(&self, counterparty_node_id: &PublicKey, init_msg: &msgs::Init, inbound: bool) -> Result<(), ()> {
8232                 if !init_msg.features.supports_static_remote_key() {
8233                         log_debug!(self.logger, "Peer {} does not support static remote key, disconnecting", log_pubkey!(counterparty_node_id));
8234                         return Err(());
8235                 }
8236
8237                 let mut res = Ok(());
8238
8239                 PersistenceNotifierGuard::optionally_notify(self, || {
8240                         // If we have too many peers connected which don't have funded channels, disconnect the
8241                         // peer immediately (as long as it doesn't have funded channels). If we have a bunch of
8242                         // unfunded channels taking up space in memory for disconnected peers, we still let new
8243                         // peers connect, but we'll reject new channels from them.
8244                         let connected_peers_without_funded_channels = self.peers_without_funded_channels(|node| node.is_connected);
8245                         let inbound_peer_limited = inbound && connected_peers_without_funded_channels >= MAX_NO_CHANNEL_PEERS;
8246
8247                         {
8248                                 let mut peer_state_lock = self.per_peer_state.write().unwrap();
8249                                 match peer_state_lock.entry(counterparty_node_id.clone()) {
8250                                         hash_map::Entry::Vacant(e) => {
8251                                                 if inbound_peer_limited {
8252                                                         res = Err(());
8253                                                         return NotifyOption::SkipPersistNoEvents;
8254                                                 }
8255                                                 e.insert(Mutex::new(PeerState {
8256                                                         channel_by_id: HashMap::new(),
8257                                                         inbound_channel_request_by_id: HashMap::new(),
8258                                                         latest_features: init_msg.features.clone(),
8259                                                         pending_msg_events: Vec::new(),
8260                                                         in_flight_monitor_updates: BTreeMap::new(),
8261                                                         monitor_update_blocked_actions: BTreeMap::new(),
8262                                                         actions_blocking_raa_monitor_updates: BTreeMap::new(),
8263                                                         is_connected: true,
8264                                                 }));
8265                                         },
8266                                         hash_map::Entry::Occupied(e) => {
8267                                                 let mut peer_state = e.get().lock().unwrap();
8268                                                 peer_state.latest_features = init_msg.features.clone();
8269
8270                                                 let best_block_height = self.best_block.read().unwrap().height();
8271                                                 if inbound_peer_limited &&
8272                                                         Self::unfunded_channel_count(&*peer_state, best_block_height) ==
8273                                                         peer_state.channel_by_id.len()
8274                                                 {
8275                                                         res = Err(());
8276                                                         return NotifyOption::SkipPersistNoEvents;
8277                                                 }
8278
8279                                                 debug_assert!(!peer_state.is_connected, "A peer shouldn't be connected twice");
8280                                                 peer_state.is_connected = true;
8281                                         },
8282                                 }
8283                         }
8284
8285                         log_debug!(self.logger, "Generating channel_reestablish events for {}", log_pubkey!(counterparty_node_id));
8286
8287                         let per_peer_state = self.per_peer_state.read().unwrap();
8288                         if let Some(peer_state_mutex) = per_peer_state.get(counterparty_node_id) {
8289                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
8290                                 let peer_state = &mut *peer_state_lock;
8291                                 let pending_msg_events = &mut peer_state.pending_msg_events;
8292
8293                                 peer_state.channel_by_id.iter_mut().filter_map(|(_, phase)|
8294                                         if let ChannelPhase::Funded(chan) = phase { Some(chan) } else {
8295                                                 // Since unfunded channel maps are cleared upon disconnecting a peer, and they're not persisted
8296                                                 // (so won't be recovered after a crash), they shouldn't exist here and we would never need to
8297                                                 // worry about closing and removing them.
8298                                                 debug_assert!(false);
8299                                                 None
8300                                         }
8301                                 ).for_each(|chan| {
8302                                         pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
8303                                                 node_id: chan.context.get_counterparty_node_id(),
8304                                                 msg: chan.get_channel_reestablish(&self.logger),
8305                                         });
8306                                 });
8307                         }
8308
8309                         return NotifyOption::SkipPersistHandleEvents;
8310                         //TODO: Also re-broadcast announcement_signatures
8311                 });
8312                 res
8313         }
8314
8315         fn handle_error(&self, counterparty_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
8316                 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8317
8318                 match &msg.data as &str {
8319                         "cannot co-op close channel w/ active htlcs"|
8320                         "link failed to shutdown" =>
8321                         {
8322                                 // LND hasn't properly handled shutdown messages ever, and force-closes any time we
8323                                 // send one while HTLCs are still present. The issue is tracked at
8324                                 // https://github.com/lightningnetwork/lnd/issues/6039 and has had multiple patches
8325                                 // to fix it but none so far have managed to land upstream. The issue appears to be
8326                                 // very low priority for the LND team despite being marked "P1".
8327                                 // We're not going to bother handling this in a sensible way, instead simply
8328                                 // repeating the Shutdown message on repeat until morale improves.
8329                                 if !msg.channel_id.is_zero() {
8330                                         let per_peer_state = self.per_peer_state.read().unwrap();
8331                                         let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
8332                                         if peer_state_mutex_opt.is_none() { return; }
8333                                         let mut peer_state = peer_state_mutex_opt.unwrap().lock().unwrap();
8334                                         if let Some(ChannelPhase::Funded(chan)) = peer_state.channel_by_id.get(&msg.channel_id) {
8335                                                 if let Some(msg) = chan.get_outbound_shutdown() {
8336                                                         peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
8337                                                                 node_id: *counterparty_node_id,
8338                                                                 msg,
8339                                                         });
8340                                                 }
8341                                                 peer_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
8342                                                         node_id: *counterparty_node_id,
8343                                                         action: msgs::ErrorAction::SendWarningMessage {
8344                                                                 msg: msgs::WarningMessage {
8345                                                                         channel_id: msg.channel_id,
8346                                                                         data: "You appear to be exhibiting LND bug 6039, we'll keep sending you shutdown messages until you handle them correctly".to_owned()
8347                                                                 },
8348                                                                 log_level: Level::Trace,
8349                                                         }
8350                                                 });
8351                                         }
8352                                 }
8353                                 return;
8354                         }
8355                         _ => {}
8356                 }
8357
8358                 if msg.channel_id.is_zero() {
8359                         let channel_ids: Vec<ChannelId> = {
8360                                 let per_peer_state = self.per_peer_state.read().unwrap();
8361                                 let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
8362                                 if peer_state_mutex_opt.is_none() { return; }
8363                                 let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
8364                                 let peer_state = &mut *peer_state_lock;
8365                                 // Note that we don't bother generating any events for pre-accept channels -
8366                                 // they're not considered "channels" yet from the PoV of our events interface.
8367                                 peer_state.inbound_channel_request_by_id.clear();
8368                                 peer_state.channel_by_id.keys().cloned().collect()
8369                         };
8370                         for channel_id in channel_ids {
8371                                 // Untrusted messages from peer, we throw away the error if id points to a non-existent channel
8372                                 let _ = self.force_close_channel_with_peer(&channel_id, counterparty_node_id, Some(&msg.data), true);
8373                         }
8374                 } else {
8375                         {
8376                                 // First check if we can advance the channel type and try again.
8377                                 let per_peer_state = self.per_peer_state.read().unwrap();
8378                                 let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
8379                                 if peer_state_mutex_opt.is_none() { return; }
8380                                 let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
8381                                 let peer_state = &mut *peer_state_lock;
8382                                 if let Some(ChannelPhase::UnfundedOutboundV1(chan)) = peer_state.channel_by_id.get_mut(&msg.channel_id) {
8383                                         if let Ok(msg) = chan.maybe_handle_error_without_close(self.chain_hash, &self.fee_estimator) {
8384                                                 peer_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
8385                                                         node_id: *counterparty_node_id,
8386                                                         msg,
8387                                                 });
8388                                                 return;
8389                                         }
8390                                 }
8391                         }
8392
8393                         // Untrusted messages from peer, we throw away the error if id points to a non-existent channel
8394                         let _ = self.force_close_channel_with_peer(&msg.channel_id, counterparty_node_id, Some(&msg.data), true);
8395                 }
8396         }
8397
8398         fn provided_node_features(&self) -> NodeFeatures {
8399                 provided_node_features(&self.default_configuration)
8400         }
8401
8402         fn provided_init_features(&self, _their_init_features: &PublicKey) -> InitFeatures {
8403                 provided_init_features(&self.default_configuration)
8404         }
8405
8406         fn get_chain_hashes(&self) -> Option<Vec<ChainHash>> {
8407                 Some(vec![self.chain_hash])
8408         }
8409
8410         fn handle_tx_add_input(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxAddInput) {
8411                 let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
8412                         "Dual-funded channels not supported".to_owned(),
8413                          msg.channel_id.clone())), *counterparty_node_id);
8414         }
8415
8416         fn handle_tx_add_output(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxAddOutput) {
8417                 let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
8418                         "Dual-funded channels not supported".to_owned(),
8419                          msg.channel_id.clone())), *counterparty_node_id);
8420         }
8421
8422         fn handle_tx_remove_input(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxRemoveInput) {
8423                 let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
8424                         "Dual-funded channels not supported".to_owned(),
8425                          msg.channel_id.clone())), *counterparty_node_id);
8426         }
8427
8428         fn handle_tx_remove_output(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxRemoveOutput) {
8429                 let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
8430                         "Dual-funded channels not supported".to_owned(),
8431                          msg.channel_id.clone())), *counterparty_node_id);
8432         }
8433
8434         fn handle_tx_complete(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxComplete) {
8435                 let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
8436                         "Dual-funded channels not supported".to_owned(),
8437                          msg.channel_id.clone())), *counterparty_node_id);
8438         }
8439
8440         fn handle_tx_signatures(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxSignatures) {
8441                 let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
8442                         "Dual-funded channels not supported".to_owned(),
8443                          msg.channel_id.clone())), *counterparty_node_id);
8444         }
8445
8446         fn handle_tx_init_rbf(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxInitRbf) {
8447                 let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
8448                         "Dual-funded channels not supported".to_owned(),
8449                          msg.channel_id.clone())), *counterparty_node_id);
8450         }
8451
8452         fn handle_tx_ack_rbf(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxAckRbf) {
8453                 let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
8454                         "Dual-funded channels not supported".to_owned(),
8455                          msg.channel_id.clone())), *counterparty_node_id);
8456         }
8457
8458         fn handle_tx_abort(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxAbort) {
8459                 let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
8460                         "Dual-funded channels not supported".to_owned(),
8461                          msg.channel_id.clone())), *counterparty_node_id);
8462         }
8463 }
8464
8465 /// Fetches the set of [`NodeFeatures`] flags which are provided by or required by
8466 /// [`ChannelManager`].
8467 pub(crate) fn provided_node_features(config: &UserConfig) -> NodeFeatures {
8468         let mut node_features = provided_init_features(config).to_context();
8469         node_features.set_keysend_optional();
8470         node_features
8471 }
8472
8473 /// Fetches the set of [`Bolt11InvoiceFeatures`] flags which are provided by or required by
8474 /// [`ChannelManager`].
8475 ///
8476 /// Note that the invoice feature flags can vary depending on if the invoice is a "phantom invoice"
8477 /// or not. Thus, this method is not public.
8478 #[cfg(any(feature = "_test_utils", test))]
8479 pub(crate) fn provided_invoice_features(config: &UserConfig) -> Bolt11InvoiceFeatures {
8480         provided_init_features(config).to_context()
8481 }
8482
8483 /// Fetches the set of [`ChannelFeatures`] flags which are provided by or required by
8484 /// [`ChannelManager`].
8485 pub(crate) fn provided_channel_features(config: &UserConfig) -> ChannelFeatures {
8486         provided_init_features(config).to_context()
8487 }
8488
8489 /// Fetches the set of [`ChannelTypeFeatures`] flags which are provided by or required by
8490 /// [`ChannelManager`].
8491 pub(crate) fn provided_channel_type_features(config: &UserConfig) -> ChannelTypeFeatures {
8492         ChannelTypeFeatures::from_init(&provided_init_features(config))
8493 }
8494
8495 /// Fetches the set of [`InitFeatures`] flags which are provided by or required by
8496 /// [`ChannelManager`].
8497 pub fn provided_init_features(config: &UserConfig) -> InitFeatures {
8498         // Note that if new features are added here which other peers may (eventually) require, we
8499         // should also add the corresponding (optional) bit to the [`ChannelMessageHandler`] impl for
8500         // [`ErroringMessageHandler`].
8501         let mut features = InitFeatures::empty();
8502         features.set_data_loss_protect_required();
8503         features.set_upfront_shutdown_script_optional();
8504         features.set_variable_length_onion_required();
8505         features.set_static_remote_key_required();
8506         features.set_payment_secret_required();
8507         features.set_basic_mpp_optional();
8508         features.set_wumbo_optional();
8509         features.set_shutdown_any_segwit_optional();
8510         features.set_channel_type_optional();
8511         features.set_scid_privacy_optional();
8512         features.set_zero_conf_optional();
8513         if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
8514                 features.set_anchors_zero_fee_htlc_tx_optional();
8515         }
8516         features
8517 }
8518
8519 const SERIALIZATION_VERSION: u8 = 1;
8520 const MIN_SERIALIZATION_VERSION: u8 = 1;
8521
8522 impl_writeable_tlv_based!(CounterpartyForwardingInfo, {
8523         (2, fee_base_msat, required),
8524         (4, fee_proportional_millionths, required),
8525         (6, cltv_expiry_delta, required),
8526 });
8527
8528 impl_writeable_tlv_based!(ChannelCounterparty, {
8529         (2, node_id, required),
8530         (4, features, required),
8531         (6, unspendable_punishment_reserve, required),
8532         (8, forwarding_info, option),
8533         (9, outbound_htlc_minimum_msat, option),
8534         (11, outbound_htlc_maximum_msat, option),
8535 });
8536
8537 impl Writeable for ChannelDetails {
8538         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
8539                 // `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
8540                 // versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
8541                 let user_channel_id_low = self.user_channel_id as u64;
8542                 let user_channel_id_high_opt = Some((self.user_channel_id >> 64) as u64);
8543                 write_tlv_fields!(writer, {
8544                         (1, self.inbound_scid_alias, option),
8545                         (2, self.channel_id, required),
8546                         (3, self.channel_type, option),
8547                         (4, self.counterparty, required),
8548                         (5, self.outbound_scid_alias, option),
8549                         (6, self.funding_txo, option),
8550                         (7, self.config, option),
8551                         (8, self.short_channel_id, option),
8552                         (9, self.confirmations, option),
8553                         (10, self.channel_value_satoshis, required),
8554                         (12, self.unspendable_punishment_reserve, option),
8555                         (14, user_channel_id_low, required),
8556                         (16, self.balance_msat, required),
8557                         (18, self.outbound_capacity_msat, required),
8558                         (19, self.next_outbound_htlc_limit_msat, required),
8559                         (20, self.inbound_capacity_msat, required),
8560                         (21, self.next_outbound_htlc_minimum_msat, required),
8561                         (22, self.confirmations_required, option),
8562                         (24, self.force_close_spend_delay, option),
8563                         (26, self.is_outbound, required),
8564                         (28, self.is_channel_ready, required),
8565                         (30, self.is_usable, required),
8566                         (32, self.is_public, required),
8567                         (33, self.inbound_htlc_minimum_msat, option),
8568                         (35, self.inbound_htlc_maximum_msat, option),
8569                         (37, user_channel_id_high_opt, option),
8570                         (39, self.feerate_sat_per_1000_weight, option),
8571                         (41, self.channel_shutdown_state, option),
8572                 });
8573                 Ok(())
8574         }
8575 }
8576
8577 impl Readable for ChannelDetails {
8578         fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
8579                 _init_and_read_len_prefixed_tlv_fields!(reader, {
8580                         (1, inbound_scid_alias, option),
8581                         (2, channel_id, required),
8582                         (3, channel_type, option),
8583                         (4, counterparty, required),
8584                         (5, outbound_scid_alias, option),
8585                         (6, funding_txo, option),
8586                         (7, config, option),
8587                         (8, short_channel_id, option),
8588                         (9, confirmations, option),
8589                         (10, channel_value_satoshis, required),
8590                         (12, unspendable_punishment_reserve, option),
8591                         (14, user_channel_id_low, required),
8592                         (16, balance_msat, required),
8593                         (18, outbound_capacity_msat, required),
8594                         // Note that by the time we get past the required read above, outbound_capacity_msat will be
8595                         // filled in, so we can safely unwrap it here.
8596                         (19, next_outbound_htlc_limit_msat, (default_value, outbound_capacity_msat.0.unwrap() as u64)),
8597                         (20, inbound_capacity_msat, required),
8598                         (21, next_outbound_htlc_minimum_msat, (default_value, 0)),
8599                         (22, confirmations_required, option),
8600                         (24, force_close_spend_delay, option),
8601                         (26, is_outbound, required),
8602                         (28, is_channel_ready, required),
8603                         (30, is_usable, required),
8604                         (32, is_public, required),
8605                         (33, inbound_htlc_minimum_msat, option),
8606                         (35, inbound_htlc_maximum_msat, option),
8607                         (37, user_channel_id_high_opt, option),
8608                         (39, feerate_sat_per_1000_weight, option),
8609                         (41, channel_shutdown_state, option),
8610                 });
8611
8612                 // `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
8613                 // versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
8614                 let user_channel_id_low: u64 = user_channel_id_low.0.unwrap();
8615                 let user_channel_id = user_channel_id_low as u128 +
8616                         ((user_channel_id_high_opt.unwrap_or(0 as u64) as u128) << 64);
8617
8618                 Ok(Self {
8619                         inbound_scid_alias,
8620                         channel_id: channel_id.0.unwrap(),
8621                         channel_type,
8622                         counterparty: counterparty.0.unwrap(),
8623                         outbound_scid_alias,
8624                         funding_txo,
8625                         config,
8626                         short_channel_id,
8627                         channel_value_satoshis: channel_value_satoshis.0.unwrap(),
8628                         unspendable_punishment_reserve,
8629                         user_channel_id,
8630                         balance_msat: balance_msat.0.unwrap(),
8631                         outbound_capacity_msat: outbound_capacity_msat.0.unwrap(),
8632                         next_outbound_htlc_limit_msat: next_outbound_htlc_limit_msat.0.unwrap(),
8633                         next_outbound_htlc_minimum_msat: next_outbound_htlc_minimum_msat.0.unwrap(),
8634                         inbound_capacity_msat: inbound_capacity_msat.0.unwrap(),
8635                         confirmations_required,
8636                         confirmations,
8637                         force_close_spend_delay,
8638                         is_outbound: is_outbound.0.unwrap(),
8639                         is_channel_ready: is_channel_ready.0.unwrap(),
8640                         is_usable: is_usable.0.unwrap(),
8641                         is_public: is_public.0.unwrap(),
8642                         inbound_htlc_minimum_msat,
8643                         inbound_htlc_maximum_msat,
8644                         feerate_sat_per_1000_weight,
8645                         channel_shutdown_state,
8646                 })
8647         }
8648 }
8649
8650 impl_writeable_tlv_based!(PhantomRouteHints, {
8651         (2, channels, required_vec),
8652         (4, phantom_scid, required),
8653         (6, real_node_pubkey, required),
8654 });
8655
8656 impl_writeable_tlv_based_enum!(PendingHTLCRouting,
8657         (0, Forward) => {
8658                 (0, onion_packet, required),
8659                 (2, short_channel_id, required),
8660         },
8661         (1, Receive) => {
8662                 (0, payment_data, required),
8663                 (1, phantom_shared_secret, option),
8664                 (2, incoming_cltv_expiry, required),
8665                 (3, payment_metadata, option),
8666                 (5, custom_tlvs, optional_vec),
8667         },
8668         (2, ReceiveKeysend) => {
8669                 (0, payment_preimage, required),
8670                 (2, incoming_cltv_expiry, required),
8671                 (3, payment_metadata, option),
8672                 (4, payment_data, option), // Added in 0.0.116
8673                 (5, custom_tlvs, optional_vec),
8674         },
8675 ;);
8676
8677 impl_writeable_tlv_based!(PendingHTLCInfo, {
8678         (0, routing, required),
8679         (2, incoming_shared_secret, required),
8680         (4, payment_hash, required),
8681         (6, outgoing_amt_msat, required),
8682         (8, outgoing_cltv_value, required),
8683         (9, incoming_amt_msat, option),
8684         (10, skimmed_fee_msat, option),
8685 });
8686
8687
8688 impl Writeable for HTLCFailureMsg {
8689         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
8690                 match self {
8691                         HTLCFailureMsg::Relay(msgs::UpdateFailHTLC { channel_id, htlc_id, reason }) => {
8692                                 0u8.write(writer)?;
8693                                 channel_id.write(writer)?;
8694                                 htlc_id.write(writer)?;
8695                                 reason.write(writer)?;
8696                         },
8697                         HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
8698                                 channel_id, htlc_id, sha256_of_onion, failure_code
8699                         }) => {
8700                                 1u8.write(writer)?;
8701                                 channel_id.write(writer)?;
8702                                 htlc_id.write(writer)?;
8703                                 sha256_of_onion.write(writer)?;
8704                                 failure_code.write(writer)?;
8705                         },
8706                 }
8707                 Ok(())
8708         }
8709 }
8710
8711 impl Readable for HTLCFailureMsg {
8712         fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
8713                 let id: u8 = Readable::read(reader)?;
8714                 match id {
8715                         0 => {
8716                                 Ok(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
8717                                         channel_id: Readable::read(reader)?,
8718                                         htlc_id: Readable::read(reader)?,
8719                                         reason: Readable::read(reader)?,
8720                                 }))
8721                         },
8722                         1 => {
8723                                 Ok(HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
8724                                         channel_id: Readable::read(reader)?,
8725                                         htlc_id: Readable::read(reader)?,
8726                                         sha256_of_onion: Readable::read(reader)?,
8727                                         failure_code: Readable::read(reader)?,
8728                                 }))
8729                         },
8730                         // In versions prior to 0.0.101, HTLCFailureMsg objects were written with type 0 or 1 but
8731                         // weren't length-prefixed and thus didn't support reading the TLV stream suffix of the network
8732                         // messages contained in the variants.
8733                         // In version 0.0.101, support for reading the variants with these types was added, and
8734                         // we should migrate to writing these variants when UpdateFailHTLC or
8735                         // UpdateFailMalformedHTLC get TLV fields.
8736                         2 => {
8737                                 let length: BigSize = Readable::read(reader)?;
8738                                 let mut s = FixedLengthReader::new(reader, length.0);
8739                                 let res = Readable::read(&mut s)?;
8740                                 s.eat_remaining()?; // Return ShortRead if there's actually not enough bytes
8741                                 Ok(HTLCFailureMsg::Relay(res))
8742                         },
8743                         3 => {
8744                                 let length: BigSize = Readable::read(reader)?;
8745                                 let mut s = FixedLengthReader::new(reader, length.0);
8746                                 let res = Readable::read(&mut s)?;
8747                                 s.eat_remaining()?; // Return ShortRead if there's actually not enough bytes
8748                                 Ok(HTLCFailureMsg::Malformed(res))
8749                         },
8750                         _ => Err(DecodeError::UnknownRequiredFeature),
8751                 }
8752         }
8753 }
8754
8755 impl_writeable_tlv_based_enum!(PendingHTLCStatus, ;
8756         (0, Forward),
8757         (1, Fail),
8758 );
8759
8760 impl_writeable_tlv_based!(HTLCPreviousHopData, {
8761         (0, short_channel_id, required),
8762         (1, phantom_shared_secret, option),
8763         (2, outpoint, required),
8764         (4, htlc_id, required),
8765         (6, incoming_packet_shared_secret, required),
8766         (7, user_channel_id, option),
8767 });
8768
8769 impl Writeable for ClaimableHTLC {
8770         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
8771                 let (payment_data, keysend_preimage) = match &self.onion_payload {
8772                         OnionPayload::Invoice { _legacy_hop_data } => (_legacy_hop_data.as_ref(), None),
8773                         OnionPayload::Spontaneous(preimage) => (None, Some(preimage)),
8774                 };
8775                 write_tlv_fields!(writer, {
8776                         (0, self.prev_hop, required),
8777                         (1, self.total_msat, required),
8778                         (2, self.value, required),
8779                         (3, self.sender_intended_value, required),
8780                         (4, payment_data, option),
8781                         (5, self.total_value_received, option),
8782                         (6, self.cltv_expiry, required),
8783                         (8, keysend_preimage, option),
8784                         (10, self.counterparty_skimmed_fee_msat, option),
8785                 });
8786                 Ok(())
8787         }
8788 }
8789
8790 impl Readable for ClaimableHTLC {
8791         fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
8792                 _init_and_read_len_prefixed_tlv_fields!(reader, {
8793                         (0, prev_hop, required),
8794                         (1, total_msat, option),
8795                         (2, value_ser, required),
8796                         (3, sender_intended_value, option),
8797                         (4, payment_data_opt, option),
8798                         (5, total_value_received, option),
8799                         (6, cltv_expiry, required),
8800                         (8, keysend_preimage, option),
8801                         (10, counterparty_skimmed_fee_msat, option),
8802                 });
8803                 let payment_data: Option<msgs::FinalOnionHopData> = payment_data_opt;
8804                 let value = value_ser.0.unwrap();
8805                 let onion_payload = match keysend_preimage {
8806                         Some(p) => {
8807                                 if payment_data.is_some() {
8808                                         return Err(DecodeError::InvalidValue)
8809                                 }
8810                                 if total_msat.is_none() {
8811                                         total_msat = Some(value);
8812                                 }
8813                                 OnionPayload::Spontaneous(p)
8814                         },
8815                         None => {
8816                                 if total_msat.is_none() {
8817                                         if payment_data.is_none() {
8818                                                 return Err(DecodeError::InvalidValue)
8819                                         }
8820                                         total_msat = Some(payment_data.as_ref().unwrap().total_msat);
8821                                 }
8822                                 OnionPayload::Invoice { _legacy_hop_data: payment_data }
8823                         },
8824                 };
8825                 Ok(Self {
8826                         prev_hop: prev_hop.0.unwrap(),
8827                         timer_ticks: 0,
8828                         value,
8829                         sender_intended_value: sender_intended_value.unwrap_or(value),
8830                         total_value_received,
8831                         total_msat: total_msat.unwrap(),
8832                         onion_payload,
8833                         cltv_expiry: cltv_expiry.0.unwrap(),
8834                         counterparty_skimmed_fee_msat,
8835                 })
8836         }
8837 }
8838
8839 impl Readable for HTLCSource {
8840         fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
8841                 let id: u8 = Readable::read(reader)?;
8842                 match id {
8843                         0 => {
8844                                 let mut session_priv: crate::util::ser::RequiredWrapper<SecretKey> = crate::util::ser::RequiredWrapper(None);
8845                                 let mut first_hop_htlc_msat: u64 = 0;
8846                                 let mut path_hops = Vec::new();
8847                                 let mut payment_id = None;
8848                                 let mut payment_params: Option<PaymentParameters> = None;
8849                                 let mut blinded_tail: Option<BlindedTail> = None;
8850                                 read_tlv_fields!(reader, {
8851                                         (0, session_priv, required),
8852                                         (1, payment_id, option),
8853                                         (2, first_hop_htlc_msat, required),
8854                                         (4, path_hops, required_vec),
8855                                         (5, payment_params, (option: ReadableArgs, 0)),
8856                                         (6, blinded_tail, option),
8857                                 });
8858                                 if payment_id.is_none() {
8859                                         // For backwards compat, if there was no payment_id written, use the session_priv bytes
8860                                         // instead.
8861                                         payment_id = Some(PaymentId(*session_priv.0.unwrap().as_ref()));
8862                                 }
8863                                 let path = Path { hops: path_hops, blinded_tail };
8864                                 if path.hops.len() == 0 {
8865                                         return Err(DecodeError::InvalidValue);
8866                                 }
8867                                 if let Some(params) = payment_params.as_mut() {
8868                                         if let Payee::Clear { ref mut final_cltv_expiry_delta, .. } = params.payee {
8869                                                 if final_cltv_expiry_delta == &0 {
8870                                                         *final_cltv_expiry_delta = path.final_cltv_expiry_delta().ok_or(DecodeError::InvalidValue)?;
8871                                                 }
8872                                         }
8873                                 }
8874                                 Ok(HTLCSource::OutboundRoute {
8875                                         session_priv: session_priv.0.unwrap(),
8876                                         first_hop_htlc_msat,
8877                                         path,
8878                                         payment_id: payment_id.unwrap(),
8879                                 })
8880                         }
8881                         1 => Ok(HTLCSource::PreviousHopData(Readable::read(reader)?)),
8882                         _ => Err(DecodeError::UnknownRequiredFeature),
8883                 }
8884         }
8885 }
8886
8887 impl Writeable for HTLCSource {
8888         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), crate::io::Error> {
8889                 match self {
8890                         HTLCSource::OutboundRoute { ref session_priv, ref first_hop_htlc_msat, ref path, payment_id } => {
8891                                 0u8.write(writer)?;
8892                                 let payment_id_opt = Some(payment_id);
8893                                 write_tlv_fields!(writer, {
8894                                         (0, session_priv, required),
8895                                         (1, payment_id_opt, option),
8896                                         (2, first_hop_htlc_msat, required),
8897                                         // 3 was previously used to write a PaymentSecret for the payment.
8898                                         (4, path.hops, required_vec),
8899                                         (5, None::<PaymentParameters>, option), // payment_params in LDK versions prior to 0.0.115
8900                                         (6, path.blinded_tail, option),
8901                                  });
8902                         }
8903                         HTLCSource::PreviousHopData(ref field) => {
8904                                 1u8.write(writer)?;
8905                                 field.write(writer)?;
8906                         }
8907                 }
8908                 Ok(())
8909         }
8910 }
8911
8912 impl_writeable_tlv_based!(PendingAddHTLCInfo, {
8913         (0, forward_info, required),
8914         (1, prev_user_channel_id, (default_value, 0)),
8915         (2, prev_short_channel_id, required),
8916         (4, prev_htlc_id, required),
8917         (6, prev_funding_outpoint, required),
8918 });
8919
8920 impl_writeable_tlv_based_enum!(HTLCForwardInfo,
8921         (1, FailHTLC) => {
8922                 (0, htlc_id, required),
8923                 (2, err_packet, required),
8924         };
8925         (0, AddHTLC)
8926 );
8927
8928 impl_writeable_tlv_based!(PendingInboundPayment, {
8929         (0, payment_secret, required),
8930         (2, expiry_time, required),
8931         (4, user_payment_id, required),
8932         (6, payment_preimage, required),
8933         (8, min_value_msat, required),
8934 });
8935
8936 impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> Writeable for ChannelManager<M, T, ES, NS, SP, F, R, L>
8937 where
8938         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
8939         T::Target: BroadcasterInterface,
8940         ES::Target: EntropySource,
8941         NS::Target: NodeSigner,
8942         SP::Target: SignerProvider,
8943         F::Target: FeeEstimator,
8944         R::Target: Router,
8945         L::Target: Logger,
8946 {
8947         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
8948                 let _consistency_lock = self.total_consistency_lock.write().unwrap();
8949
8950                 write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
8951
8952                 self.chain_hash.write(writer)?;
8953                 {
8954                         let best_block = self.best_block.read().unwrap();
8955                         best_block.height().write(writer)?;
8956                         best_block.block_hash().write(writer)?;
8957                 }
8958
8959                 let mut serializable_peer_count: u64 = 0;
8960                 {
8961                         let per_peer_state = self.per_peer_state.read().unwrap();
8962                         let mut number_of_funded_channels = 0;
8963                         for (_, peer_state_mutex) in per_peer_state.iter() {
8964                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
8965                                 let peer_state = &mut *peer_state_lock;
8966                                 if !peer_state.ok_to_remove(false) {
8967                                         serializable_peer_count += 1;
8968                                 }
8969
8970                                 number_of_funded_channels += peer_state.channel_by_id.iter().filter(
8971                                         |(_, phase)| if let ChannelPhase::Funded(chan) = phase { chan.context.is_funding_broadcast() } else { false }
8972                                 ).count();
8973                         }
8974
8975                         (number_of_funded_channels as u64).write(writer)?;
8976
8977                         for (_, peer_state_mutex) in per_peer_state.iter() {
8978                                 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
8979                                 let peer_state = &mut *peer_state_lock;
8980                                 for channel in peer_state.channel_by_id.iter().filter_map(
8981                                         |(_, phase)| if let ChannelPhase::Funded(channel) = phase {
8982                                                 if channel.context.is_funding_broadcast() { Some(channel) } else { None }
8983                                         } else { None }
8984                                 ) {
8985                                         channel.write(writer)?;
8986                                 }
8987                         }
8988                 }
8989
8990                 {
8991                         let forward_htlcs = self.forward_htlcs.lock().unwrap();
8992                         (forward_htlcs.len() as u64).write(writer)?;
8993                         for (short_channel_id, pending_forwards) in forward_htlcs.iter() {
8994                                 short_channel_id.write(writer)?;
8995                                 (pending_forwards.len() as u64).write(writer)?;
8996                                 for forward in pending_forwards {
8997                                         forward.write(writer)?;
8998                                 }
8999                         }
9000                 }
9001
9002                 let per_peer_state = self.per_peer_state.write().unwrap();
9003
9004                 let pending_inbound_payments = self.pending_inbound_payments.lock().unwrap();
9005                 let claimable_payments = self.claimable_payments.lock().unwrap();
9006                 let pending_outbound_payments = self.pending_outbound_payments.pending_outbound_payments.lock().unwrap();
9007
9008                 let mut htlc_purposes: Vec<&events::PaymentPurpose> = Vec::new();
9009                 let mut htlc_onion_fields: Vec<&_> = Vec::new();
9010                 (claimable_payments.claimable_payments.len() as u64).write(writer)?;
9011                 for (payment_hash, payment) in claimable_payments.claimable_payments.iter() {
9012                         payment_hash.write(writer)?;
9013                         (payment.htlcs.len() as u64).write(writer)?;
9014                         for htlc in payment.htlcs.iter() {
9015                                 htlc.write(writer)?;
9016                         }
9017                         htlc_purposes.push(&payment.purpose);
9018                         htlc_onion_fields.push(&payment.onion_fields);
9019                 }
9020
9021                 let mut monitor_update_blocked_actions_per_peer = None;
9022                 let mut peer_states = Vec::new();
9023                 for (_, peer_state_mutex) in per_peer_state.iter() {
9024                         // Because we're holding the owning `per_peer_state` write lock here there's no chance
9025                         // of a lockorder violation deadlock - no other thread can be holding any
9026                         // per_peer_state lock at all.
9027                         peer_states.push(peer_state_mutex.unsafe_well_ordered_double_lock_self());
9028                 }
9029
9030                 (serializable_peer_count).write(writer)?;
9031                 for ((peer_pubkey, _), peer_state) in per_peer_state.iter().zip(peer_states.iter()) {
9032                         // Peers which we have no channels to should be dropped once disconnected. As we
9033                         // disconnect all peers when shutting down and serializing the ChannelManager, we
9034                         // consider all peers as disconnected here. There's therefore no need write peers with
9035                         // no channels.
9036                         if !peer_state.ok_to_remove(false) {
9037                                 peer_pubkey.write(writer)?;
9038                                 peer_state.latest_features.write(writer)?;
9039                                 if !peer_state.monitor_update_blocked_actions.is_empty() {
9040                                         monitor_update_blocked_actions_per_peer
9041                                                 .get_or_insert_with(Vec::new)
9042                                                 .push((*peer_pubkey, &peer_state.monitor_update_blocked_actions));
9043                                 }
9044                         }
9045                 }
9046
9047                 let events = self.pending_events.lock().unwrap();
9048                 // LDK versions prior to 0.0.115 don't support post-event actions, thus if there's no
9049                 // actions at all, skip writing the required TLV. Otherwise, pre-0.0.115 versions will
9050                 // refuse to read the new ChannelManager.
9051                 let events_not_backwards_compatible = events.iter().any(|(_, action)| action.is_some());
9052                 if events_not_backwards_compatible {
9053                         // If we're gonna write a even TLV that will overwrite our events anyway we might as
9054                         // well save the space and not write any events here.
9055                         0u64.write(writer)?;
9056                 } else {
9057                         (events.len() as u64).write(writer)?;
9058                         for (event, _) in events.iter() {
9059                                 event.write(writer)?;
9060                         }
9061                 }
9062
9063                 // LDK versions prior to 0.0.116 wrote the `pending_background_events`
9064                 // `MonitorUpdateRegeneratedOnStartup`s here, however there was never a reason to do so -
9065                 // the closing monitor updates were always effectively replayed on startup (either directly
9066                 // by calling `broadcast_latest_holder_commitment_txn` on a `ChannelMonitor` during
9067                 // deserialization or, in 0.0.115, by regenerating the monitor update itself).
9068                 0u64.write(writer)?;
9069
9070                 // Prior to 0.0.111 we tracked node_announcement serials here, however that now happens in
9071                 // `PeerManager`, and thus we simply write the `highest_seen_timestamp` twice, which is
9072                 // likely to be identical.
9073                 (self.highest_seen_timestamp.load(Ordering::Acquire) as u32).write(writer)?;
9074                 (self.highest_seen_timestamp.load(Ordering::Acquire) as u32).write(writer)?;
9075
9076                 (pending_inbound_payments.len() as u64).write(writer)?;
9077                 for (hash, pending_payment) in pending_inbound_payments.iter() {
9078                         hash.write(writer)?;
9079                         pending_payment.write(writer)?;
9080                 }
9081
9082                 // For backwards compat, write the session privs and their total length.
9083                 let mut num_pending_outbounds_compat: u64 = 0;
9084                 for (_, outbound) in pending_outbound_payments.iter() {
9085                         if !outbound.is_fulfilled() && !outbound.abandoned() {
9086                                 num_pending_outbounds_compat += outbound.remaining_parts() as u64;
9087                         }
9088                 }
9089                 num_pending_outbounds_compat.write(writer)?;
9090                 for (_, outbound) in pending_outbound_payments.iter() {
9091                         match outbound {
9092                                 PendingOutboundPayment::Legacy { session_privs } |
9093                                 PendingOutboundPayment::Retryable { session_privs, .. } => {
9094                                         for session_priv in session_privs.iter() {
9095                                                 session_priv.write(writer)?;
9096                                         }
9097                                 }
9098                                 PendingOutboundPayment::AwaitingInvoice { .. } => {},
9099                                 PendingOutboundPayment::InvoiceReceived { .. } => {},
9100                                 PendingOutboundPayment::Fulfilled { .. } => {},
9101                                 PendingOutboundPayment::Abandoned { .. } => {},
9102                         }
9103                 }
9104
9105                 // Encode without retry info for 0.0.101 compatibility.
9106                 let mut pending_outbound_payments_no_retry: HashMap<PaymentId, HashSet<[u8; 32]>> = HashMap::new();
9107                 for (id, outbound) in pending_outbound_payments.iter() {
9108                         match outbound {
9109                                 PendingOutboundPayment::Legacy { session_privs } |
9110                                 PendingOutboundPayment::Retryable { session_privs, .. } => {
9111                                         pending_outbound_payments_no_retry.insert(*id, session_privs.clone());
9112                                 },
9113                                 _ => {},
9114                         }
9115                 }
9116
9117                 let mut pending_intercepted_htlcs = None;
9118                 let our_pending_intercepts = self.pending_intercepted_htlcs.lock().unwrap();
9119                 if our_pending_intercepts.len() != 0 {
9120                         pending_intercepted_htlcs = Some(our_pending_intercepts);
9121                 }
9122
9123                 let mut pending_claiming_payments = Some(&claimable_payments.pending_claiming_payments);
9124                 if pending_claiming_payments.as_ref().unwrap().is_empty() {
9125                         // LDK versions prior to 0.0.113 do not know how to read the pending claimed payments
9126                         // map. Thus, if there are no entries we skip writing a TLV for it.
9127                         pending_claiming_payments = None;
9128                 }
9129
9130                 let mut in_flight_monitor_updates: Option<HashMap<(&PublicKey, &OutPoint), &Vec<ChannelMonitorUpdate>>> = None;
9131                 for ((counterparty_id, _), peer_state) in per_peer_state.iter().zip(peer_states.iter()) {
9132                         for (funding_outpoint, updates) in peer_state.in_flight_monitor_updates.iter() {
9133                                 if !updates.is_empty() {
9134                                         if in_flight_monitor_updates.is_none() { in_flight_monitor_updates = Some(HashMap::new()); }
9135                                         in_flight_monitor_updates.as_mut().unwrap().insert((counterparty_id, funding_outpoint), updates);
9136                                 }
9137                         }
9138                 }
9139
9140                 write_tlv_fields!(writer, {
9141                         (1, pending_outbound_payments_no_retry, required),
9142                         (2, pending_intercepted_htlcs, option),
9143                         (3, pending_outbound_payments, required),
9144                         (4, pending_claiming_payments, option),
9145                         (5, self.our_network_pubkey, required),
9146                         (6, monitor_update_blocked_actions_per_peer, option),
9147                         (7, self.fake_scid_rand_bytes, required),
9148                         (8, if events_not_backwards_compatible { Some(&*events) } else { None }, option),
9149                         (9, htlc_purposes, required_vec),
9150                         (10, in_flight_monitor_updates, option),
9151                         (11, self.probing_cookie_secret, required),
9152                         (13, htlc_onion_fields, optional_vec),
9153                 });
9154
9155                 Ok(())
9156         }
9157 }
9158
9159 impl Writeable for VecDeque<(Event, Option<EventCompletionAction>)> {
9160         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
9161                 (self.len() as u64).write(w)?;
9162                 for (event, action) in self.iter() {
9163                         event.write(w)?;
9164                         action.write(w)?;
9165                         #[cfg(debug_assertions)] {
9166                                 // Events are MaybeReadable, in some cases indicating that they shouldn't actually
9167                                 // be persisted and are regenerated on restart. However, if such an event has a
9168                                 // post-event-handling action we'll write nothing for the event and would have to
9169                                 // either forget the action or fail on deserialization (which we do below). Thus,
9170                                 // check that the event is sane here.
9171                                 let event_encoded = event.encode();
9172                                 let event_read: Option<Event> =
9173                                         MaybeReadable::read(&mut &event_encoded[..]).unwrap();
9174                                 if action.is_some() { assert!(event_read.is_some()); }
9175                         }
9176                 }
9177                 Ok(())
9178         }
9179 }
9180 impl Readable for VecDeque<(Event, Option<EventCompletionAction>)> {
9181         fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
9182                 let len: u64 = Readable::read(reader)?;
9183                 const MAX_ALLOC_SIZE: u64 = 1024 * 16;
9184                 let mut events: Self = VecDeque::with_capacity(cmp::min(
9185                         MAX_ALLOC_SIZE/mem::size_of::<(events::Event, Option<EventCompletionAction>)>() as u64,
9186                         len) as usize);
9187                 for _ in 0..len {
9188                         let ev_opt = MaybeReadable::read(reader)?;
9189                         let action = Readable::read(reader)?;
9190                         if let Some(ev) = ev_opt {
9191                                 events.push_back((ev, action));
9192                         } else if action.is_some() {
9193                                 return Err(DecodeError::InvalidValue);
9194                         }
9195                 }
9196                 Ok(events)
9197         }
9198 }
9199
9200 impl_writeable_tlv_based_enum!(ChannelShutdownState,
9201         (0, NotShuttingDown) => {},
9202         (2, ShutdownInitiated) => {},
9203         (4, ResolvingHTLCs) => {},
9204         (6, NegotiatingClosingFee) => {},
9205         (8, ShutdownComplete) => {}, ;
9206 );
9207
9208 /// Arguments for the creation of a ChannelManager that are not deserialized.
9209 ///
9210 /// At a high-level, the process for deserializing a ChannelManager and resuming normal operation
9211 /// is:
9212 /// 1) Deserialize all stored [`ChannelMonitor`]s.
9213 /// 2) Deserialize the [`ChannelManager`] by filling in this struct and calling:
9214 ///    `<(BlockHash, ChannelManager)>::read(reader, args)`
9215 ///    This may result in closing some channels if the [`ChannelMonitor`] is newer than the stored
9216 ///    [`ChannelManager`] state to ensure no loss of funds. Thus, transactions may be broadcasted.
9217 /// 3) If you are not fetching full blocks, register all relevant [`ChannelMonitor`] outpoints the
9218 ///    same way you would handle a [`chain::Filter`] call using
9219 ///    [`ChannelMonitor::get_outputs_to_watch`] and [`ChannelMonitor::get_funding_txo`].
9220 /// 4) Reconnect blocks on your [`ChannelMonitor`]s.
9221 /// 5) Disconnect/connect blocks on the [`ChannelManager`].
9222 /// 6) Re-persist the [`ChannelMonitor`]s to ensure the latest state is on disk.
9223 ///    Note that if you're using a [`ChainMonitor`] for your [`chain::Watch`] implementation, you
9224 ///    will likely accomplish this as a side-effect of calling [`chain::Watch::watch_channel`] in
9225 ///    the next step.
9226 /// 7) Move the [`ChannelMonitor`]s into your local [`chain::Watch`]. If you're using a
9227 ///    [`ChainMonitor`], this is done by calling [`chain::Watch::watch_channel`].
9228 ///
9229 /// Note that the ordering of #4-7 is not of importance, however all four must occur before you
9230 /// call any other methods on the newly-deserialized [`ChannelManager`].
9231 ///
9232 /// Note that because some channels may be closed during deserialization, it is critical that you
9233 /// always deserialize only the latest version of a ChannelManager and ChannelMonitors available to
9234 /// you. If you deserialize an old ChannelManager (during which force-closure transactions may be
9235 /// broadcast), and then later deserialize a newer version of the same ChannelManager (which will
9236 /// not force-close the same channels but consider them live), you may end up revoking a state for
9237 /// which you've already broadcasted the transaction.
9238 ///
9239 /// [`ChainMonitor`]: crate::chain::chainmonitor::ChainMonitor
9240 pub struct ChannelManagerReadArgs<'a, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>
9241 where
9242         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
9243         T::Target: BroadcasterInterface,
9244         ES::Target: EntropySource,
9245         NS::Target: NodeSigner,
9246         SP::Target: SignerProvider,
9247         F::Target: FeeEstimator,
9248         R::Target: Router,
9249         L::Target: Logger,
9250 {
9251         /// A cryptographically secure source of entropy.
9252         pub entropy_source: ES,
9253
9254         /// A signer that is able to perform node-scoped cryptographic operations.
9255         pub node_signer: NS,
9256
9257         /// The keys provider which will give us relevant keys. Some keys will be loaded during
9258         /// deserialization and KeysInterface::read_chan_signer will be used to read per-Channel
9259         /// signing data.
9260         pub signer_provider: SP,
9261
9262         /// The fee_estimator for use in the ChannelManager in the future.
9263         ///
9264         /// No calls to the FeeEstimator will be made during deserialization.
9265         pub fee_estimator: F,
9266         /// The chain::Watch for use in the ChannelManager in the future.
9267         ///
9268         /// No calls to the chain::Watch will be made during deserialization. It is assumed that
9269         /// you have deserialized ChannelMonitors separately and will add them to your
9270         /// chain::Watch after deserializing this ChannelManager.
9271         pub chain_monitor: M,
9272
9273         /// The BroadcasterInterface which will be used in the ChannelManager in the future and may be
9274         /// used to broadcast the latest local commitment transactions of channels which must be
9275         /// force-closed during deserialization.
9276         pub tx_broadcaster: T,
9277         /// The router which will be used in the ChannelManager in the future for finding routes
9278         /// on-the-fly for trampoline payments. Absent in private nodes that don't support forwarding.
9279         ///
9280         /// No calls to the router will be made during deserialization.
9281         pub router: R,
9282         /// The Logger for use in the ChannelManager and which may be used to log information during
9283         /// deserialization.
9284         pub logger: L,
9285         /// Default settings used for new channels. Any existing channels will continue to use the
9286         /// runtime settings which were stored when the ChannelManager was serialized.
9287         pub default_config: UserConfig,
9288
9289         /// A map from channel funding outpoints to ChannelMonitors for those channels (ie
9290         /// value.context.get_funding_txo() should be the key).
9291         ///
9292         /// If a monitor is inconsistent with the channel state during deserialization the channel will
9293         /// be force-closed using the data in the ChannelMonitor and the channel will be dropped. This
9294         /// is true for missing channels as well. If there is a monitor missing for which we find
9295         /// channel data Err(DecodeError::InvalidValue) will be returned.
9296         ///
9297         /// In such cases the latest local transactions will be sent to the tx_broadcaster included in
9298         /// this struct.
9299         ///
9300         /// This is not exported to bindings users because we have no HashMap bindings
9301         pub channel_monitors: HashMap<OutPoint, &'a mut ChannelMonitor<<SP::Target as SignerProvider>::Signer>>,
9302 }
9303
9304 impl<'a, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>
9305                 ChannelManagerReadArgs<'a, M, T, ES, NS, SP, F, R, L>
9306 where
9307         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
9308         T::Target: BroadcasterInterface,
9309         ES::Target: EntropySource,
9310         NS::Target: NodeSigner,
9311         SP::Target: SignerProvider,
9312         F::Target: FeeEstimator,
9313         R::Target: Router,
9314         L::Target: Logger,
9315 {
9316         /// Simple utility function to create a ChannelManagerReadArgs which creates the monitor
9317         /// HashMap for you. This is primarily useful for C bindings where it is not practical to
9318         /// populate a HashMap directly from C.
9319         pub fn new(entropy_source: ES, node_signer: NS, signer_provider: SP, fee_estimator: F, chain_monitor: M, tx_broadcaster: T, router: R, logger: L, default_config: UserConfig,
9320                         mut channel_monitors: Vec<&'a mut ChannelMonitor<<SP::Target as SignerProvider>::Signer>>) -> Self {
9321                 Self {
9322                         entropy_source, node_signer, signer_provider, fee_estimator, chain_monitor, tx_broadcaster, router, logger, default_config,
9323                         channel_monitors: channel_monitors.drain(..).map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect()
9324                 }
9325         }
9326 }
9327
9328 // Implement ReadableArgs for an Arc'd ChannelManager to make it a bit easier to work with the
9329 // SipmleArcChannelManager type:
9330 impl<'a, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>
9331         ReadableArgs<ChannelManagerReadArgs<'a, M, T, ES, NS, SP, F, R, L>> for (BlockHash, Arc<ChannelManager<M, T, ES, NS, SP, F, R, L>>)
9332 where
9333         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
9334         T::Target: BroadcasterInterface,
9335         ES::Target: EntropySource,
9336         NS::Target: NodeSigner,
9337         SP::Target: SignerProvider,
9338         F::Target: FeeEstimator,
9339         R::Target: Router,
9340         L::Target: Logger,
9341 {
9342         fn read<Reader: io::Read>(reader: &mut Reader, args: ChannelManagerReadArgs<'a, M, T, ES, NS, SP, F, R, L>) -> Result<Self, DecodeError> {
9343                 let (blockhash, chan_manager) = <(BlockHash, ChannelManager<M, T, ES, NS, SP, F, R, L>)>::read(reader, args)?;
9344                 Ok((blockhash, Arc::new(chan_manager)))
9345         }
9346 }
9347
9348 impl<'a, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>
9349         ReadableArgs<ChannelManagerReadArgs<'a, M, T, ES, NS, SP, F, R, L>> for (BlockHash, ChannelManager<M, T, ES, NS, SP, F, R, L>)
9350 where
9351         M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
9352         T::Target: BroadcasterInterface,
9353         ES::Target: EntropySource,
9354         NS::Target: NodeSigner,
9355         SP::Target: SignerProvider,
9356         F::Target: FeeEstimator,
9357         R::Target: Router,
9358         L::Target: Logger,
9359 {
9360         fn read<Reader: io::Read>(reader: &mut Reader, mut args: ChannelManagerReadArgs<'a, M, T, ES, NS, SP, F, R, L>) -> Result<Self, DecodeError> {
9361                 let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
9362
9363                 let chain_hash: ChainHash = Readable::read(reader)?;
9364                 let best_block_height: u32 = Readable::read(reader)?;
9365                 let best_block_hash: BlockHash = Readable::read(reader)?;
9366
9367                 let mut failed_htlcs = Vec::new();
9368
9369                 let channel_count: u64 = Readable::read(reader)?;
9370                 let mut funding_txo_set = HashSet::with_capacity(cmp::min(channel_count as usize, 128));
9371                 let mut funded_peer_channels: HashMap<PublicKey, HashMap<ChannelId, ChannelPhase<SP>>> = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
9372                 let mut id_to_peer = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
9373                 let mut short_to_chan_info = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
9374                 let mut channel_closures = VecDeque::new();
9375                 let mut close_background_events = Vec::new();
9376                 for _ in 0..channel_count {
9377                         let mut channel: Channel<SP> = Channel::read(reader, (
9378                                 &args.entropy_source, &args.signer_provider, best_block_height, &provided_channel_type_features(&args.default_config)
9379                         ))?;
9380                         let funding_txo = channel.context.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
9381                         funding_txo_set.insert(funding_txo.clone());
9382                         if let Some(ref mut monitor) = args.channel_monitors.get_mut(&funding_txo) {
9383                                 if channel.get_cur_holder_commitment_transaction_number() > monitor.get_cur_holder_commitment_number() ||
9384                                                 channel.get_revoked_counterparty_commitment_transaction_number() > monitor.get_min_seen_secret() ||
9385                                                 channel.get_cur_counterparty_commitment_transaction_number() > monitor.get_cur_counterparty_commitment_number() ||
9386                                                 channel.context.get_latest_monitor_update_id() < monitor.get_latest_update_id() {
9387                                         // But if the channel is behind of the monitor, close the channel:
9388                                         log_error!(args.logger, "A ChannelManager is stale compared to the current ChannelMonitor!");
9389                                         log_error!(args.logger, " The channel will be force-closed and the latest commitment transaction from the ChannelMonitor broadcast.");
9390                                         if channel.context.get_latest_monitor_update_id() < monitor.get_latest_update_id() {
9391                                                 log_error!(args.logger, " The ChannelMonitor for channel {} is at update_id {} but the ChannelManager is at update_id {}.",
9392                                                         &channel.context.channel_id(), monitor.get_latest_update_id(), channel.context.get_latest_monitor_update_id());
9393                                         }
9394                                         if channel.get_cur_holder_commitment_transaction_number() > monitor.get_cur_holder_commitment_number() {
9395                                                 log_error!(args.logger, " The ChannelMonitor for channel {} is at holder commitment number {} but the ChannelManager is at holder commitment number {}.",
9396                                                         &channel.context.channel_id(), monitor.get_cur_holder_commitment_number(), channel.get_cur_holder_commitment_transaction_number());
9397                                         }
9398                                         if channel.get_revoked_counterparty_commitment_transaction_number() > monitor.get_min_seen_secret() {
9399                                                 log_error!(args.logger, " The ChannelMonitor for channel {} is at revoked counterparty transaction number {} but the ChannelManager is at revoked counterparty transaction number {}.",
9400                                                         &channel.context.channel_id(), monitor.get_min_seen_secret(), channel.get_revoked_counterparty_commitment_transaction_number());
9401                                         }
9402                                         if channel.get_cur_counterparty_commitment_transaction_number() > monitor.get_cur_counterparty_commitment_number() {
9403                                                 log_error!(args.logger, " The ChannelMonitor for channel {} is at counterparty commitment transaction number {} but the ChannelManager is at counterparty commitment transaction number {}.",
9404                                                         &channel.context.channel_id(), monitor.get_cur_counterparty_commitment_number(), channel.get_cur_counterparty_commitment_transaction_number());
9405                                         }
9406                                         let mut shutdown_result = channel.context.force_shutdown(true);
9407                                         if shutdown_result.unbroadcasted_batch_funding_txid.is_some() {
9408                                                 return Err(DecodeError::InvalidValue);
9409                                         }
9410                                         if let Some((counterparty_node_id, funding_txo, update)) = shutdown_result.monitor_update {
9411                                                 close_background_events.push(BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
9412                                                         counterparty_node_id, funding_txo, update
9413                                                 });
9414                                         }
9415                                         failed_htlcs.append(&mut shutdown_result.dropped_outbound_htlcs);
9416                                         channel_closures.push_back((events::Event::ChannelClosed {
9417                                                 channel_id: channel.context.channel_id(),
9418                                                 user_channel_id: channel.context.get_user_id(),
9419                                                 reason: ClosureReason::OutdatedChannelManager,
9420                                                 counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
9421                                                 channel_capacity_sats: Some(channel.context.get_value_satoshis()),
9422                                         }, None));
9423                                         for (channel_htlc_source, payment_hash) in channel.inflight_htlc_sources() {
9424                                                 let mut found_htlc = false;
9425                                                 for (monitor_htlc_source, _) in monitor.get_all_current_outbound_htlcs() {
9426                                                         if *channel_htlc_source == monitor_htlc_source { found_htlc = true; break; }
9427                                                 }
9428                                                 if !found_htlc {
9429                                                         // If we have some HTLCs in the channel which are not present in the newer
9430                                                         // ChannelMonitor, they have been removed and should be failed back to
9431                                                         // ensure we don't forget them entirely. Note that if the missing HTLC(s)
9432                                                         // were actually claimed we'd have generated and ensured the previous-hop
9433                                                         // claim update ChannelMonitor updates were persisted prior to persising
9434                                                         // the ChannelMonitor update for the forward leg, so attempting to fail the
9435                                                         // backwards leg of the HTLC will simply be rejected.
9436                                                         log_info!(args.logger,
9437                                                                 "Failing HTLC with hash {} as it is missing in the ChannelMonitor for channel {} but was present in the (stale) ChannelManager",
9438                                                                 &channel.context.channel_id(), &payment_hash);
9439                                                         failed_htlcs.push((channel_htlc_source.clone(), *payment_hash, channel.context.get_counterparty_node_id(), channel.context.channel_id()));
9440                                                 }
9441                                         }
9442                                 } else {
9443                                         log_info!(args.logger, "Successfully loaded channel {} at update_id {} against monitor at update id {}",
9444                                                 &channel.context.channel_id(), channel.context.get_latest_monitor_update_id(),
9445                                                 monitor.get_latest_update_id());
9446                                         if let Some(short_channel_id) = channel.context.get_short_channel_id() {
9447                                                 short_to_chan_info.insert(short_channel_id, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
9448                                         }
9449                                         if channel.context.is_funding_broadcast() {
9450                                                 id_to_peer.insert(channel.context.channel_id(), channel.context.get_counterparty_node_id());
9451                                         }
9452                                         match funded_peer_channels.entry(channel.context.get_counterparty_node_id()) {
9453                                                 hash_map::Entry::Occupied(mut entry) => {
9454                                                         let by_id_map = entry.get_mut();
9455                                                         by_id_map.insert(channel.context.channel_id(), ChannelPhase::Funded(channel));
9456                                                 },
9457                                                 hash_map::Entry::Vacant(entry) => {
9458                                                         let mut by_id_map = HashMap::new();
9459                                                         by_id_map.insert(channel.context.channel_id(), ChannelPhase::Funded(channel));
9460                                                         entry.insert(by_id_map);
9461                                                 }
9462                                         }
9463                                 }
9464                         } else if channel.is_awaiting_initial_mon_persist() {
9465                                 // If we were persisted and shut down while the initial ChannelMonitor persistence
9466                                 // was in-progress, we never broadcasted the funding transaction and can still
9467                                 // safely discard the channel.
9468                                 let _ = channel.context.force_shutdown(false);
9469                                 channel_closures.push_back((events::Event::ChannelClosed {
9470                                         channel_id: channel.context.channel_id(),
9471                                         user_channel_id: channel.context.get_user_id(),
9472                                         reason: ClosureReason::DisconnectedPeer,
9473                                         counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
9474                                         channel_capacity_sats: Some(channel.context.get_value_satoshis()),
9475                                 }, None));
9476                         } else {
9477                                 log_error!(args.logger, "Missing ChannelMonitor for channel {} needed by ChannelManager.", &channel.context.channel_id());
9478                                 log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
9479                                 log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
9480                                 log_error!(args.logger, " Without the ChannelMonitor we cannot continue without risking funds.");
9481                                 log_error!(args.logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
9482                                 return Err(DecodeError::InvalidValue);
9483                         }
9484                 }
9485
9486                 for (funding_txo, _) in args.channel_monitors.iter() {
9487                         if !funding_txo_set.contains(funding_txo) {
9488                                 log_info!(args.logger, "Queueing monitor update to ensure missing channel {} is force closed",
9489                                         &funding_txo.to_channel_id());
9490                                 let monitor_update = ChannelMonitorUpdate {
9491                                         update_id: CLOSED_CHANNEL_UPDATE_ID,
9492                                         updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast: true }],
9493                                 };
9494                                 close_background_events.push(BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup((*funding_txo, monitor_update)));
9495                         }
9496                 }
9497
9498                 const MAX_ALLOC_SIZE: usize = 1024 * 64;
9499                 let forward_htlcs_count: u64 = Readable::read(reader)?;
9500                 let mut forward_htlcs = HashMap::with_capacity(cmp::min(forward_htlcs_count as usize, 128));
9501                 for _ in 0..forward_htlcs_count {
9502                         let short_channel_id = Readable::read(reader)?;
9503                         let pending_forwards_count: u64 = Readable::read(reader)?;
9504                         let mut pending_forwards = Vec::with_capacity(cmp::min(pending_forwards_count as usize, MAX_ALLOC_SIZE/mem::size_of::<HTLCForwardInfo>()));
9505                         for _ in 0..pending_forwards_count {
9506                                 pending_forwards.push(Readable::read(reader)?);
9507                         }
9508                         forward_htlcs.insert(short_channel_id, pending_forwards);
9509                 }
9510
9511                 let claimable_htlcs_count: u64 = Readable::read(reader)?;
9512                 let mut claimable_htlcs_list = Vec::with_capacity(cmp::min(claimable_htlcs_count as usize, 128));
9513                 for _ in 0..claimable_htlcs_count {
9514                         let payment_hash = Readable::read(reader)?;
9515                         let previous_hops_len: u64 = Readable::read(reader)?;
9516                         let mut previous_hops = Vec::with_capacity(cmp::min(previous_hops_len as usize, MAX_ALLOC_SIZE/mem::size_of::<ClaimableHTLC>()));
9517                         for _ in 0..previous_hops_len {
9518                                 previous_hops.push(<ClaimableHTLC as Readable>::read(reader)?);
9519                         }
9520                         claimable_htlcs_list.push((payment_hash, previous_hops));
9521                 }
9522
9523                 let peer_state_from_chans = |channel_by_id| {
9524                         PeerState {
9525                                 channel_by_id,
9526                                 inbound_channel_request_by_id: HashMap::new(),
9527                                 latest_features: InitFeatures::empty(),
9528                                 pending_msg_events: Vec::new(),
9529                                 in_flight_monitor_updates: BTreeMap::new(),
9530                                 monitor_update_blocked_actions: BTreeMap::new(),
9531                                 actions_blocking_raa_monitor_updates: BTreeMap::new(),
9532                                 is_connected: false,
9533                         }
9534                 };
9535
9536                 let peer_count: u64 = Readable::read(reader)?;
9537                 let mut per_peer_state = HashMap::with_capacity(cmp::min(peer_count as usize, MAX_ALLOC_SIZE/mem::size_of::<(PublicKey, Mutex<PeerState<SP>>)>()));
9538                 for _ in 0..peer_count {
9539                         let peer_pubkey = Readable::read(reader)?;
9540                         let peer_chans = funded_peer_channels.remove(&peer_pubkey).unwrap_or(HashMap::new());
9541                         let mut peer_state = peer_state_from_chans(peer_chans);
9542                         peer_state.latest_features = Readable::read(reader)?;
9543                         per_peer_state.insert(peer_pubkey, Mutex::new(peer_state));
9544                 }
9545
9546                 let event_count: u64 = Readable::read(reader)?;
9547                 let mut pending_events_read: VecDeque<(events::Event, Option<EventCompletionAction>)> =
9548                         VecDeque::with_capacity(cmp::min(event_count as usize, MAX_ALLOC_SIZE/mem::size_of::<(events::Event, Option<EventCompletionAction>)>()));
9549                 for _ in 0..event_count {
9550                         match MaybeReadable::read(reader)? {
9551                                 Some(event) => pending_events_read.push_back((event, None)),
9552                                 None => continue,
9553                         }
9554                 }
9555
9556                 let background_event_count: u64 = Readable::read(reader)?;
9557                 for _ in 0..background_event_count {
9558                         match <u8 as Readable>::read(reader)? {
9559                                 0 => {
9560                                         // LDK versions prior to 0.0.116 wrote pending `MonitorUpdateRegeneratedOnStartup`s here,
9561                                         // however we really don't (and never did) need them - we regenerate all
9562                                         // on-startup monitor updates.
9563                                         let _: OutPoint = Readable::read(reader)?;
9564                                         let _: ChannelMonitorUpdate = Readable::read(reader)?;
9565                                 }
9566                                 _ => return Err(DecodeError::InvalidValue),
9567                         }
9568                 }
9569
9570                 let _last_node_announcement_serial: u32 = Readable::read(reader)?; // Only used < 0.0.111
9571                 let highest_seen_timestamp: u32 = Readable::read(reader)?;
9572
9573                 let pending_inbound_payment_count: u64 = Readable::read(reader)?;
9574                 let mut pending_inbound_payments: HashMap<PaymentHash, PendingInboundPayment> = HashMap::with_capacity(cmp::min(pending_inbound_payment_count as usize, MAX_ALLOC_SIZE/(3*32)));
9575                 for _ in 0..pending_inbound_payment_count {
9576                         if pending_inbound_payments.insert(Readable::read(reader)?, Readable::read(reader)?).is_some() {
9577                                 return Err(DecodeError::InvalidValue);
9578                         }
9579                 }
9580
9581                 let pending_outbound_payments_count_compat: u64 = Readable::read(reader)?;
9582                 let mut pending_outbound_payments_compat: HashMap<PaymentId, PendingOutboundPayment> =
9583                         HashMap::with_capacity(cmp::min(pending_outbound_payments_count_compat as usize, MAX_ALLOC_SIZE/32));
9584                 for _ in 0..pending_outbound_payments_count_compat {
9585                         let session_priv = Readable::read(reader)?;
9586                         let payment = PendingOutboundPayment::Legacy {
9587                                 session_privs: [session_priv].iter().cloned().collect()
9588                         };
9589                         if pending_outbound_payments_compat.insert(PaymentId(session_priv), payment).is_some() {
9590                                 return Err(DecodeError::InvalidValue)
9591                         };
9592                 }
9593
9594                 // pending_outbound_payments_no_retry is for compatibility with 0.0.101 clients.
9595                 let mut pending_outbound_payments_no_retry: Option<HashMap<PaymentId, HashSet<[u8; 32]>>> = None;
9596                 let mut pending_outbound_payments = None;
9597                 let mut pending_intercepted_htlcs: Option<HashMap<InterceptId, PendingAddHTLCInfo>> = Some(HashMap::new());
9598                 let mut received_network_pubkey: Option<PublicKey> = None;
9599                 let mut fake_scid_rand_bytes: Option<[u8; 32]> = None;
9600                 let mut probing_cookie_secret: Option<[u8; 32]> = None;
9601                 let mut claimable_htlc_purposes = None;
9602                 let mut claimable_htlc_onion_fields = None;
9603                 let mut pending_claiming_payments = Some(HashMap::new());
9604                 let mut monitor_update_blocked_actions_per_peer: Option<Vec<(_, BTreeMap<_, Vec<_>>)>> = Some(Vec::new());
9605                 let mut events_override = None;
9606                 let mut in_flight_monitor_updates: Option<HashMap<(PublicKey, OutPoint), Vec<ChannelMonitorUpdate>>> = None;
9607                 read_tlv_fields!(reader, {
9608                         (1, pending_outbound_payments_no_retry, option),
9609                         (2, pending_intercepted_htlcs, option),
9610                         (3, pending_outbound_payments, option),
9611                         (4, pending_claiming_payments, option),
9612                         (5, received_network_pubkey, option),
9613                         (6, monitor_update_blocked_actions_per_peer, option),
9614                         (7, fake_scid_rand_bytes, option),
9615                         (8, events_override, option),
9616                         (9, claimable_htlc_purposes, optional_vec),
9617                         (10, in_flight_monitor_updates, option),
9618                         (11, probing_cookie_secret, option),
9619                         (13, claimable_htlc_onion_fields, optional_vec),
9620                 });
9621                 if fake_scid_rand_bytes.is_none() {
9622                         fake_scid_rand_bytes = Some(args.entropy_source.get_secure_random_bytes());
9623                 }
9624
9625                 if probing_cookie_secret.is_none() {
9626                         probing_cookie_secret = Some(args.entropy_source.get_secure_random_bytes());
9627                 }
9628
9629                 if let Some(events) = events_override {
9630                         pending_events_read = events;
9631                 }
9632
9633                 if !channel_closures.is_empty() {
9634                         pending_events_read.append(&mut channel_closures);
9635                 }
9636
9637                 if pending_outbound_payments.is_none() && pending_outbound_payments_no_retry.is_none() {
9638                         pending_outbound_payments = Some(pending_outbound_payments_compat);
9639                 } else if pending_outbound_payments.is_none() {
9640                         let mut outbounds = HashMap::new();
9641                         for (id, session_privs) in pending_outbound_payments_no_retry.unwrap().drain() {
9642                                 outbounds.insert(id, PendingOutboundPayment::Legacy { session_privs });
9643                         }
9644                         pending_outbound_payments = Some(outbounds);
9645                 }
9646                 let pending_outbounds = OutboundPayments {
9647                         pending_outbound_payments: Mutex::new(pending_outbound_payments.unwrap()),
9648                         retry_lock: Mutex::new(())
9649                 };
9650
9651                 // We have to replay (or skip, if they were completed after we wrote the `ChannelManager`)
9652                 // each `ChannelMonitorUpdate` in `in_flight_monitor_updates`. After doing so, we have to
9653                 // check that each channel we have isn't newer than the latest `ChannelMonitorUpdate`(s) we
9654                 // replayed, and for each monitor update we have to replay we have to ensure there's a
9655                 // `ChannelMonitor` for it.
9656                 //
9657                 // In order to do so we first walk all of our live channels (so that we can check their
9658                 // state immediately after doing the update replays, when we have the `update_id`s
9659                 // available) and then walk any remaining in-flight updates.
9660                 //
9661                 // Because the actual handling of the in-flight updates is the same, it's macro'ized here:
9662                 let mut pending_background_events = Vec::new();
9663                 macro_rules! handle_in_flight_updates {
9664                         ($counterparty_node_id: expr, $chan_in_flight_upds: expr, $funding_txo: expr,
9665                          $monitor: expr, $peer_state: expr, $channel_info_log: expr
9666                         ) => { {
9667                                 let mut max_in_flight_update_id = 0;
9668                                 $chan_in_flight_upds.retain(|upd| upd.update_id > $monitor.get_latest_update_id());
9669                                 for update in $chan_in_flight_upds.iter() {
9670                                         log_trace!(args.logger, "Replaying ChannelMonitorUpdate {} for {}channel {}",
9671                                                 update.update_id, $channel_info_log, &$funding_txo.to_channel_id());
9672                                         max_in_flight_update_id = cmp::max(max_in_flight_update_id, update.update_id);
9673                                         pending_background_events.push(
9674                                                 BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
9675                                                         counterparty_node_id: $counterparty_node_id,
9676                                                         funding_txo: $funding_txo,
9677                                                         update: update.clone(),
9678                                                 });
9679                                 }
9680                                 if $chan_in_flight_upds.is_empty() {
9681                                         // We had some updates to apply, but it turns out they had completed before we
9682                                         // were serialized, we just weren't notified of that. Thus, we may have to run
9683                                         // the completion actions for any monitor updates, but otherwise are done.
9684                                         pending_background_events.push(
9685                                                 BackgroundEvent::MonitorUpdatesComplete {
9686                                                         counterparty_node_id: $counterparty_node_id,
9687                                                         channel_id: $funding_txo.to_channel_id(),
9688                                                 });
9689                                 }
9690                                 if $peer_state.in_flight_monitor_updates.insert($funding_txo, $chan_in_flight_upds).is_some() {
9691                                         log_error!(args.logger, "Duplicate in-flight monitor update set for the same channel!");
9692                                         return Err(DecodeError::InvalidValue);
9693                                 }
9694                                 max_in_flight_update_id
9695                         } }
9696                 }
9697
9698                 for (counterparty_id, peer_state_mtx) in per_peer_state.iter_mut() {
9699                         let mut peer_state_lock = peer_state_mtx.lock().unwrap();
9700                         let peer_state = &mut *peer_state_lock;
9701                         for phase in peer_state.channel_by_id.values() {
9702                                 if let ChannelPhase::Funded(chan) = phase {
9703                                         // Channels that were persisted have to be funded, otherwise they should have been
9704                                         // discarded.
9705                                         let funding_txo = chan.context.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
9706                                         let monitor = args.channel_monitors.get(&funding_txo)
9707                                                 .expect("We already checked for monitor presence when loading channels");
9708                                         let mut max_in_flight_update_id = monitor.get_latest_update_id();
9709                                         if let Some(in_flight_upds) = &mut in_flight_monitor_updates {
9710                                                 if let Some(mut chan_in_flight_upds) = in_flight_upds.remove(&(*counterparty_id, funding_txo)) {
9711                                                         max_in_flight_update_id = cmp::max(max_in_flight_update_id,
9712                                                                 handle_in_flight_updates!(*counterparty_id, chan_in_flight_upds,
9713                                                                         funding_txo, monitor, peer_state, ""));
9714                                                 }
9715                                         }
9716                                         if chan.get_latest_unblocked_monitor_update_id() > max_in_flight_update_id {
9717                                                 // If the channel is ahead of the monitor, return InvalidValue:
9718                                                 log_error!(args.logger, "A ChannelMonitor is stale compared to the current ChannelManager! This indicates a potentially-critical violation of the chain::Watch API!");
9719                                                 log_error!(args.logger, " The ChannelMonitor for channel {} is at update_id {} with update_id through {} in-flight",
9720                                                         chan.context.channel_id(), monitor.get_latest_update_id(), max_in_flight_update_id);
9721                                                 log_error!(args.logger, " but the ChannelManager is at update_id {}.", chan.get_latest_unblocked_monitor_update_id());
9722                                                 log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
9723                                                 log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
9724                                                 log_error!(args.logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");
9725                                                 log_error!(args.logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
9726                                                 return Err(DecodeError::InvalidValue);
9727                                         }
9728                                 } else {
9729                                         // We shouldn't have persisted (or read) any unfunded channel types so none should have been
9730                                         // created in this `channel_by_id` map.
9731                                         debug_assert!(false);
9732                                         return Err(DecodeError::InvalidValue);
9733                                 }
9734                         }
9735                 }
9736
9737                 if let Some(in_flight_upds) = in_flight_monitor_updates {
9738                         for ((counterparty_id, funding_txo), mut chan_in_flight_updates) in in_flight_upds {
9739                                 if let Some(monitor) = args.channel_monitors.get(&funding_txo) {
9740                                         // Now that we've removed all the in-flight monitor updates for channels that are
9741                                         // still open, we need to replay any monitor updates that are for closed channels,
9742                                         // creating the neccessary peer_state entries as we go.
9743                                         let peer_state_mutex = per_peer_state.entry(counterparty_id).or_insert_with(|| {
9744                                                 Mutex::new(peer_state_from_chans(HashMap::new()))
9745                                         });
9746                                         let mut peer_state = peer_state_mutex.lock().unwrap();
9747                                         handle_in_flight_updates!(counterparty_id, chan_in_flight_updates,
9748                                                 funding_txo, monitor, peer_state, "closed ");
9749                                 } else {
9750                                         log_error!(args.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!");
9751                                         log_error!(args.logger, " The ChannelMonitor for channel {} is missing.",
9752                                                 &funding_txo.to_channel_id());
9753                                         log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
9754                                         log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
9755                                         log_error!(args.logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");
9756                                         log_error!(args.logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
9757                                         return Err(DecodeError::InvalidValue);
9758                                 }
9759                         }
9760                 }
9761
9762                 // Note that we have to do the above replays before we push new monitor updates.
9763                 pending_background_events.append(&mut close_background_events);
9764
9765                 // If there's any preimages for forwarded HTLCs hanging around in ChannelMonitors we
9766                 // should ensure we try them again on the inbound edge. We put them here and do so after we
9767                 // have a fully-constructed `ChannelManager` at the end.
9768                 let mut pending_claims_to_replay = Vec::new();
9769
9770                 {
9771                         // If we're tracking pending payments, ensure we haven't lost any by looking at the
9772                         // ChannelMonitor data for any channels for which we do not have authorative state
9773                         // (i.e. those for which we just force-closed above or we otherwise don't have a
9774                         // corresponding `Channel` at all).
9775                         // This avoids several edge-cases where we would otherwise "forget" about pending
9776                         // payments which are still in-flight via their on-chain state.
9777                         // We only rebuild the pending payments map if we were most recently serialized by
9778                         // 0.0.102+
9779                         for (_, monitor) in args.channel_monitors.iter() {
9780                                 let counterparty_opt = id_to_peer.get(&monitor.get_funding_txo().0.to_channel_id());
9781                                 if counterparty_opt.is_none() {
9782                                         for (htlc_source, (htlc, _)) in monitor.get_pending_or_resolved_outbound_htlcs() {
9783                                                 if let HTLCSource::OutboundRoute { payment_id, session_priv, path, .. } = htlc_source {
9784                                                         if path.hops.is_empty() {
9785                                                                 log_error!(args.logger, "Got an empty path for a pending payment");
9786                                                                 return Err(DecodeError::InvalidValue);
9787                                                         }
9788
9789                                                         let path_amt = path.final_value_msat();
9790                                                         let mut session_priv_bytes = [0; 32];
9791                                                         session_priv_bytes[..].copy_from_slice(&session_priv[..]);
9792                                                         match pending_outbounds.pending_outbound_payments.lock().unwrap().entry(payment_id) {
9793                                                                 hash_map::Entry::Occupied(mut entry) => {
9794                                                                         let newly_added = entry.get_mut().insert(session_priv_bytes, &path);
9795                                                                         log_info!(args.logger, "{} a pending payment path for {} msat for session priv {} on an existing pending payment with payment hash {}",
9796                                                                                 if newly_added { "Added" } else { "Had" }, path_amt, log_bytes!(session_priv_bytes), &htlc.payment_hash);
9797                                                                 },
9798                                                                 hash_map::Entry::Vacant(entry) => {
9799                                                                         let path_fee = path.fee_msat();
9800                                                                         entry.insert(PendingOutboundPayment::Retryable {
9801                                                                                 retry_strategy: None,
9802                                                                                 attempts: PaymentAttempts::new(),
9803                                                                                 payment_params: None,
9804                                                                                 session_privs: [session_priv_bytes].iter().map(|a| *a).collect(),
9805                                                                                 payment_hash: htlc.payment_hash,
9806                                                                                 payment_secret: None, // only used for retries, and we'll never retry on startup
9807                                                                                 payment_metadata: None, // only used for retries, and we'll never retry on startup
9808                                                                                 keysend_preimage: None, // only used for retries, and we'll never retry on startup
9809                                                                                 custom_tlvs: Vec::new(), // only used for retries, and we'll never retry on startup
9810                                                                                 pending_amt_msat: path_amt,
9811                                                                                 pending_fee_msat: Some(path_fee),
9812                                                                                 total_msat: path_amt,
9813                                                                                 starting_block_height: best_block_height,
9814                                                                                 remaining_max_total_routing_fee_msat: None, // only used for retries, and we'll never retry on startup
9815                                                                         });
9816                                                                         log_info!(args.logger, "Added a pending payment for {} msat with payment hash {} for path with session priv {}",
9817                                                                                 path_amt, &htlc.payment_hash,  log_bytes!(session_priv_bytes));
9818                                                                 }
9819                                                         }
9820                                                 }
9821                                         }
9822                                         for (htlc_source, (htlc, preimage_opt)) in monitor.get_all_current_outbound_htlcs() {
9823                                                 match htlc_source {
9824                                                         HTLCSource::PreviousHopData(prev_hop_data) => {
9825                                                                 let pending_forward_matches_htlc = |info: &PendingAddHTLCInfo| {
9826                                                                         info.prev_funding_outpoint == prev_hop_data.outpoint &&
9827                                                                                 info.prev_htlc_id == prev_hop_data.htlc_id
9828                                                                 };
9829                                                                 // The ChannelMonitor is now responsible for this HTLC's
9830                                                                 // failure/success and will let us know what its outcome is. If we
9831                                                                 // still have an entry for this HTLC in `forward_htlcs` or
9832                                                                 // `pending_intercepted_htlcs`, we were apparently not persisted after
9833                                                                 // the monitor was when forwarding the payment.
9834                                                                 forward_htlcs.retain(|_, forwards| {
9835                                                                         forwards.retain(|forward| {
9836                                                                                 if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
9837                                                                                         if pending_forward_matches_htlc(&htlc_info) {
9838                                                                                                 log_info!(args.logger, "Removing pending to-forward HTLC with hash {} as it was forwarded to the closed channel {}",
9839                                                                                                         &htlc.payment_hash, &monitor.get_funding_txo().0.to_channel_id());
9840                                                                                                 false
9841                                                                                         } else { true }
9842                                                                                 } else { true }
9843                                                                         });
9844                                                                         !forwards.is_empty()
9845                                                                 });
9846                                                                 pending_intercepted_htlcs.as_mut().unwrap().retain(|intercepted_id, htlc_info| {
9847                                                                         if pending_forward_matches_htlc(&htlc_info) {
9848                                                                                 log_info!(args.logger, "Removing pending intercepted HTLC with hash {} as it was forwarded to the closed channel {}",
9849                                                                                         &htlc.payment_hash, &monitor.get_funding_txo().0.to_channel_id());
9850                                                                                 pending_events_read.retain(|(event, _)| {
9851                                                                                         if let Event::HTLCIntercepted { intercept_id: ev_id, .. } = event {
9852                                                                                                 intercepted_id != ev_id
9853                                                                                         } else { true }
9854                                                                                 });
9855                                                                                 false
9856                                                                         } else { true }
9857                                                                 });
9858                                                         },
9859                                                         HTLCSource::OutboundRoute { payment_id, session_priv, path, .. } => {
9860                                                                 if let Some(preimage) = preimage_opt {
9861                                                                         let pending_events = Mutex::new(pending_events_read);
9862                                                                         // Note that we set `from_onchain` to "false" here,
9863                                                                         // deliberately keeping the pending payment around forever.
9864                                                                         // Given it should only occur when we have a channel we're
9865                                                                         // force-closing for being stale that's okay.
9866                                                                         // The alternative would be to wipe the state when claiming,
9867                                                                         // generating a `PaymentPathSuccessful` event but regenerating
9868                                                                         // it and the `PaymentSent` on every restart until the
9869                                                                         // `ChannelMonitor` is removed.
9870                                                                         let compl_action =
9871                                                                                 EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
9872                                                                                         channel_funding_outpoint: monitor.get_funding_txo().0,
9873                                                                                         counterparty_node_id: path.hops[0].pubkey,
9874                                                                                 };
9875                                                                         pending_outbounds.claim_htlc(payment_id, preimage, session_priv,
9876                                                                                 path, false, compl_action, &pending_events, &args.logger);
9877                                                                         pending_events_read = pending_events.into_inner().unwrap();
9878                                                                 }
9879                                                         },
9880                                                 }
9881                                         }
9882                                 }
9883
9884                                 // Whether the downstream channel was closed or not, try to re-apply any payment
9885                                 // preimages from it which may be needed in upstream channels for forwarded
9886                                 // payments.
9887                                 let outbound_claimed_htlcs_iter = monitor.get_all_current_outbound_htlcs()
9888                                         .into_iter()
9889                                         .filter_map(|(htlc_source, (htlc, preimage_opt))| {
9890                                                 if let HTLCSource::PreviousHopData(_) = htlc_source {
9891                                                         if let Some(payment_preimage) = preimage_opt {
9892                                                                 Some((htlc_source, payment_preimage, htlc.amount_msat,
9893                                                                         // Check if `counterparty_opt.is_none()` to see if the
9894                                                                         // downstream chan is closed (because we don't have a
9895                                                                         // channel_id -> peer map entry).
9896                                                                         counterparty_opt.is_none(),
9897                                                                         counterparty_opt.cloned().or(monitor.get_counterparty_node_id()),
9898                                                                         monitor.get_funding_txo().0))
9899                                                         } else { None }
9900                                                 } else {
9901                                                         // If it was an outbound payment, we've handled it above - if a preimage
9902                                                         // came in and we persisted the `ChannelManager` we either handled it and
9903                                                         // are good to go or the channel force-closed - we don't have to handle the
9904                                                         // channel still live case here.
9905                                                         None
9906                                                 }
9907                                         });
9908                                 for tuple in outbound_claimed_htlcs_iter {
9909                                         pending_claims_to_replay.push(tuple);
9910                                 }
9911                         }
9912                 }
9913
9914                 if !forward_htlcs.is_empty() || pending_outbounds.needs_abandon() {
9915                         // If we have pending HTLCs to forward, assume we either dropped a
9916                         // `PendingHTLCsForwardable` or the user received it but never processed it as they
9917                         // shut down before the timer hit. Either way, set the time_forwardable to a small
9918                         // constant as enough time has likely passed that we should simply handle the forwards
9919                         // now, or at least after the user gets a chance to reconnect to our peers.
9920                         pending_events_read.push_back((events::Event::PendingHTLCsForwardable {
9921                                 time_forwardable: Duration::from_secs(2),
9922                         }, None));
9923                 }
9924
9925                 let inbound_pmt_key_material = args.node_signer.get_inbound_payment_key_material();
9926                 let expanded_inbound_key = inbound_payment::ExpandedKey::new(&inbound_pmt_key_material);
9927
9928                 let mut claimable_payments = HashMap::with_capacity(claimable_htlcs_list.len());
9929                 if let Some(purposes) = claimable_htlc_purposes {
9930                         if purposes.len() != claimable_htlcs_list.len() {
9931                                 return Err(DecodeError::InvalidValue);
9932                         }
9933                         if let Some(onion_fields) = claimable_htlc_onion_fields {
9934                                 if onion_fields.len() != claimable_htlcs_list.len() {
9935                                         return Err(DecodeError::InvalidValue);
9936                                 }
9937                                 for (purpose, (onion, (payment_hash, htlcs))) in
9938                                         purposes.into_iter().zip(onion_fields.into_iter().zip(claimable_htlcs_list.into_iter()))
9939                                 {
9940                                         let existing_payment = claimable_payments.insert(payment_hash, ClaimablePayment {
9941                                                 purpose, htlcs, onion_fields: onion,
9942                                         });
9943                                         if existing_payment.is_some() { return Err(DecodeError::InvalidValue); }
9944                                 }
9945                         } else {
9946                                 for (purpose, (payment_hash, htlcs)) in purposes.into_iter().zip(claimable_htlcs_list.into_iter()) {
9947                                         let existing_payment = claimable_payments.insert(payment_hash, ClaimablePayment {
9948                                                 purpose, htlcs, onion_fields: None,
9949                                         });
9950                                         if existing_payment.is_some() { return Err(DecodeError::InvalidValue); }
9951                                 }
9952                         }
9953                 } else {
9954                         // LDK versions prior to 0.0.107 did not write a `pending_htlc_purposes`, but do
9955                         // include a `_legacy_hop_data` in the `OnionPayload`.
9956                         for (payment_hash, htlcs) in claimable_htlcs_list.drain(..) {
9957                                 if htlcs.is_empty() {
9958                                         return Err(DecodeError::InvalidValue);
9959                                 }
9960                                 let purpose = match &htlcs[0].onion_payload {
9961                                         OnionPayload::Invoice { _legacy_hop_data } => {
9962                                                 if let Some(hop_data) = _legacy_hop_data {
9963                                                         events::PaymentPurpose::InvoicePayment {
9964                                                                 payment_preimage: match pending_inbound_payments.get(&payment_hash) {
9965                                                                         Some(inbound_payment) => inbound_payment.payment_preimage,
9966                                                                         None => match inbound_payment::verify(payment_hash, &hop_data, 0, &expanded_inbound_key, &args.logger) {
9967                                                                                 Ok((payment_preimage, _)) => payment_preimage,
9968                                                                                 Err(()) => {
9969                                                                                         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);
9970                                                                                         return Err(DecodeError::InvalidValue);
9971                                                                                 }
9972                                                                         }
9973                                                                 },
9974                                                                 payment_secret: hop_data.payment_secret,
9975                                                         }
9976                                                 } else { return Err(DecodeError::InvalidValue); }
9977                                         },
9978                                         OnionPayload::Spontaneous(payment_preimage) =>
9979                                                 events::PaymentPurpose::SpontaneousPayment(*payment_preimage),
9980                                 };
9981                                 claimable_payments.insert(payment_hash, ClaimablePayment {
9982                                         purpose, htlcs, onion_fields: None,
9983                                 });
9984                         }
9985                 }
9986
9987                 let mut secp_ctx = Secp256k1::new();
9988                 secp_ctx.seeded_randomize(&args.entropy_source.get_secure_random_bytes());
9989
9990                 let our_network_pubkey = match args.node_signer.get_node_id(Recipient::Node) {
9991                         Ok(key) => key,
9992                         Err(()) => return Err(DecodeError::InvalidValue)
9993                 };
9994                 if let Some(network_pubkey) = received_network_pubkey {
9995                         if network_pubkey != our_network_pubkey {
9996                                 log_error!(args.logger, "Key that was generated does not match the existing key.");
9997                                 return Err(DecodeError::InvalidValue);
9998                         }
9999                 }
10000
10001                 let mut outbound_scid_aliases = HashSet::new();
10002                 for (_peer_node_id, peer_state_mutex) in per_peer_state.iter_mut() {
10003                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10004                         let peer_state = &mut *peer_state_lock;
10005                         for (chan_id, phase) in peer_state.channel_by_id.iter_mut() {
10006                                 if let ChannelPhase::Funded(chan) = phase {
10007                                         if chan.context.outbound_scid_alias() == 0 {
10008                                                 let mut outbound_scid_alias;
10009                                                 loop {
10010                                                         outbound_scid_alias = fake_scid::Namespace::OutboundAlias
10011                                                                 .get_fake_scid(best_block_height, &chain_hash, fake_scid_rand_bytes.as_ref().unwrap(), &args.entropy_source);
10012                                                         if outbound_scid_aliases.insert(outbound_scid_alias) { break; }
10013                                                 }
10014                                                 chan.context.set_outbound_scid_alias(outbound_scid_alias);
10015                                         } else if !outbound_scid_aliases.insert(chan.context.outbound_scid_alias()) {
10016                                                 // Note that in rare cases its possible to hit this while reading an older
10017                                                 // channel if we just happened to pick a colliding outbound alias above.
10018                                                 log_error!(args.logger, "Got duplicate outbound SCID alias; {}", chan.context.outbound_scid_alias());
10019                                                 return Err(DecodeError::InvalidValue);
10020                                         }
10021                                         if chan.context.is_usable() {
10022                                                 if short_to_chan_info.insert(chan.context.outbound_scid_alias(), (chan.context.get_counterparty_node_id(), *chan_id)).is_some() {
10023                                                         // Note that in rare cases its possible to hit this while reading an older
10024                                                         // channel if we just happened to pick a colliding outbound alias above.
10025                                                         log_error!(args.logger, "Got duplicate outbound SCID alias; {}", chan.context.outbound_scid_alias());
10026                                                         return Err(DecodeError::InvalidValue);
10027                                                 }
10028                                         }
10029                                 } else {
10030                                         // We shouldn't have persisted (or read) any unfunded channel types so none should have been
10031                                         // created in this `channel_by_id` map.
10032                                         debug_assert!(false);
10033                                         return Err(DecodeError::InvalidValue);
10034                                 }
10035                         }
10036                 }
10037
10038                 let bounded_fee_estimator = LowerBoundedFeeEstimator::new(args.fee_estimator);
10039
10040                 for (_, monitor) in args.channel_monitors.iter() {
10041                         for (payment_hash, payment_preimage) in monitor.get_stored_preimages() {
10042                                 if let Some(payment) = claimable_payments.remove(&payment_hash) {
10043                                         log_info!(args.logger, "Re-claiming HTLCs with payment hash {} as we've released the preimage to a ChannelMonitor!", &payment_hash);
10044                                         let mut claimable_amt_msat = 0;
10045                                         let mut receiver_node_id = Some(our_network_pubkey);
10046                                         let phantom_shared_secret = payment.htlcs[0].prev_hop.phantom_shared_secret;
10047                                         if phantom_shared_secret.is_some() {
10048                                                 let phantom_pubkey = args.node_signer.get_node_id(Recipient::PhantomNode)
10049                                                         .expect("Failed to get node_id for phantom node recipient");
10050                                                 receiver_node_id = Some(phantom_pubkey)
10051                                         }
10052                                         for claimable_htlc in &payment.htlcs {
10053                                                 claimable_amt_msat += claimable_htlc.value;
10054
10055                                                 // Add a holding-cell claim of the payment to the Channel, which should be
10056                                                 // applied ~immediately on peer reconnection. Because it won't generate a
10057                                                 // new commitment transaction we can just provide the payment preimage to
10058                                                 // the corresponding ChannelMonitor and nothing else.
10059                                                 //
10060                                                 // We do so directly instead of via the normal ChannelMonitor update
10061                                                 // procedure as the ChainMonitor hasn't yet been initialized, implying
10062                                                 // we're not allowed to call it directly yet. Further, we do the update
10063                                                 // without incrementing the ChannelMonitor update ID as there isn't any
10064                                                 // reason to.
10065                                                 // If we were to generate a new ChannelMonitor update ID here and then
10066                                                 // crash before the user finishes block connect we'd end up force-closing
10067                                                 // this channel as well. On the flip side, there's no harm in restarting
10068                                                 // without the new monitor persisted - we'll end up right back here on
10069                                                 // restart.
10070                                                 let previous_channel_id = claimable_htlc.prev_hop.outpoint.to_channel_id();
10071                                                 if let Some(peer_node_id) = id_to_peer.get(&previous_channel_id){
10072                                                         let peer_state_mutex = per_peer_state.get(peer_node_id).unwrap();
10073                                                         let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10074                                                         let peer_state = &mut *peer_state_lock;
10075                                                         if let Some(ChannelPhase::Funded(channel)) = peer_state.channel_by_id.get_mut(&previous_channel_id) {
10076                                                                 channel.claim_htlc_while_disconnected_dropping_mon_update(claimable_htlc.prev_hop.htlc_id, payment_preimage, &args.logger);
10077                                                         }
10078                                                 }
10079                                                 if let Some(previous_hop_monitor) = args.channel_monitors.get(&claimable_htlc.prev_hop.outpoint) {
10080                                                         previous_hop_monitor.provide_payment_preimage(&payment_hash, &payment_preimage, &args.tx_broadcaster, &bounded_fee_estimator, &args.logger);
10081                                                 }
10082                                         }
10083                                         pending_events_read.push_back((events::Event::PaymentClaimed {
10084                                                 receiver_node_id,
10085                                                 payment_hash,
10086                                                 purpose: payment.purpose,
10087                                                 amount_msat: claimable_amt_msat,
10088                                                 htlcs: payment.htlcs.iter().map(events::ClaimedHTLC::from).collect(),
10089                                                 sender_intended_total_msat: payment.htlcs.first().map(|htlc| htlc.total_msat),
10090                                         }, None));
10091                                 }
10092                         }
10093                 }
10094
10095                 for (node_id, monitor_update_blocked_actions) in monitor_update_blocked_actions_per_peer.unwrap() {
10096                         if let Some(peer_state) = per_peer_state.get(&node_id) {
10097                                 for (_, actions) in monitor_update_blocked_actions.iter() {
10098                                         for action in actions.iter() {
10099                                                 if let MonitorUpdateCompletionAction::EmitEventAndFreeOtherChannel {
10100                                                         downstream_counterparty_and_funding_outpoint:
10101                                                                 Some((blocked_node_id, blocked_channel_outpoint, blocking_action)), ..
10102                                                 } = action {
10103                                                         if let Some(blocked_peer_state) = per_peer_state.get(&blocked_node_id) {
10104                                                                 blocked_peer_state.lock().unwrap().actions_blocking_raa_monitor_updates
10105                                                                         .entry(blocked_channel_outpoint.to_channel_id())
10106                                                                         .or_insert_with(Vec::new).push(blocking_action.clone());
10107                                                         } else {
10108                                                                 // If the channel we were blocking has closed, we don't need to
10109                                                                 // worry about it - the blocked monitor update should never have
10110                                                                 // been released from the `Channel` object so it can't have
10111                                                                 // completed, and if the channel closed there's no reason to bother
10112                                                                 // anymore.
10113                                                         }
10114                                                 }
10115                                         }
10116                                 }
10117                                 peer_state.lock().unwrap().monitor_update_blocked_actions = monitor_update_blocked_actions;
10118                         } else {
10119                                 log_error!(args.logger, "Got blocked actions without a per-peer-state for {}", node_id);
10120                                 return Err(DecodeError::InvalidValue);
10121                         }
10122                 }
10123
10124                 let channel_manager = ChannelManager {
10125                         chain_hash,
10126                         fee_estimator: bounded_fee_estimator,
10127                         chain_monitor: args.chain_monitor,
10128                         tx_broadcaster: args.tx_broadcaster,
10129                         router: args.router,
10130
10131                         best_block: RwLock::new(BestBlock::new(best_block_hash, best_block_height)),
10132
10133                         inbound_payment_key: expanded_inbound_key,
10134                         pending_inbound_payments: Mutex::new(pending_inbound_payments),
10135                         pending_outbound_payments: pending_outbounds,
10136                         pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()),
10137
10138                         forward_htlcs: Mutex::new(forward_htlcs),
10139                         claimable_payments: Mutex::new(ClaimablePayments { claimable_payments, pending_claiming_payments: pending_claiming_payments.unwrap() }),
10140                         outbound_scid_aliases: Mutex::new(outbound_scid_aliases),
10141                         id_to_peer: Mutex::new(id_to_peer),
10142                         short_to_chan_info: FairRwLock::new(short_to_chan_info),
10143                         fake_scid_rand_bytes: fake_scid_rand_bytes.unwrap(),
10144
10145                         probing_cookie_secret: probing_cookie_secret.unwrap(),
10146
10147                         our_network_pubkey,
10148                         secp_ctx,
10149
10150                         highest_seen_timestamp: AtomicUsize::new(highest_seen_timestamp as usize),
10151
10152                         per_peer_state: FairRwLock::new(per_peer_state),
10153
10154                         pending_events: Mutex::new(pending_events_read),
10155                         pending_events_processor: AtomicBool::new(false),
10156                         pending_background_events: Mutex::new(pending_background_events),
10157                         total_consistency_lock: RwLock::new(()),
10158                         background_events_processed_since_startup: AtomicBool::new(false),
10159
10160                         event_persist_notifier: Notifier::new(),
10161                         needs_persist_flag: AtomicBool::new(false),
10162
10163                         funding_batch_states: Mutex::new(BTreeMap::new()),
10164
10165                         entropy_source: args.entropy_source,
10166                         node_signer: args.node_signer,
10167                         signer_provider: args.signer_provider,
10168
10169                         logger: args.logger,
10170                         default_configuration: args.default_config,
10171                 };
10172
10173                 for htlc_source in failed_htlcs.drain(..) {
10174                         let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
10175                         let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
10176                         let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
10177                         channel_manager.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
10178                 }
10179
10180                 for (source, preimage, downstream_value, downstream_closed, downstream_node_id, downstream_funding) in pending_claims_to_replay {
10181                         // We use `downstream_closed` in place of `from_onchain` here just as a guess - we
10182                         // don't remember in the `ChannelMonitor` where we got a preimage from, but if the
10183                         // channel is closed we just assume that it probably came from an on-chain claim.
10184                         channel_manager.claim_funds_internal(source, preimage, Some(downstream_value),
10185                                 downstream_closed, downstream_node_id, downstream_funding);
10186                 }
10187
10188                 //TODO: Broadcast channel update for closed channels, but only after we've made a
10189                 //connection or two.
10190
10191                 Ok((best_block_hash.clone(), channel_manager))
10192         }
10193 }
10194
10195 #[cfg(test)]
10196 mod tests {
10197         use bitcoin::hashes::Hash;
10198         use bitcoin::hashes::sha256::Hash as Sha256;
10199         use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
10200         use core::sync::atomic::Ordering;
10201         use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
10202         use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
10203         use crate::ln::ChannelId;
10204         use crate::ln::channelmanager::{inbound_payment, PaymentId, PaymentSendFailure, RecipientOnionFields, InterceptId};
10205         use crate::ln::functional_test_utils::*;
10206         use crate::ln::msgs::{self, ErrorAction};
10207         use crate::ln::msgs::ChannelMessageHandler;
10208         use crate::routing::router::{PaymentParameters, RouteParameters, find_route};
10209         use crate::util::errors::APIError;
10210         use crate::util::test_utils;
10211         use crate::util::config::{ChannelConfig, ChannelConfigUpdate};
10212         use crate::sign::EntropySource;
10213
10214         #[test]
10215         fn test_notify_limits() {
10216                 // Check that a few cases which don't require the persistence of a new ChannelManager,
10217                 // indeed, do not cause the persistence of a new ChannelManager.
10218                 let chanmon_cfgs = create_chanmon_cfgs(3);
10219                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
10220                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
10221                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
10222
10223                 // All nodes start with a persistable update pending as `create_network` connects each node
10224                 // with all other nodes to make most tests simpler.
10225                 assert!(nodes[0].node.get_event_or_persistence_needed_future().poll_is_complete());
10226                 assert!(nodes[1].node.get_event_or_persistence_needed_future().poll_is_complete());
10227                 assert!(nodes[2].node.get_event_or_persistence_needed_future().poll_is_complete());
10228
10229                 let mut chan = create_announced_chan_between_nodes(&nodes, 0, 1);
10230
10231                 // We check that the channel info nodes have doesn't change too early, even though we try
10232                 // to connect messages with new values
10233                 chan.0.contents.fee_base_msat *= 2;
10234                 chan.1.contents.fee_base_msat *= 2;
10235                 let node_a_chan_info = nodes[0].node.list_channels_with_counterparty(
10236                         &nodes[1].node.get_our_node_id()).pop().unwrap();
10237                 let node_b_chan_info = nodes[1].node.list_channels_with_counterparty(
10238                         &nodes[0].node.get_our_node_id()).pop().unwrap();
10239
10240                 // The first two nodes (which opened a channel) should now require fresh persistence
10241                 assert!(nodes[0].node.get_event_or_persistence_needed_future().poll_is_complete());
10242                 assert!(nodes[1].node.get_event_or_persistence_needed_future().poll_is_complete());
10243                 // ... but the last node should not.
10244                 assert!(!nodes[2].node.get_event_or_persistence_needed_future().poll_is_complete());
10245                 // After persisting the first two nodes they should no longer need fresh persistence.
10246                 assert!(!nodes[0].node.get_event_or_persistence_needed_future().poll_is_complete());
10247                 assert!(!nodes[1].node.get_event_or_persistence_needed_future().poll_is_complete());
10248
10249                 // Node 3, unrelated to the only channel, shouldn't care if it receives a channel_update
10250                 // about the channel.
10251                 nodes[2].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan.0);
10252                 nodes[2].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan.1);
10253                 assert!(!nodes[2].node.get_event_or_persistence_needed_future().poll_is_complete());
10254
10255                 // The nodes which are a party to the channel should also ignore messages from unrelated
10256                 // parties.
10257                 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan.0);
10258                 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan.1);
10259                 nodes[1].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan.0);
10260                 nodes[1].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan.1);
10261                 assert!(!nodes[0].node.get_event_or_persistence_needed_future().poll_is_complete());
10262                 assert!(!nodes[1].node.get_event_or_persistence_needed_future().poll_is_complete());
10263
10264                 // At this point the channel info given by peers should still be the same.
10265                 assert_eq!(nodes[0].node.list_channels()[0], node_a_chan_info);
10266                 assert_eq!(nodes[1].node.list_channels()[0], node_b_chan_info);
10267
10268                 // An earlier version of handle_channel_update didn't check the directionality of the
10269                 // update message and would always update the local fee info, even if our peer was
10270                 // (spuriously) forwarding us our own channel_update.
10271                 let as_node_one = nodes[0].node.get_our_node_id().serialize()[..] < nodes[1].node.get_our_node_id().serialize()[..];
10272                 let as_update = if as_node_one == (chan.0.contents.flags & 1 == 0 /* chan.0 is from node one */) { &chan.0 } else { &chan.1 };
10273                 let bs_update = if as_node_one == (chan.0.contents.flags & 1 == 0 /* chan.0 is from node one */) { &chan.1 } else { &chan.0 };
10274
10275                 // First deliver each peers' own message, checking that the node doesn't need to be
10276                 // persisted and that its channel info remains the same.
10277                 nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &as_update);
10278                 nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &bs_update);
10279                 assert!(!nodes[0].node.get_event_or_persistence_needed_future().poll_is_complete());
10280                 assert!(!nodes[1].node.get_event_or_persistence_needed_future().poll_is_complete());
10281                 assert_eq!(nodes[0].node.list_channels()[0], node_a_chan_info);
10282                 assert_eq!(nodes[1].node.list_channels()[0], node_b_chan_info);
10283
10284                 // Finally, deliver the other peers' message, ensuring each node needs to be persisted and
10285                 // the channel info has updated.
10286                 nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &bs_update);
10287                 nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &as_update);
10288                 assert!(nodes[0].node.get_event_or_persistence_needed_future().poll_is_complete());
10289                 assert!(nodes[1].node.get_event_or_persistence_needed_future().poll_is_complete());
10290                 assert_ne!(nodes[0].node.list_channels()[0], node_a_chan_info);
10291                 assert_ne!(nodes[1].node.list_channels()[0], node_b_chan_info);
10292         }
10293
10294         #[test]
10295         fn test_keysend_dup_hash_partial_mpp() {
10296                 // Test that a keysend payment with a duplicate hash to an existing partial MPP payment fails as
10297                 // expected.
10298                 let chanmon_cfgs = create_chanmon_cfgs(2);
10299                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10300                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10301                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10302                 create_announced_chan_between_nodes(&nodes, 0, 1);
10303
10304                 // First, send a partial MPP payment.
10305                 let (route, our_payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], 100_000);
10306                 let mut mpp_route = route.clone();
10307                 mpp_route.paths.push(mpp_route.paths[0].clone());
10308
10309                 let payment_id = PaymentId([42; 32]);
10310                 // Use the utility function send_payment_along_path to send the payment with MPP data which
10311                 // indicates there are more HTLCs coming.
10312                 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.
10313                 let session_privs = nodes[0].node.test_add_new_pending_payment(our_payment_hash,
10314                         RecipientOnionFields::secret_only(payment_secret), payment_id, &mpp_route).unwrap();
10315                 nodes[0].node.test_send_payment_along_path(&mpp_route.paths[0], &our_payment_hash,
10316                         RecipientOnionFields::secret_only(payment_secret), 200_000, cur_height, payment_id, &None, session_privs[0]).unwrap();
10317                 check_added_monitors!(nodes[0], 1);
10318                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
10319                 assert_eq!(events.len(), 1);
10320                 pass_along_path(&nodes[0], &[&nodes[1]], 200_000, our_payment_hash, Some(payment_secret), events.drain(..).next().unwrap(), false, None);
10321
10322                 // Next, send a keysend payment with the same payment_hash and make sure it fails.
10323                 nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
10324                         RecipientOnionFields::spontaneous_empty(), PaymentId(payment_preimage.0)).unwrap();
10325                 check_added_monitors!(nodes[0], 1);
10326                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
10327                 assert_eq!(events.len(), 1);
10328                 let ev = events.drain(..).next().unwrap();
10329                 let payment_event = SendEvent::from_event(ev);
10330                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
10331                 check_added_monitors!(nodes[1], 0);
10332                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
10333                 expect_pending_htlcs_forwardable!(nodes[1]);
10334                 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
10335                 check_added_monitors!(nodes[1], 1);
10336                 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
10337                 assert!(updates.update_add_htlcs.is_empty());
10338                 assert!(updates.update_fulfill_htlcs.is_empty());
10339                 assert_eq!(updates.update_fail_htlcs.len(), 1);
10340                 assert!(updates.update_fail_malformed_htlcs.is_empty());
10341                 assert!(updates.update_fee.is_none());
10342                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
10343                 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
10344                 expect_payment_failed!(nodes[0], our_payment_hash, true);
10345
10346                 // Send the second half of the original MPP payment.
10347                 nodes[0].node.test_send_payment_along_path(&mpp_route.paths[1], &our_payment_hash,
10348                         RecipientOnionFields::secret_only(payment_secret), 200_000, cur_height, payment_id, &None, session_privs[1]).unwrap();
10349                 check_added_monitors!(nodes[0], 1);
10350                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
10351                 assert_eq!(events.len(), 1);
10352                 pass_along_path(&nodes[0], &[&nodes[1]], 200_000, our_payment_hash, Some(payment_secret), events.drain(..).next().unwrap(), true, None);
10353
10354                 // Claim the full MPP payment. Note that we can't use a test utility like
10355                 // claim_funds_along_route because the ordering of the messages causes the second half of the
10356                 // payment to be put in the holding cell, which confuses the test utilities. So we exchange the
10357                 // lightning messages manually.
10358                 nodes[1].node.claim_funds(payment_preimage);
10359                 expect_payment_claimed!(nodes[1], our_payment_hash, 200_000);
10360                 check_added_monitors!(nodes[1], 2);
10361
10362                 let bs_first_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
10363                 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_first_updates.update_fulfill_htlcs[0]);
10364                 expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
10365                 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_first_updates.commitment_signed);
10366                 check_added_monitors!(nodes[0], 1);
10367                 let (as_first_raa, as_first_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
10368                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_first_raa);
10369                 check_added_monitors!(nodes[1], 1);
10370                 let bs_second_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
10371                 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_first_cs);
10372                 check_added_monitors!(nodes[1], 1);
10373                 let bs_first_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
10374                 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_second_updates.update_fulfill_htlcs[0]);
10375                 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_updates.commitment_signed);
10376                 check_added_monitors!(nodes[0], 1);
10377                 let as_second_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
10378                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_first_raa);
10379                 let as_second_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
10380                 check_added_monitors!(nodes[0], 1);
10381                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_raa);
10382                 check_added_monitors!(nodes[1], 1);
10383                 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_second_updates.commitment_signed);
10384                 check_added_monitors!(nodes[1], 1);
10385                 let bs_third_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
10386                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_third_raa);
10387                 check_added_monitors!(nodes[0], 1);
10388
10389                 // Note that successful MPP payments will generate a single PaymentSent event upon the first
10390                 // path's success and a PaymentPathSuccessful event for each path's success.
10391                 let events = nodes[0].node.get_and_clear_pending_events();
10392                 assert_eq!(events.len(), 2);
10393                 match events[0] {
10394                         Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path } => {
10395                                 assert_eq!(payment_id, *actual_payment_id);
10396                                 assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
10397                                 assert_eq!(route.paths[0], *path);
10398                         },
10399                         _ => panic!("Unexpected event"),
10400                 }
10401                 match events[1] {
10402                         Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path } => {
10403                                 assert_eq!(payment_id, *actual_payment_id);
10404                                 assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
10405                                 assert_eq!(route.paths[0], *path);
10406                         },
10407                         _ => panic!("Unexpected event"),
10408                 }
10409         }
10410
10411         #[test]
10412         fn test_keysend_dup_payment_hash() {
10413                 do_test_keysend_dup_payment_hash(false);
10414                 do_test_keysend_dup_payment_hash(true);
10415         }
10416
10417         fn do_test_keysend_dup_payment_hash(accept_mpp_keysend: bool) {
10418                 // (1): Test that a keysend payment with a duplicate payment hash to an existing pending
10419                 //      outbound regular payment fails as expected.
10420                 // (2): Test that a regular payment with a duplicate payment hash to an existing keysend payment
10421                 //      fails as expected.
10422                 // (3): Test that a keysend payment with a duplicate payment hash to an existing keysend
10423                 //      payment fails as expected. When `accept_mpp_keysend` is false, this tests that we
10424                 //      reject MPP keysend payments, since in this case where the payment has no payment
10425                 //      secret, a keysend payment with a duplicate hash is basically an MPP keysend. If
10426                 //      `accept_mpp_keysend` is true, this tests that we only accept MPP keysends with
10427                 //      payment secrets and reject otherwise.
10428                 let chanmon_cfgs = create_chanmon_cfgs(2);
10429                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10430                 let mut mpp_keysend_cfg = test_default_channel_config();
10431                 mpp_keysend_cfg.accept_mpp_keysend = accept_mpp_keysend;
10432                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(mpp_keysend_cfg)]);
10433                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10434                 create_announced_chan_between_nodes(&nodes, 0, 1);
10435                 let scorer = test_utils::TestScorer::new();
10436                 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
10437
10438                 // To start (1), send a regular payment but don't claim it.
10439                 let expected_route = [&nodes[1]];
10440                 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &expected_route, 100_000);
10441
10442                 // Next, attempt a keysend payment and make sure it fails.
10443                 let route_params = RouteParameters::from_payment_params_and_value(
10444                         PaymentParameters::for_keysend(expected_route.last().unwrap().node.get_our_node_id(),
10445                         TEST_FINAL_CLTV, false), 100_000);
10446                 let route = find_route(
10447                         &nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph,
10448                         None, nodes[0].logger, &scorer, &Default::default(), &random_seed_bytes
10449                 ).unwrap();
10450                 nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
10451                         RecipientOnionFields::spontaneous_empty(), PaymentId(payment_preimage.0)).unwrap();
10452                 check_added_monitors!(nodes[0], 1);
10453                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
10454                 assert_eq!(events.len(), 1);
10455                 let ev = events.drain(..).next().unwrap();
10456                 let payment_event = SendEvent::from_event(ev);
10457                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
10458                 check_added_monitors!(nodes[1], 0);
10459                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
10460                 // We have to forward pending HTLCs twice - once tries to forward the payment forward (and
10461                 // fails), the second will process the resulting failure and fail the HTLC backward
10462                 expect_pending_htlcs_forwardable!(nodes[1]);
10463                 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash }]);
10464                 check_added_monitors!(nodes[1], 1);
10465                 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
10466                 assert!(updates.update_add_htlcs.is_empty());
10467                 assert!(updates.update_fulfill_htlcs.is_empty());
10468                 assert_eq!(updates.update_fail_htlcs.len(), 1);
10469                 assert!(updates.update_fail_malformed_htlcs.is_empty());
10470                 assert!(updates.update_fee.is_none());
10471                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
10472                 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
10473                 expect_payment_failed!(nodes[0], payment_hash, true);
10474
10475                 // Finally, claim the original payment.
10476                 claim_payment(&nodes[0], &expected_route, payment_preimage);
10477
10478                 // To start (2), send a keysend payment but don't claim it.
10479                 let payment_preimage = PaymentPreimage([42; 32]);
10480                 let route = find_route(
10481                         &nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph,
10482                         None, nodes[0].logger, &scorer, &Default::default(), &random_seed_bytes
10483                 ).unwrap();
10484                 let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
10485                         RecipientOnionFields::spontaneous_empty(), PaymentId(payment_preimage.0)).unwrap();
10486                 check_added_monitors!(nodes[0], 1);
10487                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
10488                 assert_eq!(events.len(), 1);
10489                 let event = events.pop().unwrap();
10490                 let path = vec![&nodes[1]];
10491                 pass_along_path(&nodes[0], &path, 100_000, payment_hash, None, event, true, Some(payment_preimage));
10492
10493                 // Next, attempt a regular payment and make sure it fails.
10494                 let payment_secret = PaymentSecret([43; 32]);
10495                 nodes[0].node.send_payment_with_route(&route, payment_hash,
10496                         RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
10497                 check_added_monitors!(nodes[0], 1);
10498                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
10499                 assert_eq!(events.len(), 1);
10500                 let ev = events.drain(..).next().unwrap();
10501                 let payment_event = SendEvent::from_event(ev);
10502                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
10503                 check_added_monitors!(nodes[1], 0);
10504                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
10505                 expect_pending_htlcs_forwardable!(nodes[1]);
10506                 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash }]);
10507                 check_added_monitors!(nodes[1], 1);
10508                 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
10509                 assert!(updates.update_add_htlcs.is_empty());
10510                 assert!(updates.update_fulfill_htlcs.is_empty());
10511                 assert_eq!(updates.update_fail_htlcs.len(), 1);
10512                 assert!(updates.update_fail_malformed_htlcs.is_empty());
10513                 assert!(updates.update_fee.is_none());
10514                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
10515                 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
10516                 expect_payment_failed!(nodes[0], payment_hash, true);
10517
10518                 // Finally, succeed the keysend payment.
10519                 claim_payment(&nodes[0], &expected_route, payment_preimage);
10520
10521                 // To start (3), send a keysend payment but don't claim it.
10522                 let payment_id_1 = PaymentId([44; 32]);
10523                 let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
10524                         RecipientOnionFields::spontaneous_empty(), payment_id_1).unwrap();
10525                 check_added_monitors!(nodes[0], 1);
10526                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
10527                 assert_eq!(events.len(), 1);
10528                 let event = events.pop().unwrap();
10529                 let path = vec![&nodes[1]];
10530                 pass_along_path(&nodes[0], &path, 100_000, payment_hash, None, event, true, Some(payment_preimage));
10531
10532                 // Next, attempt a keysend payment and make sure it fails.
10533                 let route_params = RouteParameters::from_payment_params_and_value(
10534                         PaymentParameters::for_keysend(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV, false),
10535                         100_000
10536                 );
10537                 let route = find_route(
10538                         &nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph,
10539                         None, nodes[0].logger, &scorer, &Default::default(), &random_seed_bytes
10540                 ).unwrap();
10541                 let payment_id_2 = PaymentId([45; 32]);
10542                 nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
10543                         RecipientOnionFields::spontaneous_empty(), payment_id_2).unwrap();
10544                 check_added_monitors!(nodes[0], 1);
10545                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
10546                 assert_eq!(events.len(), 1);
10547                 let ev = events.drain(..).next().unwrap();
10548                 let payment_event = SendEvent::from_event(ev);
10549                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
10550                 check_added_monitors!(nodes[1], 0);
10551                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
10552                 expect_pending_htlcs_forwardable!(nodes[1]);
10553                 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash }]);
10554                 check_added_monitors!(nodes[1], 1);
10555                 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
10556                 assert!(updates.update_add_htlcs.is_empty());
10557                 assert!(updates.update_fulfill_htlcs.is_empty());
10558                 assert_eq!(updates.update_fail_htlcs.len(), 1);
10559                 assert!(updates.update_fail_malformed_htlcs.is_empty());
10560                 assert!(updates.update_fee.is_none());
10561                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
10562                 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
10563                 expect_payment_failed!(nodes[0], payment_hash, true);
10564
10565                 // Finally, claim the original payment.
10566                 claim_payment(&nodes[0], &expected_route, payment_preimage);
10567         }
10568
10569         #[test]
10570         fn test_keysend_hash_mismatch() {
10571                 // Test that if we receive a keysend `update_add_htlc` msg, we fail as expected if the keysend
10572                 // preimage doesn't match the msg's payment hash.
10573                 let chanmon_cfgs = create_chanmon_cfgs(2);
10574                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10575                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10576                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10577
10578                 let payer_pubkey = nodes[0].node.get_our_node_id();
10579                 let payee_pubkey = nodes[1].node.get_our_node_id();
10580
10581                 let _chan = create_chan_between_nodes(&nodes[0], &nodes[1]);
10582                 let route_params = RouteParameters::from_payment_params_and_value(
10583                         PaymentParameters::for_keysend(payee_pubkey, 40, false), 10_000);
10584                 let network_graph = nodes[0].network_graph.clone();
10585                 let first_hops = nodes[0].node.list_usable_channels();
10586                 let scorer = test_utils::TestScorer::new();
10587                 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
10588                 let route = find_route(
10589                         &payer_pubkey, &route_params, &network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
10590                         nodes[0].logger, &scorer, &Default::default(), &random_seed_bytes
10591                 ).unwrap();
10592
10593                 let test_preimage = PaymentPreimage([42; 32]);
10594                 let mismatch_payment_hash = PaymentHash([43; 32]);
10595                 let session_privs = nodes[0].node.test_add_new_pending_payment(mismatch_payment_hash,
10596                         RecipientOnionFields::spontaneous_empty(), PaymentId(mismatch_payment_hash.0), &route).unwrap();
10597                 nodes[0].node.test_send_payment_internal(&route, mismatch_payment_hash,
10598                         RecipientOnionFields::spontaneous_empty(), Some(test_preimage), PaymentId(mismatch_payment_hash.0), None, session_privs).unwrap();
10599                 check_added_monitors!(nodes[0], 1);
10600
10601                 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
10602                 assert_eq!(updates.update_add_htlcs.len(), 1);
10603                 assert!(updates.update_fulfill_htlcs.is_empty());
10604                 assert!(updates.update_fail_htlcs.is_empty());
10605                 assert!(updates.update_fail_malformed_htlcs.is_empty());
10606                 assert!(updates.update_fee.is_none());
10607                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
10608
10609                 nodes[1].logger.assert_log_contains("lightning::ln::channelmanager", "Payment preimage didn't match payment hash", 1);
10610         }
10611
10612         #[test]
10613         fn test_keysend_msg_with_secret_err() {
10614                 // Test that we error as expected if we receive a keysend payment that includes a payment
10615                 // secret when we don't support MPP keysend.
10616                 let mut reject_mpp_keysend_cfg = test_default_channel_config();
10617                 reject_mpp_keysend_cfg.accept_mpp_keysend = false;
10618                 let chanmon_cfgs = create_chanmon_cfgs(2);
10619                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10620                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(reject_mpp_keysend_cfg)]);
10621                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10622
10623                 let payer_pubkey = nodes[0].node.get_our_node_id();
10624                 let payee_pubkey = nodes[1].node.get_our_node_id();
10625
10626                 let _chan = create_chan_between_nodes(&nodes[0], &nodes[1]);
10627                 let route_params = RouteParameters::from_payment_params_and_value(
10628                         PaymentParameters::for_keysend(payee_pubkey, 40, false), 10_000);
10629                 let network_graph = nodes[0].network_graph.clone();
10630                 let first_hops = nodes[0].node.list_usable_channels();
10631                 let scorer = test_utils::TestScorer::new();
10632                 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
10633                 let route = find_route(
10634                         &payer_pubkey, &route_params, &network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
10635                         nodes[0].logger, &scorer, &Default::default(), &random_seed_bytes
10636                 ).unwrap();
10637
10638                 let test_preimage = PaymentPreimage([42; 32]);
10639                 let test_secret = PaymentSecret([43; 32]);
10640                 let payment_hash = PaymentHash(Sha256::hash(&test_preimage.0).into_inner());
10641                 let session_privs = nodes[0].node.test_add_new_pending_payment(payment_hash,
10642                         RecipientOnionFields::secret_only(test_secret), PaymentId(payment_hash.0), &route).unwrap();
10643                 nodes[0].node.test_send_payment_internal(&route, payment_hash,
10644                         RecipientOnionFields::secret_only(test_secret), Some(test_preimage),
10645                         PaymentId(payment_hash.0), None, session_privs).unwrap();
10646                 check_added_monitors!(nodes[0], 1);
10647
10648                 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
10649                 assert_eq!(updates.update_add_htlcs.len(), 1);
10650                 assert!(updates.update_fulfill_htlcs.is_empty());
10651                 assert!(updates.update_fail_htlcs.is_empty());
10652                 assert!(updates.update_fail_malformed_htlcs.is_empty());
10653                 assert!(updates.update_fee.is_none());
10654                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
10655
10656                 nodes[1].logger.assert_log_contains("lightning::ln::channelmanager", "We don't support MPP keysend payments", 1);
10657         }
10658
10659         #[test]
10660         fn test_multi_hop_missing_secret() {
10661                 let chanmon_cfgs = create_chanmon_cfgs(4);
10662                 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
10663                 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
10664                 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
10665
10666                 let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
10667                 let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2).0.contents.short_channel_id;
10668                 let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents.short_channel_id;
10669                 let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3).0.contents.short_channel_id;
10670
10671                 // Marshall an MPP route.
10672                 let (mut route, payment_hash, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[3], 100000);
10673                 let path = route.paths[0].clone();
10674                 route.paths.push(path);
10675                 route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
10676                 route.paths[0].hops[0].short_channel_id = chan_1_id;
10677                 route.paths[0].hops[1].short_channel_id = chan_3_id;
10678                 route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
10679                 route.paths[1].hops[0].short_channel_id = chan_2_id;
10680                 route.paths[1].hops[1].short_channel_id = chan_4_id;
10681
10682                 match nodes[0].node.send_payment_with_route(&route, payment_hash,
10683                         RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0))
10684                 .unwrap_err() {
10685                         PaymentSendFailure::ParameterError(APIError::APIMisuseError { ref err }) => {
10686                                 assert!(regex::Regex::new(r"Payment secret is required for multi-path payments").unwrap().is_match(err))
10687                         },
10688                         _ => panic!("unexpected error")
10689                 }
10690         }
10691
10692         #[test]
10693         fn test_drop_disconnected_peers_when_removing_channels() {
10694                 let chanmon_cfgs = create_chanmon_cfgs(2);
10695                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10696                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10697                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10698
10699                 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
10700
10701                 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
10702                 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
10703
10704                 nodes[0].node.force_close_broadcasting_latest_txn(&chan.2, &nodes[1].node.get_our_node_id()).unwrap();
10705                 check_closed_broadcast!(nodes[0], true);
10706                 check_added_monitors!(nodes[0], 1);
10707                 check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
10708
10709                 {
10710                         // Assert that nodes[1] is awaiting removal for nodes[0] once nodes[1] has been
10711                         // disconnected and the channel between has been force closed.
10712                         let nodes_0_per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
10713                         // Assert that nodes[1] isn't removed before `timer_tick_occurred` has been executed.
10714                         assert_eq!(nodes_0_per_peer_state.len(), 1);
10715                         assert!(nodes_0_per_peer_state.get(&nodes[1].node.get_our_node_id()).is_some());
10716                 }
10717
10718                 nodes[0].node.timer_tick_occurred();
10719
10720                 {
10721                         // Assert that nodes[1] has now been removed.
10722                         assert_eq!(nodes[0].node.per_peer_state.read().unwrap().len(), 0);
10723                 }
10724         }
10725
10726         #[test]
10727         fn bad_inbound_payment_hash() {
10728                 // Add coverage for checking that a user-provided payment hash matches the payment secret.
10729                 let chanmon_cfgs = create_chanmon_cfgs(2);
10730                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10731                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10732                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10733
10734                 let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(&nodes[0]);
10735                 let payment_data = msgs::FinalOnionHopData {
10736                         payment_secret,
10737                         total_msat: 100_000,
10738                 };
10739
10740                 // Ensure that if the payment hash given to `inbound_payment::verify` differs from the original,
10741                 // payment verification fails as expected.
10742                 let mut bad_payment_hash = payment_hash.clone();
10743                 bad_payment_hash.0[0] += 1;
10744                 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) {
10745                         Ok(_) => panic!("Unexpected ok"),
10746                         Err(()) => {
10747                                 nodes[0].logger.assert_log_contains("lightning::ln::inbound_payment", "Failing HTLC with user-generated payment_hash", 1);
10748                         }
10749                 }
10750
10751                 // Check that using the original payment hash succeeds.
10752                 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());
10753         }
10754
10755         #[test]
10756         fn test_id_to_peer_coverage() {
10757                 // Test that the `ChannelManager:id_to_peer` contains channels which have been assigned
10758                 // a `channel_id` (i.e. have had the funding tx created), and that they are removed once
10759                 // the channel is successfully closed.
10760                 let chanmon_cfgs = create_chanmon_cfgs(2);
10761                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10762                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10763                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10764
10765                 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None).unwrap();
10766                 let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
10767                 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
10768                 let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
10769                 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
10770
10771                 let (temporary_channel_id, tx, _funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 1_000_000, 42);
10772                 let channel_id = ChannelId::from_bytes(tx.txid().into_inner());
10773                 {
10774                         // Ensure that the `id_to_peer` map is empty until either party has received the
10775                         // funding transaction, and have the real `channel_id`.
10776                         assert_eq!(nodes[0].node.id_to_peer.lock().unwrap().len(), 0);
10777                         assert_eq!(nodes[1].node.id_to_peer.lock().unwrap().len(), 0);
10778                 }
10779
10780                 nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
10781                 {
10782                         // Assert that `nodes[0]`'s `id_to_peer` map is populated with the channel as soon as
10783                         // as it has the funding transaction.
10784                         let nodes_0_lock = nodes[0].node.id_to_peer.lock().unwrap();
10785                         assert_eq!(nodes_0_lock.len(), 1);
10786                         assert!(nodes_0_lock.contains_key(&channel_id));
10787                 }
10788
10789                 assert_eq!(nodes[1].node.id_to_peer.lock().unwrap().len(), 0);
10790
10791                 let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
10792
10793                 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
10794                 {
10795                         let nodes_0_lock = nodes[0].node.id_to_peer.lock().unwrap();
10796                         assert_eq!(nodes_0_lock.len(), 1);
10797                         assert!(nodes_0_lock.contains_key(&channel_id));
10798                 }
10799                 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
10800
10801                 {
10802                         // Assert that `nodes[1]`'s `id_to_peer` map is populated with the channel as soon as
10803                         // as it has the funding transaction.
10804                         let nodes_1_lock = nodes[1].node.id_to_peer.lock().unwrap();
10805                         assert_eq!(nodes_1_lock.len(), 1);
10806                         assert!(nodes_1_lock.contains_key(&channel_id));
10807                 }
10808                 check_added_monitors!(nodes[1], 1);
10809                 let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
10810                 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
10811                 check_added_monitors!(nodes[0], 1);
10812                 expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
10813                 let (channel_ready, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
10814                 let (announcement, nodes_0_update, nodes_1_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
10815                 update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &nodes_0_update, &nodes_1_update);
10816
10817                 nodes[0].node.close_channel(&channel_id, &nodes[1].node.get_our_node_id()).unwrap();
10818                 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id()));
10819                 let nodes_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
10820                 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &nodes_1_shutdown);
10821
10822                 let closing_signed_node_0 = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
10823                 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &closing_signed_node_0);
10824                 {
10825                         // Assert that the channel is kept in the `id_to_peer` map for both nodes until the
10826                         // channel can be fully closed by both parties (i.e. no outstanding htlcs exists, the
10827                         // fee for the closing transaction has been negotiated and the parties has the other
10828                         // party's signature for the fee negotiated closing transaction.)
10829                         let nodes_0_lock = nodes[0].node.id_to_peer.lock().unwrap();
10830                         assert_eq!(nodes_0_lock.len(), 1);
10831                         assert!(nodes_0_lock.contains_key(&channel_id));
10832                 }
10833
10834                 {
10835                         // At this stage, `nodes[1]` has proposed a fee for the closing transaction in the
10836                         // `handle_closing_signed` call above. As `nodes[1]` has not yet received the signature
10837                         // from `nodes[0]` for the closing transaction with the proposed fee, the channel is
10838                         // kept in the `nodes[1]`'s `id_to_peer` map.
10839                         let nodes_1_lock = nodes[1].node.id_to_peer.lock().unwrap();
10840                         assert_eq!(nodes_1_lock.len(), 1);
10841                         assert!(nodes_1_lock.contains_key(&channel_id));
10842                 }
10843
10844                 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id()));
10845                 {
10846                         // `nodes[0]` accepts `nodes[1]`'s proposed fee for the closing transaction, and
10847                         // therefore has all it needs to fully close the channel (both signatures for the
10848                         // closing transaction).
10849                         // Assert that the channel is removed from `nodes[0]`'s `id_to_peer` map as it can be
10850                         // fully closed by `nodes[0]`.
10851                         assert_eq!(nodes[0].node.id_to_peer.lock().unwrap().len(), 0);
10852
10853                         // Assert that the channel is still in `nodes[1]`'s  `id_to_peer` map, as `nodes[1]`
10854                         // doesn't have `nodes[0]`'s signature for the closing transaction yet.
10855                         let nodes_1_lock = nodes[1].node.id_to_peer.lock().unwrap();
10856                         assert_eq!(nodes_1_lock.len(), 1);
10857                         assert!(nodes_1_lock.contains_key(&channel_id));
10858                 }
10859
10860                 let (_nodes_0_update, closing_signed_node_0) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
10861
10862                 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &closing_signed_node_0.unwrap());
10863                 {
10864                         // Assert that the channel has now been removed from both parties `id_to_peer` map once
10865                         // they both have everything required to fully close the channel.
10866                         assert_eq!(nodes[1].node.id_to_peer.lock().unwrap().len(), 0);
10867                 }
10868                 let (_nodes_1_update, _none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
10869
10870                 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 1000000);
10871                 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 1000000);
10872         }
10873
10874         fn check_not_connected_to_peer_error<T>(res_err: Result<T, APIError>, expected_public_key: PublicKey) {
10875                 let expected_message = format!("Not connected to node: {}", expected_public_key);
10876                 check_api_error_message(expected_message, res_err)
10877         }
10878
10879         fn check_unkown_peer_error<T>(res_err: Result<T, APIError>, expected_public_key: PublicKey) {
10880                 let expected_message = format!("Can't find a peer matching the passed counterparty node_id {}", expected_public_key);
10881                 check_api_error_message(expected_message, res_err)
10882         }
10883
10884         fn check_channel_unavailable_error<T>(res_err: Result<T, APIError>, expected_channel_id: ChannelId, peer_node_id: PublicKey) {
10885                 let expected_message = format!("Channel with id {} not found for the passed counterparty node_id {}", expected_channel_id, peer_node_id);
10886                 check_api_error_message(expected_message, res_err)
10887         }
10888
10889         fn check_api_misuse_error<T>(res_err: Result<T, APIError>) {
10890                 let expected_message = "No such channel awaiting to be accepted.".to_string();
10891                 check_api_error_message(expected_message, res_err)
10892         }
10893
10894         fn check_api_error_message<T>(expected_err_message: String, res_err: Result<T, APIError>) {
10895                 match res_err {
10896                         Err(APIError::APIMisuseError { err }) => {
10897                                 assert_eq!(err, expected_err_message);
10898                         },
10899                         Err(APIError::ChannelUnavailable { err }) => {
10900                                 assert_eq!(err, expected_err_message);
10901                         },
10902                         Ok(_) => panic!("Unexpected Ok"),
10903                         Err(_) => panic!("Unexpected Error"),
10904                 }
10905         }
10906
10907         #[test]
10908         fn test_api_calls_with_unkown_counterparty_node() {
10909                 // Tests that our API functions that expects a `counterparty_node_id` as input, behaves as
10910                 // expected if the `counterparty_node_id` is an unkown peer in the
10911                 // `ChannelManager::per_peer_state` map.
10912                 let chanmon_cfg = create_chanmon_cfgs(2);
10913                 let node_cfg = create_node_cfgs(2, &chanmon_cfg);
10914                 let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[None, None]);
10915                 let nodes = create_network(2, &node_cfg, &node_chanmgr);
10916
10917                 // Dummy values
10918                 let channel_id = ChannelId::from_bytes([4; 32]);
10919                 let unkown_public_key = PublicKey::from_secret_key(&Secp256k1::signing_only(), &SecretKey::from_slice(&[42; 32]).unwrap());
10920                 let intercept_id = InterceptId([0; 32]);
10921
10922                 // Test the API functions.
10923                 check_not_connected_to_peer_error(nodes[0].node.create_channel(unkown_public_key, 1_000_000, 500_000_000, 42, None), unkown_public_key);
10924
10925                 check_unkown_peer_error(nodes[0].node.accept_inbound_channel(&channel_id, &unkown_public_key, 42), unkown_public_key);
10926
10927                 check_unkown_peer_error(nodes[0].node.close_channel(&channel_id, &unkown_public_key), unkown_public_key);
10928
10929                 check_unkown_peer_error(nodes[0].node.force_close_broadcasting_latest_txn(&channel_id, &unkown_public_key), unkown_public_key);
10930
10931                 check_unkown_peer_error(nodes[0].node.force_close_without_broadcasting_txn(&channel_id, &unkown_public_key), unkown_public_key);
10932
10933                 check_unkown_peer_error(nodes[0].node.forward_intercepted_htlc(intercept_id, &channel_id, unkown_public_key, 1_000_000), unkown_public_key);
10934
10935                 check_unkown_peer_error(nodes[0].node.update_channel_config(&unkown_public_key, &[channel_id], &ChannelConfig::default()), unkown_public_key);
10936         }
10937
10938         #[test]
10939         fn test_api_calls_with_unavailable_channel() {
10940                 // Tests that our API functions that expects a `counterparty_node_id` and a `channel_id`
10941                 // as input, behaves as expected if the `counterparty_node_id` is a known peer in the
10942                 // `ChannelManager::per_peer_state` map, but the peer state doesn't contain a channel with
10943                 // the given `channel_id`.
10944                 let chanmon_cfg = create_chanmon_cfgs(2);
10945                 let node_cfg = create_node_cfgs(2, &chanmon_cfg);
10946                 let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[None, None]);
10947                 let nodes = create_network(2, &node_cfg, &node_chanmgr);
10948
10949                 let counterparty_node_id = nodes[1].node.get_our_node_id();
10950
10951                 // Dummy values
10952                 let channel_id = ChannelId::from_bytes([4; 32]);
10953
10954                 // Test the API functions.
10955                 check_api_misuse_error(nodes[0].node.accept_inbound_channel(&channel_id, &counterparty_node_id, 42));
10956
10957                 check_channel_unavailable_error(nodes[0].node.close_channel(&channel_id, &counterparty_node_id), channel_id, counterparty_node_id);
10958
10959                 check_channel_unavailable_error(nodes[0].node.force_close_broadcasting_latest_txn(&channel_id, &counterparty_node_id), channel_id, counterparty_node_id);
10960
10961                 check_channel_unavailable_error(nodes[0].node.force_close_without_broadcasting_txn(&channel_id, &counterparty_node_id), channel_id, counterparty_node_id);
10962
10963                 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);
10964
10965                 check_channel_unavailable_error(nodes[0].node.update_channel_config(&counterparty_node_id, &[channel_id], &ChannelConfig::default()), channel_id, counterparty_node_id);
10966         }
10967
10968         #[test]
10969         fn test_connection_limiting() {
10970                 // Test that we limit un-channel'd peers and un-funded channels properly.
10971                 let chanmon_cfgs = create_chanmon_cfgs(2);
10972                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10973                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10974                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10975
10976                 // Note that create_network connects the nodes together for us
10977
10978                 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
10979                 let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
10980
10981                 let mut funding_tx = None;
10982                 for idx in 0..super::MAX_UNFUNDED_CHANS_PER_PEER {
10983                         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
10984                         let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
10985
10986                         if idx == 0 {
10987                                 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
10988                                 let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100_000, 42);
10989                                 funding_tx = Some(tx.clone());
10990                                 nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx).unwrap();
10991                                 let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
10992
10993                                 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
10994                                 check_added_monitors!(nodes[1], 1);
10995                                 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
10996
10997                                 let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
10998
10999                                 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
11000                                 check_added_monitors!(nodes[0], 1);
11001                                 expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
11002                         }
11003                         open_channel_msg.temporary_channel_id = ChannelId::temporary_from_entropy_source(&nodes[0].keys_manager);
11004                 }
11005
11006                 // A MAX_UNFUNDED_CHANS_PER_PEER + 1 channel will be summarily rejected
11007                 open_channel_msg.temporary_channel_id = ChannelId::temporary_from_entropy_source(&nodes[0].keys_manager);
11008                 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
11009                 assert_eq!(get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id()).channel_id,
11010                         open_channel_msg.temporary_channel_id);
11011
11012                 // Further, because all of our channels with nodes[0] are inbound, and none of them funded,
11013                 // it doesn't count as a "protected" peer, i.e. it counts towards the MAX_NO_CHANNEL_PEERS
11014                 // limit.
11015                 let mut peer_pks = Vec::with_capacity(super::MAX_NO_CHANNEL_PEERS);
11016                 for _ in 1..super::MAX_NO_CHANNEL_PEERS {
11017                         let random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
11018                                 &SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
11019                         peer_pks.push(random_pk);
11020                         nodes[1].node.peer_connected(&random_pk, &msgs::Init {
11021                                 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
11022                         }, true).unwrap();
11023                 }
11024                 let last_random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
11025                         &SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
11026                 nodes[1].node.peer_connected(&last_random_pk, &msgs::Init {
11027                         features: nodes[0].node.init_features(), networks: None, remote_network_address: None
11028                 }, true).unwrap_err();
11029
11030                 // Also importantly, because nodes[0] isn't "protected", we will refuse a reconnection from
11031                 // them if we have too many un-channel'd peers.
11032                 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
11033                 let chan_closed_events = nodes[1].node.get_and_clear_pending_events();
11034                 assert_eq!(chan_closed_events.len(), super::MAX_UNFUNDED_CHANS_PER_PEER - 1);
11035                 for ev in chan_closed_events {
11036                         if let Event::ChannelClosed { .. } = ev { } else { panic!(); }
11037                 }
11038                 nodes[1].node.peer_connected(&last_random_pk, &msgs::Init {
11039                         features: nodes[0].node.init_features(), networks: None, remote_network_address: None
11040                 }, true).unwrap();
11041                 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
11042                         features: nodes[0].node.init_features(), networks: None, remote_network_address: None
11043                 }, true).unwrap_err();
11044
11045                 // but of course if the connection is outbound its allowed...
11046                 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
11047                         features: nodes[0].node.init_features(), networks: None, remote_network_address: None
11048                 }, false).unwrap();
11049                 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
11050
11051                 // Now nodes[0] is disconnected but still has a pending, un-funded channel lying around.
11052                 // Even though we accept one more connection from new peers, we won't actually let them
11053                 // open channels.
11054                 assert!(peer_pks.len() > super::MAX_UNFUNDED_CHANNEL_PEERS - 1);
11055                 for i in 0..super::MAX_UNFUNDED_CHANNEL_PEERS - 1 {
11056                         nodes[1].node.handle_open_channel(&peer_pks[i], &open_channel_msg);
11057                         get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, peer_pks[i]);
11058                         open_channel_msg.temporary_channel_id = ChannelId::temporary_from_entropy_source(&nodes[0].keys_manager);
11059                 }
11060                 nodes[1].node.handle_open_channel(&last_random_pk, &open_channel_msg);
11061                 assert_eq!(get_err_msg(&nodes[1], &last_random_pk).channel_id,
11062                         open_channel_msg.temporary_channel_id);
11063
11064                 // Of course, however, outbound channels are always allowed
11065                 nodes[1].node.create_channel(last_random_pk, 100_000, 0, 42, None).unwrap();
11066                 get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, last_random_pk);
11067
11068                 // If we fund the first channel, nodes[0] has a live on-chain channel with us, it is now
11069                 // "protected" and can connect again.
11070                 mine_transaction(&nodes[1], funding_tx.as_ref().unwrap());
11071                 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
11072                         features: nodes[0].node.init_features(), networks: None, remote_network_address: None
11073                 }, true).unwrap();
11074                 get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
11075
11076                 // Further, because the first channel was funded, we can open another channel with
11077                 // last_random_pk.
11078                 nodes[1].node.handle_open_channel(&last_random_pk, &open_channel_msg);
11079                 get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, last_random_pk);
11080         }
11081
11082         #[test]
11083         fn test_outbound_chans_unlimited() {
11084                 // Test that we never refuse an outbound channel even if a peer is unfuned-channel-limited
11085                 let chanmon_cfgs = create_chanmon_cfgs(2);
11086                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
11087                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
11088                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
11089
11090                 // Note that create_network connects the nodes together for us
11091
11092                 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
11093                 let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
11094
11095                 for _ in 0..super::MAX_UNFUNDED_CHANS_PER_PEER {
11096                         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
11097                         get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
11098                         open_channel_msg.temporary_channel_id = ChannelId::temporary_from_entropy_source(&nodes[0].keys_manager);
11099                 }
11100
11101                 // Once we have MAX_UNFUNDED_CHANS_PER_PEER unfunded channels, new inbound channels will be
11102                 // rejected.
11103                 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
11104                 assert_eq!(get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id()).channel_id,
11105                         open_channel_msg.temporary_channel_id);
11106
11107                 // but we can still open an outbound channel.
11108                 nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
11109                 get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
11110
11111                 // but even with such an outbound channel, additional inbound channels will still fail.
11112                 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
11113                 assert_eq!(get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id()).channel_id,
11114                         open_channel_msg.temporary_channel_id);
11115         }
11116
11117         #[test]
11118         fn test_0conf_limiting() {
11119                 // Tests that we properly limit inbound channels when we have the manual-channel-acceptance
11120                 // flag set and (sometimes) accept channels as 0conf.
11121                 let chanmon_cfgs = create_chanmon_cfgs(2);
11122                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
11123                 let mut settings = test_default_channel_config();
11124                 settings.manually_accept_inbound_channels = true;
11125                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(settings)]);
11126                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
11127
11128                 // Note that create_network connects the nodes together for us
11129
11130                 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
11131                 let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
11132
11133                 // First, get us up to MAX_UNFUNDED_CHANNEL_PEERS so we can test at the edge
11134                 for _ in 0..super::MAX_UNFUNDED_CHANNEL_PEERS - 1 {
11135                         let random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
11136                                 &SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
11137                         nodes[1].node.peer_connected(&random_pk, &msgs::Init {
11138                                 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
11139                         }, true).unwrap();
11140
11141                         nodes[1].node.handle_open_channel(&random_pk, &open_channel_msg);
11142                         let events = nodes[1].node.get_and_clear_pending_events();
11143                         match events[0] {
11144                                 Event::OpenChannelRequest { temporary_channel_id, .. } => {
11145                                         nodes[1].node.accept_inbound_channel(&temporary_channel_id, &random_pk, 23).unwrap();
11146                                 }
11147                                 _ => panic!("Unexpected event"),
11148                         }
11149                         get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, random_pk);
11150                         open_channel_msg.temporary_channel_id = ChannelId::temporary_from_entropy_source(&nodes[0].keys_manager);
11151                 }
11152
11153                 // If we try to accept a channel from another peer non-0conf it will fail.
11154                 let last_random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
11155                         &SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
11156                 nodes[1].node.peer_connected(&last_random_pk, &msgs::Init {
11157                         features: nodes[0].node.init_features(), networks: None, remote_network_address: None
11158                 }, true).unwrap();
11159                 nodes[1].node.handle_open_channel(&last_random_pk, &open_channel_msg);
11160                 let events = nodes[1].node.get_and_clear_pending_events();
11161                 match events[0] {
11162                         Event::OpenChannelRequest { temporary_channel_id, .. } => {
11163                                 match nodes[1].node.accept_inbound_channel(&temporary_channel_id, &last_random_pk, 23) {
11164                                         Err(APIError::APIMisuseError { err }) =>
11165                                                 assert_eq!(err, "Too many peers with unfunded channels, refusing to accept new ones"),
11166                                         _ => panic!(),
11167                                 }
11168                         }
11169                         _ => panic!("Unexpected event"),
11170                 }
11171                 assert_eq!(get_err_msg(&nodes[1], &last_random_pk).channel_id,
11172                         open_channel_msg.temporary_channel_id);
11173
11174                 // ...however if we accept the same channel 0conf it should work just fine.
11175                 nodes[1].node.handle_open_channel(&last_random_pk, &open_channel_msg);
11176                 let events = nodes[1].node.get_and_clear_pending_events();
11177                 match events[0] {
11178                         Event::OpenChannelRequest { temporary_channel_id, .. } => {
11179                                 nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(&temporary_channel_id, &last_random_pk, 23).unwrap();
11180                         }
11181                         _ => panic!("Unexpected event"),
11182                 }
11183                 get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, last_random_pk);
11184         }
11185
11186         #[test]
11187         fn reject_excessively_underpaying_htlcs() {
11188                 let chanmon_cfg = create_chanmon_cfgs(1);
11189                 let node_cfg = create_node_cfgs(1, &chanmon_cfg);
11190                 let node_chanmgr = create_node_chanmgrs(1, &node_cfg, &[None]);
11191                 let node = create_network(1, &node_cfg, &node_chanmgr);
11192                 let sender_intended_amt_msat = 100;
11193                 let extra_fee_msat = 10;
11194                 let hop_data = msgs::InboundOnionPayload::Receive {
11195                         amt_msat: 100,
11196                         outgoing_cltv_value: 42,
11197                         payment_metadata: None,
11198                         keysend_preimage: None,
11199                         payment_data: Some(msgs::FinalOnionHopData {
11200                                 payment_secret: PaymentSecret([0; 32]), total_msat: sender_intended_amt_msat,
11201                         }),
11202                         custom_tlvs: Vec::new(),
11203                 };
11204                 // Check that if the amount we received + the penultimate hop extra fee is less than the sender
11205                 // intended amount, we fail the payment.
11206                 if let Err(crate::ln::channelmanager::InboundOnionErr { err_code, .. }) =
11207                         node[0].node.construct_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
11208                                 sender_intended_amt_msat - extra_fee_msat - 1, 42, None, true, Some(extra_fee_msat))
11209                 {
11210                         assert_eq!(err_code, 19);
11211                 } else { panic!(); }
11212
11213                 // If amt_received + extra_fee is equal to the sender intended amount, we're fine.
11214                 let hop_data = msgs::InboundOnionPayload::Receive { // This is the same payload as above, InboundOnionPayload doesn't implement Clone
11215                         amt_msat: 100,
11216                         outgoing_cltv_value: 42,
11217                         payment_metadata: None,
11218                         keysend_preimage: None,
11219                         payment_data: Some(msgs::FinalOnionHopData {
11220                                 payment_secret: PaymentSecret([0; 32]), total_msat: sender_intended_amt_msat,
11221                         }),
11222                         custom_tlvs: Vec::new(),
11223                 };
11224                 assert!(node[0].node.construct_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
11225                         sender_intended_amt_msat - extra_fee_msat, 42, None, true, Some(extra_fee_msat)).is_ok());
11226         }
11227
11228         #[test]
11229         fn test_final_incorrect_cltv(){
11230                 let chanmon_cfg = create_chanmon_cfgs(1);
11231                 let node_cfg = create_node_cfgs(1, &chanmon_cfg);
11232                 let node_chanmgr = create_node_chanmgrs(1, &node_cfg, &[None]);
11233                 let node = create_network(1, &node_cfg, &node_chanmgr);
11234
11235                 let result = node[0].node.construct_recv_pending_htlc_info(msgs::InboundOnionPayload::Receive {
11236                         amt_msat: 100,
11237                         outgoing_cltv_value: 22,
11238                         payment_metadata: None,
11239                         keysend_preimage: None,
11240                         payment_data: Some(msgs::FinalOnionHopData {
11241                                 payment_secret: PaymentSecret([0; 32]), total_msat: 100,
11242                         }),
11243                         custom_tlvs: Vec::new(),
11244                 }, [0; 32], PaymentHash([0; 32]), 100, 23, None, true, None);
11245
11246                 // Should not return an error as this condition:
11247                 // https://github.com/lightning/bolts/blob/4dcc377209509b13cf89a4b91fde7d478f5b46d8/04-onion-routing.md?plain=1#L334
11248                 // is not satisfied.
11249                 assert!(result.is_ok());
11250         }
11251
11252         #[test]
11253         fn test_inbound_anchors_manual_acceptance() {
11254                 // Tests that we properly limit inbound channels when we have the manual-channel-acceptance
11255                 // flag set and (sometimes) accept channels as 0conf.
11256                 let mut anchors_cfg = test_default_channel_config();
11257                 anchors_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
11258
11259                 let mut anchors_manual_accept_cfg = anchors_cfg.clone();
11260                 anchors_manual_accept_cfg.manually_accept_inbound_channels = true;
11261
11262                 let chanmon_cfgs = create_chanmon_cfgs(3);
11263                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
11264                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs,
11265                         &[Some(anchors_cfg.clone()), Some(anchors_cfg.clone()), Some(anchors_manual_accept_cfg.clone())]);
11266                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
11267
11268                 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
11269                 let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
11270
11271                 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
11272                 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
11273                 let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
11274                 match &msg_events[0] {
11275                         MessageSendEvent::HandleError { node_id, action } => {
11276                                 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
11277                                 match action {
11278                                         ErrorAction::SendErrorMessage { msg } =>
11279                                                 assert_eq!(msg.data, "No channels with anchor outputs accepted".to_owned()),
11280                                         _ => panic!("Unexpected error action"),
11281                                 }
11282                         }
11283                         _ => panic!("Unexpected event"),
11284                 }
11285
11286                 nodes[2].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
11287                 let events = nodes[2].node.get_and_clear_pending_events();
11288                 match events[0] {
11289                         Event::OpenChannelRequest { temporary_channel_id, .. } =>
11290                                 nodes[2].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 23).unwrap(),
11291                         _ => panic!("Unexpected event"),
11292                 }
11293                 get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
11294         }
11295
11296         #[test]
11297         fn test_anchors_zero_fee_htlc_tx_fallback() {
11298                 // Tests that if both nodes support anchors, but the remote node does not want to accept
11299                 // anchor channels at the moment, an error it sent to the local node such that it can retry
11300                 // the channel without the anchors feature.
11301                 let chanmon_cfgs = create_chanmon_cfgs(2);
11302                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
11303                 let mut anchors_config = test_default_channel_config();
11304                 anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
11305                 anchors_config.manually_accept_inbound_channels = true;
11306                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config.clone()), Some(anchors_config.clone())]);
11307                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
11308
11309                 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 0, None).unwrap();
11310                 let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
11311                 assert!(open_channel_msg.channel_type.as_ref().unwrap().supports_anchors_zero_fee_htlc_tx());
11312
11313                 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
11314                 let events = nodes[1].node.get_and_clear_pending_events();
11315                 match events[0] {
11316                         Event::OpenChannelRequest { temporary_channel_id, .. } => {
11317                                 nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id()).unwrap();
11318                         }
11319                         _ => panic!("Unexpected event"),
11320                 }
11321
11322                 let error_msg = get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id());
11323                 nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &error_msg);
11324
11325                 let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
11326                 assert!(!open_channel_msg.channel_type.unwrap().supports_anchors_zero_fee_htlc_tx());
11327
11328                 // Since nodes[1] should not have accepted the channel, it should
11329                 // not have generated any events.
11330                 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
11331         }
11332
11333         #[test]
11334         fn test_update_channel_config() {
11335                 let chanmon_cfg = create_chanmon_cfgs(2);
11336                 let node_cfg = create_node_cfgs(2, &chanmon_cfg);
11337                 let mut user_config = test_default_channel_config();
11338                 let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[Some(user_config), Some(user_config)]);
11339                 let nodes = create_network(2, &node_cfg, &node_chanmgr);
11340                 let _ = create_announced_chan_between_nodes(&nodes, 0, 1);
11341                 let channel = &nodes[0].node.list_channels()[0];
11342
11343                 nodes[0].node.update_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &user_config.channel_config).unwrap();
11344                 let events = nodes[0].node.get_and_clear_pending_msg_events();
11345                 assert_eq!(events.len(), 0);
11346
11347                 user_config.channel_config.forwarding_fee_base_msat += 10;
11348                 nodes[0].node.update_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &user_config.channel_config).unwrap();
11349                 assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_base_msat, user_config.channel_config.forwarding_fee_base_msat);
11350                 let events = nodes[0].node.get_and_clear_pending_msg_events();
11351                 assert_eq!(events.len(), 1);
11352                 match &events[0] {
11353                         MessageSendEvent::BroadcastChannelUpdate { .. } => {},
11354                         _ => panic!("expected BroadcastChannelUpdate event"),
11355                 }
11356
11357                 nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &ChannelConfigUpdate::default()).unwrap();
11358                 let events = nodes[0].node.get_and_clear_pending_msg_events();
11359                 assert_eq!(events.len(), 0);
11360
11361                 let new_cltv_expiry_delta = user_config.channel_config.cltv_expiry_delta + 6;
11362                 nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &ChannelConfigUpdate {
11363                         cltv_expiry_delta: Some(new_cltv_expiry_delta),
11364                         ..Default::default()
11365                 }).unwrap();
11366                 assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().cltv_expiry_delta, new_cltv_expiry_delta);
11367                 let events = nodes[0].node.get_and_clear_pending_msg_events();
11368                 assert_eq!(events.len(), 1);
11369                 match &events[0] {
11370                         MessageSendEvent::BroadcastChannelUpdate { .. } => {},
11371                         _ => panic!("expected BroadcastChannelUpdate event"),
11372                 }
11373
11374                 let new_fee = user_config.channel_config.forwarding_fee_proportional_millionths + 100;
11375                 nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &ChannelConfigUpdate {
11376                         forwarding_fee_proportional_millionths: Some(new_fee),
11377                         ..Default::default()
11378                 }).unwrap();
11379                 assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().cltv_expiry_delta, new_cltv_expiry_delta);
11380                 assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths, new_fee);
11381                 let events = nodes[0].node.get_and_clear_pending_msg_events();
11382                 assert_eq!(events.len(), 1);
11383                 match &events[0] {
11384                         MessageSendEvent::BroadcastChannelUpdate { .. } => {},
11385                         _ => panic!("expected BroadcastChannelUpdate event"),
11386                 }
11387
11388                 // If we provide a channel_id not associated with the peer, we should get an error and no updates
11389                 // should be applied to ensure update atomicity as specified in the API docs.
11390                 let bad_channel_id = ChannelId::v1_from_funding_txid(&[10; 32], 10);
11391                 let current_fee = nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths;
11392                 let new_fee = current_fee + 100;
11393                 assert!(
11394                         matches!(
11395                                 nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id, bad_channel_id], &ChannelConfigUpdate {
11396                                         forwarding_fee_proportional_millionths: Some(new_fee),
11397                                         ..Default::default()
11398                                 }),
11399                                 Err(APIError::ChannelUnavailable { err: _ }),
11400                         )
11401                 );
11402                 // Check that the fee hasn't changed for the channel that exists.
11403                 assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths, current_fee);
11404                 let events = nodes[0].node.get_and_clear_pending_msg_events();
11405                 assert_eq!(events.len(), 0);
11406         }
11407
11408         #[test]
11409         fn test_payment_display() {
11410                 let payment_id = PaymentId([42; 32]);
11411                 assert_eq!(format!("{}", &payment_id), "2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a");
11412                 let payment_hash = PaymentHash([42; 32]);
11413                 assert_eq!(format!("{}", &payment_hash), "2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a");
11414                 let payment_preimage = PaymentPreimage([42; 32]);
11415                 assert_eq!(format!("{}", &payment_preimage), "2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a");
11416         }
11417
11418         #[test]
11419         fn test_trigger_lnd_force_close() {
11420                 let chanmon_cfg = create_chanmon_cfgs(2);
11421                 let node_cfg = create_node_cfgs(2, &chanmon_cfg);
11422                 let user_config = test_default_channel_config();
11423                 let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[Some(user_config), Some(user_config)]);
11424                 let nodes = create_network(2, &node_cfg, &node_chanmgr);
11425
11426                 // Open a channel, immediately disconnect each other, and broadcast Alice's latest state.
11427                 let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1);
11428                 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
11429                 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
11430                 nodes[0].node.force_close_broadcasting_latest_txn(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
11431                 check_closed_broadcast(&nodes[0], 1, true);
11432                 check_added_monitors(&nodes[0], 1);
11433                 check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
11434                 {
11435                         let txn = nodes[0].tx_broadcaster.txn_broadcast();
11436                         assert_eq!(txn.len(), 1);
11437                         check_spends!(txn[0], funding_tx);
11438                 }
11439
11440                 // Since they're disconnected, Bob won't receive Alice's `Error` message. Reconnect them
11441                 // such that Bob sends a `ChannelReestablish` to Alice since the channel is still open from
11442                 // their side.
11443                 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
11444                         features: nodes[1].node.init_features(), networks: None, remote_network_address: None
11445                 }, true).unwrap();
11446                 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
11447                         features: nodes[0].node.init_features(), networks: None, remote_network_address: None
11448                 }, false).unwrap();
11449                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
11450                 let channel_reestablish = get_event_msg!(
11451                         nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id()
11452                 );
11453                 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &channel_reestablish);
11454
11455                 // Alice should respond with an error since the channel isn't known, but a bogus
11456                 // `ChannelReestablish` should be sent first, such that we actually trigger Bob to force
11457                 // close even if it was an lnd node.
11458                 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
11459                 assert_eq!(msg_events.len(), 2);
11460                 if let MessageSendEvent::SendChannelReestablish { node_id, msg } = &msg_events[0] {
11461                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
11462                         assert_eq!(msg.next_local_commitment_number, 0);
11463                         assert_eq!(msg.next_remote_commitment_number, 0);
11464                         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &msg);
11465                 } else { panic!() };
11466                 check_closed_broadcast(&nodes[1], 1, true);
11467                 check_added_monitors(&nodes[1], 1);
11468                 let expected_close_reason = ClosureReason::ProcessingError {
11469                         err: "Peer sent an invalid channel_reestablish to force close in a non-standard way".to_string()
11470                 };
11471                 check_closed_event!(nodes[1], 1, expected_close_reason, [nodes[0].node.get_our_node_id()], 100000);
11472                 {
11473                         let txn = nodes[1].tx_broadcaster.txn_broadcast();
11474                         assert_eq!(txn.len(), 1);
11475                         check_spends!(txn[0], funding_tx);
11476                 }
11477         }
11478 }
11479
11480 #[cfg(ldk_bench)]
11481 pub mod bench {
11482         use crate::chain::Listen;
11483         use crate::chain::chainmonitor::{ChainMonitor, Persist};
11484         use crate::sign::{KeysManager, InMemorySigner};
11485         use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider};
11486         use crate::ln::channelmanager::{BestBlock, ChainParameters, ChannelManager, PaymentHash, PaymentPreimage, PaymentId, RecipientOnionFields, Retry};
11487         use crate::ln::functional_test_utils::*;
11488         use crate::ln::msgs::{ChannelMessageHandler, Init};
11489         use crate::routing::gossip::NetworkGraph;
11490         use crate::routing::router::{PaymentParameters, RouteParameters};
11491         use crate::util::test_utils;
11492         use crate::util::config::{UserConfig, MaxDustHTLCExposure};
11493
11494         use bitcoin::hashes::Hash;
11495         use bitcoin::hashes::sha256::Hash as Sha256;
11496         use bitcoin::{Block, BlockHeader, PackedLockTime, Transaction, TxMerkleNode, TxOut};
11497
11498         use crate::sync::{Arc, Mutex, RwLock};
11499
11500         use criterion::Criterion;
11501
11502         type Manager<'a, P> = ChannelManager<
11503                 &'a ChainMonitor<InMemorySigner, &'a test_utils::TestChainSource,
11504                         &'a test_utils::TestBroadcaster, &'a test_utils::TestFeeEstimator,
11505                         &'a test_utils::TestLogger, &'a P>,
11506                 &'a test_utils::TestBroadcaster, &'a KeysManager, &'a KeysManager, &'a KeysManager,
11507                 &'a test_utils::TestFeeEstimator, &'a test_utils::TestRouter<'a>,
11508                 &'a test_utils::TestLogger>;
11509
11510         struct ANodeHolder<'node_cfg, 'chan_mon_cfg: 'node_cfg, P: Persist<InMemorySigner>> {
11511                 node: &'node_cfg Manager<'chan_mon_cfg, P>,
11512         }
11513         impl<'node_cfg, 'chan_mon_cfg: 'node_cfg, P: Persist<InMemorySigner>> NodeHolder for ANodeHolder<'node_cfg, 'chan_mon_cfg, P> {
11514                 type CM = Manager<'chan_mon_cfg, P>;
11515                 #[inline]
11516                 fn node(&self) -> &Manager<'chan_mon_cfg, P> { self.node }
11517                 #[inline]
11518                 fn chain_monitor(&self) -> Option<&test_utils::TestChainMonitor> { None }
11519         }
11520
11521         pub fn bench_sends(bench: &mut Criterion) {
11522                 bench_two_sends(bench, "bench_sends", test_utils::TestPersister::new(), test_utils::TestPersister::new());
11523         }
11524
11525         pub fn bench_two_sends<P: Persist<InMemorySigner>>(bench: &mut Criterion, bench_name: &str, persister_a: P, persister_b: P) {
11526                 // Do a simple benchmark of sending a payment back and forth between two nodes.
11527                 // Note that this is unrealistic as each payment send will require at least two fsync
11528                 // calls per node.
11529                 let network = bitcoin::Network::Testnet;
11530                 let genesis_block = bitcoin::blockdata::constants::genesis_block(network);
11531
11532                 let tx_broadcaster = test_utils::TestBroadcaster::new(network);
11533                 let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
11534                 let logger_a = test_utils::TestLogger::with_id("node a".to_owned());
11535                 let scorer = RwLock::new(test_utils::TestScorer::new());
11536                 let router = test_utils::TestRouter::new(Arc::new(NetworkGraph::new(network, &logger_a)), &scorer);
11537
11538                 let mut config: UserConfig = Default::default();
11539                 config.channel_config.max_dust_htlc_exposure = MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253);
11540                 config.channel_handshake_config.minimum_depth = 1;
11541
11542                 let chain_monitor_a = ChainMonitor::new(None, &tx_broadcaster, &logger_a, &fee_estimator, &persister_a);
11543                 let seed_a = [1u8; 32];
11544                 let keys_manager_a = KeysManager::new(&seed_a, 42, 42);
11545                 let node_a = ChannelManager::new(&fee_estimator, &chain_monitor_a, &tx_broadcaster, &router, &logger_a, &keys_manager_a, &keys_manager_a, &keys_manager_a, config.clone(), ChainParameters {
11546                         network,
11547                         best_block: BestBlock::from_network(network),
11548                 }, genesis_block.header.time);
11549                 let node_a_holder = ANodeHolder { node: &node_a };
11550
11551                 let logger_b = test_utils::TestLogger::with_id("node a".to_owned());
11552                 let chain_monitor_b = ChainMonitor::new(None, &tx_broadcaster, &logger_a, &fee_estimator, &persister_b);
11553                 let seed_b = [2u8; 32];
11554                 let keys_manager_b = KeysManager::new(&seed_b, 42, 42);
11555                 let node_b = ChannelManager::new(&fee_estimator, &chain_monitor_b, &tx_broadcaster, &router, &logger_b, &keys_manager_b, &keys_manager_b, &keys_manager_b, config.clone(), ChainParameters {
11556                         network,
11557                         best_block: BestBlock::from_network(network),
11558                 }, genesis_block.header.time);
11559                 let node_b_holder = ANodeHolder { node: &node_b };
11560
11561                 node_a.peer_connected(&node_b.get_our_node_id(), &Init {
11562                         features: node_b.init_features(), networks: None, remote_network_address: None
11563                 }, true).unwrap();
11564                 node_b.peer_connected(&node_a.get_our_node_id(), &Init {
11565                         features: node_a.init_features(), networks: None, remote_network_address: None
11566                 }, false).unwrap();
11567                 node_a.create_channel(node_b.get_our_node_id(), 8_000_000, 100_000_000, 42, None).unwrap();
11568                 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()));
11569                 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()));
11570
11571                 let tx;
11572                 if let Event::FundingGenerationReady { temporary_channel_id, output_script, .. } = get_event!(node_a_holder, Event::FundingGenerationReady) {
11573                         tx = Transaction { version: 2, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: vec![TxOut {
11574                                 value: 8_000_000, script_pubkey: output_script,
11575                         }]};
11576                         node_a.funding_transaction_generated(&temporary_channel_id, &node_b.get_our_node_id(), tx.clone()).unwrap();
11577                 } else { panic!(); }
11578
11579                 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()));
11580                 let events_b = node_b.get_and_clear_pending_events();
11581                 assert_eq!(events_b.len(), 1);
11582                 match events_b[0] {
11583                         Event::ChannelPending{ ref counterparty_node_id, .. } => {
11584                                 assert_eq!(*counterparty_node_id, node_a.get_our_node_id());
11585                         },
11586                         _ => panic!("Unexpected event"),
11587                 }
11588
11589                 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()));
11590                 let events_a = node_a.get_and_clear_pending_events();
11591                 assert_eq!(events_a.len(), 1);
11592                 match events_a[0] {
11593                         Event::ChannelPending{ ref counterparty_node_id, .. } => {
11594                                 assert_eq!(*counterparty_node_id, node_b.get_our_node_id());
11595                         },
11596                         _ => panic!("Unexpected event"),
11597                 }
11598
11599                 assert_eq!(&tx_broadcaster.txn_broadcasted.lock().unwrap()[..], &[tx.clone()]);
11600
11601                 let block = create_dummy_block(BestBlock::from_network(network).block_hash(), 42, vec![tx]);
11602                 Listen::block_connected(&node_a, &block, 1);
11603                 Listen::block_connected(&node_b, &block, 1);
11604
11605                 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()));
11606                 let msg_events = node_a.get_and_clear_pending_msg_events();
11607                 assert_eq!(msg_events.len(), 2);
11608                 match msg_events[0] {
11609                         MessageSendEvent::SendChannelReady { ref msg, .. } => {
11610                                 node_b.handle_channel_ready(&node_a.get_our_node_id(), msg);
11611                                 get_event_msg!(node_b_holder, MessageSendEvent::SendChannelUpdate, node_a.get_our_node_id());
11612                         },
11613                         _ => panic!(),
11614                 }
11615                 match msg_events[1] {
11616                         MessageSendEvent::SendChannelUpdate { .. } => {},
11617                         _ => panic!(),
11618                 }
11619
11620                 let events_a = node_a.get_and_clear_pending_events();
11621                 assert_eq!(events_a.len(), 1);
11622                 match events_a[0] {
11623                         Event::ChannelReady{ ref counterparty_node_id, .. } => {
11624                                 assert_eq!(*counterparty_node_id, node_b.get_our_node_id());
11625                         },
11626                         _ => panic!("Unexpected event"),
11627                 }
11628
11629                 let events_b = node_b.get_and_clear_pending_events();
11630                 assert_eq!(events_b.len(), 1);
11631                 match events_b[0] {
11632                         Event::ChannelReady{ ref counterparty_node_id, .. } => {
11633                                 assert_eq!(*counterparty_node_id, node_a.get_our_node_id());
11634                         },
11635                         _ => panic!("Unexpected event"),
11636                 }
11637
11638                 let mut payment_count: u64 = 0;
11639                 macro_rules! send_payment {
11640                         ($node_a: expr, $node_b: expr) => {
11641                                 let payment_params = PaymentParameters::from_node_id($node_b.get_our_node_id(), TEST_FINAL_CLTV)
11642                                         .with_bolt11_features($node_b.invoice_features()).unwrap();
11643                                 let mut payment_preimage = PaymentPreimage([0; 32]);
11644                                 payment_preimage.0[0..8].copy_from_slice(&payment_count.to_le_bytes());
11645                                 payment_count += 1;
11646                                 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
11647                                 let payment_secret = $node_b.create_inbound_payment_for_hash(payment_hash, None, 7200, None).unwrap();
11648
11649                                 $node_a.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
11650                                         PaymentId(payment_hash.0),
11651                                         RouteParameters::from_payment_params_and_value(payment_params, 10_000),
11652                                         Retry::Attempts(0)).unwrap();
11653                                 let payment_event = SendEvent::from_event($node_a.get_and_clear_pending_msg_events().pop().unwrap());
11654                                 $node_b.handle_update_add_htlc(&$node_a.get_our_node_id(), &payment_event.msgs[0]);
11655                                 $node_b.handle_commitment_signed(&$node_a.get_our_node_id(), &payment_event.commitment_msg);
11656                                 let (raa, cs) = get_revoke_commit_msgs(&ANodeHolder { node: &$node_b }, &$node_a.get_our_node_id());
11657                                 $node_a.handle_revoke_and_ack(&$node_b.get_our_node_id(), &raa);
11658                                 $node_a.handle_commitment_signed(&$node_b.get_our_node_id(), &cs);
11659                                 $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()));
11660
11661                                 expect_pending_htlcs_forwardable!(ANodeHolder { node: &$node_b });
11662                                 expect_payment_claimable!(ANodeHolder { node: &$node_b }, payment_hash, payment_secret, 10_000);
11663                                 $node_b.claim_funds(payment_preimage);
11664                                 expect_payment_claimed!(ANodeHolder { node: &$node_b }, payment_hash, 10_000);
11665
11666                                 match $node_b.get_and_clear_pending_msg_events().pop().unwrap() {
11667                                         MessageSendEvent::UpdateHTLCs { node_id, updates } => {
11668                                                 assert_eq!(node_id, $node_a.get_our_node_id());
11669                                                 $node_a.handle_update_fulfill_htlc(&$node_b.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
11670                                                 $node_a.handle_commitment_signed(&$node_b.get_our_node_id(), &updates.commitment_signed);
11671                                         },
11672                                         _ => panic!("Failed to generate claim event"),
11673                                 }
11674
11675                                 let (raa, cs) = get_revoke_commit_msgs(&ANodeHolder { node: &$node_a }, &$node_b.get_our_node_id());
11676                                 $node_b.handle_revoke_and_ack(&$node_a.get_our_node_id(), &raa);
11677                                 $node_b.handle_commitment_signed(&$node_a.get_our_node_id(), &cs);
11678                                 $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()));
11679
11680                                 expect_payment_sent!(ANodeHolder { node: &$node_a }, payment_preimage);
11681                         }
11682                 }
11683
11684                 bench.bench_function(bench_name, |b| b.iter(|| {
11685                         send_payment!(node_a, node_b);
11686                         send_payment!(node_b, node_a);
11687                 }));
11688         }
11689 }