Skip to content

Commit c153a37

Browse files
committed
clippy
1 parent e99df02 commit c153a37

File tree

7 files changed

+59
-87
lines changed

7 files changed

+59
-87
lines changed

node/src/mev_shield/author.rs

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ impl ShieldKeys {
8282
}
8383
}
8484

85+
impl Default for ShieldKeys {
86+
fn default() -> Self {
87+
Self::new()
88+
}
89+
}
90+
8591
/// Shared context state.
8692
#[freeze_struct("62af7d26cf7c1271")]
8793
#[derive(Clone)]
@@ -94,7 +100,10 @@ pub struct ShieldContext {
94100
pub fn derive_aead_key(ss: &[u8]) -> [u8; 32] {
95101
let mut key = [0u8; 32];
96102
let n = ss.len().min(32);
97-
key[..n].copy_from_slice(&ss[..n]);
103+
104+
if let (Some(dst), Some(src)) = (key.get_mut(..n), ss.get(..n)) {
105+
dst.copy_from_slice(src);
106+
}
98107
key
99108
}
100109

@@ -123,8 +132,8 @@ const AURA_KEY_TYPE: KeyTypeId = KeyTypeId(*b"aura");
123132
/// - at ~announce_at_ms announce the next key bytes on chain,
124133
pub fn spawn_author_tasks<B, C, Pool>(
125134
task_spawner: &sc_service::SpawnTaskHandle,
126-
client: std::sync::Arc<C>,
127-
pool: std::sync::Arc<Pool>,
135+
client: Arc<C>,
136+
pool: Arc<Pool>,
128137
keystore: sp_keystore::KeystorePtr,
129138
timing: TimeParams,
130139
) -> ShieldContext
@@ -135,13 +144,13 @@ where
135144
B::Extrinsic: From<sp_runtime::OpaqueExtrinsic>,
136145
{
137146
let ctx = ShieldContext {
138-
keys: std::sync::Arc::new(std::sync::Mutex::new(ShieldKeys::new())),
147+
keys: Arc::new(Mutex::new(ShieldKeys::new())),
139148
timing: timing.clone(),
140149
};
141150

142151
let aura_keys: Vec<sp_core::sr25519::Public> = keystore.sr25519_public_keys(AURA_KEY_TYPE);
143152

144-
let local_aura_pub = match aura_keys.get(0).cloned() {
153+
let local_aura_pub = match aura_keys.first().copied() {
145154
Some(k) => k,
146155
None => {
147156
log::warn!(
@@ -173,20 +182,15 @@ where
173182
if announce_at_ms > slot_ms {
174183
log::warn!(
175184
target: "mev-shield",
176-
"spawn_author_tasks: announce_at_ms ({}) > slot_ms ({}); clamping to slot_ms",
177-
announce_at_ms,
178-
slot_ms,
185+
"spawn_author_tasks: announce_at_ms ({announce_at_ms}) > slot_ms ({slot_ms}); clamping to slot_ms",
179186
);
180187
announce_at_ms = slot_ms;
181188
}
182189
let tail_ms = slot_ms.saturating_sub(announce_at_ms);
183190

184191
log::debug!(
185192
target: "mev-shield",
186-
"author timing: slot_ms={} announce_at_ms={} (effective) tail_ms={}",
187-
slot_ms,
188-
announce_at_ms,
189-
tail_ms
193+
"author timing: slot_ms={slot_ms} announce_at_ms={announce_at_ms} (effective) tail_ms={tail_ms}",
190194
);
191195

192196
let mut import_stream = client_clone.import_notification_stream();
@@ -204,17 +208,15 @@ where
204208
Err(e) => {
205209
log::debug!(
206210
target: "mev-shield",
207-
"spawn_author_tasks: failed to lock ShieldKeys (poisoned?): {:?}",
208-
e
211+
"spawn_author_tasks: failed to lock ShieldKeys (poisoned?): {e:?}",
209212
);
210213
continue;
211214
}
212215
};
213216

214217
log::debug!(
215218
target: "mev-shield",
216-
"Slot start (local author): (pk sizes: curr={}B, next={}B)",
217-
curr_pk_len, next_pk_len
219+
"Slot start (local author): (pk sizes: curr={curr_pk_len}B, next={next_pk_len}B)",
218220
);
219221

220222
// Wait until the announce window in this slot.
@@ -228,8 +230,7 @@ where
228230
Err(e) => {
229231
log::debug!(
230232
target: "mev-shield",
231-
"spawn_author_tasks: failed to lock ShieldKeys for next_pk: {:?}",
232-
e
233+
"spawn_author_tasks: failed to lock ShieldKeys for next_pk: {e:?}",
233234
);
234235
continue;
235236
}
@@ -240,7 +241,7 @@ where
240241
client_clone.clone(),
241242
pool_clone.clone(),
242243
keystore_clone.clone(),
243-
local_aura_pub.clone(),
244+
local_aura_pub,
244245
next_pk.clone(),
245246
local_nonce,
246247
)
@@ -257,7 +258,7 @@ where
257258
client_clone.clone(),
258259
pool_clone.clone(),
259260
keystore_clone.clone(),
260-
local_aura_pub.clone(),
261+
local_aura_pub,
261262
next_pk,
262263
local_nonce.saturating_add(1),
263264
)
@@ -297,22 +298,21 @@ where
297298
Err(e) => {
298299
log::debug!(
299300
target: "mev-shield",
300-
"spawn_author_tasks: failed to lock ShieldKeys for roll_for_next_slot: {:?}",
301-
e
301+
"spawn_author_tasks: failed to lock ShieldKeys for roll_for_next_slot: {e:?}",
302302
);
303303
}
304304
}
305305
}
306-
}
306+
},
307307
);
308308

309309
ctx
310310
}
311311

312312
/// Build & submit the signed `announce_next_key` extrinsic OFF-CHAIN
313313
pub async fn submit_announce_extrinsic<B, C, Pool>(
314-
client: std::sync::Arc<C>,
315-
pool: std::sync::Arc<Pool>,
314+
client: Arc<C>,
315+
pool: Arc<Pool>,
316316
keystore: sp_keystore::KeystorePtr,
317317
aura_pub: sp_core::sr25519::Public,
318318
next_public_key: Vec<u8>,
@@ -350,10 +350,9 @@ where
350350
let src_start = bytes.len().saturating_sub(n);
351351
let dst_start = 32usize.saturating_sub(n);
352352

353-
if let (Some(dst), Some(src)) = (
354-
out.get_mut(dst_start..32),
355-
bytes.get(src_start..src_start + n),
356-
) {
353+
let src_slice = bytes.get(src_start..).and_then(|s| s.get(..n));
354+
355+
if let (Some(dst), Some(src)) = (out.get_mut(dst_start..32), src_slice) {
357356
dst.copy_from_slice(src);
358357
H256(out)
359358
} else {
@@ -411,8 +410,7 @@ where
411410
);
412411

413412
// Build the exact signable payload.
414-
let payload: SignedPayload =
415-
SignedPayload::from_raw(call.clone(), extra.clone(), implicit.clone());
413+
let payload: SignedPayload = SignedPayload::from_raw(call.clone(), extra.clone(), implicit);
416414

417415
let raw_payload = payload.encode();
418416

@@ -432,6 +430,7 @@ where
432430

433431
let xt_bytes = uxt.encode();
434432
let xt_hash = sp_core::hashing::blake2_256(&xt_bytes);
433+
let xt_hash_hex = hex::encode(xt_hash);
435434

436435
let opaque: sp_runtime::OpaqueExtrinsic = uxt.into();
437436
let xt: <B as sp_runtime::traits::Block>::Extrinsic = opaque.into();
@@ -441,9 +440,7 @@ where
441440

442441
log::debug!(
443442
target: "mev-shield",
444-
"announce_next_key submitted: xt=0x{}, nonce={}",
445-
hex::encode(xt_hash),
446-
nonce
443+
"announce_next_key submitted: xt=0x{xt_hash_hex}, nonce={nonce}",
447444
);
448445

449446
Ok(())

node/src/mev_shield/proposer.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ pub fn spawn_revealer<B, C, Pool>(
151151
Err(e) => {
152152
log::debug!(
153153
target: "mev-shield",
154-
" [xt #{idx}] failed to decode UncheckedExtrinsic: {:?}",
155-
e
154+
" [xt #{idx}] failed to decode UncheckedExtrinsic: {e:?}",
156155
);
157156
continue;
158157
}
@@ -227,14 +226,11 @@ pub fn spawn_revealer<B, C, Pool>(
227226
}
228227
Ok(None) => log::debug!(
229228
target: "mev-shield",
230-
" block_body returned None for hash={:?}",
231-
at_hash
229+
" block_body returned None for hash={at_hash:?}",
232230
),
233231
Err(e) => log::debug!(
234232
target: "mev-shield",
235-
" block_body error for hash={:?}: {:?}",
236-
at_hash,
237-
e
233+
" block_body error for hash={at_hash:?}: {e:?}",
238234
),
239235
}
240236
}
@@ -263,9 +259,7 @@ pub fn spawn_revealer<B, C, Pool>(
263259
if decrypt_window_ms > slot_ms {
264260
log::warn!(
265261
target: "mev-shield",
266-
"spawn_revealer: decrypt_window_ms ({}) > slot_ms ({}); clamping to slot_ms",
267-
decrypt_window_ms,
268-
slot_ms
262+
"spawn_revealer: decrypt_window_ms ({decrypt_window_ms}) > slot_ms ({slot_ms}); clamping to slot_ms",
269263
);
270264
decrypt_window_ms = slot_ms;
271265
}
@@ -274,19 +268,13 @@ pub fn spawn_revealer<B, C, Pool>(
274268

275269
log::debug!(
276270
target: "mev-shield",
277-
"revealer timing: slot_ms={} decrypt_window_ms={} (effective) tail_ms={}",
278-
slot_ms,
279-
decrypt_window_ms,
280-
tail_ms
271+
"revealer timing: slot_ms={slot_ms} decrypt_window_ms={decrypt_window_ms} (effective) tail_ms={tail_ms}",
281272
);
282273

283274
loop {
284275
log::debug!(
285276
target: "mev-shield",
286-
"revealer: sleeping {} ms before decrypt window (slot_ms={}, decrypt_window_ms={})",
287-
tail_ms,
288-
slot_ms,
289-
decrypt_window_ms
277+
"revealer: sleeping {tail_ms} ms before decrypt window (slot_ms={slot_ms}, decrypt_window_ms={decrypt_window_ms})",
290278
);
291279

292280
if tail_ms > 0 {
@@ -307,8 +295,7 @@ pub fn spawn_revealer<B, C, Pool>(
307295
Err(e) => {
308296
log::debug!(
309297
target: "mev-shield",
310-
"revealer: failed to lock ShieldKeys (poisoned?): {:?}",
311-
e
298+
"revealer: failed to lock ShieldKeys (poisoned?): {e:?}",
312299
);
313300
None
314301
}
@@ -346,8 +333,7 @@ pub fn spawn_revealer<B, C, Pool>(
346333
Err(e) => {
347334
log::debug!(
348335
target: "mev-shield",
349-
"revealer: failed to lock WrapperBuffer for drain_for_block: {:?}",
350-
e
336+
"revealer: failed to lock WrapperBuffer for drain_for_block: {e:?}",
351337
);
352338
Vec::new()
353339
}

node/src/service.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -540,18 +540,13 @@ where
540540

541541
if role.is_authority() {
542542
let slot_duration = consensus_mechanism.slot_duration(&client)?;
543-
let slot_duration_ms: u64 =
544-
u64::try_from(slot_duration.as_millis()).unwrap_or(u64::MAX);
543+
let slot_duration_ms: u64 = u64::try_from(slot_duration.as_millis()).unwrap_or(u64::MAX);
545544

546545
// For 12s blocks: announce ≈ 7s, decrypt window ≈ 3s.
547546
// For 250ms blocks: announce ≈ 145ms, decrypt window ≈ 62ms, etc.
548-
let announce_at_ms_raw = slot_duration_ms
549-
.saturating_mul(7)
550-
.saturating_div(12);
547+
let announce_at_ms_raw = slot_duration_ms.saturating_mul(7).saturating_div(12);
551548

552-
let decrypt_window_ms = slot_duration_ms
553-
.saturating_mul(3)
554-
.saturating_div(12);
549+
let decrypt_window_ms = slot_duration_ms.saturating_mul(3).saturating_div(12);
555550

556551
// Ensure announce_at_ms + decrypt_window_ms never exceeds slot_ms.
557552
let max_announce = slot_duration_ms.saturating_sub(decrypt_window_ms);
@@ -614,20 +609,19 @@ where
614609
let (slot_ms, decrypt_ms) = mev_timing
615610
.as_ref()
616611
.map(|t| (t.slot_ms, t.decrypt_window_ms))
617-
.unwrap_or((slot_duration.as_millis() as u64, 3_000));
612+
.unwrap_or((slot_duration.as_millis(), 3_000));
618613

619614
let guard_ms: u64 = 200; // small cushion so reveals hit the pool first
620615
let after_decrypt_ms = slot_ms.saturating_sub(decrypt_ms).saturating_add(guard_ms);
621616

622-
// Clamp into (0.5 .. 0.98] to give the proposer enough time
623-
let mut f = (after_decrypt_ms as f32) / (slot_ms as f32);
624-
if f < 0.50 {
625-
f = 0.50;
626-
}
627-
if f > 0.98 {
628-
f = 0.98;
629-
}
630-
f
617+
let f_raw = if slot_ms > 0 {
618+
(after_decrypt_ms as f32) / (slot_ms as f32)
619+
} else {
620+
// Extremely defensive fallback; should never happen in practice.
621+
0.75
622+
};
623+
624+
f_raw.clamp(0.50, 0.98)
631625
};
632626

633627
let create_inherent_data_providers =

pallets/shield/src/benchmarking.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//! Benchmarking for pallet-mev-shield.
2-
#![cfg(feature = "runtime-benchmarks")]
3-
41
use super::*;
52

63
use codec::Encode;
@@ -99,7 +96,7 @@ mod benches {
9996

10097
// Assert: NextKey should be set exactly.
10198
let stored = NextKey::<T>::get().expect("must be set by announce_next_key");
102-
assert_eq!(stored, public_key.as_slice());
99+
assert_eq!(stored, public_key);
103100
}
104101

105102
/// Benchmark `submit_encrypted`.

pallets/shield/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,13 @@ pub mod pallet {
195195
/// - `aead_ct` is XChaCha20‑Poly1305 over:
196196
/// signer || nonce || SCALE(call) || sig_kind || signature
197197
#[pallet::call_index(1)]
198-
#[pallet::weight(({
199-
let w = Weight::from_parts(13_980_000, 0)
198+
#[pallet::weight((
199+
Weight::from_parts(13_980_000, 0)
200200
.saturating_add(T::DbWeight::get().reads(1_u64))
201-
.saturating_add(T::DbWeight::get().writes(1_u64));
202-
w
203-
}, DispatchClass::Normal, Pays::Yes))]
201+
.saturating_add(T::DbWeight::get().writes(1_u64)),
202+
DispatchClass::Normal,
203+
Pays::Yes,
204+
))]
204205
pub fn submit_encrypted(
205206
origin: OriginFor<T>,
206207
commitment: T::Hash,
@@ -229,6 +230,7 @@ pub mod pallet {
229230
#[pallet::weight(Weight::from_parts(77_280_000, 0)
230231
.saturating_add(T::DbWeight::get().reads(4_u64))
231232
.saturating_add(T::DbWeight::get().writes(2_u64)))]
233+
#[allow(clippy::useless_conversion)]
232234
pub fn execute_revealed(
233235
origin: OriginFor<T>,
234236
id: T::Hash,

pallets/shield/src/mock.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![cfg(test)]
2-
31
use crate as pallet_mev_shield;
42

53
use frame_support::{construct_runtime, derive_impl, parameter_types, traits::Everything};

pallets/shield/src/tests.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![cfg(test)]
2-
31
use crate as pallet_mev_shield;
42
use crate::mock::*;
53

0 commit comments

Comments
 (0)