Skip to content

Commit 0eb6190

Browse files
Add monitor_event_source to holding cell HTLC fails
Currently, the resolution of HTLCs (and decisions on when HTLCs can be forwarded) is the responsibility of Channel objects (a part of ChannelManager) until the channel is closed, and then the ChannelMonitor thereafter. This leads to some complexity around race conditions for HTLCs right around channel closure. Additionally, there is lots of complexity reconstructing the state of all HTLCs in the ChannelManager deserialization/loading logic. Instead, we want to do all resolution in ChannelMonitors (in response to ChannelMonitorUpdates) and pass them back to ChannelManager in the form of MonitorEvents (similar to how HTLCs are resolved after channels are closed). In order to have reliable resolution, we'll need to keep MonitorEvents around in the ChannelMonitor until the ChannelManager has finished processing them. This will simplify things - on restart instead of examining the set of HTLCs in monitors we can simply replay all the pending MonitorEvents. To ensure we can resolve HTLC monitor events for forward failures, we need to pipe the event id through the HTLC failure pipeline. We started this process in the previous commit, and here we continue by storing the monitor event id in the Channel's holding cell HTLC failures. In upcoming commits we will eventually get to the point of acking the monitor event when the HTLC is irrevocably removed from the inbound edge via monitor update.
1 parent 9b33f06 commit 0eb6190

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

lightning/src/ln/channel.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::blinded_path::message::BlindedMessagePath;
3030
use crate::chain::chaininterface::{
3131
ConfirmationTarget, FeeEstimator, LowerBoundedFeeEstimator, TransactionType,
3232
};
33+
use crate::chain::chainmonitor::MonitorEventSource;
3334
use crate::chain::channelmonitor::{
3435
ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, CommitmentHTLCData,
3536
LATENCY_GRACE_PERIOD_BLOCKS,
@@ -550,11 +551,13 @@ enum HTLCUpdateAwaitingACK {
550551
FailHTLC {
551552
htlc_id: u64,
552553
err_packet: msgs::OnionErrorPacket,
554+
monitor_event_source: Option<MonitorEventSource>,
553555
},
554556
FailMalformedHTLC {
555557
htlc_id: u64,
556558
failure_code: u16,
557559
sha256_of_onion: [u8; 32],
560+
monitor_event_source: Option<MonitorEventSource>,
558561
},
559562
}
560563

@@ -6470,7 +6473,7 @@ impl FailHTLCContents for msgs::OnionErrorPacket {
64706473
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(self))
64716474
}
64726475
fn to_htlc_update_awaiting_ack(self, htlc_id: u64) -> HTLCUpdateAwaitingACK {
6473-
HTLCUpdateAwaitingACK::FailHTLC { htlc_id, err_packet: self }
6476+
HTLCUpdateAwaitingACK::FailHTLC { htlc_id, err_packet: self, monitor_event_source: None }
64746477
}
64756478
}
64766479
impl FailHTLCContents for ([u8; 32], u16) {
@@ -6494,6 +6497,7 @@ impl FailHTLCContents for ([u8; 32], u16) {
64946497
htlc_id,
64956498
sha256_of_onion: self.0,
64966499
failure_code: self.1,
6500+
monitor_event_source: None,
64976501
}
64986502
}
64996503
}
@@ -8292,14 +8296,15 @@ where
82928296
monitor_update.updates.append(&mut additional_monitor_update.updates);
82938297
None
82948298
},
8295-
&HTLCUpdateAwaitingACK::FailHTLC { htlc_id, ref err_packet } => Some(
8299+
&HTLCUpdateAwaitingACK::FailHTLC { htlc_id, ref err_packet, .. } => Some(
82968300
self.fail_htlc(htlc_id, err_packet.clone(), false, logger)
82978301
.map(|fail_msg_opt| fail_msg_opt.map(|_| ())),
82988302
),
82998303
&HTLCUpdateAwaitingACK::FailMalformedHTLC {
83008304
htlc_id,
83018305
failure_code,
83028306
sha256_of_onion,
8307+
..
83038308
} => Some(
83048309
self.fail_htlc(htlc_id, (sha256_of_onion, failure_code), false, logger)
83058310
.map(|fail_msg_opt| fail_msg_opt.map(|_| ())),
@@ -14540,7 +14545,7 @@ impl<SP: SignerProvider> Writeable for FundedChannel<SP> {
1454014545
// Store the attribution data for later writing.
1454114546
holding_cell_attribution_data.push(attribution_data.as_ref());
1454214547
},
14543-
&HTLCUpdateAwaitingACK::FailHTLC { ref htlc_id, ref err_packet } => {
14548+
&HTLCUpdateAwaitingACK::FailHTLC { ref htlc_id, ref err_packet, .. } => {
1454414549
2u8.write(writer)?;
1454514550
htlc_id.write(writer)?;
1454614551
err_packet.data.write(writer)?;
@@ -14552,6 +14557,7 @@ impl<SP: SignerProvider> Writeable for FundedChannel<SP> {
1455214557
htlc_id,
1455314558
failure_code,
1455414559
sha256_of_onion,
14560+
..
1455514561
} => {
1455614562
// We don't want to break downgrading by adding a new variant, so write a dummy
1455714563
// `::FailHTLC` variant and write the real malformed error as an optional TLV.
@@ -14991,6 +14997,7 @@ impl<'a, 'b, 'c, ES: EntropySource, SP: SignerProvider>
1499114997
data: Readable::read(reader)?,
1499214998
attribution_data: None,
1499314999
},
15000+
monitor_event_source: None,
1499415001
},
1499515002
_ => return Err(DecodeError::InvalidValue),
1499615003
});
@@ -15445,7 +15452,7 @@ impl<'a, 'b, 'c, ES: EntropySource, SP: SignerProvider>
1544515452
let htlc_idx = holding_cell_htlc_updates
1544615453
.iter()
1544715454
.position(|htlc| {
15448-
if let HTLCUpdateAwaitingACK::FailHTLC { htlc_id, err_packet } = htlc {
15455+
if let HTLCUpdateAwaitingACK::FailHTLC { htlc_id, err_packet, .. } = htlc {
1544915456
let matches = *htlc_id == malformed_htlc_id;
1545015457
if matches {
1545115458
debug_assert!(err_packet.data.is_empty())
@@ -15460,6 +15467,7 @@ impl<'a, 'b, 'c, ES: EntropySource, SP: SignerProvider>
1546015467
htlc_id: malformed_htlc_id,
1546115468
failure_code,
1546215469
sha256_of_onion,
15470+
monitor_event_source: None,
1546315471
};
1546415472
let _ =
1546515473
core::mem::replace(&mut holding_cell_htlc_updates[htlc_idx], malformed_htlc);
@@ -16486,12 +16494,14 @@ mod tests {
1648616494
|htlc_id, attribution_data| HTLCUpdateAwaitingACK::FailHTLC {
1648716495
htlc_id,
1648816496
err_packet: msgs::OnionErrorPacket { data: vec![42], attribution_data },
16497+
monitor_event_source: None,
1648916498
};
1649016499
let dummy_holding_cell_malformed_htlc =
1649116500
|htlc_id| HTLCUpdateAwaitingACK::FailMalformedHTLC {
1649216501
htlc_id,
1649316502
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
1649416503
sha256_of_onion: [0; 32],
16504+
monitor_event_source: None,
1649516505
};
1649616506
let mut holding_cell_htlc_updates = Vec::with_capacity(12);
1649716507
for i in 0..16 {

0 commit comments

Comments
 (0)