From d9517b61644a959bf167399713784a6c40597f21 Mon Sep 17 00:00:00 2001 From: Ravin Perera <33562092+ravinsp@users.noreply.github.com> Date: Mon, 23 Nov 2020 23:18:42 +0530 Subject: [PATCH] Reorganized util and hpws source files. (#161) --- CMakeLists.txt | 4 +- src/bill/corebill.cpp | 3 +- src/comm/comm_server.hpp | 4 +- src/comm/comm_session.cpp | 4 +- src/comm/comm_session.hpp | 2 +- src/{hpws => comm}/hpws.hpp | 0 src/conf.cpp | 2 +- src/consensus.cpp | 1 + src/consensus.hpp | 2 +- src/crypto.cpp | 2 +- src/hpfs/hpfs.cpp | 2 +- src/main.cpp | 2 +- src/msg/bson/usrmsg_bson.cpp | 2 +- src/msg/fbuf/p2pmsg_helpers.cpp | 2 +- src/msg/json/usrmsg_json.cpp | 2 +- src/msg/usrmsg_parser.cpp | 2 +- src/msg/usrmsg_parser.hpp | 2 +- src/p2p/p2p.cpp | 2 +- src/p2p/peer_comm_server.cpp | 2 +- src/p2p/peer_session_handler.cpp | 3 +- src/sc.hpp | 2 +- src/state/state_serve.cpp | 2 +- src/state/state_sync.cpp | 2 +- src/usr/read_req.cpp | 2 +- src/usr/user_comm_session.cpp | 2 +- src/usr/user_input.hpp | 2 +- src/usr/usr.cpp | 2 +- src/usr/usr.hpp | 3 +- src/util/rollover_hashset.cpp | 39 ++++++++++ src/util/rollover_hashset.hpp | 30 ++++++++ src/util/ttl_set.cpp | 43 +++++++++++ src/util/ttl_set.hpp | 26 +++++++ src/{ => util}/util.cpp | 127 +++++++------------------------ src/{ => util}/util.hpp | 41 +--------- 34 files changed, 201 insertions(+), 167 deletions(-) rename src/{hpws => comm}/hpws.hpp (100%) create mode 100644 src/util/rollover_hashset.cpp create mode 100644 src/util/rollover_hashset.hpp create mode 100644 src/util/ttl_set.cpp create mode 100644 src/util/ttl_set.hpp rename src/{ => util}/util.cpp (74%) rename src/{ => util}/util.hpp (70%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 32d28b70..3be0a943 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,9 @@ add_executable(appbill #-------hpcore------- add_executable(hpcore - src/util.cpp + src/util/util.cpp + src/util/rollover_hashset.cpp + src/util/ttl_set.cpp src/util/buffer_store.cpp src/crypto.cpp src/conf.cpp diff --git a/src/bill/corebill.cpp b/src/bill/corebill.cpp index bbee5efa..1d508752 100644 --- a/src/bill/corebill.cpp +++ b/src/bill/corebill.cpp @@ -1,5 +1,6 @@ #include "../pchheader.hpp" -#include "../util.hpp" +#include "../util/util.hpp" +#include "../util/ttl_set.hpp" #include "../hplog.hpp" #include "corebill.h" diff --git a/src/comm/comm_server.hpp b/src/comm/comm_server.hpp index 76316689..51a17f1f 100644 --- a/src/comm/comm_server.hpp +++ b/src/comm/comm_server.hpp @@ -3,9 +3,9 @@ #include "../pchheader.hpp" #include "../hplog.hpp" -#include "../util.hpp" +#include "../util/util.hpp" #include "../bill/corebill.h" -#include "../hpws/hpws.hpp" +#include "hpws.hpp" #include "comm_session.hpp" namespace comm diff --git a/src/comm/comm_session.cpp b/src/comm/comm_session.cpp index 711482a9..ddd16d0c 100644 --- a/src/comm/comm_session.cpp +++ b/src/comm/comm_session.cpp @@ -1,9 +1,9 @@ #include "../pchheader.hpp" #include "../hplog.hpp" -#include "../util.hpp" +#include "../util/util.hpp" #include "../conf.hpp" #include "../bill/corebill.h" -#include "../hpws/hpws.hpp" +#include "hpws.hpp" #include "comm_session.hpp" namespace comm diff --git a/src/comm/comm_session.hpp b/src/comm/comm_session.hpp index 4bdb5664..1525b198 100644 --- a/src/comm/comm_session.hpp +++ b/src/comm/comm_session.hpp @@ -3,7 +3,7 @@ #include "../pchheader.hpp" #include "../conf.hpp" -#include "../hpws/hpws.hpp" +#include "hpws.hpp" #include "comm_session_threshold.hpp" namespace comm diff --git a/src/hpws/hpws.hpp b/src/comm/hpws.hpp similarity index 100% rename from src/hpws/hpws.hpp rename to src/comm/hpws.hpp diff --git a/src/conf.cpp b/src/conf.cpp index 082f20f7..755553f0 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -1,7 +1,7 @@ #include "pchheader.hpp" #include "conf.hpp" #include "crypto.hpp" -#include "util.hpp" +#include "util/util.hpp" namespace conf { diff --git a/src/consensus.cpp b/src/consensus.cpp index 3e439855..4714307c 100644 --- a/src/consensus.cpp +++ b/src/consensus.cpp @@ -1,5 +1,6 @@ #include "pchheader.hpp" #include "conf.hpp" +#include "util/rollover_hashset.hpp" #include "usr/usr.hpp" #include "usr/user_input.hpp" #include "p2p/p2p.hpp" diff --git a/src/consensus.hpp b/src/consensus.hpp index 72bbf479..b5f96b07 100644 --- a/src/consensus.hpp +++ b/src/consensus.hpp @@ -2,7 +2,7 @@ #define _HP_CONS_ #include "pchheader.hpp" -#include "util.hpp" +#include "util/util.hpp" #include "util/buffer_store.hpp" #include "sc.hpp" #include "p2p/p2p.hpp" diff --git a/src/crypto.cpp b/src/crypto.cpp index fe5082d2..045b074b 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -1,6 +1,6 @@ #include "pchheader.hpp" #include "crypto.hpp" -#include "util.hpp" +#include "util/util.hpp" namespace crypto { diff --git a/src/hpfs/hpfs.cpp b/src/hpfs/hpfs.cpp index 7a729b7b..17506718 100644 --- a/src/hpfs/hpfs.cpp +++ b/src/hpfs/hpfs.cpp @@ -2,7 +2,7 @@ #include "h32.hpp" #include "../conf.hpp" #include "../hplog.hpp" -#include "../util.hpp" +#include "../util/util.hpp" namespace hpfs { diff --git a/src/main.cpp b/src/main.cpp index a2ec855f..a4b58ebd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,7 @@ **/ #include "pchheader.hpp" -#include "util.hpp" +#include "util/util.hpp" #include "conf.hpp" #include "crypto.hpp" #include "sc.hpp" diff --git a/src/msg/bson/usrmsg_bson.cpp b/src/msg/bson/usrmsg_bson.cpp index 6450a1ec..b31bae8d 100644 --- a/src/msg/bson/usrmsg_bson.cpp +++ b/src/msg/bson/usrmsg_bson.cpp @@ -1,5 +1,5 @@ #include "../../pchheader.hpp" -#include "../../util.hpp" +#include "../../util/util.hpp" #include "../../hplog.hpp" #include "../usrmsg_common.hpp" #include "usrmsg_bson.hpp" diff --git a/src/msg/fbuf/p2pmsg_helpers.cpp b/src/msg/fbuf/p2pmsg_helpers.cpp index 14392c4b..4c5898f8 100644 --- a/src/msg/fbuf/p2pmsg_helpers.cpp +++ b/src/msg/fbuf/p2pmsg_helpers.cpp @@ -1,7 +1,7 @@ #include "../../pchheader.hpp" #include "../../conf.hpp" #include "../../crypto.hpp" -#include "../../util.hpp" +#include "../../util/util.hpp" #include "../../hplog.hpp" #include "../../hpfs/h32.hpp" #include "../../hpfs/hpfs.hpp" diff --git a/src/msg/json/usrmsg_json.cpp b/src/msg/json/usrmsg_json.cpp index 55fdee5f..42de0417 100644 --- a/src/msg/json/usrmsg_json.cpp +++ b/src/msg/json/usrmsg_json.cpp @@ -1,5 +1,5 @@ #include "../../pchheader.hpp" -#include "../../util.hpp" +#include "../../util/util.hpp" #include "../../crypto.hpp" #include "../../hplog.hpp" #include "../usrmsg_common.hpp" diff --git a/src/msg/usrmsg_parser.cpp b/src/msg/usrmsg_parser.cpp index 7f368cb8..7b24c32f 100644 --- a/src/msg/usrmsg_parser.cpp +++ b/src/msg/usrmsg_parser.cpp @@ -1,5 +1,5 @@ #include "../pchheader.hpp" -#include "../util.hpp" +#include "../util/util.hpp" #include "json/usrmsg_json.hpp" #include "bson/usrmsg_bson.hpp" #include "usrmsg_parser.hpp" diff --git a/src/msg/usrmsg_parser.hpp b/src/msg/usrmsg_parser.hpp index 05412d3c..de3e10d7 100644 --- a/src/msg/usrmsg_parser.hpp +++ b/src/msg/usrmsg_parser.hpp @@ -1,7 +1,7 @@ #ifndef _HP_MSG_USRMSG_PARSER_ #define _HP_MSG_USRMSG_PARSER_ -#include "../util.hpp" +#include "../util/util.hpp" #include "../pchheader.hpp" namespace msg::usrmsg diff --git a/src/p2p/p2p.cpp b/src/p2p/p2p.cpp index 7a9310e7..1b2b3d80 100644 --- a/src/p2p/p2p.cpp +++ b/src/p2p/p2p.cpp @@ -1,7 +1,7 @@ #include "../pchheader.hpp" #include "../conf.hpp" #include "../crypto.hpp" -#include "../util.hpp" +#include "../util/util.hpp" #include "../hplog.hpp" #include "../msg/fbuf/p2pmsg_helpers.hpp" #include "../ledger.hpp" diff --git a/src/p2p/peer_comm_server.cpp b/src/p2p/peer_comm_server.cpp index 3753bcdb..9eca33b0 100644 --- a/src/p2p/peer_comm_server.cpp +++ b/src/p2p/peer_comm_server.cpp @@ -1,5 +1,5 @@ #include "../comm/comm_server.hpp" -#include "../util.hpp" +#include "../util/util.hpp" #include "../msg/fbuf/p2pmsg_helpers.hpp" #include "../ledger.hpp" #include "peer_comm_server.hpp" diff --git a/src/p2p/peer_session_handler.cpp b/src/p2p/peer_session_handler.cpp index 5b23a730..5580b08a 100644 --- a/src/p2p/peer_session_handler.cpp +++ b/src/p2p/peer_session_handler.cpp @@ -2,7 +2,8 @@ #include "../conf.hpp" #include "../consensus.hpp" #include "../crypto.hpp" -#include "../util.hpp" +#include "../util/util.hpp" +#include "../util/rollover_hashset.hpp" #include "../hplog.hpp" #include "../msg/fbuf/p2pmsg_container_generated.h" #include "../msg/fbuf/p2pmsg_content_generated.h" diff --git a/src/sc.hpp b/src/sc.hpp index 18ce21d8..c2334cfa 100644 --- a/src/sc.hpp +++ b/src/sc.hpp @@ -4,7 +4,7 @@ #include "pchheader.hpp" #include "usr/usr.hpp" #include "hpfs/h32.hpp" -#include "util.hpp" +#include "util/util.hpp" #include "util/buffer_store.hpp" #include "p2p/p2p.hpp" diff --git a/src/state/state_serve.cpp b/src/state/state_serve.cpp index 00856abc..fb23944f 100644 --- a/src/state/state_serve.cpp +++ b/src/state/state_serve.cpp @@ -1,7 +1,7 @@ #include "../pchheader.hpp" #include "../hpfs/hpfs.hpp" #include "../hpfs/h32.hpp" -#include "../util.hpp" +#include "../util/util.hpp" #include "../p2p/p2p.hpp" #include "../msg/fbuf/p2pmsg_content_generated.h" #include "../msg/fbuf/p2pmsg_helpers.hpp" diff --git a/src/state/state_sync.cpp b/src/state/state_sync.cpp index 22109de0..f4293dc5 100644 --- a/src/state/state_sync.cpp +++ b/src/state/state_sync.cpp @@ -5,7 +5,7 @@ #include "../pchheader.hpp" #include "../ledger.hpp" #include "../hplog.hpp" -#include "../util.hpp" +#include "../util/util.hpp" #include "../hpfs/hpfs.hpp" #include "../hpfs/h32.hpp" #include "state_sync.hpp" diff --git a/src/usr/read_req.cpp b/src/usr/read_req.cpp index dbbe5c0a..b98823e6 100644 --- a/src/usr/read_req.cpp +++ b/src/usr/read_req.cpp @@ -1,6 +1,6 @@ #include "../pchheader.hpp" #include "../hplog.hpp" -#include "../util.hpp" +#include "../util/util.hpp" #include "../util/buffer_store.hpp" #include "../conf.hpp" #include "../msg/usrmsg_parser.hpp" diff --git a/src/usr/user_comm_session.cpp b/src/usr/user_comm_session.cpp index a4ed3777..3cefa820 100644 --- a/src/usr/user_comm_session.cpp +++ b/src/usr/user_comm_session.cpp @@ -1,5 +1,5 @@ #include "../pchheader.hpp" -#include "../util.hpp" +#include "../util/util.hpp" #include "user_comm_session.hpp" #include "user_session_handler.hpp" diff --git a/src/usr/user_input.hpp b/src/usr/user_input.hpp index 3200aba8..c3a79d0d 100644 --- a/src/usr/user_input.hpp +++ b/src/usr/user_input.hpp @@ -2,7 +2,7 @@ #define _HP_USR_USER_INPUT_ #include "../pchheader.hpp" -#include "../util.hpp" +#include "../util/util.hpp" namespace usr { diff --git a/src/usr/usr.cpp b/src/usr/usr.cpp index 6fe969c5..15204cce 100644 --- a/src/usr/usr.cpp +++ b/src/usr/usr.cpp @@ -2,7 +2,7 @@ #include "../msg/json/usrmsg_json.hpp" #include "../msg/usrmsg_parser.hpp" #include "../msg/usrmsg_common.hpp" -#include "../util.hpp" +#include "../util/util.hpp" #include "../conf.hpp" #include "../crypto.hpp" #include "../hplog.hpp" diff --git a/src/usr/usr.hpp b/src/usr/usr.hpp index 3d1b22c1..8b31f014 100644 --- a/src/usr/usr.hpp +++ b/src/usr/usr.hpp @@ -2,7 +2,8 @@ #define _HP_USR_ #include "../pchheader.hpp" -#include "../util.hpp" +#include "../util/util.hpp" +#include "../util/rollover_hashset.hpp" #include "../util/buffer_store.hpp" #include "../msg/usrmsg_parser.hpp" #include "user_comm_session.hpp" diff --git a/src/util/rollover_hashset.cpp b/src/util/rollover_hashset.cpp new file mode 100644 index 00000000..d22043f4 --- /dev/null +++ b/src/util/rollover_hashset.cpp @@ -0,0 +1,39 @@ +#include "rollover_hashset.hpp" + +namespace util +{ + + rollover_hashset::rollover_hashset(const uint32_t maxsize) + { + this->maxsize = maxsize == 0 ? 1 : maxsize; + } + + /** + * Inserts the given hash to the list. + * @return True on succesful insertion. False if hash already exists. + */ + bool rollover_hashset::try_emplace(const std::string hash) + { + const auto itr = recent_hashes.find(hash); + if (itr == recent_hashes.end()) // Not found + { + // Add the new message hash to the set. + const auto [newitr, success] = recent_hashes.emplace(std::move(hash)); + + // Insert a pointer to the stored hash value to the back of the ordered list of hashes. + recent_hashes_list.push_back(&(*newitr)); + + // Remove oldest hash if exceeding max size. + if (recent_hashes_list.size() > maxsize) + { + const std::string &oldest_hash = *recent_hashes_list.front(); + recent_hashes.erase(oldest_hash); + recent_hashes_list.pop_front(); + } + + return true; // Hash was inserted successfuly. + } + + return false; // Hash already exists. + } +} \ No newline at end of file diff --git a/src/util/rollover_hashset.hpp b/src/util/rollover_hashset.hpp new file mode 100644 index 00000000..a7648904 --- /dev/null +++ b/src/util/rollover_hashset.hpp @@ -0,0 +1,30 @@ +#ifndef _HP_UTIL_ROLLOVER_HASHSET_ +#define _HP_UTIL_ROLLOVER_HASHSET_ + +#include "../pchheader.hpp" + +namespace util +{ + + /** + * FIFO hash set with a max size. + */ + class rollover_hashset + { + private: + // The set of recent hashes used for duplicate detection. + std::unordered_set recent_hashes; + + // The supporting list of recent hashes used for adding and removing hashes from + // the 'recent_hashes' in a first-in-first-out manner. + std::list recent_hashes_list; + + uint32_t maxsize; + + public: + rollover_hashset(const uint32_t maxsize); + bool try_emplace(const std::string hash); + }; +} // namespace util + +#endif \ No newline at end of file diff --git a/src/util/ttl_set.cpp b/src/util/ttl_set.cpp new file mode 100644 index 00000000..ed7e4747 --- /dev/null +++ b/src/util/ttl_set.cpp @@ -0,0 +1,43 @@ +#include "ttl_set.hpp" +#include "util.hpp" + +namespace util +{ + + /** + * If key does not exist, inserts it with the specified ttl. If key exists, + * renews the expiration time to match the time-to-live from now onwards. + * @param key Object to insert. + * @param ttl Time to live in milliseonds. + */ + void ttl_set::emplace(const std::string key, const uint64_t ttl_milli) + { + ttlmap[key] = util::get_epoch_milliseconds() + ttl_milli; + } + + void ttl_set::erase(const std::string &key) + { + const auto itr = ttlmap.find(key); + if (itr != ttlmap.end()) + ttlmap.erase(itr); + } + + /** + * Returns true of the key exists and not expired. Returns false if key does not exist + * or has expired. + */ + bool ttl_set::exists(const std::string &key) + { + const auto itr = ttlmap.find(key); + if (itr == ttlmap.end()) // Not found + return false; + + // Check whether we are passed the expiration time (itr->second is the expiration time) + const bool expired = util::get_epoch_milliseconds() > itr->second; + if (expired) + ttlmap.erase(itr); + + return !expired; + } + +} // namespace util \ No newline at end of file diff --git a/src/util/ttl_set.hpp b/src/util/ttl_set.hpp new file mode 100644 index 00000000..748ae327 --- /dev/null +++ b/src/util/ttl_set.hpp @@ -0,0 +1,26 @@ +#ifndef _HP_UTIL_TTL_SET_ +#define _HP_UTIL_TTL_SET_ + +#include "../pchheader.hpp" + +namespace util +{ + + /** + * A string set with expiration for elements. + */ + class ttl_set + { + private: + // Keeps short-lived items with their absolute expiration time. + std::unordered_map ttlmap; + + public: + void emplace(const std::string key, const uint64_t ttl_milli); + void erase(const std::string &key); + bool exists(const std::string &key); + }; + +} // namespace util + +#endif \ No newline at end of file diff --git a/src/util.cpp b/src/util/util.cpp similarity index 74% rename from src/util.cpp rename to src/util/util.cpp index df9cb402..49635f9e 100644 --- a/src/util.cpp +++ b/src/util/util.cpp @@ -1,92 +1,17 @@ -#include "pchheader.hpp" -#include "hplog.hpp" +#include "../pchheader.hpp" +#include "../hplog.hpp" #include "util.hpp" namespace util { - - // rollover_hashset class methods - - rollover_hashset::rollover_hashset(const uint32_t maxsize) - { - this->maxsize = maxsize == 0 ? 1 : maxsize; - } - /** - * Inserts the given hash to the list. - * @return True on succesful insertion. False if hash already exists. - */ - bool rollover_hashset::try_emplace(const std::string hash) - { - const auto itr = recent_hashes.find(hash); - if (itr == recent_hashes.end()) // Not found - { - // Add the new message hash to the set. - const auto [newitr, success] = recent_hashes.emplace(std::move(hash)); - - // Insert a pointer to the stored hash value to the back of the ordered list of hashes. - recent_hashes_list.push_back(&(*newitr)); - - // Remove oldest hash if exceeding max size. - if (recent_hashes_list.size() > maxsize) - { - const std::string &oldest_hash = *recent_hashes_list.front(); - recent_hashes.erase(oldest_hash); - recent_hashes_list.pop_front(); - } - - return true; // Hash was inserted successfuly. - } - - return false; // Hash already exists. - } - - // ttl_set class methods. - - /** - * If key does not exist, inserts it with the specified ttl. If key exists, - * renews the expiration time to match the time-to-live from now onwards. - * @param key Object to insert. - * @param ttl Time to live in milliseonds. - */ - void ttl_set::emplace(const std::string key, const uint64_t ttl_milli) - { - ttlmap[key] = util::get_epoch_milliseconds() + ttl_milli; - } - - void ttl_set::erase(const std::string &key) - { - const auto itr = ttlmap.find(key); - if (itr != ttlmap.end()) - ttlmap.erase(itr); - } - - /** - * Returns true of the key exists and not expired. Returns false if key does not exist - * or has expired. - */ - bool ttl_set::exists(const std::string &key) - { - const auto itr = ttlmap.find(key); - if (itr == ttlmap.end()) // Not found - return false; - - // Check whether we are passed the expiration time (itr->second is the expiration time) - const bool expired = util::get_epoch_milliseconds() > itr->second; - if (expired) - ttlmap.erase(itr); - - return !expired; - } - - /** - * Encodes provided bytes to hex string. - * - * @param encoded_string String reference to assign the hex encoded output. - * @param bin Bytes to encode. - * @param bin_len Bytes length. - * @return Always returns 0. - */ + * Encodes provided bytes to hex string. + * + * @param encoded_string String reference to assign the hex encoded output. + * @param bin Bytes to encode. + * @param bin_len Bytes length. + * @return Always returns 0. + */ int bin2hex(std::string &encoded_string, const unsigned char *bin, const size_t bin_len) { // Allocate the target string. @@ -103,12 +28,12 @@ namespace util } /** - * Decodes provided hex string into bytes. - * - * @param decodedbuf Buffer to assign decoded bytes. - * @param decodedbuf_len Decoded buffer size. - * @param hex_str hex string to decode. - */ + * Decodes provided hex string into bytes. + * + * @param decodedbuf Buffer to assign decoded bytes. + * @param decodedbuf_len Decoded buffer size. + * @param hex_str hex string to decode. + */ int hex2bin(unsigned char *decodedbuf, const size_t decodedbuf_len, std::string_view hex_str) { const char *hex_end; @@ -152,17 +77,17 @@ namespace util } /** - * Compare two version strings in the format of "1.12.3". - * v1 < v2 -> returns -1 - * v1 == v2 -> returns 0 - * v1 > v2 -> returns +1 - * Error -> returns -2 - * - * Remark on string_view: In other places of the code-base we utilize string_view - * to pass immutable string references around. However in this function we keep the 'const string&' - * syntax because istringstream doesn't support string_view. It's not worth optmising - * this code as it's not being used in high-scale processing. - */ + * Compare two version strings in the format of "1.12.3". + * v1 < v2 -> returns -1 + * v1 == v2 -> returns 0 + * v1 > v2 -> returns +1 + * Error -> returns -2 + * + * Remark on string_view: In other places of the code-base we utilize string_view + * to pass immutable string references around. However in this function we keep the 'const string&' + * syntax because istringstream doesn't support string_view. It's not worth optmising + * this code as it's not being used in high-scale processing. + */ int version_compare(const std::string &x, const std::string &y) { std::istringstream ix(x), iy(y); diff --git a/src/util.hpp b/src/util/util.hpp similarity index 70% rename from src/util.hpp rename to src/util/util.hpp index ab12fd71..034c7000 100644 --- a/src/util.hpp +++ b/src/util/util.hpp @@ -1,7 +1,7 @@ -#ifndef _HP_UTIL_ -#define _HP_UTIL_ +#ifndef _HP_UTIL_UTIL_ +#define _HP_UTIL_UTIL_ -#include "pchheader.hpp" +#include "../pchheader.hpp" /** * Contains helper functions and data structures used by multiple other subsystems. @@ -29,41 +29,6 @@ namespace util // (Keeping this as int for effcient msg payload and comparison) constexpr uint8_t MIN_NPL_INPUT_VERSION = 1; - /** - * FIFO hash set with a max size. - */ - class rollover_hashset - { - private: - // The set of recent hashes used for duplicate detection. - std::unordered_set recent_hashes; - - // The supporting list of recent hashes used for adding and removing hashes from - // the 'recent_hashes' in a first-in-first-out manner. - std::list recent_hashes_list; - - uint32_t maxsize; - - public: - rollover_hashset(const uint32_t maxsize); - bool try_emplace(const std::string hash); - }; - - /** - * A string set with expiration for elements. - */ - class ttl_set - { - private: - // Keeps short-lived items with their absolute expiration time. - std::unordered_map ttlmap; - - public: - void emplace(const std::string key, const uint64_t ttl_milli); - void erase(const std::string &key); - bool exists(const std::string &key); - }; - /** * The messaging protocol used in a web socket channel. */