Skip to content

Commit

Permalink
chore(starknet_committer_and_os_cli): moved block-hash logic into sep…
Browse files Browse the repository at this point in the history
…arate folder (#3842)
  • Loading branch information
amosStarkware authored Feb 3, 2025
1 parent 2548a08 commit 7705fb7
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 39 deletions.
1 change: 1 addition & 0 deletions crates/starknet_committer_and_os_cli/src/block_hash_cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod run_block_hash_cli;
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use clap::{Parser, Subcommand};
use starknet_api::block_hash::block_hash_calculator::{
calculate_block_commitments,
calculate_block_hash,
};
use tracing::info;

use crate::committer_cli::block_hash::{BlockCommitmentsInput, BlockHashInput};
use crate::shared_utils::read::{load_input, write_to_file};
use crate::shared_utils::types::IoArgs;

#[derive(Parser, Debug)]
pub struct BlockHashCliCommand {
#[clap(subcommand)]
command: Command,
}

#[derive(Debug, Subcommand)]
enum Command {
/// Calculates the block hash.
BlockHash {
#[clap(flatten)]
io_args: IoArgs,
},
/// Calculates commitments needed for the block hash.
BlockHashCommitments {
#[clap(flatten)]
io_args: IoArgs,
},
}

pub async fn run_block_hash_cli(block_hash_cli_command: BlockHashCliCommand) {
info!("Starting block-hash-cli with command: \n{:?}", block_hash_cli_command);
match block_hash_cli_command.command {
Command::BlockHash { io_args: IoArgs { input_path, output_path } } => {
let block_hash_input: BlockHashInput = load_input(input_path);
info!("Successfully loaded block hash input.");
let block_hash =
calculate_block_hash(block_hash_input.header, block_hash_input.block_commitments)
.unwrap_or_else(|error| panic!("Failed to calculate block hash: {}", error));
write_to_file(&output_path, &block_hash);
info!("Successfully computed block hash {:?}.", block_hash);
}

Command::BlockHashCommitments { io_args: IoArgs { input_path, output_path } } => {
let commitments_input: BlockCommitmentsInput = load_input(input_path);
info!("Successfully loaded block hash commitment input.");
let commitments = calculate_block_commitments(
&commitments_input.transactions_data,
&commitments_input.state_diff,
commitments_input.l1_da_mode,
&commitments_input.starknet_version,
);
write_to_file(&output_path, &commitments);
info!("Successfully computed block hash commitment: \n{:?}", commitments);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
use clap::{Parser, Subcommand};
use starknet_api::block_hash::block_hash_calculator::{
calculate_block_commitments,
calculate_block_hash,
};
use tracing::info;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::reload::Handle;
use tracing_subscriber::Registry;

use crate::committer_cli::block_hash::{BlockCommitmentsInput, BlockHashInput};
use crate::committer_cli::commands::parse_and_commit;
use crate::committer_cli::tests::python_tests::PythonTest;
use crate::shared_utils::read::{load_input, read_input, write_to_file};
use crate::shared_utils::read::{read_input, write_to_file};
use crate::shared_utils::types::IoArgs;

#[derive(Parser, Debug)]
Expand All @@ -22,16 +17,6 @@ pub struct CommitterCliCommand {

#[derive(Debug, Subcommand)]
enum Command {
/// Calculates the block hash.
BlockHash {
#[clap(flatten)]
io_args: IoArgs,
},
/// Calculates commitments needed for the block hash.
BlockHashCommitments {
#[clap(flatten)]
io_args: IoArgs,
},
/// Given previous state tree skeleton and a state diff, computes the new commitment.
Commit {
#[clap(flatten)]
Expand Down Expand Up @@ -72,28 +57,5 @@ pub async fn run_committer_cli(
// Write test's output.
write_to_file(&output_path, &output);
}

Command::BlockHash { io_args: IoArgs { input_path, output_path } } => {
let block_hash_input: BlockHashInput = load_input(input_path);
info!("Successfully loaded block hash input.");
let block_hash =
calculate_block_hash(block_hash_input.header, block_hash_input.block_commitments)
.unwrap_or_else(|error| panic!("Failed to calculate block hash: {}", error));
write_to_file(&output_path, &block_hash);
info!("Successfully computed block hash {:?}.", block_hash);
}

Command::BlockHashCommitments { io_args: IoArgs { input_path, output_path } } => {
let commitments_input: BlockCommitmentsInput = load_input(input_path);
info!("Successfully loaded block hash commitment input.");
let commitments = calculate_block_commitments(
&commitments_input.transactions_data,
&commitments_input.state_diff,
commitments_input.l1_da_mode,
&commitments_input.starknet_version,
);
write_to_file(&output_path, &commitments);
info!("Successfully computed block hash commitment: \n{:?}", commitments);
}
}
}
1 change: 1 addition & 0 deletions crates/starknet_committer_and_os_cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod block_hash_cli;
pub mod committer_cli;
pub mod os_cli;
pub mod shared_utils;
Expand Down
9 changes: 9 additions & 0 deletions crates/starknet_committer_and_os_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use clap::{Args, Parser, Subcommand};
use starknet_committer_and_os_cli::block_hash_cli::run_block_hash_cli::{
run_block_hash_cli,
BlockHashCliCommand,
};
use starknet_committer_and_os_cli::committer_cli::run_committer_cli::{
run_committer_cli,
CommitterCliCommand,
Expand All @@ -22,6 +26,8 @@ struct CliArgs {
enum CommitterOrOsCommand {
/// Run Committer CLI.
Committer(CommitterCliCommand),
/// Run BlockHash CLI.
BlockHash(BlockHashCliCommand),
/// Run OS CLI.
OS(OsCliCommand),
}
Expand All @@ -46,5 +52,8 @@ async fn main() {
CommitterOrOsCommand::OS(command) => {
run_os_cli(command, log_filter_handle).await;
}
CommitterOrOsCommand::BlockHash(command) => {
run_block_hash_cli(command).await;
}
}
}

0 comments on commit 7705fb7

Please sign in to comment.