Skip to content

Commit

Permalink
add more unit test for send tlc each other
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Dec 31, 2024
1 parent ed388f1 commit a869c72
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2323,7 +2323,10 @@ impl Debug for TlcInfo {

impl TlcInfo {
pub fn log(&self) -> String {
format!("id: {:?} status: {:?}", &self.tlc_id, self.status)
format!(
"id: {:?} status: {:?} amount: {:?}",
&self.tlc_id, self.status, self.amount
)
}

pub fn is_offered(&self) -> bool {
Expand Down Expand Up @@ -2439,6 +2442,7 @@ pub struct TlcState {
}

impl TlcState {
#[cfg(debug_assertions)]
pub fn debug(&self) {
for tlc in self.offered_tlcs.tlcs.iter() {
eprintln!("offered_tlc: {:?}", tlc.log());
Expand Down Expand Up @@ -5487,7 +5491,6 @@ impl ChannelActorState {
network: &ActorRef<NetworkActorMessage>,
revoke_and_ack: RevokeAndAck,
) -> Result<bool, ProcessingChannelError> {
self.tlc_state.debug();
let RevokeAndAck {
channel_id: _,
revocation_partial_signature,
Expand Down
184 changes: 184 additions & 0 deletions src/fiber/tests/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,190 @@ async fn test_network_send_payment_send_each_other() {
assert_eq!(node_b_new_balance, node_b_old_balance + 1);
}

#[tokio::test]
async fn test_network_send_payment_more_send_each_other() {
init_tracing();

// node_a -> node_b add_tlc 10000
// node_b -> node_a add_tlc 9999
// node_a -> node_b add_tlc 9999
// node_b -> node_a add_tlc 10000
//
// all the add_tlc are added at the same time
// and the final balance should be same as the initial balance

let _span = tracing::info_span!("node", node = "test").entered();
let node_a_funding_amount = 100000000000;
let node_b_funding_amount = 6200000000;

let (node_a, node_b, new_channel_id) =
create_nodes_with_established_channel(node_a_funding_amount, node_b_funding_amount, true)
.await;
// Wait for the channel announcement to be broadcasted
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
let node_a_old_balance = node_a.get_local_balance_from_channel(new_channel_id);
let node_b_old_balance = node_b.get_local_balance_from_channel(new_channel_id);

let node_a_pubkey = node_a.pubkey.clone();
let node_b_pubkey = node_b.pubkey.clone();
let message = |rpc_reply| -> NetworkActorMessage {
NetworkActorMessage::Command(NetworkActorCommand::SendPayment(
SendPaymentCommand {
target_pubkey: Some(node_b_pubkey),
amount: Some(10000),
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,
dry_run: false,
},
rpc_reply,
))
};
let res1 = call!(node_a.network_actor, message)
.expect("node_a alive")
.unwrap();
// this is the payment_hash generated by keysend
assert_eq!(res1.status, PaymentSessionStatus::Inflight);
let payment_hash1 = res1.payment_hash;

// the second payment is send from node_b to node_a
let message = |rpc_reply| -> NetworkActorMessage {
NetworkActorMessage::Command(NetworkActorCommand::SendPayment(
SendPaymentCommand {
target_pubkey: Some(node_a_pubkey),
amount: Some(9999),
payment_hash: None,
final_tlc_expiry_delta: None,
invoice: None,
timeout: None,
tlc_expiry_limit: None,
max_fee_amount: None,
max_parts: None,
keysend: Some(true),
udt_type_script: None,
allow_self_payment: false,
dry_run: false,
},
rpc_reply,
))
};
let res2 = call!(node_b.network_actor, message)
.expect("node_a alive")
.unwrap();
// this is the payment_hash generated by keysend
assert_eq!(res2.status, PaymentSessionStatus::Inflight);
let payment_hash2 = res2.payment_hash;

let message = |rpc_reply| -> NetworkActorMessage {
NetworkActorMessage::Command(NetworkActorCommand::SendPayment(
SendPaymentCommand {
target_pubkey: Some(node_b_pubkey),
amount: Some(9999),
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,
dry_run: false,
},
rpc_reply,
))
};
let res3 = call!(node_a.network_actor, message)
.expect("node_a alive")
.unwrap();
// this is the payment_hash generated by keysend
assert_eq!(res3.status, PaymentSessionStatus::Created);
let payment_hash3 = res3.payment_hash;

// the second payment is send from node_b to node_a
let message = |rpc_reply| -> NetworkActorMessage {
NetworkActorMessage::Command(NetworkActorCommand::SendPayment(
SendPaymentCommand {
target_pubkey: Some(node_a_pubkey),
amount: Some(10000),
payment_hash: None,
final_tlc_expiry_delta: None,
invoice: None,
timeout: None,
tlc_expiry_limit: None,
max_fee_amount: None,
max_parts: None,
keysend: Some(true),
udt_type_script: None,
allow_self_payment: false,
dry_run: false,
},
rpc_reply,
))
};
let res4 = call!(node_b.network_actor, message)
.expect("node_a alive")
.unwrap();
// this is the payment_hash generated by keysend
assert_eq!(res4.status, PaymentSessionStatus::Created);
let payment_hash4 = res4.payment_hash;

// sleep for 3 seconds to make sure the payment is processed
tokio::time::sleep(tokio::time::Duration::from_millis(3000)).await;

let message = |rpc_reply| -> NetworkActorMessage {
NetworkActorMessage::Command(NetworkActorCommand::GetPayment(payment_hash1, rpc_reply))
};
let res = call!(node_a.network_actor, message)
.expect("node_a alive")
.unwrap();
assert_eq!(res.status, PaymentSessionStatus::Success);
assert_eq!(res.failed_error, None);

let message = |rpc_reply| -> NetworkActorMessage {
NetworkActorMessage::Command(NetworkActorCommand::GetPayment(payment_hash2, rpc_reply))
};
let res = call!(node_b.network_actor, message)
.expect("node_a alive")
.unwrap();

assert_eq!(res.status, PaymentSessionStatus::Success);
assert_eq!(res.failed_error, None);

let message = |rpc_reply| -> NetworkActorMessage {
NetworkActorMessage::Command(NetworkActorCommand::GetPayment(payment_hash3, rpc_reply))
};
let res = call!(node_a.network_actor, message)
.expect("node_a alive")
.unwrap();
assert_eq!(res.status, PaymentSessionStatus::Success);
assert_eq!(res.failed_error, None);

let message = |rpc_reply| -> NetworkActorMessage {
NetworkActorMessage::Command(NetworkActorCommand::GetPayment(payment_hash4, rpc_reply))
};
let res = call!(node_b.network_actor, message)
.expect("node_a alive")
.unwrap();
assert_eq!(res.status, PaymentSessionStatus::Success);
assert_eq!(res.failed_error, None);

let node_a_new_balance = node_a.get_local_balance_from_channel(new_channel_id);
let node_b_new_balance = node_b.get_local_balance_from_channel(new_channel_id);

// assert the balance is right, the balance should be same as the initial balance
assert_eq!(node_a_new_balance, node_a_old_balance);
assert_eq!(node_b_new_balance, node_b_old_balance);
}

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

0 comments on commit a869c72

Please sign in to comment.