Skip to content

Commit 5a65c63

Browse files
committed
Better track lowest_inbound_channel_fees
Before this, we would not update per-node fees when deleting channels, resulting in worse routing decisions.
1 parent 3249958 commit 5a65c63

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

lightning/src/routing/gossip.rs

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,7 +1433,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
14331433
// b) we don't track UTXOs of channels we know about and remove them if they
14341434
// get reorg'd out.
14351435
// c) it's unclear how to do so without exposing ourselves to massive DoS risk.
1436-
Self::remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
1436+
self.remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
14371437
*entry.get_mut() = channel_info;
14381438
} else {
14391439
return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
@@ -1565,7 +1565,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
15651565
if is_permanent {
15661566
if let Some(chan) = channels.remove(&short_channel_id) {
15671567
let mut nodes = self.nodes.write().unwrap();
1568-
Self::remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
1568+
self.remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
15691569
}
15701570
} else {
15711571
if let Some(chan) = channels.get_mut(&short_channel_id) {
@@ -1646,7 +1646,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
16461646
let mut nodes = self.nodes.write().unwrap();
16471647
for scid in scids_to_remove {
16481648
let info = channels.remove(&scid).expect("We just accessed this scid, it should be present");
1649-
Self::remove_channel_in_nodes(&mut nodes, &info, scid);
1649+
self.remove_channel_in_nodes(&mut nodes, &info, scid);
16501650
}
16511651
}
16521652
}
@@ -1780,56 +1780,24 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
17801780

17811781
let mut nodes = self.nodes.write().unwrap();
17821782
let node = nodes.get_mut(&dest_node_id).unwrap();
1783-
let mut updated_lowest_inbound_channel_fee = None;
17841783
if chan_enabled {
17851784
let mut base_msat = msg.fee_base_msat;
17861785
let mut proportional_millionths = msg.fee_proportional_millionths;
17871786
if let Some(fees) = node.lowest_inbound_channel_fees {
17881787
base_msat = cmp::min(base_msat, fees.base_msat);
17891788
proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
17901789
}
1791-
updated_lowest_inbound_channel_fee = Some(RoutingFees {
1792-
base_msat,
1793-
proportional_millionths
1794-
});
1790+
self.update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels, Some(RoutingFees {
1791+
base_msat, proportional_millionths
1792+
}));
17951793
} else if chan_was_enabled {
1796-
for chan_id in node.channels.iter() {
1797-
let chan = channels.get(chan_id).unwrap();
1798-
let chan_info_opt;
1799-
if chan.node_one == dest_node_id {
1800-
chan_info_opt = chan.two_to_one.as_ref();
1801-
} else {
1802-
chan_info_opt = chan.one_to_two.as_ref();
1803-
}
1804-
if let Some(chan_info) = chan_info_opt {
1805-
if chan_info.enabled {
1806-
let fees = updated_lowest_inbound_channel_fee.get_or_insert(RoutingFees {
1807-
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
1808-
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
1809-
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
1810-
}
1811-
}
1812-
}
1813-
}
1814-
1815-
if updated_lowest_inbound_channel_fee.is_some() {
1816-
node.lowest_inbound_channel_fees = updated_lowest_inbound_channel_fee;
1817-
1818-
for (_, chan) in channels.iter_mut() {
1819-
if chan.node_one == dest_node_id {
1820-
chan.lowest_inbound_channel_fees_to_two = updated_lowest_inbound_channel_fee;
1821-
}
1822-
1823-
if chan.node_two == dest_node_id {
1824-
chan.lowest_inbound_channel_fees_to_one = updated_lowest_inbound_channel_fee;
1825-
}
1826-
}
1794+
self.recompute_and_update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels);
18271795
}
18281796

18291797
Ok(())
18301798
}
18311799

1832-
fn remove_channel_in_nodes(nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
1800+
fn remove_channel_in_nodes(&self, nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
18331801
macro_rules! remove_from_node {
18341802
($node_id: expr) => {
18351803
if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
@@ -1842,12 +1810,51 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
18421810
} else {
18431811
panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
18441812
}
1813+
if let Some(node) = nodes.get_mut(&$node_id) {
1814+
self.recompute_and_update_lowest_inbound_channel_fees($node_id, node, &mut self.channels.write().unwrap());
1815+
}
18451816
}
18461817
}
18471818

18481819
remove_from_node!(chan.node_one);
18491820
remove_from_node!(chan.node_two);
18501821
}
1822+
1823+
fn recompute_and_update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>) {
1824+
let mut updated_lowest_inbound_channel_fee = None;
1825+
for chan_id in node.channels.iter() {
1826+
let chan = channels.get(chan_id).unwrap();
1827+
let chan_info_opt;
1828+
if chan.node_one == node_id {
1829+
chan_info_opt = chan.two_to_one.as_ref();
1830+
} else {
1831+
chan_info_opt = chan.one_to_two.as_ref();
1832+
}
1833+
if let Some(chan_info) = chan_info_opt {
1834+
if chan_info.enabled {
1835+
let fees = updated_lowest_inbound_channel_fee.get_or_insert(RoutingFees {
1836+
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
1837+
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
1838+
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
1839+
}
1840+
}
1841+
}
1842+
self.update_lowest_inbound_channel_fees(node_id, node, channels, updated_lowest_inbound_channel_fee);
1843+
}
1844+
1845+
fn update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>, updated_fees: Option<RoutingFees>) {
1846+
node.lowest_inbound_channel_fees = updated_fees;
1847+
for (_, chan) in channels.iter_mut() {
1848+
if chan.node_one == node_id {
1849+
chan.lowest_inbound_channel_fees_to_two = updated_fees;
1850+
}
1851+
1852+
if chan.node_two == node_id {
1853+
chan.lowest_inbound_channel_fees_to_one = updated_fees;
1854+
}
1855+
}
1856+
}
1857+
18511858
}
18521859

18531860
impl ReadOnlyNetworkGraph<'_> {

0 commit comments

Comments
 (0)