From 544c025070032f488e0aae38b591b2fd26aaf28d Mon Sep 17 00:00:00 2001 From: Eason Date: Tue, 27 Jun 2023 20:49:35 +0800 Subject: [PATCH] refactor: change run function --- .gitignore | 2 +- Cargo.lock | 68 ++++++++++++++++++++- Cargo.toml | 4 +- common/src/types/relation_db/transaction.rs | 2 +- config.toml | 4 +- create_table.sql | 15 +++++ src/main.rs | 63 ++++++++++++++++++- 7 files changed, 149 insertions(+), 9 deletions(-) create mode 100644 create_table.sql diff --git a/.gitignore b/.gitignore index 1245ab1..b3d0739 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,6 @@ # database *.sqlite -free-space /devtools/tx-tests/src/config/priv_keys.toml +free-space/ diff --git a/Cargo.lock b/Cargo.lock index 9652fb6..ab5392b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,15 @@ version = "0.11.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" +[[package]] +name = "addr2line" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a76fd60b23679b7d19bd066031410fb7e458ccc5e958eb5c325888ce4baedc97" +dependencies = [ + "gimli", +] + [[package]] name = "adler" version = "1.0.2" @@ -322,6 +331,21 @@ dependencies = [ "molecule2", ] +[[package]] +name = "backtrace" +version = "0.3.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "233d376d6d185f2a3093e58f283f60f880315b6c60075b01f36b3b85154564ca" +dependencies = [ + "addr2line", + "cc", + "cfg-if 1.0.0", + "libc", + "miniz_oxide 0.6.2", + "object", + "rustc-demangle", +] + [[package]] name = "bae" version = "0.1.7" @@ -1431,7 +1455,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b9429470923de8e8cbd4d2dc513535400b4b3fef0319fb5c4e1f520a7bef743" dependencies = [ "crc32fast", - "miniz_oxide", + "miniz_oxide 0.7.1", ] [[package]] @@ -1639,6 +1663,12 @@ dependencies = [ "wasi 0.11.0+wasi-snapshot-preview1", ] +[[package]] +name = "gimli" +version = "0.27.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" + [[package]] name = "glob" version = "0.3.1" @@ -2398,6 +2428,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "miniz_oxide" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa" +dependencies = [ + "adler", +] + [[package]] name = "miniz_oxide" version = "0.7.1" @@ -2549,6 +2588,15 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "object" +version = "0.30.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03b4680b86d9cfafba8fc491dc9b6df26b68cf40e9e6cd73909194759a63c385" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.17.1" @@ -3272,6 +3320,12 @@ dependencies = [ "serde_json", ] +[[package]] +name = "rustc-demangle" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" + [[package]] name = "rustc-hash" version = "1.1.0" @@ -3752,6 +3806,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "simdutf8" version = "0.1.4" @@ -3819,7 +3882,9 @@ name = "spark" version = "0.1.0" dependencies = [ "api", + "backtrace", "common", + "log", "rpc-client", "serde", "storage", @@ -4154,6 +4219,7 @@ dependencies = [ "mio", "num_cpus", "pin-project-lite", + "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.48.0", diff --git a/Cargo.toml b/Cargo.toml index fe57d3c..13448a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,8 +11,10 @@ rpc-client = { path = "./rpc-client" } storage = { path = "./storage" } tx-builder = { path = "./tx-builder" } +backtrace = "0.3" +log = "0.4" serde = { version = "1.0", features = ["derive"] } -tokio = { version = "1.28", features = ["macros", "rt"] } +tokio = { version = "1.28", features = ["macros", "signal", "rt"] } toml = "0.7" [workspace] diff --git a/common/src/types/relation_db/transaction.rs b/common/src/types/relation_db/transaction.rs index bf22b92..b12554d 100644 --- a/common/src/types/relation_db/transaction.rs +++ b/common/src/types/relation_db/transaction.rs @@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Deserialize, Serialize)] -#[sea_orm(table_name = "transaction")] +#[sea_orm(table_name = "transaction_")] pub struct Model { #[sea_orm(primary_key)] pub id: u32, diff --git a/config.toml b/config.toml index 939c891..bb05f5f 100644 --- a/config.toml +++ b/config.toml @@ -1,4 +1,4 @@ private_key = "" -rpc_listening_address = "127.0.0.1:8000" -rdb_url = "" +rpc_listen_address = "127.0.0.1:8000" +rdb_url = "sqlite:/free-space/spark.db" kvdb_path = "free-space/db" diff --git a/create_table.sql b/create_table.sql new file mode 100644 index 0000000..d2b02ab --- /dev/null +++ b/create_table.sql @@ -0,0 +1,15 @@ +CREATE TABLE IF NOT EXISTS TRANSACTION_( + ID INTEGER PRIMARY KEY, + ADDRESS TEXT, + TIMESTAMP INT, + EVENT INT, + TX_HASH TEXT, + TOTAL_AMOUNT INT, + STAKE_AMOUNT INT, + DELEGATE_AMOUNT INT, + WITHDRAWABLE_AMOUNT INT, + STAKE_RATE TEXT, + DELEGATE_RATE TEXT, + EPOCH INT, + STATUS INT +); diff --git a/src/main.rs b/src/main.rs index af5032b..0f73314 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,13 @@ mod config; -use std::{env, sync::Arc}; +use std::{env, panic::PanicInfo, sync::Arc}; use api::{run_server, DefaultAPIAdapter}; +use backtrace::Backtrace; use config::SparkConfig; use storage::{SmtManager, TransactionHistory}; +#[cfg(unix)] +use tokio::signal::unix as os_impl; #[tokio::main] async fn main() { @@ -14,9 +17,63 @@ async fn main() { let rdb = Arc::new(TransactionHistory::new(&config.rdb_url).await); let kvdb = Arc::new(SmtManager::new(&config.kvdb_path)); let api_adapter = Arc::new(DefaultAPIAdapter::new(rdb, kvdb)); - let _handle = run_server(api_adapter, config.rpc_listen_address) + let handle = run_server(api_adapter, config.rpc_listen_address) .await .unwrap(); - println!("Hello, world!"); + tokio::spawn(handle.stopped()); + + set_ctrl_c_handle().await; +} + +async fn set_ctrl_c_handle() { + let ctrl_c_handler = tokio::spawn(async { + #[cfg(windows)] + let _ = tokio::signal::ctrl_c().await; + #[cfg(unix)] + { + let mut sigtun_int = os_impl::signal(os_impl::SignalKind::interrupt()).unwrap(); + let mut sigtun_term = os_impl::signal(os_impl::SignalKind::terminate()).unwrap(); + tokio::select! { + _ = sigtun_int.recv() => {} + _ = sigtun_term.recv() => {} + }; + } + }); + + // register channel of panic + let (panic_sender, mut panic_receiver) = tokio::sync::mpsc::channel::<()>(1); + + std::panic::set_hook(Box::new(move |info: &PanicInfo| { + let panic_sender = panic_sender.clone(); + panic_log(info); + panic_sender.try_send(()).expect("panic_receiver is droped"); + })); + + tokio::select! { + _ = ctrl_c_handler => { log::info!("ctrl + c is pressed, quit.") }, + _ = panic_receiver.recv() => { log::info!("child thread panic, quit.") }, + }; +} + +fn panic_log(info: &PanicInfo) { + let backtrace = Backtrace::new(); + let thread = std::thread::current(); + let name = thread.name().unwrap_or("unnamed"); + let location = info.location().unwrap(); // The current implementation always returns Some + let msg = match info.payload().downcast_ref::<&'static str>() { + Some(s) => *s, + None => match info.payload().downcast_ref::() { + Some(s) => &**s, + None => "Box", + }, + }; + log::error!( + target: "panic", "thread '{}' panicked at '{}': {}:{} {:?}", + name, + msg, + location.file(), + location.line(), + backtrace, + ); }