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 14, 2024
1 parent 5583b98 commit 7cad33e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 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).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).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).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).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 @@ -6547,7 +6547,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).unwrap();
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash).unwrap();

Expand Down Expand Up @@ -8290,7 +8290,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).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).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).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).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).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).unwrap();
// We only want to construct the onion packet for the last hop, not the entire route, so
Expand Down
21 changes: 16 additions & 5 deletions lightning/src/ln/onion_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::ln::channelmanager::{HTLCSource, RecipientOnionFields};
use crate::ln::msgs;
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 @@ -33,7 +33,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 @@ -185,6 +185,11 @@ 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 {
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 @@ -295,7 +300,7 @@ where
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>,
) -> 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()),
);
Expand All @@ -306,6 +311,12 @@ pub(super) fn build_onion_payloads<'a>(
excess_final_cltv_expiry_delta: bt.excess_final_cltv_expiry_delta,
});

let trampoline_payloads = if path.trampoline_hops.len() > 0 {
build_trampoline_onion_payloads(path, total_msat, recipient_onion, starting_htlc_offset, keysend_preimage)?.0
} else {
vec![]
};

let (value_msat, cltv) = build_onion_payloads_callback(
path.hops.iter(),
blinded_tail_with_hop_iter,
Expand All @@ -318,7 +329,7 @@ pub(super) fn build_onion_payloads<'a>(
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 @@ -1278,7 +1289,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 7cad33e

Please sign in to comment.