diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/schema.rs | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/src/schema.rs b/src/schema.rs index 4ac674d..6402724 100644 --- a/src/schema.rs +++ b/src/schema.rs | |||
| @@ -14,15 +14,16 @@ use serde::{Deserialize, Serialize}; | |||
| 14 | use std::collections::{HashMap, HashSet}; | 14 | use std::collections::{HashMap, HashSet}; |
| 15 | use std::fmt; | 15 | use std::fmt; |
| 16 | use std::fs; | 16 | use std::fs; |
| 17 | use std::sync::Arc; | ||
| 18 | use std::io; | 17 | use std::io; |
| 19 | use std::vec::Vec; | ||
| 20 | use std::string::String; | ||
| 21 | use std::path::PathBuf; | 18 | use std::path::PathBuf; |
| 19 | use std::string::String; | ||
| 20 | use std::sync::Arc; | ||
| 21 | use std::vec::Vec; | ||
| 22 | // use crate::validators; | 22 | // use crate::validators; |
| 23 | 23 | ||
| 24 | pub type PublicKeySignature = String; | 24 | pub type Fingerprint = String; |
| 25 | 25 | ||
| 26 | // TODO: start by reading users from file too <14-04-21, yigit> // | ||
| 26 | 27 | ||
| 27 | fn last_block_exists() -> (bool, String) { | 28 | fn last_block_exists() -> (bool, String) { |
| 28 | let blocks = read_block_name().unwrap(); | 29 | let blocks = read_block_name().unwrap(); |
| @@ -40,13 +41,6 @@ fn read_block_name() -> io::Result<Vec<PathBuf>> { | |||
| 40 | .map(|res| res.map(|e| e.path())) | 41 | .map(|res| res.map(|e| e.path())) |
| 41 | .collect::<Result<Vec<_>, io::Error>>()?; | 42 | .collect::<Result<Vec<_>, io::Error>>()?; |
| 42 | 43 | ||
| 43 | // The order in which `read_dir` returns entries is not guaranteed. If reproducible | ||
| 44 | // ordering is required the entries should be explicitly sorted. | ||
| 45 | |||
| 46 | entries.sort(); | ||
| 47 | |||
| 48 | // The entries have now been sorted by their path. | ||
| 49 | |||
| 50 | Ok(entries) | 44 | Ok(entries) |
| 51 | } | 45 | } |
| 52 | 46 | ||
| @@ -58,7 +52,8 @@ fn create_db_with_last_block(path: String) -> Db { | |||
| 58 | *db.blockchain.write() = block; | 52 | *db.blockchain.write() = block; |
| 59 | return db; | 53 | return db; |
| 60 | } | 54 | } |
| 61 | /// Creates a new database | 55 | |
| 56 | /// Creates a new database, uses the previous last block if one exists | ||
| 62 | pub fn create_database() -> Db { | 57 | pub fn create_database() -> Db { |
| 63 | fs::create_dir_all("blocks").unwrap(); | 58 | fs::create_dir_all("blocks").unwrap(); |
| 64 | fs::create_dir_all("users").unwrap(); | 59 | fs::create_dir_all("users").unwrap(); |
| @@ -96,7 +91,7 @@ pub struct Claims { | |||
| 96 | /// gradecoin balances. | 91 | /// gradecoin balances. |
| 97 | /// | 92 | /// |
| 98 | /// TODO: Replace the pending_transactions HashMap<String, Transaction> with | 93 | /// TODO: Replace the pending_transactions HashMap<String, Transaction> with |
| 99 | /// HashMap<PublicKeySignature, Transaction> | 94 | /// HashMap<Fingerprint, Transaction> |
| 100 | #[derive(Debug, Clone)] | 95 | #[derive(Debug, Clone)] |
| 101 | pub struct Db { | 96 | pub struct Db { |
| 102 | pub blockchain: Arc<RwLock<Block>>, | 97 | pub blockchain: Arc<RwLock<Block>>, |
| @@ -117,9 +112,9 @@ impl Db { | |||
| 117 | /// A transaction between `source` and `target` that moves `amount` | 112 | /// A transaction between `source` and `target` that moves `amount` |
| 118 | #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] | 113 | #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] |
| 119 | pub struct Transaction { | 114 | pub struct Transaction { |
| 120 | pub by: PublicKeySignature, | 115 | pub by: Fingerprint, |
| 121 | pub source: PublicKeySignature, | 116 | pub source: Fingerprint, |
| 122 | pub target: PublicKeySignature, | 117 | pub target: Fingerprint, |
| 123 | pub amount: i32, | 118 | pub amount: i32, |
| 124 | pub timestamp: NaiveDateTime, | 119 | pub timestamp: NaiveDateTime, |
| 125 | } | 120 | } |
| @@ -136,7 +131,7 @@ pub struct Transaction { | |||
| 136 | #[derive(Serialize, Deserialize, Debug, PartialEq)] | 131 | #[derive(Serialize, Deserialize, Debug, PartialEq)] |
| 137 | pub struct Block { | 132 | pub struct Block { |
| 138 | #[serde(skip_serializing_if = "Vec::is_empty")] | 133 | #[serde(skip_serializing_if = "Vec::is_empty")] |
| 139 | pub transaction_list: Vec<PublicKeySignature>, | 134 | pub transaction_list: Vec<Fingerprint>, |
| 140 | pub nonce: u32, | 135 | pub nonce: u32, |
| 141 | pub timestamp: NaiveDateTime, | 136 | pub timestamp: NaiveDateTime, |
| 142 | pub hash: String, | 137 | pub hash: String, |
| @@ -146,7 +141,7 @@ pub struct Block { | |||
| 146 | #[derive(Serialize, Deserialize, Debug, PartialEq)] | 141 | #[derive(Serialize, Deserialize, Debug, PartialEq)] |
| 147 | pub struct NakedBlock { | 142 | pub struct NakedBlock { |
| 148 | #[serde(skip_serializing_if = "Vec::is_empty", default)] | 143 | #[serde(skip_serializing_if = "Vec::is_empty", default)] |
| 149 | pub transaction_list: Vec<PublicKeySignature>, | 144 | pub transaction_list: Vec<Fingerprint>, |
| 150 | pub nonce: u32, | 145 | pub nonce: u32, |
| 151 | pub timestamp: NaiveDateTime, | 146 | pub timestamp: NaiveDateTime, |
| 152 | } | 147 | } |
| @@ -240,7 +235,10 @@ impl fmt::Display for MetuId { | |||
| 240 | impl MetuId { | 235 | impl MetuId { |
| 241 | pub fn new(id: String, pwd: String) -> Option<Self> { | 236 | pub fn new(id: String, pwd: String) -> Option<Self> { |
| 242 | if OUR_STUDENTS.contains(&(&*id, &*pwd)) { | 237 | if OUR_STUDENTS.contains(&(&*id, &*pwd)) { |
| 243 | Some(MetuId { id: id, passwd: pwd }) | 238 | Some(MetuId { |
| 239 | id: id, | ||
| 240 | passwd: pwd, | ||
| 241 | }) | ||
| 244 | } else { | 242 | } else { |
| 245 | None | 243 | None |
| 246 | } | 244 | } |
