///
/// [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
/// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
+ ///
+ /// Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None
payment_preimage: crate::c_types::ThirtyTwoBytes,
/// The \"payment secret\". This authenticates the sender to the recipient, preventing a
/// number of deanonymization attacks during the routing process.
},
/// Indicates an outbound payment we made succeeded (ie it made it all the way to its target
/// and we got back the payment preimage for it).
- /// Note that duplicative PaymentSent Events may be generated - it is your responsibility to
- /// deduplicate them by payment_preimage (which MUST be unique)!
PaymentSent {
/// The preimage to the hash given to ChannelManager::send_payment.
/// Note that this serves as a payment receipt, if you wish to have such a thing, you must
},
/// Indicates an outbound payment we made failed. Probably some intermediary node dropped
/// something. You may wish to retry with a different route.
- /// Note that duplicative PaymentFailed Events may be generated - it is your responsibility to
- /// deduplicate them by payment_hash (which MUST be unique)!
PaymentFailed {
/// The hash which was given to ChannelManager::send_payment.
payment_hash: crate::c_types::ThirtyTwoBytes,
/// now + 5*time_forwardable).
time_forwardable: u64,
},
- /// Used to indicate that an output was generated on-chain which you should know how to spend.
+ /// Used to indicate that an output which you should know how to spend was confirmed on chain
+ /// and is now spendable.
/// Such an output will *not* ever be spent by rust-lightning, and are not at risk of your
/// counterparty spending them due to some kind of timeout. Thus, you need to store them
/// somewhere and spend them when you create on-chain transactions.
orig.clone()
}
#[no_mangle]
+/// Utility method to constructs a new FundingGenerationReady-variant Event
+pub extern "C" fn Event_funding_generation_ready(temporary_channel_id: crate::c_types::ThirtyTwoBytes, channel_value_satoshis: u64, output_script: crate::c_types::derived::CVec_u8Z, user_channel_id: u64) -> Event {
+ Event::FundingGenerationReady {
+ temporary_channel_id,
+ channel_value_satoshis,
+ output_script,
+ user_channel_id,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new PaymentReceived-variant Event
+pub extern "C" fn Event_payment_received(payment_hash: crate::c_types::ThirtyTwoBytes, payment_preimage: crate::c_types::ThirtyTwoBytes, payment_secret: crate::c_types::ThirtyTwoBytes, amt: u64, user_payment_id: u64) -> Event {
+ Event::PaymentReceived {
+ payment_hash,
+ payment_preimage,
+ payment_secret,
+ amt,
+ user_payment_id,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new PaymentSent-variant Event
+pub extern "C" fn Event_payment_sent(payment_preimage: crate::c_types::ThirtyTwoBytes) -> Event {
+ Event::PaymentSent {
+ payment_preimage,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new PaymentFailed-variant Event
+pub extern "C" fn Event_payment_failed(payment_hash: crate::c_types::ThirtyTwoBytes, rejected_by_dest: bool) -> Event {
+ Event::PaymentFailed {
+ payment_hash,
+ rejected_by_dest,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new PendingHTLCsForwardable-variant Event
+pub extern "C" fn Event_pending_htlcs_forwardable(time_forwardable: u64) -> Event {
+ Event::PendingHTLCsForwardable {
+ time_forwardable,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SpendableOutputs-variant Event
+pub extern "C" fn Event_spendable_outputs(outputs: crate::c_types::derived::CVec_SpendableOutputDescriptorZ) -> Event {
+ Event::SpendableOutputs {
+ outputs,
+ }
+}
+#[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())
/// The channel_update which should be sent.
msg: crate::lightning::ln::msgs::ChannelUpdate,
},
+ /// Used to indicate that a channel_update should be sent to a single peer.
+ /// In contrast to [`Self::BroadcastChannelUpdate`], this is used when the channel is a
+ /// private channel and we shouldn't be informing all of our peers of channel parameters.
+ SendChannelUpdate {
+ /// The node_id of the node which should receive this message
+ node_id: crate::c_types::PublicKey,
+ /// The channel_update which should be sent.
+ msg: crate::lightning::ln::msgs::ChannelUpdate,
+ },
/// Broadcast an error downstream to be handled
HandleError {
/// The node_id of the node which should receive this message
msg: *unsafe { Box::from_raw(msg_nonref.take_inner()) },
}
},
+ MessageSendEvent::SendChannelUpdate {ref node_id, ref msg, } => {
+ let mut node_id_nonref = (*node_id).clone();
+ let mut msg_nonref = (*msg).clone();
+ nativeMessageSendEvent::SendChannelUpdate {
+ node_id: node_id_nonref.into_rust(),
+ msg: *unsafe { Box::from_raw(msg_nonref.take_inner()) },
+ }
+ },
MessageSendEvent::HandleError {ref node_id, ref action, } => {
let mut node_id_nonref = (*node_id).clone();
let mut action_nonref = (*action).clone();
msg: *unsafe { Box::from_raw(msg.take_inner()) },
}
},
+ MessageSendEvent::SendChannelUpdate {mut node_id, mut msg, } => {
+ nativeMessageSendEvent::SendChannelUpdate {
+ node_id: node_id.into_rust(),
+ msg: *unsafe { Box::from_raw(msg.take_inner()) },
+ }
+ },
MessageSendEvent::HandleError {mut node_id, mut action, } => {
nativeMessageSendEvent::HandleError {
node_id: node_id.into_rust(),
msg: crate::lightning::ln::msgs::ChannelUpdate { inner: Box::into_raw(Box::new(msg_nonref)), is_owned: true },
}
},
+ nativeMessageSendEvent::SendChannelUpdate {ref node_id, ref msg, } => {
+ let mut node_id_nonref = (*node_id).clone();
+ let mut msg_nonref = (*msg).clone();
+ MessageSendEvent::SendChannelUpdate {
+ node_id: crate::c_types::PublicKey::from_rust(&node_id_nonref),
+ msg: crate::lightning::ln::msgs::ChannelUpdate { inner: Box::into_raw(Box::new(msg_nonref)), is_owned: true },
+ }
+ },
nativeMessageSendEvent::HandleError {ref node_id, ref action, } => {
let mut node_id_nonref = (*node_id).clone();
let mut action_nonref = (*action).clone();
msg: crate::lightning::ln::msgs::ChannelUpdate { inner: Box::into_raw(Box::new(msg)), is_owned: true },
}
},
+ nativeMessageSendEvent::SendChannelUpdate {mut node_id, mut msg, } => {
+ MessageSendEvent::SendChannelUpdate {
+ node_id: crate::c_types::PublicKey::from_rust(&node_id),
+ msg: crate::lightning::ln::msgs::ChannelUpdate { inner: Box::into_raw(Box::new(msg)), is_owned: true },
+ }
+ },
nativeMessageSendEvent::HandleError {mut node_id, mut action, } => {
MessageSendEvent::HandleError {
node_id: crate::c_types::PublicKey::from_rust(&node_id),
pub extern "C" fn MessageSendEvent_clone(orig: &MessageSendEvent) -> MessageSendEvent {
orig.clone()
}
+#[no_mangle]
+/// Utility method to constructs a new SendAcceptChannel-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_accept_channel(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::AcceptChannel) -> MessageSendEvent {
+ MessageSendEvent::SendAcceptChannel {
+ node_id,
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SendOpenChannel-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_open_channel(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::OpenChannel) -> MessageSendEvent {
+ MessageSendEvent::SendOpenChannel {
+ node_id,
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SendFundingCreated-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_funding_created(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::FundingCreated) -> MessageSendEvent {
+ MessageSendEvent::SendFundingCreated {
+ node_id,
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SendFundingSigned-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_funding_signed(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::FundingSigned) -> MessageSendEvent {
+ MessageSendEvent::SendFundingSigned {
+ node_id,
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SendFundingLocked-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_funding_locked(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::FundingLocked) -> MessageSendEvent {
+ MessageSendEvent::SendFundingLocked {
+ node_id,
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SendAnnouncementSignatures-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_announcement_signatures(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::AnnouncementSignatures) -> MessageSendEvent {
+ MessageSendEvent::SendAnnouncementSignatures {
+ node_id,
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new UpdateHTLCs-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_update_htlcs(node_id: crate::c_types::PublicKey, updates: crate::lightning::ln::msgs::CommitmentUpdate) -> MessageSendEvent {
+ MessageSendEvent::UpdateHTLCs {
+ node_id,
+ updates,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SendRevokeAndACK-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_revoke_and_ack(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::RevokeAndACK) -> MessageSendEvent {
+ MessageSendEvent::SendRevokeAndACK {
+ node_id,
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SendClosingSigned-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_closing_signed(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::ClosingSigned) -> MessageSendEvent {
+ MessageSendEvent::SendClosingSigned {
+ node_id,
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SendShutdown-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_shutdown(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::Shutdown) -> MessageSendEvent {
+ MessageSendEvent::SendShutdown {
+ node_id,
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SendChannelReestablish-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_channel_reestablish(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::ChannelReestablish) -> MessageSendEvent {
+ MessageSendEvent::SendChannelReestablish {
+ node_id,
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new BroadcastChannelAnnouncement-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_broadcast_channel_announcement(msg: crate::lightning::ln::msgs::ChannelAnnouncement, update_msg: crate::lightning::ln::msgs::ChannelUpdate) -> MessageSendEvent {
+ MessageSendEvent::BroadcastChannelAnnouncement {
+ msg,
+ update_msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new BroadcastNodeAnnouncement-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_broadcast_node_announcement(msg: crate::lightning::ln::msgs::NodeAnnouncement) -> MessageSendEvent {
+ MessageSendEvent::BroadcastNodeAnnouncement {
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new BroadcastChannelUpdate-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_broadcast_channel_update(msg: crate::lightning::ln::msgs::ChannelUpdate) -> MessageSendEvent {
+ MessageSendEvent::BroadcastChannelUpdate {
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SendChannelUpdate-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_channel_update(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::ChannelUpdate) -> MessageSendEvent {
+ MessageSendEvent::SendChannelUpdate {
+ node_id,
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new HandleError-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_handle_error(node_id: crate::c_types::PublicKey, action: crate::lightning::ln::msgs::ErrorAction) -> MessageSendEvent {
+ MessageSendEvent::HandleError {
+ node_id,
+ action,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new PaymentFailureNetworkUpdate-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_payment_failure_network_update(update: crate::lightning::ln::msgs::HTLCFailChannelUpdate) -> MessageSendEvent {
+ MessageSendEvent::PaymentFailureNetworkUpdate {
+ update,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SendChannelRangeQuery-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_channel_range_query(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::QueryChannelRange) -> MessageSendEvent {
+ MessageSendEvent::SendChannelRangeQuery {
+ node_id,
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SendShortIdsQuery-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_short_ids_query(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::QueryShortChannelIds) -> MessageSendEvent {
+ MessageSendEvent::SendShortIdsQuery {
+ node_id,
+ msg,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new SendReplyChannelRange-variant MessageSendEvent
+pub extern "C" fn MessageSendEvent_send_reply_channel_range(node_id: crate::c_types::PublicKey, msg: crate::lightning::ln::msgs::ReplyChannelRange) -> MessageSendEvent {
+ MessageSendEvent::SendReplyChannelRange {
+ node_id,
+ msg,
+ }
+}
/// A trait indicating an object may generate message send events
#[repr(C)]
pub struct MessageSendEventsProvider {
/// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
}
+unsafe impl Send for MessageSendEventsProvider {}
+unsafe impl Sync for MessageSendEventsProvider {}
+#[no_mangle]
+pub(crate) extern "C" fn MessageSendEventsProvider_clone_fields(orig: &MessageSendEventsProvider) -> MessageSendEventsProvider {
+ MessageSendEventsProvider {
+ this_arg: orig.this_arg,
+ get_and_clear_pending_msg_events: Clone::clone(&orig.get_and_clear_pending_msg_events),
+ free: Clone::clone(&orig.free),
+ }
+}
use lightning::util::events::MessageSendEventsProvider as rustMessageSendEventsProvider;
impl rustMessageSendEventsProvider for MessageSendEventsProvider {
}
}
}
-/// A trait indicating an object may generate events
+/// A trait indicating an object may generate events.
+///
+/// Events are processed by passing an [`EventHandler`] to [`process_pending_events`].
+///
+/// # Requirements
+///
+/// See [`process_pending_events`] for requirements around event processing.
+///
+/// When using this trait, [`process_pending_events`] will call [`handle_event`] for each pending
+/// event since the last invocation. The handler must either act upon the event immediately
+/// or preserve it for later handling.
+///
+/// Note, handlers may call back into the provider and thus deadlocking must be avoided. Be sure to
+/// consult the provider's documentation on the implication of processing events and how a handler
+/// may safely use the provider (e.g., see [`ChannelManager::process_pending_events`] and
+/// [`ChainMonitor::process_pending_events`]).
+///
+/// (C-not implementable) As there is likely no reason for a user to implement this trait on their
+/// own type(s).
+///
+/// [`process_pending_events`]: Self::process_pending_events
+/// [`handle_event`]: EventHandler::handle_event
+/// [`ChannelManager::process_pending_events`]: crate::ln::channelmanager::ChannelManager#method.process_pending_events
+/// [`ChainMonitor::process_pending_events`]: crate::chain::chainmonitor::ChainMonitor#method.process_pending_events
#[repr(C)]
pub struct EventsProvider {
/// An opaque pointer which is passed to your function implementations as an argument.
/// This has no meaning in the LDK, and can be NULL or any other value.
pub this_arg: *mut c_void,
- /// Gets the list of pending events which were generated by previous actions, clearing the list
- /// in the process.
- #[must_use]
- pub get_and_clear_pending_events: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_EventZ,
+ /// Processes any events generated since the last call using the given event handler.
+ ///
+ /// Subsequent calls must only process new events. However, handlers must be capable of handling
+ /// duplicate events across process restarts. This may occur if the provider was recovered from
+ /// an old state (i.e., it hadn't been successfully persisted after processing pending events).
+ pub process_pending_events: extern "C" fn (this_arg: *const c_void, handler: crate::lightning::util::events::EventHandler),
/// Frees any resources associated with this object given its this_arg pointer.
/// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
}
+unsafe impl Send for EventsProvider {}
+unsafe impl Sync for EventsProvider {}
+#[no_mangle]
+pub(crate) extern "C" fn EventsProvider_clone_fields(orig: &EventsProvider) -> EventsProvider {
+ EventsProvider {
+ this_arg: orig.this_arg,
+ process_pending_events: Clone::clone(&orig.process_pending_events),
+ free: Clone::clone(&orig.free),
+ }
+}
use lightning::util::events::EventsProvider as rustEventsProvider;
-impl rustEventsProvider for EventsProvider {
- fn get_and_clear_pending_events(&self) -> Vec<lightning::util::events::Event> {
- let mut ret = (self.get_and_clear_pending_events)(self.this_arg);
- let mut local_ret = Vec::new(); for mut item in ret.into_rust().drain(..) { local_ret.push( { item.into_native() }); };
- local_ret
+/// Calls the free function if one is set
+#[no_mangle]
+pub extern "C" fn EventsProvider_free(this_ptr: EventsProvider) { }
+impl Drop for EventsProvider {
+ fn drop(&mut self) {
+ if let Some(f) = self.free {
+ f(self.this_arg);
+ }
+ }
+}
+/// A trait implemented for objects handling events from [`EventsProvider`].
+#[repr(C)]
+pub struct EventHandler {
+ /// An opaque pointer which is passed to your function implementations as an argument.
+ /// This has no meaning in the LDK, and can be NULL or any other value.
+ pub this_arg: *mut c_void,
+ /// Handles the given [`Event`].
+ ///
+ /// See [`EventsProvider`] for details that must be considered when implementing this method.
+ pub handle_event: extern "C" fn (this_arg: *const c_void, event: crate::lightning::util::events::Event),
+ /// Frees any resources associated with this object given its this_arg pointer.
+ /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
+ pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
+}
+unsafe impl Send for EventHandler {}
+unsafe impl Sync for EventHandler {}
+#[no_mangle]
+pub(crate) extern "C" fn EventHandler_clone_fields(orig: &EventHandler) -> EventHandler {
+ EventHandler {
+ this_arg: orig.this_arg,
+ handle_event: Clone::clone(&orig.handle_event),
+ free: Clone::clone(&orig.free),
+ }
+}
+
+use lightning::util::events::EventHandler as rustEventHandler;
+impl rustEventHandler for EventHandler {
+ fn handle_event(&self, mut event: lightning::util::events::Event) {
+ (self.handle_event)(self.this_arg, crate::lightning::util::events::Event::native_into(event))
}
}
// 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 EventsProvider {
+impl std::ops::Deref for EventHandler {
type Target = Self;
fn deref(&self) -> &Self {
self
}
/// Calls the free function if one is set
#[no_mangle]
-pub extern "C" fn EventsProvider_free(this_ptr: EventsProvider) { }
-impl Drop for EventsProvider {
+pub extern "C" fn EventHandler_free(this_ptr: EventHandler) { }
+impl Drop for EventHandler {
fn drop(&mut self) {
if let Some(f) = self.free {
f(self.this_arg);