aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/handlers.rs24
-rw-r--r--src/schema.rs59
2 files changed, 63 insertions, 20 deletions
diff --git a/src/handlers.rs b/src/handlers.rs
index bf554ab..ca41b61 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -19,6 +19,7 @@ use warp::{http::StatusCode, reply};
19 19
20use crate::PRIVATE_KEY; 20use crate::PRIVATE_KEY;
21const BLOCK_TRANSACTION_COUNT: u8 = 1; 21const BLOCK_TRANSACTION_COUNT: u8 = 1;
22const REGISTER_BONUS: u16 = 40;
22const BLOCK_REWARD: u16 = 3; 23const BLOCK_REWARD: u16 = 3;
23const TX_UPPER_LIMIT: u16 = 2; 24const TX_UPPER_LIMIT: u16 = 2;
24 25
@@ -274,7 +275,8 @@ pub async fn authenticate_user(
274 let new_user = User { 275 let new_user = User {
275 user_id: privileged_student_id, 276 user_id: privileged_student_id,
276 public_key: request.public_key, 277 public_key: request.public_key,
277 balance: 0, 278 balance: REGISTER_BONUS,
279 is_bot: false,
278 }; 280 };
279 281
280 debug!("New user authenticated themselves! {:?}", &new_user); 282 debug!("New user authenticated themselves! {:?}", &new_user);
@@ -284,6 +286,7 @@ pub async fn authenticate_user(
284 user_id: new_user.user_id.clone(), 286 user_id: new_user.user_id.clone(),
285 public_key: new_user.public_key.clone(), 287 public_key: new_user.public_key.clone(),
286 balance: 0, 288 balance: 0,
289 is_bot: false,
287 }, 290 },
288 fingerprint: fingerprint.clone(), 291 fingerprint: fingerprint.clone(),
289 }) 292 })
@@ -343,7 +346,11 @@ pub async fn propose_block(
343 warn!("New block proposal: {:?}", &new_block); 346 warn!("New block proposal: {:?}", &new_block);
344 347
345 if new_block.transaction_list.len() < BLOCK_TRANSACTION_COUNT as usize { 348 if new_block.transaction_list.len() < BLOCK_TRANSACTION_COUNT as usize {
346 debug!("{} transactions offered, needed {}", new_block.transaction_list.len(), BLOCK_TRANSACTION_COUNT); 349 debug!(
350 "{} transactions offered, needed {}",
351 new_block.transaction_list.len(),
352 BLOCK_TRANSACTION_COUNT
353 );
347 let res_json = warp::reply::json(&GradeCoinResponse { 354 let res_json = warp::reply::json(&GradeCoinResponse {
348 res: ResponseType::Error, 355 res: ResponseType::Error,
349 message: format!( 356 message: format!(
@@ -482,13 +489,11 @@ pub async fn propose_block(
482 489
483 // Scope the pending_transactions 490 // Scope the pending_transactions
484 { 491 {
485 let pending_transactions = db.pending_transactions.read(); 492 let mut pending_transactions = db.pending_transactions.write();
486 let mut users_store = RwLockUpgradableReadGuard::upgrade(users_store); 493 let mut users_store = RwLockUpgradableReadGuard::upgrade(users_store);
487 494
488 let coinbase_fingerprint = new_block.transaction_list.get(0).unwrap();
489
490 for fingerprint in new_block.transaction_list.iter() { 495 for fingerprint in new_block.transaction_list.iter() {
491 if let Some(transaction) = pending_transactions.get(fingerprint) { 496 if let Some(transaction) = pending_transactions.remove(fingerprint) {
492 let source = &transaction.source; 497 let source = &transaction.source;
493 let target = &transaction.target; 498 let target = &transaction.target;
494 499
@@ -502,16 +507,13 @@ pub async fn propose_block(
502 } 507 }
503 } 508 }
504 509
510 let coinbase_fingerprint = new_block.transaction_list.get(0).unwrap();
511
505 if let Some(coinbase_user) = users_store.get_mut(coinbase_fingerprint) { 512 if let Some(coinbase_user) = users_store.get_mut(coinbase_fingerprint) {
506 coinbase_user.balance += BLOCK_REWARD; 513 coinbase_user.balance += BLOCK_REWARD;
507 } 514 }
508 } 515 }
509 516
510 {
511 let mut pending_transactions = db.pending_transactions.write();
512 pending_transactions.clear();
513 }
514
515 let block_json = serde_json::to_string(&new_block).unwrap(); 517 let block_json = serde_json::to_string(&new_block).unwrap();
516 518
517 fs::write( 519 fs::write(
diff --git a/src/schema.rs b/src/schema.rs
index 40c6329..b855b1c 100644
--- a/src/schema.rs
+++ b/src/schema.rs
@@ -20,9 +20,9 @@ use std::path::PathBuf;
20use std::string::String; 20use std::string::String;
21use std::sync::Arc; 21use std::sync::Arc;
22use std::vec::Vec; 22use std::vec::Vec;
23// use crate::validators;
24 23
25pub type Fingerprint = String; 24pub type Fingerprint = String;
25pub type Id = String;
26 26
27fn block_parser(path: String) -> u64 { 27fn block_parser(path: String) -> u64 {
28 let end_pos = path.find(".block").unwrap(); 28 let end_pos = path.find(".block").unwrap();
@@ -146,7 +146,7 @@ pub struct Claims {
146#[derive(Debug, Clone)] 146#[derive(Debug, Clone)]
147pub struct Db { 147pub struct Db {
148 pub blockchain: Arc<RwLock<Block>>, 148 pub blockchain: Arc<RwLock<Block>>,
149 pub pending_transactions: Arc<RwLock<HashMap<Fingerprint, Transaction>>>, 149 pub pending_transactions: Arc<RwLock<HashMap<Id, Transaction>>>,
150 pub users: Arc<RwLock<HashMap<Fingerprint, User>>>, 150 pub users: Arc<RwLock<HashMap<Fingerprint, User>>>,
151} 151}
152 152
@@ -154,14 +154,51 @@ impl Db {
154 pub fn new() -> Self { 154 pub fn new() -> Self {
155 let mut users: HashMap<Fingerprint, User> = HashMap::new(); 155 let mut users: HashMap<Fingerprint, User> = HashMap::new();
156 156
157 let bank_acc = MetuId::new("bank".to_owned(), "P7oxDm30g1jeIId".to_owned()).unwrap(); 157 let friendly_1 = MetuId::new("friend_1".to_owned(), "not_used".to_owned()).unwrap();
158 158
159 users.insert( 159 users.insert(
160 "31415926535897932384626433832795028841971693993751058209749445923".to_owned(), 160 "cde48537ca2c28084ff560826d0e6388b7c57a51497a6cb56f397289e52ff41b".to_owned(),
161 User { 161 User {
162 user_id: bank_acc, 162 user_id: friendly_1,
163 public_key: "null".to_owned(), 163 public_key: "not_used".to_owned(),
164 balance: 27 * 80, 164 balance: 0,
165 is_bot: true,
166 },
167 );
168
169 let friendly_2 = MetuId::new("friend_2".to_owned(), "not_used".to_owned()).unwrap();
170
171 users.insert(
172 "a1a38b5bae5866d7d998a9834229ec2f9db7a4fc8fb6f58b1115a96a446875ff".to_owned(),
173 User {
174 user_id: friendly_2,
175 public_key: "not_used".to_owned(),
176 balance: 0,
177 is_bot: true,
178 },
179 );
180
181 let friendly_3 = MetuId::new("friend_4".to_owned(), "not_used".to_owned()).unwrap();
182
183 users.insert(
184 "4e048fd2a62f1307866086e803e9be43f78a702d5df10831fbf434e7663ae0e7".to_owned(),
185 User {
186 user_id: friendly_3,
187 public_key: "not_used".to_owned(),
188 balance: 0,
189 is_bot: true,
190 },
191 );
192
193 let friendly_4 = MetuId::new("friend_4".to_owned(), "not_used".to_owned()).unwrap();
194
195 users.insert(
196 "60e77101e76950a9b1830fa107fd2f8fc545255b3e0f14b6a7797cf9ee005f07".to_owned(),
197 User {
198 user_id: friendly_4,
199 public_key: "not_used".to_owned(),
200 balance: 0,
201 is_bot: true,
165 }, 202 },
166 ); 203 );
167 204
@@ -182,7 +219,6 @@ impl Default for Db {
182/// A transaction between `source` and `target` that moves `amount` 219/// A transaction between `source` and `target` that moves `amount`
183#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] 220#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
184pub struct Transaction { 221pub struct Transaction {
185 pub by: Fingerprint,
186 pub source: Fingerprint, 222 pub source: Fingerprint,
187 pub target: Fingerprint, 223 pub target: Fingerprint,
188 pub amount: u16, 224 pub amount: u16,
@@ -244,6 +280,8 @@ pub struct User {
244 pub user_id: MetuId, 280 pub user_id: MetuId,
245 pub public_key: String, 281 pub public_key: String,
246 pub balance: u16, 282 pub balance: u16,
283 #[serde(skip, default = "bool::default")]
284 pub is_bot: bool,
247} 285}
248 286
249/// The values are hard coded in [`OUR_STUDENTS`] so MetuId::new() can accept/reject values based on that 287/// The values are hard coded in [`OUR_STUDENTS`] so MetuId::new() can accept/reject values based on that
@@ -306,7 +344,10 @@ lazy_static! {
306 ("e231060", "VJgziofQQPCoisH"), 344 ("e231060", "VJgziofQQPCoisH"),
307 ("e223795", "pmcTCKox99NFsqp"), 345 ("e223795", "pmcTCKox99NFsqp"),
308 ("e223715", "1H5QuOYI1b2r9ET"), 346 ("e223715", "1H5QuOYI1b2r9ET"),
309 ("bank", "P7oxDm30g1jeIId"), 347 ("friend_1", "not_used"),
348 ("friend_2", "not_used"),
349 ("friend_3", "not_used"),
350 ("friend_4", "not_used"),
310 ] 351 ]
311 .iter() 352 .iter()
312 .cloned() 353 .cloned()