Skip to content

Commit cc6e29d

Browse files
committed
Simplify FFIRoute
* When passing struct through FFI boundary, if the object has nested array it will cause headache for preventing it from GCed. So we just pass binary serialized object and (de)serialize after passing. In this case, `FFIRoute`. * Prepare Debug trait for Route object.
1 parent b4c28b8 commit cc6e29d

File tree

2 files changed

+23
-64
lines changed

2 files changed

+23
-64
lines changed

bindings/src/adaptors/primitives.rs

Lines changed: 12 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use crate::FFIResult;
77
use lightning::chain::chaininterface::ChainError;
88
use lightning::ln::channelmanager::PaymentHash;
99
use lightning::ln::router::{RouteHop, Route};
10+
use lightning::util::ser::Readable;
11+
use bitcoin_hashes::core::fmt::{Formatter, Error};
1012

1113
macro_rules! array_struct{
1214
(
@@ -34,6 +36,12 @@ macro_rules! array_struct{
3436
}
3537
}
3638

39+
impl<'a> std::fmt::Debug for $name<'a> {
40+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41+
hex::encode(self).fmt(f)
42+
}
43+
}
44+
3745
impl<'a> AsRef<[$ty]> for $name<'a> {
3846
fn as_ref(&self) -> &[$ty] {
3947
unsafe_block!("" => std::slice::from_raw_parts(self.ptr, self.len))
@@ -132,72 +140,14 @@ impl From<FFIChainError> for ChainError {
132140
}
133141
}
134142

135-
array_struct!(NodeFeatures, u8);
136-
137-
impl<'a> From<NodeFeatures<'a>> for lightning::ln::features::NodeFeatures {
138-
fn from(value: NodeFeatures<'a>) -> Self {
139-
let v = value.as_ref().to_vec();
140-
lightning::ln::features::NodeFeatures::from_le_bytes(v)
141-
}
142-
}
143-
144-
array_struct!(ChannelFeatures, u8);
145-
impl<'a> From<ChannelFeatures<'a>> for lightning::ln::features::ChannelFeatures {
146-
fn from(value: ChannelFeatures<'a>) -> Self {
147-
let v = value.as_ref().to_vec();
148-
lightning::ln::features::ChannelFeatures::from_le_bytes(v)
149-
}
150-
}
151-
152-
153-
154-
#[derive(Clone)]
155-
#[repr(C)]
156-
pub struct FFIRouteHop<'a> {
157-
/// The node_id of the node at this hop.
158-
pub pubkey: PublicKey<'a>,
159-
/// The node_announcement features of the node at this hop. For the last hop, these may be
160-
/// amended to match the features present in the invoice this node generated.
161-
pub node_features: NodeFeatures<'a>,
162-
/// The channel that should be used from the previous hop to reach this node.
163-
pub short_channel_id: u64,
164-
/// The channel_announcement features of the channel that should be used from the previous hop
165-
/// to reach this node.
166-
pub channel_features: ChannelFeatures<'a>,
167-
/// The fee taken on this hop. For the last hop, this should be the full value of the payment.
168-
pub fee_msat: u64,
169-
/// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
170-
/// expected at the destination, in excess of the current block height.
171-
pub cltv_expiry_delta: u32,
172-
}
173-
174-
impl<'a> TryFrom<FFIRouteHop<'a>> for RouteHop {
175-
type Error = FFIResult;
176-
177-
fn try_from(value: FFIRouteHop<'a>) -> Result<Self, Self::Error> {
178-
Ok(RouteHop {
179-
pubkey: value.pubkey.try_into()?,
180-
node_features: value.node_features.into(),
181-
short_channel_id: value.short_channel_id,
182-
channel_features: value.channel_features.into(),
183-
fee_msat: value.fee_msat,
184-
cltv_expiry_delta: value.cltv_expiry_delta
185-
})
186-
}
187-
}
188-
189-
array_struct!(FFIRoute, FFIRouteHop<'a>);
143+
array_struct!(FFIRoute, u8);
190144

191145
impl<'a> TryFrom<FFIRoute<'a>> for Route {
192-
type Error = FFIResult;
146+
type Error = lightning::ln::msgs::DecodeError;
193147

194148
fn try_from(value: FFIRoute<'a>) -> Result<Self, Self::Error> {
195-
let r: &[FFIRouteHop] = value.as_ref();
196-
let mut hops: Vec<RouteHop> = Vec::with_capacity(r.len());
197-
for i in r.iter() {
198-
hops.push(i.clone().try_into()?);
199-
}
200-
Ok(Route { hops })
149+
let mut slice = value.as_ref();
150+
<Route as Readable>::read(&mut slice)
201151
}
202152
}
203153

lightning/src/ln/router.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use std::collections::btree_map::Entry as BtreeEntry;
2828
use std;
2929

3030
/// A hop in a route
31-
#[derive(Clone, PartialEq)]
31+
#[derive(Clone, PartialEq, Debug)]
3232
pub struct RouteHop {
3333
/// The node_id of the node at this hop.
3434
pub pubkey: PublicKey,
@@ -47,7 +47,16 @@ pub struct RouteHop {
4747
pub cltv_expiry_delta: u32,
4848
}
4949

50-
impl Writeable for Vec<RouteHop> {
50+
/// A route from us through the network to a destination
51+
#[derive(Clone, PartialEq, Debug)]
52+
pub struct Route {
53+
/// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
54+
/// must always be at least length one. By protocol rules, this may not currently exceed 20 in
55+
/// length.
56+
pub hops: Vec<RouteHop>,
57+
}
58+
59+
impl Writeable for Route {
5160
fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
5261
(self.len() as u8).write(writer)?;
5362
for hop in self.iter() {

0 commit comments

Comments
 (0)