Skip to content

Commit

Permalink
Add support for default domain.
Browse files Browse the repository at this point in the history
  • Loading branch information
TarekkMA committed Apr 22, 2022
1 parent d65653d commit 3da6de8
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 27 deletions.
8 changes: 8 additions & 0 deletions pallets/domains/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ benchmarks! {
assert_last_event::<T>(Event::DomainMetaUpdated { who, domain }.into());
}

set_default_domain {
let who = account_with_balance::<T>();
let domain = add_domain::<T>(&who)?;
}: _(RawOrigin::Signed(who.clone()), domain.clone())
verify {
assert_last_event::<T>(Event::DefaultDomainUpdated { who, domain }.into());
}

reserve_words {
let s in 1 .. T::DomainsInsertLimit::get() => ();
let words = mock_bounded_string_array::<T>(s as usize);
Expand Down
40 changes: 39 additions & 1 deletion pallets/domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub mod pallet {
StorageMap<_, Blake2_128Concat, DomainName<T>, DomainMeta<T>>;

/// Domains owned per account.
///
///
/// TWOX-NOTE: Safe as `AccountId`s are crypto hashes anyway.
#[pallet::storage]
#[pallet::getter(fn domains_by_owner)]
Expand All @@ -113,6 +113,18 @@ pub mod pallet {
ValueQuery,
>;

/// The default [DomainName] set for each account.
///
/// TWOX-NOTE: Safe as `AccountId`s are crypto hashes anyway.
#[pallet::storage]
pub(super) type DefaultDomain<T: Config> =
StorageMap<_,
Twox64Concat,
T::AccountId,
DomainName<T>,
OptionQuery,
>;

/// TWOX-NOTE: Safe as `AccountId`s are crypto hashes anyway.
#[pallet::storage]
pub(super) type DomainByInnerValue<T: Config> =
Expand All @@ -136,6 +148,8 @@ pub mod pallet {
DomainRegistered { who: T::AccountId, domain: DomainName<T> },
/// The domain meta was successfully updated.
DomainMetaUpdated { who: T::AccountId, domain: DomainName<T> },
/// The default domain was successfully updated.
DefaultDomainUpdated { who: T::AccountId, domain: DomainName<T> },
/// New words have been reserved.
NewWordsReserved { count: u32 },
/// Added support for new TLDs (top-level domains).
Expand All @@ -144,6 +158,8 @@ pub mod pallet {

#[pallet::error]
pub enum Error<T> {
/// The default domain was not changed.
DefaultDomainNotChanged,
/// The content stored in a domain metadata was not changed.
DomainContentNotChanged,
/// Cannot register more than `MaxDomainsPerAccount` domains.
Expand Down Expand Up @@ -310,6 +326,28 @@ pub mod pallet {
Ok(())
}

/// Sets the default domain of an account.
#[pallet::weight(<T as Config>::WeightInfo::set_default_domain())]
pub fn set_default_domain(
origin: OriginFor<T>,
domain: DomainName<T>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;

let domain_lc = Self::lower_domain_then_bound(&domain);
let meta = Self::require_domain(domain_lc.clone())?;

Self::ensure_allowed_to_update_domain(&meta, &sender)?;

let old_default_domain = <DefaultDomain<T>>::get(&sender);
ensure!(old_default_domain != Some(domain_lc.clone()), Error::<T>::DefaultDomainNotChanged);

<DefaultDomain<T>>::insert(&sender, domain_lc);

Self::deposit_event(Event::DefaultDomainUpdated { who: sender, domain });
Ok(())
}

/// Mark set of domains as not reservable by users.
#[pallet::weight((
<T as Config>::WeightInfo::reserve_words(T::DomainsInsertLimit::get()),
Expand Down
86 changes: 85 additions & 1 deletion pallets/domains/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use sp_std::convert::TryInto;
use pallet_parachain_utils::mock_functions::{another_valid_content_ipfs, invalid_content_ipfs, valid_content_ipfs};
use pallet_parachain_utils::new_who_and_when;

use crate::{DomainByInnerValue, Event, mock::*};
use crate::{DomainByInnerValue, Event, mock::*, Pallet};
use crate::Error;
use crate::Error::DefaultDomainNotChanged;
use crate::types::*;

// `register_domain` tests
Expand Down Expand Up @@ -520,6 +521,89 @@ fn set_domain_content_should_fail_when_content_is_invalid() {
});
}

// `set_default_domain` tests

#[test]
fn set_default_domain_should_fail_when_domain_not_found() {
ExtBuilder::default().build_with_default_domain_registered().execute_with(|| {
assert_noop!(
Pallet::<Test>::set_default_domain(
Origin::signed(DOMAIN_OWNER),
b"test.fail".to_vec().try_into().unwrap(),
),
Error::<Test>::DomainNotFound,
);
});
}

#[test]
fn set_default_domain_should_fail_when_domain_expired() {
ExtBuilder::default().build_with_default_domain_registered().execute_with(|| {
System::set_block_number(ExtBuilder::default().reservation_period_limit + 1);

assert_noop!(
Pallet::<Test>::set_default_domain(
Origin::signed(DOMAIN_OWNER),
default_domain(),
),
Error::<Test>::DomainHasExpired,
);
});
}

#[test]
fn set_default_domain_should_fail_when_not_domain_owner() {
ExtBuilder::default().build_with_default_domain_registered().execute_with(|| {

assert_noop!(
Pallet::<Test>::set_default_domain(
Origin::signed(DUMMY_ACCOUNT),
default_domain(),
),
Error::<Test>::NotDomainOwner,
);
});
}

#[test]
fn set_default_domain_should_work() {
ExtBuilder::default().build_with_default_domain_registered().execute_with(|| {

assert_ok!(
Pallet::<Test>::set_default_domain(
Origin::signed(DOMAIN_OWNER),
default_domain(),
),
);

System::assert_last_event(Event::<Test>::DefaultDomainUpdated {
who: DOMAIN_OWNER,
domain: default_domain(),
}.into());
});
}

#[test]
fn set_default_domain_should_fail_when_setting_the_same_value() {
ExtBuilder::default().build_with_default_domain_registered().execute_with(|| {

assert_ok!(
Pallet::<Test>::set_default_domain(
Origin::signed(DOMAIN_OWNER),
default_domain(),
),
);

assert_noop!(
Pallet::<Test>::set_default_domain(
Origin::signed(DOMAIN_OWNER),
default_domain(),
),
Error::<Test>::DefaultDomainNotChanged,
);
});
}

// `reserve_domains` tests

#[test]
Expand Down
65 changes: 40 additions & 25 deletions pallets/domains/src/weights.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Autogenerated weights for pallet_domains
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2022-04-12, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2022-04-22, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
// Executed Command:
Expand Down Expand Up @@ -43,6 +43,7 @@ pub trait WeightInfo {
fn force_set_inner_value() -> Weight;
fn set_outer_value() -> Weight;
fn set_domain_content() -> Weight;
fn set_default_domain() -> Weight;
fn reserve_words(s: u32, ) -> Weight;
fn support_tlds(s: u32, ) -> Weight;
}
Expand All @@ -56,7 +57,7 @@ pub struct SubstrateWeight<T>(PhantomData<T>);
// Storage: Domains RegisteredDomains (r:1 w:1)
// Storage: Timestamp Now (r:1 w:0)
fn register_domain() -> Weight {
(45_091_000 as Weight)
(67_000_000 as Weight)
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
Expand All @@ -65,49 +66,56 @@ pub struct SubstrateWeight<T>(PhantomData<T>);
// Storage: Domains RegisteredDomains (r:1 w:1)
// Storage: Timestamp Now (r:1 w:0)
fn force_register_domain() -> Weight {
(30_322_000 as Weight)
(44_000_000 as Weight)
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: Domains RegisteredDomains (r:1 w:1)
// Storage: Domains DomainByInnerValue (r:0 w:2)
fn set_inner_value() -> Weight {
(26_162_000 as Weight)
(38_000_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
// Storage: Domains RegisteredDomains (r:1 w:1)
// Storage: Domains DomainByInnerValue (r:0 w:2)
fn force_set_inner_value() -> Weight {
(25_274_000 as Weight)
(37_000_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
// Storage: Domains RegisteredDomains (r:1 w:1)
fn set_outer_value() -> Weight {
(35_425_000 as Weight)
(51_000_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Domains RegisteredDomains (r:1 w:1)
fn set_domain_content() -> Weight {
(21_323_000 as Weight)
(31_000_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Domains RegisteredDomains (r:1 w:0)
// Storage: Domains DefaultDomain (r:1 w:1)
fn set_default_domain() -> Weight {
(34_000_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Domains ReservedWords (r:0 w:1)
fn reserve_words(s: u32, ) -> Weight {
(56_416_000 as Weight)
// Standard Error: 2_000
.saturating_add((1_702_000 as Weight).saturating_mul(s as Weight))
(0 as Weight)
// Standard Error: 16_000
.saturating_add((2_488_000 as Weight).saturating_mul(s as Weight))
.saturating_add(T::DbWeight::get().writes(311 as Weight))
.saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight)))
}
// Storage: Domains SupportedTlds (r:0 w:1)
fn support_tlds(s: u32, ) -> Weight {
(53_216_000 as Weight)
// Standard Error: 3_000
.saturating_add((1_685_000 as Weight).saturating_mul(s as Weight))
(59_257_000 as Weight)
// Standard Error: 9_000
.saturating_add((2_269_000 as Weight).saturating_mul(s as Weight))
.saturating_add(T::DbWeight::get().writes(311 as Weight))
.saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight)))
}
Expand All @@ -121,7 +129,7 @@ pub struct SubstrateWeight<T>(PhantomData<T>);
// Storage: Domains RegisteredDomains (r:1 w:1)
// Storage: Timestamp Now (r:1 w:0)
fn register_domain() -> Weight {
(45_091_000 as Weight)
(67_000_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(5 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
Expand All @@ -130,49 +138,56 @@ pub struct SubstrateWeight<T>(PhantomData<T>);
// Storage: Domains RegisteredDomains (r:1 w:1)
// Storage: Timestamp Now (r:1 w:0)
fn force_register_domain() -> Weight {
(30_322_000 as Weight)
(44_000_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(4 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
// Storage: Domains RegisteredDomains (r:1 w:1)
// Storage: Domains DomainByInnerValue (r:0 w:2)
fn set_inner_value() -> Weight {
(26_162_000 as Weight)
(38_000_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
// Storage: Domains RegisteredDomains (r:1 w:1)
// Storage: Domains DomainByInnerValue (r:0 w:2)
fn force_set_inner_value() -> Weight {
(25_274_000 as Weight)
(37_000_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
// Storage: Domains RegisteredDomains (r:1 w:1)
fn set_outer_value() -> Weight {
(35_425_000 as Weight)
(51_000_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Domains RegisteredDomains (r:1 w:1)
fn set_domain_content() -> Weight {
(21_323_000 as Weight)
(31_000_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Domains RegisteredDomains (r:1 w:0)
// Storage: Domains DefaultDomain (r:1 w:1)
fn set_default_domain() -> Weight {
(34_000_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Domains ReservedWords (r:0 w:1)
fn reserve_words(s: u32, ) -> Weight {
(56_416_000 as Weight)
// Standard Error: 2_000
.saturating_add((1_702_000 as Weight).saturating_mul(s as Weight))
(0 as Weight)
// Standard Error: 16_000
.saturating_add((2_488_000 as Weight).saturating_mul(s as Weight))
.saturating_add(RocksDbWeight::get().writes(311 as Weight))
.saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(s as Weight)))
}
// Storage: Domains SupportedTlds (r:0 w:1)
fn support_tlds(s: u32, ) -> Weight {
(53_216_000 as Weight)
// Standard Error: 3_000
.saturating_add((1_685_000 as Weight).saturating_mul(s as Weight))
(59_257_000 as Weight)
// Standard Error: 9_000
.saturating_add((2_269_000 as Weight).saturating_mul(s as Weight))
.saturating_add(RocksDbWeight::get().writes(311 as Weight))
.saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(s as Weight)))
}
Expand Down

0 comments on commit 3da6de8

Please sign in to comment.