@@ -30,7 +30,7 @@ use crate::ln::script::{self, ShutdownScript};
30
30
use crate::ln::channelmanager::{self, CounterpartyForwardingInfo, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT, ChannelShutdownState};
31
31
use crate::ln::chan_utils::{CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, htlc_success_tx_weight, htlc_timeout_tx_weight, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction, HolderCommitmentTransaction, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, MAX_HTLCS, get_commitment_transaction_number_obscure_factor, ClosingTransaction};
32
32
use crate::ln::chan_utils;
33
- use crate::ln::onion_utils::HTLCFailReason;
33
+ use crate::ln::onion_utils::{ HTLCFailReason, INVALID_ONION_BLINDING} ;
34
34
use crate::chain::BestBlock;
35
35
use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, LowerBoundedFeeEstimator};
36
36
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS, CLOSED_CHANNEL_UPDATE_ID};
@@ -248,6 +248,9 @@ enum HTLCUpdateAwaitingACK {
248
248
htlc_id: u64,
249
249
err_packet: msgs::OnionErrorPacket,
250
250
},
251
+ FailMalformedHTLC {
252
+ htlc_id: u64,
253
+ },
251
254
}
252
255
253
256
/// There are a few "states" and then a number of flags which can be applied:
@@ -2055,6 +2058,25 @@ struct CommitmentTxInfoCached {
2055
2058
feerate: u32,
2056
2059
}
2057
2060
2061
+ trait FailHTLCMessage {
2062
+ fn new(htlc_id: u64, channel_id: ChannelId, reason: msgs::OnionErrorPacket) -> Self;
2063
+ }
2064
+ impl FailHTLCMessage for msgs::UpdateFailHTLC {
2065
+ fn new(htlc_id: u64, channel_id: ChannelId, reason: msgs::OnionErrorPacket) -> Self {
2066
+ msgs::UpdateFailHTLC { htlc_id, channel_id, reason }
2067
+ }
2068
+ }
2069
+ impl FailHTLCMessage for msgs::UpdateFailMalformedHTLC {
2070
+ fn new(htlc_id: u64, channel_id: ChannelId, _reason: msgs::OnionErrorPacket) -> Self {
2071
+ msgs::UpdateFailMalformedHTLC {
2072
+ htlc_id,
2073
+ channel_id,
2074
+ sha256_of_onion: [0; 32],
2075
+ failure_code: INVALID_ONION_BLINDING,
2076
+ }
2077
+ }
2078
+ }
2079
+
2058
2080
impl<SP: Deref> Channel<SP> where
2059
2081
SP::Target: SignerProvider,
2060
2082
<SP::Target as SignerProvider>::Signer: WriteableEcdsaChannelSigner
@@ -2279,7 +2301,9 @@ impl<SP: Deref> Channel<SP> where
2279
2301
return UpdateFulfillFetch::DuplicateClaim {};
2280
2302
}
2281
2303
},
2282
- &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
2304
+ &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } |
2305
+ &HTLCUpdateAwaitingACK::FailMalformedHTLC { htlc_id, .. } =>
2306
+ {
2283
2307
if htlc_id_arg == htlc_id {
2284
2308
log_warn!(logger, "Have preimage and want to fulfill HTLC with pending failure against channel {}", &self.context.channel_id());
2285
2309
// TODO: We may actually be able to switch to a fulfill here, though its
@@ -2372,7 +2396,15 @@ impl<SP: Deref> Channel<SP> where
2372
2396
/// [`ChannelError::Ignore`].
2373
2397
pub fn queue_fail_htlc<L: Deref>(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket, logger: &L)
2374
2398
-> Result<(), ChannelError> where L::Target: Logger {
2375
- self.fail_htlc(htlc_id_arg, err_packet, true, logger)
2399
+ self.fail_htlc::<L, msgs::UpdateFailHTLC>(htlc_id_arg, Some(err_packet), true, logger)
2400
+ .map(|msg_opt| assert!(msg_opt.is_none(), "We forced holding cell?"))
2401
+ }
2402
+
2403
+ /// Used for failing back blinded payments if we are not the intro node. See
2404
+ /// [`Self::queue_fail_htlc`] for more info.
2405
+ pub fn queue_fail_blinded_intermed_htlc<L: Deref>(&mut self, htlc_id_arg: u64, logger: &L)
2406
+ -> Result<(), ChannelError> where L::Target: Logger {
2407
+ self.fail_htlc::<L, msgs::UpdateFailMalformedHTLC>(htlc_id_arg, None, true, logger)
2376
2408
.map(|msg_opt| assert!(msg_opt.is_none(), "We forced holding cell?"))
2377
2409
}
2378
2410
@@ -2384,8 +2416,10 @@ impl<SP: Deref> Channel<SP> where
2384
2416
/// If we do fail twice, we `debug_assert!(false)` and return `Ok(None)`. Thus, this will always
2385
2417
/// return `Ok(_)` if preconditions are met. In any case, `Err`s will only be
2386
2418
/// [`ChannelError::Ignore`].
2387
- fn fail_htlc<L: Deref>(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket, mut force_holding_cell: bool, logger: &L)
2388
- -> Result<Option<msgs::UpdateFailHTLC>, ChannelError> where L::Target: Logger {
2419
+ fn fail_htlc<L: Deref, M: FailHTLCMessage>(
2420
+ &mut self, htlc_id_arg: u64, err_packet: Option<msgs::OnionErrorPacket>,
2421
+ mut force_holding_cell: bool, logger: &L
2422
+ ) -> Result<Option<M>, ChannelError> where L::Target: Logger {
2389
2423
if (self.context.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
2390
2424
panic!("Was asked to fail an HTLC when channel was not in an operational state");
2391
2425
}
@@ -2439,7 +2473,9 @@ impl<SP: Deref> Channel<SP> where
2439
2473
return Ok(None);
2440
2474
}
2441
2475
},
2442
- &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
2476
+ &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } |
2477
+ &HTLCUpdateAwaitingACK::FailMalformedHTLC { htlc_id } =>
2478
+ {
2443
2479
if htlc_id_arg == htlc_id {
2444
2480
debug_assert!(false, "Tried to fail an HTLC that was already failed");
2445
2481
return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID".to_owned()));
@@ -2448,25 +2484,34 @@ impl<SP: Deref> Channel<SP> where
2448
2484
_ => {}
2449
2485
}
2450
2486
}
2451
- log_trace!(logger, "Placing failure for HTLC ID {} in holding cell in channel {}.", htlc_id_arg, &self.context.channel_id());
2452
- self.context.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::FailHTLC {
2453
- htlc_id: htlc_id_arg,
2454
- err_packet,
2455
- });
2487
+ log_trace!(logger, "Placing failure for HTLC ID {} in holding cell in channel {}.", htlc_id_arg, self.context.channel_id());
2488
+ if let Some(err_packet) = err_packet {
2489
+ self.context.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::FailHTLC {
2490
+ htlc_id: htlc_id_arg,
2491
+ err_packet,
2492
+ });
2493
+ } else {
2494
+ self.context.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::FailMalformedHTLC {
2495
+ htlc_id: htlc_id_arg,
2496
+ });
2497
+ }
2456
2498
return Ok(None);
2457
2499
}
2458
2500
2459
- log_trace!(logger, "Failing HTLC ID {} back with a update_fail_htlc message in channel {}.", htlc_id_arg, &self.context.channel_id());
2501
+ log_trace!(logger, "Failing HTLC ID {} back with a update_fail_{}htlc message in channel {}.",
2502
+ htlc_id_arg, if err_packet.is_none() { "malformed_" } else { "" }, self.context.channel_id());
2460
2503
{
2461
2504
let htlc = &mut self.context.pending_inbound_htlcs[pending_idx];
2462
- htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(err_packet.clone()));
2505
+ if let Some(ref err_packet) = err_packet {
2506
+ htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(err_packet.clone()));
2507
+ } else {
2508
+ htlc.state = InboundHTLCState::LocalRemoved(
2509
+ InboundHTLCRemovalReason::FailMalformed(([0; 32], INVALID_ONION_BLINDING)));
2510
+ }
2463
2511
}
2464
2512
2465
- Ok(Some(msgs::UpdateFailHTLC {
2466
- channel_id: self.context.channel_id(),
2467
- htlc_id: htlc_id_arg,
2468
- reason: err_packet
2469
- }))
2513
+ let err_packet = err_packet.unwrap_or(msgs::OnionErrorPacket { data: Vec::new()});
2514
+ Ok(Some(M::new(htlc_id_arg, self.context.channel_id(), err_packet)))
2470
2515
}
2471
2516
2472
2517
// Message handlers:
@@ -3164,7 +3209,9 @@ impl<SP: Deref> Channel<SP> where
3164
3209
monitor_update.updates.append(&mut additional_monitor_update.updates);
3165
3210
},
3166
3211
&HTLCUpdateAwaitingACK::FailHTLC { htlc_id, ref err_packet } => {
3167
- match self.fail_htlc(htlc_id, err_packet.clone(), false, logger) {
3212
+ match self.fail_htlc::<L, msgs::UpdateFailHTLC>(
3213
+ htlc_id, Some(err_packet.clone()), false, logger)
3214
+ {
3168
3215
Ok(update_fail_msg_option) => {
3169
3216
// If an HTLC failure was previously added to the holding cell (via
3170
3217
// `queue_fail_htlc`) then generating the fail message itself must
@@ -3182,6 +3229,22 @@ impl<SP: Deref> Channel<SP> where
3182
3229
}
3183
3230
}
3184
3231
},
3232
+ &HTLCUpdateAwaitingACK::FailMalformedHTLC { htlc_id } => {
3233
+ match self.fail_htlc::<L, msgs::UpdateFailMalformedHTLC>(
3234
+ htlc_id, None, false, logger)
3235
+ {
3236
+ Ok(update_fail_malformed_opt) => {
3237
+ debug_assert!(update_fail_malformed_opt.is_some()); // See above comment
3238
+ update_fail_count += 1;
3239
+ },
3240
+ Err(e) => {
3241
+ if let ChannelError::Ignore(_) = e {}
3242
+ else {
3243
+ panic!("Got a non-IgnoreError action trying to fail holding cell HTLC");
3244
+ }
3245
+ }
3246
+ }
3247
+ },
3185
3248
}
3186
3249
}
3187
3250
if update_add_count == 0 && update_fulfill_count == 0 && update_fail_count == 0 && self.context.holding_cell_update_fee.is_none() {
@@ -6853,6 +6916,10 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
6853
6916
htlc_id.write(writer)?;
6854
6917
err_packet.write(writer)?;
6855
6918
}
6919
+ &HTLCUpdateAwaitingACK::FailMalformedHTLC { ref htlc_id } => {
6920
+ 3u8.write(writer)?;
6921
+ htlc_id.write(writer)?;
6922
+ }
6856
6923
}
6857
6924
}
6858
6925
@@ -7148,6 +7215,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7148
7215
htlc_id: Readable::read(reader)?,
7149
7216
err_packet: Readable::read(reader)?,
7150
7217
},
7218
+ 3 => HTLCUpdateAwaitingACK::FailMalformedHTLC {
7219
+ htlc_id: Readable::read(reader)?,
7220
+ },
7151
7221
_ => return Err(DecodeError::InvalidValue),
7152
7222
});
7153
7223
}
0 commit comments