Skip to content

Commit 8501947

Browse files
author
Antoine Riard
committed
Implement local channel tracking by Router at channel_update
Add test_channel_update_local_channel Issue #149
1 parent 2258d2b commit 8501947

File tree

5 files changed

+102
-2
lines changed

5 files changed

+102
-2
lines changed

src/ln/channelmanager.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,6 +2722,22 @@ impl ChannelMessageHandler for ChannelManager {
27222722
handle_error!(self, self.internal_channel_reestablish(their_node_id, msg), their_node_id)
27232723
}
27242724

2725+
fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Option<ChannelDetails> {
2726+
let channel_state = self.channel_state.lock().unwrap();
2727+
if let Some(channel_id) = channel_state.short_to_id.get(&msg.contents.short_channel_id) {
2728+
if let Some(channel) = channel_state.by_id.get(channel_id) {
2729+
return Some(ChannelDetails {
2730+
channel_id: channel_id.clone(),
2731+
short_channel_id: channel.get_short_channel_id(),
2732+
remote_network_id: channel.get_their_node_id(),
2733+
channel_value_satoshis: channel.get_value_satoshis(),
2734+
user_id: channel.get_user_id(),
2735+
});
2736+
}
2737+
}
2738+
None
2739+
}
2740+
27252741
fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool) {
27262742
let _ = self.total_consistency_lock.read().unwrap();
27272743
let mut failed_channels = Vec::new();
@@ -7532,4 +7548,17 @@ mod tests {
75327548
assert_eq!(msg.channel_id, channel_id);
75337549
} else { panic!("Unexpected result"); }
75347550
}
7551+
7552+
#[test]
7553+
fn test_channel_update_local_channel() {
7554+
let nodes = create_network(2);
7555+
7556+
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
7557+
7558+
if let Some(_) = nodes[0].node.handle_channel_update(&chan_1.1) {
7559+
nodes[0].router.handle_local_channel(&chan_1.1, nodes[0].node.get_our_node_id().clone(), nodes[1].node.get_our_node_id().clone(), msgs::GlobalFeatures::new())
7560+
}
7561+
nodes[0].router.handle_channel_update(&chan_1.1).unwrap();
7562+
7563+
}
75357564
}

src/ln/msgs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use secp256k1;
2121
use bitcoin::util::hash::Sha256dHash;
2222
use bitcoin::blockdata::script::Script;
2323

24+
use ln::channelmanager::ChannelDetails;
25+
2426
use std::error::Error;
2527
use std::{cmp, fmt};
2628
use std::io::Read;
@@ -559,6 +561,8 @@ pub trait ChannelMessageHandler : events::MessageSendEventsProvider + Send + Syn
559561
// Channel-to-announce:
560562
/// Handle an incoming announcement_signatures message from the given peer.
561563
fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures) -> Result<(), HandleError>;
564+
/// Handle an incoming channel_update message, return true if it's an update on local channels
565+
fn handle_channel_update(&self, msg: &ChannelUpdate) -> Option<ChannelDetails>;
562566

563567
// Connection loss/reestablish:
564568
/// Indicates a connection to the peer failed/an existing connection was lost. If no connection
@@ -598,6 +602,8 @@ pub trait RoutingMessageHandler : Send + Sync {
598602
/// starting at the node *after* the provided publickey and including batch_amount entries.
599603
/// If None is provided for starting_point, we start at the first node.
600604
fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement>;
605+
/// Add local channel into Router maps
606+
fn handle_local_channel(&self, msg: &ChannelUpdate, our_node_id: PublicKey, their_node_id: PublicKey, features: GlobalFeatures);
601607
}
602608

603609
pub(crate) struct OnionRealm0HopData {

src/ln/peer_handler.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//! they should handle, and encoding/sending response messages.
88
99
use secp256k1::key::{SecretKey,PublicKey};
10+
use secp256k1::{Secp256k1};
1011

1112
use ln::msgs;
1213
use util::ser::{Writeable, Writer, Readable};
@@ -744,8 +745,11 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
744745
},
745746
258 => {
746747
let msg = try_potential_decodeerror!(msgs::ChannelUpdate::read(&mut reader));
748+
if let Some(chan) = self.message_handler.chan_handler.handle_channel_update(&msg) {
749+
let our_node_id = PublicKey::from_secret_key(&Secp256k1::new(), &self.our_node_secret);
750+
self.message_handler.route_handler.handle_local_channel(&msg, our_node_id, chan.remote_network_id, msgs::GlobalFeatures::new());
751+
}
747752
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_update(&msg));
748-
749753
if should_forward {
750754
// TODO: forward msg along to all our other peers!
751755
}

src/ln/router.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,6 @@ impl RoutingMessageHandler for Router {
492492
Ok(msg.contents.excess_data.is_empty())
493493
}
494494

495-
496495
fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)> {
497496
let mut result = Vec::with_capacity(batch_amount as usize);
498497
let network = self.network_map.read().unwrap();
@@ -537,6 +536,63 @@ impl RoutingMessageHandler for Router {
537536
}
538537
result
539538
}
539+
540+
fn handle_local_channel(&self, msg: &msgs::ChannelUpdate, our_node_id: PublicKey, their_node_id: PublicKey, features: GlobalFeatures) {
541+
let were_node_one = our_node_id.serialize()[..] < their_node_id.serialize()[..];
542+
let chan_info = ChannelInfo {
543+
features: features,
544+
one_to_two: DirectionalChannelInfo {
545+
src_node_id: if were_node_one { our_node_id } else { their_node_id },
546+
last_update: 0,
547+
enabled: false,
548+
cltv_expiry_delta: u16::max_value(),
549+
htlc_minimum_msat: u64::max_value(),
550+
fee_base_msat: u32::max_value(),
551+
fee_proportional_millionths: u32::max_value(),
552+
last_update_message: None,
553+
},
554+
two_to_one: DirectionalChannelInfo {
555+
src_node_id: if were_node_one { their_node_id } else { our_node_id },
556+
last_update: 0,
557+
enabled: false,
558+
cltv_expiry_delta: u16::max_value(),
559+
htlc_minimum_msat: u64::max_value(),
560+
fee_base_msat: u32::max_value(),
561+
fee_proportional_millionths: u32::max_value(),
562+
last_update_message: None,
563+
},
564+
announcement_message: None,
565+
};
566+
567+
let mut network = self.network_map.write().unwrap();
568+
match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
569+
BtreeEntry::Occupied(mut entry) => {
570+
*entry.get_mut() = chan_info;
571+
},
572+
BtreeEntry::Vacant(entry) => {
573+
entry.insert(chan_info);
574+
}
575+
}
576+
577+
match network.nodes.entry(their_node_id) {
578+
BtreeEntry::Occupied(node_entry) => {
579+
node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
580+
},
581+
BtreeEntry::Vacant(node_entry) => {
582+
node_entry.insert(NodeInfo {
583+
channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
584+
lowest_inbound_channel_fee_base_msat: u32::max_value(),
585+
lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
586+
features: GlobalFeatures::new(),
587+
last_update: 0,
588+
rgb: [0; 3],
589+
alias: [0; 32],
590+
addresses: Vec::new(),
591+
announcement_message: None,
592+
});
593+
}
594+
}
595+
}
540596
}
541597

542598
#[derive(Eq, PartialEq)]

src/util/test_utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use chain::chaininterface;
22
use chain::chaininterface::ConfirmationTarget;
33
use chain::transaction::OutPoint;
4+
use ln::channelmanager::ChannelDetails;
45
use ln::channelmonitor;
56
use ln::msgs;
67
use ln::msgs::{HandleError};
@@ -136,6 +137,9 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
136137
fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelReestablish) -> Result<(), HandleError> {
137138
Err(HandleError { err: "", action: None })
138139
}
140+
fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Option<ChannelDetails> {
141+
None
142+
}
139143
fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
140144
fn peer_connected(&self, _their_node_id: &PublicKey) {}
141145
fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
@@ -174,6 +178,7 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
174178
fn get_next_node_announcements(&self, _starting_point: Option<&PublicKey>, _batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
175179
Vec::new()
176180
}
181+
fn handle_local_channel(&self, _msg: &msgs::ChannelUpdate, _our_node_id: PublicKey, _their_node_id: PublicKey, _features: msgs::GlobalFeatures) {}
177182
}
178183

179184
pub struct TestLogger {

0 commit comments

Comments
 (0)