aboutsummaryrefslogtreecommitdiffstats
path: root/src/db.rs
diff options
context:
space:
mode:
authorYigit Sever2022-04-23 20:29:36 +0300
committerYigit Sever2022-04-23 20:29:36 +0300
commit21095d29f348d8c965df9871bbc0644de5000a53 (patch)
treec13444763ca514869192022fac8b861bd8663620 /src/db.rs
parentd76de7951d995026feaeb92c278f45e810acd3de (diff)
downloadgradecoin-21095d29f348d8c965df9871bbc0644de5000a53.tar.gz
gradecoin-21095d29f348d8c965df9871bbc0644de5000a53.tar.bz2
gradecoin-21095d29f348d8c965df9871bbc0644de5000a53.zip
Format, refactor, succinct errors
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs18
1 files changed, 9 insertions, 9 deletions
diff --git a/src/db.rs b/src/db.rs
index 70204a2..64be0c1 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -9,9 +9,9 @@
9//! [`Db::users`] is the in memory representation of the users, 9//! [`Db::users`] is the in memory representation of the users,
10//! with their public keys, `metu_ids` and gradecoin balances. 10//! with their public keys, `metu_ids` and gradecoin balances.
11use crate::block::{Block, Fingerprint, Id, Transaction}; 11use crate::block::{Block, Fingerprint, Id, Transaction};
12use crate::student::{MetuId, User, UserAtRest};
13use crate::config::Config; 12use crate::config::Config;
14use log::debug; 13use crate::student::{MetuId, User, UserAtRest};
14use log::info;
15use parking_lot::RwLock; 15use parking_lot::RwLock;
16use std::{collections::HashMap, fs, io, path::PathBuf, sync::Arc}; 16use std::{collections::HashMap, fs, io, path::PathBuf, sync::Arc};
17 17
@@ -32,7 +32,7 @@ impl Db {
32 // Load bots 32 // Load bots
33 let users: HashMap<Fingerprint, User> = get_friendly_users(); 33 let users: HashMap<Fingerprint, User> = get_friendly_users();
34 34
35 // Read the list of users who can register 35 // Load the list of users who can register
36 let preapproved_users = read_approved_users(&config.preapproved_users); 36 let preapproved_users = read_approved_users(&config.preapproved_users);
37 37
38 let mut db = Db { 38 let mut db = Db {
@@ -43,21 +43,21 @@ impl Db {
43 preapproved_users, 43 preapproved_users,
44 }; 44 };
45 45
46 // Read blocks 46 // Load the latest block, continue from where we left off
47 if let Some(block_path) = last_block_content(&db.config.name) { 47 if let Some(block_path) = last_block_content(&db.config.name) {
48 db.populate_with_last_block(block_path); 48 db.populate_with_last_block(block_path);
49 } 49 }
50 50
51 // Read users 51 // Load the users that had registered themselves
52 if let Ok(users_path) = read_users(&db.config.name) { 52 if let Ok(users_path) = read_users(&db.config.name) {
53 db.populate_with_users(users_path); 53 db.populate_with_users(users_path);
54 } 54 }
55 55
56 return db; 56 db
57 } 57 }
58 58
59 fn populate_with_last_block(&mut self, path: String) { 59 fn populate_with_last_block(&mut self, path: String) {
60 debug!("Populating db with the latest block {}", path); 60 info!("Populating db with the latest block {}", path);
61 let file = fs::read(path).unwrap(); 61 let file = fs::read(path).unwrap();
62 let json = std::str::from_utf8(&file).unwrap(); 62 let json = std::str::from_utf8(&file).unwrap();
63 let block: Block = serde_json::from_str(json).unwrap(); 63 let block: Block = serde_json::from_str(json).unwrap();
@@ -71,7 +71,7 @@ impl Db {
71 String::from_utf8(file_content).expect("we have written a malformed user file"); 71 String::from_utf8(file_content).expect("we have written a malformed user file");
72 let user_at_rest: UserAtRest = serde_json::from_str(&json).unwrap(); 72 let user_at_rest: UserAtRest = serde_json::from_str(&json).unwrap();
73 73
74 debug!("Populating db with user: {:?}", user_at_rest); 74 info!("Populating db with user: {:?}", user_at_rest);
75 self.users 75 self.users
76 .write() 76 .write()
77 .insert(user_at_rest.fingerprint, user_at_rest.user); 77 .insert(user_at_rest.fingerprint, user_at_rest.user);
@@ -122,7 +122,7 @@ fn read_block_name(config_name: &str) -> io::Result<Vec<PathBuf>> {
122} 122}
123 123
124fn parse_block(path: &str) -> u64 { 124fn parse_block(path: &str) -> u64 {
125 let start_pos = path.rfind("/").unwrap() + 1; 125 let start_pos = path.rfind('/').unwrap() + 1;
126 let end_pos = path.find(".block").unwrap(); 126 let end_pos = path.find(".block").unwrap();
127 let block_str = path[start_pos..end_pos].to_string(); 127 let block_str = path[start_pos..end_pos].to_string();
128 let block_u64: u64 = block_str.parse().unwrap(); 128 let block_u64: u64 = block_str.parse().unwrap();