mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-17 08:32:13 +11:00
🐛 fixing last commit
This commit is contained in:
parent
6e46cb8f7a
commit
0c34bc3689
3 changed files with 36 additions and 50 deletions
|
|
@ -15,7 +15,9 @@ struct User
|
||||||
User(const std::string &init_pass);
|
User(const std::string &init_pass);
|
||||||
User(uint32_t init_bal, const std::string &init_pass);
|
User(uint32_t init_bal, const std::string &init_pass);
|
||||||
User(uint32_t init_bal, uint64_t init_pass);
|
User(uint32_t init_bal, uint64_t init_pass);
|
||||||
|
#if MAX_LOG_SIZE > 0
|
||||||
User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j);
|
User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j);
|
||||||
|
#endif
|
||||||
|
|
||||||
Json::Value Serialize() const; //to be removed later
|
Json::Value Serialize() const; //to be removed later
|
||||||
};
|
};
|
||||||
|
|
|
||||||
75
src/bank.cpp
75
src/bank.cpp
|
|
@ -15,12 +15,14 @@ BankResponse Bank::GetBal(const std::string &name) const noexcept
|
||||||
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
||||||
{
|
{
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
|
#if MAX_LOG_SIZE > 0
|
||||||
if (!users.modify_if(name, [&res](User &u) {
|
if (!users.modify_if(name, [&res](User &u) {
|
||||||
res = {k200OK, u.log.GetLog()};
|
res = {k200OK, u.log.GetLog()};
|
||||||
}))
|
}))
|
||||||
{
|
{
|
||||||
return BankResponse(k404NotFound, "User not found");
|
return BankResponse(k404NotFound, "User not found");
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept
|
BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept
|
||||||
|
|
@ -44,42 +46,13 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
||||||
}
|
}
|
||||||
|
|
||||||
BankResponse state;
|
BankResponse state;
|
||||||
if constexpr (MAX_LOG_SIZE > 0)
|
std::shared_lock<std::shared_mutex> lock{send_funds_l};
|
||||||
{
|
#if MAX_LOG_SIZE > 0
|
||||||
Transaction temp(a_name, b_name, amount);
|
Transaction temp(a_name, b_name, amount);
|
||||||
std::shared_lock<std::shared_mutex> lock{send_funds_l};
|
if (!users.modify_if(a_name, [&temp, &state, amount](User &a) {
|
||||||
if (!users.modify_if(a_name, [&temp, &state, amount](User &a) {
|
#else
|
||||||
//if A can afford it
|
if (!users.modify_if(a_name, [&state, amount](User &a) {
|
||||||
if (a.balance < amount)
|
|
||||||
{
|
|
||||||
state = {k402PaymentRequired, "Sender has insufficient funds"};
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
a.balance -= amount;
|
|
||||||
a.log.AddTrans(Transaction(temp));
|
|
||||||
state = {k200OK, "Transfer successful!"};
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
{
|
|
||||||
return {k404NotFound, "Sender does not exist"};
|
|
||||||
}
|
|
||||||
if (state.first == k200OK)
|
|
||||||
{
|
|
||||||
users.modify_if(b_name, [&temp, amount](User &b) {
|
|
||||||
b.balance += amount;
|
|
||||||
b.log.AddTrans(std::move(temp));
|
|
||||||
});
|
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
|
||||||
save_flag.SetChangesOn();
|
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::shared_lock<std::shared_mutex> lock{send_funds_l};
|
|
||||||
users.modify_if(a_name, [this, &state, amount](User &a) {
|
|
||||||
//if A can afford it
|
//if A can afford it
|
||||||
if (a.balance < amount)
|
if (a.balance < amount)
|
||||||
{
|
{
|
||||||
|
|
@ -88,20 +61,30 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
a.balance -= amount;
|
a.balance -= amount;
|
||||||
|
#if MAX_LOG_SIZE > 0
|
||||||
|
a.log.AddTrans(Transaction(temp));
|
||||||
|
#endif
|
||||||
state = {k200OK, "Transfer successful!"};
|
state = {k200OK, "Transfer successful!"};
|
||||||
}
|
}
|
||||||
});
|
}))
|
||||||
if (state.first == k200OK)
|
{
|
||||||
{
|
return {k404NotFound, "Sender does not exist"};
|
||||||
users.modify_if(b_name, [&a_name, &b_name, amount](User &b) {
|
|
||||||
b.balance += amount;
|
|
||||||
});
|
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
|
||||||
save_flag.SetChangesOn();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
return state;
|
|
||||||
}
|
}
|
||||||
|
if (state.first == k200OK)
|
||||||
|
{
|
||||||
|
#if MAX_LOG_SIZE > 0
|
||||||
|
users.modify_if(b_name, [&temp, amount](User &b) {
|
||||||
|
b.balance += amount;
|
||||||
|
b.log.AddTrans(std::move(temp)); });
|
||||||
|
#else
|
||||||
|
users.modify_if(b_name, [amount](User &b) { b.balance += amount; });
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
|
save_flag.SetChangesOn();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return state;
|
||||||
}
|
}
|
||||||
bool Bank::VerifyPassword(const std::string &name, const std::string &attempt) const noexcept
|
bool Bank::VerifyPassword(const std::string &name, const std::string &attempt) const noexcept
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ User::User(uint32_t init_bal, const std::string &init_pass) : balance(init_bal),
|
||||||
* @param init_pass
|
* @param init_pass
|
||||||
*/
|
*/
|
||||||
User::User(uint32_t init_bal, uint64_t init_pass) : balance(init_bal), password(init_pass) {}
|
User::User(uint32_t init_bal, uint64_t init_pass) : balance(init_bal), password(init_pass) {}
|
||||||
|
#if MAX_LOG_SIZE > 0
|
||||||
User::User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j) : balance(init_bal), password(init_pass)
|
User::User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j) : balance(init_bal), password(init_pass)
|
||||||
{
|
{
|
||||||
if (log_j.size())
|
if (log_j.size())
|
||||||
|
|
@ -41,15 +42,15 @@ User::User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j) : ba
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
Json::Value User::Serialize() const
|
Json::Value User::Serialize() const
|
||||||
{
|
{
|
||||||
Json::Value res;
|
Json::Value res;
|
||||||
res["balance"] = (Json::UInt)balance;
|
res["balance"] = (Json::UInt)balance;
|
||||||
res["password"] = (Json::UInt64)password;
|
res["password"] = (Json::UInt64)password;
|
||||||
if constexpr (MAX_LOG_SIZE > 0)
|
#if MAX_LOG_SIZE > 0
|
||||||
{
|
res["log"] = log.Serialize();
|
||||||
res["log"] = log.Serialize();
|
#endif
|
||||||
}
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue