diff options
author | necrashter | 2022-04-23 15:37:37 +0300 |
---|---|---|
committer | Yigit Sever | 2022-04-23 18:10:12 +0300 |
commit | 85dc0418b8b3b7e6d10a4ae85878a921b67c9baf (patch) | |
tree | 7d61cea14546b91fcb857bed7a2292bcbcff5d86 | |
parent | 2541f5f849e25d7a5483c5715ef2e84945016637 (diff) | |
download | gradecoin-85dc0418b8b3b7e6d10a4ae85878a921b67c9baf.tar.gz gradecoin-85dc0418b8b3b7e6d10a4ae85878a921b67c9baf.tar.bz2 gradecoin-85dc0418b8b3b7e6d10a4ae85878a921b67c9baf.zip |
Save different networks to different folders
-rw-r--r-- | src/db.rs | 22 | ||||
-rw-r--r-- | src/handlers.rs | 7 |
2 files changed, 17 insertions, 12 deletions
@@ -28,14 +28,14 @@ pub struct Db { | |||
28 | 28 | ||
29 | impl Db { | 29 | impl Db { |
30 | pub fn new(config: Config) -> Self { | 30 | pub fn new(config: Config) -> Self { |
31 | fs::create_dir_all("blocks").unwrap(); | 31 | fs::create_dir_all(format!("blocks/{}", config.name)).unwrap(); |
32 | fs::create_dir_all("users").unwrap(); | 32 | fs::create_dir_all(format!("users/{}", config.name)).unwrap(); |
33 | let mut db = Db::default(); | 33 | let mut db = Db::default(); |
34 | if let Some(block_path) = last_block_content() { | 34 | if let Some(block_path) = last_block_content(&config.name) { |
35 | db.populate_with_last_block(block_path); | 35 | db.populate_with_last_block(block_path); |
36 | } | 36 | } |
37 | 37 | ||
38 | if let Ok(users_path) = read_users() { | 38 | if let Ok(users_path) = read_users(&config.name) { |
39 | db.populate_with_users(users_path); | 39 | db.populate_with_users(users_path); |
40 | } | 40 | } |
41 | 41 | ||
@@ -85,8 +85,8 @@ impl Db { | |||
85 | } | 85 | } |
86 | } | 86 | } |
87 | 87 | ||
88 | fn last_block_content() -> Option<String> { | 88 | fn last_block_content(config_name: &str) -> Option<String> { |
89 | let blocks = read_block_name().unwrap(); | 89 | let blocks = read_block_name(config_name).unwrap(); |
90 | 90 | ||
91 | if blocks.is_empty() { | 91 | if blocks.is_empty() { |
92 | return None; | 92 | return None; |
@@ -107,8 +107,9 @@ fn last_block_content() -> Option<String> { | |||
107 | return Some(blocks[last_block_index].to_str().unwrap().parse().unwrap()); | 107 | return Some(blocks[last_block_index].to_str().unwrap().parse().unwrap()); |
108 | } | 108 | } |
109 | 109 | ||
110 | fn read_block_name() -> io::Result<Vec<PathBuf>> { | 110 | fn read_block_name(config_name: &str) -> io::Result<Vec<PathBuf>> { |
111 | let entries = fs::read_dir("./blocks")? | 111 | let path = format!("./blocks/{}", config_name); |
112 | let entries = fs::read_dir(path)? | ||
112 | .map(|res| res.map(|e| e.path())) | 113 | .map(|res| res.map(|e| e.path())) |
113 | .collect::<Result<Vec<_>, io::Error>>()?; | 114 | .collect::<Result<Vec<_>, io::Error>>()?; |
114 | 115 | ||
@@ -122,8 +123,9 @@ fn parse_block(path: &str) -> u64 { | |||
122 | block_u64 | 123 | block_u64 |
123 | } | 124 | } |
124 | 125 | ||
125 | fn read_users() -> io::Result<Vec<PathBuf>> { | 126 | fn read_users(config_name: &str) -> io::Result<Vec<PathBuf>> { |
126 | let entries = fs::read_dir("./users")? | 127 | let path = format!("./users/{}", config_name); |
128 | let entries = fs::read_dir(path)? | ||
127 | .map(|res| res.map(|e| e.path())) | 129 | .map(|res| res.map(|e| e.path())) |
128 | .collect::<Result<Vec<_>, io::Error>>()?; | 130 | .collect::<Result<Vec<_>, io::Error>>()?; |
129 | 131 | ||
diff --git a/src/handlers.rs b/src/handlers.rs index f80aa60..d2a834f 100644 --- a/src/handlers.rs +++ b/src/handlers.rs | |||
@@ -333,7 +333,10 @@ pub async fn authenticate_user( | |||
333 | }) | 333 | }) |
334 | .unwrap(); | 334 | .unwrap(); |
335 | 335 | ||
336 | fs::write(format!("users/{}.guy", new_user.user_id), user_at_rest_json).unwrap(); | 336 | fs::write( |
337 | format!("users/{}/{}.guy", db.config.name, new_user.user_id), | ||
338 | user_at_rest_json | ||
339 | ).unwrap(); | ||
337 | 340 | ||
338 | let mut userlist = db.users.write(); | 341 | let mut userlist = db.users.write(); |
339 | userlist.insert(fingerprint.clone(), new_user); | 342 | userlist.insert(fingerprint.clone(), new_user); |
@@ -601,7 +604,7 @@ pub async fn propose_block( | |||
601 | let block_json = serde_json::to_string(&new_block).unwrap(); | 604 | let block_json = serde_json::to_string(&new_block).unwrap(); |
602 | 605 | ||
603 | fs::write( | 606 | fs::write( |
604 | format!("blocks/{}.block", new_block.timestamp.timestamp()), | 607 | format!("blocks/{}/{}.block", db.config.name, new_block.timestamp.timestamp()), |
605 | block_json, | 608 | block_json, |
606 | ) | 609 | ) |
607 | .unwrap(); | 610 | .unwrap(); |