From 09422995e845e5ad39a7795c216aafdc01ea6330 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sat, 3 Dec 2022 23:45:19 -0800 Subject: [PATCH] :sparkles: MIN_API_SUPPORT --- CMakeLists.txt | 8 +++++++- benchmarking.cpp | 4 +++- ccash_config.hpp.in | 3 +++ include/bank.h | 8 +++++++- include/bank_api.h | 42 +++++++++++++++++++++++++++++++++++------- include/log.h | 11 +++++++++-- src/bank.cpp | 6 +++++- src/bank_api.cpp | 7 ++++++- src/log.cpp | 5 +++++ 9 files changed, 80 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c4e3a9..6f0aac1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ set(CMAKE_CXX_FLAGS_RELEASE "-O3") find_package(Threads REQUIRED) -add_executable(${PROJECT_NAME} main.cpp ) +add_executable(${PROJECT_NAME} main.cpp) target_sources(${PROJECT_NAME} PRIVATE src/bank_api.cpp @@ -87,6 +87,12 @@ else() set(API_VERSION_VAL 2) endif() +if(DEFINED MIN_API_SUPPORT) +set(MIN_API_SUPPORT_VAL MIN_API_SUPPORT) +else() +set(MIN_API_SUPPORT_VAL 1) +endif() + configure_file(ccash_config.hpp.in ${CMAKE_CURRENT_SOURCE_DIR}/include/ccash_config.hpp) target_include_directories(${PROJECT_NAME} PUBLIC include) diff --git a/benchmarking.cpp b/benchmarking.cpp index 121318d..bf25208 100644 --- a/benchmarking.cpp +++ b/benchmarking.cpp @@ -90,9 +90,11 @@ int main(int argc, char **argv) Op(Bank::VerifyPassword("twix", "root"), "verify pass: ", 1000000); Op(Bank::ChangePassword("twix", "root"), "change pass: ", 1000000); #if MAX_LOG_SIZE > 0 +#if MIN_API_SUPPORT == 1 Op(Bank::GetLogs("twix"), "get logs init: ", 1); Op(Bank::GetLogs("twix"), "get logs cached: ", 1000000); -#if API_VERSION >= 2 +#endif +#if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2) Op(Bank::GetLogsV2("twix"), "get logs init (v2): ", 1); Op(Bank::GetLogsV2("twix"), "get logs cached (v2): ", 1000000); #endif diff --git a/ccash_config.hpp.in b/ccash_config.hpp.in index 0c4d2a6..daae174 100644 --- a/ccash_config.hpp.in +++ b/ccash_config.hpp.in @@ -37,6 +37,9 @@ etc */ #define API_VERSION @API_VERSION_VAL@ +// doesnt compile api endpoints below MIN_API_SUPPORT +#define MIN_API_SUPPORT @MIN_API_SUPPORT_VAL@ + #define MULTI_THREADED @MULTI_THREADED_VAL@ #define ADD_USER_OPEN @ADD_USER_OPEN_VAL@ \ No newline at end of file diff --git a/include/bank.h b/include/bank.h index 85f23d0..f3c5637 100644 --- a/include/bank.h +++ b/include/bank.h @@ -43,12 +43,17 @@ public: static size_t SumBal() noexcept; static BankResponse GetBal(const std::string &name) noexcept; + #if MAX_LOG_SIZE > 0 +#if MIN_API_SUPPORT == 1 static BankResponse GetLogs(const std::string &name) noexcept; -#if API_VERSION >= 2 +#endif + +#if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2) static BankResponse GetLogsV2(const std::string &name) noexcept; #endif #endif + static BankResponse SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept; static bool VerifyPassword(const std::string &name, const std::string_view &attempt) noexcept; @@ -56,6 +61,7 @@ public: static BankResponse SetBal(const std::string &name, uint32_t amount) noexcept; static BankResponse ImpactBal(const std::string &name, int64_t amount) noexcept; static bool Contains(const std::string &name) noexcept; + #if MAX_LOG_SIZE > 0 static BankResponse PruneUsers(time_t threshold_time, uint32_t threshold_bal) noexcept; #else diff --git a/include/bank_api.h b/include/bank_api.h index cf631f5..cf5b976 100644 --- a/include/bank_api.h +++ b/include/bank_api.h @@ -13,7 +13,7 @@ class api : public HttpController public: static void GetBal(req_args, const std::string &name); static void GetLogs(req_args); -#if API_VERSION >= 2 +#if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2) static void GetLogsV2(req_args); #endif static void SendFunds(req_args); @@ -38,18 +38,13 @@ public: METHOD_LIST_BEGIN +#if MIN_API_SUPPORT == 1 //Usage METHOD_ADD(api::GetBal, "/v1/user/balance?name={name}", Get, Options, "JsonFilter"); #if MAX_LOG_SIZE > 0 METHOD_ADD(api::GetLogs, "/v1/user/log", Get, Options, "JsonFilter", "UserFilter"); -#if API_VERSION >= 2 - METHOD_ADD(api::GetLogsV2, "/v2/user/log", Get, Options, "JsonFilter", "UserFilter"); -#endif #else METHOD_ADD(api::GetLogs, "/v1/user/log", Get, Options, "JsonFilter"); -#if API_VERSION >= 2 - METHOD_ADD(api::GetLogsV2, "/v2/user/log", Get, Options, "JsonFilter"); -#endif #endif METHOD_ADD(api::SendFunds, "/v1/user/transfer", Post, Options, "JsonFilter", "UserFilter"); //expects ["name"](string) and ["amount"](uint32) METHOD_ADD(api::VerifyPassword, "/v1/user/verify_password", Post, Options, "UserFilter", "JsonFilter"); @@ -73,6 +68,39 @@ public: METHOD_ADD(api::AdminAddUser, "/v1/admin/user/register", Post, Options, "JsonFilter", "UserFilter"); //expects ["name"](string) ["amount"](uint32) ["pass"](string) METHOD_ADD(api::DelSelf, "/v1/user/delete", Delete, Options, "UserFilter", "JsonFilter"); METHOD_ADD(api::AdminDelUser, "/v1/admin/user/delete", Delete, Options, "JsonFilter", "UserFilter"); //expects ["name"](string) +#endif + +#if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2) + //Usage + METHOD_ADD(api::GetBal, "/v2/user/balance?name={name}", Get, Options, "JsonFilter"); +#if MAX_LOG_SIZE > 0 + METHOD_ADD(api::GetLogsV2, "/v2/user/log", Get, Options, "JsonFilter", "UserFilter"); +#else + METHOD_ADD(api::GetLogsV2, "/v2/user/log", Get, Options, "JsonFilter"); +#endif + METHOD_ADD(api::SendFunds, "/v2/user/transfer", Post, Options, "JsonFilter", "UserFilter"); //expects ["name"](string) and ["amount"](uint32) + METHOD_ADD(api::VerifyPassword, "/v2/user/verify_password", Post, Options, "UserFilter", "JsonFilter"); + + //Meta Usage + METHOD_ADD(api::ChangePassword, "/v2/user/change_password", Patch, Options, "JsonFilter", "UserFilter"); //expects ["pass"](string) + METHOD_ADD(api::AdminChangePassword, "/v2/admin/user/change_password", Patch, Options, "JsonFilter", "UserFilter"); //expects ["name"](string) and ["pass"](string) + METHOD_ADD(api::SetBal, "/v2/admin/set_balance", Patch, Options, "JsonFilter", "UserFilter"); //expects ["name"](string) and ["amount"](uint32) + METHOD_ADD(api::ImpactBal, "/v2/admin/impact_balance", Post, Options, "JsonFilter", "UserFilter"); //expects ["name"](string) and ["amount"](uint32) + + //System Usage + METHOD_ADD(api::Help, "/v2/help", Get, Options); + METHOD_ADD(api::Close, "/v2/admin/shutdown", Post, Options, "UserFilter", "JsonFilter"); + METHOD_ADD(api::Contains, "/v2/user/exists?name={name}", Get, Options, "JsonFilter"); + METHOD_ADD(api::AdminVerifyAccount, "/v2/admin/verify_account", Post, Options, "UserFilter", "JsonFilter"); + METHOD_ADD(api::PruneUsers, "/v2/admin/prune_users", Post, "UserFilter", "JsonFilter"); //expects ["time"](int64) and ["amount"](uint32) + METHOD_ADD(api::ApiProperties, "/properties", Get, Options); + + //User Managment + METHOD_ADD(api::AddUser, "/v2/user/register", Post, Options); //expects ["name"](string) ["pass"](string) + METHOD_ADD(api::AdminAddUser, "/v2/admin/user/register", Post, Options, "JsonFilter", "UserFilter"); //expects ["name"](string) ["amount"](uint32) ["pass"](string) + METHOD_ADD(api::DelSelf, "/v2/user/delete", Delete, Options, "UserFilter", "JsonFilter"); + METHOD_ADD(api::AdminDelUser, "/v2/admin/user/delete", Delete, Options, "JsonFilter", "UserFilter"); //expects ["name"](string) +#endif METHOD_LIST_END }; \ No newline at end of file diff --git a/include/log.h b/include/log.h index c7522fc..fc0ec92 100644 --- a/include/log.h +++ b/include/log.h @@ -10,9 +10,13 @@ using namespace simdjson; struct Log { private: + +#if MIN_API_SUPPORT == 1 ChangeFlag log_flag; std::string log_snapshot = "null"; -#if API_VERSION >= 2 +#endif + +#if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2) ChangeFlag log_flag_v2; std::string log_snapshot_v2 = "null"; #endif @@ -20,8 +24,11 @@ private: public: std::deque data; +#if MIN_API_SUPPORT == 1 std::string GetLogs(const std::string& name) noexcept; -#if API_VERSION >= 2 +#endif + +#if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2) std::string GetLogsV2() noexcept; #endif void AddTrans(const std::string &counterparty_str, bool receiving, uint32_t amount, time_t time) noexcept; diff --git a/src/bank.cpp b/src/bank.cpp index f4ee554..a6cbefe 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -86,6 +86,7 @@ BankResponse Bank::GetBal(const std::string &name) noexcept } } #if MAX_LOG_SIZE > 0 +#if MIN_API_SUPPORT == 1 BankResponse Bank::GetLogs(const std::string &name) noexcept { BankResponse res; @@ -98,7 +99,9 @@ BankResponse Bank::GetLogs(const std::string &name) noexcept return res; } } -#if API_VERSION >= 2 +#endif + +#if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2) BankResponse Bank::GetLogsV2(const std::string &name) noexcept { BankResponse res; @@ -113,6 +116,7 @@ BankResponse Bank::GetLogsV2(const std::string &name) noexcept } #endif #endif + BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept { if (!amount) diff --git a/src/bank_api.cpp b/src/bank_api.cpp index 1da47a3..eec698e 100644 --- a/src/bank_api.cpp +++ b/src/bank_api.cpp @@ -29,6 +29,8 @@ void api::GetBal(req_args, const std::string &name) { RESPONSE_PARSE(Bank::GetBal(name)); } + +#if MIN_API_SUPPORT == 1 void api::GetLogs(req_args) { #if MAX_LOG_SIZE > 0 @@ -40,7 +42,9 @@ void api::GetLogs(req_args) callback(resp); #endif } -#if API_VERSION >= 2 +#endif + +#if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2) void api::GetLogsV2(req_args) { #if MAX_LOG_SIZE > 0 @@ -53,6 +57,7 @@ void api::GetLogsV2(req_args) #endif } #endif + void api::SendFunds(req_args) { SIMD_JSON_GEN; diff --git a/src/log.cpp b/src/log.cpp index 995c11a..5d34d0c 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -10,6 +10,7 @@ void Log::AddTrans(const std::string &counterparty_str, bool receiving, uint32_t data.emplace_back(counterparty_str, receiving, amount, time); } +#if MIN_API_SUPPORT == 1 std::string Log::GetLogs(const std::string& name) noexcept { if (log_flag.GetChangeState() && data.size()) //if there are changes @@ -40,6 +41,9 @@ std::string Log::GetLogs(const std::string& name) noexcept return log_snapshot; } +#endif + +#if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2) std::string Log::GetLogsV2() noexcept { if (log_flag_v2.GetChangeState() && data.size()) //if there are changes @@ -70,3 +74,4 @@ std::string Log::GetLogsV2() noexcept return log_snapshot_v2; } +#endif