Skip to content

Commit 2ceb510

Browse files
Drop Deref indirection for UtxoSource
Reduces generics and verbosity across the codebase, should provide equivalent behavior. Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent f64da5c commit 2ceb510

File tree

4 files changed

+48
-34
lines changed

4 files changed

+48
-34
lines changed

lightning-block-sync/src/gossip.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ pub trait UtxoSource: BlockSource + 'static {
4242
) -> impl Future<Output = BlockSourceResult<bool>> + Send + 'a;
4343
}
4444

45+
impl<T: UtxoSource + ?Sized, U: Deref<Target = T> + Send + Sync + 'static> UtxoSource for U {
46+
fn get_block_hash_by_height<'a>(
47+
&'a self, block_height: u32,
48+
) -> impl Future<Output = BlockSourceResult<BlockHash>> + Send + 'a {
49+
self.deref().get_block_hash_by_height(block_height)
50+
}
51+
52+
fn is_output_unspent<'a>(
53+
&'a self, outpoint: OutPoint,
54+
) -> impl Future<Output = BlockSourceResult<bool>> + Send + 'a {
55+
self.deref().is_output_unspent(outpoint)
56+
}
57+
}
58+
4559
#[cfg(feature = "tokio")]
4660
/// A trivial [`FutureSpawner`] which delegates to `tokio::spawn`.
4761
pub struct TokioSpawner;
@@ -129,21 +143,15 @@ impl<
129143
/// value of 1024 should more than suffice), and ensure you have sufficient file descriptors
130144
/// available on both Bitcoin Core and your LDK application for each request to hold its own
131145
/// connection.
132-
pub struct GossipVerifier<S: FutureSpawner, Blocks: Deref + Send + Sync + 'static + Clone>
133-
where
134-
Blocks::Target: UtxoSource,
135-
{
146+
pub struct GossipVerifier<S: FutureSpawner, Blocks: UtxoSource + Clone> {
136147
source: Blocks,
137148
spawn: S,
138149
block_cache: Arc<Mutex<VecDeque<(u32, Block)>>>,
139150
}
140151

141152
const BLOCK_CACHE_SIZE: usize = 5;
142153

143-
impl<S: FutureSpawner, Blocks: Deref + Send + Sync + Clone> GossipVerifier<S, Blocks>
144-
where
145-
Blocks::Target: UtxoSource,
146-
{
154+
impl<S: FutureSpawner, Blocks: UtxoSource + Clone> GossipVerifier<S, Blocks> {
147155
/// Constructs a new [`GossipVerifier`] for use in a [`P2PGossipSync`].
148156
///
149157
/// [`P2PGossipSync`]: lightning::routing::gossip::P2PGossipSync
@@ -239,10 +247,7 @@ where
239247
}
240248
}
241249

242-
impl<S: FutureSpawner, Blocks: Deref + Send + Sync + Clone> UtxoLookup for GossipVerifier<S, Blocks>
243-
where
244-
Blocks::Target: UtxoSource,
245-
{
250+
impl<S: FutureSpawner, Blocks: UtxoSource + Clone> UtxoLookup for GossipVerifier<S, Blocks> {
246251
fn get_utxo(&self, _chain_hash: &ChainHash, scid: u64, notifier: Arc<Notifier>) -> UtxoResult {
247252
let res = UtxoFuture::new(notifier);
248253
let fut = res.clone();

lightning-block-sync/src/init.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,15 @@ use bitcoin::network::Network;
1111
use lightning::chain;
1212
use lightning::chain::BestBlock;
1313

14-
use std::ops::Deref;
15-
1614
/// Returns a validated block header of the source's best chain tip.
1715
///
1816
/// Upon success, the returned header can be used to initialize [`SpvClient`]. Useful during a fresh
1917
/// start when there are no chain listeners to sync yet.
2018
///
2119
/// [`SpvClient`]: crate::SpvClient
22-
pub async fn validate_best_block_header<B: Deref>(
23-
block_source: B,
24-
) -> BlockSourceResult<ValidatedBlockHeader>
25-
where
26-
B::Target: BlockSource,
27-
{
20+
pub async fn validate_best_block_header<B: BlockSource + ?Sized>(
21+
block_source: &B,
22+
) -> BlockSourceResult<ValidatedBlockHeader> {
2823
let (best_block_hash, best_block_height) = block_source.get_best_block().await?;
2924
block_source.get_header(&best_block_hash, best_block_height).await?.validate(best_block_hash)
3025
}
@@ -64,7 +59,7 @@ where
6459
/// use lightning::io::Cursor;
6560
///
6661
/// async fn init_sync<
67-
/// B: BlockSource,
62+
/// B: BlockSource + 'static,
6863
/// ES: EntropySource,
6964
/// NS: NodeSigner,
7065
/// SP: SignerProvider,
@@ -139,17 +134,14 @@ where
139134
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
140135
/// [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor
141136
pub async fn synchronize_listeners<
142-
B: Deref + Sized + Send + Sync,
137+
B: BlockSource + 'static,
143138
C: Cache,
144139
L: chain::Listen + ?Sized,
145140
>(
146-
block_source: B, network: Network, header_cache: &mut C,
141+
block_source: &B, network: Network, header_cache: &mut C,
147142
mut chain_listeners: Vec<(BlockHash, &L)>,
148-
) -> BlockSourceResult<ValidatedBlockHeader>
149-
where
150-
B::Target: BlockSource,
151-
{
152-
let best_header = validate_best_block_header(&*block_source).await?;
143+
) -> BlockSourceResult<ValidatedBlockHeader> {
144+
let best_header = validate_best_block_header(block_source).await?;
153145

154146
// Fetch the header for the block hash paired with each listener.
155147
let mut chain_listeners_with_old_headers = Vec::new();

lightning-block-sync/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,26 @@ pub trait BlockSource: Sync + Send {
8383
) -> impl Future<Output = BlockSourceResult<(BlockHash, Option<u32>)>> + Send + 'a;
8484
}
8585

86+
impl<T: BlockSource + ?Sized + 'static, B: Deref<Target = T> + Sync + Send> BlockSource for B {
87+
fn get_header<'a>(
88+
&'a self, header_hash: &'a BlockHash, height_hint: Option<u32>,
89+
) -> impl Future<Output = BlockSourceResult<BlockHeaderData>> + Send + 'a {
90+
self.deref().get_header(header_hash, height_hint)
91+
}
92+
93+
fn get_block<'a>(
94+
&'a self, header_hash: &'a BlockHash,
95+
) -> impl Future<Output = BlockSourceResult<BlockData>> + Send + 'a {
96+
self.deref().get_block(header_hash)
97+
}
98+
99+
fn get_best_block<'a>(
100+
&'a self,
101+
) -> impl Future<Output = BlockSourceResult<(BlockHash, Option<u32>)>> + Send + 'a {
102+
self.deref().get_best_block()
103+
}
104+
}
105+
86106
/// Result type for `BlockSource` requests.
87107
pub type BlockSourceResult<T> = Result<T, BlockSourceError>;
88108

lightning-block-sync/src/poll.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use bitcoin::network::Network;
77
use lightning::chain::BestBlock;
88

99
use std::future::Future;
10-
use std::ops::Deref;
1110

1211
/// The `Poll` trait defines behavior for polling block sources for a chain tip and retrieving
1312
/// related chain data. It serves as an adapter for `BlockSource`.
@@ -195,12 +194,12 @@ mod sealed {
195194
///
196195
/// Other `Poll` implementations should be built using `ChainPoller` as it provides the simplest way
197196
/// of validating chain data and checking consistency.
198-
pub struct ChainPoller<B: Deref<Target = T> + Sized + Send + Sync, T: BlockSource + ?Sized> {
197+
pub struct ChainPoller<B: BlockSource> {
199198
block_source: B,
200199
network: Network,
201200
}
202201

203-
impl<B: Deref<Target = T> + Sized + Send + Sync, T: BlockSource + ?Sized> ChainPoller<B, T> {
202+
impl<B: BlockSource> ChainPoller<B> {
204203
/// Creates a new poller for the given block source.
205204
///
206205
/// If the `network` parameter is mainnet, then the difficulty between blocks is checked for
@@ -210,9 +209,7 @@ impl<B: Deref<Target = T> + Sized + Send + Sync, T: BlockSource + ?Sized> ChainP
210209
}
211210
}
212211

213-
impl<B: Deref<Target = T> + Sized + Send + Sync, T: BlockSource + ?Sized> Poll
214-
for ChainPoller<B, T>
215-
{
212+
impl<B: BlockSource> Poll for ChainPoller<B> {
216213
fn poll_chain_tip<'a>(
217214
&'a self, best_known_chain_tip: ValidatedBlockHeader,
218215
) -> impl Future<Output = BlockSourceResult<ChainTip>> + Send + 'a {

0 commit comments

Comments
 (0)