Skip to content

Commit 2f90be2

Browse files
committed
Avoid dyn indirection in gossip validation
In the next commit we'll update `rust-lightning` with the `Pin<Box<dyn ...>>` pattern dropped in favor of native async traits (with `impl Future...`). In anticipation of those traits becoming not-object-safe here we drop the dyn indirection that exists in our gossip validation.
1 parent f81dc65 commit 2f90be2

File tree

4 files changed

+71
-14
lines changed

4 files changed

+71
-14
lines changed

src/chain/bitcoind.rs

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
1212

1313
use base64::prelude::BASE64_STANDARD;
1414
use base64::Engine;
15-
use bitcoin::{BlockHash, FeeRate, Network, Transaction, Txid};
15+
use bitcoin::{BlockHash, FeeRate, Network, OutPoint, Transaction, Txid};
1616
use lightning::chain::chaininterface::ConfirmationTarget as LdkConfirmationTarget;
1717
use lightning::chain::{BestBlock, Listen};
1818
use lightning::util::ser::Writeable;
@@ -117,7 +117,7 @@ impl BitcoindChainSource {
117117
}
118118
}
119119

120-
pub(super) fn as_utxo_source(&self) -> Arc<dyn UtxoSource> {
120+
pub(super) fn as_utxo_source(&self) -> UtxoSourceClient {
121121
self.api_client.utxo_source()
122122
}
123123

@@ -639,6 +639,65 @@ impl BitcoindChainSource {
639639
}
640640
}
641641

642+
#[derive(Clone)]
643+
pub(crate) enum UtxoSourceClient {
644+
Rpc(Arc<RpcClient>),
645+
Rest(Arc<RestClient>),
646+
}
647+
648+
impl std::ops::Deref for UtxoSourceClient {
649+
type Target = Self;
650+
fn deref(&self) -> &Self {
651+
self
652+
}
653+
}
654+
655+
impl BlockSource for UtxoSourceClient {
656+
fn get_header<'a>(
657+
&'a self, header_hash: &'a BlockHash, height_hint: Option<u32>,
658+
) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
659+
match self {
660+
Self::Rpc(client) => client.get_header(header_hash, height_hint),
661+
Self::Rest(client) => client.get_header(header_hash, height_hint),
662+
}
663+
}
664+
665+
fn get_block<'a>(
666+
&'a self, header_hash: &'a BlockHash,
667+
) -> AsyncBlockSourceResult<'a, BlockData> {
668+
match self {
669+
Self::Rpc(client) => client.get_block(header_hash),
670+
Self::Rest(client) => client.get_block(header_hash),
671+
}
672+
}
673+
674+
fn get_best_block(&self) -> AsyncBlockSourceResult<'_, (BlockHash, Option<u32>)> {
675+
match self {
676+
Self::Rpc(client) => client.get_best_block(),
677+
Self::Rest(client) => client.get_best_block(),
678+
}
679+
}
680+
}
681+
682+
683+
impl UtxoSource for UtxoSourceClient {
684+
fn get_block_hash_by_height<'a>(
685+
&'a self, block_height: u32,
686+
) -> AsyncBlockSourceResult<'a, BlockHash> {
687+
match self {
688+
Self::Rpc(client) => client.get_block_hash_by_height(block_height),
689+
Self::Rest(client) => client.get_block_hash_by_height(block_height),
690+
}
691+
}
692+
693+
fn is_output_unspent<'a>(&'a self, outpoint: OutPoint) -> AsyncBlockSourceResult<'a, bool> {
694+
match self {
695+
Self::Rpc(client) => client.is_output_unspent(outpoint),
696+
Self::Rest(client) => client.is_output_unspent(outpoint),
697+
}
698+
}
699+
}
700+
642701
pub enum BitcoindClient {
643702
Rpc {
644703
rpc_client: Arc<RpcClient>,
@@ -700,12 +759,10 @@ impl BitcoindClient {
700759
}
701760
}
702761

703-
pub(crate) fn utxo_source(&self) -> Arc<dyn UtxoSource> {
762+
fn utxo_source(&self) -> UtxoSourceClient {
704763
match self {
705-
BitcoindClient::Rpc { rpc_client, .. } => Arc::clone(rpc_client) as Arc<dyn UtxoSource>,
706-
BitcoindClient::Rest { rest_client, .. } => {
707-
Arc::clone(rest_client) as Arc<dyn UtxoSource>
708-
},
764+
Self::Rpc { rpc_client, .. } => UtxoSourceClient::Rpc(Arc::clone(&rpc_client)),
765+
Self::Rest { rest_client, .. } => UtxoSourceClient::Rest(Arc::clone(&rest_client)),
709766
}
710767
}
711768

src/chain/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66
// accordance with one or both of these licenses.
77

8-
mod bitcoind;
8+
pub(crate) mod bitcoind;
99
mod electrum;
1010
mod esplora;
1111

@@ -15,9 +15,8 @@ use std::time::Duration;
1515

1616
use bitcoin::{Script, Txid};
1717
use lightning::chain::{BestBlock, Filter};
18-
use lightning_block_sync::gossip::UtxoSource;
1918

20-
use crate::chain::bitcoind::BitcoindChainSource;
19+
use crate::chain::bitcoind::{BitcoindChainSource, UtxoSourceClient};
2120
use crate::chain::electrum::ElectrumChainSource;
2221
use crate::chain::esplora::EsploraChainSource;
2322
use crate::config::{
@@ -202,7 +201,7 @@ impl ChainSource {
202201
}
203202
}
204203

205-
pub(crate) fn as_utxo_source(&self) -> Option<Arc<dyn UtxoSource>> {
204+
pub(crate) fn as_utxo_source(&self) -> Option<UtxoSourceClient> {
206205
match &self.kind {
207206
ChainSourceKind::Bitcoind(bitcoind_chain_source) => {
208207
Some(bitcoind_chain_source.as_utxo_source())

src/gossip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl GossipSource {
7171
if let Some(utxo_source) = chain_source.as_utxo_source() {
7272
let spawner = RuntimeSpawner::new(Arc::clone(&runtime));
7373
let gossip_verifier = Arc::new(GossipVerifier::new(
74-
utxo_source,
74+
Arc::new(utxo_source),
7575
spawner,
7676
Arc::clone(gossip_sync),
7777
peer_manager,

src/types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ use lightning::sign::InMemorySigner;
2323
use lightning::util::persist::{KVStore, KVStoreSync, MonitorUpdatingPersister};
2424
use lightning::util::ser::{Readable, Writeable, Writer};
2525
use lightning::util::sweep::OutputSweeper;
26-
use lightning_block_sync::gossip::{GossipVerifier, UtxoSource};
26+
use lightning_block_sync::gossip::GossipVerifier;
2727
use lightning_liquidity::utils::time::DefaultTimeProvider;
2828
use lightning_net_tokio::SocketDescriptor;
2929

30+
use crate::chain::bitcoind::UtxoSourceClient;
3031
use crate::chain::ChainSource;
3132
use crate::config::ChannelConfig;
3233
use crate::data_store::DataStore;
@@ -119,7 +120,7 @@ pub(crate) type Scorer = CombinedScorer<Arc<Graph>, Arc<Logger>>;
119120

120121
pub(crate) type Graph = gossip::NetworkGraph<Arc<Logger>>;
121122

122-
pub(crate) type UtxoLookup = GossipVerifier<RuntimeSpawner, Arc<dyn UtxoSource>, Arc<Logger>>;
123+
pub(crate) type UtxoLookup = GossipVerifier<RuntimeSpawner, Arc<UtxoSourceClient>, Arc<Logger>>;
123124

124125
pub(crate) type P2PGossipSync =
125126
lightning::routing::gossip::P2PGossipSync<Arc<Graph>, Arc<UtxoLookup>, Arc<Logger>>;

0 commit comments

Comments
 (0)