From 491f5f9adf5d59ffbe913d2ed382b37b475457d4 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 21 Apr 2021 15:44:27 -0700 Subject: [PATCH] :racehorse: SendFunds lock time decreased --- include/bank.hpp | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/include/bank.hpp b/include/bank.hpp index 8e7ce40..107892a 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -80,27 +80,29 @@ public: } //if A exists, A can afford it, and A's password matches - bool state = false; - std::shared_lock lock{send_funds_l}; //because SendFunds requires 3 locking operations - users.modify_if(a_name, [&state, amount, &attempt](User &a) { - if (state = (a.balance >= amount) && (a.password == XXH3_64bits(attempt.data(), attempt.size()))) - { - a.balance -= amount; - } - }); - - if (state) { - //if B doesnt exist - if (!users.modify_if(b_name, [amount](User &b) { - b.balance += amount; - })) + std::shared_lock lock{send_funds_l}; //because SendFunds requires 3 locking operations + bool state = false; + users.modify_if(a_name, [&state, amount, &attempt](User &a) { + if (state = (a.balance >= amount) && (a.password == XXH3_64bits(attempt.data(), attempt.size()))) + { + a.balance -= amount; + } + }); + + if (state) { - //attempt to refund if A exist - users.modify_if(a_name, [amount](User &a) { - a.balance += amount; - }); - state = false; //because had to refund transaction + //if B doesnt exist + if (!users.modify_if(b_name, [amount](User &b) { + b.balance += amount; + })) + { + //attempt to refund if A exist + users.modify_if(a_name, [amount](User &a) { + a.balance += amount; + }); + state = false; //because had to refund transaction + } } }