Skip to content

Commit

Permalink
BLOCKCHAIN-187 added tests and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Lustyk authored and Michal Lustyk committed Oct 17, 2023
1 parent 44c27e9 commit 9f95025
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
77 changes: 71 additions & 6 deletions bin/node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -302,10 +301,10 @@ impl liberland_traits::OnLLMPoliticsUnlock<AccountId32> for OnLLMPoliticsUnlock
}
}

pub struct RootOrHalfSenateCmp;
impl PrivilegeCmp<OriginCaller> for RootOrHalfSenateCmp {
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 EnsureRootOrHalfSenate::try_origin(
if L::try_origin(
<OriginCaller as Into<RuntimeOrigin>>::into(left.clone())
).is_ok() {
Some(Ordering::Equal)
Expand Down Expand Up @@ -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 = <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 @@ -887,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
);
}
}
4 changes: 2 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Runtime>;
type OriginPrivilegeCmp = RootOrHalfSenateCmp;
type OriginPrivilegeCmp = EnsureCmp<EnsureRootOrHalfSenate>;
type Preimages = Preimage;
}

Expand Down

0 comments on commit 9f95025

Please sign in to comment.