-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathmain.rs
More file actions
302 lines (266 loc) · 11 KB
/
main.rs
File metadata and controls
302 lines (266 loc) · 11 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
mod args;
use args::GlobalArgs;
use clap::{Parser, Subcommand};
use kora_lib::{
admin::token_util::initialize_atas,
error::KoraError,
log::LoggingFormat,
rpc::get_rpc_client,
rpc_server::{run_rpc_server, server::ServerHandles, KoraRpc, RpcArgs},
signer::init::init_signers,
state::init_config,
validator::config_validator::ConfigValidator,
CacheUtil, Config,
};
#[cfg(feature = "docs")]
use kora_lib::rpc_server::openapi::docs;
#[cfg(feature = "docs")]
use utoipa::OpenApi;
#[derive(Subcommand)]
enum Commands {
/// Configuration management commands
Config {
#[command(subcommand)]
config_command: ConfigCommands,
},
/// RPC server operations
Rpc {
#[command(subcommand)]
rpc_command: RpcCommands,
},
/// Generate OpenAPI documentation
#[cfg(feature = "docs")]
Openapi {
/// Output path for the OpenAPI spec file
#[arg(short = 'o', long, default_value = "openapi.json")]
output: String,
},
}
#[derive(Subcommand)]
enum ConfigCommands {
/// Validate configuration file (fast, no RPC calls)
Validate {
/// Path to signers configuration file (optional)
#[arg(long)]
signers_config: Option<std::path::PathBuf>,
},
/// Validate configuration file with RPC validation (slower but more thorough)
ValidateWithRpc {
/// Path to signers configuration file (optional)
#[arg(long)]
signers_config: Option<std::path::PathBuf>,
},
}
#[derive(Subcommand)]
enum RpcCommands {
/// Start the RPC server
#[command(
about = "Start the RPC server",
long_about = "Start the Kora RPC server to handle gasless transactions.\n\nThe server will validate the configuration and initialize the specified signer before starting."
)]
Start {
#[command(flatten)]
rpc_args: Box<RpcArgs>,
},
/// Initialize ATAs for all allowed payment tokens
#[command(
about = "Initialize ATAs for all allowed payment tokens",
long_about = "Initialize Associated Token Accounts (ATAs) for all payment tokens configured in the system.\n\nThis command creates ATAs in the following priority order:\n1. If a payment address is configured, creates ATAs for that address only\n2. Otherwise, creates ATAs for ALL signers in the pool\n\nYou can specify which signer to use as the fee payer for the ATA creation transactions.\nIf no fee payer is specified, the first signer in the pool will be used."
)]
InitializeAtas {
#[command(flatten)]
rpc_args: Box<RpcArgs>,
/// Signer key to use as fee payer (defaults to first signer if not specified)
#[arg(long, help_heading = "Signer Options")]
fee_payer_key: Option<String>,
/// Compute unit price for priority fees (in micro-lamports)
#[arg(long, help_heading = "Transaction Options")]
compute_unit_price: Option<u64>,
/// Compute unit limit for transactions
#[arg(long, help_heading = "Transaction Options")]
compute_unit_limit: Option<u32>,
/// Number of ATAs to create per transaction
#[arg(long, help_heading = "Transaction Options")]
chunk_size: Option<usize>,
},
}
#[derive(Parser)]
#[command(author, version, about = "Kora - Solana gasless transaction relayer", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
#[command(flatten)]
pub global_args: GlobalArgs,
}
#[tokio::main]
async fn main() -> Result<(), KoraError> {
dotenv::dotenv().ok();
let cli = Cli::parse();
let config = Config::load_config(&cli.global_args.config).unwrap_or_else(|e| {
print_error(&format!("Failed to load config: {e}"));
std::process::exit(1);
});
init_config(config).unwrap_or_else(|e| {
print_error(&format!("Failed to initialize config: {e}"));
std::process::exit(1);
});
let rpc_client = get_rpc_client(&cli.global_args.rpc_url);
match cli.command {
Some(Commands::Config { config_command }) => {
match config_command {
ConfigCommands::Validate { signers_config } => {
let _ = ConfigValidator::validate_with_result_and_signers(
rpc_client.as_ref(),
true,
signers_config.as_ref(),
)
.await;
}
ConfigCommands::ValidateWithRpc { signers_config } => {
let _ = ConfigValidator::validate_with_result_and_signers(
rpc_client.as_ref(),
false,
signers_config.as_ref(),
)
.await;
}
}
std::process::exit(0);
}
Some(Commands::Rpc { rpc_command }) => {
match rpc_command {
RpcCommands::Start { rpc_args } => {
// Validate config and signers before starting server
match ConfigValidator::validate_with_result_and_signers(
rpc_client.as_ref(),
true,
rpc_args.signers_config.as_ref(),
)
.await
{
Err(errors) => {
for e in errors {
print_error(&format!("Validation error: {e}"));
}
std::process::exit(1);
}
Ok(warnings) => {
for w in warnings {
println!("Warning: {w}");
}
}
}
setup_logging(&rpc_args.logging_format);
// Initialize signer(s) - supports both single and multi-signer modes
if !rpc_args.skip_signer {
init_signers(&rpc_args).await.unwrap_or_else(|e| {
print_error(&format!("Failed to initialize signer(s): {e}"));
std::process::exit(1);
});
}
// Initialize cache
if let Err(e) = CacheUtil::init().await {
print_error(&format!("Failed to initialize cache: {e}"));
std::process::exit(1);
}
let rpc_client = get_rpc_client(&cli.global_args.rpc_url);
let kora_rpc = KoraRpc::new(rpc_client);
let ServerHandles { rpc_handle, metrics_handle, balance_tracker_handle } =
run_rpc_server(kora_rpc, rpc_args.port).await?;
if let Err(e) = tokio::signal::ctrl_c().await {
panic!("Error waiting for Ctrl+C signal: {e:?}");
}
println!("Shutting down server...");
// Stop the balance tracker task
if let Some(handle) = balance_tracker_handle {
log::info!("Stopping balance tracker background task...");
handle.abort();
}
// Stop the RPC server
if let Err(e) = rpc_handle.stop() {
panic!("Error stopping RPC server: {e:?}");
}
// Stop the metrics server if running
if let Some(handle) = metrics_handle {
if let Err(e) = handle.stop() {
panic!("Error stopping metrics server: {e:?}");
}
}
}
RpcCommands::InitializeAtas {
rpc_args,
fee_payer_key,
compute_unit_price,
compute_unit_limit,
chunk_size,
} => {
if !rpc_args.skip_signer {
init_signers(&rpc_args).await.unwrap_or_else(|e| {
print_error(&format!("Failed to initialize signer(s): {e}"));
std::process::exit(1);
});
} else {
print_error("Cannot initialize ATAs without a signer.");
std::process::exit(1);
}
// Initialize cache
if let Err(e) = CacheUtil::init().await {
print_error(&format!("Failed to initialize cache: {e}"));
std::process::exit(1);
}
// Initialize ATAs
if let Err(e) = initialize_atas(
rpc_client.as_ref(),
compute_unit_price,
compute_unit_limit,
chunk_size,
fee_payer_key,
)
.await
{
print_error(&format!("Failed to initialize ATAs: {e}"));
std::process::exit(1);
}
println!("Successfully initialized all payment ATAs");
}
}
}
#[cfg(feature = "docs")]
Some(Commands::Openapi { output }) => {
docs::update_docs();
let openapi_spec = docs::ApiDoc::openapi();
let json = serde_json::to_string_pretty(&openapi_spec).unwrap_or_else(|e| {
print_error(&format!("Failed to serialize OpenAPI spec: {e}"));
std::process::exit(1);
});
std::fs::write(&output, json).unwrap_or_else(|e| {
print_error(&format!("Failed to write OpenAPI spec to {}: {e}", output));
std::process::exit(1);
});
println!("OpenAPI spec written to: {}", output);
}
None => {
println!("No command specified. Use --help for usage information.");
println!("Available commands:");
println!(" config validate - Validate configuration");
println!(" config validate-with-rpc - Validate configuration with RPC calls");
println!(" rpc start - Start RPC server");
println!(" rpc initialize-atas - Initialize ATAs for payment tokens");
#[cfg(feature = "docs")]
println!(" openapi - Generate OpenAPI documentation");
}
}
Ok(())
}
fn print_error(message: &str) {
eprintln!("Error: {message}");
}
fn setup_logging(format: &LoggingFormat) {
let env_filter = std::env::var("RUST_LOG")
.unwrap_or_else(|_| "info,sqlx=error,sea_orm_migration=error,jsonrpsee_server=warn".into());
let subscriber = tracing_subscriber::fmt().with_env_filter(env_filter);
match format {
LoggingFormat::Standard => subscriber.init(),
LoggingFormat::Json => subscriber.json().init(),
}
}