Skip to content

Commit

Permalink
Add ChainSource::BitcoindRpc variant and basic client struct
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Oct 10, 2024
1 parent d1c922d commit 8bf54fa
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ lightning-net-tokio = { version = "0.0.124" }
lightning-persister = { version = "0.0.124" }
lightning-background-processor = { version = "0.0.124", features = ["futures"] }
lightning-rapid-gossip-sync = { version = "0.0.124" }
lightning-block-sync = { version = "0.0.124", features = ["rpc-client"] }
lightning-transaction-sync = { version = "0.0.124", features = ["esplora-async-https", "time"] }
lightning-liquidity = { version = "0.1.0-alpha.5", features = ["std"] }

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

base64 = { version = "0.22.1", default-features = false, features = ["std"] }
rand = "0.8.5"
chrono = { version = "0.4", default-features = false, features = ["clock"] }
tokio = { version = "1.37", default-features = false, features = [ "rt-multi-thread", "time", "sync", "macros" ] }
Expand Down
53 changes: 53 additions & 0 deletions src/chain/bitcoind_rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This file is Copyright its original authors, visible in version control history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
// accordance with one or both of these licenses.

use lightning_block_sync::http::HttpEndpoint;
use lightning_block_sync::rpc::RpcClient;
use lightning_block_sync::{AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource};

use bitcoin::BlockHash;

use base64::prelude::{Engine, BASE64_STANDARD};

use std::sync::Arc;

pub struct BitcoindRpcClient {
rpc_client: Arc<RpcClient>,
}

impl BitcoindRpcClient {
pub(crate) fn new(host: String, port: u16, rpc_user: String, rpc_password: String) -> Self {
let http_endpoint = HttpEndpoint::for_host(host.clone()).with_port(port);
let rpc_credentials =
BASE64_STANDARD.encode(format!("{}:{}", rpc_user.clone(), rpc_password.clone()));

let rpc_client = Arc::new(
RpcClient::new(&rpc_credentials, http_endpoint)
.expect("RpcClient::new is actually infallible"),
);

Self { rpc_client }
}
}

impl BlockSource for BitcoindRpcClient {
fn get_header<'a>(
&'a self, header_hash: &'a BlockHash, height_hint: Option<u32>,
) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
Box::pin(async move { self.rpc_client.get_header(header_hash, height_hint).await })
}

fn get_block<'a>(
&'a self, header_hash: &'a BlockHash,
) -> AsyncBlockSourceResult<'a, BlockData> {
Box::pin(async move { self.rpc_client.get_block(header_hash).await })
}

fn get_best_block<'a>(&'a self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)> {
Box::pin(async move { self.rpc_client.get_best_block().await })
}
}
47 changes: 47 additions & 0 deletions src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
// accordance with one or both of these licenses.

mod bitcoind_rpc;

use crate::config::{
Config, EsploraSyncConfig, BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP,
BDK_WALLET_SYNC_TIMEOUT_SECS, FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS, LDK_WALLET_SYNC_TIMEOUT_SECS,
Expand Down Expand Up @@ -35,6 +37,8 @@ use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use self::bitcoind_rpc::BitcoindRpcClient;

// The default Esplora server we're using.
pub(crate) const DEFAULT_ESPLORA_SERVER_URL: &str = "https://blockstream.info/api";

Expand Down Expand Up @@ -109,6 +113,18 @@ pub(crate) enum ChainSource {
logger: Arc<FilesystemLogger>,
node_metrics: Arc<RwLock<NodeMetrics>>,
},
BitcoindRpc {
bitcoind_rpc_client: Arc<BitcoindRpcClient>,
onchain_wallet: Arc<Wallet>,
onchain_wallet_sync_status: Mutex<WalletSyncStatus>,
lightning_wallet_sync_status: Mutex<WalletSyncStatus>,
fee_estimator: Arc<OnchainFeeEstimator>,
tx_broadcaster: Arc<Broadcaster>,
kv_store: Arc<DynStore>,
config: Arc<Config>,
logger: Arc<FilesystemLogger>,
node_metrics: Arc<RwLock<NodeMetrics>>,
},
}

impl ChainSource {
Expand Down Expand Up @@ -141,6 +157,30 @@ impl ChainSource {
}
}

pub(crate) fn new_bitcoind_rpc(
host: String, port: u16, rpc_user: String, rpc_password: String,
onchain_wallet: Arc<Wallet>, fee_estimator: Arc<OnchainFeeEstimator>,
tx_broadcaster: Arc<Broadcaster>, kv_store: Arc<DynStore>, config: Arc<Config>,
logger: Arc<FilesystemLogger>, node_metrics: Arc<RwLock<NodeMetrics>>,
) -> Self {
let bitcoind_rpc_client =
Arc::new(BitcoindRpcClient::new(host, port, rpc_user, rpc_password));
let onchain_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
let lightning_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
Self::BitcoindRpc {
bitcoind_rpc_client,
onchain_wallet,
onchain_wallet_sync_status,
lightning_wallet_sync_status,
fee_estimator,
tx_broadcaster,
kv_store,
config,
logger,
node_metrics,
}
}

pub(crate) async fn continuously_sync_wallets(
&self, mut stop_sync_receiver: tokio::sync::watch::Receiver<()>,
channel_manager: Arc<ChannelManager>, chain_monitor: Arc<ChainMonitor>,
Expand Down Expand Up @@ -201,6 +241,7 @@ impl ChainSource {
}
}
},
Self::BitcoindRpc { .. } => todo!(),
}
}

Expand Down Expand Up @@ -319,6 +360,7 @@ impl ChainSource {

res
},
Self::BitcoindRpc { .. } => todo!(),
}
}

Expand Down Expand Up @@ -411,6 +453,7 @@ impl ChainSource {

res
},
Self::BitcoindRpc { .. } => todo!(),
}
}

Expand Down Expand Up @@ -506,6 +549,7 @@ impl ChainSource {

Ok(())
},
Self::BitcoindRpc { .. } => todo!(),
}
}

Expand Down Expand Up @@ -582,6 +626,7 @@ impl ChainSource {
}
}
},
Self::BitcoindRpc { .. } => todo!(),
}
}
}
Expand All @@ -590,11 +635,13 @@ impl Filter for ChainSource {
fn register_tx(&self, txid: &bitcoin::Txid, script_pubkey: &bitcoin::Script) {
match self {
Self::Esplora { tx_sync, .. } => tx_sync.register_tx(txid, script_pubkey),
Self::BitcoindRpc { .. } => (),
}
}
fn register_output(&self, output: lightning::chain::WatchedOutput) {
match self {
Self::Esplora { tx_sync, .. } => tx_sync.register_output(output),
Self::BitcoindRpc { .. } => (),
}
}
}
Expand Down

0 comments on commit 8bf54fa

Please sign in to comment.