Skip to content

Commit 40ebff2

Browse files
committed
Move create_blinded_path_using_absolute_expiry to flow.rs
1 parent d1446f9 commit 40ebff2

File tree

5 files changed

+152
-115
lines changed

5 files changed

+152
-115
lines changed

lightning/src/ln/blinded_payment_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ fn blinded_keysend() {
12271227
&nodes[2].keys_manager.get_inbound_payment_key_material()
12281228
);
12291229
let payment_secret = inbound_payment::create_for_spontaneous_payment(
1230-
&inbound_payment_key, None, u32::MAX, nodes[2].node.duration_since_epoch().as_secs(), None
1230+
&inbound_payment_key, None, u32::MAX, nodes[2].offers_handler.duration_since_epoch().as_secs(), None
12311231
).unwrap();
12321232

12331233
let amt_msat = 5000;
@@ -1270,7 +1270,7 @@ fn blinded_mpp_keysend() {
12701270
&nodes[3].keys_manager.get_inbound_payment_key_material()
12711271
);
12721272
let payment_secret = inbound_payment::create_for_spontaneous_payment(
1273-
&inbound_payment_key, None, u32::MAX, nodes[3].node.duration_since_epoch().as_secs(), None
1273+
&inbound_payment_key, None, u32::MAX, nodes[3].offers_handler.duration_since_epoch().as_secs(), None
12741274
).unwrap();
12751275

12761276
let amt_msat = 15_000_000;

lightning/src/ln/channelmanager.rs

Lines changed: 22 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ use bitcoin::secp256k1::Secp256k1;
3333
use bitcoin::{secp256k1, Sequence};
3434

3535
use crate::events::FundingInfo;
36-
use crate::blinded_path::message::{AsyncPaymentsContext, MessageContext, OffersContext};
36+
use crate::blinded_path::message::{AsyncPaymentsContext, MessageContext, MessageForwardNode, OffersContext};
3737
use crate::blinded_path::NodeIdLookUp;
38-
use crate::blinded_path::message::{BlindedMessagePath, MessageForwardNode};
38+
use crate::blinded_path::message::BlindedMessagePath;
3939
use crate::blinded_path::payment::{BlindedPaymentPath, PaymentConstraints, PaymentContext, ReceiveTlvs};
4040
use crate::chain;
4141
use crate::chain::{Confirm, ChannelMonitorUpdateStatus, Watch, BestBlock};
@@ -2575,26 +2575,6 @@ const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
25752575
/// many peers we reject new (inbound) connections.
25762576
const MAX_NO_CHANNEL_PEERS: usize = 250;
25772577

2578-
/// The maximum expiration from the current time where an [`Offer`] or [`Refund`] is considered
2579-
/// short-lived, while anything with a greater expiration is considered long-lived.
2580-
///
2581-
/// Using [`OffersMessageFlow::create_offer_builder`] or [`OffersMessageFlow::create_refund_builder`],
2582-
/// will included a [`BlindedMessagePath`] created using:
2583-
/// - [`MessageRouter::create_compact_blinded_paths`] when short-lived, and
2584-
/// - [`MessageRouter::create_blinded_paths`] when long-lived.
2585-
///
2586-
/// [`OffersMessageFlow::create_offer_builder`]: crate::offers::flow::OffersMessageFlow::create_offer_builder
2587-
/// [`OffersMessageFlow::create_refund_builder`]: crate::offers::flow::OffersMessageFlow::create_refund_builder
2588-
///
2589-
///
2590-
/// Using compact [`BlindedMessagePath`]s may provide better privacy as the [`MessageRouter`] could select
2591-
/// more hops. However, since they use short channel ids instead of pubkeys, they are more likely to
2592-
/// become invalid over time as channels are closed. Thus, they are only suitable for short-term use.
2593-
///
2594-
/// [`Offer`]: crate::offers::offer
2595-
/// [`Refund`]: crate::offers::refund
2596-
pub const MAX_SHORT_LIVED_RELATIVE_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24);
2597-
25982578
/// Used by [`ChannelManager::list_recent_payments`] to express the status of recent payments.
25992579
/// These include payments that have yet to find a successful path, or have unresolved HTLCs.
26002580
#[derive(Debug, PartialEq)]
@@ -9255,6 +9235,9 @@ pub trait OffersMessageCommons {
92559235
&self, amount_msats: u64, payment_secret: PaymentSecret, payment_context: PaymentContext
92569236
) -> Result<Vec<BlindedPaymentPath>, ()>;
92579237

9238+
/// Get the vector of peers that can be used for a blinded path
9239+
fn get_peer_for_blinded_path(&self) -> Vec<MessageForwardNode>;
9240+
92589241
/// Verify bolt12 invoice
92599242
fn verify_bolt12_invoice(
92609243
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
@@ -9291,16 +9274,6 @@ pub trait OffersMessageCommons {
92919274
/// Get the current time determined by highest seen timestamp
92929275
fn get_current_blocktime(&self) -> Duration;
92939276

9294-
/// Creates a collection of blinded paths by delegating to [`MessageRouter`] based on
9295-
/// the path's intended lifetime.
9296-
///
9297-
/// Whether or not the path is compact depends on whether the path is short-lived or long-lived,
9298-
/// respectively, based on the given `absolute_expiry` as seconds since the Unix epoch. See
9299-
/// [`MAX_SHORT_LIVED_RELATIVE_EXPIRY`].
9300-
fn create_blinded_paths_using_absolute_expiry(
9301-
&self, context: OffersContext, absolute_expiry: Option<Duration>,
9302-
) -> Result<Vec<BlindedMessagePath>, ()>;
9303-
93049277
/// Get the [`ChainHash`] of the chain
93059278
fn get_chain_hash(&self) -> ChainHash;
93069279

@@ -9394,6 +9367,23 @@ where
93949367
)
93959368
}
93969369

9370+
fn get_peer_for_blinded_path(&self) -> Vec<MessageForwardNode> {
9371+
self.per_peer_state.read().unwrap()
9372+
.iter()
9373+
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
9374+
.filter(|(_, peer)| peer.is_connected)
9375+
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9376+
.map(|(node_id, peer)| MessageForwardNode {
9377+
node_id: *node_id,
9378+
short_channel_id: peer.channel_by_id
9379+
.iter()
9380+
.filter(|(_, channel)| channel.context().is_usable())
9381+
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
9382+
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
9383+
})
9384+
.collect::<Vec<_>>()
9385+
}
9386+
93979387
fn verify_bolt12_invoice(
93989388
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
93999389
) -> Result<PaymentId, ()> {
@@ -9498,19 +9488,6 @@ where
94989488
Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64)
94999489
}
95009490

9501-
fn create_blinded_paths_using_absolute_expiry(
9502-
&self, context: OffersContext, absolute_expiry: Option<Duration>,
9503-
) -> Result<Vec<BlindedMessagePath>, ()> {
9504-
let now = self.duration_since_epoch();
9505-
let max_short_lived_absolute_expiry = now.saturating_add(MAX_SHORT_LIVED_RELATIVE_EXPIRY);
9506-
9507-
if absolute_expiry.unwrap_or(Duration::MAX) <= max_short_lived_absolute_expiry {
9508-
self.create_compact_blinded_paths(context)
9509-
} else {
9510-
self.create_blinded_paths(MessageContext::Offers(context))
9511-
}
9512-
}
9513-
95149491
fn get_chain_hash(&self) -> ChainHash {
95159492
self.chain_hash
95169493
}
@@ -9624,47 +9601,6 @@ where
96249601
inbound_payment::get_payment_preimage(payment_hash, payment_secret, &self.inbound_payment_key)
96259602
}
96269603

9627-
pub(super) fn duration_since_epoch(&self) -> Duration {
9628-
#[cfg(not(feature = "std"))]
9629-
let now = Duration::from_secs(
9630-
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9631-
);
9632-
#[cfg(feature = "std")]
9633-
let now = std::time::SystemTime::now()
9634-
.duration_since(std::time::SystemTime::UNIX_EPOCH)
9635-
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
9636-
9637-
now
9638-
}
9639-
9640-
/// Creates a collection of blinded paths by delegating to
9641-
/// [`MessageRouter::create_compact_blinded_paths`].
9642-
///
9643-
/// Errors if the `MessageRouter` errors.
9644-
fn create_compact_blinded_paths(&self, context: OffersContext) -> Result<Vec<BlindedMessagePath>, ()> {
9645-
let recipient = self.get_our_node_id();
9646-
let secp_ctx = &self.secp_ctx;
9647-
9648-
let peers = self.per_peer_state.read().unwrap()
9649-
.iter()
9650-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
9651-
.filter(|(_, peer)| peer.is_connected)
9652-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9653-
.map(|(node_id, peer)| MessageForwardNode {
9654-
node_id: *node_id,
9655-
short_channel_id: peer.channel_by_id
9656-
.iter()
9657-
.filter(|(_, channel)| channel.context().is_usable())
9658-
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
9659-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
9660-
})
9661-
.collect::<Vec<_>>();
9662-
9663-
self.message_router
9664-
.create_compact_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx)
9665-
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
9666-
}
9667-
96689604
/// Gets a fake short channel id for use in receiving [phantom node payments]. These fake scids
96699605
/// are used when constructing the phantom invoice's route hints.
96709606
///

lightning/src/ln/functional_test_utils.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ type TestChannelManager<'node_cfg, 'chan_mon_cfg> = ChannelManager<
412412
pub type TestOffersMessageFlow<'chan_man, 'node_cfg, 'chan_mon_cfg> = OffersMessageFlow<
413413
&'node_cfg test_utils::TestKeysInterface,
414414
&'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>,
415+
&'node_cfg test_utils::TestMessageRouter<'chan_mon_cfg>,
415416
&'chan_mon_cfg test_utils::TestLogger,
416417
>;
417418

@@ -1191,7 +1192,7 @@ macro_rules! reload_node {
11911192
$new_channelmanager = _reload_node(&$node, $new_config, &chanman_encoded, $monitors_encoded);
11921193

11931194
let offers_handler = $crate::sync::Arc::new($crate::offers::flow::OffersMessageFlow::new(
1194-
$node.keys_manager, &$new_channelmanager, $node.logger
1195+
$node.keys_manager, &$new_channelmanager, $node.message_router, $node.logger
11951196
));
11961197

11971198
$node.node = &$new_channelmanager;
@@ -3323,7 +3324,7 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
33233324
for i in 0..node_count {
33243325
let dedicated_entropy = DedicatedEntropy(RandomBytes::new([i as u8; 32]));
33253326
let offers_handler = Arc::new(OffersMessageFlow::new(
3326-
cfgs[i].keys_manager, &chan_mgrs[i], cfgs[i].logger
3327+
cfgs[i].keys_manager, &chan_mgrs[i], &cfgs[i].message_router, cfgs[i].logger
33273328
));
33283329
#[cfg(feature = "dnssec")]
33293330
let onion_messenger = OnionMessenger::new(

lightning/src/ln/offers_tests.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ use crate::blinded_path::message::BlindedMessagePath;
4848
use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentContext};
4949
use crate::blinded_path::message::{MessageContext, OffersContext};
5050
use crate::events::{ClosureReason, Event, MessageSendEventsProvider, PaymentFailureReason, PaymentPurpose};
51-
use crate::ln::channelmanager::{Bolt12PaymentError, MAX_SHORT_LIVED_RELATIVE_EXPIRY, OffersMessageCommons, PaymentId, RecentPaymentDetails, Retry, self};
51+
use crate::ln::channelmanager::{Bolt12PaymentError, OffersMessageCommons, PaymentId, RecentPaymentDetails, Retry, self};
5252
use crate::types::features::Bolt12InvoiceFeatures;
5353
use crate::ln::functional_test_utils::*;
5454
use crate::ln::inbound_payment::ExpandedKey;
5555
use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
5656
use crate::ln::outbound_payment::IDEMPOTENCY_TIMEOUT_TICKS;
57+
use crate::offers::flow::MAX_SHORT_LIVED_RELATIVE_EXPIRY;
5758
use crate::offers::invoice::Bolt12Invoice;
5859
use crate::offers::invoice_error::InvoiceError;
5960
use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestFields};
@@ -389,7 +390,7 @@ fn creates_short_lived_offer() {
389390
let alice_id = alice.node.get_our_node_id();
390391
let bob = &nodes[1];
391392

392-
let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
393+
let absolute_expiry = alice.offers_handler.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
393394
let offer = alice.offers_handler
394395
.create_offer_builder(Some(absolute_expiry)).unwrap()
395396
.build().unwrap();
@@ -415,7 +416,7 @@ fn creates_long_lived_offer() {
415416
let alice = &nodes[0];
416417
let alice_id = alice.node.get_our_node_id();
417418

418-
let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
419+
let absolute_expiry = alice.offers_handler.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
419420
+ Duration::from_secs(1);
420421
let offer = alice.offers_handler
421422
.create_offer_builder(Some(absolute_expiry))
@@ -451,7 +452,7 @@ fn creates_short_lived_refund() {
451452
let bob = &nodes[1];
452453
let bob_id = bob.node.get_our_node_id();
453454

454-
let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
455+
let absolute_expiry = bob.offers_handler.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
455456
let payment_id = PaymentId([1; 32]);
456457
let refund = bob.offers_handler
457458
.create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
@@ -479,7 +480,7 @@ fn creates_long_lived_refund() {
479480
let bob = &nodes[1];
480481
let bob_id = bob.node.get_our_node_id();
481482

482-
let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
483+
let absolute_expiry = bob.offers_handler.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
483484
+ Duration::from_secs(1);
484485
let payment_id = PaymentId([1; 32]);
485486
let refund = bob.offers_handler
@@ -1684,7 +1685,7 @@ fn fails_creating_or_paying_for_offer_without_connected_peers() {
16841685
disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
16851686
disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
16861687

1687-
let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
1688+
let absolute_expiry = alice.offers_handler.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
16881689
match alice.offers_handler.create_offer_builder(Some(absolute_expiry)) {
16891690
Ok(_) => panic!("Expected error"),
16901691
Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
@@ -1755,7 +1756,7 @@ fn fails_creating_refund_or_sending_invoice_without_connected_peers() {
17551756
disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
17561757
disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
17571758

1758-
let absolute_expiry = david.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
1759+
let absolute_expiry = david.offers_handler.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
17591760
let payment_id = PaymentId([1; 32]);
17601761
match david.offers_handler.create_refund_builder(
17611762
10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
@@ -2221,7 +2222,7 @@ fn fails_paying_invoice_with_unknown_required_features() {
22212222
let expanded_key = ExpandedKey::new(&alice.keys_manager.get_inbound_payment_key_material());
22222223
let secp_ctx = Secp256k1::new();
22232224

2224-
let created_at = alice.node.duration_since_epoch();
2225+
let created_at = alice.offers_handler.duration_since_epoch();
22252226
let invoice = invoice_request
22262227
.verify_using_recipient_data(nonce, &expanded_key, &secp_ctx).unwrap()
22272228
.respond_using_derived_keys_no_std(payment_paths, payment_hash, created_at).unwrap()

0 commit comments

Comments
 (0)