Skip to content

Commit 65a9d0d

Browse files
committed
Expose some network graph methods
1 parent 3c36910 commit 65a9d0d

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
@@ -73,7 +73,7 @@ macro_rules! secp_verify_sig {
7373

7474
impl RoutingMessageHandler for NetGraphMsgHandler {
7575
fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
76-
self.network_graph.write().unwrap().process_node_announcement(msg, Some(&self.secp_ctx))
76+
self.network_graph.write().unwrap().update_node_from_announcement(msg, Some(&self.secp_ctx))
7777
}
7878

7979
fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
@@ -106,27 +106,27 @@ impl RoutingMessageHandler for NetGraphMsgHandler {
106106
return Err(LightningError{err: "Channel announced without corresponding UTXO entry", action: ErrorAction::IgnoreError});
107107
},
108108
};
109-
let result = self.network_graph.write().unwrap().process_channel_announcement(msg, checked_utxo, Some(&self.secp_ctx));
109+
let result = self.network_graph.write().unwrap().update_channel_from_announcement(msg, checked_utxo, Some(&self.secp_ctx));
110110
log_trace!(self, "Added channel_announcement for {}{}", msg.contents.short_channel_id, if !msg.contents.excess_data.is_empty() { " with excess uninterpreted data!" } else { "" });
111111
result
112112
}
113113

114114
fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
115115
match update {
116116
&msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
117-
let _ = self.network_graph.write().unwrap().process_channel_update(msg, Some(&self.secp_ctx));
117+
let _ = self.network_graph.write().unwrap().update_channel(msg, Some(&self.secp_ctx));
118118
},
119119
&msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
120-
self.network_graph.write().unwrap().process_channel_closing(short_channel_id, &is_permanent);
120+
self.network_graph.write().unwrap().close_channel_from_update(short_channel_id, &is_permanent);
121121
},
122122
&msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
123-
self.network_graph.write().unwrap().process_node_failure(node_id, &is_permanent);
123+
self.network_graph.write().unwrap().fail_node(node_id, &is_permanent);
124124
},
125125
}
126126
}
127127

128128
fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> {
129-
self.network_graph.write().unwrap().process_channel_update(msg, Some(&self.secp_ctx))
129+
self.network_graph.write().unwrap().update_channel(msg, Some(&self.secp_ctx))
130130
}
131131

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

486-
fn process_node_announcement(&mut self, msg: &msgs::NodeAnnouncement, secp_ctx: Option<&Secp256k1<secp256k1::VerifyOnly>>) -> Result<bool, LightningError> {
486+
/// For an already known node (from channel announcements), update its stored properties from a given node announcement
487+
/// Announcement signatures are checked here only if Secp256k1 object is provided.
488+
fn update_node_from_announcement(&mut self, msg: &msgs::NodeAnnouncement, secp_ctx: Option<&Secp256k1<secp256k1::VerifyOnly>>) -> Result<bool, LightningError> {
487489
if let Some(sig_verifier) = secp_ctx {
488490
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
489491
secp_verify_sig!(sig_verifier, &msg_hash, &msg.signature, &msg.contents.node_id);
@@ -512,7 +514,11 @@ impl NetworkGraph {
512514
}
513515
}
514516

515-
fn process_channel_announcement(&mut self, msg: &msgs::ChannelAnnouncement, checked_utxo: bool, secp_ctx: Option<&Secp256k1<secp256k1::VerifyOnly>>) -> Result<bool, LightningError> {
517+
/// For a new or already known (from previous announcement) channel, store or update channel info,
518+
/// after making sure it corresponds to a real transaction on-chain.
519+
/// Also store nodes (if not stored yet) the channel is between, and make node aware of this channel.
520+
/// Announcement signatures are checked here only if Secp256k1 object is provided.
521+
fn update_channel_from_announcement(&mut self, msg: &msgs::ChannelAnnouncement, checked_utxo: bool, secp_ctx: Option<&Secp256k1<secp256k1::VerifyOnly>>) -> Result<bool, LightningError> {
516522
if let Some(sig_verifier) = secp_ctx {
517523
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
518524
secp_verify_sig!(sig_verifier, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
@@ -605,7 +611,11 @@ impl NetworkGraph {
605611
Ok(should_relay)
606612
}
607613

608-
fn process_channel_closing(&mut self, short_channel_id: &u64, is_permanent: &bool) {
614+
/// Close a channel if a corresponding HTLC fail was sent.
615+
/// If permanent, removes a channel from the local storage.
616+
/// May cause the removal of nodes too, if this was their last channel.
617+
/// If not permanent, makes channels unavailable for routing.
618+
pub fn close_channel_from_update(&mut self, short_channel_id: &u64, is_permanent: &bool) {
609619
if *is_permanent {
610620
if let Some(chan) = self.channels.remove(short_channel_id) {
611621
Self::remove_channel_in_nodes(&mut self.nodes, &chan, *short_channel_id);
@@ -618,15 +628,17 @@ impl NetworkGraph {
618628
}
619629
}
620630

621-
fn process_node_failure(&mut self, _node_id: &PublicKey, is_permanent: &bool) {
631+
fn fail_node(&mut self, _node_id: &PublicKey, is_permanent: &bool) {
622632
if *is_permanent {
623633
// TODO: Wholly remove the node
624634
} else {
625635
// TODO: downgrade the node
626636
}
627637
}
628638

629-
fn process_channel_update(&mut self, msg: &msgs::ChannelUpdate, secp_ctx: Option<&Secp256k1<secp256k1::VerifyOnly>>) -> Result<bool, LightningError> {
639+
/// For an already known (from announcement) channel, update info regarding one of the directions of a channel.
640+
/// Announcement signatures are checked here only if Secp256k1 object is provided.
641+
fn update_channel(&mut self, msg: &msgs::ChannelUpdate, secp_ctx: Option<&Secp256k1<secp256k1::VerifyOnly>>) -> Result<bool, LightningError> {
630642
let dest_node_id;
631643
let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
632644
let chan_was_enabled;

0 commit comments

Comments
 (0)