-
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.
feat: test rust client for gnark prover (#484)
* Test rust client for gnark prover * Update README with Git LFS and submodules instructions * Refactor gnark testing and remove unused constants Removed unused constant CONTENT_TYPE and moved it inline in the test module. Refactored the prove_inclusion test to iterate over several UTXO counts for better coverage. Unnecessary logging and imports were also removed for cleaner code. * Add newline at end of 'prove' module
- Loading branch information
1 parent
44a1b32
commit 38c0c7a
Showing
12 changed files
with
240 additions
and
12 deletions.
There are no files selected for viewing
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
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,3 @@ | ||
pub const SERVER_ADDRESS: &str = "http://localhost:3001"; | ||
pub const HEALTH_CHECK: &str = "/health"; | ||
pub const PROVE: &str = "/prove"; |
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,112 @@ | ||
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) { | ||
Command::new("sh") | ||
.arg("-c") | ||
.arg("killall light-prover") | ||
.spawn() | ||
.unwrap(); | ||
gnark.kill().unwrap(); | ||
} | ||
|
||
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,3 @@ | ||
mod constants; | ||
mod helpers; | ||
mod prove; |
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,27 @@ | ||
use circuitlib_rs::helpers::init_logger; | ||
|
||
use crate::{ | ||
constants::{PROVE, SERVER_ADDRESS}, | ||
helpers::{health_check, kill_gnark_server, prepare_inputs, spawn_gnark_server}, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn prove_inclusion() { | ||
init_logger(); | ||
let mut gnark = spawn_gnark_server(); | ||
health_check().await; | ||
let client = reqwest::Client::new(); | ||
for number_of_utxos in &[1, 2, 3, 4, 8] { | ||
let inputs = prepare_inputs(*number_of_utxos as usize); | ||
let response_result = client | ||
.post(&format!("{}{}", SERVER_ADDRESS, PROVE)) | ||
.header("Content-Type", "text/plain; charset=utf-8") | ||
.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