Skip to content

Commit 2917666

Browse files
DRY Channel::fail_htlc handling on holding cell free.
1 parent 9856fb6 commit 2917666

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

lightning/src/ln/channel.rs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3576,7 +3576,7 @@ impl<SP: Deref> Channel<SP> where
35763576
// the limit. In case it's less rare than I anticipate, we may want to revisit
35773577
// handling this case better and maybe fulfilling some of the HTLCs while attempting
35783578
// to rebalance channels.
3579-
match &htlc_update {
3579+
let fail_htlc_res = match &htlc_update {
35803580
&HTLCUpdateAwaitingACK::AddHTLC {
35813581
amount_msat, cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet,
35823582
skimmed_fee_msat, blinding_point, ..
@@ -3604,6 +3604,7 @@ impl<SP: Deref> Channel<SP> where
36043604
}
36053605
}
36063606
}
3607+
None
36073608
},
36083609
&HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, htlc_id, .. } => {
36093610
// If an HTLC claim was previously added to the holding cell (via
@@ -3617,40 +3618,34 @@ impl<SP: Deref> Channel<SP> where
36173618
{ monitor_update } else { unreachable!() };
36183619
update_fulfill_count += 1;
36193620
monitor_update.updates.append(&mut additional_monitor_update.updates);
3621+
None
36203622
},
36213623
&HTLCUpdateAwaitingACK::FailHTLC { htlc_id, ref err_packet } => {
3622-
match self.fail_htlc(htlc_id, err_packet.clone(), false, logger) {
3623-
Ok(update_fail_msg_option) => {
3624-
// If an HTLC failure was previously added to the holding cell (via
3625-
// `queue_fail_htlc`) then generating the fail message itself must
3626-
// not fail - we should never end up in a state where we double-fail
3627-
// an HTLC or fail-then-claim an HTLC as it indicates we didn't wait
3628-
// for a full revocation before failing.
3629-
debug_assert!(update_fail_msg_option.is_some());
3630-
update_fail_count += 1;
3631-
},
3632-
Err(e) => {
3633-
if let ChannelError::Ignore(_) = e {}
3634-
else {
3635-
panic!("Got a non-IgnoreError action trying to fail holding cell HTLC");
3636-
}
3637-
}
3638-
}
3624+
Some(self.fail_htlc(htlc_id, err_packet.clone(), false, logger)
3625+
.map(|fail_msg_opt| fail_msg_opt.map(|_| ())))
36393626
},
36403627
&HTLCUpdateAwaitingACK::FailMalformedHTLC { htlc_id, failure_code, sha256_of_onion } => {
3641-
match self.fail_htlc(htlc_id, (failure_code, sha256_of_onion), false, logger) {
3642-
Ok(update_fail_malformed_opt) => {
3643-
debug_assert!(update_fail_malformed_opt.is_some()); // See above comment
3644-
update_fail_count += 1;
3645-
},
3646-
Err(e) => {
3647-
if let ChannelError::Ignore(_) = e {}
3648-
else {
3649-
panic!("Got a non-IgnoreError action trying to fail holding cell HTLC");
3650-
}
3651-
}
3628+
Some(self.fail_htlc(htlc_id, (failure_code, sha256_of_onion), false, logger)
3629+
.map(|fail_msg_opt| fail_msg_opt.map(|_| ())))
3630+
}
3631+
};
3632+
match fail_htlc_res {
3633+
Some(Ok(fail_msg_opt)) => {
3634+
// If an HTLC failure was previously added to the holding cell (via
3635+
// `queue_fail_{malformed_}htlc`) then generating the fail message itself must
3636+
// not fail - we should never end up in a state where we double-fail
3637+
// an HTLC or fail-then-claim an HTLC as it indicates we didn't wait
3638+
// for a full revocation before failing.
3639+
debug_assert!(fail_msg_opt.is_some());
3640+
update_fail_count += 1;
3641+
},
3642+
Some(Err(e)) => {
3643+
if let ChannelError::Ignore(_) = e {}
3644+
else {
3645+
panic!("Got a non-IgnoreError action trying to fail holding cell HTLC");
36523646
}
36533647
},
3648+
None => {}
36543649
}
36553650
}
36563651
if update_add_count == 0 && update_fulfill_count == 0 && update_fail_count == 0 && self.context.holding_cell_update_fee.is_none() {

0 commit comments

Comments
 (0)