From b490e594f17baae327baacfdad599fd1ffbf0333 Mon Sep 17 00:00:00 2001 From: Ayden Brewer Date: Tue, 11 Jul 2023 14:33:28 -0700 Subject: [PATCH 1/2] Add scheduler pallet to runtime --- Cargo.lock | 36 +++++++++++++++++++++++++ runtime/Cargo.toml | 8 ++++++ runtime/src/lib.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 107 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 367708fa43..8e994844d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4195,6 +4195,8 @@ dependencies = [ "pallet-insecure-randomness-collective-flip", "pallet-membership", "pallet-multisig", + "pallet-preimage", + "pallet-scheduler", "pallet-subtensor", "pallet-sudo", "pallet-timestamp", @@ -4545,6 +4547,40 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-preimage" +version = "4.0.0-dev" +source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.39#8c4b84520cee2d7de53cc33cb67605ce4efefba8" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "log", + "parity-scale-codec", + "scale-info", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", +] + +[[package]] +name = "pallet-scheduler" +version = "4.0.0-dev" +source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.39#8c4b84520cee2d7de53cc33cb67605ce4efefba8" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "log", + "parity-scale-codec", + "scale-info", + "sp-io", + "sp-runtime", + "sp-std", + "sp-weights", +] + [[package]] name = "pallet-session" version = "4.0.0-dev" diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index c10ed1fe4b..39c84fa592 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -55,6 +55,10 @@ pallet-membership = {version = "4.0.0-dev", default-features = false, git = "htt # Multisig pallet-multisig = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" } +# Scheduler pallet +pallet-scheduler = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" } +pallet-preimage = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" } + # Used for the node subtensor's RPCs frame-system-rpc-runtime-api = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" } pallet-transaction-payment-rpc-runtime-api = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" } @@ -90,6 +94,8 @@ std = [ "pallet-utility/std", "pallet-sudo/std", "pallet-multisig/std", + "pallet-scheduler/std", + "pallet-preimage/std", "sp-api/std", "sp-block-builder/std", "sp-consensus-aura/std", @@ -136,4 +142,6 @@ try-runtime = [ "pallet-collective/try-runtime", "pallet-membership/try-runtime", "pallet-multisig/try-runtime", + "pallet-scheduler/try-runtime", + "pallet-preimage/try-runtime", ] diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 5bb9a1be38..f4fbfae4b1 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -29,6 +29,7 @@ use sp_runtime::{ }; use sp_std::prelude::*; +use sp_std::cmp::Ordering; #[cfg(feature = "std")] use sp_version::NativeVersion; use sp_version::RuntimeVersion; @@ -37,7 +38,7 @@ use sp_version::RuntimeVersion; pub use frame_support::{ construct_runtime, parameter_types, traits::{ - ConstU128, ConstU32, ConstU64, ConstU8, KeyOwnerProofSystem, Randomness, StorageInfo, + ConstU128, ConstU32, ConstU64, ConstU8, KeyOwnerProofSystem, Randomness, StorageInfo, PrivilegeCmp }, weights::{ constants::{ @@ -491,6 +492,64 @@ impl pallet_multisig::Config for Runtime { type WeightInfo = pallet_multisig::weights::SubstrateWeight; } +parameter_types! { + pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * + BlockWeights::get().max_block; + pub const MaxScheduledPerBlock: u32 = 50; + pub const NoPreimagePostponement: Option = Some(10); +} + +/// Used the compare the privilege of an origin inside the scheduler. +pub struct OriginPrivilegeCmp; + +impl PrivilegeCmp for OriginPrivilegeCmp { + fn cmp_privilege(left: &OriginCaller, right: &OriginCaller) -> Option { + if left == right { + return Some(Ordering::Equal) + } + + match (left, right) { + // Root is greater than anything. + (OriginCaller::system(frame_system::RawOrigin::Root), _) => Some(Ordering::Greater), + // Check which one has more yes votes. + ( + OriginCaller::Senate(pallet_collective::RawOrigin::Members(l_yes_votes, l_count)), + OriginCaller::Senate(pallet_collective::RawOrigin::Members(r_yes_votes, r_count)), + ) => Some((l_yes_votes * r_count).cmp(&(r_yes_votes * l_count))), + // For every other origin we don't care, as they are not used for `ScheduleOrigin`. + _ => None, + } + } +} + +impl pallet_scheduler::Config for Runtime { + type RuntimeOrigin = RuntimeOrigin; + type RuntimeEvent = RuntimeEvent; + type PalletsOrigin = OriginCaller; + type RuntimeCall = RuntimeCall; + type MaximumWeight = MaximumSchedulerWeight; + type ScheduleOrigin = EnsureRoot; + type MaxScheduledPerBlock = MaxScheduledPerBlock; + type WeightInfo = pallet_scheduler::weights::SubstrateWeight; + type OriginPrivilegeCmp = OriginPrivilegeCmp; + type Preimages = Preimage; +} + +parameter_types! { + pub const PreimageMaxSize: u32 = 4096 * 1024; + pub const PreimageBaseDeposit: Balance = (2) as Balance * 2_000 * 10_000_000 + (64 as Balance) * 100 * 1_000_000; + pub const PreimageByteDeposit: Balance = (0) as Balance * 2_000 * 10_000_000 + (1 as Balance) * 100 * 1_000_000; +} + +impl pallet_preimage::Config for Runtime { + type WeightInfo = pallet_preimage::weights::SubstrateWeight; + type RuntimeEvent = RuntimeEvent; + type Currency = Balances; + type ManagerOrigin = EnsureRoot; + type BaseDeposit = PreimageBaseDeposit; + type ByteDeposit = PreimageByteDeposit; +} + // Configure the pallet subtensor. parameter_types! { pub const SubtensorInitialRho: u16 = 10; @@ -603,7 +662,9 @@ construct_runtime!( SenateMembers: pallet_membership::::{Pallet, Call, Storage, Event, Config}, Utility: pallet_utility, Sudo: pallet_sudo, - Multisig: pallet_multisig + Multisig: pallet_multisig, + Preimage: pallet_preimage, + Scheduler: pallet_scheduler } ); From d5089e210e55d24e5741b89731b249bb18b8e183 Mon Sep 17 00:00:00 2001 From: Ayden Brewer Date: Tue, 11 Jul 2023 16:34:19 -0700 Subject: [PATCH 2/2] Update runtime/src/lib.rs Co-authored-by: Cameron Fairchild --- runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index f4fbfae4b1..0fcf8763e7 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -514,7 +514,7 @@ impl PrivilegeCmp for OriginPrivilegeCmp { // Check which one has more yes votes. ( OriginCaller::Senate(pallet_collective::RawOrigin::Members(l_yes_votes, l_count)), - OriginCaller::Senate(pallet_collective::RawOrigin::Members(r_yes_votes, r_count)), + OriginCaller::Senate(pallet_collective::RawOrigin::Members(r_yes_votes, r_count)), // Equivalent to (l_yes_votes / l_count).cmp(&(r_yes_votes / r_count)) ) => Some((l_yes_votes * r_count).cmp(&(r_yes_votes * l_count))), // For every other origin we don't care, as they are not used for `ScheduleOrigin`. _ => None,