mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 05:51:02 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use super::{*, Pallet as Grandpa};
|
||||
use super::{Pallet as Grandpa, *};
|
||||
use frame_benchmarking::benchmarks;
|
||||
use frame_system::RawOrigin;
|
||||
use sp_core::H256;
|
||||
@@ -106,10 +106,7 @@ mod tests {
|
||||
);
|
||||
|
||||
println!("equivocation_proof: {:?}", equivocation_proof);
|
||||
println!(
|
||||
"equivocation_proof.encode(): {:?}",
|
||||
equivocation_proof.encode()
|
||||
);
|
||||
println!("equivocation_proof.encode(): {:?}", equivocation_proof.encode());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,8 @@
|
||||
//! This file was not auto-generated.
|
||||
|
||||
use frame_support::weights::{
|
||||
Weight, constants::{WEIGHT_PER_MICROS, WEIGHT_PER_NANOS, RocksDbWeight as DbWeight},
|
||||
constants::{RocksDbWeight as DbWeight, WEIGHT_PER_MICROS, WEIGHT_PER_NANOS},
|
||||
Weight,
|
||||
};
|
||||
|
||||
impl crate::WeightInfo for () {
|
||||
@@ -48,7 +49,6 @@ impl crate::WeightInfo for () {
|
||||
}
|
||||
|
||||
fn note_stalled() -> Weight {
|
||||
(3 * WEIGHT_PER_MICROS)
|
||||
.saturating_add(DbWeight::get().writes(1))
|
||||
(3 * WEIGHT_PER_MICROS).saturating_add(DbWeight::get().writes(1))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//!
|
||||
//! An opt-in utility module for reporting equivocations.
|
||||
//!
|
||||
//! This module defines an offence type for GRANDPA equivocations
|
||||
@@ -35,7 +34,6 @@
|
||||
//! When using this module for enabling equivocation reporting it is required
|
||||
//! that the `ValidateUnsigned` for the GRANDPA pallet is used in the runtime
|
||||
//! definition.
|
||||
//!
|
||||
|
||||
use sp_std::prelude::*;
|
||||
|
||||
@@ -54,7 +52,7 @@ use sp_staking::{
|
||||
SessionIndex,
|
||||
};
|
||||
|
||||
use super::{Call, Pallet, Config};
|
||||
use super::{Call, Config, Pallet};
|
||||
|
||||
/// A trait with utility methods for handling equivocation reports in GRANDPA.
|
||||
/// The offence type is generic, and the trait provides , reporting an offence
|
||||
@@ -130,9 +128,7 @@ pub struct EquivocationHandler<I, R, L, O = GrandpaEquivocationOffence<I>> {
|
||||
|
||||
impl<I, R, L, O> Default for EquivocationHandler<I, R, L, O> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
_phantom: Default::default(),
|
||||
}
|
||||
Self { _phantom: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,21 +205,22 @@ impl<T: Config> Pallet<T> {
|
||||
if let Call::report_equivocation_unsigned(equivocation_proof, key_owner_proof) = call {
|
||||
// discard equivocation report not coming from the local node
|
||||
match source {
|
||||
TransactionSource::Local | TransactionSource::InBlock => { /* allowed */ }
|
||||
TransactionSource::Local | TransactionSource::InBlock => { /* allowed */ },
|
||||
_ => {
|
||||
log::warn!(
|
||||
target: "runtime::afg",
|
||||
"rejecting unsigned report equivocation transaction because it is not local/in-block."
|
||||
);
|
||||
|
||||
return InvalidTransaction::Call.into();
|
||||
}
|
||||
return InvalidTransaction::Call.into()
|
||||
},
|
||||
}
|
||||
|
||||
// check report staleness
|
||||
is_known_offence::<T>(equivocation_proof, key_owner_proof)?;
|
||||
|
||||
let longevity = <T::HandleEquivocation as HandleEquivocation<T>>::ReportLongevity::get();
|
||||
let longevity =
|
||||
<T::HandleEquivocation as HandleEquivocation<T>>::ReportLongevity::get();
|
||||
|
||||
ValidTransaction::with_tag_prefix("GrandpaEquivocation")
|
||||
// We assign the maximum priority for any equivocation report.
|
||||
@@ -257,10 +254,7 @@ fn is_known_offence<T: Config>(
|
||||
key_owner_proof: &T::KeyOwnerProof,
|
||||
) -> Result<(), TransactionValidityError> {
|
||||
// check the membership proof to extract the offender's id
|
||||
let key = (
|
||||
sp_finality_grandpa::KEY_TYPE,
|
||||
equivocation_proof.offender().clone(),
|
||||
);
|
||||
let key = (sp_finality_grandpa::KEY_TYPE, equivocation_proof.offender().clone());
|
||||
|
||||
let offender = T::KeyOwnerProofSystem::check_proof(key, key_owner_proof.clone())
|
||||
.ok_or(InvalidTransaction::BadProof)?;
|
||||
|
||||
@@ -41,18 +41,16 @@ use fg_primitives::{
|
||||
};
|
||||
use frame_support::{
|
||||
dispatch::DispatchResultWithPostInfo,
|
||||
storage, traits::{OneSessionHandler, KeyOwnerProofSystem}, weights::{Pays, Weight},
|
||||
};
|
||||
use sp_runtime::{
|
||||
generic::DigestItem,
|
||||
traits::Zero,
|
||||
DispatchResult, KeyTypeId,
|
||||
storage,
|
||||
traits::{KeyOwnerProofSystem, OneSessionHandler},
|
||||
weights::{Pays, Weight},
|
||||
};
|
||||
use sp_runtime::{generic::DigestItem, traits::Zero, DispatchResult, KeyTypeId};
|
||||
use sp_session::{GetSessionNumber, GetValidatorCount};
|
||||
use sp_staking::SessionIndex;
|
||||
|
||||
mod equivocation;
|
||||
mod default_weights;
|
||||
mod equivocation;
|
||||
pub mod migrations;
|
||||
|
||||
#[cfg(any(feature = "runtime-benchmarks", test))]
|
||||
@@ -71,9 +69,9 @@ pub use pallet::*;
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_system::pallet_prelude::*;
|
||||
use super::*;
|
||||
|
||||
#[pallet::pallet]
|
||||
#[pallet::generate_store(pub(super) trait Store)]
|
||||
@@ -130,24 +128,20 @@ pub mod pallet {
|
||||
ScheduledChange {
|
||||
delay: pending_change.delay,
|
||||
next_authorities: pending_change.next_authorities.clone(),
|
||||
}
|
||||
},
|
||||
))
|
||||
} else {
|
||||
Self::deposit_log(ConsensusLog::ScheduledChange(
|
||||
ScheduledChange {
|
||||
delay: pending_change.delay,
|
||||
next_authorities: pending_change.next_authorities.clone(),
|
||||
}
|
||||
));
|
||||
Self::deposit_log(ConsensusLog::ScheduledChange(ScheduledChange {
|
||||
delay: pending_change.delay,
|
||||
next_authorities: pending_change.next_authorities.clone(),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
// enact the change if we've reached the enacting block
|
||||
if block_number == pending_change.scheduled_at + pending_change.delay {
|
||||
Self::set_grandpa_authorities(&pending_change.next_authorities);
|
||||
Self::deposit_event(
|
||||
Event::NewAuthorities(pending_change.next_authorities)
|
||||
);
|
||||
Self::deposit_event(Event::NewAuthorities(pending_change.next_authorities));
|
||||
<PendingChange<T>>::kill();
|
||||
}
|
||||
}
|
||||
@@ -197,11 +191,7 @@ pub mod pallet {
|
||||
) -> DispatchResultWithPostInfo {
|
||||
let reporter = ensure_signed(origin)?;
|
||||
|
||||
Self::do_report_equivocation(
|
||||
Some(reporter),
|
||||
equivocation_proof,
|
||||
key_owner_proof,
|
||||
)
|
||||
Self::do_report_equivocation(Some(reporter), equivocation_proof, key_owner_proof)
|
||||
}
|
||||
|
||||
/// Report voter equivocation/misbehavior. This method will verify the
|
||||
@@ -289,7 +279,8 @@ pub mod pallet {
|
||||
/// State of the current authority set.
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn state)]
|
||||
pub(super) type State<T: Config> = StorageValue<_, StoredState<T::BlockNumber>, ValueQuery, DefaultForState<T>>;
|
||||
pub(super) type State<T: Config> =
|
||||
StorageValue<_, StoredState<T::BlockNumber>, ValueQuery, DefaultForState<T>>;
|
||||
|
||||
/// Pending change: (signaled at, scheduled change).
|
||||
#[pallet::storage]
|
||||
@@ -328,9 +319,7 @@ pub mod pallet {
|
||||
#[cfg(feature = "std")]
|
||||
impl Default for GenesisConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
authorities: Default::default(),
|
||||
}
|
||||
Self { authorities: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -388,7 +377,7 @@ pub enum StoredState<N> {
|
||||
/// Block at which the intention to pause was scheduled.
|
||||
scheduled_at: N,
|
||||
/// Number of blocks after which the change will be enacted.
|
||||
delay: N
|
||||
delay: N,
|
||||
},
|
||||
/// The current GRANDPA authority set is paused.
|
||||
Paused,
|
||||
@@ -410,10 +399,7 @@ impl<T: Config> Pallet<T> {
|
||||
|
||||
/// Set the current set of authorities, along with their respective weights.
|
||||
fn set_grandpa_authorities(authorities: &AuthorityList) {
|
||||
storage::unhashed::put(
|
||||
GRANDPA_AUTHORITIES_KEY,
|
||||
&VersionedAuthorityList::from(authorities),
|
||||
);
|
||||
storage::unhashed::put(GRANDPA_AUTHORITIES_KEY, &VersionedAuthorityList::from(authorities));
|
||||
}
|
||||
|
||||
/// Schedule GRANDPA to pause starting in the given number of blocks.
|
||||
@@ -421,10 +407,7 @@ impl<T: Config> Pallet<T> {
|
||||
pub fn schedule_pause(in_blocks: T::BlockNumber) -> DispatchResult {
|
||||
if let StoredState::Live = <State<T>>::get() {
|
||||
let scheduled_at = <frame_system::Pallet<T>>::block_number();
|
||||
<State<T>>::put(StoredState::PendingPause {
|
||||
delay: in_blocks,
|
||||
scheduled_at,
|
||||
});
|
||||
<State<T>>::put(StoredState::PendingPause { delay: in_blocks, scheduled_at });
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
@@ -436,10 +419,7 @@ impl<T: Config> Pallet<T> {
|
||||
pub fn schedule_resume(in_blocks: T::BlockNumber) -> DispatchResult {
|
||||
if let StoredState::Paused = <State<T>>::get() {
|
||||
let scheduled_at = <frame_system::Pallet<T>>::block_number();
|
||||
<State<T>>::put(StoredState::PendingResume {
|
||||
delay: in_blocks,
|
||||
scheduled_at,
|
||||
});
|
||||
<State<T>>::put(StoredState::PendingResume { delay: in_blocks, scheduled_at });
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
@@ -502,10 +482,7 @@ impl<T: Config> Pallet<T> {
|
||||
// config builder or through `on_genesis_session`.
|
||||
fn initialize(authorities: &AuthorityList) {
|
||||
if !authorities.is_empty() {
|
||||
assert!(
|
||||
Self::grandpa_authorities().is_empty(),
|
||||
"Authorities are already initialized!"
|
||||
);
|
||||
assert!(Self::grandpa_authorities().is_empty(), "Authorities are already initialized!");
|
||||
Self::set_grandpa_authorities(authorities);
|
||||
}
|
||||
|
||||
@@ -530,16 +507,16 @@ impl<T: Config> Pallet<T> {
|
||||
let validator_count = key_owner_proof.validator_count();
|
||||
|
||||
// validate the key ownership proof extracting the id of the offender.
|
||||
let offender =
|
||||
T::KeyOwnerProofSystem::check_proof(
|
||||
(fg_primitives::KEY_TYPE, equivocation_proof.offender().clone()),
|
||||
key_owner_proof,
|
||||
).ok_or(Error::<T>::InvalidKeyOwnershipProof)?;
|
||||
let offender = T::KeyOwnerProofSystem::check_proof(
|
||||
(fg_primitives::KEY_TYPE, equivocation_proof.offender().clone()),
|
||||
key_owner_proof,
|
||||
)
|
||||
.ok_or(Error::<T>::InvalidKeyOwnershipProof)?;
|
||||
|
||||
// validate equivocation proof (check votes are different and
|
||||
// signatures are valid).
|
||||
if !sp_finality_grandpa::check_equivocation_proof(equivocation_proof) {
|
||||
return Err(Error::<T>::InvalidEquivocationProof.into());
|
||||
return Err(Error::<T>::InvalidEquivocationProof.into())
|
||||
}
|
||||
|
||||
// fetch the current and previous sets last session index. on the
|
||||
@@ -547,8 +524,8 @@ impl<T: Config> Pallet<T> {
|
||||
let previous_set_id_session_index = if set_id == 0 {
|
||||
None
|
||||
} else {
|
||||
let session_index =
|
||||
Self::session_for_set(set_id - 1).ok_or_else(|| Error::<T>::InvalidEquivocationProof)?;
|
||||
let session_index = Self::session_for_set(set_id - 1)
|
||||
.ok_or_else(|| Error::<T>::InvalidEquivocationProof)?;
|
||||
|
||||
Some(session_index)
|
||||
};
|
||||
@@ -560,10 +537,10 @@ impl<T: Config> Pallet<T> {
|
||||
// bounds of the set id reported in the equivocation.
|
||||
if session_index > set_id_session_index ||
|
||||
previous_set_id_session_index
|
||||
.map(|previous_index| session_index <= previous_index)
|
||||
.unwrap_or(false)
|
||||
.map(|previous_index| session_index <= previous_index)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return Err(Error::<T>::InvalidEquivocationProof.into());
|
||||
return Err(Error::<T>::InvalidEquivocationProof.into())
|
||||
}
|
||||
|
||||
// report to the offences module rewarding the sender.
|
||||
@@ -576,7 +553,8 @@ impl<T: Config> Pallet<T> {
|
||||
set_id,
|
||||
round,
|
||||
),
|
||||
).map_err(|_| Error::<T>::DuplicateOffenceReport)?;
|
||||
)
|
||||
.map_err(|_| Error::<T>::DuplicateOffenceReport)?;
|
||||
|
||||
// waive the fee since the report is valid and beneficial
|
||||
Ok(Pays::No.into())
|
||||
@@ -610,19 +588,22 @@ impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
|
||||
}
|
||||
|
||||
impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T>
|
||||
where T: pallet_session::Config
|
||||
where
|
||||
T: pallet_session::Config,
|
||||
{
|
||||
type Key = AuthorityId;
|
||||
|
||||
fn on_genesis_session<'a, I: 'a>(validators: I)
|
||||
where I: Iterator<Item=(&'a T::AccountId, AuthorityId)>
|
||||
where
|
||||
I: Iterator<Item = (&'a T::AccountId, AuthorityId)>,
|
||||
{
|
||||
let authorities = validators.map(|(_, k)| (k, 1)).collect::<Vec<_>>();
|
||||
Self::initialize(&authorities);
|
||||
}
|
||||
|
||||
fn on_new_session<'a, I: 'a>(changed: bool, validators: I, _queued_validators: I)
|
||||
where I: Iterator<Item=(&'a T::AccountId, AuthorityId)>
|
||||
where
|
||||
I: Iterator<Item = (&'a T::AccountId, AuthorityId)>,
|
||||
{
|
||||
// Always issue a change if `session` says that the validators have changed.
|
||||
// Even if their session keys are the same as before, the underlying economic
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
// limitations under the License.
|
||||
|
||||
use frame_support::{
|
||||
traits::{Get, GetPalletVersion, PalletVersion},
|
||||
weights::Weight,
|
||||
traits::{GetPalletVersion, PalletVersion, Get},
|
||||
};
|
||||
use sp_io::hashing::twox_128;
|
||||
|
||||
@@ -31,18 +31,15 @@ pub const OLD_PREFIX: &[u8] = b"GrandpaFinality";
|
||||
/// `<Runtime as frame_system::Config>::PalletInfo::name::<GrandpaPallet>`.
|
||||
///
|
||||
/// The old storage prefix, `GrandpaFinality` is hardcoded in the migration code.
|
||||
pub fn migrate<
|
||||
T: frame_system::Config,
|
||||
P: GetPalletVersion,
|
||||
N: AsRef<str>,
|
||||
>(new_pallet_name: N) -> Weight {
|
||||
|
||||
pub fn migrate<T: frame_system::Config, P: GetPalletVersion, N: AsRef<str>>(
|
||||
new_pallet_name: N,
|
||||
) -> Weight {
|
||||
if new_pallet_name.as_ref().as_bytes() == OLD_PREFIX {
|
||||
log::info!(
|
||||
target: "runtime::afg",
|
||||
"New pallet name is equal to the old prefix. No migration needs to be done.",
|
||||
);
|
||||
return 0;
|
||||
return 0
|
||||
}
|
||||
let maybe_storage_version = <P as GetPalletVersion>::storage_version();
|
||||
log::info!(
|
||||
@@ -59,7 +56,7 @@ pub fn migrate<
|
||||
new_pallet_name.as_ref().as_bytes(),
|
||||
);
|
||||
<T as frame_system::Config>::BlockWeights::get().max_block
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
log::warn!(
|
||||
target: "runtime::afg",
|
||||
@@ -75,11 +72,9 @@ pub fn migrate<
|
||||
/// [`frame_support::traits::OnRuntimeUpgrade::pre_upgrade`] for further testing.
|
||||
///
|
||||
/// Panics if anything goes wrong.
|
||||
pub fn pre_migration<
|
||||
T: frame_system::Config,
|
||||
P: GetPalletVersion + 'static,
|
||||
N: AsRef<str>,
|
||||
>(new: N) {
|
||||
pub fn pre_migration<T: frame_system::Config, P: GetPalletVersion + 'static, N: AsRef<str>>(
|
||||
new: N,
|
||||
) {
|
||||
let new = new.as_ref();
|
||||
log::info!("pre-migration grandpa test with new = {}", new);
|
||||
|
||||
@@ -119,10 +114,6 @@ pub fn post_migration<P: GetPalletVersion>() {
|
||||
log::info!("post-migration grandpa");
|
||||
|
||||
// Assert that nothing remains at the old prefix
|
||||
assert!(
|
||||
sp_io::storage::next_key(&twox_128(OLD_PREFIX)).map_or(
|
||||
true,
|
||||
|next_key| !next_key.starts_with(&twox_128(OLD_PREFIX))
|
||||
)
|
||||
);
|
||||
assert!(sp_io::storage::next_key(&twox_128(OLD_PREFIX))
|
||||
.map_or(true, |next_key| !next_key.starts_with(&twox_128(OLD_PREFIX))));
|
||||
}
|
||||
|
||||
@@ -19,13 +19,15 @@
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use crate::{AuthorityId, AuthorityList, ConsensusLog, Config, self as pallet_grandpa};
|
||||
use crate::{self as pallet_grandpa, AuthorityId, AuthorityList, Config, ConsensusLog};
|
||||
use ::grandpa as finality_grandpa;
|
||||
use codec::Encode;
|
||||
use frame_election_provider_support::onchain;
|
||||
use frame_support::{
|
||||
parameter_types,
|
||||
traits::{KeyOwnerProofSystem, OnFinalize, OnInitialize, GenesisBuild},
|
||||
traits::{GenesisBuild, KeyOwnerProofSystem, OnFinalize, OnInitialize},
|
||||
};
|
||||
use pallet_session::historical as pallet_session_historical;
|
||||
use pallet_staking::EraIndex;
|
||||
use sp_core::{crypto::KeyTypeId, H256};
|
||||
use sp_finality_grandpa::{RoundNumber, SetId, GRANDPA_ENGINE_ID};
|
||||
@@ -38,8 +40,6 @@ use sp_runtime::{
|
||||
DigestItem, Perbill,
|
||||
};
|
||||
use sp_staking::SessionIndex;
|
||||
use pallet_session::historical as pallet_session_historical;
|
||||
use frame_election_provider_support::onchain;
|
||||
|
||||
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
|
||||
type Block = frame_system::mocking::MockBlock<Test>;
|
||||
@@ -275,13 +275,9 @@ pub fn new_test_ext(vec: Vec<(u64, u64)>) -> sp_io::TestExternalities {
|
||||
}
|
||||
|
||||
pub fn new_test_ext_raw_authorities(authorities: AuthorityList) -> sp_io::TestExternalities {
|
||||
let mut t = frame_system::GenesisConfig::default()
|
||||
.build_storage::<Test>()
|
||||
.unwrap();
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
|
||||
let balances: Vec<_> = (0..authorities.len())
|
||||
.map(|i| (i as u64, 10_000_000))
|
||||
.collect();
|
||||
let balances: Vec<_> = (0..authorities.len()).map(|i| (i as u64, 10_000_000)).collect();
|
||||
|
||||
pallet_balances::GenesisConfig::<Test> { balances }
|
||||
.assimilate_storage(&mut t)
|
||||
@@ -295,9 +291,7 @@ pub fn new_test_ext_raw_authorities(authorities: AuthorityList) -> sp_io::TestEx
|
||||
(
|
||||
i as u64,
|
||||
i as u64,
|
||||
TestSessionKeys {
|
||||
grandpa_authority: AuthorityId::from(k.clone()),
|
||||
},
|
||||
TestSessionKeys { grandpa_authority: AuthorityId::from(k.clone()) },
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
@@ -311,12 +305,7 @@ pub fn new_test_ext_raw_authorities(authorities: AuthorityList) -> sp_io::TestEx
|
||||
// controllers are the index + 1000
|
||||
let stakers: Vec<_> = (0..authorities.len())
|
||||
.map(|i| {
|
||||
(
|
||||
i as u64,
|
||||
i as u64 + 1000,
|
||||
10_000,
|
||||
pallet_staking::StakerStatus::<u64>::Validator,
|
||||
)
|
||||
(i as u64, i as u64 + 1000, 10_000, pallet_staking::StakerStatus::<u64>::Validator)
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -348,12 +337,7 @@ pub fn start_session(session_index: SessionIndex) {
|
||||
System::parent_hash()
|
||||
};
|
||||
|
||||
System::initialize(
|
||||
&(i as u64 + 1),
|
||||
&parent_hash,
|
||||
&Default::default(),
|
||||
Default::default(),
|
||||
);
|
||||
System::initialize(&(i as u64 + 1), &parent_hash, &Default::default(), Default::default());
|
||||
System::set_block_number((i + 1).into());
|
||||
Timestamp::set_timestamp(System::block_number() * 6000);
|
||||
|
||||
@@ -372,12 +356,7 @@ pub fn start_era(era_index: EraIndex) {
|
||||
}
|
||||
|
||||
pub fn initialize_block(number: u64, parent_hash: H256) {
|
||||
System::initialize(
|
||||
&number,
|
||||
&parent_hash,
|
||||
&Default::default(),
|
||||
Default::default(),
|
||||
);
|
||||
System::initialize(&number, &parent_hash, &Default::default(), Default::default());
|
||||
}
|
||||
|
||||
pub fn generate_equivocation_proof(
|
||||
@@ -386,10 +365,7 @@ pub fn generate_equivocation_proof(
|
||||
vote2: (RoundNumber, H256, u64, &Ed25519Keyring),
|
||||
) -> sp_finality_grandpa::EquivocationProof<H256, u64> {
|
||||
let signed_prevote = |round, hash, number, keyring: &Ed25519Keyring| {
|
||||
let prevote = finality_grandpa::Prevote {
|
||||
target_hash: hash,
|
||||
target_number: number,
|
||||
};
|
||||
let prevote = finality_grandpa::Prevote { target_hash: hash, target_number: number };
|
||||
|
||||
let prevote_msg = finality_grandpa::Message::Prevote(prevote.clone());
|
||||
let payload = sp_finality_grandpa::localized_payload(round, set_id, &prevote_msg);
|
||||
|
||||
@@ -24,7 +24,7 @@ use crate::mock::*;
|
||||
use codec::Encode;
|
||||
use fg_primitives::ScheduledChange;
|
||||
use frame_support::{
|
||||
assert_err, assert_ok, assert_noop,
|
||||
assert_err, assert_noop, assert_ok,
|
||||
traits::{Currency, OnFinalize, OneSessionHandler},
|
||||
weights::{GetDispatchInfo, Pays},
|
||||
};
|
||||
@@ -43,21 +43,24 @@ fn authorities_change_logged() {
|
||||
Grandpa::on_finalize(1);
|
||||
|
||||
let header = System::finalize();
|
||||
assert_eq!(header.digest, Digest {
|
||||
logs: vec![
|
||||
grandpa_log(ConsensusLog::ScheduledChange(
|
||||
ScheduledChange { delay: 0, next_authorities: to_authorities(vec![(4, 1), (5, 1), (6, 1)]) }
|
||||
)),
|
||||
],
|
||||
});
|
||||
assert_eq!(
|
||||
header.digest,
|
||||
Digest {
|
||||
logs: vec![grandpa_log(ConsensusLog::ScheduledChange(ScheduledChange {
|
||||
delay: 0,
|
||||
next_authorities: to_authorities(vec![(4, 1), (5, 1), (6, 1)])
|
||||
})),],
|
||||
}
|
||||
);
|
||||
|
||||
assert_eq!(System::events(), vec![
|
||||
EventRecord {
|
||||
assert_eq!(
|
||||
System::events(),
|
||||
vec![EventRecord {
|
||||
phase: Phase::Finalization,
|
||||
event: Event::NewAuthorities(to_authorities(vec![(4, 1), (5, 1), (6, 1)])).into(),
|
||||
topics: vec![],
|
||||
},
|
||||
]);
|
||||
},]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -68,13 +71,15 @@ fn authorities_change_logged_after_delay() {
|
||||
Grandpa::schedule_change(to_authorities(vec![(4, 1), (5, 1), (6, 1)]), 1, None).unwrap();
|
||||
Grandpa::on_finalize(1);
|
||||
let header = System::finalize();
|
||||
assert_eq!(header.digest, Digest {
|
||||
logs: vec![
|
||||
grandpa_log(ConsensusLog::ScheduledChange(
|
||||
ScheduledChange { delay: 1, next_authorities: to_authorities(vec![(4, 1), (5, 1), (6, 1)]) }
|
||||
)),
|
||||
],
|
||||
});
|
||||
assert_eq!(
|
||||
header.digest,
|
||||
Digest {
|
||||
logs: vec![grandpa_log(ConsensusLog::ScheduledChange(ScheduledChange {
|
||||
delay: 1,
|
||||
next_authorities: to_authorities(vec![(4, 1), (5, 1), (6, 1)])
|
||||
})),],
|
||||
}
|
||||
);
|
||||
|
||||
// no change at this height.
|
||||
assert_eq!(System::events(), vec![]);
|
||||
@@ -84,13 +89,14 @@ fn authorities_change_logged_after_delay() {
|
||||
Grandpa::on_finalize(2);
|
||||
|
||||
let _header = System::finalize();
|
||||
assert_eq!(System::events(), vec![
|
||||
EventRecord {
|
||||
assert_eq!(
|
||||
System::events(),
|
||||
vec![EventRecord {
|
||||
phase: Phase::Finalization,
|
||||
event: Event::NewAuthorities(to_authorities(vec![(4, 1), (5, 1), (6, 1)])).into(),
|
||||
topics: vec![],
|
||||
},
|
||||
]);
|
||||
},]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -131,11 +137,7 @@ fn cannot_schedule_change_when_one_pending() {
|
||||
fn dispatch_forced_change() {
|
||||
new_test_ext(vec![(1, 1), (2, 1), (3, 1)]).execute_with(|| {
|
||||
initialize_block(1, Default::default());
|
||||
Grandpa::schedule_change(
|
||||
to_authorities(vec![(4, 1), (5, 1), (6, 1)]),
|
||||
5,
|
||||
Some(0),
|
||||
).unwrap();
|
||||
Grandpa::schedule_change(to_authorities(vec![(4, 1), (5, 1), (6, 1)]), 5, Some(0)).unwrap();
|
||||
|
||||
assert!(<PendingChange<Test>>::exists());
|
||||
assert_noop!(
|
||||
@@ -168,7 +170,10 @@ fn dispatch_forced_change() {
|
||||
{
|
||||
initialize_block(7, header.hash());
|
||||
assert!(!<PendingChange<Test>>::exists());
|
||||
assert_eq!(Grandpa::grandpa_authorities(), to_authorities(vec![(4, 1), (5, 1), (6, 1)]));
|
||||
assert_eq!(
|
||||
Grandpa::grandpa_authorities(),
|
||||
to_authorities(vec![(4, 1), (5, 1), (6, 1)])
|
||||
);
|
||||
assert_ok!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None));
|
||||
Grandpa::on_finalize(7);
|
||||
header = System::finalize();
|
||||
@@ -178,7 +183,10 @@ fn dispatch_forced_change() {
|
||||
{
|
||||
initialize_block(8, header.hash());
|
||||
assert!(<PendingChange<Test>>::exists());
|
||||
assert_eq!(Grandpa::grandpa_authorities(), to_authorities(vec![(4, 1), (5, 1), (6, 1)]));
|
||||
assert_eq!(
|
||||
Grandpa::grandpa_authorities(),
|
||||
to_authorities(vec![(4, 1), (5, 1), (6, 1)])
|
||||
);
|
||||
assert_noop!(
|
||||
Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None),
|
||||
Error::<Test>::ChangePending
|
||||
@@ -205,7 +213,11 @@ fn dispatch_forced_change() {
|
||||
{
|
||||
initialize_block(11, header.hash());
|
||||
assert!(!<PendingChange<Test>>::exists());
|
||||
assert_ok!(Grandpa::schedule_change(to_authorities(vec![(5, 1), (6, 1), (7, 1)]), 5, Some(0)));
|
||||
assert_ok!(Grandpa::schedule_change(
|
||||
to_authorities(vec![(5, 1), (6, 1), (7, 1)]),
|
||||
5,
|
||||
Some(0)
|
||||
));
|
||||
assert_eq!(Grandpa::next_forced(), Some(21));
|
||||
Grandpa::on_finalize(11);
|
||||
header = System::finalize();
|
||||
@@ -222,13 +234,7 @@ fn schedule_pause_only_when_live() {
|
||||
Grandpa::schedule_pause(1).unwrap();
|
||||
|
||||
// we've switched to the pending pause state
|
||||
assert_eq!(
|
||||
Grandpa::state(),
|
||||
StoredState::PendingPause {
|
||||
scheduled_at: 1u64,
|
||||
delay: 1,
|
||||
},
|
||||
);
|
||||
assert_eq!(Grandpa::state(), StoredState::PendingPause { scheduled_at: 1u64, delay: 1 },);
|
||||
|
||||
Grandpa::on_finalize(1);
|
||||
let _ = System::finalize();
|
||||
@@ -242,10 +248,7 @@ fn schedule_pause_only_when_live() {
|
||||
let _ = System::finalize();
|
||||
|
||||
// after finalizing block 2 the set should have switched to paused state
|
||||
assert_eq!(
|
||||
Grandpa::state(),
|
||||
StoredState::Paused,
|
||||
);
|
||||
assert_eq!(Grandpa::state(), StoredState::Paused,);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -257,20 +260,14 @@ fn schedule_resume_only_when_paused() {
|
||||
// the set is currently live, resuming it is an error
|
||||
assert_noop!(Grandpa::schedule_resume(1), Error::<Test>::ResumeFailed);
|
||||
|
||||
assert_eq!(
|
||||
Grandpa::state(),
|
||||
StoredState::Live,
|
||||
);
|
||||
assert_eq!(Grandpa::state(), StoredState::Live,);
|
||||
|
||||
// we schedule a pause to be applied instantly
|
||||
Grandpa::schedule_pause(0).unwrap();
|
||||
Grandpa::on_finalize(1);
|
||||
let _ = System::finalize();
|
||||
|
||||
assert_eq!(
|
||||
Grandpa::state(),
|
||||
StoredState::Paused,
|
||||
);
|
||||
assert_eq!(Grandpa::state(), StoredState::Paused,);
|
||||
|
||||
// we schedule the set to go back live in 2 blocks
|
||||
initialize_block(2, Default::default());
|
||||
@@ -287,10 +284,7 @@ fn schedule_resume_only_when_paused() {
|
||||
let _ = System::finalize();
|
||||
|
||||
// it should be live at block 4
|
||||
assert_eq!(
|
||||
Grandpa::state(),
|
||||
StoredState::Live,
|
||||
);
|
||||
assert_eq!(Grandpa::state(), StoredState::Live,);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -298,26 +292,11 @@ fn schedule_resume_only_when_paused() {
|
||||
fn time_slot_have_sane_ord() {
|
||||
// Ensure that `Ord` implementation is sane.
|
||||
const FIXTURE: &[GrandpaTimeSlot] = &[
|
||||
GrandpaTimeSlot {
|
||||
set_id: 0,
|
||||
round: 0,
|
||||
},
|
||||
GrandpaTimeSlot {
|
||||
set_id: 0,
|
||||
round: 1,
|
||||
},
|
||||
GrandpaTimeSlot {
|
||||
set_id: 1,
|
||||
round: 0,
|
||||
},
|
||||
GrandpaTimeSlot {
|
||||
set_id: 1,
|
||||
round: 1,
|
||||
},
|
||||
GrandpaTimeSlot {
|
||||
set_id: 1,
|
||||
round: 2,
|
||||
}
|
||||
GrandpaTimeSlot { set_id: 0, round: 0 },
|
||||
GrandpaTimeSlot { set_id: 0, round: 1 },
|
||||
GrandpaTimeSlot { set_id: 1, round: 0 },
|
||||
GrandpaTimeSlot { set_id: 1, round: 1 },
|
||||
GrandpaTimeSlot { set_id: 1, round: 2 },
|
||||
];
|
||||
assert!(FIXTURE.windows(2).all(|f| f[0] < f[1]));
|
||||
}
|
||||
@@ -325,16 +304,9 @@ fn time_slot_have_sane_ord() {
|
||||
/// Returns a list with 3 authorities with known keys:
|
||||
/// Alice, Bob and Charlie.
|
||||
pub fn test_authorities() -> AuthorityList {
|
||||
let authorities = vec![
|
||||
Ed25519Keyring::Alice,
|
||||
Ed25519Keyring::Bob,
|
||||
Ed25519Keyring::Charlie,
|
||||
];
|
||||
let authorities = vec![Ed25519Keyring::Alice, Ed25519Keyring::Bob, Ed25519Keyring::Charlie];
|
||||
|
||||
authorities
|
||||
.into_iter()
|
||||
.map(|id| (id.public().into(), 1u64))
|
||||
.collect()
|
||||
authorities.into_iter().map(|id| (id.public().into(), 1u64)).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -357,11 +329,7 @@ fn report_equivocation_current_set_works() {
|
||||
|
||||
assert_eq!(
|
||||
Staking::eras_stakers(1, validator),
|
||||
pallet_staking::Exposure {
|
||||
total: 10_000,
|
||||
own: 10_000,
|
||||
others: vec![],
|
||||
},
|
||||
pallet_staking::Exposure { total: 10_000, own: 10_000, others: vec![] },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -384,13 +352,11 @@ fn report_equivocation_current_set_works() {
|
||||
Historical::prove((sp_finality_grandpa::KEY_TYPE, &equivocation_key)).unwrap();
|
||||
|
||||
// report the equivocation and the tx should be dispatched successfully
|
||||
assert_ok!(
|
||||
Grandpa::report_equivocation_unsigned(
|
||||
Origin::none(),
|
||||
equivocation_proof,
|
||||
key_owner_proof,
|
||||
),
|
||||
);
|
||||
assert_ok!(Grandpa::report_equivocation_unsigned(
|
||||
Origin::none(),
|
||||
equivocation_proof,
|
||||
key_owner_proof,
|
||||
),);
|
||||
|
||||
start_era(2);
|
||||
|
||||
@@ -401,17 +367,13 @@ fn report_equivocation_current_set_works() {
|
||||
assert_eq!(Staking::slashable_balance_of(&equivocation_validator_id), 0);
|
||||
assert_eq!(
|
||||
Staking::eras_stakers(2, equivocation_validator_id),
|
||||
pallet_staking::Exposure {
|
||||
total: 0,
|
||||
own: 0,
|
||||
others: vec![],
|
||||
},
|
||||
pallet_staking::Exposure { total: 0, own: 0, others: vec![] },
|
||||
);
|
||||
|
||||
// check that the balances of all other validators are left intact.
|
||||
for validator in &validators {
|
||||
if *validator == equivocation_validator_id {
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
|
||||
assert_eq!(Balances::total_balance(validator), 10_000_000);
|
||||
@@ -419,11 +381,7 @@ fn report_equivocation_current_set_works() {
|
||||
|
||||
assert_eq!(
|
||||
Staking::eras_stakers(2, validator),
|
||||
pallet_staking::Exposure {
|
||||
total: 10_000,
|
||||
own: 10_000,
|
||||
others: vec![],
|
||||
},
|
||||
pallet_staking::Exposure { total: 10_000, own: 10_000, others: vec![] },
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -455,11 +413,7 @@ fn report_equivocation_old_set_works() {
|
||||
|
||||
assert_eq!(
|
||||
Staking::eras_stakers(2, validator),
|
||||
pallet_staking::Exposure {
|
||||
total: 10_000,
|
||||
own: 10_000,
|
||||
others: vec![],
|
||||
},
|
||||
pallet_staking::Exposure { total: 10_000, own: 10_000, others: vec![] },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -476,13 +430,11 @@ fn report_equivocation_old_set_works() {
|
||||
|
||||
// report the equivocation using the key ownership proof generated on
|
||||
// the old set, the tx should be dispatched successfully
|
||||
assert_ok!(
|
||||
Grandpa::report_equivocation_unsigned(
|
||||
Origin::none(),
|
||||
equivocation_proof,
|
||||
key_owner_proof,
|
||||
),
|
||||
);
|
||||
assert_ok!(Grandpa::report_equivocation_unsigned(
|
||||
Origin::none(),
|
||||
equivocation_proof,
|
||||
key_owner_proof,
|
||||
),);
|
||||
|
||||
start_era(3);
|
||||
|
||||
@@ -494,17 +446,13 @@ fn report_equivocation_old_set_works() {
|
||||
|
||||
assert_eq!(
|
||||
Staking::eras_stakers(3, equivocation_validator_id),
|
||||
pallet_staking::Exposure {
|
||||
total: 0,
|
||||
own: 0,
|
||||
others: vec![],
|
||||
},
|
||||
pallet_staking::Exposure { total: 0, own: 0, others: vec![] },
|
||||
);
|
||||
|
||||
// check that the balances of all other validators are left intact.
|
||||
for validator in &validators {
|
||||
if *validator == equivocation_validator_id {
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
|
||||
assert_eq!(Balances::total_balance(validator), 10_000_000);
|
||||
@@ -512,11 +460,7 @@ fn report_equivocation_old_set_works() {
|
||||
|
||||
assert_eq!(
|
||||
Staking::eras_stakers(3, validator),
|
||||
pallet_staking::Exposure {
|
||||
total: 10_000,
|
||||
own: 10_000,
|
||||
others: vec![],
|
||||
},
|
||||
pallet_staking::Exposure { total: 10_000, own: 10_000, others: vec![] },
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -737,10 +681,8 @@ fn report_equivocation_validate_unsigned_prevents_duplicates() {
|
||||
let key_owner_proof =
|
||||
Historical::prove((sp_finality_grandpa::KEY_TYPE, &equivocation_key)).unwrap();
|
||||
|
||||
let call = Call::report_equivocation_unsigned(
|
||||
equivocation_proof.clone(),
|
||||
key_owner_proof.clone(),
|
||||
);
|
||||
let call =
|
||||
Call::report_equivocation_unsigned(equivocation_proof.clone(), key_owner_proof.clone());
|
||||
|
||||
// only local/inblock reports are allowed
|
||||
assert_eq!(
|
||||
@@ -752,11 +694,7 @@ fn report_equivocation_validate_unsigned_prevents_duplicates() {
|
||||
);
|
||||
|
||||
// the transaction is valid when passed as local
|
||||
let tx_tag = (
|
||||
equivocation_key,
|
||||
set_id,
|
||||
1u64,
|
||||
);
|
||||
let tx_tag = (equivocation_key, set_id, 1u64);
|
||||
|
||||
assert_eq!(
|
||||
<Grandpa as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
|
||||
@@ -861,23 +799,19 @@ fn always_schedules_a_change_on_new_session_when_stalled() {
|
||||
fn report_equivocation_has_valid_weight() {
|
||||
// the weight depends on the size of the validator set,
|
||||
// but there's a lower bound of 100 validators.
|
||||
assert!(
|
||||
(1..=100)
|
||||
.map(<Test as Config>::WeightInfo::report_equivocation)
|
||||
.collect::<Vec<_>>()
|
||||
.windows(2)
|
||||
.all(|w| w[0] == w[1])
|
||||
);
|
||||
assert!((1..=100)
|
||||
.map(<Test as Config>::WeightInfo::report_equivocation)
|
||||
.collect::<Vec<_>>()
|
||||
.windows(2)
|
||||
.all(|w| w[0] == w[1]));
|
||||
|
||||
// after 100 validators the weight should keep increasing
|
||||
// with every extra validator.
|
||||
assert!(
|
||||
(100..=1000)
|
||||
.map(<Test as Config>::WeightInfo::report_equivocation)
|
||||
.collect::<Vec<_>>()
|
||||
.windows(2)
|
||||
.all(|w| w[0] < w[1])
|
||||
);
|
||||
assert!((100..=1000)
|
||||
.map(<Test as Config>::WeightInfo::report_equivocation)
|
||||
.collect::<Vec<_>>()
|
||||
.windows(2)
|
||||
.all(|w| w[0] < w[1]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user