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

Add bulk registration to AltheaDB #848

Merged
merged 10 commits into from
Nov 30, 2023
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
4 changes: 3 additions & 1 deletion 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 althea_kernel_interface/src/traffic_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ impl dyn KernelInterface {

/// Generates a unique traffic class id for a exit user, essentially a really dumb hashing function
pub fn get_class_id(&self, ip: Ipv4Addr) -> u32 {
error!("Trying to get class id for ip {}", ip);
let num: u32 = ip.into();
num % 9999 //9999 is the maximum flow id value allowed
}
Expand Down
72 changes: 39 additions & 33 deletions integration_tests/src/contract_test.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::collections::HashSet;

use althea_types::{ExitIdentity, Regions, SystemChain};
use clarity::{Address, PrivateKey};
use rita_client_registration::client_db::{
add_client_to_registered_list, add_exit_admin, add_exit_to_exit_list, add_user_admin,
get_all_regsitered_clients, get_client_exit_list, get_registered_client_using_wgkey,
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,
get_registered_client_using_wgkey,
};
use rita_common::usage_tracker::tests::test::random_identity;
use std::collections::HashSet;
use web30::{client::Web3, types::SendTxOption};

use crate::{
payments_eth::WEB3_TIMEOUT,
utils::{deploy_contracts, get_eth_node, wait_for_txids, MINER_PRIVATE_KEY, TX_TIMEOUT},
utils::{deploy_contracts, get_eth_node, wait_for_txids, REGISTRATION_SERVER_KEY, TX_TIMEOUT},
};

pub async fn run_altheadb_contract_test() {
Expand All @@ -27,7 +27,7 @@ pub async fn run_altheadb_contract_test() {
}

pub async fn validate_contract_exit_functionality(db_addr: Address) {
let miner_private_key: PrivateKey = MINER_PRIVATE_KEY.parse().unwrap();
let miner_private_key: PrivateKey = REGISTRATION_SERVER_KEY.parse().unwrap();
let miner_pub_key = miner_private_key.to_address();

let contact = Web3::new(&get_eth_node(), WEB3_TIMEOUT);
Expand Down Expand Up @@ -108,13 +108,13 @@ pub async fn validate_contract_exit_functionality(db_addr: Address) {
.await
.unwrap();

let res = get_client_exit_list(&contact, miner_pub_key, db_addr).await;
let res = get_exits_list(&contact, miner_pub_key, db_addr).await;

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

let _res = add_exit_to_exit_list(
let _res = add_exits_to_registration_list(
&contact,
exit_1.clone(),
vec![exit_1.clone()],
db_addr,
miner_private_key,
Some(TX_TIMEOUT),
Expand All @@ -123,9 +123,9 @@ pub async fn validate_contract_exit_functionality(db_addr: Address) {
.await
.unwrap();

let _res = add_exit_to_exit_list(
let _res = add_exits_to_registration_list(
&contact,
exit_2.clone(),
vec![exit_2.clone()],
db_addr,
miner_private_key,
Some(TX_TIMEOUT),
Expand All @@ -134,17 +134,17 @@ pub async fn validate_contract_exit_functionality(db_addr: Address) {
.await
.unwrap();

let res = get_client_exit_list(&contact, miner_pub_key, db_addr)
let res = get_exits_list(&contact, miner_pub_key, db_addr)
.await
.unwrap();

println!("res is {:?}", res);

assert_eq!(res, vec![exit_1.clone(), exit_2.clone()]);

let _res = add_exit_to_exit_list(
let _res = add_exits_to_registration_list(
&contact,
exit_3.clone(),
vec![exit_3.clone()],
db_addr,
miner_private_key,
Some(TX_TIMEOUT),
Expand All @@ -153,7 +153,7 @@ pub async fn validate_contract_exit_functionality(db_addr: Address) {
.await
.unwrap();

let res = get_client_exit_list(&contact, miner_pub_key, db_addr)
let res = get_exits_list(&contact, miner_pub_key, db_addr)
.await
.unwrap();

Expand All @@ -163,7 +163,7 @@ pub async fn validate_contract_exit_functionality(db_addr: Address) {
}

pub async fn validate_contract_user_functionality(db_addr: Address) {
let miner_private_key: PrivateKey = MINER_PRIVATE_KEY.parse().unwrap();
let miner_private_key: PrivateKey = REGISTRATION_SERVER_KEY.parse().unwrap();
let miner_pub_key = miner_private_key.to_address();

let contact = Web3::new(&get_eth_node(), WEB3_TIMEOUT);
Expand All @@ -176,7 +176,7 @@ pub async fn validate_contract_user_functionality(db_addr: Address) {
let user_5 = random_identity();
let user_6 = random_identity();

add_user_admin(
check_and_add_user_admin(
&contact,
db_addr,
miner_pub_key,
Expand All @@ -187,7 +187,7 @@ pub async fn validate_contract_user_functionality(db_addr: Address) {
.await
.unwrap();

add_user_admin(
check_and_add_user_admin(
&contact,
db_addr,
miner_pub_key,
Expand All @@ -210,10 +210,16 @@ pub async fn validate_contract_user_functionality(db_addr: Address) {
assert!(res.is_err());

// Add the first user
let res =
add_client_to_registered_list(&contact, user_1, db_addr, miner_private_key, None, vec![])
.await
.unwrap();
let res = add_users_to_registered_list(
&contact,
vec![user_1],
db_addr,
miner_private_key,
None,
vec![],
)
.await
.unwrap();

contact
.wait_for_transaction(res, TX_TIMEOUT, None)
Expand Down Expand Up @@ -253,9 +259,9 @@ pub async fn validate_contract_user_functionality(db_addr: Address) {
.unwrap();

// Add the second user
let res1 = add_client_to_registered_list(
let res1 = add_users_to_registered_list(
&contact,
user_2,
vec![user_2],
db_addr,
miner_private_key,
None,
Expand All @@ -268,9 +274,9 @@ pub async fn validate_contract_user_functionality(db_addr: Address) {
.unwrap();

// Add the third user
let res2 = add_client_to_registered_list(
let res2 = add_users_to_registered_list(
&contact,
user_3,
vec![user_3],
db_addr,
miner_private_key,
None,
Expand All @@ -282,9 +288,9 @@ pub async fn validate_contract_user_functionality(db_addr: Address) {
.await
.unwrap();

let res3 = add_client_to_registered_list(
let res3 = add_users_to_registered_list(
&contact,
user_4,
vec![user_4],
db_addr,
miner_private_key,
None,
Expand All @@ -296,9 +302,9 @@ pub async fn validate_contract_user_functionality(db_addr: Address) {
.await
.unwrap();

let res4 = add_client_to_registered_list(
let res4 = add_users_to_registered_list(
&contact,
user_5,
vec![user_5],
db_addr,
miner_private_key,
None,
Expand All @@ -310,9 +316,9 @@ pub async fn validate_contract_user_functionality(db_addr: Address) {
.await
.unwrap();

let res5 = add_client_to_registered_list(
let res5 = add_users_to_registered_list(
&contact,
user_6,
vec![user_6],
db_addr,
miner_private_key,
None,
Expand Down
35 changes: 19 additions & 16 deletions integration_tests/src/db_migration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ use std::{
use clarity::{Address, PrivateKey};
use diesel::{PgConnection, RunQueryDsl};
use rita_client_registration::{
client_db::get_all_regsitered_clients, register_client_batch_loop::register_client_batch_loop,
client_db::{check_and_add_user_admin, get_all_regsitered_clients},
register_client_batch_loop::register_client_batch_loop,
};
use rita_common::usage_tracker::tests::test::random_identity;
use rita_db_migration::{
db_migration_user_admin, get_database_connection, models::Client,
schema::clients::dsl::clients, start_db_migration,
get_database_connection, models::Client, schema::clients::dsl::clients, start_db_migration,
};
use web30::client::Web3;

use crate::{
payments_eth::{get_eth_miner_key, WEB3_TIMEOUT},
payments_eth::{TRASACTION_TIMEOUT, WEB3_TIMEOUT},
setup_utils::database::start_postgres,
utils::{deploy_contracts, get_eth_node, MINER_PRIVATE_KEY},
utils::{deploy_contracts, get_eth_node, REGISTRATION_SERVER_KEY},
};

pub const DB_URI: &str = "postgres://postgres@localhost/test";
Expand All @@ -45,28 +45,31 @@ pub async fn run_db_migration_test() {

info!("Run migration code");

let miner_private_key: PrivateKey = get_eth_miner_key();
let reg_server_key: PrivateKey = REGISTRATION_SERVER_KEY.parse().unwrap();
// Start registration loop
info!("Registering user admin");
// This request needs to be made with the state admin's key
db_migration_user_admin(
get_eth_node(),
MINER_PRIVATE_KEY.parse().unwrap(),
miner_private_key.to_address(),
check_and_add_user_admin(
&Web3::new(&get_eth_node(), WEB3_TIMEOUT),
althea_db_addr,
reg_server_key.to_address(),
reg_server_key,
Some(TRASACTION_TIMEOUT),
vec![],
)
.await;
.await
.expect("Failed to add user admin!");

thread::sleep(Duration::from_secs(5));

info!("Starting registration loop");
register_client_batch_loop(get_eth_node(), althea_db_addr, miner_private_key);
register_client_batch_loop(get_eth_node(), althea_db_addr, reg_server_key);

info!("Running user migration");
match start_db_migration(
DB_URI.to_string(),
get_eth_node(),
miner_private_key.to_address(),
reg_server_key.to_address(),
althea_db_addr,
)
.await
Expand All @@ -78,7 +81,7 @@ pub async fn run_db_migration_test() {
info!("Waiting for register loop to migrate all clients");
thread::sleep(Duration::from_secs(10));

validate_db_migration(num_clients, althea_db_addr, miner_private_key).await;
validate_db_migration(num_clients, althea_db_addr, reg_server_key).await;
}

fn add_dummy_clients_to_db(num_of_entries: usize, conn: &PgConnection) {
Expand Down Expand Up @@ -119,9 +122,9 @@ fn random_db_client() -> Client {
async fn validate_db_migration(
num_clients: usize,
althea_db_addr: Address,
miner_private_key: PrivateKey,
reg_server_key: PrivateKey,
) {
let miner_pub_key = miner_private_key.to_address();
let miner_pub_key = reg_server_key.to_address();
let contact = Web3::new(&get_eth_node(), WEB3_TIMEOUT);

let start = Instant::now();
Expand Down
1 change: 1 addition & 0 deletions integration_tests/src/payments_eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub fn eth_chain_id() -> Uint256 {
}

pub const WEB3_TIMEOUT: Duration = Duration::from_secs(1);
pub const TRASACTION_TIMEOUT: Duration = Duration::from_secs(30);
pub const ONE_ETH: u128 = 1_000_000_000_000_000_000;

/// Runs a five node fixed network map test scenario
Expand Down
8 changes: 4 additions & 4 deletions integration_tests/src/registration_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use actix_web::{
use althea_types::ExitClientIdentity;
use clarity::{Address, PrivateKey};
use rita_client_registration::{
client_db::add_user_admin, handle_sms_registration, register_client_batch_loop,
client_db::check_and_add_user_admin, handle_sms_registration, register_client_batch_loop,
};
use web30::client::Web3;

Expand All @@ -18,17 +18,17 @@ use crate::{
};
use crate::{
registration_server::register_client_batch_loop::register_client_batch_loop,
utils::MINER_PRIVATE_KEY,
utils::REGISTRATION_SERVER_KEY,
};

pub const REGISTRATION_PORT_SERVER: u16 = 40400;

pub async fn start_registration_server(db_addr: Address) {
let miner_private_key: PrivateKey = MINER_PRIVATE_KEY.parse().unwrap();
let miner_private_key: PrivateKey = REGISTRATION_SERVER_KEY.parse().unwrap();
let miner_pub_key = miner_private_key.to_address();
let contact = Web3::new(&get_eth_node(), WEB3_TIMEOUT);

add_user_admin(
check_and_add_user_admin(
&contact,
db_addr,
miner_pub_key,
Expand Down
Loading
Loading