Skip to content

Commit c3c909b

Browse files
authored
Merge pull request #3136 from tnull/2024-06-rustfmt-lightning-block-sync
Run `rustfmt` on `lightning-block-sync`
2 parents ac1463b + 12e4efa commit c3c909b

File tree

11 files changed

+623
-487
lines changed

11 files changed

+623
-487
lines changed

lightning-block-sync/src/convert.rs

Lines changed: 190 additions & 170 deletions
Large diffs are not rendered by default.

lightning-block-sync/src/gossip.rs

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ use crate::{AsyncBlockSourceResult, BlockData, BlockSource, BlockSourceError};
66

77
use bitcoin::blockdata::block::Block;
88
use bitcoin::blockdata::constants::ChainHash;
9-
use bitcoin::blockdata::transaction::{TxOut, OutPoint};
9+
use bitcoin::blockdata::transaction::{OutPoint, TxOut};
1010
use bitcoin::hash_types::BlockHash;
1111

1212
use lightning::ln::peer_handler::APeerManager;
1313

1414
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
15-
use lightning::routing::utxo::{UtxoFuture, UtxoLookup, UtxoResult, UtxoLookupError};
15+
use lightning::routing::utxo::{UtxoFuture, UtxoLookup, UtxoLookupError, UtxoResult};
1616

1717
use lightning::util::logger::Logger;
1818

19-
use std::sync::{Arc, Mutex};
2019
use std::collections::VecDeque;
2120
use std::future::Future;
2221
use std::ops::Deref;
2322
use std::pin::Pin;
23+
use std::sync::{Arc, Mutex};
2424
use std::task::Poll;
2525

2626
/// A trait which extends [`BlockSource`] and can be queried to fetch the block at a given height
@@ -29,12 +29,14 @@ use std::task::Poll;
2929
/// Note that while this is implementable for a [`BlockSource`] which returns filtered block data
3030
/// (i.e. [`BlockData::HeaderOnly`] for [`BlockSource::get_block`] requests), such an
3131
/// implementation will reject all gossip as it is not fully able to verify the UTXOs referenced.
32-
pub trait UtxoSource : BlockSource + 'static {
32+
pub trait UtxoSource: BlockSource + 'static {
3333
/// Fetches the block hash of the block at the given height.
3434
///
3535
/// This will, in turn, be passed to to [`BlockSource::get_block`] to fetch the block needed
3636
/// for gossip validation.
37-
fn get_block_hash_by_height<'a>(&'a self, block_height: u32) -> AsyncBlockSourceResult<'a, BlockHash>;
37+
fn get_block_hash_by_height<'a>(
38+
&'a self, block_height: u32,
39+
) -> AsyncBlockSourceResult<'a, BlockHash>;
3840

3941
/// Returns true if the given output has *not* been spent, i.e. is a member of the current UTXO
4042
/// set.
@@ -45,7 +47,7 @@ pub trait UtxoSource : BlockSource + 'static {
4547
///
4648
/// If the `tokio` feature is enabled, this is implemented on `TokioSpawner` struct which
4749
/// delegates to `tokio::spawn()`.
48-
pub trait FutureSpawner : Send + Sync + 'static {
50+
pub trait FutureSpawner: Send + Sync + 'static {
4951
/// Spawns the given future as a background task.
5052
///
5153
/// This method MUST NOT block on the given future immediately.
@@ -65,8 +67,8 @@ impl FutureSpawner for TokioSpawner {
6567
/// A trivial future which joins two other futures and polls them at the same time, returning only
6668
/// once both complete.
6769
pub(crate) struct Joiner<
68-
A: Future<Output=Result<(BlockHash, Option<u32>), BlockSourceError>> + Unpin,
69-
B: Future<Output=Result<BlockHash, BlockSourceError>> + Unpin,
70+
A: Future<Output = Result<(BlockHash, Option<u32>), BlockSourceError>> + Unpin,
71+
B: Future<Output = Result<BlockHash, BlockSourceError>> + Unpin,
7072
> {
7173
pub a: A,
7274
pub b: B,
@@ -75,16 +77,20 @@ pub(crate) struct Joiner<
7577
}
7678

7779
impl<
78-
A: Future<Output=Result<(BlockHash, Option<u32>), BlockSourceError>> + Unpin,
79-
B: Future<Output=Result<BlockHash, BlockSourceError>> + Unpin,
80-
> Joiner<A, B> {
81-
fn new(a: A, b: B) -> Self { Self { a, b, a_res: None, b_res: None } }
80+
A: Future<Output = Result<(BlockHash, Option<u32>), BlockSourceError>> + Unpin,
81+
B: Future<Output = Result<BlockHash, BlockSourceError>> + Unpin,
82+
> Joiner<A, B>
83+
{
84+
fn new(a: A, b: B) -> Self {
85+
Self { a, b, a_res: None, b_res: None }
86+
}
8287
}
8388

8489
impl<
85-
A: Future<Output=Result<(BlockHash, Option<u32>), BlockSourceError>> + Unpin,
86-
B: Future<Output=Result<BlockHash, BlockSourceError>> + Unpin,
87-
> Future for Joiner<A, B> {
90+
A: Future<Output = Result<(BlockHash, Option<u32>), BlockSourceError>> + Unpin,
91+
B: Future<Output = Result<BlockHash, BlockSourceError>> + Unpin,
92+
> Future for Joiner<A, B>
93+
{
8894
type Output = Result<((BlockHash, Option<u32>), BlockHash), BlockSourceError>;
8995
fn poll(mut self: Pin<&mut Self>, ctx: &mut core::task::Context<'_>) -> Poll<Self::Output> {
9096
if self.a_res.is_none() {
@@ -107,14 +113,13 @@ impl<
107113
} else {
108114
return Poll::Ready(Err(res.unwrap_err()));
109115
}
110-
111116
},
112117
Poll::Pending => {},
113118
}
114119
}
115120
if let Some(b_res) = self.b_res {
116121
if let Some(a_res) = self.a_res {
117-
return Poll::Ready(Ok((a_res, b_res)))
122+
return Poll::Ready(Ok((a_res, b_res)));
118123
}
119124
}
120125
Poll::Pending
@@ -129,7 +134,8 @@ impl<
129134
/// value of 1024 should more than suffice), and ensure you have sufficient file descriptors
130135
/// available on both Bitcoin Core and your LDK application for each request to hold its own
131136
/// connection.
132-
pub struct GossipVerifier<S: FutureSpawner,
137+
pub struct GossipVerifier<
138+
S: FutureSpawner,
133139
Blocks: Deref + Send + Sync + 'static + Clone,
134140
L: Deref + Send + Sync + 'static,
135141
> where
@@ -145,10 +151,9 @@ pub struct GossipVerifier<S: FutureSpawner,
145151

146152
const BLOCK_CACHE_SIZE: usize = 5;
147153

148-
impl<S: FutureSpawner,
149-
Blocks: Deref + Send + Sync + Clone,
150-
L: Deref + Send + Sync,
151-
> GossipVerifier<S, Blocks, L> where
154+
impl<S: FutureSpawner, Blocks: Deref + Send + Sync + Clone, L: Deref + Send + Sync>
155+
GossipVerifier<S, Blocks, L>
156+
where
152157
Blocks::Target: UtxoSource,
153158
L::Target: Logger,
154159
{
@@ -157,27 +162,34 @@ impl<S: FutureSpawner,
157162
/// This is expected to be given to a [`P2PGossipSync`] (initially constructed with `None` for
158163
/// the UTXO lookup) via [`P2PGossipSync::add_utxo_lookup`].
159164
pub fn new<APM: Deref + Send + Sync + Clone + 'static>(
160-
source: Blocks, spawn: S, gossiper: Arc<P2PGossipSync<Arc<NetworkGraph<L>>, Self, L>>, peer_manager: APM
161-
) -> Self where APM::Target: APeerManager {
165+
source: Blocks, spawn: S, gossiper: Arc<P2PGossipSync<Arc<NetworkGraph<L>>, Self, L>>,
166+
peer_manager: APM,
167+
) -> Self
168+
where
169+
APM::Target: APeerManager,
170+
{
162171
let peer_manager_wake = Arc::new(move || peer_manager.as_ref().process_events());
163172
Self {
164-
source, spawn, gossiper, peer_manager_wake,
173+
source,
174+
spawn,
175+
gossiper,
176+
peer_manager_wake,
165177
block_cache: Arc::new(Mutex::new(VecDeque::with_capacity(BLOCK_CACHE_SIZE))),
166178
}
167179
}
168180

169181
async fn retrieve_utxo(
170-
source: Blocks, block_cache: Arc<Mutex<VecDeque<(u32, Block)>>>, short_channel_id: u64
182+
source: Blocks, block_cache: Arc<Mutex<VecDeque<(u32, Block)>>>, short_channel_id: u64,
171183
) -> Result<TxOut, UtxoLookupError> {
172184
let block_height = (short_channel_id >> 5 * 8) as u32; // block height is most significant three bytes
173185
let transaction_index = ((short_channel_id >> 2 * 8) & 0xffffff) as u32;
174186
let output_index = (short_channel_id & 0xffff) as u16;
175187

176188
let (outpoint, output);
177189

178-
'tx_found: loop { // Used as a simple goto
190+
'tx_found: loop {
179191
macro_rules! process_block {
180-
($block: expr) => { {
192+
($block: expr) => {{
181193
if transaction_index as usize >= $block.txdata.len() {
182194
return Err(UtxoLookupError::UnknownTx);
183195
}
@@ -188,7 +200,7 @@ impl<S: FutureSpawner,
188200

189201
outpoint = OutPoint::new(transaction.txid(), output_index.into());
190202
output = transaction.output[output_index as usize].clone();
191-
} }
203+
}};
192204
}
193205
{
194206
let recent_blocks = block_cache.lock().unwrap();
@@ -202,8 +214,8 @@ impl<S: FutureSpawner,
202214

203215
let ((_, tip_height_opt), block_hash) =
204216
Joiner::new(source.get_best_block(), source.get_block_hash_by_height(block_height))
205-
.await
206-
.map_err(|_| UtxoLookupError::UnknownTx)?;
217+
.await
218+
.map_err(|_| UtxoLookupError::UnknownTx)?;
207219
if let Some(tip_height) = tip_height_opt {
208220
// If the block doesn't yet have five confirmations, error out.
209221
//
@@ -214,8 +226,8 @@ impl<S: FutureSpawner,
214226
return Err(UtxoLookupError::UnknownTx);
215227
}
216228
}
217-
let block_data = source.get_block(&block_hash).await
218-
.map_err(|_| UtxoLookupError::UnknownTx)?;
229+
let block_data =
230+
source.get_block(&block_hash).await.map_err(|_| UtxoLookupError::UnknownTx)?;
219231
let block = match block_data {
220232
BlockData::HeaderOnly(_) => return Err(UtxoLookupError::UnknownTx),
221233
BlockData::FullBlock(block) => block,
@@ -237,7 +249,7 @@ impl<S: FutureSpawner,
237249
}
238250
}
239251
break 'tx_found;
240-
};
252+
}
241253
let outpoint_unspent =
242254
source.is_output_unspent(outpoint).await.map_err(|_| UtxoLookupError::UnknownTx)?;
243255
if outpoint_unspent {
@@ -248,22 +260,21 @@ impl<S: FutureSpawner,
248260
}
249261
}
250262

251-
impl<S: FutureSpawner,
252-
Blocks: Deref + Send + Sync + Clone,
253-
L: Deref + Send + Sync,
254-
> Deref for GossipVerifier<S, Blocks, L> where
263+
impl<S: FutureSpawner, Blocks: Deref + Send + Sync + Clone, L: Deref + Send + Sync> Deref
264+
for GossipVerifier<S, Blocks, L>
265+
where
255266
Blocks::Target: UtxoSource,
256267
L::Target: Logger,
257268
{
258269
type Target = Self;
259-
fn deref(&self) -> &Self { self }
270+
fn deref(&self) -> &Self {
271+
self
272+
}
260273
}
261274

262-
263-
impl<S: FutureSpawner,
264-
Blocks: Deref + Send + Sync + Clone,
265-
L: Deref + Send + Sync,
266-
> UtxoLookup for GossipVerifier<S, Blocks, L> where
275+
impl<S: FutureSpawner, Blocks: Deref + Send + Sync + Clone, L: Deref + Send + Sync> UtxoLookup
276+
for GossipVerifier<S, Blocks, L>
277+
where
267278
Blocks::Target: UtxoSource,
268279
L::Target: Logger,
269280
{

0 commit comments

Comments
 (0)