Skip to content

Commit

Permalink
fix: re-arrange test cases for backend
Browse files Browse the repository at this point in the history
  • Loading branch information
sifnoc committed Sep 7, 2023
1 parent 23fbd50 commit d5f7d53
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 238 deletions.
2 changes: 1 addition & 1 deletion backend/src/apis/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod tests {
use super::*;

use crate::contracts::mock::mock_erc20::MockERC20;
use crate::contracts::tests::initialize_anvil;
use crate::tests::initialize_anvil;

#[tokio::test]
async fn test_fetch_asset_sums() {
Expand Down
52 changes: 0 additions & 52 deletions backend/src/apis/ownership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,55 +35,3 @@ impl AddressOwnership {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::contracts::{generated::summa_contract::Summa, tests::initialize_anvil};
use ethers::abi::AbiEncode;
use std::{str::from_utf8, str::FromStr, sync::Arc};

// This test actually duplicated in `backend/src/contracts/tests.rs`
// TODO: refactor this test to avoid duplication
#[tokio::test]
async fn test_submit_address_ownership() {
let (anvil, cex_addr_1, cex_addr_2, client, _mock_erc20) = initialize_anvil().await;

// No needed to deploy verifier contracts(inclusion and solvency) in this test
// use arbitrary addresses for Summa contract
let summa_contract =
Summa::deploy(Arc::clone(&client), (Address::random(), Address::random()))
.unwrap()
.send()
.await
.unwrap();

let mut address_ownership_client = AddressOwnership::new(
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
anvil.chain_id(),
anvil.endpoint().as_str(),
summa_contract.address(),
)
.unwrap();

let owned_addresses = vec![AddressOwnershipProof {
chain: "ETH".to_string(),
cex_address: cex_addr_1.to_string(),
signature:
("0x089b32327d332c295dc3b8873c205b72153211de6dc1c51235782b091cefb9d06d6df2661b86a7d441cd322f125b84901486b150e684221a7b7636eb8182af551b").parse().unwrap(),
message: "Summa proof of solvency for CryptoExchange".encode().into(),
},AddressOwnershipProof {
chain: "ETH".to_string(),
cex_address: cex_addr_2.to_string(),
signature:
("0xb17a9e25265d3b88de7bfad81e7accad6e3d5612308ff83cc0fef76a34152b0444309e8fc3dea5139e49b6fc83a8553071a7af3d0cfd3fb8c1aea2a4c171729c1c").parse().unwrap(),
message: "Summa proof of solvency for CryptoExchange".encode().into(),
}];

let result = address_ownership_client
.dispatch_proof_of_address_ownership(owned_addresses)
.await;

assert_eq!(result.is_ok(), true);
}
}
121 changes: 2 additions & 119 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::{
plonk::{ProvingKey, VerifyingKey},
poly::kzg::commitment::ParamsKZG,
};
use serde_json::to_string_pretty;
use snark_verifier_sdk::CircuitExt;
use std::error::Error;

Expand Down Expand Up @@ -110,6 +111,7 @@ where
.collect::<Vec<Fp>>()
.try_into()
.unwrap();

let proof: SolvencyProof = match snapshot.generate_proof_of_solvency(asset_sum) {
Ok(p) => p,
Err(_) => return Err("Failed to generate proof of solvency"),
Expand Down Expand Up @@ -222,122 +224,3 @@ where
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use ethers::{
core::k256::ecdsa::SigningKey,
signers::{LocalWallet, Wallet},
types::H160,
utils::{Anvil, AnvilInstance},
};
use halo2_proofs::halo2curves::ff::PrimeField;
use std::{str::from_utf8, str::FromStr, sync::Arc};

use crate::contracts::{
generated::{
inclusion_verifier::InclusionVerifier, solvency_verifier::SolvencyVerifier,
summa_contract::Summa,
},
tests::initialize_anvil,
};

const LEVELS: usize = 4;
const N_ASSETS: usize = 2;
const N_BYTES: usize = 14;

#[tokio::test]
async fn test_round_features() {
let (anvil, cex_addr_1, cex_addr_2, client, _mock_erc20) = initialize_anvil().await;

let solvency_verifer_contract = SolvencyVerifier::deploy(Arc::clone(&client), ())
.unwrap()
.send()
.await
.unwrap();

let inclusion_verifer_contract = InclusionVerifier::deploy(Arc::clone(&client), ())
.unwrap()
.send()
.await
.unwrap();

let summa_contract = Summa::deploy(
Arc::clone(&client),
(
solvency_verifer_contract.address(),
inclusion_verifer_contract.address(),
),
)
.unwrap()
.send()
.await
.unwrap();

// Initialize round
let mut round = Round::<LEVELS, N_ASSETS, N_BYTES>::new(
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", // anvil account [0]
anvil.chain_id(),
anvil.endpoint().as_str(),
summa_contract.address(),
)
.unwrap();

let entry_csv = "../zk_prover/src/merkle_sum_tree/csv/entry_16.csv";
let params_path = "ptau/hermez-raw-11";

let assets = [
Asset {
asset_name: "ETH".to_string(),
chain: "ETH".to_string(),
amount: U256::from(556863),
},
Asset {
asset_name: "USDT".to_string(),
chain: "ETH".to_string(),
amount: U256::from(556863),
},
];

// Build snapshot
round.build_snapshot(entry_csv, params_path, 1);

// Verify solvency proof
let mut logs = summa_contract
.solvency_proof_submitted_filter()
.query()
.await
.unwrap();
assert_eq!(logs.len(), 0);

assert_eq!(round.dispatch_solvency_proof(assets).await.unwrap(), ());

// after send transaction to submit proof of solvency, logs should be updated
let mut logs = summa_contract
.solvency_proof_submitted_filter()
.query()
.await
.unwrap();

assert_eq!(logs.len(), 1);

// Test inclusion proof generation
let inclusion_proof = round.get_proof_of_inclusion(0).unwrap();

assert_eq!(
inclusion_proof.public_inputs[0][0],
Fp::from_str_vartime(
"6362822108736413915574850018842190920390136280184018644072260166743334495239"
)
.unwrap()
);
assert_eq!(
inclusion_proof.public_inputs[0][1],
Fp::from_str_vartime(
"1300633067792667740851197998552728163078912135282962223512949070409098715333"
)
.unwrap()
);
}
}
1 change: 0 additions & 1 deletion backend/src/contracts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod generated;
pub mod mock;
pub(crate) mod signer;
pub(crate) mod tests;
27 changes: 27 additions & 0 deletions backend/src/contracts/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,30 @@ impl SummaSigner {
Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;
use ethers::{types::Address, utils::Anvil};

#[tokio::test]
async fn test_sign_message() {
let anvil = Anvil::new().spawn();

let signer = SummaSigner::new(
//Account #1
&vec!["0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"],
"0xde9be858da4a475276426320d5e9262ecfc3ba460bfac56360bfa6c4c28b4ee0",
31337,
anvil.endpoint().as_str(),
//Verifier deployment is not necessary for this test
Address::random(),
);

let signatures = signer.generate_signatures().await.unwrap();
assert_eq!(signatures.len(), 1);
//Signature produced by the account #1
assert_eq!(signatures[0].to_string(), "089b32327d332c295dc3b8873c205b72153211de6dc1c51235782b091cefb9d06d6df2661b86a7d441cd322f125b84901486b150e684221a7b7636eb8182af551b");
drop(anvil);
}
}
1 change: 1 addition & 0 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(generic_const_exprs)]
pub mod apis;
pub mod contracts;
pub mod tests;
Loading

0 comments on commit d5f7d53

Please sign in to comment.