Skip to content

Commit 3fd85c8

Browse files
authored
Merge pull request #2883 from tnull/2024-02-dyn-kvstore-blanket-impls
Add blanket `Persist`/`Persister` impls for `dyn KVStore + Send + Sync`
2 parents 73da722 + bf4c729 commit 3fd85c8

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

lightning/src/util/persist.rs

+66
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,41 @@ impl<'a, A: KVStore, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Der
187187
}
188188
}
189189

190+
impl<'a, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref, S: WriteableScore<'a>> Persister<'a, M, T, ES, NS, SP, F, R, L, S> for dyn KVStore + Send + Sync
191+
where M::Target: 'static + chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
192+
T::Target: 'static + BroadcasterInterface,
193+
ES::Target: 'static + EntropySource,
194+
NS::Target: 'static + NodeSigner,
195+
SP::Target: 'static + SignerProvider,
196+
F::Target: 'static + FeeEstimator,
197+
R::Target: 'static + Router,
198+
L::Target: 'static + Logger,
199+
{
200+
/// Persist the given [`ChannelManager`] to disk, returning an error if persistence failed.
201+
fn persist_manager(&self, channel_manager: &ChannelManager<M, T, ES, NS, SP, F, R, L>) -> Result<(), io::Error> {
202+
self.write(CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE,
203+
CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE,
204+
CHANNEL_MANAGER_PERSISTENCE_KEY,
205+
&channel_manager.encode())
206+
}
207+
208+
/// Persist the given [`NetworkGraph`] to disk, returning an error if persistence failed.
209+
fn persist_graph(&self, network_graph: &NetworkGraph<L>) -> Result<(), io::Error> {
210+
self.write(NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE,
211+
NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE,
212+
NETWORK_GRAPH_PERSISTENCE_KEY,
213+
&network_graph.encode())
214+
}
215+
216+
/// Persist the given [`WriteableScore`] to disk, returning an error if persistence failed.
217+
fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error> {
218+
self.write(SCORER_PERSISTENCE_PRIMARY_NAMESPACE,
219+
SCORER_PERSISTENCE_SECONDARY_NAMESPACE,
220+
SCORER_PERSISTENCE_KEY,
221+
&scorer.encode())
222+
}
223+
}
224+
190225
impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStore> Persist<ChannelSigner> for K {
191226
// TODO: We really need a way for the persister to inform the user that its time to crash/shut
192227
// down once these start returning failure.
@@ -218,6 +253,37 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStore> Persist<ChannelSign
218253
}
219254
}
220255

256+
impl<ChannelSigner: WriteableEcdsaChannelSigner> Persist<ChannelSigner> for dyn KVStore + Send + Sync {
257+
// TODO: We really need a way for the persister to inform the user that its time to crash/shut
258+
// down once these start returning failure.
259+
// Then we should return InProgress rather than UnrecoverableError, implying we should probably
260+
// just shut down the node since we're not retrying persistence!
261+
262+
fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
263+
let key = format!("{}_{}", funding_txo.txid.to_string(), funding_txo.index);
264+
match self.write(
265+
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE,
266+
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE,
267+
&key, &monitor.encode())
268+
{
269+
Ok(()) => chain::ChannelMonitorUpdateStatus::Completed,
270+
Err(_) => chain::ChannelMonitorUpdateStatus::UnrecoverableError
271+
}
272+
}
273+
274+
fn update_persisted_channel(&self, funding_txo: OutPoint, _update: Option<&ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
275+
let key = format!("{}_{}", funding_txo.txid.to_string(), funding_txo.index);
276+
match self.write(
277+
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE,
278+
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE,
279+
&key, &monitor.encode())
280+
{
281+
Ok(()) => chain::ChannelMonitorUpdateStatus::Completed,
282+
Err(_) => chain::ChannelMonitorUpdateStatus::UnrecoverableError
283+
}
284+
}
285+
}
286+
221287
/// Read previously persisted [`ChannelMonitor`]s from the store.
222288
pub fn read_channel_monitors<K: Deref, ES: Deref, SP: Deref>(
223289
kv_store: K, entropy_source: ES, signer_provider: SP,

lightning/src/util/test_utils.rs

+3
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,9 @@ impl KVStore for TestStore {
632632
}
633633
}
634634

635+
unsafe impl Sync for TestStore {}
636+
unsafe impl Send for TestStore {}
637+
635638
pub struct TestBroadcaster {
636639
pub txn_broadcasted: Mutex<Vec<Transaction>>,
637640
pub blocks: Arc<Mutex<Vec<(Block, u32)>>>,

0 commit comments

Comments
 (0)