mirror of https://github.com/oxen-io/lokinet
Merge pull request #1541 from majestrate/lns-consensus-2021-02-19
lns lookup consensuspull/1596/head
commit
6bd53484da
@ -0,0 +1,48 @@
|
|||||||
|
#include "lns_tracker.hpp"
|
||||||
|
|
||||||
|
namespace llarp::service
|
||||||
|
{
|
||||||
|
std::function<void(std::optional<LNSLookupTracker::Addr_t>)>
|
||||||
|
LNSLookupTracker::MakeResultHandler(
|
||||||
|
std::string name,
|
||||||
|
std::size_t numPeers,
|
||||||
|
std::function<void(std::optional<Addr_t>)> resultHandler)
|
||||||
|
{
|
||||||
|
m_PendingLookups.emplace(name, LookupInfo{numPeers, resultHandler});
|
||||||
|
return [name, this](std::optional<Addr_t> found) {
|
||||||
|
auto itr = m_PendingLookups.find(name);
|
||||||
|
if (itr == m_PendingLookups.end())
|
||||||
|
return;
|
||||||
|
itr->second.HandleOneResult(found);
|
||||||
|
if (itr->second.IsDone())
|
||||||
|
m_PendingLookups.erase(itr);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
LNSLookupTracker::LookupInfo::IsDone() const
|
||||||
|
{
|
||||||
|
return m_ResultsGotten == m_ResultsNeeded;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
LNSLookupTracker::LookupInfo::HandleOneResult(std::optional<Addr_t> result)
|
||||||
|
{
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
|
m_CurrentValues.insert(*result);
|
||||||
|
}
|
||||||
|
m_ResultsGotten++;
|
||||||
|
if (IsDone())
|
||||||
|
{
|
||||||
|
if (m_CurrentValues.size() == 1)
|
||||||
|
{
|
||||||
|
m_HandleResult(*m_CurrentValues.begin());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_HandleResult(std::nullopt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // namespace llarp::service
|
@ -0,0 +1,53 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <optional>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "address.hpp"
|
||||||
|
#include <llarp/router_id.hpp>
|
||||||
|
#include <oxenmq/variant.h>
|
||||||
|
|
||||||
|
namespace llarp::service
|
||||||
|
{
|
||||||
|
/// tracks and manages consensus of lns names we fetch from the network
|
||||||
|
class LNSLookupTracker
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using Addr_t = std::variant<Address, RouterID>;
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct LookupInfo
|
||||||
|
{
|
||||||
|
std::unordered_set<Addr_t> m_CurrentValues;
|
||||||
|
std::function<void(std::optional<Addr_t>)> m_HandleResult;
|
||||||
|
std::size_t m_ResultsGotten = 0;
|
||||||
|
std::size_t m_ResultsNeeded;
|
||||||
|
|
||||||
|
LookupInfo(std::size_t wantResults, std::function<void(std::optional<Addr_t>)> resultHandler)
|
||||||
|
: m_HandleResult{std::move(resultHandler)}, m_ResultsNeeded{wantResults}
|
||||||
|
{}
|
||||||
|
|
||||||
|
bool
|
||||||
|
IsDone() const;
|
||||||
|
|
||||||
|
void
|
||||||
|
HandleOneResult(std::optional<Addr_t> result);
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unordered_map<std::string, LookupInfo> m_PendingLookups;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// make a function that will handle consensus of an lns request
|
||||||
|
/// name is the name we are requesting
|
||||||
|
/// numPeers is the number of peers we asked
|
||||||
|
/// resultHandler is a function that we are wrapping that will handle the final result
|
||||||
|
std::function<void(std::optional<Addr_t>)>
|
||||||
|
MakeResultHandler(
|
||||||
|
std::string name,
|
||||||
|
std::size_t numPeers,
|
||||||
|
std::function<void(std::optional<Addr_t>)> resultHandler);
|
||||||
|
};
|
||||||
|
} // namespace llarp::service
|
Loading…
Reference in New Issue