Skip to content

Commit

Permalink
add public_id to database
Browse files Browse the repository at this point in the history
  • Loading branch information
Shengtong Zhang committed Jun 25, 2022
1 parent 55155d6 commit da589ea
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions daemon/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ constexpr size_t ENCRYPTED_ACKING_BYTES =
constexpr size_t MAX_FRIENDS = MESSAGE_SIZE / ENCRYPTED_ACKING_BYTES;

constexpr size_t MAX_ASYNC_FRIEND_REQUESTS = 500;
constexpr size_t ASYNC_FRIEND_REQUEST_BATCH_SIZE = 1000;

// NOTE: whenever these default values are changed, please make a database
// migration in the shape of UPDATE config SET value = 'new_value' WHERE value =
Expand Down
15 changes: 11 additions & 4 deletions daemon/db/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub mod ffi {
//
// NEVER EVER CHANGE THE ORDER OF THE FIELDS HERE WITHOUT LOOKING AT ALL QUERIES WHERE
// THEY ARE USED. @code_review
//
//
// TODO: try to write a macro for enforcing this in code.
//

Expand All @@ -137,6 +137,7 @@ pub mod ffi {
pub uid: i32,
pub unique_name: String,
pub display_name: String,
pub public_id: String,
pub progress: i32,
pub deleted: bool,
}
Expand All @@ -146,6 +147,7 @@ pub mod ffi {
struct FriendFragment {
pub unique_name: String,
pub display_name: String,
pub public_id: String,
pub progress: i32,
pub deleted: bool,
}
Expand All @@ -155,8 +157,8 @@ pub mod ffi {
struct Address {
pub uid: i32,
pub friend_request_public_key: Vec<u8>,
pub kx_public_key: Vec<u8>,
pub friend_request_message: String,
pub kx_public_key: Vec<u8>,
pub read_index: i32,
pub ack_index: i32,
pub read_key: Vec<u8>,
Expand All @@ -165,8 +167,8 @@ pub mod ffi {
struct AddAddress {
pub unique_name: String,
pub friend_request_public_key: Vec<u8>,
pub kx_public_key: Vec<u8>,
pub friend_request_message: String,
pub kx_public_key: Vec<u8>,
pub read_index: i32,
pub read_key: Vec<u8>,
pub write_key: Vec<u8>,
Expand All @@ -191,6 +193,7 @@ pub mod ffi {
pub pir_secret_key: Vec<u8>,
pub pir_galois_key: Vec<u8>,
pub authentication_token: String,
pub public_id: String,
}
#[derive(Insertable)]
#[diesel(table_name = crate::schema::registration)]
Expand All @@ -203,6 +206,7 @@ pub mod ffi {
pub pir_secret_key: Vec<u8>,
pub pir_galois_key: Vec<u8>,
pub authentication_token: String,
pub public_id: String
}
#[derive(Queryable)]
struct SendInfo {
Expand Down Expand Up @@ -368,6 +372,7 @@ pub mod ffi {
&self,
unique_name: &str,
display_name: &str,
public_key: &str,
max_friends: i32,
) -> Result<Friend>;
// adds a friend address and also makes the friend enabled
Expand Down Expand Up @@ -677,6 +682,7 @@ impl DB {
&self,
unique_name: &str,
display_name: &str,
public_id: &str,
max_friends: i32,
) -> Result<ffi::Friend, DbError> {
let mut conn = self.connect()?;
Expand All @@ -685,6 +691,7 @@ impl DB {
let f = ffi::FriendFragment {
unique_name: unique_name.to_string(),
display_name: display_name.to_string(),
public_id: public_id.to_string(),
progress: ACTUAL_FRIEND,
deleted: false,
};
Expand Down Expand Up @@ -1509,7 +1516,7 @@ impl DB {
use crate::schema::status;

let r = conn.transaction::<_, diesel::result::Error, _>(|conn_b| {
// IMPORTANT TODO: what if the friend already exists?
// IMPORTANT TODO: what if the friend already exists, but has been deleted?
// We can either recycle the old entry, or create a new entry.
// We choose the latter, which also erases the message history.
// insert friend and address into database
Expand Down
2 changes: 2 additions & 0 deletions daemon/db/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ diesel::table! {
uid -> Integer,
unique_name -> Text,
display_name -> Text,
public_id -> Text,
progress -> Integer,
deleted -> Bool,
}
Expand Down Expand Up @@ -90,6 +91,7 @@ diesel::table! {
pir_secret_key -> Binary,
pir_galois_key -> Binary,
authentication_token -> Text,
public_id -> Text,
}
}

Expand Down
10 changes: 8 additions & 2 deletions daemon/db/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn get_registration_fragment() -> ffi::RegistrationFragment {
let pir_secret_key: Vec<u8> = br#""hi hi"#.to_vec();
let pir_galois_key: Vec<u8> = br#""hi hi hi"#.to_vec();
let authentication_token: String = "X6H3ILWIrDGThjbi4IpYfWGtJ3YWdMIf".to_string();
let public_id: String = "wwww".to_string();

ffi::RegistrationFragment {
friend_request_public_key,
Expand All @@ -22,6 +23,7 @@ fn get_registration_fragment() -> ffi::RegistrationFragment {
pir_secret_key,
pir_galois_key,
authentication_token,
public_id
}
}

Expand Down Expand Up @@ -87,6 +89,7 @@ fn test_register() {
assert_eq!(registration.pir_secret_key, config_clone.pir_secret_key);
assert_eq!(registration.pir_galois_key, config_clone.pir_galois_key);
assert_eq!(registration.authentication_token, config_clone.authentication_token);
assert_eq!(registration.public_id, config_clone.public_id);
}
Err(_) => {
panic!("Failed to get registration");
Expand All @@ -106,7 +109,7 @@ fn test_receive_msg() {
let config_data = get_registration_fragment();
db.do_register(config_data).unwrap();

let f = db.create_friend("friend_1", "Friend 1", 20).unwrap();
let f = db.create_friend("friend_1", "Friend 1", "tttt", 20).unwrap();
db.add_friend_address(
ffi::AddAddress {
unique_name: "friend_1".to_string(),
Expand Down Expand Up @@ -175,7 +178,7 @@ fn test_send_msg() {
let config_data = get_registration_fragment();
db.do_register(config_data).unwrap();

let f = db.create_friend("friend_1", "Friend 1", 20).unwrap();
let f = db.create_friend("friend_1", "Friend 1", "tttt", 20).unwrap();
db.add_friend_address(
ffi::AddAddress {
unique_name: "friend_1".to_string(),
Expand Down Expand Up @@ -222,6 +225,7 @@ fn test_async_add_friend() {
let friend_request = ffi::FriendFragment {
unique_name: friend_name.to_string(),
display_name: "lyrica".to_string(),
public_id: "tttt".to_string(),
progress: INCOMING_REQUEST,
deleted: false,
};
Expand All @@ -240,6 +244,7 @@ fn test_async_add_friend() {
// check that we have a friend request
assert_eq!(friend_requests.len(), 1);
assert_eq!(friend_requests[0].unique_name, friend_name);
assert_eq!(friend_requests[0].public_id, "tttt");

// this uid now identifies the friend
let uid = friend_requests[0].uid;
Expand All @@ -259,6 +264,7 @@ fn test_async_add_friend() {
let friends = db.get_friends().unwrap();
assert_eq!(friends.len(), 1);
assert_eq!(friends[0].uid, uid);
assert_eq!(friends[0].public_id, "tttt");
assert_eq!(friends[0].unique_name, "friend_1");
// check the friend address
let new_address = db.get_friend_address(uid).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ CREATE TABLE registration (
allocation integer NOT NULL,
pir_secret_key blob NOT NULL,
pir_galois_key blob NOT NULL,
authentication_token text NOT NULL
authentication_token text NOT NULL,
public_id text NOT NULL -- redundant, but as this is used so prevalent, we'll keep it.
);

-- never delete a friend! instead, set `deleted` to true, or else we will lose history!
Expand All @@ -32,6 +33,7 @@ CREATE TABLE friend (
uid integer PRIMARY KEY NOT NULL,
unique_name text UNIQUE NOT NULL,
display_name text NOT NULL,
public_id text NOT NULL, -- redundant, but as this is used so prevalent, we'll keep it.
progress integer NOT NULL, -- 0-2. 0 = outgoing request, 1 = incoming request, 2 = actual friend
deleted boolean NOT NULL
);
Expand Down
18 changes: 16 additions & 2 deletions daemon/rpc/daemon_rpc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,17 @@ Status DaemonRpc::RegisterUser(
rpc_call, "RegisterUser");
return Status(grpc::StatusCode::UNKNOWN, "allocation is empty");
}

//-------------------------------------------------------------------------
// server side finished registration
// update DB now
// compute the public id here
auto public_id_ = crypto::generate_user_id(
"", allocation.at(0), kx_public_key, friend_request_public_key);
if (!public_id_.ok()) {
ASPHR_LOG_ERR("Register failed: public id generation failed.", rpc_call,
"RegisterUser");
return Status(grpc::StatusCode::UNKNOWN, "public id generation failed");
}
try {
G.db->do_register(db::RegistrationFragment{
.friend_request_public_key =
Expand All @@ -79,6 +89,7 @@ Status DaemonRpc::RegisterUser(
.pir_secret_key = string_to_rust_u8Vec(pir_secret_key),
.pir_galois_key = string_to_rust_u8Vec(pir_galois_keys),
.authentication_token = authentication_token,
.public_id = public_id_.value(),
});
} catch (const rust::Error& e) {
ASPHR_LOG_ERR("Register failed in database.", error, e.what(), rpc_call,
Expand Down Expand Up @@ -110,9 +121,12 @@ Status DaemonRpc::GetFriendList(
try {
for (auto& s : G.db->get_friends()) {
auto new_friend = getFriendListResponse->add_friend_infos();
// we need public id here
// so we need to query the address DB as well
new_friend->set_unique_name(std::string(s.unique_name));
new_friend->set_display_name(std::string(s.display_name));
new_friend->set_enabled(s.enabled);
new_friend->set_public_id(std::string(s.public_id));
new_friend->set_progress(ACTUAL_FRIEND);
}
} catch (const rust::Error& e) {
ASPHR_LOG_ERR("Database failed.", error, e.what(), rpc_call,
Expand Down
8 changes: 8 additions & 0 deletions daemon/transmitter/transmitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ class Transmitter {

// retrieve and process async friend request from the server
// and push them to the daemon
// It is important to define the behavior of this function in the case of
// duplicate requests. i.e. when a friend (request) with the same public key
// is already in the database. Here's the definition for now.
// 1. If the friend is marked as deleted, then we ignore the request.
// 2. If the friend is marked as accepted, then we ignore the request.
// 3. If the friend is marked as incoming, then we ignore the request.
// 4. If the friend is marked as outgoing, then we approve this request
// immediately.
auto retrieve_async_friend_request(int start_index, int end_index) -> void;

auto check_rep() const noexcept -> void;
Expand Down

0 comments on commit da589ea

Please sign in to comment.