Skip to content

Commit

Permalink
Merge pull request #545 from michael-loki/fix_intro_set_rule_of_one
Browse files Browse the repository at this point in the history
Fix introset tests
  • Loading branch information
majestrate authored Apr 23, 2019
2 parents e36ddfb + 3a8715d commit c5c6473
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 72 deletions.
1 change: 1 addition & 0 deletions llarp/dht/taglookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace llarp
{
std::set< service::IntroSet > found(valuesFound.begin(),
valuesFound.end());

// collect our local values if we haven't hit a limit
if(found.size() < 2)
{
Expand Down
13 changes: 10 additions & 3 deletions llarp/service/intro_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace llarp

if(key == "w")
{
W = std::make_unique< PoW >();
W = absl::make_optional< PoW >();
return W->BDecode(buf);
}

Expand Down Expand Up @@ -151,7 +151,7 @@ namespace llarp
{
return false;
}
else if(W == nullptr)
else if(!W.has_value())
{
LogWarn("intro has too high expire time");
return false;
Expand Down Expand Up @@ -195,7 +195,14 @@ namespace llarp
}

printer.printAttribute("T", T);
printer.printAttribute("W", W.get());
if(W)
{
printer.printAttribute("W", W.value());
}
else
{
printer.printAttribute("W", "NULL");
}
printer.printAttribute("V", version);
printer.printAttribute("Z", Z);

Expand Down
90 changes: 23 additions & 67 deletions llarp/service/intro_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <util/time.hpp>
#include <util/status.hpp>

#include <absl/types/optional.h>
#include <algorithm>
#include <functional>
#include <iostream>
Expand All @@ -31,75 +32,9 @@ namespace llarp
PQPubKey K;
Tag topic;
llarp_time_t T = 0;
std::unique_ptr< PoW > W;
absl::optional< PoW > W;
Signature Z;

IntroSet() = default;

IntroSet(IntroSet&& other) = default;

IntroSet(const IntroSet& other)
: IBEncodeMessage(other.version)
, A(other.A)
, I(other.I)
, K(other.K)
, topic(other.topic)
, T(other.T)
, W(std::make_unique< PoW >(*other.W))
, Z(other.Z)
{
}

IntroSet&
operator=(const IntroSet& other)
{
I.clear();
A = other.A;
I = other.I;
K = other.K;
T = other.T;
version = other.version;
topic = other.topic;
W.reset();
if(other.W)
{
W = std::make_unique< PoW >(*other.W);
}
Z = other.Z;
return *this;
}

bool
operator<(const IntroSet& other) const
{
return A < other.A;
}

bool
operator==(const IntroSet& other) const
{
if(std::tie(A, I, K, T, version, topic, Z)
!= std::tie(other.A, other.I, other.K, other.T, other.version,
other.topic, other.Z))
{
return false;
}
else if(W && other.W) // both PoW have a valid ptr
{
return *W == *other.W;
}
else
{
return W == other.W; // if one is null, verify the other is null
}
}

bool
operator!=(const IntroSet& other) const
{
return !(*this == other);
}

bool
OtherIsNewer(const IntroSet& other) const
{
Expand Down Expand Up @@ -131,6 +66,27 @@ namespace llarp
ExtractStatus() const;
};

inline bool
operator<(const IntroSet& lhs, const IntroSet& rhs)
{
return lhs.A < rhs.A;
}

inline bool
operator==(const IntroSet& lhs, const IntroSet& rhs)
{
return std::tie(lhs.A, lhs.I, lhs.K, lhs.T, lhs.version, lhs.topic, lhs.W,
lhs.Z)
== std::tie(rhs.A, rhs.I, rhs.K, rhs.T, rhs.version, rhs.topic, rhs.W,
rhs.Z);
}

inline bool
operator!=(const IntroSet& lhs, const IntroSet& rhs)
{
return !(lhs == rhs);
}

inline std::ostream&
operator<<(std::ostream& out, const IntroSet& i)
{
Expand Down
8 changes: 6 additions & 2 deletions test/dht/test_llarp_dht_taglookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ TEST_F(TestDhtTagLookup, send_reply)
{
tagLookup.valuesFound.clear();
tagLookup.valuesFound.emplace_back();
tagLookup.valuesFound.back().T = 1;
tagLookup.valuesFound.back().T = 1;
tagLookup.valuesFound.back().A.vanity[0] = 1;
tagLookup.valuesFound.back().A.UpdateAddr();
tagLookup.valuesFound.emplace_back();
tagLookup.valuesFound.back().T = 2;
tagLookup.valuesFound.back().T = 2;
tagLookup.valuesFound.back().A.vanity[0] = 2;
tagLookup.valuesFound.back().A.UpdateAddr();
// clang-format off
EXPECT_CALL(context, FindRandomIntroSetsWithTagExcluding(_, _, _)).Times(0);

Expand Down

0 comments on commit c5c6473

Please sign in to comment.