Skip to content

Commit 5271c89

Browse files
committed
PREVIEW: Reformat project
This commit is just included to give a preview for what the reformatted code will look like.
1 parent 7107944 commit 5271c89

File tree

118 files changed

+55924
-22583
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+55924
-22583
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 798 additions & 288 deletions
Large diffs are not rendered by default.

lightning-block-sync/src/convert.rs

Lines changed: 181 additions & 116 deletions
Large diffs are not rendered by default.

lightning-block-sync/src/gossip.rs

Lines changed: 100 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ 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::sign::NodeSigner;
1313

14-
use lightning::ln::peer_handler::{CustomMessageHandler, PeerManager, SocketDescriptor};
1514
use lightning::ln::msgs::{ChannelMessageHandler, OnionMessageHandler};
15+
use lightning::ln::peer_handler::{CustomMessageHandler, PeerManager, SocketDescriptor};
1616

1717
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
18-
use lightning::routing::utxo::{UtxoFuture, UtxoLookup, UtxoResult, UtxoLookupError};
18+
use lightning::routing::utxo::{UtxoFuture, UtxoLookup, UtxoLookupError, UtxoResult};
1919

2020
use lightning::util::logger::Logger;
2121

22-
use std::sync::{Arc, Mutex};
2322
use std::collections::VecDeque;
2423
use std::future::Future;
2524
use std::ops::Deref;
2625
use std::pin::Pin;
26+
use std::sync::{Arc, Mutex};
2727
use std::task::Poll;
2828

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

4244
/// Returns true if the given output has *not* been spent, i.e. is a member of the current UTXO
4345
/// set.
@@ -48,7 +50,7 @@ pub trait UtxoSource : BlockSource + 'static {
4850
///
4951
/// If the `tokio` feature is enabled, this is implemented on `TokioSpawner` struct which
5052
/// delegates to `tokio::spawn()`.
51-
pub trait FutureSpawner : Send + Sync + 'static {
53+
pub trait FutureSpawner: Send + Sync + 'static {
5254
/// Spawns the given future as a background task.
5355
///
5456
/// This method MUST NOT block on the given future immediately.
@@ -68,8 +70,8 @@ impl FutureSpawner for TokioSpawner {
6870
/// A trivial future which joins two other futures and polls them at the same time, returning only
6971
/// once both complete.
7072
pub(crate) struct Joiner<
71-
A: Future<Output=Result<(BlockHash, Option<u32>), BlockSourceError>> + Unpin,
72-
B: Future<Output=Result<BlockHash, BlockSourceError>> + Unpin,
73+
A: Future<Output = Result<(BlockHash, Option<u32>), BlockSourceError>> + Unpin,
74+
B: Future<Output = Result<BlockHash, BlockSourceError>> + Unpin,
7375
> {
7476
pub a: A,
7577
pub b: B,
@@ -78,16 +80,20 @@ pub(crate) struct Joiner<
7880
}
7981

8082
impl<
81-
A: Future<Output=Result<(BlockHash, Option<u32>), BlockSourceError>> + Unpin,
82-
B: Future<Output=Result<BlockHash, BlockSourceError>> + Unpin,
83-
> Joiner<A, B> {
84-
fn new(a: A, b: B) -> Self { Self { a, b, a_res: None, b_res: None } }
83+
A: Future<Output = Result<(BlockHash, Option<u32>), BlockSourceError>> + Unpin,
84+
B: Future<Output = Result<BlockHash, BlockSourceError>> + Unpin,
85+
> Joiner<A, B>
86+
{
87+
fn new(a: A, b: B) -> Self {
88+
Self { a, b, a_res: None, b_res: None }
89+
}
8590
}
8691

8792
impl<
88-
A: Future<Output=Result<(BlockHash, Option<u32>), BlockSourceError>> + Unpin,
89-
B: Future<Output=Result<BlockHash, BlockSourceError>> + Unpin,
90-
> Future for Joiner<A, B> {
93+
A: Future<Output = Result<(BlockHash, Option<u32>), BlockSourceError>> + Unpin,
94+
B: Future<Output = Result<BlockHash, BlockSourceError>> + Unpin,
95+
> Future for Joiner<A, B>
96+
{
9197
type Output = Result<((BlockHash, Option<u32>), BlockHash), BlockSourceError>;
9298
fn poll(mut self: Pin<&mut Self>, ctx: &mut core::task::Context<'_>) -> Poll<Self::Output> {
9399
if self.a_res.is_none() {
@@ -110,14 +116,13 @@ impl<
110116
} else {
111117
return Poll::Ready(Err(res.unwrap_err()));
112118
}
113-
114119
},
115120
Poll::Pending => {},
116121
}
117122
}
118123
if let Some(b_res) = self.b_res {
119124
if let Some(a_res) = self.a_res {
120-
return Poll::Ready(Ok((a_res, b_res)))
125+
return Poll::Ready(Ok((a_res, b_res)));
121126
}
122127
}
123128
Poll::Pending
@@ -132,7 +137,8 @@ impl<
132137
/// value of 1024 should more than suffice), and ensure you have sufficient file descriptors
133138
/// available on both Bitcoin Core and your LDK application for each request to hold its own
134139
/// connection.
135-
pub struct GossipVerifier<S: FutureSpawner,
140+
pub struct GossipVerifier<
141+
S: FutureSpawner,
136142
Blocks: Deref + Send + Sync + 'static + Clone,
137143
L: Deref + Send + Sync + 'static,
138144
Descriptor: SocketDescriptor + Send + Sync + 'static,
@@ -149,23 +155,35 @@ pub struct GossipVerifier<S: FutureSpawner,
149155
NS::Target: NodeSigner,
150156
{
151157
source: Blocks,
152-
peer_manager: Arc<PeerManager<Descriptor, CM, Arc<P2PGossipSync<Arc<NetworkGraph<L>>, Self, L>>, OM, L, CMH, NS>>,
158+
peer_manager: Arc<
159+
PeerManager<
160+
Descriptor,
161+
CM,
162+
Arc<P2PGossipSync<Arc<NetworkGraph<L>>, Self, L>>,
163+
OM,
164+
L,
165+
CMH,
166+
NS,
167+
>,
168+
>,
153169
gossiper: Arc<P2PGossipSync<Arc<NetworkGraph<L>>, Self, L>>,
154170
spawn: S,
155171
block_cache: Arc<Mutex<VecDeque<(u32, Block)>>>,
156172
}
157173

158174
const BLOCK_CACHE_SIZE: usize = 5;
159175

160-
impl<S: FutureSpawner,
161-
Blocks: Deref + Send + Sync + Clone,
162-
L: Deref + Send + Sync,
163-
Descriptor: SocketDescriptor + Send + Sync,
164-
CM: Deref + Send + Sync,
165-
OM: Deref + Send + Sync,
166-
CMH: Deref + Send + Sync,
167-
NS: Deref + Send + Sync,
168-
> GossipVerifier<S, Blocks, L, Descriptor, CM, OM, CMH, NS> where
176+
impl<
177+
S: FutureSpawner,
178+
Blocks: Deref + Send + Sync + Clone,
179+
L: Deref + Send + Sync,
180+
Descriptor: SocketDescriptor + Send + Sync,
181+
CM: Deref + Send + Sync,
182+
OM: Deref + Send + Sync,
183+
CMH: Deref + Send + Sync,
184+
NS: Deref + Send + Sync,
185+
> GossipVerifier<S, Blocks, L, Descriptor, CM, OM, CMH, NS>
186+
where
169187
Blocks::Target: UtxoSource,
170188
L::Target: Logger,
171189
CM::Target: ChannelMessageHandler,
@@ -177,25 +195,42 @@ impl<S: FutureSpawner,
177195
///
178196
/// This is expected to be given to a [`P2PGossipSync`] (initially constructed with `None` for
179197
/// the UTXO lookup) via [`P2PGossipSync::add_utxo_lookup`].
180-
pub fn new(source: Blocks, spawn: S, gossiper: Arc<P2PGossipSync<Arc<NetworkGraph<L>>, Self, L>>, peer_manager: Arc<PeerManager<Descriptor, CM, Arc<P2PGossipSync<Arc<NetworkGraph<L>>, Self, L>>, OM, L, CMH, NS>>) -> Self {
198+
pub fn new(
199+
source: Blocks, spawn: S, gossiper: Arc<P2PGossipSync<Arc<NetworkGraph<L>>, Self, L>>,
200+
peer_manager: Arc<
201+
PeerManager<
202+
Descriptor,
203+
CM,
204+
Arc<P2PGossipSync<Arc<NetworkGraph<L>>, Self, L>>,
205+
OM,
206+
L,
207+
CMH,
208+
NS,
209+
>,
210+
>,
211+
) -> Self {
181212
Self {
182-
source, spawn, gossiper, peer_manager,
213+
source,
214+
spawn,
215+
gossiper,
216+
peer_manager,
183217
block_cache: Arc::new(Mutex::new(VecDeque::with_capacity(BLOCK_CACHE_SIZE))),
184218
}
185219
}
186220

187221
async fn retrieve_utxo(
188-
source: Blocks, block_cache: Arc<Mutex<VecDeque<(u32, Block)>>>, short_channel_id: u64
222+
source: Blocks, block_cache: Arc<Mutex<VecDeque<(u32, Block)>>>, short_channel_id: u64,
189223
) -> Result<TxOut, UtxoLookupError> {
190224
let block_height = (short_channel_id >> 5 * 8) as u32; // block height is most significant three bytes
191225
let transaction_index = ((short_channel_id >> 2 * 8) & 0xffffff) as u32;
192226
let output_index = (short_channel_id & 0xffff) as u16;
193227

194228
let (outpoint, output);
195229

196-
'tx_found: loop { // Used as a simple goto
230+
'tx_found: loop {
231+
// Used as a simple goto
197232
macro_rules! process_block {
198-
($block: expr) => { {
233+
($block: expr) => {{
199234
if transaction_index as usize >= $block.txdata.len() {
200235
return Err(UtxoLookupError::UnknownTx);
201236
}
@@ -206,7 +241,7 @@ impl<S: FutureSpawner,
206241

207242
outpoint = OutPoint::new(transaction.txid(), output_index.into());
208243
output = transaction.output[output_index as usize].clone();
209-
} }
244+
}};
210245
}
211246
{
212247
let recent_blocks = block_cache.lock().unwrap();
@@ -220,8 +255,8 @@ impl<S: FutureSpawner,
220255

221256
let ((_, tip_height_opt), block_hash) =
222257
Joiner::new(source.get_best_block(), source.get_block_hash_by_height(block_height))
223-
.await
224-
.map_err(|_| UtxoLookupError::UnknownTx)?;
258+
.await
259+
.map_err(|_| UtxoLookupError::UnknownTx)?;
225260
if let Some(tip_height) = tip_height_opt {
226261
// If the block doesn't yet have five confirmations, error out.
227262
//
@@ -232,8 +267,8 @@ impl<S: FutureSpawner,
232267
return Err(UtxoLookupError::UnknownTx);
233268
}
234269
}
235-
let block_data = source.get_block(&block_hash).await
236-
.map_err(|_| UtxoLookupError::UnknownTx)?;
270+
let block_data =
271+
source.get_block(&block_hash).await.map_err(|_| UtxoLookupError::UnknownTx)?;
237272
let block = match block_data {
238273
BlockData::HeaderOnly(_) => return Err(UtxoLookupError::UnknownTx),
239274
BlockData::FullBlock(block) => block,
@@ -255,7 +290,7 @@ impl<S: FutureSpawner,
255290
}
256291
}
257292
break 'tx_found;
258-
};
293+
}
259294
let outpoint_unspent =
260295
source.is_output_unspent(outpoint).await.map_err(|_| UtxoLookupError::UnknownTx)?;
261296
if outpoint_unspent {
@@ -266,15 +301,17 @@ impl<S: FutureSpawner,
266301
}
267302
}
268303

269-
impl<S: FutureSpawner,
270-
Blocks: Deref + Send + Sync + Clone,
271-
L: Deref + Send + Sync,
272-
Descriptor: SocketDescriptor + Send + Sync,
273-
CM: Deref + Send + Sync,
274-
OM: Deref + Send + Sync,
275-
CMH: Deref + Send + Sync,
276-
NS: Deref + Send + Sync,
277-
> Deref for GossipVerifier<S, Blocks, L, Descriptor, CM, OM, CMH, NS> where
304+
impl<
305+
S: FutureSpawner,
306+
Blocks: Deref + Send + Sync + Clone,
307+
L: Deref + Send + Sync,
308+
Descriptor: SocketDescriptor + Send + Sync,
309+
CM: Deref + Send + Sync,
310+
OM: Deref + Send + Sync,
311+
CMH: Deref + Send + Sync,
312+
NS: Deref + Send + Sync,
313+
> Deref for GossipVerifier<S, Blocks, L, Descriptor, CM, OM, CMH, NS>
314+
where
278315
Blocks::Target: UtxoSource,
279316
L::Target: Logger,
280317
CM::Target: ChannelMessageHandler,
@@ -283,19 +320,22 @@ impl<S: FutureSpawner,
283320
NS::Target: NodeSigner,
284321
{
285322
type Target = Self;
286-
fn deref(&self) -> &Self { self }
323+
fn deref(&self) -> &Self {
324+
self
325+
}
287326
}
288327

289-
290-
impl<S: FutureSpawner,
291-
Blocks: Deref + Send + Sync + Clone,
292-
L: Deref + Send + Sync,
293-
Descriptor: SocketDescriptor + Send + Sync,
294-
CM: Deref + Send + Sync,
295-
OM: Deref + Send + Sync,
296-
CMH: Deref + Send + Sync,
297-
NS: Deref + Send + Sync,
298-
> UtxoLookup for GossipVerifier<S, Blocks, L, Descriptor, CM, OM, CMH, NS> where
328+
impl<
329+
S: FutureSpawner,
330+
Blocks: Deref + Send + Sync + Clone,
331+
L: Deref + Send + Sync,
332+
Descriptor: SocketDescriptor + Send + Sync,
333+
CM: Deref + Send + Sync,
334+
OM: Deref + Send + Sync,
335+
CMH: Deref + Send + Sync,
336+
NS: Deref + Send + Sync,
337+
> UtxoLookup for GossipVerifier<S, Blocks, L, Descriptor, CM, OM, CMH, NS>
338+
where
299339
Blocks::Target: UtxoSource,
300340
L::Target: Logger,
301341
CM::Target: ChannelMessageHandler,

0 commit comments

Comments
 (0)