Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Salka1988 committed Oct 10, 2023
1 parent 94fcc0c commit e1e548e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 35 deletions.
4 changes: 1 addition & 3 deletions examples/cookbook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/fuels-test-helpers/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn launch_provider_and_get_wallet() -> Result<WalletUnlocked> {
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.
Expand Down
32 changes: 12 additions & 20 deletions packages/fuels-test-helpers/src/fuel_bin_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions packages/fuels-test-helpers/src/node_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ impl From<Trigger> for fuel_core_poa::Trigger {
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DbType {
InMemory,
RocksDb,
RocksDb(Option<PathBuf>),
}

#[cfg(feature = "fuel-core-lib")]
impl From<DbType> 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,
}
}
}
Expand All @@ -62,7 +62,6 @@ impl From<DbType> 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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -112,7 +109,10 @@ impl From<Config> 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,
Expand Down
7 changes: 2 additions & 5 deletions packages/fuels/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};

Expand Down Expand Up @@ -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()
};

Expand Down

0 comments on commit e1e548e

Please sign in to comment.