Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
merge upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
frisitano committed Jun 17, 2024
1 parent e4baf7e commit 8752d77
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 392 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ alloy = { git = "https://github.com/alloy-rs/alloy", features = [

# zk-evm dependencies
plonky2 = "0.2.2"
evm_arithmetization = { git = "https://github.com/fractal-zkp/zk_evm.git", branch = "feat/partial_trie_builder" }
mpt_trie = { git = "https://github.com/fractal-zkp/zk_evm.git", branch = "feat/partial_trie_builder" }
trace_decoder = { git = "https://github.com/fractal-zkp/zk_evm.git", branch = "feat/partial_trie_builder" }
proof_gen = { git = "https://github.com/fractal-zkp/zk_evm.git", branch = "feat/partial_trie_builder" }
evm_arithmetization = { git = "https://github.com/0xPolygonZero/zk_evm.git", tag = "v0.4.0" }
mpt_trie = { git = "https://github.com/0xPolygonZero/zk_evm.git", tag = "v0.4.0" }
trace_decoder = { git = "https://github.com/0xPolygonZero/zk_evm.git", tag = "v0.4.0" }
proof_gen = { git = "https://github.com/0xPolygonZero/zk_evm.git", tag = "v0.4.0" }

[workspace.package]
edition = "2021"
Expand Down
25 changes: 19 additions & 6 deletions leader/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) struct Cli {
pub(crate) prover_state_config: CliProverStateConfig,
}

#[derive(Subcommand)]
#[derive(Subcommand, Clone)]
pub(crate) enum Command {
/// Reads input from stdin and writes output to stdout.
Stdio {
Expand All @@ -35,22 +35,35 @@ pub(crate) enum Command {
// The Jerigon RPC URL.
#[arg(long, short = 'u', value_hint = ValueHint::Url)]
rpc_url: Url,
/// The block number for which to generate a proof.
#[arg(short, long)]
block_number: u64,
/// The block interval for which to generate a proof.
#[arg(long, short = 'i')]
block_interval: String,
/// The checkpoint block number.
#[arg(short, long, default_value_t = 0)]
checkpoint_block_number: u64,
/// The previous proof output.
#[arg(long, short = 'f', value_hint = ValueHint::FilePath)]
previous_proof: Option<PathBuf>,
/// If provided, write the generated proof to this file instead of
/// If provided, write the generated proofs to this directory instead of
/// stdout.
#[arg(long, short = 'o', value_hint = ValueHint::FilePath)]
proof_output_path: Option<PathBuf>,
proof_output_dir: Option<PathBuf>,
/// If true, save the public inputs to disk on error.
#[arg(short, long, default_value_t = false)]
save_inputs_on_error: bool,
/// Network block time in milliseconds. This value is used
/// to determine the blockchain node polling interval.
#[arg(short, long, env = "ZERO_BIN_BLOCK_TIME", default_value_t = 2000)]
block_time: u64,
/// Keep intermediate proofs. Default action is to
/// delete them after the final proof is generated.
#[arg(
short,
long,
env = "ZERO_BIN_KEEP_INTERMEDIATE_PROOFS",
default_value_t = false
)]
keep_intermediate_proofs: bool,
/// Backoff in milliseconds for request retries
#[arg(long, default_value_t = 0)]
backoff: u64,
Expand Down
160 changes: 104 additions & 56 deletions leader/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,118 @@
use std::{
fs::{create_dir_all, File},
io::Write,
path::PathBuf,
sync::Arc,
};
use std::io::Write;
use std::path::PathBuf;

use alloy::transports::http::reqwest::Url;
use anyhow::Result;
use common::block_interval::BlockInterval;
use common::fs::generate_block_proof_file_name;
use paladin::runtime::Runtime;
use proof_gen::types::PlonkyProofIntern;
use rpc::retry::build_http_retry_provider;
use proof_gen::proof_types::GeneratedBlockProof;
use rpc::{retry::build_http_retry_provider, RpcType};
use tracing::{error, info, warn};

/// The main function for the jerigon mode.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn rpc_main(
rpc_type: &str,
rpc_url: Url,
#[derive(Debug)]
pub struct RpcParams {
pub rpc_url: Url,
pub rpc_type: RpcType,
pub backoff: u64,
pub max_retries: u32,
}

#[derive(Debug, Default)]
pub struct ProofParams {
pub checkpoint_block_number: u64,
pub previous_proof: Option<GeneratedBlockProof>,
pub proof_output_dir: Option<PathBuf>,
pub save_inputs_on_error: bool,
pub keep_intermediate_proofs: bool,
}

/// The main function for the client.
pub(crate) async fn client_main(
runtime: Runtime,
block_number: u64,
checkpoint_block_number: u64,
previous: Option<PlonkyProofIntern>,
proof_output_path_opt: Option<PathBuf>,
save_inputs_on_error: bool,
backoff: u64,
max_retries: u32,
) -> anyhow::Result<()> {
let prover_input = match rpc_type {
"jerigon" => {
rpc::jerigon::prover_input(
build_http_retry_provider(rpc_url, backoff, max_retries),
block_number.into(),
checkpoint_block_number.into(),
)
.await?
}
"native" => {
rpc::native::prover_input(
Arc::new(build_http_retry_provider(rpc_url, backoff, max_retries)),
block_number.into(),
checkpoint_block_number.into(),
)
.await?
}
_ => unreachable!(),
};
rpc_params: RpcParams,
block_interval: BlockInterval,
mut params: ProofParams,
) -> Result<()> {
let prover_input = rpc::prover_input(
build_http_retry_provider(
rpc_params.rpc_url,
rpc_params.backoff,
rpc_params.max_retries,
),
block_interval,
params.checkpoint_block_number.into(),
rpc_params.rpc_type,
)
.await?;

if cfg!(feature = "test_only") {
info!("All proof witnesses have been generated successfully.");
} else {
info!("All proofs have been generated successfully.");
}

let proof = prover_input
.prove(&runtime, previous, save_inputs_on_error)
// If `keep_intermediate_proofs` is not set we only keep the last block
// proof from the interval. It contains all the necessary information to
// verify the whole sequence.
let proved_blocks = prover_input
.prove(
&runtime,
params.previous_proof.take(),
params.save_inputs_on_error,
params.proof_output_dir.clone(),
)
.await;
runtime.close().await?;
let proved_blocks = proved_blocks?;

let proof = serde_json::to_vec(&proof?.intern)?;
write_proof(proof, proof_output_path_opt)
}

fn write_proof(proof: Vec<u8>, proof_output_path_opt: Option<PathBuf>) -> anyhow::Result<()> {
match proof_output_path_opt {
Some(p) => {
if let Some(parent) = p.parent() {
create_dir_all(parent)?;
}

let mut f = File::create(p)?;
f.write_all(&proof)?;
if params.keep_intermediate_proofs {
if params.proof_output_dir.is_some() {
// All proof files (including intermediary) are written to disk and kept
warn!("Skipping cleanup, intermediate proof files are kept");
} else {
// Output all proofs to stdout
std::io::stdout().write_all(&serde_json::to_vec(
&proved_blocks
.into_iter()
.filter_map(|(_, block)| block)
.collect::<Vec<_>>(),
)?)?;
}
} else if let Some(proof_output_dir) = params.proof_output_dir.as_ref() {
// Remove intermediary proof files
proved_blocks
.into_iter()
.rev()
.skip(1)
.map(|(block_number, _)| {
generate_block_proof_file_name(&proof_output_dir.to_str(), block_number)
})
.for_each(|path| {
if let Err(e) = std::fs::remove_file(path) {
error!("Failed to remove intermediate proof file: {e}");
}
});
} else {
// Output only last proof to stdout
if let Some(last_block) = proved_blocks
.into_iter()
.filter_map(|(_, block)| block)
.last()
{
std::io::stdout().write_all(&serde_json::to_vec(&last_block)?)?;
}
None => std::io::stdout().write_all(&proof)?,
}

Ok(())
}

impl From<super::cli::Command> for RpcType {
fn from(command: super::cli::Command) -> Self {
match command {
super::cli::Command::Native { .. } => RpcType::Native,
super::cli::Command::Jerigon { .. } => RpcType::Jerigon,
_ => panic!("Unsupported command type"),
}
}
}
39 changes: 15 additions & 24 deletions leader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use std::{fs::File, path::PathBuf};
use anyhow::Result;
use clap::Parser;
use cli::Command;
use client::RpcParams;
use common::block_interval::BlockInterval;
use dotenvy::dotenv;
use ops::register;
use paladin::runtime::Runtime;
use proof_gen::proof_types::GeneratedBlockProof;
use tracing::info;

use crate::jerigon::{jerigon_main, ProofParams};
use crate::client::{client_main, ProofParams};
use crate::utils::get_package_version;

mod cli;
Expand Down Expand Up @@ -62,7 +63,7 @@ async fn main() -> Result<()> {

let runtime = Runtime::from_config(&args.paladin, register()).await?;

match args.command {
match args.command.clone() {
Command::Stdio {
previous_proof,
save_inputs_on_error,
Expand Down Expand Up @@ -97,30 +98,16 @@ async fn main() -> Result<()> {
keep_intermediate_proofs,
backoff,
max_retries,
} => {
let previous_proof = get_previous_proof(previous_proof)?;

client::rpc_main(
"jerigon",
rpc_url,
runtime,
block_number,
checkpoint_block_number,
previous_proof,
proof_output_path,
save_inputs_on_error,
backoff,
max_retries,
)
.await?;
}
Command::Native {
| Command::Native {
rpc_url,
block_number,
block_interval,
checkpoint_block_number,
previous_proof,
proof_output_path,
proof_output_dir,
save_inputs_on_error,
block_time,
keep_intermediate_proofs,
backoff,
max_retries,
} => {
Expand All @@ -136,8 +123,14 @@ async fn main() -> Result<()> {
}

info!("Proving interval {block_interval}");
jerigon_main(
client_main(
runtime,
RpcParams {
rpc_url,
rpc_type: args.command.into(),
backoff,
max_retries,
},
block_interval,
ProofParams {
checkpoint_block_number,
Expand All @@ -146,8 +139,6 @@ async fn main() -> Result<()> {
save_inputs_on_error,
keep_intermediate_proofs,
},
backoff,
max_retries,
)
.await?;
}
Expand Down
Loading

0 comments on commit 8752d77

Please sign in to comment.