Skip to content

Commit

Permalink
Introduce RecipientInfo struct for send_payment
Browse files Browse the repository at this point in the history
This work is a pre-req for [Issue-1298](lightningdevkit#1298).

This is a rebase of [PR-1221](lightningdevkit#1221) with an
additional commit to move the payment_secret and the payment_metadata into a new struct called
RecipientInfo. This will allow us to add custom onion TLVs, see [discussion](lightningdevkit#1298 (comment))

No logic change, just lots of plumbing.
  • Loading branch information
andozw committed Apr 22, 2022
1 parent 03e9e2a commit f4f0621
Show file tree
Hide file tree
Showing 14 changed files with 314 additions and 162 deletions.
20 changes: 10 additions & 10 deletions lightning-invoice/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
//! # #[cfg(feature = "no-std")]
//! # extern crate core2;
//! #
//! # use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
//! # use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret, RecipientInfo};
//! # use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
//! # use lightning::ln::msgs::LightningError;
//! # use lightning::routing::scoring::Score;
Expand Down Expand Up @@ -64,8 +64,7 @@
//! # impl Payer for FakePayer {
//! # fn node_id(&self) -> PublicKey { unimplemented!() }
//! # fn first_hops(&self) -> Vec<ChannelDetails> { unimplemented!() }
//! # fn send_payment(&self, route: &Route, payment_hash: PaymentHash,
//! # payment_secret: &Option<PaymentSecret>, payment_metadata: Option<Vec<u8>>
//! # fn send_payment(&self, route: &Route, payment_hash: PaymentHash, recipient_info: &RecipientInfo
//! # ) -> Result<PaymentId, PaymentSendFailure> { unimplemented!() }
//! # fn send_spontaneous_payment(
//! # &self, route: &Route, payment_preimage: PaymentPreimage
Expand Down Expand Up @@ -139,7 +138,7 @@ use bitcoin_hashes::Hash;
use bitcoin_hashes::sha256::Hash as Sha256;

use crate::prelude::*;
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
use lightning::ln::{PaymentHash, PaymentPreimage, RecipientInfo};
use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
use lightning::ln::msgs::LightningError;
use lightning::routing::scoring::{LockableScore, Score};
Expand Down Expand Up @@ -186,8 +185,8 @@ pub trait Payer {
fn first_hops(&self) -> Vec<ChannelDetails>;

/// Sends a payment over the Lightning Network using the given [`Route`].
fn send_payment(&self, route: &Route, payment_hash: PaymentHash,
payment_secret: &Option<PaymentSecret>, payment_metadata: Option<Vec<u8>>
fn send_payment(
&self, route: &Route, payment_hash: PaymentHash, recipient_info: &RecipientInfo
) -> Result<PaymentId, PaymentSendFailure>;

/// Sends a spontaneous payment over the Lightning Network using the given [`Route`].
Expand Down Expand Up @@ -309,7 +308,8 @@ where
};

let send_payment = |route: &Route| {
self.payer.send_payment(route, payment_hash, &payment_secret, invoice.payment_metadata().cloned())
let recipient_info = RecipientInfo { payment_secret, payment_metadata: invoice.payment_metadata().cloned() };
self.payer.send_payment(route, payment_hash, &recipient_info)
};
self.pay_internal(&route_params, payment_hash, send_payment)
.map_err(|e| { self.payment_cache.lock().unwrap().remove(&payment_hash); e })
Expand Down Expand Up @@ -528,7 +528,7 @@ mod tests {
use crate::{InvoiceBuilder, Currency};
use utils::create_invoice_from_channelmanager_and_duration_since_epoch;
use bitcoin_hashes::sha256::Hash as Sha256;
use lightning::ln::PaymentPreimage;
use lightning::ln::{PaymentPreimage, PaymentSecret};
use lightning::ln::features::{ChannelFeatures, NodeFeatures, InitFeatures};
use lightning::ln::functional_test_utils::*;
use lightning::ln::msgs::{ChannelMessageHandler, ErrorAction, LightningError};
Expand Down Expand Up @@ -1463,8 +1463,8 @@ mod tests {
Vec::new()
}

fn send_payment(&self, route: &Route, _payment_hash: PaymentHash,
_payment_secret: &Option<PaymentSecret>, _payment_metadata: Option<Vec<u8>>
fn send_payment(
&self, route: &Route, _payment_hash: PaymentHash, _recipient_info: &RecipientInfo
) -> Result<PaymentId, PaymentSendFailure> {
self.check_value_msats(Amount::ForInvoice(route.get_total_amount()));
self.check_attempts()
Expand Down
22 changes: 15 additions & 7 deletions lightning-invoice/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bitcoin_hashes::{Hash, sha256};
use lightning::chain;
use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
use lightning::chain::keysinterface::{Recipient, KeysInterface, Sign};
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
use lightning::ln::{PaymentHash, PaymentPreimage, RecipientInfo};
use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, PaymentId, PaymentSendFailure, MIN_FINAL_CLTV_EXPIRY};
#[cfg(feature = "std")]
use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA};
Expand Down Expand Up @@ -474,10 +474,10 @@ where
self.list_usable_channels()
}

fn send_payment(&self, route: &Route, payment_hash: PaymentHash,
payment_secret: &Option<PaymentSecret>, payment_metadata: Option<Vec<u8>>
fn send_payment(
&self, route: &Route, payment_hash: PaymentHash, recipient_info: &RecipientInfo
) -> Result<PaymentId, PaymentSendFailure> {
self.send_payment(route, payment_hash, payment_secret, payment_metadata)
self.send_payment(route, payment_hash, recipient_info)
}

fn send_spontaneous_payment(
Expand Down Expand Up @@ -505,7 +505,7 @@ mod test {
use bitcoin_hashes::Hash;
use bitcoin_hashes::sha256::Hash as Sha256;
use lightning::chain::keysinterface::PhantomKeysManager;
use lightning::ln::{PaymentPreimage, PaymentHash};
use lightning::ln::{PaymentPreimage, PaymentHash, RecipientInfo};
use lightning::ln::channelmanager::{PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY};
use lightning::ln::functional_test_utils::*;
use lightning::ln::features::InitFeatures;
Expand Down Expand Up @@ -561,7 +561,11 @@ mod test {
let payment_event = {
let mut payment_hash = PaymentHash([0; 32]);
payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone()), None).unwrap();
let recipient_info = RecipientInfo {
payment_secret: Some(invoice.payment_secret().clone()),
payment_metadata: None
};
nodes[0].node.send_payment(&route, payment_hash, &recipient_info).unwrap();
let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
assert_eq!(added_monitors.len(), 1);
added_monitors.clear();
Expand Down Expand Up @@ -830,7 +834,11 @@ mod test {
let (payment_event, fwd_idx) = {
let mut payment_hash = PaymentHash([0; 32]);
payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone()), None).unwrap();
let recipient_info = RecipientInfo {
payment_secret: Some(invoice.payment_secret().clone()),
payment_metadata: None
};
nodes[0].node.send_payment(&route, payment_hash, &recipient_info).unwrap();
let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
assert_eq!(added_monitors.len(), 1);
added_monitors.clear();
Expand Down
7 changes: 6 additions & 1 deletion lightning/src/chain/chainmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ mod tests {
use ln::features::InitFeatures;
use ln::functional_test_utils::*;
use ln::msgs::ChannelMessageHandler;
use ln::RecipientInfo;
use util::errors::APIError;
use util::events::{ClosureReason, MessageSendEvent, MessageSendEventsProvider};
use util::test_utils::{OnRegisterOutput, TxOutReference};
Expand Down Expand Up @@ -901,7 +902,11 @@ mod tests {
// If the ChannelManager tries to update the channel, however, the ChainMonitor will pass
// the update through to the ChannelMonitor which will refuse it (as the channel is closed).
chanmon_cfgs[0].persister.set_update_ret(Ok(()));
unwrap_send_err!(nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret), None),
let recipient_info = RecipientInfo {
payment_secret: Some(second_payment_secret),
payment_metadata: None,
};
unwrap_send_err!(nodes[0].node.send_payment(&route, second_payment_hash, &recipient_info),
true, APIError::ChannelUnavailable { ref err },
assert!(err.contains("ChannelMonitor storage failure")));
check_added_monitors!(nodes[0], 2); // After the failure we generate a close-channel monitor update
Expand Down
5 changes: 3 additions & 2 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3343,7 +3343,7 @@ mod tests {
use chain::package::{weight_offered_htlc, weight_received_htlc, weight_revoked_offered_htlc, weight_revoked_received_htlc, WEIGHT_REVOKED_OUTPUT};
use chain::transaction::OutPoint;
use chain::keysinterface::InMemorySigner;
use ln::{PaymentPreimage, PaymentHash};
use ln::{PaymentPreimage, PaymentHash, RecipientInfo};
use ln::chan_utils;
use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
use ln::channelmanager::PaymentSendFailure;
Expand Down Expand Up @@ -3411,7 +3411,8 @@ mod tests {
// If the ChannelManager tries to update the channel, however, the ChainMonitor will pass
// the update through to the ChannelMonitor which will refuse it (as the channel is closed).
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 100_000);
unwrap_send_err!(nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret), None),
let recipient_info = RecipientInfo { payment_secret: Some(payment_secret), payment_metadata: None };
unwrap_send_err!(nodes[1].node.send_payment(&route, payment_hash, &recipient_info),
true, APIError::ChannelUnavailable { ref err },
assert!(err.contains("ChannelMonitor storage failure")));
check_added_monitors!(nodes[1], 2); // After the failure we generate a close-channel monitor update
Expand Down
Loading

0 comments on commit f4f0621

Please sign in to comment.