Skip to content

Commit

Permalink
Merge pull request #340 from quake/quake/fix-shutdown-script-differen…
Browse files Browse the repository at this point in the history
…t-size

fix: resolve shutdown script different size bug
  • Loading branch information
chenyukang authored Nov 25, 2024
2 parents 6b0a5a8 + 639e7db commit 8d22175
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -4322,26 +4330,15 @@ 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],
&shutdown_tx,
)?;
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));
Expand Down
71 changes: 71 additions & 0 deletions src/fiber/tests/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

0 comments on commit 8d22175

Please sign in to comment.