Skip to content

Commit

Permalink
Apply final suggestions after code review
Browse files Browse the repository at this point in the history
Co-Authored-By: Alex Siman <siman@users.noreply.github.com>
  • Loading branch information
F3Joule and siman committed Mar 27, 2024
1 parent 2cb2a22 commit 6166d7f
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pallets/creator-staking/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ mock! {
impl SpacesProvider<AccountId, SpaceId> for Spaces {
fn get_space_owner(_space_id: SpaceId) -> Result<AccountId, DispatchError>;

fn update_space_owner(_space_id: SpaceId, _new_owner: AccountId) -> DispatchResult;
fn do_update_space_owner(_space_id: SpaceId, _new_owner: AccountId) -> DispatchResult;

fn create_space(_owner: &AccountId, _content: Content) -> Result<SpaceId, DispatchError>;
}
Expand Down
24 changes: 20 additions & 4 deletions pallets/domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,15 @@ pub mod pallet {
);
Ok(())
}

/// Throws an error if domain length exceeds the `DomainName` BoundedVec size limit.
fn ensure_valid_domain_length(domain: &[u8]) -> DispatchResult {
ensure!(
domain.len() <= T::MaxDomainLength::get() as usize,
Error::<T>::DomainIsTooLong,
);
Ok(())
}

/// Throws an error if domain contains invalid character.
fn ensure_domain_contains_valid_chars(domain: &[u8]) -> DispatchResult {
Expand Down Expand Up @@ -642,7 +651,10 @@ pub mod pallet {
/// Try to get domain meta by its custom and top-level domain names.
/// This function pre-validates the passed argument.
pub fn require_domain_by_ref(domain: &[u8]) -> Result<DomainMeta<T>, DispatchError> {
ensure!(domain.len() <= T::MaxDomainLength::get() as usize, Error::<T>::DomainIsTooLong);
// Because of BoundedVec implementation speicifics, we need to
// check that the domain length does not exceed the BoundedVec size
// before converting from &[u8] to DomainName<T> in `lower_domain_then_bound`,
Self::ensure_valid_domain_length(domain)?;
let domain_lc = Self::lower_domain_then_bound(domain);

Self::require_domain(domain_lc)
Expand Down Expand Up @@ -734,7 +746,7 @@ pub mod pallet {
}

impl<T: Config> DomainsProvider<T::AccountId> for Pallet<T> {
type DomainLength = T::MaxDomainLength;
type MaxDomainLength = T::MaxDomainLength;

fn get_domain_owner(domain: &[u8]) -> Result<T::AccountId, DispatchError> {
let meta = Self::require_domain_by_ref(domain)?;
Expand All @@ -747,8 +759,12 @@ pub mod pallet {
Ok(())
}

fn update_domain_owner(domain: &[u8], new_owner: &T::AccountId) -> DispatchResult {
fn do_update_domain_owner(domain: &[u8], new_owner: &T::AccountId) -> DispatchResult {
let meta = Self::require_domain_by_ref(domain)?;
if meta.is_owner(new_owner) {
return Ok(());
}

let domain_lc = Self::lower_domain_then_bound(domain);

Self::ensure_domains_limit_not_reached(&new_owner)?;
Expand Down Expand Up @@ -778,7 +794,7 @@ pub mod pallet {

#[cfg(feature = "runtime-benchmarks")]
fn register_domain(owner: &T::AccountId, domain: &[u8]) -> Result<Vec<u8>, DispatchError> {
ensure!(domain.len() <= T::MaxDomainLength::get() as usize, Error::<T>::DomainIsTooLong);
Self::ensure_valid_domain_length(domain)?;
let domain_lc = Self::lower_domain_then_bound(domain);

Self::do_register_domain(
Expand Down
7 changes: 6 additions & 1 deletion pallets/posts/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,12 @@ impl<T: Config> PostsProvider<T::AccountId> for Pallet<T> {
Ok(())
}

fn update_post_owner(post_id: PostId, new_owner: &T::AccountId) -> DispatchResult {
fn do_update_post_owner(post_id: PostId, new_owner: &T::AccountId) -> DispatchResult {
let post = Self::require_post(post_id)?;
if post.is_owner(new_owner) {
return Ok(())
}

PostById::<T>::mutate(post_id, |stored_post_opt| {
if let Some(stored_post) = stored_post_opt {
stored_post.owner = new_owner.clone();
Expand Down
4 changes: 2 additions & 2 deletions pallets/posts/tests/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ parameter_types! {
}
pub struct MockEmptyDomainsProvider;
impl DomainsProvider<AccountId> for MockEmptyDomainsProvider {
type DomainLength = MaxDomainLength;
type MaxDomainLength = MaxDomainLength;

fn get_domain_owner(_domain: &[u8]) -> Result<AccountId, DispatchError> {
Ok(ACCOUNT1)
Expand All @@ -164,7 +164,7 @@ impl DomainsProvider<AccountId> for MockEmptyDomainsProvider {
Ok(())
}

fn update_domain_owner(_domain: &[u8], _new_owner: &AccountId) -> DispatchResult {
fn do_update_domain_owner(_domain: &[u8], _new_owner: &AccountId) -> DispatchResult {
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion pallets/profiles/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mock! {
impl SpacesProvider<AccountId, SpaceId> for Spaces {
fn get_space_owner(_space_id: SpaceId) -> Result<AccountId, DispatchError>;

fn update_space_owner(_space_id: SpaceId, _new_owner: AccountId) -> DispatchResult;
fn do_update_space_owner(_space_id: SpaceId, _new_owner: AccountId) -> DispatchResult;

fn create_space(_owner: &AccountId, _content: Content) -> Result<SpaceId, DispatchError>;
}
Expand Down
12 changes: 6 additions & 6 deletions pallets/space-ownership/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub mod pallet {
use subsocial_support::{PostId, SpaceId, SpacePermissionsInfo, traits::{CreatorStakingProvider, DomainsProvider, ProfileManager, SpacesProvider, PostsProvider, SpacePermissionsProvider}};

pub(crate) type DomainLengthOf<T> =
<<T as Config>::DomainsProvider as DomainsProvider<<T as frame_system::Config>::AccountId>>::DomainLength;
<<T as Config>::DomainsProvider as DomainsProvider<<T as frame_system::Config>::AccountId>>::MaxDomainLength;

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
Expand Down Expand Up @@ -66,7 +66,7 @@ pub mod pallet {
}

/// The current storage version
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
Expand Down Expand Up @@ -170,14 +170,14 @@ pub mod pallet {
let previous_owner = T::SpacesProvider::get_space_owner(space_id)?;

Self::ensure_not_active_creator(space_id)?;
T::SpacesProvider::update_space_owner(space_id, pending_owner.clone())?;

T::SpacesProvider::do_update_space_owner(space_id, pending_owner.clone())?;
T::ProfileManager::unlink_space_from_profile(&previous_owner, space_id);
}
OwnableEntity::Post(post_id) =>
T::PostsProvider::update_post_owner(post_id, &pending_owner)?,
T::PostsProvider::do_update_post_owner(post_id, &pending_owner)?,
OwnableEntity::Domain(domain) =>
T::DomainsProvider::update_domain_owner(&domain, &pending_owner)?,
T::DomainsProvider::do_update_domain_owner(&domain, &pending_owner)?,
}

PendingOwnershipTransfers::<T>::remove(&entity);
Expand Down
8 changes: 6 additions & 2 deletions pallets/spaces/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,13 @@ pub mod pallet {
Ok(space.owner)
}

fn update_space_owner(space_id: SpaceId, new_owner: T::AccountId) -> DispatchResult {
Self::ensure_space_limit_not_reached(&new_owner)?;
fn do_update_space_owner(space_id: SpaceId, new_owner: T::AccountId) -> DispatchResult {
let space = Pallet::<T>::require_space(space_id)?;
if space.is_owner(&new_owner) {
return Ok(());
}

Self::ensure_space_limit_not_reached(&new_owner)?;

ensure!(
T::IsAccountBlocked::is_allowed_account(new_owner.clone(), space_id),
Expand Down
8 changes: 4 additions & 4 deletions pallets/support/src/traits/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub trait SpacesProvider<AccountId, SpaceId> {

fn get_space_owner(space_id: SpaceId) -> Result<AccountId, DispatchError>;

fn update_space_owner(space_id: SpaceId, new_owner: AccountId) -> DispatchResult;
fn do_update_space_owner(space_id: SpaceId, new_owner: AccountId) -> DispatchResult;

fn create_space(owner: &AccountId, content: Content) -> Result<SpaceId, DispatchError>;
}
Expand All @@ -56,13 +56,13 @@ impl<AccountId> CreatorStakingProvider<AccountId> for () {
}

pub trait DomainsProvider<AccountId> {
type DomainLength: frame_support::traits::Get<u32>;
type MaxDomainLength: frame_support::traits::Get<u32>;

fn get_domain_owner(domain: &[u8]) -> Result<AccountId, DispatchError>;

fn ensure_domain_owner(domain: &[u8], account: &AccountId) -> DispatchResult;

fn update_domain_owner(domain: &[u8], new_owner: &AccountId) -> DispatchResult;
fn do_update_domain_owner(domain: &[u8], new_owner: &AccountId) -> DispatchResult;

#[cfg(feature = "runtime-benchmarks")]
fn register_domain(owner: &AccountId, domain: &[u8]) -> Result<sp_std::vec::Vec<u8>, DispatchError>;
Expand All @@ -73,7 +73,7 @@ pub trait PostsProvider<AccountId> {

fn ensure_post_owner(post_id: PostId, account: &AccountId) -> DispatchResult;

fn update_post_owner(post_id: PostId, new_owner: &AccountId) -> DispatchResult;
fn do_update_post_owner(post_id: PostId, new_owner: &AccountId) -> DispatchResult;

#[cfg(feature = "runtime-benchmarks")]
fn create_post(owner: &AccountId, space_id: SpaceId, content: Content) -> Result<PostId, DispatchError>;
Expand Down

0 comments on commit 6166d7f

Please sign in to comment.