X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-block-sync%2Fsrc%2Fpoll.rs;h=dcc19a4969dd3eab863c1216b17d434b93f54849;hb=2f734f97550014e5424e55523ed46d76b94b737d;hp=34be2437c8e2ddae1a3f2c07d43de702c036d10b;hpb=b1ecfe705d77b1338c7df5e208d76eb6743f270c;p=rust-lightning diff --git a/lightning-block-sync/src/poll.rs b/lightning-block-sync/src/poll.rs index 34be2437..dcc19a49 100644 --- a/lightning-block-sync/src/poll.rs +++ b/lightning-block-sync/src/poll.rs @@ -1,10 +1,12 @@ -use crate::{AsyncBlockSourceResult, BlockHeaderData, BlockSource, BlockSourceError, BlockSourceResult}; +//! Adapters that make one or more [`BlockSource`]s simpler to poll for new chain tip transitions. + +use crate::{AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource, BlockSourceError, BlockSourceResult}; -use bitcoin::blockdata::block::Block; use bitcoin::hash_types::BlockHash; use bitcoin::network::constants::Network; +use lightning::chain::BestBlock; -use std::ops::DerefMut; +use std::ops::Deref; /// The `Poll` trait defines behavior for polling block sources for a chain tip and retrieving /// related chain data. It serves as an adapter for `BlockSource`. @@ -15,20 +17,20 @@ use std::ops::DerefMut; /// [`ChainPoller`]: ../struct.ChainPoller.html pub trait Poll { /// Returns a chain tip in terms of its relationship to the provided chain tip. - fn poll_chain_tip<'a>(&'a mut self, best_known_chain_tip: ValidatedBlockHeader) -> + fn poll_chain_tip<'a>(&'a self, best_known_chain_tip: ValidatedBlockHeader) -> AsyncBlockSourceResult<'a, ChainTip>; /// Returns the header that preceded the given header in the chain. - fn look_up_previous_header<'a>(&'a mut self, header: &'a ValidatedBlockHeader) -> + fn look_up_previous_header<'a>(&'a self, header: &'a ValidatedBlockHeader) -> AsyncBlockSourceResult<'a, ValidatedBlockHeader>; /// Returns the block associated with the given header. - fn fetch_block<'a>(&'a mut self, header: &'a ValidatedBlockHeader) -> + fn fetch_block<'a>(&'a self, header: &'a ValidatedBlockHeader) -> AsyncBlockSourceResult<'a, ValidatedBlock>; } /// 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, @@ -42,7 +44,9 @@ pub enum ChainTip { } /// The `Validate` trait defines behavior for validating chain data. -pub(crate) trait Validate { +/// +/// This trait is sealed and not meant to be implemented outside of this crate. +pub trait Validate: sealed::Validate { /// The validated data wrapper which can be dereferenced to obtain the validated data. type T: std::ops::Deref; @@ -55,12 +59,11 @@ impl Validate for BlockHeaderData { type T = ValidatedBlockHeader; fn validate(self, block_hash: BlockHash) -> BlockSourceResult { - self.header - .validate_pow(&self.header.target()) - .or_else(|e| Err(BlockSourceError::persistent(e)))?; + let pow_valid_block_hash = self.header + .validate_pow(self.header.target()) + .map_err(BlockSourceError::persistent)?; - // TODO: Use the result of validate_pow instead of recomputing the block hash once upstream. - if self.header.block_hash() != block_hash { + if pow_valid_block_hash != block_hash { return Err(BlockSourceError::persistent("invalid block hash")); } @@ -68,25 +71,31 @@ impl Validate for BlockHeaderData { } } -impl Validate for Block { +impl Validate for BlockData { type T = ValidatedBlock; fn validate(self, block_hash: BlockHash) -> BlockSourceResult { - self.header - .validate_pow(&self.header.target()) - .or_else(|e| Err(BlockSourceError::persistent(e)))?; + let header = match &self { + BlockData::FullBlock(block) => &block.header, + BlockData::HeaderOnly(header) => header, + }; + + let pow_valid_block_hash = header + .validate_pow(header.target()) + .map_err(BlockSourceError::persistent)?; - // TODO: Use the result of validate_pow instead of recomputing the block hash once upstream. - if self.block_hash() != block_hash { + if pow_valid_block_hash != block_hash { return Err(BlockSourceError::persistent("invalid block hash")); } - if !self.check_merkle_root() { - return Err(BlockSourceError::persistent("invalid merkle root")); - } + if let BlockData::FullBlock(block) = &self { + if !block.check_merkle_root() { + return Err(BlockSourceError::persistent("invalid merkle root")); + } - if !self.check_witness_commitment() { - return Err(BlockSourceError::persistent("invalid witness commitment")); + if !block.check_witness_commitment() { + return Err(BlockSourceError::persistent("invalid witness commitment")); + } } Ok(ValidatedBlock { block_hash, inner: self }) @@ -94,7 +103,7 @@ impl Validate for Block { } /// 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, @@ -127,8 +136,11 @@ impl ValidatedBlockHeader { if let Network::Bitcoin = network { if self.height % 2016 == 0 { - let previous_work = previous_header.header.work(); - if work > (previous_work << 2) || work < (previous_work >> 2) { + let target = self.header.target(); + let previous_target = previous_header.header.target(); + let min_target = previous_target.min_difficulty_transition_threshold(); + let max_target = previous_target.max_difficulty_transition_threshold(); + if target > max_target || target < min_target { return Err(BlockSourceError::persistent("invalid difficulty transition")) } } else if self.header.bits != previous_header.header.bits { @@ -138,32 +150,53 @@ 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. pub struct ValidatedBlock { pub(crate) block_hash: BlockHash, - inner: Block, + inner: BlockData, } impl std::ops::Deref for ValidatedBlock { - type Target = Block; + type Target = BlockData; fn deref(&self) -> &Self::Target { &self.inner } } +mod sealed { + /// Used to prevent implementing [`super::Validate`] outside the crate but still allow its use. + pub trait Validate {} + + impl Validate for crate::BlockHeaderData {} + impl Validate for crate::BlockData {} +} + /// The canonical `Poll` implementation used for a single `BlockSource`. /// -/// Other `Poll` implementations must be built using `ChainPoller` as it provides the only means of -/// validating chain data. -pub struct ChainPoller + Sized + Sync + Send, T: BlockSource> { +/// Other `Poll` implementations should be built using `ChainPoller` as it provides the simplest way +/// of validating chain data and checking consistency. +pub struct ChainPoller + Sized + Send + Sync, T: BlockSource + ?Sized> { block_source: B, network: Network, } -impl + Sized + Sync + Send, T: BlockSource> ChainPoller { +impl + Sized + Send + Sync, T: BlockSource + ?Sized> ChainPoller { /// Creates a new poller for the given block source. /// /// If the `network` parameter is mainnet, then the difficulty between blocks is checked for @@ -173,8 +206,8 @@ impl + Sized + Sync + Send, T: BlockSource> ChainPoller + Sized + Sync + Send, T: BlockSource> Poll for ChainPoller { - fn poll_chain_tip<'a>(&'a mut self, best_known_chain_tip: ValidatedBlockHeader) -> +impl + Sized + Send + Sync, T: BlockSource + ?Sized> Poll for ChainPoller { + fn poll_chain_tip<'a>(&'a self, best_known_chain_tip: ValidatedBlockHeader) -> AsyncBlockSourceResult<'a, ChainTip> { Box::pin(async move { @@ -194,7 +227,7 @@ impl + Sized + Sync + Send, T: BlockSource> Poll for Chain }) } - fn look_up_previous_header<'a>(&'a mut self, header: &'a ValidatedBlockHeader) -> + fn look_up_previous_header<'a>(&'a self, header: &'a ValidatedBlockHeader) -> AsyncBlockSourceResult<'a, ValidatedBlockHeader> { Box::pin(async move { @@ -213,7 +246,7 @@ impl + Sized + Sync + Send, T: BlockSource> Poll for Chain }) } - fn fetch_block<'a>(&'a mut self, header: &'a ValidatedBlockHeader) -> + fn fetch_block<'a>(&'a self, header: &'a ValidatedBlockHeader) -> AsyncBlockSourceResult<'a, ValidatedBlock> { Box::pin(async move { @@ -229,7 +262,6 @@ mod tests { use crate::*; use crate::test_utils::Blockchain; use super::*; - use bitcoin::util::uint::Uint256; #[tokio::test] async fn poll_empty_chain() { @@ -237,7 +269,7 @@ mod tests { let best_known_chain_tip = chain.tip(); chain.disconnect_tip(); - let mut poller = ChainPoller::new(&mut chain, Network::Bitcoin); + let poller = ChainPoller::new(&chain, Network::Bitcoin); match poller.poll_chain_tip(best_known_chain_tip).await { Err(e) => { assert_eq!(e.kind(), BlockSourceErrorKind::Transient); @@ -249,10 +281,10 @@ mod tests { #[tokio::test] async fn poll_chain_without_headers() { - let mut chain = Blockchain::default().with_height(1).without_headers(); + let chain = Blockchain::default().with_height(1).without_headers(); let best_known_chain_tip = chain.at_height(0); - let mut poller = ChainPoller::new(&mut chain, Network::Bitcoin); + let poller = ChainPoller::new(&chain, Network::Bitcoin); match poller.poll_chain_tip(best_known_chain_tip).await { Err(e) => { assert_eq!(e.kind(), BlockSourceErrorKind::Persistent); @@ -269,9 +301,9 @@ mod tests { // Invalidate the tip by changing its target. chain.blocks.last_mut().unwrap().header.bits = - BlockHeader::compact_target_from_u256(&Uint256::from_be_bytes([0; 32])); + bitcoin::Target::from_be_bytes([0x01; 32]).to_compact_lossy(); - let mut poller = ChainPoller::new(&mut chain, Network::Bitcoin); + let poller = ChainPoller::new(&chain, Network::Bitcoin); match poller.poll_chain_tip(best_known_chain_tip).await { Err(e) => { assert_eq!(e.kind(), BlockSourceErrorKind::Persistent); @@ -283,10 +315,10 @@ mod tests { #[tokio::test] async fn poll_chain_with_malformed_headers() { - let mut chain = Blockchain::default().with_height(1).malformed_headers(); + let chain = Blockchain::default().with_height(1).malformed_headers(); let best_known_chain_tip = chain.at_height(0); - let mut poller = ChainPoller::new(&mut chain, Network::Bitcoin); + let poller = ChainPoller::new(&chain, Network::Bitcoin); match poller.poll_chain_tip(best_known_chain_tip).await { Err(e) => { assert_eq!(e.kind(), BlockSourceErrorKind::Persistent); @@ -298,10 +330,10 @@ mod tests { #[tokio::test] async fn poll_chain_with_common_tip() { - let mut chain = Blockchain::default().with_height(0); + let chain = Blockchain::default().with_height(0); let best_known_chain_tip = chain.tip(); - let mut poller = ChainPoller::new(&mut chain, Network::Bitcoin); + let poller = ChainPoller::new(&chain, Network::Bitcoin); match poller.poll_chain_tip(best_known_chain_tip).await { Err(e) => panic!("Unexpected error: {:?}", e), Ok(tip) => assert_eq!(tip, ChainTip::Common), @@ -318,7 +350,7 @@ mod tests { let worse_chain_tip = chain.tip(); assert_eq!(best_known_chain_tip.chainwork, worse_chain_tip.chainwork); - let mut poller = ChainPoller::new(&mut chain, Network::Bitcoin); + let poller = ChainPoller::new(&chain, Network::Bitcoin); match poller.poll_chain_tip(best_known_chain_tip).await { Err(e) => panic!("Unexpected error: {:?}", e), Ok(tip) => assert_eq!(tip, ChainTip::Worse(worse_chain_tip)), @@ -333,7 +365,7 @@ mod tests { chain.disconnect_tip(); let worse_chain_tip = chain.tip(); - let mut poller = ChainPoller::new(&mut chain, Network::Bitcoin); + let poller = ChainPoller::new(&chain, Network::Bitcoin); match poller.poll_chain_tip(best_known_chain_tip).await { Err(e) => panic!("Unexpected error: {:?}", e), Ok(tip) => assert_eq!(tip, ChainTip::Worse(worse_chain_tip)), @@ -342,12 +374,12 @@ mod tests { #[tokio::test] async fn poll_chain_with_better_tip() { - let mut chain = Blockchain::default().with_height(1); + let chain = Blockchain::default().with_height(1); let best_known_chain_tip = chain.at_height(0); let better_chain_tip = chain.tip(); - let mut poller = ChainPoller::new(&mut chain, Network::Bitcoin); + let poller = ChainPoller::new(&chain, Network::Bitcoin); match poller.poll_chain_tip(best_known_chain_tip).await { Err(e) => panic!("Unexpected error: {:?}", e), Ok(tip) => assert_eq!(tip, ChainTip::Better(better_chain_tip)),