From 7e0d8189ef1fab54216e446938ac473588bf21cb Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 13 Apr 2021 14:27:07 -0400 Subject: [PATCH] Update auto-generated bindings to latest upstream --- lightning-c-bindings/include/lightning.h | 249 ++++++++++++++++-- lightning-c-bindings/include/lightningpp.hpp | 84 ++++-- lightning-c-bindings/include/rust_types.h | 2 + lightning-c-bindings/src/c_types/derived.rs | 48 +++- .../src/chain/chainmonitor.rs | 61 +++++ .../src/chain/channelmonitor.rs | 65 ++++- lightning-c-bindings/src/ln/channelmanager.rs | 186 +++++++++++-- 7 files changed, 618 insertions(+), 77 deletions(-) diff --git a/lightning-c-bindings/include/lightning.h b/lightning-c-bindings/include/lightning.h index 168bc7c..608dfd5 100644 --- a/lightning-c-bindings/include/lightning.h +++ b/lightning-c-bindings/include/lightning.h @@ -2917,6 +2917,22 @@ typedef struct LDKCVec_C2Tuple_usizeTransactionZZ { uintptr_t datalen; } LDKCVec_C2Tuple_usizeTransactionZZ; +/** + * A dynamically-allocated array of crate::c_types::ThirtyTwoBytess of arbitrary size. + * This corresponds to std::vector in C++ + */ +typedef struct LDKCVec_TxidZ { + /** + * The elements in the array. + * If datalen is non-0 this must be a valid, non-NULL pointer allocated by malloc(). + */ + struct LDKThirtyTwoBytes *data; + /** + * The number of elements pointed to by `data`. + */ + uintptr_t datalen; +} LDKCVec_TxidZ; + /** * The contents of CResult_NoneChannelMonitorUpdateErrZ */ @@ -3672,7 +3688,7 @@ typedef struct LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ { * A dynamically-allocated array of crate::c_types::derived::C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZs of arbitrary size. * This corresponds to std::vector in C++ */ -typedef struct LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ { +typedef struct LDKCVec_TransactionOutputsZ { /** * The elements in the array. * If datalen is non-0 this must be a valid, non-NULL pointer allocated by malloc(). @@ -3682,7 +3698,7 @@ typedef struct LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ { * The number of elements pointed to by `data`. */ uintptr_t datalen; -} LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ; +} LDKCVec_TransactionOutputsZ; /** * A tuple of 2 elements. See the individual fields for the types contained. @@ -4703,7 +4719,7 @@ typedef struct LDKLogger { * ChannelUpdate messages informing peers that the channel is temporarily disabled. To avoid * spam due to quick disconnection/reconnection, updates are not sent until the channel has been * offline for a full minute. In order to track this, you must call - * timer_chan_freshness_every_min roughly once per minute, though it doesn't have to be perfect. + * timer_tick_occurred roughly once per minute, though it doesn't have to be perfect. * * Rather than using a plain ChannelManager, it is preferable to use either a SimpleArcChannelManager * a SimpleRefChannelManager, for conciseness. See their documentation for more details, but @@ -6942,6 +6958,26 @@ typedef struct MUST_USE_STRUCT LDKChainParameters { bool is_owned; } LDKChainParameters; + + +/** + * The best known block as identified by its hash and height. + */ +typedef struct MUST_USE_STRUCT LDKBestBlock { + /** + * A pointer to the opaque Rust object. + * Nearly everywhere, inner must be non-null, however in places where + * the Rust equivalent takes an Option, it may be set to null to indicate None. + */ + LDKnativeBestBlock *inner; + /** + * Indicates that this is the only struct which contains the same pointer. + * Rust functions which take ownership of an object provided via an argument require + * this to be true and invalidate the object pointed to by inner. + */ + bool is_owned; +} LDKBestBlock; + /** * A 3-byte byte array. */ @@ -8138,6 +8174,11 @@ void C2Tuple_usizeTransactionZ_free(struct LDKC2Tuple_usizeTransactionZ _res); */ void CVec_C2Tuple_usizeTransactionZZ_free(struct LDKCVec_C2Tuple_usizeTransactionZZ _res); +/** + * Frees the buffer pointed to by `data` if `datalen` is non-0. + */ +void CVec_TxidZ_free(struct LDKCVec_TxidZ _res); + /** * Creates a new CResult_NoneChannelMonitorUpdateErrZ in the success state. */ @@ -8344,7 +8385,7 @@ void C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_free(struct LDKC2Tuple_TxidCVec_C2Tupl /** * Frees the buffer pointed to by `data` if `datalen` is non-0. */ -void CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free(struct LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ _res); +void CVec_TransactionOutputsZ_free(struct LDKCVec_TransactionOutputsZ _res); /** * Creates a new C2Tuple_BlockHashChannelMonitorZ from the contained elements. @@ -10102,6 +10143,40 @@ void ChainMonitor_free(struct LDKChainMonitor this_obj); */ void ChainMonitor_block_connected(const struct LDKChainMonitor *NONNULL_PTR this_arg, const uint8_t (*header)[80], struct LDKCVec_C2Tuple_usizeTransactionZZ txdata, uint32_t height); +/** + * Dispatches to per-channel monitors, which are responsible for updating their on-chain view + * of a channel and reacting accordingly to newly confirmed transactions. For details, see + * [`ChannelMonitor::transactions_confirmed`]. + * + * Used instead of [`block_connected`] by clients that are notified of transactions rather than + * blocks. May be called before or after [`update_best_block`] for transactions in the + * corresponding block. See [`update_best_block`] for further calling expectations. + * + * [`block_connected`]: Self::block_connected + * [`update_best_block`]: Self::update_best_block + */ +void ChainMonitor_transactions_confirmed(const struct LDKChainMonitor *NONNULL_PTR this_arg, const uint8_t (*header)[80], struct LDKCVec_C2Tuple_usizeTransactionZZ txdata, uint32_t height); + +/** + * Dispatches to per-channel monitors, which are responsible for updating their on-chain view + * of a channel and reacting accordingly based on the new chain tip. For details, see + * [`ChannelMonitor::update_best_block`]. + * + * Used instead of [`block_connected`] by clients that are notified of transactions rather than + * blocks. May be called before or after [`transactions_confirmed`] for the corresponding + * block. + * + * Must be called after new blocks become available for the most recent block. Intermediary + * blocks, however, may be safely skipped. In the event of a chain re-organization, this only + * needs to be called for the most recent block assuming `transaction_unconfirmed` is called + * for any affected transactions. + * + * [`block_connected`]: Self::block_connected + * [`transactions_confirmed`]: Self::transactions_confirmed + * [`transaction_unconfirmed`]: Self::transaction_unconfirmed + */ +void ChainMonitor_update_best_block(const struct LDKChainMonitor *NONNULL_PTR this_arg, const uint8_t (*header)[80], uint32_t height); + /** * Dispatches to per-channel monitors, which are responsible for updating their on-chain view * of a channel based on the disconnected block. See [`ChannelMonitor::block_disconnected`] for @@ -10109,6 +10184,25 @@ void ChainMonitor_block_connected(const struct LDKChainMonitor *NONNULL_PTR this */ void ChainMonitor_block_disconnected(const struct LDKChainMonitor *NONNULL_PTR this_arg, const uint8_t (*header)[80], uint32_t disconnected_height); +/** + * Dispatches to per-channel monitors, which are responsible for updating their on-chain view + * of a channel based on transactions unconfirmed as a result of a chain reorganization. See + * [`ChannelMonitor::transaction_unconfirmed`] for details. + * + * Used instead of [`block_disconnected`] by clients that are notified of transactions rather + * than blocks. May be called before or after [`update_best_block`] for transactions in the + * corresponding block. See [`update_best_block`] for further calling expectations. + * + * [`block_disconnected`]: Self::block_disconnected + * [`update_best_block`]: Self::update_best_block + */ +void ChainMonitor_transaction_unconfirmed(const struct LDKChainMonitor *NONNULL_PTR this_arg, const uint8_t (*txid)[32]); + +/** + * Returns the set of txids that should be monitored for re-organization out of the chain. + */ +MUST_USE_RES struct LDKCVec_TxidZ ChainMonitor_get_relevant_txids(const struct LDKChainMonitor *NONNULL_PTR this_arg); + /** * Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels. * @@ -10311,7 +10405,7 @@ MUST_USE_RES struct LDKCVec_TransactionZ ChannelMonitor_get_latest_holder_commit * * [`get_outputs_to_watch`]: #method.get_outputs_to_watch */ -MUST_USE_RES struct LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ ChannelMonitor_block_connected(const struct LDKChannelMonitor *NONNULL_PTR this_arg, const uint8_t (*header)[80], struct LDKCVec_C2Tuple_usizeTransactionZZ txdata, uint32_t height, struct LDKBroadcasterInterface broadcaster, struct LDKFeeEstimator fee_estimator, struct LDKLogger logger); +MUST_USE_RES struct LDKCVec_TransactionOutputsZ ChannelMonitor_block_connected(const struct LDKChannelMonitor *NONNULL_PTR this_arg, const uint8_t (*header)[80], struct LDKCVec_C2Tuple_usizeTransactionZZ txdata, uint32_t height, struct LDKBroadcasterInterface broadcaster, struct LDKFeeEstimator fee_estimator, struct LDKLogger logger); /** * Determines if the disconnected block contained any transactions of interest and updates @@ -10319,6 +10413,55 @@ MUST_USE_RES struct LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ ChannelMonitor */ void ChannelMonitor_block_disconnected(const struct LDKChannelMonitor *NONNULL_PTR this_arg, const uint8_t (*header)[80], uint32_t height, struct LDKBroadcasterInterface broadcaster, struct LDKFeeEstimator fee_estimator, struct LDKLogger logger); +/** + * Processes transactions confirmed in a block with the given header and height, returning new + * outputs to watch. See [`block_connected`] for details. + * + * Used instead of [`block_connected`] by clients that are notified of transactions rather than + * blocks. May be called before or after [`update_best_block`] for transactions in the + * corresponding block. See [`update_best_block`] for further calling expectations. + * + * [`block_connected`]: Self::block_connected + * [`update_best_block`]: Self::update_best_block + */ +MUST_USE_RES struct LDKCVec_TransactionOutputsZ ChannelMonitor_transactions_confirmed(const struct LDKChannelMonitor *NONNULL_PTR this_arg, const uint8_t (*header)[80], struct LDKCVec_C2Tuple_usizeTransactionZZ txdata, uint32_t height, struct LDKBroadcasterInterface broadcaster, struct LDKFeeEstimator fee_estimator, struct LDKLogger logger); + +/** + * Processes a transaction that was reorganized out of the chain. + * + * Used instead of [`block_disconnected`] by clients that are notified of transactions rather + * than blocks. May be called before or after [`update_best_block`] for transactions in the + * corresponding block. See [`update_best_block`] for further calling expectations. + * + * [`block_disconnected`]: Self::block_disconnected + * [`update_best_block`]: Self::update_best_block + */ +void ChannelMonitor_transaction_unconfirmed(const struct LDKChannelMonitor *NONNULL_PTR this_arg, const uint8_t (*txid)[32], struct LDKBroadcasterInterface broadcaster, struct LDKFeeEstimator fee_estimator, struct LDKLogger logger); + +/** + * Updates the monitor with the current best chain tip, returning new outputs to watch. See + * [`block_connected`] for details. + * + * Used instead of [`block_connected`] by clients that are notified of transactions rather than + * blocks. May be called before or after [`transactions_confirmed`] for the corresponding + * block. + * + * Must be called after new blocks become available for the most recent block. Intermediary + * blocks, however, may be safely skipped. In the event of a chain re-organization, this only + * needs to be called for the most recent block assuming `transaction_unconfirmed` is called + * for any affected transactions. + * + * [`block_connected`]: Self::block_connected + * [`transactions_confirmed`]: Self::transactions_confirmed + * [`transaction_unconfirmed`]: Self::transaction_unconfirmed + */ +MUST_USE_RES struct LDKCVec_TransactionOutputsZ ChannelMonitor_update_best_block(const struct LDKChannelMonitor *NONNULL_PTR this_arg, const uint8_t (*header)[80], uint32_t height, struct LDKBroadcasterInterface broadcaster, struct LDKFeeEstimator fee_estimator, struct LDKLogger logger); + +/** + * Returns the set of txids that should be monitored for re-organization out of the chain. + */ +MUST_USE_RES struct LDKCVec_TxidZ ChannelMonitor_get_relevant_txids(const struct LDKChannelMonitor *NONNULL_PTR this_arg); + /** * Calls the free function if one is set */ @@ -10798,33 +10941,53 @@ enum LDKNetwork ChainParameters_get_network(const struct LDKChainParameters *NON void ChainParameters_set_network(struct LDKChainParameters *NONNULL_PTR this_ptr, enum LDKNetwork val); /** - * The hash of the latest block successfully connected. + * The hash and height of the latest block successfully connected. + * + * Used to track on-chain channel funding outputs and send payments with reliable timelocks. */ -const uint8_t (*ChainParameters_get_latest_hash(const struct LDKChainParameters *NONNULL_PTR this_ptr))[32]; +struct LDKBestBlock ChainParameters_get_best_block(const struct LDKChainParameters *NONNULL_PTR this_ptr); /** - * The hash of the latest block successfully connected. + * The hash and height of the latest block successfully connected. + * + * Used to track on-chain channel funding outputs and send payments with reliable timelocks. */ -void ChainParameters_set_latest_hash(struct LDKChainParameters *NONNULL_PTR this_ptr, struct LDKThirtyTwoBytes val); +void ChainParameters_set_best_block(struct LDKChainParameters *NONNULL_PTR this_ptr, struct LDKBestBlock val); /** - * The height of the latest block successfully connected. - * - * Used to track on-chain channel funding outputs and send payments with reliable timelocks. + * Constructs a new ChainParameters given each field */ -uintptr_t ChainParameters_get_latest_height(const struct LDKChainParameters *NONNULL_PTR this_ptr); +MUST_USE_RES struct LDKChainParameters ChainParameters_new(enum LDKNetwork network_arg, struct LDKBestBlock best_block_arg); /** - * The height of the latest block successfully connected. - * - * Used to track on-chain channel funding outputs and send payments with reliable timelocks. + * Frees any resources used by the BestBlock, if is_owned is set and inner is non-NULL. */ -void ChainParameters_set_latest_height(struct LDKChainParameters *NONNULL_PTR this_ptr, uintptr_t val); +void BestBlock_free(struct LDKBestBlock this_obj); /** - * Constructs a new ChainParameters given each field + * Creates a copy of the BestBlock + */ +struct LDKBestBlock BestBlock_clone(const struct LDKBestBlock *NONNULL_PTR orig); + +/** + * Returns the best block from the genesis of the given network. + */ +MUST_USE_RES struct LDKBestBlock BestBlock_from_genesis(enum LDKNetwork network); + +/** + * Returns the best block as identified by the given block hash and height. + */ +MUST_USE_RES struct LDKBestBlock BestBlock_new(struct LDKThirtyTwoBytes block_hash, uint32_t height); + +/** + * Returns the best block hash. */ -MUST_USE_RES struct LDKChainParameters ChainParameters_new(enum LDKNetwork network_arg, struct LDKThirtyTwoBytes latest_hash_arg, uintptr_t latest_height_arg); +MUST_USE_RES struct LDKThirtyTwoBytes BestBlock_block_hash(const struct LDKBestBlock *NONNULL_PTR this_arg); + +/** + * Returns the best block height. + */ +MUST_USE_RES uint32_t BestBlock_height(const struct LDKBestBlock *NONNULL_PTR this_arg); /** * Frees any resources used by the ChannelDetails, if is_owned is set and inner is non-NULL. @@ -10980,6 +11143,11 @@ struct LDKPaymentSendFailure PaymentSendFailure_clone(const struct LDKPaymentSen */ MUST_USE_RES struct LDKChannelManager ChannelManager_new(struct LDKFeeEstimator fee_est, struct LDKWatch chain_monitor, struct LDKBroadcasterInterface tx_broadcaster, struct LDKLogger logger, struct LDKKeysInterface keys_manager, struct LDKUserConfig config, struct LDKChainParameters params); +/** + * Gets the current configuration applied to all new channels, as + */ +MUST_USE_RES struct LDKUserConfig ChannelManager_get_current_default_configuration(const struct LDKChannelManager *NONNULL_PTR this_arg); + /** * Creates a new outbound channel to the given remote node and with the given value. * @@ -11132,7 +11300,7 @@ void ChannelManager_process_pending_htlc_forwards(const struct LDKChannelManager * * Note that in some rare cases this may generate a `chain::Watch::update_channel` call. */ -void ChannelManager_timer_chan_freshness_every_min(const struct LDKChannelManager *NONNULL_PTR this_arg); +void ChannelManager_timer_tick_occurred(const struct LDKChannelManager *NONNULL_PTR this_arg); /** * Indicates that the preimage for payment_hash is unknown or the received amount is incorrect @@ -11247,6 +11415,47 @@ void ChannelManager_transactions_confirmed(const struct LDKChannelManager *NONNU */ void ChannelManager_update_best_block(const struct LDKChannelManager *NONNULL_PTR this_arg, const uint8_t (*header)[80], uint32_t height); +/** + * Gets the set of txids which should be monitored for their confirmation state. + * + * If you're providing information about reorganizations via [`transaction_unconfirmed`], this + * is the set of transactions which you may need to call [`transaction_unconfirmed`] for. + * + * This may be useful to poll to determine the set of transactions which must be registered + * with an Electrum server or for which an Electrum server needs to be polled to determine + * transaction confirmation state. + * + * This may update after any [`transactions_confirmed`] or [`block_connected`] call. + * + * Note that this is NOT the set of transactions which must be included in calls to + * [`transactions_confirmed`] if they are confirmed, but a small subset of it. + * + * [`transactions_confirmed`]: Self::transactions_confirmed + * [`transaction_unconfirmed`]: Self::transaction_unconfirmed + * [`block_connected`]: chain::Listen::block_connected + */ +MUST_USE_RES struct LDKCVec_TxidZ ChannelManager_get_relevant_txids(const struct LDKChannelManager *NONNULL_PTR this_arg); + +/** + * Marks a transaction as having been reorganized out of the blockchain. + * + * If a transaction is included in [`get_relevant_txids`], and is no longer in the main branch + * of the blockchain, this function should be called to indicate that the transaction should + * be considered reorganized out. + * + * Once this is called, the given transaction will no longer appear on [`get_relevant_txids`], + * though this may be called repeatedly for a given transaction without issue. + * + * Note that if the transaction is confirmed on the main chain in a different block (indicated + * via a call to [`transactions_confirmed`]), it may re-appear in [`get_relevant_txids`], thus + * be very wary of race-conditions wherein the final state of a transaction indicated via + * these APIs is not the same as its state on the blockchain. + * + * [`transactions_confirmed`]: Self::transactions_confirmed + * [`get_relevant_txids`]: Self::get_relevant_txids + */ +void ChannelManager_transaction_unconfirmed(const struct LDKChannelManager *NONNULL_PTR this_arg, const uint8_t (*txid)[32]); + /** * Blocks until ChannelManager needs to be persisted or a timeout is reached. It returns a bool * indicating whether persistence is necessary. Only one listener on diff --git a/lightning-c-bindings/include/lightningpp.hpp b/lightning-c-bindings/include/lightningpp.hpp index ef43273..008b0f4 100644 --- a/lightning-c-bindings/include/lightningpp.hpp +++ b/lightning-c-bindings/include/lightningpp.hpp @@ -881,6 +881,21 @@ public: const LDKChainParameters* operator &() const { return &self; } const LDKChainParameters* operator ->() const { return &self; } }; +class BestBlock { +private: + LDKBestBlock self; +public: + BestBlock(const BestBlock&) = delete; + BestBlock(BestBlock&& o) : self(o.self) { memset(&o, 0, sizeof(BestBlock)); } + BestBlock(LDKBestBlock&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKBestBlock)); } + operator LDKBestBlock() && { LDKBestBlock res = self; memset(&self, 0, sizeof(LDKBestBlock)); return res; } + ~BestBlock() { BestBlock_free(self); } + BestBlock& operator=(BestBlock&& o) { BestBlock_free(self); self = o.self; memset(&o, 0, sizeof(BestBlock)); return *this; } + LDKBestBlock* operator &() { return &self; } + LDKBestBlock* operator ->() { return &self; } + const LDKBestBlock* operator &() const { return &self; } + const LDKBestBlock* operator ->() const { return &self; } +}; class ChannelDetails { private: LDKChannelDetails self; @@ -2066,6 +2081,21 @@ public: const LDKCResult_PingDecodeErrorZ* operator &() const { return &self; } const LDKCResult_PingDecodeErrorZ* operator ->() const { return &self; } }; +class CVec_TransactionOutputsZ { +private: + LDKCVec_TransactionOutputsZ self; +public: + CVec_TransactionOutputsZ(const CVec_TransactionOutputsZ&) = delete; + CVec_TransactionOutputsZ(CVec_TransactionOutputsZ&& o) : self(o.self) { memset(&o, 0, sizeof(CVec_TransactionOutputsZ)); } + CVec_TransactionOutputsZ(LDKCVec_TransactionOutputsZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCVec_TransactionOutputsZ)); } + operator LDKCVec_TransactionOutputsZ() && { LDKCVec_TransactionOutputsZ res = self; memset(&self, 0, sizeof(LDKCVec_TransactionOutputsZ)); return res; } + ~CVec_TransactionOutputsZ() { CVec_TransactionOutputsZ_free(self); } + CVec_TransactionOutputsZ& operator=(CVec_TransactionOutputsZ&& o) { CVec_TransactionOutputsZ_free(self); self = o.self; memset(&o, 0, sizeof(CVec_TransactionOutputsZ)); return *this; } + LDKCVec_TransactionOutputsZ* operator &() { return &self; } + LDKCVec_TransactionOutputsZ* operator ->() { return &self; } + const LDKCVec_TransactionOutputsZ* operator &() const { return &self; } + const LDKCVec_TransactionOutputsZ* operator ->() const { return &self; } +}; class CResult_ErrorMessageDecodeErrorZ { private: LDKCResult_ErrorMessageDecodeErrorZ self; @@ -2621,21 +2651,6 @@ public: const LDKCVec_MessageSendEventZ* operator &() const { return &self; } const LDKCVec_MessageSendEventZ* operator ->() const { return &self; } }; -class CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ { -private: - LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ self; -public: - CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ(const CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ&) = delete; - CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ(CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ&& o) : self(o.self) { memset(&o, 0, sizeof(CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ)); } - CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ(LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ)); } - operator LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ() && { LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ res = self; memset(&self, 0, sizeof(LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ)); return res; } - ~CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ() { CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free(self); } - CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ& operator=(CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ&& o) { CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free(self); self = o.self; memset(&o, 0, sizeof(CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ)); return *this; } - LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ* operator &() { return &self; } - LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ* operator ->() { return &self; } - const LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ* operator &() const { return &self; } - const LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ* operator ->() const { return &self; } -}; class C2Tuple_OutPointScriptZ { private: LDKC2Tuple_OutPointScriptZ self; @@ -2696,20 +2711,20 @@ public: const LDKCResult_UnsignedChannelAnnouncementDecodeErrorZ* operator &() const { return &self; } const LDKCResult_UnsignedChannelAnnouncementDecodeErrorZ* operator ->() const { return &self; } }; -class CResult_PongDecodeErrorZ { +class CVec_TxidZ { private: - LDKCResult_PongDecodeErrorZ self; + LDKCVec_TxidZ self; public: - CResult_PongDecodeErrorZ(const CResult_PongDecodeErrorZ&) = delete; - CResult_PongDecodeErrorZ(CResult_PongDecodeErrorZ&& o) : self(o.self) { memset(&o, 0, sizeof(CResult_PongDecodeErrorZ)); } - CResult_PongDecodeErrorZ(LDKCResult_PongDecodeErrorZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCResult_PongDecodeErrorZ)); } - operator LDKCResult_PongDecodeErrorZ() && { LDKCResult_PongDecodeErrorZ res = self; memset(&self, 0, sizeof(LDKCResult_PongDecodeErrorZ)); return res; } - ~CResult_PongDecodeErrorZ() { CResult_PongDecodeErrorZ_free(self); } - CResult_PongDecodeErrorZ& operator=(CResult_PongDecodeErrorZ&& o) { CResult_PongDecodeErrorZ_free(self); self = o.self; memset(&o, 0, sizeof(CResult_PongDecodeErrorZ)); return *this; } - LDKCResult_PongDecodeErrorZ* operator &() { return &self; } - LDKCResult_PongDecodeErrorZ* operator ->() { return &self; } - const LDKCResult_PongDecodeErrorZ* operator &() const { return &self; } - const LDKCResult_PongDecodeErrorZ* operator ->() const { return &self; } + CVec_TxidZ(const CVec_TxidZ&) = delete; + CVec_TxidZ(CVec_TxidZ&& o) : self(o.self) { memset(&o, 0, sizeof(CVec_TxidZ)); } + CVec_TxidZ(LDKCVec_TxidZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCVec_TxidZ)); } + operator LDKCVec_TxidZ() && { LDKCVec_TxidZ res = self; memset(&self, 0, sizeof(LDKCVec_TxidZ)); return res; } + ~CVec_TxidZ() { CVec_TxidZ_free(self); } + CVec_TxidZ& operator=(CVec_TxidZ&& o) { CVec_TxidZ_free(self); self = o.self; memset(&o, 0, sizeof(CVec_TxidZ)); return *this; } + LDKCVec_TxidZ* operator &() { return &self; } + LDKCVec_TxidZ* operator ->() { return &self; } + const LDKCVec_TxidZ* operator &() const { return &self; } + const LDKCVec_TxidZ* operator ->() const { return &self; } }; class CResult_NoneMonitorUpdateErrorZ { private: @@ -2801,6 +2816,21 @@ public: const LDKCResult_AcceptChannelDecodeErrorZ* operator &() const { return &self; } const LDKCResult_AcceptChannelDecodeErrorZ* operator ->() const { return &self; } }; +class CResult_PongDecodeErrorZ { +private: + LDKCResult_PongDecodeErrorZ self; +public: + CResult_PongDecodeErrorZ(const CResult_PongDecodeErrorZ&) = delete; + CResult_PongDecodeErrorZ(CResult_PongDecodeErrorZ&& o) : self(o.self) { memset(&o, 0, sizeof(CResult_PongDecodeErrorZ)); } + CResult_PongDecodeErrorZ(LDKCResult_PongDecodeErrorZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCResult_PongDecodeErrorZ)); } + operator LDKCResult_PongDecodeErrorZ() && { LDKCResult_PongDecodeErrorZ res = self; memset(&self, 0, sizeof(LDKCResult_PongDecodeErrorZ)); return res; } + ~CResult_PongDecodeErrorZ() { CResult_PongDecodeErrorZ_free(self); } + CResult_PongDecodeErrorZ& operator=(CResult_PongDecodeErrorZ&& o) { CResult_PongDecodeErrorZ_free(self); self = o.self; memset(&o, 0, sizeof(CResult_PongDecodeErrorZ)); return *this; } + LDKCResult_PongDecodeErrorZ* operator &() { return &self; } + LDKCResult_PongDecodeErrorZ* operator ->() { return &self; } + const LDKCResult_PongDecodeErrorZ* operator &() const { return &self; } + const LDKCResult_PongDecodeErrorZ* operator ->() const { return &self; } +}; class C2Tuple_BlockHashChannelManagerZ { private: LDKC2Tuple_BlockHashChannelManagerZ self; diff --git a/lightning-c-bindings/include/rust_types.h b/lightning-c-bindings/include/rust_types.h index 34db4a6..26619b5 100644 --- a/lightning-c-bindings/include/rust_types.h +++ b/lightning-c-bindings/include/rust_types.h @@ -91,6 +91,8 @@ struct nativeChannelManagerOpaque; typedef struct nativeChannelManagerOpaque LDKnativeChannelManager; struct nativeChainParametersOpaque; typedef struct nativeChainParametersOpaque LDKnativeChainParameters; +struct nativeBestBlockOpaque; +typedef struct nativeBestBlockOpaque LDKnativeBestBlock; struct nativeChannelDetailsOpaque; typedef struct nativeChannelDetailsOpaque LDKnativeChannelDetails; struct nativeChannelManagerReadArgsOpaque; diff --git a/lightning-c-bindings/src/c_types/derived.rs b/lightning-c-bindings/src/c_types/derived.rs index 2c4afae..ed869b6 100644 --- a/lightning-c-bindings/src/c_types/derived.rs +++ b/lightning-c-bindings/src/c_types/derived.rs @@ -3028,6 +3028,44 @@ impl Drop for CVec_C2Tuple_usizeTransactionZZ { } } #[repr(C)] +/// A dynamically-allocated array of crate::c_types::ThirtyTwoBytess of arbitrary size. +/// This corresponds to std::vector in C++ +pub struct CVec_TxidZ { + /// The elements in the array. + /// If datalen is non-0 this must be a valid, non-NULL pointer allocated by malloc(). + pub data: *mut crate::c_types::ThirtyTwoBytes, + /// The number of elements pointed to by `data`. + pub datalen: usize +} +impl CVec_TxidZ { + #[allow(unused)] pub(crate) fn into_rust(&mut self) -> Vec { + if self.datalen == 0 { return Vec::new(); } + let ret = unsafe { Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) }.into(); + self.data = std::ptr::null_mut(); + self.datalen = 0; + ret + } + #[allow(unused)] pub(crate) fn as_slice(&self) -> &[crate::c_types::ThirtyTwoBytes] { + unsafe { std::slice::from_raw_parts_mut(self.data, self.datalen) } + } +} +impl From> for CVec_TxidZ { + fn from(v: Vec) -> Self { + let datalen = v.len(); + let data = Box::into_raw(v.into_boxed_slice()); + Self { datalen, data: unsafe { (*data).as_mut_ptr() } } + } +} +#[no_mangle] +/// Frees the buffer pointed to by `data` if `datalen` is non-0. +pub extern "C" fn CVec_TxidZ_free(_res: CVec_TxidZ) { } +impl Drop for CVec_TxidZ { + fn drop(&mut self) { + if self.datalen == 0 { return; } + unsafe { Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) }; + } +} +#[repr(C)] /// The contents of CResult_NoneChannelMonitorUpdateErrZ pub union CResult_NoneChannelMonitorUpdateErrZPtr { /// Note that this value is always NULL, as there are no contents in the OK variant @@ -3923,14 +3961,14 @@ pub extern "C" fn C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_free(_res: C2Tuple_TxidCV #[repr(C)] /// A dynamically-allocated array of crate::c_types::derived::C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZs of arbitrary size. /// This corresponds to std::vector in C++ -pub struct CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ { +pub struct CVec_TransactionOutputsZ { /// The elements in the array. /// If datalen is non-0 this must be a valid, non-NULL pointer allocated by malloc(). pub data: *mut crate::c_types::derived::C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ, /// The number of elements pointed to by `data`. pub datalen: usize } -impl CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ { +impl CVec_TransactionOutputsZ { #[allow(unused)] pub(crate) fn into_rust(&mut self) -> Vec { if self.datalen == 0 { return Vec::new(); } let ret = unsafe { Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) }.into(); @@ -3942,7 +3980,7 @@ impl CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ { unsafe { std::slice::from_raw_parts_mut(self.data, self.datalen) } } } -impl From> for CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ { +impl From> for CVec_TransactionOutputsZ { fn from(v: Vec) -> Self { let datalen = v.len(); let data = Box::into_raw(v.into_boxed_slice()); @@ -3951,8 +3989,8 @@ impl From> fo } #[no_mangle] /// Frees the buffer pointed to by `data` if `datalen` is non-0. -pub extern "C" fn CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free(_res: CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ) { } -impl Drop for CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ { +pub extern "C" fn CVec_TransactionOutputsZ_free(_res: CVec_TransactionOutputsZ) { } +impl Drop for CVec_TransactionOutputsZ { fn drop(&mut self) { if self.datalen == 0 { return; } unsafe { Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) }; diff --git a/lightning-c-bindings/src/chain/chainmonitor.rs b/lightning-c-bindings/src/chain/chainmonitor.rs index 55c18d3..7848acc 100644 --- a/lightning-c-bindings/src/chain/chainmonitor.rs +++ b/lightning-c-bindings/src/chain/chainmonitor.rs @@ -94,6 +94,43 @@ pub extern "C" fn ChainMonitor_block_connected(this_arg: &ChainMonitor, header: unsafe { &*this_arg.inner }.block_connected(&::bitcoin::consensus::encode::deserialize(unsafe { &*header }).unwrap(), &local_txdata.iter().map(|(a, b)| (*a, b)).collect::>()[..], height) } +/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view +/// of a channel and reacting accordingly to newly confirmed transactions. For details, see +/// [`ChannelMonitor::transactions_confirmed`]. +/// +/// Used instead of [`block_connected`] by clients that are notified of transactions rather than +/// blocks. May be called before or after [`update_best_block`] for transactions in the +/// corresponding block. See [`update_best_block`] for further calling expectations. +/// +/// [`block_connected`]: Self::block_connected +/// [`update_best_block`]: Self::update_best_block +#[no_mangle] +pub extern "C" fn ChainMonitor_transactions_confirmed(this_arg: &ChainMonitor, header: *const [u8; 80], mut txdata: crate::c_types::derived::CVec_C2Tuple_usizeTransactionZZ, mut height: u32) { + let mut local_txdata = Vec::new(); for mut item in txdata.into_rust().drain(..) { local_txdata.push( { let (mut orig_txdata_0_0, mut orig_txdata_0_1) = item.to_rust(); let mut local_txdata_0 = (orig_txdata_0_0, orig_txdata_0_1.into_bitcoin()); local_txdata_0 }); }; + unsafe { &*this_arg.inner }.transactions_confirmed(&::bitcoin::consensus::encode::deserialize(unsafe { &*header }).unwrap(), &local_txdata.iter().map(|(a, b)| (*a, b)).collect::>()[..], height) +} + +/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view +/// of a channel and reacting accordingly based on the new chain tip. For details, see +/// [`ChannelMonitor::update_best_block`]. +/// +/// Used instead of [`block_connected`] by clients that are notified of transactions rather than +/// blocks. May be called before or after [`transactions_confirmed`] for the corresponding +/// block. +/// +/// Must be called after new blocks become available for the most recent block. Intermediary +/// blocks, however, may be safely skipped. In the event of a chain re-organization, this only +/// needs to be called for the most recent block assuming `transaction_unconfirmed` is called +/// for any affected transactions. +/// +/// [`block_connected`]: Self::block_connected +/// [`transactions_confirmed`]: Self::transactions_confirmed +/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed +#[no_mangle] +pub extern "C" fn ChainMonitor_update_best_block(this_arg: &ChainMonitor, header: *const [u8; 80], mut height: u32) { + unsafe { &*this_arg.inner }.update_best_block(&::bitcoin::consensus::encode::deserialize(unsafe { &*header }).unwrap(), height) +} + /// Dispatches to per-channel monitors, which are responsible for updating their on-chain view /// of a channel based on the disconnected block. See [`ChannelMonitor::block_disconnected`] for /// details. @@ -102,6 +139,30 @@ pub extern "C" fn ChainMonitor_block_disconnected(this_arg: &ChainMonitor, heade unsafe { &*this_arg.inner }.block_disconnected(&::bitcoin::consensus::encode::deserialize(unsafe { &*header }).unwrap(), disconnected_height) } +/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view +/// of a channel based on transactions unconfirmed as a result of a chain reorganization. See +/// [`ChannelMonitor::transaction_unconfirmed`] for details. +/// +/// Used instead of [`block_disconnected`] by clients that are notified of transactions rather +/// than blocks. May be called before or after [`update_best_block`] for transactions in the +/// corresponding block. See [`update_best_block`] for further calling expectations. +/// +/// [`block_disconnected`]: Self::block_disconnected +/// [`update_best_block`]: Self::update_best_block +#[no_mangle] +pub extern "C" fn ChainMonitor_transaction_unconfirmed(this_arg: &ChainMonitor, txid: *const [u8; 32]) { + unsafe { &*this_arg.inner }.transaction_unconfirmed(&::bitcoin::hash_types::Txid::from_slice(&unsafe { &*txid }[..]).unwrap()) +} + +/// Returns the set of txids that should be monitored for re-organization out of the chain. +#[must_use] +#[no_mangle] +pub extern "C" fn ChainMonitor_get_relevant_txids(this_arg: &ChainMonitor) -> crate::c_types::derived::CVec_TxidZ { + let mut ret = unsafe { &*this_arg.inner }.get_relevant_txids(); + let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::c_types::ThirtyTwoBytes { data: item.into_inner() } }); }; + local_ret.into() +} + /// Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels. /// /// When an optional chain source implementing [`chain::Filter`] is provided, the chain monitor diff --git a/lightning-c-bindings/src/chain/channelmonitor.rs b/lightning-c-bindings/src/chain/channelmonitor.rs index 5e10158..8d9402e 100644 --- a/lightning-c-bindings/src/chain/channelmonitor.rs +++ b/lightning-c-bindings/src/chain/channelmonitor.rs @@ -649,7 +649,7 @@ pub extern "C" fn ChannelMonitor_get_latest_holder_commitment_txn(this_arg: &Cha /// [`get_outputs_to_watch`]: #method.get_outputs_to_watch #[must_use] #[no_mangle] -pub extern "C" fn ChannelMonitor_block_connected(this_arg: &ChannelMonitor, header: *const [u8; 80], mut txdata: crate::c_types::derived::CVec_C2Tuple_usizeTransactionZZ, mut height: u32, mut broadcaster: crate::chain::chaininterface::BroadcasterInterface, mut fee_estimator: crate::chain::chaininterface::FeeEstimator, mut logger: crate::util::logger::Logger) -> crate::c_types::derived::CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ { +pub extern "C" fn ChannelMonitor_block_connected(this_arg: &ChannelMonitor, header: *const [u8; 80], mut txdata: crate::c_types::derived::CVec_C2Tuple_usizeTransactionZZ, mut height: u32, mut broadcaster: crate::chain::chaininterface::BroadcasterInterface, mut fee_estimator: crate::chain::chaininterface::FeeEstimator, mut logger: crate::util::logger::Logger) -> crate::c_types::derived::CVec_TransactionOutputsZ { let mut local_txdata = Vec::new(); for mut item in txdata.into_rust().drain(..) { local_txdata.push( { let (mut orig_txdata_0_0, mut orig_txdata_0_1) = item.to_rust(); let mut local_txdata_0 = (orig_txdata_0_0, orig_txdata_0_1.into_bitcoin()); local_txdata_0 }); }; let mut ret = unsafe { &*this_arg.inner }.block_connected(&::bitcoin::consensus::encode::deserialize(unsafe { &*header }).unwrap(), &local_txdata.iter().map(|(a, b)| (*a, b)).collect::>()[..], height, broadcaster, fee_estimator, logger); let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { let (mut orig_ret_0_0, mut orig_ret_0_1) = item; let mut local_orig_ret_0_1 = Vec::new(); for mut item in orig_ret_0_1.drain(..) { local_orig_ret_0_1.push( { let (mut orig_orig_ret_0_1_0_0, mut orig_orig_ret_0_1_0_1) = item; let mut local_orig_ret_0_1_0 = (orig_orig_ret_0_1_0_0, crate::c_types::TxOut::from_rust(orig_orig_ret_0_1_0_1)).into(); local_orig_ret_0_1_0 }); }; let mut local_ret_0 = (crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0.into_inner() }, local_orig_ret_0_1.into()).into(); local_ret_0 }); }; @@ -663,6 +663,69 @@ pub extern "C" fn ChannelMonitor_block_disconnected(this_arg: &ChannelMonitor, h unsafe { &*this_arg.inner }.block_disconnected(&::bitcoin::consensus::encode::deserialize(unsafe { &*header }).unwrap(), height, broadcaster, fee_estimator, logger) } +/// Processes transactions confirmed in a block with the given header and height, returning new +/// outputs to watch. See [`block_connected`] for details. +/// +/// Used instead of [`block_connected`] by clients that are notified of transactions rather than +/// blocks. May be called before or after [`update_best_block`] for transactions in the +/// corresponding block. See [`update_best_block`] for further calling expectations. +/// +/// [`block_connected`]: Self::block_connected +/// [`update_best_block`]: Self::update_best_block +#[must_use] +#[no_mangle] +pub extern "C" fn ChannelMonitor_transactions_confirmed(this_arg: &ChannelMonitor, header: *const [u8; 80], mut txdata: crate::c_types::derived::CVec_C2Tuple_usizeTransactionZZ, mut height: u32, mut broadcaster: crate::chain::chaininterface::BroadcasterInterface, mut fee_estimator: crate::chain::chaininterface::FeeEstimator, mut logger: crate::util::logger::Logger) -> crate::c_types::derived::CVec_TransactionOutputsZ { + let mut local_txdata = Vec::new(); for mut item in txdata.into_rust().drain(..) { local_txdata.push( { let (mut orig_txdata_0_0, mut orig_txdata_0_1) = item.to_rust(); let mut local_txdata_0 = (orig_txdata_0_0, orig_txdata_0_1.into_bitcoin()); local_txdata_0 }); }; + let mut ret = unsafe { &*this_arg.inner }.transactions_confirmed(&::bitcoin::consensus::encode::deserialize(unsafe { &*header }).unwrap(), &local_txdata.iter().map(|(a, b)| (*a, b)).collect::>()[..], height, broadcaster, fee_estimator, logger); + let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { let (mut orig_ret_0_0, mut orig_ret_0_1) = item; let mut local_orig_ret_0_1 = Vec::new(); for mut item in orig_ret_0_1.drain(..) { local_orig_ret_0_1.push( { let (mut orig_orig_ret_0_1_0_0, mut orig_orig_ret_0_1_0_1) = item; let mut local_orig_ret_0_1_0 = (orig_orig_ret_0_1_0_0, crate::c_types::TxOut::from_rust(orig_orig_ret_0_1_0_1)).into(); local_orig_ret_0_1_0 }); }; let mut local_ret_0 = (crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0.into_inner() }, local_orig_ret_0_1.into()).into(); local_ret_0 }); }; + local_ret.into() +} + +/// Processes a transaction that was reorganized out of the chain. +/// +/// Used instead of [`block_disconnected`] by clients that are notified of transactions rather +/// than blocks. May be called before or after [`update_best_block`] for transactions in the +/// corresponding block. See [`update_best_block`] for further calling expectations. +/// +/// [`block_disconnected`]: Self::block_disconnected +/// [`update_best_block`]: Self::update_best_block +#[no_mangle] +pub extern "C" fn ChannelMonitor_transaction_unconfirmed(this_arg: &ChannelMonitor, txid: *const [u8; 32], mut broadcaster: crate::chain::chaininterface::BroadcasterInterface, mut fee_estimator: crate::chain::chaininterface::FeeEstimator, mut logger: crate::util::logger::Logger) { + unsafe { &*this_arg.inner }.transaction_unconfirmed(&::bitcoin::hash_types::Txid::from_slice(&unsafe { &*txid }[..]).unwrap(), broadcaster, fee_estimator, logger) +} + +/// Updates the monitor with the current best chain tip, returning new outputs to watch. See +/// [`block_connected`] for details. +/// +/// Used instead of [`block_connected`] by clients that are notified of transactions rather than +/// blocks. May be called before or after [`transactions_confirmed`] for the corresponding +/// block. +/// +/// Must be called after new blocks become available for the most recent block. Intermediary +/// blocks, however, may be safely skipped. In the event of a chain re-organization, this only +/// needs to be called for the most recent block assuming `transaction_unconfirmed` is called +/// for any affected transactions. +/// +/// [`block_connected`]: Self::block_connected +/// [`transactions_confirmed`]: Self::transactions_confirmed +/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed +#[must_use] +#[no_mangle] +pub extern "C" fn ChannelMonitor_update_best_block(this_arg: &ChannelMonitor, header: *const [u8; 80], mut height: u32, mut broadcaster: crate::chain::chaininterface::BroadcasterInterface, mut fee_estimator: crate::chain::chaininterface::FeeEstimator, mut logger: crate::util::logger::Logger) -> crate::c_types::derived::CVec_TransactionOutputsZ { + let mut ret = unsafe { &*this_arg.inner }.update_best_block(&::bitcoin::consensus::encode::deserialize(unsafe { &*header }).unwrap(), height, broadcaster, fee_estimator, logger); + let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { let (mut orig_ret_0_0, mut orig_ret_0_1) = item; let mut local_orig_ret_0_1 = Vec::new(); for mut item in orig_ret_0_1.drain(..) { local_orig_ret_0_1.push( { let (mut orig_orig_ret_0_1_0_0, mut orig_orig_ret_0_1_0_1) = item; let mut local_orig_ret_0_1_0 = (orig_orig_ret_0_1_0_0, crate::c_types::TxOut::from_rust(orig_orig_ret_0_1_0_1)).into(); local_orig_ret_0_1_0 }); }; let mut local_ret_0 = (crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0.into_inner() }, local_orig_ret_0_1.into()).into(); local_ret_0 }); }; + local_ret.into() +} + +/// Returns the set of txids that should be monitored for re-organization out of the chain. +#[must_use] +#[no_mangle] +pub extern "C" fn ChannelMonitor_get_relevant_txids(this_arg: &ChannelMonitor) -> crate::c_types::derived::CVec_TxidZ { + let mut ret = unsafe { &*this_arg.inner }.get_relevant_txids(); + let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::c_types::ThirtyTwoBytes { data: item.into_inner() } }); }; + local_ret.into() +} + /// `Persist` defines behavior for persisting channel monitors: this could mean /// writing once to disk, and/or uploading to one or more backup services. /// diff --git a/lightning-c-bindings/src/ln/channelmanager.rs b/lightning-c-bindings/src/ln/channelmanager.rs index cad534b..3b2ca23 100644 --- a/lightning-c-bindings/src/ln/channelmanager.rs +++ b/lightning-c-bindings/src/ln/channelmanager.rs @@ -54,7 +54,7 @@ type nativeChannelManager = nativeChannelManagerImport cra pub extern "C" fn ChainParameters_set_network(this_ptr: &mut ChainParameters, mut val: crate::bitcoin::network::Network) { unsafe { &mut *this_ptr.inner }.network = val.into_bitcoin(); } -/// The hash of the latest block successfully connected. -#[no_mangle] -pub extern "C" fn ChainParameters_get_latest_hash(this_ptr: &ChainParameters) -> *const [u8; 32] { - let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.latest_hash; - (*inner_val).as_inner() -} -/// The hash of the latest block successfully connected. -#[no_mangle] -pub extern "C" fn ChainParameters_set_latest_hash(this_ptr: &mut ChainParameters, mut val: crate::c_types::ThirtyTwoBytes) { - unsafe { &mut *this_ptr.inner }.latest_hash = ::bitcoin::hash_types::BlockHash::from_slice(&val.data[..]).unwrap(); -} -/// The height of the latest block successfully connected. +/// The hash and height of the latest block successfully connected. /// /// Used to track on-chain channel funding outputs and send payments with reliable timelocks. #[no_mangle] -pub extern "C" fn ChainParameters_get_latest_height(this_ptr: &ChainParameters) -> usize { - let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.latest_height; - (*inner_val) +pub extern "C" fn ChainParameters_get_best_block(this_ptr: &ChainParameters) -> crate::ln::channelmanager::BestBlock { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.best_block; + crate::ln::channelmanager::BestBlock { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false } } -/// The height of the latest block successfully connected. +/// The hash and height of the latest block successfully connected. /// /// Used to track on-chain channel funding outputs and send payments with reliable timelocks. #[no_mangle] -pub extern "C" fn ChainParameters_set_latest_height(this_ptr: &mut ChainParameters, mut val: usize) { - unsafe { &mut *this_ptr.inner }.latest_height = val; +pub extern "C" fn ChainParameters_set_best_block(this_ptr: &mut ChainParameters, mut val: crate::ln::channelmanager::BestBlock) { + unsafe { &mut *this_ptr.inner }.best_block = *unsafe { Box::from_raw(val.take_inner()) }; } /// Constructs a new ChainParameters given each field #[must_use] #[no_mangle] -pub extern "C" fn ChainParameters_new(mut network_arg: crate::bitcoin::network::Network, mut latest_hash_arg: crate::c_types::ThirtyTwoBytes, mut latest_height_arg: usize) -> ChainParameters { +pub extern "C" fn ChainParameters_new(mut network_arg: crate::bitcoin::network::Network, mut best_block_arg: crate::ln::channelmanager::BestBlock) -> ChainParameters { ChainParameters { inner: Box::into_raw(Box::new(nativeChainParameters { network: network_arg.into_bitcoin(), - latest_hash: ::bitcoin::hash_types::BlockHash::from_slice(&latest_hash_arg.data[..]).unwrap(), - latest_height: latest_height_arg, + best_block: *unsafe { Box::from_raw(best_block_arg.take_inner()) }, })), is_owned: true } } + +use lightning::ln::channelmanager::BestBlock as nativeBestBlockImport; +type nativeBestBlock = nativeBestBlockImport; + +/// The best known block as identified by its hash and height. +#[must_use] +#[repr(C)] +pub struct BestBlock { + /// A pointer to the opaque Rust object. + + /// Nearly everywhere, inner must be non-null, however in places where + /// the Rust equivalent takes an Option, it may be set to null to indicate None. + pub inner: *mut nativeBestBlock, + /// Indicates that this is the only struct which contains the same pointer. + + /// Rust functions which take ownership of an object provided via an argument require + /// this to be true and invalidate the object pointed to by inner. + pub is_owned: bool, +} + +impl Drop for BestBlock { + fn drop(&mut self) { + if self.is_owned && !<*mut nativeBestBlock>::is_null(self.inner) { + let _ = unsafe { Box::from_raw(self.inner) }; + } + } +} +/// Frees any resources used by the BestBlock, if is_owned is set and inner is non-NULL. +#[no_mangle] +pub extern "C" fn BestBlock_free(this_obj: BestBlock) { } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +extern "C" fn BestBlock_free_void(this_ptr: *mut c_void) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeBestBlock); } +} +#[allow(unused)] +/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy +impl BestBlock { + pub(crate) fn take_inner(mut self) -> *mut nativeBestBlock { + assert!(self.is_owned); + let ret = self.inner; + self.inner = std::ptr::null_mut(); + ret + } +} +impl Clone for BestBlock { + fn clone(&self) -> Self { + Self { + inner: if <*mut nativeBestBlock>::is_null(self.inner) { std::ptr::null_mut() } else { + Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, + is_owned: true, + } + } +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn BestBlock_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeBestBlock)).clone() })) as *mut c_void +} +#[no_mangle] +/// Creates a copy of the BestBlock +pub extern "C" fn BestBlock_clone(orig: &BestBlock) -> BestBlock { + orig.clone() +} +/// Returns the best block from the genesis of the given network. +#[must_use] +#[no_mangle] +pub extern "C" fn BestBlock_from_genesis(mut network: crate::bitcoin::network::Network) -> BestBlock { + let mut ret = lightning::ln::channelmanager::BestBlock::from_genesis(network.into_bitcoin()); + BestBlock { inner: Box::into_raw(Box::new(ret)), is_owned: true } +} + +/// Returns the best block as identified by the given block hash and height. +#[must_use] +#[no_mangle] +pub extern "C" fn BestBlock_new(mut block_hash: crate::c_types::ThirtyTwoBytes, mut height: u32) -> BestBlock { + let mut ret = lightning::ln::channelmanager::BestBlock::new(::bitcoin::hash_types::BlockHash::from_slice(&block_hash.data[..]).unwrap(), height); + BestBlock { inner: Box::into_raw(Box::new(ret)), is_owned: true } +} + +/// Returns the best block hash. +#[must_use] +#[no_mangle] +pub extern "C" fn BestBlock_block_hash(this_arg: &BestBlock) -> crate::c_types::ThirtyTwoBytes { + let mut ret = unsafe { &*this_arg.inner }.block_hash(); + crate::c_types::ThirtyTwoBytes { data: ret.into_inner() } +} + +/// Returns the best block height. +#[must_use] +#[no_mangle] +pub extern "C" fn BestBlock_height(this_arg: &BestBlock) -> u32 { + let mut ret = unsafe { &*this_arg.inner }.height(); + ret +} + /// The amount of time in blocks we require our counterparty wait to claim their money (ie time /// between when we, or our watchtower, must check for them having broadcast a theft transaction). /// @@ -595,6 +679,14 @@ pub extern "C" fn ChannelManager_new(mut fee_est: crate::chain::chaininterface:: ChannelManager { inner: Box::into_raw(Box::new(ret)), is_owned: true } } +/// Gets the current configuration applied to all new channels, as +#[must_use] +#[no_mangle] +pub extern "C" fn ChannelManager_get_current_default_configuration(this_arg: &ChannelManager) -> crate::util::config::UserConfig { + let mut ret = unsafe { &*this_arg.inner }.get_current_default_configuration(); + crate::util::config::UserConfig { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false } +} + /// Creates a new outbound channel to the given remote node and with the given value. /// /// user_id will be provided back as user_channel_id in FundingGenerationReady events to allow @@ -780,8 +872,8 @@ pub extern "C" fn ChannelManager_process_pending_htlc_forwards(this_arg: &Channe /// /// Note that in some rare cases this may generate a `chain::Watch::update_channel` call. #[no_mangle] -pub extern "C" fn ChannelManager_timer_chan_freshness_every_min(this_arg: &ChannelManager) { - unsafe { &*this_arg.inner }.timer_chan_freshness_every_min() +pub extern "C" fn ChannelManager_timer_tick_occurred(this_arg: &ChannelManager) { + unsafe { &*this_arg.inner }.timer_tick_occurred() } /// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect @@ -979,6 +1071,52 @@ pub extern "C" fn ChannelManager_update_best_block(this_arg: &ChannelManager, he unsafe { &*this_arg.inner }.update_best_block(&::bitcoin::consensus::encode::deserialize(unsafe { &*header }).unwrap(), height) } +/// Gets the set of txids which should be monitored for their confirmation state. +/// +/// If you're providing information about reorganizations via [`transaction_unconfirmed`], this +/// is the set of transactions which you may need to call [`transaction_unconfirmed`] for. +/// +/// This may be useful to poll to determine the set of transactions which must be registered +/// with an Electrum server or for which an Electrum server needs to be polled to determine +/// transaction confirmation state. +/// +/// This may update after any [`transactions_confirmed`] or [`block_connected`] call. +/// +/// Note that this is NOT the set of transactions which must be included in calls to +/// [`transactions_confirmed`] if they are confirmed, but a small subset of it. +/// +/// [`transactions_confirmed`]: Self::transactions_confirmed +/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed +/// [`block_connected`]: chain::Listen::block_connected +#[must_use] +#[no_mangle] +pub extern "C" fn ChannelManager_get_relevant_txids(this_arg: &ChannelManager) -> crate::c_types::derived::CVec_TxidZ { + let mut ret = unsafe { &*this_arg.inner }.get_relevant_txids(); + let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::c_types::ThirtyTwoBytes { data: item.into_inner() } }); }; + local_ret.into() +} + +/// Marks a transaction as having been reorganized out of the blockchain. +/// +/// If a transaction is included in [`get_relevant_txids`], and is no longer in the main branch +/// of the blockchain, this function should be called to indicate that the transaction should +/// be considered reorganized out. +/// +/// Once this is called, the given transaction will no longer appear on [`get_relevant_txids`], +/// though this may be called repeatedly for a given transaction without issue. +/// +/// Note that if the transaction is confirmed on the main chain in a different block (indicated +/// via a call to [`transactions_confirmed`]), it may re-appear in [`get_relevant_txids`], thus +/// be very wary of race-conditions wherein the final state of a transaction indicated via +/// these APIs is not the same as its state on the blockchain. +/// +/// [`transactions_confirmed`]: Self::transactions_confirmed +/// [`get_relevant_txids`]: Self::get_relevant_txids +#[no_mangle] +pub extern "C" fn ChannelManager_transaction_unconfirmed(this_arg: &ChannelManager, txid: *const [u8; 32]) { + unsafe { &*this_arg.inner }.transaction_unconfirmed(&::bitcoin::hash_types::Txid::from_slice(&unsafe { &*txid }[..]).unwrap()) +} + /// Blocks until ChannelManager needs to be persisted or a timeout is reached. It returns a bool /// indicating whether persistence is necessary. Only one listener on /// `await_persistable_update` or `await_persistable_update_timeout` is guaranteed to be woken -- 2.39.5