Skip to content

Commit cb7f94a

Browse files
committed
rustfmt: Run on lightning-block-sync/src/lib.rs
1 parent 99f7c28 commit cb7f94a

File tree

2 files changed

+69
-89
lines changed

2 files changed

+69
-89
lines changed

lightning-block-sync/src/lib.rs

Lines changed: 69 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
1616
#![deny(rustdoc::broken_intra_doc_links)]
1717
#![deny(rustdoc::private_intra_doc_links)]
18-
1918
#![deny(missing_docs)]
2019
#![deny(unsafe_code)]
21-
2220
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2321

2422
#[cfg(any(feature = "rest-client", feature = "rpc-client"))]
@@ -58,18 +56,21 @@ use std::ops::Deref;
5856
use std::pin::Pin;
5957

6058
/// Abstract type for retrieving block headers and data.
61-
pub trait BlockSource : Sync + Send {
59+
pub trait BlockSource: Sync + Send {
6260
/// Returns the header for a given hash. A height hint may be provided in case a block source
6361
/// cannot easily find headers based on a hash. This is merely a hint and thus the returned
6462
/// header must have the same hash as was requested. Otherwise, an error must be returned.
6563
///
6664
/// Implementations that cannot find headers based on the hash should return a `Transient` error
6765
/// when `height_hint` is `None`.
68-
fn get_header<'a>(&'a self, header_hash: &'a BlockHash, height_hint: Option<u32>) -> AsyncBlockSourceResult<'a, BlockHeaderData>;
66+
fn get_header<'a>(
67+
&'a self, header_hash: &'a BlockHash, height_hint: Option<u32>,
68+
) -> AsyncBlockSourceResult<'a, BlockHeaderData>;
6969

7070
/// Returns the block for a given hash. A headers-only block source should return a `Transient`
7171
/// error.
72-
fn get_block<'a>(&'a self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, BlockData>;
72+
fn get_block<'a>(&'a self, header_hash: &'a BlockHash)
73+
-> AsyncBlockSourceResult<'a, BlockData>;
7374

7475
/// Returns the hash of the best block and, optionally, its height.
7576
///
@@ -86,7 +87,8 @@ pub type BlockSourceResult<T> = Result<T, BlockSourceError>;
8687
// TODO: Replace with BlockSourceResult once `async` trait functions are supported. For details,
8788
// see: https://areweasyncyet.rs.
8889
/// Result type for asynchronous `BlockSource` requests.
89-
pub type AsyncBlockSourceResult<'a, T> = Pin<Box<dyn Future<Output = BlockSourceResult<T>> + 'a + Send>>;
90+
pub type AsyncBlockSourceResult<'a, T> =
91+
Pin<Box<dyn Future<Output = BlockSourceResult<T>> + 'a + Send>>;
9092

9193
/// Error type for `BlockSource` requests.
9294
///
@@ -111,20 +113,18 @@ pub enum BlockSourceErrorKind {
111113
impl BlockSourceError {
112114
/// Creates a new persistent error originated from the given error.
113115
pub fn persistent<E>(error: E) -> Self
114-
where E: Into<Box<dyn std::error::Error + Send + Sync>> {
115-
Self {
116-
kind: BlockSourceErrorKind::Persistent,
117-
error: error.into(),
118-
}
116+
where
117+
E: Into<Box<dyn std::error::Error + Send + Sync>>,
118+
{
119+
Self { kind: BlockSourceErrorKind::Persistent, error: error.into() }
119120
}
120121

121122
/// Creates a new transient error originated from the given error.
122123
pub fn transient<E>(error: E) -> Self
123-
where E: Into<Box<dyn std::error::Error + Send + Sync>> {
124-
Self {
125-
kind: BlockSourceErrorKind::Transient,
126-
error: error.into(),
127-
}
124+
where
125+
E: Into<Box<dyn std::error::Error + Send + Sync>>,
126+
{
127+
Self { kind: BlockSourceErrorKind::Transient, error: error.into() }
128128
}
129129

130130
/// Returns the kind of error.
@@ -180,7 +180,9 @@ pub enum BlockData {
180180
/// Hence, there is a trade-off between a lower memory footprint and potentially increased network
181181
/// I/O as headers are re-fetched during fork detection.
182182
pub struct SpvClient<'a, P: Poll, C: Cache, L: Deref>
183-
where L::Target: chain::Listen {
183+
where
184+
L::Target: chain::Listen,
185+
{
184186
chain_tip: ValidatedBlockHeader,
185187
chain_poller: P,
186188
chain_notifier: ChainNotifier<'a, C, L>,
@@ -226,7 +228,10 @@ impl Cache for UnboundedCache {
226228
}
227229
}
228230

229-
impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L> where L::Target: chain::Listen {
231+
impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L>
232+
where
233+
L::Target: chain::Listen,
234+
{
230235
/// Creates a new SPV client using `chain_tip` as the best known chain tip.
231236
///
232237
/// Subsequent calls to [`poll_best_tip`] will poll for the best chain tip using the given chain
@@ -238,9 +243,7 @@ impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L> where L::Target: ch
238243
///
239244
/// [`poll_best_tip`]: SpvClient::poll_best_tip
240245
pub fn new(
241-
chain_tip: ValidatedBlockHeader,
242-
chain_poller: P,
243-
header_cache: &'a mut C,
246+
chain_tip: ValidatedBlockHeader, chain_poller: P, header_cache: &'a mut C,
244247
chain_listener: L,
245248
) -> Self {
246249
let chain_notifier = ChainNotifier { header_cache, chain_listener };
@@ -273,8 +276,10 @@ impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L> where L::Target: ch
273276
/// Updates the chain tip, syncing the chain listener with any connected or disconnected
274277
/// blocks. Returns whether there were any such blocks.
275278
async fn update_chain_tip(&mut self, best_chain_tip: ValidatedBlockHeader) -> bool {
276-
match self.chain_notifier.synchronize_listener(
277-
best_chain_tip, &self.chain_tip, &mut self.chain_poller).await
279+
match self
280+
.chain_notifier
281+
.synchronize_listener(best_chain_tip, &self.chain_tip, &mut self.chain_poller)
282+
.await
278283
{
279284
Ok(_) => {
280285
self.chain_tip = best_chain_tip;
@@ -292,7 +297,10 @@ impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L> where L::Target: ch
292297
/// Notifies [listeners] of blocks that have been connected or disconnected from the chain.
293298
///
294299
/// [listeners]: lightning::chain::Listen
295-
pub struct ChainNotifier<'a, C: Cache, L: Deref> where L::Target: chain::Listen {
300+
pub struct ChainNotifier<'a, C: Cache, L: Deref>
301+
where
302+
L::Target: chain::Listen,
303+
{
296304
/// Cache for looking up headers before fetching from a block source.
297305
header_cache: &'a mut C,
298306

@@ -318,7 +326,10 @@ struct ChainDifference {
318326
connected_blocks: Vec<ValidatedBlockHeader>,
319327
}
320328

321-
impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L> where L::Target: chain::Listen {
329+
impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L>
330+
where
331+
L::Target: chain::Listen,
332+
{
322333
/// Finds the first common ancestor between `new_header` and `old_header`, disconnecting blocks
323334
/// from `old_header` to get to that point and then connecting blocks until `new_header`.
324335
///
@@ -327,29 +338,24 @@ impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L> where L::Target: chain::Lis
327338
/// ended up which may not be `new_header`. Note that the returned `Err` contains `Some` header
328339
/// if and only if the transition from `old_header` to `new_header` is valid.
329340
async fn synchronize_listener<P: Poll>(
330-
&mut self,
331-
new_header: ValidatedBlockHeader,
332-
old_header: &ValidatedBlockHeader,
341+
&mut self, new_header: ValidatedBlockHeader, old_header: &ValidatedBlockHeader,
333342
chain_poller: &mut P,
334343
) -> Result<(), (BlockSourceError, Option<ValidatedBlockHeader>)> {
335-
let difference = self.find_difference(new_header, old_header, chain_poller).await
344+
let difference = self
345+
.find_difference(new_header, old_header, chain_poller)
346+
.await
336347
.map_err(|e| (e, None))?;
337348
self.disconnect_blocks(difference.disconnected_blocks);
338-
self.connect_blocks(
339-
difference.common_ancestor,
340-
difference.connected_blocks,
341-
chain_poller,
342-
).await
349+
self.connect_blocks(difference.common_ancestor, difference.connected_blocks, chain_poller)
350+
.await
343351
}
344352

345353
/// Returns the changes needed to produce the chain with `current_header` as its tip from the
346354
/// chain with `prev_header` as its tip.
347355
///
348356
/// Walks backwards from `current_header` and `prev_header`, finding the common ancestor.
349357
async fn find_difference<P: Poll>(
350-
&self,
351-
current_header: ValidatedBlockHeader,
352-
prev_header: &ValidatedBlockHeader,
358+
&self, current_header: ValidatedBlockHeader, prev_header: &ValidatedBlockHeader,
353359
chain_poller: &mut P,
354360
) -> BlockSourceResult<ChainDifference> {
355361
let mut disconnected_blocks = Vec::new();
@@ -383,9 +389,7 @@ impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L> where L::Target: chain::Lis
383389
/// Returns the previous header for the given header, either by looking it up in the cache or
384390
/// fetching it if not found.
385391
async fn look_up_previous_header<P: Poll>(
386-
&self,
387-
chain_poller: &mut P,
388-
header: &ValidatedBlockHeader,
392+
&self, chain_poller: &mut P, header: &ValidatedBlockHeader,
389393
) -> BlockSourceResult<ValidatedBlockHeader> {
390394
match self.header_cache.look_up(&header.header.prev_blockhash) {
391395
Some(prev_header) => Ok(*prev_header),
@@ -405,16 +409,13 @@ impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L> where L::Target: chain::Lis
405409

406410
/// Notifies the chain listeners of connected blocks.
407411
async fn connect_blocks<P: Poll>(
408-
&mut self,
409-
mut new_tip: ValidatedBlockHeader,
410-
mut connected_blocks: Vec<ValidatedBlockHeader>,
411-
chain_poller: &mut P,
412+
&mut self, mut new_tip: ValidatedBlockHeader,
413+
mut connected_blocks: Vec<ValidatedBlockHeader>, chain_poller: &mut P,
412414
) -> Result<(), (BlockSourceError, Option<ValidatedBlockHeader>)> {
413415
for header in connected_blocks.drain(..).rev() {
414416
let height = header.height;
415-
let block_data = chain_poller
416-
.fetch_block(&header).await
417-
.map_err(|e| (e, Some(new_tip)))?;
417+
let block_data =
418+
chain_poller.fetch_block(&header).await.map_err(|e| (e, Some(new_tip)))?;
418419
debug_assert_eq!(block_data.block_hash, header.block_hash);
419420

420421
match block_data.deref() {
@@ -436,8 +437,8 @@ impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L> where L::Target: chain::Lis
436437

437438
#[cfg(test)]
438439
mod spv_client_tests {
439-
use crate::test_utils::{Blockchain, NullChainListener};
440440
use super::*;
441+
use crate::test_utils::{Blockchain, NullChainListener};
441442

442443
use bitcoin::network::Network;
443444

@@ -563,8 +564,8 @@ mod spv_client_tests {
563564

564565
#[cfg(test)]
565566
mod chain_notifier_tests {
566-
use crate::test_utils::{Blockchain, MockChainListener};
567567
use super::*;
568+
use crate::test_utils::{Blockchain, MockChainListener};
568569

569570
use bitcoin::network::Network;
570571

@@ -577,10 +578,8 @@ mod chain_notifier_tests {
577578
let chain_listener = &MockChainListener::new()
578579
.expect_block_connected(*chain.at_height(2))
579580
.expect_block_connected(*new_tip);
580-
let mut notifier = ChainNotifier {
581-
header_cache: &mut chain.header_cache(0..=1),
582-
chain_listener,
583-
};
581+
let mut notifier =
582+
ChainNotifier { header_cache: &mut chain.header_cache(0..=1), chain_listener };
584583
let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
585584
match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
586585
Err((e, _)) => panic!("Unexpected error: {:?}", e),
@@ -596,10 +595,8 @@ mod chain_notifier_tests {
596595
let new_tip = test_chain.tip();
597596
let old_tip = main_chain.tip();
598597
let chain_listener = &MockChainListener::new();
599-
let mut notifier = ChainNotifier {
600-
header_cache: &mut main_chain.header_cache(0..=1),
601-
chain_listener,
602-
};
598+
let mut notifier =
599+
ChainNotifier { header_cache: &mut main_chain.header_cache(0..=1), chain_listener };
603600
let mut poller = poll::ChainPoller::new(&mut test_chain, Network::Testnet);
604601
match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
605602
Err((e, _)) => {
@@ -620,10 +617,8 @@ mod chain_notifier_tests {
620617
let chain_listener = &MockChainListener::new()
621618
.expect_block_disconnected(*old_tip)
622619
.expect_block_connected(*new_tip);
623-
let mut notifier = ChainNotifier {
624-
header_cache: &mut main_chain.header_cache(0..=2),
625-
chain_listener,
626-
};
620+
let mut notifier =
621+
ChainNotifier { header_cache: &mut main_chain.header_cache(0..=2), chain_listener };
627622
let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet);
628623
match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
629624
Err((e, _)) => panic!("Unexpected error: {:?}", e),
@@ -643,10 +638,8 @@ mod chain_notifier_tests {
643638
.expect_block_disconnected(*old_tip)
644639
.expect_block_disconnected(*main_chain.at_height(2))
645640
.expect_block_connected(*new_tip);
646-
let mut notifier = ChainNotifier {
647-
header_cache: &mut main_chain.header_cache(0..=3),
648-
chain_listener,
649-
};
641+
let mut notifier =
642+
ChainNotifier { header_cache: &mut main_chain.header_cache(0..=3), chain_listener };
650643
let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet);
651644
match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
652645
Err((e, _)) => panic!("Unexpected error: {:?}", e),
@@ -666,10 +659,8 @@ mod chain_notifier_tests {
666659
.expect_block_disconnected(*old_tip)
667660
.expect_block_connected(*fork_chain.at_height(2))
668661
.expect_block_connected(*new_tip);
669-
let mut notifier = ChainNotifier {
670-
header_cache: &mut main_chain.header_cache(0..=2),
671-
chain_listener,
672-
};
662+
let mut notifier =
663+
ChainNotifier { header_cache: &mut main_chain.header_cache(0..=2), chain_listener };
673664
let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet);
674665
match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
675666
Err((e, _)) => panic!("Unexpected error: {:?}", e),
@@ -684,10 +675,8 @@ mod chain_notifier_tests {
684675
let new_tip = chain.tip();
685676
let old_tip = chain.at_height(1);
686677
let chain_listener = &MockChainListener::new();
687-
let mut notifier = ChainNotifier {
688-
header_cache: &mut chain.header_cache(0..=1),
689-
chain_listener,
690-
};
678+
let mut notifier =
679+
ChainNotifier { header_cache: &mut chain.header_cache(0..=1), chain_listener };
691680
let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
692681
match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
693682
Err((_, tip)) => assert_eq!(tip, None),
@@ -702,10 +691,8 @@ mod chain_notifier_tests {
702691
let new_tip = chain.tip();
703692
let old_tip = chain.at_height(1);
704693
let chain_listener = &MockChainListener::new();
705-
let mut notifier = ChainNotifier {
706-
header_cache: &mut chain.header_cache(0..=3),
707-
chain_listener,
708-
};
694+
let mut notifier =
695+
ChainNotifier { header_cache: &mut chain.header_cache(0..=3), chain_listener };
709696
let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
710697
match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
711698
Err((_, tip)) => assert_eq!(tip, Some(old_tip)),
@@ -719,12 +706,9 @@ mod chain_notifier_tests {
719706

720707
let new_tip = chain.tip();
721708
let old_tip = chain.at_height(1);
722-
let chain_listener = &MockChainListener::new()
723-
.expect_block_connected(*chain.at_height(2));
724-
let mut notifier = ChainNotifier {
725-
header_cache: &mut chain.header_cache(0..=3),
726-
chain_listener,
727-
};
709+
let chain_listener = &MockChainListener::new().expect_block_connected(*chain.at_height(2));
710+
let mut notifier =
711+
ChainNotifier { header_cache: &mut chain.header_cache(0..=3), chain_listener };
728712
let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
729713
match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
730714
Err((_, tip)) => assert_eq!(tip, Some(chain.at_height(2))),
@@ -741,15 +725,12 @@ mod chain_notifier_tests {
741725
let chain_listener = &MockChainListener::new()
742726
.expect_filtered_block_connected(*chain.at_height(2))
743727
.expect_filtered_block_connected(*new_tip);
744-
let mut notifier = ChainNotifier {
745-
header_cache: &mut chain.header_cache(0..=1),
746-
chain_listener,
747-
};
728+
let mut notifier =
729+
ChainNotifier { header_cache: &mut chain.header_cache(0..=1), chain_listener };
748730
let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
749731
match notifier.synchronize_listener(new_tip, &old_tip, &mut poller).await {
750732
Err((e, _)) => panic!("Unexpected error: {:?}", e),
751733
Ok(_) => {},
752734
}
753735
}
754-
755736
}

rustfmt_excluded_files

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
./lightning-background-processor/src/lib.rs
2-
./lightning-block-sync/src/lib.rs
32
./lightning-custom-message/src/lib.rs
43
./lightning-invoice/fuzz/fuzz_targets/serde_data_part.rs
54
./lightning-invoice/src/de.rs

0 commit comments

Comments
 (0)