From e8a8a4dadb822bd9918fe8f28dd021789aefd1d6 Mon Sep 17 00:00:00 2001 From: iosh Date: Mon, 7 Oct 2024 18:48:07 +0800 Subject: [PATCH] chore: up binding --- conflux.d.ts | 10 +++-- conflux.js | 3 +- index.ts | 8 ++-- src/lib.rs | 125 ++++++++++++++++++++++++++++----------------------- 4 files changed, 80 insertions(+), 66 deletions(-) diff --git a/conflux.d.ts b/conflux.d.ts index d194a78..abea0d2 100644 --- a/conflux.d.ts +++ b/conflux.d.ts @@ -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. @@ -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 - diff --git a/conflux.js b/conflux.js index 672696e..7a481f2 100644 --- a/conflux.js +++ b/conflux.js @@ -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 diff --git a/index.ts b/index.ts index b9c969e..ae89c8e 100644 --- a/index.ts +++ b/index.ts @@ -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 }; @@ -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(); }, }; } diff --git a/src/lib.rs b/src/lib.rs index 19d911f..ed77d98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, 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, 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; - 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; + 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(); + } }