Skip to content

Commit dc1b36d

Browse files
Merge pull request #1423 from jkczyz/2022-04-dyn-block-source
Allow `&dyn BlockSource` in `lightning-block-sync`
2 parents 03f6550 + b902cc7 commit dc1b36d

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

lightning-block-sync/src/init.rs

+17-15
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<B: Deref + Sized + Send + Sync, C: Cache, L: chain::Listen + ?Sized>(
127+
block_source: B,
126128
network: Network,
127129
header_cache: &mut C,
128-
mut chain_listeners: Vec<(BlockHash, &'a L)>,
129-
) -> BlockSourceResult<ValidatedBlockHeader> {
130-
let best_header = validate_best_block_header(block_source).await?;
130+
mut chain_listeners: Vec<(BlockHash, &L)>,
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();
@@ -236,7 +238,7 @@ mod tests {
236238

237239
#[tokio::test]
238240
async fn sync_from_same_chain() {
239-
let mut chain = Blockchain::default().with_height(4);
241+
let chain = Blockchain::default().with_height(4);
240242

241243
let listener_1 = MockChainListener::new()
242244
.expect_block_connected(*chain.at_height(2))
@@ -254,15 +256,15 @@ mod tests {
254256
(chain.at_height(3).block_hash, &listener_3 as &dyn chain::Listen),
255257
];
256258
let mut cache = chain.header_cache(0..=4);
257-
match synchronize_listeners(&mut chain, Network::Bitcoin, &mut cache, listeners).await {
259+
match synchronize_listeners(&chain, Network::Bitcoin, &mut cache, listeners).await {
258260
Ok(header) => assert_eq!(header, chain.tip()),
259261
Err(e) => panic!("Unexpected error: {:?}", e),
260262
}
261263
}
262264

263265
#[tokio::test]
264266
async fn sync_from_different_chains() {
265-
let mut main_chain = Blockchain::default().with_height(4);
267+
let main_chain = Blockchain::default().with_height(4);
266268
let fork_chain_1 = main_chain.fork_at_height(1);
267269
let fork_chain_2 = main_chain.fork_at_height(2);
268270
let fork_chain_3 = main_chain.fork_at_height(3);
@@ -291,15 +293,15 @@ mod tests {
291293
let mut cache = fork_chain_1.header_cache(2..=4);
292294
cache.extend(fork_chain_2.header_cache(3..=4));
293295
cache.extend(fork_chain_3.header_cache(4..=4));
294-
match synchronize_listeners(&mut main_chain, Network::Bitcoin, &mut cache, listeners).await {
296+
match synchronize_listeners(&main_chain, Network::Bitcoin, &mut cache, listeners).await {
295297
Ok(header) => assert_eq!(header, main_chain.tip()),
296298
Err(e) => panic!("Unexpected error: {:?}", e),
297299
}
298300
}
299301

300302
#[tokio::test]
301303
async fn sync_from_overlapping_chains() {
302-
let mut main_chain = Blockchain::default().with_height(4);
304+
let main_chain = Blockchain::default().with_height(4);
303305
let fork_chain_1 = main_chain.fork_at_height(1);
304306
let fork_chain_2 = fork_chain_1.fork_at_height(2);
305307
let fork_chain_3 = fork_chain_2.fork_at_height(3);
@@ -334,15 +336,15 @@ mod tests {
334336
let mut cache = fork_chain_1.header_cache(2..=4);
335337
cache.extend(fork_chain_2.header_cache(3..=4));
336338
cache.extend(fork_chain_3.header_cache(4..=4));
337-
match synchronize_listeners(&mut main_chain, Network::Bitcoin, &mut cache, listeners).await {
339+
match synchronize_listeners(&main_chain, Network::Bitcoin, &mut cache, listeners).await {
338340
Ok(header) => assert_eq!(header, main_chain.tip()),
339341
Err(e) => panic!("Unexpected error: {:?}", e),
340342
}
341343
}
342344

343345
#[tokio::test]
344346
async fn cache_connected_and_keep_disconnected_blocks() {
345-
let mut main_chain = Blockchain::default().with_height(2);
347+
let main_chain = Blockchain::default().with_height(2);
346348
let fork_chain = main_chain.fork_at_height(1);
347349
let new_tip = main_chain.tip();
348350
let old_tip = fork_chain.tip();
@@ -353,7 +355,7 @@ mod tests {
353355

354356
let listeners = vec![(old_tip.block_hash, &listener as &dyn chain::Listen)];
355357
let mut cache = fork_chain.header_cache(2..=2);
356-
match synchronize_listeners(&mut main_chain, Network::Bitcoin, &mut cache, listeners).await {
358+
match synchronize_listeners(&main_chain, Network::Bitcoin, &mut cache, listeners).await {
357359
Ok(_) => {
358360
assert!(cache.contains_key(&new_tip.block_hash));
359361
assert!(cache.contains_key(&old_tip.block_hash));

lightning-block-sync/src/poll.rs

+3-3
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)