Skip to content

Commit ebcf0a0

Browse files
bkchrdavxy
andauthored
pallet-grandpa: Remove GRANDPA_AUTHORITIES_KEY (#2181)
Remove the `GRANDPA_AUTHORITIES_KEY` key and its usage. Apparently this was used in the early days to communicate the grandpa authorities to the node. However, we have now a runtime api that does this for us. So, this pull request is moving from the custom managed storage item to a FRAME managed storage item. This pr also includes a migration for doing the switch on a running chain. --------- Co-authored-by: Davide Galassi <[email protected]>
1 parent 604704a commit ebcf0a0

File tree

7 files changed

+124
-83
lines changed

7 files changed

+124
-83
lines changed

polkadot/runtime/rococo/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,8 @@ pub mod migrations {
14971497
frame_support::migrations::RemovePallet<PhragmenElectionPalletName, <Runtime as frame_system::Config>::DbWeight>,
14981498
frame_support::migrations::RemovePallet<TechnicalMembershipPalletName, <Runtime as frame_system::Config>::DbWeight>,
14991499
frame_support::migrations::RemovePallet<TipsPalletName, <Runtime as frame_system::Config>::DbWeight>,
1500+
1501+
pallet_grandpa::migrations::MigrateV4ToV5<Runtime>,
15001502
);
15011503
}
15021504

polkadot/runtime/westend/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,7 @@ pub mod migrations {
15581558
pallet_nomination_pools::migration::versioned_migrations::V5toV6<Runtime>,
15591559
pallet_referenda::migration::v1::MigrateV0ToV1<Runtime, ()>,
15601560
pallet_nomination_pools::migration::versioned_migrations::V6ToV7<Runtime>,
1561+
pallet_grandpa::migrations::MigrateV4ToV5<Runtime>,
15611562
);
15621563
}
15631564

substrate/client/consensus/grandpa/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,6 @@ where
471471
Client: ExecutorProvider<Block, Executor = E> + HeaderBackend<Block>,
472472
{
473473
fn get(&self) -> Result<AuthorityList, ClientError> {
474-
// This implementation uses the Grandpa runtime API instead of reading directly from the
475-
// `GRANDPA_AUTHORITIES_KEY` as the data may have been migrated since the genesis block of
476-
// the chain, whereas the runtime API is backwards compatible.
477474
self.executor()
478475
.call(
479476
self.expect_block_hash_from_id(&BlockId::Number(Zero::zero()))?,

substrate/frame/grandpa/src/lib.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,22 @@
3030

3131
// Re-export since this is necessary for `impl_apis` in runtime.
3232
pub use sp_consensus_grandpa::{
33-
self as fg_primitives, AuthorityId, AuthorityList, AuthorityWeight, VersionedAuthorityList,
33+
self as fg_primitives, AuthorityId, AuthorityList, AuthorityWeight,
3434
};
3535

36-
use codec::{self as codec, Decode, Encode, MaxEncodedLen};
36+
use codec::{Decode, Encode, MaxEncodedLen};
3737
use frame_support::{
3838
dispatch::{DispatchResultWithPostInfo, Pays},
3939
pallet_prelude::Get,
40-
storage,
4140
traits::OneSessionHandler,
4241
weights::Weight,
4342
WeakBoundedVec,
4443
};
4544
use frame_system::pallet_prelude::BlockNumberFor;
4645
use scale_info::TypeInfo;
4746
use sp_consensus_grandpa::{
48-
ConsensusLog, EquivocationProof, ScheduledChange, SetId, GRANDPA_AUTHORITIES_KEY,
49-
GRANDPA_ENGINE_ID, RUNTIME_LOG_TARGET as LOG_TARGET,
47+
ConsensusLog, EquivocationProof, ScheduledChange, SetId, GRANDPA_ENGINE_ID,
48+
RUNTIME_LOG_TARGET as LOG_TARGET,
5049
};
5150
use sp_runtime::{generic::DigestItem, traits::Zero, DispatchResult};
5251
use sp_session::{GetSessionNumber, GetValidatorCount};
@@ -75,7 +74,7 @@ pub mod pallet {
7574
use frame_system::pallet_prelude::*;
7675

7776
/// The current storage version.
78-
const STORAGE_VERSION: StorageVersion = StorageVersion::new(4);
77+
const STORAGE_VERSION: StorageVersion = StorageVersion::new(5);
7978

8079
#[pallet::pallet]
8180
#[pallet::storage_version(STORAGE_VERSION)]
@@ -145,7 +144,7 @@ pub mod pallet {
145144

146145
// enact the change if we've reached the enacting block
147146
if block_number == pending_change.scheduled_at + pending_change.delay {
148-
Self::set_grandpa_authorities(&pending_change.next_authorities);
147+
Authorities::<T>::put(&pending_change.next_authorities);
149148
Self::deposit_event(Event::NewAuthorities {
150149
authority_set: pending_change.next_authorities.into_inner(),
151150
});
@@ -342,6 +341,11 @@ pub mod pallet {
342341
#[pallet::getter(fn session_for_set)]
343342
pub(super) type SetIdSession<T: Config> = StorageMap<_, Twox64Concat, SetId, SessionIndex>;
344343

344+
/// The current list of authorities.
345+
#[pallet::storage]
346+
pub(crate) type Authorities<T: Config> =
347+
StorageValue<_, BoundedAuthorityList<T::MaxAuthorities>, ValueQuery>;
348+
345349
#[derive(frame_support::DefaultNoBound)]
346350
#[pallet::genesis_config]
347351
pub struct GenesisConfig<T: Config> {
@@ -354,7 +358,7 @@ pub mod pallet {
354358
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
355359
fn build(&self) {
356360
CurrentSetId::<T>::put(SetId::default());
357-
Pallet::<T>::initialize(&self.authorities)
361+
Pallet::<T>::initialize(self.authorities.clone())
358362
}
359363
}
360364

@@ -428,12 +432,7 @@ pub enum StoredState<N> {
428432
impl<T: Config> Pallet<T> {
429433
/// Get the current set of authorities, along with their respective weights.
430434
pub fn grandpa_authorities() -> AuthorityList {
431-
storage::unhashed::get_or_default::<VersionedAuthorityList>(GRANDPA_AUTHORITIES_KEY).into()
432-
}
433-
434-
/// Set the current set of authorities, along with their respective weights.
435-
fn set_grandpa_authorities(authorities: &AuthorityList) {
436-
storage::unhashed::put(GRANDPA_AUTHORITIES_KEY, &VersionedAuthorityList::from(authorities));
435+
Authorities::<T>::get().into_inner()
437436
}
438437

439438
/// Schedule GRANDPA to pause starting in the given number of blocks.
@@ -522,10 +521,14 @@ impl<T: Config> Pallet<T> {
522521

523522
// Perform module initialization, abstracted so that it can be called either through genesis
524523
// config builder or through `on_genesis_session`.
525-
fn initialize(authorities: &AuthorityList) {
524+
fn initialize(authorities: AuthorityList) {
526525
if !authorities.is_empty() {
527526
assert!(Self::grandpa_authorities().is_empty(), "Authorities are already initialized!");
528-
Self::set_grandpa_authorities(authorities);
527+
Authorities::<T>::put(
528+
&BoundedAuthorityList::<T::MaxAuthorities>::try_from(authorities).expect(
529+
"Grandpa: `Config::MaxAuthorities` is smaller than the number of genesis authorities!",
530+
),
531+
);
529532
}
530533

531534
// NOTE: initialize first session of first set. this is necessary for
@@ -568,7 +571,7 @@ where
568571
I: Iterator<Item = (&'a T::AccountId, AuthorityId)>,
569572
{
570573
let authorities = validators.map(|(_, k)| (k, 1)).collect::<Vec<_>>();
571-
Self::initialize(&authorities);
574+
Self::initialize(authorities);
572575
}
573576

574577
fn on_new_session<'a, I: 'a>(changed: bool, validators: I, _queued_validators: I)

substrate/frame/grandpa/src/migrations.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ use frame_support::{
2222

2323
use crate::{Config, CurrentSetId, SetIdSession, LOG_TARGET};
2424

25+
pub use v5::MigrateV4ToV5;
26+
2527
/// Version 4.
2628
pub mod v4;
29+
mod v5;
2730

2831
/// This migration will clean up all stale set id -> session entries from the
2932
/// `SetIdSession` storage map, only the latest `max_set_id_session_entries`
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
use crate::{BoundedAuthorityList, Pallet};
19+
use codec::Decode;
20+
use frame_support::{
21+
migrations::VersionedMigration,
22+
storage,
23+
traits::{Get, OnRuntimeUpgrade},
24+
weights::Weight,
25+
};
26+
use sp_consensus_grandpa::AuthorityList;
27+
use sp_std::{marker::PhantomData, vec::Vec};
28+
29+
const GRANDPA_AUTHORITIES_KEY: &[u8] = b":grandpa_authorities";
30+
31+
fn load_authority_list() -> AuthorityList {
32+
storage::unhashed::get_raw(GRANDPA_AUTHORITIES_KEY).map_or_else(
33+
|| Vec::new(),
34+
|l| <(u8, AuthorityList)>::decode(&mut &l[..]).unwrap_or_default().1,
35+
)
36+
}
37+
38+
/// Actual implementation of [`MigrateV4ToV5`].
39+
pub struct MigrateImpl<T>(PhantomData<T>);
40+
41+
impl<T: crate::Config> OnRuntimeUpgrade for MigrateImpl<T> {
42+
#[cfg(feature = "try-runtime")]
43+
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
44+
use codec::Encode;
45+
46+
let authority_list_len = load_authority_list().len() as u32;
47+
48+
if authority_list_len > T::MaxAuthorities::get() {
49+
return Err(
50+
"Grandpa: `Config::MaxAuthorities` is smaller than the actual number of authorities.".into()
51+
)
52+
}
53+
54+
if authority_list_len == 0 {
55+
return Err("Grandpa: Authority list is empty!".into())
56+
}
57+
58+
Ok(authority_list_len.encode())
59+
}
60+
61+
#[cfg(feature = "try-runtime")]
62+
fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
63+
let len = u32::decode(&mut &state[..]).unwrap();
64+
65+
frame_support::ensure!(
66+
len == crate::Pallet::<T>::grandpa_authorities().len() as u32,
67+
"Grandpa: pre-migrated and post-migrated list should have the same length"
68+
);
69+
70+
frame_support::ensure!(
71+
load_authority_list().is_empty(),
72+
"Old authority list shouldn't exist anymore"
73+
);
74+
75+
Ok(())
76+
}
77+
78+
fn on_runtime_upgrade() -> Weight {
79+
crate::Authorities::<T>::put(
80+
&BoundedAuthorityList::<T::MaxAuthorities>::force_from(
81+
load_authority_list(),
82+
Some("Grandpa: `Config::MaxAuthorities` is smaller than the actual number of authorities.")
83+
)
84+
);
85+
86+
storage::unhashed::kill(GRANDPA_AUTHORITIES_KEY);
87+
88+
T::DbWeight::get().reads_writes(1, 2)
89+
}
90+
}
91+
92+
/// Migrate the storage from V4 to V5.
93+
///
94+
/// Switches from `GRANDPA_AUTHORITIES_KEY` to a normal FRAME storage item.
95+
pub type MigrateV4ToV5<T> =
96+
VersionedMigration<4, 5, MigrateImpl<T>, Pallet<T>, <T as frame_system::Config>::DbWeight>;

substrate/primitives/consensus/grandpa/src/lib.rs

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,18 @@
1919
2020
#![cfg_attr(not(feature = "std"), no_std)]
2121

22-
#[cfg(not(feature = "std"))]
23-
extern crate alloc;
24-
2522
#[cfg(feature = "serde")]
2623
use serde::Serialize;
2724

28-
use codec::{Codec, Decode, Encode, Input};
25+
use codec::{Codec, Decode, Encode};
2926
use scale_info::TypeInfo;
3027
#[cfg(feature = "std")]
3128
use sp_keystore::KeystorePtr;
3229
use sp_runtime::{
3330
traits::{Header as HeaderT, NumberFor},
3431
ConsensusEngineId, RuntimeDebug,
3532
};
36-
use sp_std::{borrow::Cow, vec::Vec};
33+
use sp_std::vec::Vec;
3734

3835
/// The log target to be used by client code.
3936
pub const CLIENT_LOG_TARGET: &str = "grandpa";
@@ -62,10 +59,6 @@ pub type AuthoritySignature = app::Signature;
6259
/// The `ConsensusEngineId` of GRANDPA.
6360
pub const GRANDPA_ENGINE_ID: ConsensusEngineId = *b"FRNK";
6461

65-
/// The storage key for the current set of weighted Grandpa authorities.
66-
/// The value stored is an encoded VersionedAuthorityList.
67-
pub const GRANDPA_AUTHORITIES_KEY: &[u8] = b":grandpa_authorities";
68-
6962
/// The weight of an authority.
7063
pub type AuthorityWeight = u64;
7164

@@ -464,60 +457,6 @@ where
464457
Some(grandpa::SignedMessage { message, signature, id: public })
465458
}
466459

467-
/// WASM function call to check for pending changes.
468-
pub const PENDING_CHANGE_CALL: &str = "grandpa_pending_change";
469-
/// WASM function call to get current GRANDPA authorities.
470-
pub const AUTHORITIES_CALL: &str = "grandpa_authorities";
471-
472-
/// The current version of the stored AuthorityList type. The encoding version MUST be updated any
473-
/// time the AuthorityList type changes.
474-
const AUTHORITIES_VERSION: u8 = 1;
475-
476-
/// An AuthorityList that is encoded with a version specifier. The encoding version is updated any
477-
/// time the AuthorityList type changes. This ensures that encodings of different versions of an
478-
/// AuthorityList are differentiable. Attempting to decode an authority list with an unknown
479-
/// version will fail.
480-
#[derive(Default)]
481-
pub struct VersionedAuthorityList<'a>(Cow<'a, AuthorityList>);
482-
483-
impl<'a> From<AuthorityList> for VersionedAuthorityList<'a> {
484-
fn from(authorities: AuthorityList) -> Self {
485-
VersionedAuthorityList(Cow::Owned(authorities))
486-
}
487-
}
488-
489-
impl<'a> From<&'a AuthorityList> for VersionedAuthorityList<'a> {
490-
fn from(authorities: &'a AuthorityList) -> Self {
491-
VersionedAuthorityList(Cow::Borrowed(authorities))
492-
}
493-
}
494-
495-
impl<'a> Into<AuthorityList> for VersionedAuthorityList<'a> {
496-
fn into(self) -> AuthorityList {
497-
self.0.into_owned()
498-
}
499-
}
500-
501-
impl<'a> Encode for VersionedAuthorityList<'a> {
502-
fn size_hint(&self) -> usize {
503-
(AUTHORITIES_VERSION, self.0.as_ref()).size_hint()
504-
}
505-
506-
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
507-
(AUTHORITIES_VERSION, self.0.as_ref()).using_encoded(f)
508-
}
509-
}
510-
511-
impl<'a> Decode for VersionedAuthorityList<'a> {
512-
fn decode<I: Input>(value: &mut I) -> Result<Self, codec::Error> {
513-
let (version, authorities): (u8, AuthorityList) = Decode::decode(value)?;
514-
if version != AUTHORITIES_VERSION {
515-
return Err("unknown Grandpa authorities version".into())
516-
}
517-
Ok(authorities.into())
518-
}
519-
}
520-
521460
/// An opaque type used to represent the key ownership proof at the runtime API
522461
/// boundary. The inner value is an encoded representation of the actual key
523462
/// ownership proof which will be parameterized when defining the runtime. At

0 commit comments

Comments
 (0)