use crate::routing::gossip::{NodeId, ReadOnlyNetworkGraph};
use crate::sign::EntropySource;
use crate::util::ser::{Readable, Writeable, Writer};
+use crate::util::scid_utils;
use crate::io;
use crate::prelude::*;
},
}
}
+
+ /// Attempts to a use a compact representation for the [`IntroductionNode`] by using a directed
+ /// short channel id from a channel in `network_graph` leading to the introduction node.
+ ///
+ /// While this may result in a smaller encoding, there is a trade off in that the path may
+ /// become invalid if the channel is closed or hasn't been propagated via gossip. Therefore,
+ /// calling this may not be suitable for long-lived blinded paths.
+ pub fn use_compact_introduction_node(&mut self, network_graph: &ReadOnlyNetworkGraph) {
+ if let IntroductionNode::NodeId(pubkey) = &self.introduction_node {
+ let node_id = NodeId::from_pubkey(pubkey);
+ if let Some(node_info) = network_graph.node(&node_id) {
+ if let Some((scid, channel_info)) = node_info
+ .channels
+ .iter()
+ .filter_map(|scid| network_graph.channel(*scid).map(|info| (*scid, info)))
+ .min_by_key(|(scid, _)| scid_utils::block_from_scid(*scid))
+ {
+ let direction = if node_id == channel_info.node_one {
+ Direction::NodeOne
+ } else {
+ debug_assert_eq!(node_id, channel_info.node_two);
+ Direction::NodeTwo
+ };
+ self.introduction_node =
+ IntroductionNode::DirectedShortChannelId(direction, scid);
+ }
+ }
+ }
+ }
}
impl Writeable for BlindedPath {
//! blinded paths are used.
use bitcoin::network::constants::Network;
+use bitcoin::secp256k1::PublicKey;
use core::time::Duration;
use crate::blinded_path::{BlindedPath, IntroductionNode};
use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentContext};
}
}
+fn resolve_introduction_node<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &BlindedPath) -> PublicKey {
+ path.public_introduction_node_id(&node.network_graph.read_only())
+ .and_then(|node_id| node_id.as_pubkey().ok())
+ .unwrap()
+}
+
fn route_bolt12_payment<'a, 'b, 'c>(
node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
) {
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
- assert_ne!(path.introduction_node, IntroductionNode::NodeId(bob_id));
- assert_ne!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
+ let introduction_node_id = resolve_introduction_node(david, &path);
+ assert_ne!(introduction_node_id, bob_id);
+ assert_ne!(introduction_node_id, charlie_id);
}
// Use a one-hop blinded path when Bob is announced and all his peers are Tor-only.
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
- assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
+ let introduction_node_id = resolve_introduction_node(david, &path);
+ assert_eq!(introduction_node_id, bob_id);
}
}
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
- assert_eq!(path.introduction_node, IntroductionNode::NodeId(nodes[4].node.get_our_node_id()));
+ let introduction_node_id = resolve_introduction_node(david, &path);
+ assert_eq!(introduction_node_id, nodes[4].node.get_our_node_id());
}
}
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
- assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
+ let introduction_node_id = resolve_introduction_node(david, &path);
+ assert_eq!(introduction_node_id, bob_id);
+ assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
}
let payment_id = PaymentId([1; 32]);
payer_note_truncated: None,
},
});
+ let introduction_node_id = resolve_introduction_node(alice, &reply_path);
assert_eq!(invoice_request.amount_msats(), None);
assert_ne!(invoice_request.payer_id(), david_id);
- assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(charlie_id));
+ assert_eq!(introduction_node_id, charlie_id);
+ assert!(matches!(reply_path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
assert_ne!(refund.payer_id(), david_id);
assert!(!refund.paths().is_empty());
for path in refund.paths() {
- assert_eq!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
+ let introduction_node_id = resolve_introduction_node(alice, &path);
+ assert_eq!(introduction_node_id, charlie_id);
+ assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
}
expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
- assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
+ let introduction_node_id = resolve_introduction_node(bob, &path);
+ assert_eq!(introduction_node_id, alice_id);
+ assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
}
let payment_id = PaymentId([1; 32]);
payer_note_truncated: None,
},
});
+ let introduction_node_id = resolve_introduction_node(alice, &reply_path);
assert_eq!(invoice_request.amount_msats(), None);
assert_ne!(invoice_request.payer_id(), bob_id);
- assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(bob_id));
+ assert_eq!(introduction_node_id, bob_id);
+ assert!(matches!(reply_path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
assert_ne!(refund.payer_id(), bob_id);
assert!(!refund.paths().is_empty());
for path in refund.paths() {
- assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
+ let introduction_node_id = resolve_introduction_node(alice, &path);
+ assert_eq!(introduction_node_id, bob_id);
+ assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
}
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);