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 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, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref, S>
30 where M::Target: 'static + chain::Watch<Signer>,
31 T::Target: 'static + BroadcasterInterface,
32 K::Target: 'static + KeysInterface<Signer = Signer>,
33 F::Target: 'static + FeeEstimator,
34 L::Target: 'static + Logger,
35 S: WriteableScore<'a>,
37 /// Persist the given ['ChannelManager'] to disk, returning an error if persistence failed.
38 fn persist_manager(&self, channel_manager: &ChannelManager<Signer, M, T, K, F, L>) -> Result<(), io::Error>;
40 /// Persist the given [`NetworkGraph`] to disk, returning an error if persistence failed.
41 fn persist_graph(&self, network_graph: &NetworkGraph<L>) -> Result<(), io::Error>;
43 /// Persist the given [`WriteableScore`] to disk, returning an error if persistence failed.
44 fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error>;
47 impl<'a, A: KVStorePersister, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref, S> Persister<'a, Signer, M, T, K, F, L, S> for A
48 where M::Target: 'static + chain::Watch<Signer>,
49 T::Target: 'static + BroadcasterInterface,
50 K::Target: 'static + KeysInterface<Signer = Signer>,
51 F::Target: 'static + FeeEstimator,
52 L::Target: 'static + Logger,
53 S: WriteableScore<'a>,
55 /// Persist the given ['ChannelManager'] to disk with the name "manager", returning an error if persistence failed.
56 fn persist_manager(&self, channel_manager: &ChannelManager<Signer, M, T, K, F, L>) -> Result<(), io::Error> {
57 self.persist("manager", channel_manager)
60 /// Persist the given [`NetworkGraph`] to disk with the name "network_graph", returning an error if persistence failed.
61 fn persist_graph(&self, network_graph: &NetworkGraph<L>) -> Result<(), io::Error> {
62 self.persist("network_graph", network_graph)
65 /// Persist the given [`WriteableScore`] to disk with name "scorer", returning an error if persistence failed.
66 fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error> {
67 self.persist("scorer", &scorer)
71 impl<ChannelSigner: Sign, K: KVStorePersister> Persist<ChannelSigner> for K {
72 // TODO: We really need a way for the persister to inform the user that its time to crash/shut
73 // down once these start returning failure.
74 // A PermanentFailure implies we need to shut down since we're force-closing channels without
77 fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
78 let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
79 self.persist(&key, monitor)
80 .map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)
83 fn update_persisted_channel(&self, funding_txo: OutPoint, _update: &Option<ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
84 let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
85 self.persist(&key, monitor)
86 .map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)