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/?a=commitdiff_plain;h=0897e7fbb42596c9a890d5366b4b0b7df126b760;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 601ed296b..79493a697 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -704,7 +704,10 @@ where persister, chain_monitor, chain_monitor.process_pending_events_async(async_event_handler).await, channel_manager, channel_manager.get_cm().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_cm().get_event_or_persistence_needed_future(), @@ -729,23 +732,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")] @@ -852,7 +843,9 @@ impl BackgroundProcessor { persister, chain_monitor, chain_monitor.process_pending_events(&event_handler), channel_manager, channel_manager.get_cm().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_cm().get_event_or_persistence_needed_future(), diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 2de0397eb..7e42799bf 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -1624,7 +1624,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 3f20a07b3..e5b583a9c 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -124,6 +124,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 ade642957..6ddb5ddea 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -1006,7 +1006,7 @@ fn outbound_buffer_full(peer_node_id: &PublicKey, buffer: &HashMap EventsProvider +impl OnionMessageHandler for OnionMessenger where ES::Target: EntropySource, @@ -1017,28 +1017,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, - NL::Target: NodeIdLookUp, - MR::Target: MessageRouter, - OMH::Target: OffersMessageHandler, - CMH::Target: CustomOnionMessageHandler, -{ fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage) { let logger = WithContext::from(&self.logger, Some(*peer_node_id), None); match self.peel_onion_message(msg) {