Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework FindIntro logic per redundant strategy #1

Open
wants to merge 3 commits into
base: redundant-introset-propagation-2020-02-19
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions llarp/dht/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ namespace llarp
GetRouter()->rcLookupHandler().CheckRC(rc);
}

/// on behalf of whoasked request introset for target from dht router with
/// key askpeer
void
LookupIntroSetRecursive(
LookupIntroSetRelayed(
const Key_t& target, const Key_t& whoasked, uint64_t whoaskedTX,
const Key_t& askpeer, uint64_t recursionDepth, uint64_t relayOrder,
const Key_t& askpeer, uint64_t relayOrder,
service::EncryptedIntroSetLookupHandler result = nullptr) override;

void
LookupIntroSetIterative(
LookupIntroSetDirect(
const Key_t& target, const Key_t& whoasked, uint64_t whoaskedTX,
const Key_t& askpeer,
service::EncryptedIntroSetLookupHandler result = nullptr) override;
Expand Down Expand Up @@ -94,7 +92,6 @@ namespace llarp
void
LookupIntroSetForPath(const Key_t& addr, uint64_t txid,
const llarp::PathID_t& path, const Key_t& askpeer,
uint64_t recursionDepth,
uint64_t relayOrder) override;

/// send a dht message to peer, if keepalive is true then keep the session
Expand Down Expand Up @@ -542,16 +539,14 @@ namespace llarp
void
Context::LookupIntroSetForPath(const Key_t& addr, uint64_t txid,
const llarp::PathID_t& path,
const Key_t& askpeer,
uint64_t recursionDepth, uint64_t relayOrder)
const Key_t& askpeer, uint64_t relayOrder)
{
TXOwner asker(OurKey(), txid);
TXOwner peer(askpeer, ++ids);
_pendingIntrosetLookups.NewTX(
peer, asker, addr,
new LocalServiceAddressLookup(path, txid, relayOrder, addr, this,
askpeer),
((recursionDepth + 1) * 2000));
askpeer));
}

void
Expand All @@ -569,18 +564,16 @@ namespace llarp
}

void
Context::LookupIntroSetRecursive(
Context::LookupIntroSetRelayed(
const Key_t& addr, const Key_t& whoasked, uint64_t txid,
const Key_t& askpeer, uint64_t recursionDepth, uint64_t relayOrder,
const Key_t& askpeer, uint64_t relayOrder,
service::EncryptedIntroSetLookupHandler handler)
{
TXOwner asker(whoasked, txid);
TXOwner peer(askpeer, ++ids);
_pendingIntrosetLookups.NewTX(
peer, asker, addr,
new ServiceAddressLookup(asker, addr, this, recursionDepth,
relayOrder, handler),
((recursionDepth + 1) * 2000));
new ServiceAddressLookup(asker, addr, this, relayOrder, handler));
}

void
Expand All @@ -594,15 +587,15 @@ namespace llarp
}

void
Context::LookupIntroSetIterative(
Context::LookupIntroSetDirect(
const Key_t& addr, const Key_t& whoasked, uint64_t txid,
const Key_t& askpeer, service::EncryptedIntroSetLookupHandler handler)
{
TXOwner asker(whoasked, txid);
TXOwner peer(askpeer, ++ids);
_pendingIntrosetLookups.NewTX(
peer, asker, addr,
new ServiceAddressLookup(asker, addr, this, 0, 0, handler), 1000);
new ServiceAddressLookup(asker, addr, this, 0, handler), 1000);
}

bool
Expand Down
25 changes: 12 additions & 13 deletions llarp/dht/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,20 @@ namespace llarp
uint64_t whoaskedTX, const Key_t& askpeer,
RouterLookupHandler result = nullptr) = 0;

/// on behalf of whoasked request introset for target from dht router with
/// key askpeer
/// Ask a Service Node to perform an Introset lookup for us
virtual void
LookupIntroSetRecursive(
const Key_t& target, const Key_t& whoasked, uint64_t whoaskedTX,
const Key_t& askpeer, uint64_t recursionDepth, uint64_t relayOrder,
service::EncryptedIntroSetLookupHandler result =
service::EncryptedIntroSetLookupHandler()) = 0;
LookupIntroSetRelayed(const Key_t& target, const Key_t& whoasked,
uint64_t whoaskedTX, const Key_t& askpeer,
uint64_t relayOrder,
service::EncryptedIntroSetLookupHandler result =
service::EncryptedIntroSetLookupHandler()) = 0;

/// Directly as a Service Node for an Introset
virtual void
LookupIntroSetIterative(
const Key_t& target, const Key_t& whoasked, uint64_t whoaskedTX,
const Key_t& askpeer,
service::EncryptedIntroSetLookupHandler result =
service::EncryptedIntroSetLookupHandler()) = 0;
LookupIntroSetDirect(const Key_t& target, const Key_t& whoasked,
uint64_t whoaskedTX, const Key_t& askpeer,
service::EncryptedIntroSetLookupHandler result =
service::EncryptedIntroSetLookupHandler()) = 0;

virtual bool
HasRouterLookup(const RouterID& target) const = 0;
Expand All @@ -69,7 +68,7 @@ namespace llarp
virtual void
LookupIntroSetForPath(const Key_t& addr, uint64_t txid,
const PathID_t& path, const Key_t& askpeer,
uint64_t recursionDepth, uint64_t relayOrder) = 0;
uint64_t relayOrder) = 0;

virtual void
DHTSendTo(const RouterID& peer, IMessage* msg, bool keepalive = true) = 0;
Expand Down
2 changes: 1 addition & 1 deletion llarp/dht/localserviceaddresslookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace llarp
const PathID_t &pathid, uint64_t txid, uint64_t relayOrder,
const Key_t &addr, AbstractContext *ctx,
__attribute__((unused)) const Key_t &askpeer)
: ServiceAddressLookup(TXOwner{ctx->OurKey(), txid}, addr, ctx, 2,
: ServiceAddressLookup(TXOwner{ctx->OurKey(), txid}, addr, ctx,
relayOrder, nullptr)
, localPath(pathid)
{
Expand Down
66 changes: 13 additions & 53 deletions llarp/dht/messages/findintro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ namespace llarp
{
namespace dht
{
/// 2 ** 12 which is 4096 nodes, after which this starts to fail "more"
const uint64_t FindIntroMessage::MaxRecursionDepth = 12;
FindIntroMessage::~FindIntroMessage() = default;
FindIntroMessage::~FindIntroMessage() = default;

bool
FindIntroMessage::DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val)
Expand All @@ -24,9 +22,6 @@ namespace llarp
if(!BEncodeMaybeReadDictInt("O", relayOrder, read, k, val))
return false;

if(!BEncodeMaybeReadDictInt("R", recursionDepth, read, k, val))
return false;

if(!BEncodeMaybeReadDictEntry("S", location, read, k, val))
return false;

Expand Down Expand Up @@ -55,9 +50,6 @@ namespace llarp
if(!BEncodeWriteDictInt("O", relayOrder, buf))
return false;

// recursion
if(!BEncodeWriteDictInt("R", recursionDepth, buf))
return false;
// service address
if(!BEncodeWriteDictEntry("S", location, buf))
return false;
Expand All @@ -70,10 +62,6 @@ namespace llarp
// relay order
if(!BEncodeWriteDictInt("O", relayOrder, buf))
return false;

// recursion
if(!BEncodeWriteDictInt("R", recursionDepth, buf))
return false;
}
// txid
if(!BEncodeWriteDictInt("T", txID, buf))
Expand All @@ -89,46 +77,25 @@ namespace llarp
FindIntroMessage::HandleMessage(
llarp_dht_context* ctx, std::vector< IMessage::Ptr_t >& replies) const
{
if(recursionDepth > MaxRecursionDepth)
{
llarp::LogError("recursion depth big, ", recursionDepth, "> ",
MaxRecursionDepth);
return false;
}
auto& dht = *ctx->impl;
if(dht.pendingIntrosetLookups().HasPendingLookupFrom(TXOwner{From, txID}))
{
llarp::LogWarn("duplicate FIM from ", From, " txid=", txID);
return false;
}

std::set< Key_t > exclude = {dht.OurKey(), From};
if(not tagName.Empty())
return false;

// bad request (request for zero-key)
if(location.IsZero())
{
// we dont got it
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
const auto maybe = dht.GetIntroSetByLocation(location);
if(maybe.has_value())
{
replies.emplace_back(new GotIntroMessage({maybe.value()}, txID));
return true;
}

const Key_t us = dht.OurKey();

if(recursionDepth == 0)
{
// we don't have it
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}

// we are recursive
// we are relaying this message for e.g. a client
if(relayed)
{
uint32_t numDesired = 0;
Expand Down Expand Up @@ -158,29 +125,22 @@ namespace llarp
for(const auto& entry : closestRCs)
{
Key_t peer = Key_t(entry.pubkey);
dht.LookupIntroSetForPath(location, txID, pathID, peer,
recursionDepth - 1, 0);
dht.LookupIntroSetForPath(location, txID, pathID, peer, 0);
}
}
else
{
const auto rc = dht.GetRouter()->nodedb()->FindClosestTo(location);

Key_t peer = Key_t(rc.pubkey);

if((us ^ location) <= (peer ^ location))
// we should have this value if introset was propagated properly
const auto maybe = dht.GetIntroSetByLocation(location);
if(maybe.has_value())
{
// ask second closest as we are recursive
if(not dht.Nodes()->FindCloseExcluding(location, peer, exclude))
{
// no second closeset
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
replies.emplace_back(new GotIntroMessage({maybe.value()}, txID));
}
else
{
LogWarn("Got FIM with relayed == false and we don't have entry");
replies.emplace_back(new GotIntroMessage({}, txID));
}

dht.LookupIntroSetRecursive(location, From, txID, peer,
recursionDepth - 1, 0);
}
return true;
}
Expand Down
17 changes: 3 additions & 14 deletions llarp/dht/messages/findintro.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace llarp
{
struct FindIntroMessage final : public IMessage
{
static const uint64_t MaxRecursionDepth;
uint64_t recursionDepth = 0;
Key_t location;
llarp::service::Tag tagName;
uint64_t txID = 0;
Expand All @@ -27,23 +25,14 @@ namespace llarp
relayOrder = order;
}

FindIntroMessage(const llarp::service::Tag& tag, uint64_t txid,
bool iterate = true)
FindIntroMessage(const llarp::service::Tag& tag, uint64_t txid)
: IMessage({}), tagName(tag), txID(txid)
{
if(iterate)
recursionDepth = 0;
else
recursionDepth = 1;
}

explicit FindIntroMessage(uint64_t txid, const Key_t& addr,
uint64_t maxRecursionDepth, uint64_t order)
: IMessage({})
, recursionDepth(maxRecursionDepth)
, location(addr)
, txID(txid)
, relayOrder(order)
uint64_t order)
: IMessage({}), location(addr), txID(txid), relayOrder(order)
{
tagName.Zero();
}
Expand Down
21 changes: 5 additions & 16 deletions llarp/dht/serviceaddresslookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ namespace llarp
{
ServiceAddressLookup::ServiceAddressLookup(
const TXOwner &asker, const Key_t &addr, AbstractContext *ctx,
uint64_t recursion, uint32_t order,
service::EncryptedIntroSetLookupHandler handler)
uint32_t order, service::EncryptedIntroSetLookupHandler handler)
: TX< Key_t, service::EncryptedIntroSet >(asker, addr, ctx)
, handleResult(std::move(handler))
, recursionDepth(recursion)
, relayOrder(order)
{
peersAsked.insert(ctx->OurKey());
Expand Down Expand Up @@ -54,24 +52,15 @@ namespace llarp
void
ServiceAddressLookup::Start(const TXOwner &peer)
{
parent->DHTSendTo(
peer.node.as_array(),
new FindIntroMessage(peer.txid, target, recursionDepth, relayOrder));
parent->DHTSendTo(peer.node.as_array(),
new FindIntroMessage(peer.txid, target, relayOrder));
}

void
ServiceAddressLookup::DoNextRequest(const Key_t &ask)
{
if(recursionDepth)
{
parent->LookupIntroSetRecursive(target, whoasked.node, whoasked.txid,
ask, recursionDepth - 1, relayOrder);
}
else
{
parent->LookupIntroSetIterative(target, whoasked.node, whoasked.txid,
ask);
}
(void)ask;
// do nothing -- we handle propagating this explicitly
}

void
Expand Down
4 changes: 1 addition & 3 deletions llarp/dht/serviceaddresslookup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ namespace llarp
struct ServiceAddressLookup : public TX< Key_t, service::EncryptedIntroSet >
{
service::EncryptedIntroSetLookupHandler handleResult;
uint64_t recursionDepth;
uint32_t relayOrder;

ServiceAddressLookup(const TXOwner &asker, const Key_t &addr,
AbstractContext *ctx, uint64_t recursionDepth,
uint32_t relayOrder,
AbstractContext *ctx, uint32_t relayOrder,
service::EncryptedIntroSetLookupHandler handler);

bool
Expand Down
5 changes: 2 additions & 3 deletions llarp/dht/taglookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ namespace llarp
void
TagLookup::Start(const TXOwner &peer)
{
parent->DHTSendTo(
peer.node.as_array(),
new FindIntroMessage(target, peer.txid, recursionDepth));
parent->DHTSendTo(peer.node.as_array(),
new FindIntroMessage(target, peer.txid));
}

void
Expand Down
2 changes: 1 addition & 1 deletion llarp/service/hidden_service_address_lookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace llarp
{
auto msg = std::make_shared< routing::DHTMessage >();
msg->M.emplace_back(std::make_unique< dht::FindIntroMessage >(
txid, location, 2, relayOrder));
txid, location, relayOrder));
return msg;
}

Expand Down
Loading