Skip to content

Commit 140322d

Browse files
committed
Drop KVStore generic from Node
.. switching to `dyn KVStore + Send + Sync`
1 parent 987bce8 commit 140322d

File tree

12 files changed

+140
-152
lines changed

12 files changed

+140
-152
lines changed

src/builder.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use crate::peer_store::PeerStore;
1515
use crate::sweep::OutputSweeper;
1616
use crate::tx_broadcaster::TransactionBroadcaster;
1717
use crate::types::{
18-
ChainMonitor, ChannelManager, FakeMessageRouter, GossipSync, KeysManager, NetworkGraph,
19-
OnionMessenger, PeerManager,
18+
ChainMonitor, ChannelManager, DynStore, FakeMessageRouter, GossipSync, KeysManager,
19+
NetworkGraph, OnionMessenger, PeerManager,
2020
};
2121
use crate::wallet::Wallet;
2222
use crate::{LogLevel, Node};
@@ -33,7 +33,7 @@ use lightning::sign::EntropySource;
3333

3434
use lightning::util::config::UserConfig;
3535
use lightning::util::persist::{
36-
read_channel_monitors, KVStore, CHANNEL_MANAGER_PERSISTENCE_KEY,
36+
read_channel_monitors, CHANNEL_MANAGER_PERSISTENCE_KEY,
3737
CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE,
3838
};
3939
use lightning::util::ser::ReadableArgs;
@@ -114,12 +114,18 @@ pub enum BuildError {
114114
/// The given listening addresses are invalid, e.g. too many were passed.
115115
InvalidListeningAddresses,
116116
/// We failed to read data from the [`KVStore`].
117+
///
118+
/// [`KVStore`]: lightning::util::persist::KVStore
117119
ReadFailed,
118120
/// We failed to write data to the [`KVStore`].
121+
///
122+
/// [`KVStore`]: lightning::util::persist::KVStore
119123
WriteFailed,
120124
/// We failed to access the given `storage_dir_path`.
121125
StoragePathAccessFailed,
122126
/// We failed to setup our [`KVStore`].
127+
///
128+
/// [`KVStore`]: lightning::util::persist::KVStore
123129
KVStoreSetupFailed,
124130
/// We failed to setup the onchain wallet.
125131
WalletSetupFailed,
@@ -298,7 +304,7 @@ impl NodeBuilder {
298304

299305
/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
300306
/// previously configured.
301-
pub fn build(&self) -> Result<Node<SqliteStore>, BuildError> {
307+
pub fn build(&self) -> Result<Node, BuildError> {
302308
let storage_dir_path = self.config.storage_dir_path.clone();
303309
fs::create_dir_all(storage_dir_path.clone())
304310
.map_err(|_| BuildError::StoragePathAccessFailed)?;
@@ -315,7 +321,7 @@ impl NodeBuilder {
315321

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

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

336340
let seed_bytes = seed_bytes_from_config(
@@ -368,9 +372,7 @@ impl NodeBuilder {
368372
}
369373

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

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

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

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

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

605605
// Initialize the ChainMonitor
606-
let chain_monitor: Arc<ChainMonitor<K>> = Arc::new(chainmonitor::ChainMonitor::new(
606+
let chain_monitor: Arc<ChainMonitor> = Arc::new(chainmonitor::ChainMonitor::new(
607607
Some(Arc::clone(&tx_sync)),
608608
Arc::clone(&tx_broadcaster),
609609
Arc::clone(&logger),
@@ -734,7 +734,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
734734
channel_monitor_references,
735735
);
736736
let (_hash, channel_manager) =
737-
<(BlockHash, ChannelManager<K>)>::read(&mut reader, read_args).map_err(|e| {
737+
<(BlockHash, ChannelManager)>::read(&mut reader, read_args).map_err(|e| {
738738
log_error!(logger, "Failed to read channel manager from KVStore: {}", e);
739739
BuildError::ReadFailed
740740
})?;

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;
@@ -135,22 +134,22 @@ impl_writeable_tlv_based_enum!(Event,
135134
};
136135
);
137136

138-
pub struct EventQueue<K: KVStore + Sync + Send, L: Deref>
137+
pub struct EventQueue<L: Deref>
139138
where
140139
L::Target: Logger,
141140
{
142141
queue: Arc<Mutex<VecDeque<Event>>>,
143142
waker: Arc<Mutex<Option<Waker>>>,
144143
notifier: Condvar,
145-
kv_store: Arc<K>,
144+
kv_store: Arc<DynStore>,
146145
logger: L,
147146
}
148147

149-
impl<K: KVStore + Sync + Send, L: Deref> EventQueue<K, L>
148+
impl<L: Deref> EventQueue<L>
150149
where
151150
L::Target: Logger,
152151
{
153-
pub(crate) fn new(kv_store: Arc<K>, logger: L) -> Self {
152+
pub(crate) fn new(kv_store: Arc<DynStore>, logger: L) -> Self {
154153
let queue = Arc::new(Mutex::new(VecDeque::new()));
155154
let waker = Arc::new(Mutex::new(None));
156155
let notifier = Condvar::new();
@@ -225,13 +224,13 @@ where
225224
}
226225
}
227226

228-
impl<K: KVStore + Sync + Send, L: Deref> ReadableArgs<(Arc<K>, L)> for EventQueue<K, L>
227+
impl<L: Deref> ReadableArgs<(Arc<DynStore>, L)> for EventQueue<L>
229228
where
230229
L::Target: Logger,
231230
{
232231
#[inline]
233232
fn read<R: lightning::io::Read>(
234-
reader: &mut R, args: (Arc<K>, L),
233+
reader: &mut R, args: (Arc<DynStore>, L),
235234
) -> Result<Self, lightning::ln::msgs::DecodeError> {
236235
let (kv_store, logger) = args;
237236
let read_queue: EventQueueDeserWrapper = Readable::read(reader)?;
@@ -289,32 +288,31 @@ impl Future for EventFuture {
289288
}
290289
}
291290

292-
pub(crate) struct EventHandler<K: KVStore + Sync + Send, L: Deref>
291+
pub(crate) struct EventHandler<L: Deref>
293292
where
294293
L::Target: Logger,
295294
{
296-
event_queue: Arc<EventQueue<K, L>>,
295+
event_queue: Arc<EventQueue<L>>,
297296
wallet: Arc<Wallet>,
298-
channel_manager: Arc<ChannelManager<K>>,
299-
output_sweeper: Arc<Sweeper<K>>,
297+
channel_manager: Arc<ChannelManager>,
298+
output_sweeper: Arc<Sweeper>,
300299
network_graph: Arc<NetworkGraph>,
301-
payment_store: Arc<PaymentStore<K, L>>,
302-
peer_store: Arc<PeerStore<K, L>>,
300+
payment_store: Arc<PaymentStore<L>>,
301+
peer_store: Arc<PeerStore<L>>,
303302
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
304303
logger: L,
305304
config: Arc<Config>,
306305
}
307306

308-
impl<K: KVStore + Sync + Send + 'static, L: Deref> EventHandler<K, L>
307+
impl<L: Deref> EventHandler<L>
309308
where
310309
L::Target: Logger,
311310
{
312311
pub fn new(
313-
event_queue: Arc<EventQueue<K, L>>, wallet: Arc<Wallet>,
314-
channel_manager: Arc<ChannelManager<K>>, output_sweeper: Arc<Sweeper<K>>,
315-
network_graph: Arc<NetworkGraph>, payment_store: Arc<PaymentStore<K, L>>,
316-
peer_store: Arc<PeerStore<K, L>>, runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
317-
logger: L, config: Arc<Config>,
312+
event_queue: Arc<EventQueue<L>>, wallet: Arc<Wallet>, channel_manager: Arc<ChannelManager>,
313+
output_sweeper: Arc<Sweeper>, network_graph: Arc<NetworkGraph>,
314+
payment_store: Arc<PaymentStore<L>>, peer_store: Arc<PeerStore<L>>,
315+
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, logger: L, config: Arc<Config>,
318316
) -> Self {
319317
Self {
320318
event_queue,
@@ -888,7 +886,7 @@ mod tests {
888886

889887
#[tokio::test]
890888
async fn event_queue_persistence() {
891-
let store = Arc::new(TestStore::new(false));
889+
let store: Arc<DynStore> = Arc::new(TestStore::new(false));
892890
let logger = Arc::new(TestLogger::new());
893891
let event_queue = Arc::new(EventQueue::new(Arc::clone(&store), Arc::clone(&logger)));
894892
assert_eq!(event_queue.next_event(), None);
@@ -925,7 +923,7 @@ mod tests {
925923

926924
#[tokio::test]
927925
async fn event_queue_concurrency() {
928-
let store = Arc::new(TestStore::new(false));
926+
let store: Arc<DynStore> = Arc::new(TestStore::new(false));
929927
let logger = Arc::new(TestLogger::new());
930928
let event_queue = Arc::new(EventQueue::new(Arc::clone(&store), Arc::clone(&logger)));
931929
assert_eq!(event_queue.next_event(), None);

src/io/utils.rs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ use crate::config::WALLET_KEYS_SEED_LEN;
44
use crate::logger::log_error;
55
use crate::peer_store::PeerStore;
66
use crate::sweep::SpendableOutputInfo;
7+
use crate::types::DynStore;
78
use crate::{Error, EventQueue, PaymentDetails};
89

910
use lightning::routing::gossip::NetworkGraph;
1011
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringDecayParameters};
1112
use lightning::util::logger::Logger;
1213
use lightning::util::persist::{
13-
KVStore, KVSTORE_NAMESPACE_KEY_ALPHABET, KVSTORE_NAMESPACE_KEY_MAX_LEN,
14-
NETWORK_GRAPH_PERSISTENCE_KEY, NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE,
15-
NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE, SCORER_PERSISTENCE_KEY,
16-
SCORER_PERSISTENCE_PRIMARY_NAMESPACE, SCORER_PERSISTENCE_SECONDARY_NAMESPACE,
14+
KVSTORE_NAMESPACE_KEY_ALPHABET, KVSTORE_NAMESPACE_KEY_MAX_LEN, NETWORK_GRAPH_PERSISTENCE_KEY,
15+
NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE, NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE,
16+
SCORER_PERSISTENCE_KEY, SCORER_PERSISTENCE_PRIMARY_NAMESPACE,
17+
SCORER_PERSISTENCE_SECONDARY_NAMESPACE,
1718
};
1819
use lightning::util::ser::{Readable, ReadableArgs, Writeable};
1920
use lightning::util::string::PrintableString;
@@ -93,8 +94,8 @@ where
9394
}
9495

9596
/// Read a previously persisted [`NetworkGraph`] from the store.
96-
pub(crate) fn read_network_graph<K: KVStore + Sync + Send, L: Deref + Clone>(
97-
kv_store: Arc<K>, logger: L,
97+
pub(crate) fn read_network_graph<L: Deref + Clone>(
98+
kv_store: Arc<DynStore>, logger: L,
9899
) -> Result<NetworkGraph<L>, std::io::Error>
99100
where
100101
L::Target: Logger,
@@ -111,12 +112,8 @@ where
111112
}
112113

113114
/// Read a previously persisted [`ProbabilisticScorer`] from the store.
114-
pub(crate) fn read_scorer<
115-
K: KVStore + Send + Sync,
116-
G: Deref<Target = NetworkGraph<L>>,
117-
L: Deref + Clone,
118-
>(
119-
kv_store: Arc<K>, network_graph: G, logger: L,
115+
pub(crate) fn read_scorer<G: Deref<Target = NetworkGraph<L>>, L: Deref + Clone>(
116+
kv_store: Arc<DynStore>, network_graph: G, logger: L,
120117
) -> Result<ProbabilisticScorer<G, L>, std::io::Error>
121118
where
122119
L::Target: Logger,
@@ -135,9 +132,9 @@ where
135132
}
136133

137134
/// Read previously persisted events from the store.
138-
pub(crate) fn read_event_queue<K: KVStore + Sync + Send, L: Deref + Clone>(
139-
kv_store: Arc<K>, logger: L,
140-
) -> Result<EventQueue<K, L>, std::io::Error>
135+
pub(crate) fn read_event_queue<L: Deref + Clone>(
136+
kv_store: Arc<DynStore>, logger: L,
137+
) -> Result<EventQueue<L>, std::io::Error>
141138
where
142139
L::Target: Logger,
143140
{
@@ -153,9 +150,9 @@ where
153150
}
154151

155152
/// Read previously persisted peer info from the store.
156-
pub(crate) fn read_peer_info<K: KVStore + Sync + Send, L: Deref + Clone>(
157-
kv_store: Arc<K>, logger: L,
158-
) -> Result<PeerStore<K, L>, std::io::Error>
153+
pub(crate) fn read_peer_info<L: Deref + Clone>(
154+
kv_store: Arc<DynStore>, logger: L,
155+
) -> Result<PeerStore<L>, std::io::Error>
159156
where
160157
L::Target: Logger,
161158
{
@@ -171,8 +168,8 @@ where
171168
}
172169

173170
/// Read previously persisted payments information from the store.
174-
pub(crate) fn read_payments<K: KVStore + Sync + Send, L: Deref>(
175-
kv_store: Arc<K>, logger: L,
171+
pub(crate) fn read_payments<L: Deref>(
172+
kv_store: Arc<DynStore>, logger: L,
176173
) -> Result<Vec<PaymentDetails>, std::io::Error>
177174
where
178175
L::Target: Logger,
@@ -201,8 +198,8 @@ where
201198
}
202199

203200
/// Read previously persisted spendable output information from the store.
204-
pub(crate) fn read_spendable_outputs<K: KVStore + Sync + Send, L: Deref>(
205-
kv_store: Arc<K>, logger: L,
201+
pub(crate) fn read_spendable_outputs<L: Deref>(
202+
kv_store: Arc<DynStore>, logger: L,
206203
) -> Result<Vec<SpendableOutputInfo>, std::io::Error>
207204
where
208205
L::Target: Logger,
@@ -230,8 +227,8 @@ where
230227
Ok(res)
231228
}
232229

233-
pub(crate) fn read_latest_rgs_sync_timestamp<K: KVStore + Sync + Send, L: Deref>(
234-
kv_store: Arc<K>, logger: L,
230+
pub(crate) fn read_latest_rgs_sync_timestamp<L: Deref>(
231+
kv_store: Arc<DynStore>, logger: L,
235232
) -> Result<u32, std::io::Error>
236233
where
237234
L::Target: Logger,
@@ -250,8 +247,8 @@ where
250247
})
251248
}
252249

253-
pub(crate) fn write_latest_rgs_sync_timestamp<K: KVStore + Sync + Send, L: Deref>(
254-
updated_timestamp: u32, kv_store: Arc<K>, logger: L,
250+
pub(crate) fn write_latest_rgs_sync_timestamp<L: Deref>(
251+
updated_timestamp: u32, kv_store: Arc<DynStore>, logger: L,
255252
) -> Result<(), Error>
256253
where
257254
L::Target: Logger,
@@ -277,8 +274,8 @@ where
277274
})
278275
}
279276

280-
pub(crate) fn read_latest_node_ann_bcast_timestamp<K: KVStore + Sync + Send, L: Deref>(
281-
kv_store: Arc<K>, logger: L,
277+
pub(crate) fn read_latest_node_ann_bcast_timestamp<L: Deref>(
278+
kv_store: Arc<DynStore>, logger: L,
282279
) -> Result<u64, std::io::Error>
283280
where
284281
L::Target: Logger,
@@ -301,8 +298,8 @@ where
301298
})
302299
}
303300

304-
pub(crate) fn write_latest_node_ann_bcast_timestamp<K: KVStore + Sync + Send, L: Deref>(
305-
updated_timestamp: u64, kv_store: Arc<K>, logger: L,
301+
pub(crate) fn write_latest_node_ann_bcast_timestamp<L: Deref>(
302+
updated_timestamp: u64, kv_store: Arc<DynStore>, logger: L,
306303
) -> Result<(), Error>
307304
where
308305
L::Target: Logger,

0 commit comments

Comments
 (0)