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

Generate storage info for pallet authority_discovery #9428

Merged
20 commits merged into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions frame/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl<T: Config> Pallet<T> {

let bounded_authorities = BoundedVec::<T::AuthorityId, T::MaxAuthorities>
::try_from((*authorities).clone()).expect("authorities vec too big");

<Authorities<T>>::put(bounded_authorities);
}
}
Expand Down Expand Up @@ -196,8 +196,9 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
// instant changes
if changed {
let mut next_authorities = validators.map(|(_, k)| k).collect::<Vec<_>>();
// Truncate to MaxAuthorities, NOTE: is this the right thing to do?
// Truncate to MaxAuthorities, to not fail the conversion
next_authorities.truncate(T::MaxAuthorities::get() as usize);

let last_authorities = Self::authorities();
if next_authorities != last_authorities.into_inner() {
let bounded_authorities = BoundedVec::<T::AuthorityId, T::MaxAuthorities>
Expand Down
37 changes: 25 additions & 12 deletions frame/authority-discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{traits::OneSessionHandler, BoundedVec};
use frame_support::{
traits::{Get, OneSessionHandler},
BoundedVec,
};
use sp_authority_discovery::AuthorityId;
use sp_std::prelude::*;

Expand Down Expand Up @@ -106,17 +109,15 @@ impl<T: Config> Pallet<T> {
fn initialize_keys(keys: &Vec<AuthorityId>) {
if !keys.is_empty() {
assert!(Keys::<T>::get().is_empty(), "Keys are already initialized!");
let bounded_keys = Self::to_bounded_vec((*keys).clone());

let bounded_keys =
BoundedVec::<AuthorityId, T::MaxAuthorities>::try_from((*keys).clone())
.expect("Keys vec too big");
KiChjang marked this conversation as resolved.
Show resolved Hide resolved

Keys::<T>::put(bounded_keys.clone());
gui1117 marked this conversation as resolved.
Show resolved Hide resolved
NextKeys::<T>::put(bounded_keys);
}
}

fn to_bounded_vec(keys: Vec<AuthorityId>) -> BoundedVec<AuthorityId, T::MaxAuthorities> {
let bounded_keys = BoundedVec::<AuthorityId, T::MaxAuthorities>::try_from(keys);
assert!(bounded_keys.is_ok(), "More than the maximum number of authorities provided");
bounded_keys.unwrap()
}
}

impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
Expand All @@ -139,11 +140,23 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
{
// Remember who the authorities are for the new and next session.
if changed {
let keys = validators.map(|x| x.1).collect::<Vec<_>>();
let bounded_keys = Self::to_bounded_vec(keys);
let mut keys = validators.map(|x| x.1).collect::<Vec<_>>();
// Truncate to MaxAuthorities, to not fail the conversion
keys.truncate(T::MaxAuthorities::get() as usize);

let bounded_keys = BoundedVec::<AuthorityId, T::MaxAuthorities>::try_from(keys)
.expect("We truncated so this should never fail");

Keys::<T>::put(bounded_keys);
let next_keys = queued_validators.map(|x| x.1).collect::<Vec<_>>();
let next_bounded_keys = Self::to_bounded_vec(next_keys);

let mut next_keys = queued_validators.map(|x| x.1).collect::<Vec<_>>();
// Truncate to MaxAuthorities, to not fail the conversion
next_keys.truncate(T::MaxAuthorities::get() as usize);

let next_bounded_keys =
BoundedVec::<AuthorityId, T::MaxAuthorities>::try_from(next_keys)
.expect("We truncated so this should never fail");

NextKeys::<T>::put(next_bounded_keys);
}
}
Expand Down