Skip to content

Commit

Permalink
Merge pull request #1084 from opentensor/feat/grandpa-authorities-set…
Browse files Browse the repository at this point in the history
…ting

Add scheduleGrandpaChange call to admin utils
  • Loading branch information
sam0x17 authored Dec 11, 2024
2 parents f5542c1 + fe344c2 commit 552fa1b
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ sp-block-builder = { git = "https://github.com/paritytech/polkadot-sdk.git", tag
sp-blockchain = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
sp-consensus = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409" }
sp-consensus-aura = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
sp-consensus-grandpa = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409" }
sp-consensus-grandpa = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
sp-genesis-builder = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
sp-core = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
sp-inherents = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
Expand Down
34 changes: 20 additions & 14 deletions pallets/admin-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ workspace = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
subtensor-macros.workspace = true
subtensor-macros = { workspace = true }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive",
] }
Expand All @@ -31,6 +31,7 @@ sp-weights = { workspace = true }
substrate-fixed = { workspace = true }
pallet-evm-chain-id = { workspace = true }
pallet-drand = { workspace = true, default-features = false }
sp-consensus-grandpa = { workspace = true }

[dev-dependencies]
sp-core = { workspace = true }
Expand All @@ -39,6 +40,7 @@ sp-tracing = { workspace = true }
sp-consensus-aura = { workspace = true }
pallet-balances = { workspace = true, features = ["std"] }
pallet-scheduler = { workspace = true }
pallet-grandpa = { workspace = true }
sp-std = { workspace = true }

[features]
Expand All @@ -48,39 +50,43 @@ std = [
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"pallet-subtensor/std",
"sp-consensus-aura/std",
"log/std",
"pallet-balances/std",
"pallet-drand/std",
"pallet-evm-chain-id/std",
"pallet-grandpa/std",
"pallet-scheduler/std",
"sp-runtime/std",
"sp-tracing/std",
"sp-weights/std",
"log/std",
"pallet-subtensor/std",
"scale-info/std",
"sp-consensus-aura/std",
"sp-consensus-grandpa/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
"sp-tracing/std",
"sp-weights/std",
"substrate-fixed/std",
"pallet-drand/std"
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-subtensor/runtime-benchmarks",
"pallet-drand/runtime-benchmarks",
"pallet-grandpa/runtime-benchmarks",
"pallet-scheduler/runtime-benchmarks",
"pallet-drand/runtime-benchmarks"
"pallet-subtensor/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-balances/try-runtime",
"pallet-drand/try-runtime",
"pallet-evm-chain-id/try-runtime",
"pallet-grandpa/try-runtime",
"pallet-scheduler/try-runtime",
"sp-runtime/try-runtime",
"pallet-subtensor/try-runtime",
"pallet-drand/try-runtime"
"sp-runtime/try-runtime",
]
11 changes: 11 additions & 0 deletions pallets/admin-utils/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ mod benchmarks {
_(RawOrigin::Root, value);
}

#[benchmark]
fn schedule_grandpa_change(a: Linear<0, 32>) {
let next_authorities = (1..=a)
.map(|idx| account("Authority", idx, 0u32))
.collect::<Vec<(sp_consensus_grandpa::AuthorityId, u64)>>();
let in_blocks = BlockNumberFor::<T>::from(42u32);

#[extrinsic_call]
_(RawOrigin::Root, next_authorities, in_blocks, None);
}

#[benchmark]
fn sudo_set_default_take() {
#[extrinsic_call]
Expand Down
64 changes: 63 additions & 1 deletion pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#![cfg_attr(not(feature = "std"), no_std)]

// extern crate alloc;

pub use pallet::*;
pub mod weights;
pub use weights::WeightInfo;

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

mod benchmarking;

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

/// Implementation of [`GrandpaInterface`]
type Grandpa: crate::GrandpaInterface<Self>;

/// The identifier type for an authority.
type AuthorityId: Member
+ Parameter
Expand Down Expand Up @@ -1238,6 +1248,34 @@ pub mod pallet {
ChainId::<T>::set(chain_id);
Ok(())
}

/// A public interface for `pallet_grandpa::Pallet::schedule_grandpa_change`.
///
/// Schedule a change in the authorities.
///
/// The change will be applied at the end of execution of the block `in_blocks` after the
/// current block. This value may be 0, in which case the change is applied at the end of
/// the current block.
///
/// If the `forced` parameter is defined, this indicates that the current set has been
/// synchronously determined to be offline and that after `in_blocks` the given change
/// should be applied. The given block number indicates the median last finalized block
/// number and it should be used as the canon block when starting the new grandpa voter.
///
/// No change should be signaled while any change is pending. Returns an error if a change
/// is already pending.
#[pallet::call_index(59)]
#[pallet::weight(<T as Config>::WeightInfo::swap_authorities(next_authorities.len() as u32))]
pub fn schedule_grandpa_change(
origin: OriginFor<T>,
// grandpa ID is always the same type, so we don't need to parametrize it via `Config`
next_authorities: AuthorityList,
in_blocks: BlockNumberFor<T>,
forced: Option<BlockNumberFor<T>>,
) -> DispatchResult {
ensure_root(origin)?;
T::Grandpa::schedule_change(next_authorities, in_blocks, forced)
}
}
}

Expand All @@ -1255,3 +1293,27 @@ pub trait AuraInterface<AuthorityId, MaxAuthorities> {
impl<A, M> AuraInterface<A, M> for () {
fn change_authorities(_: BoundedVec<A, M>) {}
}

pub trait GrandpaInterface<Runtime>
where
Runtime: frame_system::Config,
{
fn schedule_change(
next_authorities: AuthorityList,
in_blocks: BlockNumberFor<Runtime>,
forced: Option<BlockNumberFor<Runtime>>,
) -> DispatchResult;
}

impl<R> GrandpaInterface<R> for ()
where
R: frame_system::Config,
{
fn schedule_change(
_next_authorities: AuthorityList,
_in_blocks: BlockNumberFor<R>,
_forced: Option<BlockNumberFor<R>>,
) -> DispatchResult {
Ok(())
}
}
29 changes: 28 additions & 1 deletion pallets/admin-utils/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use frame_support::{
use frame_system as system;
use frame_system::{limits, EnsureNever, EnsureRoot};
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_consensus_grandpa::AuthorityList as GrandpaAuthorityList;
use sp_core::U256;
use sp_core::{ConstU64, H256};
use sp_runtime::{
Expand All @@ -29,7 +30,8 @@ frame_support::construct_runtime!(
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>, Error<T>} = 4,
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 5,
Drand: pallet_drand::{Pallet, Call, Storage, Event<T>} = 6,
EVMChainId: pallet_evm_chain_id = 7,
Grandpa: pallet_grandpa = 7,
EVMChainId: pallet_evm_chain_id = 8,
}
);

Expand Down Expand Up @@ -225,6 +227,19 @@ impl system::Config for Test {
type Nonce = u64;
}

impl pallet_grandpa::Config for Test {
type RuntimeEvent = RuntimeEvent;

type KeyOwnerProof = sp_core::Void;

type WeightInfo = ();
type MaxAuthorities = ConstU32<32>;
type MaxSetIdSessionEntries = ConstU64<0>;
type MaxNominators = ConstU32<20>;

type EquivocationReportSystem = ();
}

#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Test {
type MaxLocks = ();
Expand All @@ -249,11 +264,23 @@ impl PrivilegeCmp<OriginCaller> for OriginPrivilegeCmp {
}
}

pub struct GrandpaInterfaceImpl;
impl crate::GrandpaInterface<Test> for GrandpaInterfaceImpl {
fn schedule_change(
next_authorities: GrandpaAuthorityList,
in_blocks: BlockNumber,
forced: Option<BlockNumber>,
) -> sp_runtime::DispatchResult {
Grandpa::schedule_change(next_authorities, in_blocks, forced)
}
}

impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
type AuthorityId = AuraId;
type MaxAuthorities = ConstU32<32>;
type Aura = ();
type Grandpa = GrandpaInterfaceImpl;
type Balance = Balance;
type WeightInfo = ();
}
Expand Down
26 changes: 25 additions & 1 deletion pallets/admin-utils/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use frame_support::sp_runtime::DispatchError;
use frame_support::{
assert_err, assert_noop, assert_ok,
dispatch::{DispatchClass, GetDispatchInfo, Pays},
traits::Hooks,
};
use frame_system::Config;
use pallet_subtensor::Error as SubtensorError;
use pallet_subtensor::{migrations, Event};
use sp_core::U256;
use sp_consensus_grandpa::AuthorityId as GrandpaId;
use sp_core::{ed25519, Pair, U256};

use crate::Error;
use mock::*;
Expand Down Expand Up @@ -1466,3 +1468,25 @@ fn test_sudo_non_root_cannot_set_evm_chain_id() {
assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), 0);
});
}

#[test]
fn test_schedule_grandpa_change() {
new_test_ext().execute_with(|| {
assert_eq!(Grandpa::grandpa_authorities(), vec![]);

let bob: GrandpaId = ed25519::Pair::from_legacy_string("//Bob", None)
.public()
.into();

assert_ok!(AdminUtils::schedule_grandpa_change(
RuntimeOrigin::root(),
vec![(bob.clone(), 1)],
41,
None
));

Grandpa::on_finalize(42);

assert_eq!(Grandpa::grandpa_authorities(), vec![(bob, 1)]);
});
}
12 changes: 11 additions & 1 deletion pallets/admin-utils/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub trait WeightInfo {
fn sudo_set_commit_reveal_weights_interval() -> Weight;
fn sudo_set_commit_reveal_weights_enabled() -> Weight;
fn sudo_set_evm_chain_id() -> Weight;
fn schedule_grandpa_change(a: u32) -> Weight;
}

/// Weights for `pallet_admin_utils` using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -436,6 +437,11 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
Weight::from_parts(20_200_000, 0)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}

fn schedule_grandpa_change(_a: u32) -> Weight {
// TODO should be replaced by benchmarked weights
Weight::default()
}
}

// For backwards compatibility and tests.
Expand Down Expand Up @@ -814,4 +820,8 @@ impl WeightInfo for () {
Weight::from_parts(20_200_000, 0)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
}
fn schedule_grandpa_change(_a: u32) -> Weight {
// TODO should be replaced by benchmarked weights
Weight::default()
}
}
12 changes: 12 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,11 +1121,23 @@ impl pallet_admin_utils::AuraInterface<AuraId, ConstU32<32>> for AuraPalletIntrf
}
}

pub struct GrandpaInterfaceImpl;
impl pallet_admin_utils::GrandpaInterface<Runtime> for GrandpaInterfaceImpl {
fn schedule_change(
next_authorities: Vec<(pallet_grandpa::AuthorityId, u64)>,
in_blocks: BlockNumber,
forced: Option<BlockNumber>,
) -> sp_runtime::DispatchResult {
Grandpa::schedule_change(next_authorities, in_blocks, forced)
}
}

impl pallet_admin_utils::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AuthorityId = AuraId;
type MaxAuthorities = ConstU32<32>;
type Aura = AuraPalletIntrf;
type Grandpa = GrandpaInterfaceImpl;
type Balance = Balance;
type WeightInfo = pallet_admin_utils::weights::SubstrateWeight<Runtime>;
}
Expand Down

0 comments on commit 552fa1b

Please sign in to comment.