@@ -62,7 +62,6 @@ use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError};
62
62
#[cfg(test)]
63
63
use crate::ln::outbound_payment;
64
64
use crate::ln::outbound_payment::{OutboundPayments, PendingOutboundPayment, RetryableInvoiceRequest, SendAlongPathArgs, StaleExpiration};
65
- use crate::ln::wire::Encode;
66
65
use crate::offers::invoice::{Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder, UnsignedBolt12Invoice};
67
66
use crate::offers::invoice_error::InvoiceError;
68
67
use crate::offers::invoice_request::{DerivedPayerSigningPubkey, InvoiceRequest, InvoiceRequestBuilder};
@@ -677,7 +676,7 @@ pub enum FailureCode {
677
676
}
678
677
679
678
impl Into<u16> for FailureCode {
680
- fn into(self) -> u16 {
679
+ fn into(self) -> u16 {
681
680
match self {
682
681
FailureCode::TemporaryNodeFailure => 0x2000 | 2,
683
682
FailureCode::RequiredNodeFeatureMissing => 0x4000 | 0x2000 | 3,
@@ -3841,43 +3840,39 @@ where
3841
3840
3842
3841
fn can_forward_htlc_to_outgoing_channel(
3843
3842
&self, chan: &mut Channel<SP>, msg: &msgs::UpdateAddHTLC, next_packet: &NextPacketDetails
3844
- ) -> Result<(), (&'static str, u16, Option<msgs::ChannelUpdate> )> {
3843
+ ) -> Result<(), (&'static str, u16)> {
3845
3844
if !chan.context.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
3846
3845
// Note that the behavior here should be identical to the above block - we
3847
3846
// should NOT reveal the existence or non-existence of a private channel if
3848
3847
// we don't allow forwards outbound over them.
3849
- return Err(("Refusing to forward to a private channel based on our config.", 0x4000 | 10, None ));
3848
+ return Err(("Refusing to forward to a private channel based on our config.", 0x4000 | 10));
3850
3849
}
3851
3850
if chan.context.get_channel_type().supports_scid_privacy() && next_packet.outgoing_scid != chan.context.outbound_scid_alias() {
3852
3851
// `option_scid_alias` (referred to in LDK as `scid_privacy`) means
3853
3852
// "refuse to forward unless the SCID alias was used", so we pretend
3854
3853
// we don't have the channel here.
3855
- return Err(("Refusing to forward over real channel SCID as our counterparty requested.", 0x4000 | 10, None ));
3854
+ return Err(("Refusing to forward over real channel SCID as our counterparty requested.", 0x4000 | 10));
3856
3855
}
3857
3856
3858
3857
// Note that we could technically not return an error yet here and just hope
3859
3858
// that the connection is reestablished or monitor updated by the time we get
3860
3859
// around to doing the actual forward, but better to fail early if we can and
3861
3860
// hopefully an attacker trying to path-trace payments cannot make this occur
3862
3861
// on a small/per-node/per-channel scale.
3863
- if !chan.context.is_live() { // channel_disabled
3864
- // If the channel_update we're going to return is disabled (i.e. the
3865
- // peer has been disabled for some time), return `channel_disabled`,
3866
- // otherwise return `temporary_channel_failure`.
3867
- let chan_update_opt = self.get_channel_update_for_onion(next_packet.outgoing_scid, chan).ok();
3868
- if chan_update_opt.as_ref().map(|u| u.contents.channel_flags & 2 == 2).unwrap_or(false) {
3869
- return Err(("Forwarding channel has been disconnected for some time.", 0x1000 | 20, chan_update_opt));
3862
+ if !chan.context.is_live() {
3863
+ if !chan.context.is_enabled() {
3864
+ // channel_disabled
3865
+ return Err(("Forwarding channel has been disconnected for some time.", 0x1000 | 20));
3870
3866
} else {
3871
- return Err(("Forwarding channel is not in a ready state.", 0x1000 | 7, chan_update_opt));
3867
+ // temporary_channel_failure
3868
+ return Err(("Forwarding channel is not in a ready state.", 0x1000 | 7));
3872
3869
}
3873
3870
}
3874
3871
if next_packet.outgoing_amt_msat < chan.context.get_counterparty_htlc_minimum_msat() { // amount_below_minimum
3875
- let chan_update_opt = self.get_channel_update_for_onion(next_packet.outgoing_scid, chan).ok();
3876
- return Err(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11, chan_update_opt));
3872
+ return Err(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11));
3877
3873
}
3878
3874
if let Err((err, code)) = chan.htlc_satisfies_config(msg, next_packet.outgoing_amt_msat, next_packet.outgoing_cltv_value) {
3879
- let chan_update_opt = self.get_channel_update_for_onion(next_packet.outgoing_scid, chan).ok();
3880
- return Err((err, code, chan_update_opt));
3875
+ return Err((err, code));
3881
3876
}
3882
3877
3883
3878
Ok(())
@@ -3909,7 +3904,7 @@ where
3909
3904
3910
3905
fn can_forward_htlc(
3911
3906
&self, msg: &msgs::UpdateAddHTLC, next_packet_details: &NextPacketDetails
3912
- ) -> Result<(), (&'static str, u16, Option<msgs::ChannelUpdate> )> {
3907
+ ) -> Result<(), (&'static str, u16)> {
3913
3908
match self.do_funded_channel_callback(next_packet_details.outgoing_scid, |chan: &mut Channel<SP>| {
3914
3909
self.can_forward_htlc_to_outgoing_channel(chan, msg, next_packet_details)
3915
3910
}) {
@@ -3922,7 +3917,7 @@ where
3922
3917
fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, next_packet_details.outgoing_scid, &self.chain_hash)) ||
3923
3918
fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, next_packet_details.outgoing_scid, &self.chain_hash)
3924
3919
{} else {
3925
- return Err(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None ));
3920
+ return Err(("Don't have available channel for forwarding as requested.", 0x4000 | 10));
3926
3921
}
3927
3922
}
3928
3923
}
@@ -3931,23 +3926,20 @@ where
3931
3926
if let Err((err_msg, err_code)) = check_incoming_htlc_cltv(
3932
3927
cur_height, next_packet_details.outgoing_cltv_value, msg.cltv_expiry
3933
3928
) {
3934
- let chan_update_opt = self.do_funded_channel_callback(next_packet_details.outgoing_scid, |chan: &mut Channel<SP>| {
3935
- self.get_channel_update_for_onion(next_packet_details.outgoing_scid, chan).ok()
3936
- }).flatten();
3937
- return Err((err_msg, err_code, chan_update_opt));
3929
+ return Err((err_msg, err_code));
3938
3930
}
3939
3931
3940
3932
Ok(())
3941
3933
}
3942
3934
3943
3935
fn htlc_failure_from_update_add_err(
3944
3936
&self, msg: &msgs::UpdateAddHTLC, counterparty_node_id: &PublicKey, err_msg: &'static str,
3945
- mut err_code: u16, chan_update: Option<msgs::ChannelUpdate> , is_intro_node_blinded_forward: bool,
3937
+ err_code: u16, is_intro_node_blinded_forward: bool,
3946
3938
shared_secret: &[u8; 32]
3947
3939
) -> HTLCFailureMsg {
3948
- let mut res = VecWriter(Vec::with_capacity(chan_update.serialized_length() + 2 + 8 + 2));
3949
- if chan_update.is_some() && err_code & 0x1000 == 0x1000 {
3950
- let chan_update = chan_update.unwrap();
3940
+ // at capacity, we write fields `htlc_msat` and `len`
3941
+ let mut res = VecWriter(Vec::with_capacity(8 + 2));
3942
+ if err_code & 0x1000 == 0x1000 {
3951
3943
if err_code == 0x1000 | 11 || err_code == 0x1000 | 12 {
3952
3944
msg.amount_msat.write(&mut res).expect("Writes cannot fail");
3953
3945
}
@@ -3958,15 +3950,8 @@ where
3958
3950
// TODO: underspecified, follow https://github.com/lightning/bolts/issues/791
3959
3951
0u16.write(&mut res).expect("Writes cannot fail");
3960
3952
}
3961
- (chan_update.serialized_length() as u16 + 2).write(&mut res).expect("Writes cannot fail");
3962
- msgs::ChannelUpdate::TYPE.write(&mut res).expect("Writes cannot fail");
3963
- chan_update.write(&mut res).expect("Writes cannot fail");
3964
- } else if err_code & 0x1000 == 0x1000 {
3965
- // If we're trying to return an error that requires a `channel_update` but
3966
- // we're forwarding to a phantom or intercept "channel" (i.e. cannot
3967
- // generate an update), just use the generic "temporary_node_failure"
3968
- // instead.
3969
- err_code = 0x2000 | 2;
3953
+ // See https://github.com/lightning/bolts/blob/247e83d/04-onion-routing.md?plain=1#L1414-L1415
3954
+ (0u16).write(&mut res).expect("Writes cannot fail");
3970
3955
}
3971
3956
3972
3957
log_info!(
@@ -4014,9 +3999,9 @@ where
4014
3999
// Perform outbound checks here instead of in [`Self::construct_pending_htlc_info`] because we
4015
4000
// can't hold the outbound peer state lock at the same time as the inbound peer state lock.
4016
4001
self.can_forward_htlc(&msg, &next_packet_details).map_err(|e| {
4017
- let (err_msg, err_code, chan_update_opt ) = e;
4002
+ let (err_msg, err_code) = e;
4018
4003
self.htlc_failure_from_update_add_err(
4019
- msg, counterparty_node_id, err_msg, err_code, chan_update_opt,
4004
+ msg, counterparty_node_id, err_msg, err_code,
4020
4005
next_hop.is_intro_node_blinded_forward(), &shared_secret
4021
4006
)
4022
4007
})?;
@@ -4125,20 +4110,10 @@ where
4125
4110
Some(id) => id,
4126
4111
};
4127
4112
4128
- self.get_channel_update_for_onion(short_channel_id, chan)
4129
- }
4130
-
4131
- fn get_channel_update_for_onion(&self, short_channel_id: u64, chan: &Channel<SP>) -> Result<msgs::ChannelUpdate, LightningError> {
4132
4113
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
4133
4114
log_trace!(logger, "Generating channel update for channel {}", chan.context.channel_id());
4134
4115
let were_node_one = self.our_network_pubkey.serialize()[..] < chan.context.get_counterparty_node_id().serialize()[..];
4135
-
4136
- let enabled = chan.context.is_usable() && match chan.channel_update_status() {
4137
- ChannelUpdateStatus::Enabled => true,
4138
- ChannelUpdateStatus::DisabledStaged(_) => true,
4139
- ChannelUpdateStatus::Disabled => false,
4140
- ChannelUpdateStatus::EnabledStaged(_) => false,
4141
- };
4116
+ let enabled = chan.context.is_enabled();
4142
4117
4143
4118
let unsigned = msgs::UnsignedChannelUpdate {
4144
4119
chain_hash: self.chain_hash,
@@ -5326,16 +5301,9 @@ where
5326
5301
}) {
5327
5302
Some(Ok(_)) => {},
5328
5303
Some(Err((err, code))) => {
5329
- let outgoing_chan_update_opt = if let Some(outgoing_scid) = outgoing_scid_opt.as_ref() {
5330
- self.do_funded_channel_callback(*outgoing_scid, |chan: &mut Channel<SP>| {
5331
- self.get_channel_update_for_onion(*outgoing_scid, chan).ok()
5332
- }).flatten()
5333
- } else {
5334
- None
5335
- };
5336
5304
let htlc_fail = self.htlc_failure_from_update_add_err(
5337
5305
&update_add_htlc, &incoming_counterparty_node_id, err, code,
5338
- outgoing_chan_update_opt, is_intro_node_blinded_forward, &shared_secret,
5306
+ is_intro_node_blinded_forward, &shared_secret,
5339
5307
);
5340
5308
let htlc_destination = get_failed_htlc_destination(outgoing_scid_opt, update_add_htlc.payment_hash);
5341
5309
htlc_fails.push((htlc_fail, htlc_destination));
@@ -5347,12 +5315,12 @@ where
5347
5315
5348
5316
// Now process the HTLC on the outgoing channel if it's a forward.
5349
5317
if let Some(next_packet_details) = next_packet_details_opt.as_ref() {
5350
- if let Err((err, code, chan_update_opt )) = self.can_forward_htlc(
5318
+ if let Err((err, code)) = self.can_forward_htlc(
5351
5319
&update_add_htlc, next_packet_details
5352
5320
) {
5353
5321
let htlc_fail = self.htlc_failure_from_update_add_err(
5354
5322
&update_add_htlc, &incoming_counterparty_node_id, err, code,
5355
- chan_update_opt, is_intro_node_blinded_forward, &shared_secret,
5323
+ is_intro_node_blinded_forward, &shared_secret,
5356
5324
);
5357
5325
let htlc_destination = get_failed_htlc_destination(outgoing_scid_opt, update_add_htlc.payment_hash);
5358
5326
htlc_fails.push((htlc_fail, htlc_destination));
@@ -5633,7 +5601,8 @@ where
5633
5601
}
5634
5602
5635
5603
if let Some(ChannelPhase::Funded(ref mut chan)) = peer_state.channel_by_id.get_mut(&forward_chan_id) {
5636
- let (failure_code, data) = self.get_htlc_temp_fail_err_and_data(0x1000|7, short_chan_id, chan);
5604
+ let failure_code = 0x1000|7;
5605
+ let data = self.get_htlc_inbound_temp_fail_data(failure_code);
5637
5606
failed_forwards.push((htlc_source, payment_hash,
5638
5607
HTLCFailReason::reason(failure_code, data),
5639
5608
HTLCDestination::NextHopChannel { node_id: Some(chan.context.get_counterparty_node_id()), channel_id: forward_chan_id }
@@ -6448,46 +6417,21 @@ where
6448
6417
///
6449
6418
/// This is for failures on the channel on which the HTLC was *received*, not failures
6450
6419
/// forwarding
6451
- fn get_htlc_inbound_temp_fail_err_and_data(&self, desired_err_code: u16, chan: &Channel<SP>) -> (u16, Vec<u8>) {
6452
- // We can't be sure what SCID was used when relaying inbound towards us, so we have to
6453
- // guess somewhat. If its a public channel, we figure best to just use the real SCID (as
6454
- // we're not leaking that we have a channel with the counterparty), otherwise we try to use
6455
- // an inbound SCID alias before the real SCID.
6456
- let scid_pref = if chan.context.should_announce() {
6457
- chan.context.get_short_channel_id().or(chan.context.latest_inbound_scid_alias())
6458
- } else {
6459
- chan.context.latest_inbound_scid_alias().or(chan.context.get_short_channel_id())
6460
- };
6461
- if let Some(scid) = scid_pref {
6462
- self.get_htlc_temp_fail_err_and_data(desired_err_code, scid, chan)
6463
- } else {
6464
- (0x4000|10, Vec::new())
6465
- }
6466
- }
6467
-
6468
-
6469
- /// Gets an HTLC onion failure code and error data for an `UPDATE` error, given the error code
6470
- /// that we want to return and a channel.
6471
- fn get_htlc_temp_fail_err_and_data(&self, desired_err_code: u16, scid: u64, chan: &Channel<SP>) -> (u16, Vec<u8>) {
6472
- debug_assert_eq!(desired_err_code & 0x1000, 0x1000);
6473
- if let Ok(upd) = self.get_channel_update_for_onion(scid, chan) {
6474
- let mut enc = VecWriter(Vec::with_capacity(upd.serialized_length() + 6));
6475
- if desired_err_code == 0x1000 | 20 {
6476
- // No flags for `disabled_flags` are currently defined so they're always two zero bytes.
6477
- // See https://github.com/lightning/bolts/blob/341ec84/04-onion-routing.md?plain=1#L1008
6478
- 0u16.write(&mut enc).expect("Writes cannot fail");
6479
- }
6480
- (upd.serialized_length() as u16 + 2).write(&mut enc).expect("Writes cannot fail");
6481
- msgs::ChannelUpdate::TYPE.write(&mut enc).expect("Writes cannot fail");
6482
- upd.write(&mut enc).expect("Writes cannot fail");
6483
- (desired_err_code, enc.0)
6484
- } else {
6485
- // If we fail to get a unicast channel_update, it implies we don't yet have an SCID,
6486
- // which means we really shouldn't have gotten a payment to be forwarded over this
6487
- // channel yet, or if we did it's from a route hint. Either way, returning an error of
6488
- // PERM|no_such_channel should be fine.
6489
- (0x4000|10, Vec::new())
6490
- }
6420
+ fn get_htlc_inbound_temp_fail_data(&self, err_code: u16) -> Vec<u8> {
6421
+ debug_assert_eq!(err_code & 0x1000, 0x1000);
6422
+ debug_assert_ne!(err_code, 0x1000|11);
6423
+ debug_assert_ne!(err_code, 0x1000|12);
6424
+ debug_assert_ne!(err_code, 0x1000|13);
6425
+ // at capacity, we write fields `disabled_flags` and `len`
6426
+ let mut enc = VecWriter(Vec::with_capacity(4));
6427
+ if err_code == 0x1000 | 20 {
6428
+ // No flags for `disabled_flags` are currently defined so they're always two zero bytes.
6429
+ // See https://github.com/lightning/bolts/blob/341ec84/04-onion-routing.md?plain=1#L1008
6430
+ 0u16.write(&mut enc).expect("Writes cannot fail");
6431
+ }
6432
+ // See https://github.com/lightning/bolts/blob/247e83d/04-onion-routing.md?plain=1#L1414-L1415
6433
+ (0u16).write(&mut enc).expect("Writes cannot fail");
6434
+ enc.0
6491
6435
}
6492
6436
6493
6437
// Fail a list of HTLCs that were just freed from the holding cell. The HTLCs need to be
@@ -6504,8 +6448,10 @@ where
6504
6448
let peer_state = &mut *peer_state_lock;
6505
6449
match peer_state.channel_by_id.entry(channel_id) {
6506
6450
hash_map::Entry::Occupied(chan_phase_entry) => {
6507
- if let ChannelPhase::Funded(chan) = chan_phase_entry.get() {
6508
- self.get_htlc_inbound_temp_fail_err_and_data(0x1000|7, &chan)
6451
+ if let ChannelPhase::Funded(_chan) = chan_phase_entry.get() {
6452
+ let failure_code = 0x1000|7;
6453
+ let data = self.get_htlc_inbound_temp_fail_data(failure_code);
6454
+ (failure_code, data)
6509
6455
} else {
6510
6456
// We shouldn't be trying to fail holding cell HTLCs on an unfunded channel.
6511
6457
debug_assert!(false);
@@ -8164,8 +8110,8 @@ where
8164
8110
let reason = if routing.blinded_failure().is_some() {
8165
8111
HTLCFailReason::reason(INVALID_ONION_BLINDING, vec![0; 32])
8166
8112
} else if (error_code & 0x1000) != 0 {
8167
- let (real_code, error_data) = self.get_htlc_inbound_temp_fail_err_and_data (error_code, chan );
8168
- HTLCFailReason::reason(real_code , error_data)
8113
+ let error_data = self.get_htlc_inbound_temp_fail_data (error_code);
8114
+ HTLCFailReason::reason(error_code , error_data)
8169
8115
} else {
8170
8116
HTLCFailReason::from_failure_code(error_code)
8171
8117
}.get_encrypted_failure_packet(incoming_shared_secret, &None);
@@ -10190,7 +10136,8 @@ where
10190
10136
let res = f(channel);
10191
10137
if let Ok((channel_ready_opt, mut timed_out_pending_htlcs, announcement_sigs)) = res {
10192
10138
for (source, payment_hash) in timed_out_pending_htlcs.drain(..) {
10193
- let (failure_code, data) = self.get_htlc_inbound_temp_fail_err_and_data(0x1000|14 /* expiry_too_soon */, &channel);
10139
+ let failure_code = 0x1000|14; /* expiry_too_soon */
10140
+ let data = self.get_htlc_inbound_temp_fail_data(failure_code);
10194
10141
timed_out_htlcs.push((source, payment_hash, HTLCFailReason::reason(failure_code, data),
10195
10142
HTLCDestination::NextHopChannel { node_id: Some(channel.context.get_counterparty_node_id()), channel_id: channel.context.channel_id() }));
10196
10143
}
0 commit comments