Skip to content

Commit 5dbf16e

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 84953fc commit 5dbf16e

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

src/ln/channelmanager.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,14 @@ impl ChannelMessageHandler for ChannelManager {
22492249
handle_error!(self, self.internal_channel_reestablish(their_node_id, msg), their_node_id)
22502250
}
22512251

2252+
fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> bool {
2253+
let channel_state = self.channel_state.lock().unwrap();
2254+
if channel_state.short_to_id.get(&msg.contents.short_channel_id).is_some() {
2255+
return true
2256+
}
2257+
false
2258+
}
2259+
22522260
fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool) {
22532261
let mut new_events = Vec::new();
22542262
let mut failed_channels = Vec::new();
@@ -4587,4 +4595,17 @@ mod tests {
45874595
sign_msg!(unsigned_msg);
45884596
assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err());
45894597
}
4598+
4599+
#[test]
4600+
fn test_channel_update_local_channel() {
4601+
let nodes = create_network(2);
4602+
4603+
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4604+
4605+
if nodes[0].node.handle_channel_update(&chan_1.1) {
4606+
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())
4607+
}
4608+
nodes[0].router.handle_channel_update(&chan_1.1).unwrap();
4609+
4610+
}
45904611
}

src/ln/msgs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,8 @@ pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync {
530530
// Channel-to-announce:
531531
/// Handle an incoming announcement_signatures message from the given peer.
532532
fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures) -> Result<(), HandleError>;
533+
/// Handle an incoming channel_update message, return true if it's an update on local channels
534+
fn handle_channel_update(&self, msg: &ChannelUpdate) -> bool;
533535

534536
// Connection loss/reestablish:
535537
/// Indicates a connection to the peer failed/an existing connection was lost. If no connection
@@ -561,6 +563,8 @@ pub trait RoutingMessageHandler : Send + Sync {
561563
fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<bool, HandleError>;
562564
/// Handle some updates to the route graph that we learned due to an outbound failed payment.
563565
fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate);
566+
/// Add local channel into Router maps
567+
fn handle_local_channel(&self, msg: &ChannelUpdate, our_node_id: PublicKey, their_node_id: PublicKey, features: GlobalFeatures);
564568
}
565569

566570
pub(crate) struct OnionRealm0HopData {

src/ln/peer_handler.rs

Lines changed: 10 additions & 0 deletions
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};
@@ -711,6 +712,15 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
711712
},
712713
258 => {
713714
let msg = try_potential_decodeerror!(msgs::ChannelUpdate::read(&mut reader));
715+
if self.message_handler.chan_handler.handle_channel_update(&msg) {
716+
let peers_state = self.peers.lock().unwrap();
717+
if let Some(peer) = peers_state.peers.get(peer_descriptor) {
718+
if let Some(ref their_node_id) = peer.their_node_id {
719+
let our_node_id = PublicKey::from_secret_key(&Secp256k1::new(), &self.our_node_secret);
720+
self.message_handler.route_handler.handle_local_channel(&msg, our_node_id, their_node_id.clone(), if peer.their_global_features.is_some() { peer.their_global_features.clone().unwrap() } else { msgs::GlobalFeatures::new() });
721+
}
722+
}
723+
}
714724
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_update(&msg));
715725

716726
if should_forward {

src/ln/router.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,59 @@ impl RoutingMessageHandler for Router {
426426

427427
Ok(msg.contents.excess_data.is_empty())
428428
}
429+
430+
fn handle_local_channel(&self, msg: &msgs::ChannelUpdate, our_node_id: PublicKey, their_node_id: PublicKey, features: GlobalFeatures) {
431+
let were_node_one = our_node_id.serialize()[..] < their_node_id.serialize()[..];
432+
let chan_info = ChannelInfo {
433+
features: features,
434+
one_to_two: DirectionalChannelInfo {
435+
src_node_id: if were_node_one { our_node_id } else { their_node_id },
436+
last_update: 0,
437+
enabled: false,
438+
cltv_expiry_delta: u16::max_value(),
439+
htlc_minimum_msat: u64::max_value(),
440+
fee_base_msat: u32::max_value(),
441+
fee_proportional_millionths: u32::max_value(),
442+
},
443+
two_to_one: DirectionalChannelInfo {
444+
src_node_id: if were_node_one { their_node_id } else { our_node_id },
445+
last_update: 0,
446+
enabled: false,
447+
cltv_expiry_delta: u16::max_value(),
448+
htlc_minimum_msat: u64::max_value(),
449+
fee_base_msat: u32::max_value(),
450+
fee_proportional_millionths: u32::max_value(),
451+
},
452+
};
453+
454+
let mut network = self.network_map.write().unwrap();
455+
match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
456+
Entry::Occupied(mut entry) => {
457+
*entry.get_mut() = chan_info;
458+
},
459+
Entry::Vacant(entry) => {
460+
entry.insert(chan_info);
461+
}
462+
}
463+
464+
match network.nodes.entry(their_node_id) {
465+
Entry::Occupied(node_entry) => {
466+
node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
467+
},
468+
Entry::Vacant(node_entry) => {
469+
node_entry.insert(NodeInfo {
470+
channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
471+
lowest_inbound_channel_fee_base_msat: u32::max_value(),
472+
lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
473+
features: GlobalFeatures::new(),
474+
last_update: 0,
475+
rgb: [0; 3],
476+
alias: [0; 32],
477+
addresses: Vec::new(),
478+
});
479+
}
480+
}
481+
}
429482
}
430483

431484
#[derive(Eq, PartialEq)]

src/util/test_utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
131131
fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>), HandleError> {
132132
Err(HandleError { err: "", action: None })
133133
}
134+
fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> bool {
135+
false
136+
}
134137
fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
135138
fn peer_connected(&self, _their_node_id: &PublicKey) -> Vec<msgs::ChannelReestablish> {
136139
Vec::new()
@@ -166,6 +169,8 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
166169
Err(HandleError { err: "", action: None })
167170
}
168171
fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
172+
173+
fn handle_local_channel(&self, _msg: &msgs::ChannelUpdate, _our_node_id: PublicKey, _their_node_id: PublicKey, _features: msgs::GlobalFeatures) {}
169174
}
170175

171176
pub struct TestLogger {

0 commit comments

Comments
 (0)