Skip to content

Commit 93b4e3b

Browse files
committed
Implement granpda setting extrinsic
1 parent 772fffd commit 93b4e3b

File tree

6 files changed

+88
-9
lines changed

6 files changed

+88
-9
lines changed

Cargo.lock

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

pallets/admin-utils/Cargo.toml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ sp-tracing = { workspace = true }
4040
sp-consensus-aura = { workspace = true }
4141
pallet-balances = { workspace = true, features = ["std"] }
4242
pallet-scheduler = { workspace = true }
43+
pallet-grandpa = { workspace = true }
4344
sp-std = { workspace = true }
4445

4546
[features]
@@ -53,6 +54,7 @@ std = [
5354
"pallet-balances/std",
5455
"pallet-drand/std",
5556
"pallet-evm-chain-id/std",
57+
"pallet-grandpa/std",
5658
"pallet-scheduler/std",
5759
"pallet-subtensor/std",
5860
"scale-info/std",
@@ -71,18 +73,20 @@ runtime-benchmarks = [
7173
"frame-support/runtime-benchmarks",
7274
"frame-system/runtime-benchmarks",
7375
"pallet-balances/runtime-benchmarks",
74-
"sp-runtime/runtime-benchmarks",
75-
"pallet-subtensor/runtime-benchmarks",
76-
"pallet-scheduler/runtime-benchmarks",
7776
"pallet-drand/runtime-benchmarks",
77+
"pallet-grandpa/runtime-benchmarks",
78+
"pallet-scheduler/runtime-benchmarks",
79+
"pallet-subtensor/runtime-benchmarks",
80+
"sp-runtime/runtime-benchmarks",
7881
]
7982
try-runtime = [
8083
"frame-support/try-runtime",
8184
"frame-system/try-runtime",
8285
"pallet-balances/try-runtime",
86+
"pallet-drand/try-runtime",
8387
"pallet-evm-chain-id/try-runtime",
88+
"pallet-grandpa/try-runtime",
8489
"pallet-scheduler/try-runtime",
85-
"sp-runtime/try-runtime",
8690
"pallet-subtensor/try-runtime",
87-
"pallet-drand/try-runtime",
91+
"sp-runtime/try-runtime",
8892
]

pallets/admin-utils/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,20 @@ pub mod pallet {
12441244
}
12451245

12461246
/// A public interface for `pallet_grandpa::Pallet::schedule_grandpa_change`.
1247+
///
1248+
/// Schedule a change in the authorities.
1249+
///
1250+
/// The change will be applied at the end of execution of the block `in_blocks` after the
1251+
/// current block. This value may be 0, in which case the change is applied at the end of
1252+
/// the current block.
1253+
///
1254+
/// If the `forced` parameter is defined, this indicates that the current set has been
1255+
/// synchronously determined to be offline and that after `in_blocks` the given change
1256+
/// should be applied. The given block number indicates the median last finalized block
1257+
/// number and it should be used as the canon block when starting the new grandpa voter.
1258+
///
1259+
/// No change should be signaled while any change is pending. Returns an error if a change
1260+
/// is already pending.
12471261
#[pallet::call_index(59)]
12481262
#[pallet::weight(<T as Config>::WeightInfo::swap_authorities(next_authorities.len() as u32))]
12491263
pub fn schedule_grandpa_change(

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use frame_support::{
66
weights,
77
};
88
use frame_system as system;
9-
use frame_system::{limits, EnsureNever, EnsureRoot};
9+
use frame_system::{limits, pallet_prelude::BlockNumberFor, EnsureNever, EnsureRoot};
1010
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
1111
use sp_core::U256;
1212
use sp_core::{ConstU64, H256};
@@ -29,7 +29,8 @@ frame_support::construct_runtime!(
2929
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>, Error<T>} = 4,
3030
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 5,
3131
Drand: pallet_drand::{Pallet, Call, Storage, Event<T>} = 6,
32-
EVMChainId: pallet_evm_chain_id = 7,
32+
Grandpa: pallet_grandpa = 7,
33+
EVMChainId: pallet_evm_chain_id = 8,
3334
}
3435
);
3536

@@ -225,6 +226,19 @@ impl system::Config for Test {
225226
type Nonce = u64;
226227
}
227228

229+
impl pallet_grandpa::Config for Test {
230+
type RuntimeEvent = RuntimeEvent;
231+
232+
type KeyOwnerProof = sp_core::Void;
233+
234+
type WeightInfo = ();
235+
type MaxAuthorities = ConstU32<32>;
236+
type MaxSetIdSessionEntries = ConstU64<0>;
237+
type MaxNominators = ConstU32<20>;
238+
239+
type EquivocationReportSystem = ();
240+
}
241+
228242
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
229243
impl pallet_balances::Config for Test {
230244
type MaxLocks = ();
@@ -249,11 +263,23 @@ impl PrivilegeCmp<OriginCaller> for OriginPrivilegeCmp {
249263
}
250264
}
251265

266+
pub struct GrandpaInterfaceImpl;
267+
impl crate::GrandpaInterface<Test> for GrandpaInterfaceImpl {
268+
fn schedule_change(
269+
next_authorities: Vec<(pallet_grandpa::AuthorityId, u64)>,
270+
in_blocks: BlockNumberFor<Test>,
271+
forced: Option<BlockNumberFor<Test>>,
272+
) -> sp_runtime::DispatchResult {
273+
Grandpa::schedule_change(next_authorities, in_blocks, forced)
274+
}
275+
}
276+
252277
impl crate::Config for Test {
253278
type RuntimeEvent = RuntimeEvent;
254279
type AuthorityId = AuraId;
255280
type MaxAuthorities = ConstU32<32>;
256281
type Aura = ();
282+
type Grandpa = GrandpaInterfaceImpl;
257283
type Balance = Balance;
258284
type WeightInfo = ();
259285
}

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 benchmared weights
443+
10_000.into()
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 benchmared weights
825+
10_000.into()
826+
}
827+
}

0 commit comments

Comments
 (0)