1 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
2 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
4 // You may not use this file except in accordance with one or both of these
7 //! This module contains a simple key-value store trait KVStorePersister that
8 //! allows one to implement the persistence for [`ChannelManager`], [`NetworkGraph`],
9 //! and [`ChannelMonitor`] all in one place.
12 use bitcoin::hashes::hex::ToHex;
14 use crate::routing::scoring::WriteableScore;
17 use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
18 use crate::chain::chainmonitor::{Persist, MonitorUpdateId};
19 use crate::chain::keysinterface::{EntropySource, NodeSigner, WriteableEcdsaChannelSigner, SignerProvider};
20 use crate::chain::transaction::OutPoint;
21 use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate};
22 use crate::ln::channelmanager::ChannelManager;
23 use crate::routing::router::Router;
24 use crate::routing::gossip::NetworkGraph;
25 use super::{logger::Logger, ser::Writeable};
27 /// Trait for a key-value store for persisting some writeable object at some key
28 /// Implementing `KVStorePersister` provides auto-implementations for [`Persister`]
29 /// and [`Persist`] traits. It uses "manager", "network_graph",
30 /// and "monitors/{funding_txo_id}_{funding_txo_index}" for keys.
31 pub trait KVStorePersister {
32 /// Persist the given writeable using the provided key
33 fn persist<W: Writeable>(&self, key: &str, object: &W) -> io::Result<()>;
36 /// Trait that handles persisting a [`ChannelManager`], [`NetworkGraph`], and [`WriteableScore`] to disk.
37 pub trait Persister<'a, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref, S: WriteableScore<'a>>
38 where M::Target: 'static + chain::Watch<<SP::Target as SignerProvider>::Signer>,
39 T::Target: 'static + BroadcasterInterface,
40 ES::Target: 'static + EntropySource,
41 NS::Target: 'static + NodeSigner,
42 SP::Target: 'static + SignerProvider,
43 F::Target: 'static + FeeEstimator,
44 R::Target: 'static + Router,
45 L::Target: 'static + Logger,
47 /// Persist the given ['ChannelManager'] to disk, returning an error if persistence failed.
48 fn persist_manager(&self, channel_manager: &ChannelManager<M, T, ES, NS, SP, F, R, L>) -> Result<(), io::Error>;
50 /// Persist the given [`NetworkGraph`] to disk, returning an error if persistence failed.
51 fn persist_graph(&self, network_graph: &NetworkGraph<L>) -> Result<(), io::Error>;
53 /// Persist the given [`WriteableScore`] to disk, returning an error if persistence failed.
54 fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error>;
57 impl<'a, A: KVStorePersister, 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 A
58 where M::Target: 'static + chain::Watch<<SP::Target as SignerProvider>::Signer>,
59 T::Target: 'static + BroadcasterInterface,
60 ES::Target: 'static + EntropySource,
61 NS::Target: 'static + NodeSigner,
62 SP::Target: 'static + SignerProvider,
63 F::Target: 'static + FeeEstimator,
64 R::Target: 'static + Router,
65 L::Target: 'static + Logger,
67 /// Persist the given ['ChannelManager'] to disk with the name "manager", returning an error if persistence failed.
68 fn persist_manager(&self, channel_manager: &ChannelManager<M, T, ES, NS, SP, F, R, L>) -> Result<(), io::Error> {
69 self.persist("manager", channel_manager)
72 /// Persist the given [`NetworkGraph`] to disk with the name "network_graph", returning an error if persistence failed.
73 fn persist_graph(&self, network_graph: &NetworkGraph<L>) -> Result<(), io::Error> {
74 self.persist("network_graph", network_graph)
77 /// Persist the given [`WriteableScore`] to disk with name "scorer", returning an error if persistence failed.
78 fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error> {
79 self.persist("scorer", &scorer)
83 impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStorePersister> Persist<ChannelSigner> for K {
84 // TODO: We really need a way for the persister to inform the user that its time to crash/shut
85 // down once these start returning failure.
86 // A PermanentFailure implies we should probably just shut down the node since we're
87 // force-closing channels without even broadcasting!
89 fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
90 let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
91 match self.persist(&key, monitor) {
92 Ok(()) => chain::ChannelMonitorUpdateStatus::Completed,
93 Err(_) => chain::ChannelMonitorUpdateStatus::PermanentFailure,
97 fn update_persisted_channel(&self, funding_txo: OutPoint, _update: Option<&ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
98 let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
99 match self.persist(&key, monitor) {
100 Ok(()) => chain::ChannelMonitorUpdateStatus::Completed,
101 Err(_) => chain::ChannelMonitorUpdateStatus::PermanentFailure,