Skip to content

Commit 8769dde

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 911aa2b commit 8769dde

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
@@ -1,5 +1,6 @@
11
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
22

3+
use crate::ln::channelmanager::PaymentId;
34
#[allow(unused_imports)]
45
use crate::prelude::*;
56

@@ -32,6 +33,28 @@ pub(crate) struct ReceiveTlvs {
3233
/// sending to. This is useful for receivers to check that said blinded path is being used in
3334
/// the right context.
3435
pub(crate) path_id: Option<[u8; 32]>,
36+
pub recipient_data: RecipientData,
37+
}
38+
39+
/// Represents additional data appended along with the sent reply path.
40+
///
41+
/// This data can be utilized by the final recipient for further processing
42+
/// upon receiving it back.
43+
#[derive(Clone, Debug)]
44+
pub struct RecipientData {
45+
/// Payment ID of the outbound BOLT12 payment.
46+
pub payment_id: Option<PaymentId>
47+
// TODO: Introduce a more general form of RecipientData
48+
// that can be used by Custom Messages
49+
}
50+
51+
impl RecipientData {
52+
/// Creates a new, empty RecipientData instance.
53+
pub fn new() -> Self {
54+
RecipientData {
55+
payment_id: None,
56+
}
57+
}
3558
}
3659

3760
impl Writeable for ForwardTlvs {
@@ -53,8 +76,10 @@ impl Writeable for ForwardTlvs {
5376
impl Writeable for ReceiveTlvs {
5477
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
5578
// TODO: write padding
79+
let RecipientData { payment_id } = self.recipient_data;
5680
encode_tlv_stream!(writer, {
5781
(6, self.path_id, option),
82+
(65537, payment_id, option),
5883
});
5984
Ok(())
6085
}
@@ -68,7 +93,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
6893
.skip(1) // The first node's TLVs contains the next node's pubkey
6994
.map(|pk| ForwardTlvs { next_hop: NextMessageHop::NodeId(*pk), next_blinding_override: None })
7095
.map(|tlvs| ControlTlvs::Forward(tlvs))
71-
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { path_id: None })));
96+
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { path_id: None, recipient_data: RecipientData::new() })));
7297

7398
utils::construct_blinded_hops(secp_ctx, unblinded_path.iter(), blinded_tlvs, session_priv)
7499
}

lightning/src/ln/offers_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ fn extract_invoice_request<'a, 'b, 'c>(
180180
node: &Node<'a, 'b, 'c>, message: &OnionMessage
181181
) -> (InvoiceRequest, Option<BlindedPath>) {
182182
match node.onion_messenger.peel_onion_message(message) {
183-
Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
183+
Ok(PeeledOnion::Receive(message, _, _, reply_path)) => match message {
184184
ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
185185
OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path),
186186
OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
@@ -195,7 +195,7 @@ fn extract_invoice_request<'a, 'b, 'c>(
195195

196196
fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
197197
match node.onion_messenger.peel_onion_message(message) {
198-
Ok(PeeledOnion::Receive(message, _, _)) => match message {
198+
Ok(PeeledOnion::Receive(message, _, _, _)) => match message {
199199
ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
200200
OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
201201
OffersMessage::Invoice(invoice) => invoice,
@@ -212,7 +212,7 @@ fn extract_invoice_error<'a, 'b, 'c>(
212212
node: &Node<'a, 'b, 'c>, message: &OnionMessage
213213
) -> InvoiceError {
214214
match node.onion_messenger.peel_onion_message(message) {
215-
Ok(PeeledOnion::Receive(message, _, _)) => match message {
215+
Ok(PeeledOnion::Receive(message, _, _, _)) => match message {
216216
ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
217217
OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
218218
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, ForwardTlvs, ReceiveTlvs};
19+
use crate::blinded_path::message::{advance_path_by_one, ForwardTlvs, ReceiveTlvs, RecipientData};
2020
use crate::blinded_path::utils;
2121
use crate::events::{Event, EventHandler, EventsProvider};
2222
use crate::sign::{EntropySource, NodeSigner, Recipient};
@@ -641,7 +641,7 @@ pub enum PeeledOnion<T: OnionMessageContents> {
641641
/// Forwarded onion, with the next node id and a new onion
642642
Forward(NextMessageHop, OnionMessage),
643643
/// Received onion message, with decrypted contents, path_id, and reply path
644-
Receive(ParsedOnionMessageContents<T>, Option<[u8; 32]>, Option<BlindedPath>)
644+
Receive(ParsedOnionMessageContents<T>, Option<[u8; 32]>, RecipientData, Option<BlindedPath>)
645645
}
646646

647647

@@ -791,9 +791,9 @@ where
791791
(control_tlvs_ss, custom_handler.deref(), logger.deref())
792792
) {
793793
Ok((Payload::Receive::<ParsedOnionMessageContents<<<CMH as Deref>::Target as CustomOnionMessageHandler>::CustomMessage>> {
794-
message, control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id }), reply_path,
794+
message, control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id , recipient_data }), reply_path,
795795
}, None)) => {
796-
Ok(PeeledOnion::Receive(message, path_id, reply_path))
796+
Ok(PeeledOnion::Receive(message, path_id, recipient_data, reply_path))
797797
},
798798
Ok((Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {
799799
next_hop, next_blinding_override
@@ -1209,7 +1209,7 @@ where
12091209
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage) {
12101210
let logger = WithContext::from(&self.logger, Some(*peer_node_id), None, None);
12111211
match self.peel_onion_message(msg) {
1212-
Ok(PeeledOnion::Receive(message, path_id, reply_path)) => {
1212+
Ok(PeeledOnion::Receive(message, path_id, _, reply_path)) => {
12131213
log_trace!(
12141214
logger,
12151215
"Received an onion message with path_id {:02x?} and {} reply_path: {:?}",
@@ -1496,7 +1496,7 @@ fn packet_payloads_and_keys<T: OnionMessageContents, S: secp256k1::Signing + sec
14961496
}, prev_control_tlvs_ss.unwrap()));
14971497
} else {
14981498
payloads.push((Payload::Receive {
1499-
control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id: None, }),
1499+
control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id: None, recipient_data: RecipientData::new() }),
15001500
reply_path: reply_path.take(),
15011501
message,
15021502
}, 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)