Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --proof-mode flag #165

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub async fn process_entry_run(args: ProcessArgs) -> Result<()> {
args.sound_run_cairo_file,
args.input_file,
args.cairo_pie_file,
args.proof_mode,
);
let input_string = fs::read_to_string(&config.input_file)?;
let preprocessor_result: ProcessorInput = serde_json::from_str(&input_string)
Expand All @@ -81,13 +82,20 @@ pub async fn process_entry_run(args: ProcessArgs) -> Result<()> {
);
let processor = Processor::new(config.sound_run_program_path.clone());
processor
.process(preprocessor_result, &config.cairo_pie_file)
.process(
preprocessor_result,
config.cairo_pie_file.as_ref(),
config.is_proof_mode,
)
.await?;

info!(
"finished processing the data, saved pie file in {}",
&config.cairo_pie_file.display()
);
match &config.cairo_pie_file {
Some(file_path) => info!(
"finished processing the data, saved pie file in {}",
file_path.display()
),
None => info!("finished processing the data, run in proof mode"),
}

Ok(())
}
Expand All @@ -101,6 +109,7 @@ pub async fn module_entry_run(args: RunModuleArgs) -> Result<()> {
args.save_fetch_keys_file,
args.batch_proof_file,
args.cairo_pie_file,
args.proof_mode,
args.destination_chain_id,
);
let module = Module::new_from_str(
Expand All @@ -126,6 +135,7 @@ pub async fn datalake_entry_run(args: RunDatalakeArgs) -> Result<()> {
None,
args.batch_proof_file,
args.cairo_pie_file,
args.proof_mode,
args.destination_chain_id,
);
let parsed_datalake = match args.datalake {
Expand Down Expand Up @@ -183,6 +193,7 @@ pub async fn entry_run(args: RunArgs) -> Result<()> {
None,
args.batch_proof_file,
args.cairo_pie_file,
args.proof_mode,
parsed.destination_chain_id,
);
let module_registry = ModuleRegistry::new();
Expand Down
14 changes: 12 additions & 2 deletions cli/src/commands/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ pub struct ProcessArgs {
pub sound_run_cairo_file: Option<PathBuf>,

/// Path to save pie file
#[arg(short, long)]
pub cairo_pie_file: PathBuf,
///
/// This will trigger processing(=pie generation) step
#[arg(short, long, requires("input_file"), conflicts_with = "proof_mode")]
pub cairo_pie_file: Option<PathBuf>,

/// Flag to run `cairo-run` in proof mode
///
/// This will trigger processing(=pie generation) step
/// By default, it will run in non-proof mode to generate pie
/// Note that if this flag is set
#[arg(long, default_value_t = false, conflicts_with = "cairo_pie_file")]
pub proof_mode: bool,
}
15 changes: 14 additions & 1 deletion cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ pub struct RunArgs {
/// Path to save pie file
///
/// This will trigger processing(=pie generation) step
#[arg(short, long, requires("program_input_file"))]
#[arg(
short,
long,
requires("program_input_file"),
conflicts_with = "proof_mode"
)]
pub cairo_pie_file: Option<PathBuf>,

/// Flag to run `cairo-run` in proof mode
///
/// This will trigger processing(=pie generation) step
/// By default, it will run in non-proof mode to generate pie
/// Note that if this flag is set
#[arg(long, default_value_t = false, conflicts_with = "cairo_pie_file")]
pub proof_mode: bool,
}
15 changes: 14 additions & 1 deletion cli/src/commands/run_datalake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,22 @@ pub struct RunDatalakeArgs {
/// Path to save pie file
///
/// This will trigger processing(=pie generation) step
#[arg(short, long, requires("program_input_file"))]
#[arg(
short,
long,
requires("program_input_file"),
conflicts_with = "proof_mode"
)]
pub cairo_pie_file: Option<PathBuf>,

/// Flag to run `cairo-run` in proof mode
///
/// This will trigger processing(=pie generation) step
/// By default, it will run in non-proof mode to generate pie
/// Note that if this flag is set
#[arg(long, default_value_t = false, conflicts_with = "cairo_pie_file")]
pub proof_mode: bool,

/// Destination chain id
#[arg(long)]
pub destination_chain_id: ChainId,
Expand Down
15 changes: 14 additions & 1 deletion cli/src/commands/run_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,22 @@ pub struct RunModuleArgs {
/// Path to save pie file
///
/// This will trigger processing(=pie generation) step
#[arg(short, long, requires("program_input_file"))]
#[arg(
short,
long,
requires("program_input_file"),
conflicts_with = "proof_mode"
)]
pub cairo_pie_file: Option<PathBuf>,

/// Flag to run `cairo-run` in proof mode
///
/// This will trigger processing(=pie generation) step
/// By default, it will run in non-proof mode to generate pie
/// Note that if this flag is set
#[arg(long, default_value_t = false, conflicts_with = "cairo_pie_file")]
pub proof_mode: bool,

/// Destination chain id
#[arg(long)]
pub destination_chain_id: ChainId,
Expand Down
1 change: 1 addition & 0 deletions cli/src/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ pub async fn run_interactive() -> anyhow::Result<()> {
None,
Some(output_file),
Some(pie_file),
false,
destination_chain_id,
);

Expand Down
5 changes: 3 additions & 2 deletions hdp/src/cairo_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ pub enum CairoRunnerError {
pub fn cairo_run(
program_path: &Path,
input_string: String,
pie_file_path: &PathBuf,
pie_file_path: Option<&PathBuf>,
is_proof_mode: bool,
) -> Result<run::RunResult, CairoRunnerError> {
let cairo_runner = run::Runner::new(program_path);
cairo_runner.run(input_string, pie_file_path)
cairo_runner.run(input_string, pie_file_path, is_proof_mode)
}

/// Compatible with cairo-run command, performs dry run
Expand Down
53 changes: 35 additions & 18 deletions hdp/src/cairo_runner/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::cairo_runner::CairoRunnerError;
/// Result of run
#[derive(Debug)]
pub struct RunResult {
pub pie_path: PathBuf,
pub pie_path: Option<PathBuf>,
pub cairo_run_output: CairoRunOutput,
}

Expand All @@ -38,21 +38,37 @@ impl Runner {
fn _run(
&self,
input_file_path: &Path,
cairo_pie_file_path: &Path,
cairo_pie_file_path: Option<&PathBuf>,
is_proof_mode: bool,
) -> Result<String, CairoRunnerError> {
let task = Command::new("cairo-run")
.arg("--program")
.arg(&self.program_path)
.arg("--layout")
.arg("starknet_with_keccak")
.arg("--program_input")
.arg(input_file_path)
.arg("--cairo_pie_output")
.arg(cairo_pie_file_path)
.arg("--print_output")
.arg("--print_info")
.stdout(Stdio::piped())
.spawn()?;
let task = if is_proof_mode {
Command::new("cairo-run")
.arg("--program")
.arg(&self.program_path)
.arg("--layout")
.arg("starknet_with_keccak")
.arg("--program_input")
.arg(input_file_path)
.arg("--proof_mode")
.arg("--print_output")
.arg("--print_info")
.stdout(Stdio::piped())
.spawn()?
} else {
Command::new("cairo-run")
.arg("--program")
.arg(&self.program_path)
.arg("--layout")
.arg("starknet_with_keccak")
.arg("--program_input")
.arg(input_file_path)
.arg("--cairo_pie_output")
.arg(cairo_pie_file_path.expect("pie file should be specified in non-proof mode"))
.arg("--print_output")
.arg("--print_info")
.stdout(Stdio::piped())
.spawn()?
};

let output = task.wait_with_output().expect("Failed to read stdout");
let output_str = String::from_utf8_lossy(&output.stdout);
Expand All @@ -63,7 +79,8 @@ impl Runner {
pub fn run(
&self,
input_string: String,
pie_file_path: &PathBuf,
pie_file_path: Option<&PathBuf>,
is_proof_mode: bool,
) -> Result<RunResult, CairoRunnerError> {
if input_string.is_empty() {
return Err(CairoRunnerError::EmptyInput);
Expand All @@ -73,7 +90,7 @@ impl Runner {
let input_file_path = input_file.path();
fs::write(input_file_path, input_string).expect("Failed to write input file");

let output = self._run(input_file_path, pie_file_path)?;
let output = self._run(input_file_path, pie_file_path, is_proof_mode)?;
let cairo_run_output =
self.parse_run(output, &PathBuf::from(SOUND_CAIRO_RUN_OUTPUT_FILE))?;
info!("cairo run output: {:#?}", cairo_run_output);
Expand All @@ -82,7 +99,7 @@ impl Runner {
.expect("Failed to remove cairo run output file");

Ok(RunResult {
pie_path: pie_file_path.to_owned(),
pie_path: pie_file_path.cloned(),
cairo_run_output,
})
}
Expand Down
68 changes: 40 additions & 28 deletions hdp/src/hdp_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct HdpRunConfig {
pub is_cairo_format: bool,
pub batch_proof_file: Option<PathBuf>,
pub cairo_pie_file: Option<PathBuf>,
pub is_proof_mode: bool,
pub save_fetch_keys_file: Option<PathBuf>,
pub destination_chain_id: ChainId,
}
Expand All @@ -36,6 +37,7 @@ impl Default for HdpRunConfig {
program_input_file: "program_input.json".into(),
is_cairo_format: false,
cairo_pie_file: None,
is_proof_mode: false,
batch_proof_file: None,
save_fetch_keys_file: None,
destination_chain_id: ChainId::EthereumSepolia,
Expand All @@ -52,33 +54,37 @@ impl HdpRunConfig {
cli_save_fetch_keys_file: Option<PathBuf>,
batch_proof_file: Option<PathBuf>,
cli_cairo_pie_file: Option<PathBuf>,
cli_is_proof_mode: bool,
destination_chain_id: ChainId,
) -> Self {
let mut provider_config = HashMap::new();

// Iterate through environment variables to find PROVIDER_URL and PROVIDER_CHUNK_SIZE configurations
for (key, value) in env::vars() {
if let Some(stripped_chain_id) = key.strip_prefix("PROVIDER_URL_") {
let chain_id: ChainId = stripped_chain_id
.parse()
.expect("Invalid chain ID in PROVIDER_URL env var");
let provider_url: Url = value.parse().expect("Invalid URL in PROVIDER_URL env var");
match stripped_chain_id.parse() {
Ok(chain_id) => {
let provider_url: Url =
value.parse().expect("Invalid URL in PROVIDER_URL env var");

let chunk_size_key = format!("PROVIDER_CHUNK_SIZE_{}", chain_id);
let provider_chunk_size: u64 = env::var(&chunk_size_key)
.unwrap_or_else(|_| "40".to_string())
.parse()
.unwrap_or_else(|_| panic!("{} must be a number", chunk_size_key));
let chunk_size_key = format!("PROVIDER_CHUNK_SIZE_{}", chain_id);
let provider_chunk_size: u64 = env::var(&chunk_size_key)
.unwrap_or_else(|_| "40".to_string())
.parse()
.unwrap_or_else(|_| panic!("{} must be a number", chunk_size_key));

provider_config.insert(
chain_id,
ProviderConfig {
provider_url,
chain_id,
deployed_on_chain_id: destination_chain_id,
max_requests: provider_chunk_size,
},
);
provider_config.insert(
chain_id,
ProviderConfig {
provider_url,
chain_id,
deployed_on_chain_id: destination_chain_id,
max_requests: provider_chunk_size,
},
);
}
Err(_) => continue,
};
}
}

Expand Down Expand Up @@ -106,6 +112,7 @@ impl HdpRunConfig {
save_fetch_keys_file,
batch_proof_file,
cairo_pie_file: cli_cairo_pie_file,
is_proof_mode: cli_is_proof_mode,
destination_chain_id,
};

Expand Down Expand Up @@ -159,23 +166,27 @@ pub async fn run(hdp_run_config: &HdpRunConfig, tasks: Vec<TaskEnvelope>) -> Res
&hdp_run_config.program_input_file.display()
);

if hdp_run_config.cairo_pie_file.is_none() {
if hdp_run_config.cairo_pie_file.is_none() && !hdp_run_config.is_proof_mode {
Ok(())
} else {
info!("starting processing the data... ");
let pie_file_path = &hdp_run_config
.cairo_pie_file
.clone()
.ok_or_else(|| anyhow::anyhow!("PIE path should be specified"))?;
let processor = Processor::new(hdp_run_config.sound_run_program_path.clone());
processor
.process(preprocessor_result.as_cairo_format(), pie_file_path)
.process(
preprocessor_result.as_cairo_format(),
hdp_run_config.cairo_pie_file.as_ref(),
hdp_run_config.is_proof_mode,
)
.await?;

info!(
"finished processing the data, saved pie file in {}",
pie_file_path.display()
);
match &hdp_run_config.cairo_pie_file {
Some(file_path) => info!(
"finished processing the data, saved pie file in {}",
file_path.display()
),
None => info!("finished processing the data, run in proof mode"),
}

Ok(())
}
}
Expand Down Expand Up @@ -205,6 +216,7 @@ mod tests {
None,
None,
None,
false,
ChainId::EthereumSepolia,
);

Expand Down
Loading