mirror of
https://github.com/Expand-sys/CCash
synced 2026-03-22 20:47:10 +11:00
🐎 6% faster sendfunds
This commit is contained in:
parent
a08d53bf34
commit
2dc1c0c369
1 changed files with 50 additions and 53 deletions
93
src/bank.cpp
93
src/bank.cpp
|
|
@ -75,24 +75,60 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name
|
||||||
return ErrorResponse::InvalidRequest;
|
return ErrorResponse::InvalidRequest;
|
||||||
}
|
}
|
||||||
//as first modify_if checks a_name and grabs unique lock
|
//as first modify_if checks a_name and grabs unique lock
|
||||||
if (!Contains(b_name))
|
if (!Contains(b_name) || !Contains(a_name))
|
||||||
{
|
{
|
||||||
return ErrorResponse::UserNotFound;
|
return ErrorResponse::UserNotFound;
|
||||||
}
|
}
|
||||||
|
|
||||||
int_fast8_t state = false;
|
int_fast8_t state = false;
|
||||||
{
|
{
|
||||||
std::shared_lock<std::shared_mutex> lock{send_funds_l}; //because SendFunds requires 3 locking operations
|
if constexpr (max_log_size > 0)
|
||||||
if (!users.modify_if(a_name, [&state, amount, &attempt](User &a) {
|
{
|
||||||
//if A exists, A can afford it, and A's password matches
|
Transaction temp(a_name, b_name, amount);
|
||||||
|
std::shared_lock<std::shared_mutex> lock{send_funds_l};
|
||||||
|
users.modify_if(a_name, [&temp, &state, amount, &attempt](User &a) {
|
||||||
|
//if A can afford it and A's password matches attempt
|
||||||
if (a.balance < amount)
|
if (a.balance < amount)
|
||||||
{
|
{
|
||||||
state = ErrorResponse::InsufficientFunds;
|
state = ErrorResponse::InsufficientFunds;
|
||||||
}
|
}
|
||||||
|
else if (a.password != XXH3_64bits(attempt.data(), attempt.size()))
|
||||||
|
{
|
||||||
|
|
||||||
|
state = ErrorResponse::WrongPassword;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (a.password != XXH3_64bits(attempt.data(), attempt.size()))
|
a.balance -= amount;
|
||||||
|
a.log.AddTrans(std::forward<Transaction>(temp));
|
||||||
|
state = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (state > 0)
|
||||||
{
|
{
|
||||||
|
users.modify_if(b_name, [&a_name, &b_name, &temp, amount](User &b) {
|
||||||
|
b.balance += amount;
|
||||||
|
b.log.AddTrans(std::move(temp));
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::shared_lock<std::shared_mutex> lock{send_funds_l};
|
||||||
|
users.modify_if(a_name, [&state, amount, &attempt](User &a) {
|
||||||
|
//if A can afford it and A's password matches attempt
|
||||||
|
if (a.balance < amount)
|
||||||
|
{
|
||||||
|
state = ErrorResponse::InsufficientFunds;
|
||||||
|
}
|
||||||
|
else if (a.password != XXH3_64bits(attempt.data(), attempt.size()))
|
||||||
|
{
|
||||||
|
|
||||||
state = ErrorResponse::WrongPassword;
|
state = ErrorResponse::WrongPassword;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -100,56 +136,17 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name
|
||||||
a.balance -= amount;
|
a.balance -= amount;
|
||||||
state = true;
|
state = true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}))
|
|
||||||
{
|
|
||||||
return ErrorResponse::UserNotFound;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!state > 0)
|
|
||||||
{
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if constexpr (max_log_size > 0)
|
|
||||||
{
|
|
||||||
Transaction temp;
|
|
||||||
//if B does exist
|
|
||||||
if (!users.modify_if(b_name, [&a_name, &b_name, &temp, amount](User &b) {
|
|
||||||
temp = Transaction(a_name, b_name, amount);
|
|
||||||
b.balance += amount;
|
|
||||||
b.log.AddTrans(std::forward<Transaction>(temp));
|
|
||||||
}))
|
|
||||||
{
|
|
||||||
//attempt to refund if A exist
|
|
||||||
users.modify_if(a_name, [amount](User &a) {
|
|
||||||
a.balance += amount;
|
|
||||||
});
|
});
|
||||||
return ErrorResponse::UserNotFound; //because had to refund transaction
|
if (state > 0)
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
users.modify_if(a_name, [&temp](User &a) {
|
users.modify_if(b_name, [&a_name, &b_name, amount](User &b) {
|
||||||
a.log.AddTrans(std::move(temp));
|
b.balance += amount;
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!users.modify_if(b_name, [amount](User &b) {
|
return state;
|
||||||
b.balance += amount;
|
|
||||||
}))
|
|
||||||
{
|
|
||||||
//attempt to refund if A exist
|
|
||||||
users.modify_if(a_name, [amount](User &a) {
|
|
||||||
a.balance += amount;
|
|
||||||
});
|
|
||||||
return ErrorResponse::UserNotFound; //because had to refund transaction
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue