Skip to content

Commit

Permalink
fmt +stable
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Feb 7, 2024
1 parent 5b8b3c8 commit fe3b3d6
Show file tree
Hide file tree
Showing 29 changed files with 187 additions and 186 deletions.
14 changes: 7 additions & 7 deletions balances/src/impl_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ impl<T: Config> fungibles::Inspect<T::AccountId> for Pallet<T> {
_provenance: Provenance,
) -> DepositConsequence {
if !<TotalIssuance<T>>::contains_key(asset) {
return DepositConsequence::UnknownAsset
return DepositConsequence::UnknownAsset;
};

let total_issuance = Pallet::<T>::total_issuance_entry(asset).principal;

let balance_amount = balance_type(amount);
if total_issuance.checked_add(balance_amount).is_none() {
return DepositConsequence::Overflow
return DepositConsequence::Overflow;
}

let balance = Pallet::<T>::balance(asset, who);

if balance.checked_add(balance_amount).is_none() {
return DepositConsequence::Overflow
return DepositConsequence::Overflow;
}

DepositConsequence::Success
Expand All @@ -98,22 +98,22 @@ impl<T: Config> fungibles::Inspect<T::AccountId> for Pallet<T> {
use WithdrawConsequence::*;

if !<TotalIssuance<T>>::contains_key(asset) {
return UnknownAsset
return UnknownAsset;
};

let total_issuance = Pallet::<T>::total_issuance_entry(asset);
if fungible(total_issuance.principal).checked_sub(amount).is_none() {
return Underflow
return Underflow;
}

if amount.is_zero() {
return Success
return Success;
}

let balance = fungible(Pallet::<T>::balance(asset, who));

if balance.checked_sub(amount).is_none() {
return WithdrawConsequence::BalanceLow
return WithdrawConsequence::BalanceLow;
}
WithdrawConsequence::Success
}
Expand Down
6 changes: 3 additions & 3 deletions balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl<T: Config> Pallet<T> {
// Early exist if no-op.
if amount == 0u128 {
Self::deposit_event(Event::Transferred(cid, source, dest, amount));
return Ok(amount)
return Ok(amount);
}

ensure!(Balance::<T>::contains_key(cid, &source), Error::<T>::NoAccount);
Expand All @@ -289,7 +289,7 @@ impl<T: Config> Pallet<T> {

if source == dest {
<Balance<T>>::insert(cid, &source, entry_from);
return Ok(amount)
return Ok(amount);
}

if !Balance::<T>::contains_key(cid, &dest) {
Expand Down Expand Up @@ -352,7 +352,7 @@ impl<T: Config> Pallet<T> {
ensure!(res >= 0, Error::<T>::BalanceTooLow);
res
} else {
return Err(Error::<T>::BalanceTooLow.into())
return Err(Error::<T>::BalanceTooLow.into());
};
entry_tot.principal -= amount;
//FIXME: delete account if it falls below existential deposit
Expand Down
14 changes: 7 additions & 7 deletions ceremonies/assignment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn generate_assignment_function_params<Hashing: Hash>(
AssignmentParams { m: m as u64, s1: s1 as u64, s2: s2 as u64 },
num_meetups,
) {
break
break;
} else {
skip_count += 1; // safe; skip_count <= 200;
}
Expand All @@ -65,7 +65,7 @@ fn validate_equal_mapping(
meetup_count: u64,
) -> bool {
if num_participants < 2 {
return true
return true;
}

let mut meetup_index_count: Vec<u64> = vec![0; meetup_count as usize];
Expand All @@ -80,7 +80,7 @@ fn validate_equal_mapping(

meetup_index_count[meetup_index] += 1; // safe; <= num_participants
if meetup_index_count[meetup_index] > meetup_index_count_max {
return false
return false;
}
}
true
Expand All @@ -97,14 +97,14 @@ pub fn assignment_fn_inverse(
participant_count: u64,
) -> Option<Vec<ParticipantIndexType>> {
if assignment_count == 0 {
return Some(vec![])
return Some(vec![]);
}

let mut max_index = assignment_params.m.saturating_sub(meetup_index) / assignment_count;
let mut result: Vec<ParticipantIndexType> = Vec::with_capacity(max_index as usize);
// ceil
if (assignment_params.m.saturating_sub(meetup_index) as i64).rem_euclid(assignment_count as i64) !=
0
if (assignment_params.m.saturating_sub(meetup_index) as i64).rem_euclid(assignment_count as i64)
!= 0
{
max_index += 1; //safe; m prime below num_participants
}
Expand All @@ -118,7 +118,7 @@ pub fn assignment_fn_inverse(
};

if t3 >= participant_count {
continue
continue;
}

result.push(t3);
Expand Down
26 changes: 13 additions & 13 deletions ceremonies/assignment/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,64 +40,64 @@ pub fn is_coprime(a: u64, b: u64) -> bool {

pub fn is_prime(n: u64) -> bool {
if n <= 3 {
return n > 1
return n > 1;
}
if n % 2 == 0 || n % 3 == 0 {
return false
return false;
}
if n < 25 {
return true
return true;
}
let mut i: u64 = 5;
let mut j: u64 = 25;
while j <= n {
let i_plus_two = i.checked_add(2u64).expect("i^2 does not overflow, so i + 2 is safe; qed");
if n % i == 0u64 || n % (i_plus_two) == 0u64 {
return false
return false;
}
i = i.checked_add(6u64).expect("i^2 does not overflow, so i + 6 is safe; qed");

if let Some(i_squared) = i.checked_pow(2) {
j = i_squared;
} else {
// if i overflows we can be sure that j <= n does not hold
break
break;
}
}
true
}

pub fn get_greatest_common_denominator(a: u64, b: u64) -> u64 {
if a == 0 || b == 0 {
return 0
return 0;
}

if a == b {
return a
return a;
}

if a > b {
return get_greatest_common_denominator(a.checked_sub(b).expect("a > b; qed"), b)
return get_greatest_common_denominator(a.checked_sub(b).expect("a > b; qed"), b);
}

get_greatest_common_denominator(a, b.checked_sub(a).expect("b <= a; qed"))
}

pub fn find_prime_below(mut n: u64) -> u64 {
if n <= 2 {
return 2u64
return 2u64;
}
if n % 2 == 0 {
n = n.checked_sub(1).expect("n > 2; qed");
}
while n > 0 {
if is_prime(n) {
return n
return n;
}
if let Some(n_minus_two) = n.checked_sub(2) {
n = n_minus_two;
} else {
break
break;
}
}
2u64
Expand All @@ -108,11 +108,11 @@ pub fn find_random_coprime_below<H: Hash>(
random_source: &mut RandomNumberGenerator<H>,
) -> u64 {
if upper_bound <= 1 {
return 0
return 0;
}

if upper_bound == 2 {
return 1
return 1;
}

(1..upper_bound)
Expand Down
24 changes: 12 additions & 12 deletions ceremonies/meetup-validation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn attestation_graph_is_fully_connected(
for (i, mut attestations) in participant_attestations.into_iter().enumerate() {
// only consider participants present in the meetup
if !legit_participants.contains(&i) {
continue
continue;
}
attestations.sort();
let mut expected_attestations = legit_participants.clone();
Expand All @@ -92,7 +92,7 @@ fn attestation_graph_is_fully_connected(
expected_attestations.sort();

if attestations != expected_attestations {
return false
return false;
}
}
true
Expand All @@ -105,15 +105,15 @@ fn early_rewards_possible(
n_confirmed: u32,
vote_is_unanimous: bool,
) -> bool {
if vote_is_unanimous &&
vote_yields_majority(num_total_participants, n_confirmed) &&
num_attestations_matches_vote(
if vote_is_unanimous
&& vote_yields_majority(num_total_participants, n_confirmed)
&& num_attestations_matches_vote(
&legit_participants,
&participant_attestations,
n_confirmed,
) && attestation_graph_is_fully_connected(legit_participants, participant_attestations)
{
return true
return true;
}
false
}
Expand Down Expand Up @@ -170,7 +170,7 @@ fn get_excluded_participants_num_attestations(
for _ in 0..max_iterations {
// if all participants were excluded, exit the loop
if participants_to_process.is_empty() {
return Ok(excluded_participants)
return Ok(excluded_participants);
};

let participants_grouped_by_outgoing_attestations =
Expand Down Expand Up @@ -217,10 +217,10 @@ fn get_excluded_participants_num_attestations(
participants_to_process.retain(|k| !participants_to_exclude.contains(k));
relevant_attestations =
filter_attestations(&participants_to_process, relevant_attestations.clone());
continue
continue;
} else {
// if all participants are above the threshold and therefore no participants were removed, we exit the loop
break
break;
}
}
Ok(excluded_participants)
Expand All @@ -240,12 +240,12 @@ fn find_majority_vote(
}

if n_vote_candidates.is_empty() {
return Err(MeetupValidationError::BallotEmpty)
return Err(MeetupValidationError::BallotEmpty);
}
// sort by descending vote count
n_vote_candidates.sort_by(|a, b| b.1.cmp(&a.1));
if n_vote_candidates.get_or_err(0)?.1 < 3 {
return Err(MeetupValidationError::NoDependableVote)
return Err(MeetupValidationError::NoDependableVote);
}
let (n_confirmed, vote_count) = n_vote_candidates.get_or_err(0)?;
let vote_is_unanimous = n_vote_candidates.len() == 1;
Expand Down Expand Up @@ -299,7 +299,7 @@ fn group_indices_by_value(
) -> Result<Vec<ParticipantGroup>, MeetupValidationError> {
if let Some(max) = indices.iter().max() {
if max >= &values.len() {
return Err(MeetupValidationError::IndexOutOfBounds)
return Err(MeetupValidationError::IndexOutOfBounds);
}
}

Expand Down
7 changes: 2 additions & 5 deletions ceremonies/meetup-validation/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,8 @@ fn get_excluded_participants_num_attestations_works() {
(1, ExclusionReason::TooFewOutgoingAttestations),
];
assert_eq!(
get_excluded_participants_num_attestations(
&participants,
participant_attestations,
|n| n - 1
)
get_excluded_participants_num_attestations(&participants, participant_attestations, |n| n
- 1)
.unwrap(),
excluded_participants
);
Expand Down
6 changes: 3 additions & 3 deletions ceremonies/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ where
if !self.offchain_indexing {
return Err(
Error::OffchainIndexingDisabled("ceremonies_getReputations".to_string()).into()
)
);
}

let cache_key = &reputation_cache_key(&account);
Expand All @@ -168,12 +168,12 @@ where
.map_err(|e| Error::Runtime(e.into()))?;

if self.cache_dirty(&reputation_cache_dirty_key(&account)) {
return Ok(self.refresh_reputation_cache(account, ceremony_info, at)?.reputation)
return Ok(self.refresh_reputation_cache(account, ceremony_info, at)?.reputation);
}

if let Some(reputation_cache_value) = self.get_storage::<ReputationCacheValue>(cache_key)? {
if ceremony_info == reputation_cache_value.ceremony_info {
return Ok(reputation_cache_value.reputation)
return Ok(reputation_cache_value.reputation);
}
};

Expand Down
Loading

0 comments on commit fe3b3d6

Please sign in to comment.