Skip to content

Commit ece31e6

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 46aa613 commit ece31e6

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ pub(super) enum HTLCFailReason {
268268
}
269269
}
270270

271+
impl HTLCFailReason {
272+
pub(super) fn reason(failure_code: u16, data: Vec<u8>) -> Self {
273+
Self::Reason { failure_code, data }
274+
}
275+
276+
pub(super) fn from_failure_code(failure_code: u16) -> Self {
277+
Self::Reason { failure_code, data: Vec::new() }
278+
}
279+
}
280+
271281
struct ReceiveError {
272282
err_code: u16,
273283
err_data: Vec<u8>,
@@ -1844,7 +1854,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
18441854

18451855
for htlc_source in failed_htlcs.drain(..) {
18461856
let receiver = HTLCDestination::NextHopChannel { node_id: Some(*counterparty_node_id), channel_id: *channel_id };
1847-
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
1857+
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
18481858
}
18491859

18501860
let _ = handle_error!(self, result, *counterparty_node_id);
@@ -1902,7 +1912,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
19021912
for htlc_source in failed_htlcs.drain(..) {
19031913
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
19041914
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
1905-
self.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
1915+
self.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
19061916
}
19071917
if let Some((funding_txo, monitor_update)) = monitor_update_option {
19081918
// There isn't anything we can do if we get an update failure - we're already
@@ -3063,7 +3073,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
30633073
};
30643074

30653075
failed_forwards.push((htlc_source, payment_hash,
3066-
HTLCFailReason::Reason { failure_code: $err_code, data: $err_data },
3076+
HTLCFailReason::reason($err_code, $err_data),
30673077
reason
30683078
));
30693079
continue;
@@ -3171,7 +3181,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
31713181
}
31723182
let (failure_code, data) = self.get_htlc_temp_fail_err_and_data(0x1000|7, short_chan_id, chan.get());
31733183
failed_forwards.push((htlc_source, payment_hash,
3174-
HTLCFailReason::Reason { failure_code, data },
3184+
HTLCFailReason::reason(failure_code, data),
31753185
HTLCDestination::NextHopChannel { node_id: Some(chan.get().get_counterparty_node_id()), channel_id: forward_chan_id }
31763186
));
31773187
continue;
@@ -3318,7 +3328,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
33183328
incoming_packet_shared_secret: $htlc.prev_hop.incoming_packet_shared_secret,
33193329
phantom_shared_secret,
33203330
}), payment_hash,
3321-
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data },
3331+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
33223332
HTLCDestination::FailedPayment { payment_hash: $payment_hash },
33233333
));
33243334
}
@@ -3717,7 +3727,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
37173727

37183728
for htlc_source in timed_out_mpp_htlcs.drain(..) {
37193729
let receiver = HTLCDestination::FailedPayment { payment_hash: htlc_source.1 };
3720-
self.fail_htlc_backwards_internal(HTLCSource::PreviousHopData(htlc_source.0.clone()), &htlc_source.1, HTLCFailReason::Reason { failure_code: 23, data: Vec::new() }, receiver );
3730+
self.fail_htlc_backwards_internal(HTLCSource::PreviousHopData(htlc_source.0.clone()), &htlc_source.1, HTLCFailReason::from_failure_code(23), receiver );
37213731
}
37223732

37233733
for (err, counterparty_node_id) in handle_errors.drain(..) {
@@ -3754,7 +3764,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
37543764
self.best_block.read().unwrap().height()));
37553765
self.fail_htlc_backwards_internal(
37563766
HTLCSource::PreviousHopData(htlc.prev_hop), payment_hash,
3757-
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data },
3767+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
37583768
HTLCDestination::FailedPayment { payment_hash: *payment_hash });
37593769
}
37603770
}
@@ -3824,7 +3834,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38243834
};
38253835

38263836
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id };
3827-
self.fail_htlc_backwards_internal(htlc_src, &payment_hash, HTLCFailReason::Reason { failure_code, data: onion_failure_data }, receiver);
3837+
self.fail_htlc_backwards_internal(htlc_src, &payment_hash, HTLCFailReason::reason(failure_code, onion_failure_data), receiver);
38283838
}
38293839
}
38303840

@@ -4143,7 +4153,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41434153
self.best_block.read().unwrap().height()));
41444154
self.fail_htlc_backwards_internal(
41454155
HTLCSource::PreviousHopData(htlc.prev_hop), &payment_hash,
4146-
HTLCFailReason::Reason { failure_code: 0x4000|15, data: htlc_msat_height_data },
4156+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
41474157
HTLCDestination::FailedPayment { payment_hash } );
41484158
}
41494159
}
@@ -4840,7 +4850,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
48404850
};
48414851
for htlc_source in dropped_htlcs.drain(..) {
48424852
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id: msg.channel_id };
4843-
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
4853+
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
48444854
}
48454855

48464856
let _ = handle_error!(self, result, *counterparty_node_id);
@@ -4985,7 +4995,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
49854995
let chan_err: ChannelError = ChannelError::Close("Got update_fail_malformed_htlc with BADONION not set".to_owned());
49864996
try_chan_entry!(self, Err(chan_err), chan);
49874997
}
4988-
try_chan_entry!(self, chan.get_mut().update_fail_malformed_htlc(&msg, HTLCFailReason::Reason { failure_code: msg.failure_code, data: Vec::new() }), chan);
4998+
try_chan_entry!(self, chan.get_mut().update_fail_malformed_htlc(&msg, HTLCFailReason::from_failure_code(msg.failure_code)), chan);
49894999
Ok(())
49905000
},
49915001
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
@@ -5291,7 +5301,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
52915301
} else {
52925302
log_trace!(self.logger, "Failing HTLC with hash {} from our monitor", log_bytes!(htlc_update.payment_hash.0));
52935303
let receiver = HTLCDestination::NextHopChannel { node_id: counterparty_node_id, channel_id: funding_outpoint.to_channel_id() };
5294-
self.fail_htlc_backwards_internal(htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
5304+
self.fail_htlc_backwards_internal(htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
52955305
}
52965306
},
52975307
MonitorEvent::CommitmentTxConfirmed(funding_outpoint) |
@@ -5960,9 +5970,8 @@ where
59605970
if let Ok((channel_ready_opt, mut timed_out_pending_htlcs, announcement_sigs)) = res {
59615971
for (source, payment_hash) in timed_out_pending_htlcs.drain(..) {
59625972
let (failure_code, data) = self.get_htlc_inbound_temp_fail_err_and_data(0x1000|14 /* expiry_too_soon */, &channel);
5963-
timed_out_htlcs.push((source, payment_hash, HTLCFailReason::Reason {
5964-
failure_code, data,
5965-
}, HTLCDestination::NextHopChannel { node_id: Some(channel.get_counterparty_node_id()), channel_id: channel.channel_id() }));
5973+
timed_out_htlcs.push((source, payment_hash, HTLCFailReason::reason(failure_code, data),
5974+
HTLCDestination::NextHopChannel { node_id: Some(channel.get_counterparty_node_id()), channel_id: channel.channel_id() }));
59665975
}
59675976
if let Some(channel_ready) = channel_ready_opt {
59685977
send_channel_ready!(self, pending_msg_events, channel, channel_ready);
@@ -6049,10 +6058,9 @@ where
60496058
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
60506059
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(height));
60516060

6052-
timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(), HTLCFailReason::Reason {
6053-
failure_code: 0x4000 | 15,
6054-
data: htlc_msat_height_data
6055-
}, HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }));
6061+
timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(),
6062+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
6063+
HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }));
60566064
false
60576065
} else { true }
60586066
});
@@ -7528,7 +7536,7 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
75287536
for htlc_source in failed_htlcs.drain(..) {
75297537
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
75307538
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
7531-
channel_manager.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
7539+
channel_manager.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
75327540
}
75337541

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

0 commit comments

Comments
 (0)