mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Introduced UNL update control message. (#169)
This commit is contained in:
92
src/unl.cpp
Normal file
92
src/unl.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include "util/util.hpp"
|
||||
#include "hplog.hpp"
|
||||
#include "unl.hpp"
|
||||
|
||||
/**
|
||||
* Manages the UNL public keys of this node.
|
||||
*/
|
||||
namespace unl
|
||||
{
|
||||
std::set<std::string> list; // List of binary pubkeys of UNL.
|
||||
std::string json_list; // Stringified json array of UNL. (To be fed into the contract args)
|
||||
std::shared_mutex unl_mutex;
|
||||
|
||||
size_t count()
|
||||
{
|
||||
std::shared_lock lock(unl_mutex);
|
||||
return list.size();
|
||||
}
|
||||
|
||||
std::set<std::string> get()
|
||||
{
|
||||
std::shared_lock lock(unl_mutex);
|
||||
return list;
|
||||
}
|
||||
|
||||
std::string get_json()
|
||||
{
|
||||
std::shared_lock lock(unl_mutex);
|
||||
return json_list;
|
||||
}
|
||||
|
||||
bool exists(const std::string &bin_pubkey)
|
||||
{
|
||||
std::shared_lock lock(unl_mutex);
|
||||
return list.find(bin_pubkey) != list.end();
|
||||
}
|
||||
|
||||
void add(const std::vector<std::string> &additions)
|
||||
{
|
||||
if (additions.empty())
|
||||
return;
|
||||
|
||||
std::unique_lock lock(unl_mutex);
|
||||
|
||||
for (const std::string &pubkey : additions)
|
||||
list.emplace(pubkey);
|
||||
|
||||
update_json_list();
|
||||
}
|
||||
|
||||
void update(const std::vector<std::string> &additions, const std::vector<std::string> &removals)
|
||||
{
|
||||
if (additions.empty() && removals.empty())
|
||||
return;
|
||||
|
||||
std::unique_lock lock(unl_mutex);
|
||||
|
||||
for (const std::string &pubkey : additions)
|
||||
list.emplace(pubkey);
|
||||
|
||||
for (const std::string &pubkey : removals)
|
||||
list.erase(pubkey);
|
||||
|
||||
update_json_list();
|
||||
|
||||
LOG_INFO << "UNL updated. Count:" << list.size();
|
||||
;
|
||||
}
|
||||
|
||||
void update_json_list()
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << "[";
|
||||
for (auto pk = list.begin(); pk != list.end(); pk++)
|
||||
{
|
||||
if (pk != list.begin())
|
||||
os << ","; // Trailing comma separator for previous element.
|
||||
|
||||
// Convert binary pubkey into hex.
|
||||
std::string pubkeyhex;
|
||||
util::bin2hex(
|
||||
pubkeyhex,
|
||||
reinterpret_cast<const unsigned char *>(pk->data()) + 1,
|
||||
pk->length() - 1);
|
||||
|
||||
os << "\"" << pubkeyhex << "\"";
|
||||
}
|
||||
os << "]";
|
||||
json_list = os.str();
|
||||
}
|
||||
|
||||
} // namespace unl
|
||||
Reference in New Issue
Block a user