Merge pull request #1693 from valentinewallace/2022-09-readme-updates
[rust-lightning] / lightning-block-sync / src / poll.rs
index b32d2239f69fd4b37513afc6a90076f768679a58..2bb2f4a07df9e4a0aa79759249d795cc81cd833d 100644 (file)
@@ -1,8 +1,7 @@
 //! Adapters that make one or more [`BlockSource`]s simpler to poll for new chain tip transitions.
 
-use crate::{AsyncBlockSourceResult, BlockHeaderData, BlockSource, BlockSourceError, BlockSourceResult};
+use crate::{AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource, BlockSourceError, BlockSourceResult};
 
-use bitcoin::blockdata::block::Block;
 use bitcoin::hash_types::BlockHash;
 use bitcoin::network::constants::Network;
 
@@ -59,12 +58,11 @@ impl Validate for BlockHeaderData {
        type T = ValidatedBlockHeader;
 
        fn validate(self, block_hash: BlockHash) -> BlockSourceResult<Self::T> {
-               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,25 +70,31 @@ impl Validate for BlockHeaderData {
        }
 }
 
-impl Validate for Block {
+impl Validate for BlockData {
        type T = ValidatedBlock;
 
        fn validate(self, block_hash: BlockHash) -> BlockSourceResult<Self::T> {
-               self.header
-                       .validate_pow(&self.header.target())
+               let header = match &self {
+                       BlockData::FullBlock(block) => &block.header,
+                       BlockData::HeaderOnly(header) => header,
+               };
+
+               let pow_valid_block_hash = header
+                       .validate_pow(&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"));
                }
 
-               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 })
@@ -147,11 +151,11 @@ impl ValidatedBlockHeader {
 /// 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
@@ -163,19 +167,19 @@ mod sealed {
        pub trait Validate {}
 
        impl Validate for crate::BlockHeaderData {}
-       impl Validate for bitcoin::blockdata::block::Block {}
+       impl Validate for crate::BlockData {}
 }
 
 /// The canonical `Poll` implementation used for a single `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<B: Deref<Target=T> + Sized, T: BlockSource> {
+pub struct ChainPoller<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> {
        block_source: B,
        network: Network,
 }
 
-impl<B: Deref<Target=T> + Sized, T: BlockSource> ChainPoller<B, T> {
+impl<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> ChainPoller<B, T> {
        /// Creates a new poller for the given block source.
        ///
        /// If the `network` parameter is mainnet, then the difficulty between blocks is checked for
@@ -185,7 +189,7 @@ impl<B: Deref<Target=T> + Sized, T: BlockSource> ChainPoller<B, T> {
        }
 }
 
-impl<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource> Poll for ChainPoller<B, T> {
+impl<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> Poll for ChainPoller<B, T> {
        fn poll_chain_tip<'a>(&'a self, best_known_chain_tip: ValidatedBlockHeader) ->
                AsyncBlockSourceResult<'a, ChainTip>
        {