Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
kroist committed Feb 9, 2024
1 parent ad3880f commit 2fcc4e9
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 26 deletions.
7 changes: 4 additions & 3 deletions shielder/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ help: # Show help for each of the Makefile recipes.
.PHONY: build-psp22
build-psp22: ## Builds psp22 contracts.
@echo "Building psp22 contract" ; \
git submodule update --init ; \
cargo contract build --quiet --manifest-path PSP22/Cargo.toml --features "contract" --release ; \

.PHONY: build-shielder
Expand All @@ -17,10 +18,10 @@ build-shielder: ## Builds shielder contracts.
.PHONY: setup-tests
setup-tests: build-psp22 ## Builds contracts and generates wrappers.

.PHONY: drink-tests
drink-tests: ## Runs tests for drink contract.
.PHONY: tests
tests: ## Runs tests for contract.
@echo "Running drink tests" ; \
cargo test --quiet --manifest-path contract/Cargo.toml --release -- --nocapture ; \

.PHONY:
all: setup-tests drink-tests ## Runs all tests.
all: setup-tests tests ## Runs all tests.
2 changes: 1 addition & 1 deletion shielder/contract/drink_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use drink::{session::Session, AccountId32};

use crate::{
drink_tests::utils::{chain::*, ops::*, psp22::*, shielder::*},
mocked_zk::merkle::MerkleTree,
test_utils::merkle::MerkleTree,
};

#[drink::contract_bundle_provider]
Expand Down
7 changes: 4 additions & 3 deletions shielder/contract/drink_tests/utils/chain.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::drink_tests::utils::ACCOUNT_INITIAL_AMOUNT;
use drink::{runtime::MinimalRuntime, session::Session, AccountId32};

pub fn init_acc_with_balance(
Expand All @@ -6,23 +7,23 @@ pub fn init_acc_with_balance(
) -> Result<(), Box<dyn std::error::Error>> {
session
.sandbox()
.mint_into(acc.clone(), 1000000000000000)
.mint_into(acc.clone(), ACCOUNT_INITIAL_AMOUNT)
.unwrap();
Ok(())
}

pub fn init_alice(
session: &mut Session<MinimalRuntime>,
) -> Result<AccountId32, Box<dyn std::error::Error>> {
let res: AccountId32 = AccountId32::new([2u8; 32]);
let res = AccountId32::new([2u8; 32]);
init_acc_with_balance(session, &res)?;
Ok(res)
}

pub fn init_bob(
session: &mut Session<MinimalRuntime>,
) -> Result<AccountId32, Box<dyn std::error::Error>> {
let res: AccountId32 = AccountId32::new([3u8; 32]);
let res = AccountId32::new([3u8; 32]);
init_acc_with_balance(session, &res)?;
Ok(res)
}
2 changes: 2 additions & 0 deletions shielder/contract/drink_tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ pub mod chain;
pub mod ops;
pub mod psp22;
pub mod shielder;

pub const ACCOUNT_INITIAL_AMOUNT: u128 = 1_000_000_000_000_000;
4 changes: 2 additions & 2 deletions shielder/contract/drink_tests/utils/psp22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn get_psp22_balance(
let res: u128 = session.call_with_address(
token.clone(),
"PSP22::balance_of",
&[&*(*address).to_string()],
&[&address.to_string()],
NO_ENDOWMENT,
)??;
Ok(res)
Expand All @@ -51,7 +51,7 @@ pub fn get_psp22_allowance(
let res: u128 = session.call_with_address(
token.clone(),
"PSP22::allowance",
&[&*(*from).to_string(), &*(*to).to_string()],
&[&from.to_string(), &to.to_string()],
NO_ENDOWMENT,
)??;
Ok(res)
Expand Down
2 changes: 1 addition & 1 deletion shielder/contract/drink_tests/utils/shielder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use crate::{
drink_tests::{BundleProvider, UpdateOperation},
mocked_zk::{
account::Account,
merkle::MerkleTree,
note::Note,
ops::{OpPriv, Operation},
relations::ZkProof,
traits::Hashable,
TOKENS_NUMBER,
},
test_utils::merkle::MerkleTree,
types::Scalar,
};
pub struct ShielderUserEnv {
Expand Down
2 changes: 2 additions & 0 deletions shielder/contract/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ mod drink_tests;
mod errors;
mod merkle;
pub mod mocked_zk;
#[cfg(test)]
pub mod test_utils;
mod traits;
mod types;

Expand Down
7 changes: 5 additions & 2 deletions shielder/contract/mocked_zk/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
pub mod account;
#[cfg(feature = "std")]
pub mod merkle;
pub mod note;
pub mod ops;
pub mod relations;
#[cfg(test)]
mod tests;
pub mod traits;

use crate::types::Scalar;

pub const TOKENS_NUMBER: usize = 10;
pub const MOCKED_TOKEN: Scalar = Scalar {
bytes: [228_u8; 32],
};
7 changes: 2 additions & 5 deletions shielder/contract/mocked_zk/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ use super::{
use crate::{
contract::OpPub,
errors::ShielderError,
mocked_zk::{merkle::MerkleTree, TOKENS_NUMBER},
mocked_zk::{MOCKED_TOKEN, TOKENS_NUMBER},
test_utils::merkle::MerkleTree,
types::Scalar,
};

const MOCKED_TOKEN: Scalar = Scalar {
bytes: [228 as u8; 32],
};

fn create_empty_note_proof(id: Scalar, nullifier: Scalar, trapdoor: Scalar) -> (Scalar, ZkProof) {
let mut tokens: [Scalar; TOKENS_NUMBER] = [0_u128.into(); TOKENS_NUMBER];
tokens[0] = MOCKED_TOKEN;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{errors::ShielderError, types::Scalar};
use ink::env::hash::{CryptoHash, Sha2x256};
use std::num::Wrapping;

use crate::{errors::ShielderError, types::Scalar};

/// depth of the tree
pub const DEPTH: usize = 10;
Expand Down Expand Up @@ -31,18 +31,18 @@ impl MerkleTree {
if self.next_leaf_idx == self.size {
return Err(ShielderError::MerkleTreeLimitExceeded);
}
let mut id = (Wrapping(self.next_leaf_idx) + Wrapping(self.size)).0;
let mut id = self.next_leaf_idx + self.size;
self.nodes[id] = leaf_value;

id /= 2;
while id > 0 {
let left_n = self.nodes[(Wrapping(id) * Wrapping(2)).0];
let right_n = self.nodes[(Wrapping(id) * Wrapping(2) + Wrapping(1)).0];
let left_n = self.nodes[id * 2];
let right_n = self.nodes[id * 2 + 1];
let hash = compute_hash(left_n, right_n);
self.nodes[id] = hash;
id /= 2;
}
self.next_leaf_idx = (Wrapping(self.next_leaf_idx) + Wrapping(1)).0;
self.next_leaf_idx += 1;
Ok(self.nodes[1])
}

Expand All @@ -55,9 +55,9 @@ impl MerkleTree {
if self.next_leaf_idx == self.size {
return Err(ShielderError::MerkleTreeProofGenFail);
}
let mut id = (Wrapping(leaf_id) + Wrapping(self.size)).0;
for node in res.iter_mut().take(DEPTH) {
*node = self.nodes[id ^ 1];
let mut id = leaf_id + self.size;
for i in 0..DEPTH {
res[i] = self.nodes[id ^ 1];
id /= 2;
}
Ok(res)
Expand Down
1 change: 1 addition & 0 deletions shielder/contract/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod merkle;

0 comments on commit 2fcc4e9

Please sign in to comment.