Make lightning-block-sync's init module slightly more generic 2022-03-listen-send-sync
authorMatt Corallo <git@bluematt.me>
Wed, 9 Mar 2022 18:15:30 +0000 (18:15 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 9 Mar 2022 18:20:43 +0000 (18:20 +0000)
Users who want to use lightning-block-sync's init module would
be reasonable in wanting to use it in a multithreaded environment,
however because it takes a list of listeners as dyn chain::Listen
without any Send or Sync bound they fail in doing so.

Here we make the type bounds on `chain::Listen` generic across
`chain::Listen + ?Sized`, which the existing bound of `&dyn
chain::Listen` satisfies. Thus, this is strictly less restrictive
and allows for the use of `&dyn chain::Listen + Send + Sync`.

lightning-block-sync/src/init.rs

index c0e37ac0b9dadf809365b012c9d195f6dcda57b1..d971cadcf8aea0d47a400a7a87cb4d20aaa213eb 100644 (file)
@@ -121,11 +121,11 @@ BlockSourceResult<ValidatedBlockHeader> {
 /// [`SpvClient`]: crate::SpvClient
 /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
 /// [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor
-pub async fn synchronize_listeners<B: BlockSource, C: Cache>(
+pub async fn synchronize_listeners<'a, B: BlockSource, C: Cache, L: chain::Listen + ?Sized>(
        block_source: &mut B,
        network: Network,
        header_cache: &mut C,
-       mut chain_listeners: Vec<(BlockHash, &dyn chain::Listen)>,
+       mut chain_listeners: Vec<(BlockHash, &'a L)>,
 ) -> BlockSourceResult<ValidatedBlockHeader> {
        let best_header = validate_best_block_header(block_source).await?;
 
@@ -198,9 +198,9 @@ impl<'a, C: Cache> Cache for ReadOnlyCache<'a, C> {
 }
 
 /// Wrapper for supporting dynamically sized chain listeners.
-struct DynamicChainListener<'a>(&'a dyn chain::Listen);
+struct DynamicChainListener<'a, L: chain::Listen + ?Sized>(&'a L);
 
-impl<'a> chain::Listen for DynamicChainListener<'a> {
+impl<'a, L: chain::Listen + ?Sized> chain::Listen for DynamicChainListener<'a, L> {
        fn block_connected(&self, _block: &Block, _height: u32) {
                unreachable!()
        }
@@ -211,9 +211,9 @@ impl<'a> chain::Listen for DynamicChainListener<'a> {
 }
 
 /// A set of dynamically sized chain listeners, each paired with a starting block height.
-struct ChainListenerSet<'a>(Vec<(u32, &'a dyn chain::Listen)>);
+struct ChainListenerSet<'a, L: chain::Listen + ?Sized>(Vec<(u32, &'a L)>);
 
-impl<'a> chain::Listen for ChainListenerSet<'a> {
+impl<'a, L: chain::Listen + ?Sized> chain::Listen for ChainListenerSet<'a, L> {
        fn block_connected(&self, block: &Block, height: u32) {
                for (starting_height, chain_listener) in self.0.iter() {
                        if height > *starting_height {