Introduced UNL update control message. (#169)

This commit is contained in:
Ravin Perera
2020-11-26 22:26:12 +05:30
committed by GitHub
parent 993eb11971
commit d476f787a7
20 changed files with 304 additions and 76 deletions

92
src/unl.cpp Normal file
View 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