Skip to content

Commit

Permalink
add more assertion for channel balance
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Dec 5, 2024
1 parent 415954e commit dcc8b12
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions src/fiber/tests/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,20 +1216,26 @@ async fn test_network_send_payment_with_dry_run() {
async fn test_send_payment_with_3_nodes() {
init_tracing();
let _span = tracing::info_span!("node", node = "test").entered();
let (node_a, _node_b, node_c, _, _) = create_3_nodes_with_established_channel(
let (node_a, node_b, node_c, channel_1, channel_2) = create_3_nodes_with_established_channel(
(100000000000, 100000000000),
(100000000000, 100000000000),
true,
)
.await;
let node_a_local = node_a.get_local_balance_from_channel(channel_1);
let node_b_local_left = node_b.get_local_balance_from_channel(channel_1);
let node_b_local_right = node_b.get_local_balance_from_channel(channel_2);
let node_c_local = node_c.get_local_balance_from_channel(channel_2);

// sleep for 2 seconds to make sure the channel is established
tokio::time::sleep(tokio::time::Duration::from_millis(5000)).await;
let sent_amount = 1000000 + 5;
let node_c_pubkey = node_c.pubkey.clone();
let message = |rpc_reply| -> NetworkActorMessage {
NetworkActorMessage::Command(NetworkActorCommand::SendPayment(
SendPaymentCommand {
target_pubkey: Some(node_c_pubkey),
amount: Some(1000000 + 5),
amount: Some(sent_amount),
payment_hash: None,
final_tlc_expiry_delta: None,
invoice: None,
Expand Down Expand Up @@ -1260,19 +1266,39 @@ async fn test_send_payment_with_3_nodes() {
.unwrap();
assert_eq!(res.status, PaymentSessionStatus::Success);
assert_eq!(res.failed_error, None);

let new_node_a_local = node_a.get_local_balance_from_channel(channel_1);
let new_node_b_left = node_b.get_local_balance_from_channel(channel_1);
let new_node_b_right = node_b.get_local_balance_from_channel(channel_2);
let new_node_c_local = node_c.get_local_balance_from_channel(channel_2);

let node_a_sent = node_a_local - new_node_a_local;
assert_eq!(node_a_sent, sent_amount + res.fee);
let node_b_sent = node_b_local_right - new_node_b_right;
let node_b_received = new_node_b_left - node_b_local_left;
let node_b_got = node_b_received - node_b_sent;
assert_eq!(node_b_got, res.fee);
let node_c_got = new_node_c_local - node_c_local;
assert_eq!(node_c_got, sent_amount);
}

#[tokio::test]
async fn test_send_payment_fail_with_3_nodes_invalid_hash() {
init_tracing();
let _span = tracing::info_span!("node", node = "test").entered();

let (node_a, _node_b, node_c, _, _) = create_3_nodes_with_established_channel(
let (node_a, node_b, node_c, channel_1, channel_2) = create_3_nodes_with_established_channel(
(100000000000, 100000000000),
(100000000000, 100000000000),
true,
)
.await;

let node_a_local = node_a.get_local_balance_from_channel(channel_1);
let node_b_local_left = node_b.get_local_balance_from_channel(channel_1);
let node_b_local_right = node_b.get_local_balance_from_channel(channel_2);
let node_c_local = node_c.get_local_balance_from_channel(channel_2);

// sleep for 2 seconds to make sure the channel is established
tokio::time::sleep(tokio::time::Duration::from_millis(3000)).await;
let node_c_pubkey = node_c.pubkey.clone();
Expand Down Expand Up @@ -1314,6 +1340,20 @@ async fn test_send_payment_fail_with_3_nodes_invalid_hash() {
res.failed_error,
Some("IncorrectOrUnknownPaymentDetails".to_string())
);

let new_node_a_local = node_a.get_local_balance_from_channel(channel_1);
let new_node_b_left = node_b.get_local_balance_from_channel(channel_1);
let new_node_b_right = node_b.get_local_balance_from_channel(channel_2);
let new_node_c_local = node_c.get_local_balance_from_channel(channel_2);

let node_a_sent = node_a_local - new_node_a_local;
assert_eq!(node_a_sent, 0);
let node_b_sent = node_b_local_right - new_node_b_right;
let node_b_received = new_node_b_left - node_b_local_left;
let node_b_got = node_b_received - node_b_sent;
assert_eq!(node_b_got, 0);
let node_c_got = new_node_c_local - node_c_local;
assert_eq!(node_c_got, 0);
}

#[tokio::test]
Expand Down

0 comments on commit dcc8b12

Please sign in to comment.