Skip to content

Commit

Permalink
fix: rollback applying csv_parser for AddressOwnership and Assets in …
Browse files Browse the repository at this point in the history
…Snapshot
  • Loading branch information
sifnoc committed Nov 8, 2023
1 parent 2f14b36 commit 3e8c855
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 64 deletions.
16 changes: 6 additions & 10 deletions backend/examples/summa_solvency_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use summa_backend::{
round::{MstInclusionProof, Round},
},
contracts::signer::{AddressInput, SummaSigner},
sample_data::*,
sample_entries::*,
tests::initialize_test_env,
};
use summa_solvency::merkle_sum_tree::{utils::generate_leaf_hash, MerkleSumTree};
Expand All @@ -25,9 +25,6 @@ async fn main() -> Result<(), Box<dyn Error>> {

// 1. Submit ownership proof
//
// Each CEX prepares its own `signature` CSV file.
let signature_csv_path = "src/apis/csv/signatures.csv";

// The signer instance would be shared with `address_ownership` and `round` instances
//
// Using AddressInput::Address to directly provide the summa_contract's address.
Expand All @@ -40,10 +37,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
)
.await?;

// Each CEX has to initialize the `AddressOwnership` instance with the `signer` and `address_ownership_proofs`.
let address_ownership_proofs = get_sample_address_ownership_proofs();
let mut address_ownership_client =
AddressOwnership::new(&signer, address_ownership_proofs).unwrap();
// Each CEX prepares its own `signature` CSV file.
let signature_csv_path = "src/apis/csv/signatures.csv";
let mut address_ownership_client = AddressOwnership::new(&signer, signature_csv_path).unwrap();

// Dispatch the proof of address ownership.
// the `dispatch_proof_of_address_ownership` function sends a transaction to the Summa contract.
Expand All @@ -57,15 +53,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
//
// Initialize the `Round` instance to submit the proof of solvency.
let params_path = "ptau/hermez-raw-11";
let assets_csv_path = "src/apis/csv/assets.csv";

let assets_state = get_sample_assets();
let entries = get_sample_entries();
let mst = MerkleSumTree::from_entries(entries, false).unwrap();

// Using the `round` instance, the solvency proof is dispatched to the Summa contract with the `dispatch_solvency_proof` method.
let timestamp = 1u64;
let mut round =
Round::<4, 2, 14>::new(&signer, mst, assets_state, params_path, timestamp).unwrap();
Round::<4, 2, 14>::new(&signer, mst, assets_csv_path, params_path, timestamp).unwrap();

// Sends the solvency proof, which should ideally complete without errors.
round.dispatch_solvency_proof().await?;
Expand Down
6 changes: 4 additions & 2 deletions backend/src/apis/address_ownership.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::contracts::{generated::summa_contract::AddressOwnershipProof, signer::SummaSigner};
use std::{error::Error, result::Result};

// use super::csv_parser::parse_signature_csv;
use super::csv_parser::parse_signature_csv;

pub struct AddressOwnership<'a> {
address_ownership_proofs: Vec<AddressOwnershipProof>,
Expand All @@ -11,8 +11,10 @@ pub struct AddressOwnership<'a> {
impl AddressOwnership<'_> {
pub fn new<'a>(
signer: &'a SummaSigner,
address_ownership_proofs: Vec<AddressOwnershipProof>,
signature_csv_path: &str,
) -> Result<AddressOwnership<'a>, Box<dyn Error>> {
let address_ownership_proofs = parse_signature_csv(signature_csv_path)?;

Ok(AddressOwnership {
address_ownership_proofs,
signer,
Expand Down
9 changes: 6 additions & 3 deletions backend/src/apis/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use halo2_proofs::{
use serde::{Deserialize, Serialize};
use std::error::Error;

use super::csv_parser::parse_asset_csv;
use crate::contracts::{generated::summa_contract::summa::Asset, signer::SummaSigner};
use summa_solvency::{
circuits::{
Expand Down Expand Up @@ -76,13 +77,13 @@ where
pub fn new<'a>(
signer: &'a SummaSigner,
mst: MerkleSumTree<N_ASSETS, N_BYTES>,
assets_state: [Asset; N_ASSETS],
asset_csv_path: &str,
params_path: &str,
timestamp: u64,
) -> Result<Round<'a, LEVELS, N_ASSETS, N_BYTES>, Box<dyn Error>> {
Ok(Round {
timestamp,
snapshot: Snapshot::<LEVELS, N_ASSETS, N_BYTES>::new(mst, assets_state, params_path)
snapshot: Snapshot::<LEVELS, N_ASSETS, N_BYTES>::new(mst, asset_csv_path, params_path)
.unwrap(),
signer: &signer,
})
Expand Down Expand Up @@ -129,9 +130,11 @@ where
{
pub fn new(
mst: MerkleSumTree<N_ASSETS, N_BYTES>,
assets_state: [Asset; N_ASSETS],
asset_csv_path: &str,
params_path: &str,
) -> Result<Snapshot<LEVELS, N_ASSETS, N_BYTES>, Box<dyn std::error::Error>> {
let assets_state = parse_asset_csv::<&str, N_ASSETS>(asset_csv_path).unwrap();

let mst_inclusion_circuit = MstInclusionCircuit::<LEVELS, N_ASSETS, N_BYTES>::init_empty();
let solvency_circuit = SolvencyCircuit::<N_ASSETS, N_BYTES>::init_empty();

Expand Down
4 changes: 3 additions & 1 deletion backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![feature(generic_const_exprs)]
pub mod apis;
pub mod contracts;
pub mod sample_data;
pub mod sample_entries;
pub mod tests;
pub use merkle_sum_tree::{Entry, MerkleSumTree};
use summa_solvency::merkle_sum_tree;
34 changes: 0 additions & 34 deletions backend/src/sample_data.rs → backend/src/sample_entries.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,6 @@
use crate::contracts::generated::summa_contract::{AddressOwnershipProof, Asset};
use ethers::{abi::AbiEncode, types::U256};
use num_bigint::ToBigUint;
use summa_solvency::merkle_sum_tree::Entry;

pub fn get_sample_address_ownership_proofs() -> Vec<AddressOwnershipProof> {
vec![
AddressOwnershipProof {
chain: "ETH".to_string(),
cex_address: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8".to_string(),
signature: "0x089b32327d332c295dc3b8873c205b72153211de6dc1c51235782b091cefb9d06d6df2661b86a7d441cd322f125b84901486b150e684221a7b7636eb8182af551b".parse().unwrap(),
message: "Summa proof of solvency for CryptoExchange".encode().into()
},
AddressOwnershipProof {
chain: "ETH".to_string(),
cex_address: "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC".to_string(),
signature: "0xb17a9e25265d3b88de7bfad81e7accad6e3d5612308ff83cc0fef76a34152b0444309e8fc3dea5139e49b6fc83a8553071a7af3d0cfd3fb8c1aea2a4c171729c1c".parse().unwrap(),
message: "Summa proof of solvency for CryptoExchange".encode().into()
},
]
}

pub fn get_sample_assets() -> [Asset; 2] {
[
Asset {
asset_name: "ETH".to_string(),
chain: "ETH".to_string(),
amount: U256::from_dec_str("556863").expect("Invalid decimal string for amount"),
},
Asset {
asset_name: "USDT".to_string(),
chain: "ETH".to_string(),
amount: U256::from_dec_str("556863").expect("Invalid decimal string for amount"),
},
]
}

pub fn get_sample_entries() -> Vec<Entry<2>> {
let entries = vec![
Entry::new(
Expand Down
19 changes: 8 additions & 11 deletions backend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ mod test {
},
signer::{AddressInput, SummaSigner},
};
use crate::{sample_data::*, tests::initialize_test_env};
use crate::{sample_entries::get_sample_entries, tests::initialize_test_env};

#[tokio::test]
async fn test_deployed_address() -> Result<(), Box<dyn Error>> {
Expand Down Expand Up @@ -185,26 +185,24 @@ mod test {
.await?;

// At least one address ownership proof should be submitted before submitting solvency proof
let address_ownership_proofs = get_sample_address_ownership_proofs();
let mut address_ownership_client =
AddressOwnership::new(&signer, address_ownership_proofs).unwrap();
AddressOwnership::new(&signer, "src/apis/csv/signatures.csv").unwrap();

address_ownership_client
.dispatch_proof_of_address_ownership()
.await?;

// Do sumbit solvency proofs simultaneously
let asset_csv = "src/apis/csv/assets.csv";
let params_path = "ptau/hermez-raw-11";

let assets_state = get_sample_assets();
let entries = get_sample_entries();
let mst = MerkleSumTree::from_entries(entries, false).unwrap();

let mut round_one =
Round::<4, 2, 14>::new(&signer, mst.clone(), assets_state.clone(), params_path, 1)
.unwrap();
Round::<4, 2, 14>::new(&signer, mst.clone(), asset_csv, params_path, 1).unwrap();
let mut round_two =
Round::<4, 2, 14>::new(&signer, mst, assets_state, params_path, 2).unwrap();
Round::<4, 2, 14>::new(&signer, mst, asset_csv, params_path, 2).unwrap();

// Checking block number before sending transaction of proof of solvency
let outer_provider: Provider<Http> = Provider::try_from(anvil.endpoint().as_str())?;
Expand Down Expand Up @@ -243,9 +241,8 @@ mod test {
)
.await?;

let address_ownership_proofs = get_sample_address_ownership_proofs();
let mut address_ownership_client =
AddressOwnership::new(&signer, address_ownership_proofs).unwrap();
AddressOwnership::new(&signer, "src/apis/csv/signatures.csv").unwrap();

address_ownership_client
.dispatch_proof_of_address_ownership()
Expand Down Expand Up @@ -279,12 +276,12 @@ mod test {

// Initialize round
let params_path = "ptau/hermez-raw-11";
let asset_csv = "src/apis/csv/assets.csv";

let assets_state = get_sample_assets();
let entries = get_sample_entries();
let mst = MerkleSumTree::from_entries(entries, false).unwrap();

let mut round = Round::<4, 2, 14>::new(&signer, mst, assets_state, params_path, 1).unwrap();
let mut round = Round::<4, 2, 14>::new(&signer, mst, asset_csv, params_path, 1).unwrap();

// Verify solvency proof
let mut solvency_proof_logs = summa_contract
Expand Down
4 changes: 2 additions & 2 deletions zk_prover/benches/full_solvency_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ fn generate_zk_proof_solvency_circuit(_c: &mut Criterion) {
let asset_sums = merkle_sum_tree.root().balances.map(|x| x + Fp::from(1));

// Only now we can instantiate the circuit with the actual inputs
let circuit = SolvencyCircuit::<N_ASSETS, N_BYTES>::init(merkle_sum_tree, asset_sums);
let circuit = SolvencyCircuit::<N_ASSETS, N_BYTES>::init(&merkle_sum_tree, asset_sums);

let bench_name = format!(
"generate zk proof - tree of 2 power of {} entries with {} assets solvency circuit",
Expand Down Expand Up @@ -253,7 +253,7 @@ fn verify_zk_proof_solvency_circuit(_c: &mut Criterion) {
let asset_sums = merkle_sum_tree.root().balances.map(|x| x + Fp::from(1));

// Only now we can instantiate the circuit with the actual inputs
let circuit = SolvencyCircuit::<N_ASSETS, N_BYTES>::init(merkle_sum_tree, asset_sums);
let circuit = SolvencyCircuit::<N_ASSETS, N_BYTES>::init(&merkle_sum_tree, asset_sums);

let proof = full_prover(&params, &pk, circuit.clone(), circuit.instances());

Expand Down
2 changes: 1 addition & 1 deletion zk_prover/src/circuits/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ mod test {
MerkleSumTree::<N_ASSETS, N_BYTES>::new("src/merkle_sum_tree/csv/entry_16.csv")
.unwrap();

let circuit = SolvencyCircuit::<N_ASSETS, N_BYTES>::init(merkle_sum_tree, asset_sums);
let circuit = SolvencyCircuit::<N_ASSETS, N_BYTES>::init(&merkle_sum_tree, asset_sums);

let root =
BitMapBackend::new("prints/solvency-layout.png", (2048, 32768)).into_drawing_area();
Expand Down

0 comments on commit 3e8c855

Please sign in to comment.