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::{Score, MultiThreadedLockableScore};
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.
24 pub trait KVStorePersister {
25 /// Persist the given writeable using the provided key
26 fn persist<W: Writeable>(&self, key: &str, object: &W) -> io::Result<()>;
29 /// Trait that handles persisting a [`ChannelManager`], [`NetworkGraph`], and [`MultiThreadedLockableScore`] to disk.
30 pub trait Persister<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref, S: Score>
31 where M::Target: 'static + chain::Watch<Signer>,
32 T::Target: 'static + BroadcasterInterface,
33 K::Target: 'static + KeysInterface<Signer = Signer>,
34 F::Target: 'static + FeeEstimator,
35 L::Target: 'static + Logger,
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 [`MultiThreadedLockableScore`] to disk, returning an error if persistence failed.
44 fn persist_scorer(&self, scorer: &MultiThreadedLockableScore<S>) -> Result<(), io::Error>;
47 impl<'a, A: KVStorePersister, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref, S: Score> 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
54 /// Persist the given ['ChannelManager'] to disk with the name "manager", returning an error if persistence failed.
55 fn persist_manager(&self, channel_manager: &ChannelManager<Signer, M, T, K, F, L>) -> Result<(), io::Error> {
56 self.persist("manager", channel_manager)
59 /// Persist the given [`NetworkGraph`] to disk with the name "network_graph", returning an error if persistence failed.
60 fn persist_graph(&self, network_graph: &NetworkGraph<L>) -> Result<(), io::Error> {
61 self.persist("network_graph", network_graph)
64 /// Persist the given [`MultiThreadedLockableScore`] to disk with name "scorer", returning an error if persistence failed.
65 fn persist_scorer(&self, scorer: &MultiThreadedLockableScore<S>) -> Result<(), io::Error> {
66 self.persist("scorer", &scorer)
70 impl<ChannelSigner: Sign, K: KVStorePersister> Persist<ChannelSigner> for K {
71 // TODO: We really need a way for the persister to inform the user that its time to crash/shut
72 // down once these start returning failure.
73 // A PermanentFailure implies we need to shut down since we're force-closing channels without
76 fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
77 let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
78 self.persist(&key, monitor)
79 .map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)
82 fn update_persisted_channel(&self, funding_txo: OutPoint, _update: &Option<ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
83 let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
84 self.persist(&key, monitor)
85 .map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)