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 //! [`SpvClient`]: struct.SpvClient.html
17 //! [`BlockSource`]: trait.BlockSource.html
19 #[cfg(any(feature = "rest-client", feature = "rpc-client"))]
25 #[cfg(feature = "rest-client")]
28 #[cfg(feature = "rpc-client")]
31 #[cfg(any(feature = "rest-client", feature = "rpc-client"))]
37 #[cfg(any(feature = "rest-client", feature = "rpc-client"))]
40 use crate::poll::{ChainTip, Poll, ValidatedBlockHeader};
42 use bitcoin::blockdata::block::{Block, BlockHeader};
43 use bitcoin::hash_types::BlockHash;
44 use bitcoin::util::uint::Uint256;
47 use lightning::chain::Listen;
49 use std::future::Future;
53 /// Abstract type for retrieving block headers and data.
54 pub trait BlockSource : Sync + Send {
55 /// Returns the header for a given hash. A height hint may be provided in case a block source
56 /// cannot easily find headers based on a hash. This is merely a hint and thus the returned
57 /// header must have the same hash as was requested. Otherwise, an error must be returned.
59 /// Implementations that cannot find headers based on the hash should return a `Transient` error
60 /// when `height_hint` is `None`.
61 fn get_header<'a>(&'a mut self, header_hash: &'a BlockHash, height_hint: Option<u32>) -> AsyncBlockSourceResult<'a, BlockHeaderData>;
63 /// Returns the block for a given hash. A headers-only block source should return a `Transient`
65 fn get_block<'a>(&'a mut self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block>;
67 /// Returns the hash of the best block and, optionally, its height.
69 /// When polling a block source, [`Poll`] implementations may pass the height to [`get_header`]
70 /// to allow for a more efficient lookup.
72 /// [`Poll`]: poll/trait.Poll.html
73 /// [`get_header`]: #tymethod.get_header
74 fn get_best_block<'a>(&'a mut self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)>;
77 /// Result type for `BlockSource` requests.
78 type BlockSourceResult<T> = Result<T, BlockSourceError>;
80 // TODO: Replace with BlockSourceResult once `async` trait functions are supported. For details,
81 // see: https://areweasyncyet.rs.
82 /// Result type for asynchronous `BlockSource` requests.
83 type AsyncBlockSourceResult<'a, T> = Pin<Box<dyn Future<Output = BlockSourceResult<T>> + 'a + Send>>;
85 /// Error type for `BlockSource` requests.
87 /// Transient errors may be resolved when re-polling, but no attempt will be made to re-poll on
88 /// persistent errors.
90 pub struct BlockSourceError {
91 kind: BlockSourceErrorKind,
92 error: Box<dyn std::error::Error + Send + Sync>,
95 /// The kind of `BlockSourceError`, either persistent or transient.
96 #[derive(Clone, Copy, Debug, PartialEq)]
97 pub enum BlockSourceErrorKind {
98 /// Indicates an error that won't resolve when retrying a request (e.g., invalid data).
101 /// Indicates an error that may resolve when retrying a request (e.g., unresponsive).
105 impl BlockSourceError {
106 /// Creates a new persistent error originated from the given error.
107 pub fn persistent<E>(error: E) -> Self
108 where E: Into<Box<dyn std::error::Error + Send + Sync>> {
110 kind: BlockSourceErrorKind::Persistent,
115 /// Creates a new transient error originated from the given error.
116 pub fn transient<E>(error: E) -> Self
117 where E: Into<Box<dyn std::error::Error + Send + Sync>> {
119 kind: BlockSourceErrorKind::Transient,
124 /// Returns the kind of error.
125 pub fn kind(&self) -> BlockSourceErrorKind {
129 /// Converts the error into the underlying error.
130 pub fn into_inner(self) -> Box<dyn std::error::Error + Send + Sync> {
135 /// A block header and some associated data. This information should be available from most block
136 /// sources (and, notably, is available in Bitcoin Core's RPC and REST interfaces).
137 #[derive(Clone, Copy, Debug, PartialEq)]
138 pub struct BlockHeaderData {
139 /// The block header itself.
140 pub header: BlockHeader,
142 /// The block height where the genesis block has height 0.
145 /// The total chain work in expected number of double-SHA256 hashes required to build a chain
146 /// of equivalent weight.
147 pub chainwork: Uint256,
150 /// A lightweight client for keeping a listener in sync with the chain, allowing for Simplified
151 /// Payment Verification (SPV).
153 /// The client is parameterized by a chain poller which is responsible for polling one or more block
154 /// sources for the best chain tip. During this process it detects any chain forks, determines which
155 /// constitutes the best chain, and updates the listener accordingly with any blocks that were
156 /// connected or disconnected since the last poll.
158 /// Block headers for the best chain are maintained in the parameterized cache, allowing for a
159 /// custom cache eviction policy. This offers flexibility to those sensitive to resource usage.
160 /// Hence, there is a trade-off between a lower memory footprint and potentially increased network
161 /// I/O as headers are re-fetched during fork detection.
162 pub struct SpvClient<'a, P: Poll, C: Cache, L: chain::Listen> {
163 chain_tip: ValidatedBlockHeader,
165 chain_notifier: ChainNotifier<'a, C, L>,
168 /// The `Cache` trait defines behavior for managing a block header cache, where block headers are
169 /// keyed by block hash.
171 /// Used by [`ChainNotifier`] to store headers along the best chain, which is important for ensuring
172 /// that blocks can be disconnected if they are no longer accessible from a block source (e.g., if
173 /// the block source does not store stale forks indefinitely).
175 /// Implementations may define how long to retain headers such that it's unlikely they will ever be
176 /// needed to disconnect a block. In cases where block sources provide access to headers on stale
177 /// forks reliably, caches may be entirely unnecessary.
179 /// [`ChainNotifier`]: struct.ChainNotifier.html
181 /// Retrieves the block header keyed by the given block hash.
182 fn look_up(&self, block_hash: &BlockHash) -> Option<&ValidatedBlockHeader>;
184 /// Called when a block has been connected to the best chain to ensure it is available to be
185 /// disconnected later if needed.
186 fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader);
188 /// Called when a block has been disconnected from the best chain. Once disconnected, a block's
189 /// header is no longer needed and thus can be removed.
190 fn block_disconnected(&mut self, block_hash: &BlockHash) -> Option<ValidatedBlockHeader>;
193 /// Unbounded cache of block headers keyed by block hash.
194 pub type UnboundedCache = std::collections::HashMap<BlockHash, ValidatedBlockHeader>;
196 impl Cache for UnboundedCache {
197 fn look_up(&self, block_hash: &BlockHash) -> Option<&ValidatedBlockHeader> {
201 fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
202 self.insert(block_hash, block_header);
205 fn block_disconnected(&mut self, block_hash: &BlockHash) -> Option<ValidatedBlockHeader> {
206 self.remove(block_hash)
210 impl<'a, P: Poll, C: Cache, L: chain::Listen> SpvClient<'a, P, C, L> {
211 /// Creates a new SPV client using `chain_tip` as the best known chain tip.
213 /// Subsequent calls to [`poll_best_tip`] will poll for the best chain tip using the given chain
214 /// poller, which may be configured with one or more block sources to query. At least one block
215 /// source must provide headers back from the best chain tip to its common ancestor with
217 /// * `header_cache` is used to look up and store headers on the best chain
218 /// * `chain_listener` is notified of any blocks connected or disconnected
220 /// [`poll_best_tip`]: struct.SpvClient.html#method.poll_best_tip
222 chain_tip: ValidatedBlockHeader,
224 header_cache: &'a mut C,
227 let chain_notifier = ChainNotifier { header_cache, chain_listener };
228 Self { chain_tip, chain_poller, chain_notifier }
231 /// Polls for the best tip and updates the chain listener with any connected or disconnected
232 /// blocks accordingly.
234 /// Returns the best polled chain tip relative to the previous best known tip and whether any
235 /// blocks were indeed connected or disconnected.
236 pub async fn poll_best_tip(&mut self) -> BlockSourceResult<(ChainTip, bool)> {
237 let chain_tip = self.chain_poller.poll_chain_tip(self.chain_tip).await?;
238 let blocks_connected = match chain_tip {
239 ChainTip::Common => false,
240 ChainTip::Better(chain_tip) => {
241 debug_assert_ne!(chain_tip.block_hash, self.chain_tip.block_hash);
242 debug_assert!(chain_tip.chainwork > self.chain_tip.chainwork);
243 self.update_chain_tip(chain_tip).await
245 ChainTip::Worse(chain_tip) => {
246 debug_assert_ne!(chain_tip.block_hash, self.chain_tip.block_hash);
247 debug_assert!(chain_tip.chainwork <= self.chain_tip.chainwork);
251 Ok((chain_tip, blocks_connected))
254 /// Updates the chain tip, syncing the chain listener with any connected or disconnected
255 /// blocks. Returns whether there were any such blocks.
256 async fn update_chain_tip(&mut self, best_chain_tip: ValidatedBlockHeader) -> bool {
257 match self.chain_notifier.sync_listener(best_chain_tip, &self.chain_tip, &mut self.chain_poller).await {
259 self.chain_tip = best_chain_tip;
262 Err((_, Some(chain_tip))) if chain_tip.block_hash != self.chain_tip.block_hash => {
263 self.chain_tip = chain_tip;
271 /// Notifies [listeners] of blocks that have been connected or disconnected from the chain.
273 /// [listeners]: ../../lightning/chain/trait.Listen.html
274 pub struct ChainNotifier<'a, C: Cache, L: chain::Listen> {
275 /// Cache for looking up headers before fetching from a block source.
276 header_cache: &'a mut C,
278 /// Listener that will be notified of connected or disconnected blocks.
282 /// Changes made to the chain between subsequent polls that transformed it from having one chain tip
285 /// Blocks are given in height-descending order. Therefore, blocks are first disconnected in order
286 /// before new blocks are connected in reverse order.
287 struct ChainDifference {
288 /// The most recent ancestor common between the chain tips.
290 /// If there are any disconnected blocks, this is where the chain forked.
291 common_ancestor: ValidatedBlockHeader,
293 /// Blocks that were disconnected from the chain since the last poll.
294 disconnected_blocks: Vec<ValidatedBlockHeader>,
296 /// Blocks that were connected to the chain since the last poll.
297 connected_blocks: Vec<ValidatedBlockHeader>,
300 impl<'a, C: Cache, L: chain::Listen> ChainNotifier<'a, C, L> {
301 /// Finds the first common ancestor between `new_header` and `old_header`, disconnecting blocks
302 /// from `old_header` to get to that point and then connecting blocks until `new_header`.
304 /// Validates headers along the transition path, but doesn't fetch blocks until the chain is
305 /// disconnected to the fork point. Thus, this may return an `Err` that includes where the tip
306 /// ended up which may not be `new_header`. Note that iff the returned `Err` contains `Some`
307 /// header then the transition from `old_header` to `new_header` is valid.
308 async fn sync_listener<P: Poll>(
310 new_header: ValidatedBlockHeader,
311 old_header: &ValidatedBlockHeader,
312 chain_poller: &mut P,
313 ) -> Result<(), (BlockSourceError, Option<ValidatedBlockHeader>)> {
314 let difference = self.find_difference(new_header, old_header, chain_poller).await
315 .map_err(|e| (e, None))?;
316 self.disconnect_blocks(difference.disconnected_blocks);
318 difference.common_ancestor,
319 difference.connected_blocks,
324 /// Returns the changes needed to produce the chain with `current_header` as its tip from the
325 /// chain with `prev_header` as its tip.
327 /// Walks backwards from `current_header` and `prev_header`, finding the common ancestor.
328 async fn find_difference<P: Poll>(
330 current_header: ValidatedBlockHeader,
331 prev_header: &ValidatedBlockHeader,
332 chain_poller: &mut P,
333 ) -> BlockSourceResult<ChainDifference> {
334 let mut disconnected_blocks = Vec::new();
335 let mut connected_blocks = Vec::new();
336 let mut current = current_header;
337 let mut previous = *prev_header;
339 // Found the common ancestor.
340 if current.block_hash == previous.block_hash {
344 // Walk back the chain, finding blocks needed to connect and disconnect. Only walk back
345 // the header with the greater height, or both if equal heights.
346 let current_height = current.height;
347 let previous_height = previous.height;
348 if current_height <= previous_height {
349 disconnected_blocks.push(previous);
350 previous = self.look_up_previous_header(chain_poller, &previous).await?;
352 if current_height >= previous_height {
353 connected_blocks.push(current);
354 current = self.look_up_previous_header(chain_poller, ¤t).await?;
358 let common_ancestor = current;
359 Ok(ChainDifference { common_ancestor, disconnected_blocks, connected_blocks })
362 /// Returns the previous header for the given header, either by looking it up in the cache or
363 /// fetching it if not found.
364 async fn look_up_previous_header<P: Poll>(
366 chain_poller: &mut P,
367 header: &ValidatedBlockHeader,
368 ) -> BlockSourceResult<ValidatedBlockHeader> {
369 match self.header_cache.look_up(&header.header.prev_blockhash) {
370 Some(prev_header) => Ok(*prev_header),
371 None => chain_poller.look_up_previous_header(header).await,
375 /// Notifies the chain listeners of disconnected blocks.
376 fn disconnect_blocks(&mut self, mut disconnected_blocks: Vec<ValidatedBlockHeader>) {
377 for header in disconnected_blocks.drain(..) {
378 if let Some(cached_header) = self.header_cache.block_disconnected(&header.block_hash) {
379 assert_eq!(cached_header, header);
381 self.chain_listener.block_disconnected(&header.header, header.height);
385 /// Notifies the chain listeners of connected blocks.
386 async fn connect_blocks<P: Poll>(
388 mut new_tip: ValidatedBlockHeader,
389 mut connected_blocks: Vec<ValidatedBlockHeader>,
390 chain_poller: &mut P,
391 ) -> Result<(), (BlockSourceError, Option<ValidatedBlockHeader>)> {
392 for header in connected_blocks.drain(..).rev() {
393 let block = chain_poller
394 .fetch_block(&header).await
395 .or_else(|e| Err((e, Some(new_tip))))?;
396 debug_assert_eq!(block.block_hash, header.block_hash);
398 self.header_cache.block_connected(header.block_hash, header);
399 self.chain_listener.block_connected(&block, header.height);
408 mod spv_client_tests {
409 use crate::test_utils::{Blockchain, NullChainListener};
412 use bitcoin::network::constants::Network;
415 async fn poll_from_chain_without_headers() {
416 let mut chain = Blockchain::default().with_height(3).without_headers();
417 let best_tip = chain.at_height(1);
419 let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
420 let mut cache = UnboundedCache::new();
421 let listener = NullChainListener {};
422 let mut client = SpvClient::new(best_tip, poller, &mut cache, listener);
423 match client.poll_best_tip().await {
425 assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
426 assert_eq!(e.into_inner().as_ref().to_string(), "header not found");
428 Ok(_) => panic!("Expected error"),
430 assert_eq!(client.chain_tip, best_tip);
434 async fn poll_from_chain_with_common_tip() {
435 let mut chain = Blockchain::default().with_height(3);
436 let common_tip = chain.tip();
438 let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
439 let mut cache = UnboundedCache::new();
440 let listener = NullChainListener {};
441 let mut client = SpvClient::new(common_tip, poller, &mut cache, listener);
442 match client.poll_best_tip().await {
443 Err(e) => panic!("Unexpected error: {:?}", e),
444 Ok((chain_tip, blocks_connected)) => {
445 assert_eq!(chain_tip, ChainTip::Common);
446 assert!(!blocks_connected);
449 assert_eq!(client.chain_tip, common_tip);
453 async fn poll_from_chain_with_better_tip() {
454 let mut chain = Blockchain::default().with_height(3);
455 let new_tip = chain.tip();
456 let old_tip = chain.at_height(1);
458 let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
459 let mut cache = UnboundedCache::new();
460 let listener = NullChainListener {};
461 let mut client = SpvClient::new(old_tip, poller, &mut cache, listener);
462 match client.poll_best_tip().await {
463 Err(e) => panic!("Unexpected error: {:?}", e),
464 Ok((chain_tip, blocks_connected)) => {
465 assert_eq!(chain_tip, ChainTip::Better(new_tip));
466 assert!(blocks_connected);
469 assert_eq!(client.chain_tip, new_tip);
473 async fn poll_from_chain_with_better_tip_and_without_any_new_blocks() {
474 let mut chain = Blockchain::default().with_height(3).without_blocks(2..);
475 let new_tip = chain.tip();
476 let old_tip = chain.at_height(1);
478 let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
479 let mut cache = UnboundedCache::new();
480 let listener = NullChainListener {};
481 let mut client = SpvClient::new(old_tip, poller, &mut cache, listener);
482 match client.poll_best_tip().await {
483 Err(e) => panic!("Unexpected error: {:?}", e),
484 Ok((chain_tip, blocks_connected)) => {
485 assert_eq!(chain_tip, ChainTip::Better(new_tip));
486 assert!(!blocks_connected);
489 assert_eq!(client.chain_tip, old_tip);
493 async fn poll_from_chain_with_better_tip_and_without_some_new_blocks() {
494 let mut chain = Blockchain::default().with_height(3).without_blocks(3..);
495 let new_tip = chain.tip();
496 let old_tip = chain.at_height(1);
498 let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
499 let mut cache = UnboundedCache::new();
500 let listener = NullChainListener {};
501 let mut client = SpvClient::new(old_tip, poller, &mut cache, listener);
502 match client.poll_best_tip().await {
503 Err(e) => panic!("Unexpected error: {:?}", e),
504 Ok((chain_tip, blocks_connected)) => {
505 assert_eq!(chain_tip, ChainTip::Better(new_tip));
506 assert!(blocks_connected);
509 assert_eq!(client.chain_tip, chain.at_height(2));
513 async fn poll_from_chain_with_worse_tip() {
514 let mut chain = Blockchain::default().with_height(3);
515 let best_tip = chain.tip();
516 chain.disconnect_tip();
517 let worse_tip = chain.tip();
519 let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
520 let mut cache = UnboundedCache::new();
521 let listener = NullChainListener {};
522 let mut client = SpvClient::new(best_tip, poller, &mut cache, listener);
523 match client.poll_best_tip().await {
524 Err(e) => panic!("Unexpected error: {:?}", e),
525 Ok((chain_tip, blocks_connected)) => {
526 assert_eq!(chain_tip, ChainTip::Worse(worse_tip));
527 assert!(!blocks_connected);
530 assert_eq!(client.chain_tip, best_tip);
535 mod chain_notifier_tests {
536 use crate::test_utils::{Blockchain, MockChainListener};
539 use bitcoin::network::constants::Network;
542 async fn sync_from_same_chain() {
543 let mut chain = Blockchain::default().with_height(3);
545 let new_tip = chain.tip();
546 let old_tip = chain.at_height(1);
547 let chain_listener = MockChainListener::new()
548 .expect_block_connected(*chain.at_height(2))
549 .expect_block_connected(*new_tip);
550 let mut notifier = ChainNotifier {
551 header_cache: &mut chain.header_cache(0..=1),
554 let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
555 match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
556 Err((e, _)) => panic!("Unexpected error: {:?}", e),
562 async fn sync_from_different_chains() {
563 let mut test_chain = Blockchain::with_network(Network::Testnet).with_height(1);
564 let main_chain = Blockchain::with_network(Network::Bitcoin).with_height(1);
566 let new_tip = test_chain.tip();
567 let old_tip = main_chain.tip();
568 let chain_listener = MockChainListener::new();
569 let mut notifier = ChainNotifier {
570 header_cache: &mut main_chain.header_cache(0..=1),
573 let mut poller = poll::ChainPoller::new(&mut test_chain, Network::Testnet);
574 match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
576 assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
577 assert_eq!(e.into_inner().as_ref().to_string(), "genesis block reached");
579 Ok(_) => panic!("Expected error"),
584 async fn sync_from_equal_length_fork() {
585 let main_chain = Blockchain::default().with_height(2);
586 let mut fork_chain = main_chain.fork_at_height(1);
588 let new_tip = fork_chain.tip();
589 let old_tip = main_chain.tip();
590 let chain_listener = MockChainListener::new()
591 .expect_block_disconnected(*old_tip)
592 .expect_block_connected(*new_tip);
593 let mut notifier = ChainNotifier {
594 header_cache: &mut main_chain.header_cache(0..=2),
597 let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet);
598 match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
599 Err((e, _)) => panic!("Unexpected error: {:?}", e),
605 async fn sync_from_shorter_fork() {
606 let main_chain = Blockchain::default().with_height(3);
607 let mut fork_chain = main_chain.fork_at_height(1);
608 fork_chain.disconnect_tip();
610 let new_tip = fork_chain.tip();
611 let old_tip = main_chain.tip();
612 let chain_listener = MockChainListener::new()
613 .expect_block_disconnected(*old_tip)
614 .expect_block_disconnected(*main_chain.at_height(2))
615 .expect_block_connected(*new_tip);
616 let mut notifier = ChainNotifier {
617 header_cache: &mut main_chain.header_cache(0..=3),
620 let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet);
621 match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
622 Err((e, _)) => panic!("Unexpected error: {:?}", e),
628 async fn sync_from_longer_fork() {
629 let mut main_chain = Blockchain::default().with_height(3);
630 let mut fork_chain = main_chain.fork_at_height(1);
631 main_chain.disconnect_tip();
633 let new_tip = fork_chain.tip();
634 let old_tip = main_chain.tip();
635 let chain_listener = MockChainListener::new()
636 .expect_block_disconnected(*old_tip)
637 .expect_block_connected(*fork_chain.at_height(2))
638 .expect_block_connected(*new_tip);
639 let mut notifier = ChainNotifier {
640 header_cache: &mut main_chain.header_cache(0..=2),
643 let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet);
644 match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
645 Err((e, _)) => panic!("Unexpected error: {:?}", e),
651 async fn sync_from_chain_without_headers() {
652 let mut chain = Blockchain::default().with_height(3).without_headers();
654 let new_tip = chain.tip();
655 let old_tip = chain.at_height(1);
656 let chain_listener = MockChainListener::new();
657 let mut notifier = ChainNotifier {
658 header_cache: &mut chain.header_cache(0..=1),
661 let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
662 match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
663 Err((_, tip)) => assert_eq!(tip, None),
664 Ok(_) => panic!("Expected error"),
669 async fn sync_from_chain_without_any_new_blocks() {
670 let mut chain = Blockchain::default().with_height(3).without_blocks(2..);
672 let new_tip = chain.tip();
673 let old_tip = chain.at_height(1);
674 let chain_listener = MockChainListener::new();
675 let mut notifier = ChainNotifier {
676 header_cache: &mut chain.header_cache(0..=3),
679 let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
680 match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
681 Err((_, tip)) => assert_eq!(tip, Some(old_tip)),
682 Ok(_) => panic!("Expected error"),
687 async fn sync_from_chain_without_some_new_blocks() {
688 let mut chain = Blockchain::default().with_height(3).without_blocks(3..);
690 let new_tip = chain.tip();
691 let old_tip = chain.at_height(1);
692 let chain_listener = MockChainListener::new()
693 .expect_block_connected(*chain.at_height(2));
694 let mut notifier = ChainNotifier {
695 header_cache: &mut chain.header_cache(0..=3),
698 let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
699 match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
700 Err((_, tip)) => assert_eq!(tip, Some(chain.at_height(2))),
701 Ok(_) => panic!("Expected error"),