Skip to content

Commit

Permalink
Notify world about channel updates
Browse files Browse the repository at this point in the history
  • Loading branch information
contrun committed Jan 10, 2025
1 parent dce8858 commit dfb2683
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
30 changes: 23 additions & 7 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ where
if state.reestablishing {
match message {
FiberChannelMessage::ReestablishChannel(ref reestablish_channel) => {
state.handle_reestablish_channel_message(reestablish_channel, &self.network)?;
state
.handle_reestablish_channel_message(reestablish_channel, &self.network)
.await?;
}
_ => {
debug!("Ignoring message while reestablishing: {:?}", message);
Expand Down Expand Up @@ -576,7 +578,9 @@ where
Ok(())
}
FiberChannelMessage::ReestablishChannel(ref reestablish_channel) => {
state.handle_reestablish_channel_message(reestablish_channel, &self.network)?;
state
.handle_reestablish_channel_message(reestablish_channel, &self.network)
.await?;
Ok(())
}
FiberChannelMessage::TxAbort(_)
Expand Down Expand Up @@ -1411,7 +1415,7 @@ where
}

if updated {
state.on_channel_tlc_info_updated(&self.network).await;
state.notify_owned_channel_updated(&self.network).await;
}

Ok(())
Expand Down Expand Up @@ -3684,7 +3688,9 @@ impl ChannelActorState {
.await
}

async fn on_channel_tlc_info_updated(&mut self, network: &ActorRef<NetworkActorMessage>) {
// Notify the network, network graph and channel counterparty about the channel update.
// We do this on channel ready, channel reestablishment, user channel parameters update.
async fn notify_owned_channel_updated(&mut self, network: &ActorRef<NetworkActorMessage>) {
if self.is_public() {
let channel_update = self.generate_channel_update(network).await;
network
Expand All @@ -3695,7 +3701,15 @@ impl ChannelActorState {
))
.expect(ASSUME_NETWORK_ACTOR_ALIVE);
}
self.update_graph_for_local_channel_change(network);
if let Ok(channel_info) = (&*self).try_into() {
network
.send_message(NetworkActorMessage::new_event(
NetworkActorEvent::OwnedChannelUpdateEvent(
super::graph::OwnedChannelUpdateEvent::Up(channel_info),
),
))
.expect(ASSUME_NETWORK_ACTOR_ALIVE);
}
self.send_update_tlc_info_message(network);
}

Expand Down Expand Up @@ -5760,7 +5774,7 @@ impl ChannelActorState {
self.increment_local_commitment_number();
self.increment_remote_commitment_number();
let peer_id = self.get_remote_peer_id();
self.send_update_tlc_info_message(network);
self.notify_owned_channel_updated(network).await;
if let Ok(channel_info) = (&*self).try_into() {
network
.send_message(NetworkActorMessage::new_event(
Expand Down Expand Up @@ -5960,7 +5974,7 @@ impl ChannelActorState {
Ok(need_commitment_signed)
}

fn handle_reestablish_channel_message(
async fn handle_reestablish_channel_message(
&mut self,
reestablish_channel: &ReestablishChannel,
network: &ActorRef<NetworkActorMessage>,
Expand Down Expand Up @@ -6090,6 +6104,8 @@ impl ChannelActorState {
);
}

self.notify_owned_channel_updated(network).await;

debug_event!(network, "Reestablished channel in ChannelReady");
}
_ => {
Expand Down
23 changes: 14 additions & 9 deletions src/fiber/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,24 +497,23 @@ where
// But tests in src/fiber/tests/graph.rs need to process gossip messages
// to update the network graph. Many of the tests are messages from the graph.source.
// If we ignore these messages, the graph won't be updated. And many tests will fail.
fn should_process_gossip_message_for_channel(&self, channel_outpoint: &OutPoint) -> bool {
fn should_process_gossip_message_for_nodes(&self, node1: &Pubkey, node2: &Pubkey) -> bool {
#[cfg(test)]
if self.always_process_gossip_message {
return true;
}
match self.channels.get(channel_outpoint) {
Some(channel) => !(self.source == channel.node1() || self.source == channel.node2()),
// Channel does not exist yet, process the message.
None => true,
}
!(&self.source == node1 || &self.source == node2)
}

fn process_channel_announcement(
&mut self,
timestamp: u64,
channel_announcement: ChannelAnnouncement,
) -> Option<Cursor> {
if !self.should_process_gossip_message_for_channel(&channel_announcement.channel_outpoint) {
if !self.should_process_gossip_message_for_nodes(
&channel_announcement.node1_id,
&channel_announcement.node2_id,
) {
return None;
}

Expand Down Expand Up @@ -558,8 +557,14 @@ where
}

fn process_channel_update(&mut self, channel_update: ChannelUpdate) -> Option<Cursor> {
if !self.should_process_gossip_message_for_channel(&channel_update.channel_outpoint) {
return None;
match self.get_channel(&channel_update.channel_outpoint) {
Some(channel)
if !self
.should_process_gossip_message_for_nodes(&channel.node1, &channel.node2) =>
{
return None;
}
_ => {}
}
let channel = self.load_channel_info_mut(&channel_update.channel_outpoint)?;
let update_info = if channel_update.is_update_of_node_1() {
Expand Down

0 comments on commit dfb2683

Please sign in to comment.