+#[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 cache = UnboundedCache::new();
+ let mut client = SpvClient::new(best_tip, poller, cache, NullChainListener {});
+ 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 cache = UnboundedCache::new();
+ let mut client = SpvClient::new(common_tip, poller, cache, NullChainListener {});
+ 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 cache = UnboundedCache::new();
+ let mut client = SpvClient::new(old_tip, poller, cache, NullChainListener {});
+ 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 cache = UnboundedCache::new();
+ let mut client = SpvClient::new(old_tip, poller, cache, NullChainListener {});
+ 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 cache = UnboundedCache::new();
+ let mut client = SpvClient::new(old_tip, poller, cache, NullChainListener {});
+ 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 cache = UnboundedCache::new();
+ let mut client = SpvClient::new(best_tip, poller, cache, NullChainListener {});
+ 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);
+ }
+}
+