Skip to content

Commit 0ecb4b0

Browse files
authored
Merge pull request #2258 from valentinewallace/2023-04-blinded-pathfinding-groundwork-2
Prefactor `PaymentParameters` for blinded recipients
2 parents 4062695 + d56672c commit 0ecb4b0

13 files changed

+314
-173
lines changed

fuzz/src/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
300300
let final_cltv_expiry_delta = slice_to_be32(get_slice!(4));
301301
let route_params = RouteParameters {
302302
payment_params: PaymentParameters::from_node_id(*target, final_cltv_expiry_delta)
303-
.with_route_hints(last_hops.clone()),
303+
.with_route_hints(last_hops.clone()).unwrap(),
304304
final_value_msat,
305305
};
306306
let _ = find_route(&our_pubkey, &route_params, &net_graph,

lightning-invoice/src/payment.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ fn pay_invoice_using_amount<P: Deref>(
152152
let mut payment_params = PaymentParameters::from_node_id(invoice.recover_payee_pub_key(),
153153
invoice.min_final_cltv_expiry_delta() as u32)
154154
.with_expiry_time(expiry_time_from_unix_epoch(invoice).as_secs())
155-
.with_route_hints(invoice.route_hints());
155+
.with_route_hints(invoice.route_hints()).unwrap();
156156
if let Some(features) = invoice.features() {
157-
payment_params = payment_params.with_features(features.clone());
157+
payment_params = payment_params.with_bolt11_features(features.clone()).unwrap();
158158
}
159159
let route_params = RouteParameters {
160160
payment_params,

lightning-invoice/src/utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,8 @@ mod test {
838838

839839
let payment_params = PaymentParameters::from_node_id(invoice.recover_payee_pub_key(),
840840
invoice.min_final_cltv_expiry_delta() as u32)
841-
.with_features(invoice.features().unwrap().clone())
842-
.with_route_hints(invoice.route_hints());
841+
.with_bolt11_features(invoice.features().unwrap().clone()).unwrap()
842+
.with_route_hints(invoice.route_hints()).unwrap();
843843
let route_params = RouteParameters {
844844
payment_params,
845845
final_value_msat: invoice.amount_milli_satoshis().unwrap(),
@@ -1294,8 +1294,8 @@ mod test {
12941294

12951295
let payment_params = PaymentParameters::from_node_id(invoice.recover_payee_pub_key(),
12961296
invoice.min_final_cltv_expiry_delta() as u32)
1297-
.with_features(invoice.features().unwrap().clone())
1298-
.with_route_hints(invoice.route_hints());
1297+
.with_bolt11_features(invoice.features().unwrap().clone()).unwrap()
1298+
.with_route_hints(invoice.route_hints()).unwrap();
12991299
let params = RouteParameters {
13001300
payment_params,
13011301
final_value_msat: invoice.amount_milli_satoshis().unwrap(),

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, No
4545
#[cfg(any(feature = "_test_utils", test))]
4646
use crate::ln::features::InvoiceFeatures;
4747
use crate::routing::gossip::NetworkGraph;
48-
use crate::routing::router::{BlindedTail, DefaultRouter, InFlightHtlcs, Path, PaymentParameters, Route, RouteHop, RouteParameters, Router};
48+
use crate::routing::router::{BlindedTail, DefaultRouter, InFlightHtlcs, Path, Payee, PaymentParameters, Route, RouteHop, RouteParameters, Router};
4949
use crate::routing::scoring::ProbabilisticScorer;
5050
use crate::ln::msgs;
5151
use crate::ln::onion_utils;
@@ -7251,8 +7251,10 @@ impl Readable for HTLCSource {
72517251
return Err(DecodeError::InvalidValue);
72527252
}
72537253
if let Some(params) = payment_params.as_mut() {
7254-
if params.final_cltv_expiry_delta == 0 {
7255-
params.final_cltv_expiry_delta = path.final_cltv_expiry_delta().ok_or(DecodeError::InvalidValue)?;
7254+
if let Payee::Clear { ref mut final_cltv_expiry_delta, .. } = params.payee {
7255+
if final_cltv_expiry_delta == &0 {
7256+
*final_cltv_expiry_delta = path.final_cltv_expiry_delta().ok_or(DecodeError::InvalidValue)?;
7257+
}
72567258
}
72577259
}
72587260
Ok(HTLCSource::OutboundRoute {
@@ -9428,7 +9430,7 @@ pub mod bench {
94289430
macro_rules! send_payment {
94299431
($node_a: expr, $node_b: expr) => {
94309432
let payment_params = PaymentParameters::from_node_id($node_b.get_our_node_id(), TEST_FINAL_CLTV)
9431-
.with_features($node_b.invoice_features());
9433+
.with_bolt11_features($node_b.invoice_features()).unwrap();
94329434
let mut payment_preimage = PaymentPreimage([0; 32]);
94339435
payment_preimage.0[0..8].copy_from_slice(&payment_count.to_le_bytes());
94349436
payment_count += 1;

lightning/src/ln/features.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,14 @@ impl InvoiceFeatures {
532532
}
533533
}
534534

535+
impl Bolt12InvoiceFeatures {
536+
/// Converts `Bolt12InvoiceFeatures` to `Features<C>`. Only known `Bolt12InvoiceFeatures` relevant
537+
/// to context `C` are included in the result.
538+
pub(crate) fn to_context<C: sealed::Context>(&self) -> Features<C> {
539+
self.to_context_internal()
540+
}
541+
}
542+
535543
impl ChannelTypeFeatures {
536544
// Maps the relevant `InitFeatures` to `ChannelTypeFeatures`. Any unknown features to
537545
// `ChannelTypeFeatures` are not included in the result.
@@ -791,6 +799,7 @@ impl_feature_len_prefixed_write!(InitFeatures);
791799
impl_feature_len_prefixed_write!(ChannelFeatures);
792800
impl_feature_len_prefixed_write!(NodeFeatures);
793801
impl_feature_len_prefixed_write!(InvoiceFeatures);
802+
impl_feature_len_prefixed_write!(Bolt12InvoiceFeatures);
794803
impl_feature_len_prefixed_write!(BlindedHopFeatures);
795804

796805
// Some features only appear inside of TLVs, so they don't have a length prefix when serialized.

lightning/src/ln/functional_test_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,7 @@ macro_rules! get_route {
17571757
macro_rules! get_route_and_payment_hash {
17581758
($send_node: expr, $recv_node: expr, $recv_value: expr) => {{
17591759
let payment_params = $crate::routing::router::PaymentParameters::from_node_id($recv_node.node.get_our_node_id(), TEST_FINAL_CLTV)
1760-
.with_features($recv_node.node.invoice_features());
1760+
.with_bolt11_features($recv_node.node.invoice_features()).unwrap();
17611761
$crate::get_route_and_payment_hash!($send_node, $recv_node, payment_params, $recv_value)
17621762
}};
17631763
($send_node: expr, $recv_node: expr, $payment_params: expr, $recv_value: expr) => {{
@@ -2306,7 +2306,7 @@ pub const TEST_FINAL_CLTV: u32 = 70;
23062306

23072307
pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash, PaymentSecret) {
23082308
let payment_params = PaymentParameters::from_node_id(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV)
2309-
.with_features(expected_route.last().unwrap().node.invoice_features());
2309+
.with_bolt11_features(expected_route.last().unwrap().node.invoice_features()).unwrap();
23102310
let route = get_route(origin_node, &payment_params, recv_value).unwrap();
23112311
assert_eq!(route.paths.len(), 1);
23122312
assert_eq!(route.paths[0].hops.len(), expected_route.len());
@@ -2320,7 +2320,7 @@ pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route:
23202320

23212321
pub fn route_over_limit<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) {
23222322
let payment_params = PaymentParameters::from_node_id(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV)
2323-
.with_features(expected_route.last().unwrap().node.invoice_features());
2323+
.with_bolt11_features(expected_route.last().unwrap().node.invoice_features()).unwrap();
23242324
let network_graph = origin_node.network_graph.read_only();
23252325
let scorer = test_utils::TestScorer::new();
23262326
let seed = [0u8; 32];

lightning/src/ln/functional_tests.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
18291829
// attempt to send amt_msat > their_max_htlc_value_in_flight_msat
18301830
{
18311831
let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
1832-
.with_features(nodes[2].node.invoice_features()).with_max_channel_saturation_power_of_half(0);
1832+
.with_bolt11_features(nodes[2].node.invoice_features()).unwrap().with_max_channel_saturation_power_of_half(0);
18331833
let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, recv_value_0);
18341834
route.paths[0].hops.last_mut().unwrap().fee_msat += 1;
18351835
assert!(route.paths[0].hops.iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
@@ -1856,7 +1856,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
18561856
}
18571857

18581858
let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
1859-
.with_features(nodes[2].node.invoice_features()).with_max_channel_saturation_power_of_half(0);
1859+
.with_bolt11_features(nodes[2].node.invoice_features()).unwrap().with_max_channel_saturation_power_of_half(0);
18601860
let route = get_route!(nodes[0], payment_params, recv_value_0).unwrap();
18611861
let (payment_preimage, ..) = send_along_route(&nodes[0], route, &[&nodes[1], &nodes[2]], recv_value_0);
18621862
claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
@@ -4795,7 +4795,7 @@ fn test_duplicate_payment_hash_one_failure_one_success() {
47954795
// script push size limit so that the below script length checks match
47964796
// ACCEPTED_HTLC_SCRIPT_WEIGHT.
47974797
let payment_params = PaymentParameters::from_node_id(nodes[3].node.get_our_node_id(), TEST_FINAL_CLTV - 40)
4798-
.with_features(nodes[3].node.invoice_features());
4798+
.with_bolt11_features(nodes[3].node.invoice_features()).unwrap();
47994799
let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[3], payment_params, 800_000);
48004800
send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[2], &nodes[3]]], 800_000, duplicate_payment_hash, payment_secret);
48014801

@@ -6101,7 +6101,7 @@ fn test_update_add_htlc_bolt2_sender_cltv_expiry_too_high() {
61016101
let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0);
61026102

61036103
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 0)
6104-
.with_features(nodes[1].node.invoice_features());
6104+
.with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
61056105
let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], payment_params, 100000000);
61066106
route.paths[0].hops.last_mut().unwrap().cltv_expiry_delta = 500000001;
61076107
unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
@@ -7043,7 +7043,7 @@ fn test_check_htlc_underpaying() {
70437043

70447044
let scorer = test_utils::TestScorer::new();
70457045
let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
7046-
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV).with_features(nodes[1].node.invoice_features());
7046+
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV).with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
70477047
let route = get_route(&nodes[0].node.get_our_node_id(), &payment_params, &nodes[0].network_graph.read_only(), None, 10_000, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
70487048
let (_, our_payment_hash, _) = get_payment_preimage_hash!(nodes[0]);
70497049
let our_payment_secret = nodes[1].node.create_inbound_payment_for_hash(our_payment_hash, Some(100_000), 7200, None).unwrap();
@@ -7189,7 +7189,7 @@ fn test_bump_penalty_txn_on_revoked_commitment() {
71897189

71907190
let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
71917191
let payment_params = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), 30)
7192-
.with_features(nodes[0].node.invoice_features());
7192+
.with_bolt11_features(nodes[0].node.invoice_features()).unwrap();
71937193
let (route,_, _, _) = get_route_and_payment_hash!(nodes[1], nodes[0], payment_params, 3000000);
71947194
send_along_route(&nodes[1], route, &vec!(&nodes[0])[..], 3000000);
71957195

@@ -7294,13 +7294,13 @@ fn test_bump_penalty_txn_on_revoked_htlcs() {
72947294

72957295
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000);
72967296
// Lock HTLC in both directions (using a slightly lower CLTV delay to provide timely RBF bumps)
7297-
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 50).with_features(nodes[1].node.invoice_features());
7297+
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 50).with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
72987298
let scorer = test_utils::TestScorer::new();
72997299
let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
73007300
let route = get_route(&nodes[0].node.get_our_node_id(), &payment_params, &nodes[0].network_graph.read_only(), None,
73017301
3_000_000, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
73027302
let payment_preimage = send_along_route(&nodes[0], route, &[&nodes[1]], 3_000_000).0;
7303-
let payment_params = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), 50).with_features(nodes[0].node.invoice_features());
7303+
let payment_params = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), 50).with_bolt11_features(nodes[0].node.invoice_features()).unwrap();
73047304
let route = get_route(&nodes[1].node.get_our_node_id(), &payment_params, &nodes[1].network_graph.read_only(), None,
73057305
3_000_000, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
73067306
send_along_route(&nodes[1], route, &[&nodes[0]], 3_000_000);
@@ -9300,7 +9300,7 @@ fn do_test_dup_htlc_second_rejected(test_for_second_fail_panic: bool) {
93009300
let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
93019301

93029302
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
9303-
.with_features(nodes[1].node.invoice_features());
9303+
.with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
93049304
let route = get_route!(nodes[0], payment_params, 10_000).unwrap();
93059305

93069306
let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(&nodes[1]);
@@ -9409,7 +9409,7 @@ fn test_inconsistent_mpp_params() {
94099409
let chan_2_3 =create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 100_000, 0);
94109410

94119411
let payment_params = PaymentParameters::from_node_id(nodes[3].node.get_our_node_id(), TEST_FINAL_CLTV)
9412-
.with_features(nodes[3].node.invoice_features());
9412+
.with_bolt11_features(nodes[3].node.invoice_features()).unwrap();
94139413
let mut route = get_route!(nodes[0], payment_params, 15_000_000).unwrap();
94149414
assert_eq!(route.paths.len(), 2);
94159415
route.paths.sort_by(|path_a, _| {

lightning/src/ln/onion_route_tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,8 @@ fn do_test_onion_failure_stale_channel_update(announced_channel: bool) {
714714
htlc_minimum_msat: None,
715715
}])];
716716
let payment_params = PaymentParameters::from_node_id(*channel_to_update_counterparty, TEST_FINAL_CLTV)
717-
.with_features(nodes[2].node.invoice_features())
718-
.with_route_hints(hop_hints);
717+
.with_bolt11_features(nodes[2].node.invoice_features()).unwrap()
718+
.with_route_hints(hop_hints).unwrap();
719719
get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, PAYMENT_AMT)
720720
};
721721
send_along_route_with_secret(&nodes[0], route.clone(), &[&[&nodes[1], &nodes[2]]], PAYMENT_AMT,
@@ -861,7 +861,7 @@ fn test_always_create_tlv_format_onion_payloads() {
861861
create_announced_chan_between_nodes(&nodes, 1, 2);
862862

863863
let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
864-
.with_features(InvoiceFeatures::empty());
864+
.with_bolt11_features(InvoiceFeatures::empty()).unwrap();
865865
let (route, _payment_hash, _payment_preimage, _payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 40000);
866866

867867
let hops = &route.paths[0].hops;
@@ -963,7 +963,7 @@ macro_rules! get_phantom_route {
963963
let phantom_pubkey = $nodes[1].keys_manager.get_node_id(Recipient::PhantomNode).unwrap();
964964
let phantom_route_hint = $nodes[1].node.get_phantom_route_hints();
965965
let payment_params = PaymentParameters::from_node_id(phantom_pubkey, TEST_FINAL_CLTV)
966-
.with_features($nodes[1].node.invoice_features())
966+
.with_bolt11_features($nodes[1].node.invoice_features()).unwrap()
967967
.with_route_hints(vec![RouteHint(vec![
968968
RouteHintHop {
969969
src_node_id: $nodes[0].node.get_our_node_id(),
@@ -987,7 +987,7 @@ macro_rules! get_phantom_route {
987987
htlc_minimum_msat: None,
988988
htlc_maximum_msat: None,
989989
}
990-
])]);
990+
])]).unwrap();
991991
let scorer = test_utils::TestScorer::new();
992992
let network_graph = $nodes[0].network_graph.read_only();
993993
(get_route(

0 commit comments

Comments
 (0)