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

Commit

Permalink
Generate storage info for aura pallet (#9371)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
KiChjang and kianenigma authored Sep 2, 2021
1 parent 152f764 commit 6d0c04d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
7 changes: 6 additions & 1 deletion bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -382,7 +387,7 @@ impl_runtime_apis! {
}

fn authorities() -> Vec<AuraId> {
Aura::authorities()
Aura::authorities().into_inner()
}
}

Expand Down
4 changes: 1 addition & 3 deletions frame/aura/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
35 changes: 24 additions & 11 deletions frame/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@

#![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::{
generic::DigestItem,
traits::{IsMember, Member, SaturatedConversion, Saturating, Zero},
RuntimeAppPublic,
};
use sp_std::prelude::*;
use sp_std::{convert::TryFrom, vec::Vec};

pub mod migrations;
mod mock;
Expand All @@ -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
Expand All @@ -79,6 +82,7 @@ pub mod pallet {
}

#[pallet::pallet]
#[pallet::generate_storage_info]
pub struct Pallet<T>(sp_std::marker::PhantomData<T>);

#[pallet::hooks]
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions frame/aura/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion frame/support/src/storage/bounded_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use crate::{
storage::{StorageDecodeLength, StorageTryAppend},
traits::Get,
WeakBoundedVec,
};
use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
use core::{
Expand All @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion primitives/consensus/slots/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down

0 comments on commit 6d0c04d

Please sign in to comment.