Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritization system #10

Merged
merged 4 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions daemon/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ Config::Config(const string& config_file_address)

Config::Config(const asphr::json& config_json_input,
const string& config_file_address)
: saved_file_address(config_file_address), db_rows_(CLIENT_DB_ROWS) {
: saved_file_address(config_file_address),
db_rows_(CLIENT_DB_ROWS),
dummyMe("dummyMe", 0, "", "", 0, false, 0, 0, 0, true) {
auto config_json = config_json_input;
if (!config_json.contains("has_registered")) {
cout << "WARNING (invalid config file): config file does not contain "
Expand Down Expand Up @@ -112,7 +114,7 @@ Config::Config(const asphr::json& config_json_input,

for (auto& friend_json : config_json.at("friends")) {
Friend f = Friend::from_json(friend_json);
friendTable[f.name] = f;
friendTable.try_emplace(f.name, f);
}

data_dir = config_json.at("data_dir").get<string>();
Expand Down Expand Up @@ -194,14 +196,15 @@ auto Config::num_enabled_friends() -> int {
return num_enabled_friends;
}

auto Config::random_enabled_friend() -> asphr::StatusOr<Friend> {
auto Config::random_enabled_friend(const std::unordered_set<string>& excluded)
-> asphr::StatusOr<Friend> {
const std::lock_guard<std::mutex> l(config_mtx);

check_rep();

vector<Friend> enabled_friends;
for (auto& friend_pair : friendTable) {
if (friend_pair.second.enabled) {
if (friend_pair.second.enabled && !excluded.contains(friend_pair.first)) {
enabled_friends.push_back(friend_pair.second);
}
}
Expand Down Expand Up @@ -231,7 +234,7 @@ auto Config::add_friend(const Friend& f) -> void {
check_rep();

assert(!friendTable.contains(f.name));
friendTable[f.name] = f;
friendTable.try_emplace(f.name, f);

save();
check_rep();
Expand Down Expand Up @@ -287,7 +290,7 @@ auto Config::update_friend(const Friend& f) -> void {
check_rep();

assert(friendTable.contains(f.name));
friendTable[f.name] = f;
friendTable.insert_or_assign(f.name, f);
save();

check_rep();
Expand Down Expand Up @@ -355,6 +358,7 @@ auto Config::check_rep() const -> void {
assert(registrationInfo.allocation.size() > 0);

assert(dummyMe.name == "dummyMe");
assert(dummyMe.dummy);
assert(dummyMe.write_key.size() ==
crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
assert(dummyMe.read_key.size() ==
Expand Down Expand Up @@ -400,7 +404,7 @@ auto Config::initialize_dummy_me() -> void {
dummy_friend_keypair.first);

dummyMe = Friend("dummyMe", 0, dummy_read_write_keys.first,
dummy_read_write_keys.second, 0, false, 0, 0, 0);
dummy_read_write_keys.second, 0, false, 0, 0, 0, true);

save();
}
3 changes: 2 additions & 1 deletion daemon/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class Config {

auto has_space_for_friends() -> bool;
auto num_enabled_friends() -> int;
auto random_enabled_friend() -> asphr::StatusOr<Friend>;
auto random_enabled_friend(const std::unordered_set<string>& excluded)
-> asphr::StatusOr<Friend>;
auto dummy_me() -> Friend;

auto has_registered() -> bool;
Expand Down
10 changes: 10 additions & 0 deletions daemon/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ constexpr auto DEFAULT_SERVER_ADDRESS = "server1.anysphere.co:443";
// this commit hash will be automatically updated by gui/package.json.
constexpr auto RELEASE_COMMIT_HASH = "32622be70c454ef4fc64517444727226d4df983b";

// this is the number of friends that will be received from in each round
// (ideally, they can all be received in a single PIR request using batch PIR)
// it needs to be at least 2, or else we will get a liveness problem where we
// are sending to one person who is not responding, and then continually trying
// to read from that person while never receiving from anyone else.
// TODO: when batch PIR implemented, increase this to the batch PIR number
constexpr auto RECEIVE_FRIENDS_PER_ROUND = 2;
static_assert(RECEIVE_FRIENDS_PER_ROUND >= 2,
"RECEIVE_FRIENDS_PER_ROUND must be >= 2");

// this is copied from amazon-roots.pem
// ideally we would do some kind of compile-time read file operation here, but
// that seems to not exist
Expand Down
20 changes: 9 additions & 11 deletions daemon/friend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ auto Friend::to_json() -> asphr::json {
}

auto Friend::from_json(const asphr::json& j) -> Friend {
Friend f;
f.name = j.at("name").get<string>();
f.read_index = j.at("read_index").get<int>();
asphr::Base64Unescape(j.at("write_key").get<string>(), &f.write_key);
asphr::Base64Unescape(j.at("read_key").get<string>(), &f.read_key);
f.ack_index = j.at("ack_index").get<int>();
f.enabled = j.at("enabled").get<bool>();
f.latest_ack_id = j.at("latest_ack_id").get<uint32_t>();
f.latest_send_id = j.at("latest_send_id").get<uint32_t>();
f.last_receive_id = j.at("last_receive_id").get<uint32_t>();
f.check_rep();
string read_key;
string write_key;
asphr::Base64Unescape(j.at("read_key").get<string>(), &read_key);
asphr::Base64Unescape(j.at("write_key").get<string>(), &write_key);
Friend f(j.at("name").get<string>(), j.at("read_index").get<int>(), read_key,
write_key, j.at("ack_index").get<int>(), j.at("enabled").get<bool>(),
j.at("latest_ack_id").get<uint32_t>(),
j.at("latest_send_id").get<uint32_t>(),
j.at("last_receive_id").get<uint32_t>(), false);
return f;
}

Expand Down
11 changes: 7 additions & 4 deletions daemon/friend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

class Friend {
public:
Friend() = default;
Friend(const string& name, const vector<Friend>& friends)
: name(name),
read_index(0),
Expand All @@ -20,7 +19,8 @@ class Friend {
enabled(false),
latest_ack_id(0),
latest_send_id(0),
last_receive_id(0) {
last_receive_id(0),
dummy(false) {
auto rng = std::default_random_engine{};

auto all_ack_indexes_not_used = asphr::unordered_set<int>{};
Expand All @@ -45,7 +45,7 @@ class Friend {
Friend(const string& name, const int read_index, const string& read_key,
const string& write_key, const int ack_index, const bool enabled,
const uint32_t latest_ack_id, const uint32_t latest_send_id,
const uint32_t last_receive_id)
const uint32_t last_receive_id, bool dummy)
: name(name),
read_index(read_index),
read_key(read_key),
Expand All @@ -54,7 +54,8 @@ class Friend {
enabled(enabled),
latest_ack_id(latest_ack_id),
latest_send_id(latest_send_id),
last_receive_id(last_receive_id) {
last_receive_id(last_receive_id),
dummy(dummy) {
check_rep();
}

Expand Down Expand Up @@ -83,6 +84,8 @@ class Friend {
// have received all IDs up to and including this value. Note that this refers
// to ID in the sequence_number space, not the message ID space.
uint32_t last_receive_id;
// dummy is true if the friend is a dummy friend!
bool dummy;

auto to_json() -> asphr::json;
static auto from_json(const asphr::json& j) -> Friend;
Expand Down
8 changes: 5 additions & 3 deletions daemon/inbox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ auto Inbox::update_ack_from_friend(Config& config, pir_value_t& pir_acks,
auto Inbox::receive_message(FastPIRClient& client, Config& config,
const asphrserver::ReceiveMessageResponse& reply,
const Friend& friend_info_in, const Crypto& crypto,
string& previous_success_receive_friend)
string* previous_success_receive_friend)
-> std::optional<InboxMessage> {
check_rep();
Friend friend_info = friend_info_in;
Expand Down Expand Up @@ -167,8 +167,6 @@ auto Inbox::receive_message(FastPIRClient& client, Config& config,
<< decrypted.status() << endl;
save();
return std::nullopt;
} else {
previous_success_receive_friend = friend_info.name;
}
auto& message = decrypted.value();

Expand Down Expand Up @@ -214,6 +212,10 @@ auto Inbox::receive_message(FastPIRClient& client, Config& config,
return std::nullopt;
}

// we have received a new message (chunk) that we haven't received before!
// this is a success!
*previous_success_receive_friend = friend_info.name;

if (message.num_chunks() == 0) {
save();
return InboxMessage{message.msg(), friend_info.name, message.id()};
Expand Down
2 changes: 1 addition & 1 deletion daemon/inbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Inbox {
auto receive_message(FastPIRClient& client, Config& config,
const asphrserver::ReceiveMessageResponse& reply,
const Friend& friend_info_in, const Crypto& crypto,
string& previous_success_receive_friend)
string* previous_success_receive_friend)
-> std::optional<InboxMessage>;

private:
Expand Down
16 changes: 8 additions & 8 deletions daemon/outbox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#include "client_lib/client_lib.hpp"

auto MessageToSend::from_json(const asphr::json& json) -> MessageToSend {
MessageToSend message;
message.to = Friend::from_json(json.at("to"));
message.sequence_number = json.at("sequence_number").get<uint32_t>();
message.msg = json.at("msg").get<string>();
message.chunked = json.at("chunked").get<bool>();
message.num_chunks = json.at("num_chunks").get<uint32_t>();
message.chunks_start_id = json.at("chunks_start_id").get<uint32_t>();
message.full_message_id = json.at("full_message_id").get<string>();
auto message = MessageToSend{
.to = Friend::from_json(json.at("to")),
.sequence_number = json.at("sequence_number").get<uint32_t>(),
.msg = json.at("msg").get<string>(),
.chunked = json.at("chunked").get<bool>(),
.num_chunks = json.at("num_chunks").get<uint32_t>(),
.chunks_start_id = json.at("chunks_start_id").get<uint32_t>(),
.full_message_id = json.at("full_message_id").get<string>()};
return message;
}

Expand Down
Loading