From 2a32e5c905ff854a73b87b36543854b49646bce7 Mon Sep 17 00:00:00 2001 From: Arvid Lunnemark Date: Mon, 27 Jun 2022 15:29:56 -0700 Subject: [PATCH] add identifier --- daemon/BUILD | 1 + daemon/identifier/BUILD | 1 + daemon/identifier/identifier.cc | 62 ++++++++++++++++++++------------- daemon/rpc/daemon_rpc.cc | 20 +++++------ 4 files changed, 50 insertions(+), 34 deletions(-) diff --git a/daemon/BUILD b/daemon/BUILD index 43503970..3e0616c0 100644 --- a/daemon/BUILD +++ b/daemon/BUILD @@ -69,6 +69,7 @@ cc_library( ":db_lib", "//client_lib", "//daemon/crypto:crypto_lib", + "//daemon/identifier", "@asphr//asphr:asphr_lib", ], ) diff --git a/daemon/identifier/BUILD b/daemon/identifier/BUILD index 2046394a..10315fe3 100644 --- a/daemon/identifier/BUILD +++ b/daemon/identifier/BUILD @@ -23,6 +23,7 @@ cc_library( "identifier.hpp", "wordlist.hpp", ], + visibility = ["//daemon:__subpackages__"], deps = [ ":base58", "//daemon/crypto:crypto_lib", diff --git a/daemon/identifier/identifier.cc b/daemon/identifier/identifier.cc index 39f8b0af..6d446c76 100644 --- a/daemon/identifier/identifier.cc +++ b/daemon/identifier/identifier.cc @@ -73,13 +73,14 @@ auto change_base(vector values, std::function from_base, std::function to_base) -> vector { // stored in reverse, to be reversed at the end vector result; - for (int i = 0; i < values.size(); i++) { + for (size_t i = 0; i < values.size(); i++) { // multiply everything by current base! int carry = values.at(i); - ASPHR_ASSERT(carry < from_base(values.size() - 1 - i)); + ASPHR_ASSERT(carry < from_base(std::ssize(values) - 1 - i)); int j = 0; - for (j = 0; j < result.size(); j++) { - int new_value = result.at(j) * from_base(values.size() - 1 - i) + carry; + for (j = 0; j < std::ssize(result); j++) { + int new_value = + result.at(j) * from_base(std::ssize(values) - 1 - i) + carry; result.at(j) = new_value % to_base(j); carry = new_value / to_base(j); } @@ -97,9 +98,9 @@ auto change_base(vector values, std::function from_base, auto SyncIdentifier::from_story(string story) -> asphr::StatusOr { - ASPHR_ASSERT(wordlist::nouns_vec.size() > 1000); - ASPHR_ASSERT(wordlist::verbs_vec.size() > 500); - ASPHR_ASSERT(wordlist::adjectives_vec.size() > 400); + ASPHR_ASSERT(std::ssize(wordlist::nouns_vec) > 1000); + ASPHR_ASSERT(std::ssize(wordlist::verbs_vec) > 500); + ASPHR_ASSERT(std::ssize(wordlist::adjectives_vec) > 400); // create a vector of words std::vector words; for (auto word : absl::StrSplit(story, ' ')) { @@ -115,7 +116,7 @@ auto SyncIdentifier::from_story(string story) // get indices of words in word list std::vector indices; int j = 0; - while (j < words.size()) { + while (j < std::ssize(words)) { ASPHR_ASSERT_EQ(j % 4, 0); { auto word = words.at(j + 0); @@ -126,7 +127,7 @@ auto SyncIdentifier::from_story(string story) } indices.push_back(it - wordlist::adjectives_vec.begin()); } - if (j + 1 >= words.size()) { + if (j + 1 >= std::ssize(words)) { break; } { @@ -138,7 +139,7 @@ auto SyncIdentifier::from_story(string story) } indices.push_back(it - wordlist::nouns_vec.begin()); } - if (j + 2 >= words.size()) { + if (j + 2 >= std::ssize(words)) { break; } { @@ -150,7 +151,7 @@ auto SyncIdentifier::from_story(string story) } indices.push_back(it - wordlist::verbs_vec.begin()); } - if (j + 3 >= words.size()) { + if (j + 3 >= std::ssize(words)) { break; } { @@ -169,7 +170,7 @@ auto SyncIdentifier::from_story(string story) std::reverse(indices.begin(), indices.end()); // increment all indices by 1 so as to not have any leading zeros - for (int i = 0; i < indices.size(); i++) { + for (size_t i = 0; i < indices.size(); i++) { indices.at(i) += 1; } @@ -186,13 +187,16 @@ auto SyncIdentifier::from_story(string story) return wordlist::verbs_vec.size() + 1; case 3: return wordlist::nouns_vec.size() + 1; + default: + ASPHR_LOG_ERR("unexpected case", i, i, i_mod_4, i % 4); + ASPHR_ASSERT_MSG(false, "should never ever happen"); } }, [](int i) { return 256; }); // convert to bytes string raw_bytes = ""; - for (int i = 0; i < base256_indices.size(); i++) { + for (int i = 0; i < std::ssize(base256_indices); i++) { raw_bytes += static_cast(base256_indices.at(i)); } @@ -222,7 +226,7 @@ auto SyncIdentifier::to_story() const -> string { // convert to base 256 vector base256_indices; - for (int i = 0; i < raw_bytes.size(); i++) { + for (int i = 0; i < std::ssize(raw_bytes); i++) { base256_indices.push_back((uint8_t)raw_bytes.at(i)); } std::vector wordbase_indices = change_base( @@ -230,13 +234,16 @@ auto SyncIdentifier::to_story() const -> string { [](int i) { switch (i % 4) { case 0: - return wordlist::adjectives_vec.size() + 1; + return std::ssize(wordlist::adjectives_vec) + 1; case 1: - return wordlist::nouns_vec.size() + 1; + return std::ssize(wordlist::nouns_vec) + 1; case 2: - return wordlist::verbs_vec.size() + 1; + return std::ssize(wordlist::verbs_vec) + 1; case 3: - return wordlist::nouns_vec.size() + 1; + return std::ssize(wordlist::nouns_vec) + 1; + default: + ASPHR_LOG_ERR("unexpected case", i, i, i_mod_4, i % 4); + ASPHR_ASSERT_MSG(false, "should never ever happen"); } }); @@ -245,32 +252,39 @@ auto SyncIdentifier::to_story() const -> string { // get words in wordlist std::vector words; - for (int i = 0; i < wordbase_indices.size(); i++) { + for (int i = 0; i < std::ssize(wordbase_indices); i++) { ASPHR_ASSERT(wordbase_indices.at(i) > 0); switch (i % 4) { case 0: - ASPHR_ASSERT(wordbase_indices.at(i) <= wordlist::adjectives_vec.size()); + ASPHR_ASSERT(static_cast(wordbase_indices.at(i)) <= + wordlist::adjectives_vec.size()); words.push_back( wordlist::adjectives_vec.at(wordbase_indices.at(i) - 1)); break; case 1: - ASPHR_ASSERT(wordbase_indices.at(i) <= wordlist::nouns_vec.size()); + ASPHR_ASSERT(static_cast(wordbase_indices.at(i)) <= + wordlist::nouns_vec.size()); words.push_back(wordlist::nouns_vec.at(wordbase_indices.at(i) - 1)); break; case 2: - ASPHR_ASSERT(wordbase_indices.at(i) <= wordlist::verbs_vec.size()); + ASPHR_ASSERT(static_cast(wordbase_indices.at(i)) <= + wordlist::verbs_vec.size()); words.push_back(wordlist::verbs_vec.at(wordbase_indices.at(i) - 1)); break; case 3: - ASPHR_ASSERT(wordbase_indices.at(i) <= wordlist::nouns_vec.size()); + ASPHR_ASSERT(static_cast(wordbase_indices.at(i)) <= + wordlist::nouns_vec.size()); words.push_back(wordlist::nouns_vec.at(wordbase_indices.at(i) - 1)); break; + default: + ASPHR_LOG_ERR("unexpected case", i, i, i_mod_4, i % 4); + ASPHR_ASSERT_MSG(false, "should never ever happen"); } } // join words string story = ""; - for (int i = 0; i < words.size(); i++) { + for (int i = 0; i < std::ssize(words); i++) { if (i != 0) { story += " "; } diff --git a/daemon/rpc/daemon_rpc.cc b/daemon/rpc/daemon_rpc.cc index 8e80dcf2..03c01c2c 100644 --- a/daemon/rpc/daemon_rpc.cc +++ b/daemon/rpc/daemon_rpc.cc @@ -5,7 +5,7 @@ #include "daemon_rpc.hpp" -// #include "daemon/identifier/identifier.hpp" +#include "daemon/identifier/identifier.hpp" #include "google/protobuf/util/time_util.h" using grpc::ServerContext; @@ -251,15 +251,15 @@ Status DaemonRpc::AddSyncFriend( return Status(grpc::StatusCode::UNAUTHENTICATED, "not registered"); } - // auto sync_id_maybe = - // SyncIdentifier::from_story(addSyncFriendRequest->story()); - // if (!sync_id_maybe.ok()) { - // ASPHR_LOG_ERR("Failed to parse sync ID.", rpc_call, "AddSyncFriend", - // error_message, sync_id_maybe.status().message(), - // error_code, sync_id_maybe.status().code()); - // return Status(grpc::StatusCode::INVALID_ARGUMENT, "invalid story"); - // } - // auto sync_id = sync_id_maybe.value(); + auto sync_id_maybe = + SyncIdentifier::from_story(addSyncFriendRequest->story()); + if (!sync_id_maybe.ok()) { + ASPHR_LOG_ERR("Failed to parse sync ID.", rpc_call, "AddSyncFriend", + error_message, sync_id_maybe.status().message(), error_code, + sync_id_maybe.status().code()); + return Status(grpc::StatusCode::INVALID_ARGUMENT, "invalid story"); + } + auto sync_id = sync_id_maybe.value(); // TODO: not implemented return Status(grpc::StatusCode::UNIMPLEMENTED, "not implemented");