lokinet/llarp/dht/tx.hpp

125 lines
3.1 KiB
C++
Raw Normal View History

2019-01-22 01:14:02 +00:00
#ifndef LLARP_DHT_TX
#define LLARP_DHT_TX
#include <dht/key.hpp>
#include <dht/txowner.hpp>
2019-09-01 12:10:49 +00:00
#include <util/logging/logger.hpp>
2019-02-08 19:43:25 +00:00
#include <util/status.hpp>
2019-01-22 01:14:02 +00:00
#include <set>
#include <vector>
namespace llarp
{
namespace dht
{
struct AbstractContext;
2019-01-22 01:14:02 +00:00
template < typename K, typename V >
2019-04-19 15:10:26 +00:00
struct TX
2019-01-22 01:14:02 +00:00
{
K target;
AbstractContext* parent;
2019-01-22 01:14:02 +00:00
std::set< Key_t > peersAsked;
std::vector< V > valuesFound;
TXOwner whoasked;
TX(const TXOwner& asker, const K& k, AbstractContext* p)
: target(k), parent(p), whoasked(asker)
2019-01-22 01:14:02 +00:00
{
}
2019-07-30 23:42:13 +00:00
virtual ~TX() = default;
2019-01-22 01:14:02 +00:00
2019-01-22 23:50:26 +00:00
void
OnFound(const Key_t& askedPeer, const V& value);
/// return true if we want to persist this tx
bool
AskNextPeer(const Key_t& prevPeer, const std::unique_ptr< Key_t >& next);
2019-02-11 17:14:43 +00:00
util::StatusObject
2019-04-19 15:10:26 +00:00
ExtractStatus() const
2019-02-08 19:43:25 +00:00
{
2019-02-11 17:14:43 +00:00
util::StatusObject obj{{"whoasked", whoasked.ExtractStatus()},
2019-08-19 21:26:34 +00:00
{"target", target.ToString()}};
2019-02-11 17:14:43 +00:00
std::vector< util::StatusObject > foundObjs;
std::transform(valuesFound.begin(), valuesFound.end(),
std::back_inserter(foundObjs),
[](const auto& item) -> util::StatusObject {
return item.ExtractStatus();
});
obj["found"] = foundObjs;
2019-02-11 17:14:43 +00:00
std::vector< std::string > asked;
std::transform(
peersAsked.begin(), peersAsked.end(), std::back_inserter(asked),
2019-08-19 21:26:34 +00:00
[](const auto& item) -> std::string { return item.ToString(); });
obj["asked"] = asked;
2019-02-11 17:14:43 +00:00
return obj;
2019-02-08 19:43:25 +00:00
}
2019-01-22 01:14:02 +00:00
virtual bool
Validate(const V& value) const = 0;
virtual void
Start(const TXOwner& peer) = 0;
virtual bool
GetNextPeer(Key_t& next, const std::set< Key_t >& exclude) = 0;
virtual void
DoNextRequest(const Key_t& peer) = 0;
virtual void
SendReply() = 0;
};
template < typename K, typename V >
inline void
2019-01-22 23:50:26 +00:00
TX< K, V >::OnFound(const Key_t& askedPeer, const V& value)
2019-01-22 01:14:02 +00:00
{
peersAsked.insert(askedPeer);
if(Validate(value))
{
valuesFound.push_back(value);
}
}
template < typename K, typename V >
inline bool
TX< K, V >::AskNextPeer(const Key_t& prevPeer,
const std::unique_ptr< Key_t >& next)
{
peersAsked.insert(prevPeer);
Key_t peer;
if(next)
{
// explicit next peer provided
peer = *next;
}
2019-03-27 13:53:37 +00:00
else if(GetNextPeer(peer, peersAsked))
2019-01-22 01:14:02 +00:00
{
2019-03-27 13:53:37 +00:00
const Key_t targetKey{target};
if((prevPeer ^ targetKey) < (peer ^ targetKey))
{
// next peer is not closer
2019-07-17 12:25:51 +00:00
llarp::LogDebug("next peer ", peer.SNode(), " is not closer to ",
target, " than ", prevPeer.SNode());
2019-03-27 13:53:37 +00:00
return false;
}
2019-01-22 01:14:02 +00:00
}
else
{
2019-07-17 12:25:51 +00:00
llarp::LogDebug("no more peers for request asking for ", target);
2019-03-27 13:53:37 +00:00
return false;
2019-01-22 01:14:02 +00:00
}
2019-03-27 13:53:37 +00:00
peersAsked.insert(peer);
2019-01-22 01:14:02 +00:00
DoNextRequest(peer);
return true;
}
} // namespace dht
} // namespace llarp
#endif