Skip to content

Commit 31b32c5

Browse files
Merge pull request #3578 from tnull/2025-01-rustfmt-blinded-path
` rustfmt`: Run on `blinded_path`
2 parents 93e2da4 + 996eb2a commit 31b32c5

File tree

5 files changed

+433
-318
lines changed

5 files changed

+433
-318
lines changed

lightning/src/blinded_path/message.rs

+51-32
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
1414
#[allow(unused_imports)]
1515
use crate::prelude::*;
1616

17-
use bitcoin::hashes::hmac::Hmac;
18-
use bitcoin::hashes::sha256::Hash as Sha256;
19-
use crate::blinded_path::{BlindedHop, BlindedPath, Direction, IntroductionNode, NodeIdLookUp};
2017
use crate::blinded_path::utils;
18+
use crate::blinded_path::{BlindedHop, BlindedPath, Direction, IntroductionNode, NodeIdLookUp};
19+
use crate::crypto::streams::ChaChaPolyReadAdapter;
2120
use crate::io;
2221
use crate::io::Cursor;
2322
use crate::ln::channelmanager::PaymentId;
2423
use crate::ln::msgs::DecodeError;
2524
use crate::ln::onion_utils;
26-
use crate::types::payment::PaymentHash;
2725
use crate::offers::nonce::Nonce;
2826
use crate::onion_message::packet::ControlTlvs;
2927
use crate::routing::gossip::{NodeId, ReadOnlyNetworkGraph};
3028
use crate::sign::{EntropySource, NodeSigner, Recipient};
31-
use crate::crypto::streams::ChaChaPolyReadAdapter;
29+
use crate::types::payment::PaymentHash;
3230
use crate::util::scid_utils;
3331
use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, Writeable, Writer};
32+
use bitcoin::hashes::hmac::Hmac;
33+
use bitcoin::hashes::sha256::Hash as Sha256;
3434

3535
use core::mem;
3636
use core::ops::Deref;
@@ -55,8 +55,12 @@ impl Readable for BlindedMessagePath {
5555
impl BlindedMessagePath {
5656
/// Create a one-hop blinded path for a message.
5757
pub fn one_hop<ES: Deref, T: secp256k1::Signing + secp256k1::Verification>(
58-
recipient_node_id: PublicKey, context: MessageContext, entropy_source: ES, secp_ctx: &Secp256k1<T>
59-
) -> Result<Self, ()> where ES::Target: EntropySource {
58+
recipient_node_id: PublicKey, context: MessageContext, entropy_source: ES,
59+
secp_ctx: &Secp256k1<T>,
60+
) -> Result<Self, ()>
61+
where
62+
ES::Target: EntropySource,
63+
{
6064
Self::new(&[], recipient_node_id, context, entropy_source, secp_ctx)
6165
}
6266

@@ -68,20 +72,28 @@ impl BlindedMessagePath {
6872
pub fn new<ES: Deref, T: secp256k1::Signing + secp256k1::Verification>(
6973
intermediate_nodes: &[MessageForwardNode], recipient_node_id: PublicKey,
7074
context: MessageContext, entropy_source: ES, secp_ctx: &Secp256k1<T>,
71-
) -> Result<Self, ()> where ES::Target: EntropySource {
75+
) -> Result<Self, ()>
76+
where
77+
ES::Target: EntropySource,
78+
{
7279
let introduction_node = IntroductionNode::NodeId(
73-
intermediate_nodes.first().map_or(recipient_node_id, |n| n.node_id)
80+
intermediate_nodes.first().map_or(recipient_node_id, |n| n.node_id),
7481
);
7582
let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
76-
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
83+
let blinding_secret =
84+
SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
7785

7886
Ok(Self(BlindedPath {
7987
introduction_node,
8088
blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
8189
blinded_hops: blinded_hops(
82-
secp_ctx, intermediate_nodes, recipient_node_id,
83-
context, &blinding_secret,
84-
).map_err(|_| ())?,
90+
secp_ctx,
91+
intermediate_nodes,
92+
recipient_node_id,
93+
context,
94+
&blinding_secret,
95+
)
96+
.map_err(|_| ())?,
8597
}))
8698
}
8799

@@ -97,9 +109,9 @@ impl BlindedMessagePath {
97109
if let Some(node_info) = network_graph.node(&node_id) {
98110
if let Some((scid, channel_info)) = node_info
99111
.channels
100-
.iter()
101-
.filter_map(|scid| network_graph.channel(*scid).map(|info| (*scid, info)))
102-
.min_by_key(|(scid, _)| scid_utils::block_from_scid(*scid))
112+
.iter()
113+
.filter_map(|scid| network_graph.channel(*scid).map(|info| (*scid, info)))
114+
.min_by_key(|(scid, _)| scid_utils::block_from_scid(*scid))
103115
{
104116
let direction = if node_id == channel_info.node_one {
105117
Direction::NodeOne
@@ -117,7 +129,7 @@ impl BlindedMessagePath {
117129
/// Returns the introduction [`NodeId`] of the blinded path, if it is publicly reachable (i.e.,
118130
/// it is found in the network graph).
119131
pub fn public_introduction_node_id<'a>(
120-
&self, network_graph: &'a ReadOnlyNetworkGraph
132+
&self, network_graph: &'a ReadOnlyNetworkGraph,
121133
) -> Option<&'a NodeId> {
122134
self.0.public_introduction_node_id(network_graph)
123135
}
@@ -144,7 +156,7 @@ impl BlindedMessagePath {
144156
///
145157
/// Will only modify `self` when returning `Ok`.
146158
pub fn advance_path_by_one<NS: Deref, NL: Deref, T>(
147-
&mut self, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
159+
&mut self, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>,
148160
) -> Result<(), ()>
149161
where
150162
NS::Target: NodeSigner,
@@ -158,28 +170,31 @@ impl BlindedMessagePath {
158170
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
159171
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
160172
Ok(ChaChaPolyReadAdapter {
161-
readable: ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override })
173+
readable: ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override }),
162174
}) => {
163175
let next_node_id = match next_hop {
164176
NextMessageHop::NodeId(pubkey) => pubkey,
165-
NextMessageHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid) {
177+
NextMessageHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid)
178+
{
166179
Some(pubkey) => pubkey,
167180
None => return Err(()),
168181
},
169182
};
170183
let mut new_blinding_point = match next_blinding_override {
171184
Some(blinding_point) => blinding_point,
172-
None => {
173-
onion_utils::next_hop_pubkey(secp_ctx, self.0.blinding_point,
174-
control_tlvs_ss.as_ref()).map_err(|_| ())?
175-
}
185+
None => onion_utils::next_hop_pubkey(
186+
secp_ctx,
187+
self.0.blinding_point,
188+
control_tlvs_ss.as_ref(),
189+
)
190+
.map_err(|_| ())?,
176191
};
177192
mem::swap(&mut self.0.blinding_point, &mut new_blinding_point);
178193
self.0.introduction_node = IntroductionNode::NodeId(next_node_id);
179194
self.0.blinded_hops.remove(0);
180195
Ok(())
181196
},
182-
_ => Err(())
197+
_ => Err(()),
183198
}
184199
}
185200

@@ -189,7 +204,7 @@ impl BlindedMessagePath {
189204

190205
#[cfg(test)]
191206
pub fn from_raw(
192-
introduction_node_id: PublicKey, blinding_point: PublicKey, blinded_hops: Vec<BlindedHop>
207+
introduction_node_id: PublicKey, blinding_point: PublicKey, blinded_hops: Vec<BlindedHop>,
193208
) -> Self {
194209
Self(BlindedPath {
195210
introduction_node: IntroductionNode::NodeId(introduction_node_id),
@@ -241,7 +256,7 @@ pub(crate) struct ReceiveTlvs {
241256
/// If `context` is `Some`, it is used to identify the blinded path that this onion message is
242257
/// sending to. This is useful for receivers to check that said blinded path is being used in
243258
/// the right context.
244-
pub context: Option<MessageContext>
259+
pub context: Option<MessageContext>,
245260
}
246261

247262
impl Writeable for ForwardTlvs {
@@ -480,20 +495,24 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
480495
secp_ctx: &Secp256k1<T>, intermediate_nodes: &[MessageForwardNode],
481496
recipient_node_id: PublicKey, context: MessageContext, session_priv: &SecretKey,
482497
) -> Result<Vec<BlindedHop>, secp256k1::Error> {
483-
let pks = intermediate_nodes.iter().map(|node| node.node_id)
498+
let pks = intermediate_nodes
499+
.iter()
500+
.map(|node| node.node_id)
484501
.chain(core::iter::once(recipient_node_id));
485-
let tlvs = pks.clone()
502+
let tlvs = pks
503+
.clone()
486504
.skip(1) // The first node's TLVs contains the next node's pubkey
487505
.zip(intermediate_nodes.iter().map(|node| node.short_channel_id))
488506
.map(|(pubkey, scid)| match scid {
489507
Some(scid) => NextMessageHop::ShortChannelId(scid),
490508
None => NextMessageHop::NodeId(pubkey),
491509
})
492-
.map(|next_hop| ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None }))
493-
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs{ context: Some(context) })));
510+
.map(|next_hop| {
511+
ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None })
512+
})
513+
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { context: Some(context) })));
494514

495515
let path = pks.zip(tlvs);
496516

497517
utils::construct_blinded_hops(secp_ctx, path, session_priv)
498518
}
499-

lightning/src/blinded_path/mod.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
//! Creating blinded paths and related utilities live here.
1111
12-
pub mod payment;
1312
pub mod message;
13+
pub mod payment;
1414
pub(crate) mod utils;
1515

1616
use bitcoin::secp256k1::PublicKey;
@@ -90,7 +90,9 @@ impl NodeIdLookUp for EmptyNodeIdLookUp {
9090

9191
impl Deref for EmptyNodeIdLookUp {
9292
type Target = EmptyNodeIdLookUp;
93-
fn deref(&self) -> &Self { self }
93+
fn deref(&self) -> &Self {
94+
self
95+
}
9496
}
9597

9698
/// An encrypted payload and node id corresponding to a hop in a payment or onion message path, to
@@ -108,20 +110,18 @@ pub struct BlindedHop {
108110

109111
impl BlindedPath {
110112
pub(super) fn public_introduction_node_id<'a>(
111-
&self, network_graph: &'a ReadOnlyNetworkGraph
113+
&self, network_graph: &'a ReadOnlyNetworkGraph,
112114
) -> Option<&'a NodeId> {
113115
match &self.introduction_node {
114116
IntroductionNode::NodeId(pubkey) => {
115117
let node_id = NodeId::from_pubkey(pubkey);
116118
network_graph.nodes().get_key_value(&node_id).map(|(key, _)| key)
117119
},
118120
IntroductionNode::DirectedShortChannelId(direction, scid) => {
119-
network_graph
120-
.channel(*scid)
121-
.map(|c| match direction {
122-
Direction::NodeOne => &c.node_one,
123-
Direction::NodeTwo => &c.node_two,
124-
})
121+
network_graph.channel(*scid).map(|c| match direction {
122+
Direction::NodeOne => &c.node_one,
123+
Direction::NodeTwo => &c.node_two,
124+
})
125125
},
126126
}
127127
}
@@ -155,7 +155,7 @@ impl Readable for BlindedPath {
155155
let introduction_node = match first_byte {
156156
0 => IntroductionNode::DirectedShortChannelId(Direction::NodeOne, Readable::read(r)?),
157157
1 => IntroductionNode::DirectedShortChannelId(Direction::NodeTwo, Readable::read(r)?),
158-
2|3 => {
158+
2 | 3 => {
159159
let mut bytes = [0; 33];
160160
bytes[0] = first_byte;
161161
r.read_exact(&mut bytes[1..])?;
@@ -165,16 +165,14 @@ impl Readable for BlindedPath {
165165
};
166166
let blinding_point = Readable::read(r)?;
167167
let num_hops: u8 = Readable::read(r)?;
168-
if num_hops == 0 { return Err(DecodeError::InvalidValue) }
168+
if num_hops == 0 {
169+
return Err(DecodeError::InvalidValue);
170+
}
169171
let mut blinded_hops: Vec<BlindedHop> = Vec::with_capacity(num_hops.into());
170172
for _ in 0..num_hops {
171173
blinded_hops.push(Readable::read(r)?);
172174
}
173-
Ok(BlindedPath {
174-
introduction_node,
175-
blinding_point,
176-
blinded_hops,
177-
})
175+
Ok(BlindedPath { introduction_node, blinding_point, blinded_hops })
178176
}
179177
}
180178

0 commit comments

Comments
 (0)