From 166d80b7938970dae7d8cf24805a329e0770d77f Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Fri, 16 Apr 2021 02:41:17 +0300 Subject: Fix deadlock --- src/handlers.rs | 11 ++++++----- tests/route_tests.rs | 56 ++++++++++++++++++++++++++-------------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/handlers.rs b/src/handlers.rs index 482f5b1..e63933d 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -8,6 +8,7 @@ use jsonwebtoken::errors::ErrorKind; use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation}; use log::{debug, warn}; use md5::Md5; +use parking_lot::RwLockUpgradableReadGuard; use rsa::{PaddingScheme, RSAPrivateKey}; use serde::Serialize; use sha2::Sha256; @@ -334,7 +335,7 @@ pub async fn authorized_propose_block( let users_store = db.users.upgradable_read(); - println!("{:?}", &new_block); + warn!("{:?}", &new_block); if new_block.transaction_list.is_empty() { let res_json = warp::reply::json(&GradeCoinResponse { @@ -455,7 +456,7 @@ pub async fn authorized_propose_block( { let pending_transactions = db.pending_transactions.read(); - let mut users = db.users.write(); + let mut users_store = RwLockUpgradableReadGuard::upgrade(users_store); let coinbase_fingerprint = new_block.transaction_list.get(0).unwrap(); @@ -464,17 +465,17 @@ pub async fn authorized_propose_block( let source = &transaction.source; let target = &transaction.target; - if let Some(from) = users.get_mut(source) { + if let Some(from) = users_store.get_mut(source) { from.balance -= transaction.amount; } - if let Some(to) = users.get_mut(target) { + if let Some(to) = users_store.get_mut(target) { to.balance += transaction.amount; } } } - if let Some(coinbase_user) = users.get_mut(coinbase_fingerprint) { + if let Some(coinbase_user) = users_store.get_mut(coinbase_fingerprint) { coinbase_user.balance += BLOCK_REWARD as i32; } } diff --git a/tests/route_tests.rs b/tests/route_tests.rs index de60414..59483d3 100644 --- a/tests/route_tests.rs +++ b/tests/route_tests.rs @@ -28,39 +28,39 @@ FQIDAQAB ); // -----BEGIN RSA PRIVATE KEY----- -// MIIEpAIBAAKC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83OQUn/OZqY5gIQ+jlfwqnVg+PDQQ/P09/4xygRCLvjL6NCSvtkj1 + // MRArEl4y68+jZLRu74TVG0lXi6ht6KhNHF6GiWKU9FHZ4B+btLicsg== + // -----END RSA PRIVATE KEY----- db.pending_transactions.write().insert( "mock_transaction_source".to_owned(), Transaction { by: "mock_transaction_source".to_owned(), - source: "31415926535897932384626433832795028841971693993751058209749445923".to_owned(), + source: "31415926535897932384626433832795028841971693993751058209749445923" + .to_owned(), target: "mock_transaction_source".to_owned(), amount: 3, timestamp: chrono::NaiveDate::from_ymd(2021, 04, 13).and_hms(20, 55, 30), -- cgit v1.2.3-70-g09d2