Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Rustfmt changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lldenaurois committed Aug 3, 2021
1 parent 5df6e9e commit 79eaf7c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
40 changes: 17 additions & 23 deletions runtime/parachains/src/disputes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
//! Runtime component for handling disputes of parachain candidates.

use crate::{
shared,
configuration::{self, HostConfiguration},
initializer::SessionChangeNotification,
session_info,
session_info, shared,
};
use bitvec::{bitvec, order::Lsb0 as BitOrderLsb0};
use frame_support::{ensure, traits::Get, weights::Weight};
Expand Down Expand Up @@ -554,10 +553,8 @@ impl<T: Config> Pallet<T> {
dispute.concluded_at = Some(now);
<Disputes<T>>::insert(session_index, candidate_hash, &dispute);

let is_local = <shared::Pallet<T>>::is_included_candidate(
&session_index,
&candidate_hash,
);
let is_local =
<shared::Pallet<T>>::is_included_candidate(&session_index, &candidate_hash);
if is_local {
// Local disputes don't count towards spam.

Expand Down Expand Up @@ -780,7 +777,8 @@ impl<T: Config> Pallet<T> {
};

// Apply spam slot changes. Bail early if too many occupied.
let is_local = <shared::Pallet<T>>::is_included_candidate(&set.session, &set.candidate_hash);
let is_local =
<shared::Pallet<T>>::is_included_candidate(&set.session, &set.candidate_hash);
if !is_local {
let mut spam_slots: Vec<u32> =
SpamSlots::<T>::get(&set.session).unwrap_or_else(|| vec![0; n_validators]);
Expand Down Expand Up @@ -912,10 +910,8 @@ impl<T: Config> Pallet<T> {
};

// Apply spam slot changes. Bail early if too many occupied.
let is_local = <shared::Pallet<T>>::is_included_candidate(
&set.session,
&set.candidate_hash,
);
let is_local =
<shared::Pallet<T>>::is_included_candidate(&set.session, &set.candidate_hash);
if !is_local {
let mut spam_slots: Vec<u32> =
SpamSlots::<T>::get(&set.session).unwrap_or_else(|| vec![0; n_validators]);
Expand Down Expand Up @@ -989,10 +985,9 @@ impl<T: Config> Pallet<T> {

// Freeze if just concluded against some local candidate
if summary.new_flags.contains(DisputeStateFlags::AGAINST_SUPERMAJORITY) {
if let Some((revert_to, _)) = <shared::Pallet<T>>::get_included_candidate(
&set.session,
&set.candidate_hash,
) {
if let Some((revert_to, _)) =
<shared::Pallet<T>>::get_included_candidate(&set.session, &set.candidate_hash)
{
Self::revert_and_freeze(revert_to);
}
}
Expand Down Expand Up @@ -1111,7 +1106,6 @@ fn check_signature(
#[cfg(test)]
mod tests {
use super::*;
use primitives::v1::CoreIndex;
use crate::mock::{
new_test_ext, AccountId, AllPallets, Initializer, MockGenesisConfig, System, Test,
PUNISH_VALIDATORS_AGAINST, PUNISH_VALIDATORS_FOR, PUNISH_VALIDATORS_INCONCLUSIVE,
Expand All @@ -1122,7 +1116,7 @@ mod tests {
traits::{OnFinalize, OnInitialize},
};
use frame_system::InitKind;
use primitives::v1::BlockNumber;
use primitives::v1::{BlockNumber, CoreIndex};
use sp_core::{crypto::CryptoType, Pair};

// All arguments for `initializer::on_new_session`
Expand Down Expand Up @@ -1160,18 +1154,18 @@ mod tests {
}
}

fn include_candidate(session: SessionIndex, candidate_hash: &CandidateHash, block_number: BlockNumber) {
fn include_candidate(
session: SessionIndex,
candidate_hash: &CandidateHash,
block_number: BlockNumber,
) {
if let Some(revert_to) = shared::Pallet::<Test>::note_included_candidate(
session,
candidate_hash.clone(),
block_number,
CoreIndex(0),
) {
Pallet::<Test>::process_included(
session,
candidate_hash.clone(),
revert_to,
);
Pallet::<Test>::process_included(session, candidate_hash.clone(), revert_to);
}
}

Expand Down
36 changes: 26 additions & 10 deletions runtime/parachains/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

use frame_support::pallet_prelude::*;
use primitives::v1::{CandidateHash, CoreIndex, SessionIndex, ValidatorId, ValidatorIndex};
use sp_std::vec::Vec;
use sp_runtime::traits::{One, Zero};
use sp_std::vec::Vec;

use rand::{seq::SliceRandom, SeedableRng};
use rand_chacha::ChaCha20Rng;
Expand Down Expand Up @@ -72,8 +72,10 @@ pub mod pallet {
#[pallet::getter(fn included_candidates)]
pub(super) type IncludedCandidates<T: Config> = StorageDoubleMap<
_,
Twox64Concat, SessionIndex,
Blake2_128Concat, CandidateHash,
Twox64Concat,
SessionIndex,
Blake2_128Concat,
CandidateHash,
(T::BlockNumber, CoreIndex),
>;

Expand Down Expand Up @@ -136,7 +138,9 @@ impl<T: Config> Pallet<T> {
included_in: T::BlockNumber,
core_index: CoreIndex,
) -> Option<T::BlockNumber> {
if included_in.is_zero() { return None }
if included_in.is_zero() {
return None
}

let revert_to = included_in - One::one();
<IncludedCandidates<T>>::insert(&session, &candidate_hash, (revert_to, core_index));
Expand Down Expand Up @@ -168,7 +172,7 @@ impl<T: Config> Pallet<T> {
#[cfg(test)]
pub(crate) fn included_candidates_iter_prefix(
session: SessionIndex,
) -> impl Iterator<Item=(CandidateHash, (T::BlockNumber, CoreIndex))> {
) -> impl Iterator<Item = (CandidateHash, (T::BlockNumber, CoreIndex))> {
<IncludedCandidates<T>>::iter_prefix(session)
}

Expand Down Expand Up @@ -291,7 +295,8 @@ mod tests {
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
let session = 1;
let candidate_hash = CandidateHash(sp_core::H256::repeat_byte(1));
assert!(ParasShared::note_included_candidate(session, candidate_hash, 0, CoreIndex(0)).is_none());
assert!(ParasShared::note_included_candidate(session, candidate_hash, 0, CoreIndex(0))
.is_none());
assert!(!ParasShared::is_included_candidate(&session, &candidate_hash));
assert!(ParasShared::get_included_candidate(&session, &candidate_hash).is_none());
});
Expand All @@ -305,7 +310,12 @@ mod tests {
let block_number = 1;
let core_index = CoreIndex(2);
assert_eq!(
ParasShared::note_included_candidate(session, candidate_hash, block_number, core_index),
ParasShared::note_included_candidate(
session,
candidate_hash,
block_number,
core_index
),
Some(block_number - 1),
);
assert!(ParasShared::is_included_candidate(&session, &candidate_hash));
Expand All @@ -323,9 +333,15 @@ mod tests {
let candidate_hash2 = CandidateHash(sp_core::H256::repeat_byte(2));
let candidate_hash3 = CandidateHash(sp_core::H256::repeat_byte(3));

assert!(ParasShared::note_included_candidate(1, candidate_hash1, 1, CoreIndex(0)).is_some());
assert!(ParasShared::note_included_candidate(1, candidate_hash2, 1, CoreIndex(0)).is_some());
assert!(ParasShared::note_included_candidate(2, candidate_hash3, 2, CoreIndex(0)).is_some());
assert!(
ParasShared::note_included_candidate(1, candidate_hash1, 1, CoreIndex(0)).is_some()
);
assert!(
ParasShared::note_included_candidate(1, candidate_hash2, 1, CoreIndex(0)).is_some()
);
assert!(
ParasShared::note_included_candidate(2, candidate_hash3, 2, CoreIndex(0)).is_some()
);

assert_eq!(ParasShared::included_candidates_iter_prefix(1).count(), 2);
assert_eq!(ParasShared::included_candidates_iter_prefix(2).count(), 1);
Expand Down

0 comments on commit 79eaf7c

Please sign in to comment.