From 9f950256210f0e4f3f5ebe947225be8df8dfae4e Mon Sep 17 00:00:00 2001 From: Michal Lustyk Date: Tue, 17 Oct 2023 17:20:01 +0200 Subject: [PATCH] BLOCKCHAIN-187 added tests and improvements --- bin/node/runtime/src/impls.rs | 77 ++++++++++++++++++++++++++++++++--- bin/node/runtime/src/lib.rs | 4 +- 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/bin/node/runtime/src/impls.rs b/bin/node/runtime/src/impls.rs index 1997dafd2..015ba34a8 100644 --- a/bin/node/runtime/src/impls.rs +++ b/bin/node/runtime/src/impls.rs @@ -20,13 +20,12 @@ use super::*; use crate::{ AccountId, Assets, Authorship, Balances, NegativeImbalance, Runtime, Balance, RuntimeCall, - Democracy, RuntimeOrigin, EnsureRootOrHalfSenate + 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, @@ -302,10 +301,10 @@ impl liberland_traits::OnLLMPoliticsUnlock for OnLLMPoliticsUnlock } } -pub struct RootOrHalfSenateCmp; -impl PrivilegeCmp for RootOrHalfSenateCmp { +pub struct EnsureCmp(sp_std::marker::PhantomData); +impl> PrivilegeCmp for EnsureCmp { fn cmp_privilege(left: &OriginCaller, _: &OriginCaller) -> Option { - if EnsureRootOrHalfSenate::try_origin( + if L::try_origin( >::into(left.clone()) ).is_ok() { Some(Ordering::Equal) @@ -539,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); @@ -887,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 1939bce1b..b5beb1e79 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -99,7 +99,7 @@ pub mod impls; use impls::{ Author, CreditToBlockAuthor, OnStakerSlashNoop, ToAccountId, IdentityCallFilter, RegistryCallFilter, NftsCallFilter, OnLLMPoliticsUnlock, - ContainsMember, CouncilAccountCallFilter, RootOrHalfSenateCmp + ContainsMember, CouncilAccountCallFilter, EnsureCmp }; /// Constant values used within the runtime. @@ -411,7 +411,7 @@ impl pallet_scheduler::Config for Runtime { #[cfg(not(feature = "runtime-benchmarks"))] type MaxScheduledPerBlock = ConstU32<50>; type WeightInfo = pallet_scheduler::weights::SubstrateWeight; - type OriginPrivilegeCmp = RootOrHalfSenateCmp; + type OriginPrivilegeCmp = EnsureCmp; type Preimages = Preimage; }