Skip to content

Commit a65748f

Browse files
committed
Simplify implementation for KVStore trait objects
The implementation of the Persist and Persister trait for KVStore types can also be used for trait objects if there is no implicit bound of `Sized`.
1 parent beef584 commit a65748f

File tree

1 file changed

+14
-60
lines changed

1 file changed

+14
-60
lines changed

lightning/src/util/persist.rs

+14-60
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ where
158158
}
159159

160160

161-
impl<'a, A: KVStore, CM: Deref, L: Deref, S: WriteableScore<'a>> Persister<'a, CM, L, S> for A
161+
impl<'a, A: KVStore + ?Sized, CM: Deref, L: Deref, S: WriteableScore<'a>> Persister<'a, CM, L, S> for A
162162
where
163163
CM::Target: 'static + AChannelManager,
164164
L::Target: 'static + Logger,
@@ -185,65 +185,7 @@ where
185185
}
186186
}
187187

188-
impl<'a, CM: Deref, L: Deref, S: WriteableScore<'a>> Persister<'a, CM, L, S> for dyn KVStore + Send + Sync
189-
where
190-
CM::Target: 'static + AChannelManager,
191-
L::Target: 'static + Logger,
192-
{
193-
fn persist_manager(&self, channel_manager: &CM) -> Result<(), io::Error> {
194-
self.write(CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE,
195-
CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE,
196-
CHANNEL_MANAGER_PERSISTENCE_KEY,
197-
&channel_manager.get_cm().encode())
198-
}
199-
200-
fn persist_graph(&self, network_graph: &NetworkGraph<L>) -> Result<(), io::Error> {
201-
self.write(NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE,
202-
NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE,
203-
NETWORK_GRAPH_PERSISTENCE_KEY,
204-
&network_graph.encode())
205-
}
206-
207-
fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error> {
208-
self.write(SCORER_PERSISTENCE_PRIMARY_NAMESPACE,
209-
SCORER_PERSISTENCE_SECONDARY_NAMESPACE,
210-
SCORER_PERSISTENCE_KEY,
211-
&scorer.encode())
212-
}
213-
}
214-
215-
impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStore> Persist<ChannelSigner> for K {
216-
// TODO: We really need a way for the persister to inform the user that its time to crash/shut
217-
// down once these start returning failure.
218-
// Then we should return InProgress rather than UnrecoverableError, implying we should probably
219-
// just shut down the node since we're not retrying persistence!
220-
221-
fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
222-
let key = format!("{}_{}", funding_txo.txid.to_string(), funding_txo.index);
223-
match self.write(
224-
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE,
225-
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE,
226-
&key, &monitor.encode())
227-
{
228-
Ok(()) => chain::ChannelMonitorUpdateStatus::Completed,
229-
Err(_) => chain::ChannelMonitorUpdateStatus::UnrecoverableError
230-
}
231-
}
232-
233-
fn update_persisted_channel(&self, funding_txo: OutPoint, _update: Option<&ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
234-
let key = format!("{}_{}", funding_txo.txid.to_string(), funding_txo.index);
235-
match self.write(
236-
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE,
237-
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE,
238-
&key, &monitor.encode())
239-
{
240-
Ok(()) => chain::ChannelMonitorUpdateStatus::Completed,
241-
Err(_) => chain::ChannelMonitorUpdateStatus::UnrecoverableError
242-
}
243-
}
244-
}
245-
246-
impl<ChannelSigner: WriteableEcdsaChannelSigner> Persist<ChannelSigner> for dyn KVStore + Send + Sync {
188+
impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStore + ?Sized> Persist<ChannelSigner> for K {
247189
// TODO: We really need a way for the persister to inform the user that its time to crash/shut
248190
// down once these start returning failure.
249191
// Then we should return InProgress rather than UnrecoverableError, implying we should probably
@@ -901,6 +843,8 @@ mod tests {
901843
use crate::ln::functional_test_utils::*;
902844
use crate::util::test_utils::{self, TestLogger, TestStore};
903845
use crate::{check_added_monitors, check_closed_broadcast};
846+
use crate::sync::Arc;
847+
use crate::util::test_channel_signer::TestChannelSigner;
904848

905849
const EXPECTED_UPDATES_PER_PAYMENT: u64 = 5;
906850

@@ -1241,4 +1185,14 @@ mod tests {
12411185
.read(CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE, monitor_name.as_str(), UpdateName::from(u64::MAX - 1).as_str())
12421186
.is_err());
12431187
}
1188+
1189+
fn persist_fn<P: Deref, ChannelSigner: WriteableEcdsaChannelSigner>(_persist: P) -> bool where P::Target: Persist<ChannelSigner> {
1190+
true
1191+
}
1192+
1193+
#[test]
1194+
fn kvstore_trait_object_usage() {
1195+
let store: Arc<dyn KVStore + Send + Sync> = Arc::new(TestStore::new(false));
1196+
assert!(persist_fn::<_, TestChannelSigner>(store.clone()));
1197+
}
12441198
}

0 commit comments

Comments
 (0)