Skip to content

Commit

Permalink
handle outbound payment failure
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedberg committed Jun 7, 2023
1 parent 5991072 commit 339ea63
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
49 changes: 26 additions & 23 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,12 @@ pub(crate) async fn poll_for_user_input(
}
};

let mut outbound_payments = outbound_payments.lock().unwrap();
send_payment(&*channel_manager, &invoice, &mut outbound_payments);
persister.persist(OUTBOUND_PAYMENTS_FNAME, &*outbound_payments).unwrap();
send_payment(
&channel_manager,
&invoice,
&mut outbound_payments.lock().unwrap(),
persister.clone(),
);
}
"keysend" => {
let dest_pubkey = match words.next() {
Expand Down Expand Up @@ -664,35 +667,35 @@ fn open_channel(
}

fn send_payment(
channel_manager: &ChannelManager, invoice: &Invoice, outbound_payments: &mut PaymentInfoStorage,
channel_manager: &ChannelManager, invoice: &Invoice,
outbound_payments: &mut PaymentInfoStorage, persister: Arc<FilesystemPersister>,
) {
let status =
match pay_invoice(invoice, Retry::Timeout(Duration::from_secs(10)), channel_manager) {
Ok(_payment_id) => {
let payee_pubkey = invoice.recover_payee_pub_key();
let amt_msat = invoice.amount_milli_satoshis().unwrap();
println!("EVENT: initiated sending {} msats to {}", amt_msat, payee_pubkey);
print!("> ");
HTLCStatus::Pending
}
Err(e) => {
println!("ERROR: failed to send payment: {:?}", e);
print!("> ");
HTLCStatus::Failed
}
};
let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
let payment_secret = Some(invoice.payment_secret().clone());

let payment_hash = PaymentHash((*invoice.payment_hash()).into_inner());
let payment_secret = Some(*invoice.payment_secret());
outbound_payments.payments.insert(
payment_hash,
PaymentInfo {
preimage: None,
secret: payment_secret,
status,
status: HTLCStatus::Pending,
amt_msat: MillisatAmount(invoice.amount_milli_satoshis()),
},
);
persister.persist(OUTBOUND_PAYMENTS_FNAME, &*outbound_payments).unwrap();
match pay_invoice(invoice, Retry::Timeout(Duration::from_secs(10)), channel_manager) {
Ok(_payment_id) => {
let payee_pubkey = invoice.recover_payee_pub_key();
let amt_msat = invoice.amount_milli_satoshis().unwrap();
println!("EVENT: initiated sending {} msats to {}", amt_msat, payee_pubkey);
print!("> ");
}
Err(e) => {
println!("ERROR: failed to send payment: {:?}", e);
print!("> ");
outbound_payments.payments.get_mut(&payment_hash).unwrap().status = HTLCStatus::Failed;
persister.persist(OUTBOUND_PAYMENTS_FNAME, &*outbound_payments).unwrap();
}
};
}

fn keysend<E: EntropySource>(
Expand Down
23 changes: 22 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use lightning::chain::keysinterface::{
use lightning::chain::{chainmonitor, ChannelMonitorUpdateStatus};
use lightning::chain::{Filter, Watch};
use lightning::events::{Event, PaymentFailureReason, PaymentPurpose};
use lightning::ln::channelmanager;
use lightning::ln::channelmanager::{self, RecentPaymentDetails};
use lightning::ln::channelmanager::{
ChainParameters, ChannelManagerReadArgs, SimpleArcChannelManager,
};
Expand Down Expand Up @@ -736,6 +736,27 @@ async fn start_ldk() {
"{}/{}",
ldk_data_dir, OUTBOUND_PAYMENTS_FNAME
)))));
let recent_payments_payment_hashes = channel_manager
.list_recent_payments()
.into_iter()
.filter_map(|p| match p {
RecentPaymentDetails::Pending { payment_hash, .. } => Some(payment_hash),
RecentPaymentDetails::Fulfilled { payment_hash } => payment_hash,
RecentPaymentDetails::Abandoned { payment_hash } => Some(payment_hash),
})
.collect::<Vec<PaymentHash>>();
for (payment_hash, payment_info) in outbound_payments
.lock()
.unwrap()
.payments
.iter_mut()
.filter(|(_, i)| matches!(i.status, HTLCStatus::Pending))
{
if !recent_payments_payment_hashes.contains(payment_hash) {
payment_info.status = HTLCStatus::Failed;
}
}
persister.persist(OUTBOUND_PAYMENTS_FNAME, &*outbound_payments.lock().unwrap()).unwrap();

// Step 18: Handle LDK Events
let channel_manager_event_listener = Arc::clone(&channel_manager);
Expand Down

0 comments on commit 339ea63

Please sign in to comment.