From b10217590b82574d4e0f949774da50ecf630d12b Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Fri, 15 Apr 2022 09:03:00 -0500 Subject: [PATCH] Allow &dyn BlockSource in lightning-block-sync Update lightning-block-sync's init and poll modules to support &dyn BlockSource such that the BlockSource can be determined at runtime. --- lightning-block-sync/src/init.rs | 14 ++++++++------ lightning-block-sync/src/poll.rs | 6 +++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lightning-block-sync/src/init.rs b/lightning-block-sync/src/init.rs index 6611d185d..c1f53b701 100644 --- a/lightning-block-sync/src/init.rs +++ b/lightning-block-sync/src/init.rs @@ -10,14 +10,16 @@ use bitcoin::network::constants::Network; use lightning::chain; +use std::ops::Deref; + /// Returns a validated block header of the source's best chain tip. /// /// Upon success, the returned header can be used to initialize [`SpvClient`]. Useful during a fresh /// start when there are no chain listeners to sync yet. /// /// [`SpvClient`]: crate::SpvClient -pub async fn validate_best_block_header(block_source: &B) -> -BlockSourceResult { +pub async fn validate_best_block_header(block_source: B) -> +BlockSourceResult where B::Target: BlockSource { let (best_block_hash, best_block_height) = block_source.get_best_block().await?; block_source .get_header(&best_block_hash, best_block_height).await? @@ -121,13 +123,13 @@ BlockSourceResult { /// [`SpvClient`]: crate::SpvClient /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager /// [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor -pub async fn synchronize_listeners<'a, B: BlockSource, C: Cache, L: chain::Listen + ?Sized>( - block_source: &B, +pub async fn synchronize_listeners<'a, B: Deref + Sized + Send + Sync, C: Cache, L: chain::Listen + ?Sized>( + block_source: B, network: Network, header_cache: &mut C, mut chain_listeners: Vec<(BlockHash, &'a L)>, -) -> BlockSourceResult { - let best_header = validate_best_block_header(block_source).await?; +) -> BlockSourceResult where B::Target: BlockSource { + let best_header = validate_best_block_header(&*block_source).await?; // Fetch the header for the block hash paired with each listener. let mut chain_listeners_with_old_headers = Vec::new(); diff --git a/lightning-block-sync/src/poll.rs b/lightning-block-sync/src/poll.rs index b32d2239f..6e30d2e86 100644 --- a/lightning-block-sync/src/poll.rs +++ b/lightning-block-sync/src/poll.rs @@ -170,12 +170,12 @@ mod sealed { /// /// Other `Poll` implementations should be built using `ChainPoller` as it provides the simplest way /// of validating chain data and checking consistency. -pub struct ChainPoller + Sized, T: BlockSource> { +pub struct ChainPoller + Sized + Send + Sync, T: BlockSource + ?Sized> { block_source: B, network: Network, } -impl + Sized, T: BlockSource> ChainPoller { +impl + Sized + Send + Sync, T: BlockSource + ?Sized> ChainPoller { /// Creates a new poller for the given block source. /// /// If the `network` parameter is mainnet, then the difficulty between blocks is checked for @@ -185,7 +185,7 @@ impl + Sized, T: BlockSource> ChainPoller { } } -impl + Sized + Send + Sync, T: BlockSource> Poll for ChainPoller { +impl + Sized + Send + Sync, T: BlockSource + ?Sized> Poll for ChainPoller { fn poll_chain_tip<'a>(&'a self, best_known_chain_tip: ValidatedBlockHeader) -> AsyncBlockSourceResult<'a, ChainTip> { -- 2.39.5