From 3a8715d8e4d46473f48edf56fad18b757fc48a37 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 23 Apr 2019 10:25:03 +0100 Subject: [PATCH] Fix introset tests --- llarp/dht/taglookup.cpp | 1 + llarp/service/intro_set.cpp | 13 +++- llarp/service/intro_set.hpp | 90 +++++++-------------------- test/dht/test_llarp_dht_taglookup.cpp | 8 ++- 4 files changed, 40 insertions(+), 72 deletions(-) diff --git a/llarp/dht/taglookup.cpp b/llarp/dht/taglookup.cpp index af77600813..ccca855ea8 100644 --- a/llarp/dht/taglookup.cpp +++ b/llarp/dht/taglookup.cpp @@ -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) { diff --git a/llarp/service/intro_set.cpp b/llarp/service/intro_set.cpp index 977a4fbb75..7955d775da 100644 --- a/llarp/service/intro_set.cpp +++ b/llarp/service/intro_set.cpp @@ -43,7 +43,7 @@ namespace llarp if(key == "w") { - W = std::make_unique< PoW >(); + W = absl::make_optional< PoW >(); return W->BDecode(buf); } @@ -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; @@ -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); diff --git a/llarp/service/intro_set.hpp b/llarp/service/intro_set.hpp index ab679f78b2..e8a680850e 100644 --- a/llarp/service/intro_set.hpp +++ b/llarp/service/intro_set.hpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -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 { @@ -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) { diff --git a/test/dht/test_llarp_dht_taglookup.cpp b/test/dht/test_llarp_dht_taglookup.cpp index 1f19b8a4d6..0971d62c8a 100644 --- a/test/dht/test_llarp_dht_taglookup.cpp +++ b/test/dht/test_llarp_dht_taglookup.cpp @@ -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);