lokinet/llarp/dht/bucket.hpp

205 lines
4.7 KiB
C++
Raw Normal View History

#ifndef LLARP_DHT_BUCKET_HPP
#define LLARP_DHT_BUCKET_HPP
2018-12-12 00:48:54 +00:00
#include <dht/kademlia.hpp>
#include <dht/key.hpp>
2019-02-08 19:43:25 +00:00
#include <util/status.hpp>
#include <map>
#include <set>
2018-08-04 02:59:32 +00:00
#include <vector>
namespace llarp
{
namespace dht
{
template < typename Val_t >
2019-02-08 19:43:25 +00:00
struct Bucket : public util::IStateful
{
using BucketStorage_t = std::map< Key_t, Val_t, XorMetric >;
using Random_t = std::function< uint64_t() >;
Bucket(const Key_t& us, Random_t r) : nodes(XorMetric(us)), random(r){};
2019-02-11 17:14:43 +00:00
util::StatusObject
ExtractStatus() const override
2019-02-08 19:43:25 +00:00
{
2019-02-11 17:14:43 +00:00
util::StatusObject obj{};
2019-02-08 19:43:25 +00:00
for(const auto& item : nodes)
{
2019-02-11 17:14:43 +00:00
obj.Put(item.first.ToHex(), item.second.ExtractStatus());
2019-02-08 19:43:25 +00:00
}
2019-02-11 17:14:43 +00:00
return obj;
2019-02-08 19:43:25 +00:00
}
size_t
size() const
{
return nodes.size();
}
struct SetIntersector
{
bool
operator()(const typename BucketStorage_t::value_type& lhs,
const Key_t& rhs)
{
return lhs.first < rhs;
}
bool
operator()(const Key_t& lhs,
const typename BucketStorage_t::value_type& rhs)
{
return lhs < rhs.first;
}
};
2018-08-04 02:59:32 +00:00
bool
2018-08-29 20:40:26 +00:00
GetRandomNodeExcluding(Key_t& result,
const std::set< Key_t >& exclude) const
2018-08-04 02:59:32 +00:00
{
std::vector< typename BucketStorage_t::value_type > candidates;
std::set_difference(nodes.begin(), nodes.end(), exclude.begin(),
exclude.end(), std::back_inserter(candidates),
SetIntersector());
if(candidates.empty())
2018-08-04 02:59:32 +00:00
{
return false;
}
result = candidates[random() % candidates.size()].first;
2018-08-04 02:59:32 +00:00
return true;
}
bool
FindClosest(const Key_t& target, Key_t& result) const
{
Key_t mindist;
mindist.Fill(0xff);
for(const auto& item : nodes)
{
auto curDist = item.first ^ target;
if(curDist < mindist)
{
mindist = curDist;
result = item.first;
}
}
return nodes.size() > 0;
}
2018-08-29 20:40:26 +00:00
bool
GetManyRandom(std::set< Key_t >& result, size_t N) const
{
if(nodes.size() < N || nodes.empty())
2018-11-20 16:58:18 +00:00
{
llarp::LogWarn("Not enough dht nodes, have ", nodes.size(), " want ",
N);
2018-08-29 20:40:26 +00:00
return false;
2018-11-20 16:58:18 +00:00
}
2018-08-29 20:40:26 +00:00
if(nodes.size() == N)
{
std::transform(nodes.begin(), nodes.end(),
std::inserter(result, result.end()),
[](const auto& a) { return a.first; });
2018-08-29 20:40:26 +00:00
return true;
}
size_t expecting = N;
size_t sz = nodes.size();
while(N)
{
auto itr = nodes.begin();
std::advance(itr, random() % sz);
2018-08-29 20:40:26 +00:00
if(result.insert(itr->first).second)
{
2018-08-29 20:40:26 +00:00
--N;
}
2018-08-29 20:40:26 +00:00
}
return result.size() == expecting;
}
bool
FindCloseExcluding(const Key_t& target, Key_t& result,
2018-08-29 20:40:26 +00:00
const std::set< Key_t >& exclude) const
{
Key_t maxdist;
maxdist.Fill(0xff);
Key_t mindist;
mindist.Fill(0xff);
for(const auto& item : nodes)
{
2018-08-10 21:34:11 +00:00
if(exclude.count(item.first))
{
continue;
}
2018-08-10 21:34:11 +00:00
auto curDist = item.first ^ target;
if(curDist < mindist)
{
mindist = curDist;
result = item.first;
}
}
return mindist < maxdist;
}
bool
GetManyNearExcluding(const Key_t& target, std::set< Key_t >& result,
size_t N, const std::set< Key_t >& exclude) const
{
std::set< Key_t > s(exclude.begin(), exclude.end());
Key_t peer;
while(N--)
{
if(!FindCloseExcluding(target, peer, s))
{
return false;
}
s.insert(peer);
result.insert(peer);
}
return true;
}
void
PutNode(const Val_t& val)
{
auto itr = nodes.find(val.ID);
if(itr == nodes.end() || itr->second < val)
{
nodes[val.ID] = val;
}
}
void
DelNode(const Key_t& key)
{
auto itr = nodes.find(key);
if(itr != nodes.end())
{
nodes.erase(itr);
}
}
2018-12-19 17:48:29 +00:00
bool
HasNode(const Key_t& key) const
{
return nodes.find(key) != nodes.end();
}
void
Clear()
{
nodes.clear();
}
BucketStorage_t nodes;
Random_t random;
};
2018-07-17 04:37:50 +00:00
} // namespace dht
} // namespace llarp
#endif