diff --git a/.gitignore b/.gitignore index 0e491cb..6a8bc10 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ .vscode -build -config.json \ No newline at end of file +build \ No newline at end of file diff --git a/config.json b/config.json index 5576c78..eeadc3c 100644 --- a/config.json +++ b/config.json @@ -4,13 +4,6 @@ "address": "0.0.0.0", "port": 80, "https": false - }, - { - "address": "0.0.0.0", - "port": 443, - "https": true, - "cert": "", - "key": "" } ] } \ No newline at end of file diff --git a/include/bank.hpp b/include/bank.hpp index a739367..14720b3 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -4,7 +4,6 @@ #include "xxhash.h" #include "parallel-hashmap/parallel_hashmap/phmap.h" #include "user.hpp" -#include "log.hpp" class { @@ -18,14 +17,6 @@ private: std::mutex> users; - phmap::parallel_flat_hash_map< - std::string, Log, - phmap::priv::hash_default_hash, - phmap::priv::hash_default_eq, - phmap::priv::Allocator>, - 4UL, - std::mutex> - logs; /** * @brief size_l should be grabbed if the operation MODIFIES the size (shared), this is so that when save claims unique * @@ -61,34 +52,17 @@ public: bool DelUser(const std::string &name, const std::string &attempt) { - bool state; - { - std::shared_lock lock{size_l}; - state = users.erase_if(name, [&attempt](User &u) { return (XXH3_64bits(attempt.data(), attempt.size()) == u.password); }); - } - if (state) - { - logs.erase(name); - } - return state; + std::shared_lock lock{size_l}; + return users.erase_if(name, [&attempt](User &u) { return (XXH3_64bits(attempt.data(), attempt.size()) == u.password); }); } bool AdminDelUser(const std::string &name, const std::string &attempt) { - bool state; - { - std::shared_lock lock{size_l}; - state = users.erase_if(name, [this, &attempt](const User &) { return (admin_pass == attempt); }); - } - if (state) - { - logs.erase(name); - } - return state; + std::shared_lock lock{size_l}; + return users.erase_if(name, [this, &attempt](const User &) { return (admin_pass == attempt); }); } bool SendFunds(const std::string &a_name, const std::string &b_name, uint_fast32_t amount, const std::string &attempt) { - //cant send money to self, from self or amount is 0 if (a_name == b_name || !amount) { @@ -121,28 +95,14 @@ public: } } } - if (state) { - //if user lacks a log, one is created, this is to reduce usage Transaction temp(a_name, b_name, amount); - Transaction temp2 = temp; // to keep same time - - users.if_contains(a_name, [this, &temp, &a_name](const User &u) { - if (logs.try_emplace_l(a_name, [&temp](Log &l) { l.AddTrans(std::move(temp)); })) - { - logs.modify_if(a_name, [&temp](Log &l) { - l.AddTrans(std::move(temp)); - }); - } + users.modify_if(a_name, [&temp](User &a) { + a.log.AddTrans(std::forward(temp)); }); - users.if_contains(a_name, [this, &temp2, &b_name](const User &u) { - if (logs.try_emplace_l(b_name, [&temp2](Log &l) { l.AddTrans(std::move(temp2)); })) - { - logs.modify_if(b_name, [&temp2](Log &l) { - l.AddTrans(std::move(temp2)); - }); - } + users.modify_if(b_name, [&temp](User &b) { + b.log.AddTrans(std::move(temp)); }); } @@ -199,44 +159,26 @@ public: Json::Value GetLogs(const std::string &name, const std::string &attempt) { - bool state = false; - users.if_contains(name, [&state, &attempt](const User &u) { - state = XXH3_64bits(attempt.data(), attempt.size()) == u.password; - }); - - if (!state) - { - return 0; - } - - Json::Value res; - if (!(logs.if_contains(name, [&res](const Log &l) { - uint32_t j; - for (uint32_t i = l.data.size() - 1; i > 0; --i) - { - j = 24 - i; - if (!l.data[i].amount) - { - return; - } - res[j]["to"] = l.data[i].to; - res[j]["from"] = l.data[i].from; - res[j]["amount"] = l.data[i].amount; - res[j]["time"] = (Json::UInt)l.data[i].time; - } - }))) - { - if (users.contains(name)) + Json::Value res = -1; + users.if_contains(name, [&res](const User &u) { + uint32_t j; + for (uint32_t i = u.log.data.size() - 1; i > 0; --i) { - return 1; + j = u.log.data.size() - 1 - i; + if (!u.log.data[i].amount) + { + return; + } + res[j]["to"] = u.log.data[i].to; + res[j]["from"] = u.log.data[i].from; + res[j]["amount"] = u.log.data[i].amount; + res[j]["time"] = (Json::UInt64)u.log.data[i].time; } - return -1; - } + }); return res; } - void - Save() + void Save() { Json::StreamWriterBuilder builder; const std::unique_ptr writer(builder.newStreamWriter()); diff --git a/include/log.hpp b/include/log.hpp index dfb0eed..facf5f1 100644 --- a/include/log.hpp +++ b/include/log.hpp @@ -5,9 +5,13 @@ struct Log { - std::array data; + std::vector data; void AddTrans(Transaction &&v) { + if (!data.size()) + { + data.resize(25); + } std::rotate(data.begin(), data.begin() + 1, data.end()); data[24] = std::move(v); } diff --git a/include/transactions.hpp b/include/transactions.hpp index 98ef3fb..b56648e 100644 --- a/include/transactions.hpp +++ b/include/transactions.hpp @@ -8,7 +8,7 @@ struct Transaction std::string from = "", to = ""; uint32_t amount = 0; - uint32_t time = 0; + uint64_t time = 0; void Concatinate(std::string &s) { diff --git a/include/user.hpp b/include/user.hpp index 130e776..ade7eaf 100644 --- a/include/user.hpp +++ b/include/user.hpp @@ -1,12 +1,13 @@ #pragma once #include #include +#include "log.hpp" struct User { uint_fast32_t balance = 0; uint64_t password; - bool is_admin = false; + Log log; /** * @brief User constructor @@ -21,15 +22,14 @@ struct User * @param init_bal initial balance * @param init_pass initial password */ - User(uint_fast32_t init_bal, std::string &&init_pass, bool state = false) : balance(init_bal), password(XXH3_64bits(init_pass.data(), init_pass.size())), is_admin(state) {} - User(uint_fast32_t init_bal, uint64_t init_pass, bool state = false) : balance(init_bal), password(init_pass), is_admin(state) {} + User(uint_fast32_t init_bal, std::string &&init_pass, bool state = false) : balance(init_bal), password(XXH3_64bits(init_pass.data(), init_pass.size())) {} + User(uint_fast32_t init_bal, uint64_t init_pass, bool state = false) : balance(init_bal), password(init_pass) {} Json::Value Serialize() const { Json::Value res; res["balance"] = (Json::UInt)balance; res["password"] = (Json::UInt64)password; - res["is_admin"] = is_admin; return res; } }; \ No newline at end of file