Skip to content

Commit cfe3410

Browse files
committed
Make OnionMessageHandler extend EventsProvider
An OnionMessageHandler may buffer messages that can't be sent because the recipient is not a peer. Have the trait extend EventsProvider so that implementation so that an Event::ConnectionNeeded can be generated for any nodes that fall into this category. Also, implement EventsProvider for OnionMessenger and IgnoringMessageHandler.
1 parent f965e3c commit cfe3410

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

lightning/src/ln/msgs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use core::fmt::Display;
5252
use crate::io::{self, Cursor, Read};
5353
use crate::io_extras::read_to_end;
5454

55-
use crate::events::MessageSendEventsProvider;
55+
use crate::events::{EventsProvider, MessageSendEventsProvider};
5656
use crate::util::chacha20poly1305rfc::ChaChaPolyReadAdapter;
5757
use crate::util::logger;
5858
use crate::util::ser::{LengthReadable, LengthReadableArgs, Readable, ReadableArgs, Writeable, Writer, WithoutLength, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname, TransactionU16LenLimited, BigSize};
@@ -1561,7 +1561,7 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
15611561
}
15621562

15631563
/// A handler for received [`OnionMessage`]s and for providing generated ones to send.
1564-
pub trait OnionMessageHandler {
1564+
pub trait OnionMessageHandler: EventsProvider {
15651565
/// Handle an incoming `onion_message` message from the given peer.
15661566
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage);
15671567

lightning/src/ln/peer_handler.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use bitcoin::blockdata::constants::ChainHash;
1919
use bitcoin::secp256k1::{self, Secp256k1, SecretKey, PublicKey};
2020

2121
use crate::sign::{KeysManager, NodeSigner, Recipient};
22-
use crate::events::{MessageSendEvent, MessageSendEventsProvider};
22+
use crate::events::{EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
2323
use crate::ln::ChannelId;
2424
use crate::ln::features::{InitFeatures, NodeFeatures};
2525
use crate::ln::msgs;
@@ -89,6 +89,9 @@ pub trait CustomMessageHandler: wire::CustomMessageReader {
8989
/// A dummy struct which implements `RoutingMessageHandler` without storing any routing information
9090
/// or doing any processing. You can provide one of these as the route_handler in a MessageHandler.
9191
pub struct IgnoringMessageHandler{}
92+
impl EventsProvider for IgnoringMessageHandler {
93+
fn process_pending_events<H: Deref>(&self, _handler: H) where H::Target: EventHandler {}
94+
}
9295
impl MessageSendEventsProvider for IgnoringMessageHandler {
9396
fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> { Vec::new() }
9497
}

lightning/src/onion_message/messenger.rs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
1818
use crate::blinded_path::BlindedPath;
1919
use crate::blinded_path::message::{advance_path_by_one, ForwardTlvs, ReceiveTlvs};
2020
use crate::blinded_path::utils;
21+
use crate::events::{Event, EventHandler, EventsProvider};
2122
use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient};
2223
#[cfg(not(c_bindings))]
2324
use crate::ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager};
@@ -164,21 +165,21 @@ enum OnionMessageBuffer {
164165
ConnectedPeer(VecDeque<OnionMessage>),
165166

166167
/// Messages for a node that is not yet connected.
167-
PendingConnection(VecDeque<OnionMessage>),
168+
PendingConnection(VecDeque<OnionMessage>, Option<Vec<SocketAddress>>),
168169
}
169170

170171
impl OnionMessageBuffer {
171172
fn pending_messages(&self) -> &VecDeque<OnionMessage> {
172173
match self {
173174
OnionMessageBuffer::ConnectedPeer(pending_messages) => pending_messages,
174-
OnionMessageBuffer::PendingConnection(pending_messages) => pending_messages,
175+
OnionMessageBuffer::PendingConnection(pending_messages, _) => pending_messages,
175176
}
176177
}
177178

178179
fn enqueue_message(&mut self, message: OnionMessage) {
179180
let pending_messages = match self {
180181
OnionMessageBuffer::ConnectedPeer(pending_messages) => pending_messages,
181-
OnionMessageBuffer::PendingConnection(pending_messages) => pending_messages,
182+
OnionMessageBuffer::PendingConnection(pending_messages, _) => pending_messages,
182183
};
183184

184185
pending_messages.push_back(message);
@@ -187,7 +188,7 @@ impl OnionMessageBuffer {
187188
fn dequeue_message(&mut self) -> Option<OnionMessage> {
188189
let pending_messages = match self {
189190
OnionMessageBuffer::ConnectedPeer(pending_messages) => pending_messages,
190-
OnionMessageBuffer::PendingConnection(pending_messages) => pending_messages,
191+
OnionMessageBuffer::PendingConnection(pending_messages, _) => pending_messages,
191192
};
192193

193194
pending_messages.pop_front()
@@ -197,14 +198,14 @@ impl OnionMessageBuffer {
197198
fn release_pending_messages(&mut self) -> VecDeque<OnionMessage> {
198199
let pending_messages = match self {
199200
OnionMessageBuffer::ConnectedPeer(pending_messages) => pending_messages,
200-
OnionMessageBuffer::PendingConnection(pending_messages) => pending_messages,
201+
OnionMessageBuffer::PendingConnection(pending_messages, _) => pending_messages,
201202
};
202203

203204
core::mem::take(pending_messages)
204205
}
205206

206207
fn mark_connected(&mut self) {
207-
if let OnionMessageBuffer::PendingConnection(pending_messages) = self {
208+
if let OnionMessageBuffer::PendingConnection(pending_messages, _) = self {
208209
let mut new_pending_messages = VecDeque::new();
209210
core::mem::swap(pending_messages, &mut new_pending_messages);
210211
*self = OnionMessageBuffer::ConnectedPeer(new_pending_messages);
@@ -376,6 +377,8 @@ pub enum SendError {
376377
/// The provided [`Destination`] was an invalid [`BlindedPath`] due to not having any blinded
377378
/// hops.
378379
TooFewBlindedHops,
380+
/// The first hop does not support onion message forwarding.
381+
InvalidFirstHop(PublicKey),
379382
/// A path from the sender to the destination could not be found by the [`MessageRouter`].
380383
PathNotFound,
381384
/// Onion message contents must have a TLV type >= 64.
@@ -448,12 +451,12 @@ pub enum PeeledOnion<T: OnionMessageContents> {
448451
pub fn create_onion_message<ES: Deref, NS: Deref, T: OnionMessageContents>(
449452
entropy_source: &ES, node_signer: &NS, secp_ctx: &Secp256k1<secp256k1::All>,
450453
path: OnionMessagePath, contents: T, reply_path: Option<BlindedPath>,
451-
) -> Result<(PublicKey, OnionMessage), SendError>
454+
) -> Result<(PublicKey, OnionMessage, Option<Vec<SocketAddress>>), SendError>
452455
where
453456
ES::Target: EntropySource,
454457
NS::Target: NodeSigner,
455458
{
456-
let OnionMessagePath { intermediate_nodes, mut destination, .. } = path;
459+
let OnionMessagePath { intermediate_nodes, mut destination, addresses } = path;
457460
if let Destination::BlindedPath(BlindedPath { ref blinded_hops, .. }) = destination {
458461
if blinded_hops.is_empty() {
459462
return Err(SendError::TooFewBlindedHops);
@@ -494,10 +497,8 @@ where
494497
let onion_routing_packet = construct_onion_message_packet(
495498
packet_payloads, packet_keys, prng_seed).map_err(|()| SendError::TooBigPacket)?;
496499

497-
Ok((first_node_id, OnionMessage {
498-
blinding_point,
499-
onion_routing_packet
500-
}))
500+
let message = OnionMessage { blinding_point, onion_routing_packet };
501+
Ok((first_node_id, message, addresses))
501502
}
502503

503504
/// Decode one layer of an incoming [`OnionMessage`].
@@ -653,7 +654,7 @@ where
653654
pub(super) fn send_onion_message_using_path<T: OnionMessageContents>(
654655
&self, path: OnionMessagePath, contents: T, reply_path: Option<BlindedPath>
655656
) -> Result<SendSuccess, SendError> {
656-
let (first_node_id, onion_message) = create_onion_message(
657+
let (first_node_id, onion_message, addresses) = create_onion_message(
657658
&self.entropy_source, &self.node_signer, &self.secp_ctx, path, contents, reply_path
658659
)?;
659660

@@ -663,10 +664,14 @@ where
663664
}
664665

665666
match message_buffers.entry(first_node_id) {
666-
hash_map::Entry::Vacant(e) => {
667-
e.insert(OnionMessageBuffer::PendingConnection(VecDeque::new()))
668-
.enqueue_message(onion_message);
669-
Ok(SendSuccess::BufferedAwaitingConnection(first_node_id))
667+
hash_map::Entry::Vacant(e) => match addresses {
668+
None => Err(SendError::InvalidFirstHop(first_node_id)),
669+
Some(addresses) => {
670+
e.insert(
671+
OnionMessageBuffer::PendingConnection(VecDeque::new(), Some(addresses))
672+
).enqueue_message(onion_message);
673+
Ok(SendSuccess::BufferedAwaitingConnection(first_node_id))
674+
},
670675
},
671676
hash_map::Entry::Occupied(mut e) => {
672677
e.get_mut().enqueue_message(onion_message);
@@ -754,6 +759,27 @@ fn outbound_buffer_full(peer_node_id: &PublicKey, buffer: &HashMap<PublicKey, On
754759
false
755760
}
756761

762+
impl<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref> EventsProvider
763+
for OnionMessenger<ES, NS, L, MR, OMH, CMH>
764+
where
765+
ES::Target: EntropySource,
766+
NS::Target: NodeSigner,
767+
L::Target: Logger,
768+
MR::Target: MessageRouter,
769+
OMH::Target: OffersMessageHandler,
770+
CMH::Target: CustomOnionMessageHandler,
771+
{
772+
fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler {
773+
for (node_id, recipient) in self.message_buffers.lock().unwrap().iter_mut() {
774+
if let OnionMessageBuffer::PendingConnection(_, addresses) = recipient {
775+
if let Some(addresses) = addresses.take() {
776+
handler.handle_event(Event::ConnectionNeeded { node_id: *node_id, addresses });
777+
}
778+
}
779+
}
780+
}
781+
}
782+
757783
impl<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref> OnionMessageHandler
758784
for OnionMessenger<ES, NS, L, MR, OMH, CMH>
759785
where

0 commit comments

Comments
 (0)