From: Willem Van Lint Date: Thu, 28 Mar 2024 01:16:19 +0000 (-0700) Subject: Simplify implementation for KVStore trait objects X-Git-Tag: v0.0.123-beta~23^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=a65748f613584e950845adeb450f1c9d86495a6d;p=rust-lightning 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`. --- diff --git a/lightning/src/util/persist.rs b/lightning/src/util/persist.rs index 35f5b0c7..a7b4bda6 100644 --- a/lightning/src/util/persist.rs +++ b/lightning/src/util/persist.rs @@ -158,7 +158,7 @@ where } -impl<'a, A: KVStore, CM: Deref, L: Deref, S: WriteableScore<'a>> Persister<'a, CM, L, S> for A +impl<'a, A: KVStore + ?Sized, CM: Deref, L: Deref, S: WriteableScore<'a>> Persister<'a, CM, L, S> for A where CM::Target: 'static + AChannelManager, L::Target: 'static + Logger, @@ -185,65 +185,7 @@ where } } -impl<'a, CM: Deref, L: Deref, S: WriteableScore<'a>> Persister<'a, CM, L, S> for dyn KVStore + Send + Sync -where - CM::Target: 'static + AChannelManager, - L::Target: 'static + Logger, -{ - fn persist_manager(&self, channel_manager: &CM) -> Result<(), io::Error> { - self.write(CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE, - CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE, - CHANNEL_MANAGER_PERSISTENCE_KEY, - &channel_manager.get_cm().encode()) - } - - fn persist_graph(&self, network_graph: &NetworkGraph) -> Result<(), io::Error> { - self.write(NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE, - NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE, - NETWORK_GRAPH_PERSISTENCE_KEY, - &network_graph.encode()) - } - - fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error> { - self.write(SCORER_PERSISTENCE_PRIMARY_NAMESPACE, - SCORER_PERSISTENCE_SECONDARY_NAMESPACE, - SCORER_PERSISTENCE_KEY, - &scorer.encode()) - } -} - -impl Persist for K { - // TODO: We really need a way for the persister to inform the user that its time to crash/shut - // down once these start returning failure. - // Then we should return InProgress rather than UnrecoverableError, implying we should probably - // just shut down the node since we're not retrying persistence! - - fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus { - let key = format!("{}_{}", funding_txo.txid.to_string(), funding_txo.index); - match self.write( - CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE, - CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE, - &key, &monitor.encode()) - { - Ok(()) => chain::ChannelMonitorUpdateStatus::Completed, - Err(_) => chain::ChannelMonitorUpdateStatus::UnrecoverableError - } - } - - fn update_persisted_channel(&self, funding_txo: OutPoint, _update: Option<&ChannelMonitorUpdate>, monitor: &ChannelMonitor, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus { - let key = format!("{}_{}", funding_txo.txid.to_string(), funding_txo.index); - match self.write( - CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE, - CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE, - &key, &monitor.encode()) - { - Ok(()) => chain::ChannelMonitorUpdateStatus::Completed, - Err(_) => chain::ChannelMonitorUpdateStatus::UnrecoverableError - } - } -} - -impl Persist for dyn KVStore + Send + Sync { +impl Persist for K { // TODO: We really need a way for the persister to inform the user that its time to crash/shut // down once these start returning failure. // Then we should return InProgress rather than UnrecoverableError, implying we should probably @@ -901,6 +843,8 @@ mod tests { use crate::ln::functional_test_utils::*; use crate::util::test_utils::{self, TestLogger, TestStore}; use crate::{check_added_monitors, check_closed_broadcast}; + use crate::sync::Arc; + use crate::util::test_channel_signer::TestChannelSigner; const EXPECTED_UPDATES_PER_PAYMENT: u64 = 5; @@ -1241,4 +1185,14 @@ mod tests { .read(CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE, monitor_name.as_str(), UpdateName::from(u64::MAX - 1).as_str()) .is_err()); } + + fn persist_fn(_persist: P) -> bool where P::Target: Persist { + true + } + + #[test] + fn kvstore_trait_object_usage() { + let store: Arc = Arc::new(TestStore::new(false)); + assert!(persist_fn::<_, TestChannelSigner>(store.clone())); + } }