Skip to content

Commit 2d85409

Browse files
committed
Have Node take a genric KVStore parameter
Previously, we had `Node` take a concrete `FilesystemStore` struct. Here we switch to a generic `KVStore` type parameter. To this end we switched from `K: Deref` to concrete `Arc<K>` in all of the modules to avoid confusion of the type paramters or requirements to track `Arc`-ed and non-`Arc`ed version of the `K: KVStore` paramter. Moreover, as Uniffi doesn't support exposing generics we now expose a concretized `LDKNode` type alias in bindings, which will use `SqliteStore` in the future. Note that going the generic route was necessary as `dyn KVStore` wasn't an opion due to the incompatibility of `dyn` with associated types. In this case the incompatibility is in regard to `KVStore::Reader` and I opted to go this route over forcing any implementation of the trait to allocating and returning the same concrete value (e.g., `Vec<u8>`) which could potentially have significant performance impacts over returning a buffered reader for example.
1 parent fbe1bb4 commit 2d85409

File tree

9 files changed

+104
-125
lines changed

9 files changed

+104
-125
lines changed

bindings/ldk_node.udl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ interface Builder {
1313
constructor();
1414
[Name=from_config]
1515
constructor(Config config);
16-
Node build();
16+
LDKNode build();
1717
};
1818

19-
interface Node {
19+
interface LDKNode {
2020
[Throws=NodeError]
2121
void start();
2222
[Throws=NodeError]

src/event.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,21 @@ impl_writeable_tlv_based_enum!(Event,
106106
};
107107
);
108108

109-
pub struct EventQueue<K: Deref, L: Deref>
109+
pub struct EventQueue<K: KVStore + Sync + Send, L: Deref>
110110
where
111-
K::Target: KVStore,
112111
L::Target: Logger,
113112
{
114113
queue: Mutex<VecDeque<Event>>,
115114
notifier: Condvar,
116-
kv_store: K,
115+
kv_store: Arc<K>,
117116
logger: L,
118117
}
119118

120-
impl<K: Deref, L: Deref> EventQueue<K, L>
119+
impl<K: KVStore + Sync + Send, L: Deref> EventQueue<K, L>
121120
where
122-
K::Target: KVStore,
123121
L::Target: Logger,
124122
{
125-
pub(crate) fn new(kv_store: K, logger: L) -> Self {
123+
pub(crate) fn new(kv_store: Arc<K>, logger: L) -> Self {
126124
let queue: Mutex<VecDeque<Event>> = Mutex::new(VecDeque::new());
127125
let notifier = Condvar::new();
128126
Self { queue, notifier, kv_store, logger }
@@ -178,14 +176,13 @@ where
178176
}
179177
}
180178

181-
impl<K: Deref, L: Deref> ReadableArgs<(K, L)> for EventQueue<K, L>
179+
impl<K: KVStore + Sync + Send, L: Deref> ReadableArgs<(Arc<K>, L)> for EventQueue<K, L>
182180
where
183-
K::Target: KVStore,
184181
L::Target: Logger,
185182
{
186183
#[inline]
187184
fn read<R: lightning::io::Read>(
188-
reader: &mut R, args: (K, L),
185+
reader: &mut R, args: (Arc<K>, L),
189186
) -> Result<Self, lightning::ln::msgs::DecodeError> {
190187
let (kv_store, logger) = args;
191188
let read_queue: EventQueueDeserWrapper = Readable::read(reader)?;
@@ -222,14 +219,13 @@ impl Writeable for EventQueueSerWrapper<'_> {
222219
}
223220
}
224221

225-
pub(crate) struct EventHandler<K: Deref + Clone, L: Deref>
222+
pub(crate) struct EventHandler<K: KVStore + Sync + Send, L: Deref>
226223
where
227-
K::Target: KVStore,
228224
L::Target: Logger,
229225
{
230226
wallet: Arc<Wallet<bdk::database::SqliteDatabase>>,
231227
event_queue: Arc<EventQueue<K, L>>,
232-
channel_manager: Arc<ChannelManager>,
228+
channel_manager: Arc<ChannelManager<K>>,
233229
network_graph: Arc<NetworkGraph>,
234230
keys_manager: Arc<KeysManager>,
235231
payment_store: Arc<PaymentStore<K, L>>,
@@ -238,14 +234,13 @@ where
238234
_config: Arc<Config>,
239235
}
240236

241-
impl<K: Deref + Clone, L: Deref> EventHandler<K, L>
237+
impl<K: KVStore + Sync + Send + 'static, L: Deref> EventHandler<K, L>
242238
where
243-
K::Target: KVStore,
244239
L::Target: Logger,
245240
{
246241
pub fn new(
247242
wallet: Arc<Wallet<bdk::database::SqliteDatabase>>, event_queue: Arc<EventQueue<K, L>>,
248-
channel_manager: Arc<ChannelManager>, network_graph: Arc<NetworkGraph>,
243+
channel_manager: Arc<ChannelManager<K>>, network_graph: Arc<NetworkGraph>,
249244
keys_manager: Arc<KeysManager>, payment_store: Arc<PaymentStore<K, L>>,
250245
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, logger: L, _config: Arc<Config>,
251246
) -> Self {

src/io/fs_store.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ fn path_to_windows_str<T: AsRef<OsStr>>(path: T) -> Vec<winapi::shared::ntdef::W
3838
path.as_ref().encode_wide().chain(Some(0)).collect()
3939
}
4040

41+
/// A [`KVStore`] implementation that writes to and reads from the file system.
4142
pub struct FilesystemStore {
4243
dest_dir: PathBuf,
4344
locks: Mutex<HashMap<(String, String), Arc<RwLock<()>>>>,
4445
}
4546

4647
impl FilesystemStore {
47-
pub fn new(dest_dir: PathBuf) -> Self {
48+
pub(crate) fn new(dest_dir: PathBuf) -> Self {
4849
let locks = Mutex::new(HashMap::new());
4950
Self { dest_dir, locks }
5051
}

src/io/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pub(crate) mod fs_store;
22
pub(crate) mod utils;
33

4+
pub use fs_store::FilesystemStore;
5+
46
use lightning::util::persist::KVStorePersister;
57

68
use std::io::Read;
@@ -33,8 +35,8 @@ pub(crate) const LATEST_RGS_SYNC_TIMESTAMP_NAMESPACE: &str = "";
3335
pub(crate) const LATEST_RGS_SYNC_TIMESTAMP_KEY: &str = "latest_rgs_sync_timestamp";
3436

3537
/// The last time we broadcast a node announcement will be persisted under this key.
36-
pub(crate) const LATEST_NODE_ANN_BCAST_TIMSTAMP_NAMESPACE: &str = "";
37-
pub(crate) const LATEST_NODE_ANN_BCAST_TIMSTAMP_KEY: &str = "latest_node_ann_bcast_timestamp";
38+
pub(crate) const LATEST_NODE_ANN_BCAST_TIMESTAMP_NAMESPACE: &str = "";
39+
pub(crate) const LATEST_NODE_ANN_BCAST_TIMESTAMP_KEY: &str = "latest_node_ann_bcast_timestamp";
3840

3941
/// Provides an interface that allows to store and retrieve persisted values that are associated
4042
/// with given keys.

src/io/utils.rs

Lines changed: 36 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::fs;
2020
use std::io::Write;
2121
use std::ops::Deref;
2222
use std::path::Path;
23+
use std::sync::Arc;
2324

2425
use super::KVStore;
2526

@@ -46,11 +47,10 @@ pub(crate) fn read_or_generate_seed_file(keys_seed_path: &str) -> [u8; WALLET_KE
4647
}
4748

4849
/// Read previously persisted [`ChannelMonitor`]s from the store.
49-
pub(crate) fn read_channel_monitors<K: Deref, ES: Deref, SP: Deref>(
50-
kv_store: K, entropy_source: ES, signer_provider: SP,
50+
pub(crate) fn read_channel_monitors<K: KVStore + Sync + Send, ES: Deref, SP: Deref>(
51+
kv_store: Arc<K>, entropy_source: ES, signer_provider: SP,
5152
) -> std::io::Result<Vec<(BlockHash, ChannelMonitor<<SP::Target as SignerProvider>::Signer>)>>
5253
where
53-
K::Target: KVStore,
5454
ES::Target: EntropySource + Sized,
5555
SP::Target: SignerProvider + Sized,
5656
{
@@ -92,11 +92,10 @@ where
9292
}
9393

9494
/// Read a previously persisted [`NetworkGraph`] from the store.
95-
pub(crate) fn read_network_graph<K: Deref, L: Deref>(
96-
kv_store: K, logger: L,
95+
pub(crate) fn read_network_graph<K: KVStore + Sync + Send, L: Deref>(
96+
kv_store: Arc<K>, logger: L,
9797
) -> Result<NetworkGraph<L>, std::io::Error>
9898
where
99-
K::Target: KVStore,
10099
L::Target: Logger,
101100
{
102101
let mut reader =
@@ -108,11 +107,10 @@ where
108107
}
109108

110109
/// Read a previously persisted [`Scorer`] from the store.
111-
pub(crate) fn read_scorer<K: Deref, G: Deref<Target = NetworkGraph<L>>, L: Deref>(
112-
kv_store: K, network_graph: G, logger: L,
110+
pub(crate) fn read_scorer<K: KVStore + Sync + Send, G: Deref<Target = NetworkGraph<L>>, L: Deref>(
111+
kv_store: Arc<K>, network_graph: G, logger: L,
113112
) -> Result<ProbabilisticScorer<G, L>, std::io::Error>
114113
where
115-
K::Target: KVStore,
116114
L::Target: Logger,
117115
{
118116
let params = ProbabilisticScoringParameters::default();
@@ -125,11 +123,10 @@ where
125123
}
126124

127125
/// Read previously persisted events from the store.
128-
pub(crate) fn read_event_queue<K: Deref, L: Deref>(
129-
kv_store: K, logger: L,
126+
pub(crate) fn read_event_queue<K: KVStore + Sync + Send, L: Deref>(
127+
kv_store: Arc<K>, logger: L,
130128
) -> Result<EventQueue<K, L>, std::io::Error>
131129
where
132-
K::Target: KVStore,
133130
L::Target: Logger,
134131
{
135132
let mut reader =
@@ -141,11 +138,10 @@ where
141138
}
142139

143140
/// Read previously persisted peer info from the store.
144-
pub(crate) fn read_peer_info<K: Deref, L: Deref>(
145-
kv_store: K, logger: L,
141+
pub(crate) fn read_peer_info<K: KVStore + Sync + Send, L: Deref>(
142+
kv_store: Arc<K>, logger: L,
146143
) -> Result<PeerStore<K, L>, std::io::Error>
147144
where
148-
K::Target: KVStore,
149145
L::Target: Logger,
150146
{
151147
let mut reader = kv_store.read(PEER_INFO_PERSISTENCE_NAMESPACE, PEER_INFO_PERSISTENCE_KEY)?;
@@ -156,10 +152,9 @@ where
156152
}
157153

158154
/// Read previously persisted payments information from the store.
159-
pub(crate) fn read_payments<K: Deref>(kv_store: K) -> Result<Vec<PaymentDetails>, std::io::Error>
160-
where
161-
K::Target: KVStore,
162-
{
155+
pub(crate) fn read_payments<K: KVStore + Sync + Send>(
156+
kv_store: Arc<K>,
157+
) -> Result<Vec<PaymentDetails>, std::io::Error> {
163158
let mut res = Vec::new();
164159

165160
for stored_key in kv_store.list(PAYMENT_INFO_PERSISTENCE_NAMESPACE)? {
@@ -174,10 +169,9 @@ where
174169
Ok(res)
175170
}
176171

177-
pub(crate) fn read_latest_rgs_sync_timestamp<K: Deref>(kv_store: K) -> Result<u32, std::io::Error>
178-
where
179-
K::Target: KVStore,
180-
{
172+
pub(crate) fn read_latest_rgs_sync_timestamp<K: KVStore + Sync + Send>(
173+
kv_store: Arc<K>,
174+
) -> Result<u32, std::io::Error> {
181175
let mut reader =
182176
kv_store.read(LATEST_RGS_SYNC_TIMESTAMP_NAMESPACE, LATEST_RGS_SYNC_TIMESTAMP_KEY)?;
183177
u32::read(&mut reader).map_err(|_| {
@@ -188,11 +182,10 @@ where
188182
})
189183
}
190184

191-
pub(crate) fn write_latest_rgs_sync_timestamp<K: Deref, L: Deref>(
192-
updated_timestamp: u32, kv_store: K, logger: L,
185+
pub(crate) fn write_latest_rgs_sync_timestamp<K: KVStore + Sync + Send, L: Deref>(
186+
updated_timestamp: u32, kv_store: Arc<K>, logger: L,
193187
) -> Result<(), Error>
194188
where
195-
K::Target: KVStore,
196189
L::Target: Logger,
197190
{
198191
let data = updated_timestamp.encode();
@@ -211,14 +204,11 @@ where
211204
Ok(())
212205
}
213206

214-
pub(crate) fn read_latest_node_ann_bcast_timestamp<K: Deref>(
215-
kv_store: K,
216-
) -> Result<u64, std::io::Error>
217-
where
218-
K::Target: KVStore,
219-
{
207+
pub(crate) fn read_latest_node_ann_bcast_timestamp<K: KVStore + Sync + Send>(
208+
kv_store: Arc<K>,
209+
) -> Result<u64, std::io::Error> {
220210
let mut reader = kv_store
221-
.read(LATEST_NODE_ANN_BCAST_TIMSTAMP_NAMESPACE, LATEST_NODE_ANN_BCAST_TIMSTAMP_KEY)?;
211+
.read(LATEST_NODE_ANN_BCAST_TIMESTAMP_NAMESPACE, LATEST_NODE_ANN_BCAST_TIMESTAMP_KEY)?;
222212
u64::read(&mut reader).map_err(|_| {
223213
std::io::Error::new(
224214
std::io::ErrorKind::InvalidData,
@@ -227,43 +217,28 @@ where
227217
})
228218
}
229219

230-
pub(crate) fn write_latest_node_ann_bcast_timestamp<K: Deref, L: Deref>(
231-
updated_timestamp: u64, kv_store: K, logger: L,
220+
pub(crate) fn write_latest_node_ann_bcast_timestamp<K: KVStore + Sync + Send, L: Deref>(
221+
updated_timestamp: u64, kv_store: Arc<K>, logger: L,
232222
) -> Result<(), Error>
233223
where
234-
K::Target: KVStore,
235224
L::Target: Logger,
236225
{
237-
let mut writer = kv_store
238-
.write(LATEST_NODE_ANN_BCAST_TIMSTAMP_NAMESPACE, LATEST_NODE_ANN_BCAST_TIMSTAMP_KEY)
226+
let data = updated_timestamp.encode();
227+
kv_store
228+
.write(
229+
LATEST_NODE_ANN_BCAST_TIMESTAMP_NAMESPACE,
230+
LATEST_NODE_ANN_BCAST_TIMESTAMP_KEY,
231+
&data,
232+
)
239233
.map_err(|e| {
240234
log_error!(
241235
logger,
242-
"Getting writer for key {}/{} failed due to: {}",
243-
LATEST_NODE_ANN_BCAST_TIMSTAMP_NAMESPACE,
244-
LATEST_NODE_ANN_BCAST_TIMSTAMP_KEY,
236+
"Writing data to key {}/{} failed due to: {}",
237+
LATEST_NODE_ANN_BCAST_TIMESTAMP_NAMESPACE,
238+
LATEST_NODE_ANN_BCAST_TIMESTAMP_KEY,
245239
e
246240
);
247241
Error::PersistenceFailed
248242
})?;
249-
updated_timestamp.write(&mut writer).map_err(|e| {
250-
log_error!(
251-
logger,
252-
"Writing data to key {}/{} failed due to: {}",
253-
LATEST_NODE_ANN_BCAST_TIMSTAMP_NAMESPACE,
254-
LATEST_NODE_ANN_BCAST_TIMSTAMP_KEY,
255-
e
256-
);
257-
Error::PersistenceFailed
258-
})?;
259-
writer.commit().map_err(|e| {
260-
log_error!(
261-
logger,
262-
"Committing data to key {}/{} failed due to: {}",
263-
LATEST_NODE_ANN_BCAST_TIMSTAMP_NAMESPACE,
264-
LATEST_NODE_ANN_BCAST_TIMSTAMP_KEY,
265-
e
266-
);
267-
Error::PersistenceFailed
268-
})
243+
Ok(())
269244
}

0 commit comments

Comments
 (0)