Skip to content

Commit

Permalink
chore: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
iosh committed Oct 17, 2024
1 parent 4e4d2d3 commit 1ff3b81
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

# [target.i686-pc-windows-msvc]
# rustflags = ["-C", "target-feature=+crt-static"]
[term] # whether cargo output is quiet
verbose = true
# [term] # whether cargo output is quiet
# verbose = true
41 changes: 25 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use config::convert_config;
use log::{error, info};
use napi::{
bindgen_prelude::*,
threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode, UnknownReturnValue},
threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode},
};
use napi_derive::napi;

Expand All @@ -19,11 +19,33 @@ use parking_lot::{Condvar, Mutex};
use primitives::block_header::CIP112_TRANSITION_HEIGHT;
use std::thread;
use std::{env, sync::Arc};
use tempfile::tempdir;
use tempfile::{tempdir, TempDir};
mod config;

use log4rs;

fn setup_env(
config: config::ConfluxConfig,
temp_dir: &TempDir,
) -> Result<client::common::Configuration> {
// we need set the work dir to temp dir
let temp_dir_path = temp_dir.path();
let conf = convert_config(config, &temp_dir_path);
info!("node work dir: {:?}", temp_dir_path);
env::set_current_dir(temp_dir_path).unwrap();

if let Some(ref log_conf) = conf.raw_conf.log_conf {
log4rs::init_file(log_conf, Default::default())
.map_err(|e| {
error!("failed to initialize log with log config file: {:?}", e);
format!("failed to initialize log with log config file: {:?}", e)
})
.unwrap();
};

Ok(conf)
}

#[napi]
pub struct ConfluxNode {
exit_sign: Arc<(Mutex<bool>, Condvar)>,
Expand All @@ -49,21 +71,8 @@ impl ConfluxNode {
let exit_sign = self.exit_sign.clone();

thread::spawn(move || {
// we need set the work dir to temp dir
let temp_dir = tempdir()?;
let temp_dir_path = temp_dir.path();
let conf = convert_config(config, &temp_dir_path);
let temp_dir = tempdir().unwrap();
env::set_current_dir(temp_dir.path()).unwrap();

if let Some(ref log_conf) = conf.raw_conf.log_conf {
log4rs::init_file(log_conf, Default::default())
.map_err(|e| {
error!("failed to initialize log with log config file: {:?}", e);
format!("failed to initialize log with log config file: {:?}", e)
})
.unwrap();
}
let conf = setup_env(config, &temp_dir)?;

let client_handle: Box<dyn ClientTrait>;
client_handle = match conf.node_type() {
Expand Down

0 comments on commit 1ff3b81

Please sign in to comment.