Skip to content

Commit d21b251

Browse files
committed
Implement grandpa interface from admin-utils for runtime
1 parent 846eaae commit d21b251

File tree

6 files changed

+32
-11
lines changed

6 files changed

+32
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ sp-block-builder = { git = "https://github.com/paritytech/polkadot-sdk.git", tag
138138
sp-blockchain = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
139139
sp-consensus = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409" }
140140
sp-consensus-aura = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
141-
sp-consensus-grandpa = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409" }
141+
sp-consensus-grandpa = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
142142
sp-genesis-builder = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
143143
sp-core = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
144144
sp-inherents = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }

pallets/admin-utils/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ workspace = true
1616
targets = ["x86_64-unknown-linux-gnu"]
1717

1818
[dependencies]
19-
subtensor-macros.workspace = true
19+
subtensor-macros = { workspace = true }
2020
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
2121
"derive",
2222
] }

pallets/admin-utils/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
22

3+
// extern crate alloc;
4+
35
pub use pallet::*;
46
pub mod weights;
57
pub use weights::WeightInfo;
68

79
use frame_system::pallet_prelude::BlockNumberFor;
8-
use sp_consensus_grandpa::AuthorityId as GrandpaId;
10+
// - we could replace it with Vec<(AuthorityId, u64)>, but we would need
11+
// `sp_consensus_grandpa` for `AuthorityId` anyway
12+
// - we could use a type parameter for `AuthorityId`, but there is
13+
// no sense for this as GRANDPA's `AuthorityId` is not a parameter -- it's always the same
14+
use sp_consensus_grandpa::AuthorityList;
915
use sp_runtime::{traits::Member, DispatchResult, RuntimeAppPublic};
1016

1117
mod benchmarking;
@@ -1263,7 +1269,7 @@ pub mod pallet {
12631269
pub fn schedule_grandpa_change(
12641270
origin: OriginFor<T>,
12651271
// grandpa ID is always the same type, so we don't need to parametrize it via `Config`
1266-
next_authorities: Vec<(GrandpaId, u64)>,
1272+
next_authorities: AuthorityList,
12671273
in_blocks: BlockNumberFor<T>,
12681274
forced: Option<BlockNumberFor<T>>,
12691275
) -> DispatchResult {
@@ -1288,12 +1294,14 @@ impl<A, M> AuraInterface<A, M> for () {
12881294
fn change_authorities(_: BoundedVec<A, M>) {}
12891295
}
12901296

1297+
1298+
12911299
pub trait GrandpaInterface<Runtime>
12921300
where
12931301
Runtime: frame_system::Config,
12941302
{
12951303
fn schedule_change(
1296-
next_authorities: Vec<(GrandpaId, u64)>,
1304+
next_authorities: AuthorityList,
12971305
in_blocks: BlockNumberFor<Runtime>,
12981306
forced: Option<BlockNumberFor<Runtime>>,
12991307
) -> DispatchResult;
@@ -1304,7 +1312,7 @@ where
13041312
R: frame_system::Config,
13051313
{
13061314
fn schedule_change(
1307-
_next_authorities: Vec<(GrandpaId, u64)>,
1315+
_next_authorities: AuthorityList,
13081316
_in_blocks: BlockNumberFor<R>,
13091317
_forced: Option<BlockNumberFor<R>>,
13101318
) -> DispatchResult {

pallets/admin-utils/src/tests/mock.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use frame_support::{
88
use frame_system as system;
99
use frame_system::{limits, pallet_prelude::BlockNumberFor, EnsureNever, EnsureRoot};
1010
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
11+
use sp_consensus_grandpa::AuthorityList as GrandpaAuthorityList;
1112
use sp_core::U256;
1213
use sp_core::{ConstU64, H256};
1314
use sp_runtime::{
@@ -266,7 +267,7 @@ impl PrivilegeCmp<OriginCaller> for OriginPrivilegeCmp {
266267
pub struct GrandpaInterfaceImpl;
267268
impl crate::GrandpaInterface<Test> for GrandpaInterfaceImpl {
268269
fn schedule_change(
269-
next_authorities: Vec<(pallet_grandpa::AuthorityId, u64)>,
270+
next_authorities: GrandpaAuthorityList,
270271
in_blocks: BlockNumberFor<Test>,
271272
forced: Option<BlockNumberFor<Test>>,
272273
) -> sp_runtime::DispatchResult {

pallets/admin-utils/src/weights.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
439439
}
440440

441441
fn schedule_grandpa_change(_a: u32) -> Weight {
442-
// TODO should be replaced by benchmared weights
443-
10_000.into()
442+
// TODO should be replaced by benchmarked weights
443+
Weight::default()
444444
}
445445
}
446446

@@ -821,7 +821,7 @@ impl WeightInfo for () {
821821
.saturating_add(RocksDbWeight::get().writes(1_u64))
822822
}
823823
fn schedule_grandpa_change(_a: u32) -> Weight {
824-
// TODO should be replaced by benchmared weights
825-
10_000.into()
824+
// TODO should be replaced by benchmarked weights
825+
Weight::default()
826826
}
827827
}

runtime/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,11 +1121,23 @@ impl pallet_admin_utils::AuraInterface<AuraId, ConstU32<32>> for AuraPalletIntrf
11211121
}
11221122
}
11231123

1124+
pub struct GrandpaInterfaceImpl;
1125+
impl pallet_admin_utils::GrandpaInterface<Runtime> for GrandpaInterfaceImpl {
1126+
fn schedule_change(
1127+
next_authorities: Vec<(pallet_grandpa::AuthorityId, u64)>,
1128+
in_blocks: BlockNumber,
1129+
forced: Option<BlockNumber>,
1130+
) -> sp_runtime::DispatchResult {
1131+
Grandpa::schedule_change(next_authorities, in_blocks, forced)
1132+
}
1133+
}
1134+
11241135
impl pallet_admin_utils::Config for Runtime {
11251136
type RuntimeEvent = RuntimeEvent;
11261137
type AuthorityId = AuraId;
11271138
type MaxAuthorities = ConstU32<32>;
11281139
type Aura = AuraPalletIntrf;
1140+
type Grandpa = GrandpaInterfaceImpl;
11291141
type Balance = Balance;
11301142
type WeightInfo = pallet_admin_utils::weights::SubstrateWeight<Runtime>;
11311143
}

0 commit comments

Comments
 (0)