Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change run function #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
# database
*.sqlite

free-space

/devtools/tx-tests/src/config/priv_keys.toml
free-space/
68 changes: 67 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion common/src/types/relation_db/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
@@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rdb_url = "sqlite:./free-space/spark.db"

kvdb_path = "free-space/db"
15 changes: 15 additions & 0 deletions create_table.sql
Original file line number Diff line number Diff line change
@@ -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
);
63 changes: 60 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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::<String>() {
Some(s) => &**s,
None => "Box<Any>",
},
};
log::error!(
target: "panic", "thread '{}' panicked at '{}': {}:{} {:?}",
name,
msg,
location.file(),
location.line(),
backtrace,
);
}