mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Control message to update peers. (#351)
This commit is contained in:
@@ -12,6 +12,7 @@ namespace msg::controlmsg
|
||||
|
||||
// Message types
|
||||
constexpr const char *MSGTYPE_CONTRACT_END = "contract_end";
|
||||
constexpr const char *MSGTYPE_PEER_CHANGESET = "peer_changeset";
|
||||
|
||||
} // namespace msg::controlmsg
|
||||
|
||||
|
||||
@@ -16,4 +16,9 @@ namespace msg::controlmsg
|
||||
return jctlmsg::extract_type(extracted_type, jdoc);
|
||||
}
|
||||
|
||||
int controlmsg_parser::extract_peer_changeset(std::vector<p2p::peer_properties> &added_peers, std::vector<p2p::peer_properties> &removed_peers) const
|
||||
{
|
||||
return jctlmsg::extract_peer_changeset(added_peers, removed_peers, jdoc);
|
||||
}
|
||||
|
||||
} // namespace msg::controlmsg
|
||||
@@ -2,6 +2,7 @@
|
||||
#define _HP_MSG_CONTROLMSG_PARSER_
|
||||
|
||||
#include "../pchheader.hpp"
|
||||
#include "../p2p/p2p.hpp"
|
||||
|
||||
namespace msg::controlmsg
|
||||
{
|
||||
@@ -12,6 +13,7 @@ namespace msg::controlmsg
|
||||
public:
|
||||
int parse(std::string_view message);
|
||||
int extract_type(std::string &extracted_type) const;
|
||||
int extract_peer_changeset(std::vector<p2p::peer_properties> &added_peers, std::vector<p2p::peer_properties> &removed_peers) const;
|
||||
};
|
||||
|
||||
} // namespace msg::controlmsg
|
||||
|
||||
@@ -12,14 +12,11 @@ namespace msg::controlmsg::json
|
||||
constexpr const char *SEP_COMMA_NOQUOTE = ",\"";
|
||||
constexpr const char *SEP_COLON_NOQUOTE = "\":";
|
||||
|
||||
// Message types
|
||||
constexpr const char *MSGTYPE_HANDSHAKE_CHALLENGE = "handshake_challenge";
|
||||
|
||||
/**
|
||||
* Parses a json control message sent by the contract.
|
||||
* @param d Jsoncons document to which the parsed json should be loaded.
|
||||
* @param message The message to parse.
|
||||
* Accepted message format:
|
||||
* Message format:
|
||||
* {
|
||||
* 'type': '<message type>'
|
||||
* ...
|
||||
@@ -57,4 +54,54 @@ namespace msg::controlmsg::json
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the peer changes from a peer changeset message.
|
||||
* Message format:
|
||||
* {
|
||||
* 'type': 'peer_changeset',
|
||||
* 'add': ['<ip1>','<ip2>', ...],
|
||||
* 'remove': ['<ip1>','<ip2>', ...]
|
||||
* }
|
||||
*/
|
||||
int extract_peer_changeset(std::vector<p2p::peer_properties> &added_peers, std::vector<p2p::peer_properties> &removed_peers, const jsoncons::json &d)
|
||||
{
|
||||
if (extract_peers_from_array(added_peers, msg::controlmsg::FLD_ADD, d) == -1 ||
|
||||
extract_peers_from_array(removed_peers, msg::controlmsg::FLD_REMOVE, d) == -1)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int extract_peers_from_array(std::vector<p2p::peer_properties> &peers, std::string_view field, const jsoncons::json &d)
|
||||
{
|
||||
if (d.contains(field))
|
||||
{
|
||||
if (!d[field].is_array())
|
||||
{
|
||||
LOG_ERROR << "Extract peers: Invalid array field: " << field;
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (auto &peer : d[field].array_range())
|
||||
{
|
||||
if (!peer.is<std::string>())
|
||||
{
|
||||
LOG_ERROR << "Extract peers: Invalid peer entry in field: " << field;
|
||||
return -1;
|
||||
}
|
||||
|
||||
conf::peer_ip_port ipp;
|
||||
if (ipp.from_string(peer.as<std::string_view>()) == -1)
|
||||
{
|
||||
LOG_ERROR << "Extract peers: Invalid peer format in field: " << field;
|
||||
return -1;
|
||||
}
|
||||
|
||||
peers.push_back(p2p::peer_properties{ipp, -1, 0, 0});
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace msg::controlmsg::json
|
||||
@@ -2,6 +2,7 @@
|
||||
#define _HP_MSG_JSON_CONTROLMSG_JSON_
|
||||
|
||||
#include "../../pchheader.hpp"
|
||||
#include "../../p2p/p2p.hpp"
|
||||
|
||||
/**
|
||||
* Parser helpers for smart contract control messages.
|
||||
@@ -12,6 +13,9 @@ namespace msg::controlmsg::json
|
||||
|
||||
int extract_type(std::string &extracted_type, const jsoncons::json &d);
|
||||
|
||||
int extract_peer_changeset(std::vector<p2p::peer_properties> &added_peers, std::vector<p2p::peer_properties> &removed_peers, const jsoncons::json &d);
|
||||
|
||||
int extract_peers_from_array(std::vector<p2p::peer_properties> &peers, std::string_view field, const jsoncons::json &d);
|
||||
|
||||
} // namespace msg::controlmsg::json
|
||||
|
||||
|
||||
Reference in New Issue
Block a user