summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYigit Sever2021-04-16 02:41:17 +0300
committerYigit Sever2021-04-16 02:41:17 +0300
commit166d80b7938970dae7d8cf24805a329e0770d77f (patch)
tree62b44854a605edf84c1e5dd006092e1a7af07063
parentf538ec25685fd027299bb07d482b93d8f457d9cf (diff)
downloadgradecoin-166d80b7938970dae7d8cf24805a329e0770d77f.tar.gz
gradecoin-166d80b7938970dae7d8cf24805a329e0770d77f.tar.bz2
gradecoin-166d80b7938970dae7d8cf24805a329e0770d77f.zip
Fix deadlock
-rw-r--r--src/handlers.rs11
-rw-r--r--tests/route_tests.rs56
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;
8use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation}; 8use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation};
9use log::{debug, warn}; 9use log::{debug, warn};
10use md5::Md5; 10use md5::Md5;
11use parking_lot::RwLockUpgradableReadGuard;
11use rsa::{PaddingScheme, RSAPrivateKey}; 12use rsa::{PaddingScheme, RSAPrivateKey};
12use serde::Serialize; 13use serde::Serialize;
13use sha2::Sha256; 14use sha2::Sha256;
@@ -334,7 +335,7 @@ pub async fn authorized_propose_block(
334 335
335 let users_store = db.users.upgradable_read(); 336 let users_store = db.users.upgradable_read();
336 337
337 println!("{:?}", &new_block); 338 warn!("{:?}", &new_block);
338 339
339 if new_block.transaction_list.is_empty() { 340 if new_block.transaction_list.is_empty() {
340 let res_json = warp::reply::json(&GradeCoinResponse { 341 let res_json = warp::reply::json(&GradeCoinResponse {
@@ -455,7 +456,7 @@ pub async fn authorized_propose_block(
455 456
456 { 457 {
457 let pending_transactions = db.pending_transactions.read(); 458 let pending_transactions = db.pending_transactions.read();
458 let mut users = db.users.write(); 459 let mut users_store = RwLockUpgradableReadGuard::upgrade(users_store);
459 460
460 let coinbase_fingerprint = new_block.transaction_list.get(0).unwrap(); 461 let coinbase_fingerprint = new_block.transaction_list.get(0).unwrap();
461 462
@@ -464,17 +465,17 @@ pub async fn authorized_propose_block(
464 let source = &transaction.source; 465 let source = &transaction.source;
465 let target = &transaction.target; 466 let target = &transaction.target;
466 467
467 if let Some(from) = users.get_mut(source) { 468 if let Some(from) = users_store.get_mut(source) {
468 from.balance -= transaction.amount; 469 from.balance -= transaction.amount;
469 } 470 }
470 471
471 if let Some(to) = users.get_mut(target) { 472 if let Some(to) = users_store.get_mut(target) {
472 to.balance += transaction.amount; 473 to.balance += transaction.amount;
473 } 474 }
474 } 475 }
475 } 476 }
476 477
477 if let Some(coinbase_user) = users.get_mut(coinbase_fingerprint) { 478 if let Some(coinbase_user) = users_store.get_mut(coinbase_fingerprint) {
478 coinbase_user.balance += BLOCK_REWARD as i32; 479 coinbase_user.balance += BLOCK_REWARD as i32;
479 } 480 }
480 } 481 }
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
28 ); 28 );
29 29
30 // -----BEGIN RSA PRIVATE KEY----- 30 // -----BEGIN RSA PRIVATE KEY-----
31// MIIEpAIBAAKCAQEA5yWTMeFqr2nvOC9oR5Wq/nzcNlwCIaziojt7rJ4BBvuwkT0t 31 // MIIEpAIBAAKCAQEA5yWTMeFqr2nvOC9oR5Wq/nzcNlwCIaziojt7rJ4BBvuwkT0t
32// ERDz8AgvUsaewiB+Fz5OXTeb3WAB1FEXnBXGekrGzvC8jHQMKHyNoWzUlpQJ9UMt 32 // ERDz8AgvUsaewiB+Fz5OXTeb3WAB1FEXnBXGekrGzvC8jHQMKHyNoWzUlpQJ9UMt
33// dQIWPOCuMyLpc+rNPL3428U8UpldjbTHHyq2/ef6abkdj+XWg/slYtrFeOf3ktc1 33 // dQIWPOCuMy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/jS 49 // ipVkWOg1IoH6QKUIqfLQM9OfA290Xd+ML89t2Fzq9XnLL3sFDQtwCvIM/YLSQ/jS
50// gu7yRkwttzA2NapCQ1h6mQKBgQClwBwn8Qyd01y2mCKkNzsP+2/cqTAbeSNAXFe8 50 // gu7yRkwttzA2NapCQ1h6mQKBgQClwBwn8Qyd01y2mCKkNzsP+2/cqTAbeSNAXFe8
51// pMfDowx1+hBu7/7CF+/kPwmQuTa5kSB9PgWsWzYjwNm4OX1j+mbL9lEDLf7tRVWQ 51 // pMfDowx1+hBu7/7CF+/kPwmQuTa5kSB9PgWsWzYjwNm4OX1j+mbL9lEDLf7tRVWQ
52// lydJyz7tmRYzWj6j4V/l/u90M3QgyiqTbCf73GG0AkjaRwHn3dG1gl9A0lZqDvK3 52 // lydJyz7tmRYzWj6j4V/l/u90M3QgyiqTbCf73GG0AkjaRwHn3dG1gl9A0lZqDvK3
53// iQXouwKBgQCrx6SCnEkhLISSZpzdDehtWmyCQJIwcdlRQlAmFLVn+TJHTXR7xUm2 53 // iQXouwKBgQCrx6SCnEkhLISSZpzdDehtWmyCQJIwcdlRQlAmFLVn+TJHTXR7xUm2
54// VpTrPTfaYWx83OQUn/OZqY5gIQ+jlfwqnVg+PDQQ/P09/4xygRCLvjL6NCSvtkj1 54 // VpTrPTfaYWx83OQUn/OZqY5gIQ+jlfwqnVg+PDQQ/P09/4xygRCLvjL6NCSvtkj1
55// MRArEl4y68+jZLRu74TVG0lXi6ht6KhNHF6GiWKU9FHZ4B+btLicsg== 55 // MRArEl4y68+jZLRu74TVG0lXi6ht6KhNHF6GiWKU9FHZ4B+btLicsg==
56// -----END RSA PRIVATE KEY----- 56 // -----END RSA PRIVATE KEY-----
57
58 57
59 db.pending_transactions.write().insert( 58 db.pending_transactions.write().insert(
60 "mock_transaction_source".to_owned(), 59 "mock_transaction_source".to_owned(),
61 Transaction { 60 Transaction {
62 by: "mock_transaction_source".to_owned(), 61 by: "mock_transaction_source".to_owned(),
63 source: "31415926535897932384626433832795028841971693993751058209749445923".to_owned(), 62 source: "31415926535897932384626433832795028841971693993751058209749445923"
63 .to_owned(),
64 target: "mock_transaction_source".to_owned(), 64 target: "mock_transaction_source".to_owned(),
65 amount: 3, 65 amount: 3,
66 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 13).and_hms(20, 55, 30), 66 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 13).and_hms(20, 55, 30),