aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornecrashter2022-04-23 15:51:18 +0300
committerYigit Sever2022-04-23 18:10:12 +0300
commitbcf7a379620ca433215ad9617ad9417b4f39e87d (patch)
tree2917850dec4cf8b646147e6b07e689c912e5c0d4
parent9d6eb613d489d755f0d3b5b18b4df6a363f68803 (diff)
downloadgradecoin-bcf7a379620ca433215ad9617ad9417b4f39e87d.tar.gz
gradecoin-bcf7a379620ca433215ad9617ad9417b4f39e87d.tar.bz2
gradecoin-bcf7a379620ca433215ad9617ad9417b4f39e87d.zip
Fix failure to read users after restart
db would get overwritten
-rw-r--r--src/db.rs25
1 files changed, 16 insertions, 9 deletions
diff --git a/src/db.rs b/src/db.rs
index ad4724c..e7fb137 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -30,25 +30,32 @@ impl Db {
30 pub fn new(config: Config) -> Self { 30 pub fn new(config: Config) -> Self {
31 fs::create_dir_all(format!("blocks/{}", config.name)).unwrap(); 31 fs::create_dir_all(format!("blocks/{}", config.name)).unwrap();
32 fs::create_dir_all(format!("users/{}", config.name)).unwrap(); 32 fs::create_dir_all(format!("users/{}", config.name)).unwrap();
33 let mut db = Db::default();
34 if let Some(block_path) = last_block_content(&config.name) {
35 db.populate_with_last_block(block_path);
36 }
37
38 if let Ok(users_path) = read_users(&config.name) {
39 db.populate_with_users(users_path);
40 }
41 33
34 // Load bots
42 let users: HashMap<Fingerprint, User> = get_friendly_users(); 35 let users: HashMap<Fingerprint, User> = get_friendly_users();
36
37 // Read the list of users who can register
43 let preapproved_users = read_approved_users(); 38 let preapproved_users = read_approved_users();
44 39
45 Db { 40 let mut db = Db {
46 blockchain: Arc::new(RwLock::new(Block::default())), 41 blockchain: Arc::new(RwLock::new(Block::default())),
47 pending_transactions: Arc::new(RwLock::new(HashMap::new())), 42 pending_transactions: Arc::new(RwLock::new(HashMap::new())),
48 users: Arc::new(RwLock::new(users)), 43 users: Arc::new(RwLock::new(users)),
49 config, 44 config,
50 preapproved_users, 45 preapproved_users,
46 };
47
48 // Read blocks
49 if let Some(block_path) = last_block_content(&db.config.name) {
50 db.populate_with_last_block(block_path);
51 }
52
53 // Read users
54 if let Ok(users_path) = read_users(&db.config.name) {
55 db.populate_with_users(users_path);
51 } 56 }
57
58 return db;
52 } 59 }
53 60
54 fn populate_with_last_block(&mut self, path: String) { 61 fn populate_with_last_block(&mut self, path: String) {