Return reasonable values for unlock function
This commit is contained in:
parent
849fcb3541
commit
3b1de79309
|
@ -1,4 +1,4 @@
|
|||
use chrono::{offset::Utc, DateTime};
|
||||
use chrono::{DateTime, Utc};
|
||||
use lox_library::bridge_table::{
|
||||
from_scalar, BridgeLine, BridgeTable, EncryptedBucket, MAX_BRIDGES_PER_BUCKET,
|
||||
};
|
||||
|
@ -93,6 +93,14 @@ pub const LOX_SYSTEM_INFO: LoxSystemInfo = LoxSystemInfo {
|
|||
min_blockage_migration_trust_level: check_blockage::MIN_TRUST_LEVEL,
|
||||
};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct LoxNextUnlock {
|
||||
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>,
|
||||
}
|
||||
|
||||
#[serde_as]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct EncBridgeTable {
|
||||
|
|
|
@ -16,7 +16,7 @@ import init, {
|
|||
handle_check_blockage,
|
||||
blockage_migration,
|
||||
handle_blockage_migration,
|
||||
set_panic_hook, get_last_upgrade_time, get_trust_level, get_invites_remaining, get_issued_invite_expiry, get_received_invite_expiry, get_bridgelines_from_bucket} from "./pkg/lox_wasm.js";
|
||||
set_panic_hook, get_last_upgrade_time, get_trust_level, get_invites_remaining, get_next_unlock, get_received_invite_expiry, get_bridgelines_from_bucket} from "./pkg/lox_wasm.js";
|
||||
let pubkeys = await simple_request("/pubkeys");
|
||||
console.log("Got pubkeys: " + pubkeys);
|
||||
let constants = await simple_request("/constants");
|
||||
|
@ -42,10 +42,12 @@ let open_lox_cred = await init().then(() => {
|
|||
});
|
||||
return cred;
|
||||
});
|
||||
|
||||
let unlock_info = get_next_unlock(constants, open_lox_cred);
|
||||
console.log("Unlock info: "+unlock_info);
|
||||
get_last_upgrade_time(open_lox_cred);
|
||||
get_trust_level(open_lox_cred);
|
||||
get_invites_remaining(open_lox_cred);
|
||||
|
||||
let encrypted_table = await simple_request("/reachability");
|
||||
let info_five = get_bridgelines_from_bucket(open_lox_cred, encrypted_table);
|
||||
console.log("Bridgelines available: "+info_five);
|
||||
|
@ -236,7 +238,7 @@ function loxServerPostRequest(data, payload) {
|
|||
return;
|
||||
};
|
||||
try {
|
||||
xhr.open('POST', "https://rdsys-frontend-01.torproject.org/lox"+data, true)
|
||||
xhr.open('POST', "http://localhost:8001"+data, true)
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
} catch (err) {
|
||||
console.log("Error connecting to lox bridge db");
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use chrono::{Utc, DateTime, NaiveDateTime, NaiveTime};
|
||||
use chrono::{DateTime, NaiveDateTime, NaiveTime, Utc};
|
||||
use julianday::JulianDay;
|
||||
use lox_library::cred::{Invitation, Migration};
|
||||
use lox_library::proto::{
|
||||
|
@ -793,12 +793,10 @@ pub fn get_bridgelines_from_bucket(
|
|||
pub fn invitation_is_trusted(unspecified_invitation_str: String) -> Result<bool, JsValue> {
|
||||
match serde_json::from_str::<Invitation>(&unspecified_invitation_str) {
|
||||
Ok(_) => Ok(true),
|
||||
Err(_) => {
|
||||
match serde_json::from_str::<lox_utils::Invite>(&unspecified_invitation_str){
|
||||
Ok(_) => Ok(false),
|
||||
Err(e) => Err(JsValue::from(e.to_string())),
|
||||
}
|
||||
}
|
||||
Err(_) => match serde_json::from_str::<lox_utils::Invite>(&unspecified_invitation_str) {
|
||||
Ok(_) => Ok(false),
|
||||
Err(e) => Err(JsValue::from(e.to_string())),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -816,34 +814,42 @@ pub fn get_next_unlock(constants_str: String, lox_cred_str: String) -> Result<St
|
|||
let (days_to_next_level, invitations_at_next_level) = match trust_level as usize {
|
||||
0 => (constants.untrusted_interval, 0),
|
||||
|
||||
_ => { if trust_level as usize == constants.max_level {
|
||||
(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 ])
|
||||
_ => {
|
||||
if trust_level as usize == constants.max_level {
|
||||
(
|
||||
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 days_to_blockage_migrations = match trust_level < constants.min_blockage_migration_trust_level {
|
||||
true => {
|
||||
let mut blockage_days = (scalar_u32(&lox_cred.lox_credential.level_since).unwrap()+days_to_next_level) as u32;
|
||||
let mut count = 1;
|
||||
while trust_level + count < constants.min_blockage_migration_trust_level {
|
||||
count+=1;
|
||||
blockage_days += constants.level_interval[trust_level as usize+1];
|
||||
if invitations_at_next_level == 0 {}
|
||||
let days_to_blockage_migrations =
|
||||
match trust_level < constants.min_blockage_migration_trust_level {
|
||||
true => {
|
||||
let mut blockage_days =
|
||||
scalar_u32(&lox_cred.lox_credential.level_since).unwrap() + days_to_next_level;
|
||||
let mut count = 1;
|
||||
while trust_level + count < constants.min_blockage_migration_trust_level {
|
||||
count += 1;
|
||||
blockage_days += constants.level_interval[trust_level as usize + 1];
|
||||
}
|
||||
blockage_days
|
||||
}
|
||||
blockage_days
|
||||
},
|
||||
false => 0,
|
||||
};
|
||||
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 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 {}",
|
||||
|
@ -853,22 +859,39 @@ pub fn get_next_unlock(constants_str: String, lox_cred_str: String) -> Result<St
|
|||
"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 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();
|
||||
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()), Utc),
|
||||
invitation_unlock_date: DateTime::<Utc>::from_naive_utc_and_offset(NaiveDateTime::new(invite_unlock_date, NaiveTime::from_hms_opt(0,0,0).unwrap()), Utc),
|
||||
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()),
|
||||
Utc,
|
||||
),
|
||||
invitation_unlock_date: DateTime::<Utc>::from_naive_utc_and_offset(
|
||||
NaiveDateTime::new(
|
||||
invite_unlock_date,
|
||||
NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
|
||||
),
|
||||
Utc,
|
||||
),
|
||||
num_invitations_unlocked: invitations_at_next_level,
|
||||
blockage_migration_unlock_date: DateTime::<Utc>::from_naive_utc_and_offset(NaiveDateTime::new(blockage_migration_unlock_date, NaiveTime::from_hms_opt(0,0,0).unwrap()), Utc),
|
||||
blockage_migration_unlock_date: DateTime::<Utc>::from_naive_utc_and_offset(
|
||||
NaiveDateTime::new(
|
||||
blockage_migration_unlock_date,
|
||||
NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
|
||||
),
|
||||
Utc,
|
||||
),
|
||||
};
|
||||
match serde_json::to_string(&next_unlock) {
|
||||
Ok(next_unlock) => Ok(next_unlock),
|
||||
|
|
Loading…
Reference in New Issue