From e9bf8a1a85d9366e59ec7989772d4e16490f1273 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Thu, 22 Apr 2021 20:15:40 +0300 Subject: [WIP] Starting to implement realnet --- src/handlers.rs | 24 ++++++++++++----------- src/schema.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 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}; use crate::PRIVATE_KEY; const BLOCK_TRANSACTION_COUNT: u8 = 1; +const REGISTER_BONUS: u16 = 40; const BLOCK_REWARD: u16 = 3; const TX_UPPER_LIMIT: u16 = 2; @@ -274,7 +275,8 @@ pub async fn authenticate_user( let new_user = User { user_id: privileged_student_id, public_key: request.public_key, - balance: 0, + balance: REGISTER_BONUS, + is_bot: false, }; debug!("New user authenticated themselves! {:?}", &new_user); @@ -284,6 +286,7 @@ pub async fn authenticate_user( user_id: new_user.user_id.clone(), public_key: new_user.public_key.clone(), balance: 0, + is_bot: false, }, fingerprint: fingerprint.clone(), }) @@ -343,7 +346,11 @@ pub async fn propose_block( warn!("New block proposal: {:?}", &new_block); if new_block.transaction_list.len() < BLOCK_TRANSACTION_COUNT as usize { - debug!("{} transactions offered, needed {}", new_block.transaction_list.len(), BLOCK_TRANSACTION_COUNT); + debug!( + "{} transactions offered, needed {}", + new_block.transaction_list.len(), + BLOCK_TRANSACTION_COUNT + ); let res_json = warp::reply::json(&GradeCoinResponse { res: ResponseType::Error, message: format!( @@ -482,13 +489,11 @@ pub async fn propose_block( // Scope the pending_transactions { - let pending_transactions = db.pending_transactions.read(); + let mut pending_transactions = db.pending_transactions.write(); let mut users_store = RwLockUpgradableReadGuard::upgrade(users_store); - let coinbase_fingerprint = new_block.transaction_list.get(0).unwrap(); - for fingerprint in new_block.transaction_list.iter() { - if let Some(transaction) = pending_transactions.get(fingerprint) { + if let Some(transaction) = pending_transactions.remove(fingerprint) { let source = &transaction.source; let target = &transaction.target; @@ -502,16 +507,13 @@ pub async fn propose_block( } } + let coinbase_fingerprint = new_block.transaction_list.get(0).unwrap(); + if let Some(coinbase_user) = users_store.get_mut(coinbase_fingerprint) { coinbase_user.balance += BLOCK_REWARD; } } - { - let mut pending_transactions = db.pending_transactions.write(); - pending_transactions.clear(); - } - let block_json = serde_json::to_string(&new_block).unwrap(); 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; use std::string::String; use std::sync::Arc; use std::vec::Vec; -// use crate::validators; pub type Fingerprint = String; +pub type Id = String; fn block_parser(path: String) -> u64 { let end_pos = path.find(".block").unwrap(); @@ -146,7 +146,7 @@ pub struct Claims { #[derive(Debug, Clone)] pub struct Db { pub blockchain: Arc>, - pub pending_transactions: Arc>>, + pub pending_transactions: Arc>>, pub users: Arc>>, } @@ -154,14 +154,51 @@ impl Db { pub fn new() -> Self { let mut users: HashMap = HashMap::new(); - let bank_acc = MetuId::new("bank".to_owned(), "P7oxDm30g1jeIId".to_owned()).unwrap(); + let friendly_1 = MetuId::new("friend_1".to_owned(), "not_used".to_owned()).unwrap(); users.insert( - "31415926535897932384626433832795028841971693993751058209749445923".to_owned(), + "cde48537ca2c28084ff560826d0e6388b7c57a51497a6cb56f397289e52ff41b".to_owned(), User { - user_id: bank_acc, - public_key: "null".to_owned(), - balance: 27 * 80, + user_id: friendly_1, + public_key: "not_used".to_owned(), + balance: 0, + is_bot: true, + }, + ); + + let friendly_2 = MetuId::new("friend_2".to_owned(), "not_used".to_owned()).unwrap(); + + users.insert( + "a1a38b5bae5866d7d998a9834229ec2f9db7a4fc8fb6f58b1115a96a446875ff".to_owned(), + User { + user_id: friendly_2, + public_key: "not_used".to_owned(), + balance: 0, + is_bot: true, + }, + ); + + let friendly_3 = MetuId::new("friend_4".to_owned(), "not_used".to_owned()).unwrap(); + + users.insert( + "4e048fd2a62f1307866086e803e9be43f78a702d5df10831fbf434e7663ae0e7".to_owned(), + User { + user_id: friendly_3, + public_key: "not_used".to_owned(), + balance: 0, + is_bot: true, + }, + ); + + let friendly_4 = MetuId::new("friend_4".to_owned(), "not_used".to_owned()).unwrap(); + + users.insert( + "60e77101e76950a9b1830fa107fd2f8fc545255b3e0f14b6a7797cf9ee005f07".to_owned(), + User { + user_id: friendly_4, + public_key: "not_used".to_owned(), + balance: 0, + is_bot: true, }, ); @@ -182,7 +219,6 @@ impl Default for Db { /// A transaction between `source` and `target` that moves `amount` #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct Transaction { - pub by: Fingerprint, pub source: Fingerprint, pub target: Fingerprint, pub amount: u16, @@ -244,6 +280,8 @@ pub struct User { pub user_id: MetuId, pub public_key: String, pub balance: u16, + #[serde(skip, default = "bool::default")] + pub is_bot: bool, } /// The values are hard coded in [`OUR_STUDENTS`] so MetuId::new() can accept/reject values based on that @@ -306,7 +344,10 @@ lazy_static! { ("e231060", "VJgziofQQPCoisH"), ("e223795", "pmcTCKox99NFsqp"), ("e223715", "1H5QuOYI1b2r9ET"), - ("bank", "P7oxDm30g1jeIId"), + ("friend_1", "not_used"), + ("friend_2", "not_used"), + ("friend_3", "not_used"), + ("friend_4", "not_used"), ] .iter() .cloned() -- cgit v1.2.3-70-g09d2