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