From c7695dd6db178983749933df01b0652e9e232c0a Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 12 Jun 2021 20:39:07 -0700 Subject: [PATCH 01/77] :sparkles: made responses more inuitive for vpass and admin/vpass & fixed send funds bug --- help.md | 24 ++++++++++++------------ include/bank.h | 2 +- src/bank.cpp | 42 +++++++++++++++++++++++++++++------------- src/bank_f.cpp | 2 +- 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/help.md b/help.md index 8d34a56..0650cbf 100644 --- a/help.md +++ b/help.md @@ -15,12 +15,12 @@ * "**A**" denotes requiring Authentication in the form of a header titled "**Password**" # Usage -| Name | Path | Method | A | Description | -| :------------: | :-------------------------------- | :----: | :---: | --------------------------------------------------------------------------------------------------------------------------- | -| GetBal | /{name}/bal | GET | false | returns the balance of a given user `{name}` | -| GetLog | /{name}/log | GET | true | returns a list of last `n` number of transactions (a configurable amount) of a given user `{name}` | -| SendFunds | /{name}/send/{to}/amount={amount} | POST | true | sends `{amount}` from user `{name}` to user `{to}` | -| VerifyPassword | /{name}/pass/verify | GET | true | returns `true` or `false` depending on if the supplied user `{name}`'s password matches the password supplied in the header | +| Name | Path | Method | A | Description | +| :------------: | :-------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------- | +| GetBal | /{name}/bal | GET | false | returns the balance of a given user `{name}` | +| GetLog | /{name}/log | GET | true | returns a list of last `n` number of transactions (a configurable amount) of a given user `{name}` | +| SendFunds | /{name}/send/{to}/amount={amount} | POST | true | sends `{amount}` from user `{name}` to user `{to}` | +| VerifyPassword | /{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header | # Meta Usage | Name | Path | Method | A | Description | @@ -29,12 +29,12 @@ | SetBal | /admin/{name}/bal/amount={amount} | PATCH | true | sets the balance of a give user `{name}` if the supplied password matches the admin password | # System Usage -| Name | Path | Method | A | Description | -| :-------------: | :--------------- | :----: | :---: | -------------------------------------------------------------------------------------------------------- | -| Help | /help | GET | false | the page you're looking at right now! | -| Close | /admin/close | POST | true | saves and then closes the program if the supplied password matches the admin password | -| Contains | /contains/{name} | GET | false | returns `true` or `false` depending on if the supplied user `{name}` exists | -| AdminVerifyPass | /admin/verify | GET | true | returns `true` or `false` depending on if the password supplied in the header matches the admin password | +| Name | Path | Method | A | Description | +| :-------------: | :--------------- | :----: | :---: | ------------------------------------------------------------------------------------- | +| Help | /help | GET | false | the page you're looking at right now! | +| Close | /admin/close | POST | true | saves and then closes the program if the supplied password matches the admin password | +| Contains | /contains/{name} | GET | false | returns `true` or `false` depending on if the supplied user `{name}` exists | +| AdminVerifyPass | /admin/verify | GET | true | returns `1` if the password supplied in the header matches the admin password | # User Management | Name | Path | Method | A | Description | diff --git a/include/bank.h b/include/bank.h index 1408233..81a9554 100644 --- a/include/bank.h +++ b/include/bank.h @@ -42,7 +42,7 @@ public: int_fast8_t SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, const std::string &attempt); bool Contains(const std::string &name) const; - bool AdminVerifyPass(const std::string &attempt); + int_fast8_t AdminVerifyPass(const std::string &attempt); int_fast8_t SetBal(const std::string &name, const std::string &attempt, uint32_t amount); int_fast64_t GetBal(const std::string &name) const; diff --git a/src/bank.cpp b/src/bank.cpp index 59a402c..ad18521 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -27,12 +27,12 @@ int_fast8_t Bank::AdminAddUser(const std::string &attempt, std::string &&name, u } if (admin_pass != attempt) { - return ErrorResponse::WrongAdminPassword; + return ErrorResponse::WrongPassword; } { std::shared_lock lock{size_l}; if (users.try_emplace_l( - name, [](User &) {}, std::move(init_pass))) + name, [](User &) {}, init_bal, std::move(init_pass))) { return true; } @@ -58,7 +58,7 @@ int_fast8_t Bank::DelUser(const std::string &name, const std::string &attempt) } else { - return ErrorResponse::WrongAdminPassword; + return ErrorResponse::WrongPassword; } } } @@ -78,7 +78,7 @@ int_fast8_t Bank::AdminDelUser(const std::string &name, const std::string &attem } else { - return ErrorResponse::WrongAdminPassword; + return ErrorResponse::WrongPassword; } } } @@ -90,7 +90,6 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name { return ErrorResponse::InvalidRequest; } - int_fast8_t state = false; { std::shared_lock lock{send_funds_l}; //because SendFunds requires 3 locking operations @@ -113,10 +112,6 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name } } })) - { - return ErrorResponse::UserNotFound; - } - else { if (state) { @@ -152,6 +147,13 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name return state; } } + else + { + { + std::cout << "b\n"; + return ErrorResponse::UserNotFound; + } + } } } @@ -159,16 +161,23 @@ bool Bank::Contains(const std::string &name) const { return users.contains(name); } -bool Bank::AdminVerifyPass(const std::string &attempt) +int_fast8_t Bank::AdminVerifyPass(const std::string &attempt) { - return (admin_pass == attempt); + if (admin_pass == attempt) + { + return true; + } + else + { + return ErrorResponse::WrongPassword; + } } int_fast8_t Bank::SetBal(const std::string &name, const std::string &attempt, uint32_t amount) { if (admin_pass != attempt) { - return ErrorResponse::WrongAdminPassword; + return ErrorResponse::WrongPassword; } if (users.modify_if(name, [amount](User &u) { u.balance = amount; @@ -194,7 +203,14 @@ int_fast8_t Bank::VerifyPassword(const std::string &name, const std::string &att { int_fast8_t res = ErrorResponse::UserNotFound; users.if_contains(name, [&res, &attempt](const User &u) { - res = u.password == XXH3_64bits(attempt.data(), attempt.size()); + if (u.password == XXH3_64bits(attempt.data(), attempt.size())) + { + res = true; + } + else + { + res = ErrorResponse::WrongPassword; + } }); return res; } diff --git a/src/bank_f.cpp b/src/bank_f.cpp index aea592a..23a9643 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -32,7 +32,7 @@ void BankF::Help(req_args) const { auto resp = HttpResponse::newHttpResponse(); auto handlerInfo = app().getHandlersInfo(); - resp->setBody("

Error Responses

# meaning
-1 UserNotFound
-2 WrongPassword
-3 InvalidRequest
-4 WrongAdminPassword
-5 NameTooLong
-6 UserAlreadyExists
-7 InsufficientFunds

Things of Note

  • all endpoints respond with JSON file type
  • "A" denotes requiring Authentication in the form of a header titled "Password"

Usage

Name Path Method A Description
GetBal /{name}/bal GET false returns the balance of a given user {name}
GetLog /{name}/log GET true returns a list of last n number of transactions (a configurable amount) of a given user {name}
SendFunds /{name}/send/{to}/amount={amount} POST true sends {amount} from user {name} to user {to}
VerifyPassword /{name}/pass/verify GET true returns true or false depending on if the supplied user {name}'s password matches the password supplied in the header

Meta Usage

Name Path Method A Description
ChangePassword /{name}/pass/change PATCH true if the password supplied in the header matches the user {name}'s password, the user’s password is changed to the one given in the body
SetBal /admin/{name}/bal/amount={amount} PATCH true sets the balance of a give user {name} if the supplied password matches the admin password

System Usage

Name Path Method A Description
Help /help GET false the page you’re looking at right now!
Close /admin/close POST true saves and then closes the program if the supplied password matches the admin password
Contains /contains/{name} GET false returns true or false depending on if the supplied user {name} exists
AdminVerifyPass /admin/verify GET true returns true or false depending on if the password supplied in the header matches the admin password

User Management

Name Path Method A Description
AddUser /user/{name} POST true registers a user with the name {name}, balance of 0 and a password of the password supplied in the header
AdminAddUser /admin/user/{name}?init_bal={init_bal} POST true if the password supplied in the header matches the admin password, then it registers a user with the name {name}, balance of init_bal and a password supplied by the body of the request
DelUser /user/{name} DELETE true if the password supplied in the header matches the user {name}'s password, then the user is deleted
AdminDelUser /admin/user/{name} DELETE true if the password supplied in the header matches the admin password, then the user is deleted
"); + resp->setBody("

Error Responses

# meaning
-1 UserNotFound
-2 WrongPassword
-3 InvalidRequest
-4 WrongAdminPassword
-5 NameTooLong
-6 UserAlreadyExists
-7 InsufficientFunds

Things of Note

  • all endpoints respond with JSON file type
  • "A" denotes requiring Authentication in the form of a header titled "Password"

Usage

Name Path Method A Description
GetBal /{name}/bal GET false returns the balance of a given user {name}
GetLog /{name}/log GET true returns a list of last n number of transactions (a configurable amount) of a given user {name}
SendFunds /{name}/send/{to}/amount={amount} POST true sends {amount} from user {name} to user {to}
VerifyPassword /{name}/pass/verify GET true returns 1 if the supplied user {name}'s password matches the password supplied in the header

Meta Usage

Name Path Method A Description
ChangePassword /{name}/pass/change PATCH true if the password supplied in the header matches the user {name}'s password, the user’s password is changed to the one given in the body
SetBal /admin/{name}/bal/amount={amount} PATCH true sets the balance of a give user {name} if the supplied password matches the admin password

System Usage

Name Path Method A Description
Help /help GET false the page you’re looking at right now!
Close /admin/close POST true saves and then closes the program if the supplied password matches the admin password
Contains /contains/{name} GET false returns true or false depending on if the supplied user {name} exists
AdminVerifyPass /admin/verify GET true returns 1 if the password supplied in the header matches the admin password

User Management

Name Path Method A Description
AddUser /user/{name} POST true registers a user with the name {name}, balance of 0 and a password of the password supplied in the header
AdminAddUser /admin/user/{name}?init_bal={init_bal} POST true if the password supplied in the header matches the admin password, then it registers a user with the name {name}, balance of init_bal and a password supplied by the body of the request
DelUser /user/{name} DELETE true if the password supplied in the header matches the user {name}'s password, then the user is deleted
AdminDelUser /admin/user/{name} DELETE true if the password supplied in the header matches the admin password, then the user is deleted
"); resp->setExpiredTime(0); callback(resp); } From 3e857f455eebbe6110e9bb2461681f65f4187fd9 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 12 Jun 2021 20:50:46 -0700 Subject: [PATCH 02/77] removed AdminWrongPassword error --- include/bank_f.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/bank_f.h b/include/bank_f.h index 99f8c52..5b17c17 100644 --- a/include/bank_f.h +++ b/include/bank_f.h @@ -13,6 +13,7 @@ class BankF : public HttpController public: BankF(Bank *b); void Help(req_args) const; + void Ping(req_args) const; void Close(req_args) const; void AddUser(req_args, const std::string &name) const; void AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const; @@ -41,6 +42,7 @@ public: //System Usage METHOD_ADD(BankF::Help, "/help", Get, Options); + METHOD_ADD(BankF::Ping, "/ping", Get, Options); METHOD_ADD(BankF::Close, "/admin/close", Post, Options); METHOD_ADD(BankF::Contains, "/contains/{name}", Get, Options); METHOD_ADD(BankF::AdminVerifyPass, "/admin/verify", Get, Options); From 8f47c1245099a4d2c66c23db6ed6cd2d3facbe4c Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 12 Jun 2021 20:53:00 -0700 Subject: [PATCH 03/77] :sparkles: ping function --- help.md | 18 +++++++++--------- src/bank_f.cpp | 8 +++++++- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/help.md b/help.md index 0650cbf..00ae6aa 100644 --- a/help.md +++ b/help.md @@ -1,14 +1,13 @@ # Error Responses -| # | meaning | -| --- | ------------------ | -| -1 | UserNotFound | -| -2 | WrongPassword | -| -3 | InvalidRequest | -| -4 | WrongAdminPassword | -| -5 | NameTooLong | -| -6 | UserAlreadyExists | -| -7 | InsufficientFunds | +| # | meaning | +| --- | ----------------- | +| -1 | UserNotFound | +| -2 | WrongPassword | +| -3 | InvalidRequest | +| -4 | NameTooLong | +| -5 | UserAlreadyExists | +| -6 | InsufficientFunds | # Things of Note * all endpoints respond with **JSON** file type @@ -32,6 +31,7 @@ | Name | Path | Method | A | Description | | :-------------: | :--------------- | :----: | :---: | ------------------------------------------------------------------------------------- | | Help | /help | GET | false | the page you're looking at right now! | +| Ping | /ping | GET | false | for pinging the server to see if its online | | Close | /admin/close | POST | true | saves and then closes the program if the supplied password matches the admin password | | Contains | /contains/{name} | GET | false | returns `true` or `false` depending on if the supplied user `{name}` exists | | AdminVerifyPass | /admin/verify | GET | true | returns `1` if the password supplied in the header matches the admin password | diff --git a/src/bank_f.cpp b/src/bank_f.cpp index 23a9643..548067c 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -31,7 +31,6 @@ BankF::BankF(Bank *b) : bank(*b) {} void BankF::Help(req_args) const { auto resp = HttpResponse::newHttpResponse(); - auto handlerInfo = app().getHandlersInfo(); resp->setBody("

Error Responses

# meaning
-1 UserNotFound
-2 WrongPassword
-3 InvalidRequest
-4 WrongAdminPassword
-5 NameTooLong
-6 UserAlreadyExists
-7 InsufficientFunds

Things of Note

  • all endpoints respond with JSON file type
  • "A" denotes requiring Authentication in the form of a header titled "Password"

Usage

Name Path Method A Description
GetBal /{name}/bal GET false returns the balance of a given user {name}
GetLog /{name}/log GET true returns a list of last n number of transactions (a configurable amount) of a given user {name}
SendFunds /{name}/send/{to}/amount={amount} POST true sends {amount} from user {name} to user {to}
VerifyPassword /{name}/pass/verify GET true returns 1 if the supplied user {name}'s password matches the password supplied in the header

Meta Usage

Name Path Method A Description
ChangePassword /{name}/pass/change PATCH true if the password supplied in the header matches the user {name}'s password, the user’s password is changed to the one given in the body
SetBal /admin/{name}/bal/amount={amount} PATCH true sets the balance of a give user {name} if the supplied password matches the admin password

System Usage

Name Path Method A Description
Help /help GET false the page you’re looking at right now!
Close /admin/close POST true saves and then closes the program if the supplied password matches the admin password
Contains /contains/{name} GET false returns true or false depending on if the supplied user {name} exists
AdminVerifyPass /admin/verify GET true returns 1 if the password supplied in the header matches the admin password

User Management

Name Path Method A Description
AddUser /user/{name} POST true registers a user with the name {name}, balance of 0 and a password of the password supplied in the header
AdminAddUser /admin/user/{name}?init_bal={init_bal} POST true if the password supplied in the header matches the admin password, then it registers a user with the name {name}, balance of init_bal and a password supplied by the body of the request
DelUser /user/{name} DELETE true if the password supplied in the header matches the user {name}'s password, then the user is deleted
AdminDelUser /admin/user/{name} DELETE true if the password supplied in the header matches the admin password, then the user is deleted
"); resp->setExpiredTime(0); callback(resp); @@ -52,6 +51,13 @@ void BankF::Close(req_args) const } JSON(res); } +void BankF::Ping(req_args) const +{ + auto resp = HttpResponse::newHttpResponse(); + resp->setBody("pong"); + resp->setExpiredTime(0); + callback(resp); +} void BankF::AddUser(req_args, const std::string &name) const { JSON(bank.AddUser(std::move(name), PASS_HEADER)); From 32798356966e7c5321e4f0c1e4f7075c844d92d4 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 12 Jun 2021 20:56:04 -0700 Subject: [PATCH 04/77] :racehorse: contains check for SendFunds before unique grabbed --- src/bank.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/bank.cpp b/src/bank.cpp index cb10581..01161fd 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -90,6 +90,12 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name { return ErrorResponse::InvalidRequest; } + //as first modify_if checks a_name and grabs unique lock + if (!Contains(b_name)) + { + return ErrorResponse::UserNotFound; + } + int_fast8_t state = false; { std::shared_lock lock{send_funds_l}; //because SendFunds requires 3 locking operations From c9c9bee5c69e7d4f9619619f0febbff1d938d545 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 12 Jun 2021 21:08:31 -0700 Subject: [PATCH 05/77] update APIs list --- APIs.md | 12 ++++++++++-- README.md | 10 +++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/APIs.md b/APIs.md index 9ae0aff..b1c262c 100644 --- a/APIs.md +++ b/APIs.md @@ -1,2 +1,10 @@ -[CC API](https://github.com/Reactified/rpm/blob/main/packages/ccash-api/api.lua) -[Python API](https://github.com/fearlessdoggo21/ccashpythonclient) +# Language Specific APIs + +## Complete + +## In Dev +* [CC API](https://github.com/Reactified/rpm/blob/main/packages/ccash-api/api.lua) +* [Python API](https://github.com/fearlessdoggo21/ccashpythonclient) +* [Rust API](https://git.stboyden.com/STBoyden/ccash-rs) +* [JS API](https://github.com/LukeeeeBennett/ccash-client-js) +* [CS API](https://github.com/Soverclysm/CCash-dotnet-api) \ No newline at end of file diff --git a/README.md b/README.md index f8c9e60..e65e2d9 100644 --- a/README.md +++ b/README.md @@ -54,11 +54,11 @@ Go to [here](help.md) to see the API's endpoints. Using the Bank's API allows (y ## [Contributions](https://github.com/EntireTwix/CCash/graphs/contributors) Thank you to the contributors -| Name | Work | -| :------------------------------------------ | ----------------------------------------------------------------- | -| [Expand](https://github.com/Expand-sys) | Frontend | -| [React](https://github.com/Reactified) | CC {API, Shops, and ATM, Logo} | -| [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, Python API, convention suggestions | +| Name | Work | +| :------------------------------------------ | ---------------------------------------------------------- | +| [Expand](https://github.com/Expand-sys) | Frontend | +| [React](https://github.com/Reactified) | CC {API, Shops, and ATM, Logo} | +| [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | ## Features From 47accc762bcfa112fe66c79dfb86c00bd243085e Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 12 Jun 2021 21:18:37 -0700 Subject: [PATCH 06/77] changed API list order --- APIs.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/APIs.md b/APIs.md index b1c262c..d39a8a7 100644 --- a/APIs.md +++ b/APIs.md @@ -3,8 +3,8 @@ ## Complete ## In Dev +* [JS API](https://github.com/LukeeeeBennett/ccash-client-js) * [CC API](https://github.com/Reactified/rpm/blob/main/packages/ccash-api/api.lua) * [Python API](https://github.com/fearlessdoggo21/ccashpythonclient) -* [Rust API](https://git.stboyden.com/STBoyden/ccash-rs) -* [JS API](https://github.com/LukeeeeBennett/ccash-client-js) -* [CS API](https://github.com/Soverclysm/CCash-dotnet-api) \ No newline at end of file +* [CS API](https://github.com/Soverclysm/CCash-dotnet-api) +* [Rust API](https://git.stboyden.com/STBoyden/ccash-rs) \ No newline at end of file From c9fedf3228a633e5ca859375ae51204c99d47335 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 12 Jun 2021 21:27:09 -0700 Subject: [PATCH 07/77] added \"BankF\" to paths --- help.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/help.md b/help.md index 00ae6aa..c85cf77 100644 --- a/help.md +++ b/help.md @@ -22,24 +22,24 @@ | VerifyPassword | /{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header | # Meta Usage -| Name | Path | Method | A | Description | -| :------------: | :-------------------------------- | :----: | :---: | ---------------------------------------------------------------------------------------------------------------------------------------- | -| ChangePassword | /{name}/pass/change | PATCH | true | if the password supplied in the header matches the user `{name}`'s password, the user's password is changed to the one given in the body | -| SetBal | /admin/{name}/bal/amount={amount} | PATCH | true | sets the balance of a give user `{name}` if the supplied password matches the admin password | +| Name | Path | Method | A | Description | +| :------------: | :------------------------------------- | :----: | :---: | ---------------------------------------------------------------------------------------------------------------------------------------- | +| ChangePassword | BankF/{name}/pass/change | PATCH | true | if the password supplied in the header matches the user `{name}`'s password, the user's password is changed to the one given in the body | +| SetBal | BankF/admin/{name}/bal/amount={amount} | PATCH | true | sets the balance of a give user `{name}` if the supplied password matches the admin password | # System Usage -| Name | Path | Method | A | Description | -| :-------------: | :--------------- | :----: | :---: | ------------------------------------------------------------------------------------- | -| Help | /help | GET | false | the page you're looking at right now! | -| Ping | /ping | GET | false | for pinging the server to see if its online | -| Close | /admin/close | POST | true | saves and then closes the program if the supplied password matches the admin password | -| Contains | /contains/{name} | GET | false | returns `true` or `false` depending on if the supplied user `{name}` exists | -| AdminVerifyPass | /admin/verify | GET | true | returns `1` if the password supplied in the header matches the admin password | +| Name | Path | Method | A | Description | +| :-------------: | :-------------------- | :----: | :---: | ------------------------------------------------------------------------------------- | +| Help | BankF/help | GET | false | the page you're looking at right now! | +| Ping | BankF/ping | GET | false | for pinging the server to see if its online | +| Close | BankF/admin/close | POST | true | saves and then closes the program if the supplied password matches the admin password | +| Contains | BankF/contains/{name} | GET | false | returns `true` or `false` depending on if the supplied user `{name}` exists | +| AdminVerifyPass | BankF/admin/verify | GET | true | returns `1` if the password supplied in the header matches the admin password | # User Management -| Name | Path | Method | A | Description | -| :----------: | :------------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| AddUser | /user/{name} | POST | true | registers a user with the name `{name}`, balance of 0 and a password of the password supplied in the header | -| AdminAddUser | /admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password supplied by the body of the request | -| DelUser | /user/{name} | DELETE | true | if the password supplied in the header matches the user `{name}`'s password, then the user is deleted | -| AdminDelUser | /admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted | \ No newline at end of file +| Name | Path | Method | A | Description | +| :----------: | :------------------------------------------ | :----: | :---: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| AddUser | BankF/user/{name} | POST | true | registers a user with the name `{name}`, balance of 0 and a password of the password supplied in the header | +| AdminAddUser | BankF/admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password supplied by the body of the request | +| DelUser | BankF/user/{name} | DELETE | true | if the password supplied in the header matches the user `{name}`'s password, then the user is deleted | +| AdminDelUser | BankF/admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted | \ No newline at end of file From 50d48010746b7f73774d5e769e2422c0bcec75d2 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 12 Jun 2021 21:30:13 -0700 Subject: [PATCH 08/77] updated paths --- help.md | 12 ++++++------ src/bank_f.cpp | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/help.md b/help.md index c85cf77..70ce25f 100644 --- a/help.md +++ b/help.md @@ -14,12 +14,12 @@ * "**A**" denotes requiring Authentication in the form of a header titled "**Password**" # Usage -| Name | Path | Method | A | Description | -| :------------: | :-------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------- | -| GetBal | /{name}/bal | GET | false | returns the balance of a given user `{name}` | -| GetLog | /{name}/log | GET | true | returns a list of last `n` number of transactions (a configurable amount) of a given user `{name}` | -| SendFunds | /{name}/send/{to}/amount={amount} | POST | true | sends `{amount}` from user `{name}` to user `{to}` | -| VerifyPassword | /{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header | +| Name | Path | Method | A | Description | +| :------------: | :------------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------- | +| GetBal | BankF/{name}/bal | GET | false | returns the balance of a given user `{name}` | +| GetLog | BankF/{name}/log | GET | true | returns a list of last `n` number of transactions (a configurable amount) of a given user `{name}` | +| SendFunds | BankF/{name}/send/{to}/amount={amount} | POST | true | sends `{amount}` from user `{name}` to user `{to}` | +| VerifyPassword | BankF/{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header | # Meta Usage | Name | Path | Method | A | Description | diff --git a/src/bank_f.cpp b/src/bank_f.cpp index 548067c..e1f1960 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -31,7 +31,7 @@ BankF::BankF(Bank *b) : bank(*b) {} void BankF::Help(req_args) const { auto resp = HttpResponse::newHttpResponse(); - resp->setBody("

Error Responses

# meaning
-1 UserNotFound
-2 WrongPassword
-3 InvalidRequest
-4 WrongAdminPassword
-5 NameTooLong
-6 UserAlreadyExists
-7 InsufficientFunds

Things of Note

  • all endpoints respond with JSON file type
  • "A" denotes requiring Authentication in the form of a header titled "Password"

Usage

Name Path Method A Description
GetBal /{name}/bal GET false returns the balance of a given user {name}
GetLog /{name}/log GET true returns a list of last n number of transactions (a configurable amount) of a given user {name}
SendFunds /{name}/send/{to}/amount={amount} POST true sends {amount} from user {name} to user {to}
VerifyPassword /{name}/pass/verify GET true returns 1 if the supplied user {name}'s password matches the password supplied in the header

Meta Usage

Name Path Method A Description
ChangePassword /{name}/pass/change PATCH true if the password supplied in the header matches the user {name}'s password, the user’s password is changed to the one given in the body
SetBal /admin/{name}/bal/amount={amount} PATCH true sets the balance of a give user {name} if the supplied password matches the admin password

System Usage

Name Path Method A Description
Help /help GET false the page you’re looking at right now!
Close /admin/close POST true saves and then closes the program if the supplied password matches the admin password
Contains /contains/{name} GET false returns true or false depending on if the supplied user {name} exists
AdminVerifyPass /admin/verify GET true returns 1 if the password supplied in the header matches the admin password

User Management

Name Path Method A Description
AddUser /user/{name} POST true registers a user with the name {name}, balance of 0 and a password of the password supplied in the header
AdminAddUser /admin/user/{name}?init_bal={init_bal} POST true if the password supplied in the header matches the admin password, then it registers a user with the name {name}, balance of init_bal and a password supplied by the body of the request
DelUser /user/{name} DELETE true if the password supplied in the header matches the user {name}'s password, then the user is deleted
AdminDelUser /admin/user/{name} DELETE true if the password supplied in the header matches the admin password, then the user is deleted
"); + resp->setBody("# Meta Usage

Error Responses

# meaning
-1 UserNotFound
-2 WrongPassword
-3 InvalidRequest
-4 NameTooLong
-5 UserAlreadyExists
-6 InsufficientFunds

Things of Note

  • all endpoints respond with JSON file type
  • "A" denotes requiring Authentication in the form of a header titled "Password"

Usage

Name Path Method A Description
GetBal BankF/{name}/bal GET false returns the balance of a given user {name}
GetLog BankF/{name}/log GET true returns a list of last n number of transactions (a configurable amount) of a given user {name}
SendFunds BankF/{name}/send/{to}/amount={amount} POST true sends {amount} from user {name} to user {to}
VerifyPassword BankF/{name}/pass/verify GET true returns 1 if the supplied user {name}'s password matches the password supplied in the header

Meta Usage

Name Path Method A Description
ChangePassword BankF/{name}/pass/change PATCH true if the password supplied in the header matches the user {name}'s password, the user’s password is changed to the one given in the body
SetBal BankF/admin/{name}/bal/amount={amount} PATCH true sets the balance of a give user {name} if the supplied password matches the admin password

System Usage

Name Path Method A Description
Help BankF/help GET false the page you’re looking at right now!
Ping BankF/ping GET false for pinging the server to see if its online
Close BankF/admin/close POST true saves and then closes the program if the supplied password matches the admin password
Contains BankF/contains/{name} GET false returns true or false depending on if the supplied user {name} exists
AdminVerifyPass BankF/admin/verify GET true returns 1 if the password supplied in the header matches the admin password

User Management

Name Path Method A Description
AddUser BankF/user/{name} POST true registers a user with the name {name}, balance of 0 and a password of the password supplied in the header
AdminAddUser BankF/admin/user/{name}?init_bal={init_bal} POST true if the password supplied in the header matches the admin password, then it registers a user with the name {name}, balance of init_bal and a password supplied by the body of the request
DelUser BankF/user/{name} DELETE true if the password supplied in the header matches the user {name}'s password, then the user is deleted
AdminDelUser BankF/admin/user/{name} DELETE true if the password supplied in the header matches the admin password, then the user is deleted
"); resp->setExpiredTime(0); callback(resp); } From 56bde9b341b706b6664181f8e5862726f5e145c5 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 12 Jun 2021 21:37:19 -0700 Subject: [PATCH 09/77] updated --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e65e2d9..120e215 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,10 @@ sudo ./bank ## Connected Services -Go to [here](help.md) to see the API's endpoints. Using the Bank's API allows (you/others) to (make/use) connected services that utilize the bank, a couple ideas can be found [here](services.md) +Using the Bank's API allows (you/others) to (make/use) connected services that utilize the bank, a couple ideas can be found [here](services.md) + +Go to [here](help.md) to see the API's endpoints. +Language specific APIs can be found [here](APIs.md). ## FAQ **Q:** how is money initially injected into the economy From 9b9d5cf430f2662f18d2e7535ffd558156b0a9c1 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 12 Jun 2021 22:34:19 -0700 Subject: [PATCH 10/77] made Contains return -1 instead of 0 in false case --- help.md | 2 +- include/bank.h | 2 +- src/bank.cpp | 11 +++++++++-- src/bank_f.cpp | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/help.md b/help.md index 70ce25f..c4a15ef 100644 --- a/help.md +++ b/help.md @@ -33,7 +33,7 @@ | Help | BankF/help | GET | false | the page you're looking at right now! | | Ping | BankF/ping | GET | false | for pinging the server to see if its online | | Close | BankF/admin/close | POST | true | saves and then closes the program if the supplied password matches the admin password | -| Contains | BankF/contains/{name} | GET | false | returns `true` or `false` depending on if the supplied user `{name}` exists | +| Contains | BankF/contains/{name} | GET | false | returns `1` if the supplied user `{name}` exists | | AdminVerifyPass | BankF/admin/verify | GET | true | returns `1` if the password supplied in the header matches the admin password | # User Management diff --git a/include/bank.h b/include/bank.h index 81a9554..35ec3cd 100644 --- a/include/bank.h +++ b/include/bank.h @@ -41,7 +41,7 @@ public: int_fast8_t SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, const std::string &attempt); - bool Contains(const std::string &name) const; + int_fast8_t Contains(const std::string &name) const; int_fast8_t AdminVerifyPass(const std::string &attempt); int_fast8_t SetBal(const std::string &name, const std::string &attempt, uint32_t amount); diff --git a/src/bank.cpp b/src/bank.cpp index 01161fd..ba2403b 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -163,9 +163,16 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name } } -bool Bank::Contains(const std::string &name) const +int_fast8_t Bank::Contains(const std::string &name) const { - return users.contains(name); + if (users.contains(name)) + { + return true; + } + else + { + return ErrorResponse::UserNotFound; + } } int_fast8_t Bank::AdminVerifyPass(const std::string &attempt) { diff --git a/src/bank_f.cpp b/src/bank_f.cpp index e1f1960..9cfe056 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -31,7 +31,7 @@ BankF::BankF(Bank *b) : bank(*b) {} void BankF::Help(req_args) const { auto resp = HttpResponse::newHttpResponse(); - resp->setBody("# Meta Usage

Error Responses

# meaning
-1 UserNotFound
-2 WrongPassword
-3 InvalidRequest
-4 NameTooLong
-5 UserAlreadyExists
-6 InsufficientFunds

Things of Note

  • all endpoints respond with JSON file type
  • "A" denotes requiring Authentication in the form of a header titled "Password"

Usage

Name Path Method A Description
GetBal BankF/{name}/bal GET false returns the balance of a given user {name}
GetLog BankF/{name}/log GET true returns a list of last n number of transactions (a configurable amount) of a given user {name}
SendFunds BankF/{name}/send/{to}/amount={amount} POST true sends {amount} from user {name} to user {to}
VerifyPassword BankF/{name}/pass/verify GET true returns 1 if the supplied user {name}'s password matches the password supplied in the header

Meta Usage

Name Path Method A Description
ChangePassword BankF/{name}/pass/change PATCH true if the password supplied in the header matches the user {name}'s password, the user’s password is changed to the one given in the body
SetBal BankF/admin/{name}/bal/amount={amount} PATCH true sets the balance of a give user {name} if the supplied password matches the admin password

System Usage

Name Path Method A Description
Help BankF/help GET false the page you’re looking at right now!
Ping BankF/ping GET false for pinging the server to see if its online
Close BankF/admin/close POST true saves and then closes the program if the supplied password matches the admin password
Contains BankF/contains/{name} GET false returns true or false depending on if the supplied user {name} exists
AdminVerifyPass BankF/admin/verify GET true returns 1 if the password supplied in the header matches the admin password

User Management

Name Path Method A Description
AddUser BankF/user/{name} POST true registers a user with the name {name}, balance of 0 and a password of the password supplied in the header
AdminAddUser BankF/admin/user/{name}?init_bal={init_bal} POST true if the password supplied in the header matches the admin password, then it registers a user with the name {name}, balance of init_bal and a password supplied by the body of the request
DelUser BankF/user/{name} DELETE true if the password supplied in the header matches the user {name}'s password, then the user is deleted
AdminDelUser BankF/admin/user/{name} DELETE true if the password supplied in the header matches the admin password, then the user is deleted
"); + resp->setBody("# Meta Usage

Error Responses

# meaning
-1 UserNotFound
-2 WrongPassword
-3 InvalidRequest
-4 NameTooLong
-5 UserAlreadyExists
-6 InsufficientFunds

Things of Note

  • all endpoints respond with JSON file type
  • "A" denotes requiring Authentication in the form of a header titled "Password"

Usage

Name Path Method A Description
GetBal BankF/{name}/bal GET false returns the balance of a given user {name}
GetLog BankF/{name}/log GET true returns a list of last n number of transactions (a configurable amount) of a given user {name}
SendFunds BankF/{name}/send/{to}/amount={amount} POST true sends {amount} from user {name} to user {to}
VerifyPassword BankF/{name}/pass/verify GET true returns 1 if the supplied user {name}'s password matches the password supplied in the header

Meta Usage

Name Path Method A Description
ChangePassword BankF/{name}/pass/change PATCH true if the password supplied in the header matches the user {name}'s password, the user’s password is changed to the one given in the body
SetBal BankF/admin/{name}/bal/amount={amount} PATCH true sets the balance of a give user {name} if the supplied password matches the admin password

System Usage

Name Path Method A Description
Help BankF/help GET false the page you’re looking at right now!
Ping BankF/ping GET false for pinging the server to see if its online
Close BankF/admin/close POST true saves and then closes the program if the supplied password matches the admin password
Contains BankF/contains/{name} GET false returns 1 if the supplied user {name} exists
AdminVerifyPass BankF/admin/verify GET true returns 1 if the password supplied in the header matches the admin password

User Management

Name Path Method A Description
AddUser BankF/user/{name} POST true registers a user with the name {name}, balance of 0 and a password of the password supplied in the header
AdminAddUser BankF/admin/user/{name}?init_bal={init_bal} POST true if the password supplied in the header matches the admin password, then it registers a user with the name {name}, balance of init_bal and a password supplied by the body of the request
DelUser BankF/user/{name} DELETE true if the password supplied in the header matches the user {name}'s password, then the user is deleted
AdminDelUser BankF/admin/user/{name} DELETE true if the password supplied in the header matches the admin password, then the user is deleted
"); resp->setExpiredTime(0); callback(resp); } From 3649eb2c95aec8d2e55d35110bf0e1c85254ef38 Mon Sep 17 00:00:00 2001 From: Luke Bennett Date: Sun, 13 Jun 2021 02:36:50 +0100 Subject: [PATCH 11/77] docs: fix param syntax and improve body desc --- help.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/help.md b/help.md index c4a15ef..feaa136 100644 --- a/help.md +++ b/help.md @@ -24,7 +24,7 @@ # Meta Usage | Name | Path | Method | A | Description | | :------------: | :------------------------------------- | :----: | :---: | ---------------------------------------------------------------------------------------------------------------------------------------- | -| ChangePassword | BankF/{name}/pass/change | PATCH | true | if the password supplied in the header matches the user `{name}`'s password, the user's password is changed to the one given in the body | +| ChangePassword | BankF/{name}/pass/change | PATCH | true | if the password supplied in the header matches the user `{name}`'s password, the user's password is changed to the one given in the body, with a key of `password`. i.e. { "password": "NewPassword" } | | SetBal | BankF/admin/{name}/bal/amount={amount} | PATCH | true | sets the balance of a give user `{name}` if the supplied password matches the admin password | # System Usage @@ -40,6 +40,6 @@ | Name | Path | Method | A | Description | | :----------: | :------------------------------------------ | :----: | :---: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | AddUser | BankF/user/{name} | POST | true | registers a user with the name `{name}`, balance of 0 and a password of the password supplied in the header | -| AdminAddUser | BankF/admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password supplied by the body of the request | +| AdminAddUser | BankF/admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password that is supplied in the body of the request. i.e. { "password": "ThePassword" } | | DelUser | BankF/user/{name} | DELETE | true | if the password supplied in the header matches the user `{name}`'s password, then the user is deleted | -| AdminDelUser | BankF/admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted | \ No newline at end of file +| AdminDelUser | BankF/admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted | From d90f0dc9ba1f4a7c017149056f0ea823791736b4 Mon Sep 17 00:00:00 2001 From: Luke Bennett Date: Sun, 13 Jun 2021 21:52:58 +0100 Subject: [PATCH 12/77] docs: format help.md with prettier --- help.md | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/help.md b/help.md index feaa136..f14f439 100644 --- a/help.md +++ b/help.md @@ -10,10 +10,12 @@ | -6 | InsufficientFunds | # Things of Note -* all endpoints respond with **JSON** file type -* "**A**" denotes requiring Authentication in the form of a header titled "**Password**" + +- all endpoints respond with **JSON** file type +- "**A**" denotes requiring Authentication in the form of a header titled "**Password**" # Usage + | Name | Path | Method | A | Description | | :------------: | :------------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------- | | GetBal | BankF/{name}/bal | GET | false | returns the balance of a given user `{name}` | @@ -22,12 +24,14 @@ | VerifyPassword | BankF/{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header | # Meta Usage -| Name | Path | Method | A | Description | -| :------------: | :------------------------------------- | :----: | :---: | ---------------------------------------------------------------------------------------------------------------------------------------- | -| ChangePassword | BankF/{name}/pass/change | PATCH | true | if the password supplied in the header matches the user `{name}`'s password, the user's password is changed to the one given in the body, with a key of `password`. i.e. { "password": "NewPassword" } | -| SetBal | BankF/admin/{name}/bal/amount={amount} | PATCH | true | sets the balance of a give user `{name}` if the supplied password matches the admin password | + +| Name | Path | Method | A | Description | +| :------------: | :------------------------------------- | :----: | :--: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| ChangePassword | BankF/{name}/pass/change | PATCH | true | if the password supplied in the header matches the user `{name}`'s password, the user's password is changed to the one given in the body, with a key of `password`. i.e. { "password": "NewPassword" } | +| SetBal | BankF/admin/{name}/bal/amount={amount} | PATCH | true | sets the balance of a give user `{name}` if the supplied password matches the admin password | # System Usage + | Name | Path | Method | A | Description | | :-------------: | :-------------------- | :----: | :---: | ------------------------------------------------------------------------------------- | | Help | BankF/help | GET | false | the page you're looking at right now! | @@ -37,9 +41,10 @@ | AdminVerifyPass | BankF/admin/verify | GET | true | returns `1` if the password supplied in the header matches the admin password | # User Management -| Name | Path | Method | A | Description | -| :----------: | :------------------------------------------ | :----: | :---: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| AddUser | BankF/user/{name} | POST | true | registers a user with the name `{name}`, balance of 0 and a password of the password supplied in the header | -| AdminAddUser | BankF/admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password that is supplied in the body of the request. i.e. { "password": "ThePassword" } | -| DelUser | BankF/user/{name} | DELETE | true | if the password supplied in the header matches the user `{name}`'s password, then the user is deleted | -| AdminDelUser | BankF/admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted | + +| Name | Path | Method | A | Description | +| :----------: | :------------------------------------------ | :----: | :--: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| AddUser | BankF/user/{name} | POST | true | registers a user with the name `{name}`, balance of 0 and a password of the password supplied in the header | +| AdminAddUser | BankF/admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password that is supplied in the body of the request. i.e. { "password": "ThePassword" } | +| DelUser | BankF/user/{name} | DELETE | true | if the password supplied in the header matches the user `{name}`'s password, then the user is deleted | +| AdminDelUser | BankF/admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted | From a988dab36121953fb4f4986ec8b7cb79eccd7af6 Mon Sep 17 00:00:00 2001 From: Luke Bennett Date: Sun, 13 Jun 2021 21:54:47 +0100 Subject: [PATCH 13/77] docs(help): explain where log length is configured --- help.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/help.md b/help.md index f14f439..8db4393 100644 --- a/help.md +++ b/help.md @@ -19,7 +19,7 @@ | Name | Path | Method | A | Description | | :------------: | :------------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------- | | GetBal | BankF/{name}/bal | GET | false | returns the balance of a given user `{name}` | -| GetLog | BankF/{name}/log | GET | true | returns a list of last `n` number of transactions (a configurable amount) of a given user `{name}` | +| GetLog | BankF/{name}/log | GET | true | returns a list of last `n` number of transactions (configurable in CCash webserver) of a given user `{name}` | | SendFunds | BankF/{name}/send/{to}/amount={amount} | POST | true | sends `{amount}` from user `{name}` to user `{to}` | | VerifyPassword | BankF/{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header | From ce16d5f5aeba452c6432cffc659ea4d3d48761a9 Mon Sep 17 00:00:00 2001 From: Luke Bennett Date: Sun, 13 Jun 2021 21:55:00 +0100 Subject: [PATCH 14/77] docs: format help.md with prettier --- help.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/help.md b/help.md index 8db4393..d91ee74 100644 --- a/help.md +++ b/help.md @@ -16,12 +16,12 @@ # Usage -| Name | Path | Method | A | Description | -| :------------: | :------------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------- | -| GetBal | BankF/{name}/bal | GET | false | returns the balance of a given user `{name}` | +| Name | Path | Method | A | Description | +| :------------: | :------------------------------------- | :----: | :---: | ------------------------------------------------------------------------------------------------------------ | +| GetBal | BankF/{name}/bal | GET | false | returns the balance of a given user `{name}` | | GetLog | BankF/{name}/log | GET | true | returns a list of last `n` number of transactions (configurable in CCash webserver) of a given user `{name}` | -| SendFunds | BankF/{name}/send/{to}/amount={amount} | POST | true | sends `{amount}` from user `{name}` to user `{to}` | -| VerifyPassword | BankF/{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header | +| SendFunds | BankF/{name}/send/{to}/amount={amount} | POST | true | sends `{amount}` from user `{name}` to user `{to}` | +| VerifyPassword | BankF/{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header | # Meta Usage From 4ce87f4aa13f3a9dc6575bb5b9c2e63c1b33b9ff Mon Sep 17 00:00:00 2001 From: Luke Bennett Date: Sun, 13 Jun 2021 22:02:37 +0100 Subject: [PATCH 15/77] docs(help): correct password as body explaination --- help.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/help.md b/help.md index d91ee74..d30e3df 100644 --- a/help.md +++ b/help.md @@ -27,7 +27,7 @@ | Name | Path | Method | A | Description | | :------------: | :------------------------------------- | :----: | :--: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| ChangePassword | BankF/{name}/pass/change | PATCH | true | if the password supplied in the header matches the user `{name}`'s password, the user's password is changed to the one given in the body, with a key of `password`. i.e. { "password": "NewPassword" } | +| ChangePassword | BankF/{name}/pass/change | PATCH | true | if the password supplied in the header matches the user `{name}`'s password, the user's password is changed to the one given as the body | | SetBal | BankF/admin/{name}/bal/amount={amount} | PATCH | true | sets the balance of a give user `{name}` if the supplied password matches the admin password | # System Usage @@ -45,6 +45,6 @@ | Name | Path | Method | A | Description | | :----------: | :------------------------------------------ | :----: | :--: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | AddUser | BankF/user/{name} | POST | true | registers a user with the name `{name}`, balance of 0 and a password of the password supplied in the header | -| AdminAddUser | BankF/admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password that is supplied in the body of the request. i.e. { "password": "ThePassword" } | +| AdminAddUser | BankF/admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password that is supplied as the body of the request | | DelUser | BankF/user/{name} | DELETE | true | if the password supplied in the header matches the user `{name}`'s password, then the user is deleted | | AdminDelUser | BankF/admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted | From d99e8b1adfa7d410909942705246b292a5b679cb Mon Sep 17 00:00:00 2001 From: Luke Bennett Date: Sun, 13 Jun 2021 22:02:46 +0100 Subject: [PATCH 16/77] docs: format help.md with prettier --- help.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/help.md b/help.md index d30e3df..543833e 100644 --- a/help.md +++ b/help.md @@ -25,10 +25,10 @@ # Meta Usage -| Name | Path | Method | A | Description | -| :------------: | :------------------------------------- | :----: | :--: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| Name | Path | Method | A | Description | +| :------------: | :------------------------------------- | :----: | :--: | ---------------------------------------------------------------------------------------------------------------------------------------- | | ChangePassword | BankF/{name}/pass/change | PATCH | true | if the password supplied in the header matches the user `{name}`'s password, the user's password is changed to the one given as the body | -| SetBal | BankF/admin/{name}/bal/amount={amount} | PATCH | true | sets the balance of a give user `{name}` if the supplied password matches the admin password | +| SetBal | BankF/admin/{name}/bal/amount={amount} | PATCH | true | sets the balance of a give user `{name}` if the supplied password matches the admin password | # System Usage @@ -42,9 +42,9 @@ # User Management -| Name | Path | Method | A | Description | -| :----------: | :------------------------------------------ | :----: | :--: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| AddUser | BankF/user/{name} | POST | true | registers a user with the name `{name}`, balance of 0 and a password of the password supplied in the header | +| Name | Path | Method | A | Description | +| :----------: | :------------------------------------------ | :----: | :--: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| AddUser | BankF/user/{name} | POST | true | registers a user with the name `{name}`, balance of 0 and a password of the password supplied in the header | | AdminAddUser | BankF/admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password that is supplied as the body of the request | -| DelUser | BankF/user/{name} | DELETE | true | if the password supplied in the header matches the user `{name}`'s password, then the user is deleted | -| AdminDelUser | BankF/admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted | +| DelUser | BankF/user/{name} | DELETE | true | if the password supplied in the header matches the user `{name}`'s password, then the user is deleted | +| AdminDelUser | BankF/admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted | From 67a8642245250c05ac08f556bd4e8b69965e03c4 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Mon, 14 Jun 2021 10:59:03 -0700 Subject: [PATCH 17/77] added ? to query param of setbal & sendfunds --- help.md | 4 ++-- include/bank_f.h | 4 ++-- src/bank_f.cpp | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/help.md b/help.md index c4a15ef..79ada56 100644 --- a/help.md +++ b/help.md @@ -18,14 +18,14 @@ | :------------: | :------------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------- | | GetBal | BankF/{name}/bal | GET | false | returns the balance of a given user `{name}` | | GetLog | BankF/{name}/log | GET | true | returns a list of last `n` number of transactions (a configurable amount) of a given user `{name}` | -| SendFunds | BankF/{name}/send/{to}/amount={amount} | POST | true | sends `{amount}` from user `{name}` to user `{to}` | +| SendFunds | BankF/{name}/send/{to}?amount={amount} | POST | true | sends `{amount}` from user `{name}` to user `{to}` | | VerifyPassword | BankF/{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header | # Meta Usage | Name | Path | Method | A | Description | | :------------: | :------------------------------------- | :----: | :---: | ---------------------------------------------------------------------------------------------------------------------------------------- | | ChangePassword | BankF/{name}/pass/change | PATCH | true | if the password supplied in the header matches the user `{name}`'s password, the user's password is changed to the one given in the body | -| SetBal | BankF/admin/{name}/bal/amount={amount} | PATCH | true | sets the balance of a give user `{name}` if the supplied password matches the admin password | +| SetBal | BankF/admin/{name}/bal?amount={amount} | PATCH | true | sets the balance of a give user `{name}` if the supplied password matches the admin password | # System Usage | Name | Path | Method | A | Description | diff --git a/include/bank_f.h b/include/bank_f.h index 5b17c17..fc3703f 100644 --- a/include/bank_f.h +++ b/include/bank_f.h @@ -33,12 +33,12 @@ public: //Usage METHOD_ADD(BankF::GetBal, "/{name}/bal", Get, Options); METHOD_ADD(BankF::GetLog, "/{name}/log", Get, Options); - METHOD_ADD(BankF::SendFunds, "/{name}/send/{to}/amount={amount}", Post, Options); + METHOD_ADD(BankF::SendFunds, "/{name}/send/{to}?amount={amount}", Post, Options); METHOD_ADD(BankF::VerifyPassword, "/{name}/pass/verify", Get, Options); //Meta Usage METHOD_ADD(BankF::ChangePassword, "/{name}/pass/change", Patch, Options); - METHOD_ADD(BankF::SetBal, "/admin/{name}/bal/amount={amount}", Patch, Options); + METHOD_ADD(BankF::SetBal, "/admin/{name}/bal?amount={amount}", Patch, Options); //System Usage METHOD_ADD(BankF::Help, "/help", Get, Options); diff --git a/src/bank_f.cpp b/src/bank_f.cpp index 9cfe056..b47ffe2 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -31,7 +31,7 @@ BankF::BankF(Bank *b) : bank(*b) {} void BankF::Help(req_args) const { auto resp = HttpResponse::newHttpResponse(); - resp->setBody("# Meta Usage

Error Responses

# meaning
-1 UserNotFound
-2 WrongPassword
-3 InvalidRequest
-4 NameTooLong
-5 UserAlreadyExists
-6 InsufficientFunds

Things of Note

  • all endpoints respond with JSON file type
  • "A" denotes requiring Authentication in the form of a header titled "Password"

Usage

Name Path Method A Description
GetBal BankF/{name}/bal GET false returns the balance of a given user {name}
GetLog BankF/{name}/log GET true returns a list of last n number of transactions (a configurable amount) of a given user {name}
SendFunds BankF/{name}/send/{to}/amount={amount} POST true sends {amount} from user {name} to user {to}
VerifyPassword BankF/{name}/pass/verify GET true returns 1 if the supplied user {name}'s password matches the password supplied in the header

Meta Usage

Name Path Method A Description
ChangePassword BankF/{name}/pass/change PATCH true if the password supplied in the header matches the user {name}'s password, the user’s password is changed to the one given in the body
SetBal BankF/admin/{name}/bal/amount={amount} PATCH true sets the balance of a give user {name} if the supplied password matches the admin password

System Usage

Name Path Method A Description
Help BankF/help GET false the page you’re looking at right now!
Ping BankF/ping GET false for pinging the server to see if its online
Close BankF/admin/close POST true saves and then closes the program if the supplied password matches the admin password
Contains BankF/contains/{name} GET false returns 1 if the supplied user {name} exists
AdminVerifyPass BankF/admin/verify GET true returns 1 if the password supplied in the header matches the admin password

User Management

Name Path Method A Description
AddUser BankF/user/{name} POST true registers a user with the name {name}, balance of 0 and a password of the password supplied in the header
AdminAddUser BankF/admin/user/{name}?init_bal={init_bal} POST true if the password supplied in the header matches the admin password, then it registers a user with the name {name}, balance of init_bal and a password supplied by the body of the request
DelUser BankF/user/{name} DELETE true if the password supplied in the header matches the user {name}'s password, then the user is deleted
AdminDelUser BankF/admin/user/{name} DELETE true if the password supplied in the header matches the admin password, then the user is deleted
"); + resp->setBody("# Meta Usage

Error Responses

# meaning
-1 UserNotFound
-2 WrongPassword
-3 InvalidRequest
-4 NameTooLong
-5 UserAlreadyExists
-6 InsufficientFunds

Things of Note

  • all endpoints respond with JSON file type
  • "A" denotes requiring Authentication in the form of a header titled "Password"

Usage

Name Path Method A Description
GetBal BankF/{name}/bal GET false returns the balance of a given user {name}
GetLog BankF/{name}/log GET true returns a list of last n number of transactions (a configurable amount) of a given user {name}
SendFunds BankF/{name}/send/{to}?amount={amount} POST true sends {amount} from user {name} to user {to}
VerifyPassword BankF/{name}/pass/verify GET true returns 1 if the supplied user {name}'s password matches the password supplied in the header

Meta Usage

Name Path Method A Description
ChangePassword BankF/{name}/pass/change PATCH true if the password supplied in the header matches the user {name}'s password, the user’s password is changed to the one given in the body
SetBal BankF/admin/{name}/bal?amount={amount} PATCH true sets the balance of a give user {name} if the supplied password matches the admin password

System Usage

Name Path Method A Description
Help BankF/help GET false the page you’re looking at right now!
Ping BankF/ping GET false for pinging the server to see if its online
Close BankF/admin/close POST true saves and then closes the program if the supplied password matches the admin password
Contains BankF/contains/{name} GET false returns 1 if the supplied user {name} exists
AdminVerifyPass BankF/admin/verify GET true returns 1 if the password supplied in the header matches the admin password

User Management

Name Path Method A Description
AddUser BankF/user/{name} POST true registers a user with the name {name}, balance of 0 and a password of the password supplied in the header
AdminAddUser BankF/admin/user/{name}?init_bal={init_bal} POST true if the password supplied in the header matches the admin password, then it registers a user with the name {name}, balance of init_bal and a password supplied by the body of the request
DelUser BankF/user/{name} DELETE true if the password supplied in the header matches the user {name}'s password, then the user is deleted
AdminDelUser BankF/admin/user/{name} DELETE true if the password supplied in the header matches the admin password, then the user is deleted
"); resp->setExpiredTime(0); callback(resp); } From de14e2983a184f00712ba0a13d1556fdad2c6d83 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Mon, 14 Jun 2021 11:04:06 -0700 Subject: [PATCH 18/77] removed last commits console out --- src/bank.cpp | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/bank.cpp b/src/bank.cpp index f07d5fc..241495f 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -85,7 +85,6 @@ int_fast8_t Bank::AdminDelUser(const std::string &name, const std::string &attem int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, const std::string &attempt) { - std::cout << "recieved request\n"; //cant send money to self, from self or amount is 0 if (a_name == b_name || !amount) { @@ -118,7 +117,6 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name state = true; } } - std::cout << "a\n"; })) { if (state) @@ -128,7 +126,6 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name b.balance += amount; })) { - std::cout << "c\n"; if constexpr (max_log_size) { Transaction temp(a_name, b_name, amount); @@ -144,7 +141,6 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name } else { - std::cout << "d\n"; //attempt to refund if A exist users.modify_if(a_name, [amount](User &a) { a.balance += amount; @@ -159,10 +155,7 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name } else { - { - std::cout << "b\n"; - return ErrorResponse::UserNotFound; - } + return ErrorResponse::UserNotFound; } } } From 2ccb18345f68e3671b37bcc4720876087e013d6d Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Mon, 14 Jun 2021 13:50:49 -0700 Subject: [PATCH 19/77] :bug: warnings removed --- src/bank.cpp | 6 +++--- src/bank_f.cpp | 2 +- src/user.cpp | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bank.cpp b/src/bank.cpp index 241495f..a4b57e1 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -121,12 +121,12 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name { if (state) { - //if B doesnt exist + //if B does exist if (users.modify_if(b_name, [amount](User &b) { b.balance += amount; })) { - if constexpr (max_log_size) + if constexpr (max_log_size > 0) { Transaction temp(a_name, b_name, amount); Transaction temp2 = temp; @@ -316,7 +316,7 @@ void Bank::Load() user_save.close(); for (const auto &u : temp.getMemberNames()) { - if constexpr (max_log_size) + if constexpr (max_log_size > 0) { users.try_emplace(u, temp[u]["balance"].asUInt(), std::move(temp[u]["password"].asUInt64()), temp[u]["log"]); } diff --git a/src/bank_f.cpp b/src/bank_f.cpp index b47ffe2..b9cdfe5 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -104,7 +104,7 @@ void BankF::AdminVerifyPass(req_args) } void BankF::GetLog(req_args, const std::string &name) { - if constexpr (max_log_size) + if constexpr (max_log_size > 0) { JSON(bank.GetLogs(name, PASS_HEADER)); } diff --git a/src/user.cpp b/src/user.cpp index ade67c2..bfa0aa5 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -29,11 +29,11 @@ User::User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j) : ba log.data.reserve(std::min(pre_log_size * ((log_j.size() / pre_log_size) + 1), max_log_size)); for (uint32_t i = (log_j.size() - max_log_size) * (log_j.size() > max_log_size); i < log_j.size(); i++) { - log.data.push_back(std::move(Transaction( + log.data.push_back(Transaction( log_j[i]["from"].asCString(), log_j[i]["to"].asCString(), log_j[i]["amount"].asUInt(), - log_j[i]["time"].asUInt64()))); + log_j[i]["time"].asUInt64())); } } } @@ -43,7 +43,7 @@ Json::Value User::Serialize() const Json::Value res; res["balance"] = (Json::UInt)balance; res["password"] = (Json::UInt64)password; - if constexpr (max_log_size) + if constexpr (max_log_size > 0) { res["log"] = log.Serialize(); } From 59cc8a8b2a999fb377bea04dfc2c1cd1e05decee Mon Sep 17 00:00:00 2001 From: Luke Bennett Date: Mon, 14 Jun 2021 22:23:52 +0100 Subject: [PATCH 20/77] docs(README): add homebrew deps --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 120e215..6b32337 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,11 @@ CCash solves these issues and adds a level of abstraction, the main philosophy o drogon depedencies (varies by OS/distro) ``` +# Debian sudo apt install libjsoncpp-dev uuid-dev openssl libssl-dev zlib1g-dev + +# macOS +brew install jsoncpp ossp-uuid openssl zlib ``` building the project From 79f388af72e9f32353d5e5aa70697f631a8c0a45 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Mon, 14 Jun 2021 18:27:03 -0700 Subject: [PATCH 21/77] fixed major sendfunds bug --- src/bank.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bank.cpp b/src/bank.cpp index a4b57e1..5adfef7 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -119,7 +119,7 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name } })) { - if (state) + if (state > 0) { //if B does exist if (users.modify_if(b_name, [amount](User &b) { From dedc62f0f791c5b040b5eac7c6c60ae3a30eacc7 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Mon, 14 Jun 2021 18:49:17 -0700 Subject: [PATCH 22/77] turned some conditionals into turnary for code cleanliness --- src/bank.cpp | 96 +++++++++++++--------------------------------------- 1 file changed, 24 insertions(+), 72 deletions(-) diff --git a/src/bank.cpp b/src/bank.cpp index 5adfef7..883280a 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -8,15 +8,10 @@ int_fast8_t Bank::AddUser(const std::string &name, const std::string &init_pass) } { std::shared_lock lock{size_l}; - if (users.try_emplace_l( - name, [](User &) {}, init_pass)) - { - return true; - } - else - { - return ErrorResponse::UserAlreadyExists; - } + return (users.try_emplace_l( + name, [](User &) {}, init_pass)) + ? true + : ErrorResponse::UserAlreadyExists; } } int_fast8_t Bank::AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass) @@ -31,55 +26,36 @@ int_fast8_t Bank::AdminAddUser(const std::string &attempt, std::string &&name, u } { std::shared_lock lock{size_l}; - if (users.try_emplace_l( - name, [](User &) {}, init_bal, std::move(init_pass))) - { - return true; - } - else - { - return ErrorResponse::UserAlreadyExists; - } + return (users.try_emplace_l( + name, [](User &) {}, init_bal, std::move(init_pass))) + ? true + : ErrorResponse::UserAlreadyExists; } } int_fast8_t Bank::DelUser(const std::string &name, const std::string &attempt) { std::shared_lock lock{size_l}; bool state = false; - if (!users.erase_if(name, [&state, &attempt](User &u) { return state = (XXH3_64bits(attempt.data(), attempt.size()) == u.password); })) + if (users.erase_if(name, [&state, &attempt](User &u) { return state = (XXH3_64bits(attempt.data(), attempt.size()) == u.password); })) { - return ErrorResponse::UserNotFound; + return (state) ? true : ErrorResponse::WrongPassword; } else { - if (state) - { - return true; - } - else - { - return ErrorResponse::WrongPassword; - } + return ErrorResponse::UserNotFound; } } int_fast8_t Bank::AdminDelUser(const std::string &name, const std::string &attempt) { std::shared_lock lock{size_l}; bool state = false; - if (!users.erase_if(name, [this, &state, &attempt](const User &) { return state = (admin_pass == attempt); })) + if (users.erase_if(name, [this, &state, &attempt](const User &) { return state = (admin_pass == attempt); })) { - return ErrorResponse::UserNotFound; + return (state) ? true : ErrorResponse::WrongPassword; } else { - if (state) - { - return true; - } - else - { - return ErrorResponse::WrongPassword; - } + return ErrorResponse::UserNotFound; } } @@ -162,25 +138,11 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name int_fast8_t Bank::Contains(const std::string &name) const { - if (users.contains(name)) - { - return true; - } - else - { - return ErrorResponse::UserNotFound; - } + return (users.contains(name)) ? true : ErrorResponse::UserNotFound; } int_fast8_t Bank::AdminVerifyPass(const std::string &attempt) { - if (admin_pass == attempt) - { - return true; - } - else - { - return ErrorResponse::WrongPassword; - } + return (admin_pass == attempt) ? true : ErrorResponse::WrongPassword; } int_fast8_t Bank::SetBal(const std::string &name, const std::string &attempt, uint32_t amount) @@ -189,16 +151,12 @@ int_fast8_t Bank::SetBal(const std::string &name, const std::string &attempt, ui { return ErrorResponse::WrongPassword; } - if (users.modify_if(name, [amount](User &u) { - u.balance = amount; - })) - { - return true; - } - else - { - return ErrorResponse::UserNotFound; - } + + return (users.modify_if(name, [amount](User &u) { + u.balance = amount; + })) + ? true + : ErrorResponse::UserNotFound; } int_fast64_t Bank::GetBal(const std::string &name) const { @@ -213,14 +171,7 @@ int_fast8_t Bank::VerifyPassword(const std::string &name, const std::string &att { int_fast8_t res = ErrorResponse::UserNotFound; users.if_contains(name, [&res, &attempt](const User &u) { - if (u.password == XXH3_64bits(attempt.data(), attempt.size())) - { - res = true; - } - else - { - res = ErrorResponse::WrongPassword; - } + res = (u.password == XXH3_64bits(attempt.data(), attempt.size())) ? true : ErrorResponse::WrongPassword; }); return res; } @@ -234,6 +185,7 @@ int_fast8_t Bank::ChangePassword(const std::string &name, const std::string &att } else { + res = true; u.password = XXH3_64bits(new_pass.data(), new_pass.size()); } }); From 00aea60eaaa1555427d1b5471e007a290b9ed6fb Mon Sep 17 00:00:00 2001 From: William Katz Date: Mon, 14 Jun 2021 21:24:14 -0700 Subject: [PATCH 23/77] Update users.json --- users.json | 44 +++++--------------------------------------- 1 file changed, 5 insertions(+), 39 deletions(-) diff --git a/users.json b/users.json index 9f79054..b785785 100644 --- a/users.json +++ b/users.json @@ -1,42 +1,8 @@ { - "jolly" : + "" : { - "balance" : 139, - "log" : - [ - { - "amount" : 100, - "from" : "twix", - "time" : 1623555387199, - "to" : "jolly" - }, - { - "amount" : 39, - "from" : "twix", - "time" : 1623693328822, - "to" : "jolly" - } - ], - "password" : 746744014776526199 + "balance" : 0, + "log" : null, + "password" : 0 }, - "twix" : - { - "balance" : 9861, - "log" : - [ - { - "amount" : 100, - "from" : "twix", - "time" : 1623555387199, - "to" : "jolly" - }, - { - "amount" : 39, - "from" : "twix", - "time" : 1623693328822, - "to" : "jolly" - } - ], - "password" : 746744014776526199 - } -} \ No newline at end of file +} From 706d304de8a1979541284cac203996f2483ab137 Mon Sep 17 00:00:00 2001 From: William Katz Date: Mon, 14 Jun 2021 21:38:13 -0700 Subject: [PATCH 24/77] Update services.md --- services.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services.md b/services.md index 3d11d73..4845c6e 100644 --- a/services.md +++ b/services.md @@ -24,3 +24,6 @@ - Shipping - High-level bank operations such as loans - Some trust based system for transactions similiar to Paypal + +- a better version of one of these existing ideas +- something completely different From f59c0793ee40b4709676ceca2e5e4aa7ff7e693e Mon Sep 17 00:00:00 2001 From: William Katz Date: Mon, 14 Jun 2021 22:08:23 -0700 Subject: [PATCH 25/77] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6b32337..cea51ac 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ Thank you to the contributors | [Expand](https://github.com/Expand-sys) | Frontend | | [React](https://github.com/Reactified) | CC {API, Shops, and ATM, Logo} | | [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | +| [Luke](https://github.com/LukeeeeBennett) | Docs | ## Features From 8bffcb050da66ca286ecacd7087ac163943ef1e6 Mon Sep 17 00:00:00 2001 From: William Katz Date: Mon, 14 Jun 2021 22:09:01 -0700 Subject: [PATCH 26/77] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cea51ac..e34a750 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ Thank you to the contributors | [Expand](https://github.com/Expand-sys) | Frontend | | [React](https://github.com/Reactified) | CC {API, Shops, and ATM, Logo} | | [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | -| [Luke](https://github.com/LukeeeeBennett) | Docs | +| [Luke](https://github.com/LukeeeeBennett) | Slight Doc edits | ## Features From bd357db0ac97a9c3682e8d4762a18596bbfa15de Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Mon, 14 Jun 2021 22:14:16 -0700 Subject: [PATCH 27/77] admin wrong password removed --- include/error_responses.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/include/error_responses.hpp b/include/error_responses.hpp index 5050d33..6c4de41 100644 --- a/include/error_responses.hpp +++ b/include/error_responses.hpp @@ -4,8 +4,7 @@ enum ErrorResponse UserNotFound = -1, WrongPassword = -2, InvalidRequest = -3, - WrongAdminPassword = -4, - NameTooLong = -5, - UserAlreadyExists = -6, - InsufficientFunds = -7, + NameTooLong = -4, + UserAlreadyExists = -5, + InsufficientFunds = -6, }; \ No newline at end of file From 18645f434699ededea746be269e6ca61bce10d5b Mon Sep 17 00:00:00 2001 From: William Katz Date: Mon, 14 Jun 2021 23:25:23 -0700 Subject: [PATCH 28/77] Update config.json --- config.json | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/config.json b/config.json index 331a931..fa95be9 100644 --- a/config.json +++ b/config.json @@ -4,6 +4,13 @@ "address": "0.0.0.0", "port": 80, "https": false + }, + { + "address": "0.0.0.0", + "port": 443, + "https": true, + "cert": "", + "key": "" } ] } From 1b7a0159bbc996207df2013030d68fc7119db712 Mon Sep 17 00:00:00 2001 From: William Katz Date: Mon, 14 Jun 2021 23:25:59 -0700 Subject: [PATCH 29/77] Update help.md --- help.md | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/help.md b/help.md index 386966f..a52d42a 100644 --- a/help.md +++ b/help.md @@ -10,28 +10,24 @@ | -6 | InsufficientFunds | # Things of Note - -- all endpoints respond with **JSON** file type -- "**A**" denotes requiring Authentication in the form of a header titled "**Password**" +* all endpoints respond with **JSON** file type +* "**A**" denotes requiring Authentication in the form of a header titled "**Password**" # Usage - -| Name | Path | Method | A | Description | -| :------------: | :------------------------------------- | :----: | :---: | ------------------------------------------------------------------------------------------------------------ | -| GetBal | BankF/{name}/bal | GET | false | returns the balance of a given user `{name}` | -| GetLog | BankF/{name}/log | GET | true | returns a list of last `n` number of transactions (configurable in CCash webserver) of a given user `{name}` | -| SendFunds | BankF/{name}/send/{to}?amount={amount} | POST | true | sends `{amount}` from user `{name}` to user `{to}` | -| VerifyPassword | BankF/{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header | +| Name | Path | Method | A | Description | +| :------------: | :------------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------- | +| GetBal | BankF/{name}/bal | GET | false | returns the balance of a given user `{name}` | +| GetLog | BankF/{name}/log | GET | true | returns a list of last `n` number of transactions (a configurable amount) of a given user `{name}` | +| SendFunds | BankF/{name}/send/{to}?amount={amount} | POST | true | sends `{amount}` from user `{name}` to user `{to}` | +| VerifyPassword | BankF/{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header | # Meta Usage - | Name | Path | Method | A | Description | | :------------: | :------------------------------------- | :----: | :---: | ---------------------------------------------------------------------------------------------------------------------------------------- | -| ChangePassword | BankF/{name}/pass/change | PATCH | true | if the password supplied in the header matches the user `{name}`'s password, the user's password is changed to the one given as the body | +| ChangePassword | BankF/{name}/pass/change | PATCH | true | if the password supplied in the header matches the user `{name}`'s password, the user's password is changed to the one given in the body | | SetBal | BankF/admin/{name}/bal?amount={amount} | PATCH | true | sets the balance of a give user `{name}` if the supplied password matches the admin password | # System Usage - | Name | Path | Method | A | Description | | :-------------: | :-------------------- | :----: | :---: | ------------------------------------------------------------------------------------- | | Help | BankF/help | GET | false | the page you're looking at right now! | @@ -41,10 +37,9 @@ | AdminVerifyPass | BankF/admin/verify | GET | true | returns `1` if the password supplied in the header matches the admin password | # User Management - -| Name | Path | Method | A | Description | -| :----------: | :------------------------------------------ | :----: | :---: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| AddUser | BankF/user/{name} | POST | true | registers a user with the name `{name}`, balance of 0 and a password of the password supplied in the header | -| AdminAddUser | BankF/admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password that is supplied as the body of the request | -| DelUser | BankF/user/{name} | DELETE | true | if the password supplied in the header matches the user `{name}`'s password, then the user is deleted | -| AdminDelUser | BankF/admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted | +| Name | Path | Method | A | Description | +| :----------: | :------------------------------------------ | :----: | :---: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| AddUser | BankF/user/{name} | POST | true | registers a user with the name `{name}`, balance of 0 and a password of the password supplied in the header | +| AdminAddUser | BankF/admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password supplied by the body of the request | +| DelUser | BankF/user/{name} | DELETE | true | if the password supplied in the header matches the user `{name}`'s password, then the user is deleted | +| AdminDelUser | BankF/admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted | From 1f8df1e94cb6b49929e64f7c1d50626507f8e532 Mon Sep 17 00:00:00 2001 From: William Katz Date: Mon, 14 Jun 2021 23:38:39 -0700 Subject: [PATCH 30/77] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e34a750..b4b4731 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ Thank you to the contributors - **multi-threaded** - **parallel hashmaps** a far [superior](https://greg7mdp.github.io/parallel-hashmap/) HashMap implementation to the STD, that also benefits from multi-threaded - **Drogon** is a very fast [web framework](https://www.techempower.com/benchmarks/#section=data-r20&hw=ph&test=composite) +- **xxHash** for the hashing of passwords, [graph](https://user-images.githubusercontent.com/750081/61976089-aedeab00-af9f-11e9-9239-e5375d6c080f.png) - **Lightweight**, anecodotally I experienced 0.0% idle, <1% CPU usage on average, 7% at peak, 1000 requests in 0.85s ### Safety From 9d4688e5ed2cd36f6b117b5ae5e821625e55cc44 Mon Sep 17 00:00:00 2001 From: Luke Bennett Date: Tue, 15 Jun 2021 11:02:47 +0100 Subject: [PATCH 31/77] ci: add Dockerfile and deploy workflow --- .github/workflows/deploy.yaml | 10 ++++++++++ Dockerfile | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 .github/workflows/deploy.yaml create mode 100644 Dockerfile diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml new file mode 100644 index 0000000..0c2e09f --- /dev/null +++ b/.github/workflows/deploy.yaml @@ -0,0 +1,10 @@ +name: Publish Staging +on: + push: + branches: + - main +jobs: + release: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d7215c5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM debian:latest + +WORKDIR /ccash + +RUN apt update && apt -y install build-essential g++ cmake protobuf-compiler libjsoncpp-dev uuid-dev openssl libssl-dev zlib1g-dev + +COPY . . + +RUN mkdir build + +WORKDIR /ccash/build + +RUN cmake .. +RUN make -j$(nprov) + +CMD ["/ccash/build/bank", "AdminPassword", "10", "4"] From f7684177536bcef58c16a0bf1f4bf0c7a2423f45 Mon Sep 17 00:00:00 2001 From: William Katz Date: Tue, 15 Jun 2021 03:11:42 -0700 Subject: [PATCH 32/77] Update APIs.md --- APIs.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/APIs.md b/APIs.md index d39a8a7..42d05e5 100644 --- a/APIs.md +++ b/APIs.md @@ -1,10 +1,10 @@ # Language Specific APIs ## Complete - -## In Dev * [JS API](https://github.com/LukeeeeBennett/ccash-client-js) * [CC API](https://github.com/Reactified/rpm/blob/main/packages/ccash-api/api.lua) + +## In Dev * [Python API](https://github.com/fearlessdoggo21/ccashpythonclient) * [CS API](https://github.com/Soverclysm/CCash-dotnet-api) -* [Rust API](https://git.stboyden.com/STBoyden/ccash-rs) \ No newline at end of file +* [Rust API](https://git.stboyden.com/STBoyden/ccash-rs) From 20de9805a906f0854abd1f81de95378c7f5ba26b Mon Sep 17 00:00:00 2001 From: William Katz Date: Tue, 15 Jun 2021 03:12:51 -0700 Subject: [PATCH 33/77] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b4b4731..f9589ed 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ Thank you to the contributors | [Expand](https://github.com/Expand-sys) | Frontend | | [React](https://github.com/Reactified) | CC {API, Shops, and ATM, Logo} | | [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | -| [Luke](https://github.com/LukeeeeBennett) | Slight Doc edits | +| [Luke](https://github.com/LukeeeeBennett) | JS API, Slight Doc edits | ## Features From d013abdf4c49226f376935f1f02a2df1690d3f72 Mon Sep 17 00:00:00 2001 From: Samuel Boyden Date: Tue, 15 Jun 2021 14:58:44 +0100 Subject: [PATCH 34/77] Update CC to ComputerCraft (Lua) in APIs.md So as to make it more explicit what language/usage it is. --- APIs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/APIs.md b/APIs.md index 42d05e5..7aadd85 100644 --- a/APIs.md +++ b/APIs.md @@ -2,7 +2,7 @@ ## Complete * [JS API](https://github.com/LukeeeeBennett/ccash-client-js) -* [CC API](https://github.com/Reactified/rpm/blob/main/packages/ccash-api/api.lua) +* [ComputerCraft (Lua) API](https://github.com/Reactified/rpm/blob/main/packages/ccash-api/api.lua) ## In Dev * [Python API](https://github.com/fearlessdoggo21/ccashpythonclient) From ad88f5842b4ab2d5ce5201495d1d6dfa5e9f4eae Mon Sep 17 00:00:00 2001 From: William Katz Date: Tue, 15 Jun 2021 08:07:22 -0700 Subject: [PATCH 35/77] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f9589ed..57e579d 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ Thank you to the contributors | [React](https://github.com/Reactified) | CC {API, Shops, and ATM, Logo} | | [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | | [Luke](https://github.com/LukeeeeBennett) | JS API, Slight Doc edits | - +| [Jolly](https://github.com/STBoyden) | Slight Doc edits | ## Features From 73c4a64666ac214c8682867e7f8d344ddc4e97e1 Mon Sep 17 00:00:00 2001 From: William Katz Date: Tue, 15 Jun 2021 08:27:55 -0700 Subject: [PATCH 36/77] Renamed CC ATM --- services.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services.md b/services.md index 4845c6e..e570210 100644 --- a/services.md +++ b/services.md @@ -10,7 +10,7 @@ - [CC Shop](https://github.com/Reactified/rpm/tree/main/packages/ccash-shop) ![image](https://user-images.githubusercontent.com/31377881/120050327-de163700-bfd1-11eb-9d5a-f75c003e867c.png) ![image](https://user-images.githubusercontent.com/31377881/120050367-09992180-bfd2-11eb-9a22-449d73c196cf.png) -- [CC ATM](https://github.com/Reactified/misc/tree/main/lua/ccash-bank) an ATM for economies allowing for an initial exchange to start up +- [CC Reverse ATM](https://github.com/Reactified/misc/tree/main/lua/ccash-bank) an ATM for economies allowing for an initial exchange to start up ![image](https://user-images.githubusercontent.com/31377881/121277361-4d6b1100-c885-11eb-87c8-cfebcf58da4f.png) ### In-Dev: From 17cb1a0c23e9fa493fba885c91a9579d03010fb7 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Tue, 15 Jun 2021 12:34:15 -0700 Subject: [PATCH 37/77] int 64 cast json --- src/bank_f.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bank_f.cpp b/src/bank_f.cpp index b9cdfe5..4958cc0 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -19,9 +19,9 @@ INLINE Json::Value JsonReturn(T &&val) { res["value"] = (Json::Int64)val; } - else + else if constexpr (std::is_same_v) { - res["value"] = val; + res["value"] = (Json::Int64)val; } return res; } From 153ff6e39d0e4b8962cce583c366461d52b59984 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Tue, 15 Jun 2021 13:16:18 -0700 Subject: [PATCH 38/77] temp was extraneous --- src/bank.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/bank.cpp b/src/bank.cpp index 883280a..195af3e 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -202,15 +202,13 @@ Json::Value Bank::GetLogs(const std::string &name, const std::string &attempt) } else { - Json::Value temp; for (uint32_t i = u.log.data.size(); i > 0; --i) { - temp[i - 1]["to"] = u.log.data[u.log.data.size() - i].to; - temp[i - 1]["from"] = u.log.data[u.log.data.size() - i].from; - temp[i - 1]["amount"] = (Json::UInt)u.log.data[u.log.data.size() - i].amount; - temp[i - 1]["time"] = (Json::UInt64)u.log.data[u.log.data.size() - i].time; + res[i - 1]["to"] = u.log.data[u.log.data.size() - i].to; + res[i - 1]["from"] = u.log.data[u.log.data.size() - i].from; + res[i - 1]["amount"] = (Json::UInt)u.log.data[u.log.data.size() - i].amount; + res[i - 1]["time"] = (Json::UInt64)u.log.data[u.log.data.size() - i].time; } - res = std::move(temp); } })) { From a028b9a628a8830b73ff438160895377e024a16a Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Tue, 15 Jun 2021 13:21:21 -0700 Subject: [PATCH 39/77] :bug: silly error --- src/bank_f.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/bank_f.cpp b/src/bank_f.cpp index 4958cc0..de8cb40 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -23,6 +23,10 @@ INLINE Json::Value JsonReturn(T &&val) { res["value"] = (Json::Int64)val; } + else + { + res["value"] = val; + } return res; } From 7637446362d225a7f6d4ee8d2dd347472446735d Mon Sep 17 00:00:00 2001 From: Luke Bennett Date: Wed, 16 Jun 2021 01:10:49 +0100 Subject: [PATCH 40/77] chore: add .dockerignore --- .dockerignore | 1 + .gitignore | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +/build diff --git a/.gitignore b/.gitignore index 9d01538..928d28c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ .vscode build config.json -users.json \ No newline at end of file +users.json From 7152054edbb7bc4afad335c56723e4876e736d17 Mon Sep 17 00:00:00 2001 From: Luke Bennett Date: Wed, 16 Jun 2021 01:40:30 +0100 Subject: [PATCH 41/77] ci: CMD env vars, publish to GH packages --- .dockerignore | 1 + .github/workflows/deploy.yaml | 25 +++++++++++++++++++++++-- Dockerfile | 2 +- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/.dockerignore b/.dockerignore index 796b96d..c733f93 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,2 @@ /build +/config.json diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 0c2e09f..f0362e0 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -2,9 +2,30 @@ name: Publish Staging on: push: branches: - - main + - cicd jobs: release: + name: Push Docker image to GitHub Packages runs-on: ubuntu-latest + permissions: + packages: write + contents: read steps: - - uses: actions/checkout@v2 + - name: Checkout the repo + uses: actions/checkout@v2 + - name: Login to GitHub Docker Registry + uses: docker/login-action@v1 + with: + registry: docker.pkg.github.com + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Format repository + run: | + echo IMAGE_REPOSITORY=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV + - name: Build container image + uses: docker/build-push-action@v2 + with: + push: true + tags: | + docker.pkg.github.com/${{ env.IMAGE_REPOSITORY }}/ccash:${{ github.sha }} + docker.pkg.github.com/${{ env.IMAGE_REPOSITORY }}/ccash:latest diff --git a/Dockerfile b/Dockerfile index d7215c5..c14cd74 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,4 +13,4 @@ WORKDIR /ccash/build RUN cmake .. RUN make -j$(nprov) -CMD ["/ccash/build/bank", "AdminPassword", "10", "4"] +CMD ["/ccash/build/bank", "$CCASH_ADMIN_PASSWORD", "$CCASH_SAVE_FREQUENCY", "$CCASH_THREAD_COUNT"] From 6d35eee99f042e4bb75e7a74b0b8b038f6a7d0e0 Mon Sep 17 00:00:00 2001 From: William Katz Date: Tue, 15 Jun 2021 22:37:38 -0700 Subject: [PATCH 42/77] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 57e579d..724f7af 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ Thank you to the contributors | [Expand](https://github.com/Expand-sys) | Frontend | | [React](https://github.com/Reactified) | CC {API, Shops, and ATM, Logo} | | [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | -| [Luke](https://github.com/LukeeeeBennett) | JS API, Slight Doc edits | +| [Luke](https://github.com/LukeeeeBennett) | JS API, Docker, Slight Doc edits | | [Jolly](https://github.com/STBoyden) | Slight Doc edits | ## Features From 7bf6b6c1e475a702db52e563804e21ca076e314c Mon Sep 17 00:00:00 2001 From: Luke Bennett Date: Wed, 16 Jun 2021 12:02:28 +0100 Subject: [PATCH 43/77] ci: release docker image on main branch --- .github/workflows/deploy.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index f0362e0..997bdbc 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -2,7 +2,7 @@ name: Publish Staging on: push: branches: - - cicd + - main jobs: release: name: Push Docker image to GitHub Packages From b336781e9c5192846a61a870024b27dabd09a759 Mon Sep 17 00:00:00 2001 From: William Katz Date: Wed, 16 Jun 2021 10:54:12 -0700 Subject: [PATCH 44/77] Update .dockerignore --- .dockerignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.dockerignore b/.dockerignore index c733f93..dd50c2f 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,3 @@ /build /config.json +/users.json From c4303ccfa6a70caa381c88c6ebb9af333a2e82c8 Mon Sep 17 00:00:00 2001 From: William Katz Date: Wed, 16 Jun 2021 11:11:30 -0700 Subject: [PATCH 45/77] Update help.md --- help.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/help.md b/help.md index a52d42a..ec7d690 100644 --- a/help.md +++ b/help.md @@ -14,12 +14,12 @@ * "**A**" denotes requiring Authentication in the form of a header titled "**Password**" # Usage -| Name | Path | Method | A | Description | -| :------------: | :------------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------- | -| GetBal | BankF/{name}/bal | GET | false | returns the balance of a given user `{name}` | -| GetLog | BankF/{name}/log | GET | true | returns a list of last `n` number of transactions (a configurable amount) of a given user `{name}` | -| SendFunds | BankF/{name}/send/{to}?amount={amount} | POST | true | sends `{amount}` from user `{name}` to user `{to}` | -| VerifyPassword | BankF/{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header | +| Name | Path | Method | A | Description | +| :------------: | :------------------------------------- | :----: | :---: | ------------------------------------------------------------------------------------------------------------------------------- | +| GetBal | BankF/{name}/bal | GET | false | returns the balance of a given user `{name}` | +| GetLog | BankF/{name}/log | GET | true | returns a list of last `n` number of transactions (a configurable amount when the program is compiled) of a given user `{name}` | +| SendFunds | BankF/{name}/send/{to}?amount={amount} | POST | true | sends `{amount}` from user `{name}` to user `{to}` | +| VerifyPassword | BankF/{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header | # Meta Usage | Name | Path | Method | A | Description | From abaf8a400aa114a575355df4397ec172834ef14b Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 16 Jun 2021 20:20:31 -0700 Subject: [PATCH 46/77] Project v Connected Service distinction --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 724f7af..dac15aa 100644 --- a/README.md +++ b/README.md @@ -61,13 +61,13 @@ Language specific APIs can be found [here](APIs.md). ## [Contributions](https://github.com/EntireTwix/CCash/graphs/contributors) Thank you to the contributors -| Name | Work | -| :------------------------------------------ | ---------------------------------------------------------- | -| [Expand](https://github.com/Expand-sys) | Frontend | -| [React](https://github.com/Reactified) | CC {API, Shops, and ATM, Logo} | -| [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | -| [Luke](https://github.com/LukeeeeBennett) | JS API, Docker, Slight Doc edits | -| [Jolly](https://github.com/STBoyden) | Slight Doc edits | +| Name | Project Work | Connected Service Work | +| :------------------------------------------ | ---------------------------------------------------------- | ------------------------------ | +| [Expand](https://github.com/Expand-sys) | `N/A` | Frontend | +| [React](https://github.com/Reactified) | `N/A` | CC {API, Shops, and ATM, Logo} | +| [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | `N/A` | +| [Luke](https://github.com/LukeeeeBennett) | Docker, Slight Doc edits | JS API | +| [Jolly](https://github.com/STBoyden) | Slight Doc edits | `N/A` | ## Features From 9a3a54c4cad0152b38774172f411686752302ebe Mon Sep 17 00:00:00 2001 From: William Katz Date: Wed, 16 Jun 2021 20:25:58 -0700 Subject: [PATCH 47/77] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dac15aa..31be141 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Thank you to the contributors | Name | Project Work | Connected Service Work | | :------------------------------------------ | ---------------------------------------------------------- | ------------------------------ | | [Expand](https://github.com/Expand-sys) | `N/A` | Frontend | -| [React](https://github.com/Reactified) | `N/A` | CC {API, Shops, and ATM, Logo} | +| [React](https://github.com/Reactified) | Logo | CC {API, Shops, ATM} | | [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | `N/A` | | [Luke](https://github.com/LukeeeeBennett) | Docker, Slight Doc edits | JS API | | [Jolly](https://github.com/STBoyden) | Slight Doc edits | `N/A` | From e681b4109ddc6b08a6bbdd4b20f578deaf69ba3b Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 16 Jun 2021 21:40:31 -0700 Subject: [PATCH 48/77] made user & config save configurable --- include/consts.hpp | 4 +++- main.cpp | 2 +- src/bank.cpp | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/include/consts.hpp b/include/consts.hpp index fc66c35..5d3dd20 100644 --- a/include/consts.hpp +++ b/include/consts.hpp @@ -3,4 +3,6 @@ // Setting both values to 0 does not compile logging constexpr unsigned max_log_size = 100; constexpr unsigned pre_log_size = 10; -constexpr unsigned max_name_size = 50; \ No newline at end of file +constexpr unsigned max_name_size = 50; +constexpr const char *users_location = "../users.json"; +constexpr const char *config_location = "../config.json"; \ No newline at end of file diff --git a/main.cpp b/main.cpp index d8f7283..4babcca 100644 --- a/main.cpp +++ b/main.cpp @@ -73,7 +73,7 @@ int main(int argc, char **argv) [](const drogon::HttpRequestPtr &req, const drogon::HttpResponsePtr &resp) { resp->addHeader("Access-Control-Allow-Origin", "*"); }); - app().loadConfigFile("../config.json").registerController(API).setThreadNum(std::stoul(argv[3])).run(); + app().loadConfigFile(config_location).registerController(API).setThreadNum(std::stoul(argv[3])).run(); return 0; } diff --git a/src/bank.cpp b/src/bank.cpp index 195af3e..cf2cddc 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -238,7 +238,7 @@ void Bank::Save() } else { - std::ofstream user_save("../users.json"); + std::ofstream user_save(users_location); Json::StreamWriterBuilder builder; const std::unique_ptr writer(builder.newStreamWriter()); writer->write(temp, &user_save); @@ -252,7 +252,7 @@ void Bank::Load() Json::CharReaderBuilder builder; Json::Value temp; - std::ifstream user_save("../users.json"); + std::ifstream user_save(users_location); builder["collectComments"] = true; JSONCPP_STRING errs; if (!parseFromStream(builder, user_save, &temp, &errs)) From e40aa29b59277746579a90f9bde760497b052dd6 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 16 Jun 2021 21:42:46 -0700 Subject: [PATCH 49/77] :art: formatting --- include/consts.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/consts.hpp b/include/consts.hpp index 5d3dd20..83fcff0 100644 --- a/include/consts.hpp +++ b/include/consts.hpp @@ -3,6 +3,8 @@ // Setting both values to 0 does not compile logging constexpr unsigned max_log_size = 100; constexpr unsigned pre_log_size = 10; + constexpr unsigned max_name_size = 50; + constexpr const char *users_location = "../users.json"; constexpr const char *config_location = "../config.json"; \ No newline at end of file From 315c0d934f84b03eb70fe8b965ad1e14b170248e Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 16 Jun 2021 22:44:00 -0700 Subject: [PATCH 50/77] hash comments --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 31be141..b046771 100644 --- a/README.md +++ b/README.md @@ -61,13 +61,13 @@ Language specific APIs can be found [here](APIs.md). ## [Contributions](https://github.com/EntireTwix/CCash/graphs/contributors) Thank you to the contributors -| Name | Project Work | Connected Service Work | -| :------------------------------------------ | ---------------------------------------------------------- | ------------------------------ | -| [Expand](https://github.com/Expand-sys) | `N/A` | Frontend | -| [React](https://github.com/Reactified) | Logo | CC {API, Shops, ATM} | -| [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | `N/A` | -| [Luke](https://github.com/LukeeeeBennett) | Docker, Slight Doc edits | JS API | -| [Jolly](https://github.com/STBoyden) | Slight Doc edits | `N/A` | +| Name | Project Work | Connected Service Work | +| :------------------------------------------ | ---------------------------------------------------------- | ---------------------- | +| [Expand](https://github.com/Expand-sys) | `N/A` | Frontend | +| [React](https://github.com/Reactified) | Logo | CC {API, Shops, ATM} | +| [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | `N/A` | +| [Luke](https://github.com/LukeeeeBennett) | Docker, Slight Doc edits | JS API | +| [Jolly](https://github.com/STBoyden) | Slight Doc edits | `N/A` | ## Features @@ -78,13 +78,14 @@ Thank you to the contributors - **multi-threaded** - **parallel hashmaps** a far [superior](https://greg7mdp.github.io/parallel-hashmap/) HashMap implementation to the STD, that also benefits from multi-threaded - **Drogon** is a very fast [web framework](https://www.techempower.com/benchmarks/#section=data-r20&hw=ph&test=composite) -- **xxHash** for the hashing of passwords, [graph](https://user-images.githubusercontent.com/750081/61976089-aedeab00-af9f-11e9-9239-e5375d6c080f.png) +- **xxHash** for the hashing of passwords, it is very fast: [graph](https://user-images.githubusercontent.com/750081/61976089-aedeab00-af9f-11e9-9239-e5375d6c080f.png) - **Lightweight**, anecodotally I experienced 0.0% idle, <1% CPU usage on average, 7% at peak, 1000 requests in 0.85s ### Safety - **Tamper Proof** relative to an in-game implementation - **Auto-Saving** and Saves on close +- All passwords are **Hashed** - **HTTPS** (OpenSSL) ### Accessibility From fd11a4d52a5db7bbc23f7eb079f5cecc153e9a02 Mon Sep 17 00:00:00 2001 From: Expand-sys <79956551+Expand-sys@users.noreply.github.com> Date: Fri, 18 Jun 2021 12:37:32 +1000 Subject: [PATCH 51/77] patch for ccashdeploy docker --- main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 4babcca..9f75f55 100644 --- a/main.cpp +++ b/main.cpp @@ -55,7 +55,7 @@ int main(int argc, char **argv) bank.admin_pass = argv[1]; //Auto Saving - const unsigned long saving_freq = std::stoul(argv[2]); + const unsigned long saving_freq = std::stoul(std::string(argv[2])); if (saving_freq) //if saving frequency is 0 then auto saving is turned off { std::thread([saving_freq]() { @@ -73,7 +73,7 @@ int main(int argc, char **argv) [](const drogon::HttpRequestPtr &req, const drogon::HttpResponsePtr &resp) { resp->addHeader("Access-Control-Allow-Origin", "*"); }); - app().loadConfigFile(config_location).registerController(API).setThreadNum(std::stoul(argv[3])).run(); + app().loadConfigFile(config_location).registerController(API).setThreadNum(std::stoul(std::string(argv[3]))).run(); return 0; } From 30a3a854318da1f12477aa694691329dd1e2f1f1 Mon Sep 17 00:00:00 2001 From: William Katz Date: Thu, 17 Jun 2021 21:12:11 -0700 Subject: [PATCH 52/77] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b046771..032f1cd 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ Thank you to the contributors | Name | Project Work | Connected Service Work | | :------------------------------------------ | ---------------------------------------------------------- | ---------------------- | -| [Expand](https://github.com/Expand-sys) | `N/A` | Frontend | +| [Expand](https://github.com/Expand-sys) | Slight docker changes | Frontend | | [React](https://github.com/Reactified) | Logo | CC {API, Shops, ATM} | | [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | `N/A` | | [Luke](https://github.com/LukeeeeBennett) | Docker, Slight Doc edits | JS API | From 78ca9dad02aa5aae2d8ba9ca09842aacc2fe9cc6 Mon Sep 17 00:00:00 2001 From: William Katz Date: Fri, 18 Jun 2021 08:13:48 -0700 Subject: [PATCH 53/77] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 032f1cd..977aed0 100644 --- a/README.md +++ b/README.md @@ -64,9 +64,9 @@ Thank you to the contributors | Name | Project Work | Connected Service Work | | :------------------------------------------ | ---------------------------------------------------------- | ---------------------- | | [Expand](https://github.com/Expand-sys) | Slight docker changes | Frontend | -| [React](https://github.com/Reactified) | Logo | CC {API, Shops, ATM} | +| [React](https://github.com/Reactified) | CC API, Logo | CC Shop, CC ATM. | | [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | `N/A` | -| [Luke](https://github.com/LukeeeeBennett) | Docker, Slight Doc edits | JS API | +| [Luke](https://github.com/LukeeeeBennett) | JS API, Docker, Slight Doc edits | `N/A` | | [Jolly](https://github.com/STBoyden) | Slight Doc edits | `N/A` | ## Features From 209d333240fd7471261f4ffbf87c65c16690356b Mon Sep 17 00:00:00 2001 From: William Katz Date: Fri, 18 Jun 2021 09:20:21 -0700 Subject: [PATCH 54/77] Update .dockerignore --- .dockerignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.dockerignore b/.dockerignore index dd50c2f..b28e804 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,4 @@ /build /config.json /users.json +/*.md From ccc6a10dca993c6779124d201f23a917e59b8e05 Mon Sep 17 00:00:00 2001 From: William Katz Date: Fri, 18 Jun 2021 10:44:06 -0700 Subject: [PATCH 55/77] Order --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 977aed0..d0aee46 100644 --- a/README.md +++ b/README.md @@ -63,10 +63,10 @@ Thank you to the contributors | Name | Project Work | Connected Service Work | | :------------------------------------------ | ---------------------------------------------------------- | ---------------------- | -| [Expand](https://github.com/Expand-sys) | Slight docker changes | Frontend | -| [React](https://github.com/Reactified) | CC API, Logo | CC Shop, CC ATM. | -| [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | `N/A` | | [Luke](https://github.com/LukeeeeBennett) | JS API, Docker, Slight Doc edits | `N/A` | +| [React](https://github.com/Reactified) | CC API, Logo | CC Shop, CC ATM. | +| [Expand](https://github.com/Expand-sys) | Slight docker changes | Frontend | +| [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | `N/A` | | [Jolly](https://github.com/STBoyden) | Slight Doc edits | `N/A` | ## Features From 9f58dda6d2c7d5ad7afa45d706e7798876574416 Mon Sep 17 00:00:00 2001 From: William Katz Date: Fri, 18 Jun 2021 10:48:19 -0700 Subject: [PATCH 56/77] Order --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d0aee46..eecc1a2 100644 --- a/README.md +++ b/README.md @@ -63,9 +63,9 @@ Thank you to the contributors | Name | Project Work | Connected Service Work | | :------------------------------------------ | ---------------------------------------------------------- | ---------------------- | -| [Luke](https://github.com/LukeeeeBennett) | JS API, Docker, Slight Doc edits | `N/A` | -| [React](https://github.com/Reactified) | CC API, Logo | CC Shop, CC ATM. | | [Expand](https://github.com/Expand-sys) | Slight docker changes | Frontend | +| [React](https://github.com/Reactified) | CC API, Logo | CC Shop, CC ATM. | +| [Luke](https://github.com/LukeeeeBennett) | JS API, Docker, Slight Doc edits | `N/A` | | [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | `N/A` | | [Jolly](https://github.com/STBoyden) | Slight Doc edits | `N/A` | From 04bf0c55e41a81a32f76839d3d33abe60bfe5aa9 Mon Sep 17 00:00:00 2001 From: William Katz Date: Fri, 18 Jun 2021 12:21:37 -0700 Subject: [PATCH 57/77] :racehorse: reduces json parsing to 0ns json parsing used to be 75% of functions like GetBal's cost, but now its 0% as its just moved in and implicitly cast to Json::Value --- src/bank_f.cpp | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/src/bank_f.cpp b/src/bank_f.cpp index de8cb40..c62b1d9 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -1,35 +1,12 @@ #include "bank_f.h" -#define JSON(V) callback(HttpResponse::newHttpJsonResponse(JsonReturn(V))); +#define JSON(V) callback(HttpResponse::newHttpJsonResponse(V)); #define INLINE __attribute__((always_inline)) inline #define GEN_BODY \ const auto temp_req = req->getJsonObject(); \ const auto body = temp_req ? *temp_req : Json::Value(); #define PASS_HEADER req->getHeader("Password") -template -INLINE Json::Value JsonReturn(T &&val) -{ - Json::Value res; - if constexpr (std::is_same_v) - { - res["value"] = (int)val; //becuase of json lib interpreting 67 as 'A' for example - } - else if constexpr (std::is_same_v) - { - res["value"] = (Json::Int64)val; - } - else if constexpr (std::is_same_v) - { - res["value"] = (Json::Int64)val; - } - else - { - res["value"] = val; - } - return res; -} - BankF::BankF(Bank *b) : bank(*b) {} void BankF::Help(req_args) const @@ -118,4 +95,4 @@ void BankF::GetLog(req_args, const std::string &name) resp->setExpiredTime(0); //cached forever callback(resp); } -} \ No newline at end of file +} From f541e16a4df76dc9a6cb2cedd0fc36dc8983c909 Mon Sep 17 00:00:00 2001 From: William Katz Date: Fri, 18 Jun 2021 12:22:11 -0700 Subject: [PATCH 58/77] :fire: GEN_BODY is no longer needed --- src/bank_f.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/bank_f.cpp b/src/bank_f.cpp index c62b1d9..cef52c6 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -2,9 +2,6 @@ #define JSON(V) callback(HttpResponse::newHttpJsonResponse(V)); #define INLINE __attribute__((always_inline)) inline -#define GEN_BODY \ - const auto temp_req = req->getJsonObject(); \ - const auto body = temp_req ? *temp_req : Json::Value(); #define PASS_HEADER req->getHeader("Password") BankF::BankF(Bank *b) : bank(*b) {} From 882311eb5f7786e6b14b2a5ea9bce06c71bbf5ca Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Fri, 18 Jun 2021 12:41:16 -0700 Subject: [PATCH 59/77] :bug: forgot --- src/bank_f.cpp | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/src/bank_f.cpp b/src/bank_f.cpp index de8cb40..446234e 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -1,35 +1,9 @@ #include "bank_f.h" -#define JSON(V) callback(HttpResponse::newHttpJsonResponse(JsonReturn(V))); +#define JSON(V) callback(HttpResponse::newHttpJsonResponse(V)); #define INLINE __attribute__((always_inline)) inline -#define GEN_BODY \ - const auto temp_req = req->getJsonObject(); \ - const auto body = temp_req ? *temp_req : Json::Value(); #define PASS_HEADER req->getHeader("Password") -template -INLINE Json::Value JsonReturn(T &&val) -{ - Json::Value res; - if constexpr (std::is_same_v) - { - res["value"] = (int)val; //becuase of json lib interpreting 67 as 'A' for example - } - else if constexpr (std::is_same_v) - { - res["value"] = (Json::Int64)val; - } - else if constexpr (std::is_same_v) - { - res["value"] = (Json::Int64)val; - } - else - { - res["value"] = val; - } - return res; -} - BankF::BankF(Bank *b) : bank(*b) {} void BankF::Help(req_args) const @@ -114,7 +88,7 @@ void BankF::GetLog(req_args, const std::string &name) } else { - auto resp = HttpResponse::newHttpJsonResponse(JsonReturn("Logs are Disabled")); + auto resp = HttpResponse::newHttpJsonResponse("Logs are Disabled"); resp->setExpiredTime(0); //cached forever callback(resp); } From e07a3934859f91c8e233b2ee855132404b23bded Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Fri, 18 Jun 2021 13:50:47 -0700 Subject: [PATCH 60/77] :bug: added JsonCast back --- src/bank_f.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/bank_f.cpp b/src/bank_f.cpp index 7909b82..08f30fd 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -1,9 +1,29 @@ #include "bank_f.h" -#define JSON(V) callback(HttpResponse::newHttpJsonResponse(V)); -#define INLINE __attribute__((always_inline)) inline +#define JSON(V) callback(HttpResponse::newHttpJsonResponse(JsonCast(V))); #define PASS_HEADER req->getHeader("Password") +template +constexpr Json::Value JsonCast(T &&val) +{ + if constexpr (std::is_same_v) + { + return (int)val; //becuase of json lib interpreting 67 as 'A' for example + } + else if constexpr (std::is_same_v) + { + return (Json::Int64)val; + } + else if constexpr (std::is_same_v) + { + return (Json::Int64)val; + } + else + { + return val; + } +} + BankF::BankF(Bank *b) : bank(*b) {} void BankF::Help(req_args) const From 191c308561d9d0872c43cc91320b790db1458885 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Fri, 18 Jun 2021 15:30:19 -0700 Subject: [PATCH 61/77] moved Python API to done --- APIs.md | 3 ++- README.md | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/APIs.md b/APIs.md index 7aadd85..360bc3b 100644 --- a/APIs.md +++ b/APIs.md @@ -3,8 +3,9 @@ ## Complete * [JS API](https://github.com/LukeeeeBennett/ccash-client-js) * [ComputerCraft (Lua) API](https://github.com/Reactified/rpm/blob/main/packages/ccash-api/api.lua) +* [Python API](https://github.com/fearlessdoggo21/ccashpythonclient) ## In Dev -* [Python API](https://github.com/fearlessdoggo21/ccashpythonclient) +* [C API]() * [CS API](https://github.com/Soverclysm/CCash-dotnet-api) * [Rust API](https://git.stboyden.com/STBoyden/ccash-rs) diff --git a/README.md b/README.md index eecc1a2..4346161 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,8 @@ Thank you to the contributors | :------------------------------------------ | ---------------------------------------------------------- | ---------------------- | | [Expand](https://github.com/Expand-sys) | Slight docker changes | Frontend | | [React](https://github.com/Reactified) | CC API, Logo | CC Shop, CC ATM. | +| [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | Python API | | [Luke](https://github.com/LukeeeeBennett) | JS API, Docker, Slight Doc edits | `N/A` | -| [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | `N/A` | | [Jolly](https://github.com/STBoyden) | Slight Doc edits | `N/A` | ## Features @@ -79,8 +79,12 @@ Thank you to the contributors - **parallel hashmaps** a far [superior](https://greg7mdp.github.io/parallel-hashmap/) HashMap implementation to the STD, that also benefits from multi-threaded - **Drogon** is a very fast [web framework](https://www.techempower.com/benchmarks/#section=data-r20&hw=ph&test=composite) - **xxHash** for the hashing of passwords, it is very fast: [graph](https://user-images.githubusercontent.com/750081/61976089-aedeab00-af9f-11e9-9239-e5375d6c080f.png) -- **Lightweight**, anecodotally I experienced 0.0% idle, <1% CPU usage on average, 7% at peak, 1000 requests in 0.85s - +- **Lightweight**, anecodotally I experienced (on my laptop's i7 6700K, 8 threads): + - memory usage of 8.6 MB + - 0.0% CPU usage idle + - <1% CPU on average + - 1000 requests in parallel completed in 0.85s which spiked CPU usage to 7% + ### Safety - **Tamper Proof** relative to an in-game implementation From b4013a168fd0d20f84bcac0abc99f5216b0901ed Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Fri, 18 Jun 2021 15:38:55 -0700 Subject: [PATCH 62/77] edited memory usage figure --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4346161..dcd00c3 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ Thank you to the contributors - **Drogon** is a very fast [web framework](https://www.techempower.com/benchmarks/#section=data-r20&hw=ph&test=composite) - **xxHash** for the hashing of passwords, it is very fast: [graph](https://user-images.githubusercontent.com/750081/61976089-aedeab00-af9f-11e9-9239-e5375d6c080f.png) - **Lightweight**, anecodotally I experienced (on my laptop's i7 6700K, 8 threads): - - memory usage of 8.6 MB + - memory usage of 8.5 MB (with 0 users) - 0.0% CPU usage idle - <1% CPU on average - 1000 requests in parallel completed in 0.85s which spiked CPU usage to 7% From aa9f481fbe9ab712d9ecb182a6ec68d639b25f9b Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Fri, 18 Jun 2021 17:00:27 -0700 Subject: [PATCH 63/77] names can no longer contain spaces --- src/bank.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/bank.cpp b/src/bank.cpp index cf2cddc..ea16d1c 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -6,6 +6,14 @@ int_fast8_t Bank::AddUser(const std::string &name, const std::string &init_pass) { return ErrorResponse::NameTooLong; } + for (char c : name) + { + if (c == ' ') + { + return ErrorResponse::InvalidRequest; + } + } + { std::shared_lock lock{size_l}; return (users.try_emplace_l( From 485c5f644280540e2b650d53cd2027a5d6a07a5e Mon Sep 17 00:00:00 2001 From: William Katz Date: Sat, 19 Jun 2021 10:42:31 -0700 Subject: [PATCH 64/77] Update README.md --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index dcd00c3..1157472 100644 --- a/README.md +++ b/README.md @@ -61,13 +61,13 @@ Language specific APIs can be found [here](APIs.md). ## [Contributions](https://github.com/EntireTwix/CCash/graphs/contributors) Thank you to the contributors -| Name | Project Work | Connected Service Work | -| :------------------------------------------ | ---------------------------------------------------------- | ---------------------- | -| [Expand](https://github.com/Expand-sys) | Slight docker changes | Frontend | -| [React](https://github.com/Reactified) | CC API, Logo | CC Shop, CC ATM. | -| [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions | Python API | -| [Luke](https://github.com/LukeeeeBennett) | JS API, Docker, Slight Doc edits | `N/A` | -| [Jolly](https://github.com/STBoyden) | Slight Doc edits | `N/A` | +| Name | Project Work | Connected Service Work | +| :------------------------------------------ | ----------------------------------------------------------------------- | ---------------------- | +| [Expand](https://github.com/Expand-sys) | Slight docker changes | Frontend | +| [React](https://github.com/Reactified) | CC API, Logo | CC Shop, CC ATM. | +| [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized, HTTP convention suggestions, Python API | `N/A` | +| [Luke](https://github.com/LukeeeeBennett) | JS API, Docker, Slight Doc edits | `N/A` | +| [Jolly](https://github.com/STBoyden) | Slight Doc edits | `N/A` | ## Features From 96c6ac07d542d8f34e0acfd877c4f7f48ef739f9 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 19 Jun 2021 12:23:12 -0700 Subject: [PATCH 65/77] :sparkles: benchmarking program --- .dockerignore | 6 +++- benchmarking.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 benchmarking.cpp diff --git a/.dockerignore b/.dockerignore index b28e804..2501371 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,8 @@ /build /config.json /users.json -/*.md +/help.md +/services.md +/APIs.md +/README.md +/benchmarking.cpp \ No newline at end of file diff --git a/benchmarking.cpp b/benchmarking.cpp new file mode 100644 index 0000000..2a2f3d0 --- /dev/null +++ b/benchmarking.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include "bank_f.h" + +#include +#include +#include +#include + +using namespace std::chrono; +using namespace drogon; + +static Bank bank; + +#include +#include +#include + +#define time_func_a(f, a, x) \ + { \ + using namespace std::chrono; \ + uint32_t timer = 0; \ + for (int i = 0; i < x; ++i) \ + { \ + auto t1 = high_resolution_clock::now().time_since_epoch(); \ + f; \ + auto t2 = high_resolution_clock::now().time_since_epoch(); \ + a; \ + timer += std::chrono::duration_cast((t2 - t1)).count(); \ + } \ + std::cout << timer / x << '\n'; \ + } + +#define time_func(f, x) \ + { \ + using namespace std::chrono; \ + uint32_t timer = 0; \ + for (int i = 0; i < x; ++i) \ + { \ + auto t1 = high_resolution_clock::now().time_since_epoch(); \ + f; \ + auto t2 = high_resolution_clock::now().time_since_epoch(); \ + timer += std::chrono::duration_cast((t2 - t1)).count(); \ + } \ + std::cout << timer / x << '\n'; \ + } + +#define Op_a(v, name, x, a) \ + { \ + std::cout << name; \ + time_func_a(v, a, x); \ + } + +#define Op(v, name, x) \ + { \ + std::cout << name; \ + time_func(v, x); \ + } + +int main(int argc, char **argv) +{ + bank.Load(); + bank.admin_pass = "root"; + Op_a(bank.AddUser("", ""), "add user: ", 1000000, bank.DelUser("", "")); + Op_a(bank.AdminAddUser("root", "", 0, ""), "admin add user: ", 1000000, bank.DelUser("", "")); + Op(bank.SetBal("twix", "root", 1000000), "set bal: ", 1000000); + Op(bank.SendFunds("twix", "jolly", 1, "root"), "send funds: ", 1000000); + bank.AddUser("", ""); + Op_a(bank.DelUser("", ""), "del user: ", 1000000, bank.AddUser("", "")); + bank.AddUser("", ""); + Op_a(bank.AdminDelUser("", "root"), "admin del user: ", 1000000, bank.AddUser("", "")); + Op(bank.Contains("twix"), "contains: ", 1000000); + Op(bank.AdminVerifyPass("root"), "admin verify pass: ", 1000000); + Op(bank.GetBal("twix"), "get bal: ", 1000000); + Op(bank.VerifyPassword("twix", "root"), "verify pass: ", 1000000); + Op(bank.ChangePassword("twix", "root", "root"), "change pass: ", 1000000); + Op(bank.GetLogs("twix", "root"), "get logs: ", 1000); + + return 0; +} From 84f26a5ea5e89c6697694e39807a8cd3b21cae55 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 19 Jun 2021 12:25:04 -0700 Subject: [PATCH 66/77] removed reliance on save file --- benchmarking.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/benchmarking.cpp b/benchmarking.cpp index 2a2f3d0..865c007 100644 --- a/benchmarking.cpp +++ b/benchmarking.cpp @@ -62,7 +62,8 @@ static Bank bank; int main(int argc, char **argv) { - bank.Load(); + bank.AddUser("twix", "root"); + bank.AddUser("jolly", "root"); bank.admin_pass = "root"; Op_a(bank.AddUser("", ""), "add user: ", 1000000, bank.DelUser("", "")); Op_a(bank.AdminAddUser("root", "", 0, ""), "admin add user: ", 1000000, bank.DelUser("", "")); From f2b7a563d963cd56f8768d2e8027c15a05168b78 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 19 Jun 2021 14:23:19 -0700 Subject: [PATCH 67/77] :racehorse: made send funds 30% faster --- include/bank.h | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/include/bank.h b/include/bank.h index 35ec3cd..f2343c5 100644 --- a/include/bank.h +++ b/include/bank.h @@ -33,28 +33,28 @@ private: public: std::string admin_pass; - int_fast8_t AddUser(const std::string &name, const std::string &init_pass); - int_fast8_t AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass); + int_fast8_t AddUser(const std::string &name, const std::string &init_pass) noexcept; + int_fast8_t AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass) noexcept; - int_fast8_t DelUser(const std::string &name, const std::string &attempt); - int_fast8_t AdminDelUser(const std::string &name, const std::string &attempt); + int_fast8_t DelUser(const std::string &name, const std::string &attempt) noexcept; + int_fast8_t AdminDelUser(const std::string &name, const std::string &attempt) noexcept; - int_fast8_t SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, const std::string &attempt); + int_fast8_t SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, const std::string &attempt) noexcept; - int_fast8_t Contains(const std::string &name) const; - int_fast8_t AdminVerifyPass(const std::string &attempt); + int_fast8_t Contains(const std::string &name) const noexcept; + int_fast8_t AdminVerifyPass(const std::string &attempt) noexcept; - int_fast8_t SetBal(const std::string &name, const std::string &attempt, uint32_t amount); - int_fast64_t GetBal(const std::string &name) const; + int_fast8_t SetBal(const std::string &name, const std::string &attempt, uint32_t amount) noexcept; + int_fast64_t GetBal(const std::string &name) const noexcept; - int_fast8_t VerifyPassword(const std::string &name, const std::string &attempt) const; - int_fast8_t ChangePassword(const std::string &name, const std::string &attempt, std::string &&new_pass); + int_fast8_t VerifyPassword(const std::string &name, const std::string &attempt) const noexcept; + int_fast8_t ChangePassword(const std::string &name, const std::string &attempt, std::string &&new_pass) noexcept; - Json::Value GetLogs(const std::string &name, const std::string &attempt); + Json::Value GetLogs(const std::string &name, const std::string &attempt) noexcept; void Save(); - //NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS + //NOT THREAD SAFE void Load(); }; From 31e08aeb9765d1cb87f340e7962f907ae8e6f5e8 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 19 Jun 2021 14:23:39 -0700 Subject: [PATCH 68/77] :racehorse: made send funds 30% faster --- src/bank.cpp | 105 ++++++++++++++++++++++++++++----------------------- 1 file changed, 58 insertions(+), 47 deletions(-) diff --git a/src/bank.cpp b/src/bank.cpp index ea16d1c..89d3e3e 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -1,6 +1,6 @@ #include "bank.h" -int_fast8_t Bank::AddUser(const std::string &name, const std::string &init_pass) +int_fast8_t Bank::AddUser(const std::string &name, const std::string &init_pass) noexcept { if (name.size() > max_name_size) { @@ -22,7 +22,7 @@ int_fast8_t Bank::AddUser(const std::string &name, const std::string &init_pass) : ErrorResponse::UserAlreadyExists; } } -int_fast8_t Bank::AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass) +int_fast8_t Bank::AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass) noexcept { if (name.size() > max_name_size) { @@ -40,7 +40,7 @@ int_fast8_t Bank::AdminAddUser(const std::string &attempt, std::string &&name, u : ErrorResponse::UserAlreadyExists; } } -int_fast8_t Bank::DelUser(const std::string &name, const std::string &attempt) +int_fast8_t Bank::DelUser(const std::string &name, const std::string &attempt) noexcept { std::shared_lock lock{size_l}; bool state = false; @@ -53,7 +53,7 @@ int_fast8_t Bank::DelUser(const std::string &name, const std::string &attempt) return ErrorResponse::UserNotFound; } } -int_fast8_t Bank::AdminDelUser(const std::string &name, const std::string &attempt) +int_fast8_t Bank::AdminDelUser(const std::string &name, const std::string &attempt) noexcept { std::shared_lock lock{size_l}; bool state = false; @@ -67,7 +67,7 @@ int_fast8_t Bank::AdminDelUser(const std::string &name, const std::string &attem } } -int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, const std::string &attempt) +int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, const std::string &attempt) noexcept { //cant send money to self, from self or amount is 0 if (a_name == b_name || !amount) @@ -83,7 +83,7 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name int_fast8_t state = false; { std::shared_lock lock{send_funds_l}; //because SendFunds requires 3 locking operations - if (users.modify_if(a_name, [&state, amount, &attempt](User &a) { + if (!users.modify_if(a_name, [&state, amount, &attempt](User &a) { //if A exists, A can afford it, and A's password matches if (a.balance < amount) { @@ -103,57 +103,68 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name } })) { - if (state > 0) - { - //if B does exist - if (users.modify_if(b_name, [amount](User &b) { - b.balance += amount; - })) - { - if constexpr (max_log_size > 0) - { - Transaction temp(a_name, b_name, amount); - Transaction temp2 = temp; - users.modify_if(a_name, [&temp](User &a) { - a.log.AddTrans(std::move(temp)); - }); - users.modify_if(b_name, [&temp2](User &b) { - b.log.AddTrans(std::move(temp2)); - }); - } - return true; - } - else - { - //attempt to refund if A exist - users.modify_if(a_name, [amount](User &a) { - a.balance += amount; - }); - return ErrorResponse::UserNotFound; //because had to refund transaction - } - } - else - { - return state; - } + return ErrorResponse::UserNotFound; } else { - return ErrorResponse::UserNotFound; + if (!state > 0) + { + return state; + } + else + { + if constexpr (max_log_size > 0) + { + Transaction temp; + //if B does exist + if (users.modify_if(b_name, [&a_name, &b_name, &temp, amount](User &b) { + temp = Transaction(a_name, b_name, amount); + b.balance += amount; + b.log.AddTrans(std::forward(temp)); + })) + { + users.modify_if(a_name, [&temp](User &a) { + a.log.AddTrans(std::move(temp)); + }); + return true; + } + else + { + //attempt to refund if A exist + users.modify_if(a_name, [amount](User &a) { + a.balance += amount; + }); + return ErrorResponse::UserNotFound; //because had to refund transaction + } + } + else + { + if (!users.modify_if(b_name, [amount](User &b) { + b.balance += amount; + })) + { + //attempt to refund if A exist + users.modify_if(a_name, [amount](User &a) { + a.balance += amount; + }); + return ErrorResponse::UserNotFound; //because had to refund transaction + } + } + } } } } -int_fast8_t Bank::Contains(const std::string &name) const +int_fast8_t Bank::Contains(const std::string &name) const noexcept { return (users.contains(name)) ? true : ErrorResponse::UserNotFound; } -int_fast8_t Bank::AdminVerifyPass(const std::string &attempt) +int_fast8_t Bank::AdminVerifyPass(const std::string &attempt) noexcept { return (admin_pass == attempt) ? true : ErrorResponse::WrongPassword; } -int_fast8_t Bank::SetBal(const std::string &name, const std::string &attempt, uint32_t amount) +int_fast8_t Bank::SetBal(const std::string &name, const std::string &attempt, uint32_t amount) noexcept { if (admin_pass != attempt) { @@ -166,7 +177,7 @@ int_fast8_t Bank::SetBal(const std::string &name, const std::string &attempt, ui ? true : ErrorResponse::UserNotFound; } -int_fast64_t Bank::GetBal(const std::string &name) const +int_fast64_t Bank::GetBal(const std::string &name) const noexcept { int_fast64_t res = ErrorResponse::UserNotFound; users.if_contains(name, [&res](const User &u) { @@ -175,7 +186,7 @@ int_fast64_t Bank::GetBal(const std::string &name) const return res; } -int_fast8_t Bank::VerifyPassword(const std::string &name, const std::string &attempt) const +int_fast8_t Bank::VerifyPassword(const std::string &name, const std::string &attempt) const noexcept { int_fast8_t res = ErrorResponse::UserNotFound; users.if_contains(name, [&res, &attempt](const User &u) { @@ -183,7 +194,7 @@ int_fast8_t Bank::VerifyPassword(const std::string &name, const std::string &att }); return res; } -int_fast8_t Bank::ChangePassword(const std::string &name, const std::string &attempt, std::string &&new_pass) +int_fast8_t Bank::ChangePassword(const std::string &name, const std::string &attempt, std::string &&new_pass) noexcept { int_fast8_t res = ErrorResponse::UserNotFound; users.modify_if(name, [&res, &attempt, &new_pass](User &u) { @@ -200,7 +211,7 @@ int_fast8_t Bank::ChangePassword(const std::string &name, const std::string &att return res; } -Json::Value Bank::GetLogs(const std::string &name, const std::string &attempt) +Json::Value Bank::GetLogs(const std::string &name, const std::string &attempt) noexcept { Json::Value res; if (!users.if_contains(name, [&res, &attempt](const User &u) { From a08d53bf3482a4ab54bdd4b974cb9ac911ddf86d Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 19 Jun 2021 14:28:48 -0700 Subject: [PATCH 69/77] changed order of conditional --- src/bank.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/bank.cpp b/src/bank.cpp index 89d3e3e..637db81 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -117,18 +117,11 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name { Transaction temp; //if B does exist - if (users.modify_if(b_name, [&a_name, &b_name, &temp, amount](User &b) { + if (!users.modify_if(b_name, [&a_name, &b_name, &temp, amount](User &b) { temp = Transaction(a_name, b_name, amount); b.balance += amount; b.log.AddTrans(std::forward(temp)); })) - { - users.modify_if(a_name, [&temp](User &a) { - a.log.AddTrans(std::move(temp)); - }); - return true; - } - else { //attempt to refund if A exist users.modify_if(a_name, [amount](User &a) { @@ -136,6 +129,13 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name }); return ErrorResponse::UserNotFound; //because had to refund transaction } + else + { + users.modify_if(a_name, [&temp](User &a) { + a.log.AddTrans(std::move(temp)); + }); + return true; + } } else { From 2dc1c0c36970308f5672b340fc92b4b21a419102 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 19 Jun 2021 15:19:44 -0700 Subject: [PATCH 70/77] :racehorse: 6% faster sendfunds --- src/bank.cpp | 103 +++++++++++++++++++++++++-------------------------- 1 file changed, 50 insertions(+), 53 deletions(-) diff --git a/src/bank.cpp b/src/bank.cpp index 637db81..4b03913 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -75,81 +75,78 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name return ErrorResponse::InvalidRequest; } //as first modify_if checks a_name and grabs unique lock - if (!Contains(b_name)) + if (!Contains(b_name) || !Contains(a_name)) { return ErrorResponse::UserNotFound; } int_fast8_t state = false; { - std::shared_lock lock{send_funds_l}; //because SendFunds requires 3 locking operations - if (!users.modify_if(a_name, [&state, amount, &attempt](User &a) { - //if A exists, A can afford it, and A's password matches + if constexpr (max_log_size > 0) + { + Transaction temp(a_name, b_name, amount); + std::shared_lock lock{send_funds_l}; + users.modify_if(a_name, [&temp, &state, amount, &attempt](User &a) { + //if A can afford it and A's password matches attempt if (a.balance < amount) { state = ErrorResponse::InsufficientFunds; } + else if (a.password != XXH3_64bits(attempt.data(), attempt.size())) + { + + state = ErrorResponse::WrongPassword; + } else { - if (a.password != XXH3_64bits(attempt.data(), attempt.size())) - { - state = ErrorResponse::WrongPassword; - } - else - { - a.balance -= amount; - state = true; - } + a.balance -= amount; + a.log.AddTrans(std::forward(temp)); + state = true; } - })) - { - return ErrorResponse::UserNotFound; - } - else - { - if (!state > 0) + }); + if (state > 0) { - return state; + users.modify_if(b_name, [&a_name, &b_name, &temp, amount](User &b) { + b.balance += amount; + b.log.AddTrans(std::move(temp)); + }); + return true; } else { - if constexpr (max_log_size > 0) + return state; + } + } + else + { + std::shared_lock lock{send_funds_l}; + users.modify_if(a_name, [&state, amount, &attempt](User &a) { + //if A can afford it and A's password matches attempt + if (a.balance < amount) { - Transaction temp; - //if B does exist - if (!users.modify_if(b_name, [&a_name, &b_name, &temp, amount](User &b) { - temp = Transaction(a_name, b_name, amount); - b.balance += amount; - b.log.AddTrans(std::forward(temp)); - })) - { - //attempt to refund if A exist - users.modify_if(a_name, [amount](User &a) { - a.balance += amount; - }); - return ErrorResponse::UserNotFound; //because had to refund transaction - } - else - { - users.modify_if(a_name, [&temp](User &a) { - a.log.AddTrans(std::move(temp)); - }); - return true; - } + state = ErrorResponse::InsufficientFunds; + } + else if (a.password != XXH3_64bits(attempt.data(), attempt.size())) + { + + state = ErrorResponse::WrongPassword; } else { - if (!users.modify_if(b_name, [amount](User &b) { - b.balance += amount; - })) - { - //attempt to refund if A exist - users.modify_if(a_name, [amount](User &a) { - a.balance += amount; - }); - return ErrorResponse::UserNotFound; //because had to refund transaction - } + a.balance -= amount; + state = true; } + }); + if (state > 0) + { + users.modify_if(b_name, [&a_name, &b_name, amount](User &b) { + b.balance += amount; + }); + return true; + } + else + { + return state; } } } From f58813606d5c903e16d2d0fe88d79cad68ddf4fb Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 19 Jun 2021 20:50:06 -0700 Subject: [PATCH 71/77] :racehorse: slightly faster when logs arent full --- src/bank.cpp | 113 +++++++++++++++++++++++---------------------------- 1 file changed, 51 insertions(+), 62 deletions(-) diff --git a/src/bank.cpp b/src/bank.cpp index 4b03913..84e1128 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -32,13 +32,12 @@ int_fast8_t Bank::AdminAddUser(const std::string &attempt, std::string &&name, u { return ErrorResponse::WrongPassword; } - { - std::shared_lock lock{size_l}; - return (users.try_emplace_l( - name, [](User &) {}, init_bal, std::move(init_pass))) - ? true - : ErrorResponse::UserAlreadyExists; - } + + std::shared_lock lock{size_l}; + return (users.try_emplace_l( + name, [](User &) {}, init_bal, std::move(init_pass))) + ? true + : ErrorResponse::UserAlreadyExists; } int_fast8_t Bank::DelUser(const std::string &name, const std::string &attempt) noexcept { @@ -75,80 +74,70 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name return ErrorResponse::InvalidRequest; } //as first modify_if checks a_name and grabs unique lock - if (!Contains(b_name) || !Contains(a_name)) + if (!Contains(b_name)) { return ErrorResponse::UserNotFound; } int_fast8_t state = false; + if constexpr (max_log_size > 0) { - if constexpr (max_log_size > 0) - { - Transaction temp(a_name, b_name, amount); - std::shared_lock lock{send_funds_l}; - users.modify_if(a_name, [&temp, &state, amount, &attempt](User &a) { - //if A can afford it and A's password matches attempt - if (a.balance < amount) - { - state = ErrorResponse::InsufficientFunds; - } - else if (a.password != XXH3_64bits(attempt.data(), attempt.size())) - { - - state = ErrorResponse::WrongPassword; - } - else - { - a.balance -= amount; - a.log.AddTrans(std::forward(temp)); - state = true; - } - }); - if (state > 0) + Transaction temp(a_name, b_name, amount); + std::shared_lock lock{send_funds_l}; + users.modify_if(a_name, [&temp, &state, amount, &attempt](User &a) { + //if A can afford it and A's password matches attempt + if (a.balance < amount) { - users.modify_if(b_name, [&a_name, &b_name, &temp, amount](User &b) { - b.balance += amount; - b.log.AddTrans(std::move(temp)); - }); - return true; + state = ErrorResponse::InsufficientFunds; + } + else if (a.password != XXH3_64bits(attempt.data(), attempt.size())) + { + + state = ErrorResponse::WrongPassword; } else { - return state; + a.balance -= amount; + a.log.AddTrans(std::forward(temp)); + state = true; } - } - else + }); + if (state > 0) { - std::shared_lock lock{send_funds_l}; - users.modify_if(a_name, [&state, amount, &attempt](User &a) { - //if A can afford it and A's password matches attempt - if (a.balance < amount) - { - state = ErrorResponse::InsufficientFunds; - } - else if (a.password != XXH3_64bits(attempt.data(), attempt.size())) - { - - state = ErrorResponse::WrongPassword; - } - else - { - a.balance -= amount; - state = true; - } + users.modify_if(b_name, [&a_name, &b_name, &temp, amount](User &b) { + b.balance += amount; + b.log.AddTrans(std::move(temp)); }); - if (state > 0) + } + return state; + } + else + { + std::shared_lock lock{send_funds_l}; + users.modify_if(a_name, [&state, amount, &attempt](User &a) { + //if A can afford it and A's password matches attempt + if (a.balance < amount) { - users.modify_if(b_name, [&a_name, &b_name, amount](User &b) { - b.balance += amount; - }); - return true; + state = ErrorResponse::InsufficientFunds; + } + else if (a.password != XXH3_64bits(attempt.data(), attempt.size())) + { + + state = ErrorResponse::WrongPassword; } else { - return state; + a.balance -= amount; + state = true; } + }); + if (state > 0) + { + users.modify_if(b_name, [&a_name, &b_name, amount](User &b) { + b.balance += amount; + }); } + return state; } } From f48882d446c0742b16562f905cb944853e77c6e8 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sun, 20 Jun 2021 13:25:53 -0700 Subject: [PATCH 72/77] simplified --- benchmarking.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/benchmarking.cpp b/benchmarking.cpp index 865c007..ae49f4c 100644 --- a/benchmarking.cpp +++ b/benchmarking.cpp @@ -69,16 +69,19 @@ int main(int argc, char **argv) Op_a(bank.AdminAddUser("root", "", 0, ""), "admin add user: ", 1000000, bank.DelUser("", "")); Op(bank.SetBal("twix", "root", 1000000), "set bal: ", 1000000); Op(bank.SendFunds("twix", "jolly", 1, "root"), "send funds: ", 1000000); + bank.AddUser("", ""); Op_a(bank.DelUser("", ""), "del user: ", 1000000, bank.AddUser("", "")); - bank.AddUser("", ""); Op_a(bank.AdminDelUser("", "root"), "admin del user: ", 1000000, bank.AddUser("", "")); + bank.DelUser("", ""); + Op(bank.Contains("twix"), "contains: ", 1000000); Op(bank.AdminVerifyPass("root"), "admin verify pass: ", 1000000); Op(bank.GetBal("twix"), "get bal: ", 1000000); Op(bank.VerifyPassword("twix", "root"), "verify pass: ", 1000000); Op(bank.ChangePassword("twix", "root", "root"), "change pass: ", 1000000); - Op(bank.GetLogs("twix", "root"), "get logs: ", 1000); + Op(bank.GetLogs("twix", "root"), "get logs: ", 10000); + Op(bank.Save(), "saving: ", 1); return 0; } From bc067bafe9e374f3ac658de68494838bcec9be34 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sun, 20 Jun 2021 19:21:08 -0700 Subject: [PATCH 73/77] :bug: send funds wasnt logging correctly --- src/bank.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bank.cpp b/src/bank.cpp index 84e1128..c2bd211 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -98,7 +98,7 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name else { a.balance -= amount; - a.log.AddTrans(std::forward(temp)); + a.log.AddTrans(Transaction(temp)); state = true; } }); From 0b10a51a191848e3ad9cec845e76dcf93368754a Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sun, 20 Jun 2021 21:54:32 -0700 Subject: [PATCH 74/77] :sparkles: Return to Reserve Feature --- include/consts.hpp | 6 +++++- src/bank.cpp | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/include/consts.hpp b/include/consts.hpp index 83fcff0..9545808 100644 --- a/include/consts.hpp +++ b/include/consts.hpp @@ -7,4 +7,8 @@ constexpr unsigned pre_log_size = 10; constexpr unsigned max_name_size = 50; constexpr const char *users_location = "../users.json"; -constexpr const char *config_location = "../config.json"; \ No newline at end of file +constexpr const char *config_location = "../config.json"; + +//returns money to an account on deletion +constexpr bool return_on_del = false; +constexpr const char *return_account = ""; \ No newline at end of file diff --git a/src/bank.cpp b/src/bank.cpp index c2bd211..5015488 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -43,7 +43,16 @@ int_fast8_t Bank::DelUser(const std::string &name, const std::string &attempt) n { std::shared_lock lock{size_l}; bool state = false; - if (users.erase_if(name, [&state, &attempt](User &u) { return state = (XXH3_64bits(attempt.data(), attempt.size()) == u.password); })) + if (users.erase_if(name, [this, &name, &state, &attempt](User &u) { + if constexpr (return_on_del) + { + if (SendFunds(name, return_account, u.balance, attempt) == 1) + { + return true; + } + } + return state = (XXH3_64bits(attempt.data(), attempt.size()) == u.password); + })) { return (state) ? true : ErrorResponse::WrongPassword; } @@ -56,7 +65,16 @@ int_fast8_t Bank::AdminDelUser(const std::string &name, const std::string &attem { std::shared_lock lock{size_l}; bool state = false; - if (users.erase_if(name, [this, &state, &attempt](const User &) { return state = (admin_pass == attempt); })) + if (users.erase_if(name, [this, &name, &state, &attempt](const User &u) { + if constexpr (return_on_del) + { + if (SendFunds(name, return_account, u.balance, attempt) == 1) + { + return true; + } + } + return state = (XXH3_64bits(attempt.data(), attempt.size()) == u.password); + })) { return (state) ? true : ErrorResponse::WrongPassword; } From 7550e83169b8467f7876551e47a065ce529e8fb7 Mon Sep 17 00:00:00 2001 From: William Katz Date: Sun, 20 Jun 2021 22:39:28 -0700 Subject: [PATCH 75/77] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 1157472..caa5d72 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,9 @@ Thank you to the contributors - able to be used millions of blocks away, across dimensions, servers, **vanilla or modded**. - **Logging** of all transactions, configurable in [consts.hpp](include/consts.hpp) +### Other +- **return balance on deletion**, configurable in [consts.hpp](include/consts.hpp) + ## Dependencies - [Parallel HashMap](https://github.com/greg7mdp/parallel-hashmap/tree/master) From 8698dd95704c7276fd68145a2a03b4573f85e4cd Mon Sep 17 00:00:00 2001 From: William Katz Date: Mon, 21 Jun 2021 16:52:22 -0700 Subject: [PATCH 76/77] :sparkles: Dev section --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index caa5d72..0e02783 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,11 @@ sudo ./bank Using the Bank's API allows (you/others) to (make/use) connected services that utilize the bank, a couple ideas can be found [here](services.md) -Go to [here](help.md) to see the API's endpoints. -Language specific APIs can be found [here](APIs.md). +## Developing for +as a dev check out +* [APIs](https://github.com/EntireTwix/CCash/blob/main/APIs.md) +* [connected services](https://github.com/EntireTwix/CCash/blob/main/services.md) +* [endpoints](https://github.com/EntireTwix/CCash/blob/main/help.md) ## FAQ **Q:** how is money initially injected into the economy From 3e79d4e7a30691b8f849d0379b706dc2715a91f5 Mon Sep 17 00:00:00 2001 From: William Katz Date: Mon, 21 Jun 2021 16:52:43 -0700 Subject: [PATCH 77/77] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 0e02783..1370d4c 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,6 @@ Using the Bank's API allows (you/others) to (make/use) connected services that u ## Developing for as a dev check out * [APIs](https://github.com/EntireTwix/CCash/blob/main/APIs.md) -* [connected services](https://github.com/EntireTwix/CCash/blob/main/services.md) * [endpoints](https://github.com/EntireTwix/CCash/blob/main/help.md) ## FAQ