Merge pull request #68 from jkczyz/2022-03-ldk-0-0-106
[ldk-c-bindings] / lightning-c-bindings / src / lightning / util / events.rs
index 1291bbcb27437f28366e9e76f30d7188e01d1ca4..02fd0edf07ec3f757d36b6d3b996472851408dbc 100644 (file)
 //! future, as well as generate and broadcast funding transactions handle payment preimages and a
 //! few other things.
 
-use std::str::FromStr;
-use std::ffi::c_void;
+use alloc::str::FromStr;
+use core::ffi::c_void;
 use core::convert::Infallible;
 use bitcoin::hashes::Hash;
 use crate::c_types::*;
+#[cfg(feature="no-std")]
+use alloc::{vec::Vec, boxed::Box};
 
 /// Some information provided on receipt of payment depends on whether the payment received is a
 /// spontaneous payment or a \"conventional\" lightning payment that's paying an invoice.
@@ -439,7 +441,7 @@ pub enum Event {
        /// Note that this does *not* indicate that all paths for an MPP payment have failed, see
        /// [`Event::PaymentFailed`] and [`all_paths_failed`].
        ///
-       /// [`all_paths_failed`]: Self::all_paths_failed
+       /// [`all_paths_failed`]: Self::PaymentPathFailed::all_paths_failed
        PaymentPathFailed {
                /// The id returned by [`ChannelManager::send_payment`] and used with
                /// [`ChannelManager::retry_payment`] and [`ChannelManager::abandon_payment`].
@@ -569,11 +571,15 @@ pub enum Event {
                /// The channel_id of the channel which has been closed. Note that on-chain transactions
                /// resolving the channel are likely still awaiting confirmation.
                channel_id: crate::c_types::ThirtyTwoBytes,
-               /// The `user_channel_id` value passed in to [`ChannelManager::create_channel`], or 0 for
-               /// an inbound channel. This will always be zero for objects serialized with LDK versions
-               /// prior to 0.0.102.
+               /// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
+               /// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
+               /// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
+               /// `user_channel_id` will be 0 for an inbound channel.
+               /// This will always be zero for objects serialized with LDK versions prior to 0.0.102.
                ///
                /// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
+               /// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
+               /// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
                user_channel_id: u64,
                /// The reason the channel was closed.
                reason: crate::lightning::util::events::ClosureReason,
@@ -608,6 +614,44 @@ pub enum Event {
                /// May contain a closed channel if the HTLC sent along the path was fulfilled on chain.
                path: crate::c_types::derived::CVec_RouteHopZ,
        },
+       /// Indicates a request to open a new channel by a peer.
+       ///
+       /// To accept the request, call [`ChannelManager::accept_inbound_channel`]. To reject the
+       /// request, call [`ChannelManager::force_close_channel`].
+       ///
+       /// The event is only triggered when a new open channel request is received and the
+       /// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true.
+       ///
+       /// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
+       /// [`ChannelManager::force_close_channel`]: crate::ln::channelmanager::ChannelManager::force_close_channel
+       /// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
+       OpenChannelRequest {
+               /// The temporary channel ID of the channel requested to be opened.
+               ///
+               /// When responding to the request, the `temporary_channel_id` should be passed
+               /// back to the ChannelManager with [`ChannelManager::accept_inbound_channel`] to accept,
+               /// or to [`ChannelManager::force_close_channel`] to reject.
+               ///
+               /// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
+               /// [`ChannelManager::force_close_channel`]: crate::ln::channelmanager::ChannelManager::force_close_channel
+               temporary_channel_id: crate::c_types::ThirtyTwoBytes,
+               /// The node_id of the counterparty requesting to open the channel.
+               counterparty_node_id: crate::c_types::PublicKey,
+               /// The channel value of the requested channel.
+               funding_satoshis: u64,
+               /// Our starting balance in the channel if the request is accepted, in milli-satoshi.
+               push_msat: u64,
+               /// The features that this channel will operate with. If you reject the channel, a
+               /// well-behaved counterparty may automatically re-attempt the channel with a new set of
+               /// feature flags.
+               ///
+               /// Note that if [`ChannelTypeFeatures::supports_scid_privacy`] returns true on this type,
+               /// the resulting [`ChannelManager`] will not be readable by versions of LDK prior to
+               /// 0.0.106.
+               ///
+               /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
+               channel_type: crate::lightning::ln::features::ChannelTypeFeatures,
+       },
 }
 use lightning::util::events::Event as nativeEvent;
 impl Event {
@@ -686,7 +730,7 @@ impl Event {
                        Event::PendingHTLCsForwardable {ref time_forwardable, } => {
                                let mut time_forwardable_nonref = (*time_forwardable).clone();
                                nativeEvent::PendingHTLCsForwardable {
-                                       time_forwardable: std::time::Duration::from_secs(time_forwardable_nonref),
+                                       time_forwardable: core::time::Duration::from_secs(time_forwardable_nonref),
                                }
                        },
                        Event::SpendableOutputs {ref outputs, } => {
@@ -735,6 +779,20 @@ impl Event {
                                        path: local_path_nonref,
                                }
                        },
+                       Event::OpenChannelRequest {ref temporary_channel_id, ref counterparty_node_id, ref funding_satoshis, ref push_msat, ref channel_type, } => {
+                               let mut temporary_channel_id_nonref = (*temporary_channel_id).clone();
+                               let mut counterparty_node_id_nonref = (*counterparty_node_id).clone();
+                               let mut funding_satoshis_nonref = (*funding_satoshis).clone();
+                               let mut push_msat_nonref = (*push_msat).clone();
+                               let mut channel_type_nonref = (*channel_type).clone();
+                               nativeEvent::OpenChannelRequest {
+                                       temporary_channel_id: temporary_channel_id_nonref.data,
+                                       counterparty_node_id: counterparty_node_id_nonref.into_rust(),
+                                       funding_satoshis: funding_satoshis_nonref,
+                                       push_msat: push_msat_nonref,
+                                       channel_type: *unsafe { Box::from_raw(channel_type_nonref.take_inner()) },
+                               }
+                       },
                }
        }
        #[allow(unused)]
@@ -790,7 +848,7 @@ impl Event {
                        },
                        Event::PendingHTLCsForwardable {mut time_forwardable, } => {
                                nativeEvent::PendingHTLCsForwardable {
-                                       time_forwardable: std::time::Duration::from_secs(time_forwardable),
+                                       time_forwardable: core::time::Duration::from_secs(time_forwardable),
                                }
                        },
                        Event::SpendableOutputs {mut outputs, } => {
@@ -828,6 +886,15 @@ impl Event {
                                        path: local_path,
                                }
                        },
+                       Event::OpenChannelRequest {mut temporary_channel_id, mut counterparty_node_id, mut funding_satoshis, mut push_msat, mut channel_type, } => {
+                               nativeEvent::OpenChannelRequest {
+                                       temporary_channel_id: temporary_channel_id.data,
+                                       counterparty_node_id: counterparty_node_id.into_rust(),
+                                       funding_satoshis: funding_satoshis,
+                                       push_msat: push_msat,
+                                       channel_type: *unsafe { Box::from_raw(channel_type.take_inner()) },
+                               }
+                       },
                }
        }
        #[allow(unused)]
@@ -882,7 +949,7 @@ impl Event {
                                let mut short_channel_id_nonref = (*short_channel_id).clone();
                                let mut local_short_channel_id_nonref = if short_channel_id_nonref.is_none() { crate::c_types::derived::COption_u64Z::None } else { crate::c_types::derived::COption_u64Z::Some( { short_channel_id_nonref.unwrap() }) };
                                let mut retry_nonref = (*retry).clone();
-                               let mut local_retry_nonref = crate::lightning::routing::router::RouteParameters { inner: if retry_nonref.is_none() { std::ptr::null_mut() } else {  { ObjOps::heap_alloc((retry_nonref.unwrap())) } }, is_owned: true };
+                               let mut local_retry_nonref = crate::lightning::routing::router::RouteParameters { inner: if retry_nonref.is_none() { core::ptr::null_mut() } else {  { ObjOps::heap_alloc((retry_nonref.unwrap())) } }, is_owned: true };
                                Event::PaymentPathFailed {
                                        payment_id: local_payment_id_nonref,
                                        payment_hash: crate::c_types::ThirtyTwoBytes { data: payment_hash_nonref.0 },
@@ -954,6 +1021,20 @@ impl Event {
                                        path: local_path_nonref.into(),
                                }
                        },
+                       nativeEvent::OpenChannelRequest {ref temporary_channel_id, ref counterparty_node_id, ref funding_satoshis, ref push_msat, ref channel_type, } => {
+                               let mut temporary_channel_id_nonref = (*temporary_channel_id).clone();
+                               let mut counterparty_node_id_nonref = (*counterparty_node_id).clone();
+                               let mut funding_satoshis_nonref = (*funding_satoshis).clone();
+                               let mut push_msat_nonref = (*push_msat).clone();
+                               let mut channel_type_nonref = (*channel_type).clone();
+                               Event::OpenChannelRequest {
+                                       temporary_channel_id: crate::c_types::ThirtyTwoBytes { data: temporary_channel_id_nonref },
+                                       counterparty_node_id: crate::c_types::PublicKey::from_rust(&counterparty_node_id_nonref),
+                                       funding_satoshis: funding_satoshis_nonref,
+                                       push_msat: push_msat_nonref,
+                                       channel_type: crate::lightning::ln::features::ChannelTypeFeatures { inner: ObjOps::heap_alloc(channel_type_nonref), is_owned: true },
+                               }
+                       },
                }
        }
        #[allow(unused)]
@@ -989,7 +1070,7 @@ impl Event {
                                let mut local_network_update = if network_update.is_none() { crate::c_types::derived::COption_NetworkUpdateZ::None } else { crate::c_types::derived::COption_NetworkUpdateZ::Some( { crate::lightning::routing::network_graph::NetworkUpdate::native_into(network_update.unwrap()) }) };
                                let mut local_path = Vec::new(); for mut item in path.drain(..) { local_path.push( { crate::lightning::routing::router::RouteHop { inner: ObjOps::heap_alloc(item), is_owned: true } }); };
                                let mut local_short_channel_id = if short_channel_id.is_none() { crate::c_types::derived::COption_u64Z::None } else { crate::c_types::derived::COption_u64Z::Some( { short_channel_id.unwrap() }) };
-                               let mut local_retry = crate::lightning::routing::router::RouteParameters { inner: if retry.is_none() { std::ptr::null_mut() } else {  { ObjOps::heap_alloc((retry.unwrap())) } }, is_owned: true };
+                               let mut local_retry = crate::lightning::routing::router::RouteParameters { inner: if retry.is_none() { core::ptr::null_mut() } else {  { ObjOps::heap_alloc((retry.unwrap())) } }, is_owned: true };
                                Event::PaymentPathFailed {
                                        payment_id: local_payment_id,
                                        payment_hash: crate::c_types::ThirtyTwoBytes { data: payment_hash.0 },
@@ -1047,6 +1128,15 @@ impl Event {
                                        path: local_path.into(),
                                }
                        },
+                       nativeEvent::OpenChannelRequest {mut temporary_channel_id, mut counterparty_node_id, mut funding_satoshis, mut push_msat, mut channel_type, } => {
+                               Event::OpenChannelRequest {
+                                       temporary_channel_id: crate::c_types::ThirtyTwoBytes { data: temporary_channel_id },
+                                       counterparty_node_id: crate::c_types::PublicKey::from_rust(&counterparty_node_id),
+                                       funding_satoshis: funding_satoshis,
+                                       push_msat: push_msat,
+                                       channel_type: crate::lightning::ln::features::ChannelTypeFeatures { inner: ObjOps::heap_alloc(channel_type), is_owned: true },
+                               }
+                       },
                }
        }
 }
@@ -1158,6 +1248,17 @@ pub extern "C" fn Event_payment_path_successful(payment_id: crate::c_types::Thir
        }
 }
 #[no_mangle]
+/// Utility method to constructs a new OpenChannelRequest-variant Event
+pub extern "C" fn Event_open_channel_request(temporary_channel_id: crate::c_types::ThirtyTwoBytes, counterparty_node_id: crate::c_types::PublicKey, funding_satoshis: u64, push_msat: u64, channel_type: crate::lightning::ln::features::ChannelTypeFeatures) -> Event {
+       Event::OpenChannelRequest {
+               temporary_channel_id,
+               counterparty_node_id,
+               funding_satoshis,
+               push_msat,
+               channel_type,
+       }
+}
+#[no_mangle]
 /// Serialize the Event object into a byte array which can be read by Event_read
 pub extern "C" fn Event_write(obj: &Event) -> crate::c_types::derived::CVec_u8Z {
        crate::c_types::serialize_obj(&unsafe { &*obj }.to_native())
@@ -1319,6 +1420,14 @@ pub enum MessageSendEvent {
                /// The reply_channel_range which should be sent.
                msg: crate::lightning::ln::msgs::ReplyChannelRange,
        },
+       /// Sends a timestamp filter for inbound gossip. This should be sent on each new connection to
+       /// enable receiving gossip messages from the peer.
+       SendGossipTimestampFilter {
+               /// The node_id of this message recipient
+               node_id: crate::c_types::PublicKey,
+               /// The gossip_timestamp_filter which should be sent.
+               msg: crate::lightning::ln::msgs::GossipTimestampFilter,
+       },
 }
 use lightning::util::events::MessageSendEvent as nativeMessageSendEvent;
 impl MessageSendEvent {
@@ -1473,6 +1582,14 @@ impl MessageSendEvent {
                                        msg: *unsafe { Box::from_raw(msg_nonref.take_inner()) },
                                }
                        },
+                       MessageSendEvent::SendGossipTimestampFilter {ref node_id, ref msg, } => {
+                               let mut node_id_nonref = (*node_id).clone();
+                               let mut msg_nonref = (*msg).clone();
+                               nativeMessageSendEvent::SendGossipTimestampFilter {
+                                       node_id: node_id_nonref.into_rust(),
+                                       msg: *unsafe { Box::from_raw(msg_nonref.take_inner()) },
+                               }
+                       },
                }
        }
        #[allow(unused)]
@@ -1590,6 +1707,12 @@ impl MessageSendEvent {
                                        msg: *unsafe { Box::from_raw(msg.take_inner()) },
                                }
                        },
+                       MessageSendEvent::SendGossipTimestampFilter {mut node_id, mut msg, } => {
+                               nativeMessageSendEvent::SendGossipTimestampFilter {
+                                       node_id: node_id.into_rust(),
+                                       msg: *unsafe { Box::from_raw(msg.take_inner()) },
+                               }
+                       },
                }
        }
        #[allow(unused)]
@@ -1743,6 +1866,14 @@ impl MessageSendEvent {
                                        msg: crate::lightning::ln::msgs::ReplyChannelRange { inner: ObjOps::heap_alloc(msg_nonref), is_owned: true },
                                }
                        },
+                       nativeMessageSendEvent::SendGossipTimestampFilter {ref node_id, ref msg, } => {
+                               let mut node_id_nonref = (*node_id).clone();
+                               let mut msg_nonref = (*msg).clone();
+                               MessageSendEvent::SendGossipTimestampFilter {
+                                       node_id: crate::c_types::PublicKey::from_rust(&node_id_nonref),
+                                       msg: crate::lightning::ln::msgs::GossipTimestampFilter { inner: ObjOps::heap_alloc(msg_nonref), is_owned: true },
+                               }
+                       },
                }
        }
        #[allow(unused)]
@@ -1860,6 +1991,12 @@ impl MessageSendEvent {
                                        msg: crate::lightning::ln::msgs::ReplyChannelRange { inner: ObjOps::heap_alloc(msg), is_owned: true },
                                }
                        },
+                       nativeMessageSendEvent::SendGossipTimestampFilter {mut node_id, mut msg, } => {
+                               MessageSendEvent::SendGossipTimestampFilter {
+                                       node_id: crate::c_types::PublicKey::from_rust(&node_id),
+                                       msg: crate::lightning::ln::msgs::GossipTimestampFilter { inner: ObjOps::heap_alloc(msg), is_owned: true },
+                               }
+                       },
                }
        }
 }
@@ -2021,6 +2158,14 @@ pub extern "C" fn MessageSendEvent_send_reply_channel_range(node_id: crate::c_ty
                msg,
        }
 }
+#[no_mangle]
+/// Utility method to constructs a new SendGossipTimestampFilter-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_gossip_timestamp_filter(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::GossipTimestampFilter) -> MessageSendEvent {
+       MessageSendEvent::SendGossipTimestampFilter {
+               node_id,
+               msg,
+       }
+}
 /// A trait indicating an object may generate message send events
 #[repr(C)]
 pub struct MessageSendEventsProvider {
@@ -2057,7 +2202,7 @@ impl rustMessageSendEventsProvider for MessageSendEventsProvider {
 
 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
 // directly as a Deref trait in higher-level structs:
-impl std::ops::Deref for MessageSendEventsProvider {
+impl core::ops::Deref for MessageSendEventsProvider {
        type Target = Self;
        fn deref(&self) -> &Self {
                self
@@ -2168,7 +2313,7 @@ impl rustEventHandler for EventHandler {
 
 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
 // directly as a Deref trait in higher-level structs:
-impl std::ops::Deref for EventHandler {
+impl core::ops::Deref for EventHandler {
        type Target = Self;
        fn deref(&self) -> &Self {
                self