Skip to content

Commit

Permalink
BLOCKCHAIN-187 added EnsureCmp and scheduler rights to senate
Browse files Browse the repository at this point in the history
Added senate scheduler rights
  • Loading branch information
Michal Lustyk authored and Michal Lustyk committed Oct 31, 2023
1 parent e2efbc8 commit 9eb3dbb
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 23 deletions.
89 changes: 84 additions & 5 deletions bin/node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -301,6 +301,19 @@ impl liberland_traits::OnLLMPoliticsUnlock<AccountId32> for OnLLMPoliticsUnlock
}
}

pub struct EnsureCmp<L>(sp_std::marker::PhantomData<L>);
impl<L: EnsureOrigin<RuntimeOrigin>> PrivilegeCmp<OriginCaller> for EnsureCmp<L> {
fn cmp_privilege(left: &OriginCaller, _: &OriginCaller) -> Option<Ordering> {
if L::try_origin(
<OriginCaller as Into<RuntimeOrigin>>::into(left.clone())
).is_ok() {
Some(Ordering::Equal)
} else {
None
}
}
}

#[derive(
Clone,
Copy,
Expand Down Expand Up @@ -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 = <Signature as Verify>::Signer;

/// Helper function to generate a crypto pair from seed
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::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<TPublic: Public>(seed: &str) -> AccountId
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}

#[test]
fn truth_value_update_poc_works() {
let fm = Multiplier::saturating_from_rational(1, 2);
Expand Down Expand Up @@ -873,4 +907,49 @@ mod multiplier_tests {
assert!(!LandMetadataValidator::<TestCoords>::validate_metadata(1, 1, &invalid_coord));
assert!(!LandMetadataValidator::<TestCoords>::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<AccountId>,
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::<sr25519::Public>("Alice");

type OriginPrivilegeCmp = EnsureCmp<
EitherOfDiverse<
EnsureRoot<AccountId>,
EnsureSenateMajority,
>
>;

assert_eq!(
OriginPrivilegeCmp::cmp_privilege(
&OriginCaller::system(RawOrigin::Signed(alice)),
&OriginCaller::system(RawOrigin::Root)
),
None
);
}
}
40 changes: 22 additions & 18 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -394,6 +394,21 @@ impl pallet_proxy::Config for Runtime {
type AnnouncementDepositFactor = AnnouncementDepositFactor;
}

type EnsureCouncilMajority = pallet_collective::EnsureProportionMoreThan<AccountId, CouncilCollective, 1, 2>;
type EnsureSenateMajority = pallet_collective::EnsureProportionMoreThan<AccountId, SenateCollective, 1, 2>;
type EnsureRootOrHalfCouncil = EitherOfDiverse<
EnsureRoot<AccountId>,
EnsureCouncilMajority,
>;
type EnsureSenateOrCouncilMajority = EitherOfDiverse<
EnsureSenateMajority,
EnsureCouncilMajority,
>;
type EnsureRootOrHalfSenate = EitherOfDiverse<
EnsureRoot<AccountId>,
EnsureSenateMajority,
>;

parameter_types! {
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) *
RuntimeBlockWeights::get().max_block;
Expand All @@ -405,13 +420,13 @@ impl pallet_scheduler::Config for Runtime {
type PalletsOrigin = OriginCaller;
type RuntimeCall = RuntimeCall;
type MaximumWeight = MaximumSchedulerWeight;
type ScheduleOrigin = EnsureRoot<AccountId>;
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<Runtime>;
type OriginPrivilegeCmp = EqualPrivilegeOnly;
type OriginPrivilegeCmp = EnsureCmp<EnsureRootOrHalfSenate>;
type Preimages = Preimage;
}

Expand Down Expand Up @@ -584,17 +599,6 @@ impl pallet_staking::BenchmarkingConfig for StakingBenchmarkingConfig {
type MaxValidators = ConstU32<1000>;
}

type EnsureCouncilMajority = pallet_collective::EnsureProportionMoreThan<AccountId, CouncilCollective, 1, 2>;
type EnsureSenateMajority = pallet_collective::EnsureProportionMoreThan<AccountId, SenateCollective, 1, 2>;
type EnsureRootOrHalfCouncil = EitherOfDiverse<
EnsureRoot<AccountId>,
EnsureCouncilMajority,
>;
type EnsureSenateOrCouncilMajority = EitherOfDiverse<
EnsureSenateMajority,
EnsureCouncilMajority,
>;

impl pallet_staking::Config for Runtime {
type MaxNominations = MaxNominations;
type Currency = Balances;
Expand Down

0 comments on commit 9eb3dbb

Please sign in to comment.