Skip to content
Open
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
53 changes: 53 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ tokio = { version = "1.0", features = ["full"] }
serde_json = "1.0.68"
log = "0.4.17"
env_logger = "0.9.0"
prometheus = "0.13.3"
rocket_prometheus = "0.10.0"

[dev-dependencies]
new = "0.0.0"
Expand Down
84 changes: 44 additions & 40 deletions src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
use blocking::unblock;
use serde::Deserialize;
use serde::Serialize;
use std::process::Command;
use std::process::ExitStatus;
use std::process::Stdio;
use std::sync::{Arc, Mutex};
use tokio::sync::RwLock;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TestData {
Expand Down Expand Up @@ -119,7 +113,6 @@ pub async fn generate_proof(
.output()
.expect("failed to execute process");


log::info!(target: "prover", "npm test output: {:?}", output);

let batchverify_folder = batchverify_folder();
Expand Down Expand Up @@ -225,6 +218,14 @@ pub async fn generate_proof(
.stderr(Stdio::piped())
.output();

match output{
Ok(_) => {}
Err(e) => {
log::error!(target: "prover", "Failed to execute cp proof_{}.json: {}", height, e);
return;
}
}

let output = Command::new("cp")
.args(&[
public_h_file_name.as_str(),
Expand All @@ -234,6 +235,14 @@ pub async fn generate_proof(
.stderr(Stdio::piped())
.output();

match output{
Ok(_) => {}
Err(e) => {
log::error!(target: "prover", "Failed to execute cp public_{}.json: {}", height, e);
return;
}
}

log::info!(target: "prover", "snarkjs generatecall");
let output = Command::new(path_to_gnark_js())
.args(&["generatecall"])
Expand Down Expand Up @@ -732,7 +741,8 @@ mod tests {
33, 251, 215, 204, 183, 218, 227, 31, 121, 13, 3,
]
.to_vec(),
].to_vec();
]
.to_vec();
let mut data = TestData {
msg: msg,
sigs: sig,
Expand Down Expand Up @@ -790,37 +800,31 @@ mod tests {
assert!(proof.is_some());
}


fn get_pub_inputs_12_118_the_same_12() -> TestData {
let msg = [
[
117, 8, 2, 17, 139, 68, 14, 0, 0, 0, 0, 0, 34, 72, 10, 32, 94, 6, 100, 90, 207,
146, 4, 250, 161, 103, 142, 60, 242, 24, 112, 218, 190, 149, 84, 107, 179, 191,
113, 199, 12, 203, 89, 102, 31, 250, 227, 169, 18, 36, 8, 1, 18, 32, 51, 187, 39,
182, 141, 59, 69, 240, 33, 54, 13, 12, 170, 184, 89, 35, 189, 41, 120, 6, 3, 226,
202, 202, 23, 37, 4, 207, 62, 204, 24, 188, 42, 12, 8, 133, 128, 195, 175, 6, 16,
128, 128, 128, 128, 1, 50, 16, 98, 97, 110, 107, 115, 121, 45, 116, 101, 115, 116,
110, 101, 116, 45, 53,
].to_vec()
let msg = [[
117, 8, 2, 17, 139, 68, 14, 0, 0, 0, 0, 0, 34, 72, 10, 32, 94, 6, 100, 90, 207, 146, 4,
250, 161, 103, 142, 60, 242, 24, 112, 218, 190, 149, 84, 107, 179, 191, 113, 199, 12,
203, 89, 102, 31, 250, 227, 169, 18, 36, 8, 1, 18, 32, 51, 187, 39, 182, 141, 59, 69,
240, 33, 54, 13, 12, 170, 184, 89, 35, 189, 41, 120, 6, 3, 226, 202, 202, 23, 37, 4,
207, 62, 204, 24, 188, 42, 12, 8, 133, 128, 195, 175, 6, 16, 128, 128, 128, 128, 1, 50,
16, 98, 97, 110, 107, 115, 121, 45, 116, 101, 115, 116, 110, 101, 116, 45, 53,
]
.to_vec()]
.to_vec();
let pubkey = [
[
58, 216, 70, 53, 81, 202, 230, 255, 230, 114, 57, 127, 213, 92, 106, 48, 54, 202,
226, 135, 50, 16, 58, 144, 251, 110, 244, 70, 221, 125, 5, 98,
]
.to_vec(),
let pubkey = [[
58, 216, 70, 53, 81, 202, 230, 255, 230, 114, 57, 127, 213, 92, 106, 48, 54, 202, 226,
135, 50, 16, 58, 144, 251, 110, 244, 70, 221, 125, 5, 98,
]
.to_vec()]
.to_vec();
let sig = [[
232, 29, 124, 231, 226, 58, 169, 241, 54, 121, 219, 150, 109, 78, 227, 185, 244, 118,
187, 220, 87, 100, 224, 205, 220, 185, 119, 205, 7, 78, 195, 49, 184, 96, 77, 183, 196,
120, 226, 125, 90, 86, 107, 218, 81, 193, 84, 161, 174, 43, 219, 223, 162, 175, 26,
221, 182, 220, 105, 168, 16, 45, 49, 7,
]
.to_vec()]
.to_vec();
let sig = [
[
232, 29, 124, 231, 226, 58, 169, 241, 54, 121, 219, 150, 109, 78, 227, 185, 244,
118, 187, 220, 87, 100, 224, 205, 220, 185, 119, 205, 7, 78, 195, 49, 184, 96, 77,
183, 196, 120, 226, 125, 90, 86, 107, 218, 81, 193, 84, 161, 174, 43, 219, 223,
162, 175, 26, 221, 182, 220, 105, 168, 16, 45, 49, 7,
]
.to_vec(),
].to_vec();
let mut data = TestData {
msg: msg,
sigs: sig,
Expand All @@ -834,12 +838,12 @@ mod tests {
assert!(data.msg.len() == 12);
assert!(data.sigs.len() == 12);
assert!(data.pubkeys.len() == 12);
// [
// "0",
// "211318896230210628196574274176250526140",
// "293870744648832153179681560962762359603"
// ]

// [
// "0",
// "211318896230210628196574274176250526140",
// "293870744648832153179681560962762359603"
// ]
return data;
}
}
32 changes: 23 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use base64::read;
use blocking::unblock;
use executor::TestData;
use rand::Rng;
use metrics::ZKMetrics;
use rocket::serde::json::Json;
use rocket::serde::json::{json, Value};
use rocket::time::Instant;
use rocket::tokio::sync::RwLock;
use serde::{Deserialize, Serialize};
use std::borrow::{Borrow, BorrowMut};
use std::borrow::Borrow;
use std::collections::HashMap;
use std::ops::Deref;
use std::process::{Command, Stdio};

mod executor;
mod metrics;

use std::sync::{Arc, Mutex};
use std::sync::Arc;

#[derive(Debug, Serialize, Deserialize)]
struct ProofRequestByID {
Expand All @@ -38,6 +35,7 @@ enum ProverStateStatus {

struct ProverState {
db: ProofDatabase,
metrics: ZKMetrics,
state: Arc<RwLock<ProverStateStatus>>,
}

Expand All @@ -46,11 +44,12 @@ struct ProofDatabase {
}

impl ProverState {
fn new() -> Self {
fn new(metrics: ZKMetrics) -> Self {
Self {
db: ProofDatabase {
inner: HashMap::new().into(),
},
metrics,
state: Arc::new(RwLock::new(ProverStateStatus::IDLE)),
}
}
Expand Down Expand Up @@ -129,6 +128,9 @@ async fn create_proof(
});
}

// get a handler of metrics so that they can be sent to the thread that monitors that the proof has been created.
let metrics = state.metrics.clone();

let _task_handle = tokio::spawn(async move {
let mut s = status.write().await;
*s = ProverStateStatus::PROVING;
Expand All @@ -151,6 +153,7 @@ async fn create_proof(

log::info!(target: "prover", "Proof generation completed for block_height: {:?}", create_proof_request.height);
log::info!(target: "prover", "Server went to IDLE status");
metrics.register_new_successful_proof();
});

return json!({
Expand All @@ -168,6 +171,16 @@ fn rocket() -> _ {
PORT="8000" ADDRESS="0.0.0.0" RAPIDSNARK_PATH="/home/mike/services/rapidsnark/package/bin/prover" BATCHVERIFY_FOLDER="batchverify_cpp" ED25519CIRCOM_PATH="/home/mike/services/ed25519circom" GNARK_JS_PATH="/usr/local/bin/snarkjs" cargo run
*/

// Prometheus metrics
// Create a Registry and register Counter.
// let mut r = Registry::new();

// register metrics
let r = rocket_prometheus::PrometheusMetrics::new();

let metrics = crate::metrics::ZKMetrics::new(r.registry().clone());
metrics.registr_metrics();

executor::setup_logging();

let port = std::env::var("PORT").unwrap_or("8000".to_string());
Expand Down Expand Up @@ -201,10 +214,11 @@ fn rocket() -> _ {
port: port,
..Default::default()
};
// rocket::build()
rocket::custom(config)
.mount("/", rocket::routes![status, get_proof, create_proof])
.manage(prover_state)
.attach(r.clone())
.mount("/metrics", r)
}

pub fn get_string_from_env(s: &str) -> String {
Expand Down
48 changes: 48 additions & 0 deletions src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::time::{SystemTime, UNIX_EPOCH};

use prometheus::{Counter, Gauge, Opts, Registry};

#[derive(Clone)]
pub struct ZKMetrics {
r: Registry,
latest_successful_proof_secs: Gauge,
number_successful_proofs: Counter,
}

impl ZKMetrics {
pub fn new(r: Registry) -> Self {
let counter_opts = Opts::new(
"latest_successful_proof_secs",
"latest available successful proof timestamp (in secs)",
);
let latest_successful_proof_secs = Gauge::with_opts(counter_opts).unwrap();

let counter_opts = Opts::new(
"number_successful_proofs",
"number of successful proofs created by the zk prover",
);
let number_successful_proofs = Counter::with_opts(counter_opts).unwrap();

Self {
r,
latest_successful_proof_secs,
number_successful_proofs,
}
}

pub fn registr_metrics(&self) {
self.r
.register(Box::new(self.latest_successful_proof_secs.clone()))
.unwrap();
self.r
.register(Box::new(self.number_successful_proofs.clone()))
.unwrap();
}

pub fn register_new_successful_proof(&self) {
let now = SystemTime::now();
self.latest_successful_proof_secs
.set(now.duration_since(UNIX_EPOCH).unwrap().as_secs_f64());
self.number_successful_proofs.inc();
}
}