Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add temporary grace period #117

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 50 additions & 17 deletions src/azns_registry/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ mod azns_registry {
use azns_merkle_verifier::MerkleVerifierRef;
use azns_name_checker::NameCheckerRef;

const YEAR: u64 = match cfg!(test) {
pub const YEAR: u64 = match cfg!(test) {
true => 60, // For testing purpose
false => 365 * 24 * 60 * 60 * 1000, // Year in milliseconds
};

// 1st Oct 2024 00:00 (UTC)
pub const GRACE_TIMESTAMP: u64 = 1727740800000; // TODO: set appropriate timestamp
realnimish marked this conversation as resolved.
Show resolved Hide resolved

pub type Result<T> = core::result::Result<T, Error>;

/// Different states of a name
Expand Down Expand Up @@ -1387,13 +1390,23 @@ mod azns_registry {
}

fn get_registration_period_ref(&self, name: &str) -> Result<(u64, u64)> {
self.name_to_period.get(name).ok_or(Error::NameDoesntExist)
self.name_to_period
.get(name)
.map(|(start, expiry)| {
let expiry = match expiry {
0 => 0,
_ if expiry <= GRACE_TIMESTAMP => GRACE_TIMESTAMP,
_ => expiry,
};
(start, expiry)
})
.ok_or(Error::NameDoesntExist)
}

fn has_name_expired(&self, name: &str) -> Result<bool> {
match self.name_to_period.get(name) {
Some((_, expiry)) => Ok(expiry <= self.env().block_timestamp()),
None => Err(Error::NameDoesntExist),
match self.get_registration_period_ref(name) {
Ok((_, expiry)) => Ok(expiry <= self.env().block_timestamp()),
_ => Err(Error::NameDoesntExist),
}
}

Expand Down Expand Up @@ -2916,6 +2929,8 @@ mod tests {
let name1 = "one-year".to_string();
let name2 = "two-year".to_string();

set_block_timestamp::<DefaultEnvironment>(GRACE_TIMESTAMP - YEAR);

// Register name1 for one year
transfer_in::<DefaultEnvironment>(1000);
contract.register(name1.clone(), 1, None, true).unwrap();
Expand All @@ -2924,10 +2939,7 @@ mod tests {
transfer_in::<DefaultEnvironment>(1000);
contract.register(name2.clone(), 2, None, false).unwrap();

// (for cfg(test)) block_time = 6, year = 60
for _ in 0..10 {
advance_block::<DefaultEnvironment>();
}
set_block_timestamp::<DefaultEnvironment>(GRACE_TIMESTAMP);

let address_dict = AddressDict::new(default_accounts().alice);
assert_eq!(
Expand Down Expand Up @@ -2959,6 +2971,8 @@ mod tests {
let name1 = "one-year".to_string();
let name2 = "two-year".to_string();

set_block_timestamp::<DefaultEnvironment>(GRACE_TIMESTAMP - YEAR);

// Register name1 for one year
transfer_in::<DefaultEnvironment>(1000);
contract.register(name1.clone(), 1, None, true).unwrap();
Expand All @@ -2967,10 +2981,7 @@ mod tests {
transfer_in::<DefaultEnvironment>(1000);
contract.register(name2.clone(), 2, None, false).unwrap();

// (for cfg(test)) block_time = 6, year = 60
for _ in 0..10 {
advance_block::<DefaultEnvironment>();
}
set_block_timestamp::<DefaultEnvironment>(GRACE_TIMESTAMP);

// Only the expired names are cleared
assert_eq!(
Expand Down Expand Up @@ -3010,10 +3021,7 @@ mod tests {
Err(Error::NameAlreadyExists)
);

// (for cfg(test)) block_time = 6, year = 60
for _ in 0..10 {
advance_block::<DefaultEnvironment>();
}
set_block_timestamp::<DefaultEnvironment>(GRACE_TIMESTAMP);

// Registering an expired name works
assert_eq!(contract.register(name1.clone(), 1, None, false), Ok(()));
Expand All @@ -3033,4 +3041,29 @@ mod tests {
contract.accept_ownership().unwrap();
assert_eq!(contract.get_admin(), accounts.bob);
}

#[ink::test]
fn grace_period_works() {
let mut contract = get_test_name_service();
let name1 = "one-year".to_string();

set_block_timestamp::<DefaultEnvironment>(GRACE_TIMESTAMP - YEAR - 10);

// Register name1 for one year
transfer_in::<DefaultEnvironment>(1000);
contract.register(name1.clone(), 1, None, true).unwrap();

set_block_timestamp::<DefaultEnvironment>(GRACE_TIMESTAMP - 1);
let address_dict = AddressDict::new(default_accounts().alice);
assert_eq!(
contract.get_name_status(vec![name1.clone()]),
vec![NameStatus::Registered(address_dict, None)]
);

set_block_timestamp::<DefaultEnvironment>(GRACE_TIMESTAMP);
assert_eq!(
contract.get_name_status(vec![name1.clone()]),
vec![NameStatus::Available]
);
}
}
Loading