Skip to content

Commit 555cb40

Browse files
committed
Add constructors to HTLCFailReason
We create `HTLCFailReason` inline in function calls in a bunch of places in the `channelmanager` module, we can make the code more terse with no loss of clarity by implementing a couple of constructor methods.
1 parent fb6e018 commit 555cb40

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,16 @@ pub(super) enum HTLCFailReason {
287287
}
288288
}
289289

290+
impl HTLCFailReason {
291+
pub(super) fn reason(failure_code: u16, data: Vec<u8>) -> Self {
292+
Self::Reason { failure_code, data }
293+
}
294+
295+
pub(super) fn from_failure_code(failure_code: u16) -> Self {
296+
Self::Reason { failure_code, data: Vec::new() }
297+
}
298+
}
299+
290300
struct ReceiveError {
291301
err_code: u16,
292302
err_data: Vec<u8>,
@@ -1877,7 +1887,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
18771887

18781888
for htlc_source in failed_htlcs.drain(..) {
18791889
let receiver = HTLCDestination::NextHopChannel { node_id: Some(*counterparty_node_id), channel_id: *channel_id };
1880-
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
1890+
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
18811891
}
18821892

18831893
let _ = handle_error!(self, result, *counterparty_node_id);
@@ -1935,7 +1945,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
19351945
for htlc_source in failed_htlcs.drain(..) {
19361946
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
19371947
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
1938-
self.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
1948+
self.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
19391949
}
19401950
if let Some((funding_txo, monitor_update)) = monitor_update_option {
19411951
// There isn't anything we can do if we get an update failure - we're already
@@ -3140,7 +3150,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
31403150
phantom_shared_secret: None,
31413151
});
31423152

3143-
let failure_reason = HTLCFailReason::Reason { failure_code: 0x4000 | 10, data: Vec::new() };
3153+
let failure_reason = HTLCFailReason::from_failure_code(0x4000 | 10);
31443154
let destination = HTLCDestination::UnknownNextHop { requested_forward_scid: short_channel_id };
31453155
self.fail_htlc_backwards_internal(htlc_source, &payment.forward_info.payment_hash, failure_reason, destination);
31463156
} else { unreachable!() } // Only `PendingHTLCRouting::Forward`s are intercepted
@@ -3195,7 +3205,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
31953205
};
31963206

31973207
failed_forwards.push((htlc_source, payment_hash,
3198-
HTLCFailReason::Reason { failure_code: $err_code, data: $err_data },
3208+
HTLCFailReason::reason($err_code, $err_data),
31993209
reason
32003210
));
32013211
continue;
@@ -3303,7 +3313,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
33033313
}
33043314
let (failure_code, data) = self.get_htlc_temp_fail_err_and_data(0x1000|7, short_chan_id, chan.get());
33053315
failed_forwards.push((htlc_source, payment_hash,
3306-
HTLCFailReason::Reason { failure_code, data },
3316+
HTLCFailReason::reason(failure_code, data),
33073317
HTLCDestination::NextHopChannel { node_id: Some(chan.get().get_counterparty_node_id()), channel_id: forward_chan_id }
33083318
));
33093319
continue;
@@ -3450,7 +3460,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34503460
incoming_packet_shared_secret: $htlc.prev_hop.incoming_packet_shared_secret,
34513461
phantom_shared_secret,
34523462
}), payment_hash,
3453-
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data },
3463+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
34543464
HTLCDestination::FailedPayment { payment_hash: $payment_hash },
34553465
));
34563466
}
@@ -3863,7 +3873,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38633873

38643874
for htlc_source in timed_out_mpp_htlcs.drain(..) {
38653875
let receiver = HTLCDestination::FailedPayment { payment_hash: htlc_source.1 };
3866-
self.fail_htlc_backwards_internal(HTLCSource::PreviousHopData(htlc_source.0.clone()), &htlc_source.1, HTLCFailReason::Reason { failure_code: 23, data: Vec::new() }, receiver );
3876+
self.fail_htlc_backwards_internal(HTLCSource::PreviousHopData(htlc_source.0.clone()), &htlc_source.1, HTLCFailReason::from_failure_code(23), receiver);
38673877
}
38683878

38693879
for (err, counterparty_node_id) in handle_errors.drain(..) {
@@ -3900,7 +3910,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
39003910
self.best_block.read().unwrap().height()));
39013911
self.fail_htlc_backwards_internal(
39023912
HTLCSource::PreviousHopData(htlc.prev_hop), payment_hash,
3903-
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data },
3913+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
39043914
HTLCDestination::FailedPayment { payment_hash: *payment_hash });
39053915
}
39063916
}
@@ -3970,7 +3980,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
39703980
};
39713981

39723982
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id };
3973-
self.fail_htlc_backwards_internal(htlc_src, &payment_hash, HTLCFailReason::Reason { failure_code, data: onion_failure_data }, receiver);
3983+
self.fail_htlc_backwards_internal(htlc_src, &payment_hash, HTLCFailReason::reason(failure_code, onion_failure_data), receiver);
39743984
}
39753985
}
39763986

@@ -4296,7 +4306,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42964306
self.best_block.read().unwrap().height()));
42974307
self.fail_htlc_backwards_internal(
42984308
HTLCSource::PreviousHopData(htlc.prev_hop), &payment_hash,
4299-
HTLCFailReason::Reason { failure_code: 0x4000|15, data: htlc_msat_height_data },
4309+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
43004310
HTLCDestination::FailedPayment { payment_hash } );
43014311
}
43024312
}
@@ -4989,7 +4999,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
49894999
};
49905000
for htlc_source in dropped_htlcs.drain(..) {
49915001
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id: msg.channel_id };
4992-
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
5002+
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
49935003
}
49945004

49955005
let _ = handle_error!(self, result, *counterparty_node_id);
@@ -5134,7 +5144,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
51345144
let chan_err: ChannelError = ChannelError::Close("Got update_fail_malformed_htlc with BADONION not set".to_owned());
51355145
try_chan_entry!(self, Err(chan_err), chan);
51365146
}
5137-
try_chan_entry!(self, chan.get_mut().update_fail_malformed_htlc(&msg, HTLCFailReason::Reason { failure_code: msg.failure_code, data: Vec::new() }), chan);
5147+
try_chan_entry!(self, chan.get_mut().update_fail_malformed_htlc(&msg, HTLCFailReason::from_failure_code(msg.failure_code)), chan);
51385148
Ok(())
51395149
},
51405150
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
@@ -5240,7 +5250,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
52405250
});
52415251

52425252
failed_intercept_forwards.push((htlc_source, forward_info.payment_hash,
5243-
HTLCFailReason::Reason { failure_code: 0x4000 | 10, data: Vec::new() },
5253+
HTLCFailReason::from_failure_code(0x4000 | 10),
52445254
HTLCDestination::InvalidForward { requested_forward_scid: scid },
52455255
));
52465256
}
@@ -5495,7 +5505,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
54955505
} else {
54965506
log_trace!(self.logger, "Failing HTLC with hash {} from our monitor", log_bytes!(htlc_update.payment_hash.0));
54975507
let receiver = HTLCDestination::NextHopChannel { node_id: counterparty_node_id, channel_id: funding_outpoint.to_channel_id() };
5498-
self.fail_htlc_backwards_internal(htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
5508+
self.fail_htlc_backwards_internal(htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
54995509
}
55005510
},
55015511
MonitorEvent::CommitmentTxConfirmed(funding_outpoint) |
@@ -6181,9 +6191,8 @@ where
61816191
if let Ok((channel_ready_opt, mut timed_out_pending_htlcs, announcement_sigs)) = res {
61826192
for (source, payment_hash) in timed_out_pending_htlcs.drain(..) {
61836193
let (failure_code, data) = self.get_htlc_inbound_temp_fail_err_and_data(0x1000|14 /* expiry_too_soon */, &channel);
6184-
timed_out_htlcs.push((source, payment_hash, HTLCFailReason::Reason {
6185-
failure_code, data,
6186-
}, HTLCDestination::NextHopChannel { node_id: Some(channel.get_counterparty_node_id()), channel_id: channel.channel_id() }));
6194+
timed_out_htlcs.push((source, payment_hash, HTLCFailReason::reason(failure_code, data),
6195+
HTLCDestination::NextHopChannel { node_id: Some(channel.get_counterparty_node_id()), channel_id: channel.channel_id() }));
61876196
}
61886197
if let Some(channel_ready) = channel_ready_opt {
61896198
send_channel_ready!(self, pending_msg_events, channel, channel_ready);
@@ -6269,10 +6278,10 @@ where
62696278
if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
62706279
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
62716280
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(height));
6272-
timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(), HTLCFailReason::Reason {
6273-
failure_code: 0x4000 | 15,
6274-
data: htlc_msat_height_data
6275-
}, HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }));
6281+
6282+
timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(),
6283+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
6284+
HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }));
62766285
false
62776286
} else { true }
62786287
});
@@ -6295,7 +6304,7 @@ where
62956304
_ => unreachable!(),
62966305
};
62976306
timed_out_htlcs.push((prev_hop_data, htlc.forward_info.payment_hash,
6298-
HTLCFailReason::Reason { failure_code: 0x2000 | 2, data: Vec::new() },
6307+
HTLCFailReason::from_failure_code(0x2000 | 2),
62996308
HTLCDestination::InvalidForward { requested_forward_scid }));
63006309
log_trace!(self.logger, "Timing out intercepted HTLC with requested forward scid {}", requested_forward_scid);
63016310
false
@@ -7793,7 +7802,7 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
77937802
for htlc_source in failed_htlcs.drain(..) {
77947803
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
77957804
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
7796-
channel_manager.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
7805+
channel_manager.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
77977806
}
77987807

77997808
//TODO: Broadcast channel update for closed channels, but only after we've made a

0 commit comments

Comments
 (0)