Skip to content

Commit d7b18d0

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

File tree

5 files changed

+98
-78
lines changed

5 files changed

+98
-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: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
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+
use lightning::routing::network_graph::RoutingFees;
28+
use lightning::routing::router::RouteHint;
2629

2730
use secp256k1::key::PublicKey;
2831
use secp256k1::{Message, Secp256k1};
@@ -383,26 +386,7 @@ pub struct Signature(pub RecoverableSignature);
383386
/// The encoded route has to be <1024 5bit characters long (<=639 bytes or <=12 hops)
384387
///
385388
#[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-
}
389+
pub struct Route(Vec<RouteHint>);
406390

407391
/// Tag constants as specified in BOLT11
408392
#[allow(missing_docs)]
@@ -499,7 +483,7 @@ impl<D: tb::Bool, H: tb::Bool, T: tb::Bool> InvoiceBuilder<D, H, T> {
499483
}
500484

501485
/// Adds a private route.
502-
pub fn route(mut self, route: Vec<RouteHop>) -> Self {
486+
pub fn route(mut self, route: Vec<RouteHint>) -> Self {
503487
match Route::new(route) {
504488
Ok(r) => self.tagged_fields.push(TaggedField::Route(r)),
505489
Err(e) => self.error = Some(e),
@@ -1159,7 +1143,7 @@ impl ExpiryTime {
11591143

11601144
impl Route {
11611145
/// Create a new (partial) route from a list of hops
1162-
pub fn new(hops: Vec<RouteHop>) -> Result<Route, CreationError> {
1146+
pub fn new(hops: Vec<RouteHint>) -> Result<Route, CreationError> {
11631147
if hops.len() <= 12 {
11641148
Ok(Route(hops))
11651149
} else {
@@ -1168,21 +1152,21 @@ impl Route {
11681152
}
11691153

11701154
/// Returrn the underlying vector of hops
1171-
pub fn into_inner(self) -> Vec<RouteHop> {
1155+
pub fn into_inner(self) -> Vec<RouteHint> {
11721156
self.0
11731157
}
11741158
}
11751159

1176-
impl Into<Vec<RouteHop>> for Route {
1177-
fn into(self) -> Vec<RouteHop> {
1160+
impl Into<Vec<RouteHint>> for Route {
1161+
fn into(self) -> Vec<RouteHint> {
11781162
self.into_inner()
11791163
}
11801164
}
11811165

11821166
impl Deref for Route {
1183-
type Target = Vec<RouteHop>;
1167+
type Target = Vec<RouteHint>;
11841168

1185-
fn deref(&self) -> &Vec<RouteHop> {
1169+
fn deref(&self) -> &Vec<RouteHint> {
11861170
&self.0
11871171
}
11881172
}
@@ -1458,18 +1442,22 @@ mod test {
14581442
.build_raw();
14591443
assert_eq!(long_desc_res, Err(CreationError::DescriptionTooLong));
14601444

1461-
let route_hop = RouteHop {
1462-
pubkey: PublicKey::from_slice(
1445+
let route_hop = RouteHint {
1446+
src_node_id: PublicKey::from_slice(
14631447
&[
14641448
0x03, 0x9e, 0x03, 0xa9, 0x01, 0xb8, 0x55, 0x34, 0xff, 0x1e, 0x92, 0xc4,
14651449
0x3c, 0x74, 0x43, 0x1f, 0x7c, 0xe7, 0x20, 0x46, 0x06, 0x0f, 0xcf, 0x7a,
14661450
0x95, 0xc3, 0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55
14671451
][..]
14681452
).unwrap(),
1469-
short_channel_id: [0; 8],
1470-
fee_base_msat: 0,
1471-
fee_proportional_millionths: 0,
1453+
short_channel_id: 0,
1454+
fees: RoutingFees {
1455+
base_msat: 0,
1456+
proportional_millionths: 0,
1457+
},
14721458
cltv_expiry_delta: 0,
1459+
htlc_minimum_msat: None,
1460+
htlc_maximum_msat: None,
14731461
};
14741462
let too_long_route = vec![route_hop; 13];
14751463
let long_route_res = builder.clone()
@@ -1505,36 +1493,52 @@ mod test {
15051493
let public_key = PublicKey::from_secret_key(&secp_ctx, &private_key);
15061494

15071495
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,
1496+
RouteHint {
1497+
src_node_id: public_key.clone(),
1498+
short_channel_id: u64::from_be_bytes([123; 8]),
1499+
fees: RoutingFees {
1500+
base_msat: 2,
1501+
proportional_millionths: 1,
1502+
},
15131503
cltv_expiry_delta: 145,
1504+
htlc_minimum_msat: None,
1505+
htlc_maximum_msat: None,
15141506
},
1515-
RouteHop {
1516-
pubkey: public_key.clone(),
1517-
short_channel_id: [42; 8],
1518-
fee_base_msat: 3,
1519-
fee_proportional_millionths: 2,
1507+
RouteHint {
1508+
src_node_id: public_key.clone(),
1509+
short_channel_id: u64::from_be_bytes([42; 8]),
1510+
fees: RoutingFees {
1511+
base_msat: 3,
1512+
proportional_millionths: 2,
1513+
},
15201514
cltv_expiry_delta: 146,
1515+
htlc_minimum_msat: None,
1516+
htlc_maximum_msat: None,
15211517
}
15221518
];
15231519

15241520
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,
1521+
RouteHint {
1522+
src_node_id: public_key.clone(),
1523+
short_channel_id: 0,
1524+
fees: RoutingFees {
1525+
base_msat: 4,
1526+
proportional_millionths: 3,
1527+
},
15301528
cltv_expiry_delta: 147,
1529+
htlc_minimum_msat: None,
1530+
htlc_maximum_msat: None,
15311531
},
1532-
RouteHop {
1533-
pubkey: public_key.clone(),
1534-
short_channel_id: [1; 8],
1535-
fee_base_msat: 5,
1536-
fee_proportional_millionths: 4,
1532+
RouteHint {
1533+
src_node_id: public_key.clone(),
1534+
short_channel_id: u64::from_be_bytes([1; 8]),
1535+
fees: RoutingFees {
1536+
base_msat: 5,
1537+
proportional_millionths: 4,
1538+
},
15371539
cltv_expiry_delta: 148,
1540+
htlc_minimum_msat: None,
1541+
htlc_maximum_msat: None,
15381542
}
15391543
];
15401544

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)