Merge pull request #1996 from valentinewallace/2023-01-migrate-payment-scoring
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Fri, 3 Feb 2023 18:44:13 +0000 (18:44 +0000)
committerGitHub <noreply@github.com>
Fri, 3 Feb 2023 18:44:13 +0000 (18:44 +0000)
Score payment paths from events in `BackgroundProcessor`

1  2 
lightning/src/ln/channelmanager.rs

index f2ee0860e67b85bc91223cd3f7be4b4671b45169,4ea0a5e3e7503ab3d8a56a5c6d5b6a5b4eeed600..31295d550a38d6eb531eab9b697ecf50a6643cee
@@@ -53,7 -53,7 +53,7 @@@ use crate::ln::onion_utils::HTLCFailRea
  use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError, MAX_VALUE_MSAT};
  #[cfg(test)]
  use crate::ln::outbound_payment;
- use crate::ln::outbound_payment::{OutboundPayments, PaymentAttempts, PendingOutboundPayment, Retry};
+ use crate::ln::outbound_payment::{OutboundPayments, PaymentAttempts, PendingOutboundPayment};
  use crate::ln::wire::Encode;
  use crate::chain::keysinterface::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider, ChannelSigner};
  use crate::util::config::{UserConfig, ChannelConfig};
@@@ -76,7 -76,7 +76,7 @@@ use core::time::Duration
  use core::ops::Deref;
  
  // Re-export this for use in the public API.
- pub use crate::ln::outbound_payment::PaymentSendFailure;
+ pub use crate::ln::outbound_payment::{PaymentSendFailure, Retry};
  
  // We hold various information about HTLC relay in the HTLC objects in Channel itself:
  //
@@@ -245,10 -245,6 +245,10 @@@ pub(crate) enum HTLCSource 
                first_hop_htlc_msat: u64,
                payment_id: PaymentId,
                payment_secret: Option<PaymentSecret>,
 +              /// Note that this is now "deprecated" - we write it for forwards (and read it for
 +              /// backwards) compatibility reasons, but prefer to use the data in the
 +              /// [`super::outbound_payment`] module, which stores per-payment data once instead of in
 +              /// each HTLC.
                payment_params: Option<PaymentParameters>,
        },
  }
@@@ -2477,7 -2473,7 +2477,7 @@@ wher
                self.pending_outbound_payments
                        .send_payment(payment_hash, payment_secret, payment_id, retry_strategy, route_params,
                                &self.router, self.list_usable_channels(), self.compute_inflight_htlcs(),
 -                              &self.entropy_source, &self.node_signer, best_block_height,
 +                              &self.entropy_source, &self.node_signer, best_block_height, &self.logger,
                                |path, payment_params, payment_hash, payment_secret, total_value, cur_height, payment_id, keysend_preimage, session_priv|
                                self.send_payment_along_path(path, payment_params, payment_hash, payment_secret, total_value, cur_height, payment_id, keysend_preimage, session_priv))
        }
        #[cfg(test)]
        pub(crate) fn test_add_new_pending_payment(&self, payment_hash: PaymentHash, payment_secret: Option<PaymentSecret>, payment_id: PaymentId, route: &Route) -> Result<Vec<[u8; 32]>, PaymentSendFailure> {
                let best_block_height = self.best_block.read().unwrap().height();
 -              self.pending_outbound_payments.test_add_new_pending_payment(payment_hash, payment_secret, payment_id, route, Retry::Attempts(0), &self.entropy_source, best_block_height)
 +              self.pending_outbound_payments.test_add_new_pending_payment(payment_hash, payment_secret, payment_id, route, None, &self.entropy_source, best_block_height)
        }
  
  
                events.into_inner()
        }
  
+       #[cfg(feature = "_test_utils")]
+       pub fn push_pending_event(&self, event: events::Event) {
+               let mut events = self.pending_events.lock().unwrap();
+               events.push(event);
+       }
        #[cfg(test)]
        pub fn pop_pending_event(&self) -> Option<events::Event> {
                let mut events = self.pending_events.lock().unwrap();
@@@ -5790,8 -5792,8 +5796,8 @@@ wher
                        let mut peer_state_lock = peer_state_mutex.lock().unwrap();
                        let peer_state = &mut *peer_state_lock;
                        for chan in peer_state.channel_by_id.values() {
 -                              if let (Some(funding_txo), block_hash) = (chan.get_funding_txo(), chan.get_funding_tx_confirmed_in()) {
 -                                      res.push((funding_txo.txid, block_hash));
 +                              if let (Some(funding_txo), Some(block_hash)) = (chan.get_funding_txo(), chan.get_funding_tx_confirmed_in()) {
 +                                      res.push((funding_txo.txid, Some(block_hash)));
                                }
                        }
                }
@@@ -7361,9 -7363,9 +7367,9 @@@ wher
                                                                hash_map::Entry::Vacant(entry) => {
                                                                        let path_fee = path.get_path_fees();
                                                                        entry.insert(PendingOutboundPayment::Retryable {
 -                                                                              retry_strategy: Retry::Attempts(0),
 +                                                                              retry_strategy: None,
                                                                                attempts: PaymentAttempts::new(),
 -                                                                              route_params: None,
 +                                                                              payment_params: None,
                                                                                session_privs: [session_priv_bytes].iter().map(|a| *a).collect(),
                                                                                payment_hash: htlc.payment_hash,
                                                                                payment_secret,
@@@ -7875,7 -7877,7 +7881,7 @@@ mod tests 
  
                // Next, attempt a keysend payment and make sure it fails.
                let route_params = RouteParameters {
 -                      payment_params: PaymentParameters::for_keysend(expected_route.last().unwrap().node.get_our_node_id()),
 +                      payment_params: PaymentParameters::for_keysend(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV),
                        final_value_msat: 100_000,
                        final_cltv_expiry_delta: TEST_FINAL_CLTV,
                };
  
                let _chan = create_chan_between_nodes(&nodes[0], &nodes[1]);
                let route_params = RouteParameters {
 -                      payment_params: PaymentParameters::for_keysend(payee_pubkey),
 +                      payment_params: PaymentParameters::for_keysend(payee_pubkey, 40),
                        final_value_msat: 10_000,
                        final_cltv_expiry_delta: 40,
                };
  
                let _chan = create_chan_between_nodes(&nodes[0], &nodes[1]);
                let route_params = RouteParameters {
 -                      payment_params: PaymentParameters::for_keysend(payee_pubkey),
 +                      payment_params: PaymentParameters::for_keysend(payee_pubkey, 40),
                        final_value_msat: 10_000,
                        final_cltv_expiry_delta: 40,
                };
@@@ -8578,7 -8580,7 +8584,7 @@@ pub mod bench 
                macro_rules! send_payment {
                        ($node_a: expr, $node_b: expr) => {
                                let usable_channels = $node_a.list_usable_channels();
 -                              let payment_params = PaymentParameters::from_node_id($node_b.get_our_node_id())
 +                              let payment_params = PaymentParameters::from_node_id($node_b.get_our_node_id(), TEST_FINAL_CLTV)
                                        .with_features($node_b.invoice_features());
                                let scorer = test_utils::TestScorer::with_penalty(0);
                                let seed = [3u8; 32];