Skip to content

invoice: swap RouteHop for RouteHint #887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
run: RUSTFLAGS="-C link-dead-code" cargo build --verbose --color always
- name: Build on Rust ${{ matrix.toolchain }}
if: "! matrix.build-net-tokio"
run: cargo build --verbose --color always -p lightning
run: cargo build --verbose --color always -p lightning && cargo build --verbose --color always -p lightning-invoice
- name: Build Block Sync Clients on Rust ${{ matrix.toolchain }} with features
if: "matrix.build-net-tokio && !matrix.coverage"
run: |
Expand All @@ -74,7 +74,7 @@ jobs:
run: RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always
- name: Test on Rust ${{ matrix.toolchain }}
if: "! matrix.build-net-tokio"
run: cargo test --verbose --color always -p lightning
run: cargo test --verbose --color always -p lightning && cargo test --verbose --color always -p lightning-invoice
- name: Test Block Sync Clients on Rust ${{ matrix.toolchain }} with features
if: "matrix.build-net-tokio && !matrix.coverage"
run: |
Expand Down
4 changes: 2 additions & 2 deletions fuzz/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use lightning::chain;
use lightning::ln::channelmanager::ChannelDetails;
use lightning::ln::features::InitFeatures;
use lightning::ln::msgs;
use lightning::routing::router::{get_route, RouteHint};
use lightning::routing::router::{get_route, RouteHintHop};
use lightning::util::logger::Logger;
use lightning::util::ser::Readable;
use lightning::routing::network_graph::{NetworkGraph, RoutingFees};
Expand Down Expand Up @@ -224,7 +224,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
for _ in 0..count {
scid += 1;
let rnid = node_pks.iter().skip(slice_to_be16(get_slice!(2))as usize % node_pks.len()).next().unwrap();
last_hops_vec.push(RouteHint {
last_hops_vec.push(RouteHintHop {
src_node_id: *rnid,
short_channel_id: scid,
fees: RoutingFees {
Expand Down
1 change: 1 addition & 0 deletions lightning-invoice/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ readme = "README.md"

[dependencies]
bech32 = "0.7"
lightning = { version = "0.0.13", path = "../lightning" }
secp256k1 = { version = "0.20", features = ["recovery"] }
num-traits = "0.2.8"
bitcoin_hashes = "0.9.4"
Expand Down
74 changes: 45 additions & 29 deletions lightning-invoice/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use bech32::{u5, FromBase32};

use bitcoin_hashes::Hash;
use bitcoin_hashes::sha256;
use lightning::routing::network_graph::RoutingFees;
use lightning::routing::router::RouteHintHop;

use num_traits::{CheckedAdd, CheckedMul};

Expand Down Expand Up @@ -353,7 +355,7 @@ impl FromBase32 for Signature {
}
}

fn parse_int_be<T, U>(digits: &[U], base: T) -> Option<T>
pub(crate) fn parse_int_be<T, U>(digits: &[U], base: T) -> Option<T>
where T: CheckedAdd + CheckedMul + From<u8> + Default,
U: Into<u8> + Copy
{
Expand Down Expand Up @@ -428,7 +430,7 @@ impl FromBase32 for TaggedField {
constants::TAG_FALLBACK =>
Ok(TaggedField::Fallback(Fallback::from_base32(field_data)?)),
constants::TAG_ROUTE =>
Ok(TaggedField::Route(Route::from_base32(field_data)?)),
Ok(TaggedField::Route(RouteHint::from_base32(field_data)?)),
constants::TAG_PAYMENT_SECRET =>
Ok(TaggedField::PaymentSecret(PaymentSecret::from_base32(field_data)?)),
_ => {
Expand Down Expand Up @@ -565,17 +567,17 @@ impl FromBase32 for Fallback {
}
}

impl FromBase32 for Route {
impl FromBase32 for RouteHint {
type Err = ParseError;

fn from_base32(field_data: &[u5]) -> Result<Route, ParseError> {
fn from_base32(field_data: &[u5]) -> Result<RouteHint, ParseError> {
let bytes = Vec::<u8>::from_base32(field_data)?;

if bytes.len() % 51 != 0 {
return Err(ParseError::UnexpectedEndOfTaggedFields);
}

let mut route_hops = Vec::<RouteHop>::new();
let mut route_hops = Vec::<RouteHintHop>::new();

let mut bytes = bytes.as_slice();
while !bytes.is_empty() {
Expand All @@ -585,18 +587,22 @@ impl FromBase32 for Route {
let mut channel_id: [u8; 8] = Default::default();
channel_id.copy_from_slice(&hop_bytes[33..41]);

let hop = RouteHop {
pubkey: PublicKey::from_slice(&hop_bytes[0..33])?,
short_channel_id: channel_id,
fee_base_msat: parse_int_be(&hop_bytes[41..45], 256).expect("slice too big?"),
fee_proportional_millionths: parse_int_be(&hop_bytes[45..49], 256).expect("slice too big?"),
cltv_expiry_delta: parse_int_be(&hop_bytes[49..51], 256).expect("slice too big?")
let hop = RouteHintHop {
src_node_id: PublicKey::from_slice(&hop_bytes[0..33])?,
short_channel_id: parse_int_be(&channel_id, 256).expect("short chan ID slice too big?"),
fees: RoutingFees {
base_msat: parse_int_be(&hop_bytes[41..45], 256).expect("slice too big?"),
proportional_millionths: parse_int_be(&hop_bytes[45..49], 256).expect("slice too big?"),
},
cltv_expiry_delta: parse_int_be(&hop_bytes[49..51], 256).expect("slice too big?"),
htlc_minimum_msat: None,
htlc_maximum_msat: None,
};

route_hops.push(hop);
}

Ok(Route(route_hops))
Ok(RouteHint(route_hops))
}
}

Expand Down Expand Up @@ -931,47 +937,57 @@ mod test {

#[test]
fn test_parse_route() {
use RouteHop;
use ::Route;
use lightning::routing::network_graph::RoutingFees;
use lightning::routing::router::RouteHintHop;
use ::RouteHint;
use bech32::FromBase32;
use de::parse_int_be;

let input = from_bech32(
"q20q82gphp2nflc7jtzrcazrra7wwgzxqc8u7754cdlpfrmccae92qgzqvzq2ps8pqqqqqqpqqqqq9qqqvpeuqa\
fqxu92d8lr6fvg0r5gv0heeeqgcrqlnm6jhphu9y00rrhy4grqszsvpcgpy9qqqqqqgqqqqq7qqzq".as_bytes()
);

let mut expected = Vec::<RouteHop>::new();
expected.push(RouteHop {
pubkey: PublicKey::from_slice(
let mut expected = Vec::<RouteHintHop>::new();
expected.push(RouteHintHop {
src_node_id: PublicKey::from_slice(
&[
0x02u8, 0x9e, 0x03, 0xa9, 0x01, 0xb8, 0x55, 0x34, 0xff, 0x1e, 0x92, 0xc4, 0x3c,
0x74, 0x43, 0x1f, 0x7c, 0xe7, 0x20, 0x46, 0x06, 0x0f, 0xcf, 0x7a, 0x95, 0xc3,
0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55
][..]
).unwrap(),
short_channel_id: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
fee_base_msat: 1,
fee_proportional_millionths: 20,
cltv_expiry_delta: 3
short_channel_id: parse_int_be(&[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], 256).expect("short chan ID slice too big?"),
fees: RoutingFees {
base_msat: 1,
proportional_millionths: 20,
},
cltv_expiry_delta: 3,
htlc_minimum_msat: None,
htlc_maximum_msat: None
});
expected.push(RouteHop {
pubkey: PublicKey::from_slice(
expected.push(RouteHintHop {
src_node_id: PublicKey::from_slice(
&[
0x03u8, 0x9e, 0x03, 0xa9, 0x01, 0xb8, 0x55, 0x34, 0xff, 0x1e, 0x92, 0xc4, 0x3c,
0x74, 0x43, 0x1f, 0x7c, 0xe7, 0x20, 0x46, 0x06, 0x0f, 0xcf, 0x7a, 0x95, 0xc3,
0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55
][..]
).unwrap(),
short_channel_id: [0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a],
fee_base_msat: 2,
fee_proportional_millionths: 30,
cltv_expiry_delta: 4
short_channel_id: parse_int_be(&[0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a], 256).expect("short chan ID slice too big?"),
fees: RoutingFees {
base_msat: 2,
proportional_millionths: 30,
},
cltv_expiry_delta: 4,
htlc_minimum_msat: None,
htlc_maximum_msat: None
});

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

assert_eq!(
Route::from_base32(&[u5::try_from_u8(0).unwrap(); 40][..]),
RouteHint::from_base32(&[u5::try_from_u8(0).unwrap(); 40][..]),
Err(ParseError::UnexpectedEndOfTaggedFields)
);
}
Expand Down
Loading