Skip to content

Introduce OffersMessageFlow #3412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions lightning-background-processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,7 @@ mod tests {
IgnoringMessageHandler, MessageHandler, PeerManager, SocketDescriptor,
};
use lightning::ln::types::ChannelId;
use lightning::offers::flow;
use lightning::onion_message::messenger::{DefaultMessageRouter, OnionMessenger};
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
use lightning::routing::router::{CandidateRouteHop, DefaultRouter, Path, RouteHop};
Expand Down Expand Up @@ -1178,6 +1179,19 @@ mod tests {
>,
>;

type OffersMessageFlow = flow::OffersMessageFlow<
Arc<KeysManager>,
Arc<ChannelManager>,
Arc<
DefaultMessageRouter<
Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
Arc<test_utils::TestLogger>,
Arc<KeysManager>,
>,
>,
Arc<test_utils::TestLogger>,
>;

type OM = OnionMessenger<
Arc<KeysManager>,
Arc<KeysManager>,
Expand All @@ -1191,7 +1205,7 @@ mod tests {
>,
>,
IgnoringMessageHandler,
Arc<ChannelManager>,
Arc<OffersMessageFlow>,
IgnoringMessageHandler,
IgnoringMessageHandler,
>;
Expand Down Expand Up @@ -1587,14 +1601,23 @@ mod tests {
params,
genesis_block.header.time,
));
let offers_message_flow = Arc::new(OffersMessageFlow::new(
manager.inbound_payment_key,
manager.get_our_node_id(),
keys_manager.clone(),
manager.clone(),
msg_router.clone(),
logger.clone(),
));

let messenger = Arc::new(OnionMessenger::new(
keys_manager.clone(),
keys_manager.clone(),
logger.clone(),
manager.clone(),
msg_router.clone(),
IgnoringMessageHandler {},
manager.clone(),
offers_message_flow,
IgnoringMessageHandler {},
IgnoringMessageHandler {},
));
Expand Down
6 changes: 3 additions & 3 deletions lightning-dns-resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ mod test {
let name = HumanReadableName::from_encoded("[email protected]").unwrap();

// When we get the proof back, override its contents to an offer from nodes[1]
let bs_offer = nodes[1].node.create_offer_builder(None).unwrap().build().unwrap();
let bs_offer = nodes[1].offers_handler.create_offer_builder(None).unwrap().build().unwrap();
nodes[0]
.node
.offers_handler
.testing_dnssec_proof_offer_resolution_override
.lock()
.unwrap()
Expand All @@ -404,7 +404,7 @@ mod test {
let retry = Retry::Attempts(0);
let amt = 42_000;
nodes[0]
.node
.offers_handler
.pay_for_offer_from_human_readable_name(name, amt, payment_id, retry, None, resolvers)
.unwrap();

Expand Down
1 change: 0 additions & 1 deletion lightning-net-tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,6 @@ mod tests {
fn get_chain_hashes(&self) -> Option<Vec<ChainHash>> {
Some(vec![ChainHash::using_genesis_block(Network::Testnet)])
}
fn message_received(&self) {}
}
impl MessageSendEventsProvider for MsgHandler {
fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
Expand Down
68 changes: 6 additions & 62 deletions lightning/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub mod bump_transaction;

pub use bump_transaction::BumpTransactionEvent;

use crate::blinded_path::message::OffersContext;
use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentContext, PaymentContextRef};
use crate::chain::transaction;
use crate::ln::channelmanager::{InterceptId, PaymentId, RecipientOnionFields};
Expand All @@ -27,8 +26,6 @@ use crate::types::features::ChannelTypeFeatures;
use crate::ln::msgs;
use crate::ln::types::ChannelId;
use crate::types::payment::{PaymentPreimage, PaymentHash, PaymentSecret};
use crate::offers::invoice::Bolt12Invoice;
use crate::onion_message::messenger::Responder;
use crate::routing::gossip::NetworkUpdate;
use crate::routing::router::{BlindedTail, Path, RouteHop, RouteParameters};
use crate::sign::SpendableOutputDescriptor;
Expand Down Expand Up @@ -587,6 +584,8 @@ pub enum PaymentFailureReason {
/// An invoice was received that required unknown features.
UnknownRequiredFeatures,
/// A [`Bolt12Invoice`] was not received in a reasonable amount of time.
///
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
InvoiceRequestExpired,
/// An [`InvoiceRequest`] for the payment was rejected by the recipient.
///
Expand Down Expand Up @@ -874,39 +873,6 @@ pub enum Event {
/// Sockets for connecting to the node.
addresses: Vec<msgs::SocketAddress>,
},
/// Indicates a [`Bolt12Invoice`] in response to an [`InvoiceRequest`] or a [`Refund`] was
/// received.
///
/// This event will only be generated if [`UserConfig::manually_handle_bolt12_invoices`] is set.
/// Use [`ChannelManager::send_payment_for_bolt12_invoice`] to pay the invoice or
/// [`ChannelManager::abandon_payment`] to abandon the associated payment. See those docs for
/// further details.
///
/// # Failure Behavior and Persistence
/// This event will eventually be replayed after failures-to-handle (i.e., the event handler
/// returning `Err(ReplayEvent ())`) and will be persisted across restarts.
///
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
/// [`Refund`]: crate::offers::refund::Refund
/// [`UserConfig::manually_handle_bolt12_invoices`]: crate::util::config::UserConfig::manually_handle_bolt12_invoices
/// [`ChannelManager::send_payment_for_bolt12_invoice`]: crate::ln::channelmanager::ChannelManager::send_payment_for_bolt12_invoice
/// [`ChannelManager::abandon_payment`]: crate::ln::channelmanager::ChannelManager::abandon_payment
InvoiceReceived {
/// The `payment_id` associated with payment for the invoice.
payment_id: PaymentId,
/// The invoice to pay.
invoice: Bolt12Invoice,
/// The context of the [`BlindedMessagePath`] used to send the invoice.
///
/// [`BlindedMessagePath`]: crate::blinded_path::message::BlindedMessagePath
context: Option<OffersContext>,
/// A responder for replying with an [`InvoiceError`] if needed.
///
/// `None` if the invoice wasn't sent with a reply path.
///
/// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
responder: Option<Responder>,
},
/// Indicates an outbound payment we made succeeded (i.e. it made it all the way to its target
/// and we got back the payment preimage for it).
///
Expand Down Expand Up @@ -1809,15 +1775,8 @@ impl Writeable for Event {
(0, peer_node_id, required),
});
},
&Event::InvoiceReceived { ref payment_id, ref invoice, ref context, ref responder } => {
41u8.write(writer)?;
write_tlv_fields!(writer, {
(0, payment_id, required),
(2, invoice, required),
(4, context, option),
(6, responder, option),
});
},
// Note: The type number `41u8` was previously used for `Event::InvoiceReceived`, which has now been removed.
// To prevent serialization issues, please avoid reusing `41u8` for any new events.
&Event::FundingTxBroadcastSafe { ref channel_id, ref user_channel_id, ref funding_txo, ref counterparty_node_id, ref former_temporary_channel_id} => {
43u8.write(writer)?;
write_tlv_fields!(writer, {
Expand Down Expand Up @@ -2303,23 +2262,8 @@ impl MaybeReadable for Event {
};
f()
},
41u8 => {
let mut f = || {
_init_and_read_len_prefixed_tlv_fields!(reader, {
(0, payment_id, required),
(2, invoice, required),
(4, context, option),
(6, responder, option),
});
Ok(Some(Event::InvoiceReceived {
payment_id: payment_id.0.unwrap(),
invoice: invoice.0.unwrap(),
context,
responder,
}))
};
f()
},
// Note: The type number `41u8` was previously used for `Event::InvoiceReceived`, which has now been removed.
// To prevent serialization issues, please avoid reusing `41u8` for any new events.
43u8 => {
let mut channel_id = RequiredWrapper(None);
let mut user_channel_id = RequiredWrapper(None);
Expand Down
40 changes: 20 additions & 20 deletions lightning/src/ln/async_payments_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ fn create_static_invoice<T: secp256k1::Signing + secp256k1::Verification>(
)
.unwrap();
let (offer_builder, offer_nonce) = recipient
.node
.offers_handler
.create_async_receive_offer_builder(blinded_paths_to_always_online_node)
.unwrap();
let offer = offer_builder.build().unwrap();
let static_invoice = recipient
.node
.offers_handler
.create_static_invoice_builder(&offer, offer_nonce, relative_expiry)
.unwrap()
.build_and_sign(&secp_ctx)
Expand Down Expand Up @@ -222,12 +222,12 @@ fn static_invoice_unknown_required_features() {
)
.unwrap();
let (offer_builder, nonce) = nodes[2]
.node
.offers_handler
.create_async_receive_offer_builder(blinded_paths_to_always_online_node)
.unwrap();
let offer = offer_builder.build().unwrap();
let static_invoice_unknown_req_features = nodes[2]
.node
.offers_handler
.create_static_invoice_builder(&offer, nonce, None)
.unwrap()
.features_unchecked(Bolt12InvoiceFeatures::unknown())
Expand All @@ -237,7 +237,7 @@ fn static_invoice_unknown_required_features() {
let amt_msat = 5000;
let payment_id = PaymentId([1; 32]);
nodes[0]
.node
.offers_handler
.pay_for_offer(&offer, None, Some(amt_msat), None, payment_id, Retry::Attempts(0), None)
.unwrap();

Expand Down Expand Up @@ -297,7 +297,7 @@ fn ignore_unexpected_static_invoice() {
let amt_msat = 5000;
let payment_id = PaymentId([1; 32]);
nodes[0]
.node
.offers_handler
.pay_for_offer(&offer, None, Some(amt_msat), None, payment_id, Retry::Attempts(0), None)
.unwrap();

Expand Down Expand Up @@ -332,7 +332,7 @@ fn ignore_unexpected_static_invoice() {
nodes[0]
.onion_messenger
.handle_onion_message(nodes[1].node.get_our_node_id(), &unexpected_static_invoice_om);
let async_pmts_msgs = AsyncPaymentsMessageHandler::release_pending_messages(nodes[0].node);
let async_pmts_msgs = nodes[0].offers_handler.release_pending_messages();
assert!(async_pmts_msgs.is_empty());
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());

Expand All @@ -356,7 +356,7 @@ fn ignore_unexpected_static_invoice() {
nodes[0]
.onion_messenger
.handle_onion_message(nodes[1].node.get_our_node_id(), &static_invoice_om);
let async_pmts_msgs = AsyncPaymentsMessageHandler::release_pending_messages(nodes[0].node);
let async_pmts_msgs = nodes[0].offers_handler.release_pending_messages();
assert!(!async_pmts_msgs.is_empty());
assert!(async_pmts_msgs
.into_iter()
Expand All @@ -381,7 +381,7 @@ fn ignore_unexpected_static_invoice() {
nodes[0]
.onion_messenger
.handle_onion_message(nodes[1].node.get_our_node_id(), &dup_static_invoice_om);
let async_pmts_msgs = AsyncPaymentsMessageHandler::release_pending_messages(nodes[0].node);
let async_pmts_msgs = nodes[0].offers_handler.release_pending_messages();
assert!(async_pmts_msgs.is_empty());
}

Expand Down Expand Up @@ -414,7 +414,7 @@ fn async_receive_flow_success() {
let amt_msat = 5000;
let payment_id = PaymentId([1; 32]);
nodes[0]
.node
.offers_handler
.pay_for_offer(&offer, None, Some(amt_msat), None, payment_id, Retry::Attempts(0), None)
.unwrap();
let release_held_htlc_om =
Expand Down Expand Up @@ -462,7 +462,7 @@ fn expired_static_invoice_fail() {
let amt_msat = 5000;
let payment_id = PaymentId([1; 32]);
nodes[0]
.node
.offers_handler
.pay_for_offer(&offer, None, Some(amt_msat), None, payment_id, Retry::Attempts(0), None)
.unwrap();

Expand Down Expand Up @@ -492,7 +492,7 @@ fn expired_static_invoice_fail() {
// Wait until the static invoice expires before providing it to the sender.
let block = create_dummy_block(
nodes[0].best_block_hash(),
nodes[0].node.duration_since_epoch().as_secs() as u32 + INVOICE_EXPIRY_SECS + 1,
nodes[0].offers_handler.duration_since_epoch().as_secs() as u32 + INVOICE_EXPIRY_SECS + 1,
Vec::new(),
);
connect_block(&nodes[0], &block);
Expand Down Expand Up @@ -545,7 +545,7 @@ fn async_receive_mpp() {
let amt_msat = 15_000_000;
let payment_id = PaymentId([1; 32]);
nodes[0]
.node
.offers_handler
.pay_for_offer(&offer, None, Some(amt_msat), None, payment_id, Retry::Attempts(1), None)
.unwrap();
let release_held_htlc_om_3_0 =
Expand Down Expand Up @@ -629,7 +629,7 @@ fn amount_doesnt_match_invreq() {
let amt_msat = 5000;
let payment_id = PaymentId([1; 32]);
nodes[0]
.node
.offers_handler
.pay_for_offer(&offer, None, Some(amt_msat), None, payment_id, Retry::Attempts(1), None)
.unwrap();
let release_held_htlc_om_3_0 =
Expand Down Expand Up @@ -823,7 +823,7 @@ fn invalid_async_receive_with_retry<F1, F2>(
)
.unwrap();
let (offer_builder, offer_nonce) = nodes[2]
.node
.offers_handler
.create_async_receive_offer_builder(blinded_paths_to_always_online_node)
.unwrap();
let offer = offer_builder.build().unwrap();
Expand All @@ -835,7 +835,7 @@ fn invalid_async_receive_with_retry<F1, F2>(
let mut static_invoice_paths = Vec::new();
for _ in 0..3 {
let static_inv_for_path = nodes[2]
.node
.offers_handler
.create_static_invoice_builder(&offer, offer_nonce, None)
.unwrap()
.build_and_sign(&secp_ctx)
Expand All @@ -845,7 +845,7 @@ fn invalid_async_receive_with_retry<F1, F2>(
nodes[2].router.expect_blinded_payment_paths(static_invoice_paths);

let static_invoice = nodes[2]
.node
.offers_handler
.create_static_invoice_builder(&offer, offer_nonce, None)
.unwrap()
.build_and_sign(&secp_ctx)
Expand All @@ -858,7 +858,7 @@ fn invalid_async_receive_with_retry<F1, F2>(
*nodes[0].keys_manager.override_random_bytes.lock().unwrap() = Some(hardcoded_random_bytes);

nodes[0]
.node
.offers_handler
.pay_for_offer(&offer, None, Some(amt_msat), None, payment_id, Retry::Attempts(2), None)
.unwrap();
let release_held_htlc_om_2_0 =
Expand Down Expand Up @@ -947,7 +947,7 @@ fn expired_static_invoice_message_path() {
let amt_msat = 5000;
let payment_id = PaymentId([1; 32]);
nodes[0]
.node
.offers_handler
.pay_for_offer(&offer, None, Some(amt_msat), None, payment_id, Retry::Attempts(1), None)
.unwrap();

Expand Down Expand Up @@ -1051,7 +1051,7 @@ fn expired_static_invoice_payment_path() {
let amt_msat = 5000;
let payment_id = PaymentId([1; 32]);
nodes[0]
.node
.offers_handler
.pay_for_offer(&offer, None, Some(amt_msat), None, payment_id, Retry::Attempts(0), None)
.unwrap();
let release_held_htlc_om =
Expand Down
Loading
Loading