summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/schema.rs36
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};
14use std::collections::{HashMap, HashSet}; 14use std::collections::{HashMap, HashSet};
15use std::fmt; 15use std::fmt;
16use std::fs; 16use std::fs;
17use std::sync::Arc;
18use std::io; 17use std::io;
19use std::vec::Vec;
20use std::string::String;
21use std::path::PathBuf; 18use std::path::PathBuf;
19use std::string::String;
20use std::sync::Arc;
21use std::vec::Vec;
22// use crate::validators; 22// use crate::validators;
23 23
24pub type PublicKeySignature = String; 24pub type Fingerprint = String;
25 25
26// TODO: start by reading users from file too <14-04-21, yigit> //
26 27
27fn last_block_exists() -> (bool, String) { 28fn 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
62pub fn create_database() -> Db { 57pub 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)]
101pub struct Db { 96pub 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)]
119pub struct Transaction { 114pub 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)]
137pub struct Block { 132pub 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)]
147pub struct NakedBlock { 142pub 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 {
240impl MetuId { 235impl 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 }