Skip to content

Commit 65771da

Browse files
committed
Look up short channel ids for BlindedPath
MessageRouter implementations need a mechanism for looking up a short channel id for a channel counterparty when forming a compact blinded path. Add a ShortChannelIdLookUp trait for this purpose and such a parameter to MessageRouter::crate_blinded_path. Implement the trait for ChannelManager such that it can be passed to its MessageRouter. Also, update DefaultMessageRouter to perform the lookup.
1 parent 73202a1 commit 65771da

File tree

10 files changed

+121
-31
lines changed

10 files changed

+121
-31
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
3030
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
3131
use bitcoin::hash_types::{BlockHash, WPubkeyHash};
3232

33-
use lightning::blinded_path::BlindedPath;
33+
use lightning::blinded_path::{BlindedPath, ShortChannelIdLookUp};
3434
use lightning::blinded_path::payment::ReceiveTlvs;
3535
use lightning::chain;
3636
use lightning::chain::{BestBlock, ChannelMonitorUpdateStatus, chainmonitor, channelmonitor, Confirm, Watch};
@@ -118,8 +118,12 @@ impl MessageRouter for FuzzRouter {
118118
unreachable!()
119119
}
120120

121-
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
122-
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
121+
fn create_blinded_paths<
122+
SL: ShortChannelIdLookUp,
123+
T: secp256k1::Signing + secp256k1::Verification
124+
>(
125+
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _scid_lookup: &SL,
126+
_secp_ctx: &Secp256k1<T>,
123127
) -> Result<Vec<BlindedPath>, ()> {
124128
unreachable!()
125129
}

fuzz/src/full_stack.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
2727
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
2828
use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};
2929

30-
use lightning::blinded_path::BlindedPath;
30+
use lightning::blinded_path::{BlindedPath, ShortChannelIdLookUp};
3131
use lightning::blinded_path::payment::ReceiveTlvs;
3232
use lightning::chain;
3333
use lightning::chain::{BestBlock, ChannelMonitorUpdateStatus, Confirm, Listen};
@@ -156,8 +156,12 @@ impl MessageRouter for FuzzRouter {
156156
unreachable!()
157157
}
158158

159-
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
160-
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
159+
fn create_blinded_paths<
160+
SL: ShortChannelIdLookUp,
161+
T: secp256k1::Signing + secp256k1::Verification
162+
>(
163+
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _scid_lookup: &SL,
164+
_secp_ctx: &Secp256k1<T>,
161165
) -> Result<Vec<BlindedPath>, ()> {
162166
unreachable!()
163167
}

fuzz/src/invoice_request_deser.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use bitcoin::secp256k1::{KeyPair, Parity, PublicKey, Secp256k1, SecretKey, self}
1111
use crate::utils::test_logger;
1212
use core::convert::TryFrom;
1313
use lightning::blinded_path::BlindedPath;
14+
use lightning::blinded_path::message::ForwardNode;
1415
use lightning::sign::EntropySource;
1516
use lightning::ln::PaymentHash;
1617
use lightning::ln::features::BlindedHopFeatures;
@@ -73,9 +74,19 @@ fn build_response<T: secp256k1::Signing + secp256k1::Verification>(
7374
invoice_request: &InvoiceRequest, secp_ctx: &Secp256k1<T>
7475
) -> Result<UnsignedBolt12Invoice, Bolt12SemanticError> {
7576
let entropy_source = Randomness {};
77+
let intermediate_nodes = [
78+
[
79+
ForwardNode { node_id: pubkey(43), short_channel_id: None },
80+
ForwardNode { node_id: pubkey(44), short_channel_id: None },
81+
],
82+
[
83+
ForwardNode { node_id: pubkey(45), short_channel_id: None },
84+
ForwardNode { node_id: pubkey(46), short_channel_id: None },
85+
],
86+
];
7687
let paths = vec![
77-
BlindedPath::new_for_message(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
78-
BlindedPath::new_for_message(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
88+
BlindedPath::new_for_message(&intermediate_nodes[0], pubkey(42), &entropy_source, secp_ctx).unwrap(),
89+
BlindedPath::new_for_message(&intermediate_nodes[1], pubkey(42), &entropy_source, secp_ctx).unwrap(),
7990
];
8091

8192
let payinfo = vec![

fuzz/src/onion_message.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bitcoin::secp256k1::ecdh::SharedSecret;
66
use bitcoin::secp256k1::ecdsa::RecoverableSignature;
77
use bitcoin::secp256k1::schnorr;
88

9-
use lightning::blinded_path::{BlindedPath, EmptyNodeIdLookUp};
9+
use lightning::blinded_path::{BlindedPath, EmptyNodeIdLookUp, ShortChannelIdLookUp};
1010
use lightning::ln::features::InitFeatures;
1111
use lightning::ln::msgs::{self, DecodeError, OnionMessageHandler};
1212
use lightning::ln::script::ShutdownScript;
@@ -87,8 +87,12 @@ impl MessageRouter for TestMessageRouter {
8787
})
8888
}
8989

90-
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
91-
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
90+
fn create_blinded_paths<
91+
SL: ShortChannelIdLookUp,
92+
T: secp256k1::Signing + secp256k1::Verification
93+
>(
94+
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _scid_lookup: &SL,
95+
_secp_ctx: &Secp256k1<T>,
9296
) -> Result<Vec<BlindedPath>, ()> {
9397
unreachable!()
9498
}

fuzz/src/refund_deser.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey, self};
1111
use crate::utils::test_logger;
1212
use core::convert::TryFrom;
1313
use lightning::blinded_path::BlindedPath;
14+
use lightning::blinded_path::message::ForwardNode;
1415
use lightning::sign::EntropySource;
1516
use lightning::ln::PaymentHash;
1617
use lightning::ln::features::BlindedHopFeatures;
@@ -62,9 +63,19 @@ fn build_response<T: secp256k1::Signing + secp256k1::Verification>(
6263
refund: &Refund, signing_pubkey: PublicKey, secp_ctx: &Secp256k1<T>
6364
) -> Result<UnsignedBolt12Invoice, Bolt12SemanticError> {
6465
let entropy_source = Randomness {};
66+
let intermediate_nodes = [
67+
[
68+
ForwardNode { node_id: pubkey(43), short_channel_id: None },
69+
ForwardNode { node_id: pubkey(44), short_channel_id: None },
70+
],
71+
[
72+
ForwardNode { node_id: pubkey(45), short_channel_id: None },
73+
ForwardNode { node_id: pubkey(46), short_channel_id: None },
74+
],
75+
];
6576
let paths = vec![
66-
BlindedPath::new_for_message(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
67-
BlindedPath::new_for_message(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
77+
BlindedPath::new_for_message(&intermediate_nodes[0], pubkey(42), &entropy_source, secp_ctx).unwrap(),
78+
BlindedPath::new_for_message(&intermediate_nodes[1], pubkey(42), &entropy_source, secp_ctx).unwrap(),
6879
];
6980

7081
let payinfo = vec![

lightning/src/blinded_path/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ pub trait NodeIdLookUp {
8080
fn next_node_id(&self, short_channel_id: u64) -> Option<PublicKey>;
8181
}
8282

83+
/// An interface for looking up a short channel id of a channel counterparty for the purpose of
84+
/// receiving an [`OnionMessage`].
85+
///
86+
/// [`OnionMessage`]: crate::ln::msgs::OnionMessage
87+
pub trait ShortChannelIdLookUp {
88+
/// Returns the short channel id of a channel between the recipient node and a channel
89+
/// counterparty with `counterparty_node_id`.
90+
fn next_short_channel_id(&self, counterparty_node_id: PublicKey) -> Option<u64>;
91+
}
92+
8393
/// A [`NodeIdLookUp`] that always returns `None`.
8494
pub struct EmptyNodeIdLookUp {}
8595

lightning/src/ln/channelmanager.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use bitcoin::secp256k1::{SecretKey,PublicKey};
3131
use bitcoin::secp256k1::Secp256k1;
3232
use bitcoin::{secp256k1, Sequence};
3333

34-
use crate::blinded_path::{BlindedPath, NodeIdLookUp};
34+
use crate::blinded_path::{BlindedPath, NodeIdLookUp, ShortChannelIdLookUp};
3535
use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentConstraints, PaymentContext, ReceiveTlvs};
3636
use crate::chain;
3737
use crate::chain::{Confirm, ChannelMonitorUpdateStatus, Watch, BestBlock};
@@ -8990,7 +8990,7 @@ where
89908990
.collect::<Vec<_>>();
89918991

89928992
self.router
8993-
.create_blinded_paths(recipient, peers, secp_ctx)
8993+
.create_blinded_paths(recipient, peers, self, secp_ctx)
89948994
.and_then(|paths| paths.into_iter().next().ok_or(()))
89958995
}
89968996

@@ -10478,6 +10478,31 @@ where
1047810478
}
1047910479
}
1048010480

10481+
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>
10482+
ShortChannelIdLookUp for ChannelManager<M, T, ES, NS, SP, F, R, L>
10483+
where
10484+
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
10485+
T::Target: BroadcasterInterface,
10486+
ES::Target: EntropySource,
10487+
NS::Target: NodeSigner,
10488+
SP::Target: SignerProvider,
10489+
F::Target: FeeEstimator,
10490+
R::Target: Router,
10491+
L::Target: Logger,
10492+
{
10493+
fn next_short_channel_id(&self, counterparty_node_id: PublicKey) -> Option<u64> {
10494+
self.per_peer_state.read().unwrap()
10495+
.get(&counterparty_node_id)
10496+
.map(|peer_state| peer_state.lock().unwrap())
10497+
.and_then(|peer_state|
10498+
peer_state.channel_by_id
10499+
.iter()
10500+
.find(|(_, channel)| channel.context().is_usable())
10501+
.and_then(|(_, channel)| channel.context().get_short_channel_id())
10502+
)
10503+
}
10504+
}
10505+
1048110506
/// Fetches the set of [`NodeFeatures`] flags that are provided by or required by
1048210507
/// [`ChannelManager`].
1048310508
pub(crate) fn provided_node_features(config: &UserConfig) -> NodeFeatures {

lightning/src/onion_message/messenger.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use bitcoin::hashes::hmac::{Hmac, HmacEngine};
1515
use bitcoin::hashes::sha256::Hash as Sha256;
1616
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
1717

18-
use crate::blinded_path::{BlindedPath, IntroductionNode, NodeIdLookUp};
18+
use crate::blinded_path::{BlindedPath, IntroductionNode, NodeIdLookUp, ShortChannelIdLookUp};
1919
use crate::blinded_path::message::{advance_path_by_one, ForwardNode, ForwardTlvs, NextHop, ReceiveTlvs};
2020
use crate::blinded_path::utils;
2121
use crate::events::{Event, EventHandler, EventsProvider};
@@ -70,7 +70,7 @@ pub(super) const MAX_TIMER_TICKS: usize = 2;
7070
/// # use bitcoin::hashes::_export::_core::time::Duration;
7171
/// # use bitcoin::hashes::hex::FromHex;
7272
/// # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey, self};
73-
/// # use lightning::blinded_path::{BlindedPath, EmptyNodeIdLookUp};
73+
/// # use lightning::blinded_path::{BlindedPath, EmptyNodeIdLookUp, ShortChannelIdLookUp};
7474
/// # use lightning::blinded_path::message::ForwardNode;
7575
/// # use lightning::sign::{EntropySource, KeysManager};
7676
/// # use lightning::ln::peer_handler::IgnoringMessageHandler;
@@ -97,8 +97,12 @@ pub(super) const MAX_TIMER_TICKS: usize = 2;
9797
/// # first_node_addresses: None,
9898
/// # })
9999
/// # }
100-
/// # fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
101-
/// # &self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>
100+
/// # fn create_blinded_paths<
101+
/// # SL: ShortChannelIdLookUp,
102+
/// # T: secp256k1::Signing + secp256k1::Verification>
103+
/// # (
104+
/// # &self, _recipient: PublicKey, _peers: Vec<PublicKey>, _scid_lookup: &SL,
105+
/// # _secp_ctx: &Secp256k1<T>
102106
/// # ) -> Result<Vec<BlindedPath>, ()> {
103107
/// # unreachable!()
104108
/// # }
@@ -292,9 +296,11 @@ pub trait MessageRouter {
292296
/// Creates [`BlindedPath`]s to the `recipient` node. The nodes in `peers` are assumed to be
293297
/// direct peers with the `recipient`.
294298
fn create_blinded_paths<
299+
SL: ShortChannelIdLookUp,
295300
T: secp256k1::Signing + secp256k1::Verification
296301
>(
297-
&self, recipient: PublicKey, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
302+
&self, recipient: PublicKey, peers: Vec<PublicKey>, scid_lookup: &SL,
303+
secp_ctx: &Secp256k1<T>,
298304
) -> Result<Vec<BlindedPath>, ()>;
299305
}
300306

@@ -359,9 +365,11 @@ where
359365
}
360366

361367
fn create_blinded_paths<
368+
SL: ShortChannelIdLookUp,
362369
T: secp256k1::Signing + secp256k1::Verification
363370
>(
364-
&self, recipient: PublicKey, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
371+
&self, recipient: PublicKey, peers: Vec<PublicKey>, scid_lookup: &SL,
372+
secp_ctx: &Secp256k1<T>,
365373
) -> Result<Vec<BlindedPath>, ()> {
366374
// Limit the number of blinded paths that are computed.
367375
const MAX_PATHS: usize = 3;
@@ -392,7 +400,12 @@ where
392400
});
393401

394402
let paths = peer_info.into_iter()
395-
.map(|(node_id, _, _)| vec![ForwardNode { node_id, short_channel_id: None }])
403+
.map(|(node_id, _, _)| vec![
404+
ForwardNode {
405+
node_id,
406+
short_channel_id: scid_lookup.next_short_channel_id(node_id),
407+
},
408+
])
396409
.map(|intermediate_nodes| {
397410
BlindedPath::new_for_message(
398411
&intermediate_nodes, recipient, &*self.entropy_source, secp_ctx

lightning/src/routing/router.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
1212
use bitcoin::secp256k1::{PublicKey, Secp256k1, self};
1313

14-
use crate::blinded_path::{BlindedHop, BlindedPath, Direction, IntroductionNode};
14+
use crate::blinded_path::{BlindedHop, BlindedPath, Direction, IntroductionNode, ShortChannelIdLookUp};
1515
use crate::blinded_path::payment::{ForwardNode, ForwardTlvs, PaymentConstraints, PaymentRelay, ReceiveTlvs};
1616
use crate::ln::PaymentHash;
1717
use crate::ln::channelmanager::{ChannelDetails, PaymentId, MIN_FINAL_CLTV_EXPIRY_DELTA};
@@ -168,11 +168,13 @@ impl< G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, ES: Deref, S: Deref,
168168
}
169169

170170
fn create_blinded_paths<
171+
SL: ShortChannelIdLookUp,
171172
T: secp256k1::Signing + secp256k1::Verification
172173
> (
173-
&self, recipient: PublicKey, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
174+
&self, recipient: PublicKey, peers: Vec<PublicKey>, scid_lookup: &SL,
175+
secp_ctx: &Secp256k1<T>,
174176
) -> Result<Vec<BlindedPath>, ()> {
175-
self.message_router.create_blinded_paths(recipient, peers, secp_ctx)
177+
self.message_router.create_blinded_paths(recipient, peers, scid_lookup, secp_ctx)
176178
}
177179
}
178180

lightning/src/util/test_utils.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10-
use crate::blinded_path::BlindedPath;
10+
use crate::blinded_path::{BlindedPath, ShortChannelIdLookUp};
1111
use crate::blinded_path::payment::ReceiveTlvs;
1212
use crate::chain;
1313
use crate::chain::WatchedOutput;
@@ -236,11 +236,13 @@ impl<'a> MessageRouter for TestRouter<'a> {
236236
}
237237

238238
fn create_blinded_paths<
239+
SL: ShortChannelIdLookUp,
239240
T: secp256k1::Signing + secp256k1::Verification
240241
>(
241-
&self, recipient: PublicKey, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
242+
&self, recipient: PublicKey, peers: Vec<PublicKey>, scid_lookup: &SL,
243+
secp_ctx: &Secp256k1<T>,
242244
) -> Result<Vec<BlindedPath>, ()> {
243-
self.router.create_blinded_paths(recipient, peers, secp_ctx)
245+
self.router.create_blinded_paths(recipient, peers, scid_lookup, secp_ctx)
244246
}
245247
}
246248

@@ -272,10 +274,14 @@ impl<'a> MessageRouter for TestMessageRouter<'a> {
272274
self.inner.find_path(sender, peers, destination)
273275
}
274276

275-
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
276-
&self, recipient: PublicKey, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
277+
fn create_blinded_paths<
278+
SL: ShortChannelIdLookUp,
279+
T: secp256k1::Signing + secp256k1::Verification
280+
>(
281+
&self, recipient: PublicKey, peers: Vec<PublicKey>, scid_lookup: &SL,
282+
secp_ctx: &Secp256k1<T>,
277283
) -> Result<Vec<BlindedPath>, ()> {
278-
self.inner.create_blinded_paths(recipient, peers, secp_ctx)
284+
self.inner.create_blinded_paths(recipient, peers, scid_lookup, secp_ctx)
279285
}
280286
}
281287

0 commit comments

Comments
 (0)