1 //! A lightweight client for keeping in sync with chain activity.
3 //! Defines an [`SpvClient`] utility for polling one or more block sources for the best chain tip.
4 //! It is used to notify listeners of blocks connected or disconnected since the last poll. Useful
5 //! for keeping a Lightning node in sync with the chain.
7 //! Defines a [`BlockSource`] trait, which is an asynchronous interface for retrieving block headers
10 //! Enabling feature `rest-client` or `rpc-client` allows configuring the client to fetch blocks
11 //! using Bitcoin Core's REST or RPC interface, respectively.
13 //! Both features support either blocking I/O using `std::net::TcpStream` or, with feature `tokio`,
14 //! non-blocking I/O using `tokio::net::TcpStream` from inside a Tokio runtime.
16 // Prefix these with `rustdoc::` when we update our MSRV to be >= 1.52 to remove warnings.
17 #![deny(broken_intra_doc_links)]
18 #![deny(private_intra_doc_links)]
20 #![deny(missing_docs)]
23 #![cfg_attr(docsrs, feature(doc_auto_cfg))]
25 #[cfg(any(feature = "rest-client", feature = "rpc-client"))]
31 #[cfg(feature = "rest-client")]
34 #[cfg(feature = "rpc-client")]
37 #[cfg(any(feature = "rest-client", feature = "rpc-client"))]
43 #[cfg(any(feature = "rest-client", feature = "rpc-client"))]
46 use crate::poll::{ChainTip, Poll, ValidatedBlockHeader};
48 use bitcoin::blockdata::block::{Block, BlockHeader};
49 use bitcoin::hash_types::BlockHash;
50 use bitcoin::util::uint::Uint256;
53 use lightning::chain::Listen;
55 use std::future::Future;
59 /// Abstract type for retrieving block headers and data.
60 pub trait BlockSource : Sync + Send {
61 /// Returns the header for a given hash. A height hint may be provided in case a block source
62 /// cannot easily find headers based on a hash. This is merely a hint and thus the returned
63 /// header must have the same hash as was requested. Otherwise, an error must be returned.
65 /// Implementations that cannot find headers based on the hash should return a `Transient` error
66 /// when `height_hint` is `None`.
67 fn get_header<'a>(&'a self, header_hash: &'a BlockHash, height_hint: Option<u32>) -> AsyncBlockSourceResult<'a, BlockHeaderData>;
69 /// Returns the block for a given hash. A headers-only block source should return a `Transient`
71 fn get_block<'a>(&'a self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block>;
73 /// Returns the hash of the best block and, optionally, its height.
75 /// When polling a block source, [`Poll`] implementations may pass the height to [`get_header`]
76 /// to allow for a more efficient lookup.
78 /// [`get_header`]: Self::get_header
79 fn get_best_block<'a>(&'a self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)>;
82 /// Result type for `BlockSource` requests.
83 pub type BlockSourceResult<T> = Result<T, BlockSourceError>;
85 // TODO: Replace with BlockSourceResult once `async` trait functions are supported. For details,
86 // see: https://areweasyncyet.rs.
87 /// Result type for asynchronous `BlockSource` requests.
88 pub type AsyncBlockSourceResult<'a, T> = Pin<Box<dyn Future<Output = BlockSourceResult<T>> + 'a + Send>>;
90 /// Error type for `BlockSource` requests.
92 /// Transient errors may be resolved when re-polling, but no attempt will be made to re-poll on
93 /// persistent errors.
95 pub struct BlockSourceError {
96 kind: BlockSourceErrorKind,
97 error: Box<dyn std::error::Error + Send + Sync>,
100 /// The kind of `BlockSourceError`, either persistent or transient.
101 #[derive(Clone, Copy, Debug, PartialEq)]
102 pub enum BlockSourceErrorKind {
103 /// Indicates an error that won't resolve when retrying a request (e.g., invalid data).
106 /// Indicates an error that may resolve when retrying a request (e.g., unresponsive).
110 impl BlockSourceError {
111 /// Creates a new persistent error originated from the given error.
112 pub fn persistent<E>(error: E) -> Self
113 where E: Into<Box<dyn std::error::Error + Send + Sync>> {
115 kind: BlockSourceErrorKind::Persistent,
120 /// Creates a new transient error originated from the given error.
121 pub fn transient<E>(error: E) -> Self
122 where E: Into<Box<dyn std::error::Error + Send + Sync>> {
124 kind: BlockSourceErrorKind::Transient,
129 /// Returns the kind of error.
130 pub fn kind(&self) -> BlockSourceErrorKind {
134 /// Converts the error into the underlying error.
135 pub fn into_inner(self) -> Box<dyn std::error::Error + Send + Sync> {
140 /// A block header and some associated data. This information should be available from most block
141 /// sources (and, notably, is available in Bitcoin Core's RPC and REST interfaces).
142 #[derive(Clone, Copy, Debug, PartialEq)]
143 pub struct BlockHeaderData {
144 /// The block header itself.
145 pub header: BlockHeader,
147 /// The block height where the genesis block has height 0.
150 /// The total chain work in expected number of double-SHA256 hashes required to build a chain
151 /// of equivalent weight.
152 pub chainwork: Uint256,
155 /// A lightweight client for keeping a listener in sync with the chain, allowing for Simplified
156 /// Payment Verification (SPV).
158 /// The client is parameterized by a chain poller which is responsible for polling one or more block
159 /// sources for the best chain tip. During this process it detects any chain forks, determines which
160 /// constitutes the best chain, and updates the listener accordingly with any blocks that were
161 /// connected or disconnected since the last poll.
163 /// Block headers for the best chain are maintained in the parameterized cache, allowing for a
164 /// custom cache eviction policy. This offers flexibility to those sensitive to resource usage.
165 /// Hence, there is a trade-off between a lower memory footprint and potentially increased network
166 /// I/O as headers are re-fetched during fork detection.
167 pub struct SpvClient<'a, P: Poll, C: Cache, L: Deref>
168 where L::Target: chain::Listen {
169 chain_tip: ValidatedBlockHeader,
171 chain_notifier: ChainNotifier<'a, C, L>,
174 /// The `Cache` trait defines behavior for managing a block header cache, where block headers are
175 /// keyed by block hash.
177 /// Used by [`ChainNotifier`] to store headers along the best chain, which is important for ensuring
178 /// that blocks can be disconnected if they are no longer accessible from a block source (e.g., if
179 /// the block source does not store stale forks indefinitely).
181 /// Implementations may define how long to retain headers such that it's unlikely they will ever be
182 /// needed to disconnect a block. In cases where block sources provide access to headers on stale
183 /// forks reliably, caches may be entirely unnecessary.
185 /// Retrieves the block header keyed by the given block hash.
186 fn look_up(&self, block_hash: &BlockHash) -> Option<&ValidatedBlockHeader>;
188 /// Called when a block has been connected to the best chain to ensure it is available to be
189 /// disconnected later if needed.
190 fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader);
192 /// Called when a block has been disconnected from the best chain. Once disconnected, a block's
193 /// header is no longer needed and thus can be removed.
194 fn block_disconnected(&mut self, block_hash: &BlockHash) -> Option<ValidatedBlockHeader>;
197 /// Unbounded cache of block headers keyed by block hash.
198 pub type UnboundedCache = std::collections::HashMap<BlockHash, ValidatedBlockHeader>;
200 impl Cache for UnboundedCache {
201 fn look_up(&self, block_hash: &BlockHash) -> Option<&ValidatedBlockHeader> {
205 fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
206 self.insert(block_hash, block_header);
209 fn block_disconnected(&mut self, block_hash: &BlockHash) -> Option<ValidatedBlockHeader> {
210 self.remove(block_hash)
214 impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L> where L::Target: chain::Listen {
215 /// Creates a new SPV client using `chain_tip` as the best known chain tip.
217 /// Subsequent calls to [`poll_best_tip`] will poll for the best chain tip using the given chain
218 /// poller, which may be configured with one or more block sources to query. At least one block
219 /// source must provide headers back from the best chain tip to its common ancestor with
221 /// * `header_cache` is used to look up and store headers on the best chain
222 /// * `chain_listener` is notified of any blocks connected or disconnected
224 /// [`poll_best_tip`]: SpvClient::poll_best_tip
226 chain_tip: ValidatedBlockHeader,
228 header_cache: &'a mut C,
231 let chain_notifier = ChainNotifier { header_cache, chain_listener };
232 Self { chain_tip, chain_poller, chain_notifier }
235 /// Polls for the best tip and updates the chain listener with any connected or disconnected
236 /// blocks accordingly.
238 /// Returns the best polled chain tip relative to the previous best known tip and whether any
239 /// blocks were indeed connected or disconnected.
240 pub async fn poll_best_tip(&mut self) -> BlockSourceResult<(ChainTip, bool)> {
241 let chain_tip = self.chain_poller.poll_chain_tip(self.chain_tip).await?;
242 let blocks_connected = match chain_tip {
243 ChainTip::Common => false,
244 ChainTip::Better(chain_tip) => {
245 debug_assert_ne!(chain_tip.block_hash, self.chain_tip.block_hash);
246 debug_assert!(chain_tip.chainwork > self.chain_tip.chainwork);
247 self.update_chain_tip(chain_tip).await
249 ChainTip::Worse(chain_tip) => {
250 debug_assert_ne!(chain_tip.block_hash, self.chain_tip.block_hash);
251 debug_assert!(chain_tip.chainwork <= self.chain_tip.chainwork);
255 Ok((chain_tip, blocks_connected))
258 /// Updates the chain tip, syncing the chain listener with any connected or disconnected
259 /// blocks. Returns whether there were any such blocks.
260 async fn update_chain_tip(&mut self, best_chain_tip: ValidatedBlockHeader) -> bool {
261 match self.chain_notifier.synchronize_listener(
262 best_chain_tip, &self.chain_tip, &mut self.chain_poller).await
265 self.chain_tip = best_chain_tip;
268 Err((_, Some(chain_tip))) if chain_tip.block_hash != self.chain_tip.block_hash => {
269 self.chain_tip = chain_tip;
277 /// Notifies [listeners] of blocks that have been connected or disconnected from the chain.
279 /// [listeners]: lightning::chain::Listen
280 pub struct ChainNotifier<'a, C: Cache, L: Deref> where L::Target: chain::Listen {
281 /// Cache for looking up headers before fetching from a block source.
282 header_cache: &'a mut C,
284 /// Listener that will be notified of connected or disconnected blocks.
288 /// Changes made to the chain between subsequent polls that transformed it from having one chain tip
291 /// Blocks are given in height-descending order. Therefore, blocks are first disconnected in order
292 /// before new blocks are connected in reverse order.
293 struct ChainDifference {
294 /// The most recent ancestor common between the chain tips.
296 /// If there are any disconnected blocks, this is where the chain forked.
297 common_ancestor: ValidatedBlockHeader,
299 /// Blocks that were disconnected from the chain since the last poll.
300 disconnected_blocks: Vec<ValidatedBlockHeader>,
302 /// Blocks that were connected to the chain since the last poll.
303 connected_blocks: Vec<ValidatedBlockHeader>,
306 impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L> where L::Target: chain::Listen {
307 /// Finds the first common ancestor between `new_header` and `old_header`, disconnecting blocks
308 /// from `old_header` to get to that point and then connecting blocks until `new_header`.
310 /// Validates headers along the transition path, but doesn't fetch blocks until the chain is
311 /// disconnected to the fork point. Thus, this may return an `Err` that includes where the tip
312 /// ended up which may not be `new_header`. Note that the returned `Err` contains `Some` header
313 /// if and only if the transition from `old_header` to `new_header` is valid.
314 async fn synchronize_listener<P: Poll>(
316 new_header: ValidatedBlockHeader,
317 old_header: &ValidatedBlockHeader,
318 chain_poller: &mut P,
319 ) -> Result<(), (BlockSourceError, Option<ValidatedBlockHeader>)> {
320 let difference = self.find_difference(new_header, old_header, chain_poller).await
321 .map_err(|e| (e, None))?;
322 self.disconnect_blocks(difference.disconnected_blocks);
324 difference.common_ancestor,
325 difference.connected_blocks,
330 /// Returns the changes needed to produce the chain with `current_header` as its tip from the
331 /// chain with `prev_header` as its tip.
333 /// Walks backwards from `current_header` and `prev_header`, finding the common ancestor.
334 async fn find_difference<P: Poll>(
336 current_header: ValidatedBlockHeader,
337 prev_header: &ValidatedBlockHeader,
338 chain_poller: &mut P,
339 ) -> BlockSourceResult<ChainDifference> {
340 let mut disconnected_blocks = Vec::new();
341 let mut connected_blocks = Vec::new();
342 let mut current = current_header;
343 let mut previous = *prev_header;
345 // Found the common ancestor.
346 if current.block_hash == previous.block_hash {
350 // Walk back the chain, finding blocks needed to connect and disconnect. Only walk back
351 // the header with the greater height, or both if equal heights.
352 let current_height = current.height;
353 let previous_height = previous.height;
354 if current_height <= previous_height {
355 disconnected_blocks.push(previous);
356 previous = self.look_up_previous_header(chain_poller, &previous).await?;
358 if current_height >= previous_height {
359 connected_blocks.push(current);
360 current = self.look_up_previous_header(chain_poller, ¤t).await?;
364 let common_ancestor = current;
365 Ok(ChainDifference { common_ancestor, disconnected_blocks, connected_blocks })
368 /// Returns the previous header for the given header, either by looking it up in the cache or
369 /// fetching it if not found.
370 async fn look_up_previous_header<P: Poll>(
372 chain_poller: &mut P,
373 header: &ValidatedBlockHeader,
374 ) -> BlockSourceResult<ValidatedBlockHeader> {
375 match self.header_cache.look_up(&header.header.prev_blockhash) {
376 Some(prev_header) => Ok(*prev_header),
377 None => chain_poller.look_up_previous_header(header).await,
381 /// Notifies the chain listeners of disconnected blocks.
382 fn disconnect_blocks(&mut self, mut disconnected_blocks: Vec<ValidatedBlockHeader>) {
383 for header in disconnected_blocks.drain(..) {
384 if let Some(cached_header) = self.header_cache.block_disconnected(&header.block_hash) {
385 assert_eq!(cached_header, header);
387 self.chain_listener.block_disconnected(&header.header, header.height);
391 /// Notifies the chain listeners of connected blocks.
392 async fn connect_blocks<P: Poll>(
394 mut new_tip: ValidatedBlockHeader,
395 mut connected_blocks: Vec<ValidatedBlockHeader>,
396 chain_poller: &mut P,
397 ) -> Result<(), (BlockSourceError, Option<ValidatedBlockHeader>)> {
398 for header in connected_blocks.drain(..).rev() {
399 let block = chain_poller
400 .fetch_block(&header).await
401 .or_else(|e| Err((e, Some(new_tip))))?;
402 debug_assert_eq!(block.block_hash, header.block_hash);
404 self.header_cache.block_connected(header.block_hash, header);
405 self.chain_listener.block_connected(&block, header.height);
414 mod spv_client_tests {
415 use crate::test_utils::{Blockchain, NullChainListener};
418 use bitcoin::network::constants::Network;
421 async fn poll_from_chain_without_headers() {
422 let mut chain = Blockchain::default().with_height(3).without_headers();
423 let best_tip = chain.at_height(1);
425 let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
426 let mut cache = UnboundedCache::new();
427 let mut listener = NullChainListener {};
428 let mut client = SpvClient::new(best_tip, poller, &mut cache, &mut listener);
429 match client.poll_best_tip().await {
431 assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
432 assert_eq!(e.into_inner().as_ref().to_string(), "header not found");
434 Ok(_) => panic!("Expected error"),
436 assert_eq!(client.chain_tip, best_tip);
440 async fn poll_from_chain_with_common_tip() {
441 let mut chain = Blockchain::default().with_height(3);
442 let common_tip = chain.tip();
444 let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
445 let mut cache = UnboundedCache::new();
446 let mut listener = NullChainListener {};
447 let mut client = SpvClient::new(common_tip, poller, &mut cache, &mut listener);
448 match client.poll_best_tip().await {
449 Err(e) => panic!("Unexpected error: {:?}", e),
450 Ok((chain_tip, blocks_connected)) => {
451 assert_eq!(chain_tip, ChainTip::Common);
452 assert!(!blocks_connected);
455 assert_eq!(client.chain_tip, common_tip);
459 async fn poll_from_chain_with_better_tip() {
460 let mut chain = Blockchain::default().with_height(3);
461 let new_tip = chain.tip();
462 let old_tip = chain.at_height(1);
464 let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
465 let mut cache = UnboundedCache::new();
466 let mut listener = NullChainListener {};
467 let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener);
468 match client.poll_best_tip().await {
469 Err(e) => panic!("Unexpected error: {:?}", e),
470 Ok((chain_tip, blocks_connected)) => {
471 assert_eq!(chain_tip, ChainTip::Better(new_tip));
472 assert!(blocks_connected);
475 assert_eq!(client.chain_tip, new_tip);
479 async fn poll_from_chain_with_better_tip_and_without_any_new_blocks() {
480 let mut chain = Blockchain::default().with_height(3).without_blocks(2..);
481 let new_tip = chain.tip();
482 let old_tip = chain.at_height(1);
484 let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
485 let mut cache = UnboundedCache::new();
486 let mut listener = NullChainListener {};
487 let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener);
488 match client.poll_best_tip().await {
489 Err(e) => panic!("Unexpected error: {:?}", e),
490 Ok((chain_tip, blocks_connected)) => {
491 assert_eq!(chain_tip, ChainTip::Better(new_tip));
492 assert!(!blocks_connected);
495 assert_eq!(client.chain_tip, old_tip);
499 async fn poll_from_chain_with_better_tip_and_without_some_new_blocks() {
500 let mut chain = Blockchain::default().with_height(3).without_blocks(3..);
501 let new_tip = chain.tip();
502 let old_tip = chain.at_height(1);
504 let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
505 let mut cache = UnboundedCache::new();
506 let mut listener = NullChainListener {};
507 let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener);
508 match client.poll_best_tip().await {
509 Err(e) => panic!("Unexpected error: {:?}", e),
510 Ok((chain_tip, blocks_connected)) => {
511 assert_eq!(chain_tip, ChainTip::Better(new_tip));
512 assert!(blocks_connected);
515 assert_eq!(client.chain_tip, chain.at_height(2));
519 async fn poll_from_chain_with_worse_tip() {
520 let mut chain = Blockchain::default().with_height(3);
521 let best_tip = chain.tip();
522 chain.disconnect_tip();
523 let worse_tip = chain.tip();
525 let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
526 let mut cache = UnboundedCache::new();
527 let mut listener = NullChainListener {};
528 let mut client = SpvClient::new(best_tip, poller, &mut cache, &mut listener);
529 match client.poll_best_tip().await {
530 Err(e) => panic!("Unexpected error: {:?}", e),
531 Ok((chain_tip, blocks_connected)) => {
532 assert_eq!(chain_tip, ChainTip::Worse(worse_tip));
533 assert!(!blocks_connected);
536 assert_eq!(client.chain_tip, best_tip);
541 mod chain_notifier_tests {
542 use crate::test_utils::{Blockchain, MockChainListener};
545 use bitcoin::network::constants::Network;
548 async fn sync_from_same_chain() {
549 let mut chain = Blockchain::default().with_height(3);
551 let new_tip = chain.tip();
552 let old_tip = chain.at_height(1);
553 let chain_listener = &MockChainListener::new()
554 .expect_block_connected(*chain.at_height(2))
555 .expect_block_connected(*new_tip);
556 let mut notifier = ChainNotifier {
557 header_cache: &mut chain.header_cache(0..=1),
560 let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
561 match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
562 Err((e, _)) => panic!("Unexpected error: {:?}", e),
568 async fn sync_from_different_chains() {
569 let mut test_chain = Blockchain::with_network(Network::Testnet).with_height(1);
570 let main_chain = Blockchain::with_network(Network::Bitcoin).with_height(1);
572 let new_tip = test_chain.tip();
573 let old_tip = main_chain.tip();
574 let chain_listener = &MockChainListener::new();
575 let mut notifier = ChainNotifier {
576 header_cache: &mut main_chain.header_cache(0..=1),
579 let mut poller = poll::ChainPoller::new(&mut test_chain, Network::Testnet);
580 match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
582 assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
583 assert_eq!(e.into_inner().as_ref().to_string(), "genesis block reached");
585 Ok(_) => panic!("Expected error"),
590 async fn sync_from_equal_length_fork() {
591 let main_chain = Blockchain::default().with_height(2);
592 let mut fork_chain = main_chain.fork_at_height(1);
594 let new_tip = fork_chain.tip();
595 let old_tip = main_chain.tip();
596 let chain_listener = &MockChainListener::new()
597 .expect_block_disconnected(*old_tip)
598 .expect_block_connected(*new_tip);
599 let mut notifier = ChainNotifier {
600 header_cache: &mut main_chain.header_cache(0..=2),
603 let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet);
604 match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
605 Err((e, _)) => panic!("Unexpected error: {:?}", e),
611 async fn sync_from_shorter_fork() {
612 let main_chain = Blockchain::default().with_height(3);
613 let mut fork_chain = main_chain.fork_at_height(1);
614 fork_chain.disconnect_tip();
616 let new_tip = fork_chain.tip();
617 let old_tip = main_chain.tip();
618 let chain_listener = &MockChainListener::new()
619 .expect_block_disconnected(*old_tip)
620 .expect_block_disconnected(*main_chain.at_height(2))
621 .expect_block_connected(*new_tip);
622 let mut notifier = ChainNotifier {
623 header_cache: &mut main_chain.header_cache(0..=3),
626 let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet);
627 match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
628 Err((e, _)) => panic!("Unexpected error: {:?}", e),
634 async fn sync_from_longer_fork() {
635 let mut main_chain = Blockchain::default().with_height(3);
636 let mut fork_chain = main_chain.fork_at_height(1);
637 main_chain.disconnect_tip();
639 let new_tip = fork_chain.tip();
640 let old_tip = main_chain.tip();
641 let chain_listener = &MockChainListener::new()
642 .expect_block_disconnected(*old_tip)
643 .expect_block_connected(*fork_chain.at_height(2))
644 .expect_block_connected(*new_tip);
645 let mut notifier = ChainNotifier {
646 header_cache: &mut main_chain.header_cache(0..=2),
649 let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet);
650 match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
651 Err((e, _)) => panic!("Unexpected error: {:?}", e),
657 async fn sync_from_chain_without_headers() {
658 let mut chain = Blockchain::default().with_height(3).without_headers();
660 let new_tip = chain.tip();
661 let old_tip = chain.at_height(1);
662 let chain_listener = &MockChainListener::new();
663 let mut notifier = ChainNotifier {
664 header_cache: &mut chain.header_cache(0..=1),
667 let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
668 match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
669 Err((_, tip)) => assert_eq!(tip, None),
670 Ok(_) => panic!("Expected error"),
675 async fn sync_from_chain_without_any_new_blocks() {
676 let mut chain = Blockchain::default().with_height(3).without_blocks(2..);
678 let new_tip = chain.tip();
679 let old_tip = chain.at_height(1);
680 let chain_listener = &MockChainListener::new();
681 let mut notifier = ChainNotifier {
682 header_cache: &mut chain.header_cache(0..=3),
685 let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
686 match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
687 Err((_, tip)) => assert_eq!(tip, Some(old_tip)),
688 Ok(_) => panic!("Expected error"),
693 async fn sync_from_chain_without_some_new_blocks() {
694 let mut chain = Blockchain::default().with_height(3).without_blocks(3..);
696 let new_tip = chain.tip();
697 let old_tip = chain.at_height(1);
698 let chain_listener = &MockChainListener::new()
699 .expect_block_connected(*chain.at_height(2));
700 let mut notifier = ChainNotifier {
701 header_cache: &mut chain.header_cache(0..=3),
704 let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
705 match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
706 Err((_, tip)) => assert_eq!(tip, Some(chain.at_height(2))),
707 Ok(_) => panic!("Expected error"),