Skip to content

Commit

Permalink
Rita client registration crate into exit trust root crate
Browse files Browse the repository at this point in the history
  • Loading branch information
ch-iara authored and jkilpatr committed Oct 18, 2024
1 parent 78a3014 commit be1a1ce
Show file tree
Hide file tree
Showing 25 changed files with 93 additions and 119 deletions.
31 changes: 6 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ members = [
"rita_common",
"rita_exit",
"rita_client",
"rita_client_registration",
"rita_db_migration",
"rita_bin",
"test_runner",
Expand Down
6 changes: 4 additions & 2 deletions althea_types/src/exits/server_list_signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ impl ExitServerList {
match sig.recover(&hash) {
Ok(addr) => {
println!("Recovered address is {:?}", addr);
addr == key},
addr == key
}
Err(_) => {
println!("Failed to recover address from signature");
false},
false
}
}
} else {
println!("Signature is invalid");
Expand Down
5 changes: 4 additions & 1 deletion exit_trust_root/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ clarity = "1.4"
web30 = "1.4"
crypto_box = "0.9"
lazy_static = "1.5"
phonenumber = "0.3.6"
awc = "3.5"
actix = "0.13"
tokio = { version = "1.40", features = ["macros", "time"] }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
toml = "0.5"
rita_client_registration = { path = "../rita_client_registration" }
# we don't call or us OpenSSL directly in this codebase, but by adding
# this dependency with this feature we can enforce that openssl is compiled
# in 'vendored' mode all the way down the tree. What this means is that we use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! exit and client routers can read it to coordinate user setup and two way key exchange with the blockchain
//! as the trusted party
use crate::convert_althea_types_to_web3_error;
use althea_types::{ExitIdentity, Identity, WgKey};
use clarity::{
abi::{encode_call, AbiToken},
Expand All @@ -17,10 +16,12 @@ use web30::{
types::{SendTxOption, TransactionRequest},
};

use crate::rita_client_registration::convert_althea_types_to_web3_error;

/// The EVM integer size
pub const WORD_SIZE: usize = 32;

pub async fn get_all_regsitered_clients(
pub async fn get_all_registered_clients(
web30: &Web3,
requester_address: Address,
contract: Address,
Expand Down
7 changes: 3 additions & 4 deletions exit_trust_root/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::{fs::File, io::Read};
use lazy_static::lazy_static;
use althea_types::WgKey;
use clarity::PrivateKey;
use lazy_static::lazy_static;
use log::error;
use serde::{Deserialize, Serialize};
use std::{fs::File, io::Read};

use crate::DEVELOPMENT;


///Struct containing settings for Exit root server
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ConfigStruct {
Expand Down Expand Up @@ -43,7 +42,7 @@ pub fn load_config() -> ConfigStruct {
return ConfigStruct {
clarity_private_key: PrivateKey::from_bytes([1u8; 32]).unwrap(),
wg_private_key: WgKey::from([2; 32]),
}
};
} else {
"/etc/exit_root_server.toml"
};
Expand Down
30 changes: 23 additions & 7 deletions exit_trust_root/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use actix_web::rt::System;
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
use althea_types::{ExitServerList, SignedExitServerList};
use clarity::Address;
use client_db::get_exits_list;
use config::CONFIG;
use log::info;
use rita_client_registration::client_db::get_exits_list;
use rustls::ServerConfig;
use std::collections::HashMap;
use std::net::{IpAddr, Ipv4Addr};
Expand All @@ -15,7 +15,10 @@ use tls::{load_certs, load_rustls_private_key};
use web30::client::Web3;
use web30::jsonrpc::error::Web3Error;

pub mod client_db;
pub mod config;
pub mod register_client_batch_loop;
pub mod rita_client_registration;
pub mod tls;

const RPC_SERVER: &str = "https://dai.althea.net";
Expand Down Expand Up @@ -71,7 +74,10 @@ async fn retrieve_exit_server_list(
true => {
let node_ip = IpAddr::V4(Ipv4Addr::new(7, 7, 7, 1));
let web3_url = format!("http://{}:8545", node_ip);
info!("Our address is {:?}", CONFIG.clarity_private_key.to_address());
info!(
"Our address is {:?}",
CONFIG.clarity_private_key.to_address()
);
get_exits_list(
&Web3::new(&web3_url, WEB3_TIMEOUT),
CONFIG.clarity_private_key.to_address(),
Expand All @@ -97,7 +103,10 @@ async fn retrieve_exit_server_list(
exit_list: exits,
created: std::time::SystemTime::now(),
};
println!("Signing exit list with PUBKEY: {:?}", CONFIG.clarity_private_key.to_address());
println!(
"Signing exit list with PUBKEY: {:?}",
CONFIG.clarity_private_key.to_address()
);
let cache_value = exit_list.sign(CONFIG.clarity_private_key);

// add this new exit list to the cache
Expand Down Expand Up @@ -129,8 +138,10 @@ pub fn start_exit_trust_root_server() {
});
info!("Starting exit trust root server on {:?}", EXIT_ROOT_DOMAIN);
let server = if SSL {
let cert_chain =
load_certs(&format!("/etc/letsencrypt/live/{}/fullchain.pem", EXIT_ROOT_DOMAIN));
let cert_chain = load_certs(&format!(
"/etc/letsencrypt/live/{}/fullchain.pem",
EXIT_ROOT_DOMAIN
));
let keys = load_rustls_private_key(&format!(
"/etc/letsencrypt/live/{}/privkey.pem",
EXIT_ROOT_DOMAIN
Expand All @@ -143,11 +154,16 @@ pub fn start_exit_trust_root_server() {

info!("Binding to SSL");
server
.bind_rustls(format!("{}:{}", EXIT_ROOT_DOMAIN, SERVER_PORT), config.clone())
.bind_rustls(
format!("{}:{}", EXIT_ROOT_DOMAIN, SERVER_PORT),
config.clone(),
)
.unwrap()
} else {
info!("Binding to {}:{}", EXIT_ROOT_DOMAIN, SERVER_PORT);
server.bind(format!("{}:{}", EXIT_ROOT_DOMAIN, SERVER_PORT)).unwrap()
server
.bind(format!("{}:{}", EXIT_ROOT_DOMAIN, SERVER_PORT))
.unwrap()
};

let _ = server.run().await;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
use crate::{
client_db::{add_users_to_registered_list, get_all_regsitered_clients},
get_reg_queue, remove_client_from_reg_queue, REGISTRATION_LOOP_SPEED, TX_TIMEOUT, WEB3_TIMEOUT,
};
use actix::System;
use althea_types::Identity;
use clarity::{Address, PrivateKey};
use log::{error, info};
use std::{
collections::HashSet,
thread,
time::{Duration, Instant},
};
use web30::{client::Web3, types::SendTxOption};

use crate::{
client_db::{add_users_to_registered_list, get_all_registered_clients},
rita_client_registration::{
get_reg_queue, remove_client_from_reg_queue, REGISTRATION_LOOP_SPEED, TX_TIMEOUT,
WEB3_TIMEOUT,
},
};

pub const MAX_BATCH_SIZE: usize = 75;

/// Utility function used to easily perform O(1) lookups against the identities list
Expand Down Expand Up @@ -55,7 +60,7 @@ pub fn register_client_batch_loop(
// get a copy of all existing clients, we do this in order to handle a potential future edgecase where more than one registration server
// is operating at a time and the same user attempts to register to more than one before the transaction can be sent. Without this check
// once a already registered user is in the queue all future transactions would fail and the server would no longer operate correctly
let all_clients = match get_all_regsitered_clients(&web3, our_private_key.to_address(), contract_addr).await {
let all_clients = match get_all_registered_clients(&web3, our_private_key.to_address(), contract_addr).await {
Ok(all_clients) => all_clients,
Err(e) => {
error!("Failed to get list of already registered clients {:?}, retrying", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use althea_types::{error::AltheaTypesError, ExitClientIdentity, Identity, WgKey};
use awc::error::JsonPayloadError;
use awc::error::SendRequestError;
use lazy_static::lazy_static;
use log::error;
use log::info;
use log::trace;
use phonenumber::PhoneNumber;
use serde::{Deserialize, Serialize};
use std::{
Expand All @@ -12,22 +16,14 @@ use std::{
};
use web30::jsonrpc::error::Web3Error;

#[macro_use]
extern crate log;
#[macro_use]
extern crate lazy_static;

pub mod client_db;
pub mod register_client_batch_loop;

lazy_static! {
/// A map that stores number of texts sent to a client during registration
static ref TEXTS_SENT: Arc<RwLock<HashMap<WgKey, u8>>> = Arc::new(RwLock::new(HashMap::new()));
static ref REGISTER_QUEUE: Arc<RwLock<HashSet<Identity>>> = Arc::new(RwLock::new(HashSet::new()));
}

const REGISTRATION_LOOP_SPEED: Duration = Duration::from_secs(10);
const WEB3_TIMEOUT: Duration = Duration::from_secs(15);
pub const REGISTRATION_LOOP_SPEED: Duration = Duration::from_secs(10);
pub const WEB3_TIMEOUT: Duration = Duration::from_secs(15);
pub const TX_TIMEOUT: Duration = Duration::from_secs(60);

/// Return struct from check_text and Send Text. Verified indicates status from api http req,
Expand Down Expand Up @@ -97,11 +93,11 @@ pub fn add_client_to_reg_queue(id: Identity) {
REGISTER_QUEUE.write().unwrap().insert(id);
}

fn remove_client_from_reg_queue(id: Identity) {
pub fn remove_client_from_reg_queue(id: Identity) {
REGISTER_QUEUE.write().unwrap().remove(&id);
}

fn get_reg_queue() -> Vec<Identity> {
pub fn get_reg_queue() -> Vec<Identity> {
REGISTER_QUEUE.read().unwrap().clone().into_iter().collect()
}

Expand Down
1 change: 0 additions & 1 deletion integration_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ rita_client = { path = "../rita_client", features = ["dev_env"] }
rita_common = { path = "../rita_common", features = ["integration_test"] }
rita_exit = { path = "../rita_exit", features = ["dev_env"] }
exit_trust_root = { path = "../exit_trust_root", features = ["development"] }
rita_client_registration = { path = "../rita_client_registration" }
rita_db_migration = { path = "../rita_db_migration" }
ctrlc = { version = "3.4.5", features = ["termination"] }
diesel = { version = "1.4", features = ["postgres", "r2d2"] }
Expand Down
10 changes: 5 additions & 5 deletions integration_tests/src/contract_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use althea_types::random_identity;
use althea_types::{regions::Regions, ExitIdentity, SystemChain};
use clarity::{Address, PrivateKey};
use rita_client_registration::client_db::{
use exit_trust_root::client_db::{
add_exit_admin, add_exits_to_registration_list, add_users_to_registered_list,
check_and_add_user_admin, get_all_regsitered_clients, get_exits_list,
check_and_add_user_admin, get_all_registered_clients, get_exits_list,
get_registered_client_using_wgkey,
};
use std::collections::HashSet;
Expand Down Expand Up @@ -199,7 +199,7 @@ pub async fn validate_contract_user_functionality(db_addr: Address) {
.unwrap();

// Try requests when there are no users present
let res = get_all_regsitered_clients(&contact, miner_pub_key, db_addr).await;
let res = get_all_registered_clients(&contact, miner_pub_key, db_addr).await;

assert_eq!(res.unwrap(), vec![]);

Expand Down Expand Up @@ -247,7 +247,7 @@ pub async fn validate_contract_user_functionality(db_addr: Address) {
assert_eq!(user_1, res);

// Request a list of all reg users (should be an array of one entry)
let res = get_all_regsitered_clients(&contact, miner_pub_key, db_addr)
let res = get_all_registered_clients(&contact, miner_pub_key, db_addr)
.await
.unwrap();

Expand Down Expand Up @@ -336,7 +336,7 @@ pub async fn validate_contract_user_functionality(db_addr: Address) {
)
.await;

let res = get_all_regsitered_clients(&contact, miner_pub_key, db_addr)
let res = get_all_registered_clients(&contact, miner_pub_key, db_addr)
.await
.unwrap();

Expand Down
Loading

0 comments on commit be1a1ce

Please sign in to comment.