@@ -6,24 +6,24 @@ use crate::{AsyncBlockSourceResult, BlockData, BlockSource, BlockSourceError};
6
6
7
7
use bitcoin:: blockdata:: block:: Block ;
8
8
use bitcoin:: blockdata:: constants:: ChainHash ;
9
- use bitcoin:: blockdata:: transaction:: { TxOut , OutPoint } ;
9
+ use bitcoin:: blockdata:: transaction:: { OutPoint , TxOut } ;
10
10
use bitcoin:: hash_types:: BlockHash ;
11
11
12
12
use lightning:: sign:: NodeSigner ;
13
13
14
- use lightning:: ln:: peer_handler:: { CustomMessageHandler , PeerManager , SocketDescriptor } ;
15
14
use lightning:: ln:: msgs:: { ChannelMessageHandler , OnionMessageHandler } ;
15
+ use lightning:: ln:: peer_handler:: { CustomMessageHandler , PeerManager , SocketDescriptor } ;
16
16
17
17
use lightning:: routing:: gossip:: { NetworkGraph , P2PGossipSync } ;
18
- use lightning:: routing:: utxo:: { UtxoFuture , UtxoLookup , UtxoResult , UtxoLookupError } ;
18
+ use lightning:: routing:: utxo:: { UtxoFuture , UtxoLookup , UtxoLookupError , UtxoResult } ;
19
19
20
20
use lightning:: util:: logger:: Logger ;
21
21
22
- use std:: sync:: { Arc , Mutex } ;
23
22
use std:: collections:: VecDeque ;
24
23
use std:: future:: Future ;
25
24
use std:: ops:: Deref ;
26
25
use std:: pin:: Pin ;
26
+ use std:: sync:: { Arc , Mutex } ;
27
27
use std:: task:: Poll ;
28
28
29
29
/// 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;
32
32
/// Note that while this is implementable for a [`BlockSource`] which returns filtered block data
33
33
/// (i.e. [`BlockData::HeaderOnly`] for [`BlockSource::get_block`] requests), such an
34
34
/// 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 {
36
36
/// Fetches the block hash of the block at the given height.
37
37
///
38
38
/// This will, in turn, be passed to to [`BlockSource::get_block`] to fetch the block needed
39
39
/// 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 > ;
41
43
42
44
/// Returns true if the given output has *not* been spent, i.e. is a member of the current UTXO
43
45
/// set.
@@ -48,7 +50,7 @@ pub trait UtxoSource : BlockSource + 'static {
48
50
///
49
51
/// If the `tokio` feature is enabled, this is implemented on `TokioSpawner` struct which
50
52
/// delegates to `tokio::spawn()`.
51
- pub trait FutureSpawner : Send + Sync + ' static {
53
+ pub trait FutureSpawner : Send + Sync + ' static {
52
54
/// Spawns the given future as a background task.
53
55
///
54
56
/// This method MUST NOT block on the given future immediately.
@@ -68,8 +70,8 @@ impl FutureSpawner for TokioSpawner {
68
70
/// A trivial future which joins two other futures and polls them at the same time, returning only
69
71
/// once both complete.
70
72
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 ,
73
75
> {
74
76
pub a : A ,
75
77
pub b : B ,
@@ -78,16 +80,20 @@ pub(crate) struct Joiner<
78
80
}
79
81
80
82
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
+ }
85
90
}
86
91
87
92
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
+ {
91
97
type Output = Result < ( ( BlockHash , Option < u32 > ) , BlockHash ) , BlockSourceError > ;
92
98
fn poll ( mut self : Pin < & mut Self > , ctx : & mut core:: task:: Context < ' _ > ) -> Poll < Self :: Output > {
93
99
if self . a_res . is_none ( ) {
@@ -110,14 +116,13 @@ impl<
110
116
} else {
111
117
return Poll :: Ready ( Err ( res. unwrap_err ( ) ) ) ;
112
118
}
113
-
114
119
} ,
115
120
Poll :: Pending => { } ,
116
121
}
117
122
}
118
123
if let Some ( b_res) = self . b_res {
119
124
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) ) ) ;
121
126
}
122
127
}
123
128
Poll :: Pending
@@ -132,7 +137,8 @@ impl<
132
137
/// value of 1024 should more than suffice), and ensure you have sufficient file descriptors
133
138
/// available on both Bitcoin Core and your LDK application for each request to hold its own
134
139
/// connection.
135
- pub struct GossipVerifier < S : FutureSpawner ,
140
+ pub struct GossipVerifier <
141
+ S : FutureSpawner ,
136
142
Blocks : Deref + Send + Sync + ' static + Clone ,
137
143
L : Deref + Send + Sync + ' static ,
138
144
Descriptor : SocketDescriptor + Send + Sync + ' static ,
@@ -149,23 +155,35 @@ pub struct GossipVerifier<S: FutureSpawner,
149
155
NS :: Target : NodeSigner ,
150
156
{
151
157
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
+ > ,
153
169
gossiper : Arc < P2PGossipSync < Arc < NetworkGraph < L > > , Self , L > > ,
154
170
spawn : S ,
155
171
block_cache : Arc < Mutex < VecDeque < ( u32 , Block ) > > > ,
156
172
}
157
173
158
174
const BLOCK_CACHE_SIZE : usize = 5 ;
159
175
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
169
187
Blocks :: Target : UtxoSource ,
170
188
L :: Target : Logger ,
171
189
CM :: Target : ChannelMessageHandler ,
@@ -177,25 +195,42 @@ impl<S: FutureSpawner,
177
195
///
178
196
/// This is expected to be given to a [`P2PGossipSync`] (initially constructed with `None` for
179
197
/// 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 {
181
212
Self {
182
- source, spawn, gossiper, peer_manager,
213
+ source,
214
+ spawn,
215
+ gossiper,
216
+ peer_manager,
183
217
block_cache : Arc :: new ( Mutex :: new ( VecDeque :: with_capacity ( BLOCK_CACHE_SIZE ) ) ) ,
184
218
}
185
219
}
186
220
187
221
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 ,
189
223
) -> Result < TxOut , UtxoLookupError > {
190
224
let block_height = ( short_channel_id >> 5 * 8 ) as u32 ; // block height is most significant three bytes
191
225
let transaction_index = ( ( short_channel_id >> 2 * 8 ) & 0xffffff ) as u32 ;
192
226
let output_index = ( short_channel_id & 0xffff ) as u16 ;
193
227
194
228
let ( outpoint, output) ;
195
229
196
- ' tx_found: loop { // Used as a simple goto
230
+ ' tx_found: loop {
231
+ // Used as a simple goto
197
232
macro_rules! process_block {
198
- ( $block: expr) => { {
233
+ ( $block: expr) => { {
199
234
if transaction_index as usize >= $block. txdata. len( ) {
200
235
return Err ( UtxoLookupError :: UnknownTx ) ;
201
236
}
@@ -206,7 +241,7 @@ impl<S: FutureSpawner,
206
241
207
242
outpoint = OutPoint :: new( transaction. txid( ) , output_index. into( ) ) ;
208
243
output = transaction. output[ output_index as usize ] . clone( ) ;
209
- } }
244
+ } } ;
210
245
}
211
246
{
212
247
let recent_blocks = block_cache. lock ( ) . unwrap ( ) ;
@@ -220,8 +255,8 @@ impl<S: FutureSpawner,
220
255
221
256
let ( ( _, tip_height_opt) , block_hash) =
222
257
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 ) ?;
225
260
if let Some ( tip_height) = tip_height_opt {
226
261
// If the block doesn't yet have five confirmations, error out.
227
262
//
@@ -232,8 +267,8 @@ impl<S: FutureSpawner,
232
267
return Err ( UtxoLookupError :: UnknownTx ) ;
233
268
}
234
269
}
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 ) ?;
237
272
let block = match block_data {
238
273
BlockData :: HeaderOnly ( _) => return Err ( UtxoLookupError :: UnknownTx ) ,
239
274
BlockData :: FullBlock ( block) => block,
@@ -255,7 +290,7 @@ impl<S: FutureSpawner,
255
290
}
256
291
}
257
292
break ' tx_found;
258
- } ;
293
+ }
259
294
let outpoint_unspent =
260
295
source. is_output_unspent ( outpoint) . await . map_err ( |_| UtxoLookupError :: UnknownTx ) ?;
261
296
if outpoint_unspent {
@@ -266,15 +301,17 @@ impl<S: FutureSpawner,
266
301
}
267
302
}
268
303
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
278
315
Blocks :: Target : UtxoSource ,
279
316
L :: Target : Logger ,
280
317
CM :: Target : ChannelMessageHandler ,
@@ -283,19 +320,22 @@ impl<S: FutureSpawner,
283
320
NS :: Target : NodeSigner ,
284
321
{
285
322
type Target = Self ;
286
- fn deref ( & self ) -> & Self { self }
323
+ fn deref ( & self ) -> & Self {
324
+ self
325
+ }
287
326
}
288
327
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
299
339
Blocks :: Target : UtxoSource ,
300
340
L :: Target : Logger ,
301
341
CM :: Target : ChannelMessageHandler ,
0 commit comments