Networking fixes related to peer connectivity issues (#384)

This commit is contained in:
Kithmini Gunawardhana
2023-09-23 10:03:45 +05:30
committed by GitHub
parent 6a2384cd03
commit e7e1268a99
23 changed files with 393 additions and 98 deletions

View File

@@ -11,7 +11,6 @@ namespace msg::controlmsg
constexpr const char *FLD_REMOVE = "remove";
// Message types
constexpr const char *MSGTYPE_CONTRACT_END = "contract_end";
constexpr const char *MSGTYPE_PEER_CHANGESET = "peer_changeset";
} // namespace msg::controlmsg

View File

@@ -16,9 +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
int controlmsg_parser::extract_peer_changeset(std::vector<p2p::peer_properties> &added_peers, std::vector<p2p::peer_properties> &removed_peers, bool &overwrite) const
{
return jctlmsg::extract_peer_changeset(added_peers, removed_peers, jdoc);
return jctlmsg::extract_peer_changeset(added_peers, removed_peers, overwrite, jdoc);
}
} // namespace msg::controlmsg

View File

@@ -13,7 +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;
int extract_peer_changeset(std::vector<p2p::peer_properties> &added_peers, std::vector<p2p::peer_properties> &removed_peers, bool &overwrite) const;
};
} // namespace msg::controlmsg

View File

@@ -16,7 +16,8 @@ union P2PMsgContent {
PeerListRequestMsg,
PeerListResponseMsg,
HpfsLogRequest,
HpfsLogResponse
HpfsLogResponse,
SuppressMsg
}
table P2PMsg {
@@ -82,6 +83,13 @@ table NplMsg {
// Make sure to update signature generation/verification whenever these fields are changed.
}
enum SuppressReason : byte { ContractIdMismatch = 0 }
table SuppressMsg {
pubkey:[ubyte]; // Sender pubkey.
reason: SuppressReason;
}
//--hpfs requests and responses--//
enum HpfsFsEntryResponseType : byte { Matched = 0, Mismatched = 1, Responded = 2, NotAvailable = 3 }

View File

@@ -298,6 +298,14 @@ namespace msg::fbuf::p2pmsg
return map;
}
const p2p::suppress_message create_suppress_from_msg(const p2p::peer_message_info &mi)
{
const auto &msg = *mi.p2p_msg->content_as_SuppressMsg();
return {
std::string(flatbuf_bytes_to_sv(msg.pubkey())),
(p2p::SUPPRESS_REASON)msg.reason()};
}
void flatbuf_hpfsfshashentries_to_hpfsfshashentries(std::vector<p2p::hpfs_fs_hash_entry> &fs_entries, const flatbuffers::Vector<flatbuffers::Offset<HpfsFSHashEntry>> *fhashes)
{
for (const HpfsFSHashEntry *f_hash : *fhashes)
@@ -602,6 +610,16 @@ namespace msg::fbuf::p2pmsg
create_p2p_msg(builder, P2PMsgContent_PeerListResponseMsg, msg.Union());
}
void create_suppress_msg(flatbuffers::FlatBufferBuilder &builder, const uint8_t reason)
{
const auto msg = CreateSuppressMsg(
builder,
sv_to_flatbuf_bytes(builder, conf::cfg.node.public_key),
(SuppressReason)reason);
create_p2p_msg(builder, P2PMsgContent_SuppressMsg, msg.Union());
}
const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<UserInputGroup>>>
user_input_map_to_flatbuf_user_input_group(flatbuffers::FlatBufferBuilder &builder, const std::unordered_map<std::string, std::list<usr::submitted_user_input>> &map)
{

View File

@@ -42,6 +42,8 @@ namespace msg::fbuf::p2pmsg
const p2p::hpfs_log_response create_hpfs_log_response_from_msg(const p2p::peer_message_info &mi);
const p2p::suppress_message create_suppress_from_msg(const p2p::peer_message_info &mi);
util::sequence_hash flatbuf_seqhash_to_seqhash(const msg::fbuf::p2pmsg::SequenceHash *fbseqhash);
const std::set<std::string> flatbuf_bytearrayvector_to_stringlist(const flatbuffers::Vector<flatbuffers::Offset<ByteArray>> *fbvec);
@@ -101,6 +103,8 @@ namespace msg::fbuf::p2pmsg
void create_msg_from_peer_list_response(flatbuffers::FlatBufferBuilder &builder, const std::vector<p2p::peer_properties> &peers, const std::optional<conf::peer_ip_port> &skipping_ip_port);
void create_suppress_msg(flatbuffers::FlatBufferBuilder &builder, const uint8_t reason);
const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<UserInputGroup>>>
user_input_map_to_flatbuf_user_input_group(flatbuffers::FlatBufferBuilder &builder, const std::unordered_map<std::string, std::list<usr::submitted_user_input>> &map);

View File

@@ -34,6 +34,9 @@ struct ProposalMsgBuilder;
struct NplMsg;
struct NplMsgBuilder;
struct SuppressMsg;
struct SuppressMsgBuilder;
struct HpfsFSHashEntry;
struct HpfsFSHashEntryBuilder;
@@ -100,11 +103,12 @@ enum P2PMsgContent {
P2PMsgContent_PeerListResponseMsg = 11,
P2PMsgContent_HpfsLogRequest = 12,
P2PMsgContent_HpfsLogResponse = 13,
P2PMsgContent_SuppressMsg = 14,
P2PMsgContent_MIN = P2PMsgContent_NONE,
P2PMsgContent_MAX = P2PMsgContent_HpfsLogResponse
P2PMsgContent_MAX = P2PMsgContent_SuppressMsg
};
inline const P2PMsgContent (&EnumValuesP2PMsgContent())[14] {
inline const P2PMsgContent (&EnumValuesP2PMsgContent())[15] {
static const P2PMsgContent values[] = {
P2PMsgContent_NONE,
P2PMsgContent_PeerChallengeMsg,
@@ -119,13 +123,14 @@ inline const P2PMsgContent (&EnumValuesP2PMsgContent())[14] {
P2PMsgContent_PeerListRequestMsg,
P2PMsgContent_PeerListResponseMsg,
P2PMsgContent_HpfsLogRequest,
P2PMsgContent_HpfsLogResponse
P2PMsgContent_HpfsLogResponse,
P2PMsgContent_SuppressMsg
};
return values;
}
inline const char * const *EnumNamesP2PMsgContent() {
static const char * const names[15] = {
static const char * const names[16] = {
"NONE",
"PeerChallengeMsg",
"PeerChallengeResponseMsg",
@@ -140,13 +145,14 @@ inline const char * const *EnumNamesP2PMsgContent() {
"PeerListResponseMsg",
"HpfsLogRequest",
"HpfsLogResponse",
"SuppressMsg",
nullptr
};
return names;
}
inline const char *EnumNameP2PMsgContent(P2PMsgContent e) {
if (flatbuffers::IsOutRange(e, P2PMsgContent_NONE, P2PMsgContent_HpfsLogResponse)) return "";
if (flatbuffers::IsOutRange(e, P2PMsgContent_NONE, P2PMsgContent_SuppressMsg)) return "";
const size_t index = static_cast<size_t>(e);
return EnumNamesP2PMsgContent()[index];
}
@@ -207,9 +213,40 @@ template<> struct P2PMsgContentTraits<msg::fbuf::p2pmsg::HpfsLogResponse> {
static const P2PMsgContent enum_value = P2PMsgContent_HpfsLogResponse;
};
template<> struct P2PMsgContentTraits<msg::fbuf::p2pmsg::SuppressMsg> {
static const P2PMsgContent enum_value = P2PMsgContent_SuppressMsg;
};
bool VerifyP2PMsgContent(flatbuffers::Verifier &verifier, const void *obj, P2PMsgContent type);
bool VerifyP2PMsgContentVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector<flatbuffers::Offset<void>> *values, const flatbuffers::Vector<uint8_t> *types);
enum SuppressReason {
SuppressReason_ContractIdMismatch = 0,
SuppressReason_MIN = SuppressReason_ContractIdMismatch,
SuppressReason_MAX = SuppressReason_ContractIdMismatch
};
inline const SuppressReason (&EnumValuesSuppressReason())[1] {
static const SuppressReason values[] = {
SuppressReason_ContractIdMismatch
};
return values;
}
inline const char * const *EnumNamesSuppressReason() {
static const char * const names[2] = {
"ContractIdMismatch",
nullptr
};
return names;
}
inline const char *EnumNameSuppressReason(SuppressReason e) {
if (flatbuffers::IsOutRange(e, SuppressReason_ContractIdMismatch, SuppressReason_ContractIdMismatch)) return "";
const size_t index = static_cast<size_t>(e);
return EnumNamesSuppressReason()[index];
}
enum HpfsFsEntryResponseType {
HpfsFsEntryResponseType_Matched = 0,
HpfsFsEntryResponseType_Mismatched = 1,
@@ -415,6 +452,9 @@ struct P2PMsg FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
const msg::fbuf::p2pmsg::HpfsLogResponse *content_as_HpfsLogResponse() const {
return content_type() == msg::fbuf::p2pmsg::P2PMsgContent_HpfsLogResponse ? static_cast<const msg::fbuf::p2pmsg::HpfsLogResponse *>(content()) : nullptr;
}
const msg::fbuf::p2pmsg::SuppressMsg *content_as_SuppressMsg() const {
return content_type() == msg::fbuf::p2pmsg::P2PMsgContent_SuppressMsg ? static_cast<const msg::fbuf::p2pmsg::SuppressMsg *>(content()) : nullptr;
}
void *mutable_content() {
return GetPointer<void *>(VT_CONTENT);
}
@@ -482,6 +522,10 @@ template<> inline const msg::fbuf::p2pmsg::HpfsLogResponse *P2PMsg::content_as<m
return content_as_HpfsLogResponse();
}
template<> inline const msg::fbuf::p2pmsg::SuppressMsg *P2PMsg::content_as<msg::fbuf::p2pmsg::SuppressMsg>() const {
return content_as_SuppressMsg();
}
struct P2PMsgBuilder {
typedef P2PMsg Table;
flatbuffers::FlatBufferBuilder &fbb_;
@@ -1338,6 +1382,76 @@ inline flatbuffers::Offset<NplMsg> CreateNplMsgDirect(
lcl_id);
}
struct SuppressMsg FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef SuppressMsgBuilder Builder;
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_PUBKEY = 4,
VT_REASON = 6
};
const flatbuffers::Vector<uint8_t> *pubkey() const {
return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_PUBKEY);
}
flatbuffers::Vector<uint8_t> *mutable_pubkey() {
return GetPointer<flatbuffers::Vector<uint8_t> *>(VT_PUBKEY);
}
msg::fbuf::p2pmsg::SuppressReason reason() const {
return static_cast<msg::fbuf::p2pmsg::SuppressReason>(GetField<int8_t>(VT_REASON, 0));
}
bool mutate_reason(msg::fbuf::p2pmsg::SuppressReason _reason) {
return SetField<int8_t>(VT_REASON, static_cast<int8_t>(_reason), 0);
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyOffset(verifier, VT_PUBKEY) &&
verifier.VerifyVector(pubkey()) &&
VerifyField<int8_t>(verifier, VT_REASON) &&
verifier.EndTable();
}
};
struct SuppressMsgBuilder {
typedef SuppressMsg Table;
flatbuffers::FlatBufferBuilder &fbb_;
flatbuffers::uoffset_t start_;
void add_pubkey(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> pubkey) {
fbb_.AddOffset(SuppressMsg::VT_PUBKEY, pubkey);
}
void add_reason(msg::fbuf::p2pmsg::SuppressReason reason) {
fbb_.AddElement<int8_t>(SuppressMsg::VT_REASON, static_cast<int8_t>(reason), 0);
}
explicit SuppressMsgBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
SuppressMsgBuilder &operator=(const SuppressMsgBuilder &);
flatbuffers::Offset<SuppressMsg> Finish() {
const auto end = fbb_.EndTable(start_);
auto o = flatbuffers::Offset<SuppressMsg>(end);
return o;
}
};
inline flatbuffers::Offset<SuppressMsg> CreateSuppressMsg(
flatbuffers::FlatBufferBuilder &_fbb,
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> pubkey = 0,
msg::fbuf::p2pmsg::SuppressReason reason = msg::fbuf::p2pmsg::SuppressReason_ContractIdMismatch) {
SuppressMsgBuilder builder_(_fbb);
builder_.add_pubkey(pubkey);
builder_.add_reason(reason);
return builder_.Finish();
}
inline flatbuffers::Offset<SuppressMsg> CreateSuppressMsgDirect(
flatbuffers::FlatBufferBuilder &_fbb,
const std::vector<uint8_t> *pubkey = nullptr,
msg::fbuf::p2pmsg::SuppressReason reason = msg::fbuf::p2pmsg::SuppressReason_ContractIdMismatch) {
auto pubkey__ = pubkey ? _fbb.CreateVector<uint8_t>(*pubkey) : 0;
return msg::fbuf::p2pmsg::CreateSuppressMsg(
_fbb,
pubkey__,
reason);
}
struct HpfsFSHashEntry FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef HpfsFSHashEntryBuilder Builder;
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
@@ -2692,6 +2806,10 @@ inline bool VerifyP2PMsgContent(flatbuffers::Verifier &verifier, const void *obj
auto ptr = reinterpret_cast<const msg::fbuf::p2pmsg::HpfsLogResponse *>(obj);
return verifier.VerifyTable(ptr);
}
case P2PMsgContent_SuppressMsg: {
auto ptr = reinterpret_cast<const msg::fbuf::p2pmsg::SuppressMsg *>(obj);
return verifier.VerifyTable(ptr);
}
default: return true;
}
}

View File

@@ -60,45 +60,73 @@ namespace msg::controlmsg::json
* {
* 'type': 'peer_changeset',
* 'add': ['<ip1>','<ip2>', ...],
* 'remove': ['<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)
int extract_peer_changeset(std::vector<p2p::peer_properties> &added_peers, std::vector<p2p::peer_properties> &removed_peers, bool &overwrite, 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)
overwrite = false;
if (!d.contains(msg::controlmsg::FLD_ADD) ||
extract_peers_from_array(added_peers, msg::controlmsg::FLD_ADD, d) == -1)
{
return -1;
}
if (d.contains(msg::controlmsg::FLD_REMOVE))
{
// Remove field can be string "*" or an array of strings.
// "*" means peers should be overwritten. (remove all peers and add the specified peers)
if (d[msg::controlmsg::FLD_REMOVE].is_string())
{
const std::string remove_str = d[msg::controlmsg::FLD_REMOVE].as<std::string>();
if (remove_str == "*")
{
overwrite = true;
}
else
{
return -1;
}
}
else
{
if (extract_peers_from_array(removed_peers, msg::controlmsg::FLD_REMOVE, d) == -1)
return -1;
}
}
else
{
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())
{
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 array field: " << field;
LOG_ERROR << "Extract peers: Invalid peer entry in field: " << field;
return -1;
}
for (auto &peer : d[field].array_range())
conf::peer_ip_port ipp;
if (ipp.from_string(peer.as<std::string_view>()) == -1)
{
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});
LOG_ERROR << "Extract peers: Invalid peer format in field: " << field;
return -1;
}
peers.push_back(p2p::peer_properties{ipp, -1, 0, 0});
}
return 0;

View File

@@ -13,7 +13,7 @@ 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_peer_changeset(std::vector<p2p::peer_properties> &added_peers, std::vector<p2p::peer_properties> &removed_peers, bool &overwrite, const jsoncons::json &d);
int extract_peers_from_array(std::vector<p2p::peer_properties> &peers, std::string_view field, const jsoncons::json &d);