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
113 changes: 55 additions & 58 deletions src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::str;
use serde::Deserialize;
use serde::Serialize;
use core::str;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TestData {
Expand All @@ -15,7 +15,7 @@ pub struct Request {
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Edges{
pub struct Edges {
pub requests: Vec<Request>,
}

Expand All @@ -37,23 +37,21 @@ pub struct ProofResult {
pub data: ProofData,
}


pub fn get_datadir() -> String {
let datadir_path =
std::env::var("DATADIR").unwrap_or_else(|_| ".".to_string());
return datadir_path;
let datadir_path = std::env::var("DATADIR").unwrap_or_else(|_| ".".to_string());
datadir_path
}

pub fn succinct_org_name() -> String{
pub fn succinct_org_name() -> String {
let succinct_org_name =
std::env::var("SUCCINCT_ORG_NAME").expect("SUCCINCT_ORG_NAME env variable is not set");
return succinct_org_name;
succinct_org_name
}

pub fn succinct_project_name() -> String {
let succinct_project_name =
std::env::var("SUCCINCT_PROJECT_NAME").expect("SUCCINCT_PROJECT_NAME env variable is not set");
return succinct_project_name;
let succinct_project_name = std::env::var("SUCCINCT_PROJECT_NAME")
.expect("SUCCINCT_PROJECT_NAME env variable is not set");
succinct_project_name
}

pub fn proof_wait_time_sec() -> u64 {
Expand All @@ -63,14 +61,17 @@ pub fn proof_wait_time_sec() -> u64 {
if u64.is_err() {
return 600; //10 minutes
}
return u64.expect(format!("Failed to parse PROOF_WAIT_TIME_SEC: {}", succinct_project_name).as_str());
return u64.unwrap_or_else(|_| {
panic!(
"Failed to parse PROOF_WAIT_TIME_SEC: {}",
succinct_project_name
)
});
}



pub async fn get_proof(trusted_height: u64, height: u64) -> Option<String> {
let datadir = get_datadir();

let path = format!("{}/data_proof_{}_{}.json", datadir, trusted_height, height);
log::info!(target: "prover", "Reading proof from file: {}", path);
let proof = std::fs::read_to_string(path.clone());
Expand All @@ -87,14 +88,13 @@ pub async fn get_proof(trusted_height: u64, height: u64) -> Option<String> {
}

pub async fn generate_proof(
msg: Vec<Vec<u8>>,
sigs: Vec<Vec<u8>>,
pubkeys: Vec<Vec<u8>>,
_msg: Vec<Vec<u8>>,
_sigs: Vec<Vec<u8>>,
_pubkeys: Vec<Vec<u8>>,
trusted_height: u64,
height: u64,
trusted_hash: String,
) {

if trusted_height >= height {
log::error!(target: "prover", "Trusted height is greater than the height trusted_height: {}, height: {}", trusted_height, height);
return;
Expand All @@ -105,13 +105,16 @@ pub async fn generate_proof(
let datadir: String = get_datadir();

let command: &str = "/usr/local/bin/tendermintx";
let args: [String; 3] = [trusted_height.to_string(), height.to_string(), trusted_hash.to_string()];
let args: [String; 3] = [
trusted_height.to_string(),
height.to_string(),
trusted_hash.to_string(),
];

log::info!(target: "prover", "Executing command: {} {}", command, args.join(" "));



use std::process::{Command, Stdio};

let output = Command::new(command)
.args(&args)
.stdout(Stdio::piped())
Expand All @@ -136,9 +139,9 @@ pub async fn generate_proof(
let end_index = end_index.unwrap();
let request_id = &out_str[start_index + "request____start".len()..end_index];
log::info!(target: "prover", "Request ID {} ", request_id);

//ask http server for to find a proof by request_id
//need to sleep to wait untill request id will be available via api.
//need to sleep to wait untill request id will be available via api.
//in meantime if we wait to long the proof is generating so no downtime

println!("Waiting 100 secs for request id to be available via api");
Expand All @@ -161,8 +164,6 @@ pub async fn generate_proof(
}
let text = response.unwrap();
println!("{:?}", text.clone());



let proofs: Vec<Proof> = serde_json::from_str(&text).unwrap();
for i in proofs.iter() {
Expand All @@ -173,21 +174,29 @@ pub async fn generate_proof(

//todo remove

let proof = proofs.iter().find(|proof| proof.edges.requests.iter().any(|request| request.id == request_id));
let proof = proofs.iter().find(|proof| {
proof
.edges
.requests
.iter()
.any(|request| request.id == request_id)
});

if proof.is_none() {
log::error!(target: "prover", "Failed to find a proof with request_id: {}", request_id);
return;
}
use std::time::{SystemTime};
use std::time::SystemTime;
let before = SystemTime::now();
loop {

log::info!(target: "prover", "Waiting for the proof to be ready in a loop");

//https://alpha.succinct.xyz/api/proof/4a6f8491-94c6-4cbc-b890-9708b376471a/result

let url = format!("https://alpha.succinct.xyz/api/proof/{}/result", proof.unwrap().id);

let url = format!(
"https://alpha.succinct.xyz/api/proof/{}/result",
proof.unwrap().id
);
log::info!(target: "prover", "url: {}", url);
let response = reqwest::get(url).await;
if response.is_err() {
Expand All @@ -214,11 +223,11 @@ pub async fn generate_proof(
continue;
}

let proof_result: Result::<ProofResult, _> = serde_json::from_str(&text);
let proof_result: Result<ProofResult, _> = serde_json::from_str(&text);
if proof_result.is_err() {
log::info!(target: "prover", "failed to deseerizlie text");
log::info!(target: "prover", "failed to deseerizlie text : {}", text);
log::info!(target: "prover", "need to wait more time and ask again");
log::info!(target: "prover", "need to wait more time and ask again");
std::thread::sleep(std::time::Duration::from_secs(20));
let last = SystemTime::now();
let difference = last.duration_since(before).expect("Time went backwards");
Expand All @@ -230,7 +239,7 @@ pub async fn generate_proof(
continue;
}
let proof_result = proof_result.unwrap();

log::info!(target: "prover", "Writing data to circuts folder: {}", "..");
let proof_path = format!("{}/data_proof_{}_{}.json", datadir, trusted_height, height);
match std::fs::write(proof_path.clone(), proof_result.data.proof) {
Expand All @@ -246,8 +255,6 @@ pub async fn generate_proof(
}

log::info!(target: "prover", "Proof generation finished for : {}", height);


}

use log::LevelFilter;
Expand Down Expand Up @@ -285,23 +292,14 @@ mod tests {
assert!(proof.is_some());
}



fn set_evn_vars() {

let ed25519_circom_path = std::env::var("SUCCINCT_ORG_NAME");
if ed25519_circom_path.is_err() {
std::env::set_var(
"SUCCINCT_ORG_NAME",
"blasrodri",
);
std::env::set_var("SUCCINCT_ORG_NAME", "blasrodri");
}
let ed25519_circom_path = std::env::var("SUCCINCT_PROJECT_NAME");
if ed25519_circom_path.is_err() {
std::env::set_var(
"SUCCINCT_ORG_NAME",
"tendermintx",
);
std::env::set_var("SUCCINCT_ORG_NAME", "tendermintx");
}
}

Expand All @@ -311,8 +309,7 @@ mod tests {
println!("Setting ED25519CIRCOM_PATH");
std::env::set_var("ED25519CIRCOM_PATH", "/home/mike/services/ed25519circom");
println!("/home/mike/services/ed25519circom");
}
else {
} else {
println!("{}", ed25519_circom_path.unwrap());
}
let ed25519_circom_path = std::env::var("GNARK_JS_PATH");
Expand All @@ -321,26 +318,27 @@ mod tests {
println!("Setting GNARK_JS_PATH");
std::env::set_var("GNARK_JS_PATH", "/usr/local/bin/snarkjs");
println!("/usr/local/bin/snarkjs");
}
else {
} else {
println!("{}", ed25519_circom_path.unwrap());
}

let ed25519_circom_path = std::env::var("RAPIDSNARK_PATH");
if ed25519_circom_path.is_err() {
println!("Setting RAPIDSNARK_PATH");
std::env::set_var("RAPIDSNARK_PATH", "/home/mike/services/rapidsnark/package/bin/prover");
std::env::set_var(
"RAPIDSNARK_PATH",
"/home/mike/services/rapidsnark/package/bin/prover",
);
println!("/home/mike/services/rapidsnark/package/bin/prover");
}
else {
} else {
println!("{}", ed25519_circom_path.unwrap());
}

std::env::set_var("BATCHVERIFY_FOLDER", "batchverify_cpp");
}

fn get_pub_inputs() -> TestData {
let mut data = TestData {
let data = TestData {
msg: vec![vec![
111, 8, 2, 17, 36, 0, 0, 0, 0, 0, 0, 0, 34, 72, 10, 32, 66, 15, 32, 152, 208, 7,
69, 219, 166, 26, 244, 231, 127, 247, 196, 94, 155, 127, 207, 55, 136, 84, 147,
Expand All @@ -360,7 +358,6 @@ mod tests {
100, 157, 6, 199, 160, 237, 149, 254, 14, 202, 131, 61, 183, 163,
]],
};
return data;
data
}

}
Loading