Skip to content

Commit 552fa1b

Browse files
authored
Merge pull request #1084 from opentensor/feat/grandpa-authorities-setting
Add scheduleGrandpaChange call to admin utils
2 parents f5542c1 + fe344c2 commit 552fa1b

File tree

9 files changed

+173
-19
lines changed

9 files changed

+173
-19
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 20 additions & 14 deletions
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
] }
@@ -31,6 +31,7 @@ sp-weights = { workspace = true }
3131
substrate-fixed = { workspace = true }
3232
pallet-evm-chain-id = { workspace = true }
3333
pallet-drand = { workspace = true, default-features = false }
34+
sp-consensus-grandpa = { workspace = true }
3435

3536
[dev-dependencies]
3637
sp-core = { workspace = true }
@@ -39,6 +40,7 @@ sp-tracing = { workspace = true }
3940
sp-consensus-aura = { workspace = true }
4041
pallet-balances = { workspace = true, features = ["std"] }
4142
pallet-scheduler = { workspace = true }
43+
pallet-grandpa = { workspace = true }
4244
sp-std = { workspace = true }
4345

4446
[features]
@@ -48,39 +50,43 @@ std = [
4850
"frame-benchmarking?/std",
4951
"frame-support/std",
5052
"frame-system/std",
51-
"scale-info/std",
52-
"pallet-subtensor/std",
53-
"sp-consensus-aura/std",
53+
"log/std",
5454
"pallet-balances/std",
55+
"pallet-drand/std",
5556
"pallet-evm-chain-id/std",
57+
"pallet-grandpa/std",
5658
"pallet-scheduler/std",
57-
"sp-runtime/std",
58-
"sp-tracing/std",
59-
"sp-weights/std",
60-
"log/std",
59+
"pallet-subtensor/std",
60+
"scale-info/std",
61+
"sp-consensus-aura/std",
62+
"sp-consensus-grandpa/std",
6163
"sp-core/std",
6264
"sp-io/std",
65+
"sp-runtime/std",
6366
"sp-std/std",
67+
"sp-tracing/std",
68+
"sp-weights/std",
6469
"substrate-fixed/std",
65-
"pallet-drand/std"
6670
]
6771
runtime-benchmarks = [
6872
"frame-benchmarking/runtime-benchmarks",
6973
"frame-support/runtime-benchmarks",
7074
"frame-system/runtime-benchmarks",
7175
"pallet-balances/runtime-benchmarks",
72-
"sp-runtime/runtime-benchmarks",
73-
"pallet-subtensor/runtime-benchmarks",
76+
"pallet-drand/runtime-benchmarks",
77+
"pallet-grandpa/runtime-benchmarks",
7478
"pallet-scheduler/runtime-benchmarks",
75-
"pallet-drand/runtime-benchmarks"
79+
"pallet-subtensor/runtime-benchmarks",
80+
"sp-runtime/runtime-benchmarks",
7681
]
7782
try-runtime = [
7883
"frame-support/try-runtime",
7984
"frame-system/try-runtime",
8085
"pallet-balances/try-runtime",
86+
"pallet-drand/try-runtime",
8187
"pallet-evm-chain-id/try-runtime",
88+
"pallet-grandpa/try-runtime",
8289
"pallet-scheduler/try-runtime",
83-
"sp-runtime/try-runtime",
8490
"pallet-subtensor/try-runtime",
85-
"pallet-drand/try-runtime"
91+
"sp-runtime/try-runtime",
8692
]

pallets/admin-utils/src/benchmarking.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ mod benchmarks {
3333
_(RawOrigin::Root, value);
3434
}
3535

36+
#[benchmark]
37+
fn schedule_grandpa_change(a: Linear<0, 32>) {
38+
let next_authorities = (1..=a)
39+
.map(|idx| account("Authority", idx, 0u32))
40+
.collect::<Vec<(sp_consensus_grandpa::AuthorityId, u64)>>();
41+
let in_blocks = BlockNumberFor::<T>::from(42u32);
42+
43+
#[extrinsic_call]
44+
_(RawOrigin::Root, next_authorities, in_blocks, None);
45+
}
46+
3647
#[benchmark]
3748
fn sudo_set_default_take() {
3849
#[extrinsic_call]

pallets/admin-utils/src/lib.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
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_runtime::{traits::Member, RuntimeAppPublic};
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;
15+
use sp_runtime::{traits::Member, DispatchResult, RuntimeAppPublic};
916

1017
mod benchmarking;
1118

@@ -41,6 +48,9 @@ pub mod pallet {
4148
/// Implementation of the AuraInterface
4249
type Aura: crate::AuraInterface<<Self as Config>::AuthorityId, Self::MaxAuthorities>;
4350

51+
/// Implementation of [`GrandpaInterface`]
52+
type Grandpa: crate::GrandpaInterface<Self>;
53+
4454
/// The identifier type for an authority.
4555
type AuthorityId: Member
4656
+ Parameter
@@ -1238,6 +1248,34 @@ pub mod pallet {
12381248
ChainId::<T>::set(chain_id);
12391249
Ok(())
12401250
}
1251+
1252+
/// A public interface for `pallet_grandpa::Pallet::schedule_grandpa_change`.
1253+
///
1254+
/// Schedule a change in the authorities.
1255+
///
1256+
/// The change will be applied at the end of execution of the block `in_blocks` after the
1257+
/// current block. This value may be 0, in which case the change is applied at the end of
1258+
/// the current block.
1259+
///
1260+
/// If the `forced` parameter is defined, this indicates that the current set has been
1261+
/// synchronously determined to be offline and that after `in_blocks` the given change
1262+
/// should be applied. The given block number indicates the median last finalized block
1263+
/// number and it should be used as the canon block when starting the new grandpa voter.
1264+
///
1265+
/// No change should be signaled while any change is pending. Returns an error if a change
1266+
/// is already pending.
1267+
#[pallet::call_index(59)]
1268+
#[pallet::weight(<T as Config>::WeightInfo::swap_authorities(next_authorities.len() as u32))]
1269+
pub fn schedule_grandpa_change(
1270+
origin: OriginFor<T>,
1271+
// grandpa ID is always the same type, so we don't need to parametrize it via `Config`
1272+
next_authorities: AuthorityList,
1273+
in_blocks: BlockNumberFor<T>,
1274+
forced: Option<BlockNumberFor<T>>,
1275+
) -> DispatchResult {
1276+
ensure_root(origin)?;
1277+
T::Grandpa::schedule_change(next_authorities, in_blocks, forced)
1278+
}
12411279
}
12421280
}
12431281

@@ -1255,3 +1293,27 @@ pub trait AuraInterface<AuthorityId, MaxAuthorities> {
12551293
impl<A, M> AuraInterface<A, M> for () {
12561294
fn change_authorities(_: BoundedVec<A, M>) {}
12571295
}
1296+
1297+
pub trait GrandpaInterface<Runtime>
1298+
where
1299+
Runtime: frame_system::Config,
1300+
{
1301+
fn schedule_change(
1302+
next_authorities: AuthorityList,
1303+
in_blocks: BlockNumberFor<Runtime>,
1304+
forced: Option<BlockNumberFor<Runtime>>,
1305+
) -> DispatchResult;
1306+
}
1307+
1308+
impl<R> GrandpaInterface<R> for ()
1309+
where
1310+
R: frame_system::Config,
1311+
{
1312+
fn schedule_change(
1313+
_next_authorities: AuthorityList,
1314+
_in_blocks: BlockNumberFor<R>,
1315+
_forced: Option<BlockNumberFor<R>>,
1316+
) -> DispatchResult {
1317+
Ok(())
1318+
}
1319+
}

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

Lines changed: 28 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, 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::{
@@ -29,7 +30,8 @@ frame_support::construct_runtime!(
2930
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>, Error<T>} = 4,
3031
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 5,
3132
Drand: pallet_drand::{Pallet, Call, Storage, Event<T>} = 6,
32-
EVMChainId: pallet_evm_chain_id = 7,
33+
Grandpa: pallet_grandpa = 7,
34+
EVMChainId: pallet_evm_chain_id = 8,
3335
}
3436
);
3537

@@ -225,6 +227,19 @@ impl system::Config for Test {
225227
type Nonce = u64;
226228
}
227229

230+
impl pallet_grandpa::Config for Test {
231+
type RuntimeEvent = RuntimeEvent;
232+
233+
type KeyOwnerProof = sp_core::Void;
234+
235+
type WeightInfo = ();
236+
type MaxAuthorities = ConstU32<32>;
237+
type MaxSetIdSessionEntries = ConstU64<0>;
238+
type MaxNominators = ConstU32<20>;
239+
240+
type EquivocationReportSystem = ();
241+
}
242+
228243
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
229244
impl pallet_balances::Config for Test {
230245
type MaxLocks = ();
@@ -249,11 +264,23 @@ impl PrivilegeCmp<OriginCaller> for OriginPrivilegeCmp {
249264
}
250265
}
251266

267+
pub struct GrandpaInterfaceImpl;
268+
impl crate::GrandpaInterface<Test> for GrandpaInterfaceImpl {
269+
fn schedule_change(
270+
next_authorities: GrandpaAuthorityList,
271+
in_blocks: BlockNumber,
272+
forced: Option<BlockNumber>,
273+
) -> sp_runtime::DispatchResult {
274+
Grandpa::schedule_change(next_authorities, in_blocks, forced)
275+
}
276+
}
277+
252278
impl crate::Config for Test {
253279
type RuntimeEvent = RuntimeEvent;
254280
type AuthorityId = AuraId;
255281
type MaxAuthorities = ConstU32<32>;
256282
type Aura = ();
283+
type Grandpa = GrandpaInterfaceImpl;
257284
type Balance = Balance;
258285
type WeightInfo = ();
259286
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ use frame_support::sp_runtime::DispatchError;
22
use frame_support::{
33
assert_err, assert_noop, assert_ok,
44
dispatch::{DispatchClass, GetDispatchInfo, Pays},
5+
traits::Hooks,
56
};
67
use frame_system::Config;
78
use pallet_subtensor::Error as SubtensorError;
89
use pallet_subtensor::{migrations, Event};
9-
use sp_core::U256;
10+
use sp_consensus_grandpa::AuthorityId as GrandpaId;
11+
use sp_core::{ed25519, Pair, U256};
1012

1113
use crate::Error;
1214
use mock::*;
@@ -1466,3 +1468,25 @@ fn test_sudo_non_root_cannot_set_evm_chain_id() {
14661468
assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), 0);
14671469
});
14681470
}
1471+
1472+
#[test]
1473+
fn test_schedule_grandpa_change() {
1474+
new_test_ext().execute_with(|| {
1475+
assert_eq!(Grandpa::grandpa_authorities(), vec![]);
1476+
1477+
let bob: GrandpaId = ed25519::Pair::from_legacy_string("//Bob", None)
1478+
.public()
1479+
.into();
1480+
1481+
assert_ok!(AdminUtils::schedule_grandpa_change(
1482+
RuntimeOrigin::root(),
1483+
vec![(bob.clone(), 1)],
1484+
41,
1485+
None
1486+
));
1487+
1488+
Grandpa::on_finalize(42);
1489+
1490+
assert_eq!(Grandpa::grandpa_authorities(), vec![(bob, 1)]);
1491+
});
1492+
}

pallets/admin-utils/src/weights.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub trait WeightInfo {
6363
fn sudo_set_commit_reveal_weights_interval() -> Weight;
6464
fn sudo_set_commit_reveal_weights_enabled() -> Weight;
6565
fn sudo_set_evm_chain_id() -> Weight;
66+
fn schedule_grandpa_change(a: u32) -> Weight;
6667
}
6768

6869
/// Weights for `pallet_admin_utils` using the Substrate node and recommended hardware.
@@ -436,6 +437,11 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
436437
Weight::from_parts(20_200_000, 0)
437438
.saturating_add(RocksDbWeight::get().writes(1_u64))
438439
}
440+
441+
fn schedule_grandpa_change(_a: u32) -> Weight {
442+
// TODO should be replaced by benchmarked weights
443+
Weight::default()
444+
}
439445
}
440446

441447
// For backwards compatibility and tests.
@@ -814,4 +820,8 @@ impl WeightInfo for () {
814820
Weight::from_parts(20_200_000, 0)
815821
.saturating_add(RocksDbWeight::get().writes(1_u64))
816822
}
817-
}
823+
fn schedule_grandpa_change(_a: u32) -> Weight {
824+
// TODO should be replaced by benchmarked weights
825+
Weight::default()
826+
}
827+
}

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)