Skip to content

Allow &dyn BlockSource in lightning-block-sync #1423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions lightning-block-sync/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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<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?;
mut chain_listeners: Vec<(BlockHash, &L)>,
) -> 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();
Expand Down Expand Up @@ -236,7 +238,7 @@ mod tests {

#[tokio::test]
async fn sync_from_same_chain() {
let mut chain = Blockchain::default().with_height(4);
let chain = Blockchain::default().with_height(4);

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

#[tokio::test]
async fn sync_from_different_chains() {
let mut main_chain = Blockchain::default().with_height(4);
let main_chain = Blockchain::default().with_height(4);
let fork_chain_1 = main_chain.fork_at_height(1);
let fork_chain_2 = main_chain.fork_at_height(2);
let fork_chain_3 = main_chain.fork_at_height(3);
Expand Down Expand Up @@ -291,15 +293,15 @@ mod tests {
let mut cache = fork_chain_1.header_cache(2..=4);
cache.extend(fork_chain_2.header_cache(3..=4));
cache.extend(fork_chain_3.header_cache(4..=4));
match synchronize_listeners(&mut main_chain, Network::Bitcoin, &mut cache, listeners).await {
match synchronize_listeners(&main_chain, Network::Bitcoin, &mut cache, listeners).await {
Ok(header) => assert_eq!(header, main_chain.tip()),
Err(e) => panic!("Unexpected error: {:?}", e),
}
}

#[tokio::test]
async fn sync_from_overlapping_chains() {
let mut main_chain = Blockchain::default().with_height(4);
let main_chain = Blockchain::default().with_height(4);
let fork_chain_1 = main_chain.fork_at_height(1);
let fork_chain_2 = fork_chain_1.fork_at_height(2);
let fork_chain_3 = fork_chain_2.fork_at_height(3);
Expand Down Expand Up @@ -334,15 +336,15 @@ mod tests {
let mut cache = fork_chain_1.header_cache(2..=4);
cache.extend(fork_chain_2.header_cache(3..=4));
cache.extend(fork_chain_3.header_cache(4..=4));
match synchronize_listeners(&mut main_chain, Network::Bitcoin, &mut cache, listeners).await {
match synchronize_listeners(&main_chain, Network::Bitcoin, &mut cache, listeners).await {
Ok(header) => assert_eq!(header, main_chain.tip()),
Err(e) => panic!("Unexpected error: {:?}", e),
}
}

#[tokio::test]
async fn cache_connected_and_keep_disconnected_blocks() {
let mut main_chain = Blockchain::default().with_height(2);
let main_chain = Blockchain::default().with_height(2);
let fork_chain = main_chain.fork_at_height(1);
let new_tip = main_chain.tip();
let old_tip = fork_chain.tip();
Expand All @@ -353,7 +355,7 @@ mod tests {

let listeners = vec![(old_tip.block_hash, &listener as &dyn chain::Listen)];
let mut cache = fork_chain.header_cache(2..=2);
match synchronize_listeners(&mut main_chain, Network::Bitcoin, &mut cache, listeners).await {
match synchronize_listeners(&main_chain, Network::Bitcoin, &mut cache, listeners).await {
Ok(_) => {
assert!(cache.contains_key(&new_tip.block_hash));
assert!(cache.contains_key(&old_tip.block_hash));
Expand Down
6 changes: 3 additions & 3 deletions lightning-block-sync/src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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>
{
Expand Down