Skip to content

Commit fde5b13

Browse files
committed
Remove OffersMessageHandler Implementation for ChannelManager
With `OffersMessageFlow` now serving as the source of `OffersMessageHandler` implementation, the implementation in `ChannelManager` is no longer needed and has been safely removed.
1 parent ac95bc0 commit fde5b13

File tree

1 file changed

+6
-274
lines changed

1 file changed

+6
-274
lines changed

lightning/src/ln/channelmanager.rs

+6-274
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::events::FundingInfo;
3636
use crate::blinded_path::message::{AsyncPaymentsContext, MessageContext, OffersContext};
3737
use crate::blinded_path::NodeIdLookUp;
3838
use crate::blinded_path::message::{BlindedMessagePath, MessageForwardNode};
39-
use crate::blinded_path::payment::{BlindedPaymentPath, Bolt12OfferContext, Bolt12RefundContext, PaymentConstraints, PaymentContext, ReceiveTlvs};
39+
use crate::blinded_path::payment::{BlindedPaymentPath, Bolt12RefundContext, PaymentConstraints, PaymentContext, ReceiveTlvs};
4040
use crate::chain;
4141
use crate::chain::{Confirm, ChannelMonitorUpdateStatus, Watch, BestBlock};
4242
use crate::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator, LowerBoundedFeeEstimator};
@@ -64,8 +64,7 @@ use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError};
6464
use crate::ln::outbound_payment;
6565
use crate::ln::outbound_payment::{OutboundPayments, PendingOutboundPayment, RetryableInvoiceRequest, SendAlongPathArgs, StaleExpiration};
6666
use crate::ln::wire::Encode;
67-
use crate::offers::invoice::{Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder, UnsignedBolt12Invoice};
68-
use crate::offers::invoice_error::InvoiceError;
67+
use crate::offers::invoice::{Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, InvoiceBuilder};
6968
use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestBuilder};
7069
use crate::offers::nonce::Nonce;
7170
use crate::offers::offer::{Offer, OfferBuilder};
@@ -77,7 +76,7 @@ use crate::offers::static_invoice::StaticInvoice;
7776
use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler};
7877
use crate::onion_message::dns_resolution::HumanReadableName;
7978
use crate::onion_message::messenger::{Destination, MessageRouter, Responder, ResponseInstruction, MessageSendInstructions};
80-
use crate::onion_message::offers::{OffersMessage, OffersMessageHandler};
79+
use crate::onion_message::offers::OffersMessage;
8180
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
8281
use crate::sign::ecdsa::EcdsaChannelSigner;
8382
use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
@@ -1576,7 +1575,6 @@ where
15761575
/// Additionally, it implements the following traits:
15771576
/// - [`ChannelMessageHandler`] to handle off-chain channel activity from peers
15781577
/// - [`MessageSendEventsProvider`] to similarly send such messages to peers
1579-
/// - [`OffersMessageHandler`] for BOLT 12 message handling and sending
15801578
/// - [`EventsProvider`] to generate user-actionable [`Event`]s
15811579
/// - [`chain::Listen`] and [`chain::Confirm`] for notification of on-chain activity
15821580
///
@@ -2000,10 +1998,9 @@ where
20001998
///
20011999
/// The [`offers`] module is useful for creating BOLT 12 offers. An [`Offer`] is a precursor to a
20022000
/// [`Bolt12Invoice`], which must first be requested by the payer. The interchange of these messages
2003-
/// as defined in the specification is handled by [`ChannelManager`] and its implementation of
2004-
/// [`OffersMessageHandler`]. However, this only works with an [`Offer`] created using a builder
2005-
/// returned by [`create_offer_builder`]. With this approach, BOLT 12 offers and invoices are
2006-
/// stateless just as BOLT 11 invoices are.
2001+
/// as defined in the specification is handled by [`ChannelManager`]. However, this only works with
2002+
/// an [`Offer`] created using a builder returned by [`create_offer_builder`]. With this approach,
2003+
/// BOLT 12 offers and invoices are stateless just as BOLT 11 invoices are.
20072004
///
20082005
/// ```
20092006
/// # use lightning::events::{Event, EventsProvider, PaymentPurpose};
@@ -11565,271 +11562,6 @@ where
1156511562
}
1156611563
}
1156711564

11568-
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>
11569-
OffersMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
11570-
where
11571-
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
11572-
T::Target: BroadcasterInterface,
11573-
ES::Target: EntropySource,
11574-
NS::Target: NodeSigner,
11575-
SP::Target: SignerProvider,
11576-
F::Target: FeeEstimator,
11577-
R::Target: Router,
11578-
MR::Target: MessageRouter,
11579-
L::Target: Logger,
11580-
{
11581-
fn handle_message(
11582-
&self, message: OffersMessage, context: Option<OffersContext>, responder: Option<Responder>,
11583-
) -> Option<(OffersMessage, ResponseInstruction)> {
11584-
let secp_ctx = &self.secp_ctx;
11585-
let expanded_key = &self.inbound_payment_key;
11586-
11587-
macro_rules! handle_pay_invoice_res {
11588-
($res: expr, $invoice: expr, $logger: expr) => {{
11589-
let error = match $res {
11590-
Err(Bolt12PaymentError::UnknownRequiredFeatures) => {
11591-
log_trace!(
11592-
$logger, "Invoice requires unknown features: {:?}",
11593-
$invoice.invoice_features()
11594-
);
11595-
InvoiceError::from(Bolt12SemanticError::UnknownRequiredFeatures)
11596-
},
11597-
Err(Bolt12PaymentError::SendingFailed(e)) => {
11598-
log_trace!($logger, "Failed paying invoice: {:?}", e);
11599-
InvoiceError::from_string(format!("{:?}", e))
11600-
},
11601-
#[cfg(async_payments)]
11602-
Err(Bolt12PaymentError::BlindedPathCreationFailed) => {
11603-
let err_msg = "Failed to create a blinded path back to ourselves";
11604-
log_trace!($logger, "{}", err_msg);
11605-
InvoiceError::from_string(err_msg.to_string())
11606-
},
11607-
Err(Bolt12PaymentError::UnexpectedInvoice)
11608-
| Err(Bolt12PaymentError::DuplicateInvoice)
11609-
| Ok(()) => return None,
11610-
};
11611-
11612-
match responder {
11613-
Some(responder) => return Some((OffersMessage::InvoiceError(error), responder.respond())),
11614-
None => {
11615-
log_trace!($logger, "No reply path to send error: {:?}", error);
11616-
return None
11617-
},
11618-
}
11619-
}}
11620-
}
11621-
11622-
match message {
11623-
OffersMessage::InvoiceRequest(invoice_request) => {
11624-
let responder = match responder {
11625-
Some(responder) => responder,
11626-
None => return None,
11627-
};
11628-
11629-
let nonce = match context {
11630-
None if invoice_request.metadata().is_some() => None,
11631-
Some(OffersContext::InvoiceRequest { nonce }) => Some(nonce),
11632-
_ => return None,
11633-
};
11634-
11635-
let invoice_request = match nonce {
11636-
Some(nonce) => match invoice_request.verify_using_recipient_data(
11637-
nonce, expanded_key, secp_ctx,
11638-
) {
11639-
Ok(invoice_request) => invoice_request,
11640-
Err(()) => return None,
11641-
},
11642-
None => match invoice_request.verify_using_metadata(expanded_key, secp_ctx) {
11643-
Ok(invoice_request) => invoice_request,
11644-
Err(()) => return None,
11645-
},
11646-
};
11647-
11648-
let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats(
11649-
&invoice_request.inner
11650-
) {
11651-
Ok(amount_msats) => amount_msats,
11652-
Err(error) => return Some((OffersMessage::InvoiceError(error.into()), responder.respond())),
11653-
};
11654-
11655-
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
11656-
let (payment_hash, payment_secret) = match self.create_inbound_payment(
11657-
Some(amount_msats), relative_expiry, None
11658-
) {
11659-
Ok((payment_hash, payment_secret)) => (payment_hash, payment_secret),
11660-
Err(()) => {
11661-
let error = Bolt12SemanticError::InvalidAmount;
11662-
return Some((OffersMessage::InvoiceError(error.into()), responder.respond()));
11663-
},
11664-
};
11665-
11666-
let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
11667-
offer_id: invoice_request.offer_id,
11668-
invoice_request: invoice_request.fields(),
11669-
});
11670-
let payment_paths = match self.create_blinded_payment_paths(
11671-
amount_msats, payment_secret, payment_context
11672-
) {
11673-
Ok(payment_paths) => payment_paths,
11674-
Err(()) => {
11675-
let error = Bolt12SemanticError::MissingPaths;
11676-
return Some((OffersMessage::InvoiceError(error.into()), responder.respond()));
11677-
},
11678-
};
11679-
11680-
#[cfg(not(feature = "std"))]
11681-
let created_at = Duration::from_secs(
11682-
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
11683-
);
11684-
11685-
let response = if invoice_request.keys.is_some() {
11686-
#[cfg(feature = "std")]
11687-
let builder = invoice_request.respond_using_derived_keys(
11688-
payment_paths, payment_hash
11689-
);
11690-
#[cfg(not(feature = "std"))]
11691-
let builder = invoice_request.respond_using_derived_keys_no_std(
11692-
payment_paths, payment_hash, created_at
11693-
);
11694-
builder
11695-
.map(InvoiceBuilder::<DerivedSigningPubkey>::from)
11696-
.and_then(|builder| builder.allow_mpp().build_and_sign(secp_ctx))
11697-
.map_err(InvoiceError::from)
11698-
} else {
11699-
#[cfg(feature = "std")]
11700-
let builder = invoice_request.respond_with(payment_paths, payment_hash);
11701-
#[cfg(not(feature = "std"))]
11702-
let builder = invoice_request.respond_with_no_std(
11703-
payment_paths, payment_hash, created_at
11704-
);
11705-
builder
11706-
.map(InvoiceBuilder::<ExplicitSigningPubkey>::from)
11707-
.and_then(|builder| builder.allow_mpp().build())
11708-
.map_err(InvoiceError::from)
11709-
.and_then(|invoice| {
11710-
#[cfg(c_bindings)]
11711-
let mut invoice = invoice;
11712-
invoice
11713-
.sign(|invoice: &UnsignedBolt12Invoice|
11714-
self.node_signer.sign_bolt12_invoice(invoice)
11715-
)
11716-
.map_err(InvoiceError::from)
11717-
})
11718-
};
11719-
11720-
match response {
11721-
Ok(invoice) => {
11722-
let nonce = Nonce::from_entropy_source(&*self.entropy_source);
11723-
let hmac = payment_hash.hmac_for_offer_payment(nonce, expanded_key);
11724-
let context = MessageContext::Offers(OffersContext::InboundPayment { payment_hash, nonce, hmac });
11725-
Some((OffersMessage::Invoice(invoice), responder.respond_with_reply_path(context)))
11726-
},
11727-
Err(error) => Some((OffersMessage::InvoiceError(error.into()), responder.respond())),
11728-
}
11729-
},
11730-
OffersMessage::Invoice(invoice) => {
11731-
let payment_id = match self.verify_bolt12_invoice(&invoice, context.as_ref()) {
11732-
Ok(payment_id) => payment_id,
11733-
Err(()) => return None,
11734-
};
11735-
11736-
let logger = WithContext::from(
11737-
&self.logger, None, None, Some(invoice.payment_hash()),
11738-
);
11739-
11740-
if self.default_configuration.manually_handle_bolt12_invoices {
11741-
let event = Event::InvoiceReceived {
11742-
payment_id, invoice, context, responder,
11743-
};
11744-
self.pending_events.lock().unwrap().push_back((event, None));
11745-
return None;
11746-
}
11747-
11748-
let res = self.send_payment_for_verified_bolt12_invoice(&invoice, payment_id);
11749-
handle_pay_invoice_res!(res, invoice, logger);
11750-
},
11751-
#[cfg(async_payments)]
11752-
OffersMessage::StaticInvoice(invoice) => {
11753-
let payment_id = match context {
11754-
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
11755-
if payment_id.verify_for_offer_payment(hmac, nonce, expanded_key).is_err() {
11756-
return None
11757-
}
11758-
payment_id
11759-
},
11760-
_ => return None
11761-
};
11762-
let res = self.initiate_async_payment(&invoice, payment_id);
11763-
handle_pay_invoice_res!(res, invoice, self.logger);
11764-
},
11765-
OffersMessage::InvoiceError(invoice_error) => {
11766-
let payment_hash = match context {
11767-
Some(OffersContext::InboundPayment { payment_hash, nonce, hmac }) => {
11768-
match payment_hash.verify_for_offer_payment(hmac, nonce, expanded_key) {
11769-
Ok(_) => Some(payment_hash),
11770-
Err(_) => None,
11771-
}
11772-
},
11773-
_ => None,
11774-
};
11775-
11776-
let logger = WithContext::from(&self.logger, None, None, payment_hash);
11777-
log_trace!(logger, "Received invoice_error: {}", invoice_error);
11778-
11779-
match context {
11780-
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
11781-
if let Ok(()) = payment_id.verify_for_offer_payment(hmac, nonce, expanded_key) {
11782-
self.abandon_payment_with_reason(
11783-
payment_id, PaymentFailureReason::InvoiceRequestRejected,
11784-
);
11785-
}
11786-
},
11787-
_ => {},
11788-
}
11789-
11790-
None
11791-
},
11792-
}
11793-
}
11794-
11795-
fn message_received(&self) {
11796-
for (payment_id, retryable_invoice_request) in self
11797-
.pending_outbound_payments
11798-
.release_invoice_requests_awaiting_invoice()
11799-
{
11800-
let RetryableInvoiceRequest { invoice_request, nonce } = retryable_invoice_request;
11801-
let hmac = payment_id.hmac_for_offer_payment(nonce, &self.inbound_payment_key);
11802-
let context = MessageContext::Offers(OffersContext::OutboundPayment {
11803-
payment_id,
11804-
nonce,
11805-
hmac: Some(hmac)
11806-
});
11807-
match self.create_blinded_paths(context) {
11808-
Ok(reply_paths) => match self.enqueue_invoice_request(invoice_request, reply_paths) {
11809-
Ok(_) => {}
11810-
Err(_) => {
11811-
log_warn!(self.logger,
11812-
"Retry failed for an invoice request with payment_id: {}",
11813-
payment_id
11814-
);
11815-
}
11816-
},
11817-
Err(_) => {
11818-
log_warn!(self.logger,
11819-
"Retry failed for an invoice request with payment_id: {}. \
11820-
Reason: router could not find a blinded path to include as the reply path",
11821-
payment_id
11822-
);
11823-
}
11824-
}
11825-
}
11826-
}
11827-
11828-
fn release_pending_messages(&self) -> Vec<(OffersMessage, MessageSendInstructions)> {
11829-
core::mem::take(&mut self.pending_offers_messages.lock().unwrap())
11830-
}
11831-
}
11832-
1183311565
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>
1183411566
AsyncPaymentsMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
1183511567
where

0 commit comments

Comments
 (0)