Expose the current best chain tip from ChannelManager + Monitors
[rust-lightning] / lightning / src / chain / mod.rs
index 8f27e40baeaf1e3b5d43dade8a05da0bb08dfdcb..2e1eaea4e5f5b7d54c9828f4b6db769388f0ec89 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;
@@ -28,6 +30,34 @@ 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 {
+       /// Returns the best block from the genesis of the given network.
+       pub fn from_genesis(network: Network) -> Self {
+               BestBlock {
+                       block_hash: genesis_block(network).header.block_hash(),
+                       height: 0,
+               }
+       }
+
+       /// Returns the best block 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)]
 pub enum AccessError {