From 32ea81bac2bf14e2db069d179f3fe86888fcf46c Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 21 Apr 2021 15:42:42 -0700 Subject: [PATCH] :sparkles: Intitial Logging Functionality --- config.json | 7 ----- include/bank.hpp | 68 +++++++++++++++++++++++++++++++++++++++------- include/bank_f.hpp | 6 ++++ 3 files changed, 64 insertions(+), 17 deletions(-) diff --git a/config.json b/config.json index 24792b8..9f4458f 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 c4a4bfe..8e7ce40 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -1,10 +1,10 @@ #pragma once #include #include -#include #include "xxhash.h" #include "parallel-hashmap/parallel_hashmap/phmap.h" #include "user.hpp" +#include "log.hpp" class { @@ -18,11 +18,19 @@ 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_lock should be grabbed if the operation MODIFIES the size (shared), this is so that when save claims unique + * @brief size_l should be grabbed if the operation MODIFIES the size (shared), this is so that when save claims unique * */ - std::shared_mutex size_lock; + std::shared_mutex size_l; /** * @brief send_funds_l should be grabbed if balances are being MODIFIED (shared) or if an operation needs to READ without the intermediary states that sendfunds has (unique) @@ -35,7 +43,7 @@ public: bool AddUser(const std::string &name, std::string &&init_pass) { - std::shared_lock lock{size_lock}; + std::shared_lock lock{size_l}; return users.try_emplace_l( name, [](User &) {}, std::move(init_pass)); } @@ -44,7 +52,7 @@ public: bool state = (admin_pass == attempt); if (state) { - std::shared_lock lock{size_lock}; + std::shared_lock lock{size_l}; state = users.try_emplace_l( name, [](User &) {}, init_bal, std::move(init_pass)); } @@ -53,20 +61,20 @@ public: bool DelUser(const std::string &name, const std::string &attempt) { - std::shared_lock lock{size_lock}; + std::shared_lock lock{size_l}; return users.erase_if(name, [&attempt](const User &u) { return (XXH3_64bits(attempt.data(), attempt.size()) == u.password); }); } bool AdminDelUser(const std::string &name, const std::string &attempt) { - std::shared_lock lock{size_lock}; + 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 - if (a_name == b_name) + //cant send money to self, from self or amount is 0 + if (a_name == b_name || !amount) { return false; } @@ -96,6 +104,18 @@ public: } } + if (state) + { + Transaction temp(a_name, b_name, amount, false); + logs.modify_if(a_name, [&temp](Log &l) { + l.AddTrans(temp); + }); + temp.recieving = true; + logs.modify_if(b_name, [&temp](Log &l) { + l.AddTrans(temp); + }); + } + return state; } bool Contains(const std::string &name) const @@ -147,6 +167,33 @@ public: return res; } + Json::Value GetLogs(const std::string &name, const std::string &attempt) + { + Json::Value res; + bool state = false; + users.if_contains(name, [&state, &attempt](const User &u) { + state = XXH3_64bits(attempt.data(), attempt.size()) == u.password; + }); + + if (state) + { + logs.if_contains(name, [&res](const Log &l) { + for (uint32_t i = l.data.size() - 1; i >= 0; --i) + { + if (!l.data[i].amount) + { + break; + } + res[99 - i]["to"] = l.data[i].to; + res[99 - i]["from"] = l.data[i].from; + res[99 - i]["amount"] = l.data[i].amount; + res[99 - i]["recieving"] = l.data[i].recieving; + } + }); + } + return res; + } + void Save() { Json::StreamWriterBuilder builder; @@ -157,7 +204,7 @@ public: //loading info into json temp { - std::scoped_lock lock{size_lock, send_funds_l}; + std::scoped_lock lock{size_l, send_funds_l}; for (const auto &u : users) { //we know it contains this key but we call this func to grab mutex @@ -189,6 +236,7 @@ public: user_save.close(); for (const auto &u : temp.getMemberNames()) { + logs.try_emplace(u); users.try_emplace(u, temp[u]["balance"].asUInt(), std::move(temp[u]["password"].asUInt64())); } } diff --git a/include/bank_f.hpp b/include/bank_f.hpp index 0b91229..23999fd 100644 --- a/include/bank_f.hpp +++ b/include/bank_f.hpp @@ -111,6 +111,11 @@ public: GEN_BODY JSON(bank.AdminVerifyPass(body["attempt"].asCString())); } + void GetLog(req_args, const std::string &name) + { + GEN_BODY + JSON(bank.GetLogs(name, body["attempt"].asCString())); + } METHOD_LIST_BEGIN METHOD_ADD(BankF::Close, "/admin/close", Post, Options); @@ -126,6 +131,7 @@ public: METHOD_ADD(BankF::Contains, "/contains/{name}", Get, Options); METHOD_ADD(BankF::GetBal, "/{name}/bal", Get, Options); METHOD_ADD(BankF::AdminVerifyPass, "/admin/vpass", Post, Options); + METHOD_ADD(BankF::GetLog, "/{name}/log", Post, Options); METHOD_ADD(BankF::DelUser, "/user", Delete, Options); METHOD_ADD(BankF::AdminDelUser, "/admin/user", Delete, Options);