diff --git a/bin/node/runtime/src/impls.rs b/bin/node/runtime/src/impls.rs index e2af63fd8..015ba34a8 100644 --- a/bin/node/runtime/src/impls.rs +++ b/bin/node/runtime/src/impls.rs @@ -17,25 +17,25 @@ //! Some configurable implementations as associated type for the substrate runtime. +use super::*; use crate::{ AccountId, Assets, Authorship, Balances, NegativeImbalance, Runtime, Balance, RuntimeCall, - Democracy, RuntimeOrigin, + Democracy, RuntimeOrigin }; use codec::{Encode, Decode}; use frame_support::{ - BoundedVec, + BoundedVec, RuntimeDebug, pallet_prelude::{ConstU32, PhantomData, Get, MaxEncodedLen}, - RuntimeDebug, traits::{ fungibles::{Balanced, Credit}, Currency, OnUnbalanced, InstanceFilter, - Contains, + Contains, PrivilegeCmp, EnsureOrigin }, }; use sp_runtime::{AccountId32, DispatchError, traits::{TrailingZeroInput, Morph}}; use pallet_asset_tx_payment::HandleCredit; use sp_staking::{EraIndex, OnStakerSlash}; -use sp_std::{vec, collections::btree_map::BTreeMap, cmp::{max, min}}; +use sp_std::{vec, collections::btree_map::BTreeMap, cmp::{max, min, Ordering}}; #[cfg(feature = "std")] use serde::{Deserialize, Serialize}; @@ -301,6 +301,19 @@ impl liberland_traits::OnLLMPoliticsUnlock for OnLLMPoliticsUnlock } } +pub struct EnsureCmp(sp_std::marker::PhantomData); +impl> PrivilegeCmp for EnsureCmp { + fn cmp_privilege(left: &OriginCaller, _: &OriginCaller) -> Option { + if L::try_origin( + >::into(left.clone()) + ).is_ok() { + Some(Ordering::Equal) + } else { + None + } + } +} + #[derive( Clone, Copy, @@ -525,6 +538,27 @@ mod multiplier_tests { }); } + pub use node_primitives::Signature; + use sp_core::{Public, Pair}; + use sp_runtime::traits::{IdentifyAccount, Verify}; + + type AccountPublic = ::Signer; + + /// Helper function to generate a crypto pair from seed + pub fn get_from_seed(seed: &str) -> ::Public { + TPublic::Pair::from_string(&format!("//{}", seed), None) + .expect("static values are valid; qed") + .public() + } + + /// Helper function to generate an account ID from seed + pub fn get_account_id_from_seed(seed: &str) -> AccountId + where + AccountPublic: From<::Public>, + { + AccountPublic::from(get_from_seed::(seed)).into_account() + } + #[test] fn truth_value_update_poc_works() { let fm = Multiplier::saturating_from_rational(1, 2); @@ -873,4 +907,49 @@ mod multiplier_tests { assert!(!LandMetadataValidator::::validate_metadata(1, 1, &invalid_coord)); assert!(!LandMetadataValidator::::validate_metadata(1, 1, &self_intersecting)); } + + use frame_support::traits::EitherOfDiverse; + use frame_system::{EnsureRoot, RawOrigin}; + use node_primitives::AccountId; + use core::cmp::Ordering; + use sp_runtime::testing::sr25519; + use crate::{EnsureSenateMajority, EnsureCmp, OriginCaller, sp_api_hidden_includes_construct_runtime::hidden_include::traits::PrivilegeCmp}; + + #[test] + fn ensure_cmp_works_for_root() { + type OriginPrivilegeCmp = EnsureCmp< + EitherOfDiverse< + EnsureRoot, + EnsureSenateMajority, + > + >; + + assert_eq!( + OriginPrivilegeCmp::cmp_privilege( + &OriginCaller::system(RawOrigin::Root), + &OriginCaller::system(RawOrigin::Root) + ), + Some(Ordering::Equal) + ); + } + + #[test] + fn ensure_cmp_do_not_works_for_account() { + let alice = get_account_id_from_seed::("Alice"); + + type OriginPrivilegeCmp = EnsureCmp< + EitherOfDiverse< + EnsureRoot, + EnsureSenateMajority, + > + >; + + assert_eq!( + OriginPrivilegeCmp::cmp_privilege( + &OriginCaller::system(RawOrigin::Signed(alice)), + &OriginCaller::system(RawOrigin::Root) + ), + None + ); + } } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 50f136d4e..6e39dd8be 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -37,8 +37,8 @@ use frame_support::{ traits::{ tokens::nonfungibles_v2::Inspect, AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32, MapSuccess, - Currency, EitherOf, EitherOfDiverse, EqualPrivilegeOnly, Everything, Imbalance, InstanceFilter, - KeyOwnerProofSystem, LockIdentifier, Nothing, OnUnbalanced, U128CurrencyToVote, + Currency, EitherOf, EitherOfDiverse, Everything, Imbalance, InstanceFilter, + KeyOwnerProofSystem, LockIdentifier, Nothing, OnUnbalanced, U128CurrencyToVote }, weights::{ constants::{ @@ -99,7 +99,7 @@ pub mod impls; use impls::{ Author, CreditToBlockAuthor, OnStakerSlashNoop, ToAccountId, IdentityCallFilter, RegistryCallFilter, NftsCallFilter, OnLLMPoliticsUnlock, - ContainsMember, CouncilAccountCallFilter, + ContainsMember, CouncilAccountCallFilter, EnsureCmp }; /// Constant values used within the runtime. @@ -265,8 +265,8 @@ impl frame_system::Config for Runtime { #[cfg(not(feature = "testnet-runtime"))] parameter_types! { - pub const LaunchPeriod: BlockNumber = 14 * DAYS; - pub const VotingPeriod: BlockNumber = 14 * DAYS; + pub const LaunchPeriod: BlockNumber = 10; + pub const VotingPeriod: BlockNumber = 10; pub const TermDuration: BlockNumber = 168 * DAYS; pub const EnactmentPeriod: BlockNumber = 14 * DAYS; pub const AssetName: &'static str = "Liberland Merit"; @@ -394,6 +394,21 @@ impl pallet_proxy::Config for Runtime { type AnnouncementDepositFactor = AnnouncementDepositFactor; } +type EnsureCouncilMajority = pallet_collective::EnsureProportionMoreThan; +type EnsureSenateMajority = pallet_collective::EnsureProportionMoreThan; +type EnsureRootOrHalfCouncil = EitherOfDiverse< + EnsureRoot, + EnsureCouncilMajority, +>; +type EnsureSenateOrCouncilMajority = EitherOfDiverse< + EnsureSenateMajority, + EnsureCouncilMajority, +>; +type EnsureRootOrHalfSenate = EitherOfDiverse< + EnsureRoot, + EnsureSenateMajority, +>; + parameter_types! { pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * RuntimeBlockWeights::get().max_block; @@ -405,13 +420,13 @@ impl pallet_scheduler::Config for Runtime { type PalletsOrigin = OriginCaller; type RuntimeCall = RuntimeCall; type MaximumWeight = MaximumSchedulerWeight; - type ScheduleOrigin = EnsureRoot; + type ScheduleOrigin = EnsureRootOrHalfSenate; #[cfg(feature = "runtime-benchmarks")] type MaxScheduledPerBlock = ConstU32<512>; #[cfg(not(feature = "runtime-benchmarks"))] type MaxScheduledPerBlock = ConstU32<50>; type WeightInfo = pallet_scheduler::weights::SubstrateWeight; - type OriginPrivilegeCmp = EqualPrivilegeOnly; + type OriginPrivilegeCmp = EnsureCmp; type Preimages = Preimage; } @@ -584,17 +599,6 @@ impl pallet_staking::BenchmarkingConfig for StakingBenchmarkingConfig { type MaxValidators = ConstU32<1000>; } -type EnsureCouncilMajority = pallet_collective::EnsureProportionMoreThan; -type EnsureSenateMajority = pallet_collective::EnsureProportionMoreThan; -type EnsureRootOrHalfCouncil = EitherOfDiverse< - EnsureRoot, - EnsureCouncilMajority, ->; -type EnsureSenateOrCouncilMajority = EitherOfDiverse< - EnsureSenateMajority, - EnsureCouncilMajority, ->; - impl pallet_staking::Config for Runtime { type MaxNominations = MaxNominations; type Currency = Balances;