Skip to content

Commit

Permalink
set subdomain renew policy
Browse files Browse the repository at this point in the history
  • Loading branch information
angieyth committed Aug 7, 2023
1 parent e4532b2 commit d602965
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 26 deletions.
33 changes: 25 additions & 8 deletions core_v2/sources/domains.move
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ module aptos_names_v2::domains {
const AUTO_RENEWAL_EXPIRATION_CUTOFF_SEC: u64 = 1709855999;
const SECONDS_PER_YEAR: u64 = 60 * 60 * 24 * 365;

/// enums for subdomain expiration policy
const SUBDOMAIN_POLICY_LOOKUP_DOMAIN_EXPIRATION: u8 = 0;
const SUBDOMAIN_POLICY_MANUAL_SET_EXPIRATION: u8 = 1;

/// The Naming Service contract is not enabled
const ENOT_ENABLED: u64 = 1;
/// The caller is not authorized to perform this operation
Expand Down Expand Up @@ -77,6 +81,8 @@ module aptos_names_v2::domains {
const ESUBDOMAIN_EXPIRATION_PASS_DOMAIN_EXPIRATION: u64 = 24;
/// The duration must be whole years
const EDURATION_MUST_BE_WHOLE_YEARS: u64 = 25;
/// The subdomain expiration policy is included in the enum SUBDOMAIN_POLICY_*
const ESUBDOMAIN_EXPIRATION_POLICY_INVALID: u64 = 26;

/// Tokens require a signer to create, so this is the signer for the collection
struct CollectionCapabilityV2 has key, drop {
Expand All @@ -92,7 +98,7 @@ module aptos_names_v2::domains {

struct SubdomainExt has store {
subdomain_name: String,
use_domain_expiration_sec: bool,
subdomain_expiration_policy: u8,
}

#[resource_group_member(group = aptos_framework::object::ObjectGroup)]
Expand Down Expand Up @@ -341,7 +347,7 @@ module aptos_names_v2::domains {
subdomain_name: option::extract(&mut subdomain_name),
// TODO: use_domain_expiration_sec should be passed in as a param
// Now by default subdomain follow domain's expiration
use_domain_expiration_sec: true,
subdomain_expiration_policy: SUBDOMAIN_POLICY_LOOKUP_DOMAIN_EXPIRATION,
});
}
// creating domain
Expand Down Expand Up @@ -679,7 +685,7 @@ module aptos_names_v2::domains {
let record = get_record_mut(domain_name, option::some(subdomain_name));
assert!(option::is_some(&record.subdomain_ext), error::invalid_state(ENOT_A_SUBDOMAIN));
let subdomain_ext = option::borrow(&record.subdomain_ext);
if (subdomain_ext.use_domain_expiration_sec) {
if (subdomain_ext.subdomain_expiration_policy == SUBDOMAIN_POLICY_LOOKUP_DOMAIN_EXPIRATION) {
assert!(false, error::invalid_state(ESUBDOMAIN_IS_AUTO_RENEW));
};

Expand All @@ -691,30 +697,41 @@ module aptos_names_v2::domains {
sign: &signer,
domain_name: String,
subdomain_name: String,
use_domain_expiration_sec: bool,
subdomain_expiration_policy: u8,
) acquires CollectionCapabilityV2, NameRecordV2 {
validate_subdomain_to_renew(sign, subdomain_name, domain_name);
validate_subdomain_expiration_policy(subdomain_expiration_policy);
// if manually set the expiration date
let record = get_record_mut(domain_name, option::some(subdomain_name));
// check the auto-renew flag
if (option::is_none(&record.subdomain_ext)) {
assert!(false, error::invalid_state(ENOT_A_SUBDOMAIN));
};
let subdomain_ext = option::borrow_mut(&mut record.subdomain_ext);
subdomain_ext.use_domain_expiration_sec = use_domain_expiration_sec;
subdomain_ext.subdomain_expiration_policy = subdomain_expiration_policy;
}

public fun get_subdomain_renewal_policy(
domain_name: String,
subdomain_name: String,
): bool acquires CollectionCapabilityV2, NameRecordV2 {
): u8 acquires CollectionCapabilityV2, NameRecordV2 {
let record = get_record_mut(domain_name, option::some(subdomain_name));
// check the auto-renew flag
if (!option::is_some(&record.subdomain_ext)) {
assert!(false, error::invalid_state(ESUBDOMAIN_NOT_EXIST));
};
let subdomain_ext = option::borrow_mut(&mut record.subdomain_ext);
subdomain_ext.use_domain_expiration_sec
subdomain_ext.subdomain_expiration_policy
}

fun validate_subdomain_expiration_policy(
use_domain_expiration_sec: u8,
) {
assert!(
use_domain_expiration_sec == SUBDOMAIN_POLICY_LOOKUP_DOMAIN_EXPIRATION
|| use_domain_expiration_sec == SUBDOMAIN_POLICY_MANUAL_SET_EXPIRATION,
error::invalid_argument(ESUBDOMAIN_EXPIRATION_POLICY_INVALID)
);
}

fun validate_subdomain_to_renew(
Expand Down Expand Up @@ -760,7 +777,7 @@ module aptos_names_v2::domains {
// check the auto-renew flag
if (option::is_some(&record.subdomain_ext)) {
let subdomain_ext = option::borrow(&record.subdomain_ext);
if (subdomain_ext.use_domain_expiration_sec) {
if (subdomain_ext.subdomain_expiration_policy == SUBDOMAIN_POLICY_LOOKUP_DOMAIN_EXPIRATION) {
// refer to the expiration date of the domain
let domain_record = get_record(domain_name, option::none());
return time_is_expired(domain_record.expiration_time_sec)
Expand Down
67 changes: 49 additions & 18 deletions core_v2/sources/subdomain_e2e_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module aptos_names_v2::subdomain_e2e_tests {
rando = @0x266f,
foundation = @0xf01d
)]
fun renew_domain_e2e_test(
fun test_renew_domain_e2e(
aptos_names: &signer,
aptos_names_v2: &signer,
user: signer,
Expand Down Expand Up @@ -106,7 +106,6 @@ module aptos_names_v2::subdomain_e2e_tests {
assert!(domains::name_is_expired(option::none(), test_helper::domain_name()), 5);
}


#[test(
aptos_names = @aptos_names,
aptos_names_v2 = @aptos_names_v2,
Expand All @@ -115,7 +114,7 @@ module aptos_names_v2::subdomain_e2e_tests {
rando = @0x266f,
foundation = @0xf01d
)]
fun auto_renew_subdomain_e2e_test(
fun test_auto_renew_subdomain_e2e(
aptos_names: &signer,
aptos_names_v2: &signer,
user: signer,
Expand All @@ -131,7 +130,7 @@ module aptos_names_v2::subdomain_e2e_tests {
// Register a subdomain!
test_helper::register_name(user, option::some(test_helper::subdomain_name()), test_helper::domain_name(), timestamp::now_seconds() + test_helper::one_year_secs(), test_helper::fq_subdomain_name(), 1, vector::empty<u8>());
// The subdomain auto-renewal policy is true by default
assert!(domains::get_subdomain_renewal_policy(test_helper::domain_name(), test_helper::subdomain_name()), 2);
assert!(domains::get_subdomain_renewal_policy(test_helper::domain_name(), test_helper::subdomain_name()) == 0, 2);

// Renew the domain (and the subdomain should be auto renewed)
let (original_expiration_time_sec, _) = domains::get_name_record_v1_props_for_name(option::some(test_helper::subdomain_name()), test_helper::domain_name());
Expand All @@ -152,7 +151,39 @@ module aptos_names_v2::subdomain_e2e_tests {
rando = @0x266f,
foundation = @0xf01d
)]
fun manual_renew_subdomain_e2e_test(
#[expected_failure(abort_code = 65562, location = aptos_names_v2::domains)]
fun test_set_subdomain_renewal_policy(
aptos_names: &signer,
aptos_names_v2: &signer,
user: signer,
aptos: signer,
rando: signer,
foundation: signer
) {
let users = test_helper::e2e_test_setup(aptos_names, aptos_names_v2, user, &aptos, rando, &foundation);
let user = vector::borrow(&users, 0);
// Register the domain
test_helper::register_name(user, option::none(), test_helper::domain_name(), test_helper::one_year_secs(), test_helper::fq_domain_name(), 1, vector::empty<u8>());

// Register a subdomain!
test_helper::register_name(user, option::some(test_helper::subdomain_name()), test_helper::domain_name(), test_helper::one_year_secs(), test_helper::fq_subdomain_name(), 1, vector::empty<u8>());
// test set the policy to auto-renewal
domains::set_subdomain_renewal_policy(user, test_helper::domain_name(), test_helper::subdomain_name(), 1);
assert!(domains::get_subdomain_renewal_policy(test_helper::domain_name(), test_helper::subdomain_name()) == 1, 2);

// test set the policy to something not exist
domains::set_subdomain_renewal_policy(user, test_helper::domain_name(), test_helper::subdomain_name(), 100);
}

#[test(
aptos_names = @aptos_names,
aptos_names_v2 = @aptos_names_v2,
user = @0x077,
aptos = @0x1,
rando = @0x266f,
foundation = @0xf01d
)]
fun test_manual_renew_subdomain_e2e(
aptos_names: &signer,
aptos_names_v2: &signer,
user: signer,
Expand All @@ -167,8 +198,8 @@ module aptos_names_v2::subdomain_e2e_tests {

// Register a subdomain!
test_helper::register_name(user, option::some(test_helper::subdomain_name()), test_helper::domain_name(), test_helper::one_year_secs(), test_helper::fq_subdomain_name(), 1, vector::empty<u8>());
domains::set_subdomain_renewal_policy(user, test_helper::domain_name(), test_helper::subdomain_name(), false);
assert!(!domains::get_subdomain_renewal_policy(test_helper::domain_name(), test_helper::subdomain_name()), 2);
domains::set_subdomain_renewal_policy(user, test_helper::domain_name(), test_helper::subdomain_name(), 1);
assert!(domains::get_subdomain_renewal_policy(test_helper::domain_name(), test_helper::subdomain_name()) == 1, 2);

// Set the time past the domain's expiration time
let (expiration_time_sec, _) = domains::get_name_record_v1_props_for_name(option::some(test_helper::subdomain_name()), test_helper::domain_name());
Expand Down Expand Up @@ -206,7 +237,7 @@ module aptos_names_v2::subdomain_e2e_tests {
// Register a subdomain!
test_helper::register_name(user, option::some(test_helper::subdomain_name()), test_helper::domain_name(), test_helper::one_year_secs(), test_helper::fq_subdomain_name(), 1, vector::empty<u8>());
// Set the auto-renewal flag as false
domains::set_subdomain_renewal_policy(user, test_helper::domain_name(), test_helper::subdomain_name(), false);
domains::set_subdomain_renewal_policy(user, test_helper::domain_name(), test_helper::subdomain_name(), 1);

domains::set_subdomain_expiration_as_domain_owner(user, test_helper::domain_name(), test_helper::subdomain_name(), timestamp::now_seconds() + 10);
let (expiration_time_sec, _) = domains::get_name_record_v1_props_for_name(option::some(test_helper::subdomain_name()), test_helper::domain_name());
Expand Down Expand Up @@ -271,7 +302,7 @@ module aptos_names_v2::subdomain_e2e_tests {
rando = @0x266f,
foundation = @0xf01d
)]
fun names_are_registerable_after_expiry_e2e_test(
fun test_names_are_registerable_after_expiry_e2e(
aptos_names: &signer,
aptos_names_v2: &signer,
user: signer,
Expand All @@ -289,7 +320,7 @@ module aptos_names_v2::subdomain_e2e_tests {
// Register a subdomain!
test_helper::register_name(user, option::some(test_helper::subdomain_name()), test_helper::domain_name(), timestamp::now_seconds() + test_helper::one_year_secs(), test_helper::fq_subdomain_name(), 1, vector::empty<u8>());
// Set the subdomain auto-renewal policy to false
domains::set_subdomain_renewal_policy(user, test_helper::domain_name(), test_helper::subdomain_name(), false);
domains::set_subdomain_renewal_policy(user, test_helper::domain_name(), test_helper::subdomain_name(), 1);

// Set the time past the domain's expiration time
let (expiration_time_sec, _) = domains::get_name_record_v1_props_for_name(option::some(test_helper::subdomain_name()), test_helper::domain_name());
Expand Down Expand Up @@ -350,7 +381,7 @@ module aptos_names_v2::subdomain_e2e_tests {
foundation = @0xf01d
)]
#[expected_failure(abort_code = 196611, location = aptos_names_v2::domains)]
fun dont_allow_double_subdomain_registrations_e2e_test(
fun test_dont_allow_double_subdomain_registrations_e2e(
aptos_names: &signer,
aptos_names_v2: &signer,
user: signer,
Expand All @@ -377,7 +408,7 @@ module aptos_names_v2::subdomain_e2e_tests {
foundation = @0xf01d
)]
#[expected_failure(abort_code = 327689, location = aptos_names_v2::domains)]
fun dont_allow_rando_to_set_subdomain_address_e2e_test(
fun test_dont_allow_rando_to_set_subdomain_address_e2e(
aptos_names: &signer,
aptos_names_v2: &signer,
user: signer,
Expand Down Expand Up @@ -405,7 +436,7 @@ module aptos_names_v2::subdomain_e2e_tests {
foundation = @0xf01d
)]
#[expected_failure(abort_code = 327689, location = aptos_names_v2::domains)]
fun dont_allow_rando_to_clear_subdomain_address_e2e_test(
fun test_dont_allow_rando_to_clear_subdomain_address_e2e(
aptos_names: &signer,
aptos_names_v2: &signer,
user: signer,
Expand Down Expand Up @@ -433,7 +464,7 @@ module aptos_names_v2::subdomain_e2e_tests {
rando = @0x266f,
foundation = @0xf01d
)]
fun admin_can_force_set_subdomain_address_e2e_test(
fun test_admin_can_force_set_subdomain_address_e2e(
aptos_names: &signer,
aptos_names_v2: &signer,
user: signer,
Expand Down Expand Up @@ -466,7 +497,7 @@ module aptos_names_v2::subdomain_e2e_tests {
foundation = @0xf01d
)]
#[expected_failure(abort_code = 327681, location = aptos_names_v2::config)]
fun rando_cant_force_set_subdomain_address_e2e_test(
fun test_rando_cant_force_set_subdomain_address_e2e(
aptos_names: &signer,
aptos_names_v2: &signer,
user: signer,
Expand Down Expand Up @@ -566,7 +597,7 @@ module aptos_names_v2::subdomain_e2e_tests {
foundation = @0xf01d
)]
#[expected_failure(abort_code = 131086, location = aptos_names_v2::domains)]
fun admin_cant_force_create_subdomain_more_than_domain_time_e2e_test(
fun test_admin_cant_force_create_subdomain_more_than_domain_time_e2e(
aptos_names: &signer,
aptos_names_v2: &signer,
user: signer,
Expand Down Expand Up @@ -594,7 +625,7 @@ module aptos_names_v2::subdomain_e2e_tests {
foundation = @0xf01d
)]
#[expected_failure(abort_code = 327681, location = aptos_names_v2::config)]
fun rando_cant_force_seize_subdomain_name_e2e_test(
fun test_rando_cant_force_seize_subdomain_name_e2e(
aptos_names: &signer,
aptos_names_v2: &signer,
user: signer,
Expand Down Expand Up @@ -625,7 +656,7 @@ module aptos_names_v2::subdomain_e2e_tests {
foundation = @0xf01d
)]
#[expected_failure(abort_code = 327681, location = aptos_names_v2::config)]
fun rando_cant_force_create_subdomain_name_e2e_test(
fun test_rando_cant_force_create_subdomain_name_e2e(
aptos_names: &signer,
aptos_names_v2: &signer,
user: signer,
Expand Down

0 comments on commit d602965

Please sign in to comment.