Closed
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
135 changes: 94 additions & 41 deletions lightning/src/routing/gossip.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -707,37 +707,45 @@ pub struct ChannelInfo {
/// (which we can probably assume we are - no-std environments probably won't have a full
/// network graph in memory!).
announcement_received_time: u64,
/// Lowest fees to enter the first direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_one: Option<RoutingFees>,
/// Lowest fees to enter the second direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_two: Option<RoutingFees>,
}

impl ChannelInfo {
/// Returns a [`DirectedChannelInfo`] for the channel directed to the given `target` from a
/// returned `source`, or `None` if `target` is not one of the channel's counterparties.
pub fn as_directed_to(&self, target: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, source) = {
let (direction, source, lowest_inbound_channel_fees) = {
if target == &self.node_one {
(self.two_to_one.as_ref(), &self.node_two)
(self.two_to_one.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if target == &self.node_two {
(self.one_to_two.as_ref(), &self.node_one)
(self.one_to_two.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), source))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), source))
}

/// Returns a [`DirectedChannelInfo`] for the channel directed from the given `source` to a
/// returned `target`, or `None` if `source` is not one of the channel's counterparties.
pub fn as_directed_from(&self, source: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, target) = {
let (direction, target, lowest_inbound_channel_fees) = {
if source == &self.node_one {
(self.one_to_two.as_ref(), &self.node_two)
(self.one_to_two.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if source == &self.node_two {
(self.two_to_one.as_ref(), &self.node_one)
(self.two_to_one.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), target))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), target))
}

/// Returns a [`ChannelUpdateInfo`] based on the direction implied by the channel_flag.
Expand DownExpand Up@@ -770,6 +778,8 @@ impl Writeable for ChannelInfo {
(8, self.two_to_one, required),
(10, self.capacity_sats, required),
(12, self.announcement_message, required),
(14, self.lowest_inbound_channel_fees_to_one, option),
(16, self.lowest_inbound_channel_fees_to_two, option),
});
Ok(())
}
Expand DownExpand Up@@ -803,6 +813,8 @@ impl Readable for ChannelInfo {
let mut two_to_one_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
init_tlv_field_var!(capacity_sats, required);
init_tlv_field_var!(announcement_message, required);
let mut lowest_inbound_channel_fees_to_one = None;
let mut lowest_inbound_channel_fees_to_two = None;
read_tlv_fields!(reader, {
(0, features, required),
(1, announcement_received_time, (default_value, 0)),
Expand All@@ -812,6 +824,8 @@ impl Readable for ChannelInfo {
(8, two_to_one_wrap, ignorable),
(10, capacity_sats, required),
(12, announcement_message, required),
(14, lowest_inbound_channel_fees_to_one, option),
(16, lowest_inbound_channel_fees_to_two, option),
});

Ok(ChannelInfo {
Expand All@@ -823,6 +837,8 @@ impl Readable for ChannelInfo {
capacity_sats: init_tlv_based_struct_field!(capacity_sats, required),
announcement_message: init_tlv_based_struct_field!(announcement_message, required),
announcement_received_time: init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
lowest_inbound_channel_fees_to_one: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_one, option),
lowest_inbound_channel_fees_to_two: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_two, option),
})
}
}
Expand All@@ -835,11 +851,12 @@ pub struct DirectedChannelInfo<'a> {
direction: Option<&'a ChannelUpdateInfo>,
htlc_maximum_msat: u64,
effective_capacity: EffectiveCapacity,
lowest_inbound_channel_fees: Option<RoutingFees>,
}

impl<'a> DirectedChannelInfo<'a> {
#[inline]
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>) -> Self {
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>, lowest_inbound_channel_fees: Option<RoutingFees>) -> Self {
let htlc_maximum_msat = direction.map(|direction| direction.htlc_maximum_msat);
let capacity_msat = channel.capacity_sats.map(|capacity_sats| capacity_sats * 1000);

Expand All@@ -858,7 +875,7 @@ impl<'a> DirectedChannelInfo<'a> {
};

Self {
channel, direction, htlc_maximum_msat, effective_capacity
channel, direction, htlc_maximum_msat, effective_capacity, lowest_inbound_channel_fees,
}
}

Expand All@@ -882,6 +899,13 @@ impl<'a> DirectedChannelInfo<'a> {
self.effective_capacity
}

/// Returns the [`Option<RoutingFees>`] to reach the channel in the direction.
///
/// This is based on the known and enabled channels to the entry node.
pub fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
self.lowest_inbound_channel_fees
}

/// Returns `Some` if [`ChannelUpdateInfo`] is available in the direction.
pub(super) fn with_update(self) -> Option<DirectedChannelInfoWithUpdate<'a>> {
match self.direction {
Expand DownExpand Up@@ -917,6 +941,10 @@ impl<'a> DirectedChannelInfoWithUpdate<'a> {
/// Returns the [`EffectiveCapacity`] of the channel in the direction.
#[inline]
pub(super) fn effective_capacity(&self) -> EffectiveCapacity { self.inner.effective_capacity() }

#[inline]
pub(super) fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> { self.inner.lowest_inbound_channel_fees() }

}

impl<'a> fmt::Debug for DirectedChannelInfoWithUpdate<'a> {
Expand DownExpand Up@@ -1382,6 +1410,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
capacity_sats: None,
announcement_message: None,
announcement_received_time: timestamp,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(short_channel_id, channel_info, None)
Expand All@@ -1408,7 +1438,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
// b) we don't track UTXOs of channels we know about and remove them if they
// get reorg'd out.
// c) it's unclear how to do so without exposing ourselves to massive DoS risk.
Self::remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
*entry.get_mut() = channel_info;
} else {
return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
Expand DownExpand Up@@ -1524,6 +1554,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
{ full_msg.cloned() } else { None },
announcement_received_time,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(msg.short_channel_id, chan_info, utxo_value)
Expand All@@ -1538,7 +1570,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
if is_permanent {
if let Some(chan) = channels.remove(&short_channel_id) {
let mut nodes = self.nodes.write().unwrap();
Self::remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
}
} else {
if let Some(chan) = channels.get_mut(&short_channel_id) {
Expand DownExpand Up@@ -1619,7 +1651,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
let mut nodes = self.nodes.write().unwrap();
for scid in scids_to_remove {
let info = channels.remove(&scid).expect("We just accessed this scid, it should be present");
Self::remove_channel_in_nodes(&mut nodes, &info, scid);
self.remove_channel_in_nodes(&mut nodes, &info, scid);
}
}
}
Expand DownExpand Up@@ -1752,47 +1784,25 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
}

let mut nodes = self.nodes.write().unwrap();
let node = nodes.get_mut(&dest_node_id).unwrap();
if chan_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut base_msat = msg.fee_base_msat;
let mut proportional_millionths = msg.fee_proportional_millionths;
if let Some(fees) = node.lowest_inbound_channel_fees {
base_msat = cmp::min(base_msat, fees.base_msat);
proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
}
node.lowest_inbound_channel_fees = Some(RoutingFees {
base_msat,
proportional_millionths
});
self.update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels, Some(RoutingFees {
base_msat, proportional_millionths
}));
} else if chan_was_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut lowest_inbound_channel_fees = None;

for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == dest_node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = lowest_inbound_channel_fees.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}

node.lowest_inbound_channel_fees = lowest_inbound_channel_fees;
self.recompute_and_update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels);
}

Ok(())
}

fn remove_channel_in_nodes(nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
fn remove_channel_in_nodes(&self, nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
macro_rules! remove_from_node {
($node_id: expr) => {
if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
Expand All@@ -1805,12 +1815,51 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
} else {
panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
}
if let Some(node) = nodes.get_mut(&$node_id) {
self.recompute_and_update_lowest_inbound_channel_fees($node_id, node, &mut self.channels.write().unwrap());
}
}
}

remove_from_node!(chan.node_one);
remove_from_node!(chan.node_two);
}

fn recompute_and_update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>) {
let mut updated_lowest_inbound_channel_fee = None;
for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = updated_lowest_inbound_channel_fee.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}
self.update_lowest_inbound_channel_fees(node_id, node, channels, updated_lowest_inbound_channel_fee);
}

fn update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>, updated_fees: Option<RoutingFees>) {
node.lowest_inbound_channel_fees = updated_fees;
for (_, chan) in channels.iter_mut() {
if chan.node_one == node_id {
chan.lowest_inbound_channel_fees_to_two = updated_fees;
}

if chan.node_two == node_id {
chan.lowest_inbound_channel_fees_to_one = updated_fees;
}
}
}

}

impl ReadOnlyNetworkGraph<'_> {
Expand DownExpand Up@@ -3019,6 +3068,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand All@@ -3037,6 +3088,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand Down
12 changes: 11 additions & 1 deletion lightning/src/routing/router.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -483,6 +483,16 @@ impl<'a> CandidateRouteHop<'a> {
CandidateRouteHop::PrivateHop { .. } => EffectiveCapacity::Infinite,
}
}

fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
match self {
CandidateRouteHop::FirstHop { .. } => Some(RoutingFees {
base_msat: 0, proportional_millionths: 0,
}),
CandidateRouteHop::PublicHop { info, .. } => info.lowest_inbound_channel_fees(),
CandidateRouteHop::PrivateHop { .. } => None,
}
}
}

#[inline]
Expand DownExpand Up@@ -1070,7 +1080,7 @@ where L::Target: Logger {
// as a way to reach the $dest_node_id.
let mut fee_base_msat = 0;
let mut fee_proportional_millionths = 0;
if let Some(Some(fees)) = network_nodes.get(&$src_node_id).map(|node| node.lowest_inbound_channel_fees) {
if let Some(fees) = $candidate.lowest_inbound_channel_fees() {
fee_base_msat = fees.base_msat;
fee_proportional_millionths = fees.proportional_millionths;
}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Closed
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
135 changes: 94 additions & 41 deletions lightning/src/routing/gossip.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -707,37 +707,45 @@ pub struct ChannelInfo {
/// (which we can probably assume we are - no-std environments probably won't have a full
/// network graph in memory!).
announcement_received_time: u64,
/// Lowest fees to enter the first direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_one: Option<RoutingFees>,
/// Lowest fees to enter the second direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_two: Option<RoutingFees>,
}

impl ChannelInfo {
/// Returns a [`DirectedChannelInfo`] for the channel directed to the given `target` from a
/// returned `source`, or `None` if `target` is not one of the channel's counterparties.
pub fn as_directed_to(&self, target: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, source) = {
let (direction, source, lowest_inbound_channel_fees) = {
if target == &self.node_one {
(self.two_to_one.as_ref(), &self.node_two)
(self.two_to_one.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if target == &self.node_two {
(self.one_to_two.as_ref(), &self.node_one)
(self.one_to_two.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), source))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), source))
}

/// Returns a [`DirectedChannelInfo`] for the channel directed from the given `source` to a
/// returned `target`, or `None` if `source` is not one of the channel's counterparties.
pub fn as_directed_from(&self, source: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, target) = {
let (direction, target, lowest_inbound_channel_fees) = {
if source == &self.node_one {
(self.one_to_two.as_ref(), &self.node_two)
(self.one_to_two.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if source == &self.node_two {
(self.two_to_one.as_ref(), &self.node_one)
(self.two_to_one.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), target))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), target))
}

/// Returns a [`ChannelUpdateInfo`] based on the direction implied by the channel_flag.
Expand DownExpand Up@@ -770,6 +778,8 @@ impl Writeable for ChannelInfo {
(8, self.two_to_one, required),
(10, self.capacity_sats, required),
(12, self.announcement_message, required),
(14, self.lowest_inbound_channel_fees_to_one, option),
(16, self.lowest_inbound_channel_fees_to_two, option),
});
Ok(())
}
Expand DownExpand Up@@ -803,6 +813,8 @@ impl Readable for ChannelInfo {
let mut two_to_one_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
init_tlv_field_var!(capacity_sats, required);
init_tlv_field_var!(announcement_message, required);
let mut lowest_inbound_channel_fees_to_one = None;
let mut lowest_inbound_channel_fees_to_two = None;
read_tlv_fields!(reader, {
(0, features, required),
(1, announcement_received_time, (default_value, 0)),
Expand All@@ -812,6 +824,8 @@ impl Readable for ChannelInfo {
(8, two_to_one_wrap, ignorable),
(10, capacity_sats, required),
(12, announcement_message, required),
(14, lowest_inbound_channel_fees_to_one, option),
(16, lowest_inbound_channel_fees_to_two, option),
});

Ok(ChannelInfo {
Expand All@@ -823,6 +837,8 @@ impl Readable for ChannelInfo {
capacity_sats: init_tlv_based_struct_field!(capacity_sats, required),
announcement_message: init_tlv_based_struct_field!(announcement_message, required),
announcement_received_time: init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
lowest_inbound_channel_fees_to_one: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_one, option),
lowest_inbound_channel_fees_to_two: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_two, option),
})
}
}
Expand All@@ -835,11 +851,12 @@ pub struct DirectedChannelInfo<'a> {
direction: Option<&'a ChannelUpdateInfo>,
htlc_maximum_msat: u64,
effective_capacity: EffectiveCapacity,
lowest_inbound_channel_fees: Option<RoutingFees>,
}

impl<'a> DirectedChannelInfo<'a> {
#[inline]
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>) -> Self {
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>, lowest_inbound_channel_fees: Option<RoutingFees>) -> Self {
let htlc_maximum_msat = direction.map(|direction| direction.htlc_maximum_msat);
let capacity_msat = channel.capacity_sats.map(|capacity_sats| capacity_sats * 1000);

Expand All@@ -858,7 +875,7 @@ impl<'a> DirectedChannelInfo<'a> {
};

Self {
channel, direction, htlc_maximum_msat, effective_capacity
channel, direction, htlc_maximum_msat, effective_capacity, lowest_inbound_channel_fees,
}
}

Expand All@@ -882,6 +899,13 @@ impl<'a> DirectedChannelInfo<'a> {
self.effective_capacity
}

/// Returns the [`Option<RoutingFees>`] to reach the channel in the direction.
///
/// This is based on the known and enabled channels to the entry node.
pub fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
self.lowest_inbound_channel_fees
}

/// Returns `Some` if [`ChannelUpdateInfo`] is available in the direction.
pub(super) fn with_update(self) -> Option<DirectedChannelInfoWithUpdate<'a>> {
match self.direction {
Expand DownExpand Up@@ -917,6 +941,10 @@ impl<'a> DirectedChannelInfoWithUpdate<'a> {
/// Returns the [`EffectiveCapacity`] of the channel in the direction.
#[inline]
pub(super) fn effective_capacity(&self) -> EffectiveCapacity { self.inner.effective_capacity() }

#[inline]
pub(super) fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> { self.inner.lowest_inbound_channel_fees() }

}

impl<'a> fmt::Debug for DirectedChannelInfoWithUpdate<'a> {
Expand DownExpand Up@@ -1382,6 +1410,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
capacity_sats: None,
announcement_message: None,
announcement_received_time: timestamp,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(short_channel_id, channel_info, None)
Expand All@@ -1408,7 +1438,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
// b) we don't track UTXOs of channels we know about and remove them if they
// get reorg'd out.
// c) it's unclear how to do so without exposing ourselves to massive DoS risk.
Self::remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
*entry.get_mut() = channel_info;
} else {
return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
Expand DownExpand Up@@ -1524,6 +1554,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
{ full_msg.cloned() } else { None },
announcement_received_time,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(msg.short_channel_id, chan_info, utxo_value)
Expand All@@ -1538,7 +1570,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
if is_permanent {
if let Some(chan) = channels.remove(&short_channel_id) {
let mut nodes = self.nodes.write().unwrap();
Self::remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
}
} else {
if let Some(chan) = channels.get_mut(&short_channel_id) {
Expand DownExpand Up@@ -1619,7 +1651,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
let mut nodes = self.nodes.write().unwrap();
for scid in scids_to_remove {
let info = channels.remove(&scid).expect("We just accessed this scid, it should be present");
Self::remove_channel_in_nodes(&mut nodes, &info, scid);
self.remove_channel_in_nodes(&mut nodes, &info, scid);
}
}
}
Expand DownExpand Up@@ -1752,47 +1784,25 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
}

let mut nodes = self.nodes.write().unwrap();
let node = nodes.get_mut(&dest_node_id).unwrap();
if chan_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut base_msat = msg.fee_base_msat;
let mut proportional_millionths = msg.fee_proportional_millionths;
if let Some(fees) = node.lowest_inbound_channel_fees {
base_msat = cmp::min(base_msat, fees.base_msat);
proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
}
node.lowest_inbound_channel_fees = Some(RoutingFees {
base_msat,
proportional_millionths
});
self.update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels, Some(RoutingFees {
base_msat, proportional_millionths
}));
} else if chan_was_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut lowest_inbound_channel_fees = None;

for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == dest_node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = lowest_inbound_channel_fees.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}

node.lowest_inbound_channel_fees = lowest_inbound_channel_fees;
self.recompute_and_update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels);
}

Ok(())
}

fn remove_channel_in_nodes(nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
fn remove_channel_in_nodes(&self, nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
macro_rules! remove_from_node {
($node_id: expr) => {
if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
Expand All@@ -1805,12 +1815,51 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
} else {
panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
}
if let Some(node) = nodes.get_mut(&$node_id) {
self.recompute_and_update_lowest_inbound_channel_fees($node_id, node, &mut self.channels.write().unwrap());
}
}
}

remove_from_node!(chan.node_one);
remove_from_node!(chan.node_two);
}

fn recompute_and_update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>) {
let mut updated_lowest_inbound_channel_fee = None;
for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = updated_lowest_inbound_channel_fee.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}
self.update_lowest_inbound_channel_fees(node_id, node, channels, updated_lowest_inbound_channel_fee);
}

fn update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>, updated_fees: Option<RoutingFees>) {
node.lowest_inbound_channel_fees = updated_fees;
for (_, chan) in channels.iter_mut() {
if chan.node_one == node_id {
chan.lowest_inbound_channel_fees_to_two = updated_fees;
}

if chan.node_two == node_id {
chan.lowest_inbound_channel_fees_to_one = updated_fees;
}
}
}

}

impl ReadOnlyNetworkGraph<'_> {
Expand DownExpand Up@@ -3019,6 +3068,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand All@@ -3037,6 +3088,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand Down
12 changes: 11 additions & 1 deletion lightning/src/routing/router.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -483,6 +483,16 @@ impl<'a> CandidateRouteHop<'a> {
CandidateRouteHop::PrivateHop { .. } => EffectiveCapacity::Infinite,
}
}

fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
match self {
CandidateRouteHop::FirstHop { .. } => Some(RoutingFees {
base_msat: 0, proportional_millionths: 0,
}),
CandidateRouteHop::PublicHop { info, .. } => info.lowest_inbound_channel_fees(),
CandidateRouteHop::PrivateHop { .. } => None,
}
}
}

#[inline]
Expand DownExpand Up@@ -1070,7 +1080,7 @@ where L::Target: Logger {
// as a way to reach the $dest_node_id.
let mut fee_base_msat = 0;
let mut fee_proportional_millionths = 0;
if let Some(Some(fees)) = network_nodes.get(&$src_node_id).map(|node| node.lowest_inbound_channel_fees) {
if let Some(fees) = $candidate.lowest_inbound_channel_fees() {
fee_base_msat = fees.base_msat;
fee_proportional_millionths = fees.proportional_millionths;
}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
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
135 changes: 94 additions & 41 deletions lightning/src/routing/gossip.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -707,37 +707,45 @@ pub struct ChannelInfo {
/// (which we can probably assume we are - no-std environments probably won't have a full
/// network graph in memory!).
announcement_received_time: u64,
/// Lowest fees to enter the first direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_one: Option<RoutingFees>,
/// Lowest fees to enter the second direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_two: Option<RoutingFees>,
}

impl ChannelInfo {
/// Returns a [`DirectedChannelInfo`] for the channel directed to the given `target` from a
/// returned `source`, or `None` if `target` is not one of the channel's counterparties.
pub fn as_directed_to(&self, target: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, source) = {
let (direction, source, lowest_inbound_channel_fees) = {
if target == &self.node_one {
(self.two_to_one.as_ref(), &self.node_two)
(self.two_to_one.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if target == &self.node_two {
(self.one_to_two.as_ref(), &self.node_one)
(self.one_to_two.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), source))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), source))
}

/// Returns a [`DirectedChannelInfo`] for the channel directed from the given `source` to a
/// returned `target`, or `None` if `source` is not one of the channel's counterparties.
pub fn as_directed_from(&self, source: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, target) = {
let (direction, target, lowest_inbound_channel_fees) = {
if source == &self.node_one {
(self.one_to_two.as_ref(), &self.node_two)
(self.one_to_two.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if source == &self.node_two {
(self.two_to_one.as_ref(), &self.node_one)
(self.two_to_one.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), target))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), target))
}

/// Returns a [`ChannelUpdateInfo`] based on the direction implied by the channel_flag.
Expand DownExpand Up@@ -770,6 +778,8 @@ impl Writeable for ChannelInfo {
(8, self.two_to_one, required),
(10, self.capacity_sats, required),
(12, self.announcement_message, required),
(14, self.lowest_inbound_channel_fees_to_one, option),
(16, self.lowest_inbound_channel_fees_to_two, option),
});
Ok(())
}
Expand DownExpand Up@@ -803,6 +813,8 @@ impl Readable for ChannelInfo {
let mut two_to_one_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
init_tlv_field_var!(capacity_sats, required);
init_tlv_field_var!(announcement_message, required);
let mut lowest_inbound_channel_fees_to_one = None;
let mut lowest_inbound_channel_fees_to_two = None;
read_tlv_fields!(reader, {
(0, features, required),
(1, announcement_received_time, (default_value, 0)),
Expand All@@ -812,6 +824,8 @@ impl Readable for ChannelInfo {
(8, two_to_one_wrap, ignorable),
(10, capacity_sats, required),
(12, announcement_message, required),
(14, lowest_inbound_channel_fees_to_one, option),
(16, lowest_inbound_channel_fees_to_two, option),
});

Ok(ChannelInfo {
Expand All@@ -823,6 +837,8 @@ impl Readable for ChannelInfo {
capacity_sats: init_tlv_based_struct_field!(capacity_sats, required),
announcement_message: init_tlv_based_struct_field!(announcement_message, required),
announcement_received_time: init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
lowest_inbound_channel_fees_to_one: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_one, option),
lowest_inbound_channel_fees_to_two: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_two, option),
})
}
}
Expand All@@ -835,11 +851,12 @@ pub struct DirectedChannelInfo<'a> {
direction: Option<&'a ChannelUpdateInfo>,
htlc_maximum_msat: u64,
effective_capacity: EffectiveCapacity,
lowest_inbound_channel_fees: Option<RoutingFees>,
}

impl<'a> DirectedChannelInfo<'a> {
#[inline]
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>) -> Self {
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>, lowest_inbound_channel_fees: Option<RoutingFees>) -> Self {
let htlc_maximum_msat = direction.map(|direction| direction.htlc_maximum_msat);
let capacity_msat = channel.capacity_sats.map(|capacity_sats| capacity_sats * 1000);

Expand All@@ -858,7 +875,7 @@ impl<'a> DirectedChannelInfo<'a> {
};

Self {
channel, direction, htlc_maximum_msat, effective_capacity
channel, direction, htlc_maximum_msat, effective_capacity, lowest_inbound_channel_fees,
}
}

Expand All@@ -882,6 +899,13 @@ impl<'a> DirectedChannelInfo<'a> {
self.effective_capacity
}

/// Returns the [`Option<RoutingFees>`] to reach the channel in the direction.
///
/// This is based on the known and enabled channels to the entry node.
pub fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
self.lowest_inbound_channel_fees
}

/// Returns `Some` if [`ChannelUpdateInfo`] is available in the direction.
pub(super) fn with_update(self) -> Option<DirectedChannelInfoWithUpdate<'a>> {
match self.direction {
Expand DownExpand Up@@ -917,6 +941,10 @@ impl<'a> DirectedChannelInfoWithUpdate<'a> {
/// Returns the [`EffectiveCapacity`] of the channel in the direction.
#[inline]
pub(super) fn effective_capacity(&self) -> EffectiveCapacity { self.inner.effective_capacity() }

#[inline]
pub(super) fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> { self.inner.lowest_inbound_channel_fees() }

}

impl<'a> fmt::Debug for DirectedChannelInfoWithUpdate<'a> {
Expand DownExpand Up@@ -1382,6 +1410,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
capacity_sats: None,
announcement_message: None,
announcement_received_time: timestamp,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(short_channel_id, channel_info, None)
Expand All@@ -1408,7 +1438,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
// b) we don't track UTXOs of channels we know about and remove them if they
// get reorg'd out.
// c) it's unclear how to do so without exposing ourselves to massive DoS risk.
Self::remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
*entry.get_mut() = channel_info;
} else {
return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
Expand DownExpand Up@@ -1524,6 +1554,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
{ full_msg.cloned() } else { None },
announcement_received_time,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(msg.short_channel_id, chan_info, utxo_value)
Expand All@@ -1538,7 +1570,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
if is_permanent {
if let Some(chan) = channels.remove(&short_channel_id) {
let mut nodes = self.nodes.write().unwrap();
Self::remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
}
} else {
if let Some(chan) = channels.get_mut(&short_channel_id) {
Expand DownExpand Up@@ -1619,7 +1651,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
let mut nodes = self.nodes.write().unwrap();
for scid in scids_to_remove {
let info = channels.remove(&scid).expect("We just accessed this scid, it should be present");
Self::remove_channel_in_nodes(&mut nodes, &info, scid);
self.remove_channel_in_nodes(&mut nodes, &info, scid);
}
}
}
Expand DownExpand Up@@ -1752,47 +1784,25 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
}

let mut nodes = self.nodes.write().unwrap();
let node = nodes.get_mut(&dest_node_id).unwrap();
if chan_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut base_msat = msg.fee_base_msat;
let mut proportional_millionths = msg.fee_proportional_millionths;
if let Some(fees) = node.lowest_inbound_channel_fees {
base_msat = cmp::min(base_msat, fees.base_msat);
proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
}
node.lowest_inbound_channel_fees = Some(RoutingFees {
base_msat,
proportional_millionths
});
self.update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels, Some(RoutingFees {
base_msat, proportional_millionths
}));
} else if chan_was_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut lowest_inbound_channel_fees = None;

for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == dest_node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = lowest_inbound_channel_fees.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}

node.lowest_inbound_channel_fees = lowest_inbound_channel_fees;
self.recompute_and_update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels);
}

Ok(())
}

fn remove_channel_in_nodes(nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
fn remove_channel_in_nodes(&self, nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
macro_rules! remove_from_node {
($node_id: expr) => {
if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
Expand All@@ -1805,12 +1815,51 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
} else {
panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
}
if let Some(node) = nodes.get_mut(&$node_id) {
self.recompute_and_update_lowest_inbound_channel_fees($node_id, node, &mut self.channels.write().unwrap());
}
}
}

remove_from_node!(chan.node_one);
remove_from_node!(chan.node_two);
}

fn recompute_and_update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>) {
let mut updated_lowest_inbound_channel_fee = None;
for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = updated_lowest_inbound_channel_fee.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}
self.update_lowest_inbound_channel_fees(node_id, node, channels, updated_lowest_inbound_channel_fee);
}

fn update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>, updated_fees: Option<RoutingFees>) {
node.lowest_inbound_channel_fees = updated_fees;
for (_, chan) in channels.iter_mut() {
if chan.node_one == node_id {
chan.lowest_inbound_channel_fees_to_two = updated_fees;
}

if chan.node_two == node_id {
chan.lowest_inbound_channel_fees_to_one = updated_fees;
}
}
}

}

impl ReadOnlyNetworkGraph<'_> {
Expand DownExpand Up@@ -3019,6 +3068,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand All@@ -3037,6 +3088,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand Down
12 changes: 11 additions & 1 deletion lightning/src/routing/router.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -483,6 +483,16 @@ impl<'a> CandidateRouteHop<'a> {
CandidateRouteHop::PrivateHop { .. } => EffectiveCapacity::Infinite,
}
}

fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
match self {
CandidateRouteHop::FirstHop { .. } => Some(RoutingFees {
base_msat: 0, proportional_millionths: 0,
}),
CandidateRouteHop::PublicHop { info, .. } => info.lowest_inbound_channel_fees(),
CandidateRouteHop::PrivateHop { .. } => None,
}
}
}

#[inline]
Expand DownExpand Up@@ -1070,7 +1080,7 @@ where L::Target: Logger {
// as a way to reach the $dest_node_id.
let mut fee_base_msat = 0;
let mut fee_proportional_millionths = 0;
if let Some(Some(fees)) = network_nodes.get(&$src_node_id).map(|node| node.lowest_inbound_channel_fees) {
if let Some(fees) = $candidate.lowest_inbound_channel_fees() {
fee_base_msat = fees.base_msat;
fee_proportional_millionths = fees.proportional_millionths;
}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
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
135 changes: 94 additions & 41 deletions lightning/src/routing/gossip.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -707,37 +707,45 @@ pub struct ChannelInfo {
/// (which we can probably assume we are - no-std environments probably won't have a full
/// network graph in memory!).
announcement_received_time: u64,
/// Lowest fees to enter the first direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_one: Option<RoutingFees>,
/// Lowest fees to enter the second direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_two: Option<RoutingFees>,
}

impl ChannelInfo {
/// Returns a [`DirectedChannelInfo`] for the channel directed to the given `target` from a
/// returned `source`, or `None` if `target` is not one of the channel's counterparties.
pub fn as_directed_to(&self, target: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, source) = {
let (direction, source, lowest_inbound_channel_fees) = {
if target == &self.node_one {
(self.two_to_one.as_ref(), &self.node_two)
(self.two_to_one.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if target == &self.node_two {
(self.one_to_two.as_ref(), &self.node_one)
(self.one_to_two.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), source))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), source))
}

/// Returns a [`DirectedChannelInfo`] for the channel directed from the given `source` to a
/// returned `target`, or `None` if `source` is not one of the channel's counterparties.
pub fn as_directed_from(&self, source: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, target) = {
let (direction, target, lowest_inbound_channel_fees) = {
if source == &self.node_one {
(self.one_to_two.as_ref(), &self.node_two)
(self.one_to_two.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if source == &self.node_two {
(self.two_to_one.as_ref(), &self.node_one)
(self.two_to_one.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), target))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), target))
}

/// Returns a [`ChannelUpdateInfo`] based on the direction implied by the channel_flag.
Expand DownExpand Up@@ -770,6 +778,8 @@ impl Writeable for ChannelInfo {
(8, self.two_to_one, required),
(10, self.capacity_sats, required),
(12, self.announcement_message, required),
(14, self.lowest_inbound_channel_fees_to_one, option),
(16, self.lowest_inbound_channel_fees_to_two, option),
});
Ok(())
}
Expand DownExpand Up@@ -803,6 +813,8 @@ impl Readable for ChannelInfo {
let mut two_to_one_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
init_tlv_field_var!(capacity_sats, required);
init_tlv_field_var!(announcement_message, required);
let mut lowest_inbound_channel_fees_to_one = None;
let mut lowest_inbound_channel_fees_to_two = None;
read_tlv_fields!(reader, {
(0, features, required),
(1, announcement_received_time, (default_value, 0)),
Expand All@@ -812,6 +824,8 @@ impl Readable for ChannelInfo {
(8, two_to_one_wrap, ignorable),
(10, capacity_sats, required),
(12, announcement_message, required),
(14, lowest_inbound_channel_fees_to_one, option),
(16, lowest_inbound_channel_fees_to_two, option),
});

Ok(ChannelInfo {
Expand All@@ -823,6 +837,8 @@ impl Readable for ChannelInfo {
capacity_sats: init_tlv_based_struct_field!(capacity_sats, required),
announcement_message: init_tlv_based_struct_field!(announcement_message, required),
announcement_received_time: init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
lowest_inbound_channel_fees_to_one: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_one, option),
lowest_inbound_channel_fees_to_two: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_two, option),
})
}
}
Expand All@@ -835,11 +851,12 @@ pub struct DirectedChannelInfo<'a> {
direction: Option<&'a ChannelUpdateInfo>,
htlc_maximum_msat: u64,
effective_capacity: EffectiveCapacity,
lowest_inbound_channel_fees: Option<RoutingFees>,
}

impl<'a> DirectedChannelInfo<'a> {
#[inline]
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>) -> Self {
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>, lowest_inbound_channel_fees: Option<RoutingFees>) -> Self {
let htlc_maximum_msat = direction.map(|direction| direction.htlc_maximum_msat);
let capacity_msat = channel.capacity_sats.map(|capacity_sats| capacity_sats * 1000);

Expand All@@ -858,7 +875,7 @@ impl<'a> DirectedChannelInfo<'a> {
};

Self {
channel, direction, htlc_maximum_msat, effective_capacity
channel, direction, htlc_maximum_msat, effective_capacity, lowest_inbound_channel_fees,
}
}

Expand All@@ -882,6 +899,13 @@ impl<'a> DirectedChannelInfo<'a> {
self.effective_capacity
}

/// Returns the [`Option<RoutingFees>`] to reach the channel in the direction.
///
/// This is based on the known and enabled channels to the entry node.
pub fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
self.lowest_inbound_channel_fees
}

/// Returns `Some` if [`ChannelUpdateInfo`] is available in the direction.
pub(super) fn with_update(self) -> Option<DirectedChannelInfoWithUpdate<'a>> {
match self.direction {
Expand DownExpand Up@@ -917,6 +941,10 @@ impl<'a> DirectedChannelInfoWithUpdate<'a> {
/// Returns the [`EffectiveCapacity`] of the channel in the direction.
#[inline]
pub(super) fn effective_capacity(&self) -> EffectiveCapacity { self.inner.effective_capacity() }

#[inline]
pub(super) fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> { self.inner.lowest_inbound_channel_fees() }

}

impl<'a> fmt::Debug for DirectedChannelInfoWithUpdate<'a> {
Expand DownExpand Up@@ -1382,6 +1410,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
capacity_sats: None,
announcement_message: None,
announcement_received_time: timestamp,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(short_channel_id, channel_info, None)
Expand All@@ -1408,7 +1438,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
// b) we don't track UTXOs of channels we know about and remove them if they
// get reorg'd out.
// c) it's unclear how to do so without exposing ourselves to massive DoS risk.
Self::remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
*entry.get_mut() = channel_info;
} else {
return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
Expand DownExpand Up@@ -1524,6 +1554,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
{ full_msg.cloned() } else { None },
announcement_received_time,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(msg.short_channel_id, chan_info, utxo_value)
Expand All@@ -1538,7 +1570,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
if is_permanent {
if let Some(chan) = channels.remove(&short_channel_id) {
let mut nodes = self.nodes.write().unwrap();
Self::remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
}
} else {
if let Some(chan) = channels.get_mut(&short_channel_id) {
Expand DownExpand Up@@ -1619,7 +1651,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
let mut nodes = self.nodes.write().unwrap();
for scid in scids_to_remove {
let info = channels.remove(&scid).expect("We just accessed this scid, it should be present");
Self::remove_channel_in_nodes(&mut nodes, &info, scid);
self.remove_channel_in_nodes(&mut nodes, &info, scid);
}
}
}
Expand DownExpand Up@@ -1752,47 +1784,25 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
}

let mut nodes = self.nodes.write().unwrap();
let node = nodes.get_mut(&dest_node_id).unwrap();
if chan_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut base_msat = msg.fee_base_msat;
let mut proportional_millionths = msg.fee_proportional_millionths;
if let Some(fees) = node.lowest_inbound_channel_fees {
base_msat = cmp::min(base_msat, fees.base_msat);
proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
}
node.lowest_inbound_channel_fees = Some(RoutingFees {
base_msat,
proportional_millionths
});
self.update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels, Some(RoutingFees {
base_msat, proportional_millionths
}));
} else if chan_was_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut lowest_inbound_channel_fees = None;

for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == dest_node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = lowest_inbound_channel_fees.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}

node.lowest_inbound_channel_fees = lowest_inbound_channel_fees;
self.recompute_and_update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels);
}

Ok(())
}

fn remove_channel_in_nodes(nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
fn remove_channel_in_nodes(&self, nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
macro_rules! remove_from_node {
($node_id: expr) => {
if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
Expand All@@ -1805,12 +1815,51 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
} else {
panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
}
if let Some(node) = nodes.get_mut(&$node_id) {
self.recompute_and_update_lowest_inbound_channel_fees($node_id, node, &mut self.channels.write().unwrap());
}
}
}

remove_from_node!(chan.node_one);
remove_from_node!(chan.node_two);
}

fn recompute_and_update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>) {
let mut updated_lowest_inbound_channel_fee = None;
for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = updated_lowest_inbound_channel_fee.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}
self.update_lowest_inbound_channel_fees(node_id, node, channels, updated_lowest_inbound_channel_fee);
}

fn update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>, updated_fees: Option<RoutingFees>) {
node.lowest_inbound_channel_fees = updated_fees;
for (_, chan) in channels.iter_mut() {
if chan.node_one == node_id {
chan.lowest_inbound_channel_fees_to_two = updated_fees;
}

if chan.node_two == node_id {
chan.lowest_inbound_channel_fees_to_one = updated_fees;
}
}
}

}

impl ReadOnlyNetworkGraph<'_> {
Expand DownExpand Up@@ -3019,6 +3068,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand All@@ -3037,6 +3088,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand Down
12 changes: 11 additions & 1 deletion lightning/src/routing/router.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -483,6 +483,16 @@ impl<'a> CandidateRouteHop<'a> {
CandidateRouteHop::PrivateHop { .. } => EffectiveCapacity::Infinite,
}
}

fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
match self {
CandidateRouteHop::FirstHop { .. } => Some(RoutingFees {
base_msat: 0, proportional_millionths: 0,
}),
CandidateRouteHop::PublicHop { info, .. } => info.lowest_inbound_channel_fees(),
CandidateRouteHop::PrivateHop { .. } => None,
}
}
}

#[inline]
Expand DownExpand Up@@ -1070,7 +1080,7 @@ where L::Target: Logger {
// as a way to reach the $dest_node_id.
let mut fee_base_msat = 0;
let mut fee_proportional_millionths = 0;
if let Some(Some(fees)) = network_nodes.get(&$src_node_id).map(|node| node.lowest_inbound_channel_fees) {
if let Some(fees) = $candidate.lowest_inbound_channel_fees() {
fee_base_msat = fees.base_msat;
fee_proportional_millionths = fees.proportional_millionths;
}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Closed
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
135 changes: 94 additions & 41 deletions lightning/src/routing/gossip.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -707,37 +707,45 @@ pub struct ChannelInfo {
/// (which we can probably assume we are - no-std environments probably won't have a full
/// network graph in memory!).
announcement_received_time: u64,
/// Lowest fees to enter the first direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_one: Option<RoutingFees>,
/// Lowest fees to enter the second direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_two: Option<RoutingFees>,
}

impl ChannelInfo {
/// Returns a [`DirectedChannelInfo`] for the channel directed to the given `target` from a
/// returned `source`, or `None` if `target` is not one of the channel's counterparties.
pub fn as_directed_to(&self, target: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, source) = {
let (direction, source, lowest_inbound_channel_fees) = {
if target == &self.node_one {
(self.two_to_one.as_ref(), &self.node_two)
(self.two_to_one.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if target == &self.node_two {
(self.one_to_two.as_ref(), &self.node_one)
(self.one_to_two.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), source))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), source))
}

/// Returns a [`DirectedChannelInfo`] for the channel directed from the given `source` to a
/// returned `target`, or `None` if `source` is not one of the channel's counterparties.
pub fn as_directed_from(&self, source: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, target) = {
let (direction, target, lowest_inbound_channel_fees) = {
if source == &self.node_one {
(self.one_to_two.as_ref(), &self.node_two)
(self.one_to_two.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if source == &self.node_two {
(self.two_to_one.as_ref(), &self.node_one)
(self.two_to_one.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), target))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), target))
}

/// Returns a [`ChannelUpdateInfo`] based on the direction implied by the channel_flag.
Expand DownExpand Up@@ -770,6 +778,8 @@ impl Writeable for ChannelInfo {
(8, self.two_to_one, required),
(10, self.capacity_sats, required),
(12, self.announcement_message, required),
(14, self.lowest_inbound_channel_fees_to_one, option),
(16, self.lowest_inbound_channel_fees_to_two, option),
});
Ok(())
}
Expand DownExpand Up@@ -803,6 +813,8 @@ impl Readable for ChannelInfo {
let mut two_to_one_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
init_tlv_field_var!(capacity_sats, required);
init_tlv_field_var!(announcement_message, required);
let mut lowest_inbound_channel_fees_to_one = None;
let mut lowest_inbound_channel_fees_to_two = None;
read_tlv_fields!(reader, {
(0, features, required),
(1, announcement_received_time, (default_value, 0)),
Expand All@@ -812,6 +824,8 @@ impl Readable for ChannelInfo {
(8, two_to_one_wrap, ignorable),
(10, capacity_sats, required),
(12, announcement_message, required),
(14, lowest_inbound_channel_fees_to_one, option),
(16, lowest_inbound_channel_fees_to_two, option),
});

Ok(ChannelInfo {
Expand All@@ -823,6 +837,8 @@ impl Readable for ChannelInfo {
capacity_sats: init_tlv_based_struct_field!(capacity_sats, required),
announcement_message: init_tlv_based_struct_field!(announcement_message, required),
announcement_received_time: init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
lowest_inbound_channel_fees_to_one: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_one, option),
lowest_inbound_channel_fees_to_two: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_two, option),
})
}
}
Expand All@@ -835,11 +851,12 @@ pub struct DirectedChannelInfo<'a> {
direction: Option<&'a ChannelUpdateInfo>,
htlc_maximum_msat: u64,
effective_capacity: EffectiveCapacity,
lowest_inbound_channel_fees: Option<RoutingFees>,
}

impl<'a> DirectedChannelInfo<'a> {
#[inline]
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>) -> Self {
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>, lowest_inbound_channel_fees: Option<RoutingFees>) -> Self {
let htlc_maximum_msat = direction.map(|direction| direction.htlc_maximum_msat);
let capacity_msat = channel.capacity_sats.map(|capacity_sats| capacity_sats * 1000);

Expand All@@ -858,7 +875,7 @@ impl<'a> DirectedChannelInfo<'a> {
};

Self {
channel, direction, htlc_maximum_msat, effective_capacity
channel, direction, htlc_maximum_msat, effective_capacity, lowest_inbound_channel_fees,
}
}

Expand All@@ -882,6 +899,13 @@ impl<'a> DirectedChannelInfo<'a> {
self.effective_capacity
}

/// Returns the [`Option<RoutingFees>`] to reach the channel in the direction.
///
/// This is based on the known and enabled channels to the entry node.
pub fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
self.lowest_inbound_channel_fees
}

/// Returns `Some` if [`ChannelUpdateInfo`] is available in the direction.
pub(super) fn with_update(self) -> Option<DirectedChannelInfoWithUpdate<'a>> {
match self.direction {
Expand DownExpand Up@@ -917,6 +941,10 @@ impl<'a> DirectedChannelInfoWithUpdate<'a> {
/// Returns the [`EffectiveCapacity`] of the channel in the direction.
#[inline]
pub(super) fn effective_capacity(&self) -> EffectiveCapacity { self.inner.effective_capacity() }

#[inline]
pub(super) fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> { self.inner.lowest_inbound_channel_fees() }

}

impl<'a> fmt::Debug for DirectedChannelInfoWithUpdate<'a> {
Expand DownExpand Up@@ -1382,6 +1410,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
capacity_sats: None,
announcement_message: None,
announcement_received_time: timestamp,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(short_channel_id, channel_info, None)
Expand All@@ -1408,7 +1438,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
// b) we don't track UTXOs of channels we know about and remove them if they
// get reorg'd out.
// c) it's unclear how to do so without exposing ourselves to massive DoS risk.
Self::remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
*entry.get_mut() = channel_info;
} else {
return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
Expand DownExpand Up@@ -1524,6 +1554,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
{ full_msg.cloned() } else { None },
announcement_received_time,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(msg.short_channel_id, chan_info, utxo_value)
Expand All@@ -1538,7 +1570,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
if is_permanent {
if let Some(chan) = channels.remove(&short_channel_id) {
let mut nodes = self.nodes.write().unwrap();
Self::remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
}
} else {
if let Some(chan) = channels.get_mut(&short_channel_id) {
Expand DownExpand Up@@ -1619,7 +1651,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
let mut nodes = self.nodes.write().unwrap();
for scid in scids_to_remove {
let info = channels.remove(&scid).expect("We just accessed this scid, it should be present");
Self::remove_channel_in_nodes(&mut nodes, &info, scid);
self.remove_channel_in_nodes(&mut nodes, &info, scid);
}
}
}
Expand DownExpand Up@@ -1752,47 +1784,25 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
}

let mut nodes = self.nodes.write().unwrap();
let node = nodes.get_mut(&dest_node_id).unwrap();
if chan_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut base_msat = msg.fee_base_msat;
let mut proportional_millionths = msg.fee_proportional_millionths;
if let Some(fees) = node.lowest_inbound_channel_fees {
base_msat = cmp::min(base_msat, fees.base_msat);
proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
}
node.lowest_inbound_channel_fees = Some(RoutingFees {
base_msat,
proportional_millionths
});
self.update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels, Some(RoutingFees {
base_msat, proportional_millionths
}));
} else if chan_was_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut lowest_inbound_channel_fees = None;

for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == dest_node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = lowest_inbound_channel_fees.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}

node.lowest_inbound_channel_fees = lowest_inbound_channel_fees;
self.recompute_and_update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels);
}

Ok(())
}

fn remove_channel_in_nodes(nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
fn remove_channel_in_nodes(&self, nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
macro_rules! remove_from_node {
($node_id: expr) => {
if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
Expand All@@ -1805,12 +1815,51 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
} else {
panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
}
if let Some(node) = nodes.get_mut(&$node_id) {
self.recompute_and_update_lowest_inbound_channel_fees($node_id, node, &mut self.channels.write().unwrap());
}
}
}

remove_from_node!(chan.node_one);
remove_from_node!(chan.node_two);
}

fn recompute_and_update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>) {
let mut updated_lowest_inbound_channel_fee = None;
for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = updated_lowest_inbound_channel_fee.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}
self.update_lowest_inbound_channel_fees(node_id, node, channels, updated_lowest_inbound_channel_fee);
}

fn update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>, updated_fees: Option<RoutingFees>) {
node.lowest_inbound_channel_fees = updated_fees;
for (_, chan) in channels.iter_mut() {
if chan.node_one == node_id {
chan.lowest_inbound_channel_fees_to_two = updated_fees;
}

if chan.node_two == node_id {
chan.lowest_inbound_channel_fees_to_one = updated_fees;
}
}
}

}

impl ReadOnlyNetworkGraph<'_> {
Expand DownExpand Up@@ -3019,6 +3068,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand All@@ -3037,6 +3088,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand Down
12 changes: 11 additions & 1 deletion lightning/src/routing/router.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -483,6 +483,16 @@ impl<'a> CandidateRouteHop<'a> {
CandidateRouteHop::PrivateHop { .. } => EffectiveCapacity::Infinite,
}
}

fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
match self {
CandidateRouteHop::FirstHop { .. } => Some(RoutingFees {
base_msat: 0, proportional_millionths: 0,
}),
CandidateRouteHop::PublicHop { info, .. } => info.lowest_inbound_channel_fees(),
CandidateRouteHop::PrivateHop { .. } => None,
}
}
}

#[inline]
Expand DownExpand Up@@ -1070,7 +1080,7 @@ where L::Target: Logger {
// as a way to reach the $dest_node_id.
let mut fee_base_msat = 0;
let mut fee_proportional_millionths = 0;
if let Some(Some(fees)) = network_nodes.get(&$src_node_id).map(|node| node.lowest_inbound_channel_fees) {
if let Some(fees) = $candidate.lowest_inbound_channel_fees() {
fee_base_msat = fees.base_msat;
fee_proportional_millionths = fees.proportional_millionths;
}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
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
135 changes: 94 additions & 41 deletions lightning/src/routing/gossip.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -707,37 +707,45 @@ pub struct ChannelInfo {
/// (which we can probably assume we are - no-std environments probably won't have a full
/// network graph in memory!).
announcement_received_time: u64,
/// Lowest fees to enter the first direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_one: Option<RoutingFees>,
/// Lowest fees to enter the second direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_two: Option<RoutingFees>,
}

impl ChannelInfo {
/// Returns a [`DirectedChannelInfo`] for the channel directed to the given `target` from a
/// returned `source`, or `None` if `target` is not one of the channel's counterparties.
pub fn as_directed_to(&self, target: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, source) = {
let (direction, source, lowest_inbound_channel_fees) = {
if target == &self.node_one {
(self.two_to_one.as_ref(), &self.node_two)
(self.two_to_one.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if target == &self.node_two {
(self.one_to_two.as_ref(), &self.node_one)
(self.one_to_two.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), source))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), source))
}

/// Returns a [`DirectedChannelInfo`] for the channel directed from the given `source` to a
/// returned `target`, or `None` if `source` is not one of the channel's counterparties.
pub fn as_directed_from(&self, source: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, target) = {
let (direction, target, lowest_inbound_channel_fees) = {
if source == &self.node_one {
(self.one_to_two.as_ref(), &self.node_two)
(self.one_to_two.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if source == &self.node_two {
(self.two_to_one.as_ref(), &self.node_one)
(self.two_to_one.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), target))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), target))
}

/// Returns a [`ChannelUpdateInfo`] based on the direction implied by the channel_flag.
Expand DownExpand Up@@ -770,6 +778,8 @@ impl Writeable for ChannelInfo {
(8, self.two_to_one, required),
(10, self.capacity_sats, required),
(12, self.announcement_message, required),
(14, self.lowest_inbound_channel_fees_to_one, option),
(16, self.lowest_inbound_channel_fees_to_two, option),
});
Ok(())
}
Expand DownExpand Up@@ -803,6 +813,8 @@ impl Readable for ChannelInfo {
let mut two_to_one_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
init_tlv_field_var!(capacity_sats, required);
init_tlv_field_var!(announcement_message, required);
let mut lowest_inbound_channel_fees_to_one = None;
let mut lowest_inbound_channel_fees_to_two = None;
read_tlv_fields!(reader, {
(0, features, required),
(1, announcement_received_time, (default_value, 0)),
Expand All@@ -812,6 +824,8 @@ impl Readable for ChannelInfo {
(8, two_to_one_wrap, ignorable),
(10, capacity_sats, required),
(12, announcement_message, required),
(14, lowest_inbound_channel_fees_to_one, option),
(16, lowest_inbound_channel_fees_to_two, option),
});

Ok(ChannelInfo {
Expand All@@ -823,6 +837,8 @@ impl Readable for ChannelInfo {
capacity_sats: init_tlv_based_struct_field!(capacity_sats, required),
announcement_message: init_tlv_based_struct_field!(announcement_message, required),
announcement_received_time: init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
lowest_inbound_channel_fees_to_one: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_one, option),
lowest_inbound_channel_fees_to_two: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_two, option),
})
}
}
Expand All@@ -835,11 +851,12 @@ pub struct DirectedChannelInfo<'a> {
direction: Option<&'a ChannelUpdateInfo>,
htlc_maximum_msat: u64,
effective_capacity: EffectiveCapacity,
lowest_inbound_channel_fees: Option<RoutingFees>,
}

impl<'a> DirectedChannelInfo<'a> {
#[inline]
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>) -> Self {
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>, lowest_inbound_channel_fees: Option<RoutingFees>) -> Self {
let htlc_maximum_msat = direction.map(|direction| direction.htlc_maximum_msat);
let capacity_msat = channel.capacity_sats.map(|capacity_sats| capacity_sats * 1000);

Expand All@@ -858,7 +875,7 @@ impl<'a> DirectedChannelInfo<'a> {
};

Self {
channel, direction, htlc_maximum_msat, effective_capacity
channel, direction, htlc_maximum_msat, effective_capacity, lowest_inbound_channel_fees,
}
}

Expand All@@ -882,6 +899,13 @@ impl<'a> DirectedChannelInfo<'a> {
self.effective_capacity
}

/// Returns the [`Option<RoutingFees>`] to reach the channel in the direction.
///
/// This is based on the known and enabled channels to the entry node.
pub fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
self.lowest_inbound_channel_fees
}

/// Returns `Some` if [`ChannelUpdateInfo`] is available in the direction.
pub(super) fn with_update(self) -> Option<DirectedChannelInfoWithUpdate<'a>> {
match self.direction {
Expand DownExpand Up@@ -917,6 +941,10 @@ impl<'a> DirectedChannelInfoWithUpdate<'a> {
/// Returns the [`EffectiveCapacity`] of the channel in the direction.
#[inline]
pub(super) fn effective_capacity(&self) -> EffectiveCapacity { self.inner.effective_capacity() }

#[inline]
pub(super) fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> { self.inner.lowest_inbound_channel_fees() }

}

impl<'a> fmt::Debug for DirectedChannelInfoWithUpdate<'a> {
Expand DownExpand Up@@ -1382,6 +1410,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
capacity_sats: None,
announcement_message: None,
announcement_received_time: timestamp,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(short_channel_id, channel_info, None)
Expand All@@ -1408,7 +1438,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
// b) we don't track UTXOs of channels we know about and remove them if they
// get reorg'd out.
// c) it's unclear how to do so without exposing ourselves to massive DoS risk.
Self::remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
*entry.get_mut() = channel_info;
} else {
return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
Expand DownExpand Up@@ -1524,6 +1554,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
{ full_msg.cloned() } else { None },
announcement_received_time,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(msg.short_channel_id, chan_info, utxo_value)
Expand All@@ -1538,7 +1570,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
if is_permanent {
if let Some(chan) = channels.remove(&short_channel_id) {
let mut nodes = self.nodes.write().unwrap();
Self::remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
}
} else {
if let Some(chan) = channels.get_mut(&short_channel_id) {
Expand DownExpand Up@@ -1619,7 +1651,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
let mut nodes = self.nodes.write().unwrap();
for scid in scids_to_remove {
let info = channels.remove(&scid).expect("We just accessed this scid, it should be present");
Self::remove_channel_in_nodes(&mut nodes, &info, scid);
self.remove_channel_in_nodes(&mut nodes, &info, scid);
}
}
}
Expand DownExpand Up@@ -1752,47 +1784,25 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
}

let mut nodes = self.nodes.write().unwrap();
let node = nodes.get_mut(&dest_node_id).unwrap();
if chan_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut base_msat = msg.fee_base_msat;
let mut proportional_millionths = msg.fee_proportional_millionths;
if let Some(fees) = node.lowest_inbound_channel_fees {
base_msat = cmp::min(base_msat, fees.base_msat);
proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
}
node.lowest_inbound_channel_fees = Some(RoutingFees {
base_msat,
proportional_millionths
});
self.update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels, Some(RoutingFees {
base_msat, proportional_millionths
}));
} else if chan_was_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut lowest_inbound_channel_fees = None;

for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == dest_node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = lowest_inbound_channel_fees.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}

node.lowest_inbound_channel_fees = lowest_inbound_channel_fees;
self.recompute_and_update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels);
}

Ok(())
}

fn remove_channel_in_nodes(nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
fn remove_channel_in_nodes(&self, nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
macro_rules! remove_from_node {
($node_id: expr) => {
if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
Expand All@@ -1805,12 +1815,51 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
} else {
panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
}
if let Some(node) = nodes.get_mut(&$node_id) {
self.recompute_and_update_lowest_inbound_channel_fees($node_id, node, &mut self.channels.write().unwrap());
}
}
}

remove_from_node!(chan.node_one);
remove_from_node!(chan.node_two);
}

fn recompute_and_update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>) {
let mut updated_lowest_inbound_channel_fee = None;
for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = updated_lowest_inbound_channel_fee.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}
self.update_lowest_inbound_channel_fees(node_id, node, channels, updated_lowest_inbound_channel_fee);
}

fn update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>, updated_fees: Option<RoutingFees>) {
node.lowest_inbound_channel_fees = updated_fees;
for (_, chan) in channels.iter_mut() {
if chan.node_one == node_id {
chan.lowest_inbound_channel_fees_to_two = updated_fees;
}

if chan.node_two == node_id {
chan.lowest_inbound_channel_fees_to_one = updated_fees;
}
}
}

}

impl ReadOnlyNetworkGraph<'_> {
Expand DownExpand Up@@ -3019,6 +3068,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand All@@ -3037,6 +3088,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand Down
12 changes: 11 additions & 1 deletion lightning/src/routing/router.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -483,6 +483,16 @@ impl<'a> CandidateRouteHop<'a> {
CandidateRouteHop::PrivateHop { .. } => EffectiveCapacity::Infinite,
}
}

fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
match self {
CandidateRouteHop::FirstHop { .. } => Some(RoutingFees {
base_msat: 0, proportional_millionths: 0,
}),
CandidateRouteHop::PublicHop { info, .. } => info.lowest_inbound_channel_fees(),
CandidateRouteHop::PrivateHop { .. } => None,
}
}
}

#[inline]
Expand DownExpand Up@@ -1070,7 +1080,7 @@ where L::Target: Logger {
// as a way to reach the $dest_node_id.
let mut fee_base_msat = 0;
let mut fee_proportional_millionths = 0;
if let Some(Some(fees)) = network_nodes.get(&$src_node_id).map(|node| node.lowest_inbound_channel_fees) {
if let Some(fees) = $candidate.lowest_inbound_channel_fees() {
fee_base_msat = fees.base_msat;
fee_proportional_millionths = fees.proportional_millionths;
}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
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
135 changes: 94 additions & 41 deletions lightning/src/routing/gossip.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -707,37 +707,45 @@ pub struct ChannelInfo {
/// (which we can probably assume we are - no-std environments probably won't have a full
/// network graph in memory!).
announcement_received_time: u64,
/// Lowest fees to enter the first direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_one: Option<RoutingFees>,
/// Lowest fees to enter the second direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_two: Option<RoutingFees>,
}

impl ChannelInfo {
/// Returns a [`DirectedChannelInfo`] for the channel directed to the given `target` from a
/// returned `source`, or `None` if `target` is not one of the channel's counterparties.
pub fn as_directed_to(&self, target: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, source) = {
let (direction, source, lowest_inbound_channel_fees) = {
if target == &self.node_one {
(self.two_to_one.as_ref(), &self.node_two)
(self.two_to_one.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if target == &self.node_two {
(self.one_to_two.as_ref(), &self.node_one)
(self.one_to_two.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), source))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), source))
}

/// Returns a [`DirectedChannelInfo`] for the channel directed from the given `source` to a
/// returned `target`, or `None` if `source` is not one of the channel's counterparties.
pub fn as_directed_from(&self, source: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, target) = {
let (direction, target, lowest_inbound_channel_fees) = {
if source == &self.node_one {
(self.one_to_two.as_ref(), &self.node_two)
(self.one_to_two.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if source == &self.node_two {
(self.two_to_one.as_ref(), &self.node_one)
(self.two_to_one.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), target))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), target))
}

/// Returns a [`ChannelUpdateInfo`] based on the direction implied by the channel_flag.
Expand DownExpand Up@@ -770,6 +778,8 @@ impl Writeable for ChannelInfo {
(8, self.two_to_one, required),
(10, self.capacity_sats, required),
(12, self.announcement_message, required),
(14, self.lowest_inbound_channel_fees_to_one, option),
(16, self.lowest_inbound_channel_fees_to_two, option),
});
Ok(())
}
Expand DownExpand Up@@ -803,6 +813,8 @@ impl Readable for ChannelInfo {
let mut two_to_one_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
init_tlv_field_var!(capacity_sats, required);
init_tlv_field_var!(announcement_message, required);
let mut lowest_inbound_channel_fees_to_one = None;
let mut lowest_inbound_channel_fees_to_two = None;
read_tlv_fields!(reader, {
(0, features, required),
(1, announcement_received_time, (default_value, 0)),
Expand All@@ -812,6 +824,8 @@ impl Readable for ChannelInfo {
(8, two_to_one_wrap, ignorable),
(10, capacity_sats, required),
(12, announcement_message, required),
(14, lowest_inbound_channel_fees_to_one, option),
(16, lowest_inbound_channel_fees_to_two, option),
});

Ok(ChannelInfo {
Expand All@@ -823,6 +837,8 @@ impl Readable for ChannelInfo {
capacity_sats: init_tlv_based_struct_field!(capacity_sats, required),
announcement_message: init_tlv_based_struct_field!(announcement_message, required),
announcement_received_time: init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
lowest_inbound_channel_fees_to_one: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_one, option),
lowest_inbound_channel_fees_to_two: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_two, option),
})
}
}
Expand All@@ -835,11 +851,12 @@ pub struct DirectedChannelInfo<'a> {
direction: Option<&'a ChannelUpdateInfo>,
htlc_maximum_msat: u64,
effective_capacity: EffectiveCapacity,
lowest_inbound_channel_fees: Option<RoutingFees>,
}

impl<'a> DirectedChannelInfo<'a> {
#[inline]
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>) -> Self {
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>, lowest_inbound_channel_fees: Option<RoutingFees>) -> Self {
let htlc_maximum_msat = direction.map(|direction| direction.htlc_maximum_msat);
let capacity_msat = channel.capacity_sats.map(|capacity_sats| capacity_sats * 1000);

Expand All@@ -858,7 +875,7 @@ impl<'a> DirectedChannelInfo<'a> {
};

Self {
channel, direction, htlc_maximum_msat, effective_capacity
channel, direction, htlc_maximum_msat, effective_capacity, lowest_inbound_channel_fees,
}
}

Expand All@@ -882,6 +899,13 @@ impl<'a> DirectedChannelInfo<'a> {
self.effective_capacity
}

/// Returns the [`Option<RoutingFees>`] to reach the channel in the direction.
///
/// This is based on the known and enabled channels to the entry node.
pub fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
self.lowest_inbound_channel_fees
}

/// Returns `Some` if [`ChannelUpdateInfo`] is available in the direction.
pub(super) fn with_update(self) -> Option<DirectedChannelInfoWithUpdate<'a>> {
match self.direction {
Expand DownExpand Up@@ -917,6 +941,10 @@ impl<'a> DirectedChannelInfoWithUpdate<'a> {
/// Returns the [`EffectiveCapacity`] of the channel in the direction.
#[inline]
pub(super) fn effective_capacity(&self) -> EffectiveCapacity { self.inner.effective_capacity() }

#[inline]
pub(super) fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> { self.inner.lowest_inbound_channel_fees() }

}

impl<'a> fmt::Debug for DirectedChannelInfoWithUpdate<'a> {
Expand DownExpand Up@@ -1382,6 +1410,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
capacity_sats: None,
announcement_message: None,
announcement_received_time: timestamp,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(short_channel_id, channel_info, None)
Expand All@@ -1408,7 +1438,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
// b) we don't track UTXOs of channels we know about and remove them if they
// get reorg'd out.
// c) it's unclear how to do so without exposing ourselves to massive DoS risk.
Self::remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
*entry.get_mut() = channel_info;
} else {
return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
Expand DownExpand Up@@ -1524,6 +1554,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
{ full_msg.cloned() } else { None },
announcement_received_time,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(msg.short_channel_id, chan_info, utxo_value)
Expand All@@ -1538,7 +1570,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
if is_permanent {
if let Some(chan) = channels.remove(&short_channel_id) {
let mut nodes = self.nodes.write().unwrap();
Self::remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
}
} else {
if let Some(chan) = channels.get_mut(&short_channel_id) {
Expand DownExpand Up@@ -1619,7 +1651,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
let mut nodes = self.nodes.write().unwrap();
for scid in scids_to_remove {
let info = channels.remove(&scid).expect("We just accessed this scid, it should be present");
Self::remove_channel_in_nodes(&mut nodes, &info, scid);
self.remove_channel_in_nodes(&mut nodes, &info, scid);
}
}
}
Expand DownExpand Up@@ -1752,47 +1784,25 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
}

let mut nodes = self.nodes.write().unwrap();
let node = nodes.get_mut(&dest_node_id).unwrap();
if chan_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut base_msat = msg.fee_base_msat;
let mut proportional_millionths = msg.fee_proportional_millionths;
if let Some(fees) = node.lowest_inbound_channel_fees {
base_msat = cmp::min(base_msat, fees.base_msat);
proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
}
node.lowest_inbound_channel_fees = Some(RoutingFees {
base_msat,
proportional_millionths
});
self.update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels, Some(RoutingFees {
base_msat, proportional_millionths
}));
} else if chan_was_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut lowest_inbound_channel_fees = None;

for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == dest_node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = lowest_inbound_channel_fees.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}

node.lowest_inbound_channel_fees = lowest_inbound_channel_fees;
self.recompute_and_update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels);
}

Ok(())
}

fn remove_channel_in_nodes(nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
fn remove_channel_in_nodes(&self, nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
macro_rules! remove_from_node {
($node_id: expr) => {
if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
Expand All@@ -1805,12 +1815,51 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
} else {
panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
}
if let Some(node) = nodes.get_mut(&$node_id) {
self.recompute_and_update_lowest_inbound_channel_fees($node_id, node, &mut self.channels.write().unwrap());
}
}
}

remove_from_node!(chan.node_one);
remove_from_node!(chan.node_two);
}

fn recompute_and_update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>) {
let mut updated_lowest_inbound_channel_fee = None;
for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = updated_lowest_inbound_channel_fee.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}
self.update_lowest_inbound_channel_fees(node_id, node, channels, updated_lowest_inbound_channel_fee);
}

fn update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>, updated_fees: Option<RoutingFees>) {
node.lowest_inbound_channel_fees = updated_fees;
for (_, chan) in channels.iter_mut() {
if chan.node_one == node_id {
chan.lowest_inbound_channel_fees_to_two = updated_fees;
}

if chan.node_two == node_id {
chan.lowest_inbound_channel_fees_to_one = updated_fees;
}
}
}

}

impl ReadOnlyNetworkGraph<'_> {
Expand DownExpand Up@@ -3019,6 +3068,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand All@@ -3037,6 +3088,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand Down
12 changes: 11 additions & 1 deletion lightning/src/routing/router.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -483,6 +483,16 @@ impl<'a> CandidateRouteHop<'a> {
CandidateRouteHop::PrivateHop { .. } => EffectiveCapacity::Infinite,
}
}

fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
match self {
CandidateRouteHop::FirstHop { .. } => Some(RoutingFees {
base_msat: 0, proportional_millionths: 0,
}),
CandidateRouteHop::PublicHop { info, .. } => info.lowest_inbound_channel_fees(),
CandidateRouteHop::PrivateHop { .. } => None,
}
}
}

#[inline]
Expand DownExpand Up@@ -1070,7 +1080,7 @@ where L::Target: Logger {
// as a way to reach the $dest_node_id.
let mut fee_base_msat = 0;
let mut fee_proportional_millionths = 0;
if let Some(Some(fees)) = network_nodes.get(&$src_node_id).map(|node| node.lowest_inbound_channel_fees) {
if let Some(fees) = $candidate.lowest_inbound_channel_fees() {
fee_base_msat = fees.base_msat;
fee_proportional_millionths = fees.proportional_millionths;
}
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Closed
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
135 changes: 94 additions & 41 deletions lightning/src/routing/gossip.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -707,37 +707,45 @@ pub struct ChannelInfo {
/// (which we can probably assume we are - no-std environments probably won't have a full
/// network graph in memory!).
announcement_received_time: u64,
/// Lowest fees to enter the first direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_one: Option<RoutingFees>,
/// Lowest fees to enter the second direction, based on the cheapest channel to the source node.
/// The two fields (flat and proportional fee) are independent,
/// meaning they don't have to refer to the same channel.
pub lowest_inbound_channel_fees_to_two: Option<RoutingFees>,
}

impl ChannelInfo {
/// Returns a [`DirectedChannelInfo`] for the channel directed to the given `target` from a
/// returned `source`, or `None` if `target` is not one of the channel's counterparties.
pub fn as_directed_to(&self, target: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, source) = {
let (direction, source, lowest_inbound_channel_fees) = {
if target == &self.node_one {
(self.two_to_one.as_ref(), &self.node_two)
(self.two_to_one.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if target == &self.node_two {
(self.one_to_two.as_ref(), &self.node_one)
(self.one_to_two.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), source))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), source))
}

/// Returns a [`DirectedChannelInfo`] for the channel directed from the given `source` to a
/// returned `target`, or `None` if `source` is not one of the channel's counterparties.
pub fn as_directed_from(&self, source: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
let (direction, target) = {
let (direction, target, lowest_inbound_channel_fees) = {
if source == &self.node_one {
(self.one_to_two.as_ref(), &self.node_two)
(self.one_to_two.as_ref(), &self.node_two, self.lowest_inbound_channel_fees_to_two)
} else if source == &self.node_two {
(self.two_to_one.as_ref(), &self.node_one)
(self.two_to_one.as_ref(), &self.node_one, self.lowest_inbound_channel_fees_to_one)
} else {
return None;
}
};
Some((DirectedChannelInfo::new(self, direction), target))
Some((DirectedChannelInfo::new(self, direction, lowest_inbound_channel_fees), target))
}

/// Returns a [`ChannelUpdateInfo`] based on the direction implied by the channel_flag.
Expand DownExpand Up@@ -770,6 +778,8 @@ impl Writeable for ChannelInfo {
(8, self.two_to_one, required),
(10, self.capacity_sats, required),
(12, self.announcement_message, required),
(14, self.lowest_inbound_channel_fees_to_one, option),
(16, self.lowest_inbound_channel_fees_to_two, option),
});
Ok(())
}
Expand DownExpand Up@@ -803,6 +813,8 @@ impl Readable for ChannelInfo {
let mut two_to_one_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
init_tlv_field_var!(capacity_sats, required);
init_tlv_field_var!(announcement_message, required);
let mut lowest_inbound_channel_fees_to_one = None;
let mut lowest_inbound_channel_fees_to_two = None;
read_tlv_fields!(reader, {
(0, features, required),
(1, announcement_received_time, (default_value, 0)),
Expand All@@ -812,6 +824,8 @@ impl Readable for ChannelInfo {
(8, two_to_one_wrap, ignorable),
(10, capacity_sats, required),
(12, announcement_message, required),
(14, lowest_inbound_channel_fees_to_one, option),
(16, lowest_inbound_channel_fees_to_two, option),
});

Ok(ChannelInfo {
Expand All@@ -823,6 +837,8 @@ impl Readable for ChannelInfo {
capacity_sats: init_tlv_based_struct_field!(capacity_sats, required),
announcement_message: init_tlv_based_struct_field!(announcement_message, required),
announcement_received_time: init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
lowest_inbound_channel_fees_to_one: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_one, option),
lowest_inbound_channel_fees_to_two: init_tlv_based_struct_field!(lowest_inbound_channel_fees_to_two, option),
})
}
}
Expand All@@ -835,11 +851,12 @@ pub struct DirectedChannelInfo<'a> {
direction: Option<&'a ChannelUpdateInfo>,
htlc_maximum_msat: u64,
effective_capacity: EffectiveCapacity,
lowest_inbound_channel_fees: Option<RoutingFees>,
}

impl<'a> DirectedChannelInfo<'a> {
#[inline]
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>) -> Self {
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>, lowest_inbound_channel_fees: Option<RoutingFees>) -> Self {
let htlc_maximum_msat = direction.map(|direction| direction.htlc_maximum_msat);
let capacity_msat = channel.capacity_sats.map(|capacity_sats| capacity_sats * 1000);

Expand All@@ -858,7 +875,7 @@ impl<'a> DirectedChannelInfo<'a> {
};

Self {
channel, direction, htlc_maximum_msat, effective_capacity
channel, direction, htlc_maximum_msat, effective_capacity, lowest_inbound_channel_fees,
}
}

Expand All@@ -882,6 +899,13 @@ impl<'a> DirectedChannelInfo<'a> {
self.effective_capacity
}

/// Returns the [`Option<RoutingFees>`] to reach the channel in the direction.
///
/// This is based on the known and enabled channels to the entry node.
pub fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
self.lowest_inbound_channel_fees
}

/// Returns `Some` if [`ChannelUpdateInfo`] is available in the direction.
pub(super) fn with_update(self) -> Option<DirectedChannelInfoWithUpdate<'a>> {
match self.direction {
Expand DownExpand Up@@ -917,6 +941,10 @@ impl<'a> DirectedChannelInfoWithUpdate<'a> {
/// Returns the [`EffectiveCapacity`] of the channel in the direction.
#[inline]
pub(super) fn effective_capacity(&self) -> EffectiveCapacity { self.inner.effective_capacity() }

#[inline]
pub(super) fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> { self.inner.lowest_inbound_channel_fees() }

}

impl<'a> fmt::Debug for DirectedChannelInfoWithUpdate<'a> {
Expand DownExpand Up@@ -1382,6 +1410,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
capacity_sats: None,
announcement_message: None,
announcement_received_time: timestamp,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(short_channel_id, channel_info, None)
Expand All@@ -1408,7 +1438,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
// b) we don't track UTXOs of channels we know about and remove them if they
// get reorg'd out.
// c) it's unclear how to do so without exposing ourselves to massive DoS risk.
Self::remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
*entry.get_mut() = channel_info;
} else {
return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
Expand DownExpand Up@@ -1524,6 +1554,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
{ full_msg.cloned() } else { None },
announcement_received_time,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

self.add_channel_between_nodes(msg.short_channel_id, chan_info, utxo_value)
Expand All@@ -1538,7 +1570,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
if is_permanent {
if let Some(chan) = channels.remove(&short_channel_id) {
let mut nodes = self.nodes.write().unwrap();
Self::remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
self.remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
}
} else {
if let Some(chan) = channels.get_mut(&short_channel_id) {
Expand DownExpand Up@@ -1619,7 +1651,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
let mut nodes = self.nodes.write().unwrap();
for scid in scids_to_remove {
let info = channels.remove(&scid).expect("We just accessed this scid, it should be present");
Self::remove_channel_in_nodes(&mut nodes, &info, scid);
self.remove_channel_in_nodes(&mut nodes, &info, scid);
}
}
}
Expand DownExpand Up@@ -1752,47 +1784,25 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
}

let mut nodes = self.nodes.write().unwrap();
let node = nodes.get_mut(&dest_node_id).unwrap();
if chan_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut base_msat = msg.fee_base_msat;
let mut proportional_millionths = msg.fee_proportional_millionths;
if let Some(fees) = node.lowest_inbound_channel_fees {
base_msat = cmp::min(base_msat, fees.base_msat);
proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
}
node.lowest_inbound_channel_fees = Some(RoutingFees {
base_msat,
proportional_millionths
});
self.update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels, Some(RoutingFees {
base_msat, proportional_millionths
}));
} else if chan_was_enabled {
let node = nodes.get_mut(&dest_node_id).unwrap();
let mut lowest_inbound_channel_fees = None;

for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == dest_node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = lowest_inbound_channel_fees.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}

node.lowest_inbound_channel_fees = lowest_inbound_channel_fees;
self.recompute_and_update_lowest_inbound_channel_fees(dest_node_id, node, &mut channels);
}

Ok(())
}

fn remove_channel_in_nodes(nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
fn remove_channel_in_nodes(&self, nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
macro_rules! remove_from_node {
($node_id: expr) => {
if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
Expand All@@ -1805,12 +1815,51 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
} else {
panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
}
if let Some(node) = nodes.get_mut(&$node_id) {
self.recompute_and_update_lowest_inbound_channel_fees($node_id, node, &mut self.channels.write().unwrap());
}
}
}

remove_from_node!(chan.node_one);
remove_from_node!(chan.node_two);
}

fn recompute_and_update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>) {
let mut updated_lowest_inbound_channel_fee = None;
for chan_id in node.channels.iter() {
let chan = channels.get(chan_id).unwrap();
let chan_info_opt;
if chan.node_one == node_id {
chan_info_opt = chan.two_to_one.as_ref();
} else {
chan_info_opt = chan.one_to_two.as_ref();
}
if let Some(chan_info) = chan_info_opt {
if chan_info.enabled {
let fees = updated_lowest_inbound_channel_fee.get_or_insert(RoutingFees {
base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
}
}
}
self.update_lowest_inbound_channel_fees(node_id, node, channels, updated_lowest_inbound_channel_fee);
}

fn update_lowest_inbound_channel_fees(&self, node_id: NodeId, node: &mut NodeInfo, channels: &mut BTreeMap<u64, ChannelInfo>, updated_fees: Option<RoutingFees>) {
node.lowest_inbound_channel_fees = updated_fees;
for (_, chan) in channels.iter_mut() {
if chan.node_one == node_id {
chan.lowest_inbound_channel_fees_to_two = updated_fees;
}

if chan.node_two == node_id {
chan.lowest_inbound_channel_fees_to_one = updated_fees;
}
}
}

}

impl ReadOnlyNetworkGraph<'_> {
Expand DownExpand Up@@ -3019,6 +3068,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand All@@ -3037,6 +3088,8 @@ mod tests {
capacity_sats: None,
announcement_message: None,
announcement_received_time: 87654,
lowest_inbound_channel_fees_to_one: None,
lowest_inbound_channel_fees_to_two: None,
};

let mut encoded_chan_info: Vec<u8> = Vec::new();
Expand Down
12 changes: 11 additions & 1 deletion lightning/src/routing/router.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -483,6 +483,16 @@ impl<'a> CandidateRouteHop<'a> {
CandidateRouteHop::PrivateHop { .. } => EffectiveCapacity::Infinite,
}
}

fn lowest_inbound_channel_fees(&self) -> Option<RoutingFees> {
match self {
CandidateRouteHop::FirstHop { .. } => Some(RoutingFees {
base_msat: 0, proportional_millionths: 0,
}),
CandidateRouteHop::PublicHop { info, .. } => info.lowest_inbound_channel_fees(),
CandidateRouteHop::PrivateHop { .. } => None,
}
}
}

#[inline]
Expand DownExpand Up@@ -1070,7 +1080,7 @@ where L::Target: Logger {
// as a way to reach the $dest_node_id.
let mut fee_base_msat = 0;
let mut fee_proportional_millionths = 0;
if let Some(Some(fees)) = network_nodes.get(&$src_node_id).map(|node| node.lowest_inbound_channel_fees) {
if let Some(fees) = $candidate.lowest_inbound_channel_fees() {
fee_base_msat = fees.base_msat;
fee_proportional_millionths = fees.proportional_millionths;
}
Expand Down