Skip to content

Commit cb5d6d1

Browse files
committed
Move create_blinded_path_using_absolute_expiry to flow.rs
1 parent 05ba9d7 commit cb5d6d1

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, Weight};
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};
@@ -2588,26 +2588,6 @@ const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
25882588
/// many peers we reject new (inbound) connections.
25892589
const MAX_NO_CHANNEL_PEERS: usize = 250;
25902590

2591-
/// The maximum expiration from the current time where an [`Offer`] or [`Refund`] is considered
2592-
/// short-lived, while anything with a greater expiration is considered long-lived.
2593-
///
2594-
/// Using [`OffersMessageFlow::create_offer_builder`] or [`OffersMessageFlow::create_refund_builder`],
2595-
/// will included a [`BlindedMessagePath`] created using:
2596-
/// - [`MessageRouter::create_compact_blinded_paths`] when short-lived, and
2597-
/// - [`MessageRouter::create_blinded_paths`] when long-lived.
2598-
///
2599-
/// [`OffersMessageFlow::create_offer_builder`]: crate::offers::flow::OffersMessageFlow::create_offer_builder
2600-
/// [`OffersMessageFlow::create_refund_builder`]: crate::offers::flow::OffersMessageFlow::create_refund_builder
2601-
///
2602-
///
2603-
/// Using compact [`BlindedMessagePath`]s may provide better privacy as the [`MessageRouter`] could select
2604-
/// more hops. However, since they use short channel ids instead of pubkeys, they are more likely to
2605-
/// become invalid over time as channels are closed. Thus, they are only suitable for short-term use.
2606-
///
2607-
/// [`Offer`]: crate::offers::offer
2608-
/// [`Refund`]: crate::offers::refund
2609-
pub const MAX_SHORT_LIVED_RELATIVE_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24);
2610-
26112591
/// Used by [`ChannelManager::list_recent_payments`] to express the status of recent payments.
26122592
/// These include payments that have yet to find a successful path, or have unresolved HTLCs.
26132593
#[derive(Debug, PartialEq)]
@@ -9566,6 +9546,9 @@ pub trait OffersMessageCommons {
95669546
&self, amount_msats: u64, payment_secret: PaymentSecret, payment_context: PaymentContext
95679547
) -> Result<Vec<BlindedPaymentPath>, ()>;
95689548

9549+
/// Get the vector of peers that can be used for a blinded path
9550+
fn get_peer_for_blinded_path(&self) -> Vec<MessageForwardNode>;
9551+
95699552
/// Verify bolt12 invoice
95709553
fn verify_bolt12_invoice(
95719554
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
@@ -9602,16 +9585,6 @@ pub trait OffersMessageCommons {
96029585
/// Get the current time determined by highest seen timestamp
96039586
fn get_current_blocktime(&self) -> Duration;
96049587

9605-
/// Creates a collection of blinded paths by delegating to [`MessageRouter`] based on
9606-
/// the path's intended lifetime.
9607-
///
9608-
/// Whether or not the path is compact depends on whether the path is short-lived or long-lived,
9609-
/// respectively, based on the given `absolute_expiry` as seconds since the Unix epoch. See
9610-
/// [`MAX_SHORT_LIVED_RELATIVE_EXPIRY`].
9611-
fn create_blinded_paths_using_absolute_expiry(
9612-
&self, context: OffersContext, absolute_expiry: Option<Duration>,
9613-
) -> Result<Vec<BlindedMessagePath>, ()>;
9614-
96159588
/// Get the [`ChainHash`] of the chain
96169589
fn get_chain_hash(&self) -> ChainHash;
96179590

@@ -9705,6 +9678,23 @@ where
97059678
)
97069679
}
97079680

9681+
fn get_peer_for_blinded_path(&self) -> Vec<MessageForwardNode> {
9682+
self.per_peer_state.read().unwrap()
9683+
.iter()
9684+
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
9685+
.filter(|(_, peer)| peer.is_connected)
9686+
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9687+
.map(|(node_id, peer)| MessageForwardNode {
9688+
node_id: *node_id,
9689+
short_channel_id: peer.channel_by_id
9690+
.iter()
9691+
.filter(|(_, channel)| channel.context().is_usable())
9692+
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
9693+
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
9694+
})
9695+
.collect::<Vec<_>>()
9696+
}
9697+
97089698
fn verify_bolt12_invoice(
97099699
&self, invoice: &Bolt12Invoice, context: Option<&OffersContext>,
97109700
) -> Result<PaymentId, ()> {
@@ -9809,19 +9799,6 @@ where
98099799
Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64)
98109800
}
98119801

9812-
fn create_blinded_paths_using_absolute_expiry(
9813-
&self, context: OffersContext, absolute_expiry: Option<Duration>,
9814-
) -> Result<Vec<BlindedMessagePath>, ()> {
9815-
let now = self.duration_since_epoch();
9816-
let max_short_lived_absolute_expiry = now.saturating_add(MAX_SHORT_LIVED_RELATIVE_EXPIRY);
9817-
9818-
if absolute_expiry.unwrap_or(Duration::MAX) <= max_short_lived_absolute_expiry {
9819-
self.create_compact_blinded_paths(context)
9820-
} else {
9821-
self.create_blinded_paths(MessageContext::Offers(context))
9822-
}
9823-
}
9824-
98259802
fn get_chain_hash(&self) -> ChainHash {
98269803
self.chain_hash
98279804
}
@@ -9935,47 +9912,6 @@ where
99359912
inbound_payment::get_payment_preimage(payment_hash, payment_secret, &self.inbound_payment_key)
99369913
}
99379914

9938-
pub(super) fn duration_since_epoch(&self) -> Duration {
9939-
#[cfg(not(feature = "std"))]
9940-
let now = Duration::from_secs(
9941-
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9942-
);
9943-
#[cfg(feature = "std")]
9944-
let now = std::time::SystemTime::now()
9945-
.duration_since(std::time::SystemTime::UNIX_EPOCH)
9946-
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
9947-
9948-
now
9949-
}
9950-
9951-
/// Creates a collection of blinded paths by delegating to
9952-
/// [`MessageRouter::create_compact_blinded_paths`].
9953-
///
9954-
/// Errors if the `MessageRouter` errors.
9955-
fn create_compact_blinded_paths(&self, context: OffersContext) -> Result<Vec<BlindedMessagePath>, ()> {
9956-
let recipient = self.get_our_node_id();
9957-
let secp_ctx = &self.secp_ctx;
9958-
9959-
let peers = self.per_peer_state.read().unwrap()
9960-
.iter()
9961-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
9962-
.filter(|(_, peer)| peer.is_connected)
9963-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9964-
.map(|(node_id, peer)| MessageForwardNode {
9965-
node_id: *node_id,
9966-
short_channel_id: peer.channel_by_id
9967-
.iter()
9968-
.filter(|(_, channel)| channel.context().is_usable())
9969-
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
9970-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
9971-
})
9972-
.collect::<Vec<_>>();
9973-
9974-
self.message_router
9975-
.create_compact_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx)
9976-
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
9977-
}
9978-
99799915
/// Gets a fake short channel id for use in receiving [phantom node payments]. These fake scids
99809916
/// are used when constructing the phantom invoice's route hints.
99819917
///

lightning/src/ln/functional_test_utils.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ type TestChannelManager<'node_cfg, 'chan_mon_cfg> = ChannelManager<
415415
pub type TestOffersMessageFlow<'chan_man, 'node_cfg, 'chan_mon_cfg> = OffersMessageFlow<
416416
&'node_cfg test_utils::TestKeysInterface,
417417
&'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>,
418+
&'node_cfg test_utils::TestMessageRouter<'chan_mon_cfg>,
418419
&'chan_mon_cfg test_utils::TestLogger,
419420
>;
420421

@@ -1194,7 +1195,7 @@ macro_rules! reload_node {
11941195
$new_channelmanager = _reload_node(&$node, $new_config, &chanman_encoded, $monitors_encoded);
11951196

11961197
let offers_handler = $crate::sync::Arc::new($crate::offers::flow::OffersMessageFlow::new(
1197-
$node.keys_manager, &$new_channelmanager, $node.logger
1198+
$node.keys_manager, &$new_channelmanager, $node.message_router, $node.logger
11981199
));
11991200

12001201
$node.node = &$new_channelmanager;
@@ -3357,7 +3358,7 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
33573358
for i in 0..node_count {
33583359
let dedicated_entropy = DedicatedEntropy(RandomBytes::new([i as u8; 32]));
33593360
let offers_handler = Arc::new(OffersMessageFlow::new(
3360-
cfgs[i].keys_manager, &chan_mgrs[i], cfgs[i].logger
3361+
cfgs[i].keys_manager, &chan_mgrs[i], &cfgs[i].message_router, cfgs[i].logger
33613362
));
33623363
#[cfg(feature = "dnssec")]
33633364
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)