MIN_API_SUPPORT

This commit is contained in:
EntireTwix 2022-12-03 23:45:19 -08:00
parent a68e86b216
commit 09422995e8
9 changed files with 80 additions and 14 deletions

View file

@ -12,7 +12,7 @@ set(CMAKE_CXX_FLAGS_RELEASE "-O3")
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
add_executable(${PROJECT_NAME} main.cpp ) add_executable(${PROJECT_NAME} main.cpp)
target_sources(${PROJECT_NAME} PRIVATE target_sources(${PROJECT_NAME} PRIVATE
src/bank_api.cpp src/bank_api.cpp
@ -87,6 +87,12 @@ else()
set(API_VERSION_VAL 2) set(API_VERSION_VAL 2)
endif() 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) configure_file(ccash_config.hpp.in ${CMAKE_CURRENT_SOURCE_DIR}/include/ccash_config.hpp)
target_include_directories(${PROJECT_NAME} PUBLIC include) target_include_directories(${PROJECT_NAME} PUBLIC include)

View file

@ -90,9 +90,11 @@ int main(int argc, char **argv)
Op(Bank::VerifyPassword("twix", "root"), "verify pass: ", 1000000); Op(Bank::VerifyPassword("twix", "root"), "verify pass: ", 1000000);
Op(Bank::ChangePassword("twix", "root"), "change pass: ", 1000000); Op(Bank::ChangePassword("twix", "root"), "change pass: ", 1000000);
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
#if MIN_API_SUPPORT == 1
Op(Bank::GetLogs("twix"), "get logs init: ", 1); Op(Bank::GetLogs("twix"), "get logs init: ", 1);
Op(Bank::GetLogs("twix"), "get logs cached: ", 1000000); 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 init (v2): ", 1);
Op(Bank::GetLogsV2("twix"), "get logs cached (v2): ", 1000000); Op(Bank::GetLogsV2("twix"), "get logs cached (v2): ", 1000000);
#endif #endif

View file

@ -37,6 +37,9 @@ etc
*/ */
#define API_VERSION @API_VERSION_VAL@ #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 MULTI_THREADED @MULTI_THREADED_VAL@
#define ADD_USER_OPEN @ADD_USER_OPEN_VAL@ #define ADD_USER_OPEN @ADD_USER_OPEN_VAL@

View file

@ -43,12 +43,17 @@ public:
static size_t SumBal() noexcept; static size_t SumBal() noexcept;
static BankResponse GetBal(const std::string &name) noexcept; static BankResponse GetBal(const std::string &name) noexcept;
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
#if MIN_API_SUPPORT == 1
static BankResponse GetLogs(const std::string &name) noexcept; 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; static BankResponse GetLogsV2(const std::string &name) noexcept;
#endif #endif
#endif #endif
static BankResponse SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept; 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; 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 SetBal(const std::string &name, uint32_t amount) noexcept;
static BankResponse ImpactBal(const std::string &name, int64_t amount) noexcept; static BankResponse ImpactBal(const std::string &name, int64_t amount) noexcept;
static bool Contains(const std::string &name) noexcept; static bool Contains(const std::string &name) noexcept;
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
static BankResponse PruneUsers(time_t threshold_time, uint32_t threshold_bal) noexcept; static BankResponse PruneUsers(time_t threshold_time, uint32_t threshold_bal) noexcept;
#else #else

View file

@ -13,7 +13,7 @@ class api : public HttpController<api>
public: public:
static void GetBal(req_args, const std::string &name); static void GetBal(req_args, const std::string &name);
static void GetLogs(req_args); static void GetLogs(req_args);
#if API_VERSION >= 2 #if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2)
static void GetLogsV2(req_args); static void GetLogsV2(req_args);
#endif #endif
static void SendFunds(req_args); static void SendFunds(req_args);
@ -38,18 +38,13 @@ public:
METHOD_LIST_BEGIN METHOD_LIST_BEGIN
#if MIN_API_SUPPORT == 1
//Usage //Usage
METHOD_ADD(api::GetBal, "/v1/user/balance?name={name}", Get, Options, "JsonFilter<false>"); METHOD_ADD(api::GetBal, "/v1/user/balance?name={name}", Get, Options, "JsonFilter<false>");
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
METHOD_ADD(api::GetLogs, "/v1/user/log", Get, Options, "JsonFilter<false>", "UserFilter<true, false>"); METHOD_ADD(api::GetLogs, "/v1/user/log", Get, Options, "JsonFilter<false>", "UserFilter<true, false>");
#if API_VERSION >= 2
METHOD_ADD(api::GetLogsV2, "/v2/user/log", Get, Options, "JsonFilter<false>", "UserFilter<true, false>");
#endif
#else #else
METHOD_ADD(api::GetLogs, "/v1/user/log", Get, Options, "JsonFilter<false>"); METHOD_ADD(api::GetLogs, "/v1/user/log", Get, Options, "JsonFilter<false>");
#if API_VERSION >= 2
METHOD_ADD(api::GetLogsV2, "/v2/user/log", Get, Options, "JsonFilter<false>");
#endif
#endif #endif
METHOD_ADD(api::SendFunds, "/v1/user/transfer", Post, Options, "JsonFilter<true>", "UserFilter<true, false>"); //expects ["name"](string) and ["amount"](uint32) METHOD_ADD(api::SendFunds, "/v1/user/transfer", Post, Options, "JsonFilter<true>", "UserFilter<true, false>"); //expects ["name"](string) and ["amount"](uint32)
METHOD_ADD(api::VerifyPassword, "/v1/user/verify_password", Post, Options, "UserFilter<false, false>", "JsonFilter<false>"); METHOD_ADD(api::VerifyPassword, "/v1/user/verify_password", Post, Options, "UserFilter<false, false>", "JsonFilter<false>");
@ -73,6 +68,39 @@ public:
METHOD_ADD(api::AdminAddUser, "/v1/admin/user/register", Post, Options, "JsonFilter<true>", "UserFilter<false, true>"); //expects ["name"](string) ["amount"](uint32) ["pass"](string) METHOD_ADD(api::AdminAddUser, "/v1/admin/user/register", Post, Options, "JsonFilter<true>", "UserFilter<false, true>"); //expects ["name"](string) ["amount"](uint32) ["pass"](string)
METHOD_ADD(api::DelSelf, "/v1/user/delete", Delete, Options, "UserFilter<true, false>", "JsonFilter<false>"); METHOD_ADD(api::DelSelf, "/v1/user/delete", Delete, Options, "UserFilter<true, false>", "JsonFilter<false>");
METHOD_ADD(api::AdminDelUser, "/v1/admin/user/delete", Delete, Options, "JsonFilter<true>", "UserFilter<false, true>"); //expects ["name"](string) METHOD_ADD(api::AdminDelUser, "/v1/admin/user/delete", Delete, Options, "JsonFilter<true>", "UserFilter<false, true>"); //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<false>");
#if MAX_LOG_SIZE > 0
METHOD_ADD(api::GetLogsV2, "/v2/user/log", Get, Options, "JsonFilter<false>", "UserFilter<true, false>");
#else
METHOD_ADD(api::GetLogsV2, "/v2/user/log", Get, Options, "JsonFilter<false>");
#endif
METHOD_ADD(api::SendFunds, "/v2/user/transfer", Post, Options, "JsonFilter<true>", "UserFilter<true, false>"); //expects ["name"](string) and ["amount"](uint32)
METHOD_ADD(api::VerifyPassword, "/v2/user/verify_password", Post, Options, "UserFilter<false, false>", "JsonFilter<false>");
//Meta Usage
METHOD_ADD(api::ChangePassword, "/v2/user/change_password", Patch, Options, "JsonFilter<true>", "UserFilter<true, false>"); //expects ["pass"](string)
METHOD_ADD(api::AdminChangePassword, "/v2/admin/user/change_password", Patch, Options, "JsonFilter<true>", "UserFilter<false, true>"); //expects ["name"](string) and ["pass"](string)
METHOD_ADD(api::SetBal, "/v2/admin/set_balance", Patch, Options, "JsonFilter<true>", "UserFilter<false, true>"); //expects ["name"](string) and ["amount"](uint32)
METHOD_ADD(api::ImpactBal, "/v2/admin/impact_balance", Post, Options, "JsonFilter<true>", "UserFilter<false, true>"); //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<false, true>", "JsonFilter<false>");
METHOD_ADD(api::Contains, "/v2/user/exists?name={name}", Get, Options, "JsonFilter<false>");
METHOD_ADD(api::AdminVerifyAccount, "/v2/admin/verify_account", Post, Options, "UserFilter<false, true>", "JsonFilter<false>");
METHOD_ADD(api::PruneUsers, "/v2/admin/prune_users", Post, "UserFilter<false, true>", "JsonFilter<true>"); //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<true>", "UserFilter<false, true>"); //expects ["name"](string) ["amount"](uint32) ["pass"](string)
METHOD_ADD(api::DelSelf, "/v2/user/delete", Delete, Options, "UserFilter<true, false>", "JsonFilter<false>");
METHOD_ADD(api::AdminDelUser, "/v2/admin/user/delete", Delete, Options, "JsonFilter<true>", "UserFilter<false, true>"); //expects ["name"](string)
#endif
METHOD_LIST_END METHOD_LIST_END
}; };

View file

@ -10,9 +10,13 @@ using namespace simdjson;
struct Log struct Log
{ {
private: private:
#if MIN_API_SUPPORT == 1
ChangeFlag<true> log_flag; ChangeFlag<true> log_flag;
std::string log_snapshot = "null"; std::string log_snapshot = "null";
#if API_VERSION >= 2 #endif
#if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2)
ChangeFlag<true> log_flag_v2; ChangeFlag<true> log_flag_v2;
std::string log_snapshot_v2 = "null"; std::string log_snapshot_v2 = "null";
#endif #endif
@ -20,8 +24,11 @@ private:
public: public:
std::deque<Transaction> data; std::deque<Transaction> data;
#if MIN_API_SUPPORT == 1
std::string GetLogs(const std::string& name) noexcept; 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; std::string GetLogsV2() noexcept;
#endif #endif
void AddTrans(const std::string &counterparty_str, bool receiving, uint32_t amount, time_t time) noexcept; void AddTrans(const std::string &counterparty_str, bool receiving, uint32_t amount, time_t time) noexcept;

View file

@ -86,6 +86,7 @@ BankResponse Bank::GetBal(const std::string &name) noexcept
} }
} }
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
#if MIN_API_SUPPORT == 1
BankResponse Bank::GetLogs(const std::string &name) noexcept BankResponse Bank::GetLogs(const std::string &name) noexcept
{ {
BankResponse res; BankResponse res;
@ -98,7 +99,9 @@ BankResponse Bank::GetLogs(const std::string &name) noexcept
return res; return res;
} }
} }
#if API_VERSION >= 2 #endif
#if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2)
BankResponse Bank::GetLogsV2(const std::string &name) noexcept BankResponse Bank::GetLogsV2(const std::string &name) noexcept
{ {
BankResponse res; BankResponse res;
@ -113,6 +116,7 @@ BankResponse Bank::GetLogsV2(const std::string &name) noexcept
} }
#endif #endif
#endif #endif
BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept
{ {
if (!amount) if (!amount)

View file

@ -29,6 +29,8 @@ void api::GetBal(req_args, const std::string &name)
{ {
RESPONSE_PARSE(Bank::GetBal(name)); RESPONSE_PARSE(Bank::GetBal(name));
} }
#if MIN_API_SUPPORT == 1
void api::GetLogs(req_args) void api::GetLogs(req_args)
{ {
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
@ -40,7 +42,9 @@ void api::GetLogs(req_args)
callback(resp); callback(resp);
#endif #endif
} }
#if API_VERSION >= 2 #endif
#if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2)
void api::GetLogsV2(req_args) void api::GetLogsV2(req_args)
{ {
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
@ -53,6 +57,7 @@ void api::GetLogsV2(req_args)
#endif #endif
} }
#endif #endif
void api::SendFunds(req_args) void api::SendFunds(req_args)
{ {
SIMD_JSON_GEN; SIMD_JSON_GEN;

View file

@ -10,6 +10,7 @@ void Log::AddTrans(const std::string &counterparty_str, bool receiving, uint32_t
data.emplace_back(counterparty_str, receiving, amount, time); data.emplace_back(counterparty_str, receiving, amount, time);
} }
#if MIN_API_SUPPORT == 1
std::string Log::GetLogs(const std::string& name) noexcept std::string Log::GetLogs(const std::string& name) noexcept
{ {
if (log_flag.GetChangeState() && data.size()) //if there are changes 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; return log_snapshot;
} }
#endif
#if (API_VERSION >= 2) && (MIN_API_SUPPORT <= 2)
std::string Log::GetLogsV2() noexcept std::string Log::GetLogsV2() noexcept
{ {
if (log_flag_v2.GetChangeState() && data.size()) //if there are changes if (log_flag_v2.GetChangeState() && data.size()) //if there are changes
@ -70,3 +74,4 @@ std::string Log::GetLogsV2() noexcept
return log_snapshot_v2; return log_snapshot_v2;
} }
#endif