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

Commit

Permalink
Add an option for passing in a previous proof to the prover
Browse files Browse the repository at this point in the history
  • Loading branch information
cpubot committed Nov 17, 2023
1 parent 136dbbd commit 568b819
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 21 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,24 @@ cargo r --bin leader -- --help
Usage: leader [OPTIONS]
Options:
-m, --mode <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 <PORT> The port to listen on when using the `http` mode [default: 8080]
-o, --output-dir <OUTPUT_DIR> The directory to which output should be written (`http` mode only)
--rpc-url <RPC_URL> The RPC URL to use when using the `jerigon` mode
-b, --block-number <BLOCK_NUMBER> The block number to use when using the `jerigon` mode
-r, --runtime <RUNTIME> Specifies the paladin runtime to use [default: amqp] [possible values: amqp, in-memory]
-n, --num-workers <NUM_WORKERS> Specifies the number of worker threads to spawn (in memory runtime only)
-h, --help Print help
-m, --mode <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 <PORT>
The port to listen on when using the `http` mode [default: 8080]
-o, --output-dir <OUTPUT_DIR>
The directory to which output should be written (`http` mode only)
--rpc-url <RPC_URL>
The RPC URL to use when using the `jerigon` mode
-b, --block-number <BLOCK_NUMBER>
The block number to use when using the `jerigon` mode
-r, --runtime <RUNTIME>
Specifies the paladin runtime to use [default: amqp] [possible values: amqp, in-memory]
-n, --num-workers <NUM_WORKERS>
Specifies the number of worker threads to spawn (in memory runtime only)
-p, --previous-proof <PREVIOUS_PROOF>
The previous proof output
-h, --help
Print help
```

### Paladin Runtime
Expand Down
18 changes: 15 additions & 3 deletions common/src/prover_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -28,7 +31,11 @@ impl ProverInput {
self.other_data.b_data.b_meta.block_number
}

pub async fn prove(self, runtime: &Runtime) -> Result<GeneratedBlockProof> {
pub async fn prove(
self,
runtime: &Runtime,
previous: Option<PlonkyProofIntern>,
) -> Result<GeneratedBlockProof> {
let block_number = self.get_block_number();
info!("Proving block {block_number}");

Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions leader/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub(crate) struct Cli {
/// only).
#[arg(long, short)]
pub num_workers: Option<usize>,
/// The previous proof output.
#[arg(long, short)]
pub previous_proof: Option<PathBuf>,
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, ValueEnum, Default)]
Expand Down
15 changes: 11 additions & 4 deletions leader/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -51,16 +52,22 @@ fn write_to_file(
}
}

#[derive(Serialize, Deserialize, Debug)]
struct HttpProverInput {
prover_input: ProverInput,
previous: Option<PlonkyProofIntern>,
}

async fn prove(
Json(payload): Json<ProverInput>,
Json(payload): Json<HttpProverInput>,
runtime: Arc<Runtime>,
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());
Expand Down
10 changes: 8 additions & 2 deletions leader/src/jerigon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlonkyProofIntern>,
) -> 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)?)?;
Expand Down
22 changes: 20 additions & 2 deletions leader/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
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;
mod init;
mod jerigon;
mod stdio;

fn get_previous_proof(path: Option<PathBuf>) -> Result<Option<PlonkyProofIntern>> {
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();
Expand All @@ -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
Expand All @@ -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?;
}
}

Expand Down
8 changes: 6 additions & 2 deletions leader/src/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlonkyProofIntern>,
) -> 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?;

Expand Down

0 comments on commit 568b819

Please sign in to comment.