Skip to content

Commit 78fa512

Browse files
committed
avoid dyn in gossip validation
1 parent eaad8f5 commit 78fa512

File tree

4 files changed

+74
-14
lines changed

4 files changed

+74
-14
lines changed

src/chain/bitcoind.rs

Lines changed: 67 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::Listen;
1818
use lightning::util::ser::Writeable;
@@ -120,7 +120,7 @@ impl BitcoindChainSource {
120120
}
121121
}
122122

123-
pub(super) fn as_utxo_source(&self) -> Arc<dyn UtxoSource> {
123+
pub(super) fn as_utxo_source(&self) -> UtxoSourceClient {
124124
self.api_client.utxo_source()
125125
}
126126

@@ -625,6 +625,68 @@ impl BitcoindChainSource {
625625
}
626626
}
627627

628+
#[derive(Clone)]
629+
pub(crate) enum UtxoSourceClient {
630+
Rpc(Arc<RpcClient>),
631+
Rest(Arc<RestClient>),
632+
}
633+
634+
impl std::ops::Deref for UtxoSourceClient {
635+
type Target = Self;
636+
fn deref(&self) -> &Self {
637+
self
638+
}
639+
}
640+
641+
impl BlockSource for UtxoSourceClient {
642+
fn get_header<'a>(
643+
&'a self,
644+
header_hash: &'a BlockHash,
645+
height_hint: Option<u32>,
646+
) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
647+
match self {
648+
Self::Rpc(client) => client.get_header(header_hash, height_hint),
649+
Self::Rest(client) => client.get_header(header_hash, height_hint),
650+
}
651+
}
652+
653+
fn get_block<'a>(
654+
&'a self,
655+
header_hash: &'a BlockHash,
656+
) -> AsyncBlockSourceResult<'a, BlockData> {
657+
match self {
658+
Self::Rpc(client) => client.get_block(header_hash),
659+
Self::Rest(client) => client.get_block(header_hash),
660+
}
661+
}
662+
663+
fn get_best_block(
664+
&self,
665+
) -> AsyncBlockSourceResult<'_, (BlockHash, Option<u32>)> {
666+
match self {
667+
Self::Rpc(client) => client.get_best_block(),
668+
Self::Rest(client) => client.get_best_block(),
669+
}
670+
}
671+
}
672+
673+
674+
impl UtxoSource for UtxoSourceClient {
675+
fn get_block_hash_by_height<'a>(&'a self, block_height: u32) -> AsyncBlockSourceResult<'a, BlockHash> {
676+
match self {
677+
Self::Rpc(client) => client.get_block_hash_by_height(block_height),
678+
Self::Rest(client) => client.get_block_hash_by_height(block_height),
679+
}
680+
}
681+
682+
fn is_output_unspent<'a>(&'a self, outpoint: OutPoint) -> AsyncBlockSourceResult<'a, bool> {
683+
match self {
684+
Self::Rpc(client) => client.is_output_unspent(outpoint),
685+
Self::Rest(client) => client.is_output_unspent(outpoint),
686+
}
687+
}
688+
}
689+
628690
pub enum BitcoindClient {
629691
Rpc {
630692
rpc_client: Arc<RpcClient>,
@@ -686,12 +748,10 @@ impl BitcoindClient {
686748
}
687749
}
688750

689-
pub(crate) fn utxo_source(&self) -> Arc<dyn UtxoSource> {
751+
fn utxo_source(&self) -> UtxoSourceClient {
690752
match self {
691-
BitcoindClient::Rpc { rpc_client, .. } => Arc::clone(rpc_client) as Arc<dyn UtxoSource>,
692-
BitcoindClient::Rest { rest_client, .. } => {
693-
Arc::clone(rest_client) as Arc<dyn UtxoSource>
694-
},
753+
Self::Rpc { rpc_client, .. } => UtxoSourceClient::Rpc(Arc::clone(&rpc_client)),
754+
Self::Rest { rest_client, .. } => UtxoSourceClient::Rest(Arc::clone(&rest_client)),
695755
}
696756
}
697757

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::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::{
@@ -205,7 +204,7 @@ impl ChainSource {
205204
}
206205
}
207206

208-
pub(crate) fn as_utxo_source(&self) -> Option<Arc<dyn UtxoSource>> {
207+
pub(crate) fn as_utxo_source(&self) -> Option<UtxoSourceClient> {
209208
match &self.kind {
210209
ChainSourceKind::Bitcoind(bitcoind_chain_source) => {
211210
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
@@ -24,11 +24,12 @@ use lightning::sign::InMemorySigner;
2424
use lightning::util::persist::{KVStore, KVStoreSync, MonitorUpdatingPersister};
2525
use lightning::util::ser::{Readable, Writeable, Writer};
2626
use lightning::util::sweep::OutputSweeper;
27-
use lightning_block_sync::gossip::{GossipVerifier, UtxoSource};
27+
use lightning_block_sync::gossip::GossipVerifier;
2828
use lightning_liquidity::utils::time::DefaultTimeProvider;
2929
use lightning_net_tokio::SocketDescriptor;
3030

3131
use crate::chain::ChainSource;
32+
use crate::chain::bitcoind::UtxoSourceClient;
3233
use crate::config::ChannelConfig;
3334
use crate::data_store::DataStore;
3435
use crate::fee_estimator::OnchainFeeEstimator;
@@ -120,7 +121,7 @@ pub(crate) type Scorer = CombinedScorer<Arc<Graph>, Arc<Logger>>;
120121

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

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

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

0 commit comments

Comments
 (0)