|
65 | 65 |
|
66 | 66 | use std::{cmp::Ordering, ops::RangeInclusive, time::Duration}; |
67 | 67 | use tokio::{ |
68 | | - join, |
69 | 68 | sync::{mpsc, oneshot}, |
| 69 | + try_join, |
70 | 70 | }; |
71 | 71 | use tokio_stream::{StreamExt, wrappers::ReceiverStream}; |
72 | 72 |
|
@@ -398,10 +398,8 @@ impl<N: Network> Service<N> { |
398 | 398 | self.provider.get_block_by_number(end_height) |
399 | 399 | )?; |
400 | 400 |
|
401 | | - let start_block_num = |
402 | | - start_block.ok_or_else(|| ScannerError::BlockNotFound(start_height))?.header().number(); |
403 | | - let end_block_num = |
404 | | - end_block.ok_or_else(|| ScannerError::BlockNotFound(end_height))?.header().number(); |
| 401 | + let start_block_num = start_block.header().number(); |
| 402 | + let end_block_num = end_block.header().number(); |
405 | 403 |
|
406 | 404 | let (start_block_num, end_block_num) = match start_block_num.cmp(&end_block_num) { |
407 | 405 | Ordering::Greater => (end_block_num, start_block_num), |
@@ -435,12 +433,8 @@ impl<N: Network> Service<N> { |
435 | 433 | self.provider.get_block_by_number(BlockNumberOrTag::Latest) |
436 | 434 | )?; |
437 | 435 |
|
438 | | - let start_block_num = |
439 | | - start_block.ok_or_else(|| ScannerError::BlockNotFound(start_height))?.header().number(); |
440 | | - let latest_block = latest_block |
441 | | - .ok_or_else(|| ScannerError::BlockNotFound(BlockNumberOrTag::Latest))? |
442 | | - .header() |
443 | | - .number(); |
| 436 | + let start_block_num = start_block.header().number(); |
| 437 | + let latest_block = latest_block.header().number(); |
444 | 438 |
|
445 | 439 | let confirmed_tip_num = latest_block.saturating_sub(block_confirmations); |
446 | 440 |
|
@@ -536,13 +530,10 @@ impl<N: Network> Service<N> { |
536 | 530 | start_height: BlockNumberOrTag, |
537 | 531 | end_height: BlockNumberOrTag, |
538 | 532 | ) -> Result<(), ScannerError> { |
539 | | - let (start_block, end_block) = join!( |
| 533 | + let (start_block, end_block) = try_join!( |
540 | 534 | self.provider.get_block_by_number(start_height), |
541 | 535 | self.provider.get_block_by_number(end_height), |
542 | | - ); |
543 | | - |
544 | | - let start_block = start_block?.ok_or(ScannerError::BlockNotFound(start_height))?; |
545 | | - let end_block = end_block?.ok_or(ScannerError::BlockNotFound(end_height))?; |
| 536 | + )?; |
546 | 537 |
|
547 | 538 | // normalize block range |
548 | 539 | let (from, to) = match start_block.header().number().cmp(&end_block.header().number()) { |
@@ -606,13 +597,7 @@ impl<N: Network> Service<N> { |
606 | 597 | // restart rewind |
607 | 598 | batch_from = from; |
608 | 599 | // store the updated end block hash |
609 | | - tip_hash = self |
610 | | - .provider |
611 | | - .get_block_by_number(from.into()) |
612 | | - .await? |
613 | | - .expect("Chain should have the same height post-reorg") |
614 | | - .header() |
615 | | - .hash(); |
| 600 | + tip_hash = self.provider.get_block_by_number(from.into()).await?.header().hash(); |
616 | 601 | } else { |
617 | 602 | // SAFETY: `batch_to` is always greater than `to`, so `batch_to - 1` is always |
618 | 603 | // a valid unsigned integer |
@@ -785,11 +770,7 @@ impl<N: Network> Service<N> { |
785 | 770 | async fn get_block_subscription( |
786 | 771 | provider: &RobustProvider<N>, |
787 | 772 | ) -> Result<Subscription<N::HeaderResponse>, ScannerError> { |
788 | | - let ws_stream = provider |
789 | | - .subscribe_blocks() |
790 | | - .await |
791 | | - .map_err(|_| ScannerError::WebSocketConnectionFailed(1))?; |
792 | | - |
| 773 | + let ws_stream = provider.subscribe_blocks().await?; |
793 | 774 | Ok(ws_stream) |
794 | 775 | } |
795 | 776 |
|
@@ -1656,13 +1637,11 @@ mod tests { |
1656 | 1637 | let (tx, mut rx) = mpsc::channel(1); |
1657 | 1638 | service.subscriber = Some(tx); |
1658 | 1639 |
|
1659 | | - service |
1660 | | - .send_to_subscriber(Message::Error(ScannerError::WebSocketConnectionFailed(4))) |
1661 | | - .await; |
| 1640 | + service.send_to_subscriber(Message::Error(ScannerError::BlockNotFound(4.into()))).await; |
1662 | 1641 |
|
1663 | 1642 | match rx.recv().await.expect("subscriber should stay open") { |
1664 | | - Message::Error(ScannerError::WebSocketConnectionFailed(attempts)) => { |
1665 | | - assert_eq!(attempts, 4); |
| 1643 | + Message::Error(ScannerError::BlockNotFound(attempts)) => { |
| 1644 | + assert_eq!(attempts, 4.into()); |
1666 | 1645 | } |
1667 | 1646 | other => panic!("unexpected message: {other:?}"), |
1668 | 1647 | } |
|
0 commit comments