Skip to content

Commit

Permalink
GH-541: completed and ci/all tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Syther007 committed Mar 10, 2023
1 parent 11ebc79 commit 96f55d8
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 238 deletions.
2 changes: 1 addition & 1 deletion node/src/database/db_initializer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2019, MASQ (https://masq.ai) and/or its affiliates. All rights reserved.
use crate::database::connection_wrapper::{ConnectionWrapper, ConnectionWrapperReal};

use crate::database::db_migrations::db_migrator::{DbMigrator, DbMigratorReal};
use crate::db_config::secure_config_layer::EXAMPLE_ENCRYPTED;
use crate::sub_lib::accountant::{DEFAULT_PAYMENT_THRESHOLDS, DEFAULT_SCAN_INTERVALS};
use crate::sub_lib::neighborhood::DEFAULT_RATE_PACK;
Expand All @@ -20,7 +21,6 @@ use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::path::Path;
use std::{fs, vec};
use tokio::net::TcpListener;
use crate::database::db_migrations::db_migrator::{DbMigrator, DbMigratorReal};

pub const DATABASE_FILE: &str = "node-data.db";
pub const CURRENT_SCHEMA_VERSION: usize = 7;
Expand Down
87 changes: 21 additions & 66 deletions node/src/database/db_migrations/db_migrator.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
// Copyright (c) 2019, MASQ (https://masq.ai) and/or its affiliates. All rights reserved.


use crate::accountant::big_int_processing::big_int_divider::BigIntDivider;
use crate::accountant::dao_utils::VigilantRusqliteFlatten;
use crate::accountant::gwei_to_wei;
use crate::blockchain::bip39::Bip39;
use crate::database::connection_wrapper::ConnectionWrapper;
use crate::database::db_initializer::{ExternalData, CURRENT_SCHEMA_VERSION};
use crate::db_config::db_encryption_layer::DbEncryptionLayer;
use crate::db_config::typed_config_layer::decode_bytes;
use crate::sub_lib::accountant::{DEFAULT_PAYMENT_THRESHOLDS, DEFAULT_SCAN_INTERVALS};
use crate::sub_lib::cryptde::PlainData;
use crate::sub_lib::neighborhood::{RatePack, DEFAULT_RATE_PACK};
use itertools::Itertools;
use masq_lib::logger::Logger;
use masq_lib::utils::{ExpectValue, WrapResult};
use rusqlite::{params_from_iter, Error, Row, ToSql, Transaction};
use std::fmt::{Debug, Display, Formatter};
use tiny_hderive::bip32::ExtendedPrivKey;
use crate::database::db_migrations::db_migrator_utils::{DBMigrationUtilities, DBMigrationUtilitiesReal, DBMigratorInnerConfiguration, MigDeclarationUtilities};
use crate::database::db_initializer::ExternalData;
use crate::database::db_migrations::migrations::migration_0_to_1::Migrate_0_to_1;
use crate::database::db_migrations::migrations::migration_1_to_2::Migrate_1_to_2;
use crate::database::db_migrations::migrations::migration_2_to_3::Migrate_2_to_3;
use crate::database::db_migrations::migrations::migration_3_to_4::Migrate_3_to_4;
use crate::database::db_migrations::migrations::migration_4_to_5::Migrate_4_to_5;
use crate::database::db_migrations::migrations::migration_5_to_6::Migrate_5_to_6;
use crate::database::db_migrations::migrations::migration_6_to_7::Migrate_6_to_7;

use crate::database::db_migrations::migrator_utils::{
DBMigrationUtilities, DBMigrationUtilitiesReal, DBMigratorInnerConfiguration,
MigDeclarationUtilities,
};
use masq_lib::logger::Logger;
use rusqlite::Transaction;

pub trait DbMigrator {
fn migrate_database(
Expand Down Expand Up @@ -193,54 +181,28 @@ impl DbMigratorReal {

#[cfg(test)]
mod tests {
use crate::accountant::dao_utils::{from_time_t, to_time_t};
use crate::blockchain::bip39::Bip39;
use crate::database::connection_wrapper::{ConnectionWrapper, ConnectionWrapperReal};
use crate::database::db_initializer::test_utils::ConnectionWrapperMock;
use crate::database::db_initializer::{CURRENT_SCHEMA_VERSION, DATABASE_FILE, ExternalData};
use crate::db_config::db_encryption_layer::DbEncryptionLayer;
use crate::db_config::persistent_configuration::{
PersistentConfiguration, PersistentConfigurationReal,
use crate::database::db_initializer::{ExternalData, CURRENT_SCHEMA_VERSION};
use crate::database::db_migrations::db_migrator::{
DatabaseMigration, DbMigrator, DbMigratorReal,
};
use crate::db_config::typed_config_layer::encode_bytes;
use crate::sub_lib::accountant::{DEFAULT_PAYMENT_THRESHOLDS, DEFAULT_SCAN_INTERVALS};
use crate::sub_lib::cryptde::PlainData;
use crate::sub_lib::neighborhood::DEFAULT_RATE_PACK;
use crate::sub_lib::wallet::Wallet;
use crate::test_utils::database_utils::{
assert_create_table_stm_contains_all_parts,
assert_index_stm_is_coupled_with_right_parameter, assert_no_index_exists_for_table,
assert_table_does_not_exist, bring_db_0_back_to_life_and_return_connection,
make_external_data,
use crate::database::db_migrations::migrations::migration_0_to_1::Migrate_0_to_1;
use crate::database::db_migrations::migrator_utils::{
DBMigrationUtilities, DBMigrationUtilitiesReal, DBMigratorInnerConfiguration,
MigDeclarationUtilities, StatementObject,
};
use crate::test_utils::database_utils::{assert_table_created_as_strict, retrieve_config_row};
use crate::test_utils::make_wallet;
use bip39::{Language, Mnemonic, MnemonicType, Seed};
use ethereum_types::BigEndianHash;
use itertools::Itertools;
use masq_lib::constants::DEFAULT_CHAIN;
use crate::test_utils::database_utils::make_external_data;
use masq_lib::logger::Logger;
use masq_lib::test_utils::logging::{init_test_logging, TestLogHandler};
use masq_lib::test_utils::utils::{ensure_node_home_directory_exists, TEST_DEFAULT_CHAIN};
use masq_lib::utils::{derivation_path, NeighborhoodModeLight};
use rand::Rng;
use rusqlite::types::Value::Null;
use rusqlite::{Connection, Error, OptionalExtension, Row, ToSql, Transaction};
use masq_lib::test_utils::utils::TEST_DEFAULT_CHAIN;
use masq_lib::utils::NeighborhoodModeLight;
use rusqlite::{Connection, Error, Transaction};
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt::Debug;
use std::fs::create_dir_all;
use std::iter::once;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::time::SystemTime;
use tiny_hderive::bip32::ExtendedPrivKey;
use web3::types::{H256, U256};
use crate::database::connection_wrapper::{ConnectionWrapper, ConnectionWrapperReal};
use crate::database::db_migrations::db_migrator::{DatabaseMigration, DbMigrator, DbMigratorReal};
use crate::database::db_migrations::db_migrator_utils::{DBMigrationUtilities, DBMigrationUtilitiesReal, DBMigratorInnerConfiguration, MigDeclarationUtilities, StatementObject};
use crate::database::db_migrations::migrations::migration_0_to_1::Migrate_0_to_1;


#[derive(Default)]
struct DBMigrationUtilitiesMock {
Expand Down Expand Up @@ -432,7 +394,6 @@ mod tests {
}
}


const _REMINDER_FROM_COMPILATION_TIME: () = check_schema_version_continuity();

#[allow(dead_code)]
Expand Down Expand Up @@ -481,7 +442,7 @@ mod tests {
DbMigratorReal::list_of_migrations(),
)
}))
.unwrap_err();
.unwrap_err();

let panic_message = captured_panic.downcast_ref::<String>().unwrap();
assert_eq!(
Expand All @@ -494,7 +455,6 @@ mod tests {
)
}


#[test]
#[should_panic(expected = "The list of database migrations is not ordered properly")]
fn list_validation_check_works_for_badly_ordered_migrations_when_inside() {
Expand Down Expand Up @@ -543,7 +503,6 @@ mod tests {
//success if no panicking
}


#[test]
fn list_of_migrations_ends_on_the_current_version() {
let last_entry = DbMigratorReal::list_of_migrations().into_iter().last();
Expand Down Expand Up @@ -611,7 +570,6 @@ mod tests {
);
}


fn make_success_mig_record(
old_version: usize,
empty_params_arc: &Arc<Mutex<Vec<()>>>,
Expand Down Expand Up @@ -758,7 +716,6 @@ mod tests {
assert_eq!(*fifth_record_migration_params, vec![]);
}


#[test]
fn db_migration_happy_path() {
init_test_logging();
Expand Down Expand Up @@ -848,6 +805,4 @@ mod tests {
let second_record_migration_params = second_record_migration_p_arc.lock().unwrap();
assert_eq!(*second_record_migration_params, vec![()]);
}


}
}
14 changes: 8 additions & 6 deletions node/src/database/db_migrations/migrations/migration_0_to_1.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) 2019, MASQ (https://masq.ai) and/or its affiliates. All rights reserved.


use crate::database::db_migrations::db_migrator::DatabaseMigration;
use crate::database::db_migrations::db_migrator_utils::MigDeclarationUtilities;
use crate::database::db_migrations::migrator_utils::MigDeclarationUtilities;

#[allow(non_camel_case_types)]
pub struct Migrate_0_to_1;
Expand All @@ -22,12 +21,15 @@ impl DatabaseMigration for Migrate_0_to_1 {
}
}


#[cfg(test)]
mod tests {
use crate::database::db_initializer::{
DbInitializationConfig, DbInitializer, DbInitializerReal, DATABASE_FILE,
};
use crate::test_utils::database_utils::{
bring_db_0_back_to_life_and_return_connection, make_external_data, retrieve_config_row,
};
use masq_lib::test_utils::utils::ensure_node_home_directory_exists;
use crate::database::db_initializer::{DATABASE_FILE, DbInitializationConfig, DbInitializer, DbInitializerReal};
use crate::test_utils::database_utils::{bring_db_0_back_to_life_and_return_connection, make_external_data, retrieve_config_row};
use std::fs::create_dir_all;

#[test]
Expand All @@ -54,4 +56,4 @@ mod tests {
assert_eq!(cs_value, Some("1".to_string()));
assert_eq!(cs_encrypted, false)
}
}
}
16 changes: 8 additions & 8 deletions node/src/database/db_migrations/migrations/migration_1_to_2.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) 2019, MASQ (https://masq.ai) and/or its affiliates. All rights reserved.


use crate::database::db_migrations::db_migrator::DatabaseMigration;
use crate::database::db_migrations::db_migrator_utils::MigDeclarationUtilities;
use crate::database::db_migrations::migrator_utils::MigDeclarationUtilities;

#[allow(non_camel_case_types)]
pub struct Migrate_1_to_2;
Expand All @@ -28,13 +27,16 @@ impl DatabaseMigration for Migrate_1_to_2 {
}
}


#[cfg(test)]
mod tests {
use crate::database::db_initializer::{
DbInitializationConfig, DbInitializer, DbInitializerReal, DATABASE_FILE,
};
use crate::test_utils::database_utils::{
bring_db_0_back_to_life_and_return_connection, make_external_data, retrieve_config_row,
};
use masq_lib::test_utils::logging::{init_test_logging, TestLogHandler};
use masq_lib::test_utils::utils::ensure_node_home_directory_exists;
use crate::database::db_initializer::{DATABASE_FILE, DbInitializationConfig, DbInitializer, DbInitializerReal};
use crate::test_utils::database_utils::{bring_db_0_back_to_life_and_return_connection, make_external_data, retrieve_config_row};

#[test]
fn migration_from_1_to_2_is_properly_set() {
Expand Down Expand Up @@ -74,6 +76,4 @@ mod tests {
"DbMigrator: Database successfully migrated from version 1 to 2",
);
}


}
}
15 changes: 8 additions & 7 deletions node/src/database/db_migrations/migrations/migration_2_to_3.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) 2019, MASQ (https://masq.ai) and/or its affiliates. All rights reserved.


use crate::database::db_migrations::db_migrator::DatabaseMigration;
use crate::database::db_migrations::db_migrator_utils::MigDeclarationUtilities;
use crate::database::db_migrations::migrator_utils::MigDeclarationUtilities;

#[allow(non_camel_case_types)]
pub struct Migrate_2_to_3;
Expand All @@ -26,14 +25,17 @@ impl DatabaseMigration for Migrate_2_to_3 {
}
}


#[cfg(test)]
mod tests {
use crate::database::db_initializer::{
DbInitializationConfig, DbInitializer, DbInitializerReal, ExternalData, DATABASE_FILE,
};
use crate::test_utils::database_utils::{
bring_db_0_back_to_life_and_return_connection, make_external_data, retrieve_config_row,
};
use masq_lib::constants::DEFAULT_CHAIN;
use masq_lib::test_utils::utils::ensure_node_home_directory_exists;
use masq_lib::utils::NeighborhoodModeLight;
use crate::database::db_initializer::{DATABASE_FILE, DbInitializationConfig, DbInitializer, DbInitializerReal, ExternalData};
use crate::test_utils::database_utils::{bring_db_0_back_to_life_and_return_connection, make_external_data, retrieve_config_row};

#[test]
fn migration_from_2_to_3_is_properly_set() {
Expand Down Expand Up @@ -78,5 +80,4 @@ mod tests {
assert_eq!(cs_value, Some("3".to_string()));
assert_eq!(cs_encrypted, false);
}

}
}
43 changes: 22 additions & 21 deletions node/src/database/db_migrations/migrations/migration_3_to_4.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Copyright (c) 2019, MASQ (https://masq.ai) and/or its affiliates. All rights reserved.


use itertools::Itertools;
use tiny_hderive::bip32::ExtendedPrivKey;
use crate::blockchain::bip39::Bip39;
use crate::database::db_migrations::db_migrator::DatabaseMigration;
use crate::database::db_migrations::db_migrator_utils::MigDeclarationUtilities;
use crate::database::db_migrations::migrator_utils::MigDeclarationUtilities;
use crate::db_config::db_encryption_layer::DbEncryptionLayer;
use crate::db_config::typed_config_layer::decode_bytes;
use crate::sub_lib::cryptde::PlainData;
use itertools::Itertools;
use tiny_hderive::bip32::ExtendedPrivKey;

#[allow(non_camel_case_types)]
pub struct Migrate_3_to_4;
Expand All @@ -31,10 +30,10 @@ impl DatabaseMigration for Migrate_3_to_4 {
.collect::<Vec<(String, Option<String>)>>();
if rows.iter().map(|r| r.0.as_str()).collect_vec()
!= vec![
"consuming_wallet_derivation_path",
"example_encrypted",
"seed",
]
"consuming_wallet_derivation_path",
"example_encrypted",
"seed",
]
{
panic!("Database is corrupt");
}
Expand Down Expand Up @@ -63,7 +62,7 @@ impl DatabaseMigration for Migrate_3_to_4 {
&private_key_data.as_slice(),
password_opt.as_ref().expect("Test-drive me!"),
)
.expect("Internal error: encryption failed"),
.expect("Internal error: encryption failed"),
)
}
_ => None,
Expand All @@ -85,21 +84,24 @@ impl DatabaseMigration for Migrate_3_to_4 {
}
}


#[cfg(test)]
mod tests {
use bip39::{Language, Mnemonic, MnemonicType, Seed};
use rand::Rng;
use rusqlite::ToSql;
use tiny_hderive::bip32::ExtendedPrivKey;
use masq_lib::test_utils::utils::ensure_node_home_directory_exists;
use masq_lib::utils::derivation_path;
use crate::blockchain::bip39::Bip39;
use crate::database::db_initializer::{DATABASE_FILE, DbInitializationConfig, DbInitializer, DbInitializerReal};
use crate::database::db_initializer::{
DbInitializationConfig, DbInitializer, DbInitializerReal, DATABASE_FILE,
};
use crate::db_config::db_encryption_layer::DbEncryptionLayer;
use crate::db_config::typed_config_layer::encode_bytes;
use crate::sub_lib::cryptde::PlainData;
use crate::test_utils::database_utils::{bring_db_0_back_to_life_and_return_connection, make_external_data, retrieve_config_row};
use crate::test_utils::database_utils::{
bring_db_0_back_to_life_and_return_connection, make_external_data, retrieve_config_row,
};
use bip39::{Language, Mnemonic, MnemonicType, Seed};
use masq_lib::test_utils::utils::ensure_node_home_directory_exists;
use masq_lib::utils::derivation_path;
use rand::Rng;
use rusqlite::ToSql;
use tiny_hderive::bip32::ExtendedPrivKey;

#[test]
fn migration_from_3_to_4_with_wallets() {
Expand Down Expand Up @@ -181,7 +183,7 @@ mod tests {
&private_key_encrypted.unwrap(),
password_opt.as_ref().unwrap(),
)
.unwrap();
.unwrap();
private_key.as_slice().to_vec()
};

Expand Down Expand Up @@ -224,5 +226,4 @@ mod tests {
assert_eq!(private_key_encrypted, None);
assert_eq!(encrypted, true);
}

}
}
Loading

0 comments on commit 96f55d8

Please sign in to comment.