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;
15 use crate::{chain::{keysinterface::{Sign, KeysInterface}, self, transaction::{OutPoint}, chaininterface::{BroadcasterInterface, FeeEstimator}, chainmonitor::{Persist, MonitorUpdateId}, channelmonitor::{ChannelMonitor, ChannelMonitorUpdate}}, ln::channelmanager::ChannelManager, routing::network_graph::NetworkGraph};
16 use super::{logger::Logger, ser::Writeable};
18 /// Trait for a key-value store for persisting some writeable object at some key
19 /// Implementing `KVStorePersister` provides auto-implementations for [`Persister`]
20 /// and [`Persist`] traits. It uses "manager", "network_graph",
21 /// and "monitors/{funding_txo_id}_{funding_txo_index}" for keys.
22 pub trait KVStorePersister {
23 /// Persist the given writeable using the provided key
24 fn persist<W: Writeable>(&self, key: &str, object: &W) -> io::Result<()>;
27 /// Trait that handles persisting a [`ChannelManager`] and [`NetworkGraph`] to disk.
28 pub trait Persister<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
29 where M::Target: 'static + chain::Watch<Signer>,
30 T::Target: 'static + BroadcasterInterface,
31 K::Target: 'static + KeysInterface<Signer = Signer>,
32 F::Target: 'static + FeeEstimator,
33 L::Target: 'static + Logger,
35 /// Persist the given ['ChannelManager'] to disk, returning an error if persistence failed.
36 fn persist_manager(&self, channel_manager: &ChannelManager<Signer, M, T, K, F, L>) -> Result<(), io::Error>;
38 /// Persist the given [`NetworkGraph`] to disk, returning an error if persistence failed.
39 fn persist_graph(&self, network_graph: &NetworkGraph) -> Result<(), io::Error>;
42 impl<A: KVStorePersister, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Persister<Signer, M, T, K, F, L> for A
43 where M::Target: 'static + chain::Watch<Signer>,
44 T::Target: 'static + BroadcasterInterface,
45 K::Target: 'static + KeysInterface<Signer = Signer>,
46 F::Target: 'static + FeeEstimator,
47 L::Target: 'static + Logger,
49 /// Persist the given ['ChannelManager'] to disk, returning an error if persistence failed.
50 fn persist_manager(&self, channel_manager: &ChannelManager<Signer, M, T, K, F, L>) -> Result<(), io::Error> {
51 self.persist("manager", channel_manager)
54 /// Persist the given [`NetworkGraph`] to disk, returning an error if persistence failed.
55 fn persist_graph(&self, network_graph: &NetworkGraph) -> Result<(), io::Error> {
56 self.persist("network_graph", network_graph)
60 impl<ChannelSigner: Sign, K: KVStorePersister> Persist<ChannelSigner> for K {
61 // TODO: We really need a way for the persister to inform the user that its time to crash/shut
62 // down once these start returning failure.
63 // A PermanentFailure implies we need to shut down since we're force-closing channels without
66 fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
67 let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
68 self.persist(&key, monitor)
69 .map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)
72 fn update_persisted_channel(&self, funding_txo: OutPoint, _update: &Option<ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
73 let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
74 self.persist(&key, monitor)
75 .map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)