Skip to content

Commit ffe9d87

Browse files
f Jeff fixups
1 parent 5c01f4d commit ffe9d87

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,13 @@ pub enum ChannelMonitorUpdateErr {
153153
/// corrupted.
154154
/// Contains a human-readable error message.
155155
#[derive(Debug)]
156-
pub struct MonitorUpdateError {
157-
/// The human-readable error message string.
158-
pub msg: &'static str,
159-
/// A monitor update may have an error but still need to be persisted to disk.
160-
pub persist_updated_monitor: bool
156+
pub enum MonitorUpdateError {
157+
/// While updating the monitor generated an error, still persist the monitor as it has changed
158+
/// and needs to be updated on-disk. Contains a human-readable error message.
159+
PersistMonitor(&'static str),
160+
/// Updating the monitor generated an error, no need to persist the updated monitor. Contains
161+
/// a human-readable error message.
162+
NoPersistMonitor(&'static str)
161163
}
162164

163165
/// An event to be processed by the ChannelManager.
@@ -265,10 +267,7 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref, D: Deref>
265267
/// Creates a new object which can be used to monitor several channels given the chain
266268
/// interface with which to register to receive notifications.
267269
pub fn new(chain_source: Option<C>, broadcaster: T, logger: L, feeest: F, data_persister: D) -> Result<Self, ChannelMonitorUpdateErr> {
268-
let monitors = match data_persister.load_channel_data() {
269-
Ok(mons) => mons,
270-
Err(e) => return Err(e)
271-
};
270+
let monitors = data_persister.load_channel_data()?;
272271

273272
Ok(Self {
274273
monitors: Mutex::new(monitors),
@@ -294,14 +293,12 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref, D: Deref>
294293
},
295294
hash_map::Entry::Vacant(e) => e,
296295
};
297-
{
298-
match self.data_persister.persist_channel_data(outpoint, &monitor) {
299-
Err(e) => {
300-
log_error!(self.logger, "Failed to persist new channel data");
301-
return Err(e);
302-
},
303-
_ => {}
304-
}
296+
match self.data_persister.persist_channel_data(outpoint, &monitor) {
297+
Err(e) => {
298+
log_error!(self.logger, "Failed to persist new channel data");
299+
return Err(e);
300+
},
301+
_ => {}
305302
}
306303
{
307304
let funding_txo = monitor.get_funding_txo();
@@ -327,15 +324,21 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref, D: Deref>
327324
log_trace!(self.logger, "Updating Channel Monitor for channel {}", log_funding_info!(orig_monitor));
328325
let mut should_persist = true;
329326
let res = orig_monitor.update_monitor(&update, &self.broadcaster, &self.logger);
330-
if let Err(MonitorUpdateError{ msg, persist_updated_monitor }) = res {
331-
log_error!(self.logger, "{}", msg);
332-
should_persist = persist_updated_monitor;
327+
match res {
328+
Err(MonitorUpdateError::PersistMonitor(msg)) => {
329+
log_error!(self.logger, "{}", msg);
330+
},
331+
Err(MonitorUpdateError::NoPersistMonitor(msg)) => {
332+
log_error!(self.logger, "{}", msg);
333+
should_persist = false;
334+
},
335+
Ok(()) => {},
333336
}
334337
if should_persist {
335-
match self.data_persister.update_channel_data(outpoint, &update, orig_monitor) {
336-
Err(e) => return Err(e),
337-
_ => {}
338-
}
338+
match self.data_persister.update_channel_data(outpoint, &update, orig_monitor) {
339+
Err(e) => return Err(e),
340+
_ => {}
341+
}
339342
}
340343
if res.is_err() { return Err(ChannelMonitorUpdateErr::PermanentFailure); }
341344
} else {
@@ -1227,10 +1230,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12271230
/// commitment transaction's secret, they are de facto pruned (we can use revocation key).
12281231
fn provide_secret(&mut self, idx: u64, secret: [u8; 32]) -> Result<(), MonitorUpdateError> {
12291232
if let Err(()) = self.commitment_secrets.provide_secret(idx, secret) {
1230-
return Err(MonitorUpdateError{
1231-
msg: "Previous secret did not match new one",
1232-
persist_updated_monitor: false
1233-
});
1233+
return Err(MonitorUpdateError::NoPersistMonitor("Previous secret did not match new one"));
12341234
}
12351235

12361236
// Prune HTLCs from the previous remote commitment tx so we don't generate failure/fulfill
@@ -1331,10 +1331,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
13311331
/// Panics if set_on_local_tx_csv has never been called.
13321332
fn provide_latest_local_commitment_tx_info(&mut self, commitment_tx: LocalCommitmentTransaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
13331333
if self.local_tx_signed {
1334-
return Err(MonitorUpdateError{
1335-
msg: "A local commitment tx has already been signed, no new local commitment txn can be sent to our counterparty",
1336-
persist_updated_monitor: false
1337-
});
1334+
return Err(MonitorUpdateError::NoPersistMonitor(
1335+
"A local commitment tx has already been signed, no new local commitment txn can be sent to our counterparty"));
13381336
}
13391337
let txid = commitment_tx.txid();
13401338
let sequence = commitment_tx.unsigned_tx.input[0].sequence as u64;
@@ -1355,10 +1353,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
13551353
// for which you want to spend outputs. We're NOT robust again this scenario right
13561354
// now but we should consider it later.
13571355
if let Err(_) = self.onchain_tx_handler.provide_latest_local_tx(commitment_tx) {
1358-
return Err(MonitorUpdateError{
1359-
msg: "Local commitment signed has already been signed, no further update of LOCAL commitment transaction is allowed",
1360-
persist_updated_monitor: false
1361-
});
1356+
return Err(MonitorUpdateError::PersistMonitor(
1357+
"Local commitment signed has already been signed, no further update of LOCAL commitment transaction is allowed"));
13621358
}
13631359
self.current_local_commitment_number = 0xffff_ffff_ffff - ((((sequence & 0xffffff) << 3*8) | (locktime as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor);
13641360
mem::swap(&mut new_local_commitment_tx, &mut self.current_local_commitment_tx);

0 commit comments

Comments
 (0)