From f12685e32689b620d6096ec91ba3a3f495342925 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Fri, 15 Apr 2022 19:01:40 +0300 Subject: [WIP] first part of lazy users overhaul --- src/db.rs | 14 ++++++++++++++ src/handlers.rs | 9 +++++---- src/student.rs | 7 ++++++- 3 files changed, 25 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/db.rs b/src/db.rs index bf094ab..fd5c1be 100644 --- a/src/db.rs +++ b/src/db.rs @@ -19,6 +19,7 @@ pub struct Db { pub blockchain: Arc>, pub pending_transactions: Arc>>, pub users: Arc>>, + approved_users: Vec, // TODO: metu_ids or approved_users or something, metu_id struct <11-04-22, yigit> // } @@ -36,11 +37,13 @@ impl Db { } let users: HashMap = get_friendly_users(); + let approved_users = read_approved_users(); Db { blockchain: Arc::new(RwLock::new(Block::default())), pending_transactions: Arc::new(RwLock::new(HashMap::new())), users: Arc::new(RwLock::new(users)), + approved_users, } } @@ -157,3 +160,14 @@ fn get_friendly_users() -> HashMap { ); users } + +fn read_approved_users() -> Vec { + let mut approved_students: Vec = Vec::new(); + let contents = fs::read_to_string("students.csv").unwrap(); + let mut reader = csv::Reader::from_reader(contents.as_bytes()); + for student in reader.records() { + let student = student.unwrap(); + approved_students.push(MetuId::_new(student[0].to_owned(), student[1].to_owned())); + } + approved_students +} diff --git a/src/handlers.rs b/src/handlers.rs index a64c012..96001ce 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -27,13 +27,13 @@ use warp::{http::StatusCode, reply}; use crate::PRIVATE_KEY; // Valid blocks should have this many transactions -const BLOCK_TRANSACTION_COUNT: u8 = 8; +const BLOCK_TRANSACTION_COUNT: u8 = 4; // Inital registration bonus -const REGISTER_BONUS: u16 = 40; +const REGISTER_BONUS: u16 = 20; // Coinbase reward -const BLOCK_REWARD: u16 = 3; +const BLOCK_REWARD: u16 = 2; // Transaction amount limit -const TX_UPPER_LIMIT: u16 = 10; +const TX_UPPER_LIMIT: u16 = 4; const TX_LOWER_LIMIT: u16 = 1; // Transaction traffic reward const TX_TRAFFIC_REWARD: u16 = 1; @@ -278,6 +278,7 @@ pub async fn authenticate_user( }; // is the student in AuthRequest privileged? + // TODO: this is the only check for 'if metuid is approved' <15-04-22, yigit> // let privileged_student_id = if let Some(id) = MetuId::new(request.student_id.clone(), request.passwd.clone()) { id diff --git a/src/student.rs b/src/student.rs index 4b7acf1..711eeeb 100644 --- a/src/student.rs +++ b/src/student.rs @@ -26,6 +26,7 @@ pub struct User { } /// The values are hard coded in [`static@OUR_STUDENTS`] so `MetuId::new`() can accept/reject values based on that +/// TODO update the statement above #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] pub struct MetuId { id: String, @@ -46,10 +47,14 @@ impl MetuId { None } } + + // TODO: replace the function above with this <15-04-22, yigit> // + pub fn _new(id: String, passwd: String) -> Self { + MetuId { id, passwd } + } } // TODO: remove this, read from a yaml or something, then MetuId::new gets a self <11-04-22, yigit> // - // Students who are authorized to have Gradecoin accounts lazy_static! { static ref OUR_STUDENTS: HashSet<(&'static str, &'static str)> = { -- cgit v1.2.3-70-g09d2