From 1f983322a1fcff00944ce34bd1ef643836190761 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Mon, 28 Nov 2022 16:39:32 -0800 Subject: [PATCH 1/5] :sparkles: updated fbe for new transaction struct --- fbe/user_model/bank_dom.cpp | 22 ++++++----- fbe/user_model/bank_dom.h | 42 ++++++++++---------- fbe/user_model/bank_dom_final_models.cpp | 36 +++++++++-------- fbe/user_model/bank_dom_final_models.h | 8 ++-- fbe/user_model/bank_dom_models.cpp | 50 ++++++++++++------------ fbe/user_model/bank_dom_models.h | 8 ++-- fbe/user_model/fbe.cpp | 16 +++++++- fbe/user_model/fbe.h | 37 +++++++++++++----- fbe/user_model/fbe_final_models.cpp | 4 +- fbe/user_model/fbe_final_models.h | 4 +- fbe/user_model/fbe_final_models.inl | 12 +++--- fbe/user_model/fbe_models.cpp | 4 +- fbe/user_model/fbe_models.h | 4 +- fbe/user_model/fbe_models.inl | 12 +++--- 14 files changed, 157 insertions(+), 102 deletions(-) diff --git a/fbe/user_model/bank_dom.cpp b/fbe/user_model/bank_dom.cpp index d4e60c0..2b5541a 100644 --- a/fbe/user_model/bank_dom.cpp +++ b/fbe/user_model/bank_dom.cpp @@ -1,22 +1,24 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: user_model.fbe -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ #include "bank_dom.h" namespace bank_dom { Transaction::Transaction() - : from("") - , to("") + : counterparty("") + , receiving(false) , amount((uint32_t)0ull) , time((uint64_t)0ull) {} -Transaction::Transaction(const std::string& arg_from, const std::string& arg_to, uint32_t arg_amount, uint64_t arg_time) - : from(arg_from) - , to(arg_to) +Transaction::Transaction(const std::string& arg_counterparty, bool arg_receiving, uint32_t arg_amount, uint64_t arg_time) + : counterparty(arg_counterparty) + , receiving(arg_receiving) , amount(arg_amount) , time(arg_time) {} @@ -36,8 +38,8 @@ bool Transaction::operator<(const Transaction& other) const noexcept void Transaction::swap(Transaction& other) noexcept { using std::swap; - swap(from, other.from); - swap(to, other.to); + swap(counterparty, other.counterparty); + swap(receiving, other.receiving); swap(amount, other.amount); swap(time, other.time); } @@ -45,8 +47,8 @@ void Transaction::swap(Transaction& other) noexcept std::ostream& operator<<(std::ostream& stream, const Transaction& value) { stream << "Transaction("; - stream << "from="; stream << "\"" << value.from << "\""; - stream << ",to="; stream << "\"" << value.to << "\""; + stream << "counterparty="; stream << "\"" << value.counterparty << "\""; + stream << ",receiving="; stream << (value.receiving ? "true" : "false"); stream << ",amount="; stream << value.amount; stream << ",time="; stream << value.time; stream << ")"; diff --git a/fbe/user_model/bank_dom.h b/fbe/user_model/bank_dom.h index 04f8375..75014ef 100644 --- a/fbe/user_model/bank_dom.h +++ b/fbe/user_model/bank_dom.h @@ -1,7 +1,9 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: user_model.fbe -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ #pragma once @@ -27,15 +29,15 @@ namespace bank_dom { struct Transaction { - std::string from; - std::string to; + std::string counterparty; + bool receiving; uint32_t amount; uint64_t time; size_t fbe_type() const noexcept { return 1; } Transaction(); - Transaction(const std::string& arg_from, const std::string& arg_to, uint32_t arg_amount, uint64_t arg_time); + Transaction(const std::string& arg_counterparty, bool arg_receiving, uint32_t arg_amount, uint64_t arg_time); Transaction(const Transaction& other) = default; Transaction(Transaction&& other) = default; ~Transaction() = default; @@ -60,10 +62,12 @@ struct Transaction } // namespace bank_dom -namespace std { +#if defined(FMT_VERSION) && (FMT_VERSION >= 90000) +template <> struct fmt::formatter : ostream_formatter {}; +#endif template<> -struct hash +struct std::hash { typedef bank_dom::Transaction argument_type; typedef size_t result_type; @@ -75,8 +79,6 @@ struct hash } }; -} // namespace std - namespace bank_dom { struct Logs @@ -111,10 +113,12 @@ struct Logs } // namespace bank_dom -namespace std { +#if defined(FMT_VERSION) && (FMT_VERSION >= 90000) +template <> struct fmt::formatter : ostream_formatter {}; +#endif template<> -struct hash +struct std::hash { typedef bank_dom::Logs argument_type; typedef size_t result_type; @@ -126,8 +130,6 @@ struct hash } }; -} // namespace std - namespace bank_dom { struct User @@ -164,10 +166,12 @@ struct User } // namespace bank_dom -namespace std { +#if defined(FMT_VERSION) && (FMT_VERSION >= 90000) +template <> struct fmt::formatter : ostream_formatter {}; +#endif template<> -struct hash +struct std::hash { typedef bank_dom::User argument_type; typedef size_t result_type; @@ -179,8 +183,6 @@ struct hash } }; -} // namespace std - namespace bank_dom { struct Global @@ -216,10 +218,12 @@ struct Global } // namespace bank_dom -namespace std { +#if defined(FMT_VERSION) && (FMT_VERSION >= 90000) +template <> struct fmt::formatter : ostream_formatter {}; +#endif template<> -struct hash +struct std::hash { typedef bank_dom::Global argument_type; typedef size_t result_type; @@ -231,8 +235,6 @@ struct hash } }; -} // namespace std - namespace bank_dom { } // namespace bank_dom diff --git a/fbe/user_model/bank_dom_final_models.cpp b/fbe/user_model/bank_dom_final_models.cpp index cc15906..41ac0bb 100644 --- a/fbe/user_model/bank_dom_final_models.cpp +++ b/fbe/user_model/bank_dom_final_models.cpp @@ -1,15 +1,17 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: user_model.fbe -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ #include "bank_dom_final_models.h" namespace FBE { FinalModel<::bank_dom::Transaction>::FinalModel(FBEBuffer& buffer, size_t offset) noexcept : _buffer(buffer), _offset(offset) - , from(buffer, 0) - , to(buffer, 0) + , counterparty(buffer, 0) + , receiving(buffer, 0) , amount(buffer, 0) , time(buffer, 0) {} @@ -17,8 +19,8 @@ FinalModel<::bank_dom::Transaction>::FinalModel(FBEBuffer& buffer, size_t offset size_t FinalModel<::bank_dom::Transaction>::fbe_allocation_size(const ::bank_dom::Transaction& fbe_value) const noexcept { size_t fbe_result = 0 - + from.fbe_allocation_size(fbe_value.from) - + to.fbe_allocation_size(fbe_value.to) + + counterparty.fbe_allocation_size(fbe_value.counterparty) + + receiving.fbe_allocation_size(fbe_value.receiving) + amount.fbe_allocation_size(fbe_value.amount) + time.fbe_allocation_size(fbe_value.time) ; @@ -38,14 +40,14 @@ size_t FinalModel<::bank_dom::Transaction>::verify_fields() const noexcept size_t fbe_current_offset = 0; size_t fbe_field_size; - from.fbe_offset(fbe_current_offset); - fbe_field_size = from.verify(); + counterparty.fbe_offset(fbe_current_offset); + fbe_field_size = counterparty.verify(); if (fbe_field_size == std::numeric_limits::max()) return std::numeric_limits::max(); fbe_current_offset += fbe_field_size; - to.fbe_offset(fbe_current_offset); - fbe_field_size = to.verify(); + receiving.fbe_offset(fbe_current_offset); + fbe_field_size = receiving.verify(); if (fbe_field_size == std::numeric_limits::max()) return std::numeric_limits::max(); fbe_current_offset += fbe_field_size; @@ -79,13 +81,13 @@ size_t FinalModel<::bank_dom::Transaction>::get_fields(::bank_dom::Transaction& size_t fbe_current_size = 0; size_t fbe_field_size; - from.fbe_offset(fbe_current_offset); - fbe_field_size = from.get(fbe_value.from); + counterparty.fbe_offset(fbe_current_offset); + fbe_field_size = counterparty.get(fbe_value.counterparty); fbe_current_offset += fbe_field_size; fbe_current_size += fbe_field_size; - to.fbe_offset(fbe_current_offset); - fbe_field_size = to.get(fbe_value.to); + receiving.fbe_offset(fbe_current_offset); + fbe_field_size = receiving.get(fbe_value.receiving); fbe_current_offset += fbe_field_size; fbe_current_size += fbe_field_size; @@ -116,13 +118,13 @@ size_t FinalModel<::bank_dom::Transaction>::set_fields(const ::bank_dom::Transac size_t fbe_current_size = 0; size_t fbe_field_size; - from.fbe_offset(fbe_current_offset); - fbe_field_size = from.set(fbe_value.from); + counterparty.fbe_offset(fbe_current_offset); + fbe_field_size = counterparty.set(fbe_value.counterparty); fbe_current_offset += fbe_field_size; fbe_current_size += fbe_field_size; - to.fbe_offset(fbe_current_offset); - fbe_field_size = to.set(fbe_value.to); + receiving.fbe_offset(fbe_current_offset); + fbe_field_size = receiving.set(fbe_value.receiving); fbe_current_offset += fbe_field_size; fbe_current_size += fbe_field_size; diff --git a/fbe/user_model/bank_dom_final_models.h b/fbe/user_model/bank_dom_final_models.h index 335df6f..089d0f1 100644 --- a/fbe/user_model/bank_dom_final_models.h +++ b/fbe/user_model/bank_dom_final_models.h @@ -1,7 +1,9 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: user_model.fbe -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ #pragma once @@ -60,8 +62,8 @@ private: mutable size_t _offset; public: - FinalModel from; - FinalModel to; + FinalModel counterparty; + FinalModel receiving; FinalModel amount; FinalModel time; }; diff --git a/fbe/user_model/bank_dom_models.cpp b/fbe/user_model/bank_dom_models.cpp index 9d18299..c62f767 100644 --- a/fbe/user_model/bank_dom_models.cpp +++ b/fbe/user_model/bank_dom_models.cpp @@ -1,24 +1,26 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: user_model.fbe -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ #include "bank_dom_models.h" namespace FBE { FieldModel<::bank_dom::Transaction>::FieldModel(FBEBuffer& buffer, size_t offset) noexcept : _buffer(buffer), _offset(offset) - , from(buffer, 4 + 4) - , to(buffer, from.fbe_offset() + from.fbe_size()) - , amount(buffer, to.fbe_offset() + to.fbe_size()) + , counterparty(buffer, 4 + 4) + , receiving(buffer, counterparty.fbe_offset() + counterparty.fbe_size()) + , amount(buffer, receiving.fbe_offset() + receiving.fbe_size()) , time(buffer, amount.fbe_offset() + amount.fbe_size()) {} size_t FieldModel<::bank_dom::Transaction>::fbe_body() const noexcept { size_t fbe_result = 4 + 4 - + from.fbe_size() - + to.fbe_size() + + counterparty.fbe_size() + + receiving.fbe_size() + amount.fbe_size() + time.fbe_size() ; @@ -37,8 +39,8 @@ size_t FieldModel<::bank_dom::Transaction>::fbe_extra() const noexcept _buffer.shift(fbe_struct_offset); size_t fbe_result = fbe_body() - + from.fbe_extra() - + to.fbe_extra() + + counterparty.fbe_extra() + + receiving.fbe_extra() + amount.fbe_extra() + time.fbe_extra() ; @@ -75,17 +77,17 @@ bool FieldModel<::bank_dom::Transaction>::verify_fields(size_t fbe_struct_size) { size_t fbe_current_size = 4 + 4; - if ((fbe_current_size + from.fbe_size()) > fbe_struct_size) + if ((fbe_current_size + counterparty.fbe_size()) > fbe_struct_size) return true; - if (!from.verify()) + if (!counterparty.verify()) return false; - fbe_current_size += from.fbe_size(); + fbe_current_size += counterparty.fbe_size(); - if ((fbe_current_size + to.fbe_size()) > fbe_struct_size) + if ((fbe_current_size + receiving.fbe_size()) > fbe_struct_size) return true; - if (!to.verify()) + if (!receiving.verify()) return false; - fbe_current_size += to.fbe_size(); + fbe_current_size += receiving.fbe_size(); if ((fbe_current_size + amount.fbe_size()) > fbe_struct_size) return true; @@ -141,17 +143,17 @@ void FieldModel<::bank_dom::Transaction>::get_fields(::bank_dom::Transaction& fb { size_t fbe_current_size = 4 + 4; - if ((fbe_current_size + from.fbe_size()) <= fbe_struct_size) - from.get(fbe_value.from, ""); + if ((fbe_current_size + counterparty.fbe_size()) <= fbe_struct_size) + counterparty.get(fbe_value.counterparty, ""); else - fbe_value.from = ""; - fbe_current_size += from.fbe_size(); + fbe_value.counterparty = ""; + fbe_current_size += counterparty.fbe_size(); - if ((fbe_current_size + to.fbe_size()) <= fbe_struct_size) - to.get(fbe_value.to, ""); + if ((fbe_current_size + receiving.fbe_size()) <= fbe_struct_size) + receiving.get(fbe_value.receiving, false); else - fbe_value.to = ""; - fbe_current_size += to.fbe_size(); + fbe_value.receiving = false; + fbe_current_size += receiving.fbe_size(); if ((fbe_current_size + amount.fbe_size()) <= fbe_struct_size) amount.get(fbe_value.amount, (uint32_t)0ull); @@ -203,8 +205,8 @@ void FieldModel<::bank_dom::Transaction>::set(const ::bank_dom::Transaction& fbe void FieldModel<::bank_dom::Transaction>::set_fields(const ::bank_dom::Transaction& fbe_value) noexcept { - from.set(fbe_value.from); - to.set(fbe_value.to); + counterparty.set(fbe_value.counterparty); + receiving.set(fbe_value.receiving); amount.set(fbe_value.amount); time.set(fbe_value.time); } diff --git a/fbe/user_model/bank_dom_models.h b/fbe/user_model/bank_dom_models.h index 311a1a1..c98f52b 100644 --- a/fbe/user_model/bank_dom_models.h +++ b/fbe/user_model/bank_dom_models.h @@ -1,7 +1,9 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: user_model.fbe -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ #pragma once @@ -72,8 +74,8 @@ private: size_t _offset; public: - FieldModel from; - FieldModel to; + FieldModel counterparty; + FieldModel receiving; FieldModel amount; FieldModel time; }; diff --git a/fbe/user_model/fbe.cpp b/fbe/user_model/fbe.cpp index 59d516a..c11db02 100644 --- a/fbe/user_model/fbe.cpp +++ b/fbe/user_model/fbe.cpp @@ -1,10 +1,24 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: FBE -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ #include "fbe.h" +#if defined(_WIN32) || defined(_WIN64) +#include +#include +#undef DELETE +#undef ERROR +#undef HOST_NOT_FOUND +#undef Yield +#undef min +#undef max +#undef uuid_t +#endif + namespace FBE { std::string buffer_t::base64encode() const diff --git a/fbe/user_model/fbe.h b/fbe/user_model/fbe.h index b6d11be..46384d3 100644 --- a/fbe/user_model/fbe.h +++ b/fbe/user_model/fbe.h @@ -1,7 +1,9 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: FBE -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ #pragma once @@ -41,7 +43,6 @@ #include #undef HOST_NOT_FOUND #elif defined(_WIN32) || defined(_WIN64) -#include #undef DELETE #undef ERROR #undef HOST_NOT_FOUND @@ -321,10 +322,20 @@ private: } // namespace FBE -namespace std { +#if defined(FMT_VERSION) && (FMT_VERSION >= 90000) +template <> +struct fmt::formatter : formatter +{ + template + auto format(const FBE::decimal_t& value, FormatContext& ctx) const + { + return formatter::format((double)value, ctx); + } +}; +#endif template <> -struct hash +struct std::hash { typedef FBE::decimal_t argument_type; typedef size_t result_type; @@ -337,8 +348,6 @@ struct hash } }; -} // namespace std - namespace FBE { // Register a new enum-based flags macro @@ -530,10 +539,20 @@ private: } // namespace FBE -namespace std { +#if defined(FMT_VERSION) && (FMT_VERSION >= 90000) +template <> +struct fmt::formatter : formatter +{ + template + auto format(const FBE::uuid_t& value, FormatContext& ctx) const + { + return formatter::format(value.string(), ctx); + } +}; +#endif template <> -struct hash +struct std::hash { typedef FBE::uuid_t argument_type; typedef size_t result_type; @@ -548,8 +567,6 @@ struct hash } }; -} // namespace std - namespace FBE { // Fast Binary Encoding buffer based on the dynamic byte buffer diff --git a/fbe/user_model/fbe_final_models.cpp b/fbe/user_model/fbe_final_models.cpp index 27902f8..3e90925 100644 --- a/fbe/user_model/fbe_final_models.cpp +++ b/fbe/user_model/fbe_final_models.cpp @@ -1,7 +1,9 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: FBE -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ #include "fbe_final_models.h" diff --git a/fbe/user_model/fbe_final_models.h b/fbe/user_model/fbe_final_models.h index f032be0..4c0457f 100644 --- a/fbe/user_model/fbe_final_models.h +++ b/fbe/user_model/fbe_final_models.h @@ -1,7 +1,9 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: FBE -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ #pragma once diff --git a/fbe/user_model/fbe_final_models.inl b/fbe/user_model/fbe_final_models.inl index d3035a8..0ef2274 100644 --- a/fbe/user_model/fbe_final_models.inl +++ b/fbe/user_model/fbe_final_models.inl @@ -1,7 +1,9 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: FBE -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ namespace FBE { @@ -536,8 +538,8 @@ inline size_t FinalModelMap::get(std::map& values) c FinalModel fbe_model_value(_buffer, fbe_offset() + 4); for (size_t i = fbe_map_size; i-- > 0;) { - TKey key; - TValue value; + TKey key = TKey(); + TValue value = TValue(); size_t offset_key = fbe_model_key.get(key); fbe_model_key.fbe_shift(offset_key); fbe_model_value.fbe_shift(offset_key); @@ -568,8 +570,8 @@ inline size_t FinalModelMap::get(std::unordered_map& FinalModel fbe_model_value(_buffer, fbe_offset() + 4); for (size_t i = fbe_map_size; i-- > 0;) { - TKey key; - TValue value; + TKey key = TKey(); + TValue value = TValue(); size_t offset_key = fbe_model_key.get(key); fbe_model_key.fbe_shift(offset_key); fbe_model_value.fbe_shift(offset_key); diff --git a/fbe/user_model/fbe_models.cpp b/fbe/user_model/fbe_models.cpp index dd4ecb9..949f484 100644 --- a/fbe/user_model/fbe_models.cpp +++ b/fbe/user_model/fbe_models.cpp @@ -1,7 +1,9 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: FBE -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ #include "fbe_models.h" diff --git a/fbe/user_model/fbe_models.h b/fbe/user_model/fbe_models.h index 9326a5d..968f789 100644 --- a/fbe/user_model/fbe_models.h +++ b/fbe/user_model/fbe_models.h @@ -1,7 +1,9 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: FBE -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ #pragma once diff --git a/fbe/user_model/fbe_models.inl b/fbe/user_model/fbe_models.inl index 1ddba97..d3b671e 100644 --- a/fbe/user_model/fbe_models.inl +++ b/fbe/user_model/fbe_models.inl @@ -1,7 +1,9 @@ +//------------------------------------------------------------------------------ // Automatically generated by the Fast Binary Encoding compiler, do not modify! // https://github.com/chronoxor/FastBinaryEncoding // Source: FBE -// Version: 1.7.0.0 +// FBE version: 1.14.1.0 +//------------------------------------------------------------------------------ namespace FBE { @@ -618,8 +620,8 @@ inline void FieldModelMap::get(std::map& values) con auto fbe_model = (*this)[0]; for (size_t i = fbe_map_size; i-- > 0;) { - TKey key; - TValue value; + TKey key = TKey(); + TValue value = TValue(); fbe_model.first.get(key); fbe_model.second.get(value); values.emplace(key, value); @@ -640,8 +642,8 @@ inline void FieldModelMap::get(std::unordered_map& v auto fbe_model = (*this)[0]; for (size_t i = fbe_map_size; i-- > 0;) { - TKey key; - TValue value; + TKey key = TKey(); + TValue value = TValue(); fbe_model.first.get(key); fbe_model.second.get(value); values.emplace(key, value); From 47d544070b9c729492a937d8f3dd8e4c3e21f1e3 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Mon, 28 Nov 2022 16:40:07 -0800 Subject: [PATCH 2/5] :bulb: comment error and API_VERSION incremented --- ccash_config.hpp.in | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ccash_config.hpp.in b/ccash_config.hpp.in index 3c6cfca..9e58e4d 100644 --- a/ccash_config.hpp.in +++ b/ccash_config.hpp.in @@ -3,7 +3,7 @@ // Setting to 0 does not compile logging (useful for if disk/memory is very valuable) #define MAX_LOG_SIZE @MAX_LOG_SIZE_VAL@ -//default to minecraft usernames +// Default to minecraft usernames constexpr unsigned min_name_size = 3; constexpr unsigned max_name_size = 16; @@ -29,13 +29,13 @@ if false, when frequency is hit save #define CONSERVATIVE_DISK_SAVE @CONSERVATIVE_DISK_SAVE_VAL@ /* -example, when set to 2 -version 1 will not work +e.g when set to 2 +version 1 will work version 2 will work -version 3 will work +version 3 will not work etc */ -#define API_VERSION 1 +#define API_VERSION 2 #define MULTI_THREADED @MULTI_THREADED_VAL@ From 7387156d15fb3aaaed9fe54f2141c3669915d032 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Mon, 28 Nov 2022 16:41:08 -0800 Subject: [PATCH 3/5] :fire: unessasary macro --- include/bank_api.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/bank_api.h b/include/bank_api.h index 82b28dd..aa2bf55 100644 --- a/include/bank_api.h +++ b/include/bank_api.h @@ -37,7 +37,6 @@ public: METHOD_LIST_BEGIN -#if API_VERSION >= 1 //Usage METHOD_ADD(api::GetBal, "/v1/user/balance?name={name}", Get, Options, "JsonFilter"); #if MAX_LOG_SIZE > 0 @@ -67,7 +66,6 @@ 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 METHOD_LIST_END }; \ No newline at end of file From ab27971d8edcb3d19698d60e5468fb7d7145ec64 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Mon, 28 Nov 2022 16:41:27 -0800 Subject: [PATCH 4/5] :bulb: comment clarification --- include/bank.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bank.h b/include/bank.h index 28e0a65..d3556de 100644 --- a/include/bank.h +++ b/include/bank.h @@ -31,8 +31,8 @@ private: static ChangeFlag save_flag; #endif - //must grab as shared if the operation is gonna modify "users"'s size or can be caught in a intermediary state such as SendFunds() - //must grab as unique if the operation is gonna user iterators + // must grab as shared if the operation is gonna modify "users"'s size or can be caught in a intermediary state such as SendFunds() + // must grab as unique if the operation is gonna use user iterators static std::shared_mutex iter_lock; public: From b584f5b4d516e566bdb84331c72a7b65d1672477 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Mon, 28 Nov 2022 17:06:13 -0800 Subject: [PATCH 5/5] :sparkles: improved Transaction structure --- fbe/user_model.fbe | 4 +-- include/bank.h | 3 +++ include/bank_api.h | 11 ++++++-- include/log.h | 10 ++++++-- include/transaction.h | 14 ++++++++-- src/bank.cpp | 20 ++++++++++++--- src/bank_api.cpp | 16 +++++++++--- src/log.cpp | 59 +++++++++++++++++++++++++++++++++---------- src/transaction.cpp | 2 +- src/user.cpp | 4 +-- 10 files changed, 110 insertions(+), 33 deletions(-) diff --git a/fbe/user_model.fbe b/fbe/user_model.fbe index 59a5c00..374d322 100644 --- a/fbe/user_model.fbe +++ b/fbe/user_model.fbe @@ -2,8 +2,8 @@ package bank_dom struct Transaction { - string from = ""; - string to = ""; + string counterparty = ""; + bool receiving = false; uint32 amount = 0; timestamp time; } diff --git a/include/bank.h b/include/bank.h index d3556de..85f23d0 100644 --- a/include/bank.h +++ b/include/bank.h @@ -45,6 +45,9 @@ public: static BankResponse GetBal(const std::string &name) noexcept; #if MAX_LOG_SIZE > 0 static BankResponse GetLogs(const std::string &name) noexcept; +#if API_VERSION >= 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; diff --git a/include/bank_api.h b/include/bank_api.h index aa2bf55..cf631f5 100644 --- a/include/bank_api.h +++ b/include/bank_api.h @@ -11,9 +11,11 @@ using namespace drogon; class api : public HttpController { public: -#if API_VERSION >= 1 static void GetBal(req_args, const std::string &name); static void GetLogs(req_args); +#if API_VERSION >= 2 + static void GetLogsV2(req_args); +#endif static void SendFunds(req_args); static void VerifyPassword(req_args); @@ -33,7 +35,6 @@ public: static void AdminAddUser(req_args); static void DelSelf(req_args); static void AdminDelUser(req_args); -#endif METHOD_LIST_BEGIN @@ -41,8 +42,14 @@ public: 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"); diff --git a/include/log.h b/include/log.h index 11f828e..31a5936 100644 --- a/include/log.h +++ b/include/log.h @@ -12,10 +12,16 @@ struct Log private: ChangeFlag log_flag; std::string log_snapshot = "null"; +#if API_VERSION >= 2 + std::string log_snapshot_v2 = "null"; +#endif public: std::deque data; - std::string GetLogs() noexcept; - void AddTrans(const std::string &from, const std::string &to, uint32_t amount, time_t time) noexcept; + std::string GetLogs(const std::string& name) noexcept; +#if API_VERSION >= 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/include/transaction.h b/include/transaction.h index c4c5805..0bb43ce 100644 --- a/include/transaction.h +++ b/include/transaction.h @@ -5,10 +5,20 @@ struct Transaction { - std::string from = "", to = ""; + std::string counterparty = ""; + bool receiving = false; uint32_t amount = 0; time_t time = 0; Transaction() noexcept; - Transaction(const std::string &from_str, const std::string &to_str, uint32_t amount, time_t time) noexcept; + Transaction(const std::string &counterparty_str, bool receiving, uint32_t amount, time_t time) noexcept; }; + +/* +TODO: v1 vs v2 functionality +TODO: FBE + +TODO: v2/api +TODO: update stats on run (203 bytes) +TODO: update Docs +*/ \ No newline at end of file diff --git a/src/bank.cpp b/src/bank.cpp index 2fac56a..1089806 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -89,7 +89,19 @@ BankResponse Bank::GetBal(const std::string &name) noexcept BankResponse Bank::GetLogs(const std::string &name) noexcept { BankResponse res; - if (!Bank::users.modify_if(name, [&res](User &u) { res = {k200OK, u.log.GetLogs()}; })) + if (!Bank::users.modify_if(name, [&name, &res](User &u) { res = {k200OK, u.log.GetLogs(name)}; })) + { + return {k404NotFound, "\"User not found\""}; + } + else + { + return res; + } +} +BankResponse Bank::GetLogsV2(const std::string &name) noexcept +{ + BankResponse res; + if (!Bank::users.modify_if(name, [&name, &res](User &u) { res = {k200OK, u.log.GetLogsV2()}; })) { return {k404NotFound, "\"User not found\""}; } @@ -123,7 +135,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam if (!Bank::users.modify_if(a_name, [&a_name, &b_name, &res, amount](User &a) #endif { - //if A can afford it + //if "A" can afford it if (a.balance < amount) { res = {k400BadRequest, "\"Insufficient funds\""}; @@ -132,7 +144,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam { a.balance -= amount; #if MAX_LOG_SIZE > 0 - a.log.AddTrans(a_name, b_name, amount, current_time); + a.log.AddTrans(b_name, false, amount, current_time); #endif res = {k200OK, std::to_string(a.balance)}; } @@ -145,7 +157,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam #if MAX_LOG_SIZE > 0 Bank::users.modify_if(b_name, [current_time, &a_name, &b_name, amount](User &b) { b.balance += amount; - b.log.AddTrans(a_name, b_name, amount, current_time); + b.log.AddTrans(a_name, true, amount, current_time); }); #else Bank::users.modify_if(b_name, [amount](User &b) { b.balance += amount; }); diff --git a/src/bank_api.cpp b/src/bank_api.cpp index 24e7c99..60ac43b 100644 --- a/src/bank_api.cpp +++ b/src/bank_api.cpp @@ -6,7 +6,7 @@ #define CORS resp->addHeader("Access-Control-Allow-Origin", "*") -static thread_local ondemand::parser parser; +thread_local ondemand::parser parser; #define SIMD_JSON_GEN \ simdjson::padded_string input(req->getBody()); \ ondemand::document doc; @@ -24,8 +24,6 @@ static thread_local ondemand::parser parser; #define NAME_PARAM req->getParameter("name") -#if API_VERSION >= 1 - //Usage void api::GetBal(req_args, const std::string &name) { @@ -42,6 +40,17 @@ void api::GetLogs(req_args) callback(resp); #endif } +void api::GetLogsV2(req_args) +{ +#if MAX_LOG_SIZE > 0 + RESPONSE_PARSE(Bank::GetLogsV2(NAME_PARAM)); +#else + auto resp = HttpResponse::newCustomHttpResponse(BankResponse{k404NotFound, "\"Logs are Disabled\""}); + CORS; + CACHE_FOREVER; + callback(resp); +#endif +} void api::SendFunds(req_args) { SIMD_JSON_GEN; @@ -344,4 +353,3 @@ void api::AdminDelUser(req_args) } RESPONSE_PARSE(std::move(res)); } -#endif diff --git a/src/log.cpp b/src/log.cpp index ec2e8b6..35f4fb7 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -1,22 +1,22 @@ #include "log.h" -void Log::AddTrans(const std::string &from, const std::string &to, uint32_t amount, time_t time) noexcept +void Log::AddTrans(const std::string &counterparty_str, bool receiving, uint32_t amount, time_t time) noexcept { log_flag.SetChangesOn(); if (data.size() == MAX_LOG_SIZE) { data.pop_back(); } - data.emplace_back(from, to, amount, time); + data.emplace_back(counterparty_str, receiving, amount, time); } -std::string Log::GetLogs() noexcept +std::string Log::GetLogs(const std::string& name) noexcept { if (log_flag.GetChangeState() && data.size()) //if there are changes { log_snapshot.resize(0); //re-generate snapshot - size_t predicted_size = ((59 + (2 * max_name_size)) * data.size()) + 1; + size_t predicted_size = ((57 + (2 * max_name_size)) * data.size()) + 1; if (log_snapshot.capacity() < predicted_size) { log_snapshot.reserve(predicted_size); @@ -24,18 +24,49 @@ std::string Log::GetLogs() noexcept log_snapshot = '['; //1 for (size_t i = 0; i < data.size(); ++i) { - log_snapshot += "{\"to\":\""; //8 - log_snapshot += data[i].to; //max_name_size? - log_snapshot += "\",\"from\":\""; //10 - log_snapshot += data[i].from; //max_name_size? - log_snapshot += "\",\"amount\":"; //12 - log_snapshot += std::to_string(data[i].amount); //10? - log_snapshot += ",\"time\":"; //8 - log_snapshot += std::to_string(data[i].time); //10? - log_snapshot += "},"; //2 + log_snapshot += "{\"to\":\""; //7 + log_snapshot += data[i].receiving? name : data[i].counterparty; //max_name_size? + log_snapshot += "\",\"from\":\""; //10 + log_snapshot += data[i].receiving? data[i].counterparty : name; //max_name_size? + log_snapshot += "\",\"amount\":"; //11 + log_snapshot += std::to_string(data[i].amount); //10? + log_snapshot += ",\"time\":"; //8 + log_snapshot += std::to_string(data[i].time); //10? + log_snapshot += "},"; //2 } log_snapshot.back() = ']'; log_flag.SetChangesOff(); } + return log_snapshot; -} \ No newline at end of file +} +std::string Log::GetLogsV2() noexcept +{ + if (log_flag.GetChangeState() && data.size()) //if there are changes + { + log_snapshot_v2.resize(0); + //re-generate snapshot + size_t predicted_size = ((77 + max_name_size) * data.size()) + 1; + if (log_snapshot_v2.capacity() < predicted_size) + { + log_snapshot_v2.reserve(predicted_size); + } + log_snapshot_v2 = '['; //1 + for (size_t i = 0; i < data.size(); ++i) + { + log_snapshot += "{\"counterparty\":\""; //17 + log_snapshot += data[i].counterparty; //max_name_size? + log_snapshot += "\",\"receiving\":\""; //15 + log_snapshot += std::to_string(data[i].receiving); //4 + log_snapshot += "\",\"amount\":"; //11 + log_snapshot += std::to_string(data[i].amount); //10? + log_snapshot += ",\"time\":"; //8 + log_snapshot += std::to_string(data[i].time); //10? + log_snapshot += "},"; //2 + } + log_snapshot_v2.back() = ']'; + log_flag.SetChangesOff(); + } + + return log_snapshot_v2; +} diff --git a/src/transaction.cpp b/src/transaction.cpp index c0c1456..afd5d0e 100644 --- a/src/transaction.cpp +++ b/src/transaction.cpp @@ -1,4 +1,4 @@ #include "transaction.h" Transaction::Transaction() noexcept {}; -Transaction::Transaction(const std::string &from_str, const std::string &to_str, uint32_t amount, time_t time_val) noexcept : from(from_str), to(to_str), amount(amount), time(time_val) {} +Transaction::Transaction(const std::string &counterparty_str, bool receiving, uint32_t amount, time_t time) noexcept : counterparty(counterparty_str), receiving(receiving), amount(amount), time(time) {} diff --git a/src/user.cpp b/src/user.cpp index 074c13b..b5e1f82 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -29,7 +29,7 @@ User::User(const bank_dom::User &u) noexcept : balance(u.balance), password(u.pa for (; i < u.logs.value().data.size(); ++i) { const bank_dom::Transaction &temp = u.logs.value().data[i]; - log.data.emplace_front(temp.from, temp.to, temp.amount, temp.time); + log.data.emplace_front(temp.counterparty, temp.receiving, temp.amount, temp.time); } } #endif @@ -43,7 +43,7 @@ bank_dom::User User::Encode() const noexcept save_log.data.reserve(this->log.data.size()); for (const Transaction &t : this->log.data) { - save_log.data.emplace_back(t.from, t.to, t.amount, t.time); + save_log.data.emplace_back(t.counterparty, t.receiving, t.amount, t.time); } return bank_dom::User(balance, password, save_log); }