Skip to content

Commit 4147de2

Browse files
authored
Merge pull request #3324 from tnull/2024-09-rustfmt-util-1
`rustfmt`: Run on `util/*` (1/2)
2 parents c7627df + d68a484 commit 4147de2

12 files changed

+475
-303
lines changed

lightning/src/util/atomic_counter.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! A simple atomic counter that uses mutexes if the platform doesn't support atomic u64s.
22
3-
#[cfg(target_has_atomic = "64")]
4-
use core::sync::atomic::{AtomicU64, Ordering};
53
#[cfg(not(target_has_atomic = "64"))]
64
use crate::sync::Mutex;
5+
#[cfg(target_has_atomic = "64")]
6+
use core::sync::atomic::{AtomicU64, Ordering};
77

88
pub(crate) struct AtomicCounter {
99
#[cfg(target_has_atomic = "64")]
@@ -22,23 +22,27 @@ impl AtomicCounter {
2222
}
2323
}
2424
pub(crate) fn next(&self) -> u64 {
25-
#[cfg(target_has_atomic = "64")] {
25+
#[cfg(target_has_atomic = "64")]
26+
{
2627
self.counter.fetch_add(1, Ordering::AcqRel)
2728
}
28-
#[cfg(not(target_has_atomic = "64"))] {
29+
#[cfg(not(target_has_atomic = "64"))]
30+
{
2931
let mut mtx = self.counter.lock().unwrap();
3032
*mtx += 1;
3133
*mtx - 1
3234
}
3335
}
3436
#[cfg(test)]
3537
pub(crate) fn set_counter(&self, count: u64) {
36-
#[cfg(target_has_atomic = "64")] {
38+
#[cfg(target_has_atomic = "64")]
39+
{
3740
self.counter.store(count, Ordering::Release);
3841
}
39-
#[cfg(not(target_has_atomic = "64"))] {
42+
#[cfg(not(target_has_atomic = "64"))]
43+
{
4044
let mut mtx = self.counter.lock().unwrap();
41-
*mtx = count;
45+
*mtx = count;
4246
}
4347
}
4448
}

lightning/src/util/base32.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ pub enum Alphabet {
3333
/// RFC4648 encoding.
3434
RFC4648 {
3535
/// Whether to use padding.
36-
padding: bool
36+
padding: bool,
3737
},
3838
/// Zbase32 encoding.
39-
ZBase32
39+
ZBase32,
4040
}
4141

4242
impl Alphabet {
@@ -60,9 +60,7 @@ impl Alphabet {
6060
}
6161
ret
6262
},
63-
Self::ZBase32 => {
64-
Self::encode_data(data, ZBASE_ALPHABET)
65-
},
63+
Self::ZBase32 => Self::encode_data(data, ZBASE_ALPHABET),
6664
};
6765
ret.truncate(output_length);
6866

@@ -79,7 +77,9 @@ impl Alphabet {
7977
Self::RFC4648 { padding } => {
8078
let mut unpadded_data_length = data.len();
8179
if *padding {
82-
if data.len() % 8 != 0 { return Err(()); }
80+
if data.len() % 8 != 0 {
81+
return Err(());
82+
}
8383
data.iter().rev().take(6).for_each(|&c| {
8484
if c == b'=' {
8585
unpadded_data_length -= 1;
@@ -88,13 +88,14 @@ impl Alphabet {
8888
}
8989
(&data[..unpadded_data_length], RFC4648_INV_ALPHABET)
9090
},
91-
Self::ZBase32 => {
92-
(data, ZBASE_INV_ALPHABET)
93-
}
91+
Self::ZBase32 => (data, ZBASE_INV_ALPHABET),
9492
};
9593
// If the string has more characters than are required to alphabet_encode the number of bytes
9694
// decodable, treat the string as invalid.
97-
match data.len() % 8 { 1|3|6 => return Err(()), _ => {} }
95+
match data.len() % 8 {
96+
1 | 3 | 6 => return Err(()),
97+
_ => {},
98+
}
9899
Ok(Self::decode_data(data, alphabet)?)
99100
}
100101

@@ -175,9 +176,13 @@ mod tests {
175176
("6n9hq", &[0xf0, 0xbf, 0xc7]),
176177
("4t7ye", &[0xd4, 0x7a, 0x04]),
177178
("6im5sdy", &[0xf5, 0x57, 0xbb, 0x0c]),
178-
("ybndrfg8ejkmcpqxot1uwisza345h769", &[0x00, 0x44, 0x32, 0x14, 0xc7, 0x42, 0x54, 0xb6,
179-
0x35, 0xcf, 0x84, 0x65, 0x3a, 0x56, 0xd7, 0xc6,
180-
0x75, 0xbe, 0x77, 0xdf])
179+
(
180+
"ybndrfg8ejkmcpqxot1uwisza345h769",
181+
&[
182+
0x00, 0x44, 0x32, 0x14, 0xc7, 0x42, 0x54, 0xb6, 0x35, 0xcf, 0x84, 0x65, 0x3a, 0x56,
183+
0xd7, 0xc6, 0x75, 0xbe, 0x77, 0xdf,
184+
],
185+
),
181186
];
182187

183188
#[test]
@@ -242,7 +247,9 @@ mod tests {
242247
}
243248

244249
for (input, encoded) in RFC4648_NON_PADDED_TEST_VECTORS {
245-
let res = &Alphabet::RFC4648 { padding: false }.decode(std::str::from_utf8(encoded).unwrap()).unwrap();
250+
let res = &Alphabet::RFC4648 { padding: false }
251+
.decode(std::str::from_utf8(encoded).unwrap())
252+
.unwrap();
246253
assert_eq!(&res[..], &input[..]);
247254
}
248255
}
@@ -251,9 +258,8 @@ mod tests {
251258
fn padding() {
252259
let num_padding = [0, 6, 4, 3, 1];
253260
for i in 1..6 {
254-
let encoded = Alphabet::RFC4648 { padding: true }.encode(
255-
(0..(i as u8)).collect::<Vec<u8>>().as_ref()
256-
);
261+
let encoded = Alphabet::RFC4648 { padding: true }
262+
.encode((0..(i as u8)).collect::<Vec<u8>>().as_ref());
257263
assert_eq!(encoded.len(), 8);
258264
for j in 0..(num_padding[i % 5]) {
259265
assert_eq!(encoded.as_bytes()[encoded.len() - j - 1], b'=');

lightning/src/util/byte_utils.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@
99

1010
#[inline]
1111
pub fn slice_to_be48(v: &[u8]) -> u64 {
12-
((v[0] as u64) << 8*5) |
13-
((v[1] as u64) << 8*4) |
14-
((v[2] as u64) << 8*3) |
15-
((v[3] as u64) << 8*2) |
16-
((v[4] as u64) << 8*1) |
17-
((v[5] as u64) << 8*0)
12+
((v[0] as u64) << 8 * 5)
13+
| ((v[1] as u64) << 8 * 4)
14+
| ((v[2] as u64) << 8 * 3)
15+
| ((v[3] as u64) << 8 * 2)
16+
| ((v[4] as u64) << 8 * 1)
17+
| ((v[5] as u64) << 8 * 0)
1818
}
1919
#[inline]
2020
pub fn be48_to_array(u: u64) -> [u8; 6] {
2121
assert!(u & 0xffff_0000_0000_0000 == 0);
2222
let mut v = [0; 6];
23-
v[0] = ((u >> 8*5) & 0xff) as u8;
24-
v[1] = ((u >> 8*4) & 0xff) as u8;
25-
v[2] = ((u >> 8*3) & 0xff) as u8;
26-
v[3] = ((u >> 8*2) & 0xff) as u8;
27-
v[4] = ((u >> 8*1) & 0xff) as u8;
28-
v[5] = ((u >> 8*0) & 0xff) as u8;
23+
v[0] = ((u >> 8 * 5) & 0xff) as u8;
24+
v[1] = ((u >> 8 * 4) & 0xff) as u8;
25+
v[2] = ((u >> 8 * 3) & 0xff) as u8;
26+
v[3] = ((u >> 8 * 2) & 0xff) as u8;
27+
v[4] = ((u >> 8 * 1) & 0xff) as u8;
28+
v[5] = ((u >> 8 * 0) & 0xff) as u8;
2929
v
3030
}
3131

lightning/src/util/config.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ pub struct ChannelHandshakeLimits {
327327
///
328328
/// Default value: `2016`, which we also enforce as a maximum value so you can tweak config to
329329
/// reduce the loss of having useless locked funds (if your peer accepts)
330-
pub their_to_self_delay: u16
330+
pub their_to_self_delay: u16,
331331
}
332332

333333
impl Default for ChannelHandshakeLimits {
@@ -582,7 +582,9 @@ pub struct ChannelConfig {
582582
impl ChannelConfig {
583583
/// Applies the given [`ChannelConfigUpdate`] as a partial update to the [`ChannelConfig`].
584584
pub fn apply(&mut self, update: &ChannelConfigUpdate) {
585-
if let Some(forwarding_fee_proportional_millionths) = update.forwarding_fee_proportional_millionths {
585+
if let Some(forwarding_fee_proportional_millionths) =
586+
update.forwarding_fee_proportional_millionths
587+
{
586588
self.forwarding_fee_proportional_millionths = forwarding_fee_proportional_millionths;
587589
}
588590
if let Some(forwarding_fee_base_msat) = update.forwarding_fee_base_msat {
@@ -594,7 +596,9 @@ impl ChannelConfig {
594596
if let Some(max_dust_htlc_exposure_msat) = update.max_dust_htlc_exposure_msat {
595597
self.max_dust_htlc_exposure = max_dust_htlc_exposure_msat;
596598
}
597-
if let Some(force_close_avoidance_max_fee_satoshis) = update.force_close_avoidance_max_fee_satoshis {
599+
if let Some(force_close_avoidance_max_fee_satoshis) =
600+
update.force_close_avoidance_max_fee_satoshis
601+
{
598602
self.force_close_avoidance_max_fee_satoshis = force_close_avoidance_max_fee_satoshis;
599603
}
600604
}
@@ -683,11 +687,15 @@ pub struct ChannelConfigUpdate {
683687
impl From<ChannelConfig> for ChannelConfigUpdate {
684688
fn from(config: ChannelConfig) -> ChannelConfigUpdate {
685689
ChannelConfigUpdate {
686-
forwarding_fee_proportional_millionths: Some(config.forwarding_fee_proportional_millionths),
690+
forwarding_fee_proportional_millionths: Some(
691+
config.forwarding_fee_proportional_millionths,
692+
),
687693
forwarding_fee_base_msat: Some(config.forwarding_fee_base_msat),
688694
cltv_expiry_delta: Some(config.cltv_expiry_delta),
689695
max_dust_htlc_exposure_msat: Some(config.max_dust_htlc_exposure),
690-
force_close_avoidance_max_fee_satoshis: Some(config.force_close_avoidance_max_fee_satoshis),
696+
force_close_avoidance_max_fee_satoshis: Some(
697+
config.force_close_avoidance_max_fee_satoshis,
698+
),
691699
}
692700
}
693701
}
@@ -760,8 +768,9 @@ impl crate::util::ser::Readable for LegacyChannelConfig {
760768
});
761769
let max_dust_htlc_exposure_msat_fixed_limit =
762770
max_dust_htlc_exposure_msat_fixed_limit.unwrap_or(5_000_000);
763-
let max_dust_htlc_exposure_msat = max_dust_htlc_exposure_enum
764-
.unwrap_or(MaxDustHTLCExposure::FixedLimitMsat(max_dust_htlc_exposure_msat_fixed_limit));
771+
let max_dust_htlc_exposure_msat = max_dust_htlc_exposure_enum.unwrap_or(
772+
MaxDustHTLCExposure::FixedLimitMsat(max_dust_htlc_exposure_msat_fixed_limit),
773+
);
765774
Ok(Self {
766775
options: ChannelConfig {
767776
forwarding_fee_proportional_millionths,

lightning/src/util/errors.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub enum APIError {
2424
/// are documented, but generally indicates some precondition of a function was violated.
2525
APIMisuseError {
2626
/// A human-readable error message
27-
err: String
27+
err: String,
2828
},
2929
/// Due to a high feerate, we were unable to complete the request.
3030
/// For example, this may be returned if the feerate implies we cannot open a channel at the
@@ -33,20 +33,20 @@ pub enum APIError {
3333
/// A human-readable error message
3434
err: String,
3535
/// The feerate which was too high.
36-
feerate: u32
36+
feerate: u32,
3737
},
3838
/// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
3939
/// too-many-hops, etc).
4040
InvalidRoute {
4141
/// A human-readable error message
42-
err: String
42+
err: String,
4343
},
4444
/// We were unable to complete the request as the Channel required to do so is unable to
4545
/// complete the request (or was not found). This can take many forms, including disconnected
4646
/// peer, channel at capacity, channel shutting down, etc.
4747
ChannelUnavailable {
4848
/// A human-readable error message
49-
err: String
49+
err: String,
5050
},
5151
/// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]
5252
/// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a
@@ -74,11 +74,15 @@ pub enum APIError {
7474
impl fmt::Debug for APIError {
7575
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7676
match *self {
77-
APIError::APIMisuseError {ref err} => write!(f, "Misuse error: {}", err),
78-
APIError::FeeRateTooHigh {ref err, ref feerate} => write!(f, "{} feerate: {}", err, feerate),
79-
APIError::InvalidRoute {ref err} => write!(f, "Invalid route provided: {}", err),
80-
APIError::ChannelUnavailable {ref err} => write!(f, "Channel unavailable: {}", err),
81-
APIError::MonitorUpdateInProgress => f.write_str("Client indicated a channel monitor update is in progress but not yet complete"),
77+
APIError::APIMisuseError { ref err } => write!(f, "Misuse error: {}", err),
78+
APIError::FeeRateTooHigh { ref err, ref feerate } => {
79+
write!(f, "{} feerate: {}", err, feerate)
80+
},
81+
APIError::InvalidRoute { ref err } => write!(f, "Invalid route provided: {}", err),
82+
APIError::ChannelUnavailable { ref err } => write!(f, "Channel unavailable: {}", err),
83+
APIError::MonitorUpdateInProgress => f.write_str(
84+
"Client indicated a channel monitor update is in progress but not yet complete",
85+
),
8286
APIError::IncompatibleShutdownScript { ref script } => {
8387
write!(f, "Provided a scriptpubkey format not accepted by peer: {}", script)
8488
},
@@ -101,9 +105,9 @@ impl_writeable_tlv_based_enum_upgradable!(APIError,
101105
#[inline]
102106
pub(crate) fn get_onion_debug_field(error_code: u16) -> (&'static str, usize) {
103107
match error_code & 0xff {
104-
4|5|6 => ("sha256_of_onion", 32),
105-
11|12 => ("htlc_msat", 8),
106-
13|18 => ("cltv_expiry", 4),
108+
4 | 5 | 6 => ("sha256_of_onion", 32),
109+
11 | 12 => ("htlc_msat", 8),
110+
13 | 18 => ("cltv_expiry", 4),
107111
19 => ("incoming_htlc_msat", 8),
108112
20 => ("flags", 2),
109113
_ => ("", 0),

lightning/src/util/fuzz_wrappers.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@
88
// licenses.
99

1010
macro_rules! hash_to_message {
11-
($slice: expr) => {
11+
($slice: expr) => {{
12+
#[cfg(not(fuzzing))]
1213
{
13-
#[cfg(not(fuzzing))]
14-
{
15-
::bitcoin::secp256k1::Message::from_digest_slice($slice).unwrap()
16-
}
17-
#[cfg(fuzzing)]
18-
{
19-
match ::bitcoin::secp256k1::Message::from_digest_slice($slice) {
20-
Ok(msg) => msg,
21-
Err(_) => ::bitcoin::secp256k1::Message::from_digest([1; 32])
22-
}
14+
::bitcoin::secp256k1::Message::from_digest_slice($slice).unwrap()
15+
}
16+
#[cfg(fuzzing)]
17+
{
18+
match ::bitcoin::secp256k1::Message::from_digest_slice($slice) {
19+
Ok(msg) => msg,
20+
Err(_) => ::bitcoin::secp256k1::Message::from_digest([1; 32]),
2321
}
2422
}
25-
}
23+
}};
2624
}

0 commit comments

Comments
 (0)