Skip to content

Commit fa308a4

Browse files
committed
Drop KVStore generic from Node
.. switching to `dyn KVStore + Send + Sync`
1 parent 750a701 commit fa308a4

File tree

12 files changed

+147
-161
lines changed

12 files changed

+147
-161
lines changed

src/builder.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::payment_store::PaymentStore;
1414
use crate::peer_store::PeerStore;
1515
use crate::tx_broadcaster::TransactionBroadcaster;
1616
use crate::types::{
17-
ChainMonitor, ChannelManager, GossipSync, KeysManager, MessageRouter, NetworkGraph,
17+
ChainMonitor, ChannelManager, DynStore, GossipSync, KeysManager, MessageRouter, NetworkGraph,
1818
OnionMessenger, PeerManager,
1919
};
2020
use crate::wallet::Wallet;
@@ -32,7 +32,7 @@ use lightning::sign::EntropySource;
3232

3333
use lightning::util::config::UserConfig;
3434
use lightning::util::persist::{
35-
read_channel_monitors, KVStore, CHANNEL_MANAGER_PERSISTENCE_KEY,
35+
read_channel_monitors, CHANNEL_MANAGER_PERSISTENCE_KEY,
3636
CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE,
3737
};
3838
use lightning::util::ser::ReadableArgs;
@@ -115,12 +115,18 @@ pub enum BuildError {
115115
/// The given listening addresses are invalid, e.g. too many were passed.
116116
InvalidListeningAddresses,
117117
/// We failed to read data from the [`KVStore`].
118+
///
119+
/// [`KVStore`]: lightning::util::persist::KVStore
118120
ReadFailed,
119121
/// We failed to write data to the [`KVStore`].
122+
///
123+
/// [`KVStore`]: lightning::util::persist::KVStore
120124
WriteFailed,
121125
/// We failed to access the given `storage_dir_path`.
122126
StoragePathAccessFailed,
123127
/// We failed to setup our [`KVStore`].
128+
///
129+
/// [`KVStore`]: lightning::util::persist::KVStore
124130
KVStoreSetupFailed,
125131
/// We failed to setup the onchain wallet.
126132
WalletSetupFailed,
@@ -299,7 +305,7 @@ impl NodeBuilder {
299305

300306
/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
301307
/// previously configured.
302-
pub fn build(&self) -> Result<Node<SqliteStore>, BuildError> {
308+
pub fn build(&self) -> Result<Node, BuildError> {
303309
let storage_dir_path = self.config.storage_dir_path.clone();
304310
fs::create_dir_all(storage_dir_path.clone())
305311
.map_err(|_| BuildError::StoragePathAccessFailed)?;
@@ -316,7 +322,7 @@ impl NodeBuilder {
316322

317323
/// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
318324
/// previously configured.
319-
pub fn build_with_fs_store(&self) -> Result<Node<FilesystemStore>, BuildError> {
325+
pub fn build_with_fs_store(&self) -> Result<Node, BuildError> {
320326
let mut storage_dir_path: PathBuf = self.config.storage_dir_path.clone().into();
321327
storage_dir_path.push("fs_store");
322328

@@ -329,9 +335,7 @@ impl NodeBuilder {
329335
/// Builds a [`Node`] instance with a [`VssStore`] backend and according to the options
330336
/// previously configured.
331337
#[cfg(any(vss, vss_test))]
332-
pub fn build_with_vss_store(
333-
&self, url: String, store_id: String,
334-
) -> Result<Node<VssStore>, BuildError> {
338+
pub fn build_with_vss_store(&self, url: String, store_id: String) -> Result<Node, BuildError> {
335339
let logger = setup_logger(&self.config)?;
336340

337341
let seed_bytes = seed_bytes_from_config(
@@ -369,9 +373,7 @@ impl NodeBuilder {
369373
}
370374

371375
/// Builds a [`Node`] instance according to the options previously configured.
372-
pub fn build_with_store<K: KVStore + Sync + Send + 'static>(
373-
&self, kv_store: Arc<K>,
374-
) -> Result<Node<K>, BuildError> {
376+
pub fn build_with_store(&self, kv_store: Arc<DynStore>) -> Result<Node, BuildError> {
375377
let logger = setup_logger(&self.config)?;
376378
let seed_bytes = seed_bytes_from_config(
377379
&self.config,
@@ -500,31 +502,29 @@ impl ArcedNodeBuilder {
500502

501503
/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
502504
/// previously configured.
503-
pub fn build(&self) -> Result<Arc<Node<SqliteStore>>, BuildError> {
505+
pub fn build(&self) -> Result<Arc<Node>, BuildError> {
504506
self.inner.read().unwrap().build().map(Arc::new)
505507
}
506508

507509
/// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
508510
/// previously configured.
509-
pub fn build_with_fs_store(&self) -> Result<Arc<Node<FilesystemStore>>, BuildError> {
511+
pub fn build_with_fs_store(&self) -> Result<Arc<Node>, BuildError> {
510512
self.inner.read().unwrap().build_with_fs_store().map(Arc::new)
511513
}
512514

513515
/// Builds a [`Node`] instance according to the options previously configured.
514-
pub fn build_with_store<K: KVStore + Sync + Send + 'static>(
515-
&self, kv_store: Arc<K>,
516-
) -> Result<Arc<Node<K>>, BuildError> {
516+
pub fn build_with_store(&self, kv_store: Arc<DynStore>) -> Result<Arc<Node>, BuildError> {
517517
self.inner.read().unwrap().build_with_store(kv_store).map(Arc::new)
518518
}
519519
}
520520

521521
/// Builds a [`Node`] instance according to the options previously configured.
522-
fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
522+
fn build_with_store_internal(
523523
config: Arc<Config>, chain_data_source_config: Option<&ChainDataSourceConfig>,
524524
gossip_source_config: Option<&GossipSourceConfig>,
525525
liquidity_source_config: Option<&LiquiditySourceConfig>, seed_bytes: [u8; 64],
526-
logger: Arc<FilesystemLogger>, kv_store: Arc<K>,
527-
) -> Result<Node<K>, BuildError> {
526+
logger: Arc<FilesystemLogger>, kv_store: Arc<DynStore>,
527+
) -> Result<Node, BuildError> {
528528
// Initialize the on-chain wallet and chain access
529529
let xprv = bitcoin::bip32::ExtendedPrivKey::new_master(config.network.into(), &seed_bytes)
530530
.map_err(|e| {
@@ -604,7 +604,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
604604
));
605605

606606
// Initialize the ChainMonitor
607-
let chain_monitor: Arc<ChainMonitor<K>> = Arc::new(chainmonitor::ChainMonitor::new(
607+
let chain_monitor: Arc<ChainMonitor> = Arc::new(chainmonitor::ChainMonitor::new(
608608
Some(Arc::clone(&tx_sync)),
609609
Arc::clone(&tx_broadcaster),
610610
Arc::clone(&logger),
@@ -735,7 +735,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
735735
channel_monitor_references,
736736
);
737737
let (_hash, channel_manager) =
738-
<(BlockHash, ChannelManager<K>)>::read(&mut reader, read_args).map_err(|e| {
738+
<(BlockHash, ChannelManager)>::read(&mut reader, read_args).map_err(|e| {
739739
log_error!(logger, "Failed to read channel manager from KVStore: {}", e);
740740
BuildError::ReadFailed
741741
})?;
@@ -779,7 +779,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
779779
let message_router = MessageRouter::new(Arc::clone(&network_graph), Arc::clone(&keys_manager));
780780

781781
// Initialize the PeerManager
782-
let onion_messenger: Arc<OnionMessenger<K>> = Arc::new(OnionMessenger::new(
782+
let onion_messenger: Arc<OnionMessenger> = Arc::new(OnionMessenger::new(
783783
Arc::clone(&keys_manager),
784784
Arc::clone(&keys_manager),
785785
Arc::clone(&logger),

src/event.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::types::{Sweeper, Wallet};
1+
use crate::types::{DynStore, Sweeper, Wallet};
22
use crate::{
33
hex_utils, ChannelManager, Config, Error, NetworkGraph, PeerInfo, PeerStore, UserChannelId,
44
};
@@ -20,7 +20,6 @@ use lightning::impl_writeable_tlv_based_enum;
2020
use lightning::ln::{ChannelId, PaymentHash};
2121
use lightning::routing::gossip::NodeId;
2222
use lightning::util::errors::APIError;
23-
use lightning::util::persist::KVStore;
2423
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
2524

2625
use lightning_liquidity::lsps2::utils::compute_opening_fee;
@@ -138,22 +137,22 @@ impl_writeable_tlv_based_enum!(Event,
138137
};
139138
);
140139

141-
pub struct EventQueue<K: KVStore + Sync + Send, L: Deref>
140+
pub struct EventQueue<L: Deref>
142141
where
143142
L::Target: Logger,
144143
{
145144
queue: Arc<Mutex<VecDeque<Event>>>,
146145
waker: Arc<Mutex<Option<Waker>>>,
147146
notifier: Condvar,
148-
kv_store: Arc<K>,
147+
kv_store: Arc<DynStore>,
149148
logger: L,
150149
}
151150

152-
impl<K: KVStore + Sync + Send, L: Deref> EventQueue<K, L>
151+
impl<L: Deref> EventQueue<L>
153152
where
154153
L::Target: Logger,
155154
{
156-
pub(crate) fn new(kv_store: Arc<K>, logger: L) -> Self {
155+
pub(crate) fn new(kv_store: Arc<DynStore>, logger: L) -> Self {
157156
let queue = Arc::new(Mutex::new(VecDeque::new()));
158157
let waker = Arc::new(Mutex::new(None));
159158
let notifier = Condvar::new();
@@ -228,13 +227,13 @@ where
228227
}
229228
}
230229

231-
impl<K: KVStore + Sync + Send, L: Deref> ReadableArgs<(Arc<K>, L)> for EventQueue<K, L>
230+
impl<L: Deref> ReadableArgs<(Arc<DynStore>, L)> for EventQueue<L>
232231
where
233232
L::Target: Logger,
234233
{
235234
#[inline]
236235
fn read<R: lightning::io::Read>(
237-
reader: &mut R, args: (Arc<K>, L),
236+
reader: &mut R, args: (Arc<DynStore>, L),
238237
) -> Result<Self, lightning::ln::msgs::DecodeError> {
239238
let (kv_store, logger) = args;
240239
let read_queue: EventQueueDeserWrapper = Readable::read(reader)?;
@@ -292,32 +291,31 @@ impl Future for EventFuture {
292291
}
293292
}
294293

295-
pub(crate) struct EventHandler<K: KVStore + Sync + Send, L: Deref>
294+
pub(crate) struct EventHandler<L: Deref>
296295
where
297296
L::Target: Logger,
298297
{
299-
event_queue: Arc<EventQueue<K, L>>,
298+
event_queue: Arc<EventQueue<L>>,
300299
wallet: Arc<Wallet>,
301-
channel_manager: Arc<ChannelManager<K>>,
302-
output_sweeper: Arc<Sweeper<K>>,
300+
channel_manager: Arc<ChannelManager>,
301+
output_sweeper: Arc<Sweeper>,
303302
network_graph: Arc<NetworkGraph>,
304-
payment_store: Arc<PaymentStore<K, L>>,
305-
peer_store: Arc<PeerStore<K, L>>,
303+
payment_store: Arc<PaymentStore<L>>,
304+
peer_store: Arc<PeerStore<L>>,
306305
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
307306
logger: L,
308307
config: Arc<Config>,
309308
}
310309

311-
impl<K: KVStore + Sync + Send + 'static, L: Deref> EventHandler<K, L>
310+
impl<L: Deref> EventHandler<L>
312311
where
313312
L::Target: Logger,
314313
{
315314
pub fn new(
316-
event_queue: Arc<EventQueue<K, L>>, wallet: Arc<Wallet>,
317-
channel_manager: Arc<ChannelManager<K>>, output_sweeper: Arc<Sweeper<K>>,
318-
network_graph: Arc<NetworkGraph>, payment_store: Arc<PaymentStore<K, L>>,
319-
peer_store: Arc<PeerStore<K, L>>, runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
320-
logger: L, config: Arc<Config>,
315+
event_queue: Arc<EventQueue<L>>, wallet: Arc<Wallet>, channel_manager: Arc<ChannelManager>,
316+
output_sweeper: Arc<Sweeper>, network_graph: Arc<NetworkGraph>,
317+
payment_store: Arc<PaymentStore<L>>, peer_store: Arc<PeerStore<L>>,
318+
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, logger: L, config: Arc<Config>,
321319
) -> Self {
322320
Self {
323321
event_queue,
@@ -933,7 +931,7 @@ mod tests {
933931

934932
#[tokio::test]
935933
async fn event_queue_persistence() {
936-
let store = Arc::new(TestStore::new(false));
934+
let store: Arc<DynStore> = Arc::new(TestStore::new(false));
937935
let logger = Arc::new(TestLogger::new());
938936
let event_queue = Arc::new(EventQueue::new(Arc::clone(&store), Arc::clone(&logger)));
939937
assert_eq!(event_queue.next_event(), None);
@@ -970,7 +968,7 @@ mod tests {
970968

971969
#[tokio::test]
972970
async fn event_queue_concurrency() {
973-
let store = Arc::new(TestStore::new(false));
971+
let store: Arc<DynStore> = Arc::new(TestStore::new(false));
974972
let logger = Arc::new(TestLogger::new());
975973
let event_queue = Arc::new(EventQueue::new(Arc::clone(&store), Arc::clone(&logger)));
976974
assert_eq!(event_queue.next_event(), None);

0 commit comments

Comments
 (0)