]> git.bitcoin.ninja Git - ldk-sample/commitdiff
Pass a logger through into `BitcoindClient`
authorMatt Corallo <git@bluematt.me>
Fri, 24 Mar 2023 22:15:32 +0000 (22:15 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 24 Mar 2023 23:48:47 +0000 (23:48 +0000)
src/bitcoind_client.rs
src/main.rs

index 68843296ef48e38e2aa6af1602a785eccfe513a6..d8315ac159c3e92b8014c52e3f8dd38e11aa473d 100644 (file)
@@ -1,11 +1,14 @@
 use crate::convert::{BlockchainInfo, FeeResponse, FundedTx, NewAddress, RawTx, SignedTx};
+use crate::disk::FilesystemLogger;
 use base64;
 use bitcoin::blockdata::transaction::Transaction;
 use bitcoin::consensus::encode;
 use bitcoin::hash_types::{BlockHash, Txid};
 use bitcoin::util::address::Address;
 use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
+use lightning::log_error;
 use lightning::routing::utxo::{UtxoLookup, UtxoResult};
+use lightning::util::logger::Logger;
 use lightning_block_sync::http::HttpEndpoint;
 use lightning_block_sync::rpc::RpcClient;
 use lightning_block_sync::{AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource};
@@ -24,6 +27,7 @@ pub struct BitcoindClient {
        rpc_password: String,
        fees: Arc<HashMap<Target, AtomicU32>>,
        handle: tokio::runtime::Handle,
+       logger: Arc<FilesystemLogger>,
 }
 
 #[derive(Clone, Eq, Hash, PartialEq)]
@@ -55,9 +59,9 @@ impl BlockSource for BitcoindClient {
 const MIN_FEERATE: u32 = 253;
 
 impl BitcoindClient {
-       pub async fn new(
+       pub(crate) async fn new(
                host: String, port: u16, rpc_user: String, rpc_password: String,
-               handle: tokio::runtime::Handle,
+               handle: tokio::runtime::Handle, logger: Arc<FilesystemLogger>,
        ) -> std::io::Result<Self> {
                let http_endpoint = HttpEndpoint::for_host(host.clone()).with_port(port);
                let rpc_credentials =
@@ -82,6 +86,7 @@ impl BitcoindClient {
                        rpc_password,
                        fees: Arc::new(fees),
                        handle: handle.clone(),
+                       logger,
                };
                BitcoindClient::poll_for_fee_estimates(
                        client.fees.clone(),
@@ -250,6 +255,7 @@ impl BroadcasterInterface for BitcoindClient {
        fn broadcast_transaction(&self, tx: &Transaction) {
                let bitcoind_rpc_client = self.bitcoind_rpc_client.clone();
                let tx_serialized = serde_json::json!(encode::serialize_hex(tx));
+               let logger = Arc::clone(&self.logger);
                self.handle.spawn(async move {
                        // This may error due to RL calling `broadcast_transaction` with the same transaction
                        // multiple times, but the error is safe to ignore.
index d43bb9b26a4d500e7de303911cc33bb2c61c3858..9a5b62584b7383374bda67b6db322ae46dfe2248 100644 (file)
@@ -378,6 +378,10 @@ async fn start_ldk() {
        let ldk_data_dir = format!("{}/.ldk", args.ldk_storage_dir_path);
        fs::create_dir_all(ldk_data_dir.clone()).unwrap();
 
+       // ## Setup
+       // Step 1: Initialize the Logger
+       let logger = Arc::new(FilesystemLogger::new(ldk_data_dir.clone()));
+
        // Initialize our bitcoind client.
        let bitcoind_client = match BitcoindClient::new(
                args.bitcoind_rpc_host.clone(),
@@ -385,6 +389,7 @@ async fn start_ldk() {
                args.bitcoind_rpc_username.clone(),
                args.bitcoind_rpc_password.clone(),
                tokio::runtime::Handle::current(),
+               Arc::clone(&logger),
        )
        .await
        {
@@ -411,15 +416,11 @@ async fn start_ldk() {
                return;
        }
 
-       // ## Setup
-       // Step 1: Initialize the FeeEstimator
+       // Step 2: Initialize the FeeEstimator
 
        // BitcoindClient implements the FeeEstimator trait, so it'll act as our fee estimator.
        let fee_estimator = bitcoind_client.clone();
 
-       // Step 2: Initialize the Logger
-       let logger = Arc::new(FilesystemLogger::new(ldk_data_dir.clone()));
-
        // Step 3: Initialize the BroadcasterInterface
 
        // BitcoindClient implements the BroadcasterInterface trait, so it'll act as our transaction