Skip to content

Commit f27bf09

Browse files
committed
Create simple ChannelMonitor-specific Err type
1 parent 7efaf2e commit f27bf09

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

src/ln/channel.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,8 @@ impl Channel {
18801880
return Err(HandleError{err: "Got a revoke commitment secret which didn't correspond to their current pubkey", action: None});
18811881
}
18821882
}
1883-
self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret)?;
1883+
self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret)
1884+
.map_err(|e| HandleError{err: e.0, action: None})?;
18841885
self.channel_monitor.provide_their_next_revocation_point(Some((self.cur_remote_commitment_transaction_number - 1, msg.next_per_commitment_point)));
18851886

18861887
// Update state now that we've passed all the can-fail calls...

src/ln/channelmonitor.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use secp256k1::{Secp256k1,Message,Signature};
2626
use secp256k1::key::{SecretKey,PublicKey};
2727
use secp256k1;
2828

29-
use ln::msgs::{DecodeError, HandleError};
29+
use ln::msgs::DecodeError;
3030
use ln::chan_utils;
3131
use ln::chan_utils::HTLCOutputInCommitment;
3232
use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface};
@@ -74,6 +74,14 @@ pub enum ChannelMonitorUpdateErr {
7474
PermanentFailure,
7575
}
7676

77+
/// General Err type for ChannelMonitor actions. Generally, this implies that the data provided is
78+
/// inconsistent with the ChannelMonitor being called. eg for ChannelMonitor::insert_combine this
79+
/// means you tried to merge two monitors for different channels or for a channel which was
80+
/// restored from a backup and then generated new commitment updates.
81+
/// Contains a human-readable error message.
82+
#[derive(Debug)]
83+
pub struct MonitorUpdateError(pub &'static str);
84+
7785
/// Simple trait indicating ability to track a set of ChannelMonitors and multiplex events between
7886
/// them. Generally should be implemented by keeping a local SimpleManyChannelMonitor and passing
7987
/// events to it, while also taking any add_update_monitor events and passing them to some remote
@@ -158,7 +166,7 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key>
158166
}
159167

160168
/// Adds or udpates the monitor which monitors the channel referred to by the given key.
161-
pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor) -> Result<(), HandleError> {
169+
pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor) -> Result<(), MonitorUpdateError> {
162170
let mut monitors = self.monitors.lock().unwrap();
163171
match monitors.get_mut(&key) {
164172
Some(orig_monitor) => {
@@ -408,12 +416,12 @@ impl ChannelMonitor {
408416
/// Inserts a revocation secret into this channel monitor. Prunes old preimages if neither
409417
/// needed by local commitment transactions HTCLs nor by remote ones. Unless we haven't already seen remote
410418
/// commitment transaction's secret, they are de facto pruned (we can use revocation key).
411-
pub(super) fn provide_secret(&mut self, idx: u64, secret: [u8; 32]) -> Result<(), HandleError> {
419+
pub(super) fn provide_secret(&mut self, idx: u64, secret: [u8; 32]) -> Result<(), MonitorUpdateError> {
412420
let pos = ChannelMonitor::place_secret(idx);
413421
for i in 0..pos {
414422
let (old_secret, old_idx) = self.old_secrets[i as usize];
415423
if ChannelMonitor::derive_secret(secret, pos, old_idx) != old_secret {
416-
return Err(HandleError{err: "Previous secret did not match new one", action: None})
424+
return Err(MonitorUpdateError("Previous secret did not match new one"));
417425
}
418426
}
419427
self.old_secrets[pos as usize] = (secret, idx);
@@ -537,12 +545,12 @@ impl ChannelMonitor {
537545
/// Combines this ChannelMonitor with the information contained in the other ChannelMonitor.
538546
/// After a successful call this ChannelMonitor is up-to-date and is safe to use to monitor the
539547
/// chain for new blocks/transactions.
540-
pub fn insert_combine(&mut self, mut other: ChannelMonitor) -> Result<(), HandleError> {
548+
pub fn insert_combine(&mut self, mut other: ChannelMonitor) -> Result<(), MonitorUpdateError> {
541549
if self.funding_txo.is_some() {
542550
// We should be able to compare the entire funding_txo, but in fuzztarget its trivially
543551
// easy to collide the funding_txo hash and have a different scriptPubKey.
544552
if other.funding_txo.is_some() && other.funding_txo.as_ref().unwrap().0 != self.funding_txo.as_ref().unwrap().0 {
545-
return Err(HandleError{err: "Funding transaction outputs are not identical!", action: None});
553+
return Err(MonitorUpdateError("Funding transaction outputs are not identical!"));
546554
}
547555
} else {
548556
self.funding_txo = other.funding_txo.take();
@@ -830,14 +838,14 @@ impl ChannelMonitor {
830838
//we want to leave out (eg funding_txo, etc).
831839

832840
/// Can only fail if idx is < get_min_seen_secret
833-
pub(super) fn get_secret(&self, idx: u64) -> Result<[u8; 32], HandleError> {
841+
pub(super) fn get_secret(&self, idx: u64) -> Option<[u8; 32]> {
834842
for i in 0..self.old_secrets.len() {
835843
if (idx & (!((1 << i) - 1))) == self.old_secrets[i].1 {
836-
return Ok(ChannelMonitor::derive_secret(self.old_secrets[i].0, i as u8, idx))
844+
return Some(ChannelMonitor::derive_secret(self.old_secrets[i].0, i as u8, idx))
837845
}
838846
}
839847
assert!(idx < self.get_min_seen_secret());
840-
Err(HandleError{err: "idx too low", action: None})
848+
None
841849
}
842850

843851
pub(super) fn get_min_seen_secret(&self) -> u64 {
@@ -1215,7 +1223,7 @@ impl ChannelMonitor {
12151223
};
12161224
}
12171225

1218-
let secret = ignore_error!(self.get_secret(commitment_number));
1226+
let secret = if let Some(secret) = self.get_secret(commitment_number) { secret } else { return (None, None); };
12191227
let per_commitment_key = ignore_error!(SecretKey::from_slice(&self.secp_ctx, &secret));
12201228
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
12211229
let revocation_pubkey = match self.key_storage {
@@ -1808,7 +1816,7 @@ mod tests {
18081816
idx -= 1;
18091817
}
18101818
assert_eq!(monitor.get_min_seen_secret(), idx + 1);
1811-
assert!(monitor.get_secret(idx).is_err());
1819+
assert!(monitor.get_secret(idx).is_none());
18121820
};
18131821
}
18141822

@@ -1870,7 +1878,7 @@ mod tests {
18701878

18711879
secrets.push([0; 32]);
18721880
secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
1873-
assert_eq!(monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap_err().err,
1881+
assert_eq!(monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap_err().0,
18741882
"Previous secret did not match new one");
18751883
}
18761884

@@ -1896,7 +1904,7 @@ mod tests {
18961904

18971905
secrets.push([0; 32]);
18981906
secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
1899-
assert_eq!(monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap_err().err,
1907+
assert_eq!(monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap_err().0,
19001908
"Previous secret did not match new one");
19011909
}
19021910

@@ -1922,7 +1930,7 @@ mod tests {
19221930

19231931
secrets.push([0; 32]);
19241932
secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
1925-
assert_eq!(monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap_err().err,
1933+
assert_eq!(monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap_err().0,
19261934
"Previous secret did not match new one");
19271935
}
19281936

@@ -1968,7 +1976,7 @@ mod tests {
19681976

19691977
secrets.push([0; 32]);
19701978
secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
1971-
assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().err,
1979+
assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().0,
19721980
"Previous secret did not match new one");
19731981
}
19741982

@@ -2004,7 +2012,7 @@ mod tests {
20042012

20052013
secrets.push([0; 32]);
20062014
secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
2007-
assert_eq!(monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap_err().err,
2015+
assert_eq!(monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap_err().0,
20082016
"Previous secret did not match new one");
20092017
}
20102018

@@ -2050,7 +2058,7 @@ mod tests {
20502058

20512059
secrets.push([0; 32]);
20522060
secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
2053-
assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().err,
2061+
assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().0,
20542062
"Previous secret did not match new one");
20552063
}
20562064

@@ -2096,7 +2104,7 @@ mod tests {
20962104

20972105
secrets.push([0; 32]);
20982106
secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
2099-
assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().err,
2107+
assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().0,
21002108
"Previous secret did not match new one");
21012109
}
21022110

@@ -2142,7 +2150,7 @@ mod tests {
21422150

21432151
secrets.push([0; 32]);
21442152
secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a7efbc61aac46d34f77778bac22c8a20c6a46ca460addc49009bda875ec88fa4").unwrap());
2145-
assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().err,
2153+
assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().0,
21462154
"Previous secret did not match new one");
21472155
}
21482156
}

0 commit comments

Comments
 (0)