Skip to content

Commit

Permalink
add test case for shutdown force
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Feb 19, 2025
1 parent fff5391 commit 5b3a1af
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/fiber/tests/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2189,3 +2189,47 @@ async fn test_send_payment_complex_network_payself_amount_exceeded() {
.count();
assert!(succ_count > 0);
}

#[tokio::test]
async fn test_send_payment_shutdown_with_force() {
init_tracing();
let _span = tracing::info_span!("node", node = "test").entered();
let (nodes, channels) = create_n_nodes_and_channels_with_index_amounts(
&[
((0, 1), (HUGE_CKB_AMOUNT, HUGE_CKB_AMOUNT)),
((1, 2), (HUGE_CKB_AMOUNT, HUGE_CKB_AMOUNT)),
((2, 3), (HUGE_CKB_AMOUNT, HUGE_CKB_AMOUNT)),
],
4,
true,
)
.await;

let mut all_sent = HashSet::new();
for i in 0..10 {
let res = nodes[0].send_payment_keysend(&nodes[3], 1000, false).await;
eprintln!("res: {:?}", res);
if let Ok(send_payment_res) = res {
all_sent.insert(send_payment_res.payment_hash);
}

if i == 5 {
let _ = nodes[3].send_shutdown(channels[2], true).await;
}
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
nodes[3]
.send_channel_shutdown_tx_confirmed_event(nodes[2].peer_id.clone(), channels[2], true)
.await;
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
}

for payment_hash in all_sent.clone().iter() {
let payment_status = nodes[0].get_payment_result(*payment_hash).await;
eprintln!(
"payment_hash: {:?} status: {:?}",
payment_hash, payment_status.status
);
}

tokio::time::sleep(tokio::time::Duration::from_millis(1000 * 100)).await;
}
43 changes: 43 additions & 0 deletions src/fiber/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use crate::invoice::CkbInvoice;
use crate::invoice::CkbInvoiceStatus;
use crate::invoice::InvoiceStore;
use ckb_jsonrpc_types::Status;
use ckb_sdk::core::TransactionBuilder;
use ckb_types::packed::OutPoint;
use ckb_types::packed::Script;
use ckb_types::{core::TransactionView, packed::Byte32};
use ractor::{call, Actor, ActorRef};
use rand::rngs::OsRng;
Expand Down Expand Up @@ -573,6 +575,47 @@ impl NetworkNode {
res
}

pub async fn send_shutdown(
&self,
channel_id: Hash256,
force: bool,
) -> std::result::Result<(), String> {
use crate::fiber::channel::ShutdownCommand;
use ckb_types::core::FeeRate;
let message = |rpc_reply| -> NetworkActorMessage {
NetworkActorMessage::Command(NetworkActorCommand::ControlFiberChannel(
ChannelCommandWithId {
channel_id,
command: ChannelCommand::Shutdown(
ShutdownCommand {
close_script: Script::default(),
fee_rate: FeeRate::from_u64(1000000000),
force,
},
rpc_reply,
),
},
))
};

call!(self.network_actor, message).expect("source_node alive")
}

pub async fn send_channel_shutdown_tx_confirmed_event(
&self,
peer_id: PeerId,
channel_id: Hash256,
force: bool,
) {
use crate::fiber::NetworkActorEvent::ClosingTransactionConfirmed;

let tx_hash = TransactionBuilder::default().build().hash();
let event = ClosingTransactionConfirmed(peer_id, channel_id, tx_hash, force);
self.network_actor
.send_message(NetworkActorMessage::Event(event))
.expect("network actor alive");
}

pub async fn send_payment_keysend(
&self,
recipient: &NetworkNode,
Expand Down

0 comments on commit 5b3a1af

Please sign in to comment.