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

Commit

Permalink
Add RPC command
Browse files Browse the repository at this point in the history
  • Loading branch information
cpubot committed Nov 7, 2023
1 parent 287e064 commit 569434a
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 51 deletions.
44 changes: 38 additions & 6 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["leader", "worker", "ops", "verifier"]
members = ["leader", "worker", "common", "ops", "verifier", "rpc"]
resolver = "2"

[workspace.dependencies]
Expand All @@ -11,7 +11,8 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
clap = { version = "4.4.6", features = ["derive"] }
tokio = { version = "1.33.0", features = ["full"] }
serde = "1.0.183"
plonky_block_proof_gen = { git = "https://github.com/0xPolygonZero/plonky-block-proof-gen.git", rev = "bb114bb8a3ba88cdd3c2e3bb65171b6a8e812a5b" }
proof_protocol_decoder = { git = "https://github.com/0xPolygonZero/proof-protocol-decoder.git", rev = "d28e3afa4a7852e68c3cc9e5ee509cc32a5be6c7" }
plonky_block_proof_gen = { git = "https://github.com/0xPolygonZero/plonky-block-proof-gen.git", rev = "dc5bd25f52ab612532df636eacbe9f98d5a052f6" }
proof_protocol_decoder = { git = "https://github.com/0xPolygonZero/proof-protocol-decoder.git", rev = "8a27e8cf860fe71c84e1d052dad4ecfe78bc75dd" }
serde_path_to_error = "0.1.14"
serde_json = "1.0.107"
ethereum-types = "0.14.1"
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ A composition of [`paladin`](https://github.com/0xPolygonZero/paladin) and [`plo
- [stdin](#stdin)
- [HTTP](#http)
- [Jerigon](#jerigon)
- [Docker](#docker)
- [Verifier](#verifier)
- [RPC](#rpc)
- [Docker](#docker)


## Project layout
Expand Down Expand Up @@ -123,9 +124,7 @@ curl -X POST -H "Content-Type: application/json" -d @./block_121.json http://loc
```bash
RUST_LOG=debug cargo r --release --bin leader -- --mode jerigon --runtime in-memory --rpc-url <RPC_URL> --block-number 16 > ./output/proof_16.json
```
## Docker

Docker images are provided for both the [leader](leader.Dockerfile) and [worker](worker.Dockerfile) binaries.

## Verifier

Expand All @@ -142,5 +141,30 @@ Options:

Example:
```bash
cargo r --bin verifier -- -f ./output/proof_16.json
```
cargo r --release --bin verifier -- -f ./output/proof_16.json
```

## RPC

An rpc binary is provided to generate the block trace format expected by the leader.
```
cargo r --bin rpc -- --help
Usage: rpc <COMMAND>
Commands:
fetch Fetch and generate prover input from the RPC endpoint
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
```

Example:
```bash
cargo r --release --bin rpc fetch --rpc-url <RPC_URL> --block-number 16 > ./output/block-16.json
```

## Docker

Docker images are provided for both the [leader](leader.Dockerfile) and [worker](worker.Dockerfile) binaries.
14 changes: 14 additions & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "common"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { workspace = true }
plonky_block_proof_gen = { workspace = true }
proof_protocol_decoder = { workspace = true }
tracing = { workspace = true }
paladin-core = { workspace = true }
ethereum-types = { workspace = true }
anyhow = { workspace = true }
ops = { path = "../ops" }
4 changes: 4 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub const MATIC_CHAIN_ID: u64 = 137;

mod prover_input;
pub use prover_input::ProverInput;
15 changes: 7 additions & 8 deletions leader/src/prover_input.rs → common/src/prover_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@ use proof_protocol_decoder::{
trace_protocol::BlockTrace,
types::{CodeHash, OtherBlockData},
};
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use tracing::info;

#[derive(Debug, Deserialize)]
pub(crate) struct ProverInput {
pub(crate) block_trace: BlockTrace,
pub(crate) other_data: OtherBlockData,
#[derive(Debug, Deserialize, Serialize)]
pub struct ProverInput {
pub block_trace: BlockTrace,
pub other_data: OtherBlockData,
}

fn resolve_code_hash_fn(_: &CodeHash) -> Vec<u8> {
todo!()
}

impl ProverInput {
pub(crate) fn get_block_number(&self) -> U256 {
pub fn get_block_number(&self) -> U256 {
self.other_data.b_data.b_meta.block_number
}

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

Expand Down
9 changes: 3 additions & 6 deletions leader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ serde = { workspace = true }
dotenvy = { workspace = true }
tokio = { workspace = true }
plonky_block_proof_gen = { workspace = true }
proof_protocol_decoder = { workspace = true }
serde_json = { workspace = true }
serde_path_to_error = { workspace = true }
ethereum-types = { workspace = true }
axum = "0.6.20"
reqwest = { version = "0.11.22", features = ["json"] }
thiserror = "1.0.50"
ethereum-types = "0.14.1"
plonky2_evm = { git = "https://github.com/0xPolygonZero/plonky2.git", rev = "49976ea2a98dcb6052bd6cf3a65f730e55727330" }

ops = { path = "../ops" }
common = { path = "../common" }
rpc = { path = "../rpc" }
1 change: 0 additions & 1 deletion leader/src/config.rs

This file was deleted.

3 changes: 1 addition & 2 deletions leader/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use std::{net::SocketAddr, path::PathBuf, sync::Arc};

use anyhow::{bail, Result};
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 serde_json::to_writer;
use tracing::{debug, error, info};

use crate::prover_input::ProverInput;

/// The main function for the HTTP mode.
pub(crate) async fn http_main(runtime: Runtime, port: u16, output_dir: PathBuf) -> Result<()> {
let addr = SocketAddr::from(([0, 0, 0, 0], port));
Expand Down
14 changes: 14 additions & 0 deletions leader/src/jerigon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::io::Write;

use anyhow::Result;
use paladin::runtime::Runtime;

/// The main function for the jerigon mode.
pub(crate) async fn jerigon_main(runtime: Runtime, rpc_url: &str, block_number: u64) -> Result<()> {
let prover_input = rpc::fetch_prover_input(rpc_url, block_number).await?;

let proof = prover_input.prove(&runtime).await?;
std::io::stdout().write_all(&serde_json::to_vec(&proof.intern)?)?;

Ok(())
}
6 changes: 2 additions & 4 deletions leader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ use ops::Ops;
use paladin::runtime::Runtime;

mod cli;
mod config;
mod http;
mod init;
mod prover_input;
mod rpc;
mod jerigon;
mod stdio;

#[tokio::main]
Expand Down Expand Up @@ -47,7 +45,7 @@ async fn main() -> Result<()> {
let block_number = args
.block_number
.expect("block-number is required in jerigon mode");
rpc::rpc_main(runtime, &rpc_url, block_number).await?;
jerigon::jerigon_main(runtime, &rpc_url, block_number).await?;
}
}

Expand Down
3 changes: 1 addition & 2 deletions leader/src/stdio.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::io::{Read, Write};

use anyhow::Result;
use common::ProverInput;
use paladin::runtime::Runtime;

use crate::prover_input::ProverInput;

/// The main function for the stdio mode.
pub(crate) async fn stdio_main(runtime: Runtime) -> Result<()> {
let mut buffer = String::new();
Expand Down
20 changes: 20 additions & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "rpc"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
anyhow = { workspace = true }
serde = { workspace = true }
proof_protocol_decoder = { workspace = true }
serde_json = { workspace = true }
serde_path_to_error = { workspace = true }
clap = { workspace = true }
ethereum-types = { workspace = true }
reqwest = { version = "0.11.22", features = ["json"] }
thiserror = "1.0.50"
plonky2_evm = { git = "https://github.com/0xPolygonZero/plonky2.git", rev = "49976ea2a98dcb6052bd6cf3a65f730e55727330" }
common = { path = "../common" }
20 changes: 20 additions & 0 deletions rpc/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use clap::{Parser, Subcommand, ValueHint};

#[derive(Parser)]
pub(crate) struct Cli {
#[command(subcommand)]
pub(crate) command: Commands,
}

#[derive(Subcommand)]
pub(crate) enum Commands {
/// Fetch and generate prover input from the RPC endpoint
Fetch {
/// The RPC URL
#[arg(short, long, value_hint = ValueHint::Url)]
rpc_url: String,
/// The block number
#[arg(short, long)]
block_number: u64,
},
}
11 changes: 11 additions & 0 deletions rpc/src/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use tracing_subscriber::{fmt::format::FmtSpan, prelude::*, util::SubscriberInitExt, EnvFilter};
pub(crate) fn tracing() {
tracing_subscriber::Registry::default()
.with(
tracing_subscriber::fmt::layer()
.pretty()
.with_span_events(FmtSpan::CLOSE)
.with_filter(EnvFilter::from_default_env()),
)
.init();
}
2 changes: 2 additions & 0 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod rpc;
pub use rpc::fetch_prover_input;
27 changes: 27 additions & 0 deletions rpc/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::io::Write;

use anyhow::Result;
use clap::Parser;
use cli::Commands;
use rpc::fetch_prover_input;

mod cli;
mod init;
mod rpc;

#[tokio::main]
async fn main() -> Result<()> {
init::tracing();
let args = cli::Cli::parse();

match args.command {
Commands::Fetch {
rpc_url,
block_number,
} => {
let prover_input = fetch_prover_input(&rpc_url, block_number).await?;
std::io::stdout().write_all(&serde_json::to_vec(&prover_input)?)?;
}
}
Ok(())
}
Loading

0 comments on commit 569434a

Please sign in to comment.