Skip to content

Commit 91ae665

Browse files
committed
Introduce Parsing mechanism
1 parent 19865b2 commit 91ae665

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ use crate::ln::msgs::{
7272
MessageSendEvent,
7373
};
7474
use crate::ln::onion_payment::{
75-
check_incoming_htlc_cltv, create_fwd_pending_htlc_info, create_recv_pending_htlc_info,
76-
decode_incoming_update_add_htlc_onion, invalid_payment_err_data, ForwardInfo, HopConnector,
77-
InboundHTLCErr, NextPacketDetails,
75+
check_incoming_htlc_cltv, create_fwd_pending_htlc_info, create_new_update_add_htlc,
76+
create_recv_pending_htlc_info, decode_incoming_update_add_htlc_onion, invalid_payment_err_data,
77+
ForwardInfo, HopConnector, InboundHTLCErr, NextPacketDetails,
7878
};
7979
use crate::ln::onion_utils::{self};
8080
use crate::ln::onion_utils::{
@@ -6852,6 +6852,7 @@ where
68526852
pub(crate) fn process_pending_update_add_htlcs(&self) -> bool {
68536853
let mut should_persist = false;
68546854
let mut decode_update_add_htlcs = new_hash_map();
6855+
let mut dummy_update_add_htlcs = new_hash_map();
68556856
mem::swap(&mut decode_update_add_htlcs, &mut self.decode_update_add_htlcs.lock().unwrap());
68566857

68576858
let get_htlc_failure_type = |outgoing_scid_opt: Option<u64>, payment_hash: PaymentHash| {
@@ -6915,7 +6916,39 @@ where
69156916
&*self.logger,
69166917
&self.secp_ctx,
69176918
) {
6918-
Ok(decoded_onion) => decoded_onion,
6919+
Ok(decoded_onion) => match decoded_onion {
6920+
(
6921+
onion_utils::Hop::Dummy {
6922+
intro_node_blinding_point,
6923+
next_hop_hmac,
6924+
new_packet_bytes,
6925+
..
6926+
},
6927+
Some(NextPacketDetails { next_packet_pubkey, forward_info }),
6928+
) => {
6929+
debug_assert!(
6930+
forward_info.is_none(),
6931+
"Dummy hops must not contain any forward info, since they are not actually forwarded."
6932+
);
6933+
let new_update_add_htlc = create_new_update_add_htlc(
6934+
update_add_htlc.clone(),
6935+
&*self.node_signer,
6936+
&self.secp_ctx,
6937+
intro_node_blinding_point,
6938+
next_packet_pubkey,
6939+
next_hop_hmac,
6940+
new_packet_bytes,
6941+
);
6942+
6943+
dummy_update_add_htlcs
6944+
.entry(incoming_scid_alias)
6945+
.or_insert_with(Vec::new)
6946+
.push(new_update_add_htlc);
6947+
6948+
continue;
6949+
},
6950+
_ => decoded_onion,
6951+
},
69196952

69206953
Err((htlc_fail, reason)) => {
69216954
let failure_type = HTLCHandlingFailureType::InvalidOnion;
@@ -7072,6 +7105,11 @@ where
70727105
));
70737106
}
70747107
}
7108+
7109+
// Replace the decode queue with the peeled dummy HTLCs so they can be processed in the next iteration.
7110+
let mut decode_update_add_htlc_source = self.decode_update_add_htlcs.lock().unwrap();
7111+
mem::swap(&mut *decode_update_add_htlc_source, &mut dummy_update_add_htlcs);
7112+
70757113
should_persist
70767114
}
70777115

lightning/src/ln/onion_payment.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::ln::channelmanager::{
1515
BlindedFailure, BlindedForward, HTLCFailureMsg, PendingHTLCInfo, PendingHTLCRouting,
1616
CLTV_FAR_FAR_AWAY, MIN_CLTV_EXPIRY_DELTA,
1717
};
18-
use crate::ln::msgs;
18+
use crate::ln::msgs::{self, OnionPacket, UpdateAddHTLC};
1919
use crate::ln::onion_utils;
2020
use crate::ln::onion_utils::{HTLCFailReason, LocalHTLCFailureReason, ONION_DATA_LEN};
2121
use crate::sign::{NodeSigner, Recipient};
@@ -651,6 +651,32 @@ where
651651
Ok((next_hop, next_packet_details))
652652
}
653653

654+
pub(super) fn create_new_update_add_htlc<NS: Deref, T: secp256k1::Verification>(
655+
msg: msgs::UpdateAddHTLC, node_signer: NS, secp_ctx: &Secp256k1<T>,
656+
intro_node_blinding_point: Option<PublicKey>,
657+
new_packet_pubkey: Result<PublicKey, secp256k1::Error>, next_hop_hmac: [u8; 32],
658+
new_packet_bytes: [u8; 1300],
659+
) -> UpdateAddHTLC
660+
where
661+
NS::Target: NodeSigner,
662+
{
663+
let new_packet = OnionPacket {
664+
version: 0,
665+
public_key: new_packet_pubkey,
666+
hop_data: new_packet_bytes,
667+
hmac: next_hop_hmac,
668+
};
669+
670+
let next_blinding_point =
671+
intro_node_blinding_point.or(msg.blinding_point).and_then(|blinding_point| {
672+
let encrypted_tlvs_ss =
673+
node_signer.ecdh(Recipient::Node, &blinding_point, None).unwrap().secret_bytes();
674+
onion_utils::next_hop_pubkey(&secp_ctx, blinding_point, &encrypted_tlvs_ss).ok()
675+
});
676+
677+
UpdateAddHTLC { onion_routing_packet: new_packet, blinding_point: next_blinding_point, ..msg }
678+
}
679+
654680
pub(super) fn check_incoming_htlc_cltv(
655681
cur_height: u32, outgoing_cltv_value: u32, cltv_expiry: u32,
656682
) -> Result<(), LocalHTLCFailureReason> {

lightning/src/ln/onion_utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,12 @@ where
23352335
new_packet_bytes,
23362336
})
23372337
},
2338+
msgs::InboundOnionPayload::Dummy { intro_node_blinding_point } => Ok(Hop::Dummy {
2339+
intro_node_blinding_point,
2340+
shared_secret,
2341+
next_hop_hmac,
2342+
new_packet_bytes,
2343+
}),
23382344
_ => {
23392345
if blinding_point.is_some() {
23402346
return Err(OnionDecodeErr::Malformed {

0 commit comments

Comments
 (0)