Skip to content

Commit 764522d

Browse files
fixup! f reworking PR by allowing passing an wrapper enum
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent d9fdc80 commit 764522d

File tree

4 files changed

+48
-38
lines changed

4 files changed

+48
-38
lines changed

lightning/src/events/mod.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, Paym
2323
use crate::chain::transaction;
2424
use crate::ln::channelmanager::{InterceptId, PaymentId, RecipientOnionFields};
2525
use crate::ln::channel::FUNDING_CONF_DEADLINE_BLOCKS;
26-
use crate::offers::OfferInvoice;
2726
use crate::types::features::ChannelTypeFeatures;
2827
use crate::ln::msgs;
2928
use crate::ln::types::ChannelId;
@@ -956,7 +955,7 @@ pub enum Event {
956955
/// payment hash. A third party can verify that the payment was made by
957956
/// showing the invoice and confirming that the payment hash matches
958957
/// the hash of the payment preimage.
959-
bolt12_invoice: Option<OfferInvoice>,
958+
bolt12_invoice: Option<PaidInvoice>,
960959
},
961960
/// Indicates an outbound payment failed. Individual [`Event::PaymentPathFailed`] events
962961
/// provide failure information for each path attempt in the payment, including retries.
@@ -2752,3 +2751,27 @@ impl<T: EventHandler> EventHandler for Arc<T> {
27522751
self.deref().handle_event(event)
27532752
}
27542753
}
2754+
2755+
/// Wrapper time to move the bolt12 invoice and the static
2756+
/// invoice across the same event as a unique type.
2757+
#[derive(Clone, Debug, PartialEq, Eq)]
2758+
pub enum PaidInvoice {
2759+
/// Bolt12 invoice
2760+
Bolt12Invoice(crate::offers::invoice::Bolt12Invoice),
2761+
#[cfg(async_payments)]
2762+
/// Static invoice
2763+
StaticInvoice(crate::offers::static_invoice::StaticInvoice),
2764+
}
2765+
2766+
#[cfg(not(async_payments))]
2767+
impl_writeable_tlv_based_enum_legacy!(PaidInvoice,
2768+
;
2769+
(0, Bolt12Invoice),
2770+
);
2771+
2772+
#[cfg(async_payments)]
2773+
impl_writeable_tlv_based_enum_legacy!(PaidInvoice,
2774+
;
2775+
(0, Bolt12Invoice),
2776+
(2, StaticInvoice)
2777+
);

lightning/src/ln/outbound_payment.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
1414
use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
1515

1616
use crate::blinded_path::{IntroductionNode, NodeIdLookUp};
17-
use crate::events::{self, PaymentFailureReason};
17+
use crate::events::{self, PaymentFailureReason, PaidInvoice};
1818
use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
1919
use crate::ln::channel_state::ChannelDetails;
2020
use crate::ln::channelmanager::{EventCompletionAction, HTLCSource, PaymentId};
@@ -24,7 +24,6 @@ use crate::ln::onion_utils::{DecodedOnionFailure, HTLCFailReason};
2424
use crate::offers::invoice::Bolt12Invoice;
2525
use crate::offers::invoice_request::InvoiceRequest;
2626
use crate::offers::nonce::Nonce;
27-
use crate::offers::OfferInvoice;
2827
use crate::routing::router::{BlindedTail, InFlightHtlcs, RouteParametersConfig, Path, PaymentParameters, Route, RouteParameters, Router};
2928
use crate::sign::{EntropySource, NodeSigner, Recipient};
3029
use crate::util::errors::APIError;
@@ -108,7 +107,7 @@ pub(crate) enum PendingOutboundPayment {
108107
invoice_request: Option<InvoiceRequest>,
109108
// Storing the bolt12 invoice here to allow Proof of Payment after
110109
// the payment is made.
111-
bolt12_invoice: Option<OfferInvoice>,
110+
bolt12_invoice: Option<PaidInvoice>,
112111
custom_tlvs: Vec<(u64, Vec<u8>)>,
113112
pending_amt_msat: u64,
114113
/// Used to track the fee paid. Present iff the payment was serialized on 0.0.103+.
@@ -158,7 +157,7 @@ impl_writeable_tlv_based!(RetryableInvoiceRequest, {
158157
});
159158

160159
impl PendingOutboundPayment {
161-
fn bolt12_invoice(&self) -> Option<&OfferInvoice> {
160+
fn bolt12_invoice(&self) -> Option<&PaidInvoice> {
162161
match self {
163162
PendingOutboundPayment::Retryable { bolt12_invoice, .. } => bolt12_invoice.as_ref(),
164163
_ => None,
@@ -907,7 +906,7 @@ impl OutboundPayments {
907906
if let Some(max_fee_msat) = params_config.max_total_routing_fee_msat {
908907
route_params.max_total_routing_fee_msat = Some(max_fee_msat);
909908
}
910-
let invoice = OfferInvoice::Bolt12Invoice(invoice.clone());
909+
let invoice = PaidInvoice::Bolt12Invoice(invoice.clone());
911910
self.send_payment_for_bolt12_invoice_internal(
912911
payment_id, payment_hash, None, None, Some(&invoice), route_params, retry_strategy, router, first_hops,
913912
inflight_htlcs, entropy_source, node_signer, node_id_lookup, secp_ctx, best_block_height,
@@ -920,7 +919,7 @@ impl OutboundPayments {
920919
>(
921920
&self, payment_id: PaymentId, payment_hash: PaymentHash,
922921
keysend_preimage: Option<PaymentPreimage>, invoice_request: Option<&InvoiceRequest>,
923-
bolt12_invoice: Option<&OfferInvoice>,
922+
bolt12_invoice: Option<&PaidInvoice>,
924923
mut route_params: RouteParameters, retry_strategy: Retry, router: &R,
925924
first_hops: Vec<ChannelDetails>, inflight_htlcs: IH, entropy_source: &ES, node_signer: &NS,
926925
node_id_lookup: &NL, secp_ctx: &Secp256k1<secp256k1::All>, best_block_height: u32, logger: &L,
@@ -1667,7 +1666,7 @@ impl OutboundPayments {
16671666
&self, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, payment_id: PaymentId,
16681667
keysend_preimage: Option<PaymentPreimage>, route: &Route, retry_strategy: Option<Retry>,
16691668
payment_params: Option<PaymentParameters>, entropy_source: &ES, best_block_height: u32,
1670-
bolt12_invoice: Option<OfferInvoice>
1669+
bolt12_invoice: Option<PaidInvoice>
16711670
) -> Result<Vec<[u8; 32]>, PaymentSendFailure> where ES::Target: EntropySource {
16721671
let mut pending_outbounds = self.pending_outbound_payments.lock().unwrap();
16731672
match pending_outbounds.entry(payment_id) {
@@ -1686,7 +1685,7 @@ impl OutboundPayments {
16861685
fn create_pending_payment<ES: Deref>(
16871686
payment_hash: PaymentHash, recipient_onion: RecipientOnionFields,
16881687
keysend_preimage: Option<PaymentPreimage>, invoice_request: Option<InvoiceRequest>,
1689-
bolt12_invoice: Option<OfferInvoice>, route: &Route, retry_strategy: Option<Retry>,
1688+
bolt12_invoice: Option<PaidInvoice>, route: &Route, retry_strategy: Option<Retry>,
16901689
payment_params: Option<PaymentParameters>, entropy_source: &ES, best_block_height: u32
16911690
) -> (PendingOutboundPayment, Vec<[u8; 32]>)
16921691
where

lightning/src/offers/mod.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,3 @@ pub(crate) mod signer;
2828
pub mod static_invoice;
2929
#[cfg(test)]
3030
pub(crate) mod test_utils;
31-
32-
/// Wrapper time to move the bolt12 invoice and the static invoice across the same event as a unique
33-
/// type.
34-
// P.S: `OfferInvoice` is confusing, offer is containing the info for asking an invoice :) but I will leave
35-
// this up to the reviewer that I am sure that will find a better name!
36-
#[derive(Clone, PartialEq, Eq, Debug)]
37-
pub enum OfferInvoice {
38-
/// Bolt12 invoice
39-
Bolt12Invoice(invoice::Bolt12Invoice),
40-
#[cfg(async_payments)]
41-
/// Static invoice
42-
StaticInvoice(static_invoice::StaticInvoice),
43-
}
44-
45-
// FIXME(vincenzopalazzo): I do not think there is a way (easy and trivial) that adds cfg to the macro, so
46-
// when we remove the cfg will be removed we can merge these two macro in two.
47-
impl_writeable_tlv_based_enum_legacy!(OfferInvoice,
48-
;
49-
(0, Bolt12Invoice),
50-
);
51-
52-
#[cfg(async_payments)]
53-
impl_writeable_tlv_based_enum_legacy!(OfferInvoice,
54-
;
55-
(0, Bolt12Invoice),
56-
(2, StaticInvoice)
57-
);

lightning/src/offers/static_invoice.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::offers::offer::{
3333
};
3434
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
3535
use crate::types::features::{Bolt12InvoiceFeatures, OfferFeatures};
36-
use crate::util::ser::{CursorReadable, Iterable, WithoutLength, Writeable, Writer};
36+
use crate::util::ser::{CursorReadable, Iterable, WithoutLength, Writeable, Writer, Readable};
3737
use crate::util::string::PrintableString;
3838
use bitcoin::address::Address;
3939
use bitcoin::constants::ChainHash;
@@ -70,6 +70,14 @@ pub struct StaticInvoice {
7070
signature: Signature,
7171
}
7272

73+
impl PartialEq for StaticInvoice {
74+
fn eq(&self, other: &Self) -> bool {
75+
self.bytes.eq(&other.bytes)
76+
}
77+
}
78+
79+
impl Eq for StaticInvoice {}
80+
7381
/// The contents of a [`StaticInvoice`] for responding to an [`Offer`].
7482
///
7583
/// [`Offer`]: crate::offers::offer::Offer
@@ -535,6 +543,13 @@ impl Writeable for StaticInvoice {
535543
}
536544
}
537545

546+
impl Readable for StaticInvoice {
547+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
548+
let bytes: WithoutLength<Vec<u8>> = Readable::read(reader)?;
549+
Self::try_from(bytes.0).map_err(|_| DecodeError::InvalidValue)
550+
}
551+
}
552+
538553
impl TryFrom<Vec<u8>> for StaticInvoice {
539554
type Error = Bolt12ParseError;
540555

0 commit comments

Comments
 (0)