summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authoralpaylan2021-04-16 01:02:16 +0300
committeralpaylan2021-04-16 01:02:16 +0300
commit82864341afc78b23b358cd775c70ffbfa0d0303f (patch)
tree1a1367d0036d60a4197c46706bb754c32eb37145 /src
parentbddea30ecb76f0c4805758d3b36aab52f176ff9b (diff)
downloadgradecoin-82864341afc78b23b358cd775c70ffbfa0d0303f.tar.gz
gradecoin-82864341afc78b23b358cd775c70ffbfa0d0303f.tar.bz2
gradecoin-82864341afc78b23b358cd775c70ffbfa0d0303f.zip
change last block reading method.
Diffstat (limited to 'src')
-rw-r--r--src/schema.rs31
1 files changed, 25 insertions, 6 deletions
diff --git a/src/schema.rs b/src/schema.rs
index af10b4d..264d2bd 100644
--- a/src/schema.rs
+++ b/src/schema.rs
@@ -25,15 +25,34 @@ pub type Fingerprint = String;
25 25
26// TODO: start by reading users from file too <14-04-21, yigit> // 26// TODO: start by reading users from file too <14-04-21, yigit> //
27 27
28fn last_block_exists() -> (bool, String) { 28fn block_parser(path: String) -> u64 {
29 let end_pos = path.find(".block").unwrap();
30 let block_str = path[9..end_pos].to_string();
31 let block_u64 : u64 = block_str.parse().unwrap();
32 block_u64
33}
34
35fn last_block_content() -> (bool, String) {
29 let blocks = read_block_name().unwrap(); 36 let blocks = read_block_name().unwrap();
30 for block in blocks { 37
38 if blocks.len() == 0 {
39 return (false, "".to_string());
40 }
41
42 let last_block = blocks[0].to_str().unwrap();
43 let mut last_block = block_parser(last_block.to_string());
44 let mut last_block_index = 0;
45
46 for (index, block) in blocks.iter().enumerate() {
31 let block = block.to_str().unwrap(); 47 let block = block.to_str().unwrap();
32 if block.contains("last.block") { 48 let block = block_parser(block.to_string());
33 return (true, block.to_string()); 49 if block > last_block {
50 last_block = block;
51 last_block_index = index;
34 } 52 }
35 } 53 }
36 (false, "".to_string()) 54 return (true, blocks[last_block_index].to_str().unwrap().parse().unwrap());
55
37} 56}
38 57
39fn read_block_name() -> io::Result<Vec<PathBuf>> { 58fn read_block_name() -> io::Result<Vec<PathBuf>> {
@@ -57,7 +76,7 @@ fn create_db_with_last_block(path: String) -> Db {
57pub fn create_database() -> Db { 76pub fn create_database() -> Db {
58 fs::create_dir_all("blocks").unwrap(); 77 fs::create_dir_all("blocks").unwrap();
59 fs::create_dir_all("users").unwrap(); 78 fs::create_dir_all("users").unwrap();
60 let (res, path) = last_block_exists(); 79 let (res, path) = last_block_content();
61 if res { 80 if res {
62 return create_db_with_last_block(path); 81 return create_db_with_last_block(path);
63 } else { 82 } else {