Skip to content

Commit

Permalink
Update networkgraph ChannelUpdateInfo for owned channels
Browse files Browse the repository at this point in the history
  • Loading branch information
contrun committed Jan 10, 2025
1 parent 305bdee commit 794077d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 43 deletions.
62 changes: 60 additions & 2 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use tentacle::secio::PeerId;
use thiserror::Error;
use tokio::sync::oneshot;

use super::types::ForwardTlcResult;
use super::{graph::ChannelUpdateInfo, types::ForwardTlcResult};
use std::{
collections::HashSet,
fmt::{self, Debug},
Expand Down Expand Up @@ -479,7 +479,22 @@ where
}
FiberChannelMessage::UpdateTlcInfo(update_tlc_info) => {
state.remote_tlc_info = Some(update_tlc_info.into());

let channel_outpoint = state.must_get_funding_transaction_outpoint();
let peer_id = state.get_remote_pubkey();
let channel_update_info = state
.get_remote_channel_update_info()
.expect("remote tlc info set above; qed");
self.network
.send_message(NetworkActorMessage::new_event(
NetworkActorEvent::OwnedChannelUpdateEvent(
super::graph::OwnedChannelUpdateEvent::Updated(
channel_outpoint,
peer_id,
channel_update_info,
),
),
))
.expect(ASSUME_NETWORK_ACTOR_ALIVE);
Ok(())
}
FiberChannelMessage::AddTlc(add_tlc) => {
Expand Down Expand Up @@ -3673,9 +3688,28 @@ impl ChannelActorState {
))
.expect(ASSUME_NETWORK_ACTOR_ALIVE);
}
self.update_graph_for_channel_change(network);
self.send_update_tlc_info_message(network);
}

fn update_graph_for_channel_change(&mut self, network: &ActorRef<NetworkActorMessage>) {
// Also update network graph with latest local channel update info.
let channel_outpoint = self.must_get_funding_transaction_outpoint();
let peer_id = self.get_local_pubkey();
let channel_update_info = self.get_local_channel_update_info();
network
.send_message(NetworkActorMessage::new_event(
NetworkActorEvent::OwnedChannelUpdateEvent(
super::graph::OwnedChannelUpdateEvent::Updated(
channel_outpoint,
peer_id,
channel_update_info,
),
),
))
.expect(ASSUME_NETWORK_ACTOR_ALIVE);
}

fn send_update_tlc_info_message(&mut self, network: &ActorRef<NetworkActorMessage>) {
let update_tlc_info = self.create_update_tlc_info_message();
network
Expand Down Expand Up @@ -4352,14 +4386,38 @@ impl ChannelActorState {
self.id
}

pub fn get_local_pubkey(&self) -> Pubkey {
self.local_pubkey
}

pub fn get_local_peer_id(&self) -> PeerId {
self.local_pubkey.tentacle_peer_id()
}

pub fn get_local_channel_update_info(&self) -> ChannelUpdateInfo {
let balance = self.get_remote_balance();
let mut info = ChannelUpdateInfo::from(&self.local_tlc_info);
info.receivable_balance = Some(balance);
info
}

pub fn get_remote_pubkey(&self) -> Pubkey {
self.remote_pubkey
}

pub fn get_remote_peer_id(&self) -> PeerId {
self.remote_pubkey.tentacle_peer_id()
}

pub fn get_remote_channel_update_info(&self) -> Option<ChannelUpdateInfo> {
let balance = self.get_local_balance();
self.remote_tlc_info.as_ref().map(|tlc_info| {
let mut info = ChannelUpdateInfo::from(tlc_info);
info.receivable_balance = Some(balance);
info
})
}

pub fn get_local_secnonce(&self) -> SecNonce {
self.signer
.derive_musig2_nonce(self.get_local_commitment_number())
Expand Down
46 changes: 5 additions & 41 deletions src/fiber/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,8 @@ pub enum OwnedChannelUpdateEvent {
/// The channel is down and should not be used for routing payments.
/// This normally means the peer is not reachable.
Down(OutPoint),
/// One direction of the channel is disabled for forwarding payments.
Disabled(OutPoint, Pubkey),
/// One direction of the channel is enabled for forwarding payments.
Enabled(OutPoint, Pubkey),
/// The balance of one direction has been updated (we can forward this amount to the other party).
BalanceUpdate(OutPoint, Pubkey, u128),
/// One direction of the channel is updated (e.g. new balance, new fee rate).
Updated(OutPoint, Pubkey, ChannelUpdateInfo),
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -431,45 +427,13 @@ where
OwnedChannelUpdateEvent::Down(channel_outpoint) => {
self.channels.remove(&channel_outpoint);
}
OwnedChannelUpdateEvent::Disabled(channel_outpoint, node) => {
OwnedChannelUpdateEvent::Updated(channel_outpoint, node, channel_update) => {
if let Some(channel) = self.channels.get_mut(&channel_outpoint) {
if node == channel.node2() {
if let Some(info) = channel.update_of_node2.as_mut() {
info.enabled = false;
}
}
if node == channel.node1() {
if let Some(info) = channel.update_of_node1.as_mut() {
info.enabled = false;
}
}
}
}
OwnedChannelUpdateEvent::Enabled(channel_outpoint, node) => {
if let Some(channel) = self.channels.get_mut(&channel_outpoint) {
if node == channel.node2() {
if let Some(info) = channel.update_of_node2.as_mut() {
info.enabled = true;
}
channel.update_of_node2 = Some(channel_update);
}
if node == channel.node1() {
if let Some(info) = channel.update_of_node1.as_mut() {
info.enabled = true;
}
}
}
}
OwnedChannelUpdateEvent::BalanceUpdate(channel_outpoint, node, balance) => {
if let Some(channel) = self.channels.get_mut(&channel_outpoint) {
if node == channel.node2() {
if let Some(info) = channel.update_of_node2.as_mut() {
info.receivable_balance = Some(balance);
}
}
if node == channel.node1() {
if let Some(info) = channel.update_of_node1.as_mut() {
info.receivable_balance = Some(balance);
}
channel.update_of_node1 = Some(channel_update);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/fiber/tests/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ async fn do_test_owned_channel_saved_to_the_owner_graph(public: bool) {
HashSet::from([node1_channel.node1_peerid(), node1_channel.node2_peerid()]),
HashSet::from([node1_id.clone(), node2_id.clone()])
);
assert_ne!(node1_channel.update_of_node1, None);
assert_ne!(node1_channel.update_of_node2, None);
let node1_nodes = node1.get_network_graph_nodes().await;
assert_eq!(node1_nodes.len(), 2);
for node in node1_nodes {
Expand All @@ -255,6 +257,8 @@ async fn do_test_owned_channel_saved_to_the_owner_graph(public: bool) {
let node2_channels = node2.get_network_graph_channels().await;
assert_eq!(node2_channels.len(), 1);
let node2_channel = &node2_channels[0];
assert_ne!(node2_channel.update_of_node1, None);
assert_ne!(node2_channel.update_of_node2, None);
assert_eq!(
HashSet::from([node2_channel.node1_peerid(), node2_channel.node2_peerid()]),
HashSet::from([node1_id, node2_id])
Expand Down

0 comments on commit 794077d

Please sign in to comment.