Added part of next unlock function
This commit is contained in:
parent
60a47c962f
commit
849fcb3541
|
@ -1131,6 +1131,7 @@ dependencies = [
|
|||
name = "lox_utils"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"lox-library",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
|
|
@ -12,6 +12,7 @@ categories = ["rust-patterns"]
|
|||
repository = "https://gitlab.torproject.org/tpo/anti-censorship/lox.git/"
|
||||
|
||||
[dependencies]
|
||||
chrono = { version = "0.4.31", features = ["serde", "clock"] }
|
||||
lox-library = {path = "../lox-library", version = "0.1.0"}
|
||||
serde = "1"
|
||||
serde_json = "1.0.108"
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use chrono::{offset::Utc, DateTime};
|
||||
use lox_library::bridge_table::{
|
||||
from_scalar, BridgeLine, BridgeTable, EncryptedBucket, MAX_BRIDGES_PER_BUCKET,
|
||||
};
|
||||
|
|
|
@ -236,7 +236,7 @@ function loxServerPostRequest(data, payload) {
|
|||
return;
|
||||
};
|
||||
try {
|
||||
xhr.open('POST', "http://localhost:8001"+data, true)
|
||||
xhr.open('POST', "https://rdsys-frontend-01.torproject.org/lox"+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;
|
||||
use chrono::{Utc, DateTime, NaiveDateTime, NaiveTime};
|
||||
use julianday::JulianDay;
|
||||
use lox_library::cred::{Invitation, Migration};
|
||||
use lox_library::proto::{
|
||||
|
@ -801,3 +801,77 @@ pub fn invitation_is_trusted(unspecified_invitation_str: String) -> Result<bool,
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn get_next_unlock(constants_str: String, lox_cred_str: String) -> Result<String, JsValue> {
|
||||
let constants: lox_utils::LoxSystemInfo = match serde_json::from_str(&constants_str) {
|
||||
Ok(constants) => constants,
|
||||
Err(e) => return Err(JsValue::from(e.to_string())),
|
||||
};
|
||||
let lox_cred: lox_utils::LoxCredential = match serde_json::from_str(&lox_cred_str) {
|
||||
Ok(lox_cred) => lox_cred,
|
||||
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 {
|
||||
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 ])
|
||||
}
|
||||
},
|
||||
};
|
||||
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];
|
||||
}
|
||||
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()), 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),
|
||||
};
|
||||
match serde_json::to_string(&next_unlock) {
|
||||
Ok(next_unlock) => Ok(next_unlock),
|
||||
Err(e) => Err(JsValue::from(e.to_string())),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue