Skip to content

Commit

Permalink
Replace EventsProvider on OnionMessageHandler with a single fn
Browse files Browse the repository at this point in the history
`OnionMessageHandler`s are expected to regularly release a set of
nodes which we need to directly connect to to deliver onion
messages. In order to do so, they currently extend
`EventsProvider`, returning that set as `Event::ConnectionNeeded`s.

While this works fine in Rust, the `EventsProvider` interface
doesn't map well in bindings due to it taking a flexible trait impl
as a method argument.

Instead, here, we convert `OnionMessageHandler` to include a single
function which returns nodes in the form of a `node_id` and
`Vec<SocketAddress>`. This is a bit simpler, if less flexible, API,
and while largely equivalent, is easier to map in bindings.
  • Loading branch information
TheBlueMatt committed Jan 23, 2024
1 parent 9b338cc commit 780cbe9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 34 deletions.
31 changes: 12 additions & 19 deletions lightning-background-processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,10 @@ where
persister, chain_monitor,
chain_monitor.process_pending_events_async(async_event_handler).await,
channel_manager, channel_manager.process_pending_events_async(async_event_handler).await,
peer_manager, process_onion_message_handler_events_async(&peer_manager, async_event_handler).await,
peer_manager,
for event in onion_message_handler_events(peer_manager) {
handler(event).await
},
gossip_sync, logger, scorer, should_break, {
let fut = Selector {
a: channel_manager.get_event_or_persistence_needed_future(),
Expand All @@ -719,23 +722,11 @@ where
)
}

#[cfg(feature = "futures")]
async fn process_onion_message_handler_events_async<
EventHandlerFuture: core::future::Future<Output = ()>,
EventHandler: Fn(Event) -> EventHandlerFuture,
PM: 'static + Deref + Send + Sync,
>(
peer_manager: &PM, handler: EventHandler
)
where
PM::Target: APeerManager + Send + Sync,
{
let events = core::cell::RefCell::new(Vec::new());
peer_manager.onion_message_handler().process_pending_events(&|e| events.borrow_mut().push(e));

for event in events.into_inner() {
handler(event).await
}
fn onion_message_handler_events<PM: 'static + Deref + Send + Sync>(
peer_manager: &PM
) -> impl Iterator<Item=Event> where PM::Target: APeerManager + Send + Sync {
peer_manager.onion_message_handler().get_and_clear_connections_needed()
.into_iter().map(|(node_id, addresses)| Event::ConnectionNeeded { node_id, addresses })
}

#[cfg(feature = "std")]
Expand Down Expand Up @@ -851,7 +842,9 @@ impl BackgroundProcessor {
persister, chain_monitor, chain_monitor.process_pending_events(&event_handler),
channel_manager, channel_manager.process_pending_events(&event_handler),
peer_manager,
peer_manager.onion_message_handler().process_pending_events(&event_handler),
for event in onion_message_handler_events(&peer_manager) {
event_handler.handle_event(event);
},
gossip_sync, logger, scorer, stop_thread.load(Ordering::Acquire),
{ Sleeper::from_two_futures(
channel_manager.get_event_or_persistence_needed_future(),
Expand Down
11 changes: 10 additions & 1 deletion lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,16 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
}

/// A handler for received [`OnionMessage`]s and for providing generated ones to send.
pub trait OnionMessageHandler: EventsProvider {
pub trait OnionMessageHandler {
/// Because much of the lightning network does not yet support forwarding onion messages, we
/// may need to directly connect to a node which will forward a message for us. In such a case,
/// this method will return the set of nodes which need connection by node_id and the
/// corresponding socket addresses where they may accept incoming connections.
///
/// Thus, this method should be polled regularly to detect messages await such a direct
/// connection.
fn get_and_clear_connections_needed(&self) -> Vec<(PublicKey, Vec<SocketAddress>)>;

/// Handle an incoming `onion_message` message from the given peer.
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage);

Expand Down
1 change: 1 addition & 0 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl RoutingMessageHandler for IgnoringMessageHandler {
fn processing_queue_high(&self) -> bool { false }
}
impl OnionMessageHandler for IgnoringMessageHandler {
fn get_and_clear_connections_needed(&self) -> Vec<(PublicKey, Vec<SocketAddress>)> { Vec::new() }
fn handle_onion_message(&self, _their_node_id: &PublicKey, _msg: &msgs::OnionMessage) {}
fn next_onion_message_for_peer(&self, _peer_node_id: PublicKey) -> Option<msgs::OnionMessage> { None }
fn peer_connected(&self, _their_node_id: &PublicKey, _init: &msgs::Init, _inbound: bool) -> Result<(), ()> { Ok(()) }
Expand Down
19 changes: 5 additions & 14 deletions lightning/src/onion_message/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ fn outbound_buffer_full(peer_node_id: &PublicKey, buffer: &HashMap<PublicKey, On
false
}

impl<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref> EventsProvider
impl<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref> OnionMessageHandler
for OnionMessenger<ES, NS, L, MR, OMH, CMH>
where
ES::Target: EntropySource,
Expand All @@ -900,27 +900,18 @@ where
OMH::Target: OffersMessageHandler,
CMH::Target: CustomOnionMessageHandler,
{
fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler {
fn get_and_clear_connections_needed(&self) -> Vec<(PublicKey, Vec<SocketAddress>)> {
let mut res = Vec::new();
for (node_id, recipient) in self.message_recipients.lock().unwrap().iter_mut() {
if let OnionMessageRecipient::PendingConnection(_, addresses, _) = recipient {
if let Some(addresses) = addresses.take() {
handler.handle_event(Event::ConnectionNeeded { node_id: *node_id, addresses });
res.push((*node_id, addresses));
}
}
}
res
}
}

impl<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref> OnionMessageHandler
for OnionMessenger<ES, NS, L, MR, OMH, CMH>
where
ES::Target: EntropySource,
NS::Target: NodeSigner,
L::Target: Logger,
MR::Target: MessageRouter,
OMH::Target: OffersMessageHandler,
CMH::Target: CustomOnionMessageHandler,
{
fn handle_onion_message(&self, _peer_node_id: &PublicKey, msg: &OnionMessage) {
match peel_onion_message(
msg, &self.secp_ctx, &*self.node_signer, &*self.logger, &*self.custom_handler
Expand Down

0 comments on commit 780cbe9

Please sign in to comment.