Skip to content

Commit 3da6de8

Browse files
committed
Add support for default domain.
1 parent d65653d commit 3da6de8

File tree

4 files changed

+172
-27
lines changed

4 files changed

+172
-27
lines changed

pallets/domains/src/benchmarking.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ benchmarks! {
198198
assert_last_event::<T>(Event::DomainMetaUpdated { who, domain }.into());
199199
}
200200

201+
set_default_domain {
202+
let who = account_with_balance::<T>();
203+
let domain = add_domain::<T>(&who)?;
204+
}: _(RawOrigin::Signed(who.clone()), domain.clone())
205+
verify {
206+
assert_last_event::<T>(Event::DefaultDomainUpdated { who, domain }.into());
207+
}
208+
201209
reserve_words {
202210
let s in 1 .. T::DomainsInsertLimit::get() => ();
203211
let words = mock_bounded_string_array::<T>(s as usize);

pallets/domains/src/lib.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub mod pallet {
101101
StorageMap<_, Blake2_128Concat, DomainName<T>, DomainMeta<T>>;
102102

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

116+
/// The default [DomainName] set for each account.
117+
///
118+
/// TWOX-NOTE: Safe as `AccountId`s are crypto hashes anyway.
119+
#[pallet::storage]
120+
pub(super) type DefaultDomain<T: Config> =
121+
StorageMap<_,
122+
Twox64Concat,
123+
T::AccountId,
124+
DomainName<T>,
125+
OptionQuery,
126+
>;
127+
116128
/// TWOX-NOTE: Safe as `AccountId`s are crypto hashes anyway.
117129
#[pallet::storage]
118130
pub(super) type DomainByInnerValue<T: Config> =
@@ -136,6 +148,8 @@ pub mod pallet {
136148
DomainRegistered { who: T::AccountId, domain: DomainName<T> },
137149
/// The domain meta was successfully updated.
138150
DomainMetaUpdated { who: T::AccountId, domain: DomainName<T> },
151+
/// The default domain was successfully updated.
152+
DefaultDomainUpdated { who: T::AccountId, domain: DomainName<T> },
139153
/// New words have been reserved.
140154
NewWordsReserved { count: u32 },
141155
/// Added support for new TLDs (top-level domains).
@@ -144,6 +158,8 @@ pub mod pallet {
144158

145159
#[pallet::error]
146160
pub enum Error<T> {
161+
/// The default domain was not changed.
162+
DefaultDomainNotChanged,
147163
/// The content stored in a domain metadata was not changed.
148164
DomainContentNotChanged,
149165
/// Cannot register more than `MaxDomainsPerAccount` domains.
@@ -310,6 +326,28 @@ pub mod pallet {
310326
Ok(())
311327
}
312328

329+
/// Sets the default domain of an account.
330+
#[pallet::weight(<T as Config>::WeightInfo::set_default_domain())]
331+
pub fn set_default_domain(
332+
origin: OriginFor<T>,
333+
domain: DomainName<T>,
334+
) -> DispatchResult {
335+
let sender = ensure_signed(origin)?;
336+
337+
let domain_lc = Self::lower_domain_then_bound(&domain);
338+
let meta = Self::require_domain(domain_lc.clone())?;
339+
340+
Self::ensure_allowed_to_update_domain(&meta, &sender)?;
341+
342+
let old_default_domain = <DefaultDomain<T>>::get(&sender);
343+
ensure!(old_default_domain != Some(domain_lc.clone()), Error::<T>::DefaultDomainNotChanged);
344+
345+
<DefaultDomain<T>>::insert(&sender, domain_lc);
346+
347+
Self::deposit_event(Event::DefaultDomainUpdated { who: sender, domain });
348+
Ok(())
349+
}
350+
313351
/// Mark set of domains as not reservable by users.
314352
#[pallet::weight((
315353
<T as Config>::WeightInfo::reserve_words(T::DomainsInsertLimit::get()),

pallets/domains/src/tests.rs

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use sp_std::convert::TryInto;
55
use pallet_parachain_utils::mock_functions::{another_valid_content_ipfs, invalid_content_ipfs, valid_content_ipfs};
66
use pallet_parachain_utils::new_who_and_when;
77

8-
use crate::{DomainByInnerValue, Event, mock::*};
8+
use crate::{DomainByInnerValue, Event, mock::*, Pallet};
99
use crate::Error;
10+
use crate::Error::DefaultDomainNotChanged;
1011
use crate::types::*;
1112

1213
// `register_domain` tests
@@ -520,6 +521,89 @@ fn set_domain_content_should_fail_when_content_is_invalid() {
520521
});
521522
}
522523

524+
// `set_default_domain` tests
525+
526+
#[test]
527+
fn set_default_domain_should_fail_when_domain_not_found() {
528+
ExtBuilder::default().build_with_default_domain_registered().execute_with(|| {
529+
assert_noop!(
530+
Pallet::<Test>::set_default_domain(
531+
Origin::signed(DOMAIN_OWNER),
532+
b"test.fail".to_vec().try_into().unwrap(),
533+
),
534+
Error::<Test>::DomainNotFound,
535+
);
536+
});
537+
}
538+
539+
#[test]
540+
fn set_default_domain_should_fail_when_domain_expired() {
541+
ExtBuilder::default().build_with_default_domain_registered().execute_with(|| {
542+
System::set_block_number(ExtBuilder::default().reservation_period_limit + 1);
543+
544+
assert_noop!(
545+
Pallet::<Test>::set_default_domain(
546+
Origin::signed(DOMAIN_OWNER),
547+
default_domain(),
548+
),
549+
Error::<Test>::DomainHasExpired,
550+
);
551+
});
552+
}
553+
554+
#[test]
555+
fn set_default_domain_should_fail_when_not_domain_owner() {
556+
ExtBuilder::default().build_with_default_domain_registered().execute_with(|| {
557+
558+
assert_noop!(
559+
Pallet::<Test>::set_default_domain(
560+
Origin::signed(DUMMY_ACCOUNT),
561+
default_domain(),
562+
),
563+
Error::<Test>::NotDomainOwner,
564+
);
565+
});
566+
}
567+
568+
#[test]
569+
fn set_default_domain_should_work() {
570+
ExtBuilder::default().build_with_default_domain_registered().execute_with(|| {
571+
572+
assert_ok!(
573+
Pallet::<Test>::set_default_domain(
574+
Origin::signed(DOMAIN_OWNER),
575+
default_domain(),
576+
),
577+
);
578+
579+
System::assert_last_event(Event::<Test>::DefaultDomainUpdated {
580+
who: DOMAIN_OWNER,
581+
domain: default_domain(),
582+
}.into());
583+
});
584+
}
585+
586+
#[test]
587+
fn set_default_domain_should_fail_when_setting_the_same_value() {
588+
ExtBuilder::default().build_with_default_domain_registered().execute_with(|| {
589+
590+
assert_ok!(
591+
Pallet::<Test>::set_default_domain(
592+
Origin::signed(DOMAIN_OWNER),
593+
default_domain(),
594+
),
595+
);
596+
597+
assert_noop!(
598+
Pallet::<Test>::set_default_domain(
599+
Origin::signed(DOMAIN_OWNER),
600+
default_domain(),
601+
),
602+
Error::<Test>::DefaultDomainNotChanged,
603+
);
604+
});
605+
}
606+
523607
// `reserve_domains` tests
524608

525609
#[test]

pallets/domains/src/weights.rs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Autogenerated weights for pallet_domains
22
//!
33
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
4-
//! DATE: 2022-04-12, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
4+
//! DATE: 2022-04-22, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
55
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
66
77
// Executed Command:
@@ -43,6 +43,7 @@ pub trait WeightInfo {
4343
fn force_set_inner_value() -> Weight;
4444
fn set_outer_value() -> Weight;
4545
fn set_domain_content() -> Weight;
46+
fn set_default_domain() -> Weight;
4647
fn reserve_words(s: u32, ) -> Weight;
4748
fn support_tlds(s: u32, ) -> Weight;
4849
}
@@ -56,7 +57,7 @@ pub struct SubstrateWeight<T>(PhantomData<T>);
5657
// Storage: Domains RegisteredDomains (r:1 w:1)
5758
// Storage: Timestamp Now (r:1 w:0)
5859
fn register_domain() -> Weight {
59-
(45_091_000 as Weight)
60+
(67_000_000 as Weight)
6061
.saturating_add(T::DbWeight::get().reads(5 as Weight))
6162
.saturating_add(T::DbWeight::get().writes(2 as Weight))
6263
}
@@ -65,49 +66,56 @@ pub struct SubstrateWeight<T>(PhantomData<T>);
6566
// Storage: Domains RegisteredDomains (r:1 w:1)
6667
// Storage: Timestamp Now (r:1 w:0)
6768
fn force_register_domain() -> Weight {
68-
(30_322_000 as Weight)
69+
(44_000_000 as Weight)
6970
.saturating_add(T::DbWeight::get().reads(4 as Weight))
7071
.saturating_add(T::DbWeight::get().writes(2 as Weight))
7172
}
7273
// Storage: Domains RegisteredDomains (r:1 w:1)
7374
// Storage: Domains DomainByInnerValue (r:0 w:2)
7475
fn set_inner_value() -> Weight {
75-
(26_162_000 as Weight)
76+
(38_000_000 as Weight)
7677
.saturating_add(T::DbWeight::get().reads(1 as Weight))
7778
.saturating_add(T::DbWeight::get().writes(3 as Weight))
7879
}
7980
// Storage: Domains RegisteredDomains (r:1 w:1)
8081
// Storage: Domains DomainByInnerValue (r:0 w:2)
8182
fn force_set_inner_value() -> Weight {
82-
(25_274_000 as Weight)
83+
(37_000_000 as Weight)
8384
.saturating_add(T::DbWeight::get().reads(1 as Weight))
8485
.saturating_add(T::DbWeight::get().writes(3 as Weight))
8586
}
8687
// Storage: Domains RegisteredDomains (r:1 w:1)
8788
fn set_outer_value() -> Weight {
88-
(35_425_000 as Weight)
89+
(51_000_000 as Weight)
8990
.saturating_add(T::DbWeight::get().reads(1 as Weight))
9091
.saturating_add(T::DbWeight::get().writes(1 as Weight))
9192
}
9293
// Storage: Domains RegisteredDomains (r:1 w:1)
9394
fn set_domain_content() -> Weight {
94-
(21_323_000 as Weight)
95+
(31_000_000 as Weight)
9596
.saturating_add(T::DbWeight::get().reads(1 as Weight))
9697
.saturating_add(T::DbWeight::get().writes(1 as Weight))
98+
}
99+
// Storage: Domains RegisteredDomains (r:1 w:0)
100+
// Storage: Domains DefaultDomain (r:1 w:1)
101+
fn set_default_domain() -> Weight {
102+
(34_000_000 as Weight)
103+
.saturating_add(T::DbWeight::get().reads(2 as Weight))
104+
.saturating_add(T::DbWeight::get().writes(1 as Weight))
97105
}
98106
// Storage: Domains ReservedWords (r:0 w:1)
99107
fn reserve_words(s: u32, ) -> Weight {
100-
(56_416_000 as Weight)
101-
// Standard Error: 2_000
102-
.saturating_add((1_702_000 as Weight).saturating_mul(s as Weight))
108+
(0 as Weight)
109+
// Standard Error: 16_000
110+
.saturating_add((2_488_000 as Weight).saturating_mul(s as Weight))
103111
.saturating_add(T::DbWeight::get().writes(311 as Weight))
104112
.saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight)))
105113
}
106114
// Storage: Domains SupportedTlds (r:0 w:1)
107115
fn support_tlds(s: u32, ) -> Weight {
108-
(53_216_000 as Weight)
109-
// Standard Error: 3_000
110-
.saturating_add((1_685_000 as Weight).saturating_mul(s as Weight))
116+
(59_257_000 as Weight)
117+
// Standard Error: 9_000
118+
.saturating_add((2_269_000 as Weight).saturating_mul(s as Weight))
111119
.saturating_add(T::DbWeight::get().writes(311 as Weight))
112120
.saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight)))
113121
}
@@ -121,7 +129,7 @@ pub struct SubstrateWeight<T>(PhantomData<T>);
121129
// Storage: Domains RegisteredDomains (r:1 w:1)
122130
// Storage: Timestamp Now (r:1 w:0)
123131
fn register_domain() -> Weight {
124-
(45_091_000 as Weight)
132+
(67_000_000 as Weight)
125133
.saturating_add(RocksDbWeight::get().reads(5 as Weight))
126134
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
127135
}
@@ -130,49 +138,56 @@ pub struct SubstrateWeight<T>(PhantomData<T>);
130138
// Storage: Domains RegisteredDomains (r:1 w:1)
131139
// Storage: Timestamp Now (r:1 w:0)
132140
fn force_register_domain() -> Weight {
133-
(30_322_000 as Weight)
141+
(44_000_000 as Weight)
134142
.saturating_add(RocksDbWeight::get().reads(4 as Weight))
135143
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
136144
}
137145
// Storage: Domains RegisteredDomains (r:1 w:1)
138146
// Storage: Domains DomainByInnerValue (r:0 w:2)
139147
fn set_inner_value() -> Weight {
140-
(26_162_000 as Weight)
148+
(38_000_000 as Weight)
141149
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
142150
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
143151
}
144152
// Storage: Domains RegisteredDomains (r:1 w:1)
145153
// Storage: Domains DomainByInnerValue (r:0 w:2)
146154
fn force_set_inner_value() -> Weight {
147-
(25_274_000 as Weight)
155+
(37_000_000 as Weight)
148156
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
149157
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
150158
}
151159
// Storage: Domains RegisteredDomains (r:1 w:1)
152160
fn set_outer_value() -> Weight {
153-
(35_425_000 as Weight)
161+
(51_000_000 as Weight)
154162
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
155163
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
156164
}
157165
// Storage: Domains RegisteredDomains (r:1 w:1)
158166
fn set_domain_content() -> Weight {
159-
(21_323_000 as Weight)
167+
(31_000_000 as Weight)
160168
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
161169
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
170+
}
171+
// Storage: Domains RegisteredDomains (r:1 w:0)
172+
// Storage: Domains DefaultDomain (r:1 w:1)
173+
fn set_default_domain() -> Weight {
174+
(34_000_000 as Weight)
175+
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
176+
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
162177
}
163178
// Storage: Domains ReservedWords (r:0 w:1)
164179
fn reserve_words(s: u32, ) -> Weight {
165-
(56_416_000 as Weight)
166-
// Standard Error: 2_000
167-
.saturating_add((1_702_000 as Weight).saturating_mul(s as Weight))
180+
(0 as Weight)
181+
// Standard Error: 16_000
182+
.saturating_add((2_488_000 as Weight).saturating_mul(s as Weight))
168183
.saturating_add(RocksDbWeight::get().writes(311 as Weight))
169184
.saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(s as Weight)))
170185
}
171186
// Storage: Domains SupportedTlds (r:0 w:1)
172187
fn support_tlds(s: u32, ) -> Weight {
173-
(53_216_000 as Weight)
174-
// Standard Error: 3_000
175-
.saturating_add((1_685_000 as Weight).saturating_mul(s as Weight))
188+
(59_257_000 as Weight)
189+
// Standard Error: 9_000
190+
.saturating_add((2_269_000 as Weight).saturating_mul(s as Weight))
176191
.saturating_add(RocksDbWeight::get().writes(311 as Weight))
177192
.saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(s as Weight)))
178193
}

0 commit comments

Comments
 (0)