mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-17 00:22:14 +11:00
🚧 bank class and re-structure
This commit is contained in:
parent
80ffdff068
commit
efb769ee29
2 changed files with 112 additions and 65 deletions
63
include/bank.hpp
Normal file
63
include/bank.hpp
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
#pragma once
|
||||||
|
#include "parallel-hashmap/parallel_hashmap/phmap.h"
|
||||||
|
#include "user.hpp"
|
||||||
|
|
||||||
|
class
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
phmap::parallel_node_hash_map<
|
||||||
|
std::string, User,
|
||||||
|
phmap::priv::hash_default_hash<std::string>,
|
||||||
|
phmap::priv::hash_default_eq<std::string>,
|
||||||
|
phmap::priv::Allocator<phmap::priv::Pair<const std::string, User>>,
|
||||||
|
4UL,
|
||||||
|
std::mutex>
|
||||||
|
users;
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::string admin_pass;
|
||||||
|
|
||||||
|
bool AddUser(const std::string &name, std::string &&init_pass)
|
||||||
|
{
|
||||||
|
return users.try_emplace_l(
|
||||||
|
name, []() {}, init_pass);
|
||||||
|
}
|
||||||
|
bool AdminAddUser(const std::string &attempt, std::string &&name, uint_fast64_t init_bal, std::string &&init_pass)
|
||||||
|
{
|
||||||
|
const bool state = (admin_pass == attempt);
|
||||||
|
if (state)
|
||||||
|
{
|
||||||
|
users.try_emplace_l(
|
||||||
|
name, []() {}, init_bal, init_pass);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
bool SendFunds(const std::string &a_name, const std::string &b_name, uint_fast64_t amount, const std::string &attempt)
|
||||||
|
{
|
||||||
|
//if A exists, A can afford it, and A's password matches
|
||||||
|
bool state = false;
|
||||||
|
|
||||||
|
users.modify_if(a_name, [&state, amount, &attempt](User &a) {
|
||||||
|
if (state = (a.balance >= amount) && (a.password == attempt))
|
||||||
|
{
|
||||||
|
a.balance -= amount;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (state)
|
||||||
|
{
|
||||||
|
//if B doesnt exist
|
||||||
|
if (!users.modify_if(b_name, [amount](User &b) {
|
||||||
|
b.balance += amount;
|
||||||
|
}))
|
||||||
|
{
|
||||||
|
//attempt to refund if destination doesnt exist
|
||||||
|
users.modify_if(a_name, [amount](User &a) {
|
||||||
|
a.balance += amount;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
} Bank;
|
||||||
114
include/user.hpp
114
include/user.hpp
|
|
@ -2,17 +2,11 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
class User
|
struct User
|
||||||
{
|
{
|
||||||
private:
|
uint_fast64_t balance = 0;
|
||||||
uint_fast64_t balance;
|
|
||||||
std::string password;
|
std::string password;
|
||||||
|
|
||||||
//for read/write of object's state concurrently
|
|
||||||
std::mutex bal_lock;
|
|
||||||
std::mutex pass_lock;
|
|
||||||
|
|
||||||
public:
|
|
||||||
/**
|
/**
|
||||||
* @brief User constructor
|
* @brief User constructor
|
||||||
*
|
*
|
||||||
|
|
@ -28,64 +22,54 @@ public:
|
||||||
*/
|
*/
|
||||||
User(uint_fast64_t init_bal, std::string &&init_pass) : balance(init_bal), password(init_pass) {}
|
User(uint_fast64_t init_bal, std::string &&init_pass) : balance(init_bal), password(init_pass) {}
|
||||||
|
|
||||||
bool ChangePassword(const std::string &attempt, std::string &&new_pass)
|
// bool ChangePassword(const std::string &attempt, std::string &&new_pass)
|
||||||
{
|
// {
|
||||||
std::lock_guard<std::mutex> lock{pass_lock};
|
// const bool state = (password == attempt);
|
||||||
const bool state = (password == attempt);
|
// if (state)
|
||||||
if (state)
|
// {
|
||||||
{
|
// password = new_pass;
|
||||||
password = new_pass;
|
// }
|
||||||
}
|
// return state;
|
||||||
return state;
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// // /**
|
||||||
* @brief SendFunds allows sending of money between users if verification is provided in the form of a password and if the user has sufficient funds
|
// // * @brief SendFunds allows sending of money between users if verification is provided in the form of a password and if the user has sufficient funds
|
||||||
*
|
// // *
|
||||||
* @param a first user
|
// // * @param a first user
|
||||||
* @param b second user
|
// // * @param b second user
|
||||||
* @param amount amount being sent
|
// // * @param amount amount being sent
|
||||||
* @param attempt password of first user
|
// // * @param attempt password of first user
|
||||||
* @return wether transaction was succesful
|
// // * @return wether transaction was succesful
|
||||||
*/
|
// // */
|
||||||
static bool SendFunds(User &a, User &b, uint_fast64_t amount, const std::string &attempt)
|
// // static bool SendFunds(User &a, User &b, uint_fast64_t amount, const std::string &attempt)
|
||||||
{
|
// // {
|
||||||
bool state;
|
// // const bool state = (a.password == attempt) && (a.balance >= amount);
|
||||||
{
|
// // if (state)
|
||||||
std::lock_guard<std::mutex> lock{a.pass_lock};
|
// // {
|
||||||
state = (a.password == attempt);
|
// // a.balance -= amount;
|
||||||
}
|
// // b.balance += amount;
|
||||||
|
// // }
|
||||||
|
// // return state;
|
||||||
|
// // }
|
||||||
|
|
||||||
std::scoped_lock<std::mutex, std::mutex> lock{a.bal_lock, b.bal_lock};
|
// /**
|
||||||
state = state && (a.balance >= amount);
|
// * @brief Get the balance of the User object
|
||||||
if (state)
|
// *
|
||||||
{
|
// * @return the balance
|
||||||
a.balance -= amount;
|
// */
|
||||||
b.balance += amount;
|
// uint_fast64_t GetBal() const
|
||||||
}
|
// {
|
||||||
return state;
|
// return balance;
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
// /**
|
||||||
* @brief Get the balance of the User object
|
// * @brief Used for Verification of password by external services, this can be used for logging in or signature
|
||||||
*
|
// *
|
||||||
* @return the balance
|
// * @param attempt the password
|
||||||
*/
|
// * @return wether the passwords match
|
||||||
uint_fast64_t GetBal()
|
// */
|
||||||
{
|
// bool VerifyPassword(const std::string &attempt) const
|
||||||
std::lock_guard<std::mutex> lock{bal_lock};
|
// {
|
||||||
return balance;
|
// return (password == attempt);
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Used for Verification of password by external services, this can be used for logging in or signature
|
|
||||||
*
|
|
||||||
* @param attempt the password
|
|
||||||
* @return wether the passwords match
|
|
||||||
*/
|
|
||||||
bool VerifyPassword(const std::string &attempt) //for connected services
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock{pass_lock};
|
|
||||||
return (password == attempt);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
Loading…
Reference in a new issue