mirror of
https://github.com/oxen-io/lokinet
synced 2023-12-14 06:53:00 +01:00
Handle get_peer_stats request's list of router ids
This commit is contained in:
parent
4699280d97
commit
b037cf0ae4
|
@ -156,6 +156,24 @@ namespace llarp
|
|||
return statsList;
|
||||
}
|
||||
|
||||
std::vector<PeerStats>
|
||||
PeerDb::listPeerStats(const std::vector<RouterID>& ids) const
|
||||
{
|
||||
std::lock_guard guard(m_statsLock);
|
||||
|
||||
std::vector<PeerStats> statsList;
|
||||
statsList.reserve(ids.size());
|
||||
|
||||
for (const auto& id : ids)
|
||||
{
|
||||
const auto itr = m_peerStats.find(id);
|
||||
if (itr != m_peerStats.end())
|
||||
statsList.push_back(itr->second);
|
||||
}
|
||||
|
||||
return statsList;
|
||||
}
|
||||
|
||||
/// Assume we receive an RC at some point `R` in time which was signed at some point `S` in time
|
||||
/// and expires at some point `E` in time, as depicted below:
|
||||
///
|
||||
|
|
|
@ -90,6 +90,13 @@ namespace llarp
|
|||
std::vector<PeerStats>
|
||||
listAllPeerStats() const;
|
||||
|
||||
/// Lists specific peer stats.
|
||||
///
|
||||
/// @param peers is list of RouterIDs which are desired
|
||||
/// @return a list of the requested peers. Peers not found will be omitted.
|
||||
std::vector<PeerStats>
|
||||
listPeerStats(const std::vector<RouterID>& ids) const;
|
||||
|
||||
/// Handles a new gossiped RC, updating stats as needed. The database tracks the last
|
||||
/// advertised update time, so it knows whether this is a new RC or not.
|
||||
///
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <rpc/lokid_rpc_client.hpp>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <util/logging/logger.hpp>
|
||||
|
||||
#include <router/abstractrouter.hpp>
|
||||
|
@ -228,14 +229,40 @@ namespace llarp
|
|||
if (not m_Router->peerDb())
|
||||
{
|
||||
LogWarn("HandleGetPeerStats called when router has no peerDb set up.");
|
||||
throw std::runtime_error("Cannot handle get_peer_stats request when no peer db available");
|
||||
|
||||
// TODO: this can sometimes occur if lokid hits our API before we're done configuring
|
||||
// (mostly an issue in a loopback testnet)
|
||||
msg.send_reply("EAGAIN");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// TODO: parse input, expect list of peers to query for
|
||||
// msg.data[0] is expected to contain a bt list of router ids (in our preferred string
|
||||
// format)
|
||||
if (msg.data.empty())
|
||||
{
|
||||
LogWarn("lokid requested peer stats with no request body");
|
||||
msg.send_reply("peer stats request requires list of router IDs");
|
||||
return;
|
||||
}
|
||||
|
||||
auto statsList = m_Router->peerDb()->listAllPeerStats();
|
||||
std::vector<std::string> routerIdStrings;
|
||||
lokimq::bt_deserialize(msg.data[0], routerIdStrings);
|
||||
|
||||
std::vector<RouterID> routerIds;
|
||||
routerIds.reserve(routerIdStrings.size());
|
||||
|
||||
for (const auto& routerIdString : routerIdStrings)
|
||||
{
|
||||
RouterID id;
|
||||
if (not id.FromString(routerIdString))
|
||||
throw std::invalid_argument(stringify("Invalid router id: ", routerIdString));
|
||||
|
||||
routerIds.push_back(std::move(id));
|
||||
}
|
||||
|
||||
auto statsList = m_Router->peerDb()->listPeerStats(routerIds);
|
||||
|
||||
int32_t bufSize =
|
||||
256 + (statsList.size() * 1024); // TODO: tune this or allow to grow dynamically
|
||||
|
|
Loading…
Reference in a new issue