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.
This commit is contained in:
Jeffrey Czyz 2022-04-15 09:03:00 -05:00
parent 03f655003d
commit b10217590b
No known key found for this signature in database
GPG key ID: 3A4E08275D5E96D2
2 changed files with 11 additions and 9 deletions

View 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();

View 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>
{