aboutsummaryrefslogtreecommitdiffstats
path: root/src/db.rs
diff options
context:
space:
mode:
authorYigit Sever2022-04-16 18:06:37 +0300
committerYigit Sever2022-04-16 18:06:37 +0300
commit3224b9fdd9174e51eb3e9842ce5abccf735abdfd (patch)
treed752246c4506ef586fba18d9026357de44a386ef /src/db.rs
parent43ad693490d3c2770bdcad7f59be50d6f1aa79dc (diff)
downloadgradecoin-3224b9fdd9174e51eb3e9842ce5abccf735abdfd.tar.gz
gradecoin-3224b9fdd9174e51eb3e9842ce5abccf735abdfd.tar.bz2
gradecoin-3224b9fdd9174e51eb3e9842ce5abccf735abdfd.zip
final touches for 2022 spring
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs39
1 files changed, 29 insertions, 10 deletions
diff --git a/src/db.rs b/src/db.rs
index fd5c1be..f2921f8 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -14,13 +14,14 @@ use log::debug;
14use parking_lot::RwLock; 14use parking_lot::RwLock;
15use std::{collections::HashMap, fs, io, path::PathBuf, sync::Arc}; 15use std::{collections::HashMap, fs, io, path::PathBuf, sync::Arc};
16 16
17const PREAPPROVED_STU_FILENAME: &str = "students.csv";
18
17#[derive(Debug, Clone, Default)] 19#[derive(Debug, Clone, Default)]
18pub struct Db { 20pub struct Db {
19 pub blockchain: Arc<RwLock<Block>>, 21 pub blockchain: Arc<RwLock<Block>>,
20 pub pending_transactions: Arc<RwLock<HashMap<Id, Transaction>>>, 22 pub pending_transactions: Arc<RwLock<HashMap<Id, Transaction>>>,
21 pub users: Arc<RwLock<HashMap<Fingerprint, User>>>, 23 pub users: Arc<RwLock<HashMap<Fingerprint, User>>>,
22 approved_users: Vec<MetuId>, 24 preapproved_users: Vec<MetuId>,
23 // TODO: metu_ids or approved_users or something, metu_id struct <11-04-22, yigit> //
24} 25}
25 26
26impl Db { 27impl Db {
@@ -37,13 +38,13 @@ impl Db {
37 } 38 }
38 39
39 let users: HashMap<Fingerprint, User> = get_friendly_users(); 40 let users: HashMap<Fingerprint, User> = get_friendly_users();
40 let approved_users = read_approved_users(); 41 let preapproved_users = read_approved_users();
41 42
42 Db { 43 Db {
43 blockchain: Arc::new(RwLock::new(Block::default())), 44 blockchain: Arc::new(RwLock::new(Block::default())),
44 pending_transactions: Arc::new(RwLock::new(HashMap::new())), 45 pending_transactions: Arc::new(RwLock::new(HashMap::new())),
45 users: Arc::new(RwLock::new(users)), 46 users: Arc::new(RwLock::new(users)),
46 approved_users, 47 preapproved_users,
47 } 48 }
48 } 49 }
49 50
@@ -69,6 +70,16 @@ impl Db {
69 } 70 }
70 } 71 }
71 } 72 }
73
74 pub fn is_user_preapproved(&self, id: &Id, passwd: &String) -> bool {
75 for user in &self.preapproved_users {
76 if *user.get_id() == *id && *user.get_passwd() == *passwd {
77 return true;
78 }
79 }
80
81 false
82 }
72} 83}
73 84
74fn last_block_content() -> Option<String> { 85fn last_block_content() -> Option<String> {
@@ -122,7 +133,7 @@ fn get_friendly_users() -> HashMap<Fingerprint, User> {
122 users.insert( 133 users.insert(
123 "cde48537ca2c28084ff560826d0e6388b7c57a51497a6cb56f397289e52ff41b".to_owned(), 134 "cde48537ca2c28084ff560826d0e6388b7c57a51497a6cb56f397289e52ff41b".to_owned(),
124 User { 135 User {
125 user_id: MetuId::new("friend_1".to_owned(), "not_used".to_owned()).unwrap(), 136 user_id: MetuId::new("friend_1".to_owned(), "not_used".to_owned()),
126 public_key: "not_used".to_owned(), 137 public_key: "not_used".to_owned(),
127 balance: 70, 138 balance: 70,
128 is_bot: true, 139 is_bot: true,
@@ -132,7 +143,7 @@ fn get_friendly_users() -> HashMap<Fingerprint, User> {
132 users.insert( 143 users.insert(
133 "a1a38b5bae5866d7d998a9834229ec2f9db7a4fc8fb6f58b1115a96a446875ff".to_owned(), 144 "a1a38b5bae5866d7d998a9834229ec2f9db7a4fc8fb6f58b1115a96a446875ff".to_owned(),
134 User { 145 User {
135 user_id: MetuId::new("friend_2".to_owned(), "not_used".to_owned()).unwrap(), 146 user_id: MetuId::new("friend_2".to_owned(), "not_used".to_owned()),
136 public_key: "not_used".to_owned(), 147 public_key: "not_used".to_owned(),
137 balance: 20, 148 balance: 20,
138 is_bot: true, 149 is_bot: true,
@@ -142,7 +153,7 @@ fn get_friendly_users() -> HashMap<Fingerprint, User> {
142 users.insert( 153 users.insert(
143 "4e048fd2a62f1307866086e803e9be43f78a702d5df10831fbf434e7663ae0e7".to_owned(), 154 "4e048fd2a62f1307866086e803e9be43f78a702d5df10831fbf434e7663ae0e7".to_owned(),
144 User { 155 User {
145 user_id: MetuId::new("friend_4".to_owned(), "not_used".to_owned()).unwrap(), 156 user_id: MetuId::new("friend_4".to_owned(), "not_used".to_owned()),
146 public_key: "not_used".to_owned(), 157 public_key: "not_used".to_owned(),
147 balance: 120, 158 balance: 120,
148 is_bot: true, 159 is_bot: true,
@@ -152,7 +163,7 @@ fn get_friendly_users() -> HashMap<Fingerprint, User> {
152 users.insert( 163 users.insert(
153 "60e77101e76950a9b1830fa107fd2f8fc545255b3e0f14b6a7797cf9ee005f07".to_owned(), 164 "60e77101e76950a9b1830fa107fd2f8fc545255b3e0f14b6a7797cf9ee005f07".to_owned(),
154 User { 165 User {
155 user_id: MetuId::new("friend_4".to_owned(), "not_used".to_owned()).unwrap(), 166 user_id: MetuId::new("friend_4".to_owned(), "not_used".to_owned()),
156 public_key: "not_used".to_owned(), 167 public_key: "not_used".to_owned(),
157 balance: 40, 168 balance: 40,
158 is_bot: true, 169 is_bot: true,
@@ -163,11 +174,19 @@ fn get_friendly_users() -> HashMap<Fingerprint, User> {
163 174
164fn read_approved_users() -> Vec<MetuId> { 175fn read_approved_users() -> Vec<MetuId> {
165 let mut approved_students: Vec<MetuId> = Vec::new(); 176 let mut approved_students: Vec<MetuId> = Vec::new();
166 let contents = fs::read_to_string("students.csv").unwrap(); 177 let contents = fs::read_to_string(PREAPPROVED_STU_FILENAME).unwrap_or_else(|_| {
178 panic!(
179 "{}",
180 format!(
181 "Expected {} to load preapproved students",
182 PREAPPROVED_STU_FILENAME
183 )
184 )
185 });
167 let mut reader = csv::Reader::from_reader(contents.as_bytes()); 186 let mut reader = csv::Reader::from_reader(contents.as_bytes());
168 for student in reader.records() { 187 for student in reader.records() {
169 let student = student.unwrap(); 188 let student = student.unwrap();
170 approved_students.push(MetuId::_new(student[0].to_owned(), student[1].to_owned())); 189 approved_students.push(MetuId::new(student[0].to_owned(), student[1].to_owned()));
171 } 190 }
172 approved_students 191 approved_students
173} 192}