diff --git a/src/fiber/tests/payment.rs b/src/fiber/tests/payment.rs index 1d80f921b..2e0a11361 100644 --- a/src/fiber/tests/payment.rs +++ b/src/fiber/tests/payment.rs @@ -10,6 +10,9 @@ use crate::fiber::NetworkActorCommand; use crate::fiber::NetworkActorMessage; use ractor::call; +// This test will send two payments from node_0 to node_1, the first payment will run +// with dry_run, the second payment will run without dry_run. Both payments will be successful. +// But only one payment balance will be deducted from node_0. #[tokio::test] async fn test_send_payment_for_direct_channel_and_dry_run() { init_tracing(); @@ -17,15 +20,13 @@ async fn test_send_payment_for_direct_channel_and_dry_run() { // from https://github.com/nervosnetwork/fiber/issues/359 let (nodes, channels) = create_n_nodes_with_index_and_amounts_with_established_channel( - &[ - ((0, 1), (MIN_RESERVED_CKB + 10000000000, MIN_RESERVED_CKB)), - ((0, 1), (MIN_RESERVED_CKB, MIN_RESERVED_CKB + 10000000000)), - ], + &[((0, 1), (MIN_RESERVED_CKB + 10000000000, MIN_RESERVED_CKB))], 2, true, ) .await; - let [mut node_0, mut node_1] = nodes.try_into().expect("2 nodes"); + let [mut node_0, node_1] = nodes.try_into().expect("2 nodes"); + let channel = channels[0]; let source_node = &mut node_0; let target_pubkey = node_1.pubkey.clone(); @@ -73,12 +74,38 @@ async fn test_send_payment_for_direct_channel_and_dry_run() { eprintln!("res: {:?}", res); assert!(res.is_ok()); let payment_hash = res.unwrap().payment_hash; - source_node.wait_until_success(payment_hash).await; - let res = node_1 + let node_0_balance = source_node.get_local_balance_from_channel(channel); + let node_1_balance = node_1.get_local_balance_from_channel(channel); + + // A -> B: 10000000000 use the first channel + assert_eq!(node_0_balance, 0); + assert_eq!(node_1_balance, 10000000000); +} + +#[tokio::test] +async fn test_send_payment_prefer_new_channels() { + init_tracing(); + let _span = tracing::info_span!("node", node = "test").entered(); + // from https://github.com/nervosnetwork/fiber/issues/359 + + let (nodes, channels) = create_n_nodes_with_index_and_amounts_with_established_channel( + &[ + ((0, 1), (MIN_RESERVED_CKB + 10000000000, MIN_RESERVED_CKB)), + ((0, 1), (MIN_RESERVED_CKB + 10000000000, MIN_RESERVED_CKB)), + ], + 2, + true, + ) + .await; + let [mut node_0, node_1] = nodes.try_into().expect("2 nodes"); + let source_node = &mut node_0; + let target_pubkey = node_1.pubkey.clone(); + + let res = source_node .send_payment(SendPaymentCommand { - target_pubkey: Some(source_node.pubkey.clone()), + target_pubkey: Some(target_pubkey.clone()), amount: Some(10000000000), payment_hash: None, final_tlc_expiry_delta: None, @@ -97,27 +124,20 @@ async fn test_send_payment_for_direct_channel_and_dry_run() { eprintln!("res: {:?}", res); assert!(res.is_ok()); - - // sleep for a while let payment_hash = res.unwrap().payment_hash; - node_1.wait_until_success(payment_hash).await; - node_1 - .assert_payment_status(payment_hash, PaymentSessionStatus::Success, Some(1)) - .await; + source_node.wait_until_success(payment_hash).await; + // We are using the second (newer) channel, so the first channel's balances are unchanged. let node_0_balance = source_node.get_local_balance_from_channel(channels[0]); let node_1_balance = node_1.get_local_balance_from_channel(channels[0]); + assert_eq!(node_0_balance, 10000000000); + assert_eq!(node_1_balance, 0); - // A -> B: 10000000000 use the first channel - assert_eq!(node_0_balance, 0); - assert_eq!(node_1_balance, 10000000000); - + // We are using the second (newer) channel, so the second channel's balances are changed. let node_0_balance = source_node.get_local_balance_from_channel(channels[1]); let node_1_balance = node_1.get_local_balance_from_channel(channels[1]); - - // B -> A: 10000000000 use the second channel - assert_eq!(node_0_balance, 10000000000); - assert_eq!(node_1_balance, 0); + assert_eq!(node_0_balance, 0); + assert_eq!(node_1_balance, 10000000000); } #[tokio::test]