Rename `Payee` to `PaymentParameters`
[rust-lightning] / lightning / src / ln / channelmanager.rs
index c51d17f67b9df16701321b687e23d533e191cd68..e234b1ed92d2b0723a0aa23ea4cb07937e374555 100644 (file)
@@ -45,7 +45,7 @@ use chain::transaction::{OutPoint, TransactionData};
 use ln::{PaymentHash, PaymentPreimage, PaymentSecret};
 use ln::channel::{Channel, ChannelError, ChannelUpdateStatus, UpdateFulfillCommitFetch};
 use ln::features::{InitFeatures, NodeFeatures};
-use routing::router::{Payee, Route, RouteHop, RoutePath, RouteParameters};
+use routing::router::{PaymentParameters, Route, RouteHop, RoutePath, RouteParameters};
 use ln::msgs;
 use ln::msgs::NetAddress;
 use ln::onion_utils;
@@ -491,7 +491,7 @@ pub(crate) enum HTLCSource {
                first_hop_htlc_msat: u64,
                payment_id: PaymentId,
                payment_secret: Option<PaymentSecret>,
-               payee: Option<Payee>,
+               payment_params: Option<PaymentParameters>,
        },
 }
 #[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
@@ -502,14 +502,14 @@ impl core::hash::Hash for HTLCSource {
                                0u8.hash(hasher);
                                prev_hop_data.hash(hasher);
                        },
-                       HTLCSource::OutboundRoute { path, session_priv, payment_id, payment_secret, first_hop_htlc_msat, payee } => {
+                       HTLCSource::OutboundRoute { path, session_priv, payment_id, payment_secret, first_hop_htlc_msat, payment_params } => {
                                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);
+                               payment_params.hash(hasher);
                        },
                }
        }
@@ -523,7 +523,7 @@ impl HTLCSource {
                        first_hop_htlc_msat: 0,
                        payment_id: PaymentId([2; 32]),
                        payment_secret: None,
-                       payee: None,
+                       payment_params: None,
                }
        }
 }
@@ -613,8 +613,14 @@ impl MsgHandleErrInternal {
                Self {
                        err: match err {
                                ChannelError::Warn(msg) =>  LightningError {
-                                       err: msg,
-                                       action: msgs::ErrorAction::IgnoreError,
+                                       err: msg.clone(),
+                                       action: msgs::ErrorAction::SendWarningMessage {
+                                               msg: msgs::WarningMessage {
+                                                       channel_id,
+                                                       data: msg
+                                               },
+                                               log_level: Level::Warn,
+                                       },
                                },
                                ChannelError::Ignore(msg) => LightningError {
                                        err: msg,
@@ -1373,9 +1379,7 @@ macro_rules! convert_chan_err {
        ($self: ident, $err: expr, $short_to_id: expr, $channel: expr, $channel_id: expr) => {
                match $err {
                        ChannelError::Warn(msg) => {
-                               //TODO: Once warning messages are merged, we should send a `warning` message to our
-                               //peer here.
-                               (false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $channel_id.clone()))
+                               (false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(msg), $channel_id.clone()))
                        },
                        ChannelError::Ignore(msg) => {
                                (false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $channel_id.clone()))
@@ -2424,7 +2428,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
        }
 
        // Only public for testing, this should otherwise never be called direcly
-       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> {
+       pub(crate) fn send_payment_along_path(&self, path: &Vec<RouteHop>, payment_params: &Option<PaymentParameters>, 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();
@@ -2489,7 +2493,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
                                                        first_hop_htlc_msat: htlc_msat,
                                                        payment_id,
                                                        payment_secret: payment_secret.clone(),
-                                                       payee: payee.clone(),
+                                                       payment_params: payment_params.clone(),
                                                }, onion_packet, &self.logger),
                                        channel_state, chan)
                                } {
@@ -2618,7 +2622,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
                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, &route.payee, &payment_hash, payment_secret, total_value, cur_height, payment_id, &keysend_preimage));
+                       results.push(self.send_payment_along_path(&path, &route.payment_params, &payment_hash, payment_secret, total_value, cur_height, payment_id, &keysend_preimage));
                }
                let mut has_ok = false;
                let mut has_err = false;
@@ -2642,9 +2646,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
                                results,
                                payment_id,
                                failed_paths_retry: if pending_amt_unsent != 0 {
-                                       if let Some(payee) = &route.payee {
+                                       if let Some(payment_params) = &route.payment_params {
                                                Some(RouteParameters {
-                                                       payee: payee.clone(),
+                                                       payment_params: payment_params.clone(),
                                                        final_value_msat: pending_amt_unsent,
                                                        final_cltv_expiry_delta: max_unsent_cltv_delta,
                                                })
@@ -3581,16 +3585,16 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
                                        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, payee, .. } => {
+                               HTLCSource::OutboundRoute { session_priv, payment_id, path, payment_params, .. } => {
                                        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, Some(&path)) && !payment.get().is_fulfilled() {
-                                                       let retry = if let Some(payee_data) = payee {
+                                                       let retry = if let Some(payment_params_data) = payment_params {
                                                                let path_last_hop = path.last().expect("Outbound payments must have had a valid path");
                                                                Some(RouteParameters {
-                                                                       payee: payee_data,
+                                                                       payment_params: payment_params_data,
                                                                        final_value_msat: path_last_hop.fee_msat,
                                                                        final_cltv_expiry_delta: path_last_hop.cltv_expiry_delta,
                                                                })
@@ -3642,7 +3646,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
                // 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, ref payee, .. } => {
+                       HTLCSource::OutboundRoute { ref path, session_priv, payment_id, ref payment_params, .. } => {
                                let mut session_priv_bytes = [0; 32];
                                session_priv_bytes.copy_from_slice(&session_priv[..]);
                                let mut outbounds = self.pending_outbound_payments.lock().unwrap();
@@ -3672,10 +3676,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
                                        return;
                                }
                                mem::drop(channel_state_lock);
-                               let retry = if let Some(payee_data) = payee {
+                               let retry = if let Some(payment_params_data) = payment_params {
                                        let path_last_hop = path.last().expect("Outbound payments must have had a valid path");
                                        Some(RouteParameters {
-                                               payee: payee_data.clone(),
+                                               payment_params: payment_params_data.clone(),
                                                final_value_msat: path_last_hop.fee_msat,
                                                final_cltv_expiry_delta: path_last_hop.cltv_expiry_delta,
                                        })
@@ -3927,12 +3931,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
        }
 
        fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
+               let mut outbounds = self.pending_outbound_payments.lock().unwrap();
                let mut pending_events = self.pending_events.lock().unwrap();
                for source in sources.drain(..) {
                        if let HTLCSource::OutboundRoute { session_priv, payment_id, path, .. } = 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());
                                        if payment.get_mut().remove(&session_priv_bytes, None) {
@@ -5317,8 +5321,8 @@ where
                        inbound_payment.expiry_time > header.time as u64
                });
 
-               let mut pending_events = self.pending_events.lock().unwrap();
                let mut outbounds = self.pending_outbound_payments.lock().unwrap();
+               let mut pending_events = self.pending_events.lock().unwrap();
                outbounds.retain(|payment_id, payment| {
                        if payment.remaining_parts() != 0 { return true }
                        if let PendingOutboundPayment::Retryable { starting_block_height, payment_hash, .. } = payment {
@@ -5981,14 +5985,14 @@ impl Readable for HTLCSource {
                                let mut path = Some(Vec::new());
                                let mut payment_id = None;
                                let mut payment_secret = None;
-                               let mut payee = None;
+                               let mut payment_params = 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),
+                                       (5, payment_params, option),
                                });
                                if payment_id.is_none() {
                                        // For backwards compat, if there was no payment_id written, use the session_priv bytes
@@ -6001,7 +6005,7 @@ impl Readable for HTLCSource {
                                        path: path.unwrap(),
                                        payment_id: payment_id.unwrap(),
                                        payment_secret,
-                                       payee,
+                                       payment_params,
                                })
                        }
                        1 => Ok(HTLCSource::PreviousHopData(Readable::read(reader)?)),
@@ -6013,7 +6017,7 @@ impl Readable for HTLCSource {
 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, payment_secret, payee } => {
+                       HTLCSource::OutboundRoute { ref session_priv, ref first_hop_htlc_msat, ref path, payment_id, payment_secret, payment_params } => {
                                0u8.write(writer)?;
                                let payment_id_opt = Some(payment_id);
                                write_tlv_fields!(writer, {
@@ -6022,7 +6026,7 @@ impl Writeable for HTLCSource {
                                        (2, first_hop_htlc_msat, required),
                                        (3, payment_secret, option),
                                        (4, path, vec_type),
-                                       (5, payee, option),
+                                       (5, payment_params, option),
                                 });
                        }
                        HTLCSource::PreviousHopData(ref field) => {
@@ -6147,6 +6151,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
                        peer_state.latest_features.write(writer)?;
                }
 
+               let pending_inbound_payments = self.pending_inbound_payments.lock().unwrap();
+               let pending_outbound_payments = self.pending_outbound_payments.lock().unwrap();
                let events = self.pending_events.lock().unwrap();
                (events.len() as u64).write(writer)?;
                for event in events.iter() {
@@ -6168,14 +6174,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
                (self.last_node_announcement_serial.load(Ordering::Acquire) as u32).write(writer)?;
                (self.highest_seen_timestamp.load(Ordering::Acquire) as u32).write(writer)?;
 
-               let pending_inbound_payments = self.pending_inbound_payments.lock().unwrap();
                (pending_inbound_payments.len() as u64).write(writer)?;
                for (hash, pending_payment) in pending_inbound_payments.iter() {
                        hash.write(writer)?;
                        pending_payment.write(writer)?;
                }
 
-               let pending_outbound_payments = self.pending_outbound_payments.lock().unwrap();
                // 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() {
@@ -6372,7 +6376,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
                                        log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
                                        log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
                                        log_error!(args.logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");
-                                       log_error!(args.logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/rust-bitcoin/rust-lightning");
+                                       log_error!(args.logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
                                        return Err(DecodeError::InvalidValue);
                                } else if channel.get_cur_holder_commitment_transaction_number() > monitor.get_cur_holder_commitment_number() ||
                                                channel.get_revoked_counterparty_commitment_transaction_number() > monitor.get_min_seen_secret() ||
@@ -6403,7 +6407,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
                                log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
                                log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
                                log_error!(args.logger, " Without the ChannelMonitor we cannot continue without risking funds.");
-                               log_error!(args.logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/rust-bitcoin/rust-lightning");
+                               log_error!(args.logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
                                return Err(DecodeError::InvalidValue);
                        }
                }
@@ -6635,7 +6639,7 @@ mod tests {
        use ln::functional_test_utils::*;
        use ln::msgs;
        use ln::msgs::ChannelMessageHandler;
-       use routing::router::{Payee, RouteParameters, find_route};
+       use routing::router::{PaymentParameters, RouteParameters, find_route};
        use util::errors::APIError;
        use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
        use util::test_utils;
@@ -6782,7 +6786,7 @@ mod tests {
                // 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], &route.payee, &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.payment_params, &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);
@@ -6812,7 +6816,7 @@ mod tests {
                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], &route.payee, &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.payment_params, &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);
@@ -6898,13 +6902,13 @@ mod tests {
                let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &expected_route, 100_000);
 
                // Next, attempt a keysend payment and make sure it fails.
-               let params = RouteParameters {
-                       payee: Payee::for_keysend(expected_route.last().unwrap().node.get_our_node_id()),
+               let route_params = RouteParameters {
+                       payment_params: PaymentParameters::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].node.get_our_node_id(), &route_params, nodes[0].network_graph, None,
                        nodes[0].logger, &scorer
                ).unwrap();
                nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage)).unwrap();
@@ -6935,7 +6939,7 @@ mod tests {
                // To start (2), send a keysend payment but don't claim it.
                let payment_preimage = PaymentPreimage([42; 32]);
                let route = find_route(
-                       &nodes[0].node.get_our_node_id(), &params, nodes[0].network_graph, None,
+                       &nodes[0].node.get_our_node_id(), &route_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();
@@ -6989,8 +6993,8 @@ mod tests {
                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 params = RouteParameters {
-                       payee: Payee::for_keysend(payee_pubkey),
+               let route_params = RouteParameters {
+                       payment_params: PaymentParameters::for_keysend(payee_pubkey),
                        final_value_msat: 10000,
                        final_cltv_expiry_delta: 40,
                };
@@ -6998,7 +7002,7 @@ mod tests {
                let first_hops = nodes[0].node.list_usable_channels();
                let scorer = test_utils::TestScorer::with_fixed_penalty(0);
                let route = find_route(
-                       &payer_pubkey, &params, network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
+                       &payer_pubkey, &route_params, network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
                        nodes[0].logger, &scorer
                ).unwrap();
 
@@ -7032,8 +7036,8 @@ mod tests {
                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 params = RouteParameters {
-                       payee: Payee::for_keysend(payee_pubkey),
+               let route_params = RouteParameters {
+                       payment_params: PaymentParameters::for_keysend(payee_pubkey),
                        final_value_msat: 10000,
                        final_cltv_expiry_delta: 40,
                };
@@ -7041,7 +7045,7 @@ mod tests {
                let first_hops = nodes[0].node.list_usable_channels();
                let scorer = test_utils::TestScorer::with_fixed_penalty(0);
                let route = find_route(
-                       &payer_pubkey, &params, network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
+                       &payer_pubkey, &route_params, network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
                        nodes[0].logger, &scorer
                ).unwrap();
 
@@ -7132,7 +7136,7 @@ pub mod bench {
        use ln::functional_test_utils::*;
        use ln::msgs::{ChannelMessageHandler, Init};
        use routing::network_graph::NetworkGraph;
-       use routing::router::{Payee, get_route};
+       use routing::router::{PaymentParameters, get_route};
        use routing::scoring::Scorer;
        use util::test_utils;
        use util::config::UserConfig;
@@ -7241,10 +7245,10 @@ pub mod bench {
                macro_rules! send_payment {
                        ($node_a: expr, $node_b: expr) => {
                                let usable_channels = $node_a.list_usable_channels();
-                               let payee = Payee::from_node_id($node_b.get_our_node_id())
+                               let payment_params = PaymentParameters::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,
+                               let route = get_route(&$node_a.get_our_node_id(), &payment_params, &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]);