Skip to content

Commit 7df042b

Browse files
authored
Merge pull request #551 from TheBlueMatt/2020-03-no-chan-mon
Generate latest local commitment transactions via monitor avoiding Channel's copy
2 parents 082b720 + feca83a commit 7df042b

8 files changed

+293
-175
lines changed

fuzz/src/chanmon_consistency.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl channelmonitor::ManyChannelMonitor<EnforcingChannelKeys> for TestChannelMon
116116
};
117117
let mut deserialized_monitor = <(Sha256d, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::
118118
read(&mut Cursor::new(&map_entry.get().1), Arc::clone(&self.logger)).unwrap().1;
119-
deserialized_monitor.update_monitor(update.clone()).unwrap();
119+
deserialized_monitor.update_monitor(update.clone(), &&TestBroadcaster {}).unwrap();
120120
let mut ser = VecWriter(Vec::new());
121121
deserialized_monitor.write_for_disk(&mut ser).unwrap();
122122
map_entry.insert((update.update_id, ser.0));

lightning/src/ln/chanmon_update_fail_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn test_simple_monitor_permanent_update_fail() {
3131

3232
*nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::PermanentFailure);
3333
if let Err(APIError::ChannelUnavailable {..}) = nodes[0].node.send_payment(route, payment_hash_1) {} else { panic!(); }
34-
check_added_monitors!(nodes[0], 1);
34+
check_added_monitors!(nodes[0], 2);
3535

3636
let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
3737
assert_eq!(events_1.len(), 2);
@@ -120,7 +120,7 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) {
120120

121121
// ...and make sure we can force-close a frozen channel
122122
nodes[0].node.force_close_channel(&channel_id);
123-
check_added_monitors!(nodes[0], 0);
123+
check_added_monitors!(nodes[0], 1);
124124
check_closed_broadcast!(nodes[0], false);
125125

126126
// TODO: Once we hit the chain with the failure transaction we should check that we get a

lightning/src/ln/channel.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -1138,8 +1138,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
11381138
}
11391139

11401140
/// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
1141-
/// In such cases we debug_assert!(false) and return an IgnoreError. Thus, will always return
1142-
/// Ok(_) if debug assertions are turned on and preconditions are met.
1141+
/// In such cases we debug_assert!(false) and return a ChannelError::Ignore. Thus, will always
1142+
/// return Ok(_) if debug assertions are turned on or preconditions are met.
1143+
///
1144+
/// Note that it is still possible to hit these assertions in case we find a preimage on-chain
1145+
/// but then have a reorg which settles on an HTLC-failure on chain.
11431146
fn get_update_fulfill_htlc(&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage) -> Result<(Option<msgs::UpdateFulfillHTLC>, Option<ChannelMonitorUpdate>), ChannelError> {
11441147
// Either ChannelFunded got set (which means it won't be unset) or there is no way any
11451148
// caller thought we could have something claimed (cause we wouldn't have accepted in an
@@ -1167,6 +1170,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
11671170
} else {
11681171
log_warn!(self, "Have preimage and want to fulfill HTLC with payment hash {} we already failed against channel {}", log_bytes!(htlc.payment_hash.0), log_bytes!(self.channel_id()));
11691172
}
1173+
debug_assert!(false, "Tried to fulfill an HTLC that was already fail/fulfilled");
11701174
return Ok((None, None));
11711175
},
11721176
_ => {
@@ -1200,6 +1204,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
12001204
match pending_update {
12011205
&HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
12021206
if htlc_id_arg == htlc_id {
1207+
// Make sure we don't leave latest_monitor_update_id incremented here:
1208+
self.latest_monitor_update_id -= 1;
1209+
debug_assert!(false, "Tried to fulfill an HTLC that was already fulfilled");
12031210
return Ok((None, None));
12041211
}
12051212
},
@@ -1208,6 +1215,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
12081215
log_warn!(self, "Have preimage and want to fulfill HTLC with pending failure against channel {}", log_bytes!(self.channel_id()));
12091216
// TODO: We may actually be able to switch to a fulfill here, though its
12101217
// rare enough it may not be worth the complexity burden.
1218+
debug_assert!(false, "Tried to fulfill an HTLC that was already failed");
12111219
return Ok((None, Some(monitor_update)));
12121220
}
12131221
},
@@ -1259,8 +1267,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
12591267
}
12601268

12611269
/// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
1262-
/// In such cases we debug_assert!(false) and return an IgnoreError. Thus, will always return
1263-
/// Ok(_) if debug assertions are turned on and preconditions are met.
1270+
/// In such cases we debug_assert!(false) and return a ChannelError::Ignore. Thus, will always
1271+
/// return Ok(_) if debug assertions are turned on or preconditions are met.
1272+
///
1273+
/// Note that it is still possible to hit these assertions in case we find a preimage on-chain
1274+
/// but then have a reorg which settles on an HTLC-failure on chain.
12641275
pub fn get_update_fail_htlc(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket) -> Result<Option<msgs::UpdateFailHTLC>, ChannelError> {
12651276
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
12661277
panic!("Was asked to fail an HTLC when channel was not in an operational state");
@@ -1277,6 +1288,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
12771288
match htlc.state {
12781289
InboundHTLCState::Committed => {},
12791290
InboundHTLCState::LocalRemoved(_) => {
1291+
debug_assert!(false, "Tried to fail an HTLC that was already fail/fulfilled");
12801292
return Ok(None);
12811293
},
12821294
_ => {
@@ -1297,11 +1309,13 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
12971309
match pending_update {
12981310
&HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
12991311
if htlc_id_arg == htlc_id {
1312+
debug_assert!(false, "Tried to fail an HTLC that was already fulfilled");
13001313
return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
13011314
}
13021315
},
13031316
&HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
13041317
if htlc_id_arg == htlc_id {
1318+
debug_assert!(false, "Tried to fail an HTLC that was already failed");
13051319
return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
13061320
}
13071321
},
@@ -3760,7 +3774,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
37603774
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
37613775
/// Also returns the list of payment_hashes for channels which we can safely fail backwards
37623776
/// immediately (others we will have to allow to time out).
3763-
pub fn force_shutdown(&mut self) -> (Vec<Transaction>, Vec<(HTLCSource, PaymentHash)>) {
3777+
pub fn force_shutdown(&mut self, should_broadcast: bool) -> (Option<OutPoint>, ChannelMonitorUpdate, Vec<(HTLCSource, PaymentHash)>) {
37643778
assert!(self.channel_state != ChannelState::ShutdownComplete as u32);
37653779

37663780
// We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
@@ -3783,12 +3797,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
37833797

37843798
self.channel_state = ChannelState::ShutdownComplete as u32;
37853799
self.update_time_counter += 1;
3786-
if self.channel_monitor.is_some() {
3787-
(self.channel_monitor.as_mut().unwrap().get_latest_local_commitment_txn(), dropped_outbound_htlcs)
3788-
} else {
3789-
// We aren't even signed funding yet, so can't broadcast anything
3790-
(Vec::new(), dropped_outbound_htlcs)
3791-
}
3800+
self.latest_monitor_update_id += 1;
3801+
(self.funding_txo.clone(), ChannelMonitorUpdate {
3802+
update_id: self.latest_monitor_update_id,
3803+
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast }],
3804+
}, dropped_outbound_htlcs)
37923805
}
37933806
}
37943807

0 commit comments

Comments
 (0)