Skip to content

Commit 1cde3ec

Browse files
invoice: swap RouteHop for RouteHint
To prevent naming conflicts in bindings
1 parent 52f1d45 commit 1cde3ec

File tree

5 files changed

+99
-78
lines changed

5 files changed

+99
-78
lines changed

lightning-invoice/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ readme = "README.md"
1010

1111
[dependencies]
1212
bech32 = "0.7"
13+
lightning = { version = "0.0.13", path = "../lightning" }
1314
secp256k1 = { version = "0.20", features = ["recovery"] }
1415
num-traits = "0.2.8"
1516
bitcoin_hashes = "0.9.4"

lightning-invoice/src/de.rs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use bech32::{u5, FromBase32};
1010

1111
use bitcoin_hashes::Hash;
1212
use bitcoin_hashes::sha256;
13+
use lightning::routing::network_graph::RoutingFees;
14+
use lightning::routing::router::RouteHint;
1315

1416
use num_traits::{CheckedAdd, CheckedMul};
1517

@@ -575,7 +577,7 @@ impl FromBase32 for Route {
575577
return Err(ParseError::UnexpectedEndOfTaggedFields);
576578
}
577579

578-
let mut route_hops = Vec::<RouteHop>::new();
580+
let mut route_hops = Vec::<RouteHint>::new();
579581

580582
let mut bytes = bytes.as_slice();
581583
while !bytes.is_empty() {
@@ -585,12 +587,16 @@ impl FromBase32 for Route {
585587
let mut channel_id: [u8; 8] = Default::default();
586588
channel_id.copy_from_slice(&hop_bytes[33..41]);
587589

588-
let hop = RouteHop {
589-
pubkey: PublicKey::from_slice(&hop_bytes[0..33])?,
590-
short_channel_id: channel_id,
591-
fee_base_msat: parse_int_be(&hop_bytes[41..45], 256).expect("slice too big?"),
592-
fee_proportional_millionths: parse_int_be(&hop_bytes[45..49], 256).expect("slice too big?"),
593-
cltv_expiry_delta: parse_int_be(&hop_bytes[49..51], 256).expect("slice too big?")
590+
let hop = RouteHint {
591+
src_node_id: PublicKey::from_slice(&hop_bytes[0..33])?,
592+
short_channel_id: u64::from_be_bytes(channel_id),
593+
fees: RoutingFees {
594+
base_msat: parse_int_be(&hop_bytes[41..45], 256).expect("slice too big?"),
595+
proportional_millionths: parse_int_be(&hop_bytes[45..49], 256).expect("slice too big?"),
596+
},
597+
cltv_expiry_delta: parse_int_be(&hop_bytes[49..51], 256).expect("slice too big?"),
598+
htlc_minimum_msat: None,
599+
htlc_maximum_msat: None,
594600
};
595601

596602
route_hops.push(hop);
@@ -931,7 +937,8 @@ mod test {
931937

932938
#[test]
933939
fn test_parse_route() {
934-
use RouteHop;
940+
use lightning::routing::network_graph::RoutingFees;
941+
use lightning::routing::router::RouteHint;
935942
use ::Route;
936943
use bech32::FromBase32;
937944

@@ -940,32 +947,40 @@ mod test {
940947
fqxu92d8lr6fvg0r5gv0heeeqgcrqlnm6jhphu9y00rrhy4grqszsvpcgpy9qqqqqqgqqqqq7qqzq".as_bytes()
941948
);
942949

943-
let mut expected = Vec::<RouteHop>::new();
944-
expected.push(RouteHop {
945-
pubkey: PublicKey::from_slice(
950+
let mut expected = Vec::<RouteHint>::new();
951+
expected.push(RouteHint {
952+
src_node_id: PublicKey::from_slice(
946953
&[
947954
0x02u8, 0x9e, 0x03, 0xa9, 0x01, 0xb8, 0x55, 0x34, 0xff, 0x1e, 0x92, 0xc4, 0x3c,
948955
0x74, 0x43, 0x1f, 0x7c, 0xe7, 0x20, 0x46, 0x06, 0x0f, 0xcf, 0x7a, 0x95, 0xc3,
949956
0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55
950957
][..]
951958
).unwrap(),
952-
short_channel_id: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
953-
fee_base_msat: 1,
954-
fee_proportional_millionths: 20,
955-
cltv_expiry_delta: 3
959+
short_channel_id: u64::from_be_bytes([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]),
960+
fees: RoutingFees {
961+
base_msat: 1,
962+
proportional_millionths: 20,
963+
},
964+
cltv_expiry_delta: 3,
965+
htlc_minimum_msat: None,
966+
htlc_maximum_msat: None
956967
});
957-
expected.push(RouteHop {
958-
pubkey: PublicKey::from_slice(
968+
expected.push(RouteHint {
969+
src_node_id: PublicKey::from_slice(
959970
&[
960971
0x03u8, 0x9e, 0x03, 0xa9, 0x01, 0xb8, 0x55, 0x34, 0xff, 0x1e, 0x92, 0xc4, 0x3c,
961972
0x74, 0x43, 0x1f, 0x7c, 0xe7, 0x20, 0x46, 0x06, 0x0f, 0xcf, 0x7a, 0x95, 0xc3,
962973
0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55
963974
][..]
964975
).unwrap(),
965-
short_channel_id: [0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a],
966-
fee_base_msat: 2,
967-
fee_proportional_millionths: 30,
968-
cltv_expiry_delta: 4
976+
short_channel_id: u64::from_be_bytes([0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a]),
977+
fees: RoutingFees {
978+
base_msat: 2,
979+
proportional_millionths: 30,
980+
},
981+
cltv_expiry_delta: 4,
982+
htlc_minimum_msat: None,
983+
htlc_maximum_msat: None
969984
});
970985

971986
assert_eq!(Route::from_base32(&input), Ok(Route(expected)));

lightning-invoice/src/lib.rs

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
1818
extern crate bech32;
1919
extern crate bitcoin_hashes;
20+
extern crate lightning;
2021
extern crate num_traits;
2122
extern crate secp256k1;
2223

2324
use bech32::u5;
2425
use bitcoin_hashes::Hash;
2526
use bitcoin_hashes::sha256;
27+
#[cfg(any(doc, test))]
28+
use lightning::routing::network_graph::RoutingFees;
29+
use lightning::routing::router::RouteHint;
2630

2731
use secp256k1::key::PublicKey;
2832
use secp256k1::{Message, Secp256k1};
@@ -383,26 +387,7 @@ pub struct Signature(pub RecoverableSignature);
383387
/// The encoded route has to be <1024 5bit characters long (<=639 bytes or <=12 hops)
384388
///
385389
#[derive(Eq, PartialEq, Debug, Clone)]
386-
pub struct Route(Vec<RouteHop>);
387-
388-
/// Node on a private route
389-
#[derive(Eq, PartialEq, Debug, Clone)]
390-
pub struct RouteHop {
391-
/// Node's public key
392-
pub pubkey: PublicKey,
393-
394-
/// Which channel of this node we would be using
395-
pub short_channel_id: [u8; 8],
396-
397-
/// Fee charged by this node per transaction
398-
pub fee_base_msat: u32,
399-
400-
/// Fee charged by this node proportional to the amount routed
401-
pub fee_proportional_millionths: u32,
402-
403-
/// Delta substracted by this node from incoming cltv_expiry value
404-
pub cltv_expiry_delta: u16,
405-
}
390+
pub struct Route(Vec<RouteHint>);
406391

407392
/// Tag constants as specified in BOLT11
408393
#[allow(missing_docs)]
@@ -499,7 +484,7 @@ impl<D: tb::Bool, H: tb::Bool, T: tb::Bool> InvoiceBuilder<D, H, T> {
499484
}
500485

501486
/// Adds a private route.
502-
pub fn route(mut self, route: Vec<RouteHop>) -> Self {
487+
pub fn route(mut self, route: Vec<RouteHint>) -> Self {
503488
match Route::new(route) {
504489
Ok(r) => self.tagged_fields.push(TaggedField::Route(r)),
505490
Err(e) => self.error = Some(e),
@@ -1159,7 +1144,7 @@ impl ExpiryTime {
11591144

11601145
impl Route {
11611146
/// Create a new (partial) route from a list of hops
1162-
pub fn new(hops: Vec<RouteHop>) -> Result<Route, CreationError> {
1147+
pub fn new(hops: Vec<RouteHint>) -> Result<Route, CreationError> {
11631148
if hops.len() <= 12 {
11641149
Ok(Route(hops))
11651150
} else {
@@ -1168,21 +1153,21 @@ impl Route {
11681153
}
11691154

11701155
/// Returrn the underlying vector of hops
1171-
pub fn into_inner(self) -> Vec<RouteHop> {
1156+
pub fn into_inner(self) -> Vec<RouteHint> {
11721157
self.0
11731158
}
11741159
}
11751160

1176-
impl Into<Vec<RouteHop>> for Route {
1177-
fn into(self) -> Vec<RouteHop> {
1161+
impl Into<Vec<RouteHint>> for Route {
1162+
fn into(self) -> Vec<RouteHint> {
11781163
self.into_inner()
11791164
}
11801165
}
11811166

11821167
impl Deref for Route {
1183-
type Target = Vec<RouteHop>;
1168+
type Target = Vec<RouteHint>;
11841169

1185-
fn deref(&self) -> &Vec<RouteHop> {
1170+
fn deref(&self) -> &Vec<RouteHint> {
11861171
&self.0
11871172
}
11881173
}
@@ -1458,18 +1443,22 @@ mod test {
14581443
.build_raw();
14591444
assert_eq!(long_desc_res, Err(CreationError::DescriptionTooLong));
14601445

1461-
let route_hop = RouteHop {
1462-
pubkey: PublicKey::from_slice(
1446+
let route_hop = RouteHint {
1447+
src_node_id: PublicKey::from_slice(
14631448
&[
14641449
0x03, 0x9e, 0x03, 0xa9, 0x01, 0xb8, 0x55, 0x34, 0xff, 0x1e, 0x92, 0xc4,
14651450
0x3c, 0x74, 0x43, 0x1f, 0x7c, 0xe7, 0x20, 0x46, 0x06, 0x0f, 0xcf, 0x7a,
14661451
0x95, 0xc3, 0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55
14671452
][..]
14681453
).unwrap(),
1469-
short_channel_id: [0; 8],
1470-
fee_base_msat: 0,
1471-
fee_proportional_millionths: 0,
1454+
short_channel_id: 0,
1455+
fees: RoutingFees {
1456+
base_msat: 0,
1457+
proportional_millionths: 0,
1458+
},
14721459
cltv_expiry_delta: 0,
1460+
htlc_minimum_msat: None,
1461+
htlc_maximum_msat: None,
14731462
};
14741463
let too_long_route = vec![route_hop; 13];
14751464
let long_route_res = builder.clone()
@@ -1505,36 +1494,52 @@ mod test {
15051494
let public_key = PublicKey::from_secret_key(&secp_ctx, &private_key);
15061495

15071496
let route_1 = vec![
1508-
RouteHop {
1509-
pubkey: public_key.clone(),
1510-
short_channel_id: [123; 8],
1511-
fee_base_msat: 2,
1512-
fee_proportional_millionths: 1,
1497+
RouteHint {
1498+
src_node_id: public_key.clone(),
1499+
short_channel_id: u64::from_be_bytes([123; 8]),
1500+
fees: RoutingFees {
1501+
base_msat: 2,
1502+
proportional_millionths: 1,
1503+
},
15131504
cltv_expiry_delta: 145,
1505+
htlc_minimum_msat: None,
1506+
htlc_maximum_msat: None,
15141507
},
1515-
RouteHop {
1516-
pubkey: public_key.clone(),
1517-
short_channel_id: [42; 8],
1518-
fee_base_msat: 3,
1519-
fee_proportional_millionths: 2,
1508+
RouteHint {
1509+
src_node_id: public_key.clone(),
1510+
short_channel_id: u64::from_be_bytes([42; 8]),
1511+
fees: RoutingFees {
1512+
base_msat: 3,
1513+
proportional_millionths: 2,
1514+
},
15201515
cltv_expiry_delta: 146,
1516+
htlc_minimum_msat: None,
1517+
htlc_maximum_msat: None,
15211518
}
15221519
];
15231520

15241521
let route_2 = vec![
1525-
RouteHop {
1526-
pubkey: public_key.clone(),
1527-
short_channel_id: [0; 8],
1528-
fee_base_msat: 4,
1529-
fee_proportional_millionths: 3,
1522+
RouteHint {
1523+
src_node_id: public_key.clone(),
1524+
short_channel_id: 0,
1525+
fees: RoutingFees {
1526+
base_msat: 4,
1527+
proportional_millionths: 3,
1528+
},
15301529
cltv_expiry_delta: 147,
1530+
htlc_minimum_msat: None,
1531+
htlc_maximum_msat: None,
15311532
},
1532-
RouteHop {
1533-
pubkey: public_key.clone(),
1534-
short_channel_id: [1; 8],
1535-
fee_base_msat: 5,
1536-
fee_proportional_millionths: 4,
1533+
RouteHint {
1534+
src_node_id: public_key.clone(),
1535+
short_channel_id: u64::from_be_bytes([1; 8]),
1536+
fees: RoutingFees {
1537+
base_msat: 5,
1538+
proportional_millionths: 4,
1539+
},
15371540
cltv_expiry_delta: 148,
1541+
htlc_minimum_msat: None,
1542+
htlc_maximum_msat: None,
15381543
}
15391544
];
15401545

lightning-invoice/src/ser.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,17 +370,17 @@ impl ToBase32 for Route {
370370
let mut converter = BytesToBase32::new(writer);
371371

372372
for hop in self.iter() {
373-
converter.append(&hop.pubkey.serialize()[..])?;
374-
converter.append(&hop.short_channel_id[..])?;
373+
converter.append(&hop.src_node_id.serialize()[..])?;
374+
converter.append(&hop.short_channel_id.to_be_bytes())?;
375375

376376
let fee_base_msat = try_stretch(
377-
encode_int_be_base256(hop.fee_base_msat),
377+
encode_int_be_base256(hop.fees.base_msat),
378378
4
379379
).expect("sizeof(u32) == 4");
380380
converter.append(&fee_base_msat)?;
381381

382382
let fee_proportional_millionths = try_stretch(
383-
encode_int_be_base256(hop.fee_proportional_millionths),
383+
encode_int_be_base256(hop.fees.proportional_millionths),
384384
4
385385
).expect("sizeof(u32) == 4");
386386
converter.append(&fee_proportional_millionths)?;

lightning/src/routing/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl Readable for Route {
117117
}
118118

119119
/// A channel descriptor which provides a last-hop route to get_route
120-
#[derive(Clone)]
120+
#[derive(Eq, PartialEq, Debug, Clone)]
121121
pub struct RouteHint {
122122
/// The node_id of the non-target end of the route
123123
pub src_node_id: PublicKey,

0 commit comments

Comments
 (0)