Merge pull request #3144 from TheBlueMatt/2024-06-message-flags
[rust-lightning] / lightning / src / chain / mod.rs
index 239f7c91dbc5d09c433db0ccf53306ca43385034..e43c08d55e513a906000d52a2fd6795580bb5037 100644 (file)
@@ -13,13 +13,16 @@ use bitcoin::blockdata::block::{Block, Header};
 use bitcoin::blockdata::constants::genesis_block;
 use bitcoin::blockdata::script::{Script, ScriptBuf};
 use bitcoin::hash_types::{BlockHash, Txid};
-use bitcoin::network::constants::Network;
+use bitcoin::network::Network;
 use bitcoin::secp256k1::PublicKey;
 
 use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, MonitorEvent};
-use crate::sign::WriteableEcdsaChannelSigner;
+use crate::ln::types::ChannelId;
+use crate::sign::ecdsa::EcdsaChannelSigner;
 use crate::chain::transaction::{OutPoint, TransactionData};
+use crate::impl_writeable_tlv_based;
 
+#[allow(unused_imports)]
 use crate::prelude::*;
 
 pub mod chaininterface;
@@ -30,10 +33,12 @@ pub(crate) mod onchaintx;
 pub(crate) mod package;
 
 /// The best known block as identified by its hash and height.
-#[derive(Clone, Copy, PartialEq, Eq)]
+#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
 pub struct BestBlock {
-       block_hash: BlockHash,
-       height: u32,
+       /// The block's hash
+       pub block_hash: BlockHash,
+       /// The height at which the block was confirmed.
+       pub height: u32,
 }
 
 impl BestBlock {
@@ -47,17 +52,19 @@ impl BestBlock {
        }
 
        /// Returns a `BestBlock` as identified by the given block hash and height.
+       ///
+       /// This is not exported to bindings users directly as the bindings auto-generate an
+       /// equivalent `new`.
        pub fn new(block_hash: BlockHash, height: u32) -> Self {
                BestBlock { block_hash, height }
        }
-
-       /// Returns the best block hash.
-       pub fn block_hash(&self) -> BlockHash { self.block_hash }
-
-       /// Returns the best block height.
-       pub fn height(&self) -> u32 { self.height }
 }
 
+impl_writeable_tlv_based!(BestBlock, {
+       (0, block_hash, required),
+       (2, height, required),
+});
+
 
 /// The `Listen` trait is used to notify when blocks have been connected or disconnected from the
 /// chain.
@@ -152,7 +159,7 @@ pub trait Confirm {
        /// blocks.
        fn best_block_updated(&self, header: &Header, height: u32);
        /// Returns transactions that must be monitored for reorganization out of the chain along
-       /// with the hash of the block as part of which it had been previously confirmed.
+       /// with the height and the hash of the block as part of which it had been previously confirmed.
        ///
        /// Note that the returned `Option<BlockHash>` might be `None` for channels created with LDK
        /// 0.0.112 and prior, in which case you need to manually track previous confirmations.
@@ -167,12 +174,12 @@ pub trait Confirm {
        /// given to [`transaction_unconfirmed`].
        ///
        /// If any of the returned transactions are confirmed in a block other than the one with the
-       /// given hash, they need to be unconfirmed and reconfirmed via [`transaction_unconfirmed`] and
-       /// [`transactions_confirmed`], respectively.
+       /// given hash at the given height, they need to be unconfirmed and reconfirmed via
+       /// [`transaction_unconfirmed`] and [`transactions_confirmed`], respectively.
        ///
        /// [`transactions_confirmed`]: Self::transactions_confirmed
        /// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
-       fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)>;
+       fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)>;
 }
 
 /// An enum representing the status of a channel monitor update persistence.
@@ -253,7 +260,7 @@ pub enum ChannelMonitorUpdateStatus {
 /// application crashes.
 ///
 /// See method documentation and [`ChannelMonitorUpdateStatus`] for specific requirements.
-pub trait Watch<ChannelSigner: WriteableEcdsaChannelSigner> {
+pub trait Watch<ChannelSigner: EcdsaChannelSigner> {
        /// Watches a channel identified by `funding_txo` using `monitor`.
        ///
        /// Implementations are responsible for watching the chain for the funding transaction along
@@ -297,7 +304,7 @@ pub trait Watch<ChannelSigner: WriteableEcdsaChannelSigner> {
        ///
        /// For details on asynchronous [`ChannelMonitor`] updating and returning
        /// [`MonitorEvent::Completed`] here, see [`ChannelMonitorUpdateStatus::InProgress`].
-       fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>, Option<PublicKey>)>;
+       fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)>;
 }
 
 /// The `Filter` trait defines behavior for indicating chain activity of interest pertaining to
@@ -323,6 +330,11 @@ pub trait Watch<ChannelSigner: WriteableEcdsaChannelSigner> {
 pub trait Filter {
        /// Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
        /// a spending condition.
+       ///
+       /// This may be used, for example, to monitor for when a funding transaction confirms.
+       ///
+       /// The `script_pubkey` is provided for informational purposes and may be useful for block
+       /// sources which only support filtering on scripts.
        fn register_tx(&self, txid: &Txid, script_pubkey: &Script);
 
        /// Registers interest in spends of a transaction output.
@@ -331,6 +343,9 @@ pub trait Filter {
        /// to ensure that also dependent output spents within an already connected block are correctly
        /// handled, e.g., by re-scanning the block in question whenever new outputs have been
        /// registered mid-processing.
+       ///
+       /// This may be used, for example, to monitor for when a funding output is spent (by any
+       /// transaction).
        fn register_output(&self, output: WatchedOutput);
 }
 
@@ -343,6 +358,9 @@ pub trait Filter {
 /// If `block_hash` is `Some`, this indicates the output was created in the corresponding block and
 /// may have been spent there. See [`Filter::register_output`] for details.
 ///
+/// Depending on your block source, you may need one or both of either [`Self::outpoint`] or
+/// [`Self::script_pubkey`].
+///
 /// [`ChannelMonitor`]: channelmonitor::ChannelMonitor
 /// [`ChannelMonitor::block_connected`]: channelmonitor::ChannelMonitor::block_connected
 #[derive(Clone, PartialEq, Eq, Hash)]
@@ -357,7 +375,7 @@ pub struct WatchedOutput {
        pub script_pubkey: ScriptBuf,
 }
 
-impl<T: Listen> Listen for core::ops::Deref<Target = T> {
+impl<T: Listen> Listen for dyn core::ops::Deref<Target = T> {
        fn filtered_block_connected(&self, header: &Header, txdata: &TransactionData, height: u32) {
                (**self).filtered_block_connected(header, txdata, height);
        }