Skip to content

Commit 1bbfe12

Browse files
committed
Update SpvClient to not use Deref
1 parent da79fb0 commit 1bbfe12

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

lightning-block-sync/src/init.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ use lightning::chain;
9292
/// chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
9393
///
9494
/// let chain_poller = poll::ChainPoller::new(block_source, Network::Bitcoin);
95-
/// let mut chain_listener = (chain_monitor, &manager);
96-
/// let spv_client = SpvClient::new(chain_tip, chain_poller, &mut cache, &chain_listener);
95+
/// //let mut chain_listener = (chain_monitor, &manager);
96+
/// let chain_listener = std::rc::Rc::new(manager);
97+
/// let spv_client = SpvClient::new(chain_tip, chain_poller, &mut cache, chain_listener);
9798
/// }
9899
/// ```
99100
///

lightning-block-sync/src/lib.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ pub struct BlockHeaderData {
159159
/// custom cache eviction policy. This offers flexibility to those sensitive to resource usage.
160160
/// Hence, there is a trade-off between a lower memory footprint and potentially increased network
161161
/// I/O as headers are re-fetched during fork detection.
162-
pub struct SpvClient<'a, P: Poll, C: Cache, L: Deref>
163-
where L::Target: chain::Listen {
162+
pub struct SpvClient<'a, P: Poll, C: Cache, L: chain::Listen> {
164163
chain_tip: ValidatedBlockHeader,
165164
chain_poller: P,
166165
chain_notifier: ChainNotifier<'a, C, L>,
@@ -208,7 +207,7 @@ impl Cache for UnboundedCache {
208207
}
209208
}
210209

211-
impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L> where L::Target: chain::Listen {
210+
impl<'a, P: Poll, C: Cache, L: chain::Listen> SpvClient<'a, P, C, L> {
212211
/// Creates a new SPV client using `chain_tip` as the best known chain tip.
213212
///
214213
/// Subsequent calls to [`poll_best_tip`] will poll for the best chain tip using the given chain
@@ -272,7 +271,7 @@ impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L> where L::Target: ch
272271
/// Notifies [listeners] of blocks that have been connected or disconnected from the chain.
273272
///
274273
/// [listeners]: ../../lightning/chain/trait.Listen.html
275-
pub struct ChainNotifier<'a, C: Cache, L: Deref> where L::Target: chain::Listen {
274+
pub struct ChainNotifier<'a, C: Cache, L: chain::Listen> {
276275
/// Cache for looking up headers before fetching from a block source.
277276
header_cache: &'a mut C,
278277

@@ -298,7 +297,7 @@ struct ChainDifference {
298297
connected_blocks: Vec<ValidatedBlockHeader>,
299298
}
300299

301-
impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L> where L::Target: chain::Listen {
300+
impl<'a, C: Cache, L: chain::Listen> ChainNotifier<'a, C, L> {
302301
/// Finds the first common ancestor between `new_header` and `old_header`, disconnecting blocks
303302
/// from `old_header` to get to that point and then connecting blocks until `new_header`.
304303
///
@@ -420,7 +419,7 @@ mod spv_client_tests {
420419
let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
421420
let mut cache = UnboundedCache::new();
422421
let mut listener = NullChainListener {};
423-
let mut client = SpvClient::new(best_tip, poller, &mut cache, &mut listener);
422+
let mut client = SpvClient::new(best_tip, poller, &mut cache, &listener);
424423
match client.poll_best_tip().await {
425424
Err(e) => {
426425
assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
@@ -439,7 +438,7 @@ mod spv_client_tests {
439438
let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
440439
let mut cache = UnboundedCache::new();
441440
let mut listener = NullChainListener {};
442-
let mut client = SpvClient::new(common_tip, poller, &mut cache, &mut listener);
441+
let mut client = SpvClient::new(common_tip, poller, &mut cache, &listener);
443442
match client.poll_best_tip().await {
444443
Err(e) => panic!("Unexpected error: {:?}", e),
445444
Ok((chain_tip, blocks_connected)) => {
@@ -459,7 +458,7 @@ mod spv_client_tests {
459458
let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
460459
let mut cache = UnboundedCache::new();
461460
let mut listener = NullChainListener {};
462-
let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener);
461+
let mut client = SpvClient::new(old_tip, poller, &mut cache, &listener);
463462
match client.poll_best_tip().await {
464463
Err(e) => panic!("Unexpected error: {:?}", e),
465464
Ok((chain_tip, blocks_connected)) => {
@@ -479,7 +478,7 @@ mod spv_client_tests {
479478
let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
480479
let mut cache = UnboundedCache::new();
481480
let mut listener = NullChainListener {};
482-
let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener);
481+
let mut client = SpvClient::new(old_tip, poller, &mut cache, &listener);
483482
match client.poll_best_tip().await {
484483
Err(e) => panic!("Unexpected error: {:?}", e),
485484
Ok((chain_tip, blocks_connected)) => {
@@ -499,7 +498,7 @@ mod spv_client_tests {
499498
let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
500499
let mut cache = UnboundedCache::new();
501500
let mut listener = NullChainListener {};
502-
let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener);
501+
let mut client = SpvClient::new(old_tip, poller, &mut cache, &listener);
503502
match client.poll_best_tip().await {
504503
Err(e) => panic!("Unexpected error: {:?}", e),
505504
Ok((chain_tip, blocks_connected)) => {
@@ -520,7 +519,7 @@ mod spv_client_tests {
520519
let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
521520
let mut cache = UnboundedCache::new();
522521
let mut listener = NullChainListener {};
523-
let mut client = SpvClient::new(best_tip, poller, &mut cache, &mut listener);
522+
let mut client = SpvClient::new(best_tip, poller, &mut cache, &listener);
524523
match client.poll_best_tip().await {
525524
Err(e) => panic!("Unexpected error: {:?}", e),
526525
Ok((chain_tip, blocks_connected)) => {

lightning/src/chain/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ pub trait Filter: Send + Sync {
137137
fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script);
138138
}
139139

140+
impl<T: Listen> Listen for &T {
141+
fn block_connected(&self, block: &Block, height: u32) {
142+
(**self).block_connected(block, height);
143+
}
144+
145+
fn block_disconnected(&self, header: &BlockHeader, height: u32) {
146+
(**self).block_disconnected(header, height);
147+
}
148+
}
149+
140150
impl<T: Listen> Listen for std::ops::Deref<Target = T> {
141151
fn block_connected(&self, block: &Block, height: u32) {
142152
(**self).block_connected(block, height);

0 commit comments

Comments
 (0)