Skip to content

Commit

Permalink
cleanup BitcoindClient usage
Browse files Browse the repository at this point in the history
  • Loading branch information
johncantrell97 committed Apr 3, 2022
1 parent 502ae9f commit b6f2b9a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/chain/broadcaster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use std::sync::Arc;
use bitcoin::Transaction;
use lightning::chain::chaininterface::BroadcasterInterface;

use super::{bitcoind_client::BitcoindClient, listener_database::ListenerDatabase};
use super::{listener_database::ListenerDatabase};

pub struct SenseiBroadcaster {
pub bitcoind_client: Arc<BitcoindClient>,
pub broadcaster: Arc<dyn BroadcasterInterface + Send + Sync>,
pub listener_database: ListenerDatabase,
}

impl BroadcasterInterface for SenseiBroadcaster {
fn broadcast_transaction(&self, tx: &Transaction) {
self.bitcoind_client.broadcast_transaction(tx);
self.broadcaster.broadcast_transaction(tx);

// TODO: there's a bug here if the broadcast fails
// best solution is to probably setup a zmq listener
Expand Down
12 changes: 12 additions & 0 deletions src/chain/fee_estimator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::sync::Arc;
use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget};

pub struct SenseiFeeEstimator {
pub fee_estimator: Arc<dyn FeeEstimator + Send + Sync>,
}

impl FeeEstimator for SenseiFeeEstimator {
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 {
self.fee_estimator.get_est_sat_per_1000_weight(confirmation_target)
}
}
11 changes: 6 additions & 5 deletions src/chain/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
};
use bitcoin::BlockHash;
use lightning::chain::{BestBlock, Listen};
use lightning_block_sync::poll::ValidatedBlockHeader;
use lightning_block_sync::{poll::ValidatedBlockHeader, BlockSource};
use lightning_block_sync::SpvClient;
use lightning_block_sync::{init, poll, UnboundedCache};
use std::ops::Deref;
Expand Down Expand Up @@ -37,7 +37,7 @@ impl SenseiChainManager {
.await
.expect("invalid bitcoind rpc config"),
);

let block_source_poller = bitcoind_client.clone();
let listener_poller = listener.clone();
tokio::spawn(async move {
Expand Down Expand Up @@ -88,10 +88,11 @@ impl SenseiChainManager {
}

pub async fn get_best_block(&self) -> Result<BestBlock, crate::error::Error> {
let blockchain_info = self.bitcoind_client.get_blockchain_info().await;
let mut block_source = self.bitcoind_client.deref();
let (latest_blockhash, latest_height) = block_source.get_best_block().await.unwrap();
Ok(BestBlock::new(
blockchain_info.latest_blockhash,
blockchain_info.latest_height as u32,
latest_blockhash,
latest_height.unwrap(),
))
}
}
1 change: 1 addition & 0 deletions src/chain/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod bitcoind_client;
pub mod broadcaster;
pub mod fee_estimator;
pub mod listener;
pub mod listener_database;
pub mod manager;
Expand Down
8 changes: 0 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ impl SenseiConfig {

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct LightningNodeConfig {
pub bitcoind_rpc_host: String,
pub bitcoind_rpc_port: u16,
pub bitcoind_rpc_username: String,
pub bitcoind_rpc_password: String,
pub data_dir: String,
pub ldk_peer_listening_port: u16,
pub ldk_announced_listen_addr: Vec<String>,
Expand All @@ -106,10 +102,6 @@ pub struct LightningNodeConfig {
impl Default for LightningNodeConfig {
fn default() -> Self {
LightningNodeConfig {
bitcoind_rpc_host: String::from("127.0.0.1"),
bitcoind_rpc_port: 8133,
bitcoind_rpc_username: String::from("bitcoin"),
bitcoind_rpc_password: String::from("bitcoin"),
data_dir: ".".into(),
ldk_peer_listening_port: 9735,
ldk_announced_listen_addr: vec![],
Expand Down
27 changes: 13 additions & 14 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// You may not use this file except in accordance with one or both of these
// licenses.

use crate::chain::bitcoind_client::BitcoindClient;
use crate::chain::broadcaster::SenseiBroadcaster;
use crate::chain::fee_estimator::SenseiFeeEstimator;
use crate::chain::listener_database::ListenerDatabase;
use crate::chain::manager::SenseiChainManager;
use crate::config::LightningNodeConfig;
Expand All @@ -21,20 +21,15 @@ use crate::services::node::{Channel, NodeInfo, NodeRequest, NodeRequestError, No
use crate::services::{PaginationRequest, PaginationResponse, PaymentsFilter};
use crate::utils::PagedVec;
use crate::{database, disk, hex_utils};
use bdk::blockchain::ConfigurableBlockchain;
use bdk::database::SqliteDatabase;
use bdk::keys::ExtendedKey;
use bdk::wallet::AddressIndex;
use bdk::TransactionDetails;
use bitcoin::hashes::Hash;
use lightning::chain::channelmonitor::ChannelMonitor;
use lightning::chain::{Confirm, Listen};
use lightning_block_sync::BlockSource;
use lightning_block_sync::UnboundedCache;

use lightning::ln::msgs::NetAddress;
use lightning::routing::router::{self, Payee, RouteParameters};
use lightning_block_sync::init;
use lightning_invoice::payment::PaymentError;
use tindercrypt::cryptors::RingCryptor;

Expand All @@ -47,7 +42,7 @@ use bitcoin::BlockHash;
use lightning::chain::chainmonitor;
use lightning::chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager};
use lightning::chain::{self, Filter};
use lightning::chain::{BestBlock, Watch};
use lightning::chain::{Watch};
use lightning::ln::channelmanager::{self, ChannelDetails, SimpleArcChannelManager};
use lightning::ln::channelmanager::{ChainParameters, ChannelManagerReadArgs};
use lightning::ln::peer_handler::{
Expand Down Expand Up @@ -148,7 +143,7 @@ pub type ChainMonitor = chainmonitor::ChainMonitor<
InMemorySigner,
Arc<dyn Filter + Send + Sync>,
Arc<SenseiBroadcaster>,
Arc<BitcoindClient>,
Arc<SenseiFeeEstimator>,
Arc<FilesystemLogger>,
Arc<FilesystemPersister>,
>;
Expand All @@ -167,12 +162,12 @@ pub type PeerManager = SimpleArcPeerManager<
SocketDescriptor,
ChainMonitor,
SenseiBroadcaster,
BitcoindClient,
SenseiFeeEstimator,
FilesystemLogger,
>;

pub type ChannelManager =
SimpleArcChannelManager<ChainMonitor, SenseiBroadcaster, BitcoindClient, FilesystemLogger>;
SimpleArcChannelManager<ChainMonitor, SenseiBroadcaster, SenseiFeeEstimator, FilesystemLogger>;

pub type Router = DefaultRouter<Arc<NetworkGraph>, Arc<FilesystemLogger>>;

Expand All @@ -187,7 +182,7 @@ pub type InvoicePayer = payment::InvoicePayer<
pub type SyncableMonitor = (
ChannelMonitor<InMemorySigner>,
Arc<SenseiBroadcaster>,
Arc<BitcoindClient>,
Arc<SenseiFeeEstimator>,
Arc<FilesystemLogger>,
);

Expand Down Expand Up @@ -406,10 +401,14 @@ impl LightningNode {
let listener_database =
ListenerDatabase::new(config.bdk_database_path(), config.node_database_path());

let fee_estimator = chain_manager.bitcoind_client.clone();
let logger = Arc::new(FilesystemLogger::new(data_dir.clone()));
let logger = Arc::new(FilesystemLogger::new(data_dir.clone()));

let fee_estimator = Arc::new(SenseiFeeEstimator {
fee_estimator: chain_manager.bitcoind_client.clone()
});

let broadcaster = Arc::new(SenseiBroadcaster {
bitcoind_client: chain_manager.bitcoind_client.clone(),
broadcaster: chain_manager.bitcoind_client.clone(),
listener_database: listener_database.clone(),
});

Expand Down
4 changes: 0 additions & 4 deletions src/services/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,6 @@ impl AdminService {
let external_router = node.is_user();
let config = self.config.lock().await;
LightningNodeConfig {
bitcoind_rpc_host: config.bitcoind_rpc_host.clone(),
bitcoind_rpc_port: config.bitcoind_rpc_port,
bitcoind_rpc_username: config.bitcoind_rpc_username.clone(),
bitcoind_rpc_password: config.bitcoind_rpc_password.clone(),
data_dir: format!("{}/{}/{}", self.data_dir, config.network, node.external_id),
ldk_peer_listening_port: node.listen_port,
ldk_announced_listen_addr: vec![],
Expand Down

0 comments on commit b6f2b9a

Please sign in to comment.