🐛 patched for MAX_LOG_SIZE == 1 usecase

This commit is contained in:
EntireTwix 2021-07-19 23:33:03 -07:00
parent a37825fd51
commit 3bb7640cd6
4 changed files with 32 additions and 28 deletions

View file

@ -16,9 +16,9 @@ struct User
User(uint32_t init_bal, XXH64_hash_t init_pass) noexcept; User(uint32_t init_bal, XXH64_hash_t init_pass) noexcept;
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
User(uint32_t init_bal, XXH64_hash_t init_pass, const Json::Value &log_j) noexcept; User(uint32_t init_bal, XXH64_hash_t init_pass, const Json::Value &log_j) noexcept;
#endif
User(const bank_dom::User &u) noexcept; User(const bank_dom::User &u) noexcept;
bank_dom::User Encode() const noexcept; bank_dom::User Encode() const noexcept;
#endif
Json::Value Serialize() const; //to be removed later Json::Value Serialize() const; //to be removed later
}; };

View file

@ -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}; std::shared_lock<std::shared_mutex> lock{iter_lock};
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
time_t current_time = time(NULL); 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 #endif
if (!users.modify_if(a_name, [current_time, &a_name, &b_name, &res, amount](User &a) { {
//if A can afford it //if A can afford it
if (a.balance < amount) if (a.balance < amount)
{ {
res = {k400BadRequest, "\"Insufficient funds\""}; res = {k400BadRequest, "\"Insufficient funds\""};
} }
else else
{ {
a.balance -= amount; a.balance -= amount;
#if MAX_LOG_SIZE > 0 #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 #endif
res = {k200OK, std::to_string(a.balance)}; res = {k200OK, std::to_string(a.balance)};
} }
})) }))
{ {
return {k404NotFound, "\"Sender does not exist\""}; 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) for (const auto &u : users)
{ {
users.erase_if(u.first, [threshold_time, threshold_bal, &deleted_count](User &u) -> bool { 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; return ++deleted_count;
} }

View file

@ -37,17 +37,14 @@ void api::GetBal(req_args, const std::string &name) const
} }
void api::GetLogs(req_args) void api::GetLogs(req_args)
{ {
if constexpr (MAX_LOG_SIZE > 0) #if MAX_LOG_SIZE > 0
{ RESPONSE_PARSE(bank.GetLogs(NAME_PARAM));
RESPONSE_PARSE(bank.GetLogs(NAME_PARAM)); #else
} static thread_local auto resp = HttpResponse::newCustomHttpResponse(BankResponse{k404NotFound, "\"Logs are Disabled\""});
else CORS;
{ CACHE_FOREVER;
static thread_local auto resp = HttpResponse::newCustomHttpResponse(BankResponse{k404NotFound, "\"Logs are Disabled\""}); callback(resp);
CORS; #endif
CACHE_FOREVER;
callback(resp);
}
} }
void api::SendFunds(req_args) const void api::SendFunds(req_args) const
{ {

View file

@ -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) {} 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) User::User(const bank_dom::User &u) noexcept : balance(u.balance), password(u.password)
{ {
#if MAX_LOG_SIZE > 0
if (u.logs) if (u.logs)
{ {
for (uint32_t i = (u.logs.value().data.size() - MAX_LOG_SIZE); i < u.logs.value().data.size(); ++i) 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); log.data.emplace_front(temp.from, temp.to, temp.amount, temp.time);
} }
} }
}
#endif #endif
}
bank_dom::User User::Encode() const noexcept bank_dom::User User::Encode() const noexcept
{ {
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0