Skip to content

Commit

Permalink
check point. Most of the code compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Shengtong Zhang committed Jun 25, 2022
1 parent da589ea commit e5d3588
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 61 deletions.
61 changes: 29 additions & 32 deletions daemon/rpc/daemon_rpc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ Status DaemonRpc::GetPublicID(
auto kx_public_key = rust_u8Vec_to_string(registration_info.kx_public_key);

// generate the public ID
auto public_id_ = crypto::generate_public_id(
name, allocation, kx_public_key, friend_request_public_key);
auto public_id_ = crypto::generate_user_id(name, allocation, kx_public_key,
friend_request_public_key);

if (!public_id_.ok()) {
ASPHR_LOG_ERR("Failed to generate public ID.", rpc_call, "GetPublicID");
Expand All @@ -171,6 +171,7 @@ Status DaemonRpc::GetPublicID(
ASPHR_LOG_ERR("Database failed.", error, e.what(), rpc_call, "GetPublicID");
return Status(grpc::StatusCode::UNKNOWN, e.what());
}
return Status::OK;
}

// ---------------------------------------
Expand Down Expand Up @@ -204,42 +205,34 @@ auto DaemonRpc::convertStructRPCtoDB(asphrdaemon::FriendInfo& friend_info,
friend_request_public_key] = friend_public_id_.value();

// construct friend request
return std::pair(db::FriendFragment{
unique_name : friend_info.unique_name(),
display_name : friend_info.display_name(),
progress : progress,
deleted : false,
},
db::AddAddress{
unique_name : friend_info.unique_name(),
friend_request_public_key :
string_to_rust_u8Vec(friend_request_public_key),
kx_public_key : string_to_rust_u8Vec(friend_kx_public_key),
friend_request_message : message,
read_index : friend_allocation,
read_key : string_to_rust_u8Vec(read_key),
write_key : string_to_rust_u8Vec(write_key),
});
return std::pair(
db::FriendFragment{
.unique_name = friend_info.unique_name(),
.display_name = friend_info.display_name(),
.public_id = friend_info.public_id(),
.progress = progress,
.deleted = false,
},
db::AddAddress{
.unique_name = friend_info.unique_name(),
.friend_request_public_key =
string_to_rust_u8Vec(friend_request_public_key),
.friend_request_message = message,
.kx_public_key = string_to_rust_u8Vec(friend_kx_public_key),
.read_index = friend_allocation,
.read_key = string_to_rust_u8Vec(read_key),
.write_key = string_to_rust_u8Vec(write_key),
});
}

// helper method to convert from DB structs to RPC structs
auto DaemonRpc::convertStructDBtoRPC(const db::Friend& db_friend,
const db::Address& db_address)
-> asphr::StatusOr<std::pair<asphrdaemon::FriendInfo, string>> {
auto public_id_ = crypto::generate_user_id(
std::string(db_friend.unique_name), db_address.read_index,
rust_u8Vec_to_string(db_address.kx_public_key),
rust_u8Vec_to_string(db_address.friend_request_public_key));
if (!public_id_.ok()) {
ASPHR_LOG_ERR("Failed to encode public ID.", rpc_call,
"SendAsyncFriendRequest");
return absl::InvalidArgumentError("friend struct has invalid fields");
}

asphrdaemon::FriendInfo friend_info;
friend_info.set_unique_name(db_friend.unique_name);
friend_info.set_display_name(db_friend.display_name);
friend_info.set_public_id(public_id_.value());
friend_info.set_unique_name(std::string(db_friend.unique_name));
friend_info.set_display_name(std::string(db_friend.display_name));
friend_info.set_public_id(std::string(db_friend.public_id));
friend_info.set_progress(db_friend.progress);
return std::pair(friend_info, std::string(db_address.friend_request_message));
}
Expand Down Expand Up @@ -297,6 +290,7 @@ Status DaemonRpc::SendAsyncFriendRequest(
"AddFriend");
return Status(grpc::StatusCode::UNKNOWN, e.what());
}
return Status::OK;
}

Status DaemonRpc::GetOutgoingAsyncFriendRequests(
Expand Down Expand Up @@ -329,6 +323,7 @@ Status DaemonRpc::GetOutgoingAsyncFriendRequests(
rpc_call, "GetOutgoingAsyncFriendRequests");
return Status(grpc::StatusCode::UNKNOWN, e.what());
}
return Status::OK;
}

Status DaemonRpc::GetIncomingAsyncFriendRequests(
Expand Down Expand Up @@ -362,6 +357,7 @@ Status DaemonRpc::GetIncomingAsyncFriendRequests(
rpc_call, "GetIncomingAsyncFriendRequests");
return Status(grpc::StatusCode::UNKNOWN, e.what());
}
return Status::OK;
}

Status DaemonRpc::DecideAsyncFriendRequest(
Expand All @@ -374,7 +370,7 @@ Status DaemonRpc::DecideAsyncFriendRequest(
if (decideAsyncFriendRequestRequest->accept()) {
// call rust db to accept the friend request
G.db->approve_async_friend_request(
decideAsyncFriendRequestRequest->unique_name());
decideAsyncFriendRequestRequest->unique_name(), MAX_FRIENDS);
} else {
// call rust db to reject the friend request
G.db->deny_async_friend_request(
Expand All @@ -385,6 +381,7 @@ Status DaemonRpc::DecideAsyncFriendRequest(
rpc_call, "DecideAsyncFriendRequest");
return Status(grpc::StatusCode::UNKNOWN, e.what());
}
return Status::OK;
}

Status DaemonRpc::RemoveFriend(
Expand Down
12 changes: 6 additions & 6 deletions daemon/rpc/daemon_rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,17 @@ class DaemonRpc final : public asphrdaemon::Daemon::Service {
// The RPC speaks in the FriendInfo Struct
// The DB speaks in the Friend and Address Structs
// we provide a way to translate between them
auto DaemonRpc::convertStructRPCtoDB(asphrdaemon::FriendInfo& friend_info,
string message, int progress,
string read_key, string write_key)
auto convertStructRPCtoDB(asphrdaemon::FriendInfo& friend_info,
string message, int progress, string read_key,
string write_key)
-> asphr::StatusOr<std::pair<db::FriendFragment, db::AddAddress>>;

auto DaemonRpc::convertStructDBtoRPC(const db::Friend& db_friend,
const db::Address& db_address)
auto convertStructDBtoRPC(const db::Friend& db_friend,
const db::Address& db_address)
-> asphr::StatusOr<std::pair<asphrdaemon::FriendInfo, string>>;

// constant for the progress field
const int INCOMING_REQUEST = 0;
const int OUTGOING_REQUEST = 1;
const int ACTUAL_FRIEND = 2;
}
};
59 changes: 36 additions & 23 deletions daemon/transmitter/transmitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

auto generate_dummy_address(const Global& G, const db::Registration& reg)
-> db::Address {
auto dummy_friend_keypair = crypto::generate_keypair();
auto dummy_friend_keypair = crypto::generate_kx_keypair();

// convert reg.kx_public_key, kx_private_key to a string

auto kx_public_key_str = rust_u8Vec_to_string(reg.kx_public_key);
auto kx_private_key_str = rust_u8Vec_to_string(reg.kx_private_key);

Expand All @@ -24,11 +25,18 @@ auto generate_dummy_address(const Global& G, const db::Registration& reg)
auto read_key_vec_rust = string_to_rust_u8Vec(dummy_read_write_keys.first);
auto write_key_vec_rust = string_to_rust_u8Vec(dummy_read_write_keys.second);

return (db::Address){-1, 0, 0, read_key_vec_rust, write_key_vec_rust};
return (db::Address){-1,
reg.friend_request_public_key,
"this is a dummy",
reg.kx_public_key,
0,
0,
read_key_vec_rust,
write_key_vec_rust};
}

Transmitter::Transmitter(Global& G, shared_ptr<asphrserver::Server::Stub> stub)
: G(G), stub(stub) {
: G(G), stub(stub), next_async_friend_request_retrieve_index(0) {
check_rep();
}

Expand Down Expand Up @@ -311,7 +319,20 @@ auto Transmitter::retrieve() -> void {
pir_replies.at(i).status().message());
}
}

// Crawl the async friend request database
// TODO: we could accelerate / deccelerate this as the client desires/
// by changing ASYNC_FRIEND_REQUEST_BATCH_SIZE
int start_index = next_async_friend_request_retrieve_index;
int end_index = std::min(next_async_friend_request_retrieve_index +
ASYNC_FRIEND_REQUEST_BATCH_SIZE,
CLIENT_DB_ROWS);
// call the server to retrieve the async friend requests
retrieve_async_friend_request(start_index, end_index);
if (end_index == CLIENT_DB_ROWS) {
next_async_friend_request_retrieve_index = 0;
} else {
next_async_friend_request_retrieve_index = end_index;
}
check_rep();
}

Expand Down Expand Up @@ -414,6 +435,7 @@ auto Transmitter::send() -> void {
server_status_code, status.error_code(),
server_status_message, status.error_message());
}
transmit_async_friend_request();
check_rep();
}

Expand Down Expand Up @@ -481,23 +503,13 @@ auto Transmitter::transmit_async_friend_request() -> void {
string my_id; // we could probably cache this in DB, but we don't need to
string my_friend_request_private_key;
string friend_id;
db::Registration reg_info;
try {
auto reg_info = G.db->get_registration();
auto my_id_ = crypto::generate_user_id(
"", reg_info.allocation, rust_u8Vec_to_string(reg_info.kx_public_key),
rust_u8Vec_to_string(reg_info.friend_request_public_key));
if (!my_id_.ok()) {
ASPHR_LOG_ERR("Could not generate user ID.", error_msg,
my_id_.status().message());
return;
}
my_id = my_id_.value();
reg_info = G.db->get_registration();
auto my_id = reg_info.public_id;
my_friend_request_private_key =
rust_u8Vec_to_string(reg_info.friend_request_private_key);
auto friend_id_ = crypto::generate_user_id(
"", async_friend_address.read_index,
rust_u8Vec_to_string(async_friend_address.kx_public_key),
rust_u8Vec_to_string(async_friend_address.friend_request_public_key));
auto friend_id_ = async_friend.public_id;
} catch (const rust::Error& e) {
ASPHR_LOG_ERR("Could not generate user ID.", error_msg, e.what());
return;
Expand All @@ -515,9 +527,8 @@ auto Transmitter::transmit_async_friend_request() -> void {
auto encrypted_friend_request = encrypted_friend_request_status_.value();
// Send to server
asphrserver::AddFriendAsyncInfo request;
request.set_index(config->registration_info().allocation.at(0));
request.set_authentication_token(
config->registration_info().authentication_token);
request.set_index(reg_info.allocation);
request.set_authentication_token(std::string(reg_info.authentication_token));
request.set_request(encrypted_friend_request);
asphrserver::AddFriendAsyncResponse reply;
grpc::ClientContext context;
Expand All @@ -534,8 +545,10 @@ auto Transmitter::transmit_async_friend_request() -> void {
// Retrieves async friend requests from the server
// Returns a map of friend public key to friend info, aimed at the client
auto Transmitter::retrieve_async_friend_request(int start_index, int end_index)
-> asphr::StatusOr<std::map<string, Friend>> {
-> void {
ASPHR_LOG_INFO("Not implemented.");
// check input
/**
if (start_index < 0 || end_index < 0 || start_index > end_index ||
end_index - start_index > ASYNC_FRIEND_REQUEST_BATCH_SIZE) {
return absl::InvalidArgumentError("Invalid indices");
Expand Down Expand Up @@ -615,7 +628,7 @@ auto Transmitter::retrieve_async_friend_request(int start_index, int end_index)
// add the friend to the map
friends.insert({friend_public_key, friend_instance});
}
return friends;
**/
}

auto Transmitter::check_rep() const noexcept -> void {
Expand Down
3 changes: 3 additions & 0 deletions daemon/transmitter/transmitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class Transmitter {
optional<int> previous_success_receive_friend;
optional<int> just_acked_friend;

// used to crawl the db
int next_async_friend_request_retrieve_index;

// for each index, get the PIR response for that index
auto batch_retrieve_pir(FastPIRClient& client, vector<pir_index_t> indices)
-> vector<asphr::StatusOr<asphrserver::ReceiveMessageResponse>>;
Expand Down

0 comments on commit e5d3588

Please sign in to comment.