Skip to content

Commit

Permalink
f Account for lightning-invoice payment utils being removed
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Dec 12, 2023
1 parent d482871 commit 7a61ce5
Showing 1 changed file with 53 additions and 42 deletions.
95 changes: 53 additions & 42 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,10 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
return Err(Error::NotRunning);
}

let payment_hash = PaymentHash((*invoice.payment_hash()).to_byte_array());
let (payment_hash, recipient_onion, route_params) = payment::payment_parameters_from_invoice(&invoice).map_err(|_| {
log_error!(self.logger, "Failed to send payment due to the given invoice being \"zero-amount\". Please use send_payment_using_amount instead.");
Error::InvalidInvoice
})?;

if let Some(payment) = self.payment_store.get(&payment_hash) {
if payment.status == PaymentStatus::Pending
Expand All @@ -1129,13 +1132,17 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
}

let payment_secret = Some(*invoice.payment_secret());
let payment_id = PaymentId(invoice.payment_hash().to_byte_array());
let retry_strategy = Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT);

match lightning_invoice::payment::pay_invoice(
&invoice,
Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT),
self.channel_manager.as_ref(),
match self.channel_manager.send_payment(
payment_hash,
recipient_onion,
payment_id,
route_params,
retry_strategy,
) {
Ok(_payment_id) => {
Ok(()) => {
let payee_pubkey = invoice.recover_payee_pub_key();
let amt_msat = invoice.amount_milli_satoshis().unwrap();
log_info!(self.logger, "Initiated sending {}msat to {}", amt_msat, payee_pubkey);
Expand All @@ -1152,11 +1159,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {

Ok(payment_hash)
}
Err(payment::PaymentError::Invoice(e)) => {
log_error!(self.logger, "Failed to send payment due to invalid invoice: {}", e);
Err(Error::InvalidInvoice)
}
Err(payment::PaymentError::Sending(e)) => {
Err(e) => {
log_error!(self.logger, "Failed to send payment: {:?}", e);
match e {
channelmanager::RetryableSendFailure::DuplicatePayment => {
Expand Down Expand Up @@ -1234,11 +1237,13 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
let retry_strategy = Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT);
let recipient_fields = RecipientOnionFields::secret_only(*payment_secret);

match self
.channel_manager
.send_payment(payment_hash, recipient_fields, payment_id, route_params, retry_strategy)
.map_err(payment::PaymentError::Sending)
{
match self.channel_manager.send_payment(
payment_hash,
recipient_fields,
payment_id,
route_params,
retry_strategy,
) {
Ok(_payment_id) => {
let payee_pubkey = invoice.recover_payee_pub_key();
log_info!(
Expand All @@ -1260,11 +1265,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {

Ok(payment_hash)
}
Err(payment::PaymentError::Invoice(e)) => {
log_error!(self.logger, "Failed to send payment due to invalid invoice: {}", e);
Err(Error::InvalidInvoice)
}
Err(payment::PaymentError::Sending(e)) => {
Err(e) => {
log_error!(self.logger, "Failed to send payment: {:?}", e);

match e {
Expand Down Expand Up @@ -1382,17 +1383,19 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
return Err(Error::NotRunning);
}

let (_payment_hash, _recipient_onion, route_params) = payment::payment_parameters_from_invoice(&invoice).map_err(|_| {
log_error!(self.logger, "Failed to send probes due to the given invoice being \"zero-amount\". Please use send_payment_probes_using_amount instead.");
Error::InvalidInvoice
})?;

let liquidity_limit_multiplier = Some(self.config.probing_liquidity_limit_multiplier);

payment::preflight_probe_invoice(
invoice,
&*self.channel_manager,
liquidity_limit_multiplier,
)
.map_err(|e| {
log_error!(self.logger, "Failed to send payment probes: {:?}", e);
Error::ProbeSendingFailed
})?;
self.channel_manager
.send_preflight_probes(route_params, liquidity_limit_multiplier)
.map_err(|e| {
log_error!(self.logger, "Failed to send payment probes: {:?}", e);
Error::ProbeSendingFailed
})?;

Ok(())
}
Expand Down Expand Up @@ -1442,27 +1445,35 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
return Err(Error::NotRunning);
}

if let Some(invoice_amount_msat) = invoice.amount_milli_satoshis() {
let (_payment_hash, _recipient_onion, route_params) = if let Some(invoice_amount_msat) =
invoice.amount_milli_satoshis()
{
if amount_msat < invoice_amount_msat {
log_error!(
self.logger,
"Failed to send probes as the given amount needs to be at least the invoice amount: required {}msat, gave {}msat.", invoice_amount_msat, amount_msat);
return Err(Error::InvalidAmount);
}
}

payment::payment_parameters_from_invoice(&invoice).map_err(|_| {
log_error!(self.logger, "Failed to send probes due to the given invoice unexpectedly being \"zero-amount\".");
Error::InvalidInvoice
})?
} else {
payment::payment_parameters_from_zero_amount_invoice(&invoice, amount_msat).map_err(|_| {
log_error!(self.logger, "Failed to send probes due to the given invoice unexpectedly being not \"zero-amount\".");
Error::InvalidInvoice
})?
};

let liquidity_limit_multiplier = Some(self.config.probing_liquidity_limit_multiplier);

payment::preflight_probe_zero_value_invoice(
invoice,
amount_msat,
&*self.channel_manager,
liquidity_limit_multiplier,
)
.map_err(|e| {
log_error!(self.logger, "Failed to send payment probes: {:?}", e);
Error::ProbeSendingFailed
})?;
self.channel_manager
.send_preflight_probes(route_params, liquidity_limit_multiplier)
.map_err(|e| {
log_error!(self.logger, "Failed to send payment probes: {:?}", e);
Error::ProbeSendingFailed
})?;

Ok(())
}
Expand Down

0 comments on commit 7a61ce5

Please sign in to comment.