-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
110 lines (91 loc) · 3.21 KB
/
main.rs
File metadata and controls
110 lines (91 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//! ASM Runner Binary
//!
//! Standalone binary that runs the ASM (Anchor State Machine) STF and exposes an RPC API
//! for querying ASM state.
mod block_watcher;
mod bootstrap;
mod config;
mod prover;
mod rpc_server;
mod storage;
mod worker_context;
use std::{fs::read_to_string, path::PathBuf, time::Duration};
use anyhow::Result;
use clap::Parser;
use strata_asm_params::AsmParams;
use strata_tasks::TaskManager;
use tokio::runtime::Builder;
use tracing::info;
use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};
use zkaleido_native_adapter as _;
use crate::{bootstrap::bootstrap, config::AsmRpcConfig};
/// Timeout for graceful shutdown.
const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(30);
/// ASM Runner - Run the ASM STF and expose RPC API
#[derive(Parser, Debug)]
#[command(name = "asm-runner")]
#[command(about = "ASM runner for executing ASM STF", long_about = None)]
struct Cli {
/// Path to configuration file
#[arg(short, long, default_value = "config.toml")]
config: PathBuf,
/// Path to ASM params JSON file
#[arg(short, long, default_value = "asm-params.json")]
params: PathBuf,
}
fn main() {
// 1. Initialize logging
init_logging();
// 2. Parse CLI args
let cli = Cli::parse();
// 3. Load configuration
let config = load_config(&cli.config).expect("Failed to load config");
// 4. Load ASM params
let params = load_params(&cli.params).expect("Failed to load ASM params");
info!(
"Starting ASM RPC server with config: {:?}, params: {:?}",
config, params
);
// 5. Create tokio runtime
let runtime = Builder::new_multi_thread()
.enable_all()
.build()
.expect("Failed to create tokio runtime");
// 6. Create task manager and start signal listeners
let task_manager = TaskManager::new(runtime.handle().clone());
task_manager.start_signal_listeners();
let executor = task_manager.create_executor();
// 7. Spawn the main async initialization and server logic as a critical task
let executor_clone = executor.clone();
executor.spawn_critical_async("main_task", async move {
bootstrap(config, params, executor_clone).await
});
// 8. Monitor all tasks and handle shutdown
if let Err(e) = task_manager.monitor(Some(SHUTDOWN_TIMEOUT)) {
panic!("ASM RPC server crashed: {e:?}");
}
tracing::info!("ASM RPC server shutdown complete");
}
/// Load ASM parameters
fn load_params(params_path: &PathBuf) -> Result<AsmParams> {
let contents = read_to_string(params_path)?;
let params: AsmParams = serde_json::from_str(&contents)?;
Ok(params)
}
/// Load configuration from file
fn load_config(path: &PathBuf) -> Result<AsmRpcConfig> {
let contents = read_to_string(path)?;
let config: AsmRpcConfig = toml::from_str(&contents)?;
Ok(config)
}
/// Initialize tracing-based logging with an env filter.
///
/// Honors the `RUST_LOG` environment variable. When unset, defaults to `info`.
fn init_logging() {
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
let layer = fmt::layer().compact();
tracing_subscriber::registry()
.with(filter)
.with(layer)
.init();
}