Skip to content

Commit 6daa533

Browse files
Add empty OnionMessenger, OnionMessageHandler trait, and boilerplate
This fills in the boilerplate needed to hook up the OnionMessenger to send and receive messages through the PeerManager. It also sets up the OnionMessenger and its struct fields.
1 parent 0e184cb commit 6daa533

File tree

7 files changed

+140
-26
lines changed

7 files changed

+140
-26
lines changed

lightning-background-processor/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,15 @@ impl BackgroundProcessor {
178178
P: 'static + Deref + Send + Sync,
179179
Descriptor: 'static + SocketDescriptor + Send + Sync,
180180
CMH: 'static + Deref + Send + Sync,
181+
OMH: 'static + Deref + Send + Sync,
181182
RMH: 'static + Deref + Send + Sync,
182183
EH: 'static + EventHandler + Send,
183184
PS: 'static + Deref + Send,
184185
M: 'static + Deref<Target = ChainMonitor<Signer, CF, T, F, L, P>> + Send + Sync,
185186
CM: 'static + Deref<Target = ChannelManager<Signer, CW, T, K, F, L>> + Send + Sync,
186187
PGS: 'static + Deref<Target = P2PGossipSync<G, CA, L>> + Send + Sync,
187188
UMH: 'static + Deref + Send + Sync,
188-
PM: 'static + Deref<Target = PeerManager<Descriptor, CMH, RMH, L, UMH>> + Send + Sync,
189+
PM: 'static + Deref<Target = PeerManager<Descriptor, CMH, RMH, OMH, L, UMH>> + Send + Sync,
189190
S: 'static + Deref<Target = SC> + Send + Sync,
190191
SC: WriteableScore<'a>,
191192
RGS: 'static + Deref<Target = RapidGossipSync<G>> + Send
@@ -204,6 +205,7 @@ impl BackgroundProcessor {
204205
L::Target: 'static + Logger,
205206
P::Target: 'static + Persist<Signer>,
206207
CMH::Target: 'static + ChannelMessageHandler,
208+
OMH::Target: 'static + OnionMessageHandler,
207209
RMH::Target: 'static + RoutingMessageHandler,
208210
UMH::Target: 'static + CustomMessageHandler,
209211
PS::Target: 'static + Persister<'a, Signer, CW, T, K, F, L, SC>,

lightning-net-tokio/src/lib.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ struct Connection {
120120
id: u64,
121121
}
122122
impl Connection {
123-
async fn poll_event_process<CMH, RMH, L, UMH>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<RMH>, Arc<L>, Arc<UMH>>>, mut event_receiver: mpsc::Receiver<()>) where
123+
async fn poll_event_process<CMH, RMH, OMH, L, UMH>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<RMH>, Arc<OMH>, Arc<L>, Arc<UMH>>>, mut event_receiver: mpsc::Receiver<()>) where
124124
CMH: ChannelMessageHandler + 'static + Send + Sync,
125125
RMH: RoutingMessageHandler + 'static + Send + Sync,
126+
OMH: OnionMessageHandler + 'static + Send + Sync,
126127
L: Logger + 'static + ?Sized + Send + Sync,
127128
UMH: CustomMessageHandler + 'static + Send + Sync {
128129
loop {
@@ -133,9 +134,10 @@ impl Connection {
133134
}
134135
}
135136

136-
async fn schedule_read<CMH, RMH, L, UMH>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<RMH>, Arc<L>, Arc<UMH>>>, us: Arc<Mutex<Self>>, mut reader: io::ReadHalf<TcpStream>, mut read_wake_receiver: mpsc::Receiver<()>, mut write_avail_receiver: mpsc::Receiver<()>) where
137+
async fn schedule_read<CMH, RMH, OMH, L, UMH>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<RMH>, Arc<OMH>, Arc<L>, Arc<UMH>>>, us: Arc<Mutex<Self>>, mut reader: io::ReadHalf<TcpStream>, mut read_wake_receiver: mpsc::Receiver<()>, mut write_avail_receiver: mpsc::Receiver<()>) where
137138
CMH: ChannelMessageHandler + 'static + Send + Sync,
138139
RMH: RoutingMessageHandler + 'static + Send + Sync,
140+
OMH: OnionMessageHandler + 'static + Send + Sync,
139141
L: Logger + 'static + ?Sized + Send + Sync,
140142
UMH: CustomMessageHandler + 'static + Send + Sync {
141143
// Create a waker to wake up poll_event_process, above
@@ -255,9 +257,10 @@ fn get_addr_from_stream(stream: &StdTcpStream) -> Option<NetAddress> {
255257
/// The returned future will complete when the peer is disconnected and associated handling
256258
/// futures are freed, though, because all processing futures are spawned with tokio::spawn, you do
257259
/// not need to poll the provided future in order to make progress.
258-
pub fn setup_inbound<CMH, RMH, L, UMH>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<RMH>, Arc<L>, Arc<UMH>>>, stream: StdTcpStream) -> impl std::future::Future<Output=()> where
260+
pub fn setup_inbound<CMH, RMH, OMH, L, UMH>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<RMH>, Arc<OMH>, Arc<L>, Arc<UMH>>>, stream: StdTcpStream) -> impl std::future::Future<Output=()> where
259261
CMH: ChannelMessageHandler + 'static + Send + Sync,
260262
RMH: RoutingMessageHandler + 'static + Send + Sync,
263+
OMH: OnionMessageHandler + 'static + Send + Sync,
261264
L: Logger + 'static + ?Sized + Send + Sync,
262265
UMH: CustomMessageHandler + 'static + Send + Sync {
263266
let remote_addr = get_addr_from_stream(&stream);
@@ -297,9 +300,10 @@ pub fn setup_inbound<CMH, RMH, L, UMH>(peer_manager: Arc<peer_handler::PeerManag
297300
/// The returned future will complete when the peer is disconnected and associated handling
298301
/// futures are freed, though, because all processing futures are spawned with tokio::spawn, you do
299302
/// not need to poll the provided future in order to make progress.
300-
pub fn setup_outbound<CMH, RMH, L, UMH>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<RMH>, Arc<L>, Arc<UMH>>>, their_node_id: PublicKey, stream: StdTcpStream) -> impl std::future::Future<Output=()> where
303+
pub fn setup_outbound<CMH, RMH, OMH, L, UMH>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<RMH>, Arc<OMH>, Arc<L>, Arc<UMH>>>, their_node_id: PublicKey, stream: StdTcpStream) -> impl std::future::Future<Output=()> where
301304
CMH: ChannelMessageHandler + 'static + Send + Sync,
302305
RMH: RoutingMessageHandler + 'static + Send + Sync,
306+
OMH: OnionMessageHandler + 'static + Send + Sync,
303307
L: Logger + 'static + ?Sized + Send + Sync,
304308
UMH: CustomMessageHandler + 'static + Send + Sync {
305309
let remote_addr = get_addr_from_stream(&stream);
@@ -368,9 +372,10 @@ pub fn setup_outbound<CMH, RMH, L, UMH>(peer_manager: Arc<peer_handler::PeerMana
368372
/// disconnected and associated handling futures are freed, though, because all processing in said
369373
/// futures are spawned with tokio::spawn, you do not need to poll the second future in order to
370374
/// make progress.
371-
pub async fn connect_outbound<CMH, RMH, L, UMH>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<RMH>, Arc<L>, Arc<UMH>>>, their_node_id: PublicKey, addr: SocketAddr) -> Option<impl std::future::Future<Output=()>> where
375+
pub async fn connect_outbound<CMH, RMH, OMH, L, UMH>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<RMH>, Arc<OMH>, Arc<L>, Arc<UMH>>>, their_node_id: PublicKey, addr: SocketAddr) -> Option<impl std::future::Future<Output=()>> where
372376
CMH: ChannelMessageHandler + 'static + Send + Sync,
373377
RMH: RoutingMessageHandler + 'static + Send + Sync,
378+
OMH: OnionMessageHandler + 'static + Send + Sync,
374379
L: Logger + 'static + ?Sized + Send + Sync,
375380
UMH: CustomMessageHandler + 'static + Send + Sync {
376381
if let Ok(Ok(stream)) = time::timeout(Duration::from_secs(10), async { TcpStream::connect(&addr).await.map(|s| s.into_std().unwrap()) }).await {

lightning/src/ln/msgs.rs

+25
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,16 @@ pub struct UpdateAddHTLC {
304304
pub(crate) onion_routing_packet: OnionPacket,
305305
}
306306

307+
/// An onion message to be sent or received from a peer
308+
#[derive(Clone, Debug, PartialEq)]
309+
pub struct OnionMessage {
310+
/// This blinding point is used in the shared secret that is used to decrypt the onion message
311+
/// payload's `encrypted_data` field.
312+
pub(crate) blinding_point: PublicKey,
313+
pub(crate) len: u16,
314+
pub(crate) onion_routing_packet: onion_message::Packet,
315+
}
316+
307317
/// An update_fulfill_htlc message to be sent or received from a peer
308318
#[derive(Clone, Debug, PartialEq)]
309319
pub struct UpdateFulfillHTLC {
@@ -912,6 +922,12 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
912922
fn handle_query_short_channel_ids(&self, their_node_id: &PublicKey, msg: QueryShortChannelIds) -> Result<(), LightningError>;
913923
}
914924

925+
/// A trait to describe an object that can receive onion messages.
926+
pub trait OnionMessageHandler : MessageSendEventsProvider {
927+
/// Handle an incoming onion_message message from the given peer.
928+
fn handle_onion_message(&self, their_node_id: &PublicKey, msg: &OnionMessage);
929+
}
930+
915931
mod fuzzy_internal_msgs {
916932
use prelude::*;
917933
use ln::{PaymentPreimage, PaymentSecret};
@@ -1304,6 +1320,15 @@ impl_writeable_msg!(UpdateAddHTLC, {
13041320
onion_routing_packet
13051321
}, {});
13061322

1323+
impl Readable for OnionMessage {
1324+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1325+
}
1326+
}
1327+
1328+
impl Writeable for OnionMessage {
1329+
}
1330+
}
1331+
13071332
impl Writeable for FinalOnionHopData {
13081333
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
13091334
self.payment_secret.0.write(w)?;

lightning/src/ln/onion_message.rs

+55
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,54 @@ impl BlindedRoute {
105105
fn encrypt_payload(payload: ControlTlvs, encrypted_tlvs_ss: SharedSecret) -> Vec<u8> {}
106106
}
107107

108+
/// A sender, receiver and forwarder of onion messages. In upcoming releases, this object will be
109+
/// used to retrieve invoices and fulfill invoice requests from offers.
110+
pub struct OnionMessenger<Signer: Sign, K: Deref, L: Deref>
111+
where K::Target: KeysInterface<Signer = Signer>,
112+
L::Target: Logger,
113+
{
114+
keys_manager: K,
115+
logger: L,
116+
pending_msg_events: Mutex<Vec<MessageSendEvent>>,
117+
secp_ctx: Secp256k1<secp256k1::All>,
118+
// Coming soon:
119+
// invoice_handler: InvoiceHandler,
120+
// custom_handler: CustomHandler, // handles custom onion messages
121+
}
122+
123+
impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
124+
where K::Target: KeysInterface<Signer = Signer>,
125+
L::Target: Logger,
126+
{
127+
/// Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
128+
/// their respective handlers.
129+
pub fn new(keys_manager: K, logger: L) -> Self {
130+
let mut secp_ctx = Secp256k1::new();
131+
secp_ctx.seeded_randomize(&keys_manager.get_secure_random_bytes());
132+
OnionMessenger {
133+
keys_manager,
134+
pending_msg_events: Mutex::new(Vec::new()),
135+
secp_ctx,
136+
logger,
137+
}
138+
}
139+
}
140+
141+
impl<Signer: Sign, K: Deref, L: Deref> OnionMessageHandler for OnionMessenger<Signer, K, L>
142+
where K::Target: KeysInterface<Signer = Signer>,
143+
L::Target: Logger,
144+
{
145+
fn handle_onion_message(&self, _peer_node_id: &PublicKey, msg: &msgs::OnionMessage) {}
146+
}
147+
148+
impl<Signer: Sign, K: Deref, L: Deref> MessageSendEventsProvider for OnionMessenger<Signer, K, L>
149+
where K::Target: KeysInterface<Signer = Signer>,
150+
L::Target: Logger,
151+
{
152+
fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
153+
}
154+
}
155+
108156
#[allow(unused_assignments)]
109157
#[inline]
110158
fn construct_keys_callback<T: secp256k1::Signing + secp256k1::Verification, FType: FnMut(PublicKey, SharedSecret, [u8; 32], PublicKey, SharedSecret)> (secp_ctx: &Secp256k1<T>, unblinded_path: &Vec<PublicKey>, session_priv: &SecretKey, mut callback: FType) -> Result<(), secp256k1::Error> {}
@@ -119,3 +167,10 @@ fn construct_blinded_route_keys<T: secp256k1::Signing + secp256k1::Verification>
119167
) -> Result<(Vec<SharedSecret>, Vec<PublicKey>), secp256k1::Error> {
120168
// calls construct_keys_callback
121169
}
170+
171+
/// Useful for simplifying the parameters of [`SimpleArcChannelManager`] and
172+
/// [`SimpleArcPeerManager`]. See their docs for more details.
173+
pub type SimpleArcOnionMessenger<L> = OnionMessenger<InMemorySigner, Arc<KeysManager>, Arc<L>>;
174+
/// Useful for simplifying the parameters of [`SimpleRefChannelManager`] and
175+
/// [`SimpleRefPeerManager`]. See their docs for more details.
176+
pub type SimpleRefOnionMessenger<'a, 'b, L> = OnionMessenger<InMemorySigner, &'a KeysManager, &'b L>;

0 commit comments

Comments
 (0)