Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

liquidity: Allow setting process_events callback in c_bindings #3533

Conversation

tnull
Copy link
Contributor

@tnull tnull commented Jan 14, 2025

To trigger message processing, we previously had the user set a callback to PeerManager::process_events via an Fn() callback. This is however not supported by c_bindings.

Here, we therefore introduce as ProcessMesssagesCallback trait that can be used via LiquidityManager::set_process_msgs_callback_fn, which is exposed in c_bindings.

(Also confirmed to work with LDK Node)

@tnull tnull force-pushed the 2025-01-allow-setting-process-callback-in-c-bindings branch from b80059d to c6abf09 Compare January 14, 2025 10:24
@TheBlueMatt TheBlueMatt mentioned this pull request Jan 14, 2025
/// Hence we simply allow setting a callback function that will be set via
/// [`Self::set_process_msgs_callback`] internally.
#[cfg(c_bindings)]
pub fn set_process_msgs_callback_fn<F: 'static + ProcessMessagesCallback>(&self, callback: F) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference here compared to just leaving only this for Rust users? If we require ownership anyway, the caller probably doesn't have the callback in a Box to begin with, so us doing it for them even in Rust seems fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhh, good point. I basically followed the recipe in Future/FutureCallback. I guess there we could also consider dropping the trait version in favor of the _fn version?

To trigger message processing, we previously had the user set a callback
to `PeerManager::process_events` via an `Fn()` callback. This is however
not supported by `c_bindings`.

Here, we therefore introduce as `ProcessMesssagesCallback` trait that
can be used via `LiquidityManager::set_process_msgs_callback_fn`, which
is exposed in `c_bindings`.
@tnull tnull force-pushed the 2025-01-allow-setting-process-callback-in-c-bindings branch from c6abf09 to e05b76a Compare January 15, 2025 08:58
@tnull
Copy link
Contributor Author

tnull commented Jan 15, 2025

Took the liberty to force-push with these additional changes (to avoid another review roundtrip for the squashing):

> git
diff-tree -U2 c6abf0902 e05b76af7
diff --git a/lightning-liquidity/src/manager.rs b/lightning-liquidity/src/manager.rs
index 6b6b146af..a4c130333 100644
--- a/lightning-liquidity/src/manager.rs
+++ b/lightning-liquidity/src/manager.rs
@@ -311,25 +311,11 @@ where {
        /// let process_msgs_callback = move || process_msgs_pm.process_events();
        ///
-       /// my_liquidity_manager.set_process_msgs_callback(Box::new(process_msgs_callback));
+       /// my_liquidity_manager.set_process_msgs_callback(process_msgs_callback);
        /// # }
        /// ```
        ///
        /// [`PeerManager::process_events`]: lightning::ln::peer_handler::PeerManager::process_events
-       pub fn set_process_msgs_callback(&self, callback: Box<dyn ProcessMessagesCallback>) {
-               self.pending_messages.set_process_msgs_callback(callback);
-       }
-
-       /// Allows to set a callback that will be called after new messages are pushed to the message
-       /// queue.
-       ///
-       /// C bindings don't (currently) know how to map `Box<dyn Trait>`, and while it could add the
-       /// following wrapper, doing it in the bindings is currently much more work than simply doing it
-       /// here.
-       ///
-       /// Hence we simply allow setting a callback function that will be set via
-       /// [`Self::set_process_msgs_callback`] internally.
-       #[cfg(c_bindings)]
-       pub fn set_process_msgs_callback_fn<F: 'static + ProcessMessagesCallback>(&self, callback: F) {
-               self.set_process_msgs_callback(Box::new(callback));
+       pub fn set_process_msgs_callback<F: 'static + ProcessMessagesCallback>(&self, callback: F) {
+               self.pending_messages.set_process_msgs_callback(Box::new(callback));
        }

diff --git a/lightning-liquidity/tests/common/mod.rs b/lightning-liquidity/tests/common/mod.rs
index faf4cc5a6..8b8507a9f 100644
--- a/lightning-liquidity/tests/common/mod.rs
+++ b/lightning-liquidity/tests/common/mod.rs
@@ -479,8 +479,5 @@ pub(crate) fn create_liquidity_node(
        let process_msgs_flag = Arc::clone(&check_msgs_processed);
        let process_msgs_callback = move || process_msgs_flag.store(true, Ordering::Release);
-       #[cfg(not(c_bindings))]
-       liquidity_manager.set_process_msgs_callback(Box::new(process_msgs_callback));
-       #[cfg(c_bindings)]
-       liquidity_manager.set_process_msgs_callback_fn(process_msgs_callback);
+       liquidity_manager.set_process_msgs_callback(process_msgs_callback);

        Node {

Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super straightforward, just gonna land this.

@TheBlueMatt TheBlueMatt merged commit bcbff65 into lightningdevkit:main Jan 15, 2025
25 checks passed
@TheBlueMatt
Copy link
Collaborator

Backported in #3536.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants