Skip to content

Commit 5be465c

Browse files
committed
Refactor: move inline use statements to top of file and use core instead of std
Move inline `use` statements from function bodies to the top of consensus.rs to follow Rust best practices. Replace `std::cmp` with `core::cmp` for basic comparison functionality that doesn't require the full standard library, making the code more suitable for no_std environments. Changes: - Move `use std::cmp::{max, min}` from `relative_min_window_density()` to file top - Move `use std::cmp::Ordering::*` from fork choice functions to file top - Replace `std::cmp` with `core::cmp` - Use fully qualified `Ordering::` variants instead of wildcard imports - Update all match expressions to use explicit `Ordering::Greater/Less/Equal` All tests pass and documentation builds successfully.
1 parent 771d88b commit 5be465c

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

core/src/consensus.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::cmp::{max, min, Ordering};
12
use mina_p2p_messages::v2::{
23
self, BlockTimeTimeStableV1,
34
ConsensusProofOfStakeDataConsensusStateValueStableV2 as MinaConsensusState, StateHash,
@@ -196,8 +197,6 @@ pub fn is_short_range_fork(a: &MinaConsensusState, b: &MinaConsensusState) -> bo
196197
/// See [Relative Minimum Window Density](https://github.com/MinaProtocol/mina/blob/compatible/docs/specs/consensus/README.md#5412-relative-minimum-window-density)
197198
/// in the consensus specification for detailed mathematical definitions.
198199
pub fn relative_min_window_density(b1: &MinaConsensusState, b2: &MinaConsensusState) -> u32 {
199-
use std::cmp::{max, min};
200-
201200
let max_slot = max(global_slot(b1), global_slot(b2));
202201

203202
if max_slot < GRACE_PERIOD_END {
@@ -279,23 +278,22 @@ pub fn short_range_fork_take(
279278
tip_hash: &StateHash,
280279
candidate_hash: &StateHash,
281280
) -> (bool, ConsensusShortRangeForkDecisionReason) {
282-
use std::cmp::Ordering::*;
283281
use ConsensusShortRangeForkDecisionReason::*;
284282

285283
let tip_height = &tip_cs.blockchain_length;
286284
let candidate_height = &candidate_cs.blockchain_length;
287285
match candidate_height.cmp(tip_height) {
288-
Greater => return (true, ChainLength),
289-
Less => return (false, ChainLength),
290-
Equal => {}
286+
Ordering::Greater => return (true, ChainLength),
287+
Ordering::Less => return (false, ChainLength),
288+
Ordering::Equal => {}
291289
}
292290

293291
let tip_vrf = tip_cs.last_vrf_output.blake2b();
294292
let candidate_vrf = candidate_cs.last_vrf_output.blake2b();
295293
match candidate_vrf.cmp(&tip_vrf) {
296-
Greater => return (true, Vrf),
297-
Less => return (false, Vrf),
298-
Equal => {}
294+
Ordering::Greater => return (true, Vrf),
295+
Ordering::Less => return (false, Vrf),
296+
Ordering::Equal => {}
299297
}
300298

301299
(candidate_hash > tip_hash, StateHash)
@@ -344,31 +342,30 @@ pub fn long_range_fork_take(
344342
tip_hash: &StateHash,
345343
candidate_hash: &StateHash,
346344
) -> (bool, ConsensusLongRangeForkDecisionReason) {
347-
use std::cmp::Ordering::*;
348345
use ConsensusLongRangeForkDecisionReason::*;
349346

350347
let tip_density = relative_min_window_density(tip_cs, candidate_cs);
351348
let candidate_density = relative_min_window_density(candidate_cs, tip_cs);
352349
match candidate_density.cmp(&tip_density) {
353-
Greater => return (true, SubWindowDensity),
354-
Less => return (false, SubWindowDensity),
355-
Equal => {}
350+
Ordering::Greater => return (true, SubWindowDensity),
351+
Ordering::Less => return (false, SubWindowDensity),
352+
Ordering::Equal => {}
356353
}
357354

358355
let tip_height = &tip_cs.blockchain_length;
359356
let candidate_height = &candidate_cs.blockchain_length;
360357
match candidate_height.cmp(tip_height) {
361-
Greater => return (true, ChainLength),
362-
Less => return (false, ChainLength),
363-
Equal => {}
358+
Ordering::Greater => return (true, ChainLength),
359+
Ordering::Less => return (false, ChainLength),
360+
Ordering::Equal => {}
364361
}
365362

366363
let tip_vrf = tip_cs.last_vrf_output.blake2b();
367364
let candidate_vrf = candidate_cs.last_vrf_output.blake2b();
368365
match candidate_vrf.cmp(&tip_vrf) {
369-
Greater => return (true, Vrf),
370-
Less => return (false, Vrf),
371-
Equal => {}
366+
Ordering::Greater => return (true, Vrf),
367+
Ordering::Less => return (false, Vrf),
368+
Ordering::Equal => {}
372369
}
373370

374371
(candidate_hash > tip_hash, StateHash)

0 commit comments

Comments
 (0)