From e1e548e9bd52d8b2911aa77807c8ef9070461a92 Mon Sep 17 00:00:00 2001 From: Emir Salkic Date: Tue, 10 Oct 2023 15:32:13 +0200 Subject: [PATCH] refactoring --- examples/cookbook/src/lib.rs | 4 +-- packages/fuels-test-helpers/src/accounts.rs | 2 +- .../src/fuel_bin_service.rs | 32 +++++++------------ packages/fuels-test-helpers/src/node_types.rs | 12 +++---- packages/fuels/tests/contracts.rs | 7 ++-- 5 files changed, 22 insertions(+), 35 deletions(-) diff --git a/examples/cookbook/src/lib.rs b/examples/cookbook/src/lib.rs index 5f1eaf53b6..39a44d7854 100644 --- a/examples/cookbook/src/lib.rs +++ b/examples/cookbook/src/lib.rs @@ -195,11 +195,9 @@ mod tests { use std::path::PathBuf; use fuels::prelude::*; - // ANCHOR: create_or_use_rocksdb let provider_config = Config { - database_path: PathBuf::from("/tmp/.spider/db"), - database_type: DbType::RocksDb, + database_type: DbType::RocksDb(Some(PathBuf::from("/tmp/.spider/db"))), ..Config::local_node() }; // ANCHOR_END: create_or_use_rocksdb diff --git a/packages/fuels-test-helpers/src/accounts.rs b/packages/fuels-test-helpers/src/accounts.rs index ac24d49500..7e70240b19 100644 --- a/packages/fuels-test-helpers/src/accounts.rs +++ b/packages/fuels-test-helpers/src/accounts.rs @@ -25,7 +25,7 @@ pub async fn launch_provider_and_get_wallet() -> Result { launch_custom_provider_and_get_wallets(WalletsConfig::new(Some(1), None, None), None, None) .await?; - Ok(wallets.pop().unwrap()) + Ok(wallets.pop().expect("should have one wallet")) } /// Launches a custom node and provider, along with a configurable number of wallets. diff --git a/packages/fuels-test-helpers/src/fuel_bin_service.rs b/packages/fuels-test-helpers/src/fuel_bin_service.rs index 567cfcb1d7..d6db10207c 100644 --- a/packages/fuels-test-helpers/src/fuel_bin_service.rs +++ b/packages/fuels-test-helpers/src/fuel_bin_service.rs @@ -6,8 +6,7 @@ use fuel_core_services::{RunnableService, RunnableTask, ServiceRunner, State, St use fuel_core_services::Service as ServiceTrait; use fuel_core_client::client::FuelClient; -use serde_json::{to_value, Value}; -use std::{io::Write, net::SocketAddr, path::PathBuf, pin::Pin, time::Duration}; +use std::{net::SocketAddr, path::PathBuf, pin::Pin, time::Duration}; use crate::node_types::{Config, DbType, Trigger, DEFAULT_CACHE_SIZE}; use portpicker::{is_free, pick_unused_port}; @@ -38,25 +37,18 @@ impl ExtendedConfig { .to_string(), ]; - args.extend(vec![ - "--db-type".to_string(), - match self.config.database_type { - DbType::InMemory => "in-memory", - DbType::RocksDb => "rocks-db", + args.push("--db-type".to_string()); + match &self.config.database_type { + DbType::InMemory => args.push("in-memory".to_string()), + DbType::RocksDb(path_to_db) => { + args.push("rocks-db".to_string()); + let path = path_to_db.as_ref().map(Clone::clone).unwrap_or_else(|| { + PathBuf::from(std::env::var("HOME").expect("HOME env var missing")) + .join(".fuel/db") + }); + args.push("--db-path".to_string()); + args.push(path.to_string_lossy().to_string()); } - .to_string(), - ]); - - if let DbType::RocksDb = self.config.database_type { - let path = if self.config.database_path.as_os_str().is_empty() { - PathBuf::from(std::env::var("HOME").expect("HOME env var missing")).join(".fuel/db") - } else { - self.config.database_path.clone() - }; - args.extend(vec![ - "--db-path".to_string(), - path.to_string_lossy().to_string(), - ]); } if self.config.max_database_cache_size != DEFAULT_CACHE_SIZE { diff --git a/packages/fuels-test-helpers/src/node_types.rs b/packages/fuels-test-helpers/src/node_types.rs index 9fc3962441..1b5fc5b02a 100644 --- a/packages/fuels-test-helpers/src/node_types.rs +++ b/packages/fuels-test-helpers/src/node_types.rs @@ -45,7 +45,7 @@ impl From for fuel_core_poa::Trigger { #[derive(Clone, Debug, Eq, PartialEq)] pub enum DbType { InMemory, - RocksDb, + RocksDb(Option), } #[cfg(feature = "fuel-core-lib")] @@ -53,7 +53,7 @@ impl From for fuel_core::service::DbType { fn from(value: DbType) -> Self { match value { DbType::InMemory => fuel_core::service::DbType::InMemory, - DbType::RocksDb => fuel_core::service::DbType::RocksDb, + DbType::RocksDb(..) => fuel_core::service::DbType::RocksDb, } } } @@ -62,7 +62,6 @@ impl From for fuel_core::service::DbType { pub struct Config { pub addr: SocketAddr, pub max_database_cache_size: usize, - pub database_path: PathBuf, pub database_type: DbType, pub utxo_validation: bool, pub manual_blocks_enabled: bool, @@ -77,7 +76,6 @@ impl Config { Self { addr: SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), 0), max_database_cache_size: DEFAULT_CACHE_SIZE, - database_path: Default::default(), database_type: DbType::InMemory, utxo_validation: false, manual_blocks_enabled: false, @@ -94,7 +92,6 @@ impl Default for Config { Self { addr: SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), 0), max_database_cache_size: DEFAULT_CACHE_SIZE, - database_path: Default::default(), database_type: DbType::InMemory, utxo_validation: false, manual_blocks_enabled: false, @@ -112,7 +109,10 @@ impl From for fuel_core::service::Config { Self { addr: value.addr, max_database_cache_size: value.max_database_cache_size, - database_path: value.database_path, + database_path: match &value.database_type { + DbType::InMemory => Default::default(), + DbType::RocksDb(path) => path.clone().unwrap_or_default(), + }, database_type: value.database_type.into(), utxo_validation: value.utxo_validation, manual_blocks_enabled: value.manual_blocks_enabled, diff --git a/packages/fuels/tests/contracts.rs b/packages/fuels/tests/contracts.rs index 1a53d3ac36..a200c34b93 100644 --- a/packages/fuels/tests/contracts.rs +++ b/packages/fuels/tests/contracts.rs @@ -1359,10 +1359,8 @@ fn db_rocksdb() { ); const NUMBER_OF_ASSETS: u64 = 2; - let mut node_config = Config { - database_path: temp_database_path.clone(), - database_type: DbType::RocksDb, + database_type: DbType::RocksDb(Some(temp_database_path.clone())), ..Config::local_node() }; @@ -1398,8 +1396,7 @@ fn db_rocksdb() { .expect("Tokio runtime failed") .block_on(async { let node_config = Config { - database_path: temp_database_path.clone(), - database_type: DbType::RocksDb, + database_type: DbType::RocksDb(Some(temp_database_path.clone())), ..Config::local_node() };