Skip to content

Commit

Permalink
fix: error propagation with try operator; remove unnecessaries
Browse files Browse the repository at this point in the history
  • Loading branch information
sifnoc committed Oct 31, 2023
1 parent 44ee5c7 commit 5b2f510
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 87 deletions.
61 changes: 7 additions & 54 deletions backend/examples/summa_solvency_flow.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#![feature(generic_const_exprs)]
use std::{error::Error, fs::File, io::BufReader, io::Write};

use ethers::{
abi::{encode, Token},
types::{Bytes, U256},
utils::keccak256,
};
use ethers::types::{Bytes, U256};
use serde_json::{from_reader, to_string_pretty};

use summa_backend::{
Expand Down Expand Up @@ -38,43 +34,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
)
.unwrap();

// Retrieve hashed addresses using the `keccak256` method.
let address_hashes = address_ownership_client
.get_ownership_proofs()
.iter()
.map(|x| keccak256(encode(&[Token::String(x.cex_address.clone())])))
.collect::<Vec<[u8; 32]>>();

// Dispatch the proof of address ownership.
// the `dispatch_proof_of_address_ownership` function sends a transaction to the Summa contract.
address_ownership_client
.dispatch_proof_of_address_ownership()
.await
.unwrap();
.await?;

// If the `addressHash` isn't found in the `addressOwnershipProofs` mapping of the Summa contract,
// it will return 0; otherwise, it will return a non-zero value.
//
// You can find unregistered address with null bytes as follows:
//
// let unregistered = summa_contract
// .ownership_proof_by_address([0u8; 32])
// .call()
// .await
// .unwrap();
//
// assert_eq!(unregistered, 0);

// Verify whether the addresses have been registered within the Summa contract.
for address_hash in address_hashes.iter() {
let registered = summa_contract
.ownership_proof_by_address(*address_hash)
.call()
.await
.unwrap();

assert_ne!(registered, U256::from(0));
}
println!("1. Ownership proofs are submitted successfully!");

// 2. Submit solvency proof
Expand All @@ -98,21 +63,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
.unwrap();

// Sends the solvency proof, which should ideally complete without errors.
assert_eq!(round.dispatch_solvency_proof().await.unwrap(), ());

// You can also use the `solvency_proof_submitted_filter` method to check if the solvency proof is submitted.
// println!("{:?}", summa_contract
// .solvency_proof_submitted_filter()
// .query()
// .await
// .unwrap(););
round.dispatch_solvency_proof().await?;

println!("2. Solvency proof is submitted successfully!");

// 3. Generate Inclusion Proof
//
// In a production setup, the CEX should first dispatch the solvency proof to update the Merkle sum tree's root before generating any inclusion proofs.
// Otherwise, users might distrust the provided `root_hash` in the inclusion proof, as it hasn't been published on-chain.
// Generate and export the inclusion proof for the specified user to a JSON file.
let inclusion_proof = round.get_proof_of_inclusion(USER_INDEX).unwrap();

let filename = format!("user_{}_proof.json", USER_INDEX);
Expand Down Expand Up @@ -142,6 +99,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

// Verify the `leaf_hash` from the proof file.
// It's assumed that both `user_name` and `balances` are provided by the CEX.
// The `balances` represent the user's balances on the CEX at `snapshot_time`.
let user_name = "dxGaEAii".to_string();
let balances = vec![11888, 41163];

Expand All @@ -166,20 +124,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
.collect();

// Get `mst_root` from contract. the `mst_root` is disptached by CEX with specific time `snapshot_time`.
let mst_root = summa_contract
.mst_roots(snapshot_time)
.call()
.await
.unwrap();
let mst_root = summa_contract.mst_roots(snapshot_time).call().await?;

// Match the `mst_root` with the `root_hash` derived from the proof.
assert_eq!(mst_root, public_inputs[1]);

// Validate the inclusion proof using the contract verifier.
let verification_result = summa_contract
.verify_inclusion_proof(proof, public_inputs, snapshot_time)
.await
.unwrap();
.await?;

println!(
"4. Verifying the proof on contract veirifer for User #{}: {}",
Expand Down
39 changes: 7 additions & 32 deletions backend/src/contracts/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use ethers::{
signers::{LocalWallet, Signer},
types::Address,
};
use serde_json::Value;
use std::{
error::Error, fs::File, io::BufReader, path::Path, str::FromStr, sync::Arc, time::Duration,
};
use std::{str::FromStr, sync::Arc, time::Duration};

use super::generated::summa_contract::{AddressOwnershipProof, Asset};

Expand Down Expand Up @@ -41,40 +38,17 @@ impl SummaSigner {
}
}

pub fn get_deployment_address<P: AsRef<Path>>(
path: P,
chain_id: u64,
) -> Result<Address, Box<dyn Error>> {
// Open file in RO mode with buffer
let file = File::open(path)?;
let reader = BufReader::new(file);

// Read the JSON contents of the file
let payload: Value = serde_json::from_reader(reader)?;

// Retrieve the contract address from the deployments.json file
let deployment_address: &Value = &payload.as_object().unwrap()
[chain_id.to_string().as_str()]
.as_object()
.unwrap()["address"];

let summa_address: &str = deployment_address.as_str().unwrap();

let address: Address = summa_address.parse().unwrap();

Ok(address)
}

pub async fn submit_proof_of_address_ownership(
&self,
address_ownership_proofs: Vec<AddressOwnershipProof>,
) -> Result<(), Box<dyn std::error::Error>> {
let submit_proof_of_address_ownership = &self
.summa_contract
.submit_proof_of_address_ownership(address_ownership_proofs);
let tx = submit_proof_of_address_ownership.send().await.unwrap();
let tx = submit_proof_of_address_ownership.send().await?;

tx.await.unwrap();
// Wait for the pending transaction to be mined
tx.await?;

Ok(())
}
Expand All @@ -89,9 +63,10 @@ impl SummaSigner {
let submit_proof_of_solvency_call = &self
.summa_contract
.submit_proof_of_solvency(mst_root, assets, proof, timestamp);
let tx = submit_proof_of_solvency_call.send().await.unwrap();
let tx = submit_proof_of_solvency_call.send().await?;

tx.await.unwrap();
// Wait for the pending transaction to be mined
tx.await?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ mod test {
.dispatch_proof_of_address_ownership()
.await;

assert_eq!(ownership_submitted_result.is_ok(), true);
assert!(ownership_submitted_result.is_ok());

let logs = summa_contract
.address_ownership_proof_submitted_filter()
Expand Down

0 comments on commit 5b2f510

Please sign in to comment.