Skip to content

Commit b102175

Browse files
committed
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.
1 parent 03f6550 commit b102175

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

lightning-block-sync/src/init.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ use bitcoin::network::constants::Network;
1010

1111
use lightning::chain;
1212

13+
use std::ops::Deref;
14+
1315
/// Returns a validated block header of the source's best chain tip.
1416
///
1517
/// Upon success, the returned header can be used to initialize [`SpvClient`]. Useful during a fresh
1618
/// start when there are no chain listeners to sync yet.
1719
///
1820
/// [`SpvClient`]: crate::SpvClient
19-
pub async fn validate_best_block_header<B: BlockSource>(block_source: &B) ->
20-
BlockSourceResult<ValidatedBlockHeader> {
21+
pub async fn validate_best_block_header<B: Deref>(block_source: B) ->
22+
BlockSourceResult<ValidatedBlockHeader> where B::Target: BlockSource {
2123
let (best_block_hash, best_block_height) = block_source.get_best_block().await?;
2224
block_source
2325
.get_header(&best_block_hash, best_block_height).await?
@@ -121,13 +123,13 @@ BlockSourceResult<ValidatedBlockHeader> {
121123
/// [`SpvClient`]: crate::SpvClient
122124
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
123125
/// [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor
124-
pub async fn synchronize_listeners<'a, B: BlockSource, C: Cache, L: chain::Listen + ?Sized>(
125-
block_source: &B,
126+
pub async fn synchronize_listeners<'a, B: Deref + Sized + Send + Sync, C: Cache, L: chain::Listen + ?Sized>(
127+
block_source: B,
126128
network: Network,
127129
header_cache: &mut C,
128130
mut chain_listeners: Vec<(BlockHash, &'a L)>,
129-
) -> BlockSourceResult<ValidatedBlockHeader> {
130-
let best_header = validate_best_block_header(block_source).await?;
131+
) -> BlockSourceResult<ValidatedBlockHeader> where B::Target: BlockSource {
132+
let best_header = validate_best_block_header(&*block_source).await?;
131133

132134
// Fetch the header for the block hash paired with each listener.
133135
let mut chain_listeners_with_old_headers = Vec::new();

lightning-block-sync/src/poll.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ mod sealed {
170170
///
171171
/// Other `Poll` implementations should be built using `ChainPoller` as it provides the simplest way
172172
/// of validating chain data and checking consistency.
173-
pub struct ChainPoller<B: Deref<Target=T> + Sized, T: BlockSource> {
173+
pub struct ChainPoller<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> {
174174
block_source: B,
175175
network: Network,
176176
}
177177

178-
impl<B: Deref<Target=T> + Sized, T: BlockSource> ChainPoller<B, T> {
178+
impl<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> ChainPoller<B, T> {
179179
/// Creates a new poller for the given block source.
180180
///
181181
/// 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> {
185185
}
186186
}
187187

188-
impl<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource> Poll for ChainPoller<B, T> {
188+
impl<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> Poll for ChainPoller<B, T> {
189189
fn poll_chain_tip<'a>(&'a self, best_known_chain_tip: ValidatedBlockHeader) ->
190190
AsyncBlockSourceResult<'a, ChainTip>
191191
{

0 commit comments

Comments
 (0)