Skip to content

Commit

Permalink
f test receiver broadcast original psbt
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa committed Jul 24, 2024
1 parent c96086e commit cfa3b87
Showing 1 changed file with 61 additions and 4 deletions.
65 changes: 61 additions & 4 deletions tests/integration_tests_payjoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use common::{
};

use bitcoin::Amount;
use bitcoincore_rpc::{Client as BitcoindClient, RawTx, RpcApi};
use ldk_node::{
payment::{PaymentDirection, PaymentKind, PaymentStatus},
Event,
Expand Down Expand Up @@ -77,10 +78,11 @@ impl PayjoinReceiver {
payjoin_uri.to_string()
}

pub(crate) fn process_payjoin_request(self, substitue_address: Option<bitcoin::Address>) {
pub(crate) fn process_payjoin_request(
self, substitue_address: Option<bitcoin::Address>, bitcoind: Option<&BitcoindClient>,
) {
let mut enrolled = self.enrolled;
let (req, context) = enrolled.extract_req().unwrap();

let client = reqwest::blocking::Client::new();
let response = client
.post(req.url.to_string())
Expand All @@ -91,6 +93,12 @@ impl PayjoinReceiver {
let response = response.bytes().unwrap();
let response = enrolled.process_res(response.to_vec().as_slice(), context).unwrap();
let unchecked_proposal = response.unwrap();
if let Some(bitcoind) = bitcoind {
let tx = unchecked_proposal.extract_tx_to_schedule_broadcast();
let raw_tx = tx.raw_hex();
bitcoind.send_raw_transaction(raw_tx).unwrap();
return;
}

let proposal = unchecked_proposal.assume_interactive_receiver();
let proposal = proposal.check_inputs_not_owned(|_script| Ok(false)).unwrap();
Expand Down Expand Up @@ -126,6 +134,7 @@ impl PayjoinReceiver {
}
}

// Test sending payjoin transaction with original PSBT used eventually
#[test]
fn send_payjoin_transaction_original_psbt_used() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
Expand Down Expand Up @@ -159,7 +168,7 @@ fn send_payjoin_transaction_original_psbt_used() {
assert_eq!(payment.direction, PaymentDirection::Outbound);
assert_eq!(payment.kind, PaymentKind::Payjoin);

payjoin_receiver.process_payjoin_request(None);
payjoin_receiver.process_payjoin_request(None, None);

expect_payjoin_tx_broadcasted!(payjoin_sender);

Expand All @@ -170,6 +179,7 @@ fn send_payjoin_transaction_original_psbt_used() {
let _ = expect_payjoin_tx_sent_successfully_event!(payjoin_sender, false);
}

// Test sending payjoin transaction with changes to the original PSBT
#[test]
fn send_payjoin_transaction() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
Expand Down Expand Up @@ -204,7 +214,7 @@ fn send_payjoin_transaction() {
assert_eq!(payment.kind, PaymentKind::Payjoin);

let substitue_address = receiver.onchain_payment().new_address().unwrap();
payjoin_receiver.process_payjoin_request(Some(substitue_address));
payjoin_receiver.process_payjoin_request(Some(substitue_address), None);

let txid = expect_payjoin_tx_broadcasted!(payjoin_sender);

Expand All @@ -214,3 +224,50 @@ fn send_payjoin_transaction() {

expect_payjoin_tx_sent_successfully_event!(payjoin_sender, true);
}

// Test sending payjoin transaction with receiver broadcasting and not responding to the payjoin
// request
#[test]
fn send_payjoin_transaction_with_receiver_broadcasting() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let config_a = random_config(false);
let (receiver, _) = setup_two_nodes(&electrsd, false, false, false);
let payjoin_sender = setup_payjoin_node(&electrsd, config_a);
let addr_a = payjoin_sender.onchain_payment().new_address().unwrap();
let premine_amount_sat = 100_000_00;
premine_and_distribute_funds(
&bitcoind.client,
&electrsd.client,
vec![addr_a],
Amount::from_sat(premine_amount_sat),
);
payjoin_sender.sync_wallets().unwrap();
assert_eq!(payjoin_sender.list_balances().spendable_onchain_balance_sats, premine_amount_sat);
assert_eq!(payjoin_sender.list_balances().spendable_onchain_balance_sats, 100_000_00);
assert_eq!(payjoin_sender.next_event(), None);

let payjoin_receiver = PayjoinReceiver::enroll();
let payjoin_uri = payjoin_receiver
.receive(Amount::from_sat(80_000), receiver.onchain_payment().new_address().unwrap());

assert!(payjoin_sender.payjoin_payment().send(payjoin_uri).is_ok());

let txid = expect_payjoin_tx_pending_event!(payjoin_sender);
let payments = payjoin_sender.list_payments();
let payment = payments.first().unwrap();
assert_eq!(payment.amount_msat, Some(80_000));
assert_eq!(payment.status, PaymentStatus::Pending);
assert_eq!(payment.direction, PaymentDirection::Outbound);
assert_eq!(payment.kind, PaymentKind::Payjoin);

let substitue_address = receiver.onchain_payment().new_address().unwrap();
payjoin_receiver.process_payjoin_request(Some(substitue_address), Some(&bitcoind.client));

// let txid = expect_payjoin_tx_broadcasted!(payjoin_sender);

wait_for_tx(&electrsd.client, txid);
generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 1);
payjoin_sender.sync_wallets().unwrap();

expect_payjoin_tx_sent_successfully_event!(payjoin_sender, false);
}

0 comments on commit cfa3b87

Please sign in to comment.