Skip to content

Commit

Permalink
Add dedicated test to check path finding prefers newer channel
Browse files Browse the repository at this point in the history
  • Loading branch information
contrun committed Jan 8, 2025
1 parent fb4691e commit 09f197a
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions src/fiber/tests/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ 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();
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, 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();

Expand Down Expand Up @@ -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,
Expand All @@ -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]
Expand Down

0 comments on commit 09f197a

Please sign in to comment.