Skip to content

Commit a249303

Browse files
committed
Introduce RecipientData in ReceiveTlvs
1. This struct will be used by the node to add data along with the reply_path sent to the counterparty. The node expects to receive this data back and use it for doing further processing. 2. In its first iteration, RecipientData contains the payment_id of the corresponding Outbound BOLT12 Payment. 3. This will be used in a later commit to abandon outbound payments that have failed to complete for some reason.
1 parent 834779d commit a249303

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1414
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
1515

16+
use crate::ln::channelmanager::PaymentId;
1617
#[allow(unused_imports)]
1718
use crate::prelude::*;
1819

@@ -56,6 +57,28 @@ pub(crate) struct ReceiveTlvs {
5657
/// sending to. This is useful for receivers to check that said blinded path is being used in
5758
/// the right context.
5859
pub(crate) path_id: Option<[u8; 32]>,
60+
pub recipient_data: RecipientData,
61+
}
62+
63+
/// Represents additional data appended along with the sent reply path.
64+
///
65+
/// This data can be utilized by the final recipient for further processing
66+
/// upon receiving it back.
67+
#[derive(Clone, Debug)]
68+
pub struct RecipientData {
69+
/// Payment ID of the outbound BOLT12 payment.
70+
pub payment_id: Option<PaymentId>
71+
// TODO: Introduce a more general form of RecipientData
72+
// that can be used by Custom Messages
73+
}
74+
75+
impl RecipientData {
76+
/// Creates a new, empty RecipientData instance.
77+
pub fn new() -> Self {
78+
RecipientData {
79+
payment_id: None,
80+
}
81+
}
5982
}
6083

6184
impl Writeable for ForwardTlvs {
@@ -77,8 +100,10 @@ impl Writeable for ForwardTlvs {
77100
impl Writeable for ReceiveTlvs {
78101
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
79102
// TODO: write padding
103+
let RecipientData { payment_id } = self.recipient_data;
80104
encode_tlv_stream!(writer, {
81105
(6, self.path_id, option),
106+
(65537, payment_id, option),
82107
});
83108
Ok(())
84109
}
@@ -99,7 +124,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
99124
None => NextMessageHop::NodeId(*pubkey),
100125
})
101126
.map(|next_hop| ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None }))
102-
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { path_id: None })));
127+
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { path_id: None, recipient_data: RecipientData::new() })));
103128

104129
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
105130
}

lightning/src/ln/offers_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ fn extract_invoice_request<'a, 'b, 'c>(
187187
node: &Node<'a, 'b, 'c>, message: &OnionMessage
188188
) -> (InvoiceRequest, BlindedPath) {
189189
match node.onion_messenger.peel_onion_message(message) {
190-
Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
190+
Ok(PeeledOnion::Receive(message, _, _, reply_path)) => match message {
191191
ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
192192
OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path.unwrap()),
193193
OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
@@ -202,7 +202,7 @@ fn extract_invoice_request<'a, 'b, 'c>(
202202

203203
fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
204204
match node.onion_messenger.peel_onion_message(message) {
205-
Ok(PeeledOnion::Receive(message, _, _)) => match message {
205+
Ok(PeeledOnion::Receive(message, _, _, _)) => match message {
206206
ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
207207
OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
208208
OffersMessage::Invoice(invoice) => invoice,
@@ -219,7 +219,7 @@ fn extract_invoice_error<'a, 'b, 'c>(
219219
node: &Node<'a, 'b, 'c>, message: &OnionMessage
220220
) -> InvoiceError {
221221
match node.onion_messenger.peel_onion_message(message) {
222-
Ok(PeeledOnion::Receive(message, _, _)) => match message {
222+
Ok(PeeledOnion::Receive(message, _, _, _)) => match message {
223223
ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
224224
OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
225225
OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),

lightning/src/onion_message/messenger.rs

Lines changed: 6 additions & 6 deletions
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, ReceiveTlvs};
19+
use crate::blinded_path::message::{advance_path_by_one, ForwardNode, ForwardTlvs, ReceiveTlvs, RecipientData};
2020
use crate::blinded_path::utils;
2121
use crate::events::{Event, EventHandler, EventsProvider};
2222
use crate::sign::{EntropySource, NodeSigner, Recipient};
@@ -651,7 +651,7 @@ pub enum PeeledOnion<T: OnionMessageContents> {
651651
/// Forwarded onion, with the next node id and a new onion
652652
Forward(NextMessageHop, OnionMessage),
653653
/// Received onion message, with decrypted contents, path_id, and reply path
654-
Receive(ParsedOnionMessageContents<T>, Option<[u8; 32]>, Option<BlindedPath>)
654+
Receive(ParsedOnionMessageContents<T>, Option<[u8; 32]>, RecipientData, Option<BlindedPath>)
655655
}
656656

657657

@@ -801,9 +801,9 @@ where
801801
(control_tlvs_ss, custom_handler.deref(), logger.deref())
802802
) {
803803
Ok((Payload::Receive::<ParsedOnionMessageContents<<<CMH as Deref>::Target as CustomOnionMessageHandler>::CustomMessage>> {
804-
message, control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id }), reply_path,
804+
message, control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id , recipient_data }), reply_path,
805805
}, None)) => {
806-
Ok(PeeledOnion::Receive(message, path_id, reply_path))
806+
Ok(PeeledOnion::Receive(message, path_id, recipient_data, reply_path))
807807
},
808808
Ok((Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {
809809
next_hop, next_blinding_override
@@ -1236,7 +1236,7 @@ where
12361236
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage) {
12371237
let logger = WithContext::from(&self.logger, Some(*peer_node_id), None, None);
12381238
match self.peel_onion_message(msg) {
1239-
Ok(PeeledOnion::Receive(message, path_id, reply_path)) => {
1239+
Ok(PeeledOnion::Receive(message, path_id, _, reply_path)) => {
12401240
log_trace!(
12411241
logger,
12421242
"Received an onion message with path_id {:02x?} and {} reply_path: {:?}",
@@ -1523,7 +1523,7 @@ fn packet_payloads_and_keys<T: OnionMessageContents, S: secp256k1::Signing + sec
15231523
}, prev_control_tlvs_ss.unwrap()));
15241524
} else {
15251525
payloads.push((Payload::Receive {
1526-
control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id: None, }),
1526+
control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id: None, recipient_data: RecipientData::new() }),
15271527
reply_path: reply_path.take(),
15281528
message,
15291529
}, prev_control_tlvs_ss.unwrap()));

lightning/src/onion_message/packet.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use bitcoin::secp256k1::PublicKey;
1313
use bitcoin::secp256k1::ecdh::SharedSecret;
1414

1515
use crate::blinded_path::{BlindedPath, NextMessageHop};
16-
use crate::blinded_path::message::{ForwardTlvs, ReceiveTlvs};
16+
use crate::blinded_path::message::{ForwardTlvs, ReceiveTlvs, RecipientData};
1717
use crate::blinded_path::utils::Padding;
1818
use crate::ln::msgs::DecodeError;
1919
use crate::ln::onion_utils;
@@ -304,6 +304,7 @@ impl Readable for ControlTlvs {
304304
(4, next_node_id, option),
305305
(6, path_id, option),
306306
(8, next_blinding_override, option),
307+
(65537, payment_id, option),
307308
});
308309
let _padding: Option<Padding> = _padding;
309310

@@ -325,6 +326,7 @@ impl Readable for ControlTlvs {
325326
} else if valid_recv_fmt {
326327
ControlTlvs::Receive(ReceiveTlvs {
327328
path_id,
329+
recipient_data: RecipientData { payment_id },
328330
})
329331
} else {
330332
return Err(DecodeError::InvalidValue)

0 commit comments

Comments
 (0)