X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-block-sync%2Fsrc%2Fpoll.rs;h=9f7e8becf5060c2f62471825a2aa0cfeca573f5e;hb=14ee173593e6ee5501e10767ffa6ce1533a9bb2e;hp=2bb2f4a07df9e4a0aa79759249d795cc81cd833d;hpb=c1938e8c9fb5d531dd735e889b2ab0f7bc8580b8;p=rust-lightning diff --git a/lightning-block-sync/src/poll.rs b/lightning-block-sync/src/poll.rs index 2bb2f4a0..9f7e8bec 100644 --- a/lightning-block-sync/src/poll.rs +++ b/lightning-block-sync/src/poll.rs @@ -4,6 +4,7 @@ use crate::{AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource, Blo use bitcoin::hash_types::BlockHash; use bitcoin::network::constants::Network; +use lightning::chain::BestBlock; use std::ops::Deref; @@ -29,7 +30,7 @@ pub trait Poll { } /// A chain tip relative to another chain tip in terms of block hash and chainwork. -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub enum ChainTip { /// A chain tip with the same hash as another chain's tip. Common, @@ -60,7 +61,7 @@ impl Validate for BlockHeaderData { fn validate(self, block_hash: BlockHash) -> BlockSourceResult { let pow_valid_block_hash = self.header .validate_pow(&self.header.target()) - .or_else(|e| Err(BlockSourceError::persistent(e)))?; + .map_err(BlockSourceError::persistent)?; if pow_valid_block_hash != block_hash { return Err(BlockSourceError::persistent("invalid block hash")); @@ -81,7 +82,7 @@ impl Validate for BlockData { let pow_valid_block_hash = header .validate_pow(&header.target()) - .or_else(|e| Err(BlockSourceError::persistent(e)))?; + .map_err(BlockSourceError::persistent)?; if pow_valid_block_hash != block_hash { return Err(BlockSourceError::persistent("invalid block hash")); @@ -102,7 +103,7 @@ impl Validate for BlockData { } /// A block header with validated proof of work and corresponding block hash. -#[derive(Clone, Copy, Debug, PartialEq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ValidatedBlockHeader { pub(crate) block_hash: BlockHash, inner: BlockHeaderData, @@ -146,6 +147,19 @@ impl ValidatedBlockHeader { Ok(()) } + + /// Returns the [`BestBlock`] corresponding to this validated block header, which can be passed + /// into [`ChannelManager::new`] as part of its [`ChainParameters`]. Useful for ensuring that + /// the [`SpvClient`] and [`ChannelManager`] are initialized to the same block during a fresh + /// start. + /// + /// [`SpvClient`]: crate::SpvClient + /// [`ChainParameters`]: lightning::ln::channelmanager::ChainParameters + /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager + /// [`ChannelManager::new`]: lightning::ln::channelmanager::ChannelManager::new + pub fn to_best_block(&self) -> BestBlock { + BestBlock::new(self.block_hash, self.inner.height) + } } /// A block with validated data against its transaction list and corresponding block hash.