1 //! Structs and traits which allow other parts of rust-lightning to interact with the blockchain.
4 use bitcoin::hashes::Hash;
7 pub mod chaininterface;
9 pub mod channelmonitor;
11 pub mod keysinterface;
12 /// An error when accessing the chain via [`Access`].
14 /// [`Access`]: trait.Access.html
18 pub enum AccessError {
19 /// The requested chain is unknown.
21 /// The requested transaction doesn't exist or hasn't confirmed.
24 use lightning::chain::AccessError as nativeAccessError;
27 pub(crate) fn to_native(&self) -> nativeAccessError {
29 AccessError::UnknownChain => nativeAccessError::UnknownChain,
30 AccessError::UnknownTx => nativeAccessError::UnknownTx,
34 pub(crate) fn into_native(self) -> nativeAccessError {
36 AccessError::UnknownChain => nativeAccessError::UnknownChain,
37 AccessError::UnknownTx => nativeAccessError::UnknownTx,
41 pub(crate) fn from_native(native: &nativeAccessError) -> Self {
43 nativeAccessError::UnknownChain => AccessError::UnknownChain,
44 nativeAccessError::UnknownTx => AccessError::UnknownTx,
48 pub(crate) fn native_into(native: nativeAccessError) -> Self {
50 nativeAccessError::UnknownChain => AccessError::UnknownChain,
51 nativeAccessError::UnknownTx => AccessError::UnknownTx,
56 pub extern "C" fn AccessError_clone(orig: &AccessError) -> AccessError {
59 /// The `Access` trait defines behavior for accessing chain data and state, such as blocks and
63 pub this_arg: *mut c_void,
64 /// Returns the transaction output of a funding transaction encoded by [`short_channel_id`].
65 /// Returns an error if `genesis_hash` is for a different chain or if such a transaction output
68 /// [`short_channel_id`]: https://github.com/lightningnetwork/lightning-rfc/blob/master/07-routing-gossip.md#definition-of-short_channel_id
70 pub get_utxo: extern "C" fn (this_arg: *const c_void, genesis_hash: *const [u8; 32], short_channel_id: u64) -> crate::c_types::derived::CResult_TxOutAccessErrorZ,
71 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
73 unsafe impl Send for Access {}
74 unsafe impl Sync for Access {}
76 use lightning::chain::Access as rustAccess;
77 impl rustAccess for Access {
78 fn get_utxo(&self, genesis_hash: &bitcoin::hash_types::BlockHash, short_channel_id: u64) -> Result<bitcoin::blockdata::transaction::TxOut, lightning::chain::AccessError> {
79 let mut ret = (self.get_utxo)(self.this_arg, genesis_hash.as_inner(), short_channel_id);
80 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).into_rust() }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })};
85 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
86 // directly as a Deref trait in higher-level structs:
87 impl std::ops::Deref for Access {
89 fn deref(&self) -> &Self {
93 /// Calls the free function if one is set
95 pub extern "C" fn Access_free(this_ptr: Access) { }
96 impl Drop for Access {
98 if let Some(f) = self.free {
103 /// The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
104 /// blocks are connected and disconnected.
106 /// Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
107 /// responsible for maintaining a set of monitors such that they can be updated accordingly as
108 /// channel state changes and HTLCs are resolved. See method documentation for specific
111 /// Implementations **must** ensure that updates are successfully applied and persisted upon method
112 /// completion. If an update fails with a [`PermanentFailure`], then it must immediately shut down
113 /// without taking any further action such as persisting the current state.
115 /// If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
116 /// backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
117 /// could result in a revoked transaction being broadcast, allowing the counterparty to claim all
118 /// funds in the channel. See [`ChannelMonitorUpdateErr`] for more details about how to handle
119 /// multiple instances.
121 /// [`ChannelMonitor`]: channelmonitor/struct.ChannelMonitor.html
122 /// [`ChannelMonitorUpdateErr`]: channelmonitor/enum.ChannelMonitorUpdateErr.html
123 /// [`PermanentFailure`]: channelmonitor/enum.ChannelMonitorUpdateErr.html#variant.PermanentFailure
126 pub this_arg: *mut c_void,
127 /// Watches a channel identified by `funding_txo` using `monitor`.
129 /// Implementations are responsible for watching the chain for the funding transaction along
130 /// with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
131 /// calling [`block_connected`] and [`block_disconnected`] on the monitor.
133 /// [`get_outputs_to_watch`]: channelmonitor/struct.ChannelMonitor.html#method.get_outputs_to_watch
134 /// [`block_connected`]: channelmonitor/struct.ChannelMonitor.html#method.block_connected
135 /// [`block_disconnected`]: channelmonitor/struct.ChannelMonitor.html#method.block_disconnected
137 pub watch_channel: extern "C" fn (this_arg: *const c_void, funding_txo: crate::chain::transaction::OutPoint, monitor: crate::chain::channelmonitor::ChannelMonitor) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ,
138 /// Updates a channel identified by `funding_txo` by applying `update` to its monitor.
140 /// Implementations must call [`update_monitor`] with the given update. See
141 /// [`ChannelMonitorUpdateErr`] for invariants around returning an error.
143 /// [`update_monitor`]: channelmonitor/struct.ChannelMonitor.html#method.update_monitor
144 /// [`ChannelMonitorUpdateErr`]: channelmonitor/enum.ChannelMonitorUpdateErr.html
146 pub update_channel: extern "C" fn (this_arg: *const c_void, funding_txo: crate::chain::transaction::OutPoint, update: crate::chain::channelmonitor::ChannelMonitorUpdate) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ,
147 /// Returns any monitor events since the last call. Subsequent calls must only return new
150 pub release_pending_monitor_events: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_MonitorEventZ,
151 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
153 unsafe impl Send for Watch {}
154 unsafe impl Sync for Watch {}
156 use lightning::chain::Watch as rustWatch;
157 impl rustWatch for Watch {
158 type Keys = crate::chain::keysinterface::ChannelKeys;
159 fn watch_channel(&self, funding_txo: lightning::chain::transaction::OutPoint, monitor: lightning::chain::channelmonitor::ChannelMonitor<crate::chain::keysinterface::ChannelKeys>) -> Result<(), lightning::chain::channelmonitor::ChannelMonitorUpdateErr> {
160 let mut ret = (self.watch_channel)(self.this_arg, crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(funding_txo)), is_owned: true }, crate::chain::channelmonitor::ChannelMonitor { inner: Box::into_raw(Box::new(monitor)), is_owned: true });
161 let mut local_ret = match ret.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })};
164 fn update_channel(&self, funding_txo: lightning::chain::transaction::OutPoint, update: lightning::chain::channelmonitor::ChannelMonitorUpdate) -> Result<(), lightning::chain::channelmonitor::ChannelMonitorUpdateErr> {
165 let mut ret = (self.update_channel)(self.this_arg, crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(funding_txo)), is_owned: true }, crate::chain::channelmonitor::ChannelMonitorUpdate { inner: Box::into_raw(Box::new(update)), is_owned: true });
166 let mut local_ret = match ret.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })};
169 fn release_pending_monitor_events(&self) -> Vec<lightning::chain::channelmonitor::MonitorEvent> {
170 let mut ret = (self.release_pending_monitor_events)(self.this_arg);
171 let mut local_ret = Vec::new(); for mut item in ret.into_rust().drain(..) { local_ret.push( { item.into_native() }); };
176 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
177 // directly as a Deref trait in higher-level structs:
178 impl std::ops::Deref for Watch {
180 fn deref(&self) -> &Self {
184 /// Calls the free function if one is set
186 pub extern "C" fn Watch_free(this_ptr: Watch) { }
187 impl Drop for Watch {
189 if let Some(f) = self.free {
194 /// The `Filter` trait defines behavior for indicating chain activity of interest pertaining to
197 /// This is useful in order to have a [`Watch`] implementation convey to a chain source which
198 /// transactions to be notified of. Notification may take the form of pre-filtering blocks or, in
199 /// the case of [BIP 157]/[BIP 158], only fetching a block if the compact filter matches. If
200 /// receiving full blocks from a chain source, any further filtering is unnecessary.
202 /// After an output has been registered, subsequent block retrievals from the chain source must not
203 /// exclude any transactions matching the new criteria nor any in-block descendants of such
206 /// Note that use as part of a [`Watch`] implementation involves reentrancy. Therefore, the `Filter`
207 /// should not block on I/O. Implementations should instead queue the newly monitored data to be
208 /// processed later. Then, in order to block until the data has been processed, any `Watch`
209 /// invocation that has called the `Filter` must return [`TemporaryFailure`].
211 /// [`Watch`]: trait.Watch.html
212 /// [`TemporaryFailure`]: channelmonitor/enum.ChannelMonitorUpdateErr.html#variant.TemporaryFailure
213 /// [BIP 157]: https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki
214 /// [BIP 158]: https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki
217 pub this_arg: *mut c_void,
218 /// Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
219 /// a spending condition.
220 pub register_tx: extern "C" fn (this_arg: *const c_void, txid: *const [u8; 32], script_pubkey: crate::c_types::u8slice),
221 /// Registers interest in spends of a transaction output identified by `outpoint` having
222 /// `script_pubkey` as the spending condition.
223 pub register_output: extern "C" fn (this_arg: *const c_void, outpoint: &crate::chain::transaction::OutPoint, script_pubkey: crate::c_types::u8slice),
224 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
226 unsafe impl Send for Filter {}
227 unsafe impl Sync for Filter {}
229 use lightning::chain::Filter as rustFilter;
230 impl rustFilter for Filter {
231 fn register_tx(&self, txid: &bitcoin::hash_types::Txid, script_pubkey: &bitcoin::blockdata::script::Script) {
232 (self.register_tx)(self.this_arg, txid.as_inner(), crate::c_types::u8slice::from_slice(&script_pubkey[..]))
234 fn register_output(&self, outpoint: &lightning::chain::transaction::OutPoint, script_pubkey: &bitcoin::blockdata::script::Script) {
235 (self.register_output)(self.this_arg, &crate::chain::transaction::OutPoint { inner: unsafe { (outpoint as *const _) as *mut _ }, is_owned: false }, crate::c_types::u8slice::from_slice(&script_pubkey[..]))
239 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
240 // directly as a Deref trait in higher-level structs:
241 impl std::ops::Deref for Filter {
243 fn deref(&self) -> &Self {
247 /// Calls the free function if one is set
249 pub extern "C" fn Filter_free(this_ptr: Filter) { }
250 impl Drop for Filter {
252 if let Some(f) = self.free {