Skip to content

Commit 01c90bb

Browse files
committed
Move ChannelMonitorUpdateErr to chain as it is a chain::Watch val
1 parent 83c53bd commit 01c90bb

File tree

7 files changed

+84
-88
lines changed

7 files changed

+84
-88
lines changed

lightning-persister/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use bitcoin::hashes::hex::{FromHex, ToHex};
1717
use crate::util::DiskWriteable;
1818
use lightning::chain;
1919
use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
20-
use lightning::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr};
20+
use lightning::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate};
2121
use lightning::chain::chainmonitor;
2222
use lightning::chain::keysinterface::{Sign, KeysInterface};
2323
use lightning::chain::transaction::OutPoint;
@@ -165,16 +165,16 @@ impl<ChannelSigner: Sign> chainmonitor::Persist<ChannelSigner> for FilesystemPer
165165
// even broadcasting, and sync_persisted_channel's docs are even more explicit that its time to
166166
// shut down!
167167

168-
fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr> {
168+
fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>) -> Result<(), chain::ChannelMonitorUpdateErr> {
169169
let filename = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
170170
util::write_to_file(self.path_to_monitor_data(), filename, monitor)
171-
.map_err(|_| ChannelMonitorUpdateErr::PermanentFailure)
171+
.map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)
172172
}
173173

174-
fn update_persisted_channel(&self, funding_txo: OutPoint, _update: &ChannelMonitorUpdate, monitor: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr> {
174+
fn update_persisted_channel(&self, funding_txo: OutPoint, _update: &ChannelMonitorUpdate, monitor: &ChannelMonitor<ChannelSigner>) -> Result<(), chain::ChannelMonitorUpdateErr> {
175175
let filename = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
176176
util::write_to_file(self.path_to_monitor_data(), filename, monitor)
177-
.map_err(|_| ChannelMonitorUpdateErr::PermanentFailure)
177+
.map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)
178178
}
179179

180180
fn sync_persisted_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>) -> Result<(), ()> {
@@ -192,8 +192,8 @@ mod tests {
192192
use bitcoin::blockdata::block::{Block, BlockHeader};
193193
use bitcoin::hashes::hex::FromHex;
194194
use bitcoin::Txid;
195+
use lightning::chain::ChannelMonitorUpdateErr;
195196
use lightning::chain::chainmonitor::Persist;
196-
use lightning::chain::channelmonitor::ChannelMonitorUpdateErr;
197197
use lightning::chain::transaction::OutPoint;
198198
use lightning::{check_closed_broadcast, check_closed_event, check_added_monitors};
199199
use lightning::ln::features::InitFeatures;

lightning/src/chain/chainmonitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
2727
use bitcoin::hash_types::Txid;
2828

2929
use chain;
30-
use chain::{Filter, WatchedOutput};
30+
use chain::{ChannelMonitorUpdateErr, Filter, WatchedOutput};
3131
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
32-
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, Balance, MonitorEvent, TransactionOutputs};
32+
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, Balance, MonitorEvent, TransactionOutputs};
3333
use chain::transaction::{OutPoint, TransactionData};
3434
use chain::keysinterface::Sign;
3535
use util::logger::Logger;

lightning/src/chain/channelmonitor.rs

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -115,67 +115,6 @@ impl Readable for ChannelMonitorUpdate {
115115
}
116116
}
117117

118-
/// An error enum representing a failure to persist a channel monitor update.
119-
#[derive(Clone, Copy, Debug, PartialEq)]
120-
pub enum ChannelMonitorUpdateErr {
121-
/// Used to indicate a temporary failure (eg connection to a watchtower or remote backup of
122-
/// our state failed, but is expected to succeed at some point in the future).
123-
///
124-
/// Such a failure will "freeze" a channel, preventing us from revoking old states or
125-
/// submitting new commitment transactions to the counterparty. Once the update(s) which failed
126-
/// have been successfully applied, ChannelManager::channel_monitor_updated can be used to
127-
/// restore the channel to an operational state.
128-
///
129-
/// Note that a given ChannelManager will *never* re-generate a given ChannelMonitorUpdate. If
130-
/// you return a TemporaryFailure you must ensure that it is written to disk safely before
131-
/// writing out the latest ChannelManager state.
132-
///
133-
/// Even when a channel has been "frozen" updates to the ChannelMonitor can continue to occur
134-
/// (eg if an inbound HTLC which we forwarded was claimed upstream resulting in us attempting
135-
/// to claim it on this channel) and those updates must be applied wherever they can be. At
136-
/// least one such updated ChannelMonitor must be persisted otherwise PermanentFailure should
137-
/// be returned to get things on-chain ASAP using only the in-memory copy. Obviously updates to
138-
/// the channel which would invalidate previous ChannelMonitors are not made when a channel has
139-
/// been "frozen".
140-
///
141-
/// Note that even if updates made after TemporaryFailure succeed you must still call
142-
/// channel_monitor_updated to ensure you have the latest monitor and re-enable normal channel
143-
/// operation.
144-
///
145-
/// Note that the update being processed here will not be replayed for you when you call
146-
/// ChannelManager::channel_monitor_updated, so you must store the update itself along
147-
/// with the persisted ChannelMonitor on your own local disk prior to returning a
148-
/// TemporaryFailure. You may, of course, employ a journaling approach, storing only the
149-
/// ChannelMonitorUpdate on disk without updating the monitor itself, replaying the journal at
150-
/// reload-time.
151-
///
152-
/// For deployments where a copy of ChannelMonitors and other local state are backed up in a
153-
/// remote location (with local copies persisted immediately), it is anticipated that all
154-
/// updates will return TemporaryFailure until the remote copies could be updated.
155-
TemporaryFailure,
156-
/// Used to indicate no further channel monitor updates will be allowed (eg we've moved on to a
157-
/// different watchtower and cannot update with all watchtowers that were previously informed
158-
/// of this channel).
159-
///
160-
/// At reception of this error, ChannelManager will force-close the channel and return at
161-
/// least a final ChannelMonitorUpdate::ChannelForceClosed which must be delivered to at
162-
/// least one ChannelMonitor copy. Revocation secret MUST NOT be released and offchain channel
163-
/// update must be rejected.
164-
///
165-
/// This failure may also signal a failure to update the local persisted copy of one of
166-
/// the channel monitor instance.
167-
///
168-
/// Note that even when you fail a holder commitment transaction update, you must store the
169-
/// update to ensure you can claim from it in case of a duplicate copy of this ChannelMonitor
170-
/// broadcasts it (e.g distributed channel-monitor deployment)
171-
///
172-
/// In case of distributed watchtowers deployment, the new version must be written to disk, as
173-
/// state may have been stored but rejected due to a block forcing a commitment broadcast. This
174-
/// storage is used to claim outputs of rejected state confirmed onchain by another watchtower,
175-
/// lagging behind on block processing.
176-
PermanentFailure,
177-
}
178-
179118
/// General Err type for ChannelMonitor actions. Generally, this implies that the data provided is
180119
/// inconsistent with the ChannelMonitor being called. eg for ChannelMonitor::update_monitor this
181120
/// means you tried to update a monitor for a different channel or the ChannelMonitorUpdate was

lightning/src/chain/mod.rs

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use bitcoin::blockdata::transaction::{Transaction, TxOut};
1616
use bitcoin::hash_types::{BlockHash, Txid};
1717
use bitcoin::network::constants::Network;
1818

19-
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, MonitorEvent};
19+
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, MonitorEvent};
2020
use chain::keysinterface::Sign;
2121
use chain::transaction::{OutPoint, TransactionData};
2222

@@ -175,6 +175,67 @@ pub trait Confirm {
175175
fn get_relevant_txids(&self) -> Vec<Txid>;
176176
}
177177

178+
/// An error enum representing a failure to persist a channel monitor update.
179+
#[derive(Clone, Copy, Debug, PartialEq)]
180+
pub enum ChannelMonitorUpdateErr {
181+
/// Used to indicate a temporary failure (eg connection to a watchtower or remote backup of
182+
/// our state failed, but is expected to succeed at some point in the future).
183+
///
184+
/// Such a failure will "freeze" a channel, preventing us from revoking old states or
185+
/// submitting new commitment transactions to the counterparty. Once the update(s) which failed
186+
/// have been successfully applied, ChannelManager::channel_monitor_updated can be used to
187+
/// restore the channel to an operational state.
188+
///
189+
/// Note that a given ChannelManager will *never* re-generate a given ChannelMonitorUpdate. If
190+
/// you return a TemporaryFailure you must ensure that it is written to disk safely before
191+
/// writing out the latest ChannelManager state.
192+
///
193+
/// Even when a channel has been "frozen" updates to the ChannelMonitor can continue to occur
194+
/// (eg if an inbound HTLC which we forwarded was claimed upstream resulting in us attempting
195+
/// to claim it on this channel) and those updates must be applied wherever they can be. At
196+
/// least one such updated ChannelMonitor must be persisted otherwise PermanentFailure should
197+
/// be returned to get things on-chain ASAP using only the in-memory copy. Obviously updates to
198+
/// the channel which would invalidate previous ChannelMonitors are not made when a channel has
199+
/// been "frozen".
200+
///
201+
/// Note that even if updates made after TemporaryFailure succeed you must still call
202+
/// channel_monitor_updated to ensure you have the latest monitor and re-enable normal channel
203+
/// operation.
204+
///
205+
/// Note that the update being processed here will not be replayed for you when you call
206+
/// ChannelManager::channel_monitor_updated, so you must store the update itself along
207+
/// with the persisted ChannelMonitor on your own local disk prior to returning a
208+
/// TemporaryFailure. You may, of course, employ a journaling approach, storing only the
209+
/// ChannelMonitorUpdate on disk without updating the monitor itself, replaying the journal at
210+
/// reload-time.
211+
///
212+
/// For deployments where a copy of ChannelMonitors and other local state are backed up in a
213+
/// remote location (with local copies persisted immediately), it is anticipated that all
214+
/// updates will return TemporaryFailure until the remote copies could be updated.
215+
TemporaryFailure,
216+
/// Used to indicate no further channel monitor updates will be allowed (eg we've moved on to a
217+
/// different watchtower and cannot update with all watchtowers that were previously informed
218+
/// of this channel).
219+
///
220+
/// At reception of this error, ChannelManager will force-close the channel and return at
221+
/// least a final ChannelMonitorUpdate::ChannelForceClosed which must be delivered to at
222+
/// least one ChannelMonitor copy. Revocation secret MUST NOT be released and offchain channel
223+
/// update must be rejected.
224+
///
225+
/// This failure may also signal a failure to update the local persisted copy of one of
226+
/// the channel monitor instance.
227+
///
228+
/// Note that even when you fail a holder commitment transaction update, you must store the
229+
/// update to ensure you can claim from it in case of a duplicate copy of this ChannelMonitor
230+
/// broadcasts it (e.g distributed channel-monitor deployment)
231+
///
232+
/// In case of distributed watchtowers deployment, the new version must be written to disk, as
233+
/// state may have been stored but rejected due to a block forcing a commitment broadcast. This
234+
/// storage is used to claim outputs of rejected state confirmed onchain by another watchtower,
235+
/// lagging behind on block processing.
236+
PermanentFailure,
237+
}
238+
178239
/// The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
179240
/// blocks are connected and disconnected.
180241
///
@@ -193,9 +254,7 @@ pub trait Confirm {
193254
/// funds in the channel. See [`ChannelMonitorUpdateErr`] for more details about how to handle
194255
/// multiple instances.
195256
///
196-
/// [`ChannelMonitor`]: channelmonitor::ChannelMonitor
197-
/// [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
198-
/// [`PermanentFailure`]: channelmonitor::ChannelMonitorUpdateErr::PermanentFailure
257+
/// [`PermanentFailure`]: ChannelMonitorUpdateErr::PermanentFailure
199258
pub trait Watch<ChannelSigner: Sign> {
200259
/// Watches a channel identified by `funding_txo` using `monitor`.
201260
///
@@ -217,7 +276,6 @@ pub trait Watch<ChannelSigner: Sign> {
217276
/// [`ChannelMonitorUpdateErr`] for invariants around returning an error.
218277
///
219278
/// [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
220-
/// [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
221279
fn update_channel(&self, funding_txo: OutPoint, update: ChannelMonitorUpdate) -> Result<(), ChannelMonitorUpdateErr>;
222280

223281
/// Returns any monitor events since the last call. Subsequent calls must only return new
@@ -242,7 +300,7 @@ pub trait Watch<ChannelSigner: Sign> {
242300
/// processed later. Then, in order to block until the data has been processed, any [`Watch`]
243301
/// invocation that has called the `Filter` must return [`TemporaryFailure`].
244302
///
245-
/// [`TemporaryFailure`]: channelmonitor::ChannelMonitorUpdateErr::TemporaryFailure
303+
/// [`TemporaryFailure`]: ChannelMonitorUpdateErr::TemporaryFailure
246304
/// [BIP 157]: https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki
247305
/// [BIP 158]: https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki
248306
pub trait Filter {

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
1616
use bitcoin::blockdata::constants::genesis_block;
1717
use bitcoin::hash_types::BlockHash;
1818
use bitcoin::network::constants::Network;
19-
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr};
19+
use chain::channelmonitor::ChannelMonitor;
2020
use chain::transaction::OutPoint;
21-
use chain::Listen;
22-
use chain::Watch;
21+
use chain::{ChannelMonitorUpdateErr, Listen, Watch};
2322
use ln::{PaymentPreimage, PaymentHash};
2423
use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure};
2524
use ln::features::{InitFeatures, InvoiceFeatures};

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ use bitcoin::secp256k1::ecdh::SharedSecret;
3636
use bitcoin::secp256k1;
3737

3838
use chain;
39-
use chain::{Confirm, Watch, BestBlock};
39+
use chain::{Confirm, ChannelMonitorUpdateErr, Watch, BestBlock};
4040
use chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
41-
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, ChannelMonitorUpdateErr, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent, CLOSED_CHANNEL_UPDATE_ID};
41+
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent, CLOSED_CHANNEL_UPDATE_ID};
4242
use chain::transaction::{OutPoint, TransactionData};
4343
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
4444
// construct one themselves.

lightning/src/util/test_utils.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ pub struct TestChainMonitor<'a> {
9191
pub latest_monitor_update_id: Mutex<HashMap<[u8; 32], (OutPoint, u64)>>,
9292
pub chain_monitor: chainmonitor::ChainMonitor<EnforcingSigner, &'a TestChainSource, &'a chaininterface::BroadcasterInterface, &'a TestFeeEstimator, &'a TestLogger, &'a chainmonitor::Persist<EnforcingSigner>>,
9393
pub keys_manager: &'a TestKeysInterface,
94-
pub update_ret: Mutex<Option<Result<(), channelmonitor::ChannelMonitorUpdateErr>>>,
94+
pub update_ret: Mutex<Option<Result<(), chain::ChannelMonitorUpdateErr>>>,
9595
/// If this is set to Some(), after the next return, we'll always return this until update_ret
9696
/// is changed:
97-
pub next_update_ret: Mutex<Option<Result<(), channelmonitor::ChannelMonitorUpdateErr>>>,
97+
pub next_update_ret: Mutex<Option<Result<(), chain::ChannelMonitorUpdateErr>>>,
9898
/// If this is set to Some(), the next update_channel call (not watch_channel) must be a
9999
/// ChannelForceClosed event for the given channel_id with should_broadcast set to the given
100100
/// boolean.
@@ -114,7 +114,7 @@ impl<'a> TestChainMonitor<'a> {
114114
}
115115
}
116116
impl<'a> chain::Watch<EnforcingSigner> for TestChainMonitor<'a> {
117-
fn watch_channel(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<EnforcingSigner>) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
117+
fn watch_channel(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<EnforcingSigner>) -> Result<(), chain::ChannelMonitorUpdateErr> {
118118
// At every point where we get a monitor update, we should be able to send a useful monitor
119119
// to a watchtower and disk...
120120
let mut w = TestVecWriter(Vec::new());
@@ -137,7 +137,7 @@ impl<'a> chain::Watch<EnforcingSigner> for TestChainMonitor<'a> {
137137
watch_res
138138
}
139139

140-
fn update_channel(&self, funding_txo: OutPoint, update: channelmonitor::ChannelMonitorUpdate) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
140+
fn update_channel(&self, funding_txo: OutPoint, update: channelmonitor::ChannelMonitorUpdate) -> Result<(), chain::ChannelMonitorUpdateErr> {
141141
// Every monitor update should survive roundtrip
142142
let mut w = TestVecWriter(Vec::new());
143143
update.write(&mut w).unwrap();
@@ -182,7 +182,7 @@ impl<'a> chain::Watch<EnforcingSigner> for TestChainMonitor<'a> {
182182
}
183183

184184
pub struct TestPersister {
185-
pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>
185+
pub update_ret: Mutex<Result<(), chain::ChannelMonitorUpdateErr>>
186186
}
187187
impl TestPersister {
188188
pub fn new() -> Self {
@@ -191,16 +191,16 @@ impl TestPersister {
191191
}
192192
}
193193

194-
pub fn set_update_ret(&self, ret: Result<(), channelmonitor::ChannelMonitorUpdateErr>) {
194+
pub fn set_update_ret(&self, ret: Result<(), chain::ChannelMonitorUpdateErr>) {
195195
*self.update_ret.lock().unwrap() = ret;
196196
}
197197
}
198198
impl<Signer: keysinterface::Sign> chainmonitor::Persist<Signer> for TestPersister {
199-
fn persist_new_channel(&self, _funding_txo: OutPoint, _data: &channelmonitor::ChannelMonitor<Signer>) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
199+
fn persist_new_channel(&self, _funding_txo: OutPoint, _data: &channelmonitor::ChannelMonitor<Signer>) -> Result<(), chain::ChannelMonitorUpdateErr> {
200200
self.update_ret.lock().unwrap().clone()
201201
}
202202

203-
fn update_persisted_channel(&self, _funding_txo: OutPoint, _update: &channelmonitor::ChannelMonitorUpdate, _data: &channelmonitor::ChannelMonitor<Signer>) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
203+
fn update_persisted_channel(&self, _funding_txo: OutPoint, _update: &channelmonitor::ChannelMonitorUpdate, _data: &channelmonitor::ChannelMonitor<Signer>) -> Result<(), chain::ChannelMonitorUpdateErr> {
204204
self.update_ret.lock().unwrap().clone()
205205
}
206206
fn sync_persisted_channel(&self, _funding_txo: OutPoint, _data: &channelmonitor::ChannelMonitor<Signer>) -> Result<(), ()> {

0 commit comments

Comments
 (0)