Skip to content

Commit 45c7439

Browse files
committed
PREVIEW
1 parent 74997cb commit 45c7439

File tree

125 files changed

+61070
-25213
lines changed

Some content is hidden

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

125 files changed

+61070
-25213
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 775 additions & 276 deletions
Large diffs are not rendered by default.

lightning-block-sync/src/convert.rs

Lines changed: 194 additions & 118 deletions
Large diffs are not rendered by default.

lightning-block-sync/src/gossip.rs

Lines changed: 56 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,35 @@ 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 {
191+
// Used as a simple goto
179192
macro_rules! process_block {
180-
($block: expr) => { {
193+
($block: expr) => {{
181194
if transaction_index as usize >= $block.txdata.len() {
182195
return Err(UtxoLookupError::UnknownTx);
183196
}
@@ -188,7 +201,7 @@ impl<S: FutureSpawner,
188201

189202
outpoint = OutPoint::new(transaction.txid(), output_index.into());
190203
output = transaction.output[output_index as usize].clone();
191-
} }
204+
}};
192205
}
193206
{
194207
let recent_blocks = block_cache.lock().unwrap();
@@ -202,8 +215,8 @@ impl<S: FutureSpawner,
202215

203216
let ((_, tip_height_opt), block_hash) =
204217
Joiner::new(source.get_best_block(), source.get_block_hash_by_height(block_height))
205-
.await
206-
.map_err(|_| UtxoLookupError::UnknownTx)?;
218+
.await
219+
.map_err(|_| UtxoLookupError::UnknownTx)?;
207220
if let Some(tip_height) = tip_height_opt {
208221
// If the block doesn't yet have five confirmations, error out.
209222
//
@@ -214,8 +227,8 @@ impl<S: FutureSpawner,
214227
return Err(UtxoLookupError::UnknownTx);
215228
}
216229
}
217-
let block_data = source.get_block(&block_hash).await
218-
.map_err(|_| UtxoLookupError::UnknownTx)?;
230+
let block_data =
231+
source.get_block(&block_hash).await.map_err(|_| UtxoLookupError::UnknownTx)?;
219232
let block = match block_data {
220233
BlockData::HeaderOnly(_) => return Err(UtxoLookupError::UnknownTx),
221234
BlockData::FullBlock(block) => block,
@@ -237,7 +250,7 @@ impl<S: FutureSpawner,
237250
}
238251
}
239252
break 'tx_found;
240-
};
253+
}
241254
let outpoint_unspent =
242255
source.is_output_unspent(outpoint).await.map_err(|_| UtxoLookupError::UnknownTx)?;
243256
if outpoint_unspent {
@@ -248,22 +261,21 @@ impl<S: FutureSpawner,
248261
}
249262
}
250263

251-
impl<S: FutureSpawner,
252-
Blocks: Deref + Send + Sync + Clone,
253-
L: Deref + Send + Sync,
254-
> Deref for GossipVerifier<S, Blocks, L> where
264+
impl<S: FutureSpawner, Blocks: Deref + Send + Sync + Clone, L: Deref + Send + Sync> Deref
265+
for GossipVerifier<S, Blocks, L>
266+
where
255267
Blocks::Target: UtxoSource,
256268
L::Target: Logger,
257269
{
258270
type Target = Self;
259-
fn deref(&self) -> &Self { self }
271+
fn deref(&self) -> &Self {
272+
self
273+
}
260274
}
261275

262-
263-
impl<S: FutureSpawner,
264-
Blocks: Deref + Send + Sync + Clone,
265-
L: Deref + Send + Sync,
266-
> UtxoLookup for GossipVerifier<S, Blocks, L> where
276+
impl<S: FutureSpawner, Blocks: Deref + Send + Sync + Clone, L: Deref + Send + Sync> UtxoLookup
277+
for GossipVerifier<S, Blocks, L>
278+
where
267279
Blocks::Target: UtxoSource,
268280
L::Target: Logger,
269281
{

0 commit comments

Comments
 (0)