Skip to content

Commit 9b18b7e

Browse files
committed
Replace use of OffersContext::Unknown with None
Now that ChannelManager uses a known OffersContext when creating blinded paths, OffersContext::Unknown is no longer needed. Remove it and update OffersMessageHandler to us an Option, which is more idiomatic for signifying whether a message was delivered with or without an OffersContext.
1 parent 34b2be7 commit 9b18b7e

File tree

9 files changed

+36
-35
lines changed

9 files changed

+36
-35
lines changed

fuzz/src/onion_message.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ struct TestOffersMessageHandler {}
106106

107107
impl OffersMessageHandler for TestOffersMessageHandler {
108108
fn handle_message(
109-
&self, _message: OffersMessage, _context: OffersContext, _responder: Option<Responder>,
109+
&self, _message: OffersMessage, _context: Option<OffersContext>,
110+
_responder: Option<Responder>,
110111
) -> ResponseInstruction<OffersMessage> {
111112
ResponseInstruction::NoResponse
112113
}

lightning/src/blinded_path/message.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ pub enum MessageContext {
108108
/// [`OffersMessage`]: crate::onion_message::offers::OffersMessage
109109
#[derive(Clone, Debug, Eq, PartialEq)]
110110
pub enum OffersContext {
111-
/// Represents an unknown BOLT12 message context.
112-
///
113-
/// This variant is used when a message is sent without using a [`BlindedPath`] or over one
114-
/// created prior to LDK version 0.0.124.
115-
Unknown {},
116111
/// Context used by a [`BlindedPath`] within an [`Offer`].
117112
///
118113
/// This variant is intended to be received when handling an [`InvoiceRequest`].
@@ -172,15 +167,14 @@ impl_writeable_tlv_based_enum!(MessageContext,
172167
);
173168

174169
impl_writeable_tlv_based_enum!(OffersContext,
175-
(0, Unknown) => {},
176-
(1, InvoiceRequest) => {
170+
(0, InvoiceRequest) => {
177171
(0, nonce, required),
178172
},
179-
(2, OutboundPayment) => {
173+
(1, OutboundPayment) => {
180174
(0, payment_id, required),
181175
(1, nonce, required),
182176
},
183-
(3, InboundPayment) => {
177+
(2, InboundPayment) => {
184178
(0, payment_hash, required),
185179
},
186180
);

lightning/src/events/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ pub enum Event {
810810
/// The context of the [`BlindedPath`] used to send the invoice.
811811
///
812812
/// [`BlindedPath`]: crate::blinded_path::BlindedPath
813-
context: OffersContext,
813+
context: Option<OffersContext>,
814814
/// A responder for replying with an [`InvoiceError`] if needed.
815815
///
816816
/// `None` if the invoice wasn't sent with a reply path.
@@ -1658,7 +1658,7 @@ impl Writeable for Event {
16581658
write_tlv_fields!(writer, {
16591659
(0, payment_id, required),
16601660
(2, invoice, required),
1661-
(4, context, required),
1661+
(4, context, option),
16621662
(6, responder, option),
16631663
});
16641664
},
@@ -2113,13 +2113,13 @@ impl MaybeReadable for Event {
21132113
_init_and_read_len_prefixed_tlv_fields!(reader, {
21142114
(0, payment_id, required),
21152115
(2, invoice, required),
2116-
(4, context, required),
2116+
(4, context, option),
21172117
(6, responder, option),
21182118
});
21192119
Ok(Some(Event::InvoiceReceived {
21202120
payment_id: payment_id.0.unwrap(),
21212121
invoice: invoice.0.unwrap(),
2122-
context: context.0.unwrap(),
2122+
context,
21232123
responder,
21242124
}))
21252125
};

lightning/src/ln/channelmanager.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -4204,7 +4204,7 @@ where
42044204
///
42054205
/// [timer tick]: Self::timer_tick_occurred
42064206
pub fn send_payment_for_bolt12_invoice(
4207-
&self, invoice: &Bolt12Invoice, context: &OffersContext,
4207+
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
42084208
) -> Result<(), Bolt12PaymentError> {
42094209
match self.verify_bolt12_invoice(invoice, context) {
42104210
Ok(payment_id) => self.send_payment_for_verified_bolt12_invoice(invoice, payment_id),
@@ -4213,17 +4213,17 @@ where
42134213
}
42144214

42154215
fn verify_bolt12_invoice(
4216-
&self, invoice: &Bolt12Invoice, context: &OffersContext,
4216+
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
42174217
) -> Result<PaymentId, ()> {
42184218
let secp_ctx = &self.secp_ctx;
42194219
let expanded_key = &self.inbound_payment_key;
42204220

42214221
match context {
4222-
OffersContext::Unknown {} if invoice.is_for_refund_without_paths() => {
4222+
None if invoice.is_for_refund_without_paths() => {
42234223
invoice.verify_using_metadata(expanded_key, secp_ctx)
42244224
},
4225-
OffersContext::OutboundPayment { payment_id, nonce } => {
4226-
invoice.verify_using_payer_data(*payment_id, *nonce, expanded_key, secp_ctx)
4225+
Some(&OffersContext::OutboundPayment { payment_id, nonce }) => {
4226+
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
42274227
},
42284228
_ => Err(()),
42294229
}
@@ -10713,13 +10713,17 @@ where
1071310713
R::Target: Router,
1071410714
L::Target: Logger,
1071510715
{
10716-
fn handle_message(&self, message: OffersMessage, context: OffersContext, responder: Option<Responder>) -> ResponseInstruction<OffersMessage> {
10716+
fn handle_message(
10717+
&self, message: OffersMessage, context: Option<OffersContext>, responder: Option<Responder>,
10718+
) -> ResponseInstruction<OffersMessage> {
1071710719
let secp_ctx = &self.secp_ctx;
1071810720
let expanded_key = &self.inbound_payment_key;
1071910721

1072010722
let abandon_if_payment = |context| {
1072110723
match context {
10722-
OffersContext::OutboundPayment { payment_id, .. } => self.abandon_payment(payment_id),
10724+
Some(OffersContext::OutboundPayment { payment_id, .. }) => {
10725+
self.abandon_payment(payment_id)
10726+
},
1072310727
_ => {},
1072410728
}
1072510729
};
@@ -10732,8 +10736,8 @@ where
1073210736
};
1073310737

1073410738
let nonce = match context {
10735-
OffersContext::Unknown {} if invoice_request.metadata().is_some() => None,
10736-
OffersContext::InvoiceRequest { nonce } => Some(nonce),
10739+
None if invoice_request.metadata().is_some() => None,
10740+
Some(OffersContext::InvoiceRequest { nonce }) => Some(nonce),
1073710741
_ => return ResponseInstruction::NoResponse,
1073810742
};
1073910743

@@ -10828,7 +10832,7 @@ where
1082810832
}
1082910833
},
1083010834
OffersMessage::Invoice(invoice) => {
10831-
let payment_id = match self.verify_bolt12_invoice(&invoice, &context) {
10835+
let payment_id = match self.verify_bolt12_invoice(&invoice, context.as_ref()) {
1083210836
Ok(payment_id) => payment_id,
1083310837
Err(()) => return ResponseInstruction::NoResponse,
1083410838
};
@@ -10889,7 +10893,7 @@ where
1088910893
},
1089010894
OffersMessage::InvoiceError(invoice_error) => {
1089110895
let payment_hash = match context {
10892-
OffersContext::InboundPayment { payment_hash } => Some(payment_hash),
10896+
Some(OffersContext::InboundPayment { payment_hash }) => Some(payment_hash),
1089310897
_ => None,
1089410898
};
1089510899
let logger = WithContext::from(&self.logger, None, None, payment_hash);

lightning/src/ln/offers_tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1099,9 +1099,9 @@ fn pays_bolt12_invoice_asynchronously() {
10991099
assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
11001100
}
11011101

1102-
assert!(bob.node.send_payment_for_bolt12_invoice(&invoice, &context).is_ok());
1102+
assert!(bob.node.send_payment_for_bolt12_invoice(&invoice, context.as_ref()).is_ok());
11031103
assert_eq!(
1104-
bob.node.send_payment_for_bolt12_invoice(&invoice, &context),
1104+
bob.node.send_payment_for_bolt12_invoice(&invoice, context.as_ref()),
11051105
Err(Bolt12PaymentError::DuplicateInvoice),
11061106
);
11071107

@@ -1112,7 +1112,7 @@ fn pays_bolt12_invoice_asynchronously() {
11121112
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
11131113

11141114
assert_eq!(
1115-
bob.node.send_payment_for_bolt12_invoice(&invoice, &context),
1115+
bob.node.send_payment_for_bolt12_invoice(&invoice, context.as_ref()),
11161116
Err(Bolt12PaymentError::DuplicateInvoice),
11171117
);
11181118

@@ -1121,7 +1121,7 @@ fn pays_bolt12_invoice_asynchronously() {
11211121
}
11221122

11231123
assert_eq!(
1124-
bob.node.send_payment_for_bolt12_invoice(&invoice, &context),
1124+
bob.node.send_payment_for_bolt12_invoice(&invoice, context.as_ref()),
11251125
Err(Bolt12PaymentError::UnexpectedInvoice),
11261126
);
11271127
}

lightning/src/ln/peer_handler.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl OnionMessageHandler for IgnoringMessageHandler {
144144
}
145145

146146
impl OffersMessageHandler for IgnoringMessageHandler {
147-
fn handle_message(&self, _message: OffersMessage, _context: OffersContext, _responder: Option<Responder>) -> ResponseInstruction<OffersMessage> {
147+
fn handle_message(&self, _message: OffersMessage, _context: Option<OffersContext>, _responder: Option<Responder>) -> ResponseInstruction<OffersMessage> {
148148
ResponseInstruction::NoResponse
149149
}
150150
}

lightning/src/onion_message/functional_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl Drop for MessengerNode {
7676
struct TestOffersMessageHandler {}
7777

7878
impl OffersMessageHandler for TestOffersMessageHandler {
79-
fn handle_message(&self, _message: OffersMessage, _context: OffersContext, _responder: Option<Responder>) -> ResponseInstruction<OffersMessage> {
79+
fn handle_message(&self, _message: OffersMessage, _context: Option<OffersContext>, _responder: Option<Responder>) -> ResponseInstruction<OffersMessage> {
8080
ResponseInstruction::NoResponse
8181
}
8282
}

lightning/src/onion_message/messenger.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
1616
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
1717

1818
use crate::blinded_path::{BlindedPath, IntroductionNode, NextMessageHop, NodeIdLookUp};
19-
use crate::blinded_path::message::{advance_path_by_one, ForwardNode, ForwardTlvs, MessageContext, OffersContext, ReceiveTlvs};
19+
use crate::blinded_path::message::{advance_path_by_one, ForwardNode, ForwardTlvs, MessageContext, ReceiveTlvs};
2020
use crate::blinded_path::utils;
2121
use crate::events::{Event, EventHandler, EventsProvider, ReplayEvent};
2222
use crate::sign::{EntropySource, NodeSigner, Recipient};
@@ -1514,8 +1514,8 @@ where
15141514
match message {
15151515
ParsedOnionMessageContents::Offers(msg) => {
15161516
let context = match context {
1517-
None => OffersContext::Unknown {},
1518-
Some(MessageContext::Offers(context)) => context,
1517+
None => None,
1518+
Some(MessageContext::Offers(context)) => Some(context),
15191519
Some(MessageContext::Custom(_)) => {
15201520
debug_assert!(false, "Shouldn't have triggered this case.");
15211521
return

lightning/src/onion_message/offers.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ pub trait OffersMessageHandler {
4545
/// The returned [`OffersMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
4646
///
4747
/// [`OnionMessenger`]: crate::onion_message::messenger::OnionMessenger
48-
fn handle_message(&self, message: OffersMessage, context: OffersContext, responder: Option<Responder>) -> ResponseInstruction<OffersMessage>;
48+
fn handle_message(
49+
&self, message: OffersMessage, context: Option<OffersContext>, responder: Option<Responder>,
50+
) -> ResponseInstruction<OffersMessage>;
4951

5052
/// Releases any [`OffersMessage`]s that need to be sent.
5153
///

0 commit comments

Comments
 (0)