Skip to content

Commit 98ba859

Browse files
committed
Introduce Parsing mechanism
1 parent 9e091c2 commit 98ba859

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ use crate::ln::msgs::{
7171
MessageSendEvent,
7272
};
7373
use crate::ln::onion_payment::{
74-
check_incoming_htlc_cltv, create_fwd_pending_htlc_info, create_recv_pending_htlc_info,
75-
decode_incoming_update_add_htlc_onion, invalid_payment_err_data, ForwardInfo, HopConnector,
76-
InboundHTLCErr, NextPacketDetails,
74+
check_incoming_htlc_cltv, create_fwd_pending_htlc_info, create_new_update_add_htlc,
75+
create_recv_pending_htlc_info, decode_incoming_update_add_htlc_onion, invalid_payment_err_data,
76+
ForwardInfo, HopConnector, InboundHTLCErr, NextPacketDetails,
7777
};
7878
use crate::ln::onion_utils::{self};
7979
use crate::ln::onion_utils::{
@@ -6550,6 +6550,7 @@ where
65506550
pub(crate) fn process_pending_update_add_htlcs(&self) -> bool {
65516551
let mut should_persist = false;
65526552
let mut decode_update_add_htlcs = new_hash_map();
6553+
let mut dummy_update_add_htlcs = new_hash_map();
65536554
mem::swap(&mut decode_update_add_htlcs, &mut self.decode_update_add_htlcs.lock().unwrap());
65546555

65556556
let get_htlc_failure_type = |outgoing_scid_opt: Option<u64>, payment_hash: PaymentHash| {
@@ -6613,7 +6614,39 @@ where
66136614
&*self.logger,
66146615
&self.secp_ctx,
66156616
) {
6616-
Ok(decoded_onion) => decoded_onion,
6617+
Ok(decoded_onion) => match decoded_onion {
6618+
(
6619+
onion_utils::Hop::Dummy {
6620+
intro_node_blinding_point,
6621+
next_hop_hmac,
6622+
new_packet_bytes,
6623+
..
6624+
},
6625+
Some(NextPacketDetails { next_packet_pubkey, forward_info }),
6626+
) => {
6627+
debug_assert!(
6628+
forward_info.is_none(),
6629+
"Dummy hops must not contain any forward info, since they are not actually forwarded."
6630+
);
6631+
let new_update_add_htlc = create_new_update_add_htlc(
6632+
update_add_htlc.clone(),
6633+
&*self.node_signer,
6634+
&self.secp_ctx,
6635+
intro_node_blinding_point,
6636+
next_packet_pubkey,
6637+
next_hop_hmac,
6638+
new_packet_bytes,
6639+
);
6640+
6641+
dummy_update_add_htlcs
6642+
.entry(incoming_scid_alias)
6643+
.or_insert_with(Vec::new)
6644+
.push(new_update_add_htlc);
6645+
6646+
continue;
6647+
},
6648+
_ => decoded_onion,
6649+
},
66176650

66186651
Err((htlc_fail, reason)) => {
66196652
let failure_type = HTLCHandlingFailureType::InvalidOnion;
@@ -6770,6 +6803,11 @@ where
67706803
));
67716804
}
67726805
}
6806+
6807+
// Replace the decode queue with the peeled dummy HTLCs so they can be processed in the next iteration.
6808+
let mut decode_update_add_htlc_source = self.decode_update_add_htlcs.lock().unwrap();
6809+
mem::swap(&mut *decode_update_add_htlc_source, &mut dummy_update_add_htlcs);
6810+
67736811
should_persist
67746812
}
67756813

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};
@@ -668,6 +668,32 @@ where
668668
Ok((next_hop, next_packet_details))
669669
}
670670

671+
pub(super) fn create_new_update_add_htlc<NS: Deref, T: secp256k1::Verification>(
672+
msg: msgs::UpdateAddHTLC, node_signer: NS, secp_ctx: &Secp256k1<T>,
673+
intro_node_blinding_point: Option<PublicKey>,
674+
new_packet_pubkey: Result<PublicKey, secp256k1::Error>, next_hop_hmac: [u8; 32],
675+
new_packet_bytes: [u8; 1300],
676+
) -> UpdateAddHTLC
677+
where
678+
NS::Target: NodeSigner,
679+
{
680+
let new_packet = OnionPacket {
681+
version: 0,
682+
public_key: new_packet_pubkey,
683+
hop_data: new_packet_bytes,
684+
hmac: next_hop_hmac,
685+
};
686+
687+
let next_blinding_point =
688+
intro_node_blinding_point.or(msg.blinding_point).and_then(|blinding_point| {
689+
let encrypted_tlvs_ss =
690+
node_signer.ecdh(Recipient::Node, &blinding_point, None).unwrap().secret_bytes();
691+
onion_utils::next_hop_pubkey(&secp_ctx, blinding_point, &encrypted_tlvs_ss).ok()
692+
});
693+
694+
UpdateAddHTLC { onion_routing_packet: new_packet, blinding_point: next_blinding_point, ..msg }
695+
}
696+
671697
pub(super) fn check_incoming_htlc_cltv(
672698
cur_height: u32, outgoing_cltv_value: u32, cltv_expiry: u32,
673699
) -> Result<(), LocalHTLCFailureReason> {

lightning/src/ln/onion_utils.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,18 @@ where
23392339
let shared_secret =
23402340
node_signer.ecdh(recipient, hop_pubkey, blinded_node_id_tweak.as_ref()).unwrap();
23412341

2342+
let check_authentication = |payment_tlvs_authenticated: bool| -> Result<(), OnionDecodeErr> {
2343+
if !payment_tlvs_authenticated {
2344+
return Err(OnionDecodeErr::Relay {
2345+
err_msg: "Final Node OnionHopData provided for us as an intermediary node",
2346+
reason: LocalHTLCFailureReason::InvalidOnionPayload,
2347+
shared_secret,
2348+
trampoline_shared_secret: None,
2349+
});
2350+
}
2351+
Ok(())
2352+
};
2353+
23422354
let decoded_hop: Result<(msgs::InboundOnionPayload, Option<_>), _> = decode_next_hop(
23432355
shared_secret.secret_bytes(),
23442356
hop_data,
@@ -2363,6 +2375,18 @@ where
23632375
new_packet_bytes,
23642376
})
23652377
},
2378+
msgs::InboundOnionPayload::Dummy {
2379+
intro_node_blinding_point,
2380+
payment_tlvs_authenticated,
2381+
} => {
2382+
check_authentication(payment_tlvs_authenticated)?;
2383+
Ok(Hop::Dummy {
2384+
intro_node_blinding_point,
2385+
shared_secret,
2386+
next_hop_hmac,
2387+
new_packet_bytes,
2388+
})
2389+
},
23662390
_ => {
23672391
if blinding_point.is_some() {
23682392
return Err(OnionDecodeErr::Malformed {

0 commit comments

Comments
 (0)