Skip to content

Commit 4634abc

Browse files
committed
Introduce MessageContext in ReceiveTlvs
1. New Enum for Enhanced Data Handling: - Introduced the `MessageContext` enum, which allows the node to include additional context data along with the `reply_path` sent to the counterparty. - The node anticipates receiving this data back for further processing. 2. Variants in MessageContext: - The `MessageContext` enum includes two variants: "Offers" and "Context" - One of the variants, `Offers`, holds the `payment_id` of the associated Outbound BOLT12 Payment. 3. Future Usage: - This enum will be utilized in a subsequent commit to abandon outbound payments that have failed to complete for various reasons.
1 parent 069ee2a commit 4634abc

File tree

3 files changed

+82
-17
lines changed

3 files changed

+82
-17
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@
1212
//! [`BlindedPath`]: crate::blinded_path::BlindedPath
1313
1414
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
15-
1615
#[allow(unused_imports)]
1716
use crate::prelude::*;
1817

1918
use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NextMessageHop, NodeIdLookUp};
2019
use crate::blinded_path::utils;
2120
use crate::io;
2221
use crate::io::Cursor;
22+
use crate::ln::channelmanager::PaymentId;
23+
use crate::ln::msgs::DecodeError;
2324
use crate::ln::onion_utils;
2425
use crate::onion_message::packet::ControlTlvs;
2526
use crate::sign::{NodeSigner, Recipient};
2627
use crate::crypto::streams::ChaChaPolyReadAdapter;
27-
use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Writeable, Writer};
28+
use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, Writeable, Writer};
2829

2930
use core::mem;
3031
use core::ops::Deref;
@@ -55,7 +56,7 @@ pub(crate) struct ReceiveTlvs {
5556
/// If `path_id` is `Some`, it is used to identify the blinded path that this onion message is
5657
/// sending to. This is useful for receivers to check that said blinded path is being used in
5758
/// the right context.
58-
pub(crate) path_id: Option<[u8; 32]>,
59+
pub context: Option<MessageContext>
5960
}
6061

6162
impl Writeable for ForwardTlvs {
@@ -78,12 +79,62 @@ impl Writeable for ReceiveTlvs {
7879
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
7980
// TODO: write padding
8081
encode_tlv_stream!(writer, {
81-
(6, self.path_id, option),
82+
(65537, self.context, option),
8283
});
8384
Ok(())
8485
}
8586
}
8687

88+
impl Readable for ReceiveTlvs {
89+
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
90+
_init_and_read_tlv_stream!(r, {
91+
(65537, context, option),
92+
});
93+
Ok(ReceiveTlvs { context })
94+
}
95+
}
96+
97+
/// Represents additional data included by the recipient in a [`BlindedPath`].
98+
///
99+
/// This data is encrypted and will be included when sending through
100+
/// [`BlindedPath`]. The recipient can authenticate the message and
101+
/// utilize it for further processing if needed.
102+
#[derive(Clone, Debug)]
103+
pub enum MessageContext {
104+
/// Represents the data specific to [`OffersMessage`]
105+
///
106+
/// [`OffersMessage`]: crate::onion_message::offers::OffersMessage
107+
Offers(OffersContext),
108+
/// Represents the data appended with Custom Onion Message.
109+
Custom(Vec<u8>),
110+
}
111+
112+
/// Contains the data specific to [`OffersMessage`]
113+
///
114+
/// [`OffersMessage`]: crate::onion_message::offers::OffersMessage
115+
#[derive(Clone, Debug)]
116+
pub enum OffersContext {
117+
/// Represents an unknown context.
118+
Unknown {},
119+
/// Represents an outbound BOLT12 payment context.
120+
OutboundPayment {
121+
/// Payment ID of the outbound BOLT12 payment.
122+
payment_id: PaymentId
123+
},
124+
}
125+
126+
impl_writeable_tlv_based_enum!(MessageContext, ;
127+
(0, Offers),
128+
(1, Custom),
129+
);
130+
131+
impl_writeable_tlv_based_enum!(OffersContext,
132+
(0, Unknown) => {},
133+
(1, OutboundPayment) => {
134+
(0, payment_id, required),
135+
},
136+
;);
137+
87138
/// Construct blinded onion message hops for the given `intermediate_nodes` and `recipient_node_id`.
88139
pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
89140
secp_ctx: &Secp256k1<T>, intermediate_nodes: &[ForwardNode], recipient_node_id: PublicKey,
@@ -99,7 +150,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
99150
None => NextMessageHop::NodeId(*pubkey),
100151
})
101152
.map(|next_hop| ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None }))
102-
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { path_id: None })));
153+
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { context: None })));
103154

104155
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
105156
}

lightning/src/onion_message/messenger.rs

Lines changed: 23 additions & 9 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, MessageContext};
2020
use crate::blinded_path::utils;
2121
use crate::events::{Event, EventHandler, EventsProvider};
2222
use crate::sign::{EntropySource, NodeSigner, Recipient};
@@ -795,8 +795,8 @@ pub trait CustomOnionMessageHandler {
795795
pub enum PeeledOnion<T: OnionMessageContents> {
796796
/// Forwarded onion, with the next node id and a new onion
797797
Forward(NextMessageHop, OnionMessage),
798-
/// Received onion message, with decrypted contents, path_id, and reply path
799-
Receive(ParsedOnionMessageContents<T>, Option<[u8; 32]>, Option<BlindedPath>)
798+
/// Received onion message, with decrypted contents, context, and reply path
799+
Receive(ParsedOnionMessageContents<T>, Option<MessageContext>, Option<BlindedPath>)
800800
}
801801

802802

@@ -946,9 +946,23 @@ where
946946
(control_tlvs_ss, custom_handler.deref(), logger.deref())
947947
) {
948948
Ok((Payload::Receive::<ParsedOnionMessageContents<<<CMH as Deref>::Target as CustomOnionMessageHandler>::CustomMessage>> {
949-
message, control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id }), reply_path,
949+
message, control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { context }), reply_path,
950950
}, None)) => {
951-
Ok(PeeledOnion::Receive(message, path_id, reply_path))
951+
match (&message, &context) {
952+
(_, None) => {
953+
Ok(PeeledOnion::Receive(message, None, reply_path))
954+
}
955+
(ParsedOnionMessageContents::Offers(_), Some(MessageContext::Offers(_))) => {
956+
Ok(PeeledOnion::Receive(message, context, reply_path))
957+
}
958+
(ParsedOnionMessageContents::Custom(_), Some(MessageContext::Custom(_))) => {
959+
Ok(PeeledOnion::Receive(message, context, reply_path))
960+
}
961+
_ => {
962+
log_trace!(logger, "Received message was sent on a blinded path with the wrong context.");
963+
Err(())
964+
}
965+
}
952966
},
953967
Ok((Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {
954968
next_hop, next_blinding_override
@@ -1432,11 +1446,11 @@ where
14321446
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage) {
14331447
let logger = WithContext::from(&self.logger, Some(*peer_node_id), None, None);
14341448
match self.peel_onion_message(msg) {
1435-
Ok(PeeledOnion::Receive(message, path_id, reply_path)) => {
1449+
Ok(PeeledOnion::Receive(message, _context, reply_path)) => {
14361450
log_trace!(
14371451
logger,
1438-
"Received an onion message with path_id {:02x?} and {} reply_path: {:?}",
1439-
path_id, if reply_path.is_some() { "a" } else { "no" }, message);
1452+
"Received an onion message with {} reply_path: {:?}",
1453+
if reply_path.is_some() { "a" } else { "no" }, message);
14401454

14411455
let responder = reply_path.map(Responder::new);
14421456
match message {
@@ -1727,7 +1741,7 @@ fn packet_payloads_and_keys<T: OnionMessageContents, S: secp256k1::Signing + sec
17271741
}, prev_control_tlvs_ss.unwrap()));
17281742
} else {
17291743
payloads.push((Payload::Receive {
1730-
control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id: None, }),
1744+
control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { context: None }),
17311745
reply_path: reply_path.take(),
17321746
message,
17331747
}, prev_control_tlvs_ss.unwrap()));

lightning/src/onion_message/packet.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ impl Readable for ControlTlvs {
319319
(1, _padding, option),
320320
(2, short_channel_id, option),
321321
(4, next_node_id, option),
322-
(6, path_id, option),
323322
(8, next_blinding_override, option),
323+
(65537, context, option),
324324
});
325325
let _padding: Option<Padding> = _padding;
326326

@@ -331,7 +331,7 @@ impl Readable for ControlTlvs {
331331
(None, None) => None,
332332
};
333333

334-
let valid_fwd_fmt = next_hop.is_some() && path_id.is_none();
334+
let valid_fwd_fmt = next_hop.is_some();
335335
let valid_recv_fmt = next_hop.is_none() && next_blinding_override.is_none();
336336

337337
let payload_fmt = if valid_fwd_fmt {
@@ -341,7 +341,7 @@ impl Readable for ControlTlvs {
341341
})
342342
} else if valid_recv_fmt {
343343
ControlTlvs::Receive(ReceiveTlvs {
344-
path_id,
344+
context,
345345
})
346346
} else {
347347
return Err(DecodeError::InvalidValue)

0 commit comments

Comments
 (0)