From 60595deee85dd65dc2c2a9fd22ce41d666497e64 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Sun, 18 Jul 2021 15:32:08 -0700 Subject: [PATCH 1/8] Generate storage info for aura pallet --- frame/aura/Cargo.toml | 2 +- frame/aura/src/lib.rs | 30 +++++++++++++++++++-------- frame/aura/src/mock.rs | 5 +++++ primitives/consensus/slots/Cargo.toml | 2 +- primitives/consensus/slots/src/lib.rs | 4 ++-- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/frame/aura/Cargo.toml b/frame/aura/Cargo.toml index 9c4a31017bac5..fac09a954991a 100644 --- a/frame/aura/Cargo.toml +++ b/frame/aura/Cargo.toml @@ -14,7 +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" } pallet-session = { version = "4.0.0-dev", default-features = false, path = "../session" } sp-runtime = { version = "4.0.0-dev", default-features = false, path = "../../primitives/runtime" } diff --git a/frame/aura/src/lib.rs b/frame/aura/src/lib.rs index 7cc9412776df7..2cb63b99357a1 100644 --- a/frame/aura/src/lib.rs +++ b/frame/aura/src/lib.rs @@ -37,10 +37,11 @@ #![cfg_attr(not(feature = "std"), no_std)] -use sp_std::prelude::*; -use codec::{Encode, Decode}; +use sp_std::{convert::{TryFrom, TryInto}, prelude::*}; +use codec::{Encode, Decode, MaxEncodedLen}; use frame_support::{ Parameter, traits::{Get, FindAuthor, OneSessionHandler, OnTimestampSet}, ConsensusEngineId, + BoundedSlice, BoundedVec, }; use sp_runtime::{ RuntimeAppPublic, @@ -63,10 +64,14 @@ pub mod pallet { #[pallet::config] pub trait Config: pallet_timestamp::Config + frame_system::Config { /// The identifier type for an authority. - type AuthorityId: Member + Parameter + RuntimeAppPublic + Default + MaybeSerializeDeserialize; + type AuthorityId: Member + Parameter + RuntimeAppPublic + Default + + MaybeSerializeDeserialize + MaxEncodedLen; + /// The maximum number of authorities that the pallet can hold. + type MaxAuthorities: Get; } #[pallet::pallet] + #[pallet::generate_storage_info] pub struct Pallet(sp_std::marker::PhantomData); #[pallet::hooks] @@ -90,7 +95,8 @@ pub mod pallet { /// The current authority set. #[pallet::storage] #[pallet::getter(fn authorities)] - pub(super) type Authorities = StorageValue<_, Vec, ValueQuery>; + pub(super) type Authorities = + StorageValue<_, BoundedVec, ValueQuery>; /// The current slot of this block. /// @@ -120,12 +126,12 @@ pub mod pallet { } impl Pallet { - fn change_authorities(new: Vec) { + fn change_authorities(new: BoundedVec) { >::put(&new); let log: DigestItem = DigestItem::Consensus( AURA_ENGINE_ID, - ConsensusLog::AuthoritiesChange(new).encode() + ConsensusLog::AuthoritiesChange(new.into_inner()).encode() ); >::deposit_log(log.into()); } @@ -133,7 +139,9 @@ impl Pallet { fn initialize_authorities(authorities: &[T::AuthorityId]) { if !authorities.is_empty() { assert!(>::get().is_empty(), "Authorities are already initialized!"); - >::put(authorities); + let bounded = >::try_from(authorities) + .expect("Initial authority set must be less than T::MaxAuthorities"); + >::put(bounded); } } @@ -179,8 +187,12 @@ impl OneSessionHandler for Pallet { if changed { let next_authorities = validators.map(|(_, k)| k).collect::>(); let last_authorities = Self::authorities(); - if next_authorities != last_authorities { - Self::change_authorities(next_authorities); + if last_authorities != next_authorities { + let bounded = match next_authorities.try_into() { + Ok(v) => v, + Err(_) => return, + }; + Self::change_authorities(bounded); } } } diff --git a/frame/aura/src/mock.rs b/frame/aura/src/mock.rs index aff6b76a7a49f..48c09fd65500c 100644 --- a/frame/aura/src/mock.rs +++ b/frame/aura/src/mock.rs @@ -80,8 +80,13 @@ impl pallet_timestamp::Config for Test { type WeightInfo = (); } +parameter_types! { + pub const MaxAuthorities: u32 = 10; +} + impl pallet_aura::Config for Test { type AuthorityId = AuthorityId; + type MaxAuthorities = MaxAuthorities; } pub fn new_test_ext(authorities: Vec) -> sp_io::TestExternalities { diff --git a/primitives/consensus/slots/Cargo.toml b/primitives/consensus/slots/Cargo.toml index 9619f627a0b7c..2718158cfb7d6 100644 --- a/primitives/consensus/slots/Cargo.toml +++ b/primitives/consensus/slots/Cargo.toml @@ -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" } diff --git a/primitives/consensus/slots/src/lib.rs b/primitives/consensus/slots/src/lib.rs index 545d18af1f9be..49510922a82c5 100644 --- a/primitives/consensus/slots/src/lib.rs +++ b/primitives/consensus/slots/src/lib.rs @@ -19,10 +19,10 @@ #![cfg_attr(not(feature = "std"), no_std)] -use codec::{Decode, Encode}; +use codec::{Decode, Encode, MaxEncodedLen}; /// Unit type wrapper that represents a slot. -#[derive(Debug, Encode, Decode, Eq, Clone, Copy, Default, Ord)] +#[derive(Debug, Encode, Decode, Eq, Clone, Copy, Default, Ord, MaxEncodedLen)] pub struct Slot(u64); impl core::ops::Deref for Slot { From 57d8d821ee339dfe98cf3f258bb30c2e7f67ce56 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Sun, 18 Jul 2021 19:13:09 -0700 Subject: [PATCH 2/8] Add MaxAuthorities to node-template aura pallet config --- bin/node-template/runtime/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index c92eb8a1aadf8..eef563e85cf36 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -197,8 +197,13 @@ impl frame_system::Config for Runtime { impl pallet_randomness_collective_flip::Config for Runtime {} +parameter_types! { + pub const MaxAuthorities: u32 = 10; +} + impl pallet_aura::Config for Runtime { type AuthorityId = AuraId; + type MaxAuthorities = MaxAuthorities; } impl pallet_grandpa::Config for Runtime { From e57f30eae7575f53769fb4249e5312c856085b26 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Sun, 18 Jul 2021 19:51:20 -0700 Subject: [PATCH 3/8] Fix compilation errors on node-template --- bin/node-template/runtime/src/lib.rs | 2 +- primitives/application-crypto/src/lib.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index eef563e85cf36..3015c1c1d3692 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -388,7 +388,7 @@ impl_runtime_apis! { } fn authorities() -> Vec { - Aura::authorities() + Aura::authorities().into_inner() } } diff --git a/primitives/application-crypto/src/lib.rs b/primitives/application-crypto/src/lib.rs index ca175ddbed915..25376f48b5360 100644 --- a/primitives/application-crypto/src/lib.rs +++ b/primitives/application-crypto/src/lib.rs @@ -234,6 +234,7 @@ macro_rules! app_crypto_public_not_full_crypto { $crate::codec::Encode, $crate::codec::Decode, $crate::RuntimeDebug, + $crate::codec::MaxEncodedLen, )] pub struct Public($public); } From a8fb9331d5858efc1d34359a94e12e5324536e14 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Mon, 19 Jul 2021 16:42:25 -0700 Subject: [PATCH 4/8] Use WeakBoundedVec instead of BoundedVec --- frame/aura/src/lib.rs | 16 ++++++++-------- frame/support/src/storage/bounded_vec.rs | 2 ++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/frame/aura/src/lib.rs b/frame/aura/src/lib.rs index 2cb63b99357a1..9c1ee3f8b8d32 100644 --- a/frame/aura/src/lib.rs +++ b/frame/aura/src/lib.rs @@ -37,11 +37,11 @@ #![cfg_attr(not(feature = "std"), no_std)] -use sp_std::{convert::{TryFrom, TryInto}, prelude::*}; +use sp_std::{convert::TryFrom, prelude::*}; use codec::{Encode, Decode, MaxEncodedLen}; use frame_support::{ Parameter, traits::{Get, FindAuthor, OneSessionHandler, OnTimestampSet}, ConsensusEngineId, - BoundedSlice, BoundedVec, + BoundedSlice, WeakBoundedVec, }; use sp_runtime::{ RuntimeAppPublic, @@ -96,7 +96,7 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn authorities)] pub(super) type Authorities = - StorageValue<_, BoundedVec, ValueQuery>; + StorageValue<_, WeakBoundedVec, ValueQuery>; /// The current slot of this block. /// @@ -126,7 +126,7 @@ pub mod pallet { } impl Pallet { - fn change_authorities(new: BoundedVec) { + fn change_authorities(new: WeakBoundedVec) { >::put(&new); let log: DigestItem = DigestItem::Consensus( @@ -188,10 +188,10 @@ impl OneSessionHandler for Pallet { let next_authorities = validators.map(|(_, k)| k).collect::>(); let last_authorities = Self::authorities(); if last_authorities != next_authorities { - let bounded = match next_authorities.try_into() { - Ok(v) => v, - Err(_) => return, - }; + let bounded = >::force_from( + next_authorities, + Some("AuRa new session"), + ); Self::change_authorities(bounded); } } diff --git a/frame/support/src/storage/bounded_vec.rs b/frame/support/src/storage/bounded_vec.rs index b5b5252f9ec41..796c40ef89abe 100644 --- a/frame/support/src/storage/bounded_vec.rs +++ b/frame/support/src/storage/bounded_vec.rs @@ -28,6 +28,7 @@ use core::{ use crate::{ traits::Get, storage::{StorageDecodeLength, StorageTryAppend}, + WeakBoundedVec, }; /// A bounded vector. @@ -48,6 +49,7 @@ pub struct BoundedSlice<'a, T, S>(&'a [T], PhantomData); // `BoundedSlice`s encode to something which will always decode into a `BoundedVec` or a `Vec`. impl<'a, T: Encode + Decode, S: Get> EncodeLike> for BoundedSlice<'a, T, S> {} +impl<'a, T: Encode + Decode, S: Get> EncodeLike> for BoundedSlice<'a, T, S> {} impl<'a, T: Encode + Decode, S: Get> EncodeLike> for BoundedSlice<'a, T, S> {} impl<'a, T, S: Get> TryFrom<&'a [T]> for BoundedSlice<'a, T, S> { From 3774b7d603accf93a25ec71fe7a4df64bd5e72be Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Tue, 17 Aug 2021 20:44:57 -0700 Subject: [PATCH 5/8] Improve comment on BoundedSlice's EncodeLike impl Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> --- frame/support/src/storage/bounded_vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/support/src/storage/bounded_vec.rs b/frame/support/src/storage/bounded_vec.rs index 796c40ef89abe..71d06fd2f8a57 100644 --- a/frame/support/src/storage/bounded_vec.rs +++ b/frame/support/src/storage/bounded_vec.rs @@ -47,7 +47,7 @@ pub struct BoundedVec(Vec, PhantomData); #[derive(Encode)] pub struct BoundedSlice<'a, T, S>(&'a [T], PhantomData); -// `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> EncodeLike> for BoundedSlice<'a, T, S> {} impl<'a, T: Encode + Decode, S: Get> EncodeLike> for BoundedSlice<'a, T, S> {} impl<'a, T: Encode + Decode, S: Get> EncodeLike> for BoundedSlice<'a, T, S> {} From 8b7888eeb9a21df254a89abc2d6f26b1dd3cf8a6 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Tue, 17 Aug 2021 20:45:54 -0700 Subject: [PATCH 6/8] Bump MaxAuthorities count to 32 for node template --- bin/node-template/runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 3015c1c1d3692..354113c20bc64 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -198,7 +198,7 @@ impl frame_system::Config for Runtime { impl pallet_randomness_collective_flip::Config for Runtime {} parameter_types! { - pub const MaxAuthorities: u32 = 10; + pub const MaxAuthorities: u32 = 32; } impl pallet_aura::Config for Runtime { From 756e10f9f6730b9034e46b66941a496c9460f1bc Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 1 Sep 2021 13:02:49 -0700 Subject: [PATCH 7/8] cargo fmt --- frame/aura/src/lib.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/frame/aura/src/lib.rs b/frame/aura/src/lib.rs index 56adaa19ba949..89fb0ca169f73 100644 --- a/frame/aura/src/lib.rs +++ b/frame/aura/src/lib.rs @@ -38,8 +38,7 @@ #![cfg_attr(not(feature = "std"), no_std)] -use sp_std::{convert::TryFrom, prelude::*}; -use codec::{Encode, Decode, MaxEncodedLen}; +use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::{ traits::{DisabledValidators, FindAuthor, Get, OnTimestampSet, OneSessionHandler}, BoundedSlice, ConsensusEngineId, Parameter, WeakBoundedVec, @@ -50,7 +49,7 @@ use sp_runtime::{ traits::{IsMember, Member, SaturatedConversion, Saturating, Zero}, RuntimeAppPublic, }; -use sp_std::prelude::*; +use sp_std::convert::TryFrom; pub mod migrations; mod mock; @@ -154,7 +153,7 @@ impl Pallet { let log: DigestItem = DigestItem::Consensus( AURA_ENGINE_ID, - ConsensusLog::AuthoritiesChange(new.into_inner()).encode() + ConsensusLog::AuthoritiesChange(new.into_inner()).encode(), ); >::deposit_log(log.into()); } From 5a75a6e08844c8c943ad435da1876a18e91c55a2 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 1 Sep 2021 13:44:15 -0700 Subject: [PATCH 8/8] cargo fmt --- frame/aura/src/lib.rs | 2 +- frame/support/src/storage/bounded_vec.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/frame/aura/src/lib.rs b/frame/aura/src/lib.rs index 89fb0ca169f73..e8b68f928e087 100644 --- a/frame/aura/src/lib.rs +++ b/frame/aura/src/lib.rs @@ -49,7 +49,7 @@ use sp_runtime::{ traits::{IsMember, Member, SaturatedConversion, Saturating, Zero}, RuntimeAppPublic, }; -use sp_std::convert::TryFrom; +use sp_std::{convert::TryFrom, vec::Vec}; pub mod migrations; mod mock; diff --git a/frame/support/src/storage/bounded_vec.rs b/frame/support/src/storage/bounded_vec.rs index b4a5c2334d9bc..0f56511e6edd8 100644 --- a/frame/support/src/storage/bounded_vec.rs +++ b/frame/support/src/storage/bounded_vec.rs @@ -46,9 +46,13 @@ pub struct BoundedVec(Vec, PhantomData); #[derive(Encode)] pub struct BoundedSlice<'a, T, S>(&'a [T], PhantomData); -// `BoundedSlice`s encode to something which will always decode into a `BoundedVec`, `WeakBoundedVec`, 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> EncodeLike> for BoundedSlice<'a, T, S> {} -impl<'a, T: Encode + Decode, S: Get> EncodeLike> for BoundedSlice<'a, T, S> {} +impl<'a, T: Encode + Decode, S: Get> EncodeLike> + for BoundedSlice<'a, T, S> +{ +} impl<'a, T: Encode + Decode, S: Get> EncodeLike> for BoundedSlice<'a, T, S> {} impl<'a, T, S: Get> TryFrom<&'a [T]> for BoundedSlice<'a, T, S> {