Skip to content

Commit

Permalink
Return Trampoline payloads in build_onion_payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
arik-so committed Nov 21, 2024
1 parent 33804c3 commit 5ea3ada
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
12 changes: 6 additions & 6 deletions lightning/src/ln/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ fn test_fee_spike_violation_fails_htlc() {

let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
let recipient_onion_fields = RecipientOnionFields::secret_only(payment_secret);
let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0],
let (onion_payloads, _, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0],
3460001, &recipient_onion_fields, cur_height, &None, None).unwrap();
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
let msg = msgs::UpdateAddHTLC {
Expand Down Expand Up @@ -1636,7 +1636,7 @@ fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
let cur_height = nodes[1].node.best_block.read().unwrap().height + 1;
let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
let recipient_onion_fields = RecipientOnionFields::secret_only(payment_secret);
let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0],
let (onion_payloads, _, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0],
700_000, &recipient_onion_fields, cur_height, &None, None).unwrap();
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
let msg = msgs::UpdateAddHTLC {
Expand Down Expand Up @@ -1816,7 +1816,7 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
let cur_height = nodes[0].node.best_block.read().unwrap().height + 1;
let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route_2.paths[0], &session_priv).unwrap();
let recipient_onion_fields = RecipientOnionFields::spontaneous_empty();
let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(
let (onion_payloads, _, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(
&route_2.paths[0], recv_value_2, &recipient_onion_fields, cur_height, &None, None).unwrap();
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash_1).unwrap();
let msg = msgs::UpdateAddHTLC {
Expand Down Expand Up @@ -3555,7 +3555,7 @@ fn fail_backward_pending_htlc_upon_channel_failure() {
let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
let current_height = nodes[1].node.best_block.read().unwrap().height + 1;
let recipient_onion_fields = RecipientOnionFields::secret_only(payment_secret);
let (onion_payloads, _amount_msat, cltv_expiry) = onion_utils::build_onion_payloads(
let (onion_payloads, _trampoline_payloads, _amount_msat, cltv_expiry) = onion_utils::build_onion_payloads(
&route.paths[0], 50_000, &recipient_onion_fields, current_height, &None, None).unwrap();
let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
let onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
Expand Down Expand Up @@ -6550,7 +6550,7 @@ fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
let cur_height = nodes[0].node.best_block.read().unwrap().height + 1;
let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::signing_only(), &route.paths[0], &session_priv).unwrap();
let recipient_onion_fields = RecipientOnionFields::secret_only(our_payment_secret);
let (onion_payloads, _htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(
let (onion_payloads, _trampoline_payloads, _htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(
&route.paths[0], send_amt, &recipient_onion_fields, cur_height, &None, None).unwrap();
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash).unwrap();

Expand Down Expand Up @@ -8293,7 +8293,7 @@ fn test_onion_value_mpp_set_calculation() {
let session_priv = SecretKey::from_slice(&session_priv).unwrap();
let mut onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
let recipient_onion_fields = RecipientOnionFields::secret_only(our_payment_secret);
let (mut onion_payloads, _, _) = onion_utils::build_onion_payloads(&route.paths[0], 100_000,
let (mut onion_payloads, _, _, _) = onion_utils::build_onion_payloads(&route.paths[0], 100_000,
&recipient_onion_fields, height + 1, &None, None).unwrap();
// Edit amt_to_forward to simulate the sender having set
// the final amount and the routing node taking less fee
Expand Down
10 changes: 5 additions & 5 deletions lightning/src/ln/onion_route_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ fn test_onion_failure() {
let cur_height = nodes[0].best_block_info().1 + 1;
let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
let recipient_onion_fields = RecipientOnionFields::spontaneous_empty();
let (mut onion_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(
let (mut onion_payloads, _trampoline_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(
&route.paths[0], 40000, &recipient_onion_fields, cur_height, &None, None).unwrap();
let mut new_payloads = Vec::new();
for payload in onion_payloads.drain(..) {
Expand All @@ -374,7 +374,7 @@ fn test_onion_failure() {
let cur_height = nodes[0].best_block_info().1 + 1;
let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
let recipient_onion_fields = RecipientOnionFields::spontaneous_empty();
let (mut onion_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(
let (mut onion_payloads, _trampoline_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(
&route.paths[0], 40000, &recipient_onion_fields, cur_height, &None, None).unwrap();
let mut new_payloads = Vec::new();
for payload in onion_payloads.drain(..) {
Expand Down Expand Up @@ -626,7 +626,7 @@ fn test_onion_failure() {
route.paths[0].hops[1].cltv_expiry_delta += CLTV_FAR_FAR_AWAY + route.paths[0].hops[0].cltv_expiry_delta + 1;
let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
let recipient_onion_fields = RecipientOnionFields::spontaneous_empty();
let (onion_payloads, _, htlc_cltv) = onion_utils::build_onion_payloads(
let (onion_payloads, _, _, htlc_cltv) = onion_utils::build_onion_payloads(
&route.paths[0], 40000, &recipient_onion_fields, height, &None, None).unwrap();
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
msg.cltv_expiry = htlc_cltv;
Expand Down Expand Up @@ -963,7 +963,7 @@ fn test_always_create_tlv_format_onion_payloads() {

let cur_height = nodes[0].best_block_info().1 + 1;
let recipient_onion_fields = RecipientOnionFields::spontaneous_empty();
let (onion_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(
let (onion_payloads, _trampoline_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(
&route.paths[0], 40000, &recipient_onion_fields, cur_height, &None, None).unwrap();

match onion_payloads[0] {
Expand Down Expand Up @@ -1219,7 +1219,7 @@ fn test_phantom_invalid_onion_payload() {
let session_priv = SecretKey::from_slice(&session_priv).unwrap();
let mut onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
let recipient_onion_fields = RecipientOnionFields::secret_only(payment_secret);
let (mut onion_payloads, _, _) = onion_utils::build_onion_payloads(
let (mut onion_payloads, _, _, _) = onion_utils::build_onion_payloads(
&route.paths[0], msgs::MAX_VALUE_MSAT + 1,
&recipient_onion_fields, height + 1, &None, None).unwrap();
// We only want to construct the onion packet for the last hop, not the entire route, so
Expand Down
45 changes: 32 additions & 13 deletions lightning/src/ln/onion_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::ln::msgs;
use crate::offers::invoice_request::InvoiceRequest;
use crate::routing::gossip::NetworkUpdate;
use crate::routing::router::{Path, RouteHop, RouteParameters, TrampolineHop};
use crate::sign::NodeSigner;
use crate::sign::{EntropySource, NodeSigner};
use crate::types::features::{ChannelFeatures, NodeFeatures};
use crate::types::payment::{PaymentHash, PaymentPreimage};
use crate::util::errors::{self, APIError};
Expand All @@ -34,7 +34,7 @@ use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey};

use crate::io::{Cursor, Read};
use core::ops::Deref;

use crate::blinded_path::payment::BlindedPaymentPath;
#[allow(unused_imports)]
use crate::prelude::*;

Expand Down Expand Up @@ -186,6 +186,13 @@ fn build_trampoline_onion_payloads<'a>(
let blinded_tail = path.blinded_tail.as_ref().ok_or(APIError::InvalidRoute {
err: "Routes using Trampoline must terminate blindly.".to_string(),
})?;

if !blinded_tail.final_hop_supports_trampoline {
// if the final hop does not support Trampoline, we need to generate blinded tails
// here instead
debug_assert!(false);
}

let blinded_tail_with_hop_iter = BlindedTailHopIter {
hops: blinded_tail.hops.iter(),
blinding_point: blinded_tail.blinding_point,
Expand Down Expand Up @@ -297,31 +304,43 @@ pub(super) fn build_onion_payloads<'a>(
path: &'a Path, total_msat: u64, recipient_onion: &'a RecipientOnionFields,
starting_htlc_offset: u32, keysend_preimage: &Option<PaymentPreimage>,
invoice_request: Option<&'a InvoiceRequest>,
) -> Result<(Vec<msgs::OutboundOnionPayload<'a>>, u64, u32), APIError> {
) -> Result<(Vec<msgs::OutboundOnionPayload<'a>>, Vec<msgs::OutboundTrampolinePayload<'a>>, u64, u32), APIError> {
let mut res: Vec<msgs::OutboundOnionPayload> = Vec::with_capacity(
path.hops.len() + path.blinded_tail.as_ref().map_or(0, |t| t.hops.len()),
);
let blinded_tail_with_hop_iter = path.blinded_tail.as_ref().map(|bt| BlindedTailHopIter {
hops: bt.hops.iter(),
blinding_point: bt.blinding_point,
final_value_msat: bt.final_value_msat,
excess_final_cltv_expiry_delta: bt.excess_final_cltv_expiry_delta,
});

let mut non_trampoline_total_msat = total_msat;
let mut non_trampoline_starting_htlc_offset = starting_htlc_offset;

let (blinded_tail_with_hop_iter, trampoline_payloads) = if path.trampoline_hops.len() > 0 {
let trampoline_payload_details = build_trampoline_onion_payloads(path, total_msat, recipient_onion, starting_htlc_offset, keysend_preimage)?;
non_trampoline_total_msat = trampoline_payload_details.1;
non_trampoline_starting_htlc_offset = trampoline_payload_details.2;
(None, trampoline_payload_details.0)
} else {
let blinded_tail_with_hop_iter = path.blinded_tail.as_ref().map(|bt| BlindedTailHopIter {
hops: bt.hops.iter(),
blinding_point: bt.blinding_point,
final_value_msat: bt.final_value_msat,
excess_final_cltv_expiry_delta: bt.excess_final_cltv_expiry_delta,
});
(blinded_tail_with_hop_iter, vec![])
};

let (value_msat, cltv) = build_onion_payloads_callback(
path.hops.iter(),
blinded_tail_with_hop_iter,
total_msat,
non_trampoline_total_msat,
recipient_onion,
starting_htlc_offset,
non_trampoline_starting_htlc_offset,
keysend_preimage,
invoice_request,
|action, payload| match action {
PayloadCallbackAction::PushBack => res.push(payload),
PayloadCallbackAction::PushFront => res.insert(0, payload),
},
)?;
Ok((res, value_msat, cltv))
Ok((res, trampoline_payloads, value_msat, cltv))
}

struct BlindedTailHopIter<'a, I: Iterator<Item = &'a BlindedHop>> {
Expand Down Expand Up @@ -1286,7 +1305,7 @@ pub fn create_payment_onion<T: secp256k1::Signing>(
let onion_keys = construct_onion_keys(&secp_ctx, &path, &session_priv).map_err(|_| {
APIError::InvalidRoute { err: "Pubkey along hop was maliciously selected".to_owned() }
})?;
let (onion_payloads, htlc_msat, htlc_cltv) = build_onion_payloads(
let (onion_payloads, _, htlc_msat, htlc_cltv) = build_onion_payloads(
&path,
total_msat,
recipient_onion,
Expand Down

0 comments on commit 5ea3ada

Please sign in to comment.