Skip to content

Commit 8c7d23d

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 440c3ee commit 8c7d23d

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
@@ -269,6 +269,16 @@ pub(super) enum HTLCFailReason {
269269
}
270270
}
271271

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

18521862
for htlc_source in failed_htlcs.drain(..) {
18531863
let receiver = HTLCDestination::NextHopChannel { node_id: Some(*counterparty_node_id), channel_id: *channel_id };
1854-
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
1864+
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
18551865
}
18561866

18571867
let _ = handle_error!(self, result, *counterparty_node_id);
@@ -1909,7 +1919,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
19091919
for htlc_source in failed_htlcs.drain(..) {
19101920
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
19111921
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
1912-
self.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
1922+
self.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
19131923
}
19141924
if let Some((funding_txo, monitor_update)) = monitor_update_option {
19151925
// There isn't anything we can do if we get an update failure - we're already
@@ -3070,7 +3080,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
30703080
};
30713081

30723082
failed_forwards.push((htlc_source, payment_hash,
3073-
HTLCFailReason::Reason { failure_code: $err_code, data: $err_data },
3083+
HTLCFailReason::reason($err_code, $err_data),
30743084
reason
30753085
));
30763086
continue;
@@ -3178,7 +3188,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
31783188
}
31793189
let (failure_code, data) = self.get_htlc_temp_fail_err_and_data(0x1000|7, short_chan_id, chan.get());
31803190
failed_forwards.push((htlc_source, payment_hash,
3181-
HTLCFailReason::Reason { failure_code, data },
3191+
HTLCFailReason::reason(failure_code, data),
31823192
HTLCDestination::NextHopChannel { node_id: Some(chan.get().get_counterparty_node_id()), channel_id: forward_chan_id }
31833193
));
31843194
continue;
@@ -3325,7 +3335,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
33253335
incoming_packet_shared_secret: $htlc.prev_hop.incoming_packet_shared_secret,
33263336
phantom_shared_secret,
33273337
}), payment_hash,
3328-
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data },
3338+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
33293339
HTLCDestination::FailedPayment { payment_hash: $payment_hash },
33303340
));
33313341
}
@@ -3738,7 +3748,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
37383748

37393749
for htlc_source in timed_out_mpp_htlcs.drain(..) {
37403750
let receiver = HTLCDestination::FailedPayment { payment_hash: htlc_source.1 };
3741-
self.fail_htlc_backwards_internal(HTLCSource::PreviousHopData(htlc_source.0.clone()), &htlc_source.1, HTLCFailReason::Reason { failure_code: 23, data: Vec::new() }, receiver );
3751+
self.fail_htlc_backwards_internal(HTLCSource::PreviousHopData(htlc_source.0.clone()), &htlc_source.1, HTLCFailReason::from_failure_code(23), receiver );
37423752
}
37433753

37443754
for (err, counterparty_node_id) in handle_errors.drain(..) {
@@ -3775,7 +3785,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
37753785
self.best_block.read().unwrap().height()));
37763786
self.fail_htlc_backwards_internal(
37773787
HTLCSource::PreviousHopData(htlc.prev_hop), payment_hash,
3778-
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data },
3788+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
37793789
HTLCDestination::FailedPayment { payment_hash: *payment_hash });
37803790
}
37813791
}
@@ -3845,7 +3855,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38453855
};
38463856

38473857
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id };
3848-
self.fail_htlc_backwards_internal(htlc_src, &payment_hash, HTLCFailReason::Reason { failure_code, data: onion_failure_data }, receiver);
3858+
self.fail_htlc_backwards_internal(htlc_src, &payment_hash, HTLCFailReason::reason(failure_code, onion_failure_data), receiver);
38493859
}
38503860
}
38513861

@@ -4171,7 +4181,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41714181
self.best_block.read().unwrap().height()));
41724182
self.fail_htlc_backwards_internal(
41734183
HTLCSource::PreviousHopData(htlc.prev_hop), &payment_hash,
4174-
HTLCFailReason::Reason { failure_code: 0x4000|15, data: htlc_msat_height_data },
4184+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
41754185
HTLCDestination::FailedPayment { payment_hash } );
41764186
}
41774187
}
@@ -4864,7 +4874,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
48644874
};
48654875
for htlc_source in dropped_htlcs.drain(..) {
48664876
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id: msg.channel_id };
4867-
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
4877+
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
48684878
}
48694879

48704880
let _ = handle_error!(self, result, *counterparty_node_id);
@@ -5009,7 +5019,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
50095019
let chan_err: ChannelError = ChannelError::Close("Got update_fail_malformed_htlc with BADONION not set".to_owned());
50105020
try_chan_entry!(self, Err(chan_err), chan);
50115021
}
5012-
try_chan_entry!(self, chan.get_mut().update_fail_malformed_htlc(&msg, HTLCFailReason::Reason { failure_code: msg.failure_code, data: Vec::new() }), chan);
5022+
try_chan_entry!(self, chan.get_mut().update_fail_malformed_htlc(&msg, HTLCFailReason::from_failure_code(msg.failure_code)), chan);
50135023
Ok(())
50145024
},
50155025
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
@@ -5316,7 +5326,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
53165326
} else {
53175327
log_trace!(self.logger, "Failing HTLC with hash {} from our monitor", log_bytes!(htlc_update.payment_hash.0));
53185328
let receiver = HTLCDestination::NextHopChannel { node_id: counterparty_node_id, channel_id: funding_outpoint.to_channel_id() };
5319-
self.fail_htlc_backwards_internal(htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
5329+
self.fail_htlc_backwards_internal(htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
53205330
}
53215331
},
53225332
MonitorEvent::CommitmentTxConfirmed(funding_outpoint) |
@@ -5985,9 +5995,8 @@ where
59855995
if let Ok((channel_ready_opt, mut timed_out_pending_htlcs, announcement_sigs)) = res {
59865996
for (source, payment_hash) in timed_out_pending_htlcs.drain(..) {
59875997
let (failure_code, data) = self.get_htlc_inbound_temp_fail_err_and_data(0x1000|14 /* expiry_too_soon */, &channel);
5988-
timed_out_htlcs.push((source, payment_hash, HTLCFailReason::Reason {
5989-
failure_code, data,
5990-
}, HTLCDestination::NextHopChannel { node_id: Some(channel.get_counterparty_node_id()), channel_id: channel.channel_id() }));
5998+
timed_out_htlcs.push((source, payment_hash, HTLCFailReason::reason(failure_code, data),
5999+
HTLCDestination::NextHopChannel { node_id: Some(channel.get_counterparty_node_id()), channel_id: channel.channel_id() }));
59916000
}
59926001
if let Some(channel_ready) = channel_ready_opt {
59936002
send_channel_ready!(self, pending_msg_events, channel, channel_ready);
@@ -6074,10 +6083,9 @@ where
60746083
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
60756084
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(height));
60766085

6077-
timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(), HTLCFailReason::Reason {
6078-
failure_code: 0x4000 | 15,
6079-
data: htlc_msat_height_data
6080-
}, HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }));
6086+
timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(),
6087+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
6088+
HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }));
60816089
false
60826090
} else { true }
60836091
});
@@ -7565,7 +7573,7 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
75657573
for htlc_source in failed_htlcs.drain(..) {
75667574
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
75677575
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
7568-
channel_manager.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
7576+
channel_manager.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
75697577
}
75707578

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

0 commit comments

Comments
 (0)