mirror of
https://github.com/Expand-sys/CCash
synced 2026-03-22 20:47:10 +11:00
🐎 slightly faster when logs arent full
This commit is contained in:
parent
2dc1c0c369
commit
f58813606d
1 changed files with 51 additions and 62 deletions
113
src/bank.cpp
113
src/bank.cpp
|
|
@ -32,13 +32,12 @@ int_fast8_t Bank::AdminAddUser(const std::string &attempt, std::string &&name, u
|
||||||
{
|
{
|
||||||
return ErrorResponse::WrongPassword;
|
return ErrorResponse::WrongPassword;
|
||||||
}
|
}
|
||||||
{
|
|
||||||
std::shared_lock<std::shared_mutex> lock{size_l};
|
std::shared_lock<std::shared_mutex> lock{size_l};
|
||||||
return (users.try_emplace_l(
|
return (users.try_emplace_l(
|
||||||
name, [](User &) {}, init_bal, std::move(init_pass)))
|
name, [](User &) {}, init_bal, std::move(init_pass)))
|
||||||
? true
|
? true
|
||||||
: ErrorResponse::UserAlreadyExists;
|
: ErrorResponse::UserAlreadyExists;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
int_fast8_t Bank::DelUser(const std::string &name, const std::string &attempt) noexcept
|
int_fast8_t Bank::DelUser(const std::string &name, const std::string &attempt) noexcept
|
||||||
{
|
{
|
||||||
|
|
@ -75,80 +74,70 @@ 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) || !Contains(a_name))
|
if (!Contains(b_name))
|
||||||
{
|
{
|
||||||
return ErrorResponse::UserNotFound;
|
return ErrorResponse::UserNotFound;
|
||||||
}
|
}
|
||||||
|
|
||||||
int_fast8_t state = false;
|
int_fast8_t state = false;
|
||||||
|
if constexpr (max_log_size > 0)
|
||||||
{
|
{
|
||||||
if constexpr (max_log_size > 0)
|
Transaction temp(a_name, b_name, amount);
|
||||||
{
|
std::shared_lock<std::shared_mutex> lock{send_funds_l};
|
||||||
Transaction temp(a_name, b_name, amount);
|
users.modify_if(a_name, [&temp, &state, amount, &attempt](User &a) {
|
||||||
std::shared_lock<std::shared_mutex> lock{send_funds_l};
|
//if A can afford it and A's password matches attempt
|
||||||
users.modify_if(a_name, [&temp, &state, amount, &attempt](User &a) {
|
if (a.balance < amount)
|
||||||
//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;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
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) {
|
state = ErrorResponse::InsufficientFunds;
|
||||||
b.balance += amount;
|
}
|
||||||
b.log.AddTrans(std::move(temp));
|
else if (a.password != XXH3_64bits(attempt.data(), attempt.size()))
|
||||||
});
|
{
|
||||||
return true;
|
|
||||||
|
state = ErrorResponse::WrongPassword;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return state;
|
a.balance -= amount;
|
||||||
|
a.log.AddTrans(std::forward<Transaction>(temp));
|
||||||
|
state = true;
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
else
|
if (state > 0)
|
||||||
{
|
{
|
||||||
std::shared_lock<std::shared_mutex> lock{send_funds_l};
|
users.modify_if(b_name, [&a_name, &b_name, &temp, amount](User &b) {
|
||||||
users.modify_if(a_name, [&state, amount, &attempt](User &a) {
|
b.balance += amount;
|
||||||
//if A can afford it and A's password matches attempt
|
b.log.AddTrans(std::move(temp));
|
||||||
if (a.balance < amount)
|
|
||||||
{
|
|
||||||
state = ErrorResponse::InsufficientFunds;
|
|
||||||
}
|
|
||||||
else if (a.password != XXH3_64bits(attempt.data(), attempt.size()))
|
|
||||||
{
|
|
||||||
|
|
||||||
state = ErrorResponse::WrongPassword;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
a.balance -= amount;
|
|
||||||
state = true;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
if (state > 0)
|
}
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
users.modify_if(b_name, [&a_name, &b_name, amount](User &b) {
|
state = ErrorResponse::InsufficientFunds;
|
||||||
b.balance += amount;
|
}
|
||||||
});
|
else if (a.password != XXH3_64bits(attempt.data(), attempt.size()))
|
||||||
return true;
|
{
|
||||||
|
|
||||||
|
state = ErrorResponse::WrongPassword;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return state;
|
a.balance -= amount;
|
||||||
|
state = true;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
if (state > 0)
|
||||||
|
{
|
||||||
|
users.modify_if(b_name, [&a_name, &b_name, amount](User &b) {
|
||||||
|
b.balance += amount;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
return state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue