Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/async_friending_schema' into asy…
Browse files Browse the repository at this point in the history
…nc_friending_schema
  • Loading branch information
Shengtong Zhang committed Jun 27, 2022
2 parents 68b3271 + 2a32e5c commit 3700ab7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
1 change: 1 addition & 0 deletions daemon/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ cc_library(
":db_lib",
"//client_lib",
"//daemon/crypto:crypto_lib",
"//daemon/identifier",
"@asphr//asphr:asphr_lib",
],
)
Expand Down
1 change: 1 addition & 0 deletions daemon/identifier/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ cc_library(
"identifier.hpp",
"wordlist.hpp",
],
visibility = ["//daemon:__subpackages__"],
deps = [
":base58",
"//daemon/crypto:crypto_lib",
Expand Down
62 changes: 38 additions & 24 deletions daemon/identifier/identifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ auto change_base(vector<int> values, std::function<int(int)> from_base,
std::function<int(int)> to_base) -> vector<int> {
// stored in reverse, to be reversed at the end
vector<int> 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);
}
Expand All @@ -97,9 +98,9 @@ auto change_base(vector<int> values, std::function<int(int)> from_base,

auto SyncIdentifier::from_story(string story)
-> asphr::StatusOr<SyncIdentifier> {
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<string> words;
for (auto word : absl::StrSplit(story, ' ')) {
Expand All @@ -115,7 +116,7 @@ auto SyncIdentifier::from_story(string story)
// get indices of words in word list
std::vector<int> 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);
Expand All @@ -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;
}
{
Expand All @@ -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;
}
{
Expand All @@ -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;
}
{
Expand All @@ -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;
}

Expand All @@ -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<unsigned char>(base256_indices.at(i));
}

Expand Down Expand Up @@ -222,21 +226,24 @@ auto SyncIdentifier::to_story() const -> string {

// convert to base 256
vector<int> 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<int> wordbase_indices = change_base(
base256_indices, [](int i) { return 256; },
[](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");
}
});

Expand All @@ -245,32 +252,39 @@ auto SyncIdentifier::to_story() const -> string {

// get words in wordlist
std::vector<string> 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<size_t>(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<size_t>(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<size_t>(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<size_t>(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 += " ";
}
Expand Down
20 changes: 10 additions & 10 deletions daemon/rpc/daemon_rpc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 3700ab7

Please sign in to comment.