From c49c3d79265dcd8eb931933544a22f6d1c1dd76f Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 30 Jan 2024 11:00:25 +0100 Subject: [PATCH 1/3] Drop `KVStore` generic from `Node` .. switching to `dyn KVStore + Send + Sync` --- src/builder.rs | 42 ++++++++++----------- src/event.rs | 42 ++++++++++----------- src/io/utils.rs | 67 +++++++++++++++------------------ src/lib.rs | 36 +++++++++--------- src/liquidity.rs | 17 ++++----- src/message_handler.rs | 15 +++----- src/payment_store.rs | 12 +++--- src/peer_store.rs | 16 ++++---- src/types.rs | 34 ++++++++--------- src/uniffi_types.rs | 3 +- tests/common/mod.rs | 19 ++++------ tests/integration_tests_rust.rs | 5 ++- 12 files changed, 147 insertions(+), 161 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index daf3afd47..5edbd55ab 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -14,7 +14,7 @@ use crate::payment_store::PaymentStore; use crate::peer_store::PeerStore; use crate::tx_broadcaster::TransactionBroadcaster; use crate::types::{ - ChainMonitor, ChannelManager, GossipSync, KeysManager, MessageRouter, NetworkGraph, + ChainMonitor, ChannelManager, DynStore, GossipSync, KeysManager, MessageRouter, NetworkGraph, OnionMessenger, PeerManager, }; use crate::wallet::Wallet; @@ -32,7 +32,7 @@ use lightning::sign::EntropySource; use lightning::util::config::UserConfig; use lightning::util::persist::{ - read_channel_monitors, KVStore, CHANNEL_MANAGER_PERSISTENCE_KEY, + read_channel_monitors, CHANNEL_MANAGER_PERSISTENCE_KEY, CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE, }; use lightning::util::ser::ReadableArgs; @@ -115,12 +115,18 @@ pub enum BuildError { /// The given listening addresses are invalid, e.g. too many were passed. InvalidListeningAddresses, /// We failed to read data from the [`KVStore`]. + /// + /// [`KVStore`]: lightning::util::persist::KVStore ReadFailed, /// We failed to write data to the [`KVStore`]. + /// + /// [`KVStore`]: lightning::util::persist::KVStore WriteFailed, /// We failed to access the given `storage_dir_path`. StoragePathAccessFailed, /// We failed to setup our [`KVStore`]. + /// + /// [`KVStore`]: lightning::util::persist::KVStore KVStoreSetupFailed, /// We failed to setup the onchain wallet. WalletSetupFailed, @@ -299,7 +305,7 @@ impl NodeBuilder { /// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options /// previously configured. - pub fn build(&self) -> Result, BuildError> { + pub fn build(&self) -> Result { let storage_dir_path = self.config.storage_dir_path.clone(); fs::create_dir_all(storage_dir_path.clone()) .map_err(|_| BuildError::StoragePathAccessFailed)?; @@ -316,7 +322,7 @@ impl NodeBuilder { /// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options /// previously configured. - pub fn build_with_fs_store(&self) -> Result, BuildError> { + pub fn build_with_fs_store(&self) -> Result { let mut storage_dir_path: PathBuf = self.config.storage_dir_path.clone().into(); storage_dir_path.push("fs_store"); @@ -329,9 +335,7 @@ impl NodeBuilder { /// Builds a [`Node`] instance with a [`VssStore`] backend and according to the options /// previously configured. #[cfg(any(vss, vss_test))] - pub fn build_with_vss_store( - &self, url: String, store_id: String, - ) -> Result, BuildError> { + pub fn build_with_vss_store(&self, url: String, store_id: String) -> Result { let logger = setup_logger(&self.config)?; let seed_bytes = seed_bytes_from_config( @@ -369,9 +373,7 @@ impl NodeBuilder { } /// Builds a [`Node`] instance according to the options previously configured. - pub fn build_with_store( - &self, kv_store: Arc, - ) -> Result, BuildError> { + pub fn build_with_store(&self, kv_store: Arc) -> Result { let logger = setup_logger(&self.config)?; let seed_bytes = seed_bytes_from_config( &self.config, @@ -500,31 +502,29 @@ impl ArcedNodeBuilder { /// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options /// previously configured. - pub fn build(&self) -> Result>, BuildError> { + pub fn build(&self) -> Result, BuildError> { self.inner.read().unwrap().build().map(Arc::new) } /// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options /// previously configured. - pub fn build_with_fs_store(&self) -> Result>, BuildError> { + pub fn build_with_fs_store(&self) -> Result, BuildError> { self.inner.read().unwrap().build_with_fs_store().map(Arc::new) } /// Builds a [`Node`] instance according to the options previously configured. - pub fn build_with_store( - &self, kv_store: Arc, - ) -> Result>, BuildError> { + pub fn build_with_store(&self, kv_store: Arc) -> Result, BuildError> { self.inner.read().unwrap().build_with_store(kv_store).map(Arc::new) } } /// Builds a [`Node`] instance according to the options previously configured. -fn build_with_store_internal( +fn build_with_store_internal( config: Arc, chain_data_source_config: Option<&ChainDataSourceConfig>, gossip_source_config: Option<&GossipSourceConfig>, liquidity_source_config: Option<&LiquiditySourceConfig>, seed_bytes: [u8; 64], - logger: Arc, kv_store: Arc, -) -> Result, BuildError> { + logger: Arc, kv_store: Arc, +) -> Result { // Initialize the on-chain wallet and chain access let xprv = bitcoin::bip32::ExtendedPrivKey::new_master(config.network.into(), &seed_bytes) .map_err(|e| { @@ -604,7 +604,7 @@ fn build_with_store_internal( )); // Initialize the ChainMonitor - let chain_monitor: Arc> = Arc::new(chainmonitor::ChainMonitor::new( + let chain_monitor: Arc = Arc::new(chainmonitor::ChainMonitor::new( Some(Arc::clone(&tx_sync)), Arc::clone(&tx_broadcaster), Arc::clone(&logger), @@ -735,7 +735,7 @@ fn build_with_store_internal( channel_monitor_references, ); let (_hash, channel_manager) = - <(BlockHash, ChannelManager)>::read(&mut reader, read_args).map_err(|e| { + <(BlockHash, ChannelManager)>::read(&mut reader, read_args).map_err(|e| { log_error!(logger, "Failed to read channel manager from KVStore: {}", e); BuildError::ReadFailed })?; @@ -779,7 +779,7 @@ fn build_with_store_internal( let message_router = MessageRouter::new(Arc::clone(&network_graph), Arc::clone(&keys_manager)); // Initialize the PeerManager - let onion_messenger: Arc> = Arc::new(OnionMessenger::new( + let onion_messenger: Arc = Arc::new(OnionMessenger::new( Arc::clone(&keys_manager), Arc::clone(&keys_manager), Arc::clone(&logger), diff --git a/src/event.rs b/src/event.rs index 29ebbef43..61dc748d4 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,4 +1,4 @@ -use crate::types::{Sweeper, Wallet}; +use crate::types::{DynStore, Sweeper, Wallet}; use crate::{ hex_utils, ChannelManager, Config, Error, NetworkGraph, PeerInfo, PeerStore, UserChannelId, }; @@ -20,7 +20,6 @@ use lightning::impl_writeable_tlv_based_enum; use lightning::ln::{ChannelId, PaymentHash}; use lightning::routing::gossip::NodeId; use lightning::util::errors::APIError; -use lightning::util::persist::KVStore; use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer}; use lightning_liquidity::lsps2::utils::compute_opening_fee; @@ -138,22 +137,22 @@ impl_writeable_tlv_based_enum!(Event, }; ); -pub struct EventQueue +pub struct EventQueue where L::Target: Logger, { queue: Arc>>, waker: Arc>>, notifier: Condvar, - kv_store: Arc, + kv_store: Arc, logger: L, } -impl EventQueue +impl EventQueue where L::Target: Logger, { - pub(crate) fn new(kv_store: Arc, logger: L) -> Self { + pub(crate) fn new(kv_store: Arc, logger: L) -> Self { let queue = Arc::new(Mutex::new(VecDeque::new())); let waker = Arc::new(Mutex::new(None)); let notifier = Condvar::new(); @@ -228,13 +227,13 @@ where } } -impl ReadableArgs<(Arc, L)> for EventQueue +impl ReadableArgs<(Arc, L)> for EventQueue where L::Target: Logger, { #[inline] fn read( - reader: &mut R, args: (Arc, L), + reader: &mut R, args: (Arc, L), ) -> Result { let (kv_store, logger) = args; let read_queue: EventQueueDeserWrapper = Readable::read(reader)?; @@ -292,32 +291,31 @@ impl Future for EventFuture { } } -pub(crate) struct EventHandler +pub(crate) struct EventHandler where L::Target: Logger, { - event_queue: Arc>, + event_queue: Arc>, wallet: Arc, - channel_manager: Arc>, - output_sweeper: Arc>, + channel_manager: Arc, + output_sweeper: Arc, network_graph: Arc, - payment_store: Arc>, - peer_store: Arc>, + payment_store: Arc>, + peer_store: Arc>, runtime: Arc>>, logger: L, config: Arc, } -impl EventHandler +impl EventHandler where L::Target: Logger, { pub fn new( - event_queue: Arc>, wallet: Arc, - channel_manager: Arc>, output_sweeper: Arc>, - network_graph: Arc, payment_store: Arc>, - peer_store: Arc>, runtime: Arc>>, - logger: L, config: Arc, + event_queue: Arc>, wallet: Arc, channel_manager: Arc, + output_sweeper: Arc, network_graph: Arc, + payment_store: Arc>, peer_store: Arc>, + runtime: Arc>>, logger: L, config: Arc, ) -> Self { Self { event_queue, @@ -933,7 +931,7 @@ mod tests { #[tokio::test] async fn event_queue_persistence() { - let store = Arc::new(TestStore::new(false)); + let store: Arc = Arc::new(TestStore::new(false)); let logger = Arc::new(TestLogger::new()); let event_queue = Arc::new(EventQueue::new(Arc::clone(&store), Arc::clone(&logger))); assert_eq!(event_queue.next_event(), None); @@ -970,7 +968,7 @@ mod tests { #[tokio::test] async fn event_queue_concurrency() { - let store = Arc::new(TestStore::new(false)); + let store: Arc = Arc::new(TestStore::new(false)); let logger = Arc::new(TestLogger::new()); let event_queue = Arc::new(EventQueue::new(Arc::clone(&store), Arc::clone(&logger))); assert_eq!(event_queue.next_event(), None); diff --git a/src/io/utils.rs b/src/io/utils.rs index 937cc706c..3a0429ed2 100644 --- a/src/io/utils.rs +++ b/src/io/utils.rs @@ -4,19 +4,18 @@ use crate::config::WALLET_KEYS_SEED_LEN; use crate::logger::{log_error, FilesystemLogger}; use crate::peer_store::PeerStore; use crate::sweep::DeprecatedSpendableOutputInfo; -use crate::types::{Broadcaster, ChainSource, FeeEstimator, KeysManager, Sweeper}; +use crate::types::{Broadcaster, ChainSource, DynStore, FeeEstimator, KeysManager, Sweeper}; use crate::{Error, EventQueue, PaymentDetails}; use lightning::routing::gossip::NetworkGraph; use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringDecayParameters}; use lightning::util::logger::Logger; use lightning::util::persist::{ - KVStore, KVSTORE_NAMESPACE_KEY_ALPHABET, KVSTORE_NAMESPACE_KEY_MAX_LEN, - NETWORK_GRAPH_PERSISTENCE_KEY, NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE, - NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE, OUTPUT_SWEEPER_PERSISTENCE_KEY, - OUTPUT_SWEEPER_PERSISTENCE_PRIMARY_NAMESPACE, OUTPUT_SWEEPER_PERSISTENCE_SECONDARY_NAMESPACE, - SCORER_PERSISTENCE_KEY, SCORER_PERSISTENCE_PRIMARY_NAMESPACE, - SCORER_PERSISTENCE_SECONDARY_NAMESPACE, + KVSTORE_NAMESPACE_KEY_ALPHABET, KVSTORE_NAMESPACE_KEY_MAX_LEN, NETWORK_GRAPH_PERSISTENCE_KEY, + NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE, NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE, + OUTPUT_SWEEPER_PERSISTENCE_KEY, OUTPUT_SWEEPER_PERSISTENCE_PRIMARY_NAMESPACE, + OUTPUT_SWEEPER_PERSISTENCE_SECONDARY_NAMESPACE, SCORER_PERSISTENCE_KEY, + SCORER_PERSISTENCE_PRIMARY_NAMESPACE, SCORER_PERSISTENCE_SECONDARY_NAMESPACE, }; use lightning::util::ser::{Readable, ReadableArgs, Writeable}; use lightning::util::string::PrintableString; @@ -97,8 +96,8 @@ where } /// Read a previously persisted [`NetworkGraph`] from the store. -pub(crate) fn read_network_graph( - kv_store: Arc, logger: L, +pub(crate) fn read_network_graph( + kv_store: Arc, logger: L, ) -> Result, std::io::Error> where L::Target: Logger, @@ -115,12 +114,8 @@ where } /// Read a previously persisted [`ProbabilisticScorer`] from the store. -pub(crate) fn read_scorer< - K: KVStore + Send + Sync, - G: Deref>, - L: Deref + Clone, ->( - kv_store: Arc, network_graph: G, logger: L, +pub(crate) fn read_scorer>, L: Deref + Clone>( + kv_store: Arc, network_graph: G, logger: L, ) -> Result, std::io::Error> where L::Target: Logger, @@ -139,9 +134,9 @@ where } /// Read previously persisted events from the store. -pub(crate) fn read_event_queue( - kv_store: Arc, logger: L, -) -> Result, std::io::Error> +pub(crate) fn read_event_queue( + kv_store: Arc, logger: L, +) -> Result, std::io::Error> where L::Target: Logger, { @@ -157,9 +152,9 @@ where } /// Read previously persisted peer info from the store. -pub(crate) fn read_peer_info( - kv_store: Arc, logger: L, -) -> Result, std::io::Error> +pub(crate) fn read_peer_info( + kv_store: Arc, logger: L, +) -> Result, std::io::Error> where L::Target: Logger, { @@ -175,8 +170,8 @@ where } /// Read previously persisted payments information from the store. -pub(crate) fn read_payments( - kv_store: Arc, logger: L, +pub(crate) fn read_payments( + kv_store: Arc, logger: L, ) -> Result, std::io::Error> where L::Target: Logger, @@ -205,11 +200,11 @@ where } /// Read `OutputSweeper` state from the store. -pub(crate) fn read_output_sweeper( +pub(crate) fn read_output_sweeper( broadcaster: Arc, fee_estimator: Arc, - chain_data_source: Arc, keys_manager: Arc, kv_store: Arc, + chain_data_source: Arc, keys_manager: Arc, kv_store: Arc, logger: Arc, -) -> Result, std::io::Error> { +) -> Result { let mut reader = Cursor::new(kv_store.read( OUTPUT_SWEEPER_PERSISTENCE_PRIMARY_NAMESPACE, OUTPUT_SWEEPER_PERSISTENCE_SECONDARY_NAMESPACE, @@ -242,8 +237,8 @@ pub(crate) fn read_output_sweeper( /// Note that this migration will be run in the `Builder`, i.e., at the time when the migration is /// happening no background sync is ongoing, so we shouldn't have a risk of interleaving block /// connections during the migration. -pub(crate) fn migrate_deprecated_spendable_outputs( - sweeper: Arc>, kv_store: Arc, logger: L, +pub(crate) fn migrate_deprecated_spendable_outputs( + sweeper: Arc, kv_store: Arc, logger: L, ) -> Result<(), std::io::Error> where L::Target: Logger, @@ -318,8 +313,8 @@ where Ok(()) } -pub(crate) fn read_latest_rgs_sync_timestamp( - kv_store: Arc, logger: L, +pub(crate) fn read_latest_rgs_sync_timestamp( + kv_store: Arc, logger: L, ) -> Result where L::Target: Logger, @@ -338,8 +333,8 @@ where }) } -pub(crate) fn write_latest_rgs_sync_timestamp( - updated_timestamp: u32, kv_store: Arc, logger: L, +pub(crate) fn write_latest_rgs_sync_timestamp( + updated_timestamp: u32, kv_store: Arc, logger: L, ) -> Result<(), Error> where L::Target: Logger, @@ -365,8 +360,8 @@ where }) } -pub(crate) fn read_latest_node_ann_bcast_timestamp( - kv_store: Arc, logger: L, +pub(crate) fn read_latest_node_ann_bcast_timestamp( + kv_store: Arc, logger: L, ) -> Result where L::Target: Logger, @@ -389,8 +384,8 @@ where }) } -pub(crate) fn write_latest_node_ann_bcast_timestamp( - updated_timestamp: u64, kv_store: Arc, logger: L, +pub(crate) fn write_latest_node_ann_bcast_timestamp( + updated_timestamp: u64, kv_store: Arc, logger: L, ) -> Result<(), Error> where L::Target: Logger, diff --git a/src/lib.rs b/src/lib.rs index 5a6e1dd8c..3f240e980 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -131,7 +131,7 @@ use payment_store::PaymentStore; pub use payment_store::{LSPFeeLimits, PaymentDetails, PaymentDirection, PaymentStatus}; use peer_store::{PeerInfo, PeerStore}; use types::{ - Broadcaster, ChainMonitor, ChannelManager, FeeEstimator, KeysManager, NetworkGraph, + Broadcaster, ChainMonitor, ChannelManager, DynStore, FeeEstimator, KeysManager, NetworkGraph, PeerManager, Router, Scorer, Sweeper, Wallet, }; pub use types::{ChannelDetails, PeerDetails, UserChannelId}; @@ -145,8 +145,6 @@ use lightning::ln::{PaymentHash, PaymentPreimage}; use lightning::sign::EntropySource; -use lightning::util::persist::KVStore; - use lightning::util::config::{ChannelHandshakeConfig, UserConfig}; pub use lightning::util::logger::Level as LogLevel; @@ -176,7 +174,7 @@ uniffi::include_scaffolding!("ldk_node"); /// The main interface object of LDK Node, wrapping the necessary LDK and BDK functionalities. /// /// Needs to be initialized and instantiated through [`Builder::build`]. -pub struct Node { +pub struct Node { runtime: Arc>>, stop_sender: tokio::sync::watch::Sender<()>, config: Arc, @@ -184,21 +182,21 @@ pub struct Node { tx_sync: Arc>>, tx_broadcaster: Arc, fee_estimator: Arc, - event_queue: Arc>>, - channel_manager: Arc>, - chain_monitor: Arc>, - output_sweeper: Arc>, - peer_manager: Arc>, + event_queue: Arc>>, + channel_manager: Arc, + chain_monitor: Arc, + output_sweeper: Arc, + peer_manager: Arc, keys_manager: Arc, network_graph: Arc, gossip_source: Arc, - liquidity_source: Option>>>, - kv_store: Arc, + liquidity_source: Option>>>, + kv_store: Arc, logger: Arc, _router: Arc, scorer: Arc>, - peer_store: Arc>>, - payment_store: Arc>>, + peer_store: Arc>>, + payment_store: Arc>>, is_listening: Arc, latest_wallet_sync_timestamp: Arc>>, latest_onchain_wallet_sync_timestamp: Arc>>, @@ -207,7 +205,7 @@ pub struct Node { latest_node_announcement_broadcast_timestamp: Arc>>, } -impl Node { +impl Node { /// Starts the necessary background tasks, such as handling events coming from user input, /// LDK/BDK, and the peer-to-peer network. /// @@ -1809,7 +1807,7 @@ impl Node { } } -impl Drop for Node { +impl Drop for Node { fn drop(&mut self) { let _ = self.stop(); } @@ -1852,8 +1850,8 @@ pub struct NodeStatus { pub latest_node_announcement_broadcast_timestamp: Option, } -async fn connect_peer_if_necessary( - node_id: PublicKey, addr: SocketAddress, peer_manager: Arc>, +async fn connect_peer_if_necessary( + node_id: PublicKey, addr: SocketAddress, peer_manager: Arc, logger: Arc, ) -> Result<(), Error> { if peer_manager.peer_by_node_id(&node_id).is_some() { @@ -1863,8 +1861,8 @@ async fn connect_peer_if_necessary( do_connect_peer(node_id, addr, peer_manager, logger).await } -async fn do_connect_peer( - node_id: PublicKey, addr: SocketAddress, peer_manager: Arc>, +async fn do_connect_peer( + node_id: PublicKey, addr: SocketAddress, peer_manager: Arc, logger: Arc, ) -> Result<(), Error> { log_info!(logger, "Connecting to peer: {}@{}", node_id, addr); diff --git a/src/liquidity.rs b/src/liquidity.rs index 0404fe64e..00e9f5717 100644 --- a/src/liquidity.rs +++ b/src/liquidity.rs @@ -5,7 +5,6 @@ use crate::{Config, Error}; use lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA; use lightning::ln::msgs::SocketAddress; use lightning::routing::router::{RouteHint, RouteHintHop}; -use lightning::util::persist::KVStore; use lightning_invoice::{Bolt11Invoice, InvoiceBuilder, RoutingFees}; use lightning_liquidity::events::Event; use lightning_liquidity::lsps0::ser::RequestId; @@ -33,26 +32,26 @@ struct LSPS2Service { pending_buy_requests: Mutex>>, } -pub(crate) struct LiquiditySource +pub(crate) struct LiquiditySource where L::Target: Logger, { lsps2_service: Option, - channel_manager: Arc>, + channel_manager: Arc, keys_manager: Arc, - liquidity_manager: Arc>, + liquidity_manager: Arc, config: Arc, logger: L, } -impl LiquiditySource +impl LiquiditySource where L::Target: Logger, { pub(crate) fn new_lsps2( address: SocketAddress, node_id: PublicKey, token: Option, - channel_manager: Arc>, keys_manager: Arc, - liquidity_manager: Arc>, config: Arc, logger: L, + channel_manager: Arc, keys_manager: Arc, + liquidity_manager: Arc, config: Arc, logger: L, ) -> Self { let pending_fee_requests = Mutex::new(HashMap::new()); let pending_buy_requests = Mutex::new(HashMap::new()); @@ -66,12 +65,12 @@ where Self { lsps2_service, channel_manager, keys_manager, liquidity_manager, config, logger } } - pub(crate) fn set_peer_manager(&self, peer_manager: Arc>) { + pub(crate) fn set_peer_manager(&self, peer_manager: Arc) { let process_msgs_callback = move || peer_manager.process_events(); self.liquidity_manager.set_process_msgs_callback(process_msgs_callback); } - pub(crate) fn liquidity_manager(&self) -> &LiquidityManager { + pub(crate) fn liquidity_manager(&self) -> &LiquidityManager { self.liquidity_manager.as_ref() } diff --git a/src/message_handler.rs b/src/message_handler.rs index 852f63cec..89d67d846 100644 --- a/src/message_handler.rs +++ b/src/message_handler.rs @@ -4,7 +4,6 @@ use lightning::ln::features::{InitFeatures, NodeFeatures}; use lightning::ln::peer_handler::CustomMessageHandler; use lightning::ln::wire::CustomMessageReader; use lightning::util::logger::Logger; -use lightning::util::persist::KVStore; use lightning_liquidity::lsps0::ser::RawLSPSMessage; @@ -13,19 +12,19 @@ use bitcoin::secp256k1::PublicKey; use std::ops::Deref; use std::sync::Arc; -pub(crate) enum NodeCustomMessageHandler +pub(crate) enum NodeCustomMessageHandler where L::Target: Logger, { Ignoring, - Liquidity { liquidity_source: Arc> }, + Liquidity { liquidity_source: Arc> }, } -impl NodeCustomMessageHandler +impl NodeCustomMessageHandler where L::Target: Logger, { - pub(crate) fn new_liquidity(liquidity_source: Arc>) -> Self { + pub(crate) fn new_liquidity(liquidity_source: Arc>) -> Self { Self::Liquidity { liquidity_source } } @@ -34,8 +33,7 @@ where } } -impl CustomMessageReader - for NodeCustomMessageHandler +impl CustomMessageReader for NodeCustomMessageHandler where L::Target: Logger, { @@ -53,8 +51,7 @@ where } } -impl CustomMessageHandler - for NodeCustomMessageHandler +impl CustomMessageHandler for NodeCustomMessageHandler where L::Target: Logger, { diff --git a/src/payment_store.rs b/src/payment_store.rs index 704966878..12e331ad4 100644 --- a/src/payment_store.rs +++ b/src/payment_store.rs @@ -3,10 +3,10 @@ use crate::io::{ PAYMENT_INFO_PERSISTENCE_PRIMARY_NAMESPACE, PAYMENT_INFO_PERSISTENCE_SECONDARY_NAMESPACE, }; use crate::logger::{log_error, Logger}; +use crate::types::DynStore; use crate::Error; use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret}; -use lightning::util::persist::KVStore; use lightning::util::ser::Writeable; use lightning::{impl_writeable_tlv_based, impl_writeable_tlv_based_enum}; @@ -127,20 +127,20 @@ impl PaymentDetailsUpdate { } } -pub(crate) struct PaymentStore +pub(crate) struct PaymentStore where L::Target: Logger, { payments: Mutex>, - kv_store: Arc, + kv_store: Arc, logger: L, } -impl PaymentStore +impl PaymentStore where L::Target: Logger, { - pub(crate) fn new(payments: Vec, kv_store: Arc, logger: L) -> Self { + pub(crate) fn new(payments: Vec, kv_store: Arc, logger: L) -> Self { let payments = Mutex::new(HashMap::from_iter( payments.into_iter().map(|payment| (payment.hash, payment)), )); @@ -260,7 +260,7 @@ mod tests { #[test] fn payment_info_is_persisted() { - let store = Arc::new(TestStore::new(false)); + let store: Arc = Arc::new(TestStore::new(false)); let logger = Arc::new(TestLogger::new()); let payment_store = PaymentStore::new(Vec::new(), Arc::clone(&store), logger); diff --git a/src/peer_store.rs b/src/peer_store.rs index 46ba1dbe2..21bd50872 100644 --- a/src/peer_store.rs +++ b/src/peer_store.rs @@ -3,10 +3,10 @@ use crate::io::{ PEER_INFO_PERSISTENCE_SECONDARY_NAMESPACE, }; use crate::logger::{log_error, Logger}; +use crate::types::DynStore; use crate::{Error, SocketAddress}; use lightning::impl_writeable_tlv_based; -use lightning::util::persist::KVStore; use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer}; use bitcoin::secp256k1::PublicKey; @@ -15,20 +15,20 @@ use std::collections::HashMap; use std::ops::Deref; use std::sync::{Arc, RwLock}; -pub struct PeerStore +pub struct PeerStore where L::Target: Logger, { peers: RwLock>, - kv_store: Arc, + kv_store: Arc, logger: L, } -impl PeerStore +impl PeerStore where L::Target: Logger, { - pub(crate) fn new(kv_store: Arc, logger: L) -> Self { + pub(crate) fn new(kv_store: Arc, logger: L) -> Self { let peers = RwLock::new(HashMap::new()); Self { peers, kv_store, logger } } @@ -83,13 +83,13 @@ where } } -impl ReadableArgs<(Arc, L)> for PeerStore +impl ReadableArgs<(Arc, L)> for PeerStore where L::Target: Logger, { #[inline] fn read( - reader: &mut R, args: (Arc, L), + reader: &mut R, args: (Arc, L), ) -> Result { let (kv_store, logger) = args; let read_peers: PeerStoreDeserWrapper = Readable::read(reader)?; @@ -150,7 +150,7 @@ mod tests { #[test] fn peer_info_persistence() { - let store = Arc::new(TestStore::new(false)); + let store: Arc = Arc::new(TestStore::new(false)); let logger = Arc::new(TestLogger::new()); let peer_store = PeerStore::new(Arc::clone(&store), Arc::clone(&logger)); diff --git a/src/types.rs b/src/types.rs index afed1320a..bf467dc49 100644 --- a/src/types.rs +++ b/src/types.rs @@ -13,6 +13,7 @@ use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeePa use lightning::sign::InMemorySigner; use lightning::util::config::ChannelConfig as LdkChannelConfig; use lightning::util::config::MaxDustHTLCExposure as LdkMaxDustHTLCExposure; +use lightning::util::persist::KVStore; use lightning::util::ser::{Readable, Writeable, Writer}; use lightning::util::sweep::OutputSweeper; use lightning_net_tokio::SocketDescriptor; @@ -23,35 +24,34 @@ use bitcoin::OutPoint; use std::sync::{Arc, Mutex, RwLock}; -pub(crate) type ChainMonitor = chainmonitor::ChainMonitor< +pub(crate) type DynStore = dyn KVStore + Sync + Send; + +pub(crate) type ChainMonitor = chainmonitor::ChainMonitor< InMemorySigner, Arc, Arc, Arc, Arc, - Arc, + Arc, >; -pub(crate) type PeerManager = lightning::ln::peer_handler::PeerManager< +pub(crate) type PeerManager = lightning::ln::peer_handler::PeerManager< SocketDescriptor, - Arc>, + Arc, Arc, - Arc>, + Arc, Arc, - Arc>>, + Arc>>, Arc, >; pub(crate) type ChainSource = EsploraSyncClient>; -pub(crate) type LiquidityManager = lightning_liquidity::LiquidityManager< - Arc, - Arc>, - Arc, ->; +pub(crate) type LiquidityManager = + lightning_liquidity::LiquidityManager, Arc, Arc>; -pub(crate) type ChannelManager = lightning::ln::channelmanager::ChannelManager< - Arc>, +pub(crate) type ChannelManager = lightning::ln::channelmanager::ChannelManager< + Arc, Arc, Arc, Arc, @@ -109,11 +109,11 @@ pub(crate) type GossipSync = lightning_background_processor::GossipSync< Arc, >; -pub(crate) type OnionMessenger = lightning::onion_message::messenger::OnionMessenger< +pub(crate) type OnionMessenger = lightning::onion_message::messenger::OnionMessenger< Arc, Arc, Arc, - Arc>, + Arc, Arc, IgnoringMessageHandler, IgnoringMessageHandler, @@ -125,12 +125,12 @@ pub(crate) type MessageRouter = lightning::onion_message::messenger::DefaultMess Arc, >; -pub(crate) type Sweeper = OutputSweeper< +pub(crate) type Sweeper = OutputSweeper< Arc, Arc, Arc, Arc, - Arc, + Arc, Arc, Arc, >; diff --git a/src/uniffi_types.rs b/src/uniffi_types.rs index 7c6343b84..3aef1010d 100644 --- a/src/uniffi_types.rs +++ b/src/uniffi_types.rs @@ -11,7 +11,6 @@ use crate::UniffiCustomTypeConverter; use crate::error::Error; use crate::hex_utils; -use crate::io::sqlite_store::SqliteStore; use crate::{Node, SocketAddress, UserChannelId}; use bitcoin::hashes::sha256::Hash as Sha256; @@ -26,7 +25,7 @@ use std::str::FromStr; /// This type alias is required as Uniffi doesn't support generics, i.e., we can only expose the /// concretized types via this aliasing hack. -pub type LDKNode = Node; +pub type LDKNode = Node; impl UniffiCustomTypeConverter for PublicKey { type Builtin = String; diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 3be36869d..b72ecdc75 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -182,9 +182,9 @@ pub(crate) fn random_config() -> Config { } #[cfg(feature = "uniffi")] -type TestNode = Arc>; +type TestNode = Arc; #[cfg(not(feature = "uniffi"))] -type TestNode = Node; +type TestNode = Node; macro_rules! setup_builder { ($builder: ident, $config: expr) => { @@ -197,9 +197,7 @@ macro_rules! setup_builder { pub(crate) use setup_builder; -pub(crate) fn setup_two_nodes( - electrsd: &ElectrsD, allow_0conf: bool, -) -> (TestNode, TestNode) { +pub(crate) fn setup_two_nodes(electrsd: &ElectrsD, allow_0conf: bool) -> (TestNode, TestNode) { println!("== Node A =="); let config_a = random_config(); let node_a = setup_node(electrsd, config_a); @@ -213,7 +211,7 @@ pub(crate) fn setup_two_nodes( (node_a, node_b) } -pub(crate) fn setup_node(electrsd: &ElectrsD, config: Config) -> TestNode { +pub(crate) fn setup_node(electrsd: &ElectrsD, config: Config) -> TestNode { let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap()); setup_builder!(builder, config); builder.set_esplora_server(esplora_url.clone()); @@ -332,8 +330,8 @@ pub(crate) fn premine_and_distribute_funds( generate_blocks_and_wait(bitcoind, electrs, 1); } -pub fn open_channel( - node_a: &TestNode, node_b: &TestNode, funding_amount_sat: u64, announce: bool, +pub fn open_channel( + node_a: &TestNode, node_b: &TestNode, funding_amount_sat: u64, announce: bool, electrsd: &ElectrsD, ) { node_a @@ -354,9 +352,8 @@ pub fn open_channel( wait_for_tx(&electrsd.client, funding_txo_a.txid); } -pub(crate) fn do_channel_full_cycle( - node_a: TestNode, node_b: TestNode, bitcoind: &BitcoindClient, electrsd: &E, - allow_0conf: bool, +pub(crate) fn do_channel_full_cycle( + node_a: TestNode, node_b: TestNode, bitcoind: &BitcoindClient, electrsd: &E, allow_0conf: bool, ) { let addr_a = node_a.new_onchain_address().unwrap(); let addr_b = node_b.new_onchain_address().unwrap(); diff --git a/tests/integration_tests_rust.rs b/tests/integration_tests_rust.rs index 0f1689ecd..71867f8f2 100644 --- a/tests/integration_tests_rust.rs +++ b/tests/integration_tests_rust.rs @@ -9,6 +9,8 @@ use common::{ use ldk_node::{Builder, Event, NodeError}; +use lightning::util::persist::KVStore; + use bitcoin::{Amount, Network}; use std::sync::Arc; @@ -157,7 +159,8 @@ fn start_stop_reinit() { let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap()); - let test_sync_store = Arc::new(TestSyncStore::new(config.storage_dir_path.clone().into())); + let test_sync_store: Arc = + Arc::new(TestSyncStore::new(config.storage_dir_path.clone().into())); setup_builder!(builder, config); builder.set_esplora_server(esplora_url.clone()); From ba28a8ddb2e151b38ff52d23317bf7e27ac35774 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 7 Feb 2024 13:36:49 +0100 Subject: [PATCH 2/3] Drop unnecessary `LDKNode` type alias completely ... now that we don't have any generic to hide, we can just use the `Node` type directly. --- bindings/ldk_node.udl | 4 ++-- src/uniffi_types.rs | 6 +----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/bindings/ldk_node.udl b/bindings/ldk_node.udl index 02dbc2fb2..619a29667 100644 --- a/bindings/ldk_node.udl +++ b/bindings/ldk_node.udl @@ -34,10 +34,10 @@ interface Builder { [Throws=BuildError] void set_listening_addresses(sequence listening_addresses); [Throws=BuildError] - LDKNode build(); + Node build(); }; -interface LDKNode { +interface Node { [Throws=NodeError] void start(); [Throws=NodeError] diff --git a/src/uniffi_types.rs b/src/uniffi_types.rs index 3aef1010d..0cef5d682 100644 --- a/src/uniffi_types.rs +++ b/src/uniffi_types.rs @@ -11,7 +11,7 @@ use crate::UniffiCustomTypeConverter; use crate::error::Error; use crate::hex_utils; -use crate::{Node, SocketAddress, UserChannelId}; +use crate::{SocketAddress, UserChannelId}; use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hashes::Hash; @@ -23,10 +23,6 @@ use lightning_invoice::{Bolt11Invoice, SignedRawBolt11Invoice}; use std::convert::TryInto; use std::str::FromStr; -/// This type alias is required as Uniffi doesn't support generics, i.e., we can only expose the -/// concretized types via this aliasing hack. -pub type LDKNode = Node; - impl UniffiCustomTypeConverter for PublicKey { type Builtin = String; From c566fd51469bf4096f2181243d2ce7663824971c Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 7 Feb 2024 14:10:02 +0100 Subject: [PATCH 3/3] Expose `build_with_fs_store` in bindings .. now that we can. --- bindings/ldk_node.udl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bindings/ldk_node.udl b/bindings/ldk_node.udl index 619a29667..c39d841cb 100644 --- a/bindings/ldk_node.udl +++ b/bindings/ldk_node.udl @@ -35,6 +35,8 @@ interface Builder { void set_listening_addresses(sequence listening_addresses); [Throws=BuildError] Node build(); + [Throws=BuildError] + Node build_with_fs_store(); }; interface Node {