From 4867c1c2245d35f7850ba6bf68be8ecb1105f580 Mon Sep 17 00:00:00 2001 From: yukang Date: Thu, 2 Jan 2025 12:57:20 +0800 Subject: [PATCH] Avoid send broadcast message when shutdown private channel --- src/fiber/channel.rs | 21 ++++++++++++--------- src/fiber/tests/channel.rs | 10 ++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/fiber/channel.rs b/src/fiber/channel.rs index 33e28e5ce..43e1b75f6 100644 --- a/src/fiber/channel.rs +++ b/src/fiber/channel.rs @@ -1760,16 +1760,18 @@ where } ChannelEvent::ClosingTransactionConfirmed => { // Broadcast the channel update message which disables the channel. - let update = state.generate_disabled_channel_update(&self.network).await; - - self.network - .send_message(NetworkActorMessage::new_command( - NetworkActorCommand::BroadcastMessages(vec![ - BroadcastMessage::ChannelUpdate(update), - ]), - )) - .expect(ASSUME_NETWORK_ACTOR_ALIVE); + if state.is_public() { + let update = state.generate_disabled_channel_update(&self.network).await; + self.network + .send_message(NetworkActorMessage::new_command( + NetworkActorCommand::BroadcastMessages(vec![ + BroadcastMessage::ChannelUpdate(update), + ]), + )) + .expect(ASSUME_NETWORK_ACTOR_ALIVE); + } + debug_event!(self.network, "ChannelClosed"); myself.stop(Some("ChannelClosed".to_string())); } } @@ -3407,6 +3409,7 @@ impl ChannelActorState { // The function that would change the channel update parameters. f: impl FnOnce(&mut ChannelUpdate), ) -> ChannelUpdate { + assert!(self.is_public()); let mut channel_update = self .get_unsigned_channel_update_message() .expect("public channel can generate channel update message"); diff --git a/src/fiber/tests/channel.rs b/src/fiber/tests/channel.rs index a6c53e3f6..c908f3812 100644 --- a/src/fiber/tests/channel.rs +++ b/src/fiber/tests/channel.rs @@ -4293,6 +4293,8 @@ async fn test_shutdown_channel_with_different_size_shutdown_script() { let node_a_funding_amount = 100000000000; let node_b_funding_amount = 6200000000; + // create a private channel for testing shutdown, + // https://github.com/nervosnetwork/fiber/issues/431 let (mut node_a, mut node_b, new_channel_id) = create_nodes_with_established_channel(node_a_funding_amount, node_b_funding_amount, false) .await; @@ -4347,6 +4349,14 @@ async fn test_shutdown_channel_with_different_size_shutdown_script() { }) .await; + node_a + .expect_event(|event| matches!(event, NetworkServiceEvent::DebugEvent(DebugEvent::Common(message)) if message == "ChannelClosed")) + .await; + + node_b + .expect_event(|event| matches!(event, NetworkServiceEvent::DebugEvent(DebugEvent::Common(message)) if message == "ChannelClosed")) + .await; + assert_eq!(node_a_shutdown_tx_hash, node_b_shutdown_tx_hash); assert_eq!(