Merge pull request #1119 from TheBlueMatt/2021-10-less-aggressive-htlc-timeouts
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Tue, 16 Nov 2021 16:18:20 +0000 (16:18 +0000)
committerGitHub <noreply@github.com>
Tue, 16 Nov 2021 16:18:20 +0000 (16:18 +0000)
Be less aggressive in outbound HTLC CLTV timeout checks

1  2 
lightning/src/chain/channelmonitor.rs
lightning/src/ln/channel.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/functional_tests.rs

index 8be785f29d2c38c3165023816e8821b1853418d1,d7395c337fc96a6193f61bb5dd4271ced09179e5..425a1890a8f46acfabd68773dbc32f51172e2676
@@@ -115,6 -115,67 +115,6 @@@ impl Readable for ChannelMonitorUpdate 
        }
  }
  
 -/// An error enum representing a failure to persist a channel monitor update.
 -#[derive(Clone, Copy, Debug, PartialEq)]
 -pub enum ChannelMonitorUpdateErr {
 -      /// Used to indicate a temporary failure (eg connection to a watchtower or remote backup of
 -      /// our state failed, but is expected to succeed at some point in the future).
 -      ///
 -      /// Such a failure will "freeze" a channel, preventing us from revoking old states or
 -      /// submitting new commitment transactions to the counterparty. Once the update(s) which failed
 -      /// have been successfully applied, ChannelManager::channel_monitor_updated can be used to
 -      /// restore the channel to an operational state.
 -      ///
 -      /// Note that a given ChannelManager will *never* re-generate a given ChannelMonitorUpdate. If
 -      /// you return a TemporaryFailure you must ensure that it is written to disk safely before
 -      /// writing out the latest ChannelManager state.
 -      ///
 -      /// Even when a channel has been "frozen" updates to the ChannelMonitor can continue to occur
 -      /// (eg if an inbound HTLC which we forwarded was claimed upstream resulting in us attempting
 -      /// to claim it on this channel) and those updates must be applied wherever they can be. At
 -      /// least one such updated ChannelMonitor must be persisted otherwise PermanentFailure should
 -      /// be returned to get things on-chain ASAP using only the in-memory copy. Obviously updates to
 -      /// the channel which would invalidate previous ChannelMonitors are not made when a channel has
 -      /// been "frozen".
 -      ///
 -      /// Note that even if updates made after TemporaryFailure succeed you must still call
 -      /// channel_monitor_updated to ensure you have the latest monitor and re-enable normal channel
 -      /// operation.
 -      ///
 -      /// Note that the update being processed here will not be replayed for you when you call
 -      /// ChannelManager::channel_monitor_updated, so you must store the update itself along
 -      /// with the persisted ChannelMonitor on your own local disk prior to returning a
 -      /// TemporaryFailure. You may, of course, employ a journaling approach, storing only the
 -      /// ChannelMonitorUpdate on disk without updating the monitor itself, replaying the journal at
 -      /// reload-time.
 -      ///
 -      /// For deployments where a copy of ChannelMonitors and other local state are backed up in a
 -      /// remote location (with local copies persisted immediately), it is anticipated that all
 -      /// updates will return TemporaryFailure until the remote copies could be updated.
 -      TemporaryFailure,
 -      /// Used to indicate no further channel monitor updates will be allowed (eg we've moved on to a
 -      /// different watchtower and cannot update with all watchtowers that were previously informed
 -      /// of this channel).
 -      ///
 -      /// At reception of this error, ChannelManager will force-close the channel and return at
 -      /// least a final ChannelMonitorUpdate::ChannelForceClosed which must be delivered to at
 -      /// least one ChannelMonitor copy. Revocation secret MUST NOT be released and offchain channel
 -      /// update must be rejected.
 -      ///
 -      /// This failure may also signal a failure to update the local persisted copy of one of
 -      /// the channel monitor instance.
 -      ///
 -      /// Note that even when you fail a holder commitment transaction update, you must store the
 -      /// update to ensure you can claim from it in case of a duplicate copy of this ChannelMonitor
 -      /// broadcasts it (e.g distributed channel-monitor deployment)
 -      ///
 -      /// In case of distributed watchtowers deployment, the new version must be written to disk, as
 -      /// state may have been stored but rejected due to a block forcing a commitment broadcast. This
 -      /// storage is used to claim outputs of rejected state confirmed onchain by another watchtower,
 -      /// lagging behind on block processing.
 -      PermanentFailure,
 -}
 -
  /// General Err type for ChannelMonitor actions. Generally, this implies that the data provided is
  /// inconsistent with the ChannelMonitor being called. eg for ChannelMonitor::update_monitor this
  /// means you tried to update a monitor for a different channel or the ChannelMonitorUpdate was
@@@ -131,40 -192,7 +131,40 @@@ pub enum MonitorEvent 
  
        /// A monitor event that the Channel's commitment transaction was confirmed.
        CommitmentTxConfirmed(OutPoint),
 +
 +      /// Indicates a [`ChannelMonitor`] update has completed. See
 +      /// [`ChannelMonitorUpdateErr::TemporaryFailure`] for more information on how this is used.
 +      ///
 +      /// [`ChannelMonitorUpdateErr::TemporaryFailure`]: super::ChannelMonitorUpdateErr::TemporaryFailure
 +      UpdateCompleted {
 +              /// The funding outpoint of the [`ChannelMonitor`] that was updated
 +              funding_txo: OutPoint,
 +              /// The Update ID from [`ChannelMonitorUpdate::update_id`] which was applied or
 +              /// [`ChannelMonitor::get_latest_update_id`].
 +              ///
 +              /// Note that this should only be set to a given update's ID if all previous updates for the
 +              /// same [`ChannelMonitor`] have been applied and persisted.
 +              monitor_update_id: u64,
 +      },
 +
 +      /// Indicates a [`ChannelMonitor`] update has failed. See
 +      /// [`ChannelMonitorUpdateErr::PermanentFailure`] for more information on how this is used.
 +      ///
 +      /// [`ChannelMonitorUpdateErr::PermanentFailure`]: super::ChannelMonitorUpdateErr::PermanentFailure
 +      UpdateFailed(OutPoint),
  }
 +impl_writeable_tlv_based_enum_upgradable!(MonitorEvent,
 +      // Note that UpdateCompleted and UpdateFailed are currently never serialized to disk as they are
 +      // generated only in ChainMonitor
 +      (0, UpdateCompleted) => {
 +              (0, funding_txo, required),
 +              (2, monitor_update_id, required),
 +      },
 +;
 +      (2, HTLCEvent),
 +      (4, CommitmentTxConfirmed),
 +      (6, UpdateFailed),
 +);
  
  /// Simple structure sent back by `chain::Watch` when an HTLC from a forward channel is detected on
  /// chain. Used to update the corresponding HTLC in the backward channel. Failing to pass the
@@@ -225,8 -253,6 +225,6 @@@ pub const ANTI_REORG_DELAY: u32 = 6
  ///    fail this HTLC,
  /// 2) if we receive an HTLC within this many blocks of its expiry (plus one to avoid a race
  ///    condition with the above), we will fail this HTLC without telling the user we received it,
- /// 3) if we are waiting on a connection or a channel state update to send an HTLC to a peer, and
- ///    that HTLC expires within this many blocks, we will simply fail the HTLC instead.
  ///
  /// (1) is all about protecting us - we need enough time to update the channel state before we hit
  /// CLTV_CLAIM_BUFFER, at which point we'd go on chain to claim the HTLC with the preimage.
  /// (2) is the same, but with an additional buffer to avoid accepting an HTLC which is immediately
  /// in a race condition between the user connecting a block (which would fail it) and the user
  /// providing us the preimage (which would claim it).
- ///
- /// (3) is about our counterparty - we don't want to relay an HTLC to a counterparty when they may
- /// end up force-closing the channel on us to claim it.
  pub(crate) const HTLC_FAIL_BACK_BUFFER: u32 = CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS;
  
  // TODO(devrandom) replace this with HolderCommitmentTransaction
@@@ -657,17 -680,7 +652,17 @@@ pub(crate) struct ChannelMonitorImpl<Si
  
        payment_preimages: HashMap<PaymentHash, PaymentPreimage>,
  
 +      // Note that `MonitorEvent`s MUST NOT be generated during update processing, only generated
 +      // during chain data processing. This prevents a race in `ChainMonitor::update_channel` (and
 +      // presumably user implementations thereof as well) where we update the in-memory channel
 +      // object, then before the persistence finishes (as it's all under a read-lock), we return
 +      // pending events to the user or to the relevant `ChannelManager`. Then, on reload, we'll have
 +      // the pre-event state here, but have processed the event in the `ChannelManager`.
 +      // Note that because the `event_lock` in `ChainMonitor` is only taken in
 +      // block/transaction-connected events and *not* during block/transaction-disconnected events,
 +      // we further MUST NOT generate events during block/transaction-disconnection.
        pending_monitor_events: Vec<MonitorEvent>,
 +
        pending_events: Vec<Event>,
  
        // Used to track on-chain events (i.e., transactions part of channels confirmed on chain) on
@@@ -893,19 -906,14 +888,19 @@@ impl<Signer: Sign> Writeable for Channe
                        writer.write_all(&payment_preimage.0[..])?;
                }
  
 -              writer.write_all(&byte_utils::be64_to_array(self.pending_monitor_events.len() as u64))?;
 +              writer.write_all(&(self.pending_monitor_events.iter().filter(|ev| match ev {
 +                      MonitorEvent::HTLCEvent(_) => true,
 +                      MonitorEvent::CommitmentTxConfirmed(_) => true,
 +                      _ => false,
 +              }).count() as u64).to_be_bytes())?;
                for event in self.pending_monitor_events.iter() {
                        match event {
                                MonitorEvent::HTLCEvent(upd) => {
                                        0u8.write(writer)?;
                                        upd.write(writer)?;
                                },
 -                              MonitorEvent::CommitmentTxConfirmed(_) => 1u8.write(writer)?
 +                              MonitorEvent::CommitmentTxConfirmed(_) => 1u8.write(writer)?,
 +                              _ => {}, // Covered in the TLV writes below
                        }
                }
  
                write_tlv_fields!(writer, {
                        (1, self.funding_spend_confirmed, option),
                        (3, self.htlcs_resolved_on_chain, vec_type),
 +                      (5, self.pending_monitor_events, vec_type),
                });
  
                Ok(())
@@@ -1515,101 -1522,6 +1510,101 @@@ impl<Signer: Sign> ChannelMonitor<Signe
  
                res
        }
 +
 +      /// Gets the set of outbound HTLCs which are pending resolution in this channel.
 +      /// This is used to reconstruct pending outbound payments on restart in the ChannelManager.
 +      pub(crate) fn get_pending_outbound_htlcs(&self) -> HashMap<HTLCSource, HTLCOutputInCommitment> {
 +              let mut res = HashMap::new();
 +              let us = self.inner.lock().unwrap();
 +
 +              macro_rules! walk_htlcs {
 +                      ($holder_commitment: expr, $htlc_iter: expr) => {
 +                              for (htlc, source) in $htlc_iter {
 +                                      if us.htlcs_resolved_on_chain.iter().any(|v| Some(v.input_idx) == htlc.transaction_output_index) {
 +                                              // We should assert that funding_spend_confirmed is_some() here, but we
 +                                              // have some unit tests which violate HTLC transaction CSVs entirely and
 +                                              // would fail.
 +                                              // TODO: Once tests all connect transactions at consensus-valid times, we
 +                                              // should assert here like we do in `get_claimable_balances`.
 +                                      } else if htlc.offered == $holder_commitment {
 +                                              // If the payment was outbound, check if there's an HTLCUpdate
 +                                              // indicating we have spent this HTLC with a timeout, claiming it back
 +                                              // and awaiting confirmations on it.
 +                                              let htlc_update_confd = us.onchain_events_awaiting_threshold_conf.iter().any(|event| {
 +                                                      if let OnchainEvent::HTLCUpdate { input_idx: Some(input_idx), .. } = event.event {
 +                                                              // If the HTLC was timed out, we wait for ANTI_REORG_DELAY blocks
 +                                                              // before considering it "no longer pending" - this matches when we
 +                                                              // provide the ChannelManager an HTLC failure event.
 +                                                              Some(input_idx) == htlc.transaction_output_index &&
 +                                                                      us.best_block.height() >= event.height + ANTI_REORG_DELAY - 1
 +                                                      } else if let OnchainEvent::HTLCSpendConfirmation { input_idx, .. } = event.event {
 +                                                              // If the HTLC was fulfilled with a preimage, we consider the HTLC
 +                                                              // immediately non-pending, matching when we provide ChannelManager
 +                                                              // the preimage.
 +                                                              Some(input_idx) == htlc.transaction_output_index
 +                                                      } else { false }
 +                                              });
 +                                              if !htlc_update_confd {
 +                                                      res.insert(source.clone(), htlc.clone());
 +                                              }
 +                                      }
 +                              }
 +                      }
 +              }
 +
 +              // We're only concerned with the confirmation count of HTLC transactions, and don't
 +              // actually care how many confirmations a commitment transaction may or may not have. Thus,
 +              // we look for either a FundingSpendConfirmation event or a funding_spend_confirmed.
 +              let confirmed_txid = us.funding_spend_confirmed.or_else(|| {
 +                      us.onchain_events_awaiting_threshold_conf.iter().find_map(|event| {
 +                              if let OnchainEvent::FundingSpendConfirmation { .. } = event.event {
 +                                      Some(event.txid)
 +                              } else { None }
 +                      })
 +              });
 +              if let Some(txid) = confirmed_txid {
 +                      if Some(txid) == us.current_counterparty_commitment_txid || Some(txid) == us.prev_counterparty_commitment_txid {
 +                              walk_htlcs!(false, us.counterparty_claimable_outpoints.get(&txid).unwrap().iter().filter_map(|(a, b)| {
 +                                      if let &Some(ref source) = b {
 +                                              Some((a, &**source))
 +                                      } else { None }
 +                              }));
 +                      } else if txid == us.current_holder_commitment_tx.txid {
 +                              walk_htlcs!(true, us.current_holder_commitment_tx.htlc_outputs.iter().filter_map(|(a, _, c)| {
 +                                      if let Some(source) = c { Some((a, source)) } else { None }
 +                              }));
 +                      } else if let Some(prev_commitment) = &us.prev_holder_signed_commitment_tx {
 +                              if txid == prev_commitment.txid {
 +                                      walk_htlcs!(true, prev_commitment.htlc_outputs.iter().filter_map(|(a, _, c)| {
 +                                              if let Some(source) = c { Some((a, source)) } else { None }
 +                                      }));
 +                              }
 +                      }
 +              } else {
 +                      // If we have not seen a commitment transaction on-chain (ie the channel is not yet
 +                      // closed), just examine the available counterparty commitment transactions. See docs
 +                      // on `fail_unbroadcast_htlcs`, below, for justification.
 +                      macro_rules! walk_counterparty_commitment {
 +                              ($txid: expr) => {
 +                                      if let Some(ref latest_outpoints) = us.counterparty_claimable_outpoints.get($txid) {
 +                                              for &(ref htlc, ref source_option) in latest_outpoints.iter() {
 +                                                      if let &Some(ref source) = source_option {
 +                                                              res.insert((**source).clone(), htlc.clone());
 +                                                      }
 +                                              }
 +                                      }
 +                              }
 +                      }
 +                      if let Some(ref txid) = us.current_counterparty_commitment_txid {
 +                              walk_counterparty_commitment!(txid);
 +                      }
 +                      if let Some(ref txid) = us.prev_counterparty_commitment_txid {
 +                              walk_counterparty_commitment!(txid);
 +                      }
 +              }
 +
 +              res
 +      }
  }
  
  /// Compares a broadcasted commitment transaction's HTLCs with those in the latest state,
@@@ -2698,10 -2610,8 +2693,10 @@@ impl<Signer: Sign> ChannelMonitorImpl<S
                        let revocation_sig_claim = (input.witness.len() == 3 && HTLCType::scriptlen_to_htlctype(input.witness[2].len()) == Some(HTLCType::OfferedHTLC) && input.witness[1].len() == 33)
                                || (input.witness.len() == 3 && HTLCType::scriptlen_to_htlctype(input.witness[2].len()) == Some(HTLCType::AcceptedHTLC) && input.witness[1].len() == 33);
                        let accepted_preimage_claim = input.witness.len() == 5 && HTLCType::scriptlen_to_htlctype(input.witness[4].len()) == Some(HTLCType::AcceptedHTLC);
 +                      #[cfg(not(fuzzing))]
                        let accepted_timeout_claim = input.witness.len() == 3 && HTLCType::scriptlen_to_htlctype(input.witness[2].len()) == Some(HTLCType::AcceptedHTLC) && !revocation_sig_claim;
                        let offered_preimage_claim = input.witness.len() == 3 && HTLCType::scriptlen_to_htlctype(input.witness[2].len()) == Some(HTLCType::OfferedHTLC) && !revocation_sig_claim;
 +                      #[cfg(not(fuzzing))]
                        let offered_timeout_claim = input.witness.len() == 5 && HTLCType::scriptlen_to_htlctype(input.witness[4].len()) == Some(HTLCType::OfferedHTLC);
  
                        let mut payment_preimage = PaymentPreimage([0; 32]);
        }
  }
  
 -/// `Persist` defines behavior for persisting channel monitors: this could mean
 -/// writing once to disk, and/or uploading to one or more backup services.
 -///
 -/// Note that for every new monitor, you **must** persist the new `ChannelMonitor`
 -/// to disk/backups. And, on every update, you **must** persist either the
 -/// `ChannelMonitorUpdate` or the updated monitor itself. Otherwise, there is risk
 -/// of situations such as revoking a transaction, then crashing before this
 -/// revocation can be persisted, then unintentionally broadcasting a revoked
 -/// transaction and losing money. This is a risk because previous channel states
 -/// are toxic, so it's important that whatever channel state is persisted is
 -/// kept up-to-date.
 -pub trait Persist<ChannelSigner: Sign> {
 -      /// Persist a new channel's data. The data can be stored any way you want, but
 -      /// the identifier provided by Rust-Lightning is the channel's outpoint (and
 -      /// it is up to you to maintain a correct mapping between the outpoint and the
 -      /// stored channel data). Note that you **must** persist every new monitor to
 -      /// disk. See the `Persist` trait documentation for more details.
 -      ///
 -      /// See [`ChannelMonitor::write`] for writing out a `ChannelMonitor`,
 -      /// and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
 -      fn persist_new_channel(&self, id: OutPoint, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
 -
 -      /// Update one channel's data. The provided `ChannelMonitor` has already
 -      /// applied the given update.
 -      ///
 -      /// Note that on every update, you **must** persist either the
 -      /// `ChannelMonitorUpdate` or the updated monitor itself to disk/backups. See
 -      /// the `Persist` trait documentation for more details.
 -      ///
 -      /// If an implementer chooses to persist the updates only, they need to make
 -      /// sure that all the updates are applied to the `ChannelMonitors` *before*
 -      /// the set of channel monitors is given to the `ChannelManager`
 -      /// deserialization routine. See [`ChannelMonitor::update_monitor`] for
 -      /// applying a monitor update to a monitor. If full `ChannelMonitors` are
 -      /// persisted, then there is no need to persist individual updates.
 -      ///
 -      /// Note that there could be a performance tradeoff between persisting complete
 -      /// channel monitors on every update vs. persisting only updates and applying
 -      /// them in batches. The size of each monitor grows `O(number of state updates)`
 -      /// whereas updates are small and `O(1)`.
 -      ///
 -      /// See [`ChannelMonitor::write`] for writing out a `ChannelMonitor`,
 -      /// [`ChannelMonitorUpdate::write`] for writing out an update, and
 -      /// [`ChannelMonitorUpdateErr`] for requirements when returning errors.
 -      fn update_persisted_channel(&self, id: OutPoint, update: &ChannelMonitorUpdate, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
 -}
 -
  impl<Signer: Sign, T: Deref, F: Deref, L: Deref> chain::Listen for (ChannelMonitor<Signer>, T, F, L)
  where
        T::Target: BroadcasterInterface,
@@@ -3144,15 -3101,14 +3139,15 @@@ impl<'a, Signer: Sign, K: KeysInterface
                }
  
                let pending_monitor_events_len: u64 = Readable::read(reader)?;
 -              let mut pending_monitor_events = Vec::with_capacity(cmp::min(pending_monitor_events_len as usize, MAX_ALLOC_SIZE / (32 + 8*3)));
 +              let mut pending_monitor_events = Some(
 +                      Vec::with_capacity(cmp::min(pending_monitor_events_len as usize, MAX_ALLOC_SIZE / (32 + 8*3))));
                for _ in 0..pending_monitor_events_len {
                        let ev = match <u8 as Readable>::read(reader)? {
                                0 => MonitorEvent::HTLCEvent(Readable::read(reader)?),
                                1 => MonitorEvent::CommitmentTxConfirmed(funding_info.0),
                                _ => return Err(DecodeError::InvalidValue)
                        };
 -                      pending_monitor_events.push(ev);
 +                      pending_monitor_events.as_mut().unwrap().push(ev);
                }
  
                let pending_events_len: u64 = Readable::read(reader)?;
                read_tlv_fields!(reader, {
                        (1, funding_spend_confirmed, option),
                        (3, htlcs_resolved_on_chain, vec_type),
 +                      (5, pending_monitor_events, vec_type),
                });
  
                let mut secp_ctx = Secp256k1::new();
                                current_holder_commitment_number,
  
                                payment_preimages,
 -                              pending_monitor_events,
 +                              pending_monitor_events: pending_monitor_events.unwrap(),
                                pending_events,
  
                                onchain_events_awaiting_threshold_conf,
index e39478bd740c063ce1f29a7dc853238ec75fcf48,5250abc4dac065ab458ec59d26857dc33bee4eb9..2976882c8dd5888ec0e1f0fdc6b74ef5d008e892
@@@ -23,7 -23,7 +23,7 @@@ use bitcoin::secp256k1::{Secp256k1,Sign
  use bitcoin::secp256k1;
  
  use ln::{PaymentPreimage, PaymentHash};
 -use ln::features::{ChannelFeatures, InitFeatures};
 +use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures};
  use ln::msgs;
  use ln::msgs::{DecodeError, OptionalField, DataLossProtect};
  use ln::script::{self, ShutdownScript};
@@@ -32,7 -32,7 +32,7 @@@ use ln::chan_utils::{CounterpartyCommit
  use ln::chan_utils;
  use chain::BestBlock;
  use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
- use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, HTLC_FAIL_BACK_BUFFER};
+ use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS};
  use chain::transaction::{OutPoint, TransactionData};
  use chain::keysinterface::{Sign, KeysInterface};
  use util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter};
@@@ -339,29 -339,6 +339,29 @@@ pub enum UpdateFulfillCommitFetch 
        DuplicateClaim {},
  }
  
 +/// The return value of `revoke_and_ack` on success, primarily updates to other channels or HTLC
 +/// state.
 +pub(super) struct RAAUpdates {
 +      pub commitment_update: Option<msgs::CommitmentUpdate>,
 +      pub accepted_htlcs: Vec<(PendingHTLCInfo, u64)>,
 +      pub failed_htlcs: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
 +      pub finalized_claimed_htlcs: Vec<HTLCSource>,
 +      pub monitor_update: ChannelMonitorUpdate,
 +      pub holding_cell_failed_htlcs: Vec<(HTLCSource, PaymentHash)>,
 +}
 +
 +/// The return value of `monitor_updating_restored`
 +pub(super) struct MonitorRestoreUpdates {
 +      pub raa: Option<msgs::RevokeAndACK>,
 +      pub commitment_update: Option<msgs::CommitmentUpdate>,
 +      pub order: RAACommitmentOrder,
 +      pub accepted_htlcs: Vec<(PendingHTLCInfo, u64)>,
 +      pub failed_htlcs: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
 +      pub finalized_claimed_htlcs: Vec<HTLCSource>,
 +      pub funding_broadcastable: Option<Transaction>,
 +      pub funding_locked: Option<msgs::FundingLocked>,
 +}
 +
  /// If the majority of the channels funds are to the fundee and the initiator holds only just
  /// enough funds to cover their reserve value, channels are at risk of getting "stuck". Because the
  /// initiator controls the feerate, if they then go to increase the channel fee, they may have no
@@@ -429,7 -406,6 +429,7 @@@ pub(super) struct Channel<Signer: Sign
        monitor_pending_commitment_signed: bool,
        monitor_pending_forwards: Vec<(PendingHTLCInfo, u64)>,
        monitor_pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
 +      monitor_pending_finalized_fulfills: Vec<HTLCSource>,
  
        // pending_update_fee is filled when sending and receiving update_fee.
        //
        // is fine, but as a sanity check in our failure to generate the second claim, we check here
        // that the original was a claim, and that we aren't now trying to fulfill a failed HTLC.
        historical_inbound_htlc_fulfills: HashSet<u64>,
 +
 +      /// This channel's type, as negotiated during channel open
 +      channel_type: ChannelTypeFeatures,
  }
  
  #[cfg(any(test, feature = "fuzztarget"))]
@@@ -719,7 -692,6 +719,7 @@@ impl<Signer: Sign> Channel<Signer> 
                        monitor_pending_commitment_signed: false,
                        monitor_pending_forwards: Vec::new(),
                        monitor_pending_failures: Vec::new(),
 +                      monitor_pending_finalized_fulfills: Vec::new(),
  
                        #[cfg(debug_assertions)]
                        holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
  
                        #[cfg(any(test, feature = "fuzztarget"))]
                        historical_inbound_htlc_fulfills: HashSet::new(),
 +
 +                      // We currently only actually support one channel type, so don't retry with new types
 +                      // on error messages. When we support more we'll need fallback support (assuming we
 +                      // want to support old types).
 +                      channel_type: ChannelTypeFeatures::only_static_remote_key(),
                })
        }
  
                where K::Target: KeysInterface<Signer = Signer>,
            F::Target: FeeEstimator
        {
 +              // First check the channel type is known, failing before we do anything else if we don't
 +              // support this channel type.
 +              let channel_type = if let Some(channel_type) = &msg.channel_type {
 +                      if channel_type.supports_any_optional_bits() {
 +                              return Err(ChannelError::Close("Channel Type field contained optional bits - this is not allowed".to_owned()));
 +                      }
 +                      if *channel_type != ChannelTypeFeatures::only_static_remote_key() {
 +                              return Err(ChannelError::Close("Channel Type was not understood".to_owned()));
 +                      }
 +                      channel_type.clone()
 +              } else {
 +                      ChannelTypeFeatures::from_counterparty_init(&their_features)
 +              };
 +              if !channel_type.supports_static_remote_key() {
 +                      return Err(ChannelError::Close("Channel Type was not understood - we require static remote key".to_owned()));
 +              }
 +
                let holder_signer = keys_provider.get_channel_signer(true, msg.funding_satoshis);
                let pubkeys = holder_signer.pubkeys().clone();
                let counterparty_pubkeys = ChannelPublicKeys {
                        monitor_pending_commitment_signed: false,
                        monitor_pending_forwards: Vec::new(),
                        monitor_pending_failures: Vec::new(),
 +                      monitor_pending_finalized_fulfills: Vec::new(),
  
                        #[cfg(debug_assertions)]
                        holder_max_commitment_tx_output: Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
  
                        #[cfg(any(test, feature = "fuzztarget"))]
                        historical_inbound_htlc_fulfills: HashSet::new(),
 +
 +                      channel_type,
                };
  
                Ok(chan)
        /// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
        /// generating an appropriate error *after* the channel state has been updated based on the
        /// revoke_and_ack message.
 -      pub fn revoke_and_ack<L: Deref>(&mut self, msg: &msgs::RevokeAndACK, logger: &L) -> Result<(Option<msgs::CommitmentUpdate>, Vec<(PendingHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, ChannelMonitorUpdate, Vec<(HTLCSource, PaymentHash)>), ChannelError>
 +      pub fn revoke_and_ack<L: Deref>(&mut self, msg: &msgs::RevokeAndACK, logger: &L) -> Result<RAAUpdates, ChannelError>
                where L::Target: Logger,
        {
                if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
                log_trace!(logger, "Updating HTLCs on receipt of RAA in channel {}...", log_bytes!(self.channel_id()));
                let mut to_forward_infos = Vec::new();
                let mut revoked_htlcs = Vec::new();
 +              let mut finalized_claimed_htlcs = Vec::new();
                let mut update_fail_htlcs = Vec::new();
                let mut update_fail_malformed_htlcs = Vec::new();
                let mut require_commitment = false;
                                        if let Some(reason) = fail_reason.clone() { // We really want take() here, but, again, non-mut ref :(
                                                revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
                                        } else {
 +                                              finalized_claimed_htlcs.push(htlc.source.clone());
                                                // They fulfilled, so we sent them money
                                                value_to_self_msat_diff -= htlc.amount_msat as i64;
                                        }
                        }
                        self.monitor_pending_forwards.append(&mut to_forward_infos);
                        self.monitor_pending_failures.append(&mut revoked_htlcs);
 +                      self.monitor_pending_finalized_fulfills.append(&mut finalized_claimed_htlcs);
                        log_debug!(logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply.", log_bytes!(self.channel_id()));
 -                      return Ok((None, Vec::new(), Vec::new(), monitor_update, Vec::new()))
 +                      return Ok(RAAUpdates {
 +                              commitment_update: None, finalized_claimed_htlcs: Vec::new(),
 +                              accepted_htlcs: Vec::new(), failed_htlcs: Vec::new(),
 +                              monitor_update,
 +                              holding_cell_failed_htlcs: Vec::new()
 +                      });
                }
  
                match self.free_holding_cell_htlcs(logger)? {
                                self.latest_monitor_update_id = monitor_update.update_id;
                                monitor_update.updates.append(&mut additional_update.updates);
  
 -                              Ok((Some(commitment_update), to_forward_infos, revoked_htlcs, monitor_update, htlcs_to_fail))
 +                              Ok(RAAUpdates {
 +                                      commitment_update: Some(commitment_update),
 +                                      finalized_claimed_htlcs,
 +                                      accepted_htlcs: to_forward_infos,
 +                                      failed_htlcs: revoked_htlcs,
 +                                      monitor_update,
 +                                      holding_cell_failed_htlcs: htlcs_to_fail
 +                              })
                        },
                        (None, htlcs_to_fail) => {
                                if require_commitment {
  
                                        log_debug!(logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed.",
                                                log_bytes!(self.channel_id()), update_fail_htlcs.len() + update_fail_malformed_htlcs.len());
 -                                      Ok((Some(msgs::CommitmentUpdate {
 -                                              update_add_htlcs: Vec::new(),
 -                                              update_fulfill_htlcs: Vec::new(),
 -                                              update_fail_htlcs,
 -                                              update_fail_malformed_htlcs,
 -                                              update_fee: None,
 -                                              commitment_signed
 -                                      }), to_forward_infos, revoked_htlcs, monitor_update, htlcs_to_fail))
 +                                      Ok(RAAUpdates {
 +                                              commitment_update: Some(msgs::CommitmentUpdate {
 +                                                      update_add_htlcs: Vec::new(),
 +                                                      update_fulfill_htlcs: Vec::new(),
 +                                                      update_fail_htlcs,
 +                                                      update_fail_malformed_htlcs,
 +                                                      update_fee: None,
 +                                                      commitment_signed
 +                                              }),
 +                                              finalized_claimed_htlcs,
 +                                              accepted_htlcs: to_forward_infos, failed_htlcs: revoked_htlcs,
 +                                              monitor_update, holding_cell_failed_htlcs: htlcs_to_fail
 +                                      })
                                } else {
                                        log_debug!(logger, "Received a valid revoke_and_ack for channel {} with no reply necessary.", log_bytes!(self.channel_id()));
 -                                      Ok((None, to_forward_infos, revoked_htlcs, monitor_update, htlcs_to_fail))
 +                                      Ok(RAAUpdates {
 +                                              commitment_update: None,
 +                                              finalized_claimed_htlcs,
 +                                              accepted_htlcs: to_forward_infos, failed_htlcs: revoked_htlcs,
 +                                              monitor_update, holding_cell_failed_htlcs: htlcs_to_fail
 +                                      })
                                }
                        }
                }
        /// which failed. The messages which were generated from that call which generated the
        /// monitor update failure must *not* have been sent to the remote end, and must instead
        /// have been dropped. They will be regenerated when monitor_updating_restored is called.
 -      pub fn monitor_update_failed(&mut self, resend_raa: bool, resend_commitment: bool, mut pending_forwards: Vec<(PendingHTLCInfo, u64)>, mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>) {
 +      pub fn monitor_update_failed(&mut self, resend_raa: bool, resend_commitment: bool,
 +              mut pending_forwards: Vec<(PendingHTLCInfo, u64)>,
 +              mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
 +              mut pending_finalized_claimed_htlcs: Vec<HTLCSource>
 +      ) {
                self.monitor_pending_revoke_and_ack |= resend_raa;
                self.monitor_pending_commitment_signed |= resend_commitment;
                self.monitor_pending_forwards.append(&mut pending_forwards);
                self.monitor_pending_failures.append(&mut pending_fails);
 +              self.monitor_pending_finalized_fulfills.append(&mut pending_finalized_claimed_htlcs);
                self.channel_state |= ChannelState::MonitorUpdateFailed as u32;
        }
  
        /// Indicates that the latest ChannelMonitor update has been committed by the client
        /// successfully and we should restore normal operation. Returns messages which should be sent
        /// to the remote side.
 -      pub fn monitor_updating_restored<L: Deref>(&mut self, logger: &L) -> (Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, RAACommitmentOrder, Vec<(PendingHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, Option<Transaction>, Option<msgs::FundingLocked>) where L::Target: Logger {
 +      pub fn monitor_updating_restored<L: Deref>(&mut self, logger: &L) -> MonitorRestoreUpdates where L::Target: Logger {
                assert_eq!(self.channel_state & ChannelState::MonitorUpdateFailed as u32, ChannelState::MonitorUpdateFailed as u32);
                self.channel_state &= !(ChannelState::MonitorUpdateFailed as u32);
  
                        })
                } else { None };
  
 -              let mut forwards = Vec::new();
 -              mem::swap(&mut forwards, &mut self.monitor_pending_forwards);
 -              let mut failures = Vec::new();
 -              mem::swap(&mut failures, &mut self.monitor_pending_failures);
 +              let mut accepted_htlcs = Vec::new();
 +              mem::swap(&mut accepted_htlcs, &mut self.monitor_pending_forwards);
 +              let mut failed_htlcs = Vec::new();
 +              mem::swap(&mut failed_htlcs, &mut self.monitor_pending_failures);
 +              let mut finalized_claimed_htlcs = Vec::new();
 +              mem::swap(&mut finalized_claimed_htlcs, &mut self.monitor_pending_finalized_fulfills);
  
                if self.channel_state & (ChannelState::PeerDisconnected as u32) != 0 {
                        self.monitor_pending_revoke_and_ack = false;
                        self.monitor_pending_commitment_signed = false;
 -                      return (None, None, RAACommitmentOrder::RevokeAndACKFirst, forwards, failures, funding_broadcastable, funding_locked);
 +                      return MonitorRestoreUpdates {
 +                              raa: None, commitment_update: None, order: RAACommitmentOrder::RevokeAndACKFirst,
 +                              accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, funding_broadcastable, funding_locked
 +                      };
                }
  
                let raa = if self.monitor_pending_revoke_and_ack {
                        log_bytes!(self.channel_id()), if funding_broadcastable.is_some() { "a funding broadcastable, " } else { "" },
                        if commitment_update.is_some() { "a" } else { "no" }, if raa.is_some() { "an" } else { "no" },
                        match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
 -              (raa, commitment_update, order, forwards, failures, funding_broadcastable, funding_locked)
 +              MonitorRestoreUpdates {
 +                      raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, funding_broadcastable, funding_locked
 +              }
        }
  
        pub fn update_fee<F: Deref>(&mut self, fee_estimator: &F, msg: &msgs::UpdateFee) -> Result<(), ChannelError>
        pub fn best_block_updated<L: Deref>(&mut self, height: u32, highest_header_time: u32, logger: &L)
                        -> Result<(Option<msgs::FundingLocked>, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> where L::Target: Logger {
                let mut timed_out_htlcs = Vec::new();
-               let unforwarded_htlc_cltv_limit = height + HTLC_FAIL_BACK_BUFFER;
+               // This mirrors the check in ChannelManager::decode_update_add_htlc_onion, refusing to
+               // forward an HTLC when our counterparty should almost certainly just fail it for expiring
+               // ~now.
+               let unforwarded_htlc_cltv_limit = height + LATENCY_GRACE_PERIOD_BLOCKS;
                self.holding_cell_htlc_updates.retain(|htlc_update| {
                        match htlc_update {
                                &HTLCUpdateAwaitingACK::AddHTLC { ref payment_hash, ref source, ref cltv_expiry, .. } => {
                                Some(script) => script.clone().into_inner(),
                                None => Builder::new().into_script(),
                        }),
 +                      channel_type: Some(self.channel_type.clone()),
                }
        }
  
@@@ -5267,8 -5179,6 +5270,8 @@@ impl<Signer: Sign> Writeable for Channe
                        (5, self.config, required),
                        (7, self.shutdown_scriptpubkey, option),
                        (9, self.target_closing_feerate_sats_per_kw, option),
 +                      (11, self.monitor_pending_finalized_fulfills, vec_type),
 +                      (13, self.channel_type, required),
                });
  
                Ok(())
@@@ -5502,10 -5412,6 +5505,10 @@@ impl<'a, Signer: Sign, K: Deref> Readab
  
                let mut announcement_sigs = None;
                let mut target_closing_feerate_sats_per_kw = None;
 +              let mut monitor_pending_finalized_fulfills = Some(Vec::new());
 +              // Prior to supporting channel type negotiation, all of our channels were static_remotekey
 +              // only, so we default to that if none was written.
 +              let mut channel_type = Some(ChannelTypeFeatures::only_static_remote_key());
                read_tlv_fields!(reader, {
                        (0, announcement_sigs, option),
                        (1, minimum_depth, option),
                        (5, config, option), // Note that if none is provided we will *not* overwrite the existing one.
                        (7, shutdown_scriptpubkey, option),
                        (9, target_closing_feerate_sats_per_kw, option),
 +                      (11, monitor_pending_finalized_fulfills, vec_type),
 +                      (13, channel_type, option),
                });
  
 +              let chan_features = channel_type.as_ref().unwrap();
 +              if chan_features.supports_unknown_bits() || chan_features.requires_unknown_bits() {
 +                      // If the channel was written by a new version and negotiated with features we don't
 +                      // understand yet, refuse to read it.
 +                      return Err(DecodeError::UnknownRequiredFeature);
 +              }
 +
                let mut secp_ctx = Secp256k1::new();
                secp_ctx.seeded_randomize(&keys_source.get_secure_random_bytes());
  
                        monitor_pending_commitment_signed,
                        monitor_pending_forwards,
                        monitor_pending_failures,
 +                      monitor_pending_finalized_fulfills: monitor_pending_finalized_fulfills.unwrap(),
  
                        pending_update_fee,
                        holding_cell_update_fee,
  
                        #[cfg(any(test, feature = "fuzztarget"))]
                        historical_inbound_htlc_fulfills,
 +
 +                      channel_type: channel_type.unwrap(),
                })
        }
  }
@@@ -5809,8 -5703,6 +5812,8 @@@ mod tests 
                                session_priv: SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap(),
                                first_hop_htlc_msat: 548,
                                payment_id: PaymentId([42; 32]),
 +                              payment_secret: None,
 +                              payee: None,
                        }
                });
  
index 5aebf3dbf5c334f19bcc8c4de00d5db10b7621d3,e4ffc4d51eb54049c9213081944ba381bbabdcf5..631edfe6cf375bd1f419d7b949b9cd728a100621
@@@ -36,16 -36,16 +36,16 @@@ use bitcoin::secp256k1::ecdh::SharedSec
  use bitcoin::secp256k1;
  
  use chain;
 -use chain::{Confirm, Watch, BestBlock};
 +use chain::{Confirm, ChannelMonitorUpdateErr, Watch, BestBlock};
  use chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
 -use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, ChannelMonitorUpdateErr, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent, CLOSED_CHANNEL_UPDATE_ID};
 +use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent, CLOSED_CHANNEL_UPDATE_ID};
  use chain::transaction::{OutPoint, TransactionData};
  // Since this struct is returned in `list_channels` methods, expose it here in case users want to
  // construct one themselves.
  use ln::{PaymentHash, PaymentPreimage, PaymentSecret};
  use ln::channel::{Channel, ChannelError, ChannelUpdateStatus, UpdateFulfillCommitFetch};
  use ln::features::{InitFeatures, NodeFeatures};
 -use routing::router::{Route, RouteHop};
 +use routing::router::{Payee, Route, RouteHop, RoutePath, RouteParameters};
  use ln::msgs;
  use ln::msgs::NetAddress;
  use ln::onion_utils;
@@@ -145,7 -145,7 +145,7 @@@ pub(super) enum HTLCForwardInfo 
  }
  
  /// Tracks the inbound corresponding to an outbound HTLC
 -#[derive(Clone, PartialEq)]
 +#[derive(Clone, Hash, PartialEq, Eq)]
  pub(crate) struct HTLCPreviousHopData {
        short_channel_id: u64,
        htlc_id: u64,
@@@ -173,7 -173,6 +173,7 @@@ struct ClaimableHTLC 
  }
  
  /// A payment identifier used to uniquely identify a payment to LDK.
 +/// (C-not exported) as we just use [u8; 32] directly
  #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
  pub struct PaymentId(pub [u8; 32]);
  
@@@ -190,8 -189,7 +190,8 @@@ impl Readable for PaymentId 
        }
  }
  /// Tracks the inbound corresponding to an outbound HTLC
 -#[derive(Clone, PartialEq)]
 +#[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
 +#[derive(Clone, PartialEq, Eq)]
  pub(crate) enum HTLCSource {
        PreviousHopData(HTLCPreviousHopData),
        OutboundRoute {
                /// doing a double-pass on route when we get a failure back
                first_hop_htlc_msat: u64,
                payment_id: PaymentId,
 +              payment_secret: Option<PaymentSecret>,
 +              payee: Option<Payee>,
        },
  }
 +#[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
 +impl core::hash::Hash for HTLCSource {
 +      fn hash<H: core::hash::Hasher>(&self, hasher: &mut H) {
 +              match self {
 +                      HTLCSource::PreviousHopData(prev_hop_data) => {
 +                              0u8.hash(hasher);
 +                              prev_hop_data.hash(hasher);
 +                      },
 +                      HTLCSource::OutboundRoute { path, session_priv, payment_id, payment_secret, first_hop_htlc_msat, payee } => {
 +                              1u8.hash(hasher);
 +                              path.hash(hasher);
 +                              session_priv[..].hash(hasher);
 +                              payment_id.hash(hasher);
 +                              payment_secret.hash(hasher);
 +                              first_hop_htlc_msat.hash(hasher);
 +                              payee.hash(hasher);
 +                      },
 +              }
 +      }
 +}
  #[cfg(test)]
  impl HTLCSource {
        pub fn dummy() -> Self {
                        session_priv: SecretKey::from_slice(&[1; 32]).unwrap(),
                        first_hop_htlc_msat: 0,
                        payment_id: PaymentId([2; 32]),
 +                      payment_secret: None,
 +                      payee: None,
                }
        }
  }
@@@ -268,7 -242,7 +268,7 @@@ type ShutdownResult = (Option<(OutPoint
  
  struct MsgHandleErrInternal {
        err: msgs::LightningError,
 -      chan_id: Option<[u8; 32]>, // If Some a channel of ours has been closed
 +      chan_id: Option<([u8; 32], u64)>, // If Some a channel of ours has been closed
        shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
  }
  impl MsgHandleErrInternal {
                Self { err, chan_id: None, shutdown_finish: None }
        }
        #[inline]
 -      fn from_finish_shutdown(err: String, channel_id: [u8; 32], shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
 +      fn from_finish_shutdown(err: String, channel_id: [u8; 32], user_channel_id: u64, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
                Self {
                        err: LightningError {
                                err: err.clone(),
                                        },
                                },
                        },
 -                      chan_id: Some(channel_id),
 +                      chan_id: Some((channel_id, user_channel_id)),
                        shutdown_finish: Some((shutdown_res, channel_update)),
                }
        }
@@@ -437,89 -411,39 +437,89 @@@ pub(crate) enum PendingOutboundPayment 
                payment_hash: PaymentHash,
                payment_secret: Option<PaymentSecret>,
                pending_amt_msat: u64,
 +              /// Used to track the fee paid. Only present if the payment was serialized on 0.0.103+.
 +              pending_fee_msat: Option<u64>,
                /// The total payment amount across all paths, used to verify that a retry is not overpaying.
                total_msat: u64,
                /// Our best known block height at the time this payment was initiated.
                starting_block_height: u32,
        },
 +      /// When a pending payment is fulfilled, we continue tracking it until all pending HTLCs have
 +      /// been resolved. This ensures we don't look up pending payments in ChannelMonitors on restart
 +      /// and add a pending payment that was already fulfilled.
 +      Fulfilled {
 +              session_privs: HashSet<[u8; 32]>,
 +      },
  }
  
  impl PendingOutboundPayment {
 -      fn remove(&mut self, session_priv: &[u8; 32], part_amt_msat: u64) -> bool {
 +      fn is_retryable(&self) -> bool {
 +              match self {
 +                      PendingOutboundPayment::Retryable { .. } => true,
 +                      _ => false,
 +              }
 +      }
 +      fn is_fulfilled(&self) -> bool {
 +              match self {
 +                      PendingOutboundPayment::Fulfilled { .. } => true,
 +                      _ => false,
 +              }
 +      }
 +      fn get_pending_fee_msat(&self) -> Option<u64> {
 +              match self {
 +                      PendingOutboundPayment::Retryable { pending_fee_msat, .. } => pending_fee_msat.clone(),
 +                      _ => None,
 +              }
 +      }
 +
 +      fn mark_fulfilled(&mut self) {
 +              let mut session_privs = HashSet::new();
 +              core::mem::swap(&mut session_privs, match self {
 +                      PendingOutboundPayment::Legacy { session_privs } |
 +                      PendingOutboundPayment::Retryable { session_privs, .. } |
 +                      PendingOutboundPayment::Fulfilled { session_privs }
 +                              => session_privs
 +              });
 +              *self = PendingOutboundPayment::Fulfilled { session_privs };
 +      }
 +
 +      /// panics if path is None and !self.is_fulfilled
 +      fn remove(&mut self, session_priv: &[u8; 32], path: Option<&Vec<RouteHop>>) -> bool {
                let remove_res = match self {
                        PendingOutboundPayment::Legacy { session_privs } |
 -                      PendingOutboundPayment::Retryable { session_privs, .. } => {
 +                      PendingOutboundPayment::Retryable { session_privs, .. } |
 +                      PendingOutboundPayment::Fulfilled { session_privs } => {
                                session_privs.remove(session_priv)
                        }
                };
                if remove_res {
 -                      if let PendingOutboundPayment::Retryable { ref mut pending_amt_msat, .. } = self {
 -                              *pending_amt_msat -= part_amt_msat;
 +                      if let PendingOutboundPayment::Retryable { ref mut pending_amt_msat, ref mut pending_fee_msat, .. } = self {
 +                              let path = path.expect("Fulfilling a payment should always come with a path");
 +                              let path_last_hop = path.last().expect("Outbound payments must have had a valid path");
 +                              *pending_amt_msat -= path_last_hop.fee_msat;
 +                              if let Some(fee_msat) = pending_fee_msat.as_mut() {
 +                                      *fee_msat -= path.get_path_fees();
 +                              }
                        }
                }
                remove_res
        }
  
 -      fn insert(&mut self, session_priv: [u8; 32], part_amt_msat: u64) -> bool {
 +      fn insert(&mut self, session_priv: [u8; 32], path: &Vec<RouteHop>) -> bool {
                let insert_res = match self {
                        PendingOutboundPayment::Legacy { session_privs } |
                        PendingOutboundPayment::Retryable { session_privs, .. } => {
                                session_privs.insert(session_priv)
                        }
 +                      PendingOutboundPayment::Fulfilled { .. } => false
                };
                if insert_res {
 -                      if let PendingOutboundPayment::Retryable { ref mut pending_amt_msat, .. } = self {
 -                              *pending_amt_msat += part_amt_msat;
 +                      if let PendingOutboundPayment::Retryable { ref mut pending_amt_msat, ref mut pending_fee_msat, .. } = self {
 +                              let path_last_hop = path.last().expect("Outbound payments must have had a valid path");
 +                              *pending_amt_msat += path_last_hop.fee_msat;
 +                              if let Some(fee_msat) = pending_fee_msat.as_mut() {
 +                                      *fee_msat += path.get_path_fees();
 +                              }
                        }
                }
                insert_res
        fn remaining_parts(&self) -> usize {
                match self {
                        PendingOutboundPayment::Legacy { session_privs } |
 -                      PendingOutboundPayment::Retryable { session_privs, .. } => {
 +                      PendingOutboundPayment::Retryable { session_privs, .. } |
 +                      PendingOutboundPayment::Fulfilled { session_privs } => {
                                session_privs.len()
                        }
                }
@@@ -853,8 -776,8 +853,8 @@@ pub struct ChannelDetails 
        ///
        /// [`outbound_capacity_msat`]: ChannelDetails::outbound_capacity_msat
        pub unspendable_punishment_reserve: Option<u64>,
 -      /// The user_id passed in to create_channel, or 0 if the channel was inbound.
 -      pub user_id: u64,
 +      /// The `user_channel_id` passed in to create_channel, or 0 if the channel was inbound.
 +      pub user_channel_id: u64,
        /// The available outbound capacity for sending HTLCs to the remote peer. This does not include
        /// any pending HTLCs which are not yet fully resolved (and, thus, who's balance is not
        /// available for inclusion in new outbound HTLCs). This further does not include any pending
@@@ -946,16 -869,7 +946,16 @@@ pub enum PaymentSendFailure 
        /// as they will result in over-/re-payment. These HTLCs all either successfully sent (in the
        /// case of Ok(())) or will send once channel_monitor_updated is called on the next-hop channel
        /// with the latest update_id.
 -      PartialFailure(Vec<Result<(), APIError>>),
 +      PartialFailure {
 +              /// The errors themselves, in the same order as the route hops.
 +              results: Vec<Result<(), APIError>>,
 +              /// If some paths failed without irrevocably committing to the new HTLC(s), this will
 +              /// contain a [`RouteParameters`] object which can be used to calculate a new route that
 +              /// will pay all remaining unpaid balance.
 +              failed_paths_retry: Option<RouteParameters>,
 +              /// The payment id for the payment, which is now at least partially pending.
 +              payment_id: PaymentId,
 +      },
  }
  
  macro_rules! handle_error {
                                                        msg: update
                                                });
                                        }
 -                                      if let Some(channel_id) = chan_id {
 -                                              $self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id,  reason: ClosureReason::ProcessingError { err: err.err.clone() } });
 +                                      if let Some((channel_id, user_channel_id)) = chan_id {
 +                                              $self.pending_events.lock().unwrap().push(events::Event::ChannelClosed {
 +                                                      channel_id, user_channel_id,
 +                                                      reason: ClosureReason::ProcessingError { err: err.err.clone() }
 +                                              });
                                        }
                                }
  
@@@ -1026,8 -937,7 +1026,8 @@@ macro_rules! convert_chan_err 
                                        $short_to_id.remove(&short_id);
                                }
                                let shutdown_res = $channel.force_shutdown(true);
 -                              (true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
 +                              (true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.get_user_id(),
 +                                      shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
                        },
                        ChannelError::CloseDelayBroadcast(msg) => {
                                log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($channel_id[..]), msg);
                                        $short_to_id.remove(&short_id);
                                }
                                let shutdown_res = $channel.force_shutdown(false);
 -                              (true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
 +                              (true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.get_user_id(),
 +                                      shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
                        }
                }
        }
@@@ -1088,7 -997,7 +1088,7 @@@ macro_rules! handle_monitor_err 
        ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
                handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, Vec::new(), Vec::new())
        };
 -      ($self: ident, $err: expr, $short_to_id: expr, $chan: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr, $chan_id: expr) => {
 +      ($self: ident, $err: expr, $short_to_id: expr, $chan: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr, $failed_finalized_fulfills: expr, $chan_id: expr) => {
                match $err {
                        ChannelMonitorUpdateErr::PermanentFailure => {
                                log_error!($self.logger, "Closing channel {} due to monitor update ChannelMonitorUpdateErr::PermanentFailure", log_bytes!($chan_id[..]));
                                // splitting hairs we'd prefer to claim payments that were to us, but we haven't
                                // given up the preimage yet, so might as well just wait until the payment is
                                // retried, avoiding the on-chain fees.
 -                              let res: Result<(), _> = Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure".to_owned(), *$chan_id,
 +                              let res: Result<(), _> = Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure".to_owned(), *$chan_id, $chan.get_user_id(),
                                                $chan.force_shutdown(true), $self.get_channel_update_for_broadcast(&$chan).ok() ));
                                (res, true)
                        },
                        ChannelMonitorUpdateErr::TemporaryFailure => {
 -                              log_info!($self.logger, "Disabling channel {} due to monitor update TemporaryFailure. On restore will send {} and process {} forwards and {} fails",
 +                              log_info!($self.logger, "Disabling channel {} due to monitor update TemporaryFailure. On restore will send {} and process {} forwards, {} fails, and {} fulfill finalizations",
                                                log_bytes!($chan_id[..]),
                                                if $resend_commitment && $resend_raa {
                                                                match $action_type {
                                                        else if $resend_raa { "RAA" }
                                                        else { "nothing" },
                                                (&$failed_forwards as &Vec<(PendingHTLCInfo, u64)>).len(),
 -                                              (&$failed_fails as &Vec<(HTLCSource, PaymentHash, HTLCFailReason)>).len());
 +                                              (&$failed_fails as &Vec<(HTLCSource, PaymentHash, HTLCFailReason)>).len(),
 +                                              (&$failed_finalized_fulfills as &Vec<HTLCSource>).len());
                                if !$resend_commitment {
                                        debug_assert!($action_type == RAACommitmentOrder::RevokeAndACKFirst || !$resend_raa);
                                }
                                if !$resend_raa {
                                        debug_assert!($action_type == RAACommitmentOrder::CommitmentFirst || !$resend_commitment);
                                }
 -                              $chan.monitor_update_failed($resend_raa, $resend_commitment, $failed_forwards, $failed_fails);
 +                              $chan.monitor_update_failed($resend_raa, $resend_commitment, $failed_forwards, $failed_fails, $failed_finalized_fulfills);
                                (Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore("Failed to update ChannelMonitor".to_owned()), *$chan_id)), false)
                        },
                }
        };
 -      ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => { {
 -              let (res, drop) = handle_monitor_err!($self, $err, $channel_state.short_to_id, $entry.get_mut(), $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails, $entry.key());
 +      ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr, $failed_finalized_fulfills: expr) => { {
 +              let (res, drop) = handle_monitor_err!($self, $err, $channel_state.short_to_id, $entry.get_mut(), $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails, $failed_finalized_fulfills, $entry.key());
                if drop {
                        $entry.remove_entry();
                }
                res
        } };
 +      ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => {
 +              handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails, Vec::new())
 +      }
  }
  
  macro_rules! return_monitor_err {
@@@ -1362,31 -1267,22 +1362,31 @@@ impl<Signer: Sign, M: Deref, T: Deref, 
  
        /// Creates a new outbound channel to the given remote node and with the given value.
        ///
 -      /// user_id will be provided back as user_channel_id in FundingGenerationReady events to allow
 -      /// tracking of which events correspond with which create_channel call. Note that the
 -      /// user_channel_id defaults to 0 for inbound channels, so you may wish to avoid using 0 for
 -      /// user_id here. user_id has no meaning inside of LDK, it is simply copied to events and
 -      /// otherwise ignored.
 +      /// `user_channel_id` will be provided back as in
 +      /// [`Event::FundingGenerationReady::user_channel_id`] to allow tracking of which events
 +      /// correspond with which `create_channel` call. Note that the `user_channel_id` defaults to 0
 +      /// for inbound channels, so you may wish to avoid using 0 for `user_channel_id` here.
 +      /// `user_channel_id` has no meaning inside of LDK, it is simply copied to events and otherwise
 +      /// ignored.
        ///
 -      /// If successful, will generate a SendOpenChannel message event, so you should probably poll
 -      /// PeerManager::process_events afterwards.
 -      ///
 -      /// Raises APIError::APIMisuseError when channel_value_satoshis > 2**24 or push_msat is
 -      /// greater than channel_value_satoshis * 1k or channel_value_satoshis is < 1000.
 +      /// Raises [`APIError::APIMisuseError`] when `channel_value_satoshis` > 2**24 or `push_msat` is
 +      /// greater than `channel_value_satoshis * 1k` or `channel_value_satoshis < 1000`.
        ///
        /// Note that we do not check if you are currently connected to the given peer. If no
        /// connection is available, the outbound `open_channel` message may fail to send, resulting in
 -      /// the channel eventually being silently forgotten.
 -      pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64, override_config: Option<UserConfig>) -> Result<(), APIError> {
 +      /// the channel eventually being silently forgotten (dropped on reload).
 +      ///
 +      /// Returns the new Channel's temporary `channel_id`. This ID will appear as
 +      /// [`Event::FundingGenerationReady::temporary_channel_id`] and in
 +      /// [`ChannelDetails::channel_id`] until after
 +      /// [`ChannelManager::funding_transaction_generated`] is called, swapping the Channel's ID for
 +      /// one derived from the funding transaction's TXID. If the counterparty rejects the channel
 +      /// immediately, this temporary ID will appear in [`Event::ChannelClosed::channel_id`].
 +      ///
 +      /// [`Event::FundingGenerationReady::user_channel_id`]: events::Event::FundingGenerationReady::user_channel_id
 +      /// [`Event::FundingGenerationReady::temporary_channel_id`]: events::Event::FundingGenerationReady::temporary_channel_id
 +      /// [`Event::ChannelClosed::channel_id`]: events::Event::ChannelClosed::channel_id
 +      pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_channel_id: u64, override_config: Option<UserConfig>) -> Result<[u8; 32], APIError> {
                if channel_value_satoshis < 1000 {
                        return Err(APIError::APIMisuseError { err: format!("Channel value must be at least 1000 satoshis. It was {}", channel_value_satoshis) });
                }
                                        let peer_state = peer_state.lock().unwrap();
                                        let their_features = &peer_state.latest_features;
                                        let config = if override_config.is_some() { override_config.as_ref().unwrap() } else { &self.default_configuration };
 -                                      Channel::new_outbound(&self.fee_estimator, &self.keys_manager, their_network_key, their_features, channel_value_satoshis, push_msat, user_id, config)?
 +                                      Channel::new_outbound(&self.fee_estimator, &self.keys_manager, their_network_key, their_features, channel_value_satoshis, push_msat, user_channel_id, config)?
                                },
                                None => return Err(APIError::ChannelUnavailable { err: format!("Not connected to node: {}", their_network_key) }),
                        }
                // We want to make sure the lock is actually acquired by PersistenceNotifierGuard.
                debug_assert!(&self.total_consistency_lock.try_write().is_err());
  
 +              let temporary_channel_id = channel.channel_id();
                let mut channel_state = self.channel_state.lock().unwrap();
 -              match channel_state.by_id.entry(channel.channel_id()) {
 +              match channel_state.by_id.entry(temporary_channel_id) {
                        hash_map::Entry::Occupied(_) => {
                                if cfg!(feature = "fuzztarget") {
                                        return Err(APIError::APIMisuseError { err: "Fuzzy bad RNG".to_owned() });
                        node_id: their_network_key,
                        msg: res,
                });
 -              Ok(())
 +              Ok(temporary_channel_id)
        }
  
        fn list_channels_with_filter<Fn: FnMut(&(&[u8; 32], &Channel<Signer>)) -> bool>(&self, f: Fn) -> Vec<ChannelDetails> {
                                        unspendable_punishment_reserve: to_self_reserve_satoshis,
                                        inbound_capacity_msat,
                                        outbound_capacity_msat,
 -                                      user_id: channel.get_user_id(),
 +                                      user_channel_id: channel.get_user_id(),
                                        confirmations_required: channel.minimum_depth(),
                                        force_close_spend_delay: channel.get_counterparty_selected_contest_delay(),
                                        is_outbound: channel.is_outbound(),
                        },
                        None => {},
                }
 -              pending_events_lock.push(events::Event::ChannelClosed { channel_id: channel.channel_id(), reason: closure_reason });
 +              pending_events_lock.push(events::Event::ChannelClosed {
 +                      channel_id: channel.channel_id(),
 +                      user_channel_id: channel.get_user_id(),
 +                      reason: closure_reason
 +              });
        }
  
        fn close_channel_internal(&self, channel_id: &[u8; 32], target_feerate_sats_per_1000_weight: Option<u32>) -> Result<(), APIError> {
                                        if let Some(monitor_update) = monitor_update {
                                                if let Err(e) = self.chain_monitor.update_channel(chan_entry.get().get_funding_txo().unwrap(), monitor_update) {
                                                        let (result, is_permanent) =
 -                                                              handle_monitor_err!(self, e, channel_state.short_to_id, chan_entry.get_mut(), RAACommitmentOrder::CommitmentFirst, false, false, Vec::new(), Vec::new(), chan_entry.key());
 +                                                              handle_monitor_err!(self, e, channel_state.short_to_id, chan_entry.get_mut(), RAACommitmentOrder::CommitmentFirst, false, false, Vec::new(), Vec::new(), Vec::new(), chan_entry.key());
                                                        if is_permanent {
                                                                remove_channel!(channel_state, chan_entry);
                                                                break result;
                                                break Some(("Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta", 0x1000 | 13, Some(self.get_channel_update_for_unicast(chan).unwrap())));
                                        }
                                        let cur_height = self.best_block.read().unwrap().height() + 1;
-                                       // Theoretically, channel counterparty shouldn't send us a HTLC expiring now, but we want to be robust wrt to counterparty
-                                       // packet sanitization (see HTLC_FAIL_BACK_BUFFER rational)
+                                       // Theoretically, channel counterparty shouldn't send us a HTLC expiring now,
+                                       // but we want to be robust wrt to counterparty packet sanitization (see
+                                       // HTLC_FAIL_BACK_BUFFER rationale).
                                        if msg.cltv_expiry <= cur_height + HTLC_FAIL_BACK_BUFFER as u32 { // expiry_too_soon
                                                break Some(("CLTV expiry is too close", 0x1000 | 14, Some(self.get_channel_update_for_unicast(chan).unwrap())));
                                        }
                                        if msg.cltv_expiry > cur_height + CLTV_FAR_FAR_AWAY as u32 { // expiry_too_far
                                                break Some(("CLTV expiry is too far in the future", 21, None));
                                        }
-                                       // In theory, we would be safe against unintentional channel-closure, if we only required a margin of LATENCY_GRACE_PERIOD_BLOCKS.
-                                       // But, to be safe against policy reception, we use a longer delay.
-                                       if (*outgoing_cltv_value) as u64 <= (cur_height + HTLC_FAIL_BACK_BUFFER) as u64 {
+                                       // If the HTLC expires ~now, don't bother trying to forward it to our
+                                       // counterparty. They should fail it anyway, but we don't want to bother with
+                                       // the round-trips or risk them deciding they definitely want the HTLC and
+                                       // force-closing to ensure they get it if we're offline.
+                                       // We previously had a much more aggressive check here which tried to ensure
+                                       // our counterparty receives an HTLC which has *our* risk threshold met on it,
+                                       // but there is no need to do that, and since we're a bit conservative with our
+                                       // risk threshold it just results in failing to forward payments.
+                                       if (*outgoing_cltv_value) as u64 <= (cur_height + LATENCY_GRACE_PERIOD_BLOCKS) as u64 {
                                                break Some(("Outgoing CLTV value is too soon", 0x1000 | 14, Some(self.get_channel_update_for_unicast(chan).unwrap())));
                                        }
  
        }
  
        // Only public for testing, this should otherwise never be called direcly
 -      pub(crate) fn send_payment_along_path(&self, path: &Vec<RouteHop>, payment_hash: &PaymentHash, payment_secret: &Option<PaymentSecret>, total_value: u64, cur_height: u32, payment_id: PaymentId, keysend_preimage: &Option<PaymentPreimage>) -> Result<(), APIError> {
 +      pub(crate) fn send_payment_along_path(&self, path: &Vec<RouteHop>, payee: &Option<Payee>, payment_hash: &PaymentHash, payment_secret: &Option<PaymentSecret>, total_value: u64, cur_height: u32, payment_id: PaymentId, keysend_preimage: &Option<PaymentPreimage>) -> Result<(), APIError> {
                log_trace!(self.logger, "Attempting to send payment for path with next hop {}", path.first().unwrap().short_channel_id);
                let prng_seed = self.keys_manager.get_secure_random_bytes();
                let session_priv_bytes = self.keys_manager.get_secure_random_bytes();
  
                let err: Result<(), _> = loop {
                        let mut channel_lock = self.channel_state.lock().unwrap();
 +
 +                      let mut pending_outbounds = self.pending_outbound_payments.lock().unwrap();
 +                      let payment_entry = pending_outbounds.entry(payment_id);
 +                      if let hash_map::Entry::Occupied(payment) = &payment_entry {
 +                              if !payment.get().is_retryable() {
 +                                      return Err(APIError::RouteError {
 +                                              err: "Payment already completed"
 +                                      });
 +                              }
 +                      }
 +
                        let id = match channel_lock.short_to_id.get(&path.first().unwrap().short_channel_id) {
                                None => return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()}),
                                Some(id) => id.clone(),
                        };
  
 +                      macro_rules! insert_outbound_payment {
 +                              () => {
 +                                      let payment = payment_entry.or_insert_with(|| PendingOutboundPayment::Retryable {
 +                                              session_privs: HashSet::new(),
 +                                              pending_amt_msat: 0,
 +                                              pending_fee_msat: Some(0),
 +                                              payment_hash: *payment_hash,
 +                                              payment_secret: *payment_secret,
 +                                              starting_block_height: self.best_block.read().unwrap().height(),
 +                                              total_msat: total_value,
 +                                      });
 +                                      assert!(payment.insert(session_priv_bytes, path));
 +                              }
 +                      }
 +
                        let channel_state = &mut *channel_lock;
                        if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(id) {
                                match {
                                        if !chan.get().is_live() {
                                                return Err(APIError::ChannelUnavailable{err: "Peer for first hop currently disconnected/pending monitor update!".to_owned()});
                                        }
 -                                      let send_res = break_chan_entry!(self, chan.get_mut().send_htlc_and_commit(
 +                                      break_chan_entry!(self, chan.get_mut().send_htlc_and_commit(
                                                htlc_msat, payment_hash.clone(), htlc_cltv, HTLCSource::OutboundRoute {
                                                        path: path.clone(),
                                                        session_priv: session_priv.clone(),
                                                        first_hop_htlc_msat: htlc_msat,
                                                        payment_id,
 +                                                      payment_secret: payment_secret.clone(),
 +                                                      payee: payee.clone(),
                                                }, onion_packet, &self.logger),
 -                                      channel_state, chan);
 -
 -                                      let mut pending_outbounds = self.pending_outbound_payments.lock().unwrap();
 -                                      let payment = pending_outbounds.entry(payment_id).or_insert_with(|| PendingOutboundPayment::Retryable {
 -                                              session_privs: HashSet::new(),
 -                                              pending_amt_msat: 0,
 -                                              payment_hash: *payment_hash,
 -                                              payment_secret: *payment_secret,
 -                                              starting_block_height: self.best_block.read().unwrap().height(),
 -                                              total_msat: total_value,
 -                                      });
 -                                      assert!(payment.insert(session_priv_bytes, path.last().unwrap().fee_msat));
 -
 -                                      send_res
 +                                      channel_state, chan)
                                } {
                                        Some((update_add, commitment_signed, monitor_update)) => {
                                                if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
                                                        // is restored. Therefore, we must return an error indicating that
                                                        // it is unsafe to retry the payment wholesale, which we do in the
                                                        // send_payment check for MonitorUpdateFailed, below.
 +                                                      insert_outbound_payment!(); // Only do this after possibly break'ing on Perm failure above.
                                                        return Err(APIError::MonitorUpdateFailed);
                                                }
 +                                              insert_outbound_payment!();
  
                                                log_debug!(self.logger, "Sending payment along path resulted in a commitment_signed for channel {}", log_bytes!(chan.get().channel_id()));
                                                channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
                                                        },
                                                });
                                        },
 -                                      None => {},
 +                                      None => { insert_outbound_payment!(); },
                                }
                        } else { unreachable!(); }
                        return Ok(());
                let cur_height = self.best_block.read().unwrap().height() + 1;
                let mut results = Vec::new();
                for path in route.paths.iter() {
 -                      results.push(self.send_payment_along_path(&path, &payment_hash, payment_secret, total_value, cur_height, payment_id, &keysend_preimage));
 +                      results.push(self.send_payment_along_path(&path, &route.payee, &payment_hash, payment_secret, total_value, cur_height, payment_id, &keysend_preimage));
                }
                let mut has_ok = false;
                let mut has_err = false;
 -              for res in results.iter() {
 +              let mut pending_amt_unsent = 0;
 +              let mut max_unsent_cltv_delta = 0;
 +              for (res, path) in results.iter().zip(route.paths.iter()) {
                        if res.is_ok() { has_ok = true; }
                        if res.is_err() { has_err = true; }
                        if let &Err(APIError::MonitorUpdateFailed) = res {
                                // PartialFailure.
                                has_err = true;
                                has_ok = true;
 -                              break;
 +                      } else if res.is_err() {
 +                              pending_amt_unsent += path.last().unwrap().fee_msat;
 +                              max_unsent_cltv_delta = cmp::max(max_unsent_cltv_delta, path.last().unwrap().cltv_expiry_delta);
                        }
                }
                if has_err && has_ok {
 -                      Err(PaymentSendFailure::PartialFailure(results))
 +                      Err(PaymentSendFailure::PartialFailure {
 +                              results,
 +                              payment_id,
 +                              failed_paths_retry: if pending_amt_unsent != 0 {
 +                                      if let Some(payee) = &route.payee {
 +                                              Some(RouteParameters {
 +                                                      payee: payee.clone(),
 +                                                      final_value_msat: pending_amt_unsent,
 +                                                      final_cltv_expiry_delta: max_unsent_cltv_delta,
 +                                              })
 +                                      } else { None }
 +                              } else { None },
 +                      })
                } else if has_err {
 +                      // If we failed to send any paths, we shouldn't have inserted the new PaymentId into
 +                      // our `pending_outbound_payments` map at all.
 +                      debug_assert!(self.pending_outbound_payments.lock().unwrap().get(&payment_id).is_none());
                        Err(PaymentSendFailure::AllFailedRetrySafe(results.drain(..).map(|r| r.unwrap_err()).collect()))
                } else {
                        Ok(payment_id)
                                                return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
                                                        err: "Unable to retry payments that were initially sent on LDK versions prior to 0.0.102".to_string()
                                                }))
 -                                      }
 +                                      },
 +                                      PendingOutboundPayment::Fulfilled { .. } => {
 +                                              return Err(PaymentSendFailure::ParameterError(APIError::RouteError {
 +                                                      err: "Payment already completed"
 +                                              }));
 +                                      },
                                }
                        } else {
                                return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
  
                                        (chan.get_outbound_funding_created(funding_transaction, funding_txo, &self.logger)
                                                .map_err(|e| if let ChannelError::Close(msg) = e {
 -                                                      MsgHandleErrInternal::from_finish_shutdown(msg, chan.channel_id(), chan.force_shutdown(true), None)
 +                                                      MsgHandleErrInternal::from_finish_shutdown(msg, chan.channel_id(), chan.get_user_id(), chan.force_shutdown(true), None)
                                                } else { unreachable!(); })
                                        , chan)
                                },
                                                                                                channel_state.short_to_id.remove(&short_id);
                                                                                        }
                                                                                        // ChannelClosed event is generated by handle_error for us.
 -                                                                                      Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, channel.force_shutdown(true), self.get_channel_update_for_broadcast(&channel).ok()))
 +                                                                                      Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, channel.get_user_id(), channel.force_shutdown(true), self.get_channel_update_for_broadcast(&channel).ok()))
                                                                                },
                                                                                ChannelError::CloseDelayBroadcast(_) => { panic!("Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here"); }
                                                                        };
                let ret_err = match res {
                        Ok(Some((update_fee, commitment_signed, monitor_update))) => {
                                if let Err(e) = self.chain_monitor.update_channel(chan.get_funding_txo().unwrap(), monitor_update) {
 -                                      let (res, drop) = handle_monitor_err!(self, e, short_to_id, chan, RAACommitmentOrder::CommitmentFirst, false, true, Vec::new(), Vec::new(), chan_id);
 +                                      let (res, drop) = handle_monitor_err!(self, e, short_to_id, chan, RAACommitmentOrder::CommitmentFirst, false, true, Vec::new(), Vec::new(), Vec::new(), chan_id);
                                        if drop { retain_channel = false; }
                                        res
                                } else {
                                        self.fail_htlc_backwards_internal(channel_state,
                                                htlc_src, &payment_hash, HTLCFailReason::Reason { failure_code, data: onion_failure_data});
                                },
 -                              HTLCSource::OutboundRoute { session_priv, payment_id, path, .. } => {
 +                              HTLCSource::OutboundRoute { session_priv, payment_id, path, payee, .. } => {
                                        let mut session_priv_bytes = [0; 32];
                                        session_priv_bytes.copy_from_slice(&session_priv[..]);
                                        let mut outbounds = self.pending_outbound_payments.lock().unwrap();
                                        if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(payment_id) {
 -                                              if payment.get_mut().remove(&session_priv_bytes, path.last().unwrap().fee_msat) {
 +                                              if payment.get_mut().remove(&session_priv_bytes, Some(&path)) && !payment.get().is_fulfilled() {
 +                                                      let retry = if let Some(payee_data) = payee {
 +                                                              let path_last_hop = path.last().expect("Outbound payments must have had a valid path");
 +                                                              Some(RouteParameters {
 +                                                                      payee: payee_data,
 +                                                                      final_value_msat: path_last_hop.fee_msat,
 +                                                                      final_cltv_expiry_delta: path_last_hop.cltv_expiry_delta,
 +                                                              })
 +                                                      } else { None };
                                                        self.pending_events.lock().unwrap().push(
                                                                events::Event::PaymentPathFailed {
 +                                                                      payment_id: Some(payment_id),
                                                                        payment_hash,
                                                                        rejected_by_dest: false,
                                                                        network_update: None,
                                                                        all_paths_failed: payment.get().remaining_parts() == 0,
                                                                        path: path.clone(),
                                                                        short_channel_id: None,
 +                                                                      retry,
                                                                        #[cfg(test)]
                                                                        error_code: None,
                                                                        #[cfg(test)]
                // from block_connected which may run during initialization prior to the chain_monitor
                // being fully configured. See the docs for `ChannelManagerReadArgs` for more.
                match source {
 -                      HTLCSource::OutboundRoute { ref path, session_priv, payment_id, .. } => {
 +                      HTLCSource::OutboundRoute { ref path, session_priv, payment_id, ref payee, .. } => {
                                let mut session_priv_bytes = [0; 32];
                                session_priv_bytes.copy_from_slice(&session_priv[..]);
                                let mut outbounds = self.pending_outbound_payments.lock().unwrap();
                                let mut all_paths_failed = false;
 -                              if let hash_map::Entry::Occupied(mut sessions) = outbounds.entry(payment_id) {
 -                                      if !sessions.get_mut().remove(&session_priv_bytes, path.last().unwrap().fee_msat) {
 +                              if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(payment_id) {
 +                                      if !payment.get_mut().remove(&session_priv_bytes, Some(&path)) {
                                                log_trace!(self.logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
                                                return;
                                        }
 -                                      if sessions.get().remaining_parts() == 0 {
 +                                      if payment.get().is_fulfilled() {
 +                                              log_trace!(self.logger, "Received failure of HTLC with payment_hash {} after payment completion", log_bytes!(payment_hash.0));
 +                                              return;
 +                                      }
 +                                      if payment.get().remaining_parts() == 0 {
                                                all_paths_failed = true;
                                        }
                                } else {
                                        log_trace!(self.logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
                                        return;
                                }
 -                              log_trace!(self.logger, "Failing outbound payment HTLC with payment_hash {}", log_bytes!(payment_hash.0));
                                mem::drop(channel_state_lock);
 +                              let retry = if let Some(payee_data) = payee {
 +                                      let path_last_hop = path.last().expect("Outbound payments must have had a valid path");
 +                                      Some(RouteParameters {
 +                                              payee: payee_data.clone(),
 +                                              final_value_msat: path_last_hop.fee_msat,
 +                                              final_cltv_expiry_delta: path_last_hop.cltv_expiry_delta,
 +                                      })
 +                              } else { None };
 +                              log_trace!(self.logger, "Failing outbound payment HTLC with payment_hash {}", log_bytes!(payment_hash.0));
                                match &onion_error {
                                        &HTLCFailReason::LightningError { ref err } => {
  #[cfg(test)]
                                                // next-hop is needlessly blaming us!
                                                self.pending_events.lock().unwrap().push(
                                                        events::Event::PaymentPathFailed {
 +                                                              payment_id: Some(payment_id),
                                                                payment_hash: payment_hash.clone(),
                                                                rejected_by_dest: !payment_retryable,
                                                                network_update,
                                                                all_paths_failed,
                                                                path: path.clone(),
                                                                short_channel_id,
 +                                                              retry,
  #[cfg(test)]
                                                                error_code: onion_error_code,
  #[cfg(test)]
                                                // channel here as we apparently can't relay through them anyway.
                                                self.pending_events.lock().unwrap().push(
                                                        events::Event::PaymentPathFailed {
 +                                                              payment_id: Some(payment_id),
                                                                payment_hash: payment_hash.clone(),
                                                                rejected_by_dest: path.len() == 1,
                                                                network_update: None,
                                                                all_paths_failed,
                                                                path: path.clone(),
                                                                short_channel_id: Some(path.first().unwrap().short_channel_id),
 +                                                              retry,
  #[cfg(test)]
                                                                error_code: Some(*failure_code),
  #[cfg(test)]
                } else { unreachable!(); }
        }
  
 +      fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
 +              for source in sources.drain(..) {
 +                      if let HTLCSource::OutboundRoute { session_priv, payment_id, .. } = source {
 +                              let mut session_priv_bytes = [0; 32];
 +                              session_priv_bytes.copy_from_slice(&session_priv[..]);
 +                              let mut outbounds = self.pending_outbound_payments.lock().unwrap();
 +                              if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(payment_id) {
 +                                      assert!(payment.get().is_fulfilled());
 +                                      payment.get_mut().remove(&session_priv_bytes, None);
 +                                      if payment.get().remaining_parts() == 0 {
 +                                              payment.remove();
 +                                      }
 +                              }
 +                      }
 +              }
 +      }
 +
        fn claim_funds_internal(&self, mut channel_state_lock: MutexGuard<ChannelHolder<Signer>>, source: HTLCSource, payment_preimage: PaymentPreimage, forwarded_htlc_value_msat: Option<u64>, from_onchain: bool) {
                match source {
                        HTLCSource::OutboundRoute { session_priv, payment_id, path, .. } => {
                                let mut session_priv_bytes = [0; 32];
                                session_priv_bytes.copy_from_slice(&session_priv[..]);
                                let mut outbounds = self.pending_outbound_payments.lock().unwrap();
 -                              let found_payment = if let Some(mut sessions) = outbounds.remove(&payment_id) {
 -                                      sessions.remove(&session_priv_bytes, path.last().unwrap().fee_msat)
 -                              } else { false };
 -                              if found_payment {
 -                                      let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
 -                                      self.pending_events.lock().unwrap().push(
 -                                              events::Event::PaymentSent {
 -                                                      payment_preimage,
 -                                                      payment_hash: payment_hash
 +                              if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(payment_id) {
 +                                      let found_payment = !payment.get().is_fulfilled();
 +                                      let fee_paid_msat = payment.get().get_pending_fee_msat();
 +                                      payment.get_mut().mark_fulfilled();
 +                                      if from_onchain {
 +                                              // We currently immediately remove HTLCs which were fulfilled on-chain.
 +                                              // This could potentially lead to removing a pending payment too early,
 +                                              // with a reorg of one block causing us to re-add the fulfilled payment on
 +                                              // restart.
 +                                              // TODO: We should have a second monitor event that informs us of payments
 +                                              // irrevocably fulfilled.
 +                                              payment.get_mut().remove(&session_priv_bytes, Some(&path));
 +                                              if payment.get().remaining_parts() == 0 {
 +                                                      payment.remove();
                                                }
 -                                      );
 +                                      }
 +                                      if found_payment {
 +                                              let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
 +                                              self.pending_events.lock().unwrap().push(
 +                                                      events::Event::PaymentSent {
 +                                                              payment_id: Some(payment_id),
 +                                                              payment_preimage,
 +                                                              payment_hash: payment_hash,
 +                                                              fee_paid_msat,
 +                                                      }
 +                                              );
 +                                      }
                                } else {
                                        log_trace!(self.logger, "Received duplicative fulfill for HTLC with payment_preimage {}", log_bytes!(payment_preimage.0));
                                }
                self.our_network_pubkey.clone()
        }
  
 -      /// Restores a single, given channel to normal operation after a
 -      /// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
 -      /// operation.
 -      ///
 -      /// All ChannelMonitor updates up to and including highest_applied_update_id must have been
 -      /// fully committed in every copy of the given channels' ChannelMonitors.
 -      ///
 -      /// Note that there is no effect to calling with a highest_applied_update_id other than the
 -      /// current latest ChannelMonitorUpdate and one call to this function after multiple
 -      /// ChannelMonitorUpdateErr::TemporaryFailures is fine. The highest_applied_update_id field
 -      /// exists largely only to prevent races between this and concurrent update_monitor calls.
 -      ///
 -      /// Thus, the anticipated use is, at a high level:
 -      ///  1) You register a chain::Watch with this ChannelManager,
 -      ///  2) it stores each update to disk, and begins updating any remote (eg watchtower) copies of
 -      ///     said ChannelMonitors as it can, returning ChannelMonitorUpdateErr::TemporaryFailures
 -      ///     any time it cannot do so instantly,
 -      ///  3) update(s) are applied to each remote copy of a ChannelMonitor,
 -      ///  4) once all remote copies are updated, you call this function with the update_id that
 -      ///     completed, and once it is the latest the Channel will be re-enabled.
 -      pub fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64) {
 +      fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64) {
                let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
  
                let chan_restoration_res;
 -              let mut pending_failures = {
 +              let (mut pending_failures, finalized_claims) = {
                        let mut channel_lock = self.channel_state.lock().unwrap();
                        let channel_state = &mut *channel_lock;
                        let mut channel = match channel_state.by_id.entry(funding_txo.to_channel_id()) {
                                return;
                        }
  
 -                      let (raa, commitment_update, order, pending_forwards, pending_failures, funding_broadcastable, funding_locked) = channel.get_mut().monitor_updating_restored(&self.logger);
 -                      let channel_update = if funding_locked.is_some() && channel.get().is_usable() && !channel.get().should_announce() {
 +                      let updates = channel.get_mut().monitor_updating_restored(&self.logger);
 +                      let channel_update = if updates.funding_locked.is_some() && channel.get().is_usable() && !channel.get().should_announce() {
                                // We only send a channel_update in the case where we are just now sending a
                                // funding_locked and the channel is in a usable state. Further, we rely on the
                                // normal announcement_signatures process to send a channel_update for public
                                        msg: self.get_channel_update_for_unicast(channel.get()).unwrap(),
                                })
                        } else { None };
 -                      chan_restoration_res = handle_chan_restoration_locked!(self, channel_lock, channel_state, channel, raa, commitment_update, order, None, pending_forwards, funding_broadcastable, funding_locked);
 +                      chan_restoration_res = handle_chan_restoration_locked!(self, channel_lock, channel_state, channel, updates.raa, updates.commitment_update, updates.order, None, updates.accepted_htlcs, updates.funding_broadcastable, updates.funding_locked);
                        if let Some(upd) = channel_update {
                                channel_state.pending_msg_events.push(upd);
                        }
 -                      pending_failures
 +                      (updates.failed_htlcs, updates.finalized_claimed_htlcs)
                };
                post_handle_chan_restoration!(self, chan_restoration_res);
 +              self.finalize_claims(finalized_claims);
                for failure in pending_failures.drain(..) {
                        self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
                }
                                        // hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
                                        // accepted payment from yet. We do, however, need to wait to send our funding_locked
                                        // until we have persisted our monitor.
 -                                      chan.monitor_update_failed(false, false, Vec::new(), Vec::new());
 +                                      chan.monitor_update_failed(false, false, Vec::new(), Vec::new(), Vec::new());
                                },
                        }
                }
                                        if let Some(monitor_update) = monitor_update {
                                                if let Err(e) = self.chain_monitor.update_channel(chan_entry.get().get_funding_txo().unwrap(), monitor_update) {
                                                        let (result, is_permanent) =
 -                                                              handle_monitor_err!(self, e, channel_state.short_to_id, chan_entry.get_mut(), RAACommitmentOrder::CommitmentFirst, false, false, Vec::new(), Vec::new(), chan_entry.key());
 +                                                              handle_monitor_err!(self, e, channel_state.short_to_id, chan_entry.get_mut(), RAACommitmentOrder::CommitmentFirst, false, false, Vec::new(), Vec::new(), Vec::new(), chan_entry.key());
                                                        if is_permanent {
                                                                remove_channel!(channel_state, chan_entry);
                                                                break result;
                                                break Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
                                        }
                                        let was_frozen_for_monitor = chan.get().is_awaiting_monitor_update();
 -                                      let (commitment_update, pending_forwards, pending_failures, monitor_update, htlcs_to_fail_in) =
 -                                              break_chan_entry!(self, chan.get_mut().revoke_and_ack(&msg, &self.logger), channel_state, chan);
 -                                      htlcs_to_fail = htlcs_to_fail_in;
 -                                      if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
 +                                      let raa_updates = break_chan_entry!(self,
 +                                              chan.get_mut().revoke_and_ack(&msg, &self.logger), channel_state, chan);
 +                                      htlcs_to_fail = raa_updates.holding_cell_failed_htlcs;
 +                                      if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), raa_updates.monitor_update) {
                                                if was_frozen_for_monitor {
 -                                                      assert!(commitment_update.is_none() && pending_forwards.is_empty() && pending_failures.is_empty());
 +                                                      assert!(raa_updates.commitment_update.is_none());
 +                                                      assert!(raa_updates.accepted_htlcs.is_empty());
 +                                                      assert!(raa_updates.failed_htlcs.is_empty());
 +                                                      assert!(raa_updates.finalized_claimed_htlcs.is_empty());
                                                        break Err(MsgHandleErrInternal::ignore_no_close("Previous monitor update failure prevented responses to RAA".to_owned()));
                                                } else {
 -                                                      if let Err(e) = handle_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, commitment_update.is_some(), pending_forwards, pending_failures) {
 +                                                      if let Err(e) = handle_monitor_err!(self, e, channel_state, chan,
 +                                                                      RAACommitmentOrder::CommitmentFirst, false,
 +                                                                      raa_updates.commitment_update.is_some(),
 +                                                                      raa_updates.accepted_htlcs, raa_updates.failed_htlcs,
 +                                                                      raa_updates.finalized_claimed_htlcs) {
                                                                break Err(e);
                                                        } else { unreachable!(); }
                                                }
                                        }
 -                                      if let Some(updates) = commitment_update {
 +                                      if let Some(updates) = raa_updates.commitment_update {
                                                channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
                                                        node_id: counterparty_node_id.clone(),
                                                        updates,
                                                });
                                        }
 -                                      break Ok((pending_forwards, pending_failures, chan.get().get_short_channel_id().expect("RAA should only work on a short-id-available channel"), chan.get().get_funding_txo().unwrap()))
 +                                      break Ok((raa_updates.accepted_htlcs, raa_updates.failed_htlcs,
 +                                                      raa_updates.finalized_claimed_htlcs,
 +                                                      chan.get().get_short_channel_id()
 +                                                              .expect("RAA should only work on a short-id-available channel"),
 +                                                      chan.get().get_funding_txo().unwrap()))
                                },
                                hash_map::Entry::Vacant(_) => break Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
                        }
                };
                self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id);
                match res {
 -                      Ok((pending_forwards, mut pending_failures, short_channel_id, channel_outpoint)) => {
 +                      Ok((pending_forwards, mut pending_failures, finalized_claim_htlcs,
 +                              short_channel_id, channel_outpoint)) =>
 +                      {
                                for failure in pending_failures.drain(..) {
                                        self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
                                }
                                self.forward_htlcs(&mut [(short_channel_id, channel_outpoint, pending_forwards)]);
 +                              self.finalize_claims(finalized_claim_htlcs);
                                Ok(())
                        },
                        Err(e) => Err(e)
                                                self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() });
                                        }
                                },
 -                              MonitorEvent::CommitmentTxConfirmed(funding_outpoint) => {
 +                              MonitorEvent::CommitmentTxConfirmed(funding_outpoint) |
 +                              MonitorEvent::UpdateFailed(funding_outpoint) => {
                                        let mut channel_lock = self.channel_state.lock().unwrap();
                                        let channel_state = &mut *channel_lock;
                                        let by_id = &mut channel_state.by_id;
                                                                msg: update
                                                        });
                                                }
 -                                              self.issue_channel_close_events(&chan, ClosureReason::CommitmentTxConfirmed);
 +                                              let reason = if let MonitorEvent::UpdateFailed(_) = monitor_event {
 +                                                      ClosureReason::ProcessingError { err: "Failed to persist ChannelMonitor update during chain sync".to_string() }
 +                                              } else {
 +                                                      ClosureReason::CommitmentTxConfirmed
 +                                              };
 +                                              self.issue_channel_close_events(&chan, reason);
                                                pending_msg_events.push(events::MessageSendEvent::HandleError {
                                                        node_id: chan.get_counterparty_node_id(),
                                                        action: msgs::ErrorAction::SendErrorMessage {
                                                });
                                        }
                                },
 +                              MonitorEvent::UpdateCompleted { funding_txo, monitor_update_id } => {
 +                                      self.channel_monitor_updated(&funding_txo, monitor_update_id);
 +                              },
                        }
                }
  
                has_pending_monitor_events
        }
  
 +      /// In chanmon_consistency_target, we'd like to be able to restore monitor updating without
 +      /// handling all pending events (i.e. not PendingHTLCsForwardable). Thus, we expose monitor
 +      /// update events as a separate process method here.
 +      #[cfg(feature = "fuzztarget")]
 +      pub fn process_monitor_events(&self) {
 +              self.process_pending_monitor_events();
 +      }
 +
        /// Check the holding cell in each channel and free any pending HTLCs in them if possible.
        /// Returns whether there were any updates such as if pending HTLCs were freed or a monitor
        /// update was applied.
                                                if let Some((commitment_update, monitor_update)) = commitment_opt {
                                                        if let Err(e) = self.chain_monitor.update_channel(chan.get_funding_txo().unwrap(), monitor_update) {
                                                                has_monitor_update = true;
 -                                                              let (res, close_channel) = handle_monitor_err!(self, e, short_to_id, chan, RAACommitmentOrder::CommitmentFirst, false, true, Vec::new(), Vec::new(), channel_id);
 +                                                              let (res, close_channel) = handle_monitor_err!(self, e, short_to_id, chan, RAACommitmentOrder::CommitmentFirst, false, true, Vec::new(), Vec::new(), Vec::new(), channel_id);
                                                                handle_errors.push((chan.get_counterparty_node_id(), res));
                                                                if close_channel { return false; }
                                                        } else {
@@@ -5451,15 -5237,11 +5458,15 @@@ impl Readable for HTLCSource 
                                let mut first_hop_htlc_msat: u64 = 0;
                                let mut path = Some(Vec::new());
                                let mut payment_id = None;
 +                              let mut payment_secret = None;
 +                              let mut payee = None;
                                read_tlv_fields!(reader, {
                                        (0, session_priv, required),
                                        (1, payment_id, option),
                                        (2, first_hop_htlc_msat, required),
 +                                      (3, payment_secret, option),
                                        (4, path, vec_type),
 +                                      (5, payee, option),
                                });
                                if payment_id.is_none() {
                                        // For backwards compat, if there was no payment_id written, use the session_priv bytes
                                        first_hop_htlc_msat: first_hop_htlc_msat,
                                        path: path.unwrap(),
                                        payment_id: payment_id.unwrap(),
 +                                      payment_secret,
 +                                      payee,
                                })
                        }
                        1 => Ok(HTLCSource::PreviousHopData(Readable::read(reader)?)),
  impl Writeable for HTLCSource {
        fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::io::Error> {
                match self {
 -                      HTLCSource::OutboundRoute { ref session_priv, ref first_hop_htlc_msat, ref path, payment_id } => {
 +                      HTLCSource::OutboundRoute { ref session_priv, ref first_hop_htlc_msat, ref path, payment_id, payment_secret, payee } => {
                                0u8.write(writer)?;
                                let payment_id_opt = Some(payment_id);
                                write_tlv_fields!(writer, {
                                        (0, session_priv, required),
                                        (1, payment_id_opt, option),
                                        (2, first_hop_htlc_msat, required),
 +                                      (3, payment_secret, option),
                                        (4, path, vec_type),
 +                                      (5, payee, option),
                                 });
                        }
                        HTLCSource::PreviousHopData(ref field) => {
@@@ -5536,23 -5314,19 +5543,23 @@@ impl_writeable_tlv_based!(PendingInboun
        (8, min_value_msat, required),
  });
  
 -impl_writeable_tlv_based_enum!(PendingOutboundPayment,
 +impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
        (0, Legacy) => {
                (0, session_privs, required),
        },
 +      (1, Fulfilled) => {
 +              (0, session_privs, required),
 +      },
        (2, Retryable) => {
                (0, session_privs, required),
 +              (1, pending_fee_msat, option),
                (2, payment_hash, required),
                (4, payment_secret, option),
                (6, total_msat, required),
                (8, pending_amt_msat, required),
                (10, starting_block_height, required),
        },
 -;);
 +);
  
  impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable for ChannelManager<Signer, M, T, K, F, L>
        where M::Target: chain::Watch<Signer>,
                // For backwards compat, write the session privs and their total length.
                let mut num_pending_outbounds_compat: u64 = 0;
                for (_, outbound) in pending_outbound_payments.iter() {
 -                      num_pending_outbounds_compat += outbound.remaining_parts() as u64;
 +                      if !outbound.is_fulfilled() {
 +                              num_pending_outbounds_compat += outbound.remaining_parts() as u64;
 +                      }
                }
                num_pending_outbounds_compat.write(writer)?;
                for (_, outbound) in pending_outbound_payments.iter() {
                                                session_priv.write(writer)?;
                                        }
                                }
 +                              PendingOutboundPayment::Fulfilled { .. } => {},
                        }
                }
  
                                PendingOutboundPayment::Legacy { session_privs } |
                                PendingOutboundPayment::Retryable { session_privs, .. } => {
                                        pending_outbound_payments_no_retry.insert(*id, session_privs.clone());
 -                              }
 +                              },
 +                              _ => {},
                        }
                }
                write_tlv_fields!(writer, {
  ///
  /// At a high-level, the process for deserializing a ChannelManager and resuming normal operation
  /// is:
 -/// 1) Deserialize all stored ChannelMonitors.
 -/// 2) Deserialize the ChannelManager by filling in this struct and calling:
 -///    <(BlockHash, ChannelManager)>::read(reader, args)
 -///    This may result in closing some Channels if the ChannelMonitor is newer than the stored
 -///    ChannelManager state to ensure no loss of funds. Thus, transactions may be broadcasted.
 -/// 3) If you are not fetching full blocks, register all relevant ChannelMonitor outpoints the same
 -///    way you would handle a `chain::Filter` call using ChannelMonitor::get_outputs_to_watch() and
 -///    ChannelMonitor::get_funding_txo().
 -/// 4) Reconnect blocks on your ChannelMonitors.
 -/// 5) Disconnect/connect blocks on the ChannelManager.
 -/// 6) Move the ChannelMonitors into your local chain::Watch.
 +/// 1) Deserialize all stored [`ChannelMonitor`]s.
 +/// 2) Deserialize the [`ChannelManager`] by filling in this struct and calling:
 +///    `<(BlockHash, ChannelManager)>::read(reader, args)`
 +///    This may result in closing some channels if the [`ChannelMonitor`] is newer than the stored
 +///    [`ChannelManager`] state to ensure no loss of funds. Thus, transactions may be broadcasted.
 +/// 3) If you are not fetching full blocks, register all relevant [`ChannelMonitor`] outpoints the
 +///    same way you would handle a [`chain::Filter`] call using
 +///    [`ChannelMonitor::get_outputs_to_watch`] and [`ChannelMonitor::get_funding_txo`].
 +/// 4) Reconnect blocks on your [`ChannelMonitor`]s.
 +/// 5) Disconnect/connect blocks on the [`ChannelManager`].
 +/// 6) Re-persist the [`ChannelMonitor`]s to ensure the latest state is on disk.
 +///    Note that if you're using a [`ChainMonitor`] for your [`chain::Watch`] implementation, you
 +///    will likely accomplish this as a side-effect of calling [`chain::Watch::watch_channel`] in
 +///    the next step.
 +/// 7) Move the [`ChannelMonitor`]s into your local [`chain::Watch`]. If you're using a
 +///    [`ChainMonitor`], this is done by calling [`chain::Watch::watch_channel`].
  ///
 -/// Note that the ordering of #4-6 is not of importance, however all three must occur before you
 -/// call any other methods on the newly-deserialized ChannelManager.
 +/// Note that the ordering of #4-7 is not of importance, however all four must occur before you
 +/// call any other methods on the newly-deserialized [`ChannelManager`].
  ///
  /// Note that because some channels may be closed during deserialization, it is critical that you
  /// always deserialize only the latest version of a ChannelManager and ChannelMonitors available to
  /// broadcast), and then later deserialize a newer version of the same ChannelManager (which will
  /// not force-close the same channels but consider them live), you may end up revoking a state for
  /// which you've already broadcasted the transaction.
 +///
 +/// [`ChainMonitor`]: crate::chain::chainmonitor::ChainMonitor
  pub struct ChannelManagerReadArgs<'a, Signer: 'a + Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
        where M::Target: chain::Watch<Signer>,
          T::Target: BroadcasterInterface,
@@@ -5853,7 -5616,6 +5860,7 @@@ impl<'a, Signer: Sign, M: Deref, T: Der
                                        monitor.broadcast_latest_holder_commitment_txn(&args.tx_broadcaster, &args.logger);
                                        channel_closures.push(events::Event::ChannelClosed {
                                                channel_id: channel.channel_id(),
 +                                              user_channel_id: channel.get_user_id(),
                                                reason: ClosureReason::OutdatedChannelManager
                                        });
                                } else {
                                outbounds.insert(id, PendingOutboundPayment::Legacy { session_privs });
                        }
                        pending_outbound_payments = Some(outbounds);
 +              } else {
 +                      // If we're tracking pending payments, ensure we haven't lost any by looking at the
 +                      // ChannelMonitor data for any channels for which we do not have authorative state
 +                      // (i.e. those for which we just force-closed above or we otherwise don't have a
 +                      // corresponding `Channel` at all).
 +                      // This avoids several edge-cases where we would otherwise "forget" about pending
 +                      // payments which are still in-flight via their on-chain state.
 +                      // We only rebuild the pending payments map if we were most recently serialized by
 +                      // 0.0.102+
 +                      for (_, monitor) in args.channel_monitors {
 +                              if by_id.get(&monitor.get_funding_txo().0.to_channel_id()).is_none() {
 +                                      for (htlc_source, htlc) in monitor.get_pending_outbound_htlcs() {
 +                                              if let HTLCSource::OutboundRoute { payment_id, session_priv, path, payment_secret, .. } = htlc_source {
 +                                                      if path.is_empty() {
 +                                                              log_error!(args.logger, "Got an empty path for a pending payment");
 +                                                              return Err(DecodeError::InvalidValue);
 +                                                      }
 +                                                      let path_amt = path.last().unwrap().fee_msat;
 +                                                      let mut session_priv_bytes = [0; 32];
 +                                                      session_priv_bytes[..].copy_from_slice(&session_priv[..]);
 +                                                      match pending_outbound_payments.as_mut().unwrap().entry(payment_id) {
 +                                                              hash_map::Entry::Occupied(mut entry) => {
 +                                                                      let newly_added = entry.get_mut().insert(session_priv_bytes, &path);
 +                                                                      log_info!(args.logger, "{} a pending payment path for {} msat for session priv {} on an existing pending payment with payment hash {}",
 +                                                                              if newly_added { "Added" } else { "Had" }, path_amt, log_bytes!(session_priv_bytes), log_bytes!(htlc.payment_hash.0));
 +                                                              },
 +                                                              hash_map::Entry::Vacant(entry) => {
 +                                                                      let path_fee = path.get_path_fees();
 +                                                                      entry.insert(PendingOutboundPayment::Retryable {
 +                                                                              session_privs: [session_priv_bytes].iter().map(|a| *a).collect(),
 +                                                                              payment_hash: htlc.payment_hash,
 +                                                                              payment_secret,
 +                                                                              pending_amt_msat: path_amt,
 +                                                                              pending_fee_msat: Some(path_fee),
 +                                                                              total_msat: path_amt,
 +                                                                              starting_block_height: best_block_height,
 +                                                                      });
 +                                                                      log_info!(args.logger, "Added a pending payment for {} msat with payment hash {} for path with session priv {}",
 +                                                                              path_amt, log_bytes!(htlc.payment_hash.0),  log_bytes!(session_priv_bytes));
 +                                                              }
 +                                                      }
 +                                              }
 +                                      }
 +                              }
 +                      }
                }
  
                let mut secp_ctx = Secp256k1::new();
@@@ -6089,11 -5806,11 +6096,11 @@@ mod tests 
        use core::time::Duration;
        use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
        use ln::channelmanager::{PaymentId, PaymentSendFailure};
 -      use ln::features::{InitFeatures, InvoiceFeatures};
 +      use ln::features::InitFeatures;
        use ln::functional_test_utils::*;
        use ln::msgs;
        use ln::msgs::ChannelMessageHandler;
 -      use routing::router::{get_keysend_route, get_route};
 +      use routing::router::{Payee, RouteParameters, find_route};
        use util::errors::APIError;
        use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
        use util::test_utils;
                let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
                let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
                create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -              let logger = test_utils::TestLogger::new();
  
                // First, send a partial MPP payment.
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100_000, TEST_FINAL_CLTV, &logger).unwrap();
 -              let (payment_preimage, our_payment_hash, payment_secret) = get_payment_preimage_hash!(&nodes[1]);
 +              let (route, our_payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], 100_000);
                let payment_id = PaymentId([42; 32]);
                // Use the utility function send_payment_along_path to send the payment with MPP data which
                // indicates there are more HTLCs coming.
                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.
 -              nodes[0].node.send_payment_along_path(&route.paths[0], &our_payment_hash, &Some(payment_secret), 200_000, cur_height, payment_id, &None).unwrap();
 +              nodes[0].node.send_payment_along_path(&route.paths[0], &route.payee, &our_payment_hash, &Some(payment_secret), 200_000, cur_height, payment_id, &None).unwrap();
                check_added_monitors!(nodes[0], 1);
                let mut events = nodes[0].node.get_and_clear_pending_msg_events();
                assert_eq!(events.len(), 1);
                expect_payment_failed!(nodes[0], our_payment_hash, true);
  
                // Send the second half of the original MPP payment.
 -              nodes[0].node.send_payment_along_path(&route.paths[0], &our_payment_hash, &Some(payment_secret), 200_000, cur_height, payment_id, &None).unwrap();
 +              nodes[0].node.send_payment_along_path(&route.paths[0], &route.payee, &our_payment_hash, &Some(payment_secret), 200_000, cur_height, payment_id, &None).unwrap();
                check_added_monitors!(nodes[0], 1);
                let mut events = nodes[0].node.get_and_clear_pending_msg_events();
                assert_eq!(events.len(), 1);
                // further events will be generated for subsequence path successes.
                let events = nodes[0].node.get_and_clear_pending_events();
                match events[0] {
 -                      Event::PaymentSent { payment_preimage: ref preimage, payment_hash: ref hash } => {
 +                      Event::PaymentSent { payment_id: ref id, payment_preimage: ref preimage, payment_hash: ref hash, .. } => {
 +                              assert_eq!(Some(payment_id), *id);
                                assert_eq!(payment_preimage, *preimage);
                                assert_eq!(our_payment_hash, *hash);
                        },
                let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
                let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
                create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -              let logger = test_utils::TestLogger::new();
 +              let scorer = test_utils::TestScorer::with_fixed_penalty(0);
  
                // To start (1), send a regular payment but don't claim it.
                let expected_route = [&nodes[1]];
                let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &expected_route, 100_000);
  
                // Next, attempt a keysend payment and make sure it fails.
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &nodes[0].net_graph_msg_handler.network_graph, &expected_route.last().unwrap().node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100_000, TEST_FINAL_CLTV, &logger).unwrap();
 +              let params = RouteParameters {
 +                      payee: Payee::for_keysend(expected_route.last().unwrap().node.get_our_node_id()),
 +                      final_value_msat: 100_000,
 +                      final_cltv_expiry_delta: TEST_FINAL_CLTV,
 +              };
 +              let route = find_route(
 +                      &nodes[0].node.get_our_node_id(), &params, nodes[0].network_graph, None,
 +                      nodes[0].logger, &scorer
 +              ).unwrap();
                nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage)).unwrap();
                check_added_monitors!(nodes[0], 1);
                let mut events = nodes[0].node.get_and_clear_pending_msg_events();
  
                // To start (2), send a keysend payment but don't claim it.
                let payment_preimage = PaymentPreimage([42; 32]);
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &nodes[0].net_graph_msg_handler.network_graph, &expected_route.last().unwrap().node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100_000, TEST_FINAL_CLTV, &logger).unwrap();
 +              let route = find_route(
 +                      &nodes[0].node.get_our_node_id(), &params, nodes[0].network_graph, None,
 +                      nodes[0].logger, &scorer
 +              ).unwrap();
                let (payment_hash, _) = nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage)).unwrap();
                check_added_monitors!(nodes[0], 1);
                let mut events = nodes[0].node.get_and_clear_pending_msg_events();
                nodes[1].node.peer_connected(&payer_pubkey, &msgs::Init { features: InitFeatures::known() });
  
                let _chan = create_chan_between_nodes(&nodes[0], &nodes[1], InitFeatures::known(), InitFeatures::known());
 -              let network_graph = &nodes[0].net_graph_msg_handler.network_graph;
 +              let params = RouteParameters {
 +                      payee: Payee::for_keysend(payee_pubkey),
 +                      final_value_msat: 10000,
 +                      final_cltv_expiry_delta: 40,
 +              };
 +              let network_graph = nodes[0].network_graph;
                let first_hops = nodes[0].node.list_usable_channels();
 -              let route = get_keysend_route(&payer_pubkey, network_graph, &payee_pubkey,
 -                                  Some(&first_hops.iter().collect::<Vec<_>>()), &vec![], 10000, 40,
 -                                  nodes[0].logger).unwrap();
 +              let scorer = test_utils::TestScorer::with_fixed_penalty(0);
 +              let route = find_route(
 +                      &payer_pubkey, &params, network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
 +                      nodes[0].logger, &scorer
 +              ).unwrap();
  
                let test_preimage = PaymentPreimage([42; 32]);
                let mismatch_payment_hash = PaymentHash([43; 32]);
                nodes[1].node.peer_connected(&payer_pubkey, &msgs::Init { features: InitFeatures::known() });
  
                let _chan = create_chan_between_nodes(&nodes[0], &nodes[1], InitFeatures::known(), InitFeatures::known());
 -              let network_graph = &nodes[0].net_graph_msg_handler.network_graph;
 +              let params = RouteParameters {
 +                      payee: Payee::for_keysend(payee_pubkey),
 +                      final_value_msat: 10000,
 +                      final_cltv_expiry_delta: 40,
 +              };
 +              let network_graph = nodes[0].network_graph;
                let first_hops = nodes[0].node.list_usable_channels();
 -              let route = get_keysend_route(&payer_pubkey, network_graph, &payee_pubkey,
 -                                  Some(&first_hops.iter().collect::<Vec<_>>()), &vec![], 10000, 40,
 -                                  nodes[0].logger).unwrap();
 +              let scorer = test_utils::TestScorer::with_fixed_penalty(0);
 +              let route = find_route(
 +                      &payer_pubkey, &params, network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
 +                      nodes[0].logger, &scorer
 +              ).unwrap();
  
                let test_preimage = PaymentPreimage([42; 32]);
                let test_secret = PaymentSecret([43; 32]);
                let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
                let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
                let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
 -              let logger = test_utils::TestLogger::new();
  
                // Marshall an MPP route.
 -              let (_, payment_hash, _) = get_payment_preimage_hash!(&nodes[3]);
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let mut route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[3].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
 +              let (mut route, payment_hash, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[3], 100000);
                let path = route.paths[0].clone();
                route.paths.push(path);
                route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
  #[cfg(all(any(test, feature = "_test_utils"), feature = "unstable"))]
  pub mod bench {
        use chain::Listen;
 -      use chain::chainmonitor::ChainMonitor;
 -      use chain::channelmonitor::Persist;
 +      use chain::chainmonitor::{ChainMonitor, Persist};
        use chain::keysinterface::{KeysManager, InMemorySigner};
        use ln::channelmanager::{BestBlock, ChainParameters, ChannelManager, PaymentHash, PaymentPreimage};
        use ln::features::{InitFeatures, InvoiceFeatures};
        use ln::functional_test_utils::*;
        use ln::msgs::{ChannelMessageHandler, Init};
        use routing::network_graph::NetworkGraph;
 -      use routing::router::get_route;
 +      use routing::router::{Payee, get_route};
 +      use routing::scorer::Scorer;
        use util::test_utils;
        use util::config::UserConfig;
        use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose};
                macro_rules! send_payment {
                        ($node_a: expr, $node_b: expr) => {
                                let usable_channels = $node_a.list_usable_channels();
 -                              let route = get_route(&$node_a.get_our_node_id(), &dummy_graph, &$node_b.get_our_node_id(), Some(InvoiceFeatures::known()),
 -                                      Some(&usable_channels.iter().map(|r| r).collect::<Vec<_>>()), &[], 10_000, TEST_FINAL_CLTV, &logger_a).unwrap();
 +                              let payee = Payee::from_node_id($node_b.get_our_node_id())
 +                                      .with_features(InvoiceFeatures::known());
 +                              let scorer = Scorer::with_fixed_penalty(0);
 +                              let route = get_route(&$node_a.get_our_node_id(), &payee, &dummy_graph,
 +                                      Some(&usable_channels.iter().map(|r| r).collect::<Vec<_>>()), 10_000, TEST_FINAL_CLTV, &logger_a, &scorer).unwrap();
  
                                let mut payment_preimage = PaymentPreimage([0; 32]);
                                payment_preimage.0[0..8].copy_from_slice(&payment_count.to_le_bytes());
index 5e87aaff3c40a93c67ddcf012a67098a33d427b7,5c5a2f2af4469111a13c3ad12948f5f257e533e2..118bb1497d8f20a833a2c7273e56a25c81b4dea7
@@@ -23,8 -23,8 +23,8 @@@ use ln::channelmanager::{ChannelManager
  use ln::channel::{Channel, ChannelError};
  use ln::{chan_utils, onion_utils};
  use ln::chan_utils::HTLC_SUCCESS_TX_WEIGHT;
 -use routing::router::{Route, RouteHop, RouteHint, RouteHintHop, get_route, get_keysend_route};
  use routing::network_graph::{NetworkUpdate, RoutingFees};
 +use routing::router::{Payee, Route, RouteHop, RouteHint, RouteHintHop, RouteParameters, find_route, get_route};
  use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
  use ln::msgs;
  use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ErrorAction};
@@@ -35,7 -35,7 +35,7 @@@ use util::errors::APIError
  use util::ser::{Writeable, ReadableArgs};
  use util::config::UserConfig;
  
 -use bitcoin::hash_types::{Txid, BlockHash};
 +use bitcoin::hash_types::BlockHash;
  use bitcoin::blockdata::block::{Block, BlockHeader};
  use bitcoin::blockdata::script::Builder;
  use bitcoin::blockdata::opcodes;
@@@ -126,6 -126,7 +126,6 @@@ fn test_async_inbound_update_fee() 
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        // balancing
        send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
        nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
  
        // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[0]);
 -      let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -      nodes[1].node.send_payment(&get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 40000, TEST_FINAL_CLTV, &logger).unwrap(), our_payment_hash, &Some(our_payment_secret)).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 40000);
 +      nodes[1].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        check_added_monitors!(nodes[1], 1);
  
        let payment_event = {
@@@ -242,6 -244,7 +242,6 @@@ fn test_update_fee_unordered_raa() 
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        // balancing
        send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
        nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
  
        // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[0]);
 -      let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -      nodes[1].node.send_payment(&get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 40000, TEST_FINAL_CLTV, &logger).unwrap(), our_payment_hash, &Some(our_payment_secret)).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 40000);
 +      nodes[1].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        check_added_monitors!(nodes[1], 1);
  
        let payment_event = {
@@@ -644,6 -648,7 +644,6 @@@ fn test_update_fee_with_fundee_update_a
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        // balancing
        send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
        let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
        check_added_monitors!(nodes[1], 1);
  
 -      let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[0]);
 -      let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -      let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 800000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 800000);
  
        // nothing happens since node[1] is in AwaitingRemoteRevoke
        nodes[1].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
@@@ -918,7 -925,7 +918,7 @@@ fn fake_network_test() 
        });
        hops[1].fee_msat = chan_4.1.contents.fee_base_msat as u64 + chan_4.1.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
        hops[0].fee_msat = chan_3.0.contents.fee_base_msat as u64 + chan_3.0.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
 -      let payment_preimage_1 = send_along_route(&nodes[1], Route { paths: vec![hops] }, &vec!(&nodes[2], &nodes[3], &nodes[1])[..], 1000000).0;
 +      let payment_preimage_1 = send_along_route(&nodes[1], Route { paths: vec![hops], payee: None }, &vec!(&nodes[2], &nodes[3], &nodes[1])[..], 1000000).0;
  
        let mut hops = Vec::with_capacity(3);
        hops.push(RouteHop {
        });
        hops[1].fee_msat = chan_2.1.contents.fee_base_msat as u64 + chan_2.1.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
        hops[0].fee_msat = chan_3.1.contents.fee_base_msat as u64 + chan_3.1.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
 -      let payment_hash_2 = send_along_route(&nodes[1], Route { paths: vec![hops] }, &vec!(&nodes[3], &nodes[2], &nodes[1])[..], 1000000).1;
 +      let payment_hash_2 = send_along_route(&nodes[1], Route { paths: vec![hops], payee: None }, &vec!(&nodes[3], &nodes[2], &nodes[1])[..], 1000000).1;
  
        // Claim the rebalances...
        fail_payment(&nodes[1], &vec!(&nodes[3], &nodes[2], &nodes[1])[..], payment_hash_2);
@@@ -1002,10 -1009,13 +1002,10 @@@ fn holding_cell_htlc_counting() 
        let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
        create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
        let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        let mut payments = Vec::new();
        for _ in 0..::ln::channel::OUR_MAX_HTLCS {
 -              let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[2]);
 -              let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -              let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
 +              let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
                nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
                payments.push((payment_preimage, payment_hash));
        }
        // There is now one HTLC in an outbound commitment transaction and (OUR_MAX_HTLCS - 1) HTLCs in
        // the holding cell waiting on B's RAA to send. At this point we should not be able to add
        // another HTLC.
 -      let (_, payment_hash_1, payment_secret_1) = get_payment_preimage_hash!(nodes[2]);
 +      let (route, payment_hash_1, _, payment_secret_1) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
        {
 -              let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -              let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
                unwrap_send_err!(nodes[1].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)), true, APIError::ChannelUnavailable { ref err },
                        assert!(regex::Regex::new(r"Cannot push more than their max accepted HTLCs \(\d+\)").unwrap().is_match(err)));
                assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
        }
  
        // This should also be true if we try to forward a payment.
 -      let (_, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[2]);
 +      let (route, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], 100000);
        {
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
                nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
                check_added_monitors!(nodes[0], 1);
        }
@@@ -1145,13 -1159,15 +1145,13 @@@ fn test_duplicate_htlc_different_direct
        let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
  
        let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        // balancing
        send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
  
        let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 900_000);
  
 -      let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -      let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 800_000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[0], 800_000);
        let node_a_payment_secret = nodes[0].node.create_inbound_payment_for_hash(payment_hash, None, 7200, 0).unwrap();
        send_along_route_with_secret(&nodes[1], route, &[&[&nodes[0]]], 800_000, payment_hash, node_a_payment_secret);
  
@@@ -1228,14 -1244,17 +1228,14 @@@ fn test_basic_channel_reserve() 
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        let chan_stat = get_channel_value_stat!(nodes[0], chan.2);
        let channel_reserve = chan_stat.channel_reserve_msat;
  
        // The 2* and +1 are for the fee spike reserve.
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
        let commit_tx_fee = 2 * commit_tx_fee_msat(get_feerate!(nodes[0], chan.2), 1 + 1);
        let max_can_send = 5000000 - channel_reserve - commit_tx_fee;
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes.last().unwrap().node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), max_can_send + 1, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send + 1);
        let err = nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).err().unwrap();
        match err {
                PaymentSendFailure::AllFailedRetrySafe(ref fails) => {
@@@ -1872,6 -1891,7 +1872,6 @@@ fn channel_reserve_in_flight_removes() 
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        let b_chan_values = get_channel_value_stat!(nodes[1], chan_1.2);
        // Route the first two HTLCs.
        let (payment_preimage_2, _, _) = route_payment(&nodes[0], &[&nodes[1]], 20000);
  
        // Start routing the third HTLC (this is just used to get everyone in the right state).
 -      let (payment_preimage_3, payment_hash_3, payment_secret_3) = get_payment_preimage_hash!(nodes[1]);
 +      let (route, payment_hash_3, payment_preimage_3, payment_secret_3) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
        let send_1 = {
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
                nodes[0].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3)).unwrap();
                check_added_monitors!(nodes[0], 1);
                let mut events = nodes[0].node.get_and_clear_pending_msg_events();
  
        // Now that B doesn't have the second RAA anymore, but A still does, send a payment from B back
        // to A to ensure that A doesn't count the almost-removed HTLC in update_add processing.
 -      let (payment_preimage_4, payment_hash_4, payment_secret_4) = get_payment_preimage_hash!(nodes[0]);
 +      let (route, payment_hash_4, payment_preimage_4, payment_secret_4) = get_route_and_payment_hash!(nodes[1], nodes[0], 10000);
        let send_2 = {
 -              let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -              let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 10000, TEST_FINAL_CLTV, &logger).unwrap();
                nodes[1].node.send_payment(&route, payment_hash_4, &Some(payment_secret_4)).unwrap();
                check_added_monitors!(nodes[1], 1);
                let mut events = nodes[1].node.get_and_clear_pending_msg_events();
@@@ -2104,7 -2128,7 +2104,7 @@@ fn channel_monitor_network_test() 
  
        // Drop the ChannelMonitor for the previous channel to avoid it broadcasting transactions and
        // confusing us in the following tests.
 -      let chan_3_mon = nodes[3].chain_monitor.chain_monitor.monitors.write().unwrap().remove(&OutPoint { txid: chan_3.3.txid(), index: 0 }).unwrap();
 +      let chan_3_mon = nodes[3].chain_monitor.chain_monitor.remove_monitor(&OutPoint { txid: chan_3.3.txid(), index: 0 });
  
        // One pending HTLC to time out:
        let payment_preimage_2 = route_payment(&nodes[3], &vec!(&nodes[4])[..], 3000000).0;
        assert_eq!(nodes[3].node.list_channels().len(), 0);
        assert_eq!(nodes[4].node.list_channels().len(), 0);
  
 -      nodes[3].chain_monitor.chain_monitor.monitors.write().unwrap().insert(OutPoint { txid: chan_3.3.txid(), index: 0 }, chan_3_mon);
 +      nodes[3].chain_monitor.chain_monitor.watch_channel(OutPoint { txid: chan_3.3.txid(), index: 0 }, chan_3_mon).unwrap();
        check_closed_event!(nodes[3], 1, ClosureReason::CommitmentTxConfirmed);
        check_closed_event!(nodes[4], 1, ClosureReason::CommitmentTxConfirmed);
  }
@@@ -2658,7 -2682,7 +2658,7 @@@ fn test_htlc_on_chain_success() 
        let mut first_claimed = false;
        for event in events {
                match event {
 -                      Event::PaymentSent { payment_preimage, payment_hash } => {
 +                      Event::PaymentSent { payment_preimage, payment_hash, .. } => {
                                if payment_preimage == our_payment_preimage && payment_hash == payment_hash_1 {
                                        assert!(!first_claimed);
                                        first_claimed = true;
@@@ -2959,7 -2983,10 +2959,7 @@@ fn do_test_commitment_revoked_fail_back
  
        // Add a fourth HTLC, this one will get sequestered away in nodes[1]'s holding cell waiting
        // on nodes[2]'s RAA.
 -      let (_, fourth_payment_hash, fourth_payment_secret) = get_payment_preimage_hash!(nodes[2]);
 -      let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -      let logger = test_utils::TestLogger::new();
 -      let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, fourth_payment_hash, _, fourth_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 1000000);
        nodes[1].node.send_payment(&route, fourth_payment_hash, &Some(fourth_payment_secret)).unwrap();
        assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
        assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
@@@ -3106,10 -3133,13 +3106,10 @@@ fn fail_backward_pending_htlc_upon_chan
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        // Alice -> Bob: Route a payment but without Bob sending revoke_and_ack.
        {
 -              let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 50_000, TEST_FINAL_CLTV, &logger).unwrap();
 +              let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 50_000);
                nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
                check_added_monitors!(nodes[0], 1);
  
        }
  
        // Alice -> Bob: Route another payment but now Alice waits for Bob's earlier revoke_and_ack.
 -      let (_, failed_payment_hash, failed_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 +      let (route, failed_payment_hash, _, failed_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 50_000);
        {
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 50_000, TEST_FINAL_CLTV, &logger).unwrap();
                nodes[0].node.send_payment(&route, failed_payment_hash, &Some(failed_payment_secret)).unwrap();
                check_added_monitors!(nodes[0], 0);
  
  
        // Alice <- Bob: Send a malformed update_add_htlc so Alice fails the channel.
        {
 -              let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[0]);
 +              let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 50_000);
  
                let secp_ctx = Secp256k1::new();
                let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
                let current_height = nodes[1].node.best_block.read().unwrap().height() + 1;
 -              let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -              let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 50_000, TEST_FINAL_CLTV, &logger).unwrap();
                let (onion_payloads, _amount_msat, cltv_expiry) = onion_utils::build_onion_payloads(&route.paths[0], 50_000, &Some(payment_secret), current_height, &None).unwrap();
                let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
                let onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash);
@@@ -3214,10 -3248,13 +3214,10 @@@ fn test_force_close_fail_back() 
        let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
        create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
        create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
 -      let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[2]);
 +      let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1000000);
  
        let mut payment_event = {
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 1000000, 42, &logger).unwrap();
                nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
                check_added_monitors!(nodes[0], 1);
  
  
        // Now check that if we add the preimage to ChannelMonitor it broadcasts our HTLC-Success..
        {
 -              let mut monitors = nodes[2].chain_monitor.chain_monitor.monitors.read().unwrap();
 -              monitors.get(&OutPoint{ txid: Txid::from_slice(&payment_event.commitment_msg.channel_id[..]).unwrap(), index: 0 }).unwrap()
 -                      .provide_payment_preimage(&our_payment_hash, &our_payment_preimage, &node_cfgs[2].tx_broadcaster, &node_cfgs[2].fee_estimator, &&logger);
 +              get_monitor!(nodes[2], payment_event.commitment_msg.channel_id)
 +                      .provide_payment_preimage(&our_payment_hash, &our_payment_preimage, &node_cfgs[2].tx_broadcaster, &node_cfgs[2].fee_estimator, &node_cfgs[2].logger);
        }
        mine_transaction(&nodes[2], &tx);
        let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
@@@ -3349,7 -3387,7 +3349,7 @@@ fn test_simple_peer_disconnect() 
                let events = nodes[0].node.get_and_clear_pending_events();
                assert_eq!(events.len(), 2);
                match events[0] {
 -                      Event::PaymentSent { payment_preimage, payment_hash } => {
 +                      Event::PaymentSent { payment_preimage, payment_hash, .. } => {
                                assert_eq!(payment_preimage, payment_preimage_3);
                                assert_eq!(payment_hash, payment_hash_3);
                        },
@@@ -3386,9 -3424,14 +3386,9 @@@ fn do_test_drop_messages_peer_disconnec
                create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
        }
  
 -      let (payment_preimage_1, payment_hash_1, payment_secret_1) = get_payment_preimage_hash!(nodes[1]);
 +      let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
  
 -      let logger = test_utils::TestLogger::new();
        let payment_event = {
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph,
 -                      &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), Some(&nodes[0].node.list_usable_channels().iter().collect::<Vec<_>>()),
 -                      &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
                nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
                check_added_monitors!(nodes[0], 1);
  
                let events_4 = nodes[0].node.get_and_clear_pending_events();
                assert_eq!(events_4.len(), 1);
                match events_4[0] {
 -                      Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
 +                      Event::PaymentSent { ref payment_preimage, ref payment_hash, .. } => {
                                assert_eq!(payment_preimage_1, *payment_preimage);
                                assert_eq!(payment_hash_1, *payment_hash);
                        },
                        let events_4 = nodes[0].node.get_and_clear_pending_events();
                        assert_eq!(events_4.len(), 1);
                        match events_4[0] {
 -                              Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
 +                              Event::PaymentSent { ref payment_preimage, ref payment_hash, .. } => {
                                        assert_eq!(payment_preimage_1, *payment_preimage);
                                        assert_eq!(payment_hash_1, *payment_hash);
                                },
        reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
  
        // Channel should still work fine...
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph,
 -              &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), Some(&nodes[0].node.list_usable_channels().iter().collect::<Vec<_>>()),
 -              &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        let payment_preimage_2 = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000).0;
        claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
  }
@@@ -3620,12 -3666,10 +3620,12 @@@ fn test_funding_peer_disconnect() 
  
        confirm_transaction(&nodes[0], &tx);
        let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
 +      let chan_id;
        assert_eq!(events_1.len(), 1);
        match events_1[0] {
 -              MessageSendEvent::SendFundingLocked { ref node_id, msg: _ } => {
 +              MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
                        assert_eq!(*node_id, nodes[1].node.get_our_node_id());
 +                      chan_id = msg.channel_id;
                },
                _ => panic!("Unexpected event"),
        }
        nodes[0].net_graph_msg_handler.handle_channel_update(&bs_update).unwrap();
        nodes[0].net_graph_msg_handler.handle_channel_update(&as_update).unwrap();
  
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let logger = test_utils::TestLogger::new();
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
 -      let (payment_preimage, _, _) = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000);
 +      let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
 +      let payment_preimage = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000).0;
        claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
  
        // Check that after deserialization and reconnection we can still generate an identical
  
        let nodes_0_serialized = nodes[0].node.encode();
        let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
 -      nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
 +      get_monitor!(nodes[0], chan_id).write(&mut chan_0_monitor_serialized).unwrap();
  
        persister = test_utils::TestPersister::new();
        let keys_manager = &chanmon_cfgs[0].keys_manager;
@@@ -3756,11 -3802,14 +3756,11 @@@ fn test_drop_messages_peer_disconnect_d
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
  
        // Now try to send a second payment which will fail to send
 -      let (payment_preimage_2, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
        check_added_monitors!(nodes[0], 1);
  
                        let events_3 = nodes[0].node.get_and_clear_pending_events();
                        assert_eq!(events_3.len(), 1);
                        match events_3[0] {
 -                              Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
 +                              Event::PaymentSent { ref payment_preimage, ref payment_hash, .. } => {
                                        assert_eq!(*payment_preimage, payment_preimage_1);
                                        assert_eq!(*payment_hash, payment_hash_1);
                                },
@@@ -3904,14 -3953,17 +3904,14 @@@ fn do_test_htlc_timeout(send_partial_mp
        let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
  
        create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        let our_payment_hash = if send_partial_mpp {
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
 -              let (_, our_payment_hash, payment_secret) = get_payment_preimage_hash!(&nodes[1]);
 +              let (route, our_payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], 100000);
                // Use the utility function send_payment_along_path to send the payment with MPP data which
                // indicates there are more HTLCs coming.
                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.
                let payment_id = PaymentId([42; 32]);
 -              nodes[0].node.send_payment_along_path(&route.paths[0], &our_payment_hash, &Some(payment_secret), 200000, cur_height, payment_id, &None).unwrap();
 +              nodes[0].node.send_payment_along_path(&route.paths[0], &route.payee, &our_payment_hash, &Some(payment_secret), 200000, cur_height, payment_id, &None).unwrap();
                check_added_monitors!(nodes[0], 1);
                let mut events = nodes[0].node.get_and_clear_pending_msg_events();
                assert_eq!(events.len(), 1);
@@@ -3973,28 -4025,38 +3973,28 @@@ fn do_test_holding_cell_htlc_add_timeou
        connect_blocks(&nodes[1], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1);
        connect_blocks(&nodes[2], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1);
  
 -      let logger = test_utils::TestLogger::new();
 -
        // Route a first payment to get the 1 -> 2 channel in awaiting_raa...
 -      let (_, first_payment_hash, first_payment_secret) = get_payment_preimage_hash!(nodes[2]);
 +      let (route, first_payment_hash, _, first_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
        {
 -              let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -              let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
                nodes[1].node.send_payment(&route, first_payment_hash, &Some(first_payment_secret)).unwrap();
        }
        assert_eq!(nodes[1].node.get_and_clear_pending_msg_events().len(), 1);
        check_added_monitors!(nodes[1], 1);
  
        // Now attempt to route a second payment, which should be placed in the holding cell
 -      let (_, second_payment_hash, second_payment_secret) = get_payment_preimage_hash!(nodes[2]);
 +      let sending_node = if forwarded_htlc { &nodes[0] } else { &nodes[1] };
 +      let (route, second_payment_hash, _, second_payment_secret) = get_route_and_payment_hash!(sending_node, nodes[2], 100000);
 +      sending_node.node.send_payment(&route, second_payment_hash, &Some(second_payment_secret)).unwrap();
        if forwarded_htlc {
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
 -              nodes[0].node.send_payment(&route, second_payment_hash, &Some(first_payment_secret)).unwrap();
                check_added_monitors!(nodes[0], 1);
                let payment_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
                nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
                commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
                expect_pending_htlcs_forwardable!(nodes[1]);
 -              check_added_monitors!(nodes[1], 0);
 -      } else {
 -              let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -              let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
 -              nodes[1].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret)).unwrap();
 -              check_added_monitors!(nodes[1], 0);
        }
 +      check_added_monitors!(nodes[1], 0);
  
-       connect_blocks(&nodes[1], TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS);
+       connect_blocks(&nodes[1], TEST_FINAL_CLTV - LATENCY_GRACE_PERIOD_BLOCKS);
        assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
        assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
        connect_blocks(&nodes[1], 1);
@@@ -4041,8 -4103,7 +4041,8 @@@ fn test_no_txn_manager_serialize_deseri
  
        let nodes_0_serialized = nodes[0].node.encode();
        let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
 -      nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
 +      get_monitor!(nodes[0], OutPoint { txid: tx.txid(), index: 0 }.to_channel_id())
 +              .write(&mut chan_0_monitor_serialized).unwrap();
  
        logger = test_utils::TestLogger::new();
        fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
        send_payment(&nodes[0], &[&nodes[1]], 1000000);
  }
  
 -#[test]
 -fn mpp_failure() {
 -      let chanmon_cfgs = create_chanmon_cfgs(4);
 -      let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
 -      let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
 -      let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
 -
 -      let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
 -      let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
 -      let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
 -      let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
 -      let logger = test_utils::TestLogger::new();
 -
 -      let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(&nodes[3]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let mut route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[3].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
 -      let path = route.paths[0].clone();
 -      route.paths.push(path);
 -      route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
 -      route.paths[0][0].short_channel_id = chan_1_id;
 -      route.paths[0][1].short_channel_id = chan_3_id;
 -      route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
 -      route.paths[1][0].short_channel_id = chan_2_id;
 -      route.paths[1][1].short_channel_id = chan_4_id;
 -      send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], 200_000, payment_hash, payment_secret);
 -      fail_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_hash);
 -}
 -
 -#[test]
 -fn test_dup_htlc_onchain_fails_on_reload() {
 -      // When a Channel is closed, any outbound HTLCs which were relayed through it are simply
 -      // dropped when the Channel is. From there, the ChannelManager relies on the ChannelMonitor
 -      // having a copy of the relevant fail-/claim-back data and processes the HTLC fail/claim when
 -      // the ChannelMonitor tells it to.
 -      //
 -      // If, due to an on-chain event, an HTLC is failed/claimed, and then we serialize the
 -      // ChannelManager, we generally expect there not to be a duplicate HTLC fail/claim (eg via a
 -      // PaymentPathFailed event appearing). However, because we may not serialize the relevant
 -      // ChannelMonitor at the same time, this isn't strictly guaranteed. In order to provide this
 -      // consistency, the ChannelManager explicitly tracks pending-onchain-resolution outbound HTLCs
 -      // and de-duplicates ChannelMonitor events.
 -      //
 -      // This tests that explicit tracking behavior.
 -      let chanmon_cfgs = create_chanmon_cfgs(2);
 -      let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
 -      let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
 -      let persister: test_utils::TestPersister;
 -      let new_chain_monitor: test_utils::TestChainMonitor;
 -      let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
 -      let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
 -
 -      create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -
 -      // Route a payment, but force-close the channel before the HTLC fulfill message arrives at
 -      // nodes[0].
 -      let (payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1]], 10000000);
 -      nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id).unwrap();
 -      check_closed_broadcast!(nodes[0], true);
 -      check_added_monitors!(nodes[0], 1);
 -      check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed);
 -
 -      nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
 -      nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
 -
 -      // Connect blocks until the CLTV timeout is up so that we get an HTLC-Timeout transaction
 -      connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
 -      let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
 -      assert_eq!(node_txn.len(), 3);
 -      assert_eq!(node_txn[0], node_txn[1]);
 -
 -      assert!(nodes[1].node.claim_funds(payment_preimage));
 -      check_added_monitors!(nodes[1], 1);
 -
 -      let mut header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
 -      connect_block(&nodes[1], &Block { header, txdata: vec![node_txn[1].clone(), node_txn[2].clone()]});
 -      check_closed_broadcast!(nodes[1], true);
 -      check_added_monitors!(nodes[1], 1);
 -      check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
 -      let claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
 -
 -      header.prev_blockhash = nodes[0].best_block_hash();
 -      connect_block(&nodes[0], &Block { header, txdata: vec![node_txn[1].clone(), node_txn[2].clone()]});
 -
 -      // Serialize out the ChannelMonitor before connecting the on-chain claim transactions. This is
 -      // fairly normal behavior as ChannelMonitor(s) are often not re-serialized when on-chain events
 -      // happen, unlike ChannelManager which tends to be re-serialized after any relevant event(s).
 -      let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
 -      nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
 -
 -      header.prev_blockhash = nodes[0].best_block_hash();
 -      let claim_block = Block { header, txdata: claim_txn};
 -      connect_block(&nodes[0], &claim_block);
 -      expect_payment_sent!(nodes[0], payment_preimage);
 -
 -      // ChannelManagers generally get re-serialized after any relevant event(s). Since we just
 -      // connected a highly-relevant block, it likely gets serialized out now.
 -      let mut chan_manager_serialized = test_utils::TestVecWriter(Vec::new());
 -      nodes[0].node.write(&mut chan_manager_serialized).unwrap();
 -
 -      // Now reload nodes[0]...
 -      persister = test_utils::TestPersister::new();
 -      let keys_manager = &chanmon_cfgs[0].keys_manager;
 -      new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), nodes[0].logger, node_cfgs[0].fee_estimator, &persister, keys_manager);
 -      nodes[0].chain_monitor = &new_chain_monitor;
 -      let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
 -      let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(
 -              &mut chan_0_monitor_read, keys_manager).unwrap();
 -      assert!(chan_0_monitor_read.is_empty());
 -
 -      let (_, nodes_0_deserialized_tmp) = {
 -              let mut channel_monitors = HashMap::new();
 -              channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor);
 -              <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>
 -                      ::read(&mut io::Cursor::new(&chan_manager_serialized.0[..]), ChannelManagerReadArgs {
 -                              default_config: Default::default(),
 -                              keys_manager,
 -                              fee_estimator: node_cfgs[0].fee_estimator,
 -                              chain_monitor: nodes[0].chain_monitor,
 -                              tx_broadcaster: nodes[0].tx_broadcaster.clone(),
 -                              logger: nodes[0].logger,
 -                              channel_monitors,
 -                      }).unwrap()
 -      };
 -      nodes_0_deserialized = nodes_0_deserialized_tmp;
 -
 -      assert!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor).is_ok());
 -      check_added_monitors!(nodes[0], 1);
 -      nodes[0].node = &nodes_0_deserialized;
 -
 -      // Note that if we re-connect the block which exposed nodes[0] to the payment preimage (but
 -      // which the current ChannelMonitor has not seen), the ChannelManager's de-duplication of
 -      // payment events should kick in, leaving us with no pending events here.
 -      let height = nodes[0].blocks.lock().unwrap().len() as u32 - 1;
 -      nodes[0].chain_monitor.chain_monitor.block_connected(&claim_block, height);
 -      assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
 -}
 -
  #[test]
  fn test_manager_serialize_deserialize_events() {
        // This test makes sure the events field in ChannelManager survives de/serialization
                added_monitors.clear();
        }
  
 -      node_a.node.handle_funding_signed(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendFundingSigned, node_a.node.get_our_node_id()));
 +      let bs_funding_signed = get_event_msg!(node_b, MessageSendEvent::SendFundingSigned, node_a.node.get_our_node_id());
 +      node_a.node.handle_funding_signed(&node_b.node.get_our_node_id(), &bs_funding_signed);
        {
                let mut added_monitors = node_a.chain_monitor.added_monitors.lock().unwrap();
                assert_eq!(added_monitors.len(), 1);
        // Start the de/seriailization process mid-channel creation to check that the channel manager will hold onto events that are serialized
        let nodes_0_serialized = nodes[0].node.encode();
        let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
 -      nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
 +      get_monitor!(nodes[0], bs_funding_signed.channel_id).write(&mut chan_0_monitor_serialized).unwrap();
  
        fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
        logger = test_utils::TestLogger::new();
@@@ -4230,7 -4427,7 +4230,7 @@@ fn test_simple_manager_serialize_deseri
        let new_chain_monitor: test_utils::TestChainMonitor;
        let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
 -      create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 +      let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
  
        let (our_payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
        let (_, our_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
  
        let nodes_0_serialized = nodes[0].node.encode();
        let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
 -      nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
 +      get_monitor!(nodes[0], chan_id).write(&mut chan_0_monitor_serialized).unwrap();
  
        logger = test_utils::TestLogger::new();
        fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
@@@ -4291,14 -4488,14 +4291,14 @@@ fn test_manager_serialize_deserialize_i
        let new_chain_monitor: test_utils::TestChainMonitor;
        let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
        let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
 -      create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      create_announced_chan_between_nodes(&nodes, 2, 0, InitFeatures::known(), InitFeatures::known());
 +      let chan_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
 +      let chan_id_2 = create_announced_chan_between_nodes(&nodes, 2, 0, InitFeatures::known(), InitFeatures::known()).2;
        let (_, _, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 3, InitFeatures::known(), InitFeatures::known());
  
        let mut node_0_stale_monitors_serialized = Vec::new();
 -      for monitor in nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter() {
 +      for chan_id_iter in &[chan_id_1, chan_id_2, channel_id] {
                let mut writer = test_utils::TestVecWriter(Vec::new());
 -              monitor.1.write(&mut writer).unwrap();
 +              get_monitor!(nodes[0], chan_id_iter).write(&mut writer).unwrap();
                node_0_stale_monitors_serialized.push(writer.0);
        }
  
        // Now the ChannelMonitor (which is now out-of-sync with ChannelManager for channel w/
        // nodes[3])
        let mut node_0_monitors_serialized = Vec::new();
 -      for monitor in nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter() {
 +      for chan_id_iter in &[chan_id_1, chan_id_2, channel_id] {
                let mut writer = test_utils::TestVecWriter(Vec::new());
 -              monitor.1.write(&mut writer).unwrap();
 +              get_monitor!(nodes[0], chan_id_iter).write(&mut writer).unwrap();
                node_0_monitors_serialized.push(writer.0);
        }
  
@@@ -4961,7 -5158,8 +4961,7 @@@ fn test_duplicate_payment_hash_one_fail
        // We reduce the final CLTV here by a somewhat arbitrary constant to keep it under the one-byte
        // script push size limit so that the below script length checks match
        // ACCEPTED_HTLC_SCRIPT_WEIGHT.
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &nodes[0].net_graph_msg_handler.network_graph,
 -              &nodes[3].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 900000, TEST_FINAL_CLTV - 40, nodes[0].logger).unwrap();
 +      let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[3], vec![], 900000, TEST_FINAL_CLTV - 40);
        send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[2], &nodes[3]]], 900000, duplicate_payment_hash, payment_secret);
  
        let commitment_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
  
        let events = nodes[0].node.get_and_clear_pending_events();
        match events[0] {
 -              Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
 +              Event::PaymentSent { ref payment_preimage, ref payment_hash, .. } => {
                        assert_eq!(*payment_preimage, our_payment_preimage);
                        assert_eq!(*payment_hash, duplicate_payment_hash);
                }
@@@ -5141,6 -5339,7 +5141,6 @@@ fn do_test_fail_backwards_unrevoked_rem
        let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs,
                &[Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone())]);
        let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
 -      let logger = test_utils::TestLogger::new();
  
        create_announced_chan_between_nodes(&nodes, 0, 2, InitFeatures::known(), InitFeatures::known());
        create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
        let (_, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
        // 1st HTLC:
        let (_, payment_hash_2, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
 -      let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -      let our_node_id = &nodes[1].node.get_our_node_id();
 -      let route = get_route(our_node_id, &net_graph_msg_handler.network_graph, &nodes[5].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], ds_dust_limit*1000);
        // 2nd HTLC:
        send_along_route_with_secret(&nodes[1], route.clone(), &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_1, nodes[5].node.create_inbound_payment_for_hash(payment_hash_1, None, 7200, 0).unwrap()); // not added < dust limit + HTLC tx fee
        // 3rd HTLC:
        let (_, payment_hash_3, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
        // 5th HTLC:
        let (_, payment_hash_4, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
 -      let route = get_route(our_node_id, &net_graph_msg_handler.network_graph, &nodes[5].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], 1000000);
        // 6th HTLC:
        send_along_route_with_secret(&nodes[1], route.clone(), &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_3, nodes[5].node.create_inbound_payment_for_hash(payment_hash_3, None, 7200, 0).unwrap());
        // 7th HTLC:
        // 8th HTLC:
        let (_, payment_hash_5, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
        // 9th HTLC:
 -      let route = get_route(our_node_id, &net_graph_msg_handler.network_graph, &nodes[5].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], ds_dust_limit*1000);
        send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_5, nodes[5].node.create_inbound_payment_for_hash(payment_hash_5, None, 7200, 0).unwrap()); // not added < dust limit + HTLC tx fee
  
        // 10th HTLC:
        let (_, payment_hash_6, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
        // 11th HTLC:
 -      let route = get_route(our_node_id, &net_graph_msg_handler.network_graph, &nodes[5].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], 1000000);
        send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_6, nodes[5].node.create_inbound_payment_for_hash(payment_hash_6, None, 7200, 0).unwrap());
  
        // Double-check that six of the new HTLC were added
@@@ -5453,7 -5654,7 +5453,7 @@@ fn test_key_derivation_params() 
        let seed = [42; 32];
        let keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
        let chain_monitor = test_utils::TestChainMonitor::new(Some(&chanmon_cfgs[0].chain_source), &chanmon_cfgs[0].tx_broadcaster, &chanmon_cfgs[0].logger, &chanmon_cfgs[0].fee_estimator, &chanmon_cfgs[0].persister, &keys_manager);
 -      let node = NodeCfg { chain_source: &chanmon_cfgs[0].chain_source, logger: &chanmon_cfgs[0].logger, tx_broadcaster: &chanmon_cfgs[0].tx_broadcaster, fee_estimator: &chanmon_cfgs[0].fee_estimator, chain_monitor, keys_manager: &keys_manager, node_seed: seed, features: InitFeatures::known() };
 +      let node = NodeCfg { chain_source: &chanmon_cfgs[0].chain_source, logger: &chanmon_cfgs[0].logger, tx_broadcaster: &chanmon_cfgs[0].tx_broadcaster, fee_estimator: &chanmon_cfgs[0].fee_estimator, chain_monitor, keys_manager: &keys_manager, network_graph: &chanmon_cfgs[0].network_graph, node_seed: seed, features: InitFeatures::known() };
        let mut node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
        node_cfgs.remove(0);
        node_cfgs.insert(0, node);
@@@ -5571,7 -5772,7 +5571,7 @@@ fn do_htlc_claim_local_commitment_only(
        let events = nodes[0].node.get_and_clear_pending_events();
        assert_eq!(events.len(), 1);
        match events[0] {
 -              Event::PaymentSent { payment_preimage, payment_hash } => {
 +              Event::PaymentSent { payment_preimage, payment_hash, .. } => {
                        assert_eq!(payment_preimage, our_payment_preimage);
                        assert_eq!(payment_hash, our_payment_hash);
                },
@@@ -5605,8 -5806,11 +5605,8 @@@ fn do_htlc_claim_current_remote_commitm
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
 -      let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), if use_dust { 50000 } else { 3000000 }, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], if use_dust { 50000 } else { 3000000 });
        nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
        check_added_monitors!(nodes[0], 1);
  
@@@ -5813,6 -6017,7 +5813,6 @@@ fn test_fail_holding_cell_htlc_upon_fre
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        // First nodes[0] generates an update_fee, setting the channel's
        // pending_update_fee.
        let feerate = get_feerate!(nodes[0], chan.2);
  
        // 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
        let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], max_can_send, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
  
        // Send a payment which passes reserve checks but gets stuck in the holding cell.
 -      nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
 +      let our_payment_id = nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        chan_stat = get_channel_value_stat!(nodes[0], chan.2);
        assert_eq!(chan_stat.holding_cell_outbound_amount_msat, max_can_send);
  
        let events = nodes[0].node.get_and_clear_pending_events();
        assert_eq!(events.len(), 1);
        match &events[0] {
 -              &Event::PaymentPathFailed { ref payment_hash, ref rejected_by_dest, ref network_update, ref all_paths_failed, path: _, ref short_channel_id, ref error_code, ref error_data } => {
 +              &Event::PaymentPathFailed { ref payment_id, ref payment_hash, ref rejected_by_dest, ref network_update, ref all_paths_failed, ref short_channel_id, ref error_code, ref error_data, .. } => {
 +                      assert_eq!(our_payment_id, *payment_id.as_ref().unwrap());
                        assert_eq!(our_payment_hash.clone(), *payment_hash);
                        assert_eq!(*rejected_by_dest, false);
                        assert_eq!(*all_paths_failed, true);
@@@ -5892,6 -6098,7 +5892,6 @@@ fn test_free_and_fail_holding_cell_htlc
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        // First nodes[0] generates an update_fee, setting the channel's
        // pending_update_fee.
        let feerate = get_feerate!(nodes[0], chan.2);
  
        // 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
 -      let (payment_preimage_1, payment_hash_1, payment_secret_1) = get_payment_preimage_hash!(nodes[1]);
        let amt_1 = 20000;
 -      let (_, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
        let amt_2 = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 2 + 1) - amt_1;
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route_1 = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], amt_1, TEST_FINAL_CLTV, &logger).unwrap();
 -      let route_2 = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], amt_2, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route_1, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], amt_1);
 +      let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], amt_2);
  
        // Send 2 payments which pass reserve checks but get stuck in the holding cell.
        nodes[0].node.send_payment(&route_1, payment_hash_1, &Some(payment_secret_1)).unwrap();
        chan_stat = get_channel_value_stat!(nodes[0], chan.2);
        assert_eq!(chan_stat.holding_cell_outbound_amount_msat, amt_1);
 -      nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2)).unwrap();
 +      let payment_id_2 = nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2)).unwrap();
        chan_stat = get_channel_value_stat!(nodes[0], chan.2);
        assert_eq!(chan_stat.holding_cell_outbound_amount_msat, amt_1 + amt_2);
  
        let events = nodes[0].node.get_and_clear_pending_events();
        assert_eq!(events.len(), 1);
        match &events[0] {
 -              &Event::PaymentPathFailed { ref payment_hash, ref rejected_by_dest, ref network_update, ref all_paths_failed, path: _, ref short_channel_id, ref error_code, ref error_data } => {
 +              &Event::PaymentPathFailed { ref payment_id, ref payment_hash, ref rejected_by_dest, ref network_update, ref all_paths_failed, ref short_channel_id, ref error_code, ref error_data, .. } => {
 +                      assert_eq!(payment_id_2, *payment_id.as_ref().unwrap());
                        assert_eq!(payment_hash_2.clone(), *payment_hash);
                        assert_eq!(*rejected_by_dest, false);
                        assert_eq!(*all_paths_failed, true);
        let events = nodes[0].node.get_and_clear_pending_events();
        assert_eq!(events.len(), 1);
        match events[0] {
 -              Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
 +              Event::PaymentSent { ref payment_preimage, ref payment_hash, .. } => {
                        assert_eq!(*payment_preimage, payment_preimage_1);
                        assert_eq!(*payment_hash, payment_hash_1);
                }
@@@ -6025,6 -6234,7 +6025,6 @@@ fn test_fail_holding_cell_htlc_upon_fre
        let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
        let chan_0_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
        let chan_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        // First nodes[1] generates an update_fee, setting the channel's
        // pending_update_fee.
        // Send a payment which passes reserve checks but gets stuck in the holding cell.
        let feemsat = 239;
        let total_routing_fee_msat = (nodes.len() - 2) as u64 * feemsat;
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[2]);
        let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1) - total_routing_fee_msat;
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], max_can_send);
        let payment_event = {
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], max_can_send, TEST_FINAL_CLTV, &logger).unwrap();
                nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
                check_added_monitors!(nodes[0], 1);
  
@@@ -6155,7 -6367,10 +6155,7 @@@ fn test_update_add_htlc_bolt2_sender_va
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
  
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let logger = test_utils::TestLogger::new();
 -      let mut route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
        route.paths[0][0].fee_msat = 100;
  
        unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err },
@@@ -6172,8 -6387,11 +6172,8 @@@ fn test_update_add_htlc_bolt2_sender_ze
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
  
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let logger = test_utils::TestLogger::new();
 -      let mut route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
        route.paths[0][0].fee_msat = 0;
        unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err },
                assert_eq!(err, "Cannot send 0-msat HTLC"));
@@@ -6191,7 -6409,10 +6191,7 @@@ fn test_update_add_htlc_bolt2_receiver_
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
  
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let logger = test_utils::TestLogger::new();
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
        nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        check_added_monitors!(nodes[0], 1);
        let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
@@@ -6213,8 -6434,12 +6213,8 @@@ fn test_update_add_htlc_bolt2_sender_cl
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000000, 500000001, &logger).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], vec![], 100000000, 500000001);
        unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::RouteError { ref err },
                assert_eq!(err, &"Channel CLTV overflowed?"));
  }
@@@ -6231,9 -6456,12 +6231,9 @@@ fn test_update_add_htlc_bolt2_sender_ex
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0, InitFeatures::known(), InitFeatures::known());
        let max_accepted_htlcs = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().counterparty_max_accepted_htlcs as u64;
  
 -      let logger = test_utils::TestLogger::new();
        for i in 0..max_accepted_htlcs {
 -              let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 +              let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
                let payment_event = {
 -                      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -                      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
                        nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
                        check_added_monitors!(nodes[0], 1);
  
                expect_pending_htlcs_forwardable!(nodes[1]);
                expect_payment_received!(nodes[1], our_payment_hash, our_payment_secret, 100000);
        }
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
        unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err },
                assert!(regex::Regex::new(r"Cannot push more than their max accepted HTLCs \(\d+\)").unwrap().is_match(err)));
  
@@@ -6274,10 -6504,14 +6274,10 @@@ fn test_update_add_htlc_bolt2_sender_ex
  
        send_payment(&nodes[0], &vec!(&nodes[1])[..], max_in_flight);
  
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 +      let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_in_flight);
        // Manually create a route over our max in flight (which our router normally automatically
        // limits us to.
 -      let route = Route { paths: vec![vec![RouteHop {
 -         pubkey: nodes[1].node.get_our_node_id(), node_features: NodeFeatures::known(), channel_features: ChannelFeatures::known(),
 -         short_channel_id: nodes[1].node.list_usable_channels()[0].short_channel_id.unwrap(),
 -         fee_msat: max_in_flight + 1, cltv_expiry_delta: TEST_FINAL_CLTV
 -      }]] };
 +      route.paths[0][0].fee_msat =  max_in_flight + 1;
        unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err },
                assert!(regex::Regex::new(r"Cannot send value that would put us over the max HTLC value in flight our peer will accept \(\d+\)").unwrap().is_match(err)));
  
@@@ -6303,7 -6537,10 +6303,7 @@@ fn test_update_add_htlc_bolt2_receiver_
                htlc_minimum_msat = channel.get_holder_htlc_minimum_msat();
        }
  
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let logger = test_utils::TestLogger::new();
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], htlc_minimum_msat, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], htlc_minimum_msat);
        nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        check_added_monitors!(nodes[0], 1);
        let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
@@@ -6324,6 -6561,7 +6324,6 @@@ fn test_update_add_htlc_bolt2_receiver_
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        let chan_stat = get_channel_value_stat!(nodes[0], chan.2);
        let channel_reserve = chan_stat.channel_reserve_msat;
        let commit_tx_fee_outbound = 2 * commit_tx_fee_msat(feerate, 1 + 1);
  
        let max_can_send = 5000000 - channel_reserve - commit_tx_fee_outbound;
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], max_can_send, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
        nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        check_added_monitors!(nodes[0], 1);
        let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
@@@ -6359,9 -6599,14 +6359,9 @@@ fn test_update_add_htlc_bolt2_receiver_
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 3999999);
        let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
 -
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 3999999, TEST_FINAL_CLTV, &logger).unwrap();
 -
        let cur_height = nodes[0].node.best_block.read().unwrap().height() + 1;
        let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::signing_only(), &route.paths[0], &session_priv).unwrap();
        let (onion_payloads, _htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0], 3999999, &Some(our_payment_secret), cur_height, &None).unwrap();
@@@ -6398,8 -6643,11 +6398,8 @@@ fn test_update_add_htlc_bolt2_receiver_
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        check_added_monitors!(nodes[0], 1);
        let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
@@@ -6420,9 -6668,12 +6420,9 @@@ fn test_update_add_htlc_bolt2_receiver_
        let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
 -      let logger = test_utils::TestLogger::new();
  
        create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        check_added_monitors!(nodes[0], 1);
        let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
@@@ -6445,9 -6696,12 +6445,9 @@@ fn test_update_add_htlc_bolt2_receiver_
        let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
 -      let logger = test_utils::TestLogger::new();
  
        create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        check_added_monitors!(nodes[0], 1);
        let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
@@@ -6491,8 -6745,11 +6491,8 @@@ fn test_update_fulfill_htlc_bolt2_updat
        let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
 -      let logger = test_utils::TestLogger::new();
        let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
  
        check_added_monitors!(nodes[0], 1);
@@@ -6523,8 -6780,11 +6523,8 @@@ fn test_update_fulfill_htlc_bolt2_updat
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        check_added_monitors!(nodes[0], 1);
        let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
@@@ -6554,8 -6814,11 +6554,8 @@@ fn test_update_fulfill_htlc_bolt2_updat
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        check_added_monitors!(nodes[0], 1);
        let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
@@@ -6669,8 -6932,11 +6669,8 @@@ fn test_update_fulfill_htlc_bolt2_missi
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        check_added_monitors!(nodes[0], 1);
  
@@@ -6717,11 -6983,14 +6717,11 @@@ fn test_update_fulfill_htlc_bolt2_after
        let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
        create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
        create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
 -      let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[2]);
 +      let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 100000);
  
        //First hop
        let mut payment_event = {
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
                nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
                check_added_monitors!(nodes[0], 1);
                let mut events = nodes[0].node.get_and_clear_pending_msg_events();
@@@ -7059,7 -7328,7 +7059,7 @@@ fn test_data_loss_protect() 
        // Cache node A state before any channel update
        let previous_node_state = nodes[0].node.encode();
        let mut previous_chain_monitor_state = test_utils::TestVecWriter(Vec::new());
 -      nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter().next().unwrap().1.write(&mut previous_chain_monitor_state).unwrap();
 +      get_monitor!(nodes[0], chan.2).write(&mut previous_chain_monitor_state).unwrap();
  
        send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
        send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
@@@ -7160,9 -7429,7 +7160,9 @@@ fn test_check_htlc_underpaying() 
        // Create some initial channels
        create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
  
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &nodes[0].net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 10_000, TEST_FINAL_CLTV, nodes[0].logger).unwrap();
 +      let scorer = test_utils::TestScorer::with_fixed_penalty(0);
 +      let payee = Payee::from_node_id(nodes[1].node.get_our_node_id()).with_features(InvoiceFeatures::known());
 +      let route = get_route(&nodes[0].node.get_our_node_id(), &payee, nodes[0].network_graph, None, 10_000, TEST_FINAL_CLTV, nodes[0].logger, &scorer).unwrap();
        let (_, our_payment_hash, _) = get_payment_preimage_hash!(nodes[0]);
        let our_payment_secret = nodes[1].node.create_inbound_payment_for_hash(our_payment_hash, Some(100_000), 7200, 0).unwrap();
        nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
@@@ -7302,7 -7569,7 +7302,7 @@@ fn test_priv_forwarding_rejection() 
        let nodes_1_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
        let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
  
 -      create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known());
 +      let chan_id_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known()).2;
  
        // Note that the create_*_chan functions in utils requires announcement_signatures, which we do
        // not send for private channels.
        nodes[2].node.handle_funding_created(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingCreated, nodes[2].node.get_our_node_id()));
        check_added_monitors!(nodes[2], 1);
  
 -      nodes[1].node.handle_funding_signed(&nodes[2].node.get_our_node_id(), &get_event_msg!(nodes[2], MessageSendEvent::SendFundingSigned, nodes[1].node.get_our_node_id()));
 +      let cs_funding_signed = get_event_msg!(nodes[2], MessageSendEvent::SendFundingSigned, nodes[1].node.get_our_node_id());
 +      nodes[1].node.handle_funding_signed(&nodes[2].node.get_our_node_id(), &cs_funding_signed);
        check_added_monitors!(nodes[1], 1);
  
        let conf_height = core::cmp::max(nodes[1].best_block_info().1 + 1, nodes[2].best_block_info().1 + 1);
  
        // ... however, if we send to nodes[2], we will have to pass the private channel from nodes[1]
        // to nodes[2], which should be rejected:
 -      let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[2]);
 -      let route = get_route(&nodes[0].node.get_our_node_id(),
 -              &nodes[0].net_graph_msg_handler.network_graph,
 -              &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None,
 -              &[&RouteHint(vec![RouteHintHop {
 -                      src_node_id: nodes[1].node.get_our_node_id(),
 -                      short_channel_id: nodes[2].node.list_channels()[0].short_channel_id.unwrap(),
 -                      fees: RoutingFees { base_msat: 1000, proportional_millionths: 0 },
 -                      cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
 -                      htlc_minimum_msat: None,
 -                      htlc_maximum_msat: None,
 -              }])], 10_000, TEST_FINAL_CLTV, nodes[0].logger).unwrap();
 +      let route_hint = RouteHint(vec![RouteHintHop {
 +              src_node_id: nodes[1].node.get_our_node_id(),
 +              short_channel_id: nodes[2].node.list_channels()[0].short_channel_id.unwrap(),
 +              fees: RoutingFees { base_msat: 1000, proportional_millionths: 0 },
 +              cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
 +              htlc_minimum_msat: None,
 +              htlc_maximum_msat: None,
 +      }]);
 +      let last_hops = vec![route_hint];
 +      let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], last_hops, 10_000, TEST_FINAL_CLTV);
  
        nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        check_added_monitors!(nodes[0], 1);
        let nodes_1_serialized = nodes[1].node.encode();
        let mut monitor_a_serialized = test_utils::TestVecWriter(Vec::new());
        let mut monitor_b_serialized = test_utils::TestVecWriter(Vec::new());
 -      {
 -              let mons = nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap();
 -              let mut mon_iter = mons.iter();
 -              mon_iter.next().unwrap().1.write(&mut monitor_a_serialized).unwrap();
 -              mon_iter.next().unwrap().1.write(&mut monitor_b_serialized).unwrap();
 -      }
 +      get_monitor!(nodes[1], chan_id_1).write(&mut monitor_a_serialized).unwrap();
 +      get_monitor!(nodes[1], cs_funding_signed.channel_id).write(&mut monitor_b_serialized).unwrap();
  
        persister = test_utils::TestPersister::new();
        let keys_manager = &chanmon_cfgs[1].keys_manager;
@@@ -7453,9 -7725,11 +7453,9 @@@ fn test_bump_penalty_txn_on_revoked_com
        let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
  
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, InitFeatures::known(), InitFeatures::known());
 -      let logger = test_utils::TestLogger::new();
  
        let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
 -      let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -      let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 3000000, 30, &logger).unwrap();
 +      let (route,_, _, _) = get_route_and_payment_hash!(nodes[1], nodes[0], vec![], 3000000, 30);
        send_along_route(&nodes[1], route, &vec!(&nodes[0])[..], 3000000);
  
        let revoked_txn = get_local_commitment_txn!(nodes[0], chan.2);
@@@ -7559,14 -7833,11 +7559,14 @@@ fn test_bump_penalty_txn_on_revoked_htl
  
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, InitFeatures::known(), InitFeatures::known());
        // Lock HTLC in both directions (using a slightly lower CLTV delay to provide timely RBF bumps)
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &nodes[0].net_graph_msg_handler.network_graph,
 -              &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 3_000_000, 50, nodes[0].logger).unwrap();
 +      let payee = Payee::from_node_id(nodes[1].node.get_our_node_id()).with_features(InvoiceFeatures::known());
 +      let scorer = test_utils::TestScorer::with_fixed_penalty(0);
 +      let route = get_route(&nodes[0].node.get_our_node_id(), &payee, &nodes[0].network_graph, None,
 +              3_000_000, 50, nodes[0].logger, &scorer).unwrap();
        let payment_preimage = send_along_route(&nodes[0], route, &[&nodes[1]], 3_000_000).0;
 -      let route = get_route(&nodes[1].node.get_our_node_id(), &nodes[1].net_graph_msg_handler.network_graph,
 -              &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 3_000_000, 50, nodes[0].logger).unwrap();
 +      let payee = Payee::from_node_id(nodes[0].node.get_our_node_id()).with_features(InvoiceFeatures::known());
 +      let route = get_route(&nodes[1].node.get_our_node_id(), &payee, nodes[1].network_graph, None,
 +              3_000_000, 50, nodes[0].logger, &scorer).unwrap();
        send_along_route(&nodes[1], route, &[&nodes[0]], 3_000_000);
  
        let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan.2);
@@@ -7902,9 -8173,11 +7902,9 @@@ fn test_bump_txn_sanitize_tracking_maps
        connect_block(&nodes[0], &Block { header: header_130, txdata: penalty_txn });
        connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
        {
 -              let monitors = nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap();
 -              if let Some(monitor) = monitors.get(&OutPoint { txid: chan.3.txid(), index: 0 }) {
 -                      assert!(monitor.inner.lock().unwrap().onchain_tx_handler.pending_claim_requests.is_empty());
 -                      assert!(monitor.inner.lock().unwrap().onchain_tx_handler.claimable_outpoints.is_empty());
 -              }
 +              let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(OutPoint { txid: chan.3.txid(), index: 0 }).unwrap();
 +              assert!(monitor.inner.lock().unwrap().onchain_tx_handler.pending_claim_requests.is_empty());
 +              assert!(monitor.inner.lock().unwrap().onchain_tx_handler.claimable_outpoints.is_empty());
        }
  }
  
@@@ -7957,8 -8230,11 +7957,8 @@@ fn test_simple_mpp() 
        let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
        let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
        let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
 -      let logger = test_utils::TestLogger::new();
  
 -      let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(&nodes[3]);
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let mut route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[3].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[3], 100000);
        let path = route.paths[0].clone();
        route.paths.push(path);
        route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
@@@ -7983,7 -8259,10 +7983,7 @@@ fn test_preimage_storage() 
  
        {
                let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(100_000), 7200, 42);
 -
 -              let logger = test_utils::TestLogger::new();
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100_000, TEST_FINAL_CLTV, &logger).unwrap();
 +              let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
                nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
                check_added_monitors!(nodes[0], 1);
                let mut events = nodes[0].node.get_and_clear_pending_msg_events();
@@@ -8052,7 -8331,9 +8052,7 @@@ fn test_secret_timeout() 
        assert_ne!(payment_secret_1, our_payment_secret);
  
        {
 -              let logger = test_utils::TestLogger::new();
 -              let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -              let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100_000, TEST_FINAL_CLTV, &logger).unwrap();
 +              let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
                nodes[0].node.send_payment(&route, payment_hash, &Some(our_payment_secret)).unwrap();
                check_added_monitors!(nodes[0], 1);
                let mut events = nodes[0].node.get_and_clear_pending_msg_events();
@@@ -8089,7 -8370,10 +8089,7 @@@ fn test_bad_secret_hash() 
        let random_payment_hash = PaymentHash([42; 32]);
        let random_payment_secret = PaymentSecret([43; 32]);
        let (our_payment_hash, our_payment_secret) = nodes[1].node.create_inbound_payment(Some(100_000), 2, 0);
 -
 -      let logger = test_utils::TestLogger::new();
 -      let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
 -      let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100_000, TEST_FINAL_CLTV, &logger).unwrap();
 +      let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
  
        // All the below cases should end up being handled exactly identically, so we macro the
        // resulting events.
@@@ -8169,7 -8453,8 +8169,7 @@@ fn test_update_err_monitor_lockdown() 
        let logger = test_utils::TestLogger::with_id(format!("node {}", 0));
        let persister = test_utils::TestPersister::new();
        let watchtower = {
 -              let monitors = nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap();
 -              let monitor = monitors.get(&outpoint).unwrap();
 +              let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
                let mut w = test_utils::TestVecWriter(Vec::new());
                monitor.write(&mut w).unwrap();
                let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingSigner>)>::read(
@@@ -8230,7 -8515,8 +8230,7 @@@ fn test_concurrent_monitor_claim() 
        let logger = test_utils::TestLogger::with_id(format!("node {}", "Alice"));
        let persister = test_utils::TestPersister::new();
        let watchtower_alice = {
 -              let monitors = nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap();
 -              let monitor = monitors.get(&outpoint).unwrap();
 +              let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
                let mut w = test_utils::TestVecWriter(Vec::new());
                monitor.write(&mut w).unwrap();
                let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingSigner>)>::read(
        let logger = test_utils::TestLogger::with_id(format!("node {}", "Bob"));
        let persister = test_utils::TestPersister::new();
        let watchtower_bob = {
 -              let monitors = nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap();
 -              let monitor = monitors.get(&outpoint).unwrap();
 +              let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
                let mut w = test_utils::TestVecWriter(Vec::new());
                monitor.write(&mut w).unwrap();
                let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingSigner>)>::read(
        watchtower_bob.chain_monitor.block_connected(&Block { header, txdata: vec![] }, CHAN_CONFIRM_DEPTH + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
  
        // Route another payment to generate another update with still previous HTLC pending
 -      let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[0]);
 +      let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 3000000);
        {
 -              let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
 -              let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 3000000 , TEST_FINAL_CLTV, &logger).unwrap();
                nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
        }
        check_added_monitors!(nodes[1], 1);
@@@ -8943,8 -9232,8 +8943,8 @@@ fn test_forwardable_regen() 
        let new_chain_monitor: test_utils::TestChainMonitor;
        let nodes_1_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
        let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
 -      create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
 -      create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
 +      let chan_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
 +      let chan_id_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known()).2;
  
        // First send a payment to nodes[1]
        let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
        let nodes_1_serialized = nodes[1].node.encode();
        let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
        let mut chan_1_monitor_serialized = test_utils::TestVecWriter(Vec::new());
 -      {
 -              let monitors = nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap();
 -              let mut monitor_iter = monitors.iter();
 -              monitor_iter.next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
 -              monitor_iter.next().unwrap().1.write(&mut chan_1_monitor_serialized).unwrap();
 -      }
 +      get_monitor!(nodes[1], chan_id_1).write(&mut chan_0_monitor_serialized).unwrap();
 +      get_monitor!(nodes[1], chan_id_2).write(&mut chan_1_monitor_serialized).unwrap();
  
        persister = test_utils::TestPersister::new();
        let keys_manager = &chanmon_cfgs[1].keys_manager;
@@@ -9052,16 -9345,12 +9052,16 @@@ fn test_keysend_payments_to_public_node
        let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
  
        let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
 -      let network_graph = &nodes[0].net_graph_msg_handler.network_graph;
 +      let network_graph = nodes[0].network_graph;
        let payer_pubkey = nodes[0].node.get_our_node_id();
        let payee_pubkey = nodes[1].node.get_our_node_id();
 -      let route = get_route(&payer_pubkey, network_graph, &payee_pubkey, None,
 -                        None, &vec![], 10000, 40,
 -                        nodes[0].logger).unwrap();
 +      let params = RouteParameters {
 +              payee: Payee::for_keysend(payee_pubkey),
 +              final_value_msat: 10000,
 +              final_cltv_expiry_delta: 40,
 +      };
 +      let scorer = test_utils::TestScorer::with_fixed_penalty(0);
 +      let route = find_route(&payer_pubkey, &params, network_graph, None, nodes[0].logger, &scorer).unwrap();
  
        let test_preimage = PaymentPreimage([42; 32]);
        let (payment_hash, _) = nodes[0].node.send_spontaneous_payment(&route, Some(test_preimage)).unwrap();
@@@ -9087,18 -9376,11 +9087,18 @@@ fn test_keysend_payments_to_private_nod
        nodes[1].node.peer_connected(&payer_pubkey, &msgs::Init { features: InitFeatures::known() });
  
        let _chan = create_chan_between_nodes(&nodes[0], &nodes[1], InitFeatures::known(), InitFeatures::known());
 -      let network_graph = &nodes[0].net_graph_msg_handler.network_graph;
 +      let params = RouteParameters {
 +              payee: Payee::for_keysend(payee_pubkey),
 +              final_value_msat: 10000,
 +              final_cltv_expiry_delta: 40,
 +      };
 +      let network_graph = nodes[0].network_graph;
        let first_hops = nodes[0].node.list_usable_channels();
 -      let route = get_keysend_route(&payer_pubkey, &network_graph, &payee_pubkey,
 -                                Some(&first_hops.iter().collect::<Vec<_>>()), &vec![], 10000, 40,
 -                                nodes[0].logger).unwrap();
 +      let scorer = test_utils::TestScorer::with_fixed_penalty(0);
 +      let route = find_route(
 +              &payer_pubkey, &params, network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
 +              nodes[0].logger, &scorer
 +      ).unwrap();
  
        let test_preimage = PaymentPreimage([42; 32]);
        let (payment_hash, _) = nodes[0].node.send_spontaneous_payment(&route, Some(test_preimage)).unwrap();