-
Notifications
You must be signed in to change notification settings - Fork 1.2k
pallet-grandpa: Remove GRANDPA_AUTHORITIES_KEY
#2181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
476f183
845efeb
67c9f03
3849737
6f119a4
6a7459d
fbb0665
4ec1966
cd88a2c
e565ed1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,23 +30,22 @@ | |
|
|
||
| // Re-export since this is necessary for `impl_apis` in runtime. | ||
| pub use sp_consensus_grandpa::{ | ||
| self as fg_primitives, AuthorityId, AuthorityList, AuthorityWeight, VersionedAuthorityList, | ||
| self as fg_primitives, AuthorityId, AuthorityList, AuthorityWeight, | ||
| }; | ||
|
|
||
| use codec::{self as codec, Decode, Encode, MaxEncodedLen}; | ||
| use codec::{Decode, Encode, MaxEncodedLen}; | ||
| use frame_support::{ | ||
| dispatch::{DispatchResultWithPostInfo, Pays}, | ||
| pallet_prelude::Get, | ||
| storage, | ||
| traits::OneSessionHandler, | ||
| weights::Weight, | ||
| WeakBoundedVec, | ||
| }; | ||
| use frame_system::pallet_prelude::BlockNumberFor; | ||
| use scale_info::TypeInfo; | ||
| use sp_consensus_grandpa::{ | ||
| ConsensusLog, EquivocationProof, ScheduledChange, SetId, GRANDPA_AUTHORITIES_KEY, | ||
| GRANDPA_ENGINE_ID, RUNTIME_LOG_TARGET as LOG_TARGET, | ||
| ConsensusLog, EquivocationProof, ScheduledChange, SetId, GRANDPA_ENGINE_ID, | ||
| RUNTIME_LOG_TARGET as LOG_TARGET, | ||
| }; | ||
| use sp_runtime::{generic::DigestItem, traits::Zero, DispatchResult}; | ||
| use sp_session::{GetSessionNumber, GetValidatorCount}; | ||
|
|
@@ -75,7 +74,7 @@ pub mod pallet { | |
| use frame_system::pallet_prelude::*; | ||
|
|
||
| /// The current storage version. | ||
| const STORAGE_VERSION: StorageVersion = StorageVersion::new(4); | ||
| const STORAGE_VERSION: StorageVersion = StorageVersion::new(5); | ||
|
|
||
| #[pallet::pallet] | ||
| #[pallet::storage_version(STORAGE_VERSION)] | ||
|
|
@@ -342,6 +341,11 @@ pub mod pallet { | |
| #[pallet::getter(fn session_for_set)] | ||
| pub(super) type SetIdSession<T: Config> = StorageMap<_, Twox64Concat, SetId, SessionIndex>; | ||
|
|
||
| /// The current list of authorities. | ||
| #[pallet::storage] | ||
| pub(super) type Authorities<T: Config> = | ||
| StorageValue<_, BoundedAuthorityList<T::MaxAuthorities>, ValueQuery>; | ||
|
|
||
| #[derive(frame_support::DefaultNoBound)] | ||
| #[pallet::genesis_config] | ||
| pub struct GenesisConfig<T: Config> { | ||
|
|
@@ -354,7 +358,7 @@ pub mod pallet { | |
| impl<T: Config> BuildGenesisConfig for GenesisConfig<T> { | ||
| fn build(&self) { | ||
| CurrentSetId::<T>::put(SetId::default()); | ||
| Pallet::<T>::initialize(&self.authorities) | ||
| Pallet::<T>::initialize(self.authorities.clone()) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -428,12 +432,12 @@ pub enum StoredState<N> { | |
| impl<T: Config> Pallet<T> { | ||
| /// Get the current set of authorities, along with their respective weights. | ||
| pub fn grandpa_authorities() -> AuthorityList { | ||
| storage::unhashed::get_or_default::<VersionedAuthorityList>(GRANDPA_AUTHORITIES_KEY).into() | ||
| Authorities::<T>::get().to_vec() | ||
| } | ||
|
|
||
| /// 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)); | ||
| pub(crate) fn set_grandpa_authorities(authorities: &BoundedAuthorityList<T::MaxAuthorities>) { | ||
|
||
| Authorities::<T>::put(authorities); | ||
| } | ||
|
|
||
| /// Schedule GRANDPA to pause starting in the given number of blocks. | ||
|
|
@@ -522,10 +526,14 @@ impl<T: Config> Pallet<T> { | |
|
|
||
| // Perform module initialization, abstracted so that it can be called either through genesis | ||
| // config builder or through `on_genesis_session`. | ||
| fn initialize(authorities: &AuthorityList) { | ||
| fn initialize(authorities: AuthorityList) { | ||
| if !authorities.is_empty() { | ||
| assert!(Self::grandpa_authorities().is_empty(), "Authorities are already initialized!"); | ||
| Self::set_grandpa_authorities(authorities); | ||
| Self::set_grandpa_authorities( | ||
| &BoundedAuthorityList::<T::MaxAuthorities>::try_from(authorities).expect( | ||
| "Granpa: `Config::MaxAuthorities` is smaller than the number of genesis authorities!", | ||
bkchr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ), | ||
| ); | ||
| } | ||
|
|
||
| // NOTE: initialize first session of first set. this is necessary for | ||
|
|
@@ -568,7 +576,7 @@ where | |
| I: Iterator<Item = (&'a T::AccountId, AuthorityId)>, | ||
| { | ||
| let authorities = validators.map(|(_, k)| (k, 1)).collect::<Vec<_>>(); | ||
| Self::initialize(&authorities); | ||
| Self::initialize(authorities); | ||
| } | ||
|
|
||
| fn on_new_session<'a, I: 'a>(changed: bool, validators: I, _queued_validators: I) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use crate::{BoundedAuthorityList, Pallet}; | ||
| use codec::Decode; | ||
| use frame_support::{ | ||
| migrations::VersionedMigration, | ||
| storage, | ||
| traits::{Get, OnRuntimeUpgrade}, | ||
| weights::Weight, | ||
| }; | ||
| use sp_consensus_grandpa::AuthorityList; | ||
| use sp_std::{marker::PhantomData, vec::Vec}; | ||
|
|
||
| const GRANDPA_AUTHORITIES_KEY: &[u8] = b":grandpa_authorities"; | ||
|
|
||
| fn load_authority_list() -> AuthorityList { | ||
| storage::unhashed::get_raw(GRANDPA_AUTHORITIES_KEY).map_or_else( | ||
| || Vec::new(), | ||
| |l| <(u8, AuthorityList)>::decode(&mut &l[..]).unwrap_or_default().1, | ||
| ) | ||
| } | ||
|
|
||
| /// Actual implementation of [`MigrateV4ToV5`]. | ||
| pub struct MigrateImpl<T>(PhantomData<T>); | ||
|
|
||
| impl<T: crate::Config> OnRuntimeUpgrade for MigrateImpl<T> { | ||
| #[cfg(feature = "try-runtime")] | ||
| fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> { | ||
| use codec::Encode; | ||
|
|
||
| let authority_list_len = load_authority_list().len() as u32; | ||
|
|
||
| if authority_list_len > T::MaxAuthorities::get() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this fail as well if |
||
| return Err( | ||
| "Grandpa: `Config::MaxAuthorities` is smaller than the actual number of authorities.".into() | ||
| ) | ||
| } | ||
|
|
||
| Ok(authority_list_len.encode()) | ||
| } | ||
|
|
||
| #[cfg(feature = "try-runtime")] | ||
| fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> { | ||
| let len = u32::decode(&mut &state[..]).unwrap(); | ||
|
|
||
| frame_support::ensure!( | ||
| len == crate::Pallet::<T>::grandpa_authorities().len() as u32, | ||
| "Grandpa: pre-migrated and post-migrated list should have the same length" | ||
| ); | ||
|
|
||
| frame_support::ensure!( | ||
| load_authority_list().is_empty(), | ||
| "Old authority list shouldn't exist anymore" | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn on_runtime_upgrade() -> Weight { | ||
| crate::Pallet::<T>::set_grandpa_authorities( | ||
| &BoundedAuthorityList::<T::MaxAuthorities>::force_from( | ||
| load_authority_list(), | ||
| Some("Grandpa: `Config::MaxAuthorities` is smaller than the actual number of authorities.") | ||
| ) | ||
| ); | ||
|
|
||
| storage::unhashed::kill(GRANDPA_AUTHORITIES_KEY); | ||
|
|
||
| T::DbWeight::get().reads_writes(1, 1) | ||
|
||
| } | ||
| } | ||
|
|
||
| /// Migrate the storage from V4 to V5. | ||
| /// | ||
| /// Switches from `GRANDPA_AUTHORITIES_KEY` to a normal FRAME storage item. | ||
| pub type MigrateV4ToV5<T> = | ||
| VersionedMigration<4, 5, MigrateImpl<T>, Pallet<T>, <T as frame_system::Config>::DbWeight>; | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -19,21 +19,18 @@ | |||||
|
|
||||||
| #![cfg_attr(not(feature = "std"), no_std)] | ||||||
|
|
||||||
| #[cfg(not(feature = "std"))] | ||||||
| extern crate alloc; | ||||||
|
|
||||||
| #[cfg(feature = "serde")] | ||||||
| use serde::Serialize; | ||||||
|
|
||||||
| use codec::{Codec, Decode, Encode, Input}; | ||||||
| use codec::{Codec, Decode, Encode}; | ||||||
| use scale_info::TypeInfo; | ||||||
| #[cfg(feature = "std")] | ||||||
| use sp_keystore::KeystorePtr; | ||||||
| use sp_runtime::{ | ||||||
| traits::{Header as HeaderT, NumberFor}, | ||||||
| ConsensusEngineId, RuntimeDebug, | ||||||
| }; | ||||||
| use sp_std::{borrow::Cow, vec::Vec}; | ||||||
| use sp_std::vec::Vec; | ||||||
|
|
||||||
| /// The log target to be used by client code. | ||||||
| pub const CLIENT_LOG_TARGET: &str = "grandpa"; | ||||||
|
|
@@ -62,10 +59,6 @@ pub type AuthoritySignature = app::Signature; | |||||
| /// The `ConsensusEngineId` of GRANDPA. | ||||||
| pub const GRANDPA_ENGINE_ID: ConsensusEngineId = *b"FRNK"; | ||||||
|
|
||||||
| /// The storage key for the current set of weighted Grandpa authorities. | ||||||
| /// The value stored is an encoded VersionedAuthorityList. | ||||||
| pub const GRANDPA_AUTHORITIES_KEY: &[u8] = b":grandpa_authorities"; | ||||||
|
|
||||||
| /// The weight of an authority. | ||||||
| pub type AuthorityWeight = u64; | ||||||
|
|
||||||
|
|
@@ -469,55 +462,6 @@ pub const PENDING_CHANGE_CALL: &str = "grandpa_pending_change"; | |||||
| /// WASM function call to get current GRANDPA authorities. | ||||||
| pub const AUTHORITIES_CALL: &str = "grandpa_authorities"; | ||||||
|
||||||
| /// WASM function call to get current GRANDPA authorities. | |
| pub const AUTHORITIES_CALL: &str = "grandpa_authorities"; |
Unrelated to this PR but these can be removed (they're unused). The one above as well but github doesn't allow me to comment on it.
Uh oh!
There was an error while loading. Please reload this page.