From 568b819cc418c56973addb0592ef2d035941af15 Mon Sep 17 00:00:00 2001 From: cpu Date: Fri, 17 Nov 2023 13:04:59 -0800 Subject: [PATCH] Add an option for passing in a previous proof to the prover --- README.md | 26 ++++++++++++++++++-------- common/src/prover_input.rs | 18 +++++++++++++++--- leader/src/cli.rs | 3 +++ leader/src/http.rs | 15 +++++++++++---- leader/src/jerigon.rs | 10 ++++++++-- leader/src/main.rs | 22 ++++++++++++++++++++-- leader/src/stdio.rs | 8 ++++++-- 7 files changed, 81 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ca0d0e7f..212ad7f2 100644 --- a/README.md +++ b/README.md @@ -55,14 +55,24 @@ cargo r --bin leader -- --help Usage: leader [OPTIONS] Options: - -m, --mode The input mode. If `std-io`, the input is read from stdin. If `http`, the input is read from HTTP requests. If `jerigon`, the input is read from the `debug_traceBlockByNumber` and `eth_getBlockByNumber` RPC methods from Jerigon [default: std-io] [possible values: std-io, http, jerigon] - -p, --port The port to listen on when using the `http` mode [default: 8080] - -o, --output-dir The directory to which output should be written (`http` mode only) - --rpc-url The RPC URL to use when using the `jerigon` mode - -b, --block-number The block number to use when using the `jerigon` mode - -r, --runtime Specifies the paladin runtime to use [default: amqp] [possible values: amqp, in-memory] - -n, --num-workers Specifies the number of worker threads to spawn (in memory runtime only) - -h, --help Print help + -m, --mode + The input mode. If `std-io`, the input is read from stdin. If `http`, the input is read from HTTP requests. If `jerigon`, the input is read from the `debug_traceBlockByNumber` and `eth_getBlockByNumber` RPC methods from Jerigon [default: std-io] [possible values: std-io, http, jerigon] + -p, --port + The port to listen on when using the `http` mode [default: 8080] + -o, --output-dir + The directory to which output should be written (`http` mode only) + --rpc-url + The RPC URL to use when using the `jerigon` mode + -b, --block-number + The block number to use when using the `jerigon` mode + -r, --runtime + Specifies the paladin runtime to use [default: amqp] [possible values: amqp, in-memory] + -n, --num-workers + Specifies the number of worker threads to spawn (in memory runtime only) + -p, --previous-proof + The previous proof output + -h, --help + Print help ``` ### Paladin Runtime diff --git a/common/src/prover_input.rs b/common/src/prover_input.rs index ec9e7045..092d7832 100644 --- a/common/src/prover_input.rs +++ b/common/src/prover_input.rs @@ -5,7 +5,10 @@ use paladin::{ directive::{Directive, IndexedStream, Literal}, runtime::Runtime, }; -use plonky_block_proof_gen::proof_types::{AggregatableProof, GeneratedBlockProof}; +use plonky_block_proof_gen::{ + proof_types::{AggregatableProof, GeneratedBlockProof}, + types::PlonkyProofIntern, +}; use proof_protocol_decoder::{ processed_block_trace::ProcessingMeta, trace_protocol::BlockTrace, @@ -28,7 +31,11 @@ impl ProverInput { self.other_data.b_data.b_meta.block_number } - pub async fn prove(self, runtime: &Runtime) -> Result { + pub async fn prove( + self, + runtime: &Runtime, + previous: Option, + ) -> Result { let block_number = self.get_block_number(); info!("Proving block {block_number}"); @@ -47,9 +54,14 @@ impl ProverInput { .await?; if let AggregatableProof::Agg(proof) = agg_proof { + let prev = previous.map(|p| GeneratedBlockProof { + b_height: block_number.as_u64() - 1, + intern: p, + }); + let block_proof = Literal(proof) .map(BlockProof { - prev: None, + prev, other: other_data, }) .run(runtime) diff --git a/leader/src/cli.rs b/leader/src/cli.rs index ea90edcd..746be463 100644 --- a/leader/src/cli.rs +++ b/leader/src/cli.rs @@ -30,6 +30,9 @@ pub(crate) struct Cli { /// only). #[arg(long, short)] pub num_workers: Option, + /// The previous proof output. + #[arg(long, short)] + pub previous_proof: Option, } #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, ValueEnum, Default)] diff --git a/leader/src/http.rs b/leader/src/http.rs index 222a673b..71815a1a 100644 --- a/leader/src/http.rs +++ b/leader/src/http.rs @@ -5,7 +5,8 @@ use axum::{http::StatusCode, routing::post, Json, Router}; use common::ProverInput; use ethereum_types::U256; use paladin::runtime::Runtime; -use plonky_block_proof_gen::proof_types::GeneratedBlockProof; +use plonky_block_proof_gen::{proof_types::GeneratedBlockProof, types::PlonkyProofIntern}; +use serde::{Deserialize, Serialize}; use serde_json::to_writer; use tracing::{debug, error, info}; @@ -51,16 +52,22 @@ fn write_to_file( } } +#[derive(Serialize, Deserialize, Debug)] +struct HttpProverInput { + prover_input: ProverInput, + previous: Option, +} + async fn prove( - Json(payload): Json, + Json(payload): Json, runtime: Arc, output_dir: PathBuf, ) -> StatusCode { debug!("Received payload: {:#?}", payload); - let block_number = payload.get_block_number(); + let block_number = payload.prover_input.get_block_number(); - match payload.prove(&runtime).await { + match payload.prover_input.prove(&runtime, payload.previous).await { Ok(b_proof) => match write_to_file(output_dir, block_number, &b_proof) { Ok(file) => { info!("Successfully wrote proof to {}", file.display()); diff --git a/leader/src/jerigon.rs b/leader/src/jerigon.rs index 2fae3dca..368c7314 100644 --- a/leader/src/jerigon.rs +++ b/leader/src/jerigon.rs @@ -2,12 +2,18 @@ use std::io::Write; use anyhow::Result; use paladin::runtime::Runtime; +use plonky_block_proof_gen::types::PlonkyProofIntern; /// The main function for the jerigon mode. -pub(crate) async fn jerigon_main(runtime: Runtime, rpc_url: &str, block_number: u64) -> Result<()> { +pub(crate) async fn jerigon_main( + runtime: Runtime, + rpc_url: &str, + block_number: u64, + previous: Option, +) -> Result<()> { let prover_input = rpc::fetch_prover_input(rpc_url, block_number).await?; - let proof = prover_input.prove(&runtime).await; + let proof = prover_input.prove(&runtime, previous).await; runtime.close().await?; let proof = proof?; std::io::stdout().write_all(&serde_json::to_vec(&proof.intern)?)?; diff --git a/leader/src/main.rs b/leader/src/main.rs index a1d34927..ee9eee84 100644 --- a/leader/src/main.rs +++ b/leader/src/main.rs @@ -1,9 +1,12 @@ +use std::{fs::File, path::PathBuf}; + use anyhow::Result; use clap::Parser; use cli::Mode; use dotenvy::dotenv; use ops::Ops; use paladin::runtime::Runtime; +use plonky_block_proof_gen::types::PlonkyProofIntern; mod cli; mod http; @@ -11,6 +14,18 @@ mod init; mod jerigon; mod stdio; +fn get_previous_proof(path: Option) -> Result> { + if path.is_none() { + return Ok(None); + } + + let path = path.unwrap(); + let file = File::open(path)?; + let des = &mut serde_json::Deserializer::from_reader(&file); + let proof: PlonkyProofIntern = serde_path_to_error::deserialize(des)?; + Ok(Some(proof)) +} + #[tokio::main] async fn main() -> Result<()> { dotenv().ok(); @@ -26,7 +41,8 @@ async fn main() -> Result<()> { match args.mode { Mode::StdIo => { - stdio::stdio_main(runtime).await?; + let previous_proof = get_previous_proof(args.previous_proof)?; + stdio::stdio_main(runtime, previous_proof).await?; } Mode::Http => { let output_dir = args @@ -46,7 +62,9 @@ async fn main() -> Result<()> { let block_number = args .block_number .expect("block-number is required in jerigon mode"); - jerigon::jerigon_main(runtime, &rpc_url, block_number).await?; + let previous_proof = get_previous_proof(args.previous_proof)?; + + jerigon::jerigon_main(runtime, &rpc_url, block_number, previous_proof).await?; } } diff --git a/leader/src/stdio.rs b/leader/src/stdio.rs index 6c44c2b4..28d10b4f 100644 --- a/leader/src/stdio.rs +++ b/leader/src/stdio.rs @@ -3,15 +3,19 @@ use std::io::{Read, Write}; use anyhow::Result; use common::ProverInput; use paladin::runtime::Runtime; +use plonky_block_proof_gen::types::PlonkyProofIntern; /// The main function for the stdio mode. -pub(crate) async fn stdio_main(runtime: Runtime) -> Result<()> { +pub(crate) async fn stdio_main( + runtime: Runtime, + previous: Option, +) -> Result<()> { let mut buffer = String::new(); std::io::stdin().read_to_string(&mut buffer)?; let des = &mut serde_json::Deserializer::from_str(&buffer); let input: ProverInput = serde_path_to_error::deserialize(des)?; - let proof = input.prove(&runtime).await; + let proof = input.prove(&runtime, previous).await; runtime.close().await?; let proof = proof?;