|
| 1 | +use super::*; |
| 2 | + |
| 3 | +const TEST_DIR_BASE: &str = "tmp/ifa_channel/"; |
| 4 | + |
| 5 | +#[serial_test::serial] |
| 6 | +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] |
| 7 | +#[traced_test] |
| 8 | +async fn open_pay_close() { |
| 9 | + initialize(); |
| 10 | + |
| 11 | + let test_dir_base = format!("{TEST_DIR_BASE}open_pay_close/"); |
| 12 | + let test_dir_node1 = format!("{test_dir_base}node1"); |
| 13 | + let test_dir_node2 = format!("{test_dir_base}node2"); |
| 14 | + let (node1_addr, _) = start_node(&test_dir_node1, NODE1_PEER_PORT, false).await; |
| 15 | + let (node2_addr, _) = start_node(&test_dir_node2, NODE2_PEER_PORT, false).await; |
| 16 | + |
| 17 | + fund_and_create_utxos(node1_addr, None).await; |
| 18 | + fund_and_create_utxos(node2_addr, None).await; |
| 19 | + |
| 20 | + let asset_id = issue_asset_ifa(node1_addr).await.asset_id; |
| 21 | + |
| 22 | + let node2_pubkey = node_info(node2_addr).await.pubkey; |
| 23 | + let channel = open_channel( |
| 24 | + node1_addr, |
| 25 | + &node2_pubkey, |
| 26 | + Some(NODE2_PEER_PORT), |
| 27 | + None, |
| 28 | + Some(3500000), |
| 29 | + Some(600), |
| 30 | + Some(&asset_id), |
| 31 | + ) |
| 32 | + .await; |
| 33 | + assert_eq!(asset_balance_spendable(node1_addr, &asset_id).await, 400); |
| 34 | + |
| 35 | + let asset_amount = Some(100); |
| 36 | + let LNInvoiceResponse { invoice } = |
| 37 | + ln_invoice(node2_addr, None, Some(&asset_id), asset_amount, 900).await; |
| 38 | + send_payment(node1_addr, invoice.clone()).await; |
| 39 | + |
| 40 | + let decoded = decode_ln_invoice(node1_addr, &invoice).await; |
| 41 | + let payment = get_payment(node1_addr, &decoded.payment_hash, PaymentType::Outbound).await; |
| 42 | + assert_eq!(payment.asset_id, Some(asset_id.clone())); |
| 43 | + assert_eq!(payment.asset_amount, asset_amount); |
| 44 | + assert_eq!(payment.status, HTLCStatus::Succeeded); |
| 45 | + let payment = get_payment( |
| 46 | + node2_addr, |
| 47 | + &decoded.payment_hash, |
| 48 | + PaymentType::InboundAutoClaim, |
| 49 | + ) |
| 50 | + .await; |
| 51 | + assert_eq!(payment.asset_id, Some(asset_id.clone())); |
| 52 | + assert_eq!(payment.asset_amount, asset_amount); |
| 53 | + assert_eq!(payment.status, HTLCStatus::Succeeded); |
| 54 | + |
| 55 | + close_channel(node1_addr, &channel.channel_id, &node2_pubkey, false).await; |
| 56 | + wait_for_balance(node1_addr, &asset_id, 900).await; |
| 57 | + wait_for_balance(node2_addr, &asset_id, 100).await; |
| 58 | +} |
| 59 | + |
| 60 | +#[serial_test::serial] |
| 61 | +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] |
| 62 | +#[traced_test] |
| 63 | +async fn virtual_open_pay_close() { |
| 64 | + initialize(); |
| 65 | + |
| 66 | + let test_dir_base = format!("{TEST_DIR_BASE}virtual_open_pay_close/"); |
| 67 | + let host_peer_port = next_peer_port(); |
| 68 | + let client_peer_port = next_peer_port(); |
| 69 | + |
| 70 | + let (host_addr, _) = start_node_with_virtual_options( |
| 71 | + &format!("{test_dir_base}host_node"), |
| 72 | + host_peer_port, |
| 73 | + false, |
| 74 | + true, |
| 75 | + vec![], |
| 76 | + ) |
| 77 | + .await; |
| 78 | + let host_pubkey = node_info(host_addr).await.pubkey; |
| 79 | + |
| 80 | + fund_and_create_utxos(host_addr, None).await; |
| 81 | + let asset_id = issue_asset_ifa(host_addr).await.asset_id; |
| 82 | + |
| 83 | + let (client_addr, _) = start_node_with_virtual_options( |
| 84 | + &format!("{test_dir_base}client_node"), |
| 85 | + client_peer_port, |
| 86 | + false, |
| 87 | + true, |
| 88 | + vec![bitcoin::secp256k1::PublicKey::from_str(&host_pubkey).unwrap()], |
| 89 | + ) |
| 90 | + .await; |
| 91 | + let client_pubkey = node_info(client_addr).await.pubkey; |
| 92 | + |
| 93 | + let channel = open_virtual_channel( |
| 94 | + host_addr, |
| 95 | + &client_pubkey, |
| 96 | + Some(client_peer_port), |
| 97 | + Some(100_000), |
| 98 | + Some(0), |
| 99 | + Some(600), |
| 100 | + Some(&asset_id), |
| 101 | + None, |
| 102 | + ) |
| 103 | + .await; |
| 104 | + assert_eq!(channel.asset_id.as_deref(), Some(asset_id.as_str())); |
| 105 | + |
| 106 | + let asset_amount = Some(100); |
| 107 | + let LNInvoiceResponse { invoice } = |
| 108 | + ln_invoice(client_addr, None, Some(&asset_id), asset_amount, 900).await; |
| 109 | + let payment = send_payment(host_addr, invoice).await; |
| 110 | + assert_eq!(payment.asset_id, Some(asset_id.clone())); |
| 111 | + assert_eq!(payment.asset_amount, asset_amount); |
| 112 | + assert_eq!(payment.status, HTLCStatus::Succeeded); |
| 113 | + wait_for_ln_balance(host_addr, &asset_id, 500).await; |
| 114 | + wait_for_ln_balance(client_addr, &asset_id, 100).await; |
| 115 | + |
| 116 | + // virtual close requires the counterparty balance back at zero |
| 117 | + let LNInvoiceResponse { invoice } = ln_invoice( |
| 118 | + host_addr, |
| 119 | + Some(3_000_000), |
| 120 | + Some(&asset_id), |
| 121 | + asset_amount, |
| 122 | + 900, |
| 123 | + ) |
| 124 | + .await; |
| 125 | + let payment = send_payment(client_addr, invoice).await; |
| 126 | + assert_eq!(payment.status, HTLCStatus::Succeeded); |
| 127 | + wait_for_ln_balance(host_addr, &asset_id, 600).await; |
| 128 | + wait_for_ln_balance(client_addr, &asset_id, 0).await; |
| 129 | + |
| 130 | + close_channel(host_addr, &channel.channel_id, &client_pubkey, false).await; |
| 131 | + |
| 132 | + shutdown(&[host_addr, client_addr]).await; |
| 133 | +} |
0 commit comments