Allow &dyn BlockSource in lightning-block-sync
authorJeffrey Czyz <jkczyz@gmail.com>
Fri, 15 Apr 2022 14:03:00 +0000 (09:03 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Fri, 15 Apr 2022 16:38:48 +0000 (11:38 -0500)
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
lightning-block-sync/src/poll.rs

index 6611d185da393e3da10e11b720468adc06084a4c..c1f53b70171a4c77853b0a07457d4b33af921200 100644 (file)
@@ -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<B: BlockSource>(block_source: &B) ->
-BlockSourceResult<ValidatedBlockHeader> {
+pub async fn validate_best_block_header<B: Deref>(block_source: B) ->
+BlockSourceResult<ValidatedBlockHeader> 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<ValidatedBlockHeader> {
 /// [`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<ValidatedBlockHeader> {
-       let best_header = validate_best_block_header(block_source).await?;
+) -> BlockSourceResult<ValidatedBlockHeader> 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();
index b32d2239f69fd4b37513afc6a90076f768679a58..6e30d2e86d20d268529bd37d942492db810d84b6 100644 (file)
@@ -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<B: Deref<Target=T> + Sized, T: BlockSource> {
+pub struct ChainPoller<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> {
        block_source: B,
        network: Network,
 }
 
-impl<B: Deref<Target=T> + Sized, T: BlockSource> ChainPoller<B, T> {
+impl<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> ChainPoller<B, T> {
        /// 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<B: Deref<Target=T> + Sized, T: BlockSource> ChainPoller<B, T> {
        }
 }
 
-impl<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource> Poll for ChainPoller<B, T> {
+impl<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> Poll for ChainPoller<B, T> {
        fn poll_chain_tip<'a>(&'a self, best_known_chain_tip: ValidatedBlockHeader) ->
                AsyncBlockSourceResult<'a, ChainTip>
        {