Reorganized util and hpws source files. (#161)

This commit is contained in:
Ravin Perera
2020-11-23 23:18:42 +05:30
committed by GitHub
parent 86e2e1211c
commit d9517b6164
34 changed files with 201 additions and 167 deletions

View File

@@ -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

View File

@@ -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"

View File

@@ -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

View File

@@ -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

View File

@@ -3,7 +3,7 @@
#include "../pchheader.hpp"
#include "../conf.hpp"
#include "../hpws/hpws.hpp"
#include "hpws.hpp"
#include "comm_session_threshold.hpp"
namespace comm

View File

@@ -1,7 +1,7 @@
#include "pchheader.hpp"
#include "conf.hpp"
#include "crypto.hpp"
#include "util.hpp"
#include "util/util.hpp"
namespace conf
{

View File

@@ -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"

View File

@@ -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"

View File

@@ -1,6 +1,6 @@
#include "pchheader.hpp"
#include "crypto.hpp"
#include "util.hpp"
#include "util/util.hpp"
namespace crypto
{

View File

@@ -2,7 +2,7 @@
#include "h32.hpp"
#include "../conf.hpp"
#include "../hplog.hpp"
#include "../util.hpp"
#include "../util/util.hpp"
namespace hpfs
{

View File

@@ -3,7 +3,7 @@
**/
#include "pchheader.hpp"
#include "util.hpp"
#include "util/util.hpp"
#include "conf.hpp"
#include "crypto.hpp"
#include "sc.hpp"

View File

@@ -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"

View File

@@ -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"

View File

@@ -1,5 +1,5 @@
#include "../../pchheader.hpp"
#include "../../util.hpp"
#include "../../util/util.hpp"
#include "../../crypto.hpp"
#include "../../hplog.hpp"
#include "../usrmsg_common.hpp"

View File

@@ -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"

View File

@@ -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

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -1,5 +1,5 @@
#include "../pchheader.hpp"
#include "../util.hpp"
#include "../util/util.hpp"
#include "user_comm_session.hpp"
#include "user_session_handler.hpp"

View File

@@ -2,7 +2,7 @@
#define _HP_USR_USER_INPUT_
#include "../pchheader.hpp"
#include "../util.hpp"
#include "../util/util.hpp"
namespace usr
{

View File

@@ -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"

View File

@@ -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"

View File

@@ -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.
}
}

View File

@@ -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<std::string> 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<const std::string *> recent_hashes_list;
uint32_t maxsize;
public:
rollover_hashset(const uint32_t maxsize);
bool try_emplace(const std::string hash);
};
} // namespace util
#endif

43
src/util/ttl_set.cpp Normal file
View File

@@ -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

26
src/util/ttl_set.hpp Normal file
View File

@@ -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<std::string, uint64_t> 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

View File

@@ -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);

View File

@@ -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<std::string> 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<const std::string *> 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<std::string, uint64_t> 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.
*/