From: Matt Corallo Date: Sun, 17 Dec 2023 19:43:28 +0000 (+0000) Subject: Replace `EventsProvider` on `OnionMessageHandler` with a single fn X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=06066446620c7f9d70614adfce5c88891c1042f6;p=rust-lightning Replace `EventsProvider` on `OnionMessageHandler` with a single fn `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`. This is a bit simpler, if less flexible, API, and while largely equivalent, is easier to map in bindings. --- diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index ddedc9dac..dd4eea960 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -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(), @@ -719,23 +722,11 @@ where ) } -#[cfg(feature = "futures")] -async fn process_onion_message_handler_events_async< - EventHandlerFuture: core::future::Future, - 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( + peer_manager: &PM +) -> impl Iterator 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")] @@ -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(), diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index e833e3ebf..c5763d08d 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -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)>; + /// Handle an incoming `onion_message` message from the given peer. fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage); diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 77208921c..3ad75ccce 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -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)> { 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 { None } fn peer_connected(&self, _their_node_id: &PublicKey, _init: &msgs::Init, _inbound: bool) -> Result<(), ()> { Ok(()) } diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 5554497a4..a0e674fa4 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -890,7 +890,7 @@ fn outbound_buffer_full(peer_node_id: &PublicKey, buffer: &HashMap EventsProvider +impl OnionMessageHandler for OnionMessenger where ES::Target: EntropySource, @@ -900,27 +900,18 @@ where OMH::Target: OffersMessageHandler, CMH::Target: CustomOnionMessageHandler, { - fn process_pending_events(&self, handler: H) where H::Target: EventHandler { + fn get_and_clear_connections_needed(&self) -> Vec<(PublicKey, Vec)> { + 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 OnionMessageHandler -for OnionMessenger -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