mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-16 16:12:14 +11:00
🐛 patched for MAX_LOG_SIZE == 1 usecase
This commit is contained in:
parent
a37825fd51
commit
3bb7640cd6
4 changed files with 32 additions and 28 deletions
|
|
@ -16,9 +16,9 @@ struct User
|
|||
User(uint32_t init_bal, XXH64_hash_t init_pass) noexcept;
|
||||
#if MAX_LOG_SIZE > 0
|
||||
User(uint32_t init_bal, XXH64_hash_t init_pass, const Json::Value &log_j) noexcept;
|
||||
#endif
|
||||
User(const bank_dom::User &u) noexcept;
|
||||
bank_dom::User Encode() const noexcept;
|
||||
#endif
|
||||
|
||||
Json::Value Serialize() const; //to be removed later
|
||||
};
|
||||
|
|
|
|||
35
src/bank.cpp
35
src/bank.cpp
|
|
@ -92,22 +92,25 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
|||
std::shared_lock<std::shared_mutex> lock{iter_lock};
|
||||
#if MAX_LOG_SIZE > 0
|
||||
time_t current_time = time(NULL);
|
||||
if (!users.modify_if(a_name, [current_time, &a_name, &b_name, &res, amount](User &a)
|
||||
#else
|
||||
if (!users.modify_if(a_name, [&a_name, &b_name, &res, amount](User &a)
|
||||
#endif
|
||||
if (!users.modify_if(a_name, [current_time, &a_name, &b_name, &res, amount](User &a) {
|
||||
//if A can afford it
|
||||
if (a.balance < amount)
|
||||
{
|
||||
res = {k400BadRequest, "\"Insufficient funds\""};
|
||||
}
|
||||
else
|
||||
{
|
||||
a.balance -= amount;
|
||||
{
|
||||
//if A can afford it
|
||||
if (a.balance < amount)
|
||||
{
|
||||
res = {k400BadRequest, "\"Insufficient funds\""};
|
||||
}
|
||||
else
|
||||
{
|
||||
a.balance -= amount;
|
||||
#if MAX_LOG_SIZE > 0
|
||||
a.log.AddTrans(a_name, b_name, amount, current_time);
|
||||
a.log.AddTrans(a_name, b_name, amount, current_time);
|
||||
#endif
|
||||
res = {k200OK, std::to_string(a.balance)};
|
||||
}
|
||||
}))
|
||||
res = {k200OK, std::to_string(a.balance)};
|
||||
}
|
||||
}))
|
||||
{
|
||||
return {k404NotFound, "\"Sender does not exist\""};
|
||||
}
|
||||
|
|
@ -201,7 +204,11 @@ BankResponse Bank::PruneUsers(time_t threshold_time, uint32_t threshold_bal) noe
|
|||
for (const auto &u : users)
|
||||
{
|
||||
users.erase_if(u.first, [threshold_time, threshold_bal, &deleted_count](User &u) -> bool {
|
||||
if (u.log.data.back().time < threshold_time && u.balance < threshold_bal)
|
||||
#if MAX_LOG_SIZE > 0
|
||||
if (u.data.back().time < threshold_time && u.balance < threshold_bal)
|
||||
#else
|
||||
if (u.balance < threshold_bal)
|
||||
#endif
|
||||
{
|
||||
return ++deleted_count;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,17 +37,14 @@ void api::GetBal(req_args, const std::string &name) const
|
|||
}
|
||||
void api::GetLogs(req_args)
|
||||
{
|
||||
if constexpr (MAX_LOG_SIZE > 0)
|
||||
{
|
||||
RESPONSE_PARSE(bank.GetLogs(NAME_PARAM));
|
||||
}
|
||||
else
|
||||
{
|
||||
static thread_local auto resp = HttpResponse::newCustomHttpResponse(BankResponse{k404NotFound, "\"Logs are Disabled\""});
|
||||
CORS;
|
||||
CACHE_FOREVER;
|
||||
callback(resp);
|
||||
}
|
||||
#if MAX_LOG_SIZE > 0
|
||||
RESPONSE_PARSE(bank.GetLogs(NAME_PARAM));
|
||||
#else
|
||||
static thread_local auto resp = HttpResponse::newCustomHttpResponse(BankResponse{k404NotFound, "\"Logs are Disabled\""});
|
||||
CORS;
|
||||
CACHE_FOREVER;
|
||||
callback(resp);
|
||||
#endif
|
||||
}
|
||||
void api::SendFunds(req_args) const
|
||||
{
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@ User::User(uint32_t init_bal, const std::string &init_pass) noexcept : balance(i
|
|||
*/
|
||||
User::User(uint32_t init_bal, XXH64_hash_t init_pass) noexcept : balance(init_bal), password(init_pass) {}
|
||||
|
||||
#if MAX_LOG_SIZE > 0
|
||||
User::User(const bank_dom::User &u) noexcept : balance(u.balance), password(u.password)
|
||||
{
|
||||
#if MAX_LOG_SIZE > 0
|
||||
if (u.logs)
|
||||
{
|
||||
for (uint32_t i = (u.logs.value().data.size() - MAX_LOG_SIZE); i < u.logs.value().data.size(); ++i)
|
||||
|
|
@ -27,8 +27,8 @@ User::User(const bank_dom::User &u) noexcept : balance(u.balance), password(u.pa
|
|||
log.data.emplace_front(temp.from, temp.to, temp.amount, temp.time);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
bank_dom::User User::Encode() const noexcept
|
||||
{
|
||||
#if MAX_LOG_SIZE > 0
|
||||
|
|
|
|||
Loading…
Reference in a new issue