Tag `KVStore` `(C-not exported)` as `Writeable` isn't mapped
[rust-lightning] / lightning / src / util / persist.rs
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
5 // licenses.
6
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.
10
11 use core::ops::Deref;
12 use bitcoin::hashes::hex::ToHex;
13 use crate::io;
14 use crate::routing::scoring::WriteableScore;
15
16 use crate::chain;
17 use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
18 use crate::chain::chainmonitor::{Persist, MonitorUpdateId};
19 use crate::sign::{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};
26
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 /// (C-not exported)
32 pub trait KVStorePersister {
33         /// Persist the given writeable using the provided key
34         fn persist<W: Writeable>(&self, key: &str, object: &W) -> io::Result<()>;
35 }
36
37 /// Trait that handles persisting a [`ChannelManager`], [`NetworkGraph`], and [`WriteableScore`] to disk.
38 pub trait Persister<'a, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref, S: WriteableScore<'a>>
39         where M::Target: 'static + chain::Watch<<SP::Target as SignerProvider>::Signer>,
40                 T::Target: 'static + BroadcasterInterface,
41                 ES::Target: 'static + EntropySource,
42                 NS::Target: 'static + NodeSigner,
43                 SP::Target: 'static + SignerProvider,
44                 F::Target: 'static + FeeEstimator,
45                 R::Target: 'static + Router,
46                 L::Target: 'static + Logger,
47 {
48         /// Persist the given ['ChannelManager'] to disk, returning an error if persistence failed.
49         fn persist_manager(&self, channel_manager: &ChannelManager<M, T, ES, NS, SP, F, R, L>) -> Result<(), io::Error>;
50
51         /// Persist the given [`NetworkGraph`] to disk, returning an error if persistence failed.
52         fn persist_graph(&self, network_graph: &NetworkGraph<L>) -> Result<(), io::Error>;
53
54         /// Persist the given [`WriteableScore`] to disk, returning an error if persistence failed.
55         fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error>;
56 }
57
58 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
59         where M::Target: 'static + chain::Watch<<SP::Target as SignerProvider>::Signer>,
60                 T::Target: 'static + BroadcasterInterface,
61                 ES::Target: 'static + EntropySource,
62                 NS::Target: 'static + NodeSigner,
63                 SP::Target: 'static + SignerProvider,
64                 F::Target: 'static + FeeEstimator,
65                 R::Target: 'static + Router,
66                 L::Target: 'static + Logger,
67 {
68         /// Persist the given ['ChannelManager'] to disk with the name "manager", returning an error if persistence failed.
69         fn persist_manager(&self, channel_manager: &ChannelManager<M, T, ES, NS, SP, F, R, L>) -> Result<(), io::Error> {
70                 self.persist("manager", channel_manager)
71         }
72
73         /// Persist the given [`NetworkGraph`] to disk with the name "network_graph", returning an error if persistence failed.
74         fn persist_graph(&self, network_graph: &NetworkGraph<L>) -> Result<(), io::Error> {
75                 self.persist("network_graph", network_graph)
76         }
77
78         /// Persist the given [`WriteableScore`] to disk with name "scorer", returning an error if persistence failed.
79         fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error> {
80                 self.persist("scorer", &scorer)
81         }
82 }
83
84 impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStorePersister> Persist<ChannelSigner> for K {
85         // TODO: We really need a way for the persister to inform the user that its time to crash/shut
86         // down once these start returning failure.
87         // A PermanentFailure implies we should probably just shut down the node since we're
88         // force-closing channels without even broadcasting!
89
90         fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
91                 let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
92                 match self.persist(&key, monitor) {
93                         Ok(()) => chain::ChannelMonitorUpdateStatus::Completed,
94                         Err(_) => chain::ChannelMonitorUpdateStatus::PermanentFailure,
95                 }
96         }
97
98         fn update_persisted_channel(&self, funding_txo: OutPoint, _update: Option<&ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
99                 let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
100                 match self.persist(&key, monitor) {
101                         Ok(()) => chain::ChannelMonitorUpdateStatus::Completed,
102                         Err(_) => chain::ChannelMonitorUpdateStatus::PermanentFailure,
103                 }
104         }
105 }