From 5c7af8c6d32dd4bb2b307ba29c68667a15cb8509 Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Thu, 13 Jun 2024 14:09:12 -0400 Subject: [PATCH] Cfg-gate async payments module and public enum variants. --- ci/check-cfg-flags.py | 2 ++ ci/ci-tests.sh | 2 ++ lightning/src/ln/channelmanager.rs | 1 + lightning/src/ln/offers_tests.rs | 6 ++++++ lightning/src/offers/mod.rs | 4 ++-- lightning/src/offers/offer.rs | 1 + lightning/src/onion_message/messenger.rs | 7 ++++++- lightning/src/onion_message/offers.rs | 13 +++++++++++-- lightning/src/onion_message/packet.rs | 6 ++++++ 9 files changed, 37 insertions(+), 5 deletions(-) diff --git a/ci/check-cfg-flags.py b/ci/check-cfg-flags.py index c33e8aa3a..d73bf50a1 100755 --- a/ci/check-cfg-flags.py +++ b/ci/check-cfg-flags.py @@ -104,6 +104,8 @@ def check_cfg_tag(cfg): pass elif cfg == "splicing": pass + elif cfg == "async_payments": + pass else: print("Bad cfg tag: " + cfg) assert False diff --git a/ci/ci-tests.sh b/ci/ci-tests.sh index 0dc654d8b..d0ba7c7fe 100755 --- a/ci/ci-tests.sh +++ b/ci/ci-tests.sh @@ -179,3 +179,5 @@ RUSTFLAGS="--cfg=async_signing" cargo test --verbose --color always -p lightning RUSTFLAGS="--cfg=dual_funding" cargo test --verbose --color always -p lightning [ "$CI_MINIMIZE_DISK_USAGE" != "" ] && cargo clean RUSTFLAGS="--cfg=splicing" cargo test --verbose --color always -p lightning +[ "$CI_MINIMIZE_DISK_USAGE" != "" ] && cargo clean +RUSTFLAGS="--cfg=async_payments" cargo test --verbose --color always -p lightning diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index f189aee04..fe3320a13 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -10337,6 +10337,7 @@ where }, } }, + #[cfg(async_payments)] OffersMessage::StaticInvoice(_invoice) => { match responder { Some(responder) => { diff --git a/lightning/src/ln/offers_tests.rs b/lightning/src/ln/offers_tests.rs index 10ebd599b..e174dfc1c 100644 --- a/lightning/src/ln/offers_tests.rs +++ b/lightning/src/ln/offers_tests.rs @@ -192,9 +192,11 @@ fn extract_invoice_request<'a, 'b, 'c>( ParsedOnionMessageContents::Offers(offers_message) => match offers_message { OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path.unwrap()), OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice), + #[cfg(async_payments)] OffersMessage::StaticInvoice(invoice) => panic!("Unexpected static invoice: {:?}", invoice), OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error), }, + #[cfg(async_payments)] ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message), ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message), }, @@ -209,9 +211,11 @@ fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) ParsedOnionMessageContents::Offers(offers_message) => match offers_message { OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request), OffersMessage::Invoice(invoice) => invoice, + #[cfg(async_payments)] OffersMessage::StaticInvoice(invoice) => panic!("Unexpected static invoice: {:?}", invoice), OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error), }, + #[cfg(async_payments)] ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message), ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message), }, @@ -228,9 +232,11 @@ fn extract_invoice_error<'a, 'b, 'c>( ParsedOnionMessageContents::Offers(offers_message) => match offers_message { OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request), OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice), + #[cfg(async_payments)] OffersMessage::StaticInvoice(invoice) => panic!("Unexpected invoice: {:?}", invoice), OffersMessage::InvoiceError(error) => error, }, + #[cfg(async_payments)] ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message), ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message), }, diff --git a/lightning/src/offers/mod.rs b/lightning/src/offers/mod.rs index b77eec161..e5e894e2a 100644 --- a/lightning/src/offers/mod.rs +++ b/lightning/src/offers/mod.rs @@ -24,7 +24,7 @@ pub mod parse; mod payer; pub mod refund; pub(crate) mod signer; -#[allow(unused)] -pub(crate) mod static_invoice; +#[cfg(async_payments)] +pub mod static_invoice; #[cfg(test)] pub(crate) mod test_utils; diff --git a/lightning/src/offers/offer.rs b/lightning/src/offers/offer.rs index dd58c75ce..253de8652 100644 --- a/lightning/src/offers/offer.rs +++ b/lightning/src/offers/offer.rs @@ -665,6 +665,7 @@ impl Offer { self.contents.expects_quantity() } + #[cfg(async_payments)] pub(super) fn verify( &self, key: &ExpandedKey, secp_ctx: &Secp256k1 ) -> Result<(OfferId, Option), ()> { diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 4182fdf03..c7da29307 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -24,7 +24,9 @@ use crate::ln::features::{InitFeatures, NodeFeatures}; use crate::ln::msgs::{self, OnionMessage, OnionMessageHandler, SocketAddress}; use crate::ln::onion_utils; use crate::routing::gossip::{NetworkGraph, NodeId, ReadOnlyNetworkGraph}; -use super::async_payments::{AsyncPaymentsMessage, AsyncPaymentsMessageHandler}; +use super::async_payments::AsyncPaymentsMessageHandler; +#[cfg(async_payments)] +use super::async_payments::AsyncPaymentsMessage; use super::packet::OnionMessageContents; use super::packet::ParsedOnionMessageContents; use super::offers::OffersMessageHandler; @@ -254,6 +256,7 @@ pub struct OnionMessenger< node_id_lookup: NL, message_router: MR, offers_handler: OMH, + #[allow(unused)] async_payments_handler: APH, custom_handler: CMH, intercept_messages_for_offline_peers: bool, @@ -1444,12 +1447,14 @@ where let response_instructions = self.offers_handler.handle_message(msg, responder); let _ = self.handle_onion_message_response(response_instructions); }, + #[cfg(async_payments)] ParsedOnionMessageContents::AsyncPayments(AsyncPaymentsMessage::HeldHtlcAvailable(msg)) => { let response_instructions = self.async_payments_handler.held_htlc_available( msg, responder ); let _ = self.handle_onion_message_response(response_instructions); }, + #[cfg(async_payments)] ParsedOnionMessageContents::AsyncPayments(AsyncPaymentsMessage::ReleaseHeldHtlc(msg)) => { self.async_payments_handler.release_held_htlc(msg); }, diff --git a/lightning/src/onion_message/offers.rs b/lightning/src/onion_message/offers.rs index 52dacd84e..397f4b8a7 100644 --- a/lightning/src/onion_message/offers.rs +++ b/lightning/src/onion_message/offers.rs @@ -16,6 +16,7 @@ use crate::offers::invoice_error::InvoiceError; use crate::offers::invoice_request::InvoiceRequest; use crate::offers::invoice::Bolt12Invoice; use crate::offers::parse::Bolt12ParseError; +#[cfg(async_payments)] use crate::offers::static_invoice::StaticInvoice; use crate::onion_message::packet::OnionMessageContents; use crate::util::logger::Logger; @@ -30,6 +31,7 @@ use crate::prelude::*; const INVOICE_REQUEST_TLV_TYPE: u64 = 64; const INVOICE_TLV_TYPE: u64 = 66; const INVOICE_ERROR_TLV_TYPE: u64 = 68; +#[cfg(async_payments)] const STATIC_INVOICE_TLV_TYPE: u64 = 70; /// A handler for an [`OnionMessage`] containing a BOLT 12 Offers message as its payload. @@ -74,7 +76,8 @@ pub enum OffersMessage { /// [`Refund`]: crate::offers::refund::Refund Invoice(Bolt12Invoice), - /// A `StaticInvoice` sent in response to an [`InvoiceRequest`]. + #[cfg(async_payments)] + /// A [`StaticInvoice`] sent in response to an [`InvoiceRequest`]. StaticInvoice(StaticInvoice), /// An error from handling an [`OffersMessage`]. @@ -87,8 +90,9 @@ impl OffersMessage { match tlv_type { INVOICE_REQUEST_TLV_TYPE | INVOICE_TLV_TYPE - | STATIC_INVOICE_TLV_TYPE | INVOICE_ERROR_TLV_TYPE => true, + #[cfg(async_payments)] + STATIC_INVOICE_TLV_TYPE => true, _ => false, } } @@ -97,6 +101,7 @@ impl OffersMessage { match tlv_type { INVOICE_REQUEST_TLV_TYPE => Ok(Self::InvoiceRequest(InvoiceRequest::try_from(bytes)?)), INVOICE_TLV_TYPE => Ok(Self::Invoice(Bolt12Invoice::try_from(bytes)?)), + #[cfg(async_payments)] STATIC_INVOICE_TLV_TYPE => Ok(Self::StaticInvoice(StaticInvoice::try_from(bytes)?)), _ => Err(Bolt12ParseError::Decode(DecodeError::InvalidValue)), } @@ -112,6 +117,7 @@ impl fmt::Debug for OffersMessage { OffersMessage::Invoice(message) => { write!(f, "{:?}", message.as_tlv_stream()) } + #[cfg(async_payments)] OffersMessage::StaticInvoice(message) => { write!(f, "{:?}", message) } @@ -127,6 +133,7 @@ impl OnionMessageContents for OffersMessage { match self { OffersMessage::InvoiceRequest(_) => INVOICE_REQUEST_TLV_TYPE, OffersMessage::Invoice(_) => INVOICE_TLV_TYPE, + #[cfg(async_payments)] OffersMessage::StaticInvoice(_) => STATIC_INVOICE_TLV_TYPE, OffersMessage::InvoiceError(_) => INVOICE_ERROR_TLV_TYPE, } @@ -135,6 +142,7 @@ impl OnionMessageContents for OffersMessage { match &self { OffersMessage::InvoiceRequest(_) => "Invoice Request", OffersMessage::Invoice(_) => "Invoice", + #[cfg(async_payments)] OffersMessage::StaticInvoice(_) => "Static Invoice", OffersMessage::InvoiceError(_) => "Invoice Error", } @@ -146,6 +154,7 @@ impl Writeable for OffersMessage { match self { OffersMessage::InvoiceRequest(message) => message.write(w), OffersMessage::Invoice(message) => message.write(w), + #[cfg(async_payments)] OffersMessage::StaticInvoice(message) => message.write(w), OffersMessage::InvoiceError(message) => message.write(w), } diff --git a/lightning/src/onion_message/packet.rs b/lightning/src/onion_message/packet.rs index 47b1a0313..fca7dd6a9 100644 --- a/lightning/src/onion_message/packet.rs +++ b/lightning/src/onion_message/packet.rs @@ -17,6 +17,7 @@ use crate::blinded_path::message::{ForwardTlvs, ReceiveTlvs}; use crate::blinded_path::utils::Padding; use crate::ln::msgs::DecodeError; use crate::ln::onion_utils; +#[cfg(async_payments)] use super::async_payments::AsyncPaymentsMessage; use super::messenger::CustomOnionMessageHandler; use super::offers::OffersMessage; @@ -130,6 +131,7 @@ pub enum ParsedOnionMessageContents { /// A message related to BOLT 12 Offers. Offers(OffersMessage), /// A message related to async payments. + #[cfg(async_payments)] AsyncPayments(AsyncPaymentsMessage), /// A custom onion message specified by the user. Custom(T), @@ -142,6 +144,7 @@ impl OnionMessageContents for ParsedOnionMessageContent fn tlv_type(&self) -> u64 { match self { &ParsedOnionMessageContents::Offers(ref msg) => msg.tlv_type(), + #[cfg(async_payments)] &ParsedOnionMessageContents::AsyncPayments(ref msg) => msg.tlv_type(), &ParsedOnionMessageContents::Custom(ref msg) => msg.tlv_type(), } @@ -149,6 +152,7 @@ impl OnionMessageContents for ParsedOnionMessageContent fn msg_type(&self) -> &'static str { match self { ParsedOnionMessageContents::Offers(ref msg) => msg.msg_type(), + #[cfg(async_payments)] ParsedOnionMessageContents::AsyncPayments(ref msg) => msg.msg_type(), ParsedOnionMessageContents::Custom(ref msg) => msg.msg_type(), } @@ -159,6 +163,7 @@ impl Writeable for ParsedOnionMessageContents { fn write(&self, w: &mut W) -> Result<(), io::Error> { match self { ParsedOnionMessageContents::Offers(msg) => Ok(msg.write(w)?), + #[cfg(async_payments)] ParsedOnionMessageContents::AsyncPayments(msg) => Ok(msg.write(w)?), ParsedOnionMessageContents::Custom(msg) => Ok(msg.write(w)?), } @@ -261,6 +266,7 @@ for Payload::CustomM message = Some(ParsedOnionMessageContents::Offers(msg)); Ok(true) }, + #[cfg(async_payments)] tlv_type if AsyncPaymentsMessage::is_known_type(tlv_type) => { let msg = AsyncPaymentsMessage::read(msg_reader, tlv_type)?; message = Some(ParsedOnionMessageContents::AsyncPayments(msg)); -- 2.39.5