Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into inlinemacros
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Aug 3, 2023
2 parents 10a3897 + 62e2391 commit 1966b7b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 0 additions & 2 deletions crates/katana/core/src/sequencer_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,4 @@ pub enum SequencerError {
DataUnavailable,
#[error("Failed to decode state")]
FailedToDecodeStateDump,
#[error("Failed to set storage")]
FailedToSetStorage,
}
1 change: 0 additions & 1 deletion crates/katana/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub struct KatanaArgs {
pub block_time: Option<u64>,

#[arg(long)]
#[arg(hide = true)]
#[arg(value_name = "PATH")]
#[arg(help = "Dump the state of chain on exit to the given file.")]
#[arg(long_help = "Dump the state of chain on exit to the given file. If the value is a \
Expand Down
34 changes: 29 additions & 5 deletions crates/katana/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::io;
use std::process::exit;
use std::sync::Arc;
use std::{fs, io};

use clap::{CommandFactory, Parser};
use clap_complete::{generate, Shell};
use env_logger::Env;
use katana_core::sequencer::KatanaSequencer;
use katana_core::sequencer::{KatanaSequencer, Sequencer};
use katana_rpc::{spawn, KatanaApi, NodeHandle, StarknetApi};
use log::error;
use log::{error, info};
use tokio::signal::ctrl_c;
use yansi::Paint;

mod args;
Expand Down Expand Up @@ -47,13 +48,17 @@ async fn main() {

print_intro(
accounts,
config.starknet.seed,
config.starknet.seed.clone(),
format!("🚀 JSON-RPC server started: {}", Paint::red(format!("http://{addr}"))),
);
}

sequencer.start().await;
handle.stopped().await;

// Wait until Ctrl + C is pressed, then shutdown
ctrl_c().await.unwrap();
shutdown_handler(sequencer.clone(), config).await;
handle.stop().unwrap();
}
Err(err) => {
error! {"{}", err};
Expand Down Expand Up @@ -104,3 +109,22 @@ ACCOUNTS SEED

println!("\n{address}\n\n");
}

pub async fn shutdown_handler(sequencer: Arc<impl Sequencer>, config: KatanaArgs) {
if let Some(path) = config.dump_state {
info!("Dumping state on shutdown");
let state = (*sequencer).backend().dump_state().await;
if let Ok(state) = state {
match fs::write(path.clone(), state) {
Ok(_) => {
info!("Successfully dumped state")
}
Err(_) => {
error!("Failed to write state dump to {:?}", path)
}
};
} else {
error!("Failed to fetch state dump.")
}
};
}

0 comments on commit 1966b7b

Please sign in to comment.