Merge pull request #2973 from valentinewallace/2024-03-om-mailbox
[rust-lightning] / lightning / src / ln / channelmanager.rs
index 07fe697a8f6d2ac508ee2bab75973d1b2c6150e5..6ae78ec9ced4c45a7189237b2ca91640c97ddd68 100644 (file)
@@ -42,7 +42,8 @@ use crate::events;
 use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination, PaymentFailureReason};
 // Since this struct is returned in `list_channels` methods, expose it here in case users want to
 // construct one themselves.
-use crate::ln::{inbound_payment, ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
+use crate::ln::inbound_payment;
+use crate::ln::types::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
 use crate::ln::channel::{self, Channel, ChannelPhase, ChannelContext, ChannelError, ChannelUpdateStatus, ShutdownResult, UnfundedChannelContext, UpdateFulfillCommitFetch, OutboundV1Channel, InboundV1Channel, WithChannelContext};
 pub use crate::ln::channel::{InboundHTLCDetails, InboundHTLCStateDetails, OutboundHTLCDetails, OutboundHTLCStateDetails};
 use crate::ln::features::{Bolt12InvoiceFeatures, ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
@@ -64,7 +65,7 @@ use crate::offers::invoice_request::{DerivedPayerId, InvoiceRequestBuilder};
 use crate::offers::offer::{Offer, OfferBuilder};
 use crate::offers::parse::Bolt12SemanticError;
 use crate::offers::refund::{Refund, RefundBuilder};
-use crate::onion_message::messenger::{Destination, MessageRouter, PendingOnionMessage, new_pending_onion_message};
+use crate::onion_message::messenger::{new_pending_onion_message, Destination, MessageRouter, PendingOnionMessage, Responder, ResponseInstruction};
 use crate::onion_message::offers::{OffersMessage, OffersMessageHandler};
 use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
 use crate::sign::ecdsa::WriteableEcdsaChannelSigner;
@@ -75,6 +76,7 @@ use crate::util::string::UntrustedString;
 use crate::util::ser::{BigSize, FixedLengthReader, Readable, ReadableArgs, MaybeReadable, Writeable, Writer, VecWriter};
 use crate::util::logger::{Level, Logger, WithContext};
 use crate::util::errors::APIError;
+
 #[cfg(not(c_bindings))]
 use {
        crate::offers::offer::DerivedMetadata,
@@ -1387,7 +1389,7 @@ where
 ///
 /// ```
 /// # use bitcoin::secp256k1::PublicKey;
-/// # use lightning::ln::ChannelId;
+/// # use lightning::ln::types::ChannelId;
 /// # use lightning::ln::channelmanager::AChannelManager;
 /// # use lightning::events::{Event, EventsProvider};
 /// #
@@ -1490,7 +1492,7 @@ where
 ///
 /// ```
 /// # use lightning::events::{Event, EventsProvider};
-/// # use lightning::ln::PaymentHash;
+/// # use lightning::ln::types::PaymentHash;
 /// # use lightning::ln::channelmanager::{AChannelManager, PaymentId, RecentPaymentDetails, RecipientOnionFields, Retry};
 /// # use lightning::routing::router::RouteParameters;
 /// #
@@ -1549,11 +1551,12 @@ where
 /// # fn example<T: AChannelManager>(channel_manager: T) -> Result<(), Bolt12SemanticError> {
 /// # let channel_manager = channel_manager.get_cm();
 /// let offer = channel_manager
-///     .create_offer_builder("coffee".to_string())?
+///     .create_offer_builder()?
 /// # ;
 /// # // Needed for compiling for c_bindings
 /// # let builder: lightning::offers::offer::OfferBuilder<_, _> = offer.into();
 /// # let offer = builder
+///     .description("coffee".to_string())
 ///     .amount_msats(10_000_000)
 ///     .build()?;
 /// let bech32_offer = offer.to_string();
@@ -1652,13 +1655,13 @@ where
 /// let payment_id = PaymentId([42; 32]);
 /// let refund = channel_manager
 ///     .create_refund_builder(
-///         "coffee".to_string(), amount_msats, absolute_expiry, payment_id, retry,
-///         max_total_routing_fee_msat
+///         amount_msats, absolute_expiry, payment_id, retry, max_total_routing_fee_msat
 ///     )?
 /// # ;
 /// # // Needed for compiling for c_bindings
 /// # let builder: lightning::offers::refund::RefundBuilder<_> = refund.into();
 /// # let refund = builder
+///     .description("coffee".to_string())
 ///     .payer_note("refund for order 1234".to_string())
 ///     .build()?;
 /// let bech32_refund = refund.to_string();
@@ -7682,7 +7685,7 @@ where
                                                        }
                                                }
                                        }
-                                       try_chan_phase_entry!(self, chan.update_add_htlc(&msg, pending_forward_info), chan_phase_entry);
+                                       try_chan_phase_entry!(self, chan.update_add_htlc(&msg, pending_forward_info, &self.fee_estimator), chan_phase_entry);
                                } else {
                                        return try_chan_phase_entry!(self, Err(ChannelError::Close(
                                                "Got an update_add_htlc message for an unfunded channel!".into())), chan_phase_entry);
@@ -8562,9 +8565,7 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
        ///
        /// [`Offer`]: crate::offers::offer::Offer
        /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
-       pub fn create_offer_builder(
-               &$self, description: String
-       ) -> Result<$builder, Bolt12SemanticError> {
+       pub fn create_offer_builder(&$self) -> Result<$builder, Bolt12SemanticError> {
                let node_id = $self.get_our_node_id();
                let expanded_key = &$self.inbound_payment_key;
                let entropy = &*$self.entropy_source;
@@ -8572,7 +8573,7 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
 
                let path = $self.create_blinded_path().map_err(|_| Bolt12SemanticError::MissingPaths)?;
                let builder = OfferBuilder::deriving_signing_pubkey(
-                       description, node_id, expanded_key, entropy, secp_ctx
+                       node_id, expanded_key, entropy, secp_ctx
                )
                        .chain_hash($self.chain_hash)
                        .path(path);
@@ -8631,8 +8632,8 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
        /// [`Bolt12Invoice::payment_paths`]: crate::offers::invoice::Bolt12Invoice::payment_paths
        /// [Avoiding Duplicate Payments]: #avoiding-duplicate-payments
        pub fn create_refund_builder(
-               &$self, description: String, amount_msats: u64, absolute_expiry: Duration,
-               payment_id: PaymentId, retry_strategy: Retry, max_total_routing_fee_msat: Option<u64>
+               &$self, amount_msats: u64, absolute_expiry: Duration, payment_id: PaymentId,
+               retry_strategy: Retry, max_total_routing_fee_msat: Option<u64>
        ) -> Result<$builder, Bolt12SemanticError> {
                let node_id = $self.get_our_node_id();
                let expanded_key = &$self.inbound_payment_key;
@@ -8641,7 +8642,7 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
 
                let path = $self.create_blinded_path().map_err(|_| Bolt12SemanticError::MissingPaths)?;
                let builder = RefundBuilder::deriving_payer_id(
-                       description, node_id, expanded_key, entropy, secp_ctx, amount_msats, payment_id
+                       node_id, expanded_key, entropy, secp_ctx, amount_msats, payment_id
                )?
                        .chain_hash($self.chain_hash)
                        .absolute_expiry(absolute_expiry)
@@ -10349,23 +10350,27 @@ where
        R::Target: Router,
        L::Target: Logger,
 {
-       fn handle_message(&self, message: OffersMessage) -> Option<OffersMessage> {
+       fn handle_message(&self, message: OffersMessage, responder: Option<Responder>) -> ResponseInstruction<OffersMessage> {
                let secp_ctx = &self.secp_ctx;
                let expanded_key = &self.inbound_payment_key;
 
                match message {
                        OffersMessage::InvoiceRequest(invoice_request) => {
+                               let responder = match responder {
+                                       Some(responder) => responder,
+                                       None => return ResponseInstruction::NoResponse,
+                               };
                                let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats(
                                        &invoice_request
                                ) {
                                        Ok(amount_msats) => amount_msats,
-                                       Err(error) => return Some(OffersMessage::InvoiceError(error.into())),
+                                       Err(error) => return responder.respond(OffersMessage::InvoiceError(error.into())),
                                };
                                let invoice_request = match invoice_request.verify(expanded_key, secp_ctx) {
                                        Ok(invoice_request) => invoice_request,
                                        Err(()) => {
                                                let error = Bolt12SemanticError::InvalidMetadata;
-                                               return Some(OffersMessage::InvoiceError(error.into()));
+                                               return responder.respond(OffersMessage::InvoiceError(error.into()));
                                        },
                                };
 
@@ -10376,7 +10381,7 @@ where
                                        Ok((payment_hash, payment_secret)) => (payment_hash, payment_secret),
                                        Err(()) => {
                                                let error = Bolt12SemanticError::InvalidAmount;
-                                               return Some(OffersMessage::InvoiceError(error.into()));
+                                               return responder.respond(OffersMessage::InvoiceError(error.into()));
                                        },
                                };
 
@@ -10390,7 +10395,7 @@ where
                                        Ok(payment_paths) => payment_paths,
                                        Err(()) => {
                                                let error = Bolt12SemanticError::MissingPaths;
-                                               return Some(OffersMessage::InvoiceError(error.into()));
+                                               return responder.respond(OffersMessage::InvoiceError(error.into()));
                                        },
                                };
 
@@ -10435,8 +10440,8 @@ where
                                };
 
                                match response {
-                                       Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
-                                       Err(error) => Some(OffersMessage::InvoiceError(error.into())),
+                                       Ok(invoice) => return responder.respond(OffersMessage::Invoice(invoice)),
+                                       Err(error) => return responder.respond(OffersMessage::InvoiceError(error.into())),
                                }
                        },
                        OffersMessage::Invoice(invoice) => {
@@ -10456,14 +10461,21 @@ where
                                                }
                                        });
 
-                               match response {
-                                       Ok(()) => None,
-                                       Err(e) => Some(OffersMessage::InvoiceError(e)),
+                               match (responder, response) {
+                                       (Some(responder), Err(e)) => responder.respond(OffersMessage::InvoiceError(e)),
+                                       (None, Err(_)) => {
+                                               log_trace!(
+                                                       self.logger,
+                                                       "A response was generated, but there is no reply_path specified for sending the response."
+                                               );
+                                               return ResponseInstruction::NoResponse;
+                                       }
+                                       _ => return ResponseInstruction::NoResponse,
                                }
                        },
                        OffersMessage::InvoiceError(invoice_error) => {
                                log_trace!(self.logger, "Received invoice_error: {}", invoice_error);
-                               None
+                               return ResponseInstruction::NoResponse;
                        },
                }
        }
@@ -12375,8 +12387,7 @@ mod tests {
        use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
        use core::sync::atomic::Ordering;
        use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
-       use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
-       use crate::ln::ChannelId;
+       use crate::ln::types::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret};
        use crate::ln::channelmanager::{create_recv_pending_htlc_info, HTLCForwardInfo, inbound_payment, PaymentId, PaymentSendFailure, RecipientOnionFields, InterceptId};
        use crate::ln::functional_test_utils::*;
        use crate::ln::msgs::{self, ErrorAction};