]> git.bitcoin.ninja Git - rust-lightning/blob - lightning/src/util/persist.rs
Persist scorer upon update based on event handling
[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::{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 pub trait KVStorePersister {
32         /// Persist the given writeable using the provided key
33         fn persist<W: Writeable>(&self, key: &str, object: &W) -> io::Result<()>;
34 }
35
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,
46 {
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>;
49
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>;
52
53         /// Persist the given [`WriteableScore`] to disk, returning an error if persistence failed.
54         fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error>;
55 }
56
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,
66 {
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)
70         }
71
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)
75         }
76
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)
80         }
81 }
82
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!
88
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,
94                 }
95         }
96
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,
102                 }
103         }
104 }