Ensure calculations return correct unlock times

This commit is contained in:
onyinyang 2024-01-22 13:29:22 -05:00
parent 3b1de79309
commit a8be1a8043
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
2 changed files with 28 additions and 45 deletions

View File

@ -95,10 +95,10 @@ pub const LOX_SYSTEM_INFO: LoxSystemInfo = LoxSystemInfo {
#[derive(Debug, Deserialize, Serialize)]
pub struct LoxNextUnlock {
pub trust_level_unlock_date: DateTime::<Utc>,
pub invitation_unlock_date: DateTime::<Utc>,
pub trust_level_unlock_date: DateTime<Utc>,
pub invitation_unlock_date: DateTime<Utc>,
pub num_invitations_unlocked: u32,
pub blockage_migration_unlock_date: DateTime::<Utc>,
pub blockage_migration_unlock_date: DateTime<Utc>,
}
#[serde_as]

View File

@ -811,67 +811,50 @@ pub fn get_next_unlock(constants_str: String, lox_cred_str: String) -> Result<St
Err(e) => return Err(JsValue::from(e.to_string())),
};
let trust_level = scalar_u32(&lox_cred.lox_credential.trust_level).unwrap();
let (days_to_next_level, invitations_at_next_level) = match trust_level as usize {
let (days_to_next_level, mut invitations_at_next_level) = match trust_level as usize {
// If the credential is at trust level 0, we use the untrusted interval from the
// trust promotion protocol to calculate the date of the next level update
0 => (constants.untrusted_interval, 0),
_ => {
if trust_level as usize == constants.max_level {
(
// Otherwise, we usethe invitation and upgrade dates from the level up protocol constants
_ => (
constants.level_interval[trust_level as usize],
constants.level_invitations[trust_level as usize],
)
} else {
(
constants.level_interval[trust_level as usize - 1],
constants.level_invitations[trust_level as usize - 1],
)
}
}
),
};
let days_to_invite_inc = days_to_next_level;
if invitations_at_next_level == 0 {}
let mut days_to_invite_inc = days_to_next_level;
// If there are no invitations at the next trust level upgrade
// i.e., if the credential is at level 0, calculate the time until they will
// unlock invitations
if invitations_at_next_level == 0 {
days_to_invite_inc =
days_to_next_level + constants.level_interval[trust_level as usize + 1];
invitations_at_next_level = constants.level_invitations[trust_level as usize + 1];
}
let days_to_blockage_migrations =
match trust_level < constants.min_blockage_migration_trust_level {
// If the credential is greater than the minimum level that enables
// migrating after a blockage, the time to unlock is 0, otherwise we
// add the time to upgrade until that level
true => {
let mut blockage_days =
scalar_u32(&lox_cred.lox_credential.level_since).unwrap() + days_to_next_level;
let mut blockage_days = days_to_next_level;
let mut count = 1;
while trust_level + count < constants.min_blockage_migration_trust_level {
count += 1;
while trust_level + count <= constants.min_blockage_migration_trust_level {
blockage_days += constants.level_interval[trust_level as usize + 1];
count += 1;
}
blockage_days
scalar_u32(&lox_cred.lox_credential.level_since).unwrap() + blockage_days
}
false => 0,
};
log(&format!(
"Trust level {}",
serde_json::to_string(&trust_level).unwrap()
));
let days_to_level_unlock =
(scalar_u32(&lox_cred.lox_credential.level_since).unwrap() + days_to_next_level) as i32;
let level_unlock_date = JulianDay::new(days_to_level_unlock).to_date();
log(&format!(
"Days to next level unlock {}",
serde_json::to_string(&days_to_level_unlock).unwrap()
));
log(&format!(
"Date of unlock {}",
serde_json::to_string(&level_unlock_date).unwrap()
));
let days_to_invite_unlock =
(scalar_u32(&lox_cred.lox_credential.level_since).unwrap() + days_to_invite_inc) as i32;
let invite_unlock_date = JulianDay::new(days_to_invite_unlock).to_date();
log(&format!(
"Days to next level unlock {}",
serde_json::to_string(&days_to_level_unlock).unwrap()
));
let blockage_migration_unlock_date =
JulianDay::new(days_to_blockage_migrations as i32).to_date();
log(&format!(
"Date of unblock {}",
serde_json::to_string(&blockage_migration_unlock_date).unwrap()
));
let next_unlock: lox_utils::LoxNextUnlock = lox_utils::LoxNextUnlock {
trust_level_unlock_date: DateTime::<Utc>::from_naive_utc_and_offset(
NaiveDateTime::new(level_unlock_date, NaiveTime::from_hms_opt(0, 0, 0).unwrap()),