Skip to content

Commit 79ef565

Browse files
committed
Demacro forwarding_channel_not_found
1 parent ff279d6 commit 79ef565

File tree

1 file changed

+151
-115
lines changed

1 file changed

+151
-115
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 151 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -6393,137 +6393,155 @@ where
63936393
should_persist
63946394
}
63956395

6396-
fn process_forward_htlcs(
6397-
&self, short_chan_id: u64, pending_forwards: &mut Vec<HTLCForwardInfo>,
6398-
failed_forwards: &mut Vec<FailedHTLCForward>,
6396+
/// Fail the list of provided HTLC forwards because the channel they were to be forwarded over does no longer exist.
6397+
#[rustfmt::skip]
6398+
fn forwarding_channel_not_found(
6399+
&self, forward_infos: impl Iterator<Item = HTLCForwardInfo>, short_chan_id: u64,
6400+
forwarding_counterparty: Option<PublicKey>, failed_forwards: &mut Vec<FailedHTLCForward>,
63996401
phantom_receives: &mut Vec<PerSourcePendingForward>,
64006402
) {
6401-
let mut forwarding_counterparty = None;
6402-
macro_rules! forwarding_channel_not_found {
6403-
($forward_infos: expr) => {
6404-
for forward_info in $forward_infos {
6405-
match forward_info {
6406-
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
6407-
prev_short_channel_id, prev_htlc_id, prev_channel_id, prev_funding_outpoint,
6408-
prev_user_channel_id, prev_counterparty_node_id, forward_info: PendingHTLCInfo {
6409-
routing, incoming_shared_secret, payment_hash, outgoing_amt_msat,
6410-
outgoing_cltv_value, ..
6411-
}
6412-
}) => {
6413-
let cltv_expiry = routing.incoming_cltv_expiry();
6414-
macro_rules! failure_handler {
6415-
($msg: expr, $reason: expr, $err_data: expr, $phantom_ss: expr, $next_hop_unknown: expr) => {
6416-
let logger = WithContext::from(&self.logger, forwarding_counterparty, Some(prev_channel_id), Some(payment_hash));
6417-
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", $msg);
6418-
6419-
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
6420-
short_channel_id: prev_short_channel_id,
6421-
user_channel_id: Some(prev_user_channel_id),
6422-
channel_id: prev_channel_id,
6423-
outpoint: prev_funding_outpoint,
6424-
counterparty_node_id: prev_counterparty_node_id,
6425-
htlc_id: prev_htlc_id,
6426-
incoming_packet_shared_secret: incoming_shared_secret,
6427-
phantom_shared_secret: $phantom_ss,
6428-
blinded_failure: routing.blinded_failure(),
6429-
cltv_expiry,
6430-
});
6403+
for forward_info in forward_infos {
6404+
match forward_info {
6405+
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
6406+
prev_short_channel_id, prev_htlc_id, prev_channel_id, prev_funding_outpoint,
6407+
prev_user_channel_id, prev_counterparty_node_id, forward_info: PendingHTLCInfo {
6408+
routing, incoming_shared_secret, payment_hash, outgoing_amt_msat,
6409+
outgoing_cltv_value, ..
6410+
}
6411+
}) => {
6412+
let cltv_expiry = routing.incoming_cltv_expiry();
6413+
macro_rules! failure_handler {
6414+
($msg: expr, $reason: expr, $err_data: expr, $phantom_ss: expr, $next_hop_unknown: expr) => {
6415+
let logger = WithContext::from(&self.logger, forwarding_counterparty, Some(prev_channel_id), Some(payment_hash));
6416+
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", $msg);
6417+
6418+
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
6419+
short_channel_id: prev_short_channel_id,
6420+
user_channel_id: Some(prev_user_channel_id),
6421+
channel_id: prev_channel_id,
6422+
outpoint: prev_funding_outpoint,
6423+
counterparty_node_id: prev_counterparty_node_id,
6424+
htlc_id: prev_htlc_id,
6425+
incoming_packet_shared_secret: incoming_shared_secret,
6426+
phantom_shared_secret: $phantom_ss,
6427+
blinded_failure: routing.blinded_failure(),
6428+
cltv_expiry,
6429+
});
64316430

6432-
let reason = if $next_hop_unknown {
6433-
HTLCHandlingFailureType::InvalidForward { requested_forward_scid: short_chan_id }
6434-
} else {
6435-
HTLCHandlingFailureType::Receive{ payment_hash }
6436-
};
6431+
let reason = if $next_hop_unknown {
6432+
HTLCHandlingFailureType::InvalidForward { requested_forward_scid: short_chan_id }
6433+
} else {
6434+
HTLCHandlingFailureType::Receive{ payment_hash }
6435+
};
64376436

6438-
failed_forwards.push((htlc_source, payment_hash,
6439-
HTLCFailReason::reason($reason, $err_data),
6440-
reason
6441-
));
6442-
continue;
6443-
}
6444-
}
6445-
macro_rules! fail_forward {
6446-
($msg: expr, $reason: expr, $err_data: expr, $phantom_ss: expr) => {
6447-
{
6448-
failure_handler!($msg, $reason, $err_data, $phantom_ss, true);
6449-
}
6450-
}
6437+
failed_forwards.push((htlc_source, payment_hash,
6438+
HTLCFailReason::reason($reason, $err_data),
6439+
reason
6440+
));
6441+
continue;
6442+
}
6443+
}
6444+
macro_rules! fail_forward {
6445+
($msg: expr, $reason: expr, $err_data: expr, $phantom_ss: expr) => {
6446+
{
6447+
failure_handler!($msg, $reason, $err_data, $phantom_ss, true);
64516448
}
6452-
macro_rules! failed_payment {
6453-
($msg: expr, $reason: expr, $err_data: expr, $phantom_ss: expr) => {
6454-
{
6455-
failure_handler!($msg, $reason, $err_data, $phantom_ss, false);
6456-
}
6457-
}
6449+
}
6450+
}
6451+
macro_rules! failed_payment {
6452+
($msg: expr, $reason: expr, $err_data: expr, $phantom_ss: expr) => {
6453+
{
6454+
failure_handler!($msg, $reason, $err_data, $phantom_ss, false);
64586455
}
6459-
if let PendingHTLCRouting::Forward { ref onion_packet, .. } = routing {
6460-
let phantom_pubkey_res = self.node_signer.get_node_id(Recipient::PhantomNode);
6461-
if phantom_pubkey_res.is_ok() && fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, short_chan_id, &self.chain_hash) {
6462-
let decode_res = onion_utils::decode_next_payment_hop(
6463-
Recipient::PhantomNode, &onion_packet.public_key.unwrap(), &onion_packet.hop_data,
6464-
onion_packet.hmac, payment_hash, None, &*self.node_signer
6465-
);
6466-
let next_hop = match decode_res {
6467-
Ok(res) => res,
6468-
Err(onion_utils::OnionDecodeErr::Malformed { err_msg, reason }) => {
6469-
let sha256_of_onion = Sha256::hash(&onion_packet.hop_data).to_byte_array();
6470-
// In this scenario, the phantom would have sent us an
6471-
// `update_fail_malformed_htlc`, meaning here we encrypt the error as
6472-
// if it came from us (the second-to-last hop) but contains the sha256
6473-
// of the onion.
6474-
failed_payment!(err_msg, reason, sha256_of_onion.to_vec(), None);
6475-
},
6476-
Err(onion_utils::OnionDecodeErr::Relay { err_msg, reason, shared_secret, .. }) => {
6477-
let phantom_shared_secret = shared_secret.secret_bytes();
6478-
failed_payment!(err_msg, reason, Vec::new(), Some(phantom_shared_secret));
6479-
},
6480-
};
6481-
let phantom_shared_secret = next_hop.shared_secret().secret_bytes();
6482-
let current_height: u32 = self.best_block.read().unwrap().height;
6483-
let create_res = create_recv_pending_htlc_info(next_hop,
6484-
incoming_shared_secret, payment_hash, outgoing_amt_msat,
6485-
outgoing_cltv_value, Some(phantom_shared_secret), false, None,
6486-
current_height);
6487-
match create_res
6488-
{
6489-
Ok(info) => phantom_receives.push((
6490-
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
6491-
prev_channel_id, prev_user_channel_id, vec![(info, prev_htlc_id)]
6492-
)),
6493-
Err(InboundHTLCErr { reason, err_data, msg }) => failed_payment!(msg, reason, err_data, Some(phantom_shared_secret))
6494-
}
6495-
} else {
6496-
fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id),
6497-
LocalHTLCFailureReason::UnknownNextPeer, Vec::new(), None);
6498-
}
6499-
} else {
6500-
fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id),
6501-
LocalHTLCFailureReason::UnknownNextPeer, Vec::new(), None);
6456+
}
6457+
}
6458+
if let PendingHTLCRouting::Forward { ref onion_packet, .. } = routing {
6459+
let phantom_pubkey_res = self.node_signer.get_node_id(Recipient::PhantomNode);
6460+
if phantom_pubkey_res.is_ok() && fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, short_chan_id, &self.chain_hash) {
6461+
let decode_res = onion_utils::decode_next_payment_hop(
6462+
Recipient::PhantomNode, &onion_packet.public_key.unwrap(), &onion_packet.hop_data,
6463+
onion_packet.hmac, payment_hash, None, &*self.node_signer
6464+
);
6465+
let next_hop = match decode_res {
6466+
Ok(res) => res,
6467+
Err(onion_utils::OnionDecodeErr::Malformed { err_msg, reason }) => {
6468+
let sha256_of_onion = Sha256::hash(&onion_packet.hop_data).to_byte_array();
6469+
// In this scenario, the phantom would have sent us an
6470+
// `update_fail_malformed_htlc`, meaning here we encrypt the error as
6471+
// if it came from us (the second-to-last hop) but contains the sha256
6472+
// of the onion.
6473+
failed_payment!(err_msg, reason, sha256_of_onion.to_vec(), None);
6474+
},
6475+
Err(onion_utils::OnionDecodeErr::Relay { err_msg, reason, shared_secret, .. }) => {
6476+
let phantom_shared_secret = shared_secret.secret_bytes();
6477+
failed_payment!(err_msg, reason, Vec::new(), Some(phantom_shared_secret));
6478+
},
6479+
};
6480+
let phantom_shared_secret = next_hop.shared_secret().secret_bytes();
6481+
let current_height: u32 = self.best_block.read().unwrap().height;
6482+
let create_res = create_recv_pending_htlc_info(next_hop,
6483+
incoming_shared_secret, payment_hash, outgoing_amt_msat,
6484+
outgoing_cltv_value, Some(phantom_shared_secret), false, None,
6485+
current_height);
6486+
match create_res
6487+
{
6488+
Ok(info) => phantom_receives.push((
6489+
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
6490+
prev_channel_id, prev_user_channel_id, vec![(info, prev_htlc_id)]
6491+
)),
6492+
Err(InboundHTLCErr { reason, err_data, msg }) => failed_payment!(msg, reason, err_data, Some(phantom_shared_secret))
65026493
}
6503-
},
6504-
HTLCForwardInfo::FailHTLC { .. } | HTLCForwardInfo::FailMalformedHTLC { .. } => {
6505-
// Channel went away before we could fail it. This implies
6506-
// the channel is now on chain and our counterparty is
6507-
// trying to broadcast the HTLC-Timeout, but that's their
6508-
// problem, not ours.
6494+
} else {
6495+
fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id),
6496+
LocalHTLCFailureReason::UnknownNextPeer, Vec::new(), None);
65096497
}
6498+
} else {
6499+
fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id),
6500+
LocalHTLCFailureReason::UnknownNextPeer, Vec::new(), None);
65106501
}
6502+
},
6503+
HTLCForwardInfo::FailHTLC { .. } | HTLCForwardInfo::FailMalformedHTLC { .. } => {
6504+
// Channel went away before we could fail it. This implies
6505+
// the channel is now on chain and our counterparty is
6506+
// trying to broadcast the HTLC-Timeout, but that's their
6507+
// problem, not ours.
65116508
}
65126509
}
65136510
}
6511+
}
6512+
6513+
fn process_forward_htlcs(
6514+
&self, short_chan_id: u64, pending_forwards: &mut Vec<HTLCForwardInfo>,
6515+
failed_forwards: &mut Vec<FailedHTLCForward>,
6516+
phantom_receives: &mut Vec<PerSourcePendingForward>,
6517+
) {
6518+
let mut forwarding_counterparty = None;
6519+
65146520
let chan_info_opt = self.short_to_chan_info.read().unwrap().get(&short_chan_id).cloned();
65156521
let (counterparty_node_id, forward_chan_id) = match chan_info_opt {
65166522
Some((cp_id, chan_id)) => (cp_id, chan_id),
65176523
None => {
6518-
forwarding_channel_not_found!(pending_forwards.drain(..));
6524+
self.forwarding_channel_not_found(
6525+
pending_forwards.drain(..),
6526+
short_chan_id,
6527+
forwarding_counterparty,
6528+
failed_forwards,
6529+
phantom_receives,
6530+
);
65196531
return;
65206532
},
65216533
};
65226534
forwarding_counterparty = Some(counterparty_node_id);
65236535
let per_peer_state = self.per_peer_state.read().unwrap();
65246536
let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
65256537
if peer_state_mutex_opt.is_none() {
6526-
forwarding_channel_not_found!(pending_forwards.drain(..));
6538+
self.forwarding_channel_not_found(
6539+
pending_forwards.drain(..),
6540+
short_chan_id,
6541+
forwarding_counterparty,
6542+
failed_forwards,
6543+
phantom_receives,
6544+
);
65276545
return;
65286546
}
65296547
let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
@@ -6618,7 +6636,13 @@ where
66186636
} else {
66196637
let fwd_iter =
66206638
core::iter::once(forward_info).chain(draining_pending_forwards);
6621-
forwarding_channel_not_found!(fwd_iter);
6639+
self.forwarding_channel_not_found(
6640+
fwd_iter,
6641+
short_chan_id,
6642+
forwarding_counterparty,
6643+
failed_forwards,
6644+
phantom_receives,
6645+
);
66226646
break;
66236647
}
66246648
},
@@ -6673,8 +6697,12 @@ where
66736697
failure_type,
66746698
));
66756699
} else {
6676-
forwarding_channel_not_found!(
6677-
core::iter::once(forward_info).chain(draining_pending_forwards)
6700+
self.forwarding_channel_not_found(
6701+
core::iter::once(forward_info).chain(draining_pending_forwards),
6702+
short_chan_id,
6703+
forwarding_counterparty,
6704+
failed_forwards,
6705+
phantom_receives,
66786706
);
66796707
break;
66806708
}
@@ -6694,8 +6722,12 @@ where
66946722
log_trace!(logger, "Failing HTLC back to channel with short id {} (backward HTLC ID {}) after delay", short_chan_id, htlc_id);
66956723
Some((chan.queue_fail_htlc(htlc_id, err_packet.clone(), &&logger), htlc_id))
66966724
} else {
6697-
forwarding_channel_not_found!(
6698-
core::iter::once(forward_info).chain(draining_pending_forwards)
6725+
self.forwarding_channel_not_found(
6726+
core::iter::once(forward_info).chain(draining_pending_forwards),
6727+
short_chan_id,
6728+
forwarding_counterparty,
6729+
failed_forwards,
6730+
phantom_receives,
66996731
);
67006732
break;
67016733
}
@@ -6716,8 +6748,12 @@ where
67166748
);
67176749
Some((res, htlc_id))
67186750
} else {
6719-
forwarding_channel_not_found!(
6720-
core::iter::once(forward_info).chain(draining_pending_forwards)
6751+
self.forwarding_channel_not_found(
6752+
core::iter::once(forward_info).chain(draining_pending_forwards),
6753+
short_chan_id,
6754+
forwarding_counterparty,
6755+
failed_forwards,
6756+
phantom_receives,
67216757
);
67226758
break;
67236759
}

0 commit comments

Comments
 (0)