diff --git a/Cargo.lock b/Cargo.lock index cb138e2a..5b1e6290 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6277,6 +6277,7 @@ dependencies = [ name = "pallet-roles" version = "0.2.0" dependencies = [ + "cfg-if", "frame-benchmarking", "frame-support", "frame-system", diff --git a/pallets/roles/Cargo.toml b/pallets/roles/Cargo.toml index e925b4f3..2a167f42 100644 --- a/pallets/roles/Cargo.toml +++ b/pallets/roles/Cargo.toml @@ -33,6 +33,7 @@ try-runtime = ['frame-support/try-runtime'] [dependencies] codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } +cfg-if = "1.0.0" scale-info = { version = "2.2.0", default-features = false, features = ["derive"] } # Local dependencies diff --git a/pallets/roles/src/benchmarking.rs b/pallets/roles/src/benchmarking.rs index 1ab88d6c..76a6763e 100644 --- a/pallets/roles/src/benchmarking.rs +++ b/pallets/roles/src/benchmarking.rs @@ -10,27 +10,48 @@ // FIXME: refactor once SpacesInterface is added. -use super::*; +use cfg_if::cfg_if; use frame_benchmarking::{account, benchmarks}; -use frame_support::dispatch::DispatchError; +use frame_support::{dispatch::DispatchError, traits::Get}; use frame_system::RawOrigin; -use pallet_permissions::SpacePermission as SP; -use pallet_spaces::types::Space; use sp_std::{prelude::Vec, vec}; -use subsocial_support::{Content, User}; -use subsocial_support::mock_functions::{valid_content_ipfs, another_valid_content_ipfs}; -fn create_dummy_space( - origin: RawOrigin, -) -> Result, DispatchError> { - let space_id = pallet_spaces::NextSpaceId::::get(); +use pallet_permissions::SpacePermission as SP; +use subsocial_support::{ + mock_functions::{another_valid_content_ipfs, valid_content_ipfs}, + Content, User, +}; + +use super::*; + +fn get_dummy_space_id( + #[allow(unused_variables)] origin: RawOrigin, +) -> Result { + cfg_if! { + if #[cfg(test)] { + Ok(crate::mock::SPACE1) + } else { + let space_id = pallet_spaces::NextSpaceId::::get(); - pallet_spaces::Pallet::::create_space(origin.into(), Content::None, None)?; + pallet_spaces::Pallet::::create_space(origin.into(), Content::None, None)?; - let space = pallet_spaces::SpaceById::::get(space_id) - .ok_or(DispatchError::Other("Space not found"))?; + let space = pallet_spaces::SpaceById::::get(space_id) + .ok_or(DispatchError::Other("Space not found"))?; - Ok(space) + Ok(space.id) + } + } +} + +fn get_caller_account() -> T::AccountId { + cfg_if! { + if #[cfg(test)] { + let mut bytes: &[u8] = &crate::mock::ACCOUNT1.to_le_bytes(); + T::AccountId::decode(&mut bytes).expect("failed to get caller_account") + } else { + account::("Acc1", 1, 0) + } + } } fn dummy_list_of_users(num_of_users: u32) -> Vec> { @@ -74,25 +95,25 @@ benchmarks! { where_clause { where T: pallet_spaces::Config } create_role { - let caller_origin = RawOrigin::Signed(account::("Acc1", 1, 0)); - let space = create_dummy_space::(caller_origin.clone())?; + let caller_origin = RawOrigin::Signed(get_caller_account::()); + let space_id = get_dummy_space_id::(caller_origin.clone())?; let time_to_live: Option = Some(100u32.into()); let content = valid_content_ipfs(); let perms = vec![SP::ManageRoles]; let role_id = NextRoleId::::get(); - }: _(caller_origin, space.id, time_to_live, content, perms) + }: _(caller_origin, space_id, time_to_live, content, perms) verify { let role = RoleById::::get(role_id).unwrap(); - let space_roles_ids = RoleIdsBySpaceId::::get(space.id); + let space_roles_ids = RoleIdsBySpaceId::::get(space_id); ensure!(role.id == role_id, "Role id doesn't match"); ensure!(space_roles_ids.contains(&role_id), "Role id not in space roles"); } update_role { - let caller_origin = RawOrigin::Signed(account::("Acc1", 1, 0)); - let space = create_dummy_space::(caller_origin.clone())?; - let (role, _) = create_dummy_role::(caller_origin.clone(), space.id, 10)?; + let caller_origin = RawOrigin::Signed(get_caller_account::()); + let space_id = get_dummy_space_id::(caller_origin.clone())?; + let (role, _) = create_dummy_role::(caller_origin.clone(), space_id, 10)?; ensure!(!role.disabled, "Role should be enabled"); @@ -109,9 +130,9 @@ benchmarks! { delete_role { let x in 0..T::MaxUsersToProcessPerDeleteRole::get().into(); - let caller_origin = RawOrigin::Signed(account::("Acc1", 1, 0)); - let space = create_dummy_space::(caller_origin.clone())?; - let (role, _) = create_dummy_role::(caller_origin.clone(), space.id, x)?; + let caller_origin = RawOrigin::Signed(get_caller_account::()); + let space_id = get_dummy_space_id::(caller_origin.clone())?; + let (role, _) = create_dummy_role::(caller_origin.clone(), space_id, x)?; }: _(caller_origin, role.id, x) verify { let deleted = RoleById::::get(role.id).is_none(); @@ -120,9 +141,9 @@ benchmarks! { grant_role { let x in 1..500; - let caller_origin = RawOrigin::Signed(account::("Acc1", 1, 0)); - let space = create_dummy_space::(caller_origin.clone())?; - let (role, _) = create_dummy_role::(caller_origin.clone(), space.id, 0)?; + let caller_origin = RawOrigin::Signed(get_caller_account::()); + let space_id = get_dummy_space_id::(caller_origin.clone())?; + let (role, _) = create_dummy_role::(caller_origin.clone(), space_id, 0)?; let users_to_grant = dummy_list_of_users::(x); }: _(caller_origin, role.id, users_to_grant.clone()) @@ -135,9 +156,9 @@ benchmarks! { revoke_role { let x in 1..500; - let caller_origin = RawOrigin::Signed(account::("Acc1", 1, 0)); - let space = create_dummy_space::(caller_origin.clone())?; - let (role, users_to_revoke) = create_dummy_role::(caller_origin.clone(), space.id, x)?; + let caller_origin = RawOrigin::Signed(get_caller_account::()); + let space_id = get_dummy_space_id::(caller_origin.clone())?; + let (role, users_to_revoke) = create_dummy_role::(caller_origin.clone(), space_id, x)?; }: _(caller_origin, role.id, users_to_revoke) verify { let granted_users = UsersByRoleId::::get(role.id); diff --git a/pallets/roles/src/lib.rs b/pallets/roles/src/lib.rs index ee6f39a2..ea054874 100644 --- a/pallets/roles/src/lib.rs +++ b/pallets/roles/src/lib.rs @@ -6,7 +6,7 @@ //! # Roles Module //! -//! This module allow you to create dynalic roles with an associated set of permissions +//! This module allow you to create dynamic roles with an associated set of permissions //! and grant them to users (accounts or space ids) within a given space. //! //! For example if you want to create a space that enables editors in a similar way to Medium, @@ -17,12 +17,13 @@ #![cfg_attr(not(feature = "std"), no_std)] use codec::{Decode, Encode}; -use frame_support::{dispatch::DispatchResult, ensure, traits::Get}; -use frame_system::{self as system, ensure_signed}; +use frame_support::{dispatch::DispatchResult, ensure}; +use frame_system::{self as system}; use scale_info::TypeInfo; use sp_runtime::RuntimeDebug; use sp_std::{collections::btree_set::BTreeSet, prelude::*}; +pub use pallet::*; use pallet_permissions::{ Pallet as Permissions, PermissionChecker, SpacePermission, SpacePermissionSet, }; @@ -31,12 +32,11 @@ use subsocial_support::{ traits::{IsAccountBlocked, IsContentBlocked, SpaceFollowsProvider, SpacePermissionsProvider}, Content, ModerationError, SpaceId, User, WhoAndWhenOf, }; +pub use types::*; -pub use pallet::*; pub mod functions; pub mod types; -pub use types::*; // pub mod rpc; #[cfg(test)] @@ -44,19 +44,22 @@ mod mock; #[cfg(feature = "runtime-benchmarks")] mod benchmarking; -#[cfg(all(test, not(feature = "runtime-benchmarks")))] +#[cfg(test)] mod tests; pub mod weights; #[frame_support::pallet] pub mod pallet { - use super::*; - use crate::weights::WeightInfo; use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; + use pallet_permissions::SpacePermissionsInfoOf; use subsocial_support::{remove_from_vec, WhoAndWhen}; + use crate::weights::WeightInfo; + + use super::*; + #[pallet::config] pub trait Config: frame_system::Config + pallet_permissions::Config + pallet_timestamp::Config diff --git a/pallets/roles/src/mock.rs b/pallets/roles/src/mock.rs index bf362ba7..745a69a9 100644 --- a/pallets/roles/src/mock.rs +++ b/pallets/roles/src/mock.rs @@ -4,24 +4,24 @@ // Full notice is available at https://github.com/dappforce/subsocial-parachain/blob/main/COPYRIGHT // Full license is available at https://github.com/dappforce/subsocial-parachain/blob/main/LICENSE -use super::*; - -use sp_core::H256; -use sp_io::TestExternalities; -use sp_std::{collections::btree_set::BTreeSet, prelude::Vec}; - use frame_support::{ assert_ok, dispatch::{DispatchError, DispatchResult}, parameter_types, traits::{ConstU32, Everything}, }; +use sp_core::H256; +use sp_io::TestExternalities; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup}, }; +use sp_std::{collections::btree_set::BTreeSet, prelude::Vec}; -use pallet_permissions::{SpacePermission, SpacePermission as SP, SpacePermissions}; +use pallet_permissions::{ + default_permissions::DefaultSpacePermissions, SpacePermission, SpacePermission as SP, + SpacePermissions, +}; use subsocial_support::{ traits::{SpaceFollowsProvider, SpacePermissionsProvider as SpacePermissionsProviderT}, Content, SpaceId, SpacePermissionsInfo, User, @@ -29,6 +29,8 @@ use subsocial_support::{ use crate as roles; +use super::*; + type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; @@ -109,8 +111,6 @@ impl pallet_balances::Config for Test { type ReserveIdentifier = (); } -use pallet_permissions::default_permissions::DefaultSpacePermissions; - impl pallet_permissions::Config for Test { type DefaultSpacePermissions = DefaultSpacePermissions; } @@ -122,9 +122,6 @@ parameter_types! { impl Config for Test { type RuntimeEvent = RuntimeEvent; type MaxUsersToProcessPerDeleteRole = MaxUsersToProcessPerDeleteRole; - #[cfg(feature = "runtime-benchmarks")] - type SpacePermissionsProvider = Spaces; - #[cfg(not(feature = "runtime-benchmarks"))] type SpacePermissionsProvider = Self; type SpaceFollows = Roles; type IsAccountBlocked = ();