From d198e4341d07d3c0e16b513a27657438247ba51c Mon Sep 17 00:00:00 2001 From: amos rothberg Date: Thu, 30 Jan 2025 13:43:50 +0200 Subject: [PATCH] chore(starknet_committer_and_os_cli): moved block-hash logic into separate folder --- .../src/block_hash_cli.rs | 1 + .../src/block_hash_cli/run_block_hash_cli.rs | 58 +++++++++++++++++++ .../starknet_committer_and_os_cli/src/lib.rs | 1 + .../starknet_committer_and_os_cli/src/main.rs | 9 +++ 4 files changed, 69 insertions(+) create mode 100644 crates/starknet_committer_and_os_cli/src/block_hash_cli.rs create mode 100644 crates/starknet_committer_and_os_cli/src/block_hash_cli/run_block_hash_cli.rs diff --git a/crates/starknet_committer_and_os_cli/src/block_hash_cli.rs b/crates/starknet_committer_and_os_cli/src/block_hash_cli.rs new file mode 100644 index 00000000000..7d0636f22d8 --- /dev/null +++ b/crates/starknet_committer_and_os_cli/src/block_hash_cli.rs @@ -0,0 +1 @@ +pub mod run_block_hash_cli; diff --git a/crates/starknet_committer_and_os_cli/src/block_hash_cli/run_block_hash_cli.rs b/crates/starknet_committer_and_os_cli/src/block_hash_cli/run_block_hash_cli.rs new file mode 100644 index 00000000000..2adf9eb6dfd --- /dev/null +++ b/crates/starknet_committer_and_os_cli/src/block_hash_cli/run_block_hash_cli.rs @@ -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); + } + } +} diff --git a/crates/starknet_committer_and_os_cli/src/lib.rs b/crates/starknet_committer_and_os_cli/src/lib.rs index 766265e3a57..b1c73f78349 100644 --- a/crates/starknet_committer_and_os_cli/src/lib.rs +++ b/crates/starknet_committer_and_os_cli/src/lib.rs @@ -1,3 +1,4 @@ +pub mod block_hash_cli; pub mod committer_cli; pub mod os_cli; pub mod shared_utils; diff --git a/crates/starknet_committer_and_os_cli/src/main.rs b/crates/starknet_committer_and_os_cli/src/main.rs index 42bfe89b230..d43d6cec7fc 100644 --- a/crates/starknet_committer_and_os_cli/src/main.rs +++ b/crates/starknet_committer_and_os_cli/src/main.rs @@ -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, @@ -22,6 +26,8 @@ struct CliArgs { enum CommitterOrOsCommand { /// Run Committer CLI. Committer(CommitterCliCommand), + /// Run BlockHash CLI. + BlockHash(BlockHashCliCommand), /// Run OS CLI. OS(OsCliCommand), } @@ -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; + } } }