Fix weird import format in persist
[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::chain::keysinterface::{Sign, KeysInterface};
20 use crate::chain::transaction::OutPoint;
21 use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate};
22 use crate::ln::channelmanager::ChannelManager;
23 use crate::routing::gossip::NetworkGraph;
24 use super::{logger::Logger, ser::Writeable};
25
26 /// Trait for a key-value store for persisting some writeable object at some key
27 /// Implementing `KVStorePersister` provides auto-implementations for [`Persister`]
28 /// and [`Persist`] traits.  It uses "manager", "network_graph",
29 /// and "monitors/{funding_txo_id}_{funding_txo_index}" for keys.
30 pub trait KVStorePersister {
31         /// Persist the given writeable using the provided key
32         fn persist<W: Writeable>(&self, key: &str, object: &W) -> io::Result<()>;
33 }
34
35 /// Trait that handles persisting a [`ChannelManager`], [`NetworkGraph`], and [`WriteableScore`] to disk.
36 pub trait Persister<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref, S: WriteableScore<'a>>
37         where M::Target: 'static + chain::Watch<<K::Target as KeysInterface>::Signer>,
38                 T::Target: 'static + BroadcasterInterface,
39                 K::Target: 'static + KeysInterface,
40                 F::Target: 'static + FeeEstimator,
41                 L::Target: 'static + Logger,
42 {
43         /// Persist the given ['ChannelManager'] to disk, returning an error if persistence failed.
44         fn persist_manager(&self, channel_manager: &ChannelManager<M, T, K, F, L>) -> Result<(), io::Error>;
45
46         /// Persist the given [`NetworkGraph`] to disk, returning an error if persistence failed.
47         fn persist_graph(&self, network_graph: &NetworkGraph<L>) -> Result<(), io::Error>;
48
49         /// Persist the given [`WriteableScore`] to disk, returning an error if persistence failed.
50         fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error>;
51 }
52
53 impl<'a, A: KVStorePersister, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref, S: WriteableScore<'a>> Persister<'a, M, T, K, F, L, S> for A
54         where M::Target: 'static + chain::Watch<<K::Target as KeysInterface>::Signer>,
55                 T::Target: 'static + BroadcasterInterface,
56                 K::Target: 'static + KeysInterface,
57                 F::Target: 'static + FeeEstimator,
58                 L::Target: 'static + Logger,
59 {
60         /// Persist the given ['ChannelManager'] to disk with the name "manager", returning an error if persistence failed.
61         fn persist_manager(&self, channel_manager: &ChannelManager<M, T, K, F, L>) -> Result<(), io::Error> {
62                 self.persist("manager", channel_manager)
63         }
64
65         /// Persist the given [`NetworkGraph`] to disk with the name "network_graph", returning an error if persistence failed.
66         fn persist_graph(&self, network_graph: &NetworkGraph<L>) -> Result<(), io::Error> {
67                 self.persist("network_graph", network_graph)
68         }
69
70         /// Persist the given [`WriteableScore`] to disk with name "scorer", returning an error if persistence failed.
71         fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error> {
72                 self.persist("scorer", &scorer)
73         }
74 }
75
76 impl<ChannelSigner: Sign, K: KVStorePersister> Persist<ChannelSigner> for K {
77         // TODO: We really need a way for the persister to inform the user that its time to crash/shut
78         // down once these start returning failure.
79         // A PermanentFailure implies we should probably just shut down the node since we're
80         // force-closing channels without even broadcasting!
81
82         fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
83                 let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
84                 match self.persist(&key, monitor) {
85                         Ok(()) => chain::ChannelMonitorUpdateStatus::Completed,
86                         Err(_) => chain::ChannelMonitorUpdateStatus::PermanentFailure,
87                 }
88         }
89
90         fn update_persisted_channel(&self, funding_txo: OutPoint, _update: &Option<ChannelMonitorUpdate>, 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 }