+
+ /// Notifies the chain listeners of disconnected blocks.
+ fn disconnect_blocks(&mut self, mut disconnected_blocks: Vec<ValidatedBlockHeader>) {
+ for header in disconnected_blocks.drain(..) {
+ if let Some(cached_header) = self.header_cache.block_disconnected(&header.block_hash) {
+ assert_eq!(cached_header, header);
+ }
+ self.chain_listener.block_disconnected(&header.header, header.height);
+ }
+ }
+
+ /// Notifies the chain listeners of connected blocks.
+ async fn connect_blocks<P: Poll>(
+ &mut self,
+ mut new_tip: ValidatedBlockHeader,
+ mut connected_blocks: Vec<ValidatedBlockHeader>,
+ chain_poller: &mut P,
+ ) -> Result<(), (BlockSourceError, Option<ValidatedBlockHeader>)> {
+ for header in connected_blocks.drain(..).rev() {
+ let height = header.height;
+ let block_data = chain_poller
+ .fetch_block(&header).await
+ .or_else(|e| Err((e, Some(new_tip))))?;
+ debug_assert_eq!(block_data.block_hash, header.block_hash);
+
+ match block_data.deref() {
+ BlockData::FullBlock(block) => {
+ self.chain_listener.block_connected(&block, height);
+ },
+ BlockData::HeaderOnly(header) => {
+ self.chain_listener.filtered_block_connected(&header, &[], height);
+ },
+ }
+
+ self.header_cache.block_connected(header.block_hash, header);
+ new_tip = header;
+ }
+
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod spv_client_tests {
+ use crate::test_utils::{Blockchain, NullChainListener};
+ use super::*;
+
+ use bitcoin::network::constants::Network;
+
+ #[tokio::test]
+ async fn poll_from_chain_without_headers() {
+ let mut chain = Blockchain::default().with_height(3).without_headers();
+ let best_tip = chain.at_height(1);
+
+ let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
+ let mut cache = UnboundedCache::new();
+ let mut listener = NullChainListener {};
+ let mut client = SpvClient::new(best_tip, poller, &mut cache, &mut listener);
+ match client.poll_best_tip().await {
+ Err(e) => {
+ assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
+ assert_eq!(e.into_inner().as_ref().to_string(), "header not found");
+ },
+ Ok(_) => panic!("Expected error"),
+ }
+ assert_eq!(client.chain_tip, best_tip);
+ }
+
+ #[tokio::test]
+ async fn poll_from_chain_with_common_tip() {
+ let mut chain = Blockchain::default().with_height(3);
+ let common_tip = chain.tip();
+
+ let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
+ let mut cache = UnboundedCache::new();
+ let mut listener = NullChainListener {};
+ let mut client = SpvClient::new(common_tip, poller, &mut cache, &mut listener);
+ match client.poll_best_tip().await {
+ Err(e) => panic!("Unexpected error: {:?}", e),
+ Ok((chain_tip, blocks_connected)) => {
+ assert_eq!(chain_tip, ChainTip::Common);
+ assert!(!blocks_connected);
+ },
+ }
+ assert_eq!(client.chain_tip, common_tip);
+ }
+
+ #[tokio::test]
+ async fn poll_from_chain_with_better_tip() {
+ let mut chain = Blockchain::default().with_height(3);
+ let new_tip = chain.tip();
+ let old_tip = chain.at_height(1);
+
+ let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
+ let mut cache = UnboundedCache::new();
+ let mut listener = NullChainListener {};
+ let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener);
+ match client.poll_best_tip().await {
+ Err(e) => panic!("Unexpected error: {:?}", e),
+ Ok((chain_tip, blocks_connected)) => {
+ assert_eq!(chain_tip, ChainTip::Better(new_tip));
+ assert!(blocks_connected);
+ },
+ }
+ assert_eq!(client.chain_tip, new_tip);
+ }
+
+ #[tokio::test]
+ async fn poll_from_chain_with_better_tip_and_without_any_new_blocks() {
+ let mut chain = Blockchain::default().with_height(3).without_blocks(2..);
+ let new_tip = chain.tip();
+ let old_tip = chain.at_height(1);
+
+ let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
+ let mut cache = UnboundedCache::new();
+ let mut listener = NullChainListener {};
+ let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener);
+ match client.poll_best_tip().await {
+ Err(e) => panic!("Unexpected error: {:?}", e),
+ Ok((chain_tip, blocks_connected)) => {
+ assert_eq!(chain_tip, ChainTip::Better(new_tip));
+ assert!(!blocks_connected);
+ },
+ }
+ assert_eq!(client.chain_tip, old_tip);
+ }
+
+ #[tokio::test]
+ async fn poll_from_chain_with_better_tip_and_without_some_new_blocks() {
+ let mut chain = Blockchain::default().with_height(3).without_blocks(3..);
+ let new_tip = chain.tip();
+ let old_tip = chain.at_height(1);
+
+ let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
+ let mut cache = UnboundedCache::new();
+ let mut listener = NullChainListener {};
+ let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener);
+ match client.poll_best_tip().await {
+ Err(e) => panic!("Unexpected error: {:?}", e),
+ Ok((chain_tip, blocks_connected)) => {
+ assert_eq!(chain_tip, ChainTip::Better(new_tip));
+ assert!(blocks_connected);
+ },
+ }
+ assert_eq!(client.chain_tip, chain.at_height(2));
+ }
+
+ #[tokio::test]
+ async fn poll_from_chain_with_worse_tip() {
+ let mut chain = Blockchain::default().with_height(3);
+ let best_tip = chain.tip();
+ chain.disconnect_tip();
+ let worse_tip = chain.tip();
+
+ let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
+ let mut cache = UnboundedCache::new();
+ let mut listener = NullChainListener {};
+ let mut client = SpvClient::new(best_tip, poller, &mut cache, &mut listener);
+ match client.poll_best_tip().await {
+ Err(e) => panic!("Unexpected error: {:?}", e),
+ Ok((chain_tip, blocks_connected)) => {
+ assert_eq!(chain_tip, ChainTip::Worse(worse_tip));
+ assert!(!blocks_connected);
+ },
+ }
+ assert_eq!(client.chain_tip, best_tip);
+ }