Skip to content

Commit 5a3e383

Browse files
Move payee node id from top level PaymentParams to Payee::Clear
Since blinded payees don't have one
1 parent cea78f5 commit 5a3e383

File tree

2 files changed

+51
-35
lines changed

2 files changed

+51
-35
lines changed

lightning/src/routing/router.rs

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,6 @@ const MAX_PATH_LENGTH_ESTIMATE: u8 = 19;
493493
/// Information used to route a payment.
494494
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
495495
pub struct PaymentParameters {
496-
/// The node id of the payee.
497-
pub payee_pubkey: PublicKey,
498-
499496
/// Features supported by the payee.
500497
///
501498
/// May be set from the payee's invoice or via [`for_keysend`]. May be `None` if the invoice
@@ -551,7 +548,7 @@ impl Writeable for PaymentParameters {
551548
Payee::Blinded { route_hints } => blinded_hints = route_hints,
552549
}
553550
write_tlv_fields!(writer, {
554-
(0, self.payee_pubkey, required),
551+
(0, self.payee.node_id(), option),
555552
(1, self.max_total_cltv_expiry_delta, required),
556553
(2, self.features, option),
557554
(3, self.max_path_count, required),
@@ -569,7 +566,7 @@ impl Writeable for PaymentParameters {
569566
impl ReadableArgs<u32> for PaymentParameters {
570567
fn read<R: io::Read>(reader: &mut R, default_final_cltv_expiry_delta: u32) -> Result<Self, DecodeError> {
571568
_init_and_read_tlv_fields!(reader, {
572-
(0, payee_pubkey, required),
569+
(0, payee_pubkey, option),
573570
(1, max_total_cltv_expiry_delta, (default_value, DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA)),
574571
(2, features, option),
575572
(3, max_path_count, (default_value, DEFAULT_MAX_PATH_COUNT)),
@@ -583,13 +580,15 @@ impl ReadableArgs<u32> for PaymentParameters {
583580
let clear_route_hints = route_hints.unwrap_or(vec![]);
584581
let blinded_route_hints = blinded_route_hints.unwrap_or(vec![]);
585582
let payee = if blinded_route_hints.len() != 0 {
586-
if clear_route_hints.len() != 0 { return Err(DecodeError::InvalidValue) }
583+
if clear_route_hints.len() != 0 || payee_pubkey.is_some() { return Err(DecodeError::InvalidValue) }
587584
Payee::Blinded { route_hints: blinded_route_hints }
588585
} else {
589-
Payee::Clear { route_hints: clear_route_hints }
586+
Payee::Clear {
587+
route_hints: clear_route_hints,
588+
node_id: payee_pubkey.ok_or(DecodeError::InvalidValue)?,
589+
}
590590
};
591591
Ok(Self {
592-
payee_pubkey: _init_tlv_based_struct_field!(payee_pubkey, required),
593592
max_total_cltv_expiry_delta: _init_tlv_based_struct_field!(max_total_cltv_expiry_delta, (default_value, unused)),
594593
features,
595594
max_path_count: _init_tlv_based_struct_field!(max_path_count, (default_value, unused)),
@@ -610,9 +609,8 @@ impl PaymentParameters {
610609
/// provided.
611610
pub fn from_node_id(payee_pubkey: PublicKey, final_cltv_expiry_delta: u32) -> Self {
612611
Self {
613-
payee_pubkey,
614612
features: None,
615-
payee: Payee::Clear { route_hints: vec![] },
613+
payee: Payee::Clear { node_id: payee_pubkey, route_hints: vec![] },
616614
expiry_time: None,
617615
max_total_cltv_expiry_delta: DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA,
618616
max_path_count: DEFAULT_MAX_PATH_COUNT,
@@ -644,8 +642,8 @@ impl PaymentParameters {
644642
pub fn with_route_hints(self, route_hints: Vec<RouteHint>) -> Result<Self, ()> {
645643
match self.payee {
646644
Payee::Blinded { .. } => Err(()),
647-
Payee::Clear { .. } =>
648-
Ok(Self { payee: Payee::Clear { route_hints }, ..self })
645+
Payee::Clear { node_id, .. } =>
646+
Ok(Self { payee: Payee::Clear { route_hints, node_id }, ..self })
649647
}
650648
}
651649

@@ -691,11 +689,22 @@ pub enum Payee {
691689
},
692690
/// The recipient included these route hints in their BOLT11 invoice.
693691
Clear {
692+
/// The node id of the payee.
693+
node_id: PublicKey,
694694
/// Hints for routing to the payee, containing channels connecting the payee to public nodes.
695695
route_hints: Vec<RouteHint>,
696696
},
697697
}
698698

699+
impl Payee {
700+
fn node_id(&self) -> Option<PublicKey> {
701+
match self {
702+
Self::Clear { node_id, .. } => Some(*node_id),
703+
_ => None,
704+
}
705+
}
706+
}
707+
699708
/// A list of hops along a payment path terminating with a channel to the recipient.
700709
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
701710
pub struct RouteHint(pub Vec<RouteHintHop>);
@@ -1129,10 +1138,13 @@ pub(crate) fn get_route<L: Deref, S: Score>(
11291138
_random_seed_bytes: &[u8; 32]
11301139
) -> Result<Route, LightningError>
11311140
where L::Target: Logger {
1132-
let payee_node_id = NodeId::from_pubkey(&payment_params.payee_pubkey);
1141+
let payee_node_id = payment_params.payee.node_id().map(|pk| NodeId::from_pubkey(&pk));
1142+
const DUMMY_BLINDED_PAYEE_ID: [u8; 33] = [42u8; 33];
1143+
let target_pubkey = payment_params.payee.node_id().unwrap_or_else(|| PublicKey::from_slice(&DUMMY_BLINDED_PAYEE_ID).unwrap());
1144+
let target_node_id = NodeId::from_pubkey(&target_pubkey);
11331145
let our_node_id = NodeId::from_pubkey(&our_node_pubkey);
11341146

1135-
if payee_node_id == our_node_id {
1147+
if payee_node_id.map_or(false, |payee| payee == our_node_id) {
11361148
return Err(LightningError{err: "Cannot generate a route to ourselves".to_owned(), action: ErrorAction::IgnoreError});
11371149
}
11381150

@@ -1145,10 +1157,10 @@ where L::Target: Logger {
11451157
}
11461158

11471159
match &payment_params.payee {
1148-
Payee::Clear { route_hints } => {
1160+
Payee::Clear { route_hints, node_id } => {
11491161
for route in route_hints.iter() {
11501162
for hop in &route.0 {
1151-
if hop.src_node_id == payment_params.payee_pubkey {
1163+
if hop.src_node_id == *node_id {
11521164
return Err(LightningError{err: "Route hint cannot have the payee as the source.".to_owned(), action: ErrorAction::IgnoreError});
11531165
}
11541166
}
@@ -1231,14 +1243,13 @@ where L::Target: Logger {
12311243
false
12321244
} else if let Some(features) = &payment_params.features {
12331245
features.supports_basic_mpp()
1234-
} else if let Some(node) = network_nodes.get(&payee_node_id) {
1235-
if let Some(node_info) = node.announcement_info.as_ref() {
1236-
node_info.features.supports_basic_mpp()
1237-
} else { false }
1246+
} else if let Some(payee) = payee_node_id {
1247+
network_nodes.get(&payee).map_or(false, |node| node.announcement_info.as_ref().map_or(false,
1248+
|info| info.features.supports_basic_mpp()))
12381249
} else { false };
12391250

1240-
log_trace!(logger, "Searching for a route from payer {} to payee {} {} MPP and {} first hops {}overriding the network graph", our_node_pubkey,
1241-
payment_params.payee_pubkey, if allow_mpp { "with" } else { "without" },
1251+
log_trace!(logger, "Searching for a route from payer {} to payee {:?} {} MPP and {} first hops {}overriding the network graph", our_node_pubkey,
1252+
payment_params.payee, if allow_mpp { "with" } else { "without" },
12421253
first_hops.map(|hops| hops.len()).unwrap_or(0), if first_hops.is_some() { "" } else { "not " });
12431254

12441255
// Step (1).
@@ -1341,7 +1352,9 @@ where L::Target: Logger {
13411352
});
13421353
}
13431354

1344-
log_trace!(logger, "Building path from {} (payee) to {} (us/payer) for value {} msat.", payment_params.payee_pubkey, our_node_pubkey, final_value_msat);
1355+
log_trace!(logger, "Building path from {}payee with node id {:?} to payer {} for value {} msat.",
1356+
if payment_params.payee.node_id().is_some() { "blinded " } else { "" },
1357+
payment_params.payee.node_id(), our_node_pubkey, final_value_msat);
13451358

13461359
macro_rules! add_entry {
13471360
// Adds entry which goes from $src_node_id to $dest_node_id over the $candidate hop.
@@ -1590,7 +1603,7 @@ where L::Target: Logger {
15901603
// Entries are added to dist in add_entry!() when there is a channel from a node.
15911604
// Because there are no channels from payee, it will not have a dist entry at this point.
15921605
// If we're processing any other node, it is always be the result of a channel from it.
1593-
assert_eq!($node_id, payee_node_id);
1606+
assert_eq!($node_id, target_node_id);
15941607
false
15951608
};
15961609

@@ -1650,35 +1663,35 @@ where L::Target: Logger {
16501663

16511664
// If first hop is a private channel and the only way to reach the payee, this is the only
16521665
// place where it could be added.
1653-
if let Some(first_channels) = first_hop_targets.get(&payee_node_id) {
1666+
payee_node_id.map(|payee| first_hop_targets.get(&payee).map(|first_channels| {
16541667
for details in first_channels {
16551668
let candidate = CandidateRouteHop::FirstHop { details };
1656-
let added = add_entry!(candidate, our_node_id, payee_node_id, 0, path_value_msat,
1669+
let added = add_entry!(candidate, our_node_id, payee, 0, path_value_msat,
16571670
0, 0u64, 0, 0);
16581671
log_trace!(logger, "{} direct route to payee via SCID {}",
16591672
if added { "Added" } else { "Skipped" }, candidate.short_channel_id());
16601673
}
1661-
}
1674+
}));
16621675

16631676
// Add the payee as a target, so that the payee-to-payer
16641677
// search algorithm knows what to start with.
1665-
match network_nodes.get(&payee_node_id) {
1678+
payee_node_id.map(|payee| match network_nodes.get(&payee) {
16661679
// The payee is not in our network graph, so nothing to add here.
16671680
// There is still a chance of reaching them via last_hops though,
16681681
// so don't yet fail the payment here.
16691682
// If not, targets.pop() will not even let us enter the loop in step 2.
16701683
None => {},
16711684
Some(node) => {
1672-
add_entries_to_cheapest_to_target_node!(node, payee_node_id, 0, path_value_msat, 0, 0u64, 0, 0);
1685+
add_entries_to_cheapest_to_target_node!(node, payee, 0, path_value_msat, 0, 0u64, 0, 0);
16731686
},
1674-
}
1687+
});
16751688

16761689
// Step (2).
16771690
// If a caller provided us with last hops, add them to routing targets. Since this happens
16781691
// earlier than general path finding, they will be somewhat prioritized, although currently
16791692
// it matters only if the fees are exactly the same.
16801693
let route_hints = match &payment_params.payee {
1681-
Payee::Clear { route_hints } => route_hints,
1694+
Payee::Clear { route_hints, .. } => route_hints,
16821695
_ => return Err(LightningError{err: "Routing to blinded paths isn't supported yet".to_owned(), action: ErrorAction::IgnoreError}),
16831696
};
16841697
for route in route_hints.iter().filter(|route| !route.0.is_empty()) {
@@ -1693,7 +1706,7 @@ where L::Target: Logger {
16931706
// We start building the path from reverse, i.e., from payee
16941707
// to the first RouteHintHop in the path.
16951708
let hop_iter = route.0.iter().rev();
1696-
let prev_hop_iter = core::iter::once(&payment_params.payee_pubkey).chain(
1709+
let prev_hop_iter = core::iter::once(&target_pubkey).chain(
16971710
route.0.iter().skip(1).rev().map(|hop| &hop.src_node_id));
16981711
let mut hop_used = true;
16991712
let mut aggregate_next_hops_fee_msat: u64 = 0;
@@ -1853,7 +1866,7 @@ where L::Target: Logger {
18531866
// save this path for the payment route. Also, update the liquidity
18541867
// remaining on the used hops, so that we take them into account
18551868
// while looking for more paths.
1856-
if ordered_hops.last().unwrap().0.node_id == payee_node_id {
1869+
if ordered_hops.last().unwrap().0.node_id == target_node_id {
18571870
break 'path_walk;
18581871
}
18591872

@@ -1936,7 +1949,7 @@ where L::Target: Logger {
19361949
// If we found a path back to the payee, we shouldn't try to process it again. This is
19371950
// the equivalent of the `elem.was_processed` check in
19381951
// add_entries_to_cheapest_to_target_node!() (see comment there for more info).
1939-
if node_id == payee_node_id { continue 'path_construction; }
1952+
if node_id == target_node_id { continue 'path_construction; }
19401953

19411954
// Otherwise, since the current target node is not us,
19421955
// keep "unrolling" the payment graph from payee to payer by
@@ -2106,7 +2119,7 @@ where L::Target: Logger {
21062119
paths,
21072120
payment_params: Some(payment_params.clone()),
21082121
};
2109-
log_info!(logger, "Got route to {}: {}", payment_params.payee_pubkey, log_route!(route));
2122+
log_info!(logger, "Got route: {}", log_route!(route));
21102123
Ok(route)
21112124
}
21122125

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Backwards Compatibility
2+
3+
* `PaymentParameters` written with blinded path info using 0.0.115 will not be readable in 0.0.116

0 commit comments

Comments
 (0)