From 639e7db832f79376111a4fcf072f6a4bae48e827 Mon Sep 17 00:00:00 2001 From: quake Date: Mon, 25 Nov 2024 18:49:11 +0900 Subject: [PATCH] fix: resolve shutdown script different size bug --- src/fiber/channel.rs | 23 ++++++------ src/fiber/tests/channel.rs | 71 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 13 deletions(-) diff --git a/src/fiber/channel.rs b/src/fiber/channel.rs index ff93f181..d4b5cbb8 100644 --- a/src/fiber/channel.rs +++ b/src/fiber/channel.rs @@ -4301,6 +4301,14 @@ impl ChannelActorState { .local_shutdown_info .as_mut() .expect("local shudown info exists"); + let remote_shutdown_info = self + .remote_shutdown_info + .as_ref() + .expect("remote shudown info exists"); + let shutdown_scripts = ( + local_shutdown_info.close_script.clone(), + remote_shutdown_info.close_script.clone(), + ); let local_shutdown_signature = match local_shutdown_info.signature { Some(signature) => signature, None => { @@ -4322,12 +4330,7 @@ impl ChannelActorState { } }; - if let Some(remote_shutdown_signature) = self - .remote_shutdown_info - .as_ref() - .expect("remote shutdown info exists") - .signature - { + if let Some(remote_shutdown_signature) = remote_shutdown_info.signature { let tx: TransactionView = self .aggregate_partial_signatures_to_consume_funding_cell( [local_shutdown_signature, remote_shutdown_signature], @@ -4335,13 +4338,7 @@ impl ChannelActorState { )?; assert_eq!( tx.data().serialized_size_in_block(), - shutdown_tx_size( - &self.funding_udt_type_script, - ( - self.get_local_shutdown_script(), - self.get_remote_shutdown_script() - ) - ) + shutdown_tx_size(&self.funding_udt_type_script, shutdown_scripts) ); self.update_state(ChannelState::Closed(CloseFlags::COOPERATIVE)); diff --git a/src/fiber/tests/channel.rs b/src/fiber/tests/channel.rs index 6024239f..bb794028 100644 --- a/src/fiber/tests/channel.rs +++ b/src/fiber/tests/channel.rs @@ -2109,3 +2109,74 @@ async fn test_shutdown_channel_with_large_size_shutdown_script_should_fail() { .unwrap() .contains("Local balance is not enough to pay the fee")); } + +#[tokio::test] +async fn test_shutdown_channel_with_different_size_shutdown_script() { + let node_a_funding_amount = 100000000000; + let node_b_funding_amount = 6200000000; + + 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; + + let message = |rpc_reply| { + NetworkActorMessage::Command(NetworkActorCommand::ControlFiberChannel( + ChannelCommandWithId { + channel_id: new_channel_id, + command: ChannelCommand::Shutdown( + ShutdownCommand { + close_script: Script::new_builder().args(vec![0u8; 19].pack()).build(), + fee_rate: FeeRate::from_u64(DEFAULT_COMMITMENT_FEE_RATE), + force: false, + }, + rpc_reply, + ), + }, + )) + }; + + call!(node_b.network_actor, message) + .expect("node_b alive") + .expect("successfully shutdown channel"); + + let node_a_shutdown_tx_hash = node_a + .expect_to_process_event(|event| match event { + NetworkServiceEvent::ChannelClosed(peer_id, channel_id, tx_hash) => { + println!( + "Shutdown tx ({:?}) from {:?} for channel {:?} received", + &tx_hash, &peer_id, channel_id + ); + assert_eq!(peer_id, &node_b.peer_id); + assert_eq!(channel_id, &new_channel_id); + Some(tx_hash.clone()) + } + _ => None, + }) + .await; + + let node_b_shutdown_tx_hash = node_b + .expect_to_process_event(|event| match event { + NetworkServiceEvent::ChannelClosed(peer_id, channel_id, tx_hash) => { + println!( + "Shutdown tx ({:?}) from {:?} for channel {:?} received", + &tx_hash, &peer_id, channel_id + ); + assert_eq!(peer_id, &node_a.peer_id); + assert_eq!(channel_id, &new_channel_id); + Some(tx_hash.clone()) + } + _ => None, + }) + .await; + + assert_eq!(node_a_shutdown_tx_hash, node_b_shutdown_tx_hash); + + assert_eq!( + node_a.trace_tx_hash(node_a_shutdown_tx_hash.clone()).await, + Status::Committed + ); + assert_eq!( + node_b.trace_tx_hash(node_b_shutdown_tx_hash.clone()).await, + Status::Committed + ); +}