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 --- Cargo.lock | 41 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/db.rs | 14 ++++++++++++++ src/handlers.rs | 9 +++++---- src/student.rs | 7 ++++++- 5 files changed, 67 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8228d5f..5a8a201 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -193,6 +193,18 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" +[[package]] +name = "bstr" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a40b47ad93e1a5404e6c18dec46b628214fee441c70f4ab5d6942142cc268a3d" +dependencies = [ + "lazy_static", + "memchr", + "regex-automata", + "serde", +] + [[package]] name = "buf_redux" version = "0.8.4" @@ -293,6 +305,28 @@ dependencies = [ "subtle", ] +[[package]] +name = "csv" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1" +dependencies = [ + "bstr", + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" +dependencies = [ + "memchr", +] + [[package]] name = "ctor" version = "0.1.20" @@ -477,6 +511,7 @@ dependencies = [ "blake2", "block-modes", "chrono", + "csv", "hex-literal", "jsonwebtoken", "lazy_static", @@ -1300,6 +1335,12 @@ dependencies = [ "regex-syntax", ] +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" + [[package]] name = "regex-syntax" version = "0.6.23" diff --git a/Cargo.toml b/Cargo.toml index 77efd25..065cd84 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ sha2 = "0.9.3" block-modes = "0.7.0" aes = "0.6.0" askama = "0.10.5" +csv = "1.1.6" [dev-dependencies] serde_test = "1.0.117" 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