From bdc139462624e4a60a140c899992c0fa23cd7923 Mon Sep 17 00:00:00 2001 From: Pranay Tulugu Date: Wed, 13 Sep 2023 11:15:54 -0700 Subject: [PATCH] Fix CI tests and general cleanup --- .github/workflows/rust.yml | 29 +- Cargo.lock | 78 ++--- Cargo.toml | 1 - exit_db/Cargo.toml | 13 - exit_db/diesel.toml | 5 - .../down.sql | 6 - .../up.sql | 36 --- .../2019-02-22-214628_rita-setup/down.sql | 2 - .../2019-02-22-214628_rita-setup/up.sql | 18 -- .../2022-05-18-184145_ipv6_migration/down.sql | 5 - .../2022-05-18-184145_ipv6_migration/up.sql | 10 - .../down.sql | 4 - .../up.sql | 4 - exit_db/src/lib.rs | 7 - exit_db/src/models.rs | 38 --- exit_db/src/schema.rs | 28 -- integration_tests/src/contract_test.rs | 196 ++++------- integration_tests/src/debts.rs | 25 +- integration_tests/src/five_nodes.rs | 37 +-- integration_tests/src/mutli_exit.rs | 30 +- integration_tests/src/payments_althea.rs | 24 +- integration_tests/src/payments_eth.rs | 47 +-- integration_tests/src/registration_server.rs | 46 ++- integration_tests/src/setup_utils/rita.rs | 5 + integration_tests/src/utils.rs | 89 +++-- rita_client/src/exit_manager/mod.rs | 1 - rita_client_registration/src/client_db.rs | 305 ++++++++++++++++-- rita_client_registration/src/lib.rs | 172 +--------- .../src/register_client_batch_loop.rs | 164 ++++++++++ rita_exit/src/database/database_tools.rs | 116 ------- rita_exit/src/database/geoip.rs | 16 +- ...{struct_tools.rs => in_memory_database.rs} | 142 +++++++- rita_exit/src/database/mod.rs | 34 +- rita_exit/src/lib.rs | 3 +- rita_exit/src/rita_loop/mod.rs | 2 - .../container_scripts/all-up-test-internal.sh | 6 + settings/example_exit.toml | 1 + settings/test_exit.toml | 1 + solidity/contract-deployer.ts | 131 ++++---- test_runner/src/main.rs | 8 - 40 files changed, 971 insertions(+), 914 deletions(-) delete mode 100644 exit_db/Cargo.toml delete mode 100644 exit_db/diesel.toml delete mode 100644 exit_db/migrations/00000000000000_diesel_initial_setup/down.sql delete mode 100644 exit_db/migrations/00000000000000_diesel_initial_setup/up.sql delete mode 100644 exit_db/migrations/2019-02-22-214628_rita-setup/down.sql delete mode 100644 exit_db/migrations/2019-02-22-214628_rita-setup/up.sql delete mode 100644 exit_db/migrations/2022-05-18-184145_ipv6_migration/down.sql delete mode 100644 exit_db/migrations/2022-05-18-184145_ipv6_migration/up.sql delete mode 100644 exit_db/migrations/2023-07-03-185432_ipv6_varchar_update/down.sql delete mode 100644 exit_db/migrations/2023-07-03-185432_ipv6_varchar_update/up.sql delete mode 100644 exit_db/src/lib.rs delete mode 100644 exit_db/src/models.rs delete mode 100644 exit_db/src/schema.rs create mode 100644 rita_client_registration/src/register_client_batch_loop.rs delete mode 100644 rita_exit/src/database/database_tools.rs rename rita_exit/src/database/{struct_tools.rs => in_memory_database.rs} (74%) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 0ccc1a885..b469455ad 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -80,24 +80,6 @@ jobs: - uses: Swatinem/rust-cache@v1 - name: Cross test armv7 run: cargo install cross && cross test --target armv7-unknown-linux-musleabihf --verbose -- --test-threads=1 - legacy_integration-test-with-backcompat: - needs: test - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Install Wireguard - run: sudo apt-get update && sudo apt install -y wireguard linux-source linux-headers-$(uname -r) build-essential && sudo modprobe wireguard - - name: Run integration test - run: bash scripts/legacy_integration_test/test-ci-backcompat.sh - legacy_integration-test: - needs: test - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Install Wireguard - run: sudo apt-get update && sudo apt install -y wireguard linux-source linux-headers-$(uname -r) build-essential && sudo modprobe wireguard - - name: Run integration test - run: bash scripts/legacy_integration_test/test-ci.sh integration-test-five-nodes: needs: test runs-on: ubuntu-latest @@ -144,4 +126,13 @@ jobs: - name: Install Wireguard run: sudo apt-get update && sudo apt install -y wireguard linux-source linux-headers-$(uname -r) build-essential && sudo modprobe wireguard - name: Run integration test - run: bash scripts/integration_tests/all-up-test.sh MULTI_EXIT \ No newline at end of file + run: bash scripts/integration_tests/all-up-test.sh MULTI_EXIT + integration-test-validate-contract: + needs: integration-test-five-nodes + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install Wireguard + run: sudo apt-get update && sudo apt install -y wireguard linux-source linux-headers-$(uname -r) build-essential && sudo modprobe wireguard + - name: Run integration test + run: bash scripts/integration_tests/all-up-test.sh CONTRACT_TEST \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 0bf160cdd..162854f50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -91,7 +91,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" dependencies = [ "quote", - "syn 2.0.32", + "syn 2.0.33", ] [[package]] @@ -130,7 +130,7 @@ dependencies = [ "futures-core", "futures-util", "mio", - "socket2 0.5.3", + "socket2 0.5.4", "tokio", "tracing", ] @@ -211,7 +211,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "smallvec", - "socket2 0.5.3", + "socket2 0.5.4", "time", "url", ] @@ -239,7 +239,7 @@ checksum = "7c7db3d5a9718568e4cf4a537cfd7070e6e6ff7481510d0237fb529ac850f6d3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", ] [[package]] @@ -422,7 +422,7 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", ] [[package]] @@ -433,7 +433,7 @@ checksum = "bc00ceb34980c03614e35a3a4e218276a0a824e911d07651cd0d858a51e8c0f0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", ] [[package]] @@ -1047,12 +1047,6 @@ dependencies = [ "strsim", ] -[[package]] -name = "dotenv" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" - [[package]] name = "ed25519" version = "1.5.3" @@ -1133,18 +1127,6 @@ dependencies = [ "libc", ] -[[package]] -name = "exit_db" -version = "0.1.0" -dependencies = [ - "althea_types", - "diesel", - "dotenv", - "serde", - "serde_derive", - "serde_json", -] - [[package]] name = "fastrand" version = "1.9.0" @@ -1274,7 +1256,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", ] [[package]] @@ -1784,9 +1766,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.147" +version = "0.2.148" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" [[package]] name = "libsodium-sys" @@ -2116,7 +2098,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", ] [[package]] @@ -2234,7 +2216,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", ] [[package]] @@ -2295,7 +2277,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", ] [[package]] @@ -2342,9 +2324,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.66" +version = "1.0.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" dependencies = [ "unicode-ident", ] @@ -2369,7 +2351,7 @@ dependencies = [ "itertools 0.11.0", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", ] [[package]] @@ -2884,9 +2866,9 @@ dependencies = [ [[package]] name = "rustls-webpki" -version = "0.101.4" +version = "0.101.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d93931baf2d282fff8d3a532bbfd7653f734643161b87e3e01e59a04439bf0d" +checksum = "45a27e3b59326c16e23d30aeb7a36a24cc0d29e71d68ff611cdfb4a01d013bed" dependencies = [ "ring", "untrusted", @@ -3027,7 +3009,7 @@ checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", ] [[package]] @@ -3153,9 +3135,9 @@ dependencies = [ [[package]] name = "socket2" -version = "0.5.3" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2538b18701741680e0322a2302176d3253a35388e2e62f172f64f4f16605f877" +checksum = "4031e820eb552adee9295814c0ced9e5cf38ddf1e8b7d566d6de8e2538ea989e" dependencies = [ "libc", "windows-sys", @@ -3204,9 +3186,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.32" +version = "2.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2" +checksum = "9caece70c63bfba29ec2fed841a09851b14a235c60010fa4de58089b6c025668" dependencies = [ "proc-macro2", "quote", @@ -3285,7 +3267,7 @@ checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", ] [[package]] @@ -3345,7 +3327,7 @@ dependencies = [ "parking_lot", "pin-project-lite", "signal-hook-registry", - "socket2 0.5.3", + "socket2 0.5.4", "tokio-macros", "windows-sys", ] @@ -3368,7 +3350,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", ] [[package]] @@ -3521,7 +3503,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", ] [[package]] @@ -3559,9 +3541,9 @@ checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" [[package]] name = "unicode-ident" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "unicode-normalization" @@ -3656,7 +3638,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", "wasm-bindgen-shared", ] @@ -3690,7 +3672,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.33", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/Cargo.toml b/Cargo.toml index b3eac0262..0dc588d9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ members = [ "althea_kernel_interface", "settings", "clu", - "exit_db", "antenna_forwarding_client", "antenna_forwarding_protocol", "auto_bridge", diff --git a/exit_db/Cargo.toml b/exit_db/Cargo.toml deleted file mode 100644 index a519fedc1..000000000 --- a/exit_db/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "exit_db" -version = "0.1.0" -authors = ["Ben "] -edition = "2018" - -[dependencies] -diesel = { version = "1.4", features = ["postgres"] } -dotenv = "0.15" -althea_types = { path = "../althea_types"} -serde = "1.0" -serde_derive = "1.0" -serde_json = "1.0" diff --git a/exit_db/diesel.toml b/exit_db/diesel.toml deleted file mode 100644 index 92267c829..000000000 --- a/exit_db/diesel.toml +++ /dev/null @@ -1,5 +0,0 @@ -# For documentation on how to configure this file, -# see diesel.rs/guides/configuring-diesel-cli - -[print_schema] -file = "src/schema.rs" diff --git a/exit_db/migrations/00000000000000_diesel_initial_setup/down.sql b/exit_db/migrations/00000000000000_diesel_initial_setup/down.sql deleted file mode 100644 index a9f526091..000000000 --- a/exit_db/migrations/00000000000000_diesel_initial_setup/down.sql +++ /dev/null @@ -1,6 +0,0 @@ --- This file was automatically created by Diesel to setup helper functions --- and other internal bookkeeping. This file is safe to edit, any future --- changes will be added to existing projects as new migrations. - -DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); -DROP FUNCTION IF EXISTS diesel_set_updated_at(); diff --git a/exit_db/migrations/00000000000000_diesel_initial_setup/up.sql b/exit_db/migrations/00000000000000_diesel_initial_setup/up.sql deleted file mode 100644 index d68895b1a..000000000 --- a/exit_db/migrations/00000000000000_diesel_initial_setup/up.sql +++ /dev/null @@ -1,36 +0,0 @@ --- This file was automatically created by Diesel to setup helper functions --- and other internal bookkeeping. This file is safe to edit, any future --- changes will be added to existing projects as new migrations. - - - - --- Sets up a trigger for the given table to automatically set a column called --- `updated_at` whenever the row is modified (unless `updated_at` was included --- in the modified columns) --- --- # Example --- --- ```sql --- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); --- --- SELECT diesel_manage_updated_at('users'); --- ``` -CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ -BEGIN - EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s - FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ -BEGIN - IF ( - NEW IS DISTINCT FROM OLD AND - NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at - ) THEN - NEW.updated_at := current_timestamp; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; diff --git a/exit_db/migrations/2019-02-22-214628_rita-setup/down.sql b/exit_db/migrations/2019-02-22-214628_rita-setup/down.sql deleted file mode 100644 index 036a199de..000000000 --- a/exit_db/migrations/2019-02-22-214628_rita-setup/down.sql +++ /dev/null @@ -1,2 +0,0 @@ --- This file should undo anything in `up.sql` -DROP TABLE clients; \ No newline at end of file diff --git a/exit_db/migrations/2019-02-22-214628_rita-setup/up.sql b/exit_db/migrations/2019-02-22-214628_rita-setup/up.sql deleted file mode 100644 index 5e2c70792..000000000 --- a/exit_db/migrations/2019-02-22-214628_rita-setup/up.sql +++ /dev/null @@ -1,18 +0,0 @@ -CREATE TABLE clients -( - mesh_ip varchar(40) CONSTRAINT firstkey PRIMARY KEY, - wg_pubkey varchar(44) NOT NULL, - wg_port integer NOT NULL, - eth_address varchar(64) NOT NULL, - internal_ip varchar(42) NOT NULL, - nickname varchar(32) NOT NULL, - email varchar(512) NOT NULL, - phone varchar(32) NOT NULL, - country varchar(8) NOT NULL, - email_code varchar(16) NOT NULL, - verified boolean DEFAULT FALSE NOT NULL, - email_sent_time bigint DEFAULT 0 NOT NULL, - text_sent integer DEFAULT 0 NOT NULL, - last_seen bigint DEFAULT 0 NOT NULL, - last_balance_warning_time bigint DEFAULT 0 NOT NULL -); \ No newline at end of file diff --git a/exit_db/migrations/2022-05-18-184145_ipv6_migration/down.sql b/exit_db/migrations/2022-05-18-184145_ipv6_migration/down.sql deleted file mode 100644 index e42354d99..000000000 --- a/exit_db/migrations/2022-05-18-184145_ipv6_migration/down.sql +++ /dev/null @@ -1,5 +0,0 @@ --- This file should undo anything in `up.sql` -ALTER TABLE clients - DROP COLUMN internet_ipv6 -; -DROP TABLE assigned_ips; \ No newline at end of file diff --git a/exit_db/migrations/2022-05-18-184145_ipv6_migration/up.sql b/exit_db/migrations/2022-05-18-184145_ipv6_migration/up.sql deleted file mode 100644 index cd3982421..000000000 --- a/exit_db/migrations/2022-05-18-184145_ipv6_migration/up.sql +++ /dev/null @@ -1,10 +0,0 @@ --- Your SQL goes here -ALTER TABLE clients - ADD COLUMN internet_ipv6 varchar(132) NOT NULL DEFAULT '' -; -CREATE TABLE assigned_ips -( - subnet varchar(132) CONSTRAINT secondkey PRIMARY KEY, - available_subnets varchar(512) NOT NULL, - iterative_index bigint DEFAULT 0 NOT NULL -); \ No newline at end of file diff --git a/exit_db/migrations/2023-07-03-185432_ipv6_varchar_update/down.sql b/exit_db/migrations/2023-07-03-185432_ipv6_varchar_update/down.sql deleted file mode 100644 index c6d3c27da..000000000 --- a/exit_db/migrations/2023-07-03-185432_ipv6_varchar_update/down.sql +++ /dev/null @@ -1,4 +0,0 @@ --- This file should undo anything in `up.sql` -ALTER TABLE assigned_ips - ALTER COLUMN available_subnets type varchar(512) -; diff --git a/exit_db/migrations/2023-07-03-185432_ipv6_varchar_update/up.sql b/exit_db/migrations/2023-07-03-185432_ipv6_varchar_update/up.sql deleted file mode 100644 index edbd08fe1..000000000 --- a/exit_db/migrations/2023-07-03-185432_ipv6_varchar_update/up.sql +++ /dev/null @@ -1,4 +0,0 @@ --- Your SQL goes here -ALTER TABLE assigned_ips - ALTER COLUMN available_subnets type varchar -; \ No newline at end of file diff --git a/exit_db/src/lib.rs b/exit_db/src/lib.rs deleted file mode 100644 index 244963cc8..000000000 --- a/exit_db/src/lib.rs +++ /dev/null @@ -1,7 +0,0 @@ -#[macro_use] -extern crate diesel; -#[macro_use] -extern crate serde_derive; - -pub mod models; -pub mod schema; diff --git a/exit_db/src/models.rs b/exit_db/src/models.rs deleted file mode 100644 index cd9babc70..000000000 --- a/exit_db/src/models.rs +++ /dev/null @@ -1,38 +0,0 @@ -#![allow(clippy::extra_unused_lifetimes)] -use crate::schema::assigned_ips; -use crate::schema::clients; - -#[derive(Queryable, Serialize, Deserialize, Debug, Insertable, Clone, Default)] -#[table_name = "clients"] -pub struct Client { - pub mesh_ip: String, - pub wg_pubkey: String, - pub wg_port: i32, - pub eth_address: String, - pub internal_ip: String, - pub internet_ipv6: String, - pub nickname: String, - pub email: String, - pub phone: String, - pub country: String, - pub email_code: String, - pub verified: bool, - pub email_sent_time: i64, - pub text_sent: i32, - pub last_seen: i64, - pub last_balance_warning_time: i64, -} - -/// This struct holds information about the ipv6 subnets being assigned to clients who connect. -/// The vector available subnets is a stack that has a list of available subnets to use. This stack gets populated whenever -/// a client gets removed from the database. It is stored as a string of indecies, for example, "1,24,36" -/// The iterative index stores the index at which we assign a subnet to a client -/// For example, if our exit subnet is fd00::1000/120 and our client subnets are /124, index 0 represents -/// fd00::1000/124 index 1 represents fd00::1010/124, 2 is fd00::1120/124 etc... -#[derive(Queryable, Serialize, Deserialize, Debug, Insertable, Clone, Default)] -#[table_name = "assigned_ips"] -pub struct AssignedIps { - pub subnet: String, - pub available_subnets: String, - pub iterative_index: i64, -} diff --git a/exit_db/src/schema.rs b/exit_db/src/schema.rs deleted file mode 100644 index d8470b758..000000000 --- a/exit_db/src/schema.rs +++ /dev/null @@ -1,28 +0,0 @@ -table! { - clients (mesh_ip) { - mesh_ip -> Varchar, - wg_pubkey -> Varchar, - wg_port -> Int4, - eth_address -> Varchar, - internal_ip -> Varchar, - internet_ipv6 -> Varchar, - nickname -> Varchar, - email -> Varchar, - phone -> Varchar, - country -> Varchar, - email_code -> Varchar, - verified -> Bool, - email_sent_time -> Int8, - text_sent -> Int4, - last_seen -> Int8, - last_balance_warning_time -> Int8, - } -} - -table! { - assigned_ips (subnet) { - subnet -> Varchar, - available_subnets -> Varchar, - iterative_index -> Int8, - } -} diff --git a/integration_tests/src/contract_test.rs b/integration_tests/src/contract_test.rs index 8f57d3c0c..6c7d893db 100644 --- a/integration_tests/src/contract_test.rs +++ b/integration_tests/src/contract_test.rs @@ -1,99 +1,55 @@ use std::{thread, time::Duration}; -use althea_types::Identity; +use clarity::{Address, PrivateKey}; +use log::info; use rita_client_registration::client_db::{ add_client_to_registered_list, get_all_regsitered_clients, get_registered_client_using_ethkey, get_registered_client_using_meship, get_registered_client_using_wgkey, }; +use rita_common::usage_tracker::tests::test::random_identity; use web30::client::Web3; use crate::{ - payments_eth::{ETH_MINER_KEY, WEB3_TIMEOUT}, - utils::{get_altheadb_contract_addr, get_eth_node}, + payments_eth::{get_miner_key, WEB3_TIMEOUT}, + utils::{deploy_contracts, get_eth_node}, }; pub async fn run_altheadb_contract_test() { - // Try adding a dummy entry and validating that we can retrive them - validate_contract_functionality().await; + info!("Waiting to deploy contracts"); + let althea_db_addr = deploy_contracts().await; + info!("DB addr is {}", althea_db_addr); - thread::sleep(Duration::from_secs(1000)); + // Try adding a dummy entry and validating that we can retrive them + validate_contract_functionality(althea_db_addr).await; } -pub async fn validate_contract_functionality() { +pub async fn validate_contract_functionality(db_addr: Address) { + let miner_private_key: PrivateKey = get_miner_key(); + let miner_pub_key = miner_private_key.to_address(); + let contact = Web3::new(&get_eth_node(), WEB3_TIMEOUT); // Define the users - let user = Identity { - mesh_ip: "fd00::1337".parse().unwrap(), - eth_address: "0x02ad6b480DFeD806C63a0839C6f1f3136c5fD515" - .parse() - .unwrap(), - wg_public_key: "sPtNGQbyPpCsqSKD6PbnflB1lIUCd259Vhd0mJfJeGo=" - .parse() - .unwrap(), - nickname: None, - }; + let user_1 = random_identity(); + let user_2 = random_identity(); + let user_3 = random_identity(); - let user_2 = Identity { - mesh_ip: "fd00::1447:1447".parse().unwrap(), - eth_address: "0x1994A73F79F9648d4a8064D9C0F221fB1007Fd2F" - .parse() - .unwrap(), - wg_public_key: "Yhyj+CKZbyEKea/9hdIFje98yc5Cukt1Pbq0qWB4Aqw=" - .parse() - .unwrap(), - nickname: None, - }; + // Try requests when there are no users present + let res = get_all_regsitered_clients(&contact, miner_pub_key, db_addr).await; - let user_3 = Identity { - mesh_ip: "fd00::3000:1117".parse().unwrap(), - eth_address: "0x9c33D0dFdc9E3f7cC73bE3A575C31cfe3059C76a" - .parse() - .unwrap(), - wg_public_key: "fzOUfEqYzRE0MwfR5o7XV+MKZKj/qEfELRzQTRTKAB8=" - .parse() - .unwrap(), - nickname: None, - }; + assert!(res.is_err()); - // Try requests when there are no users present - let res = get_all_regsitered_clients( - &contact, - ETH_MINER_KEY - .parse::() - .unwrap() - .to_address(), - get_altheadb_contract_addr(), - ) - .await - .unwrap(); - - assert!(res.is_empty()); - - let res = get_registered_client_using_wgkey( - user.wg_public_key, - ETH_MINER_KEY - .parse::() - .unwrap() - .to_address(), - get_altheadb_contract_addr(), - &contact, - ) - .await; + let res = + get_registered_client_using_wgkey(user_1.wg_public_key, miner_pub_key, db_addr, &contact) + .await; assert!(res.is_err()); // Add the first user - let _res = add_client_to_registered_list( - &contact, - user, - get_altheadb_contract_addr(), - ETH_MINER_KEY.parse().unwrap(), - None, - vec![], - ) - .await - .unwrap(); + let _res = + add_client_to_registered_list(&contact, user_1, db_addr, miner_private_key, None, vec![]) + .await + .unwrap(); thread::sleep(Duration::from_secs(5)); @@ -102,11 +58,8 @@ pub async fn validate_contract_functionality() { "0x3d261902a988d94599d7f0Bd4c2e4514D73BB329" .parse() .unwrap(), - ETH_MINER_KEY - .parse::() - .unwrap() - .to_address(), - get_altheadb_contract_addr(), + miner_pub_key, + db_addr, &contact, ) .await; @@ -114,83 +67,44 @@ pub async fn validate_contract_functionality() { assert!(res.is_err()); // Add the second user - let _res = add_client_to_registered_list( - &contact, - user_2, - get_altheadb_contract_addr(), - ETH_MINER_KEY.parse().unwrap(), - None, - vec![], - ) - .await - .unwrap(); + let _res = + add_client_to_registered_list(&contact, user_2, db_addr, miner_private_key, None, vec![]) + .await + .unwrap(); thread::sleep(Duration::from_secs(5)); // Add the third user - let _res = add_client_to_registered_list( - &contact, - user_3, - get_altheadb_contract_addr(), - ETH_MINER_KEY.parse().unwrap(), - None, - vec![], - ) - .await - .unwrap(); + let _res = + add_client_to_registered_list(&contact, user_3, db_addr, miner_private_key, None, vec![]) + .await + .unwrap(); thread::sleep(Duration::from_secs(10)); - let res = get_all_regsitered_clients( - &contact, - ETH_MINER_KEY - .parse::() - .unwrap() - .to_address(), - get_altheadb_contract_addr(), - ) - .await; + let res = get_all_regsitered_clients(&contact, miner_pub_key, db_addr).await; - println!("All users are : {:?}", res); + info!("All users are : {:?}", res); thread::sleep(Duration::from_secs(5)); - let res = get_registered_client_using_wgkey( - user.wg_public_key, - ETH_MINER_KEY - .parse::() - .unwrap() - .to_address(), - get_altheadb_contract_addr(), - &contact, - ) - .await - .unwrap(); - assert_eq!(res, user); - - let res = get_registered_client_using_ethkey( - user_2.eth_address, - ETH_MINER_KEY - .parse::() - .unwrap() - .to_address(), - get_altheadb_contract_addr(), - &contact, - ) - .await - .unwrap(); + info!("Trying to retrive user 1"); + let res = + get_registered_client_using_wgkey(user_1.wg_public_key, miner_pub_key, db_addr, &contact) + .await + .unwrap(); + assert_eq!(res, user_1); + + info!("Trying to retrive user 2"); + let res = + get_registered_client_using_ethkey(user_2.eth_address, miner_pub_key, db_addr, &contact) + .await + .unwrap(); assert_eq!(res, user_2); - let res = get_registered_client_using_meship( - user_3.mesh_ip, - ETH_MINER_KEY - .parse::() - .unwrap() - .to_address(), - get_altheadb_contract_addr(), - &contact, - ) - .await - .unwrap(); + info!("Trying to retrive user 3"); + let res = get_registered_client_using_meship(user_3.mesh_ip, miner_pub_key, db_addr, &contact) + .await + .unwrap(); assert_eq!(res, user_3); } diff --git a/integration_tests/src/debts.rs b/integration_tests/src/debts.rs index 122e62927..ee5f39aa0 100644 --- a/integration_tests/src/debts.rs +++ b/integration_tests/src/debts.rs @@ -3,13 +3,14 @@ use std::thread; use std::time::Duration; use crate::five_nodes::five_node_config; -use crate::setup_utils::database::start_postgres; +use crate::registration_server::start_registration_server; use crate::setup_utils::namespaces::*; use crate::setup_utils::rita::thread_spawner; use crate::utils::{ - generate_traffic, get_default_settings, get_ip_from_namespace, query_debts, - register_all_namespaces_to_exit, test_all_internet_connectivity, test_reach_all, test_routes, - DEBT_ACCURACY_THRES, TEST_EXIT_DETAILS, + deploy_contracts, generate_traffic, get_default_settings, get_ip_from_namespace, + populate_routers_eth, query_debts, register_all_namespaces_to_exit, + test_all_internet_connectivity, test_reach_all, test_routes, DEBT_ACCURACY_THRES, + TEST_EXIT_DETAILS, }; use log::info; use num256::Int256; @@ -44,6 +45,12 @@ pub async fn run_debts_test() { let namespaces = node_config.0; let expected_routes = node_config.1; + info!("Waiting to deploy contracts"); + let db_addr = deploy_contracts().await; + + info!("Starting registration server"); + start_registration_server(db_addr); + let (client_settings, exit_settings) = get_default_settings("test".to_string(), namespaces.clone()); @@ -51,15 +58,17 @@ pub async fn run_debts_test() { let exit_price = namespaces.get_namespace(4).unwrap().cost; namespaces.validate(); - start_postgres(); let res = setup_ns(namespaces.clone()); info!("Namespaces setup: {res:?}"); - let _rita_identities = thread_spawner(namespaces.clone(), client_settings, exit_settings) - .expect("Could not spawn Rita threads"); + let rita_identities = + thread_spawner(namespaces.clone(), client_settings, exit_settings, db_addr) + .expect("Could not spawn Rita threads"); info!("Thread Spawner: {res:?}"); + populate_routers_eth(rita_identities).await; + // Test for network convergence test_reach_all(namespaces.clone()); @@ -69,7 +78,7 @@ pub async fn run_debts_test() { register_all_namespaces_to_exit(namespaces.clone()).await; // Let network stabalize - thread::sleep(Duration::from_secs(20)); + thread::sleep(Duration::from_secs(10)); test_all_internet_connectivity(namespaces.clone()); diff --git a/integration_tests/src/five_nodes.rs b/integration_tests/src/five_nodes.rs index bcd3b1a63..96e731c15 100644 --- a/integration_tests/src/five_nodes.rs +++ b/integration_tests/src/five_nodes.rs @@ -1,16 +1,14 @@ -use crate::payments_eth::{ONE_ETH, WEB3_TIMEOUT}; -use crate::setup_utils::database::start_postgres; +use crate::registration_server::start_registration_server; use crate::setup_utils::namespaces::*; use crate::setup_utils::rita::thread_spawner; use crate::utils::{ - get_default_settings, register_all_namespaces_to_exit, send_eth_bulk, + deploy_contracts, get_default_settings, populate_routers_eth, register_all_namespaces_to_exit, test_all_internet_connectivity, test_reach_all, test_routes, }; use log::info; use std::collections::HashMap; use std::thread; use std::time::Duration; -use web30::client::Web3; /// Runs a five node fixed network map test scenario, this does basic network setup and tests reachability to /// all destinations @@ -20,40 +18,35 @@ pub async fn run_five_node_test_scenario() { let namespaces = node_config.0; let expected_routes = node_config.1; + info!("Waiting to deploy contracts"); + let db_addr = deploy_contracts().await; + + info!("Starting registration server"); + start_registration_server(db_addr); + let (client_settings, exit_settings) = get_default_settings("test".to_string(), namespaces.clone()); namespaces.validate(); - start_postgres(); let res = setup_ns(namespaces.clone()); info!("Namespaces setup: {res:?}"); - let rita_identities = thread_spawner(namespaces.clone(), client_settings, exit_settings) - .expect("Could not spawn Rita threads"); + let rita_identities = + thread_spawner(namespaces.clone(), client_settings, exit_settings, db_addr) + .expect("Could not spawn Rita threads"); info!("Thread Spawner: {res:?}"); // this sleep is for debugging so that the container can be accessed to poke around in //thread::sleep(SETUP_WAIT * 500); + info!("About to populate routers with eth"); + populate_routers_eth(rita_identities).await; + test_reach_all(namespaces.clone()); test_routes(namespaces.clone(), expected_routes); - // Exits need to have funds to request a registered client list, which is needed for proper setup - info!("Topup exits with funds"); - let web3 = Web3::new("http://localhost:8545", WEB3_TIMEOUT); - let mut to_top_up = Vec::new(); - for c in rita_identities.client_identities { - to_top_up.push(c.eth_address); - } - for e in rita_identities.exit_identities { - to_top_up.push(e.eth_address) - } - - info!("Sending 50 eth to all routers"); - send_eth_bulk((ONE_ETH * 50).into(), &to_top_up, &web3).await; - info!("Registering routers to the exit"); register_all_namespaces_to_exit(namespaces.clone()).await; @@ -61,6 +54,8 @@ pub async fn run_five_node_test_scenario() { info!("Checking for wg_exit tunnel setup"); test_all_internet_connectivity(namespaces.clone()); + + info!("All clients successfully registered!"); } /// This defines the network map for a five node scenario diff --git a/integration_tests/src/mutli_exit.rs b/integration_tests/src/mutli_exit.rs index bb5b40e9a..cc8a5dcbc 100644 --- a/integration_tests/src/mutli_exit.rs +++ b/integration_tests/src/mutli_exit.rs @@ -1,14 +1,15 @@ use std::{collections::HashMap, str::from_utf8, thread, time::Duration}; use crate::{ + registration_server::start_registration_server, setup_utils::{ - database::start_postgres, namespaces::{setup_ns, Namespace, NamespaceInfo, NodeType, PriceId, RouteHop}, rita::thread_spawner, }, utils::{ - get_default_settings, get_node_id_from_ip, register_all_namespaces_to_exit, - test_all_internet_connectivity, test_reach_all, test_routes, + deploy_contracts, get_default_settings, get_node_id_from_ip, populate_routers_eth, + register_all_namespaces_to_exit, test_all_internet_connectivity, test_reach_all, + test_routes, }, }; use althea_kernel_interface::KI; @@ -37,19 +38,30 @@ pub async fn run_multi_exit_test() { let namespaces = node_configs.0; let expected_routes = node_configs.1; + info!("Waiting to deploy contracts"); + let db_addr = deploy_contracts().await; + + info!("Starting registration server"); + start_registration_server(db_addr); + let (rita_client_settings, rita_exit_settings) = get_default_settings("test".to_string(), namespaces.clone()); namespaces.validate(); - start_postgres(); let res = setup_ns(namespaces.clone()); - let _rita_identities = - thread_spawner(namespaces.clone(), rita_client_settings, rita_exit_settings) - .expect("Could not spawn Rita threads"); + let rita_identities = thread_spawner( + namespaces.clone(), + rita_client_settings, + rita_exit_settings, + db_addr, + ) + .expect("Could not spawn Rita threads"); info!("Thread Spawner: {res:?}"); + populate_routers_eth(rita_identities).await; + // Test for network convergence test_reach_all(namespaces.clone()); @@ -58,10 +70,12 @@ pub async fn run_multi_exit_test() { info!("Registering routers to the exit"); register_all_namespaces_to_exit(namespaces.clone()).await; - thread::sleep(Duration::from_secs(5)); + thread::sleep(Duration::from_secs(10)); test_all_internet_connectivity(namespaces.clone()); + info!("All clients successfully registered!"); + let current_exit = get_current_exit(namespaces.names[0].clone(), namespaces.clone()); info!( "All nodes are set up correctly and are connected to exit {}", diff --git a/integration_tests/src/payments_althea.rs b/integration_tests/src/payments_althea.rs index 70b0dd9b9..92b6fc911 100644 --- a/integration_tests/src/payments_althea.rs +++ b/integration_tests/src/payments_althea.rs @@ -3,12 +3,13 @@ use std::thread; use std::time::Duration; use crate::five_nodes::five_node_config; -use crate::setup_utils::database::start_postgres; +use crate::registration_server::start_registration_server; use crate::setup_utils::namespaces::*; use crate::setup_utils::rita::thread_spawner; use crate::utils::{ - generate_traffic, get_default_settings, print_althea_balances, register_all_namespaces_to_exit, - register_erc20_usdc_token, send_althea_tokens, test_reach_all, test_routes, + deploy_contracts, generate_traffic, get_default_settings, populate_routers_eth, + print_althea_balances, register_all_namespaces_to_exit, register_erc20_usdc_token, + send_althea_tokens, test_all_internet_connectivity, test_reach_all, test_routes, validate_debt_entry, TEST_PAY_THRESH, }; use althea_types::{Denom, SystemChain, ALTHEA_PREFIX}; @@ -43,11 +44,16 @@ pub async fn run_althea_payments_test_scenario() { let namespaces = node_config.0; let expected_routes = node_config.1; + info!("Waiting to deploy contracts"); + let db_addr = deploy_contracts().await; + + info!("Starting registration server"); + start_registration_server(db_addr); + let (mut client_settings, mut exit_settings) = get_default_settings("test".to_string(), namespaces.clone()); namespaces.validate(); - start_postgres(); let res = setup_ns(namespaces.clone()); info!("Namespaces setup: {res:?}"); @@ -56,10 +62,13 @@ pub async fn run_althea_payments_test_scenario() { let (client_settings, exit_settings) = althea_payments_map(&mut client_settings, &mut exit_settings); - let rita_identities = thread_spawner(namespaces.clone(), client_settings, exit_settings) - .expect("Could not spawn Rita threads"); + let rita_identities = + thread_spawner(namespaces.clone(), client_settings, exit_settings, db_addr) + .expect("Could not spawn Rita threads"); info!("Thread Spawner: {res:?}"); + populate_routers_eth(rita_identities.clone()).await; + // Test for network convergence test_reach_all(namespaces.clone()); @@ -70,6 +79,9 @@ pub async fn run_althea_payments_test_scenario() { thread::sleep(Duration::from_secs(10)); + test_all_internet_connectivity(namespaces.clone()); + info!("Successfully registered all clients"); + let from_node: Option = namespaces.get_namespace(1); let forward_node: Option = namespaces.get_namespace(3); let end_node: Option = namespaces.get_namespace(6); diff --git a/integration_tests/src/payments_eth.rs b/integration_tests/src/payments_eth.rs index dde886b2f..f1b972f3e 100644 --- a/integration_tests/src/payments_eth.rs +++ b/integration_tests/src/payments_eth.rs @@ -1,12 +1,13 @@ use crate::five_nodes::five_node_config; -use crate::setup_utils::database::start_postgres; +use crate::registration_server::start_registration_server; use crate::setup_utils::namespaces::setup_ns; use crate::setup_utils::namespaces::Namespace; use crate::setup_utils::rita::thread_spawner; +use crate::utils::deploy_contracts; +use crate::utils::populate_routers_eth; +use crate::utils::test_all_internet_connectivity; use crate::utils::{generate_traffic, register_all_namespaces_to_exit, validate_debt_entry}; -use crate::utils::{ - get_default_settings, send_eth_bulk, test_reach_all, test_routes, TEST_PAY_THRESH, -}; +use crate::utils::{get_default_settings, test_reach_all, test_routes, TEST_PAY_THRESH}; use clarity::Address as EthAddress; use clarity::{PrivateKey as EthPrivateKey, Uint256}; use log::info; @@ -15,11 +16,9 @@ use settings::client::RitaClientSettings; use settings::exit::RitaExitSettingsStruct; use std::thread; use std::time::Duration; -use web30::client::Web3; /// Key with funds in the EVM that can be sent to routers -pub const ETH_MINER_KEY: &str = - "0xb1bab011e03a9862664706fc3bbaa1b16651528e5f0e7fbfcbfdd8be302a13e7"; +const ETH_MINER_KEY: &str = "0xb1bab011e03a9862664706fc3bbaa1b16651528e5f0e7fbfcbfdd8be302a13e7"; pub fn get_miner_key() -> EthPrivateKey { ETH_MINER_KEY.parse().unwrap() @@ -44,6 +43,12 @@ pub async fn run_eth_payments_test_scenario() { let namespaces = node_config.0; let expected_routes = node_config.1; + info!("Waiting to deploy contracts"); + let db_addr = deploy_contracts().await; + + info!("Starting registration server"); + start_registration_server(db_addr); + let (mut client_settings, mut exit_settings) = get_default_settings("test".to_string(), namespaces.clone()); @@ -52,15 +57,17 @@ pub async fn run_eth_payments_test_scenario() { eth_payments_map(&mut client_settings, &mut exit_settings); namespaces.validate(); - start_postgres(); let res = setup_ns(namespaces.clone()); info!("Namespaces setup: {res:?}"); - let rita_identities = thread_spawner(namespaces.clone(), client_settings, exit_settings) - .expect("Could not spawn Rita threads"); + let rita_identities = + thread_spawner(namespaces.clone(), client_settings, exit_settings, db_addr) + .expect("Could not spawn Rita threads"); info!("Thread Spawner: {res:?}"); + populate_routers_eth(rita_identities).await; + test_reach_all(namespaces.clone()); test_routes(namespaces.clone(), expected_routes); @@ -69,23 +76,17 @@ pub async fn run_eth_payments_test_scenario() { thread::sleep(Duration::from_secs(10)); + info!("Checking for wg_exit tunnel setup"); + test_all_internet_connectivity(namespaces.clone()); + + info!("All clients successfully registered!"); + + thread::sleep(Duration::from_secs(10)); + let from_node: Option = namespaces.get_namespace(1); let forward_node: Option = namespaces.get_namespace(3); let end_node: Option = namespaces.get_namespace(6); - // start main test content - let web3 = Web3::new("http://localhost:8545", WEB3_TIMEOUT); - let mut to_top_up = Vec::new(); - for c in rita_identities.client_identities { - to_top_up.push(c.eth_address); - } - for e in rita_identities.exit_identities { - to_top_up.push(e.eth_address) - } - - info!("Sending 50 eth to all routers"); - send_eth_bulk((ONE_ETH * 50).into(), &to_top_up, &web3).await; - info!("Trying to generate traffic"); generate_traffic( from_node.clone().unwrap(), diff --git a/integration_tests/src/registration_server.rs b/integration_tests/src/registration_server.rs index da184a948..8fbf1f60b 100644 --- a/integration_tests/src/registration_server.rs +++ b/integration_tests/src/registration_server.rs @@ -1,4 +1,7 @@ -use std::thread; +use std::{ + sync::{Arc, RwLock}, + thread, +}; use actix_rt::System; use actix_web::{ @@ -6,27 +9,44 @@ use actix_web::{ App, HttpResponse, HttpServer, }; use althea_types::ExitClientIdentity; +use clarity::Address; use rita_client_registration::{ client_conflict, handle_sms_registration, register_client_batch_loop, }; use web30::client::Web3; +use crate::registration_server::register_client_batch_loop::register_client_batch_loop; use crate::{ - payments_eth::{ETH_MINER_KEY, WEB3_TIMEOUT}, - utils::{get_altheadb_contract_addr, get_eth_node, get_test_runner_magic_phone}, + payments_eth::{get_miner_address, get_miner_key, WEB3_TIMEOUT}, + utils::{get_eth_node, get_test_runner_magic_phone}, }; use log::{error, info}; pub const REGISTRATION_PORT_SERVER: u16 = 40400; -pub fn start_registration_server() { +#[derive(Clone, Copy, Debug, Default)] +struct RegistrationServerState { + pub db_contract_addr: Option
, +} + +lazy_static! { + static ref REGISTRATION_SERVER_STATE: Arc> = + Arc::new(RwLock::new(RegistrationServerState::default())); +} + +fn get_althea_db_addr() -> Option
{ + REGISTRATION_SERVER_STATE.read().unwrap().db_contract_addr +} + +fn set_althea_db_addr(addr: Address) { + REGISTRATION_SERVER_STATE.write().unwrap().db_contract_addr = Some(addr) +} + +pub fn start_registration_server(db_addr: Address) { // Start the register loop - register_client_batch_loop( - get_eth_node(), - get_altheadb_contract_addr(), - ETH_MINER_KEY.parse().unwrap(), - ); + register_client_batch_loop(get_eth_node(), db_addr, get_miner_key()); + set_althea_db_addr(db_addr); // Start endpoint listener thread::spawn(move || { let runner = System::new(); @@ -50,17 +70,15 @@ async fn register_router(client: Json) -> HttpResponse { let client = client.into_inner(); info!("Attempting to register client: {}", client.global.mesh_ip); let contact = Web3::new(&get_eth_node(), WEB3_TIMEOUT); + let db_addr = get_althea_db_addr(); // Check for an existing client let client = client; if client_conflict( &client, &contact, - get_altheadb_contract_addr(), - ETH_MINER_KEY - .parse::() - .unwrap() - .to_address(), + db_addr.expect("This should be set"), + get_miner_address(), ) .await { diff --git a/integration_tests/src/setup_utils/rita.rs b/integration_tests/src/setup_utils/rita.rs index f65973620..2287d10b3 100644 --- a/integration_tests/src/setup_utils/rita.rs +++ b/integration_tests/src/setup_utils/rita.rs @@ -6,6 +6,7 @@ use super::namespaces::NamespaceInfo; use super::namespaces::NodeType; use althea_kernel_interface::KernelInterfaceError; use althea_types::Identity; +use clarity::Address; use ipnetwork::IpNetwork; use ipnetwork::Ipv6Network; use log::info; @@ -49,6 +50,7 @@ pub fn thread_spawner( namespaces: NamespaceInfo, client_settings: RitaClientSettings, exit_settings: RitaExitSettingsStruct, + db_addr: Address, ) -> Result { let mut instance_data = InstanceData::default(); let babeld_path = "/var/babeld/babeld/babeld".to_string(); @@ -86,6 +88,7 @@ pub fn thread_spawner( exit_settings.clone(), ns.cost as u64, ns.cost, + db_addr, ); instance_data.exit_identities.push(instance_info); } @@ -192,6 +195,7 @@ pub fn spawn_rita_exit( mut resettings: RitaExitSettingsStruct, exit_fee: u64, local_fee: u32, + db_addr: Address, ) -> Identity { let ns_dup = ns.clone(); let wg_keypath = format!("/var/tmp/{ns}"); @@ -231,6 +235,7 @@ pub fn spawn_rita_exit( resettings.exit_network.subnet = Some(IpNetwork::V6( Ipv6Network::new(instance.subnet, 40).unwrap(), )); + resettings.exit_network.registered_users_contract_addr = db_addr; resettings.network.wg_private_key = Some(instance.wg_priv_key); resettings.network.wg_public_key = Some(instance.wg_pub_key); resettings.network.wg_private_key_path = wg_keypath; diff --git a/integration_tests/src/utils.rs b/integration_tests/src/utils.rs index 1563cd118..418019756 100644 --- a/integration_tests/src/utils.rs +++ b/integration_tests/src/utils.rs @@ -1,8 +1,11 @@ use crate::{ config::{CONFIG_FILE_PATH, EXIT_CONFIG_PATH}, payments_althea::get_althea_evm_priv, - payments_eth::{eth_chain_id, get_miner_address, get_miner_key}, - setup_utils::namespaces::{get_nsfd, Namespace, NamespaceInfo, NodeType, RouteHop}, + payments_eth::{eth_chain_id, get_miner_address, get_miner_key, ONE_ETH, WEB3_TIMEOUT}, + setup_utils::{ + namespaces::{get_nsfd, Namespace, NamespaceInfo, NodeType, RouteHop}, + rita::InstanceData, + }, }; use actix_rt::time::sleep; use actix_rt::System; @@ -135,17 +138,11 @@ pub fn get_eth_node() -> String { format!("http://{}:8545", NODE_IP) } -pub fn get_altheadb_contract_addr() -> Address { - "0xb9b674D720F96995ca033ec347df080d500c2230" - .parse() - .unwrap() -} - pub fn get_test_runner_magic_phone() -> String { "+17040000000".to_string() } -pub async fn deploy_contracts() { +pub async fn deploy_contracts() -> Address { let contact = Contact::new( &get_althea_grpc(), ALTHEA_CONTACT_TIMEOUT, @@ -166,7 +163,15 @@ pub async fn deploy_contracts() { .output() .expect("Failed to deploy contracts!"); - info!("Contract deploy returned {:?}", from_utf8(&res.stdout)); + error!( + "Contract deploy stderr: {}", + from_utf8(&res.stderr).unwrap() + ); + let contract_addr = from_utf8(&res.stdout).unwrap(); + info!("Contract is: {}", contract_addr); + let mut res = contract_addr.split(' ').last().unwrap().to_string(); + res.pop(); + res.parse().unwrap() } /// Test pingability waiting and failing if it is not successful @@ -277,25 +282,35 @@ pub fn test_routes_async(nsinfo: NamespaceInfo, expected: HashMap Duration::from_secs(60) { + panic!("{} does not have internet connectivity", ns.get_name()); + } + error!("Ping failed for {}, trying again", ns.get_name()); + thread::sleep(Duration::from_secs(5)); + } } } } @@ -1073,3 +1088,19 @@ pub async fn register_all_namespaces_to_exit(namespaces: NamespaceInfo) { } } } + +pub async fn populate_routers_eth(rita_identities: InstanceData) { + // Exits need to have funds to request a registered client list, which is needed for proper setup + info!("Topup exits with funds"); + let web3 = Web3::new("http://localhost:8545", WEB3_TIMEOUT); + let mut to_top_up = Vec::new(); + for c in rita_identities.client_identities { + to_top_up.push(c.eth_address); + } + for e in rita_identities.exit_identities { + to_top_up.push(e.eth_address) + } + + info!("Sending 50 eth to all routers"); + send_eth_bulk((ONE_ETH * 50).into(), &to_top_up, &web3).await; +} diff --git a/rita_client/src/exit_manager/mod.rs b/rita_client/src/exit_manager/mod.rs index e584b5831..0817fefcb 100644 --- a/rita_client/src/exit_manager/mod.rs +++ b/rita_client/src/exit_manager/mod.rs @@ -363,7 +363,6 @@ async fn send_exit_setup_request( ident: ExitClientIdentity, ) -> Result { let endpoint = format!("http://[{}]:{}/secure_setup", to.ip(), to.port()); - error!("Trying to hit endpoint: {:?}", endpoint); let ident = encrypt_exit_client_id(&exit_pubkey.into(), ident); diff --git a/rita_client_registration/src/client_db.rs b/rita_client_registration/src/client_db.rs index 86a737641..04e8d4576 100644 --- a/rita_client_registration/src/client_db.rs +++ b/rita_client_registration/src/client_db.rs @@ -129,7 +129,14 @@ pub async fn add_client_to_registered_list( fn parse_identity_abi_internal(byte_chunks: Vec<&[u8]>) -> Result { // 3th entry is the address chunk - let eth_address = match Address::from_slice(&byte_chunks[2][12..]) { + let eth_address = match Address::from_slice(match byte_chunks.get(2) { + Some(a) => a.get(12..).unwrap_or(b""), + None => { + return Err(Web3Error::BadInput(format!( + "Cant get index 2 with byte chunks {byte_chunks:?}" + ))) + } + }) { Ok(a) => a, Err(e) => { error!("Error parse eth address chunk: {}", e); @@ -139,26 +146,56 @@ fn parse_identity_abi_internal(byte_chunks: Vec<&[u8]>) -> Result a, + None => { + return Err(Web3Error::BadInput(format!( + "No index {} in bytes chunks {:?}", + index, byte_chunks + ))) + } + }), + 16, + ) { Ok(a) => a, Err(e) => { error!("Error parsing len of mesh ip: {}", e); return Err(e.into()); } }; + + // Try to get the mesh ip bytes let mut mesh_bytes: Vec = vec![]; loop { index += 1; - let curr_word = byte_chunks[index]; + let curr_word = match byte_chunks.get(index) { + Some(a) => *a, + None => { + return Err(Web3Error::BadInput(format!( + "No index {} in bytes chunks {:?}", + index, byte_chunks + ))) + } + }; if mesh_len >= WORD_SIZE { mesh_bytes.extend(curr_word); mesh_len -= WORD_SIZE } else { - mesh_bytes.extend(&(byte_chunks[index])[..mesh_len]); + mesh_bytes.extend(match (byte_chunks[index]).get(..mesh_len) { + Some(a) => a, + None => { + return Err(Web3Error::BadInput(format!( + "No mesh len {} in index {} bytes chunks {:?}", + mesh_len, index, byte_chunks + ))) + } + }); break; } } + // Parse mesh ip bytes to string let mesh_ip = match String::from_utf8(mesh_bytes) { Ok(a) => a, Err(e) => { @@ -170,6 +207,7 @@ fn parse_identity_abi_internal(byte_chunks: Vec<&[u8]>) -> Result a, Err(e) => { @@ -183,26 +221,56 @@ fn parse_identity_abi_internal(byte_chunks: Vec<&[u8]>) -> Result a, + None => { + return Err(Web3Error::BadInput(format!( + "No index {} in bytes chunks {:?}", + index, byte_chunks + ))) + } + }), + 16, + ) { Ok(a) => a, Err(e) => { error!("Error parsing len of wg key: {}", e); return Err(e.into()); } }; + + // Try to parse wg key bytes let mut wgkey_bytes: Vec = vec![]; loop { index += 1; - let curr_word = byte_chunks[index]; + let curr_word = match byte_chunks.get(index) { + Some(a) => *a, + None => { + return Err(Web3Error::BadInput(format!( + "No index {} in bytes chunks {:?}", + index, byte_chunks + ))) + } + }; if wgkey_len >= WORD_SIZE { wgkey_bytes.extend(curr_word); wgkey_len -= WORD_SIZE } else { - wgkey_bytes.extend(&(byte_chunks[index])[..wgkey_len]); + wgkey_bytes.extend(match (byte_chunks[index]).get(..wgkey_len) { + Some(a) => a, + None => { + return Err(Web3Error::BadInput(format!( + "No wg len {} in index {} bytes chunks {:?}", + wgkey_len, index, byte_chunks + ))) + } + }); break; } } + // Parse wg key bytes to string let wg_key = match String::from_utf8(wgkey_bytes) { Ok(a) => a, Err(e) => { @@ -214,6 +282,7 @@ fn parse_identity_abi_internal(byte_chunks: Vec<&[u8]>) -> Result a, Err(e) => { @@ -247,6 +316,12 @@ pub fn parse_identity_abi(bytes: Vec) -> Result { */ let byte_chunks: Vec<&[u8]> = bytes.chunks(WORD_SIZE).collect(); + // A correct input should have atleast 8 lines, anything below we return an error + if byte_chunks.len() < 8 { + return Err(Web3Error::BadInput(format!( + "Received byte chunks: {byte_chunks:?}" + ))); + } // Remove start offset info parse_identity_abi_internal(byte_chunks[1..].to_vec()) } @@ -295,8 +370,25 @@ pub fn parse_identity_array_abi(bytes: Vec) -> Result, Web3Err let mut ret = vec![]; let byte_chunks: Vec<&[u8]> = bytes.chunks(WORD_SIZE).collect(); + // A valid array with 1 entry has 10 lines. An empty list has 2 lines + if byte_chunks.len() < 10 { + return Err(Web3Error::BadInput(format!( + "Received byte chunks: {byte_chunks:?}" + ))); + } + let mut index = 1; - let arr_len = match usize::from_str_radix(&bytes_to_hex_str(byte_chunks[index]), 16) { + let arr_len = match usize::from_str_radix( + &bytes_to_hex_str(match byte_chunks.get(index) { + Some(a) => a, + None => { + return Err(Web3Error::BadInput(format!( + "Received invalid index {index} byte chunks: {byte_chunks:?}" + ))) + } + }), + 16, + ) { Ok(a) => a, Err(e) => { error!("Cannot parse array len {}", e); @@ -306,21 +398,40 @@ pub fn parse_identity_array_abi(bytes: Vec) -> Result, Web3Err index += 1; for i in 0..arr_len { - let arr_start_offset = - match usize::from_str_radix(&bytes_to_hex_str(byte_chunks[index + i]), 16) { - Ok(a) => a / WORD_SIZE, - Err(e) => { - error!( - "Cannot parse array offset: {:?} with {}", - byte_chunks[index + 1], - e - ); - continue; + let arr_start_offset = match usize::from_str_radix( + &bytes_to_hex_str(match byte_chunks.get(index + i) { + Some(a) => a, + None => { + return Err(Web3Error::BadInput(format!( + "Received invalid index {} byte chunks: {byte_chunks:?}", + index + i + ))) } - }; - + }), + 16, + ) { + Ok(a) => a / WORD_SIZE, + Err(e) => { + error!( + "Cannot parse array offset: {:?} with {}", + byte_chunks[index + 1], + e + ); + continue; + } + }; + + let bytes_to_pass = match byte_chunks.get((index + arr_start_offset)..) { + Some(a) => a, + None => { + return Err(Web3Error::BadInput(format!( + "Received invalid index {} byte chunks: {byte_chunks:?}", + index + arr_start_offset + ))) + } + }; ret.push( - match parse_identity_abi_internal(byte_chunks[(index + arr_start_offset)..].to_vec()) { + match parse_identity_abi_internal((*bytes_to_pass).to_vec()) { Ok(a) => a, Err(e) => { error!( @@ -336,3 +447,155 @@ pub fn parse_identity_array_abi(bytes: Vec) -> Result, Web3Err Ok(ret) } + +#[test] +fn test_parse_abi() { + use clarity::utils::hex_str_to_bytes; + // test parsing an abi struct with various input types + let id = Identity { + mesh_ip: "fd00::1337".parse().unwrap(), + eth_address: "0x02ad6b480DFeD806C63a0839C6f1f3136c5fD515" + .parse() + .unwrap(), + wg_public_key: "sPtNGQbyPpCsqSKD6PbnflB1lIUCd259Vhd0mJfJeGo=" + .parse() + .unwrap(), + nickname: None, + }; + let bytes = "\ + 0000000000000000000000000000000000000000000000000000000000000020\ + 0000000000000000000000000000000000000000000000000000000000000060\ + 00000000000000000000000000000000000000000000000000000000000000a0\ + 00000000000000000000000002ad6b480dfed806c63a0839c6f1f3136c5fd515\ + 000000000000000000000000000000000000000000000000000000000000000a\ + 666430303a3a3133333700000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000002c\ + 7350744e475162795070437371534b443650626e666c42316c49554364323539\ + 566864306d4a664a65476f3d0000000000000000000000000000000000000000"; + + let bytes = hex_str_to_bytes(bytes).unwrap(); + assert_eq!(parse_identity_abi(bytes).unwrap(), id); + + // invalid input + let bytes = "\ + 0000000000000000000000000000000000000000000000000000000000000020\ + 0000000000000000000000000000000000000000000000000000000000000060\ + 00000000000000000000000000000000000000000000000000000000000000a0\ + 00000000000000000000000002ad6b480dfed806c63a0839c6f1f3136c5fd515\ + 000000000000000000000000000000000000000000000000000000000000000a\ + 666430303a3a3133333700000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000002c"; + + let bytes = hex_str_to_bytes(bytes).unwrap(); + assert!(parse_identity_abi(bytes).is_err()); + + // invalid input + let bytes = "\ + 0000000000000000000000000000000000000000000000000000000000000020\ + 0000000000000000000000000000000000000000000000000000000000000060\ + 0000000000000000000000000000000000000000000000000000000000000060\ + 0000000000000000000000000000000000000000000000000000000000000060\ + 00000000000000000000000000000000000000000000000000000000000000a0\ + 00000000000000000000000002ad6b480dfed806c63a0839c6f1f3136c5fd515\ + 000000000000000000000000000000000000000000000000000000000000000a\ + 666430303a3a3133333700000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000002c"; + + let bytes = hex_str_to_bytes(bytes).unwrap(); + assert!(parse_identity_abi(bytes).is_err()); +} + +#[test] +fn test_parse_abi_array() { + use clarity::utils::hex_str_to_bytes; + // empty string + let bytes = ""; + let bytes = hex_str_to_bytes(bytes).unwrap(); + assert!(parse_identity_array_abi(bytes).is_err()); + + // valid entry + let bytes = "\ + 0000000000000000000000000000000000000000000000000000000000000020\ + 0000000000000000000000000000000000000000000000000000000000000002\ + 0000000000000000000000000000000000000000000000000000000000000040\ + 0000000000000000000000000000000000000000000000000000000000000140\ + 0000000000000000000000000000000000000000000000000000000000000060\ + 00000000000000000000000000000000000000000000000000000000000000a0\ + 00000000000000000000000002ad6b480dfed806c63a0839c6f1f3136c5fd515\ + 000000000000000000000000000000000000000000000000000000000000000a\ + 666430303a3a3133333700000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000002c\ + 7350744e475162795070437371534b443650626e666c42316c49554364323539\ + 566864306d4a664a65476f3d0000000000000000000000000000000000000000\ + 0000000000000000000000000000000000000000000000000000000000000060\ + 00000000000000000000000000000000000000000000000000000000000000a0\ + 0000000000000000000000001994a73f79f9648d4a8064d9c0f221fb1007fd2f\ + 000000000000000000000000000000000000000000000000000000000000000f\ + 666430303a3a313434373a313434370000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000002c\ + 5968796a2b434b5a6279454b65612f39686449466a65393879633543756b7431\ + 50627130715742344171773d0000000000000000000000000000000000000000"; + + let bytes = hex_str_to_bytes(bytes).unwrap(); + assert!(parse_identity_array_abi(bytes.clone()).is_ok()); + assert!(parse_identity_array_abi(bytes).unwrap().len() == 2); + + // Second entry invalid + let bytes = "\ + 0000000000000000000000000000000000000000000000000000000000000020\ + 0000000000000000000000000000000000000000000000000000000000000002\ + 0000000000000000000000000000000000000000000000000000000000000040\ + 0000000000000000000000000000000000000000000000000000000000000140\ + 0000000000000000000000000000000000000000000000000000000000000060\ + 00000000000000000000000000000000000000000000000000000000000000a0\ + 00000000000000000000000002ad6b480dfed806c63a0839c6f1f3136c5fd515\ + 000000000000000000000000000000000000000000000000000000000000000a\ + 666430303a3a3133333700000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000002c\ + 7350744e475162795070437371534b443650626e666c42316c49554364323539\ + 566864306d4a664a65476f3d0000000000000000000000000000000000000000\ + 0000000000000000000000000000000000000000000000000000000000000060\ + 000000000000000000000000000000000000000000000000000000000000002c\ + 5968796a2b434b5a6279454b65612f39686449466a65393879633543756b7431\ + 50627130715742344171773d0000000000000000000000000000000000000000"; + + let bytes = hex_str_to_bytes(bytes).unwrap(); + assert!(parse_identity_array_abi(bytes.clone()).is_ok()); + assert!(parse_identity_array_abi(bytes).unwrap().len() == 1); + + // No valid entries + let bytes = "\ + 0000000000000000000000000000000000000000000000000000000000000020\ + 0000000000000000000000000000000000000000000000000000000000000002\ + 0000000000000000000000000000000000000000000000000000000000000040\ + 0000000000000000000000000000000000000000000000000000000000000140\ + 0000000000000000000000000000000000000000000000000000000000000060\ + 666430303a3a3133333700000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000002c\ + 5968796a2b434b5a6279454b65612f39686449466a65393879633543756b7431\ + 5968796a2b434b5a6279454b65612f39686449466a65393879633543756b7431\ + 5968796a2b434b5a6279454b65612f39686449466a65393879633543756b7431\ + 5968796a2b434b5a6279454b65612f39686449466a65393879633543756b7431\ + 50627130715742344171773d0000000000000000000000000000000000000000"; + + let bytes = hex_str_to_bytes(bytes).unwrap(); + assert!(parse_identity_array_abi(bytes.clone()).unwrap().is_empty()); + + // One valid entry + let bytes = "\ + 0000000000000000000000000000000000000000000000000000000000000020\ + 0000000000000000000000000000000000000000000000000000000000000001\ + 0000000000000000000000000000000000000000000000000000000000000020\ + 0000000000000000000000000000000000000000000000000000000000000060\ + 00000000000000000000000000000000000000000000000000000000000000a0\ + 00000000000000000000000002ad6b480dfed806c63a0839c6f1f3136c5fd515\ + 000000000000000000000000000000000000000000000000000000000000000a\ + 666430303a3a3133333700000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000002c\ + 7350744e475162795070437371534b443650626e666c42316c49554364323539\ + 566864306d4a664a65476f3d0000000000000000000000000000000000000000"; + + let bytes = hex_str_to_bytes(bytes).unwrap(); + assert!(parse_identity_array_abi(bytes.clone()).is_ok()); + assert!(parse_identity_array_abi(bytes).unwrap().len() == 1); +} diff --git a/rita_client_registration/src/lib.rs b/rita_client_registration/src/lib.rs index d80af01cd..7bb43d80e 100644 --- a/rita_client_registration/src/lib.rs +++ b/rita_client_registration/src/lib.rs @@ -2,25 +2,15 @@ use std::{ collections::{HashMap, HashSet}, sync::{Arc, RwLock}, - thread, - time::{Duration, Instant}, + time::Duration, }; -use actix::System; use althea_types::{ExitClientIdentity, Identity, WgKey}; -use clarity::{ - abi::{encode_call, AbiToken}, - Address, PrivateKey, Uint256, -}; -use futures::future::join_all; +use clarity::Address; use phonenumber::PhoneNumber; use serde::{Deserialize, Serialize}; use tokio::join; -use web30::{ - client::Web3, - jsonrpc::error::Web3Error, - types::{SendTxOption, TransactionResponse}, -}; +use web30::client::Web3; use crate::client_db::{ get_registered_client_using_ethkey, get_registered_client_using_meship, @@ -33,6 +23,7 @@ extern crate log; 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 @@ -158,13 +149,7 @@ async fn client_exists( .await; match c_id { Ok(a) => client.global == a, - Err(e) => { - warn!( - "Error retrieving an identity with wg key {} with {}", - client.global.wg_public_key, e - ); - false - } + Err(_) => false, } } @@ -178,10 +163,6 @@ pub async fn handle_sms_registration( "Handling phone registration for {}", client.global.wg_public_key ); - error!( - "Handling phone registration for {}", - client.global.wg_public_key - ); // Get magic phone number let magic_phone_number = magic_number; @@ -333,146 +314,3 @@ async fn send_text(number: String, api_key: String) -> Result<(), TextApiError> } } } - -pub fn register_client_batch_loop( - web3_url: String, - contract_addr: Address, - our_private_key: PrivateKey, -) { - let mut last_restart = Instant::now(); - thread::spawn(move || { - // this will always be an error, so it's really just a loop statement - // with some fancy destructuring - while let Err(e) = { - let web3 = web3_url.clone(); - thread::spawn(move || { - // Our Exit state variabl - let runner = System::new(); - - runner.block_on(async move { - loop { - let start = Instant::now(); - info!("Registration Loop tick"); - - let reg_clients = get_reg_batch(); - let contact = Web3::new(&web3, WEB3_TIMEOUT); - let mut nonce_retries = 0; - let mut nonce; - loop { - match contact - .eth_get_transaction_count(our_private_key.to_address()) - .await - { - Ok(a) => { - nonce = a; - break; - } - Err(e) => { - error!("Unable to get nonce to register routers: {}", e); - if nonce_retries > 10 { - error!("Cant register routers, panicing!"); - let sys = System::current(); - sys.stop(); - panic!( - "{}", - format!( - "Unable to get nonce to register routers: {}", - e - ) - ); - } - nonce_retries += 1; - thread::sleep(Duration::from_secs(5)); - continue; - } - } - } - - let mut batch = vec![]; - for id in reg_clients { - error!("This batch has {}", id.mesh_ip); - match contact - .send_transaction( - contract_addr, - match encode_call( - "add_registered_user((string,string,address))", - &[AbiToken::Struct(vec![ - AbiToken::String(id.mesh_ip.to_string()), - AbiToken::String(id.wg_public_key.to_string()), - AbiToken::Address(id.eth_address), - ])], - ) { - Ok(a) => a, - Err(e) => { - error!( - "REGISTRATION ERROR: Why cant we encode this call? {}", - e - ); - continue; - } - }, - 0u32.into(), - our_private_key, - vec![SendTxOption::Nonce(nonce)], - ) - .await - { - Ok(tx_id) => { - //increment nonce for next tx - nonce += 1u64.into(); - remove_client_from_reg_batch(id); - batch.push(tx_id); - } - Err(e) => { - error!( - "Failed registration for {} with {}", - id.wg_public_key, e - ); - } - } - } - - // Join on txs - let res = wait_for_txids(batch, &contact).await; - trace!("Received Transactions: {:?}", res); - - info!("Registration loop elapsed in = {:?}", start.elapsed()); - if start.elapsed() < REGISTRATION_LOOP_SPEED { - info!( - "Registration Loop sleeping for {:?}", - REGISTRATION_LOOP_SPEED - start.elapsed() - ); - thread::sleep(REGISTRATION_LOOP_SPEED - start.elapsed()); - } - info!("Registration loop sleeping Done!"); - } - }); - }) - .join() - } { - error!( - "Rita client Exit Manager loop thread paniced! Respawning {:?}", - e - ); - if Instant::now() - last_restart < Duration::from_secs(60) { - error!("Restarting too quickly, leaving it to auto rescue!"); - let sys = System::current(); - sys.stop_with_code(121); - } - last_restart = Instant::now(); - } - }); -} - -/// utility function that waits for a large number of txids to enter a block -async fn wait_for_txids( - txids: Vec, - web3: &Web3, -) -> Vec> { - let mut wait_for_txid = Vec::new(); - for txid in txids { - let wait = web3.wait_for_transaction(txid, TX_TIMEOUT, None); - wait_for_txid.push(wait); - } - join_all(wait_for_txid).await -} diff --git a/rita_client_registration/src/register_client_batch_loop.rs b/rita_client_registration/src/register_client_batch_loop.rs new file mode 100644 index 000000000..78fd77dba --- /dev/null +++ b/rita_client_registration/src/register_client_batch_loop.rs @@ -0,0 +1,164 @@ +use std::{ + thread, + time::{Duration, Instant}, +}; + +use actix::System; +use clarity::{ + abi::{encode_call, AbiToken}, + Address, PrivateKey, Uint256, +}; +use futures::future::join_all; +use web30::{ + client::Web3, + jsonrpc::error::Web3Error, + types::{SendTxOption, TransactionResponse}, +}; + +use crate::{ + get_reg_batch, remove_client_from_reg_batch, REGISTRATION_LOOP_SPEED, TX_TIMEOUT, WEB3_TIMEOUT, +}; + +/// This loop pull a queue of routers to be registered, batches them and setting up their +/// nonces, and sends a registrations request for all of them +pub fn register_client_batch_loop( + web3_url: String, + contract_addr: Address, + our_private_key: PrivateKey, +) { + let mut last_restart = Instant::now(); + thread::spawn(move || { + // this will always be an error, so it's really just a loop statement + // with some fancy destructuring + while let Err(e) = { + let web3 = web3_url.clone(); + thread::spawn(move || { + // Our Exit state variabl + let runner = System::new(); + + runner.block_on(async move { + loop { + let start = Instant::now(); + info!("Registration Loop tick"); + + let reg_clients = get_reg_batch(); + let contact = Web3::new(&web3, WEB3_TIMEOUT); + let mut nonce_retries = 0; + let mut nonce; + loop { + match contact + .eth_get_transaction_count(our_private_key.to_address()) + .await + { + Ok(a) => { + nonce = a; + break; + } + Err(e) => { + error!("Unable to get nonce to register routers: {}", e); + if nonce_retries > 10 { + error!("Cant register routers, panicing!"); + let sys = System::current(); + sys.stop(); + panic!( + "{}", + format!( + "Unable to get nonce to register routers: {}", + e + ) + ); + } + nonce_retries += 1; + thread::sleep(Duration::from_secs(5)); + continue; + } + } + } + + let mut batch = vec![]; + for id in reg_clients { + match contact + .send_transaction( + contract_addr, + match encode_call( + "add_registered_user((string,string,address))", + &[AbiToken::Struct(vec![ + AbiToken::String(id.mesh_ip.to_string()), + AbiToken::String(id.wg_public_key.to_string()), + AbiToken::Address(id.eth_address), + ])], + ) { + Ok(a) => a, + Err(e) => { + error!( + "REGISTRATION ERROR: Why cant we encode this call? {}", + e + ); + continue; + } + }, + 0u32.into(), + our_private_key, + vec![SendTxOption::Nonce(nonce)], + ) + .await + { + Ok(tx_id) => { + //increment nonce for next tx + nonce += 1u64.into(); + remove_client_from_reg_batch(id); + batch.push(tx_id); + } + Err(e) => { + error!( + "Failed registration for {} with {}", + id.wg_public_key, e + ); + } + } + } + + // Join on txs + let res = wait_for_txids(batch, &contact).await; + trace!("Received Transactions: {:?}", res); + + info!("Registration loop elapsed in = {:?}", start.elapsed()); + if start.elapsed() < REGISTRATION_LOOP_SPEED { + info!( + "Registration Loop sleeping for {:?}", + REGISTRATION_LOOP_SPEED - start.elapsed() + ); + thread::sleep(REGISTRATION_LOOP_SPEED - start.elapsed()); + } + info!("Registration loop sleeping Done!"); + } + }); + }) + .join() + } { + error!( + "Rita client Exit Manager loop thread paniced! Respawning {:?}", + e + ); + if Instant::now() - last_restart < Duration::from_secs(60) { + error!("Restarting too quickly, leaving it to auto rescue!"); + let sys = System::current(); + sys.stop_with_code(121); + } + last_restart = Instant::now(); + } + }); +} + +/// utility function that waits for a large number of txids to enter a block +async fn wait_for_txids( + txids: Vec, + web3: &Web3, +) -> Vec> { + let mut wait_for_txid = Vec::new(); + for txid in txids { + let wait = web3.wait_for_transaction(txid, TX_TIMEOUT, None); + wait_for_txid.push(wait); + } + join_all(wait_for_txid).await +} diff --git a/rita_exit/src/database/database_tools.rs b/rita_exit/src/database/database_tools.rs deleted file mode 100644 index a007090cf..000000000 --- a/rita_exit/src/database/database_tools.rs +++ /dev/null @@ -1,116 +0,0 @@ -use ipnetwork::{IpNetwork, Ipv6Network}; - -use crate::RitaExitError; -use std::net::{IpAddr, Ipv6Addr}; - -// Default Subnet size assigned to each client -pub const DEFAULT_CLIENT_SUBNET_SIZE: u8 = 56; - -/// Take an index i, a larger subnet and a smaller subnet length and generate the ith smaller subnet in the larger subnet -/// For instance, if our larger subnet is fd00::1330/120, smaller sub len is 124, and index is 1, our generated subnet would be fd00::1310/124 -pub fn generate_iterative_client_subnet( - exit_sub: IpNetwork, - ind: u64, - subprefix: u8, -) -> Result> { - let net; - - // Covert the subnet's ip address into a u128 integer to allow for easy iterative - // addition operations. To this u128, we add (interative_index * client_subnet_size) - // and convert this result into an ipv6 addr. This is the starting ip in the client subnet - // - // For example, if we have exit subnet: fbad::1000/120, client subnet size is 124, index is 1 - // we do (fbad::1000).to_int() + (16 * 1) = fbad::1010/124 is the client subnet - let net_as_int: u128 = if let IpAddr::V6(addr) = exit_sub.network() { - net = Ipv6Network::new(addr, subprefix).unwrap(); - addr.into() - } else { - return Err(Box::new(RitaExitError::MiscStringError( - "Exit subnet expected to be ipv6!!".to_string(), - ))); - }; - - if subprefix < exit_sub.prefix() { - return Err(Box::new(RitaExitError::MiscStringError( - "Client subnet larger than exit subnet".to_string(), - ))); - } - - // This bitshifting is the total number of client subnets available. We are checking that our iterative index - // is lower than this number. For example, exit subnet: fd00:1000/120, client subnet /124, number of subnets will be - // 2^(124 - 120) => 2^4 => 16 - if ind < (1 << (subprefix - exit_sub.prefix())) { - let ret = net_as_int + (ind as u128 * net.size()); - let v6addr = Ipv6Addr::from(ret); - let ret = IpNetwork::from(match Ipv6Network::new(v6addr, subprefix) { - Ok(a) => a, - Err(e) => { - return Err(Box::new(RitaExitError::MiscStringError(format!( - "Unable to parse a valid client subnet: {e:?}" - )))) - } - }); - - Ok(ret) - } else { - error!( - "Our index is larger than available subnets, either error in logic or no more subnets" - ); - Err(Box::new(RitaExitError::MiscStringError( - "Index larger than available subnets".to_string(), - ))) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Test iterative subnet generation - #[test] - fn test_generate_iterative_subnet() { - // Complex subnet example - let net: IpNetwork = "2602:FBAD::/40".parse().unwrap(); - let ret = generate_iterative_client_subnet(net, 0, 64); - assert_eq!("2602:FBAD::/64".parse::().unwrap(), ret.unwrap()); - - let net: IpNetwork = "2602:FBAD::/40".parse().unwrap(); - let ret = generate_iterative_client_subnet(net, 1, 64); - assert_eq!( - "2602:FBAD:0:1::/64".parse::().unwrap(), - ret.unwrap() - ); - - let net: IpNetwork = "2602:FBAD::/40".parse().unwrap(); - let ret = generate_iterative_client_subnet(net, 50, 64); - assert_eq!( - "2602:FBAD:0:32::/64".parse::().unwrap(), - ret.unwrap() - ); - - let net: IpNetwork = "2602:FBAD::/40".parse().unwrap(); - let ret = generate_iterative_client_subnet(net, 2_u64.pow(24), 64); - assert!(ret.is_err()); - - let net: IpNetwork = "2602:FBAD::/40".parse().unwrap(); - let ret = generate_iterative_client_subnet(net, 0, 30); - assert!(ret.is_err()); - - // Simple subnet example - let net: IpNetwork = "fd00::1337/120".parse().unwrap(); - let ret = generate_iterative_client_subnet(net, 0, 124); - assert_eq!("fd00::1300/124".parse::().unwrap(), ret.unwrap()); - - let net: IpNetwork = "fd00::1337/120".parse().unwrap(); - let ret = generate_iterative_client_subnet(net, 2, 124); - assert_eq!("fd00::1320/124".parse::().unwrap(), ret.unwrap()); - - let net: IpNetwork = "fd00::1337/120".parse().unwrap(); - let ret = generate_iterative_client_subnet(net, 15, 124); - assert_eq!("fd00::13f0/124".parse::().unwrap(), ret.unwrap()); - - let net: IpNetwork = "fd00::1337/120".parse().unwrap(); - let ret = generate_iterative_client_subnet(net, 16, 124); - assert!(ret.is_err()); - } -} diff --git a/rita_exit/src/database/geoip.rs b/rita_exit/src/database/geoip.rs index 22960164a..45ff3f917 100644 --- a/rita_exit/src/database/geoip.rs +++ b/rita_exit/src/database/geoip.rs @@ -5,16 +5,11 @@ use rita_common::utils::ip_increment::is_unicast_link_local; use rita_common::KI; use std::collections::HashMap; use std::net::IpAddr; -use std::sync::{Arc, RwLock}; use std::time::Duration; +use crate::database::RITA_EXIT_STATE; use crate::RitaExitError; -lazy_static! { - static ref GEOIP_CACHE: Arc>> = - Arc::new(RwLock::new(HashMap::new())); -} - /// gets the gateway ip for a given mesh IP pub fn get_gateway_ip_single(mesh_ip: IpAddr) -> Result> { let babel_port = settings::get_rita_exit().network.babel_port; @@ -174,9 +169,10 @@ pub fn get_country(ip: IpAddr) -> Result> { // we have to turn this option into a string in order to avoid // the borrow checker trying to keep this lock open for a long period - let cache_result = GEOIP_CACHE + let cache_result = RITA_EXIT_STATE .read() .unwrap() + .geoip_cache .get(&ip) .map(|val| val.to_string()); @@ -201,7 +197,11 @@ pub fn get_country(ip: IpAddr) -> Result> { let value: GeoIpRet = res; let code = value.country.iso_code; trace!("Adding GeoIP value {:?} to cache", code); - GEOIP_CACHE.write().unwrap().insert(ip, code.clone()); + RITA_EXIT_STATE + .write() + .unwrap() + .geoip_cache + .insert(ip, code.clone()); trace!("Added to cache, returning"); Ok(code) } else { diff --git a/rita_exit/src/database/struct_tools.rs b/rita_exit/src/database/in_memory_database.rs similarity index 74% rename from rita_exit/src/database/struct_tools.rs rename to rita_exit/src/database/in_memory_database.rs index 765ce6d80..74f549404 100644 --- a/rita_exit/src/database/struct_tools.rs +++ b/rita_exit/src/database/in_memory_database.rs @@ -1,15 +1,16 @@ use althea_kernel_interface::ExitClient; use althea_types::{Identity, WgKey}; -use ipnetwork::{IpNetwork, Ipv4Network}; +use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network}; use std::collections::hash_map::DefaultHasher; use std::collections::{HashMap, HashSet}; use std::convert::TryInto; use std::fmt::Write; use std::hash::{Hash, Hasher}; -use std::net::{IpAddr, Ipv4Addr}; -use std::sync::{Arc, RwLock}; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; -use crate::{generate_iterative_client_subnet, RitaExitError, DEFAULT_CLIENT_SUBNET_SIZE}; +use crate::RitaExitError; + +use super::RITA_EXIT_STATE; /// Wg exit port on client side pub const CLIENT_WG_PORT: u16 = 59999; @@ -17,12 +18,8 @@ pub const CLIENT_WG_PORT: u16 = 59999; /// Max number of time we try to generate a valid ip addr before returning an eror pub const MAX_IP_RETRIES: u8 = 10; -lazy_static! { - /// Keep track of ip addrs assigned to clients and ensure collisions dont happen. In worst case - /// the exit restarts and loses all this data in which case those client they had collision may get new - /// ip addrs and would need to setup wg exit tunnel again - static ref IP_ASSIGNMENT_MAP: Arc> = Arc::new(RwLock::new(IpAssignmentMap::default())); -} +// Default Subnet size assigned to each client +pub const DEFAULT_CLIENT_SUBNET_SIZE: u8 = 56; #[derive(Clone, Debug, Default)] pub struct IpAssignmentMap { @@ -32,33 +29,97 @@ pub struct IpAssignmentMap { // Lazy static setters/getters pub fn get_ipv6_assignments() -> HashMap { - IP_ASSIGNMENT_MAP.read().unwrap().ipv6_assignments.clone() + RITA_EXIT_STATE + .read() + .unwrap() + .ip_assignment_map + .ipv6_assignments + .clone() } pub fn get_internal_ip_assignments() -> HashMap { - IP_ASSIGNMENT_MAP + RITA_EXIT_STATE .read() .unwrap() + .ip_assignment_map .internal_ip_assignments .clone() } pub fn add_new_ipv6_assignment(addr: IpAddr, key: WgKey) { - IP_ASSIGNMENT_MAP + RITA_EXIT_STATE .write() .unwrap() + .ip_assignment_map .ipv6_assignments .insert(addr, key); } pub fn add_new_internal_ip_assignement(addr: IpAddr, key: WgKey) { - IP_ASSIGNMENT_MAP + RITA_EXIT_STATE .write() .unwrap() + .ip_assignment_map .internal_ip_assignments .insert(addr, key); } +/// Take an index i, a larger subnet and a smaller subnet length and generate the ith smaller subnet in the larger subnet +/// For instance, if our larger subnet is fd00::1330/120, smaller sub len is 124, and index is 1, our generated subnet would be fd00::1310/124 +pub fn generate_iterative_client_subnet( + exit_sub: IpNetwork, + ind: u64, + subprefix: u8, +) -> Result> { + let net; + + // Covert the subnet's ip address into a u128 integer to allow for easy iterative + // addition operations. To this u128, we add (interative_index * client_subnet_size) + // and convert this result into an ipv6 addr. This is the starting ip in the client subnet + // + // For example, if we have exit subnet: fbad::1000/120, client subnet size is 124, index is 1 + // we do (fbad::1000).to_int() + (16 * 1) = fbad::1010/124 is the client subnet + let net_as_int: u128 = if let IpAddr::V6(addr) = exit_sub.network() { + net = Ipv6Network::new(addr, subprefix).unwrap(); + addr.into() + } else { + return Err(Box::new(RitaExitError::MiscStringError( + "Exit subnet expected to be ipv6!!".to_string(), + ))); + }; + + if subprefix < exit_sub.prefix() { + return Err(Box::new(RitaExitError::MiscStringError( + "Client subnet larger than exit subnet".to_string(), + ))); + } + + // This bitshifting is the total number of client subnets available. We are checking that our iterative index + // is lower than this number. For example, exit subnet: fd00:1000/120, client subnet /124, number of subnets will be + // 2^(124 - 120) => 2^4 => 16 + if ind < (1 << (subprefix - exit_sub.prefix())) { + let ret = net_as_int + (ind as u128 * net.size()); + let v6addr = Ipv6Addr::from(ret); + let ret = IpNetwork::from(match Ipv6Network::new(v6addr, subprefix) { + Ok(a) => a, + Err(e) => { + return Err(Box::new(RitaExitError::MiscStringError(format!( + "Unable to parse a valid client subnet: {e:?}" + )))) + } + }); + + Ok(ret) + } else { + error!( + "Our index is larger than available subnets, either error in logic or no more subnets" + ); + Err(Box::new(RitaExitError::MiscStringError( + "Index larger than available subnets".to_string(), + ))) + } +} + /// Given a client identity, get the clients ipv6 addr using the wgkey as a generative seed pub fn get_client_ipv6( their_record: Identity, @@ -255,9 +316,11 @@ pub fn display_hashset(input: &HashSet) -> String { #[cfg(test)] mod tests { use althea_types::Identity; + use ipnetwork::IpNetwork; - use crate::database::struct_tools::{ - get_client_internal_ip, get_internal_ip_assignments, get_ipv6_assignments, + use crate::database::in_memory_database::{ + generate_iterative_client_subnet, get_client_internal_ip, get_internal_ip_assignments, + get_ipv6_assignments, }; use super::{get_client_ipv6, hash_wgkey}; @@ -477,4 +540,51 @@ mod tests { println!("Internal ip client 2: {}", ip); } + + /// Test iterative subnet generation + #[test] + fn test_generate_iterative_subnet() { + // Complex subnet example + let net: IpNetwork = "2602:FBAD::/40".parse().unwrap(); + let ret = generate_iterative_client_subnet(net, 0, 64); + assert_eq!("2602:FBAD::/64".parse::().unwrap(), ret.unwrap()); + + let net: IpNetwork = "2602:FBAD::/40".parse().unwrap(); + let ret = generate_iterative_client_subnet(net, 1, 64); + assert_eq!( + "2602:FBAD:0:1::/64".parse::().unwrap(), + ret.unwrap() + ); + + let net: IpNetwork = "2602:FBAD::/40".parse().unwrap(); + let ret = generate_iterative_client_subnet(net, 50, 64); + assert_eq!( + "2602:FBAD:0:32::/64".parse::().unwrap(), + ret.unwrap() + ); + + let net: IpNetwork = "2602:FBAD::/40".parse().unwrap(); + let ret = generate_iterative_client_subnet(net, 2_u64.pow(24), 64); + assert!(ret.is_err()); + + let net: IpNetwork = "2602:FBAD::/40".parse().unwrap(); + let ret = generate_iterative_client_subnet(net, 0, 30); + assert!(ret.is_err()); + + // Simple subnet example + let net: IpNetwork = "fd00::1337/120".parse().unwrap(); + let ret = generate_iterative_client_subnet(net, 0, 124); + assert_eq!("fd00::1300/124".parse::().unwrap(), ret.unwrap()); + + let net: IpNetwork = "fd00::1337/120".parse().unwrap(); + let ret = generate_iterative_client_subnet(net, 2, 124); + assert_eq!("fd00::1320/124".parse::().unwrap(), ret.unwrap()); + + let net: IpNetwork = "fd00::1337/120".parse().unwrap(); + let ret = generate_iterative_client_subnet(net, 15, 124); + assert_eq!("fd00::13f0/124".parse::().unwrap(), ret.unwrap()); + let net: IpNetwork = "fd00::1337/120".parse().unwrap(); + let ret = generate_iterative_client_subnet(net, 16, 124); + assert!(ret.is_err()); + } } diff --git a/rita_exit/src/database/mod.rs b/rita_exit/src/database/mod.rs index ae06e6502..8eec34729 100644 --- a/rita_exit/src/database/mod.rs +++ b/rita_exit/src/database/mod.rs @@ -4,15 +4,16 @@ use crate::database::geoip::get_gateway_ip_bulk; use crate::database::geoip::get_gateway_ip_single; use crate::database::geoip::verify_ip; -use crate::database::struct_tools::display_hashset; -use crate::database::struct_tools::get_client_internal_ip; -use crate::database::struct_tools::get_client_ipv6; -use crate::database::struct_tools::to_exit_client; +use crate::database::in_memory_database::display_hashset; +use crate::database::in_memory_database::get_client_internal_ip; +use crate::database::in_memory_database::get_client_ipv6; +use crate::database::in_memory_database::to_exit_client; +use crate::database::in_memory_database::DEFAULT_CLIENT_SUBNET_SIZE; use crate::rita_loop::EXIT_INTERFACE; use crate::rita_loop::EXIT_LOOP_TIMEOUT; use crate::rita_loop::LEGACY_INTERFACE; +use crate::IpAssignmentMap; use crate::RitaExitError; -use crate::DEFAULT_CLIENT_SUBNET_SIZE; use althea_kernel_interface::ExitClient; use althea_types::Identity; use althea_types::WgKey; @@ -28,14 +29,28 @@ use settings::get_rita_exit; use std::collections::HashMap; use std::collections::HashSet; use std::net::IpAddr; +use std::sync::Arc; +use std::sync::RwLock; use std::time::Duration; use std::time::Instant; use std::time::SystemTime; use web30::client::Web3; -pub mod database_tools; pub mod geoip; -pub mod struct_tools; +pub mod in_memory_database; + +#[derive(Clone, Debug, Default)] +pub struct RitaExitState { + ip_assignment_map: IpAssignmentMap, + geoip_cache: HashMap, +} + +lazy_static! { + /// Keep track of geoip information as well as ip addrs assigned to clients and ensure collisions dont happen. In worst case + /// the exit restarts and loses all this data in which case those client they had collision may get new + /// ip addrs and would need to setup wg exit tunnel again + static ref RITA_EXIT_STATE: Arc> = Arc::new(RwLock::new(RitaExitState::default())); +} /// one day in seconds pub const ONE_DAY: i64 = 86400; @@ -129,10 +144,6 @@ pub async fn forward_client_signup_request(exit_client: ExitClientIdentity) -> E "About to request client {} registration with {}", exit_client.global, url ); - error!( - "About to request client {} registration with {}", - exit_client.global, url - ); let client = awc::Client::default(); let response = client @@ -318,7 +329,6 @@ pub fn setup_clients( ); } } - //key_to_client_map.insert(c.wg_public_key, c.clone()); } for c in geoip_blacklist.iter() { diff --git a/rita_exit/src/lib.rs b/rita_exit/src/lib.rs index d03973ac7..f5bc67d3a 100644 --- a/rita_exit/src/lib.rs +++ b/rita_exit/src/lib.rs @@ -20,9 +20,8 @@ use actix_web_async::App; use actix_web_async::HttpServer; pub use error::RitaExitError; -pub use crate::database::database_tools::*; -pub use crate::database::database_tools::*; pub use crate::database::geoip::*; +pub use crate::database::in_memory_database::*; use rita_common::dashboard::babel::*; use rita_common::dashboard::debts::*; use rita_common::dashboard::development::*; diff --git a/rita_exit/src/rita_loop/mod.rs b/rita_exit/src/rita_loop/mod.rs index 5ee1a9284..9912ddba2 100644 --- a/rita_exit/src/rita_loop/mod.rs +++ b/rita_exit/src/rita_loop/mod.rs @@ -157,8 +157,6 @@ async fn rita_exit_loop(rita_exit_cache: RitaExitCache, usage_history: ExitLock) }; } - error!("received reg clients: {:?}", reg_clients_list); - info!( "Finished Rita get clients, got {:?} clients in {}ms", reg_clients_list.len(), diff --git a/scripts/integration_tests/container_scripts/all-up-test-internal.sh b/scripts/integration_tests/container_scripts/all-up-test-internal.sh index 37e0282b6..1918a0c0a 100644 --- a/scripts/integration_tests/container_scripts/all-up-test-internal.sh +++ b/scripts/integration_tests/container_scripts/all-up-test-internal.sh @@ -10,4 +10,10 @@ bash /althea_rs/scripts/integration_tests/container_scripts/run-testnet.sh $NODE sleep 10 +pushd /althea_rs/solidity + +npm install + +npm run typechain + bash /althea_rs/scripts/integration_tests/container_scripts/integration-tests.sh $TEST_TYPE \ No newline at end of file diff --git a/settings/example_exit.toml b/settings/example_exit.toml index ae636e0cd..3615e4ece 100644 --- a/settings/example_exit.toml +++ b/settings/example_exit.toml @@ -44,6 +44,7 @@ subnet = "fd00::1337/40" entry_timeout = 7776000 wg_public_key = "H/ABwzXk834OwGYU8CZGfFxNZOd+BAJEaVDHiEiWWhU=" wg_private_key = "ALxcZm2r58gY0sB4vIfnjShc86qBoVK3f32H9VrwqWU=" +registered_users_contract_addr = "0x5AeE3Dff733F56cFe7E5390B9cC3A46a90cA1CfA" wg_private_key_path = "/tmp/exit-priv" pass = "Some pass here" diff --git a/settings/test_exit.toml b/settings/test_exit.toml index 476684050..761b9d678 100644 --- a/settings/test_exit.toml +++ b/settings/test_exit.toml @@ -41,6 +41,7 @@ entry_timeout = 7776000 wg_public_key = "H/ABwzXk834OwGYU8CZGfFxNZOd+BAJEaVDHiEiWWhU=" wg_private_key = "ALxcZm2r58gY0sB4vIfnjShc86qBoVK3f32H9VrwqWU=" wg_private_key_path = "/tmp/exit-priv" +registered_users_contract_addr = "0x5AeE3Dff733F56cFe7E5390B9cC3A46a90cA1CfA" pass = "Some pass here" [[exit_network.cluster_exits]] diff --git a/solidity/contract-deployer.ts b/solidity/contract-deployer.ts index e065ca6ca..6dd191ca4 100644 --- a/solidity/contract-deployer.ts +++ b/solidity/contract-deployer.ts @@ -1,9 +1,9 @@ // Contract deployer reads the compiled source contract code from disk and deploys several // ERC20 contracts and one ERC721 contract used for testing -import { TestERC20A } from "./typechain/TestERC20A"; -import { TestERC20B } from "./typechain/TestERC20B"; -import { TestERC20C } from "./typechain/TestERC20C"; -import { TestERC721A } from "./typechain/TestERC721A"; +// import { TestERC20A } from "./typechain/TestERC20A"; +// import { TestERC20B } from "./typechain/TestERC20B"; +// import { TestERC20C } from "./typechain/TestERC20C"; +// import { TestERC721A } from "./typechain/TestERC721A"; import { AltheaDB } from "./typechain/AltheaDB"; import { ethers } from "ethers"; import fs from "fs"; @@ -33,13 +33,10 @@ async function deploy() { let wallet = new ethers.Wallet(args["eth-privkey"], provider); var success = false; - var tries = 0; while (!success) { var present = new Date(); var timeDiff: number = present.getTime() - startTime.getTime(); timeDiff = timeDiff / 1000 - console.log(tries); - tries++; provider.getBlockNumber().then(e => { console.log(e); success = true; }).catch(e => { console.log(e); console.log("Ethereum RPC error, trying again"); }) if (timeDiff > 600) { @@ -52,48 +49,48 @@ async function deploy() { console.log("Test mode, deploying ERC20 contracts"); // this handles several possible locations for the ERC20 artifacts - var erc20_a_path: string - var erc20_b_path: string - var erc20_c_path: string - var erc721_a_path: string + // var erc20_a_path: string + // var erc20_b_path: string + // var erc20_c_path: string + // var erc721_a_path: string var althea_db_path: string - const main_location_a = "/althea_rs/solidity/artifacts/contracts/TestERC20A.sol/TestERC20A.json" - const main_location_b = "/althea_rs/solidity/artifacts/contracts/TestERC20B.sol/TestERC20B.json" - const main_location_c = "/althea_rs/solidity/artifacts/contracts/TestERC20C.sol/TestERC20C.json" - const main_location_721_a = "/althea_rs/solidity/artifacts/contracts/TestERC721A.sol/TestERC721A.json" + // const main_location_a = "/althea_rs/solidity/artifacts/contracts/TestERC20A.sol/TestERC20A.json" + // const main_location_b = "/althea_rs/solidity/artifacts/contracts/TestERC20B.sol/TestERC20B.json" + // const main_location_c = "/althea_rs/solidity/artifacts/contracts/TestERC20C.sol/TestERC20C.json" + // const main_location_721_a = "/althea_rs/solidity/artifacts/contracts/TestERC721A.sol/TestERC721A.json" const main_location_altheadb = "/althea_rs/solidity/artifacts/contracts/AltheaDB.sol/AltheaDB.json" - const alt_location_1_a = "/solidity/TestERC20A.json" - const alt_location_1_b = "/solidity/TestERC20B.json" - const alt_location_1_c = "/solidity/TestERC20C.json" - const alt_location_1_721a = "/solidity/TestERC721A.json" + // const alt_location_1_a = "/solidity/TestERC20A.json" + // const alt_location_1_b = "/solidity/TestERC20B.json" + // const alt_location_1_c = "/solidity/TestERC20C.json" + // const alt_location_1_721a = "/solidity/TestERC721A.json" const alt_location_1_altheadb = "/solidity/AltheaDB.json" - const alt_location_2_a = "TestERC20A.json" - const alt_location_2_b = "TestERC20B.json" - const alt_location_2_c = "TestERC20C.json" - const alt_location_2_721a = "TestERC721A.json" + // const alt_location_2_a = "TestERC20A.json" + // const alt_location_2_b = "TestERC20B.json" + // const alt_location_2_c = "TestERC20C.json" + // const alt_location_2_721a = "TestERC721A.json" const alt_location_2_altheadb = "AltheaDB.json" - if (fs.existsSync(main_location_a)) { - erc20_a_path = main_location_a - erc20_b_path = main_location_b - erc20_c_path = main_location_c - erc721_a_path = main_location_721_a + if (fs.existsSync(main_location_altheadb)) { + // erc20_a_path = main_location_a + // erc20_b_path = main_location_b + // erc20_c_path = main_location_c + // erc721_a_path = main_location_721_a althea_db_path = main_location_altheadb - } else if (fs.existsSync(alt_location_1_a)) { - erc20_a_path = alt_location_1_a - erc20_b_path = alt_location_1_b - erc20_c_path = alt_location_1_c - erc721_a_path = alt_location_1_721a + } else if (fs.existsSync(alt_location_1_altheadb)) { + // erc20_a_path = alt_location_1_a + // erc20_b_path = alt_location_1_b + // erc20_c_path = alt_location_1_c + // erc721_a_path = alt_location_1_721a althea_db_path = alt_location_1_altheadb - } else if (fs.existsSync(alt_location_2_a)) { - erc20_a_path = alt_location_2_a - erc20_b_path = alt_location_2_b - erc20_c_path = alt_location_2_c - erc721_a_path = alt_location_2_721a + } else if (fs.existsSync(alt_location_2_altheadb)) { + // erc20_a_path = alt_location_2_a + // erc20_b_path = alt_location_2_b + // erc20_c_path = alt_location_2_c + // erc721_a_path = alt_location_2_721a althea_db_path = alt_location_2_altheadb } else { console.log("Test mode was enabled but the ERC20 contracts can't be found!") @@ -109,43 +106,43 @@ async function deploy() { // and cast it to the generated Class in that .d.ts file // Read ABI (blockchain API) and bytecode from disk - const { abi, bytecode } = getContractArtifacts(erc20_a_path); - // Create a contract deployer factory - const erc20Factory = new ethers.ContractFactory(abi, bytecode, wallet); - // Deploy the contract and cast to the typechain generated class - const testERC20 = (await erc20Factory.deploy(overrides)) as TestERC20A; - await testERC20.deployed(); // Wait - const erc20TestAddress = testERC20.address; - console.log("ERC20 deployed at Address - ", erc20TestAddress); + // const { abi, bytecode } = getContractArtifacts(erc20_a_path); + // // Create a contract deployer factory + // const erc20Factory = new ethers.ContractFactory(abi, bytecode, wallet); + // // Deploy the contract and cast to the typechain generated class + // const testERC20 = (await erc20Factory.deploy(overrides)) as TestERC20A; + // await testERC20.deployed(); // Wait + // const erc20TestAddress = testERC20.address; + // console.log("ERC20 deployed at Address - ", erc20TestAddress); // Now testERC20 is ready to use, e.g.testERC20.transfer(from, to, amount) - const { abi: abi1, bytecode: bytecode1 } = getContractArtifacts(erc20_b_path); - const erc20Factory1 = new ethers.ContractFactory(abi1, bytecode1, wallet); - const testERC201 = (await erc20Factory1.deploy(overrides)) as TestERC20B; - await testERC201.deployed(); - const erc20TestAddress1 = testERC201.address; - console.log("ERC20 deployed at Address - ", erc20TestAddress1); - - const { abi: abi2, bytecode: bytecode2 } = getContractArtifacts(erc20_c_path); - const erc20Factory2 = new ethers.ContractFactory(abi2, bytecode2, wallet); - const testERC202 = (await erc20Factory2.deploy(overrides)) as TestERC20C; - await testERC202.deployed(); - const erc20TestAddress2 = testERC202.address; - console.log("ERC20 deployed at Address - ", erc20TestAddress2); - - const { abi: abi3, bytecode: bytecode3 } = getContractArtifacts(erc721_a_path); - const erc721Factory1 = new ethers.ContractFactory(abi3, bytecode3, wallet); - const testERC721 = (await erc721Factory1.deploy(overrides)) as TestERC721A; - await testERC721.deployed(); - const erc721TestAddress = testERC721.address; - console.log("ERC721 deployed at Address - ", erc721TestAddress); + // const { abi: abi1, bytecode: bytecode1 } = getContractArtifacts(erc20_b_path); + // const erc20Factory1 = new ethers.ContractFactory(abi1, bytecode1, wallet); + // const testERC201 = (await erc20Factory1.deploy(overrides)) as TestERC20B; + // await testERC201.deployed(); + // const erc20TestAddress1 = testERC201.address; + // console.log("ERC20 deployed at Address - ", erc20TestAddress1); + + // const { abi: abi2, bytecode: bytecode2 } = getContractArtifacts(erc20_c_path); + // const erc20Factory2 = new ethers.ContractFactory(abi2, bytecode2, wallet); + // const testERC202 = (await erc20Factory2.deploy(overrides)) as TestERC20C; + // await testERC202.deployed(); + // const erc20TestAddress2 = testERC202.address; + // console.log("ERC20 deployed at Address - ", erc20TestAddress2); + + // const { abi: abi3, bytecode: bytecode3 } = getContractArtifacts(erc721_a_path); + // const erc721Factory1 = new ethers.ContractFactory(abi3, bytecode3, wallet); + // const testERC721 = (await erc721Factory1.deploy(overrides)) as TestERC721A; + // await testERC721.deployed(); + // const erc721TestAddress = testERC721.address; + // console.log("ERC721 deployed at Address - ", erc721TestAddress); const { abi: abi4, bytecode: bytecode4 } = getContractArtifacts(althea_db_path); const altheadbFactory1 = new ethers.ContractFactory(abi4, bytecode4, wallet); const testAltheaDB = (await altheadbFactory1.deploy(overrides)) as AltheaDB; await testAltheaDB.deployed(); const altheaDBTestAddress = testAltheaDB.address; - console.log("Althea DB deployed at Address - ", altheaDBTestAddress); + console.log("Althea_DB_addr: ", altheaDBTestAddress); } function getContractArtifacts(path: string): { bytecode: string; abi: string } { diff --git a/test_runner/src/main.rs b/test_runner/src/main.rs index e52ba9606..127a6277f 100644 --- a/test_runner/src/main.rs +++ b/test_runner/src/main.rs @@ -6,8 +6,6 @@ use integration_tests::debts::run_debts_test; /// Binary crate for actually running the integration tests use integration_tests::five_nodes::run_five_node_test_scenario; use integration_tests::mutli_exit::run_multi_exit_test; -use integration_tests::registration_server::start_registration_server; -use integration_tests::utils::deploy_contracts; use integration_tests::{ payments_althea::run_althea_payments_test_scenario, payments_eth::run_eth_payments_test_scenario, utils::set_sigterm, @@ -42,12 +40,6 @@ async fn main() { let conf = generate_exit_config_file(EXIT_CONFIG_PATH.to_string()); info!("Generating exit config file: {:?}", conf); - println!("Waiting to deploy contracts"); - deploy_contracts().await; - - println!("Starting registration server"); - start_registration_server(); - let test_type = env::var("TEST_TYPE"); info!("Starting tests with {:?}", test_type); if let Ok(test_type) = test_type {