Tweak documentation in `BestBlock` to be a bit more clear
[rust-lightning] / lightning / src / chain / mod.rs
index 7d25a066076380896b54040edd3c361faf6494d2..cec09459233daef7b9c38582e17f984d10fa0d6d 100644 (file)
 //! Structs and traits which allow other parts of rust-lightning to interact with the blockchain.
 
 use bitcoin::blockdata::block::{Block, BlockHeader};
+use bitcoin::blockdata::constants::genesis_block;
 use bitcoin::blockdata::script::Script;
 use bitcoin::blockdata::transaction::{Transaction, TxOut};
 use bitcoin::hash_types::{BlockHash, Txid};
+use bitcoin::network::constants::Network;
 
 use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, MonitorEvent};
 use chain::keysinterface::Sign;
 use chain::transaction::{OutPoint, TransactionData};
 
+use prelude::*;
+
 pub mod chaininterface;
 pub mod chainmonitor;
 pub mod channelmonitor;
 pub mod transaction;
 pub mod keysinterface;
+pub(crate) mod onchaintx;
+pub(crate) mod package;
+
+/// The best known block as identified by its hash and height.
+#[derive(Clone, Copy, PartialEq)]
+pub struct BestBlock {
+       block_hash: BlockHash,
+       height: u32,
+}
+
+impl BestBlock {
+       /// Constructs a `BestBlock` that represents the genesis block at height 0 of the given
+       /// network.
+       pub fn from_genesis(network: Network) -> Self {
+               BestBlock {
+                       block_hash: genesis_block(network).header.block_hash(),
+                       height: 0,
+               }
+       }
+
+       /// Returns a `BestBlock` as identified by the given block hash and height.
+       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 }
+}
 
 /// An error when accessing the chain via [`Access`].
 #[derive(Clone)]
@@ -36,7 +71,7 @@ pub enum AccessError {
 
 /// The `Access` trait defines behavior for accessing chain data and state, such as blocks and
 /// UTXOs.
-pub trait Access: Send + Sync {
+pub trait Access {
        /// Returns the transaction output of a funding transaction encoded by [`short_channel_id`].
        /// Returns an error if `genesis_hash` is for a different chain or if such a transaction output
        /// is unknown.
@@ -104,7 +139,7 @@ pub trait Confirm {
        /// in the event of a chain reorganization, it must not be called with a `header` that is no
        /// longer in the chain as of the last call to [`best_block_updated`].
        ///
-       /// [chain order]: Self#order
+       /// [chain order]: Confirm#Order
        /// [`best_block_updated`]: Self::best_block_updated
        fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32);
 
@@ -161,7 +196,7 @@ pub trait Confirm {
 /// [`ChannelMonitor`]: channelmonitor::ChannelMonitor
 /// [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
 /// [`PermanentFailure`]: channelmonitor::ChannelMonitorUpdateErr::PermanentFailure
-pub trait Watch<ChannelSigner: Sign>: Send + Sync {
+pub trait Watch<ChannelSigner: Sign> {
        /// Watches a channel identified by `funding_txo` using `monitor`.
        ///
        /// Implementations are responsible for watching the chain for the funding transaction along
@@ -207,7 +242,7 @@ pub trait Watch<ChannelSigner: Sign>: Send + Sync {
 /// [`TemporaryFailure`]: channelmonitor::ChannelMonitorUpdateErr::TemporaryFailure
 /// [BIP 157]: https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki
 /// [BIP 158]: https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki
-pub trait Filter: Send + Sync {
+pub trait Filter {
        /// Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
        /// a spending condition.
        fn register_tx(&self, txid: &Txid, script_pubkey: &Script);
@@ -235,6 +270,7 @@ pub trait Filter: Send + Sync {
 ///
 /// [`ChannelMonitor`]: channelmonitor::ChannelMonitor
 /// [`ChannelMonitor::block_connected`]: channelmonitor::ChannelMonitor::block_connected
+#[derive(Clone, PartialEq, Hash)]
 pub struct WatchedOutput {
        /// First block where the transaction output may have been spent.
        pub block_hash: Option<BlockHash>,
@@ -246,7 +282,7 @@ pub struct WatchedOutput {
        pub script_pubkey: Script,
 }
 
-impl<T: Listen> Listen for std::ops::Deref<Target = T> {
+impl<T: Listen> Listen for core::ops::Deref<Target = T> {
        fn block_connected(&self, block: &Block, height: u32) {
                (**self).block_connected(block, height);
        }
@@ -256,7 +292,7 @@ impl<T: Listen> Listen for std::ops::Deref<Target = T> {
        }
 }
 
-impl<T: std::ops::Deref, U: std::ops::Deref> Listen for (T, U)
+impl<T: core::ops::Deref, U: core::ops::Deref> Listen for (T, U)
 where
        T::Target: Listen,
        U::Target: Listen,