Skip to content

Commit

Permalink
chore: up binding
Browse files Browse the repository at this point in the history
  • Loading branch information
iosh committed Oct 7, 2024
1 parent 1b6e1d5 commit e8a8a4d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 66 deletions.
10 changes: 6 additions & 4 deletions conflux.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
export declare class ConfluxNode {
constructor()
startNode(config: ConfluxConfig): void
stopNode(): void
}

export interface ConfluxConfig {
/**
* Set the node type to Full node, Archive node, or Light node.
Expand Down Expand Up @@ -91,7 +97,3 @@ export interface ConfluxConfig {
blockDbType?: string
}

export declare export declare function runNode(config: ConfluxConfig): void

export declare export declare function stopNode(): void

3 changes: 1 addition & 2 deletions conflux.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,5 +361,4 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

module.exports.runNode = nativeBinding.runNode
module.exports.stopNode = nativeBinding.stopNode
module.exports.ConfluxNode = nativeBinding.ConfluxNode
8 changes: 5 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { type ConfluxConfig, runNode, stopNode } from "./conflux";
import { type ConfluxConfig, ConfluxNode } from "./conflux";

export type { ConfluxConfig };

Expand All @@ -13,12 +13,14 @@ export async function createServer(userConfig: ConfluxConfig = {}) {
posPrivateKeyPath: path.join(__dirname, "./pos_config/pos_key"),
...userConfig,
};

const node = new ConfluxNode()
return {
async start() {
runNode(config);
node.startNode(config);
},
async stop() {
stopNode();
node.stopNode();
},
};
}
125 changes: 68 additions & 57 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,73 +11,84 @@ use client::{
full::FullClient,
light::LightClient,
};
use log::info;
use parking_lot::{Condvar, Mutex};
use primitives::block_header::CIP112_TRANSITION_HEIGHT;
use std::thread;
use std::{
env,
sync::{Arc, LazyLock},
};
use std::{env, sync::Arc};
use tempfile::tempdir;
mod config;
static EXIT_SIGN: LazyLock<Arc<(Mutex<bool>, Condvar)>> =
LazyLock::new(|| Arc::new((Mutex::new(false), Condvar::new())));

#[napi]
pub fn run_node(config: config::ConfluxConfig) -> Result<(), napi::Error> {
let temp_dir = tempdir()?;
let temp_dir_path = temp_dir.path();
let conf = convert_config(config, &temp_dir_path);
CIP112_TRANSITION_HEIGHT.set(u64::MAX).expect("called once");
pub struct ConfluxNode {
exit_sign: Arc<(Mutex<bool>, Condvar)>,
}
#[napi]
impl ConfluxNode {
#[napi(constructor)]
pub fn new() -> Self {
ConfluxNode {
exit_sign: Arc::new((Mutex::new(false), Condvar::new())),
}
}
#[napi]
pub fn start_node(&self, config: config::ConfluxConfig) -> Result<(), napi::Error> {
if CIP112_TRANSITION_HEIGHT.get().is_none() {
CIP112_TRANSITION_HEIGHT.set(u64::MAX).expect("called once");
}

thread::spawn(|| {
// we need set the work dir to temp dir
let exit_sign = self.exit_sign.clone();

let temp_dir = tempdir().unwrap();
env::set_current_dir(temp_dir.path()).unwrap();
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);

println!("current dir thread 1 {:?}", env::current_dir());
let client_handle: Box<dyn ClientTrait>;
client_handle = match conf.node_type() {
NodeType::Archive => {
println!("Starting archive client...");
ArchiveClient::start(conf, EXIT_SIGN.clone()).map_err(|e| {
Error::new(
napi::Status::Unknown,
format!("failed to start archive client: {}", e),
)
})?
}
NodeType::Full => {
println!("Starting full client...");
FullClient::start(conf, EXIT_SIGN.clone()).map_err(|e| {
Error::new(
napi::Status::Unknown,
format!("failed to start full client: {}", e),
)
})?
}
NodeType::Light => {
println!("Starting light client...");
LightClient::start(conf, EXIT_SIGN.clone()).map_err(|e| {
Error::new(
napi::Status::Unknown,
format!("failed to start light client: {}", e),
)
})?
}
NodeType::Unknown => return Err(Error::new(napi::Status::InvalidArg, "Unknown node type")),
};
println!("Conflux client started");
shutdown_handler::run(client_handle, EXIT_SIGN.clone());
let temp_dir = tempdir().unwrap();
env::set_current_dir(temp_dir.path()).unwrap();

println!("current dir thread 1 {:?}", env::current_dir());
let client_handle: Box<dyn ClientTrait>;
client_handle = match conf.node_type() {
NodeType::Archive => {
println!("Starting archive client...");
ArchiveClient::start(conf, exit_sign.clone()).map_err(|e| {
Error::new(
napi::Status::Unknown,
format!("failed to start archive client: {}", e),
)
})?
}
NodeType::Full => {
println!("Starting full client...");
FullClient::start(conf, exit_sign.clone()).map_err(|e| {
Error::new(
napi::Status::Unknown,
format!("failed to start full client: {}", e),
)
})?
}
NodeType::Light => {
println!("Starting light client...");
LightClient::start(conf, exit_sign.clone()).map_err(|e| {
Error::new(
napi::Status::Unknown,
format!("failed to start light client: {}", e),
)
})?
}
NodeType::Unknown => return Err(Error::new(napi::Status::InvalidArg, "Unknown node type")),
};
println!("Conflux client started");
shutdown_handler::run(client_handle, exit_sign.clone());
Ok(())
});
Ok(())
});
Ok(())
}
}

#[napi]
pub fn stop_node() {
*EXIT_SIGN.0.lock() = true;
EXIT_SIGN.1.notify_all();
#[napi]
pub fn stop_node(&self) {
*self.exit_sign.0.lock() = true;
self.exit_sign.1.notify_all();
}
}

0 comments on commit e8a8a4d

Please sign in to comment.