-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from okx/feature/add-merkle-proof-sdk
Feature/add merkle proof sdk
- Loading branch information
Showing
17 changed files
with
821 additions
and
147 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ config/local.toml | |
config/prod.toml | ||
test-data/user-data*/ | ||
my_permanent_leveldb/ | ||
*level_db*/ | ||
*level_db*/ | ||
*.json |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod constant; | ||
pub mod merkle_proof; | ||
pub mod prover; | ||
pub mod verifier; |
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,67 @@ | ||
use std::{fs::File, io::Write, str::FromStr}; | ||
|
||
use serde_json::json; | ||
use zk_por_core::{ | ||
account::Account, | ||
config::ProverConfig, | ||
database::{DataBase, DbOption}, | ||
error::PoRError, | ||
global::GlobalConfig, | ||
merkle_proof::MerkleProof, | ||
parser::{AccountParser, FileAccountReader, FileManager, FilesCfg}, | ||
}; | ||
|
||
use crate::constant::RECURSION_BRANCHOUT_NUM; | ||
|
||
pub fn get_merkle_proof( | ||
account_path: String, | ||
cfg: ProverConfig, | ||
output_path: String, | ||
) -> Result<(), PoRError> { | ||
let database = DataBase::new(DbOption { | ||
user_map_dir: cfg.db.level_db_user_path.to_string(), | ||
gmst_dir: cfg.db.level_db_gmst_path.to_string(), | ||
}); | ||
|
||
let batch_size = cfg.prover.batch_size as usize; | ||
let token_num = cfg.prover.num_of_tokens as usize; | ||
|
||
// the path to dump the final generated proof | ||
let file_manager = FileManager {}; | ||
let account_parser = FileAccountReader::new( | ||
FilesCfg { | ||
dir: std::path::PathBuf::from_str(&cfg.prover.user_data_path).unwrap(), | ||
batch_size: cfg.prover.batch_size, | ||
num_of_tokens: cfg.prover.num_of_tokens, | ||
}, | ||
&file_manager, | ||
); | ||
account_parser.log_state(); | ||
// let mut account_parser: Box<dyn AccountParser> = Box::new(parser); | ||
|
||
let batch_num = account_parser.total_num_of_users().div_ceil(batch_size); | ||
|
||
let global_cfg = GlobalConfig { | ||
num_of_tokens: token_num, | ||
num_of_batches: batch_num, | ||
batch_size: batch_size, | ||
recursion_branchout_num: RECURSION_BRANCHOUT_NUM, | ||
}; | ||
|
||
// the account json format is a map of tokens to token values, wheras the format in the merkle proof is given by a vec of token values. | ||
let account = Account::new_from_file_path(account_path); | ||
|
||
if account.is_err() { | ||
return Err(account.unwrap_err()); | ||
} | ||
|
||
let merkle_proof = MerkleProof::new_from_account(&account.unwrap(), &database, &global_cfg) | ||
.expect("Una ble to generate merkle proof"); | ||
|
||
let mut file = File::create(output_path.clone()) | ||
.expect(format!("fail to create proof file at {:#?}", output_path).as_str()); | ||
file.write_all(json!(merkle_proof).to_string().as_bytes()) | ||
.expect("fail to write proof to file"); | ||
|
||
Ok(()) | ||
} |
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
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
Oops, something went wrong.