-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e984843
commit a970a0a
Showing
11 changed files
with
227 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../circuit-lib.js/scripts/prover.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub const SERVER_ADDRESS: &str = "http://localhost:3001"; | ||
pub const HEALTH_CHECK: &str = "/health"; | ||
pub const PROVE: &str = "/prove"; | ||
|
||
const CONTENT_TYPE: &str = "text/plain; charset=utf-8"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use std::{ | ||
process::{Child, Command}, | ||
thread, | ||
time::Duration, | ||
}; | ||
|
||
use circuitlib_rs::{init_merkle_tree::merkle_tree_inputs, merkle_proof_inputs::MerkleTreeInfo}; | ||
use num_bigint::BigInt; | ||
use num_traits::ToPrimitive; | ||
use serde::Serialize; | ||
use serde_json::json; | ||
|
||
use crate::constants::{HEALTH_CHECK, SERVER_ADDRESS}; | ||
|
||
#[allow(non_snake_case)] | ||
#[derive(Serialize)] | ||
pub struct JsonStruct { | ||
root: Vec<String>, | ||
leaf: Vec<String>, | ||
inPathIndices: Vec<u32>, | ||
inPathElements: Vec<Vec<String>>, | ||
} | ||
|
||
impl JsonStruct { | ||
fn new(number_of_utxos: usize) -> Self { | ||
let merkle_inputs = merkle_tree_inputs(MerkleTreeInfo::H26); | ||
let roots = create_vec_of_string(number_of_utxos, &merkle_inputs.root); | ||
let leafs = create_vec_of_string(number_of_utxos, &merkle_inputs.leaf); | ||
let in_path_indices = create_vec_of_u32(number_of_utxos, &merkle_inputs.in_path_indices); | ||
let in_path_elements = | ||
create_vec_of_vec_of_string(number_of_utxos, &merkle_inputs.in_path_elements); | ||
Self { | ||
root: roots, | ||
leaf: leafs, | ||
inPathIndices: in_path_indices, | ||
inPathElements: in_path_elements, | ||
} | ||
} | ||
} | ||
pub fn prepare_inputs(number_of_utxos: usize) -> String { | ||
let json_struct = JsonStruct::new(number_of_utxos); | ||
create_json_from_struct(&json_struct) | ||
} | ||
|
||
pub fn spawn_gnark_server() -> Child { | ||
let server_process = Command::new("sh") | ||
.arg("-c") | ||
.arg("scripts/prover.sh") | ||
.spawn() | ||
.expect("Failed to start server process"); | ||
|
||
// Wait for the server to launch before proceeding. | ||
thread::sleep(Duration::from_secs(5)); | ||
|
||
server_process | ||
} | ||
|
||
pub fn kill_gnark_server(gnark: &mut Child) { | ||
println!("kill gnark!"); | ||
Command::new("sh") | ||
.arg("-c") | ||
.arg("killall light-prover") | ||
.spawn() | ||
.unwrap(); | ||
gnark.kill().unwrap(); | ||
println!("gnark killed!"); | ||
} | ||
|
||
pub async fn health_check() { | ||
const MAX_RETRIES: usize = 20; | ||
const TIMEOUT: usize = 5; | ||
|
||
let client = reqwest::Client::new(); | ||
|
||
for _ in 0..MAX_RETRIES { | ||
match client | ||
.get(&format!("{}{}", SERVER_ADDRESS, HEALTH_CHECK)) | ||
.send() | ||
.await | ||
{ | ||
Ok(_) => break, | ||
Err(_) => { | ||
tokio::time::sleep(Duration::from_secs(TIMEOUT as u64)).await; | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn create_vec_of_string(number_of_utxos: usize, element: &BigInt) -> Vec<String> { | ||
vec![format!("0x{}", element.to_str_radix(16)); number_of_utxos] | ||
} | ||
|
||
pub fn create_vec_of_u32(number_of_utxos: usize, element: &BigInt) -> Vec<u32> { | ||
vec![element.to_u32().unwrap(); number_of_utxos] | ||
} | ||
|
||
pub fn create_vec_of_vec_of_string( | ||
number_of_utxos: usize, | ||
elements: &[BigInt], | ||
) -> Vec<Vec<String>> { | ||
let vec: Vec<String> = elements | ||
.iter() | ||
.map(|e| format!("0x{}", e.to_str_radix(16))) | ||
.collect(); | ||
vec![vec; number_of_utxos] | ||
} | ||
|
||
pub fn create_json_from_struct(json_struct: &JsonStruct) -> String { | ||
let json = json!(json_struct); | ||
match serde_json::to_string_pretty(&json) { | ||
Ok(json) => json, | ||
Err(_) => panic!("Merkle tree data invalid"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
mod constants; | ||
mod helpers; | ||
mod prove; | ||
|
||
use std::{ | ||
process::{Child, Command}, | ||
thread, | ||
time::Duration, | ||
}; | ||
|
||
use circuitlib_rs::helpers::init_logger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use circuitlib_rs::helpers::init_logger; | ||
use reqwest::header::CONTENT_TYPE; | ||
|
||
use crate::{ | ||
constants::{PROVE, SERVER_ADDRESS}, | ||
helpers::{health_check, kill_gnark_server, prepare_inputs, spawn_gnark_server}, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn prove_inclusion() { | ||
let number_of_utxos = 1; | ||
init_logger(); | ||
let mut gnark = spawn_gnark_server(); | ||
health_check().await; | ||
let inputs = prepare_inputs(number_of_utxos); | ||
let client = reqwest::Client::new(); | ||
let response_result = client | ||
.post(&format!("{}{}", SERVER_ADDRESS, PROVE)) | ||
.header("Content-Type", CONTENT_TYPE) | ||
.body(inputs) | ||
.send() | ||
.await | ||
.expect("Failed to execute request."); | ||
assert!(response_result.status().is_success()); | ||
kill_gnark_server(&mut gnark); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters