Unl change announcement to connected users. (#224)

Introduced json and bson messages for unl list announcement.
When the unl set is modified send a json or bson unl list message (according to the user protocol) to all the connected users.
This commit is contained in:
Chalith Desaman
2021-01-19 11:33:27 +05:30
committed by GitHub
parent cf02108576
commit 73f5eea451
12 changed files with 109 additions and 0 deletions

View File

@@ -493,6 +493,12 @@
})
statResponseResolvers = [];
}
else if (m.type == "unl_change") {
// UNL change announcement message is handled in this block.
console.log("Received :", m.type);
console.log(m.unl);
}
else {
console.log("Received unrecognized contract message: type:" + m.type);
return false;

View File

@@ -133,6 +133,31 @@ namespace msg::usrmsg::bson
encoder.flush();
}
/**
* Constructs unl list container message.
* @param msg String reference to copy the generated bson message string into.
* Message format:
* {
* "type": "unl_change",
* "unl": ["<pubkey1>{[1byte(11101101) prefix][32byte]}", ...], // Binary pubkey list of unl nodes.
* }
* @param unl_list The unl node pubkey list to be put in the message.
*/
void create_unl_list_container(std::vector<uint8_t> &msg, const ::std::set<std::string> &unl_list)
{
jsoncons::bson::bson_bytes_encoder encoder(msg);
encoder.begin_object();
encoder.key(msg::usrmsg::FLD_TYPE);
encoder.string_value(msg::usrmsg::MSGTYPE_UNL_CHANGE);
encoder.key(msg::usrmsg::FLD_UNL);
encoder.begin_array();
for (std::string_view unl : unl_list)
encoder.byte_string_value(unl);
encoder.end_array();
encoder.end_object();
encoder.flush();
}
/**
* Parses a bson message sent by a user.
* @param d BSON document to which the parsed bson should be loaded.

View File

@@ -18,6 +18,8 @@ namespace msg::usrmsg::bson
const util::merkle_hash_node &hash_root, const std::vector<std::pair<std::string, std::string>> &unl_sig,
const uint64_t lcl_seq_no, std::string_view lcl);
void create_unl_list_container(std::vector<uint8_t> &msg, const ::std::set<std::string> &unl_list);
int verify_user_handshake_response(std::string &extracted_pubkeyhex, std::string &extracted_protocol,
std::string_view response, std::string_view original_challenge);

View File

@@ -15,6 +15,7 @@ namespace msg::usrmsg::json
constexpr const char *SEP_COLON = "\":\"";
constexpr const char *SEP_COMMA_NOQUOTE = ",\"";
constexpr const char *SEP_COLON_NOQUOTE = "\":";
constexpr const char *DOUBLE_QUOTE = "\"";
// std::vector overload to concatonate string.
std::vector<uint8_t> &operator+=(std::vector<uint8_t> &vec, std::string_view sv)
@@ -312,6 +313,41 @@ namespace msg::usrmsg::json
msg += "]}";
}
/**
* Constructs unl list container message.
* @param msg String reference to copy the generated json message string into.
* Message format:
* {
* "type": "unl_change",
* ["<pubkey1>{[ed prefix][64 characters]}", ...], // Hex pubkey list of unl nodes.
* }
* @param unl_list The unl node pubkey list to be put in the message.
*/
void create_unl_list_container(std::vector<uint8_t> &msg, const ::std::set<std::string> &unl_list)
{
msg.reserve((69 * unl_list.size()) + 30);
msg += "{\"";
msg += msg::usrmsg::FLD_TYPE;
msg += SEP_COLON;
msg += msg::usrmsg::MSGTYPE_UNL_CHANGE;
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_UNL;
msg += "\":[";
int i = 0;
for (std::string_view unl : unl_list)
{
msg += DOUBLE_QUOTE;
msg += util::to_hex(unl);
msg += DOUBLE_QUOTE;
if (i < unl_list.size() - 1)
msg += ",";
i++;
}
msg += "]}";
}
/**
* Verifies the user handshake response with the original challenge issued to the user
* and the user public key contained in the response.

View File

@@ -22,6 +22,8 @@ namespace msg::usrmsg::json
const util::merkle_hash_node &hash_root, const std::vector<std::pair<std::string, std::string>> &unl_sig,
const uint64_t lcl_seq_no, std::string_view lcl);
void create_unl_list_container(std::vector<uint8_t> &msg, const ::std::set<std::string> &unl_list);
int verify_user_challenge(std::string &extracted_pubkeyhex, std::string &extracted_protocol, std::string &extracted_server_challenge,
std::string_view response, std::string_view original_challenge);

View File

@@ -45,6 +45,7 @@ namespace msg::usrmsg
constexpr const char *MSGTYPE_CONTRACT_OUTPUT = "contract_output";
constexpr const char *MSGTYPE_STAT = "stat";
constexpr const char *MSGTYPE_STAT_RESPONSE = "stat_response";
constexpr const char *MSGTYPE_UNL_CHANGE = "unl_change";
constexpr const char *MSGTYPE_UNKNOWN = "unknown";
// Values

View File

@@ -48,6 +48,14 @@ namespace msg::usrmsg
busrmsg::create_contract_output_container(msg, outputs, hash_root, unl_sig, lcl_seq_no, lcl);
}
void usrmsg_parser::create_unl_list_container(std::vector<uint8_t> &msg, const ::std::set<std::string> &unl_list) const
{
if (protocol == util::PROTOCOL::JSON)
jusrmsg::create_unl_list_container(msg, unl_list);
else
busrmsg::create_unl_list_container(msg, unl_list);
}
int usrmsg_parser::parse(std::string_view message)
{
if (protocol == util::PROTOCOL::JSON)

View File

@@ -30,6 +30,8 @@ namespace msg::usrmsg
const util::merkle_hash_node &hash_root, const std::vector<std::pair<std::string, std::string>> &unl_sig,
const uint64_t lcl_seq_no, std::string_view lcl) const;
void create_unl_list_container(std::vector<uint8_t> &msg, const ::std::set<std::string> &unl_list) const;
int parse(std::string_view message);
int extract_type(std::string &extracted_type) const;

View File

@@ -111,8 +111,12 @@ namespace unl
}
// Update the is_unl flag of peer sessions.
// Broadcast changed unl list to all the connected users.
if (is_unl_list_changed)
{
p2p::update_unl_connections();
usr::announce_unl_list(list);
}
}
} // namespace unl

View File

@@ -3,6 +3,7 @@
#include "pchheader.hpp"
#include "p2p/p2p.hpp"
#include "usr/usr.hpp"
/**
* Manages the UNL public keys of this node.

View File

@@ -417,4 +417,24 @@ namespace usr
}
}
/**
* Send unl list to all the connected users.
* @param unl_list Set of unl pubkeys.
*/
void announce_unl_list(const std::set<std::string> &unl_list)
{
std::scoped_lock<std::mutex> lock(ctx.users_mutex);
for (const auto &user : ctx.users)
{
const usr::connected_user &connected_user = user.second;
msg::usrmsg::usrmsg_parser parser(connected_user.protocol);
std::vector<uint8_t> msg;
parser.create_unl_list_container(msg, unl_list);
connected_user.session.send(msg);
}
}
} // namespace usr

View File

@@ -85,6 +85,8 @@ namespace usr
bool verify_appbill_check(std::string_view pubkey, const size_t input_len);
void announce_unl_list(const std::set<std::string> &unl_list);
} // namespace usr
#endif