Merge pull request #1134 from jkczyz/2021-10-payee-arg
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Mon, 25 Oct 2021 16:09:06 +0000 (16:09 +0000)
committerGitHub <noreply@github.com>
Mon, 25 Oct 2021 16:09:06 +0000 (16:09 +0000)
Payee abstraction for use in get_route and PaymentPathFailed

1  2 
lightning/src/ln/channelmanager.rs
lightning/src/ln/functional_test_utils.rs
lightning/src/ln/functional_tests.rs
lightning/src/ln/onion_route_tests.rs

index 9e4708dcfd6a6dbeb4e676a5e7f495d09cac7c97,385a031cabbec5db8cb1b78e6a5f88aaf1e4b441..63c4bcc6b13813df6ef93472532ab4e67bb5ef4b
@@@ -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,
@@@ -189,8 -189,7 +189,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>,
        },
  }
 +#[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 } => {
 +                              1u8.hash(hasher);
 +                              path.hash(hasher);
 +                              session_priv[..].hash(hasher);
 +                              payment_id.hash(hasher);
 +                              payment_secret.hash(hasher);
 +                              first_hop_htlc_msat.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,
                }
        }
  }
@@@ -438,51 -416,19 +438,51 @@@ pub(crate) enum PendingOutboundPayment 
                /// 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 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 part_amt_msat is None and !self.is_fulfilled
 +      fn remove(&mut self, session_priv: &[u8; 32], part_amt_msat: Option<u64>) -> 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;
 +                              *pending_amt_msat -= part_amt_msat.expect("We must only not provide an amount if the payment was already fulfilled");
                        }
                }
                remove_res
                        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 {
        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()
                        }
                }
@@@ -1058,7 -1002,7 +1058,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[..]));
                                (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 {
@@@ -1501,7 -1441,7 +1501,7 @@@ impl<Signer: Sign, M: Deref, T: Deref, 
                                        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;
  
                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(),
                                                        session_priv: session_priv.clone(),
                                                        first_hop_htlc_msat: htlc_msat,
                                                        payment_id,
 +                                                      payment_secret: payment_secret.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 {
 +                                      let payment = payment_entry.or_insert_with(|| PendingOutboundPayment::Retryable {
                                                session_privs: HashSet::new(),
                                                pending_amt_msat: 0,
                                                payment_hash: *payment_hash,
                                                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 {
                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 {
                                        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.last().unwrap().fee_msat)) &&
 +                                                      !payment.get().is_fulfilled()
 +                                              {
                                                        self.pending_events.lock().unwrap().push(
                                                                events::Event::PaymentPathFailed {
                                                                        payment_hash,
                                                                        all_paths_failed: payment.get().remaining_parts() == 0,
                                                                        path: path.clone(),
                                                                        short_channel_id: None,
+                                                                       retry: None,
                                                                        #[cfg(test)]
                                                                        error_code: None,
                                                                        #[cfg(test)]
                                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.last().unwrap().fee_msat)) {
                                                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 {
                                                                all_paths_failed,
                                                                path: path.clone(),
                                                                short_channel_id,
+                                                               retry: None,
  #[cfg(test)]
                                                                error_code: onion_error_code,
  #[cfg(test)]
                                                                all_paths_failed,
                                                                path: path.clone(),
                                                                short_channel_id: Some(path.first().unwrap().short_channel_id),
+                                                               retry: None,
  #[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)
 +                              let found_payment = if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(payment_id) {
 +                                      let found_payment = !payment.get().is_fulfilled();
 +                                      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.last().unwrap().fee_msat));
 +                                              if payment.get().remaining_parts() == 0 {
 +                                                      payment.remove();
 +                                              }
 +                                      }
 +                                      found_payment
                                } else { false };
                                if found_payment {
                                        let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
                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)
                                                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 {
@@@ -5374,12 -5249,10 +5377,12 @@@ 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;
                                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),
                                });
                                if payment_id.is_none() {
                                        first_hop_htlc_msat: first_hop_htlc_msat,
                                        path: path.unwrap(),
                                        payment_id: payment_id.unwrap(),
 +                                      payment_secret,
                                })
                        }
                        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 } => {
                                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),
                                 });
                        }
@@@ -5455,13 -5326,10 +5458,13 @@@ 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),
                (2, payment_hash, 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, {
@@@ -5898,49 -5762,6 +5901,49 @@@ impl<'a, Signer: Sign, M: Deref, T: Der
                                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_amt);
 +                                                                      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) => {
 +                                                                      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,
 +                                                                              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();
@@@ -6009,7 -5830,7 +6012,7 @@@ mod tests 
        use ln::functional_test_utils::*;
        use ln::msgs;
        use ln::msgs::ChannelMessageHandler;
-       use routing::router::{get_keysend_route, get_route};
+       use routing::router::{Payee, get_keysend_route, get_route};
        use routing::scorer::Scorer;
        use util::errors::APIError;
        use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
                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, &scorer).unwrap();
+               let payee = Payee::new(expected_route.last().unwrap().node.get_our_node_id())
+                       .with_features(InvoiceFeatures::known());
+               let route = get_route(&nodes[0].node.get_our_node_id(), &payee, &nodes[0].net_graph_msg_handler.network_graph, None, 100_000, TEST_FINAL_CLTV, &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, &scorer).unwrap();
+               let route = get_route(&nodes[0].node.get_our_node_id(), &payee, &nodes[0].net_graph_msg_handler.network_graph, None, 100_000, TEST_FINAL_CLTV, &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();
@@@ -6438,7 -6261,7 +6443,7 @@@ pub mod bench 
        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;
                macro_rules! send_payment {
                        ($node_a: expr, $node_b: expr) => {
                                let usable_channels = $node_a.list_usable_channels();
+                               let payee = Payee::new($node_b.get_our_node_id())
+                                       .with_features(InvoiceFeatures::known());
                                let scorer = Scorer::new(0);
-                               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, &scorer).unwrap();
+                               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 fc57bafdd03c47d10a6e0bfa2f376fe27cae8c48,b45fbd3184c9849320bb1b1c165881e1e7afcf51..fb6c1e489f4d08c711baa366503c664201a5526c
@@@ -14,9 -14,9 +14,9 @@@ use chain::{BestBlock, Confirm, Listen
  use chain::channelmonitor::ChannelMonitor;
  use chain::transaction::OutPoint;
  use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
 -use ln::channelmanager::{ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure};
 +use ln::channelmanager::{ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure, PaymentId};
  use routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
- use routing::router::{Route, get_route};
+ use routing::router::{Payee, Route, get_route};
  use routing::scorer::Scorer;
  use ln::features::{InitFeatures, InvoiceFeatures};
  use ln::msgs;
@@@ -1011,13 -1011,15 +1011,15 @@@ macro_rules! get_route_and_payment_has
        }};
        ($send_node: expr, $recv_node: expr, $last_hops: expr, $recv_value: expr, $cltv: expr) => {{
                let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!($recv_node, Some($recv_value));
+               let payee = $crate::routing::router::Payee::new($recv_node.node.get_our_node_id())
+                       .with_features($crate::ln::features::InvoiceFeatures::known())
+                       .with_route_hints($last_hops);
                let net_graph_msg_handler = &$send_node.net_graph_msg_handler;
                let scorer = ::routing::scorer::Scorer::new(0);
                let route = ::routing::router::get_route(
-                       &$send_node.node.get_our_node_id(), &net_graph_msg_handler.network_graph,
-                       &$recv_node.node.get_our_node_id(), Some(::ln::features::InvoiceFeatures::known()),
+                       &$send_node.node.get_our_node_id(), &payee, &net_graph_msg_handler.network_graph,
                        Some(&$send_node.node.list_usable_channels().iter().collect::<Vec<_>>()),
-                       &$last_hops, $recv_value, $cltv, $send_node.logger, &scorer
+                       $recv_value, $cltv, $send_node.logger, &scorer
                ).unwrap();
                (route, payment_hash, payment_preimage, payment_secret)
        }}
@@@ -1156,11 -1158,10 +1158,11 @@@ macro_rules! expect_payment_failed 
        }
  }
  
 -pub fn send_along_route_with_secret<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, route: Route, expected_paths: &[&[&Node<'a, 'b, 'c>]], recv_value: u64, our_payment_hash: PaymentHash, our_payment_secret: PaymentSecret) {
 -      origin_node.node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
 +pub fn send_along_route_with_secret<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, route: Route, expected_paths: &[&[&Node<'a, 'b, 'c>]], recv_value: u64, our_payment_hash: PaymentHash, our_payment_secret: PaymentSecret) -> PaymentId {
 +      let payment_id = origin_node.node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
        check_added_monitors!(origin_node, expected_paths.len());
        pass_along_route(origin_node, expected_paths, recv_value, our_payment_hash, our_payment_secret);
 +      payment_id
  }
  
  pub fn pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_path: &[&Node<'a, 'b, 'c>], recv_value: u64, our_payment_hash: PaymentHash, our_payment_secret: Option<PaymentSecret>, ev: MessageSendEvent, payment_received_expected: bool, expected_preimage: Option<PaymentPreimage>) {
@@@ -1223,10 -1224,10 +1225,10 @@@ pub fn pass_along_route<'a, 'b, 'c>(ori
        }
  }
  
 -pub fn send_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, route: Route, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash, PaymentSecret) {
 +pub fn send_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, route: Route, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash, PaymentSecret, PaymentId) {
        let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(expected_route.last().unwrap());
 -      send_along_route_with_secret(origin_node, route, &[expected_route], recv_value, our_payment_hash, our_payment_secret);
 -      (our_payment_preimage, our_payment_hash, our_payment_secret)
 +      let payment_id = send_along_route_with_secret(origin_node, route, &[expected_route], recv_value, our_payment_hash, our_payment_secret);
 +      (our_payment_preimage, our_payment_hash, our_payment_secret, payment_id)
  }
  
  pub fn claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_paths: &[&[&Node<'a, 'b, 'c>]], skip_last: bool, our_payment_preimage: PaymentPreimage) {
@@@ -1328,11 -1329,13 +1330,13 @@@ pub fn claim_payment<'a, 'b, 'c>(origin
  pub const TEST_FINAL_CLTV: u32 = 70;
  
  pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash, PaymentSecret) {
+       let payee = Payee::new(expected_route.last().unwrap().node.get_our_node_id())
+               .with_features(InvoiceFeatures::known());
        let net_graph_msg_handler = &origin_node.net_graph_msg_handler;
        let scorer = Scorer::new(0);
-       let route = get_route(&origin_node.node.get_our_node_id(), &net_graph_msg_handler.network_graph,
-               &expected_route.last().unwrap().node.get_our_node_id(), Some(InvoiceFeatures::known()),
-               Some(&origin_node.node.list_usable_channels().iter().collect::<Vec<_>>()), &[],
+       let route = get_route(
+               &origin_node.node.get_our_node_id(), &payee, &net_graph_msg_handler.network_graph,
+               Some(&origin_node.node.list_usable_channels().iter().collect::<Vec<_>>()),
                recv_value, TEST_FINAL_CLTV, origin_node.logger, &scorer).unwrap();
        assert_eq!(route.paths.len(), 1);
        assert_eq!(route.paths[0].len(), expected_route.len());
                assert_eq!(hop.pubkey, node.node.get_our_node_id());
        }
  
 -      send_along_route(origin_node, route, expected_route, recv_value)
 +      let res = send_along_route(origin_node, route, expected_route, recv_value);
 +      (res.0, res.1, res.2)
  }
  
  pub fn route_over_limit<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64)  {
+       let payee = Payee::new(expected_route.last().unwrap().node.get_our_node_id())
+               .with_features(InvoiceFeatures::known());
        let net_graph_msg_handler = &origin_node.net_graph_msg_handler;
        let scorer = Scorer::new(0);
-       let route = get_route(&origin_node.node.get_our_node_id(), &net_graph_msg_handler.network_graph, &expected_route.last().unwrap().node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), recv_value, TEST_FINAL_CLTV, origin_node.logger, &scorer).unwrap();
+       let route = get_route(&origin_node.node.get_our_node_id(), &payee, &net_graph_msg_handler.network_graph, None, recv_value, TEST_FINAL_CLTV, origin_node.logger, &scorer).unwrap();
        assert_eq!(route.paths.len(), 1);
        assert_eq!(route.paths[0].len(), expected_route.len());
        for (node, hop) in expected_route.iter().zip(route.paths[0].iter()) {
index 4e2e15502093155927084c8554cd6fe39afbe337,6d509f8f07b62edf442810a1c9e216f238f6a034..61e2afef34c4af6ca67ad5fe3ec7a1be8027476b
@@@ -12,7 -12,7 +12,7 @@@
  //! claim outputs on-chain.
  
  use chain;
 -use chain::{Confirm, Listen, Watch, ChannelMonitorUpdateErr};
 +use chain::{Confirm, Listen, Watch};
  use chain::channelmonitor;
  use chain::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
  use chain::transaction::OutPoint;
@@@ -24,7 -24,7 +24,7 @@@ use ln::channel::{Channel, ChannelError
  use ln::{chan_utils, onion_utils};
  use ln::chan_utils::HTLC_SUCCESS_TX_WEIGHT;
  use routing::network_graph::{NetworkUpdate, RoutingFees};
- use routing::router::{Route, RouteHop, RouteHint, RouteHintHop, get_route, get_keysend_route};
+ use routing::router::{Payee, Route, RouteHop, RouteHint, RouteHintHop, get_route, get_keysend_route};
  use routing::scorer::Scorer;
  use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
  use ln::msgs;
@@@ -3689,7 -3689,7 +3689,7 @@@ fn test_funding_peer_disconnect() 
        nodes[0].net_graph_msg_handler.handle_channel_update(&as_update).unwrap();
  
        let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
 -      let (payment_preimage, _, _) = send_along_route(&nodes[0], route, &[&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
@@@ -4100,6 -4100,145 +4100,6 @@@ fn test_no_txn_manager_serialize_deseri
        send_payment(&nodes[0], &[&nodes[1]], 1000000);
  }
  
 -fn do_test_dup_htlc_onchain_fails_on_reload(persist_manager_post_event: bool) {
 -      // 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, we should avoid providing the
 -      // ChannelManager the HTLC event until after the monitor is re-persisted. This should prevent a
 -      // duplicate HTLC fail/claim (e.g. via a PaymentPathFailed event).
 -      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);
 -
 -      let (_, _, chan_id, funding_tx) = 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]);
 -      check_spends!(node_txn[1], funding_tx);
 -      check_spends!(node_txn[2], 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()]});
 -      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()]});
 -
 -      // Now connect the HTLC claim transaction with the ChainMonitor-generated ChannelMonitor update
 -      // returning TemporaryFailure. This should cause the claim event to never make its way to the
 -      // ChannelManager.
 -      chanmon_cfgs[0].persister.chain_sync_monitor_persistences.lock().unwrap().clear();
 -      chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
 -
 -      header.prev_blockhash = nodes[0].best_block_hash();
 -      let claim_block = Block { header, txdata: claim_txn };
 -      connect_block(&nodes[0], &claim_block);
 -
 -      let funding_txo = OutPoint { txid: funding_tx.txid(), index: 0 };
 -      let mon_updates: Vec<_> = chanmon_cfgs[0].persister.chain_sync_monitor_persistences.lock().unwrap()
 -              .get_mut(&funding_txo).unwrap().drain().collect();
 -      assert_eq!(mon_updates.len(), 1);
 -      assert!(nodes[0].chain_monitor.release_pending_monitor_events().is_empty());
 -      assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
 -
 -      // If we persist the ChannelManager here, we should get the PaymentSent event after
 -      // deserialization.
 -      let mut chan_manager_serialized = test_utils::TestVecWriter(Vec::new());
 -      if !persist_manager_post_event {
 -              nodes[0].node.write(&mut chan_manager_serialized).unwrap();
 -      }
 -
 -      // Now persist the ChannelMonitor and inform the ChainMonitor that we're done, generating the
 -      // payment sent event.
 -      chanmon_cfgs[0].persister.set_update_ret(Ok(()));
 -      let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
 -      get_monitor!(nodes[0], chan_id).write(&mut chan_0_monitor_serialized).unwrap();
 -      nodes[0].chain_monitor.chain_monitor.channel_monitor_updated(funding_txo, mon_updates[0]).unwrap();
 -      expect_payment_sent!(nodes[0], payment_preimage);
 -
 -      // If we persist the ChannelManager after we get the PaymentSent event, we shouldn't get it
 -      // twice.
 -      if persist_manager_post_event {
 -              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;
 -
 -      if persist_manager_post_event {
 -              assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
 -      } else {
 -              expect_payment_sent!(nodes[0], payment_preimage);
 -      }
 -
 -      // 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_dup_htlc_onchain_fails_on_reload() {
 -      do_test_dup_htlc_onchain_fails_on_reload(true);
 -      do_test_dup_htlc_onchain_fails_on_reload(false);
 -}
 -
  #[test]
  fn test_manager_serialize_deserialize_events() {
        // This test makes sure the events field in ChannelManager survives de/serialization
@@@ -5869,7 -6008,7 +5869,7 @@@ fn test_fail_holding_cell_htlc_upon_fre
        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_hash, ref rejected_by_dest, ref network_update, ref all_paths_failed, path: _, ref short_channel_id, retry: _, ref error_code, ref error_data } => {
                        assert_eq!(our_payment_hash.clone(), *payment_hash);
                        assert_eq!(*rejected_by_dest, false);
                        assert_eq!(*all_paths_failed, true);
@@@ -5953,7 -6092,7 +5953,7 @@@ fn test_free_and_fail_holding_cell_htlc
        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_hash, ref rejected_by_dest, ref network_update, ref all_paths_failed, path: _, ref short_channel_id, retry: _, ref error_code, ref error_data } => {
                        assert_eq!(payment_hash_2.clone(), *payment_hash);
                        assert_eq!(*rejected_by_dest, false);
                        assert_eq!(*all_paths_failed, true);
@@@ -7160,7 -7299,8 +7160,8 @@@ fn test_check_htlc_underpaying() 
        create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
  
        let scorer = Scorer::new(0);
-       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, &scorer).unwrap();
+       let payee = Payee::new(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].net_graph_msg_handler.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();
@@@ -7348,7 -7488,7 +7349,7 @@@ fn test_priv_forwarding_rejection() 
                htlc_minimum_msat: None,
                htlc_maximum_msat: None,
        }]);
-       let last_hops = vec![&route_hint];
+       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();
@@@ -7557,12 -7697,14 +7558,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 payee = Payee::new(nodes[1].node.get_our_node_id()).with_features(InvoiceFeatures::known());
        let scorer = Scorer::new(0);
-       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, &scorer).unwrap();
+       let route = get_route(&nodes[0].node.get_our_node_id(), &payee, &nodes[0].net_graph_msg_handler.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, &scorer).unwrap();
+       let payee = Payee::new(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].net_graph_msg_handler.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);
index c6f55e384d8432c6d5bfb0958662aa62b5d3b884,39c1aa378d1bf490ac9e18ffe32d2285d99cfbeb..419d5c158c63ef57245280f62e57579fc5d1e08b
@@@ -162,7 -162,7 +162,7 @@@ fn run_onion_failure_test_with_fail_int
  
        let events = nodes[0].node.get_and_clear_pending_events();
        assert_eq!(events.len(), 1);
-       if let &Event::PaymentPathFailed { payment_hash: _, ref rejected_by_dest, ref network_update, ref all_paths_failed, path: _, ref short_channel_id, ref error_code, error_data: _ } = &events[0] {
+       if let &Event::PaymentPathFailed { payment_hash: _, ref rejected_by_dest, ref network_update, ref all_paths_failed, path: _, ref short_channel_id, retry: _, ref error_code, error_data: _ } = &events[0] {
                assert_eq!(*rejected_by_dest, !expected_retryable);
                assert_eq!(*all_paths_failed, true);
                assert_eq!(*error_code, expected_error_code);
@@@ -479,7 -479,7 +479,7 @@@ fn test_onion_failure() 
  
        // Test a positive test-case with one extra msat, meeting the minimum.
        bogus_route.paths[0][route_len-1].fee_msat = amt_to_forward + 1;
 -      let (preimage, _, _) = send_along_route(&nodes[0], bogus_route, &[&nodes[1], &nodes[2]], amt_to_forward+1);
 +      let preimage = send_along_route(&nodes[0], bogus_route, &[&nodes[1], &nodes[2]], amt_to_forward+1).0;
        claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], preimage);
  
        //TODO: with new config API, we will be able to generate both valid and