Skip to content

Commit

Permalink
Refactor transfer space ownership restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
F3Joule committed Oct 17, 2023
1 parent 21d7359 commit d8269fb
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 45 deletions.
2 changes: 1 addition & 1 deletion integration-tests/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
}

Expand Down
19 changes: 6 additions & 13 deletions pallets/creator-staking/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Config> Pallet<T> {
/// `Err` if pallet disabled for maintenance, `Ok` otherwise
Expand Down Expand Up @@ -484,16 +483,10 @@ impl<T: Config> Pallet<T> {
/// 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<T: Config> OwnershipTransferValidator<T::AccountId> for Pallet<T> {
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<T: Config> CreatorStakingProvider<T::AccountId> for Pallet<T> {
fn is_creator_active(
creator_id: CreatorId,
) -> bool {
Self::is_creator_active(creator_id)
}
}
2 changes: 1 addition & 1 deletion pallets/posts/tests/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
}
26 changes: 12 additions & 14 deletions pallets/space-ownership/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ 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 {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

type ProfileManager: ProfileManager<Self::AccountId>;

type OwnershipTransferValidator: OwnershipTransferValidator<Self::AccountId>;
type CreatorStakingProvider: CreatorStakingProvider<Self::AccountId>;

type WeightInfo: WeightInfo;
}
Expand All @@ -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.
Expand Down Expand Up @@ -96,11 +96,10 @@ pub mod pallet {
ModerationError::AccountIsBlocked
);

T::OwnershipTransferValidator::ensure_can_transfer_ownership(
&who,
&transfer_to,
space_id,
).map_err(|_| Error::<T>::CannotTransferSpaceOwnership)?;
ensure!(
!T::CreatorStakingProvider::is_creator_active(space_id),
Error::<T>::ActiveCreatorCannotTransferOwnership,
);

PendingSpaceOwner::<T>::insert(space_id, transfer_to.clone());

Expand All @@ -125,11 +124,10 @@ pub mod pallet {

ensure!(new_owner == transfer_to, Error::<T>::NotAllowedToAcceptOwnershipTransfer);

T::OwnershipTransferValidator::ensure_can_transfer_ownership(
&space.owner,
&transfer_to,
space_id,
).map_err(|_| Error::<T>::NotAllowedToAcceptOwnershipTransfer)?;
ensure!(
!T::CreatorStakingProvider::is_creator_active(space_id),
Error::<T>::ActiveCreatorCannotTransferOwnership,
);

Spaces::<T>::ensure_space_limit_not_reached(&transfer_to)?;

Expand Down
2 changes: 1 addition & 1 deletion pallets/space-ownership/tests/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
}
2 changes: 1 addition & 1 deletion pallets/support/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub use common::{
OwnershipTransferValidator, ProfileManager, SpaceFollowsProvider, SpacePermissionsProvider,
CreatorStakingProvider, ProfileManager, SpaceFollowsProvider, SpacePermissionsProvider,
SpacesInterface, PostFollowsProvider,
};
pub use moderation::{IsAccountBlocked, IsContentBlocked, IsPostBlocked, IsSpaceBlocked};
Expand Down
22 changes: 9 additions & 13 deletions pallets/support/src/traits/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,16 @@ pub trait SpacesInterface<AccountId, SpaceId> {
fn create_space(owner: &AccountId, content: Content) -> Result<SpaceId, DispatchError>;
}

pub trait OwnershipTransferValidator<AccountId> {
fn ensure_can_transfer_ownership(
current_owner: &AccountId,
new_owner: &AccountId,
space_id: SpaceId,
) -> Result<(), &'static str>;
pub trait CreatorStakingProvider<AccountId> {
fn is_creator_active(
creator_id: SpaceId,
) -> bool;
}

impl<AccountId> OwnershipTransferValidator<AccountId> for () {
fn ensure_can_transfer_ownership(
_current_owner: &AccountId,
_new_owner: &AccountId,
_space_id: SpaceId,
) -> Result<(), &'static str> {
Ok(())
impl<AccountId> CreatorStakingProvider<AccountId> for () {
fn is_creator_active(
_creator_id: SpaceId,
) -> bool {
true
}
}
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runtime>;
}

Expand Down

0 comments on commit d8269fb

Please sign in to comment.