Skip to content

Commit

Permalink
Add unit tests for hop hints
Browse files Browse the repository at this point in the history
  • Loading branch information
contrun committed Jan 16, 2025
1 parent 966e126 commit 102b66b
Showing 1 changed file with 236 additions and 0 deletions.
236 changes: 236 additions & 0 deletions src/fiber/tests/payment.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use std::collections::HashSet;

use super::test_utils::init_tracing;
use crate::fiber::config::DEFAULT_TLC_EXPIRY_DELTA;
use crate::fiber::config::DEFAULT_TLC_FEE_PROPORTIONAL_MILLIONTHS;
use crate::fiber::graph::PaymentSessionStatus;
use crate::fiber::network::HopHint;
use crate::fiber::network::SendPaymentCommand;
use crate::fiber::tests::test_utils::*;
use crate::fiber::types::Hash256;
use crate::fiber::NetworkActorCommand;
use crate::fiber::NetworkActorMessage;
use ckb_types::packed::OutPoint;
use ractor::call;

// This test will send two payments from node_0 to node_1, the first payment will run
Expand Down Expand Up @@ -453,6 +456,239 @@ async fn test_send_payment_with_more_capacity_for_payself() {
assert_eq!(node1_fee + node2_fee, res.fee);
}

#[tokio::test]
async fn test_send_payment_with_private_channel_hints() {
async fn test(amount_to_send: u128, is_payment_ok: bool) {
let (nodes, _channels) = create_n_nodes_with_index_and_amounts_with_established_channel(
&[((0, 1), (MIN_RESERVED_CKB + 40000000000, MIN_RESERVED_CKB))],
3,
true,
)
.await;
let [mut node1, mut node2, mut node3] = nodes.try_into().expect("3 nodes");

let (_new_channel_id, funding_tx) = establish_channel_between_nodes(
&mut node2,
&mut node3,
false,
MIN_RESERVED_CKB + 20000000000,
MIN_RESERVED_CKB,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
)
.await;

let outpoint = funding_tx.output_pts_iter().next().unwrap();
// sleep for a while
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;

let source_node = &mut node1;
let target_pubkey = node3.pubkey.clone();

let res = source_node
.send_payment(SendPaymentCommand {
target_pubkey: Some(target_pubkey.clone()),
amount: Some(amount_to_send),
payment_hash: None,
final_tlc_expiry_delta: None,
tlc_expiry_limit: None,
invoice: None,
timeout: None,
max_fee_amount: None,
max_parts: None,
keysend: Some(true),
udt_type_script: None,
allow_self_payment: false,
hop_hints: Some(vec![HopHint {
pubkey: node2.pubkey.clone(),
channel_outpoint: outpoint,
fee_rate: DEFAULT_TLC_FEE_PROPORTIONAL_MILLIONTHS as u64,
tlc_expiry_delta: DEFAULT_TLC_EXPIRY_DELTA,
}]),
dry_run: false,
})
.await;

assert!(res.is_ok(), "Send payment failed: {:?}", res);
let res = res.unwrap();
let payment_hash = res.payment_hash;
if is_payment_ok {
source_node.wait_until_success(payment_hash).await;
} else {
source_node.wait_until_failed(payment_hash).await;
}
}

test(10000000000, true).await;
test(30000000000, false).await;
}

#[tokio::test]
async fn test_send_payment_with_private_channel_hints_fallback() {
let (nodes, _channels) = create_n_nodes_with_index_and_amounts_with_established_channel(
&[
((0, 1), (MIN_RESERVED_CKB + 40000000000, MIN_RESERVED_CKB)),
((1, 2), (MIN_RESERVED_CKB + 40000000000, MIN_RESERVED_CKB)),
],
3,
true,
)
.await;
let [mut node1, mut node2, mut node3] = nodes.try_into().expect("3 nodes");

let (_new_channel_id, funding_tx) = establish_channel_between_nodes(
&mut node2,
&mut node3,
false,
MIN_RESERVED_CKB + 20000000000,
MIN_RESERVED_CKB,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
)
.await;

let outpoint = funding_tx.output_pts_iter().next().unwrap();
// sleep for a while
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;

let source_node = &mut node1;
let target_pubkey = node3.pubkey.clone();

let res = source_node
.send_payment(SendPaymentCommand {
target_pubkey: Some(target_pubkey.clone()),
amount: Some(30000000000),
payment_hash: None,
final_tlc_expiry_delta: None,
tlc_expiry_limit: None,
invoice: None,
timeout: None,
max_fee_amount: None,
max_parts: None,
keysend: Some(true),
udt_type_script: None,
allow_self_payment: false,
hop_hints: Some(vec![HopHint {
pubkey: node2.pubkey.clone(),
channel_outpoint: outpoint,
fee_rate: DEFAULT_TLC_FEE_PROPORTIONAL_MILLIONTHS as u64,
tlc_expiry_delta: DEFAULT_TLC_EXPIRY_DELTA,
}]),
dry_run: false,
})
.await;

assert!(res.is_ok(), "Send payment failed: {:?}", res);
let res = res.unwrap();
let payment_hash = res.payment_hash;
source_node.wait_until_success(payment_hash).await;
source_node
.assert_payment_status(payment_hash, PaymentSessionStatus::Success, Some(2))
.await;
}

#[tokio::test]
async fn test_send_payment_with_private_multiple_channel_hints_fallback() {
let (nodes, _channels) = create_n_nodes_with_index_and_amounts_with_established_channel(
&[
((0, 1), (MIN_RESERVED_CKB + 40000000000, MIN_RESERVED_CKB)),
((1, 2), (MIN_RESERVED_CKB + 40000000000, MIN_RESERVED_CKB)),
],
3,
true,
)
.await;
let [mut node1, mut node2, mut node3] = nodes.try_into().expect("3 nodes");

async fn create_channel(
node2: &mut NetworkNode,
node3: &mut NetworkNode,
amount: u128,
) -> OutPoint {
let (_new_channel_id, funding_tx) = establish_channel_between_nodes(
node2,
node3,
false,
MIN_RESERVED_CKB + amount,
MIN_RESERVED_CKB,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
)
.await;
funding_tx.output_pts_iter().next().unwrap()
}

let outpoint1 = create_channel(&mut node2, &mut node3, 20000000000).await;
let outpoint2 = create_channel(&mut node2, &mut node3, 40000000000).await;

// sleep for a while
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;

let source_node = &mut node1;
let target_pubkey = node3.pubkey.clone();

let res = source_node
.send_payment(SendPaymentCommand {
target_pubkey: Some(target_pubkey.clone()),
amount: Some(30000000000),
payment_hash: None,
final_tlc_expiry_delta: None,
tlc_expiry_limit: None,
invoice: None,
timeout: None,
max_fee_amount: None,
max_parts: None,
keysend: Some(true),
udt_type_script: None,
allow_self_payment: false,
hop_hints: Some(vec![
HopHint {
pubkey: node2.pubkey.clone(),
channel_outpoint: outpoint1,
fee_rate: DEFAULT_TLC_FEE_PROPORTIONAL_MILLIONTHS as u64,
tlc_expiry_delta: DEFAULT_TLC_EXPIRY_DELTA,
},
HopHint {
pubkey: node2.pubkey.clone(),
channel_outpoint: outpoint2,
fee_rate: DEFAULT_TLC_FEE_PROPORTIONAL_MILLIONTHS as u64,
tlc_expiry_delta: DEFAULT_TLC_EXPIRY_DELTA,
},
]),
dry_run: false,
})
.await;

assert!(res.is_ok(), "Send payment failed: {:?}", res);
let res = res.unwrap();
let payment_hash = res.payment_hash;
source_node.wait_until_success(payment_hash).await;
}

// #[tokio::test]
// async fn test_send_payment_with_route_to_self_with_hop_hints() {
// init_tracing();
Expand Down

0 comments on commit 102b66b

Please sign in to comment.