Skip to content

Commit 7bca3a1

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 64b9e83 commit 7bca3a1

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
}
@@ -3725,7 +3735,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
37253735

37263736
for htlc_source in timed_out_mpp_htlcs.drain(..) {
37273737
let receiver = HTLCDestination::FailedPayment { payment_hash: htlc_source.1 };
3728-
self.fail_htlc_backwards_internal(HTLCSource::PreviousHopData(htlc_source.0.clone()), &htlc_source.1, HTLCFailReason::Reason { failure_code: 23, data: Vec::new() }, receiver );
3738+
self.fail_htlc_backwards_internal(HTLCSource::PreviousHopData(htlc_source.0.clone()), &htlc_source.1, HTLCFailReason::from_failure_code(23), receiver );
37293739
}
37303740

37313741
for (err, counterparty_node_id) in handle_errors.drain(..) {
@@ -3762,7 +3772,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
37623772
self.best_block.read().unwrap().height()));
37633773
self.fail_htlc_backwards_internal(
37643774
HTLCSource::PreviousHopData(htlc.prev_hop), payment_hash,
3765-
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data },
3775+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
37663776
HTLCDestination::FailedPayment { payment_hash: *payment_hash });
37673777
}
37683778
}
@@ -3832,7 +3842,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38323842
};
38333843

38343844
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id };
3835-
self.fail_htlc_backwards_internal(htlc_src, &payment_hash, HTLCFailReason::Reason { failure_code, data: onion_failure_data }, receiver);
3845+
self.fail_htlc_backwards_internal(htlc_src, &payment_hash, HTLCFailReason::reason(failure_code, onion_failure_data), receiver);
38363846
}
38373847
}
38383848

@@ -4158,7 +4168,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41584168
self.best_block.read().unwrap().height()));
41594169
self.fail_htlc_backwards_internal(
41604170
HTLCSource::PreviousHopData(htlc.prev_hop), &payment_hash,
4161-
HTLCFailReason::Reason { failure_code: 0x4000|15, data: htlc_msat_height_data },
4171+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
41624172
HTLCDestination::FailedPayment { payment_hash } );
41634173
}
41644174
}
@@ -4856,7 +4866,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
48564866
};
48574867
for htlc_source in dropped_htlcs.drain(..) {
48584868
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id: msg.channel_id };
4859-
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
4869+
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
48604870
}
48614871

48624872
let _ = handle_error!(self, result, *counterparty_node_id);
@@ -5001,7 +5011,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
50015011
let chan_err: ChannelError = ChannelError::Close("Got update_fail_malformed_htlc with BADONION not set".to_owned());
50025012
try_chan_entry!(self, Err(chan_err), chan);
50035013
}
5004-
try_chan_entry!(self, chan.get_mut().update_fail_malformed_htlc(&msg, HTLCFailReason::Reason { failure_code: msg.failure_code, data: Vec::new() }), chan);
5014+
try_chan_entry!(self, chan.get_mut().update_fail_malformed_htlc(&msg, HTLCFailReason::from_failure_code(msg.failure_code)), chan);
50055015
Ok(())
50065016
},
50075017
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
@@ -5307,7 +5317,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
53075317
} else {
53085318
log_trace!(self.logger, "Failing HTLC with hash {} from our monitor", log_bytes!(htlc_update.payment_hash.0));
53095319
let receiver = HTLCDestination::NextHopChannel { node_id: counterparty_node_id, channel_id: funding_outpoint.to_channel_id() };
5310-
self.fail_htlc_backwards_internal(htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
5320+
self.fail_htlc_backwards_internal(htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
53115321
}
53125322
},
53135323
MonitorEvent::CommitmentTxConfirmed(funding_outpoint) |
@@ -5976,9 +5986,8 @@ where
59765986
if let Ok((channel_ready_opt, mut timed_out_pending_htlcs, announcement_sigs)) = res {
59775987
for (source, payment_hash) in timed_out_pending_htlcs.drain(..) {
59785988
let (failure_code, data) = self.get_htlc_inbound_temp_fail_err_and_data(0x1000|14 /* expiry_too_soon */, &channel);
5979-
timed_out_htlcs.push((source, payment_hash, HTLCFailReason::Reason {
5980-
failure_code, data,
5981-
}, HTLCDestination::NextHopChannel { node_id: Some(channel.get_counterparty_node_id()), channel_id: channel.channel_id() }));
5989+
timed_out_htlcs.push((source, payment_hash, HTLCFailReason::reason(failure_code, data),
5990+
HTLCDestination::NextHopChannel { node_id: Some(channel.get_counterparty_node_id()), channel_id: channel.channel_id() }));
59825991
}
59835992
if let Some(channel_ready) = channel_ready_opt {
59845993
send_channel_ready!(self, pending_msg_events, channel, channel_ready);
@@ -6065,10 +6074,9 @@ where
60656074
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
60666075
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(height));
60676076

6068-
timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(), HTLCFailReason::Reason {
6069-
failure_code: 0x4000 | 15,
6070-
data: htlc_msat_height_data
6071-
}, HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }));
6077+
timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(),
6078+
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
6079+
HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }));
60726080
false
60736081
} else { true }
60746082
});
@@ -7552,7 +7560,7 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
75527560
for htlc_source in failed_htlcs.drain(..) {
75537561
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
75547562
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
7555-
channel_manager.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() }, receiver);
7563+
channel_manager.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
75567564
}
75577565

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

0 commit comments

Comments
 (0)