Skip to content

Commit 3bcd911

Browse files
authored
Merge pull request #213 from TheBlueMatt/2018-10-monitor-fail-pause
Add ChannelManager support for monitor update failure in one place
2 parents 65b23d8 + 497643a commit 3bcd911

File tree

8 files changed

+984
-217
lines changed

8 files changed

+984
-217
lines changed

src/ln/channel.rs

Lines changed: 217 additions & 78 deletions
Large diffs are not rendered by default.

src/ln/channelmanager.rs

Lines changed: 698 additions & 130 deletions
Large diffs are not rendered by default.

src/ln/channelmonitor.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use std::sync::{Arc,Mutex};
3939
use std::{hash,cmp};
4040

4141
/// An error enum representing a failure to persist a channel monitor update.
42+
#[derive(Clone)]
4243
pub enum ChannelMonitorUpdateErr {
4344
/// Used to indicate a temporary failure (eg connection to a watchtower failed, but is expected
4445
/// to succeed at some point in the future).
@@ -47,6 +48,22 @@ pub enum ChannelMonitorUpdateErr {
4748
/// submitting new commitment transactions to the remote party.
4849
/// ChannelManager::test_restore_channel_monitor can be used to retry the update(s) and restore
4950
/// the channel to an operational state.
51+
///
52+
/// Note that continuing to operate when no copy of the updated ChannelMonitor could be
53+
/// persisted is unsafe - if you failed to store the update on your own local disk you should
54+
/// instead return PermanentFailure to force closure of the channel ASAP.
55+
///
56+
/// Even when a channel has been "frozen" updates to the ChannelMonitor can continue to occur
57+
/// (eg if an inbound HTLC which we forwarded was claimed upstream resulting in us attempting
58+
/// to claim it on this channel) and those updates must be applied wherever they can be. At
59+
/// least one such updated ChannelMonitor must be persisted otherwise PermanentFailure should
60+
/// be returned to get things on-chain ASAP using only the in-memory copy. Obviously updates to
61+
/// the channel which would invalidate previous ChannelMonitors are not made when a channel has
62+
/// been "frozen".
63+
///
64+
/// Note that even if updates made after TemporaryFailure succeed you must still call
65+
/// test_restore_channel_monitor to ensure you have the latest monitor and re-enable normal
66+
/// channel operation.
5067
TemporaryFailure,
5168
/// Used to indicate no further channel monitor updates will be allowed (eg we've moved on to a
5269
/// different watchtower and cannot update with all watchtowers that were previously informed

src/ln/msgs.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ pub struct FundingSigned {
224224
}
225225

226226
/// A funding_locked message to be sent or received from a peer
227-
#[derive(Clone)]
227+
#[derive(Clone, PartialEq)]
228228
pub struct FundingLocked {
229229
pub(crate) channel_id: [u8; 32],
230230
pub(crate) next_per_commitment_point: PublicKey,
@@ -244,7 +244,7 @@ pub struct ClosingSigned {
244244
}
245245

246246
/// An update_add_htlc message to be sent or received from a peer
247-
#[derive(Clone)]
247+
#[derive(Clone, PartialEq)]
248248
pub struct UpdateAddHTLC {
249249
pub(crate) channel_id: [u8; 32],
250250
pub(crate) htlc_id: u64,
@@ -255,23 +255,23 @@ pub struct UpdateAddHTLC {
255255
}
256256

257257
/// An update_fulfill_htlc message to be sent or received from a peer
258-
#[derive(Clone)]
258+
#[derive(Clone, PartialEq)]
259259
pub struct UpdateFulfillHTLC {
260260
pub(crate) channel_id: [u8; 32],
261261
pub(crate) htlc_id: u64,
262262
pub(crate) payment_preimage: [u8; 32],
263263
}
264264

265265
/// An update_fail_htlc message to be sent or received from a peer
266-
#[derive(Clone)]
266+
#[derive(Clone, PartialEq)]
267267
pub struct UpdateFailHTLC {
268268
pub(crate) channel_id: [u8; 32],
269269
pub(crate) htlc_id: u64,
270270
pub(crate) reason: OnionErrorPacket,
271271
}
272272

273273
/// An update_fail_malformed_htlc message to be sent or received from a peer
274-
#[derive(Clone)]
274+
#[derive(Clone, PartialEq)]
275275
pub struct UpdateFailMalformedHTLC {
276276
pub(crate) channel_id: [u8; 32],
277277
pub(crate) htlc_id: u64,
@@ -280,32 +280,36 @@ pub struct UpdateFailMalformedHTLC {
280280
}
281281

282282
/// A commitment_signed message to be sent or received from a peer
283-
#[derive(Clone)]
283+
#[derive(Clone, PartialEq)]
284284
pub struct CommitmentSigned {
285285
pub(crate) channel_id: [u8; 32],
286286
pub(crate) signature: Signature,
287287
pub(crate) htlc_signatures: Vec<Signature>,
288288
}
289289

290290
/// A revoke_and_ack message to be sent or received from a peer
291+
#[derive(Clone, PartialEq)]
291292
pub struct RevokeAndACK {
292293
pub(crate) channel_id: [u8; 32],
293294
pub(crate) per_commitment_secret: [u8; 32],
294295
pub(crate) next_per_commitment_point: PublicKey,
295296
}
296297

297298
/// An update_fee message to be sent or received from a peer
299+
#[derive(PartialEq)]
298300
pub struct UpdateFee {
299301
pub(crate) channel_id: [u8; 32],
300302
pub(crate) feerate_per_kw: u32,
301303
}
302304

305+
#[derive(PartialEq)]
303306
pub(crate) struct DataLossProtect {
304307
pub(crate) your_last_per_commitment_secret: [u8; 32],
305308
pub(crate) my_current_per_commitment_point: PublicKey,
306309
}
307310

308311
/// A channel_reestablish message to be sent or received from a peer
312+
#[derive(PartialEq)]
309313
pub struct ChannelReestablish {
310314
pub(crate) channel_id: [u8; 32],
311315
pub(crate) next_local_commitment_number: u64,
@@ -463,6 +467,7 @@ pub struct HandleError { //TODO: rename me
463467

464468
/// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment
465469
/// transaction updates if they were pending.
470+
#[derive(PartialEq)]
466471
pub struct CommitmentUpdate {
467472
pub(crate) update_add_htlcs: Vec<UpdateAddHTLC>,
468473
pub(crate) update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
@@ -629,7 +634,18 @@ pub(crate) struct OnionPacket {
629634
pub(crate) hmac: [u8; 32],
630635
}
631636

632-
#[derive(Clone)]
637+
impl PartialEq for OnionPacket {
638+
fn eq(&self, other: &OnionPacket) -> bool {
639+
for (i, j) in self.hop_data.iter().zip(other.hop_data.iter()) {
640+
if i != j { return false; }
641+
}
642+
self.version == other.version &&
643+
self.public_key == other.public_key &&
644+
self.hmac == other.hmac
645+
}
646+
}
647+
648+
#[derive(Clone, PartialEq)]
633649
pub(crate) struct OnionErrorPacket {
634650
// This really should be a constant size slice, but the spec lets these things be up to 128KB?
635651
// (TODO) We limit it in decode to much lower...

src/ln/peer_handler.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,17 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
866866
Self::do_attempt_write_data(&mut descriptor, peer);
867867
continue;
868868
},
869+
Event::SendRevokeAndACK { ref node_id, ref msg } => {
870+
log_trace!(self, "Handling SendRevokeAndACK event in peer_handler for node {} for channel {}",
871+
log_pubkey!(node_id),
872+
log_bytes!(msg.channel_id));
873+
let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
874+
//TODO: Do whatever we're gonna do for handling dropped messages
875+
});
876+
peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 133)));
877+
Self::do_attempt_write_data(&mut descriptor, peer);
878+
continue;
879+
},
869880
Event::SendShutdown { ref node_id, ref msg } => {
870881
log_trace!(self, "Handling Shutdown event in peer_handler for node {} for channel {}",
871882
log_pubkey!(node_id),

src/util/errors.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ pub enum APIError {
3232
ChannelUnavailable {
3333
/// A human-readable error message
3434
err: &'static str
35-
}
35+
},
36+
/// An attempt to call add_update_monitor returned an Err (ie you did this!), causing the
37+
/// attempted action to fail.
38+
MonitorUpdateFailed,
3639
}
3740

3841
impl fmt::Debug for APIError {
@@ -42,6 +45,7 @@ impl fmt::Debug for APIError {
4245
APIError::FeeRateTooHigh {ref err, ref feerate} => write!(f, "{} feerate: {}", err, feerate),
4346
APIError::RouteError {ref err} => f.write_str(err),
4447
APIError::ChannelUnavailable {ref err} => f.write_str(err),
48+
APIError::MonitorUpdateFailed => f.write_str("Client indicated a channel monitor update failed"),
4549
}
4650
}
4751
}

src/util/events.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ pub enum Event {
129129
/// The update messages which should be sent. ALL messages in the struct should be sent!
130130
updates: msgs::CommitmentUpdate,
131131
},
132+
/// Used to indicate that a revoke_and_ack message should be sent to the peer with the given node_id.
133+
///
134+
/// This event is handled by PeerManager::process_events if you are using a PeerManager.
135+
SendRevokeAndACK {
136+
/// The node_id of the node which should receive this message
137+
node_id: PublicKey,
138+
/// The message which should be sent.
139+
msg: msgs::RevokeAndACK,
140+
},
132141
/// Used to indicate that a shutdown message should be sent to the peer with the given node_id.
133142
///
134143
/// This event is handled by PeerManager::process_events if you are using a PeerManager.

src/util/test_utils.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ impl chaininterface::FeeEstimator for TestFeeEstimator {
3838
pub struct TestChannelMonitor {
3939
pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor)>>,
4040
pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
41+
pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
4142
}
4243
impl TestChannelMonitor {
4344
pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>) -> Self {
4445
Self {
4546
added_monitors: Mutex::new(Vec::new()),
4647
simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster),
48+
update_ret: Mutex::new(Ok(())),
4749
}
4850
}
4951
}
@@ -57,7 +59,8 @@ impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
5759
w.0.clear();
5860
monitor.write_for_watchtower(&mut w).unwrap(); // This at least shouldn't crash...
5961
self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));
60-
self.simple_monitor.add_update_monitor(funding_txo, monitor)
62+
assert!(self.simple_monitor.add_update_monitor(funding_txo, monitor).is_ok());
63+
self.update_ret.lock().unwrap().clone()
6164
}
6265
}
6366

0 commit comments

Comments
 (0)