mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 05:51:02 +00:00
Generate storage info for aura pallet (#9371)
* Generate storage info for aura pallet * Add MaxAuthorities to node-template aura pallet config * Fix compilation errors on node-template * Use WeakBoundedVec instead of BoundedVec * Improve comment on BoundedSlice's EncodeLike impl Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Bump MaxAuthorities count to 32 for node template * cargo fmt * cargo fmt Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
@@ -195,9 +195,14 @@ impl frame_system::Config for Runtime {
|
||||
|
||||
impl pallet_randomness_collective_flip::Config for Runtime {}
|
||||
|
||||
parameter_types! {
|
||||
pub const MaxAuthorities: u32 = 32;
|
||||
}
|
||||
|
||||
impl pallet_aura::Config for Runtime {
|
||||
type AuthorityId = AuraId;
|
||||
type DisabledValidators = ();
|
||||
type MaxAuthorities = MaxAuthorities;
|
||||
}
|
||||
|
||||
impl pallet_grandpa::Config for Runtime {
|
||||
@@ -382,7 +387,7 @@ impl_runtime_apis! {
|
||||
}
|
||||
|
||||
fn authorities() -> Vec<AuraId> {
|
||||
Aura::authorities()
|
||||
Aura::authorities().into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,9 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
sp-application-crypto = { version = "4.0.0-dev", default-features = false, path = "../../primitives/application-crypto" }
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = [
|
||||
"derive",
|
||||
] }
|
||||
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false, features = ["derive", "max-encoded-len"] }
|
||||
sp-std = { version = "4.0.0-dev", default-features = false, path = "../../primitives/std" }
|
||||
sp-runtime = { version = "4.0.0-dev", default-features = false, path = "../../primitives/runtime" }
|
||||
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
|
||||
|
||||
@@ -38,10 +38,10 @@
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
use codec::{Decode, Encode, MaxEncodedLen};
|
||||
use frame_support::{
|
||||
traits::{DisabledValidators, FindAuthor, Get, OnTimestampSet, OneSessionHandler},
|
||||
ConsensusEngineId, Parameter,
|
||||
BoundedSlice, ConsensusEngineId, Parameter, WeakBoundedVec,
|
||||
};
|
||||
use sp_consensus_aura::{AuthorityIndex, ConsensusLog, Slot, AURA_ENGINE_ID};
|
||||
use sp_runtime::{
|
||||
@@ -49,7 +49,7 @@ use sp_runtime::{
|
||||
traits::{IsMember, Member, SaturatedConversion, Saturating, Zero},
|
||||
RuntimeAppPublic,
|
||||
};
|
||||
use sp_std::prelude::*;
|
||||
use sp_std::{convert::TryFrom, vec::Vec};
|
||||
|
||||
pub mod migrations;
|
||||
mod mock;
|
||||
@@ -70,7 +70,10 @@ pub mod pallet {
|
||||
+ Parameter
|
||||
+ RuntimeAppPublic
|
||||
+ Default
|
||||
+ MaybeSerializeDeserialize;
|
||||
+ MaybeSerializeDeserialize
|
||||
+ MaxEncodedLen;
|
||||
/// The maximum number of authorities that the pallet can hold.
|
||||
type MaxAuthorities: Get<u32>;
|
||||
|
||||
/// A way to check whether a given validator is disabled and should not be authoring blocks.
|
||||
/// Blocks authored by a disabled validator will lead to a panic as part of this module's
|
||||
@@ -79,6 +82,7 @@ pub mod pallet {
|
||||
}
|
||||
|
||||
#[pallet::pallet]
|
||||
#[pallet::generate_storage_info]
|
||||
pub struct Pallet<T>(sp_std::marker::PhantomData<T>);
|
||||
|
||||
#[pallet::hooks]
|
||||
@@ -113,7 +117,8 @@ pub mod pallet {
|
||||
/// The current authority set.
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn authorities)]
|
||||
pub(super) type Authorities<T: Config> = StorageValue<_, Vec<T::AuthorityId>, ValueQuery>;
|
||||
pub(super) type Authorities<T: Config> =
|
||||
StorageValue<_, WeakBoundedVec<T::AuthorityId, T::MaxAuthorities>, ValueQuery>;
|
||||
|
||||
/// The current slot of this block.
|
||||
///
|
||||
@@ -143,18 +148,22 @@ pub mod pallet {
|
||||
}
|
||||
|
||||
impl<T: Config> Pallet<T> {
|
||||
fn change_authorities(new: Vec<T::AuthorityId>) {
|
||||
fn change_authorities(new: WeakBoundedVec<T::AuthorityId, T::MaxAuthorities>) {
|
||||
<Authorities<T>>::put(&new);
|
||||
|
||||
let log: DigestItem<T::Hash> =
|
||||
DigestItem::Consensus(AURA_ENGINE_ID, ConsensusLog::AuthoritiesChange(new).encode());
|
||||
let log: DigestItem<T::Hash> = DigestItem::Consensus(
|
||||
AURA_ENGINE_ID,
|
||||
ConsensusLog::AuthoritiesChange(new.into_inner()).encode(),
|
||||
);
|
||||
<frame_system::Pallet<T>>::deposit_log(log.into());
|
||||
}
|
||||
|
||||
fn initialize_authorities(authorities: &[T::AuthorityId]) {
|
||||
if !authorities.is_empty() {
|
||||
assert!(<Authorities<T>>::get().is_empty(), "Authorities are already initialized!");
|
||||
<Authorities<T>>::put(authorities);
|
||||
let bounded = <BoundedSlice<'_, _, T::MaxAuthorities>>::try_from(authorities)
|
||||
.expect("Initial authority set must be less than T::MaxAuthorities");
|
||||
<Authorities<T>>::put(bounded);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,8 +211,12 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
|
||||
if changed {
|
||||
let next_authorities = validators.map(|(_, k)| k).collect::<Vec<_>>();
|
||||
let last_authorities = Self::authorities();
|
||||
if next_authorities != last_authorities {
|
||||
Self::change_authorities(next_authorities);
|
||||
if last_authorities != next_authorities {
|
||||
let bounded = <WeakBoundedVec<_, T::MaxAuthorities>>::force_from(
|
||||
next_authorities,
|
||||
Some("AuRa new session"),
|
||||
);
|
||||
Self::change_authorities(bounded);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,6 +87,10 @@ impl pallet_timestamp::Config for Test {
|
||||
type WeightInfo = ();
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub const MaxAuthorities: u32 = 10;
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static DISABLED_VALIDATORS: RefCell<Vec<AuthorityIndex>> = RefCell::new(Default::default());
|
||||
}
|
||||
@@ -113,6 +117,7 @@ impl DisabledValidators for MockDisabledValidators {
|
||||
impl pallet_aura::Config for Test {
|
||||
type AuthorityId = AuthorityId;
|
||||
type DisabledValidators = MockDisabledValidators;
|
||||
type MaxAuthorities = MaxAuthorities;
|
||||
}
|
||||
|
||||
pub fn new_test_ext(authorities: Vec<u64>) -> sp_io::TestExternalities {
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
use crate::{
|
||||
storage::{StorageDecodeLength, StorageTryAppend},
|
||||
traits::Get,
|
||||
WeakBoundedVec,
|
||||
};
|
||||
use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
|
||||
use core::{
|
||||
@@ -45,8 +46,13 @@ pub struct BoundedVec<T, S>(Vec<T>, PhantomData<S>);
|
||||
#[derive(Encode)]
|
||||
pub struct BoundedSlice<'a, T, S>(&'a [T], PhantomData<S>);
|
||||
|
||||
// `BoundedSlice`s encode to something which will always decode into a `BoundedVec` or a `Vec`.
|
||||
// `BoundedSlice`s encode to something which will always decode into a `BoundedVec`,
|
||||
// `WeakBoundedVec`, or a `Vec`.
|
||||
impl<'a, T: Encode + Decode, S: Get<u32>> EncodeLike<BoundedVec<T, S>> for BoundedSlice<'a, T, S> {}
|
||||
impl<'a, T: Encode + Decode, S: Get<u32>> EncodeLike<WeakBoundedVec<T, S>>
|
||||
for BoundedSlice<'a, T, S>
|
||||
{
|
||||
}
|
||||
impl<'a, T: Encode + Decode, S: Get<u32>> EncodeLike<Vec<T>> for BoundedSlice<'a, T, S> {}
|
||||
|
||||
impl<'a, T, S: Get<u32>> TryFrom<&'a [T]> for BoundedSlice<'a, T, S> {
|
||||
|
||||
@@ -13,7 +13,7 @@ readme = "README.md"
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
|
||||
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false, features = ["derive", "max-encoded-len"] }
|
||||
sp-runtime = { version = "4.0.0-dev", default-features = false, path = "../../runtime" }
|
||||
sp-arithmetic = { version = "4.0.0-dev", default-features = false, path = "../../arithmetic" }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user