LCL history request and response. (#59)

Detect and request missing lcl history from another random node.
Sending lcl history response to a asked node.
Getting lcl history response and applying it.
Delete lcl that exceeds max ledger sequence.
This commit is contained in:
Asanka Indrajith
2019-11-19 07:50:33 -05:00
committed by GitHub
parent 0439ec93e2
commit 95683035b9
18 changed files with 1006 additions and 98 deletions

View File

@@ -71,7 +71,8 @@ void peer_connection_watchdog()
}
}
util::sleep(conf::cfg.roundtime * 4);
//util::sleep(conf::cfg.roundtime * 4);
util::sleep(200);
}
}
@@ -97,4 +98,38 @@ void broadcast_message(const peer_outbound_message msg, bool send_to_self)
}
}
/**
* Send the given message to a random peer from currently connected outbound peers.
* @param msg peer outbound message to be sent to peer
*/
void send_message_to_random_peer(peer_outbound_message msg)
{
size_t connected_peers = ctx.peer_connections.size();
if (connected_peers == 0)
{
LOG_DBG << "No peers to send (not even self).";
return;
}
else if (connected_peers == 1 && ctx.peer_connections.begin()->second->is_self)
{
LOG_DBG << "Only self is connected.";
return;
}
//Send while locking the peer_connections.
std::lock_guard<std::mutex> lock(p2p::ctx.peer_connections_mutex);
// Initialize random number generator with current timestamp.
int random_peer_index = (rand() % connected_peers); // select a random peer index.
auto it = ctx.peer_connections.begin();
std::advance(it, random_peer_index); //move iterator to point to random selected peer.
//send message to selecte peer.
auto session = it->second;
if (!session->is_self)
{
session->send(msg);
}
}
} // namespace p2p

View File

@@ -8,7 +8,7 @@
namespace p2p
{
struct proposal
{
std::string pubkey;
@@ -26,6 +26,23 @@ struct nonunl_proposal
std::unordered_map<std::string, const std::list<usr::user_submitted_message>> user_messages;
};
struct history_request
{
std::string minimum_lcl;
std::string required_lcl;
};
struct history_ledger
{
std::string lcl;
std::vector<uint8_t> raw_ledger;
};
struct history_response
{
std::map<uint64_t,const history_ledger> hist_ledgers;
};
struct npl_message
{
std::string data;
@@ -56,6 +73,7 @@ struct connected_context
// Peer connection watchdog runs on this thread.
std::thread peer_watchdog_thread;
};
extern connected_context ctx;
struct listener_context
@@ -85,6 +103,10 @@ void peer_connection_watchdog();
void broadcast_message(const peer_outbound_message msg, bool send_to_self);
void send_message_to_random_peer(peer_outbound_message msg);
void send_message_to_peer(std::string peer_session_id, peer_outbound_message msg);
} // namespace p2p
#endif

View File

@@ -11,6 +11,7 @@
#include "../sock/socket_session.hpp"
#include "p2p.hpp"
#include "peer_session_handler.hpp"
#include "../cons/ledger_handler.hpp"
namespace p2pmsg = fbschema::p2pmsg;
@@ -109,6 +110,26 @@ void peer_session_handler::on_message(sock::socket_session<peer_outbound_message
const std::string npl_message(reinterpret_cast<const char *>(container_buf_ptr), container_buf_size);
ctx.collected_msgs.npl_messages.push_back(std::move(npl_message));
}
else if (content_message_type == p2pmsg::Message_History_Request_Message) //message is a lcl history request message
{
LOG_DBG << "Received history request message type from peer.";
const p2p::history_request hr = p2pmsg::create_history_request_from_msg(*content->message_as_History_Request_Message());
//first check node has the required lcl available. -> if so send lcl history accordingly.
bool req_lcl_avail = cons::check_required_lcl_availability(hr);
if (req_lcl_avail > 0)
{
p2p::peer_outbound_message hr_msg = cons::send_ledger_history(hr);
session->send(hr_msg);
}
}
else if (content_message_type == p2pmsg::Message_History_Response_Message) //message is a lcl history response message
{
LOG_DBG << "Received history response message type from peer.";
cons::handle_ledger_history_response(
p2pmsg::create_history_response_from_msg(*content->message_as_History_Response_Message()));
}
else
{
session->increment_metric(sock::SESSION_THRESHOLDS::MAX_BADMSGS_PER_MINUTE, 1);