Skip to content

Commit 4c79f1e

Browse files
committed
Expose some network graph methods
1 parent 4df746c commit 4c79f1e

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl RoutingMessageHandler for NetGraphMsgHandler {
7575
fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
7676
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
7777
secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
78-
self.network_graph.write().unwrap().process_node_announcement(msg)
78+
self.network_graph.write().unwrap().update_node_from_announcement(msg)
7979
}
8080

8181
fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
@@ -114,27 +114,27 @@ impl RoutingMessageHandler for NetGraphMsgHandler {
114114
return Err(LightningError{err: "Channel announced without corresponding UTXO entry", action: ErrorAction::IgnoreError});
115115
},
116116
};
117-
let result = self.network_graph.write().unwrap().process_channel_announcement(msg, checked_utxo);
117+
let result = self.network_graph.write().unwrap().update_channel_from_announcement(msg, checked_utxo);
118118
log_trace!(self, "Added channel_announcement for {}{}", msg.contents.short_channel_id, if !msg.contents.excess_data.is_empty() { " with excess uninterpreted data!" } else { "" });
119119
result
120120
}
121121

122122
fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
123123
match update {
124124
&msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
125-
let _ = self.network_graph.write().unwrap().process_channel_update(msg, &self.secp_ctx);
125+
let _ = self.network_graph.write().unwrap().update_channel(msg, &self.secp_ctx);
126126
},
127127
&msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
128-
self.network_graph.write().unwrap().process_channel_closing(short_channel_id, &is_permanent);
128+
self.network_graph.write().unwrap().close_channel_from_update(short_channel_id, &is_permanent);
129129
},
130130
&msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
131-
self.network_graph.write().unwrap().process_node_failure(node_id, &is_permanent);
131+
self.network_graph.write().unwrap().fail_node(node_id, &is_permanent);
132132
},
133133
}
134134
}
135135

136136
fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> {
137-
self.network_graph.write().unwrap().process_channel_update(msg, &self.secp_ctx)
137+
self.network_graph.write().unwrap().update_channel(msg, &self.secp_ctx)
138138
}
139139

140140
fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, Option<msgs::ChannelUpdate>, Option<msgs::ChannelUpdate>)> {
@@ -491,7 +491,9 @@ impl NetworkGraph {
491491
/// Returns all known nodes
492492
pub fn get_nodes<'a>(&'a self) -> &'a BTreeMap<PublicKey, NodeInfo> { &self.nodes }
493493

494-
fn process_node_announcement(&mut self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
494+
/// For an already known node (from channel announcements), update its stored properties from a given node announcement
495+
/// Announcement signatures *are not checked* here and should be checked by a caller beforehand.
496+
pub fn update_node_from_announcement(&mut self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
495497
match self.nodes.get_mut(&msg.contents.node_id) {
496498
None => Err(LightningError{err: "No existing channels for node_announcement", action: ErrorAction::IgnoreError}),
497499
Some(node) => {
@@ -515,7 +517,11 @@ impl NetworkGraph {
515517
}
516518
}
517519

518-
fn process_channel_announcement(&mut self, msg: &msgs::ChannelAnnouncement, checked_utxo: bool) -> Result<bool, LightningError> {
520+
/// For a new or already known (from previous announcement) channel, store or update channel info,
521+
/// after making sure it corresponds to a real transaction on-chain.
522+
/// Also store nodes (if not stored yet) the channel is between, and make node aware of this channel.
523+
/// Announcement signatures *are not checked* here and should be checked by a caller beforehand.
524+
pub fn update_channel_from_announcement(&mut self, msg: &msgs::ChannelAnnouncement, checked_utxo: bool) -> Result<bool, LightningError> {
519525
let should_relay = msg.contents.excess_data.is_empty();
520526

521527
let chan_info = ChannelInfo {
@@ -600,7 +606,11 @@ impl NetworkGraph {
600606
Ok(should_relay)
601607
}
602608

603-
fn process_channel_closing(&mut self, short_channel_id: &u64, is_permanent: &bool) {
609+
/// Close a channel if a corresponding HTLC fail was sent.
610+
/// If permanent, removes a channel from the local storage.
611+
/// May cause the removal of nodes too, if this was their last channel.
612+
/// If not permanent, makes channels unavailable for routing.
613+
pub fn close_channel_from_update(&mut self, short_channel_id: &u64, is_permanent: &bool) {
604614
if *is_permanent {
605615
if let Some(chan) = self.channels.remove(short_channel_id) {
606616
Self::remove_channel_in_nodes(&mut self.nodes, &chan, *short_channel_id);
@@ -613,15 +623,17 @@ impl NetworkGraph {
613623
}
614624
}
615625

616-
fn process_node_failure(&mut self, _node_id: &PublicKey, is_permanent: &bool) {
626+
fn fail_node(&mut self, _node_id: &PublicKey, is_permanent: &bool) {
617627
if *is_permanent {
618628
// TODO: Wholly remove the node
619629
} else {
620630
// TODO: downgrade the node
621631
}
622632
}
623633

624-
fn process_channel_update(&mut self, msg: &msgs::ChannelUpdate, secp_ctx: &Secp256k1<secp256k1::VerifyOnly>) -> Result<bool, LightningError> {
634+
/// For an already known (from announcement) channel, update info regarding one of the directions of a channel.
635+
/// Announcement signatures *are checked* here and should be checked by a caller beforehand.
636+
fn update_channel(&mut self, msg: &msgs::ChannelUpdate, secp_ctx: &Secp256k1<secp256k1::VerifyOnly>) -> Result<bool, LightningError> {
625637
let dest_node_id;
626638
let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
627639
let chan_was_enabled;

0 commit comments

Comments
 (0)