mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Implemented user duplicate input message check.
This commit is contained in:
37
src/util.cpp
37
src/util.cpp
@@ -1,8 +1,45 @@
|
||||
#include "pchheader.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
namespace util
|
||||
{
|
||||
|
||||
// rollover_hashset class methods
|
||||
|
||||
rollover_hashset::rollover_hashset(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(std::string hash)
|
||||
{
|
||||
auto itr = recent_hashes.find(hash);
|
||||
if (itr == recent_hashes.end()) // Not found
|
||||
{
|
||||
// Add the new message hash to the set.
|
||||
auto [newitr, success] = recent_hashes.emplace(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.
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes provided bytes to hex string.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user