Skip to content

Commit 371fac9

Browse files
committed
Make routing fees an optional struct for a node info
1 parent f2e0334 commit 371fac9

File tree

2 files changed

+86
-40
lines changed

2 files changed

+86
-40
lines changed

lightning/src/ln/network_graph.rs

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ impl NetworkGraph {
4040
let mut nodes = BTreeMap::new();
4141
nodes.insert(our_pubkey.clone(), NodeInfo {
4242
channels: Vec::new(),
43-
lowest_inbound_channel_fee_base_msat: u32::max_value(),
44-
lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
43+
lowest_inbound_channel_fees: None,
4544
features: NodeFeatures::empty(),
4645
last_update: None,
4746
rgb: [0; 3],
@@ -229,15 +228,43 @@ impl_writeable!(ChannelInfo, 0, {
229228
announcement_message
230229
});
231230

231+
232+
/// Fees for routing via a given channel or a node
233+
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
234+
pub struct RoutingFees {
235+
/// Flat routing fee
236+
pub base_msat: u32,
237+
/// Liquidity-based routing fee
238+
pub proportional_millionths: u32,
239+
}
240+
241+
impl Readable for RoutingFees{
242+
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<RoutingFees, DecodeError> {
243+
let base_msat: u32 = Readable::read(reader)?;
244+
let proportional_millionths: u32 = Readable::read(reader)?;
245+
Ok(RoutingFees {
246+
base_msat,
247+
proportional_millionths,
248+
})
249+
}
250+
}
251+
252+
impl Writeable for RoutingFees {
253+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
254+
self.base_msat.write(writer)?;
255+
self.proportional_millionths.write(writer)?;
256+
Ok(())
257+
}
258+
}
259+
260+
232261
#[derive(PartialEq)]
233262
/// Details regarding a node in the network
234263
pub struct NodeInfo {
235264
/// All valid channels a node has announced
236265
pub channels: Vec<u64>,
237-
/// Lowest flat routing fee of all known channels to the node
238-
pub lowest_inbound_channel_fee_base_msat: u32,
239-
/// Lowest liquidity-based routing fee of all known channels to the node
240-
pub lowest_inbound_channel_fee_proportional_millionths: u32,
266+
/// Lowest fees enabling routing via any of the known channels to a node
267+
pub lowest_inbound_channel_fees: Option<RoutingFees>,
241268
/// Protocol features the node announced support for
242269
pub features: NodeFeatures,
243270
/// When the last known update to the node state was issued
@@ -259,7 +286,7 @@ pub struct NodeInfo {
259286

260287
impl std::fmt::Display for NodeInfo {
261288
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
262-
write!(f, "features: {}, last_update: {:?}, lowest_inbound_channel_fee_base_msat: {}, lowest_inbound_channel_fee_proportional_millionths: {}, channels: {:?}", log_bytes!(self.features.encode()), self.last_update, self.lowest_inbound_channel_fee_base_msat, self.lowest_inbound_channel_fee_proportional_millionths, &self.channels[..])?;
289+
write!(f, "features: {}, last_update: {:?}, lowest_inbound_channel_fees: {:?}, channels: {:?}", log_bytes!(self.features.encode()), self.last_update, self.lowest_inbound_channel_fees, &self.channels[..])?;
263290
Ok(())
264291
}
265292
}
@@ -270,8 +297,7 @@ impl Writeable for NodeInfo {
270297
for ref chan in self.channels.iter() {
271298
chan.write(writer)?;
272299
}
273-
self.lowest_inbound_channel_fee_base_msat.write(writer)?;
274-
self.lowest_inbound_channel_fee_proportional_millionths.write(writer)?;
300+
self.lowest_inbound_channel_fees.write(writer)?;
275301
self.features.write(writer)?;
276302
self.last_update.write(writer)?;
277303
self.rgb.write(writer)?;
@@ -294,8 +320,7 @@ impl Readable for NodeInfo {
294320
for _ in 0..channels_count {
295321
channels.push(Readable::read(reader)?);
296322
}
297-
let lowest_inbound_channel_fee_base_msat = Readable::read(reader)?;
298-
let lowest_inbound_channel_fee_proportional_millionths = Readable::read(reader)?;
323+
let lowest_inbound_channel_fees = Readable::read(reader)?;
299324
let features = Readable::read(reader)?;
300325
let last_update = Readable::read(reader)?;
301326
let rgb = Readable::read(reader)?;
@@ -313,8 +338,7 @@ impl Readable for NodeInfo {
313338
let announcement_message = Readable::read(reader)?;
314339
Ok(NodeInfo {
315340
channels,
316-
lowest_inbound_channel_fee_base_msat,
317-
lowest_inbound_channel_fee_proportional_millionths,
341+
lowest_inbound_channel_fees,
318342
features,
319343
last_update,
320344
rgb,
@@ -538,8 +562,7 @@ impl RoutingMessageHandler for NetworkGraph {
538562
BtreeEntry::Vacant(node_entry) => {
539563
node_entry.insert(NodeInfo {
540564
channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
541-
lowest_inbound_channel_fee_base_msat: u32::max_value(),
542-
lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
565+
lowest_inbound_channel_fees: None,
543566
features: NodeFeatures::empty(),
544567
last_update: None,
545568
rgb: [0; 3],
@@ -630,8 +653,16 @@ impl RoutingMessageHandler for NetworkGraph {
630653

631654
if chan_enabled {
632655
let node = network.nodes.get_mut(&dest_node_id).unwrap();
633-
node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
634-
node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
656+
let mut base_msat = msg.contents.fee_base_msat;
657+
let mut proportional_millionths = msg.contents.fee_proportional_millionths;
658+
if let Some(fees) = node.lowest_inbound_channel_fees {
659+
base_msat = cmp::min(base_msat, fees.base_msat);
660+
proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
661+
}
662+
node.lowest_inbound_channel_fees = Some(RoutingFees {
663+
base_msat,
664+
proportional_millionths
665+
});
635666
} else if chan_was_enabled {
636667
let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
637668
let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
@@ -653,8 +684,12 @@ impl RoutingMessageHandler for NetworkGraph {
653684

654685
//TODO: satisfy the borrow-checker without a double-map-lookup :(
655686
let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
656-
mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
657-
mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
687+
if mut_node.channels.len() > 0 {
688+
mut_node.lowest_inbound_channel_fees = Some(RoutingFees {
689+
base_msat: lowest_inbound_channel_fee_base_msat,
690+
proportional_millionths: lowest_inbound_channel_fee_proportional_millionths
691+
});
692+
}
658693
}
659694

660695
Ok(msg.contents.excess_data.is_empty())
@@ -720,7 +755,7 @@ impl RoutingMessageHandler for NetworkGraph {
720755
mod tests {
721756
use chain::chaininterface;
722757
use ln::features::{ChannelFeatures, NodeFeatures};
723-
use ln::network_graph::{NetworkGraph, NetworkMap, DirectionalChannelInfo, ChannelInfo, NodeInfo};
758+
use ln::network_graph::{NetworkGraph, NetworkMap, DirectionalChannelInfo, ChannelInfo, NodeInfo, RoutingFees};
724759
use ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
725760
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate};
726761
use util::test_utils;
@@ -1541,10 +1576,14 @@ mod tests {
15411576
let mut network = network_graph.network_map.write().unwrap();
15421577
let zero_hash = Sha256dHash::hash(&[0; 32]);
15431578

1579+
1580+
let node_routing_fees = RoutingFees {
1581+
base_msat: 100,
1582+
proportional_millionths: 0,
1583+
};
15441584
network.nodes.insert(node1.clone(), NodeInfo {
15451585
channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
1546-
lowest_inbound_channel_fee_base_msat: 100,
1547-
lowest_inbound_channel_fee_proportional_millionths: 0,
1586+
lowest_inbound_channel_fees: Some(node_routing_fees),
15481587
features: NodeFeatures::supported(),
15491588
last_update: Some(1),
15501589
rgb: [0; 3],

lightning/src/ln/router.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use secp256k1::key::PublicKey;
88
use ln::channelmanager;
99
use ln::features::{ChannelFeatures, NodeFeatures};
1010
use ln::msgs::{DecodeError,ErrorAction,LightningError};
11-
use ln::network_graph::NetworkGraph;
11+
use ln::network_graph::{NetworkGraph};
1212
use util::ser::{Writeable, Readable};
1313
use util::logger::Logger;
1414

@@ -245,9 +245,15 @@ impl Router {
245245
let hm_entry = dist.entry(&$directional_info.src_node_id);
246246
let old_entry = hm_entry.or_insert_with(|| {
247247
let node = network.nodes.get(&$directional_info.src_node_id).unwrap();
248+
let mut fee_base_msat = 0;
249+
let mut fee_proportional_millionths = 0;
250+
if let Some(fees) = node.lowest_inbound_channel_fees {
251+
fee_base_msat = fees.base_msat;
252+
fee_proportional_millionths = fees.proportional_millionths;
253+
};
248254
(u64::max_value(),
249-
node.lowest_inbound_channel_fee_base_msat,
250-
node.lowest_inbound_channel_fee_proportional_millionths,
255+
fee_base_msat,
256+
fee_proportional_millionths,
251257
RouteHop {
252258
pubkey: $dest_node_id.clone(),
253259
node_features: NodeFeatures::empty(),
@@ -397,7 +403,7 @@ impl Router {
397403
#[cfg(test)]
398404
mod tests {
399405
use chain::chaininterface;
400-
use ln::network_graph::{NetworkGraph, NodeInfo, NetworkMap, ChannelInfo, DirectionalChannelInfo};
406+
use ln::network_graph::{NetworkGraph, NodeInfo, NetworkMap, ChannelInfo, DirectionalChannelInfo, RoutingFees};
401407
use ln::router::{Router, RouteHint};
402408
use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
403409
use ln::msgs::{ErrorAction, LightningError};
@@ -512,10 +518,13 @@ mod tests {
512518
{
513519
let mut network = network_graph.network_map.write().unwrap();
514520

521+
let routing_fees = RoutingFees {
522+
base_msat: 100,
523+
proportional_millionths: 0
524+
};
515525
network.nodes.insert(node1.clone(), NodeInfo {
516526
channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
517-
lowest_inbound_channel_fee_base_msat: 100,
518-
lowest_inbound_channel_fee_proportional_millionths: 0,
527+
lowest_inbound_channel_fees: Some(routing_fees),
519528
features: NodeFeatures::from_le_bytes(id_to_feature_flags!(1)),
520529
last_update: Some(1),
521530
rgb: [0; 3],
@@ -546,10 +555,13 @@ mod tests {
546555
},
547556
announcement_message: None,
548557
});
558+
let routing_fees = RoutingFees {
559+
base_msat: 0,
560+
proportional_millionths: 0
561+
};
549562
network.nodes.insert(node2.clone(), NodeInfo {
550563
channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
551-
lowest_inbound_channel_fee_base_msat: 0,
552-
lowest_inbound_channel_fee_proportional_millionths: 0,
564+
lowest_inbound_channel_fees: Some(routing_fees.clone()),
553565
features: NodeFeatures::from_le_bytes(id_to_feature_flags!(2)),
554566
last_update: Some(1),
555567
rgb: [0; 3],
@@ -582,8 +594,7 @@ mod tests {
582594
});
583595
network.nodes.insert(node8.clone(), NodeInfo {
584596
channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
585-
lowest_inbound_channel_fee_base_msat: 0,
586-
lowest_inbound_channel_fee_proportional_millionths: 0,
597+
lowest_inbound_channel_fees: Some(routing_fees.clone()),
587598
features: NodeFeatures::from_le_bytes(id_to_feature_flags!(8)),
588599
last_update: Some(1),
589600
rgb: [0; 3],
@@ -622,8 +633,7 @@ mod tests {
622633
NetworkMap::get_key(5, zero_hash.clone()),
623634
NetworkMap::get_key(6, zero_hash.clone()),
624635
NetworkMap::get_key(7, zero_hash.clone())),
625-
lowest_inbound_channel_fee_base_msat: 0,
626-
lowest_inbound_channel_fee_proportional_millionths: 0,
636+
lowest_inbound_channel_fees: Some(routing_fees.clone()),
627637
features: NodeFeatures::from_le_bytes(id_to_feature_flags!(3)),
628638
last_update: Some(1),
629639
rgb: [0; 3],
@@ -702,8 +712,7 @@ mod tests {
702712
});
703713
network.nodes.insert(node4.clone(), NodeInfo {
704714
channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
705-
lowest_inbound_channel_fee_base_msat: 0,
706-
lowest_inbound_channel_fee_proportional_millionths: 0,
715+
lowest_inbound_channel_fees: Some(routing_fees.clone()),
707716
features: NodeFeatures::from_le_bytes(id_to_feature_flags!(4)),
708717
last_update: Some(1),
709718
rgb: [0; 3],
@@ -736,8 +745,7 @@ mod tests {
736745
});
737746
network.nodes.insert(node5.clone(), NodeInfo {
738747
channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
739-
lowest_inbound_channel_fee_base_msat: 0,
740-
lowest_inbound_channel_fee_proportional_millionths: 0,
748+
lowest_inbound_channel_fees: Some(routing_fees.clone()),
741749
features: NodeFeatures::from_le_bytes(id_to_feature_flags!(5)),
742750
last_update: Some(1),
743751
rgb: [0; 3],
@@ -793,8 +801,7 @@ mod tests {
793801
});
794802
network.nodes.insert(node6.clone(), NodeInfo {
795803
channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
796-
lowest_inbound_channel_fee_base_msat: 0,
797-
lowest_inbound_channel_fee_proportional_millionths: 0,
804+
lowest_inbound_channel_fees: Some(routing_fees.clone()),
798805
features: NodeFeatures::from_le_bytes(id_to_feature_flags!(6)),
799806
last_update: Some(1),
800807
rgb: [0; 3],

0 commit comments

Comments
 (0)