From d8269fbb72a30a3bd8c31d3888abf3a00fad5349 Mon Sep 17 00:00:00 2001 From: Vlad Proshchavaiev <32250097+F3Joule@users.noreply.github.com> Date: Tue, 17 Oct 2023 16:34:16 +0300 Subject: [PATCH] Refactor transfer space ownership restriction --- integration-tests/src/mock.rs | 2 +- pallets/creator-staking/src/functions.rs | 19 ++++++----------- pallets/posts/tests/src/mock.rs | 2 +- pallets/space-ownership/src/lib.rs | 26 +++++++++++------------ pallets/space-ownership/tests/src/mock.rs | 2 +- pallets/support/src/traits.rs | 2 +- pallets/support/src/traits/common.rs | 22 ++++++++----------- runtime/src/lib.rs | 2 +- 8 files changed, 32 insertions(+), 45 deletions(-) diff --git a/integration-tests/src/mock.rs b/integration-tests/src/mock.rs index a64b494e..f957ec8a 100644 --- a/integration-tests/src/mock.rs +++ b/integration-tests/src/mock.rs @@ -157,7 +157,7 @@ impl pallet_space_follows::Config for TestRuntime { impl pallet_space_ownership::Config for TestRuntime { type RuntimeEvent = RuntimeEvent; type ProfileManager = Profiles; - type OwnershipTransferValidator = (); + type CreatorStakingProvider = (); type WeightInfo = (); } diff --git a/pallets/creator-staking/src/functions.rs b/pallets/creator-staking/src/functions.rs index 38dbdb52..74fe1a26 100644 --- a/pallets/creator-staking/src/functions.rs +++ b/pallets/creator-staking/src/functions.rs @@ -2,8 +2,7 @@ use crate::pallet::*; use frame_support::{pallet_prelude::*, traits::{Currency, ReservableCurrency, LockableCurrency, WithdrawReasons}}; use sp_runtime::{traits::{AccountIdConversion, Zero}, Perbill, Saturating}; use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; -use subsocial_support::SpaceId; -use subsocial_support::traits::{OwnershipTransferValidator, SpacePermissionsProvider}; +use subsocial_support::traits::{CreatorStakingProvider, SpacePermissionsProvider}; impl Pallet { /// `Err` if pallet disabled for maintenance, `Ok` otherwise @@ -484,16 +483,10 @@ impl Pallet { /// Implementation of `OwnershipTransferValidator` for `creator-staking` pallet. /// This will be used in space-ownership pallet to forbid ownership transfer for spaces, which /// are registered as creators. -impl OwnershipTransferValidator for Pallet { - fn ensure_can_transfer_ownership( - _current_owner: &T::AccountId, - _new_owner: &T::AccountId, - space_id: SpaceId, - ) -> Result<(), &'static str> { - match Self::require_creator(space_id) { - Ok(_) if Self::is_creator_active(space_id) => - Err("Cannot transfer space ownership of an active creator"), - _ => Ok(()) - } +impl CreatorStakingProvider for Pallet { + fn is_creator_active( + creator_id: CreatorId, + ) -> bool { + Self::is_creator_active(creator_id) } } diff --git a/pallets/posts/tests/src/mock.rs b/pallets/posts/tests/src/mock.rs index aa631744..d1be5ba6 100644 --- a/pallets/posts/tests/src/mock.rs +++ b/pallets/posts/tests/src/mock.rs @@ -147,6 +147,6 @@ impl pallet_space_follows::Config for Test { impl pallet_space_ownership::Config for Test { type RuntimeEvent = RuntimeEvent; type ProfileManager = Profiles; - type OwnershipTransferValidator = (); + type CreatorStakingProvider = (); type WeightInfo = (); } diff --git a/pallets/space-ownership/src/lib.rs b/pallets/space-ownership/src/lib.rs index acbc2b7a..e3075380 100644 --- a/pallets/space-ownership/src/lib.rs +++ b/pallets/space-ownership/src/lib.rs @@ -21,7 +21,7 @@ pub mod pallet { use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; - use subsocial_support::traits::{OwnershipTransferValidator, ProfileManager}; + use subsocial_support::traits::{CreatorStakingProvider, ProfileManager}; #[pallet::config] pub trait Config: frame_system::Config + pallet_spaces::Config { @@ -29,7 +29,7 @@ pub mod pallet { type ProfileManager: ProfileManager; - type OwnershipTransferValidator: OwnershipTransferValidator; + type CreatorStakingProvider: CreatorStakingProvider; type WeightInfo: WeightInfo; } @@ -44,8 +44,8 @@ pub mod pallet { CannotTransferToCurrentOwner, /// Account is already an owner of a space. AlreadyASpaceOwner, - /// Account cannot transfer space ownership because of external reasons. - CannotTransferSpaceOwnership, + /// Cannot transfer ownership, because a space is registered as an active creator. + ActiveCreatorCannotTransferOwnership, /// There is no pending ownership transfer for a given space. NoPendingTransferOnSpace, /// Account is not allowed to accept ownership transfer. @@ -96,11 +96,10 @@ pub mod pallet { ModerationError::AccountIsBlocked ); - T::OwnershipTransferValidator::ensure_can_transfer_ownership( - &who, - &transfer_to, - space_id, - ).map_err(|_| Error::::CannotTransferSpaceOwnership)?; + ensure!( + !T::CreatorStakingProvider::is_creator_active(space_id), + Error::::ActiveCreatorCannotTransferOwnership, + ); PendingSpaceOwner::::insert(space_id, transfer_to.clone()); @@ -125,11 +124,10 @@ pub mod pallet { ensure!(new_owner == transfer_to, Error::::NotAllowedToAcceptOwnershipTransfer); - T::OwnershipTransferValidator::ensure_can_transfer_ownership( - &space.owner, - &transfer_to, - space_id, - ).map_err(|_| Error::::NotAllowedToAcceptOwnershipTransfer)?; + ensure!( + !T::CreatorStakingProvider::is_creator_active(space_id), + Error::::ActiveCreatorCannotTransferOwnership, + ); Spaces::::ensure_space_limit_not_reached(&transfer_to)?; diff --git a/pallets/space-ownership/tests/src/mock.rs b/pallets/space-ownership/tests/src/mock.rs index 599533b8..9ff5a67a 100644 --- a/pallets/space-ownership/tests/src/mock.rs +++ b/pallets/space-ownership/tests/src/mock.rs @@ -133,6 +133,6 @@ impl pallet_space_follows::Config for Test { impl pallet_space_ownership::Config for Test { type RuntimeEvent = RuntimeEvent; type ProfileManager = Profiles; - type OwnershipTransferValidator = (); + type CreatorStakingProvider = (); type WeightInfo = (); } diff --git a/pallets/support/src/traits.rs b/pallets/support/src/traits.rs index 0b0238f4..30803275 100644 --- a/pallets/support/src/traits.rs +++ b/pallets/support/src/traits.rs @@ -1,5 +1,5 @@ pub use common::{ - OwnershipTransferValidator, ProfileManager, SpaceFollowsProvider, SpacePermissionsProvider, + CreatorStakingProvider, ProfileManager, SpaceFollowsProvider, SpacePermissionsProvider, SpacesInterface, PostFollowsProvider, }; pub use moderation::{IsAccountBlocked, IsContentBlocked, IsPostBlocked, IsSpaceBlocked}; diff --git a/pallets/support/src/traits/common.rs b/pallets/support/src/traits/common.rs index 89e42aab..d3f64167 100644 --- a/pallets/support/src/traits/common.rs +++ b/pallets/support/src/traits/common.rs @@ -32,20 +32,16 @@ pub trait SpacesInterface { fn create_space(owner: &AccountId, content: Content) -> Result; } -pub trait OwnershipTransferValidator { - fn ensure_can_transfer_ownership( - current_owner: &AccountId, - new_owner: &AccountId, - space_id: SpaceId, - ) -> Result<(), &'static str>; +pub trait CreatorStakingProvider { + fn is_creator_active( + creator_id: SpaceId, + ) -> bool; } -impl OwnershipTransferValidator for () { - fn ensure_can_transfer_ownership( - _current_owner: &AccountId, - _new_owner: &AccountId, - _space_id: SpaceId, - ) -> Result<(), &'static str> { - Ok(()) +impl CreatorStakingProvider for () { + fn is_creator_active( + _creator_id: SpaceId, + ) -> bool { + true } } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 000e6f32..4008d32b 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -707,7 +707,7 @@ impl pallet_spaces::Config for Runtime { impl pallet_space_ownership::Config for Runtime { type RuntimeEvent = RuntimeEvent; type ProfileManager = Profiles; - type OwnershipTransferValidator = CreatorStaking; + type CreatorStakingProvider = CreatorStaking; type WeightInfo = pallet_space_ownership::weights::SubstrateWeight; }