X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-block-sync%2Fsrc%2Fpoll.rs;h=4c6cb0c0600725e9cf3552045865e8b5be472199;hb=f99301dd8a7f056c0ed09ec827fddc00f325b3e5;hp=3ff7606b8d9cfdd8db1f9a936c14d4624c9e8945;hpb=05ec06129c00655523780b2ea6ce57a538456164;p=rust-lightning diff --git a/lightning-block-sync/src/poll.rs b/lightning-block-sync/src/poll.rs index 3ff7606b..4c6cb0c0 100644 --- a/lightning-block-sync/src/poll.rs +++ b/lightning-block-sync/src/poll.rs @@ -1,10 +1,12 @@ +//! Adapters that make one or more [`BlockSource`]s simpler to poll for new chain tip transitions. + use crate::{AsyncBlockSourceResult, BlockHeaderData, BlockSource, BlockSourceError, BlockSourceResult}; use bitcoin::blockdata::block::Block; use bitcoin::hash_types::BlockHash; use bitcoin::network::constants::Network; -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,15 +17,15 @@ 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>; } @@ -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 + let pow_valid_block_hash = self.header .validate_pow(&self.header.target()) .or_else(|e| Err(BlockSourceError::persistent(e)))?; - // 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")); } @@ -72,12 +75,11 @@ impl Validate for Block { type T = ValidatedBlock; fn validate(self, block_hash: BlockHash) -> BlockSourceResult { - self.header + let pow_valid_block_hash = self.header .validate_pow(&self.header.target()) .or_else(|e| Err(BlockSourceError::persistent(e)))?; - // 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")); } @@ -96,7 +98,7 @@ impl Validate for Block { /// A block header with validated proof of work and corresponding block hash. #[derive(Clone, Copy, Debug, PartialEq)] pub struct ValidatedBlockHeader { - block_hash: BlockHash, + pub(crate) block_hash: BlockHash, inner: BlockHeaderData, } @@ -142,7 +144,7 @@ impl ValidatedBlockHeader { /// A block with validated data against its transaction list and corresponding block hash. pub struct ValidatedBlock { - block_hash: BlockHash, + pub(crate) block_hash: BlockHash, inner: Block, } @@ -154,16 +156,24 @@ impl std::ops::Deref for ValidatedBlock { } } +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 bitcoin::blockdata::block::Block {} +} + /// 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 +183,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 +204,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 +223,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 { @@ -237,7 +247,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 +259,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); @@ -271,7 +281,7 @@ mod tests { chain.blocks.last_mut().unwrap().header.bits = BlockHeader::compact_target_from_u256(&Uint256::from_be_bytes([0; 32])); - 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 +293,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 +308,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 +328,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 +343,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 +352,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)),