Skip to content

Commit

Permalink
rustfmt: Run on lightning-block-sync/src/init.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Jul 15, 2024
1 parent 310ad9a commit fb57e04
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
54 changes: 33 additions & 21 deletions lightning-block-sync/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Utilities to assist in the initial sync required to initialize or reload Rust-Lightning objects
//! from disk.
use crate::{BlockSource, BlockSourceResult, Cache, ChainNotifier};
use crate::poll::{ChainPoller, Validate, ValidatedBlockHeader};
use crate::{BlockSource, BlockSourceResult, Cache, ChainNotifier};

use bitcoin::blockdata::block::Header;
use bitcoin::hash_types::BlockHash;
Expand All @@ -18,12 +18,14 @@ use std::ops::Deref;
/// start when there are no chain listeners to sync yet.
///
/// [`SpvClient`]: crate::SpvClient
pub async fn validate_best_block_header<B: Deref>(block_source: B) ->
BlockSourceResult<ValidatedBlockHeader> where B::Target: BlockSource {
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?
.validate(best_block_hash)
block_source.get_header(&best_block_hash, best_block_height).await?.validate(best_block_hash)
}

/// Performs a one-time sync of chain listeners using a single *trusted* block source, bringing each
Expand Down Expand Up @@ -131,22 +133,27 @@ BlockSourceResult<ValidatedBlockHeader> where B::Target: BlockSource {
/// [`SpvClient`]: crate::SpvClient
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
/// [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor
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,
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, &L)>,
) -> BlockSourceResult<ValidatedBlockHeader> where B::Target: BlockSource {
) -> 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();
for (old_block_hash, chain_listener) in chain_listeners.drain(..) {
let old_header = match header_cache.look_up(&old_block_hash) {
Some(header) => *header,
None => block_source
.get_header(&old_block_hash, None).await?
.validate(old_block_hash)?
None => {
block_source.get_header(&old_block_hash, None).await?.validate(old_block_hash)?
},
};
chain_listeners_with_old_headers.push((old_header, chain_listener))
}
Expand Down Expand Up @@ -180,8 +187,10 @@ pub async fn synchronize_listeners<B: Deref + Sized + Send + Sync, C: Cache, L:
if let Some(common_ancestor) = most_common_ancestor {
let chain_listener = &ChainListenerSet(chain_listeners_at_height);
let mut chain_notifier = ChainNotifier { header_cache, chain_listener };
chain_notifier.connect_blocks(common_ancestor, most_connected_blocks, &mut chain_poller)
.await.map_err(|(e, _)| e)?;
chain_notifier
.connect_blocks(common_ancestor, most_connected_blocks, &mut chain_poller)
.await
.map_err(|(e, _)| e)?;
}

Ok(best_header)
Expand Down Expand Up @@ -211,7 +220,9 @@ impl<'a, C: Cache> Cache for ReadOnlyCache<'a, C> {
struct DynamicChainListener<'a, L: chain::Listen + ?Sized>(&'a L);

impl<'a, L: chain::Listen + ?Sized> chain::Listen for DynamicChainListener<'a, L> {
fn filtered_block_connected(&self, _header: &Header, _txdata: &chain::transaction::TransactionData, _height: u32) {
fn filtered_block_connected(
&self, _header: &Header, _txdata: &chain::transaction::TransactionData, _height: u32,
) {
unreachable!()
}

Expand All @@ -234,7 +245,9 @@ impl<'a, L: chain::Listen + ?Sized> chain::Listen for ChainListenerSet<'a, L> {
}
}

fn filtered_block_connected(&self, header: &Header, txdata: &chain::transaction::TransactionData, height: u32) {
fn filtered_block_connected(
&self, header: &Header, txdata: &chain::transaction::TransactionData, height: u32,
) {
for (starting_height, chain_listener) in self.0.iter() {
if height > *starting_height {
chain_listener.filtered_block_connected(header, txdata, height);
Expand All @@ -249,8 +262,8 @@ impl<'a, L: chain::Listen + ?Sized> chain::Listen for ChainListenerSet<'a, L> {

#[cfg(test)]
mod tests {
use crate::test_utils::{Blockchain, MockChainListener};
use super::*;
use crate::test_utils::{Blockchain, MockChainListener};

#[tokio::test]
async fn sync_from_same_chain() {
Expand All @@ -263,8 +276,7 @@ mod tests {
let listener_2 = MockChainListener::new()
.expect_block_connected(*chain.at_height(3))
.expect_block_connected(*chain.at_height(4));
let listener_3 = MockChainListener::new()
.expect_block_connected(*chain.at_height(4));
let listener_3 = MockChainListener::new().expect_block_connected(*chain.at_height(4));

let listeners = vec![
(chain.at_height(1).block_hash, &listener_1 as &dyn chain::Listen),
Expand Down
1 change: 0 additions & 1 deletion rustfmt_excluded_files
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
./lightning-background-processor/src/lib.rs
./lightning-block-sync/src/init.rs
./lightning-block-sync/src/lib.rs
./lightning-block-sync/src/poll.rs
./lightning-block-sync/src/rest.rs
Expand Down

0 comments on commit fb57e04

Please sign in to comment.