Skip to content

Commit 0b2cf5c

Browse files
committed
Add ChainSource::BitcoindRpc variant and basic client struct
1 parent 6dd6a48 commit 0b2cf5c

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ lightning-net-tokio = { version = "0.0.125" }
3434
lightning-persister = { version = "0.0.125" }
3535
lightning-background-processor = { version = "0.0.125", features = ["futures"] }
3636
lightning-rapid-gossip-sync = { version = "0.0.125" }
37+
lightning-block-sync = { version = "0.0.125", features = ["rpc-client", "tokio"] }
3738
lightning-transaction-sync = { version = "0.0.125", features = ["esplora-async-https", "time"] }
3839
lightning-liquidity = { version = "0.1.0-alpha.6", features = ["std"] }
3940

@@ -65,6 +66,7 @@ bitcoin = "0.32.2"
6566
bip39 = "2.0.0"
6667
bip21 = { version = "0.5", features = ["std"], default-features = false }
6768

69+
base64 = { version = "0.22.1", default-features = false, features = ["std"] }
6870
rand = "0.8.5"
6971
chrono = { version = "0.4", default-features = false, features = ["clock"] }
7072
tokio = { version = "1.37", default-features = false, features = [ "rt-multi-thread", "time", "sync", "macros" ] }

src/chain/bitcoind_rpc.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// This file is Copyright its original authors, visible in version control history.
2+
//
3+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5+
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
6+
// accordance with one or both of these licenses.
7+
8+
use lightning_block_sync::http::HttpEndpoint;
9+
use lightning_block_sync::rpc::RpcClient;
10+
use lightning_block_sync::{AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource};
11+
12+
use bitcoin::BlockHash;
13+
14+
use base64::prelude::{Engine, BASE64_STANDARD};
15+
16+
use std::sync::Arc;
17+
18+
pub struct BitcoindRpcClient {
19+
rpc_client: Arc<RpcClient>,
20+
}
21+
22+
impl BitcoindRpcClient {
23+
pub(crate) fn new(host: String, port: u16, rpc_user: String, rpc_password: String) -> Self {
24+
let http_endpoint = HttpEndpoint::for_host(host.clone()).with_port(port);
25+
let rpc_credentials =
26+
BASE64_STANDARD.encode(format!("{}:{}", rpc_user.clone(), rpc_password.clone()));
27+
28+
let rpc_client = Arc::new(
29+
RpcClient::new(&rpc_credentials, http_endpoint)
30+
.expect("RpcClient::new is actually infallible"),
31+
);
32+
33+
Self { rpc_client }
34+
}
35+
}
36+
37+
impl BlockSource for BitcoindRpcClient {
38+
fn get_header<'a>(
39+
&'a self, header_hash: &'a BlockHash, height_hint: Option<u32>,
40+
) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
41+
Box::pin(async move { self.rpc_client.get_header(header_hash, height_hint).await })
42+
}
43+
44+
fn get_block<'a>(
45+
&'a self, header_hash: &'a BlockHash,
46+
) -> AsyncBlockSourceResult<'a, BlockData> {
47+
Box::pin(async move { self.rpc_client.get_block(header_hash).await })
48+
}
49+
50+
fn get_best_block<'a>(&'a self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)> {
51+
Box::pin(async move { self.rpc_client.get_best_block().await })
52+
}
53+
}

src/chain/mod.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
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_rpc;
9+
810
use crate::config::{
911
Config, EsploraSyncConfig, BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP,
1012
BDK_WALLET_SYNC_TIMEOUT_SECS, FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS, LDK_WALLET_SYNC_TIMEOUT_SECS,
@@ -35,6 +37,8 @@ use std::collections::HashMap;
3537
use std::sync::{Arc, Mutex, RwLock};
3638
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
3739

40+
use self::bitcoind_rpc::BitcoindRpcClient;
41+
3842
// The default Esplora server we're using.
3943
pub(crate) const DEFAULT_ESPLORA_SERVER_URL: &str = "https://blockstream.info/api";
4044

@@ -109,6 +113,18 @@ pub(crate) enum ChainSource {
109113
logger: Arc<FilesystemLogger>,
110114
node_metrics: Arc<RwLock<NodeMetrics>>,
111115
},
116+
BitcoindRpc {
117+
bitcoind_rpc_client: Arc<BitcoindRpcClient>,
118+
onchain_wallet: Arc<Wallet>,
119+
onchain_wallet_sync_status: Mutex<WalletSyncStatus>,
120+
lightning_wallet_sync_status: Mutex<WalletSyncStatus>,
121+
fee_estimator: Arc<OnchainFeeEstimator>,
122+
tx_broadcaster: Arc<Broadcaster>,
123+
kv_store: Arc<DynStore>,
124+
config: Arc<Config>,
125+
logger: Arc<FilesystemLogger>,
126+
node_metrics: Arc<RwLock<NodeMetrics>>,
127+
},
112128
}
113129

114130
impl ChainSource {
@@ -141,6 +157,30 @@ impl ChainSource {
141157
}
142158
}
143159

160+
pub(crate) fn new_bitcoind_rpc(
161+
host: String, port: u16, rpc_user: String, rpc_password: String,
162+
onchain_wallet: Arc<Wallet>, fee_estimator: Arc<OnchainFeeEstimator>,
163+
tx_broadcaster: Arc<Broadcaster>, kv_store: Arc<DynStore>, config: Arc<Config>,
164+
logger: Arc<FilesystemLogger>, node_metrics: Arc<RwLock<NodeMetrics>>,
165+
) -> Self {
166+
let bitcoind_rpc_client =
167+
Arc::new(BitcoindRpcClient::new(host, port, rpc_user, rpc_password));
168+
let onchain_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
169+
let lightning_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
170+
Self::BitcoindRpc {
171+
bitcoind_rpc_client,
172+
onchain_wallet,
173+
onchain_wallet_sync_status,
174+
lightning_wallet_sync_status,
175+
fee_estimator,
176+
tx_broadcaster,
177+
kv_store,
178+
config,
179+
logger,
180+
node_metrics,
181+
}
182+
}
183+
144184
pub(crate) async fn continuously_sync_wallets(
145185
&self, mut stop_sync_receiver: tokio::sync::watch::Receiver<()>,
146186
channel_manager: Arc<ChannelManager>, chain_monitor: Arc<ChainMonitor>,
@@ -201,6 +241,7 @@ impl ChainSource {
201241
}
202242
}
203243
},
244+
Self::BitcoindRpc { .. } => todo!(),
204245
}
205246
}
206247

@@ -319,6 +360,7 @@ impl ChainSource {
319360

320361
res
321362
},
363+
Self::BitcoindRpc { .. } => todo!(),
322364
}
323365
}
324366

@@ -411,6 +453,7 @@ impl ChainSource {
411453

412454
res
413455
},
456+
Self::BitcoindRpc { .. } => todo!(),
414457
}
415458
}
416459

@@ -506,6 +549,7 @@ impl ChainSource {
506549

507550
Ok(())
508551
},
552+
Self::BitcoindRpc { .. } => todo!(),
509553
}
510554
}
511555

@@ -582,6 +626,7 @@ impl ChainSource {
582626
}
583627
}
584628
},
629+
Self::BitcoindRpc { .. } => todo!(),
585630
}
586631
}
587632
}
@@ -590,11 +635,13 @@ impl Filter for ChainSource {
590635
fn register_tx(&self, txid: &bitcoin::Txid, script_pubkey: &bitcoin::Script) {
591636
match self {
592637
Self::Esplora { tx_sync, .. } => tx_sync.register_tx(txid, script_pubkey),
638+
Self::BitcoindRpc { .. } => (),
593639
}
594640
}
595641
fn register_output(&self, output: lightning::chain::WatchedOutput) {
596642
match self {
597643
Self::Esplora { tx_sync, .. } => tx_sync.register_output(output),
644+
Self::BitcoindRpc { .. } => (),
598645
}
599646
}
600647
}

0 commit comments

Comments
 (0)