Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 29, 2024
1 parent 38aa85e commit 30c0f62
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 257 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::test::utils;
use crate::{Token, types::transparent_note::TransparentNote};
use dep::aztec::{hash::compute_secret_hash, oracle::random::random, test::helpers::cheatcodes};
use dep::aztec::{oracle::random::random, test::helpers::cheatcodes};

#[test]
unconstrained fn mint_public_success() {
Expand Down Expand Up @@ -56,185 +56,3 @@ unconstrained fn mint_public_failures() {
utils::check_public_balance(token_contract_address, recipient, mint_for_recipient_amount);
utils::check_public_balance(token_contract_address, owner, 0);
}

#[test]
unconstrained fn mint_private_success() {
// Setup without account contracts. We are not using authwits here, so dummy accounts are enough
let (env, token_contract_address, owner, _) = utils::setup(/* with_account_contracts */ false);
let mint_amount = 10000;
// Mint some tokens
let secret = random();
let secret_hash = compute_secret_hash(secret);
Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public());

Token::at(token_contract_address).mint_public(owner, mint_amount).call(&mut env.public());

// Time travel so we can read keys from the registry
env.advance_block_by(6);

// We need to manually add the note to TXE because `TransparentNote` does not support automatic note log delivery.
env.add_note(
&mut TransparentNote::new(mint_amount, secret_hash),
Token::storage_layout().pending_shields.slot,
token_contract_address,
);

// Redeem our shielded tokens
Token::at(token_contract_address).redeem_shield(owner, mint_amount, secret).call(
&mut env.private(),
);

utils::check_private_balance(token_contract_address, owner, mint_amount);
}

#[test(should_fail_with = "note not popped")]
unconstrained fn mint_private_failure_double_spend() {
// Setup without account contracts. We are not using authwits here, so dummy accounts are enough
let (env, token_contract_address, owner, recipient) =
utils::setup(/* with_account_contracts */ false);
let mint_amount = 10000;
// Mint some tokens
let secret = random();
let secret_hash = compute_secret_hash(secret);
Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public());

Token::at(token_contract_address).mint_public(owner, mint_amount).call(&mut env.public());

// Time travel so we can read keys from the registry
env.advance_block_by(6);

// We need to manually add the note to TXE because `TransparentNote` does not support automatic note log delivery.
env.add_note(
&mut TransparentNote::new(mint_amount, secret_hash),
Token::storage_layout().pending_shields.slot,
token_contract_address,
);

// Redeem our shielded tokens
Token::at(token_contract_address).redeem_shield(owner, mint_amount, secret).call(
&mut env.private(),
);

utils::check_private_balance(token_contract_address, owner, mint_amount);

// Attempt to double spend
Token::at(token_contract_address).redeem_shield(recipient, mint_amount, secret).call(
&mut env.private(),
);
}

#[test(should_fail_with = "caller is not minter")]
unconstrained fn mint_private_failure_non_minter() {
// Setup without account contracts. We are not using authwits here, so dummy accounts are enough
let (env, token_contract_address, _, recipient) =
utils::setup(/* with_account_contracts */ false);
let mint_amount = 10000;
// Try to mint some tokens impersonating recipient
env.impersonate(recipient);

let secret = random();
let secret_hash = compute_secret_hash(secret);
Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public());
}

#[test(should_fail_with = "call to assert_max_bit_size")]
unconstrained fn mint_private_failure_overflow() {
// Setup without account contracts. We are not using authwits here, so dummy accounts are enough
let (env, token_contract_address, _, _) = utils::setup(/* with_account_contracts */ false);

// Overflow recipient
let mint_amount = 2.pow_32(128);
let secret = random();
let secret_hash = compute_secret_hash(secret);
Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public());
}

#[test(should_fail_with = "attempt to add with overflow")]
unconstrained fn mint_private_failure_overflow_recipient() {
// Setup without account contracts. We are not using authwits here, so dummy accounts are enough
let (env, token_contract_address, owner, _) = utils::setup(/* with_account_contracts */ false);
let mint_amount = 10000;
// Mint some tokens
let secret = random();
let secret_hash = compute_secret_hash(secret);
Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public());
// Time travel so we can read keys from the registry
env.advance_block_by(6);

// We need to manually add the note to TXE because `TransparentNote` does not support automatic note log delivery.
env.add_note(
&mut TransparentNote::new(mint_amount, secret_hash),
Token::storage_layout().pending_shields.slot,
token_contract_address,
);

// Redeem our shielded tokens
Token::at(token_contract_address).redeem_shield(owner, mint_amount, secret).call(
&mut env.private(),
);

utils::check_private_balance(token_contract_address, owner, mint_amount);

let mint_amount = 2.pow_32(128) - mint_amount;
// Mint some tokens
let secret = random();
let secret_hash = compute_secret_hash(secret);
Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public());
}

#[test(should_fail_with = "attempt to add with overflow")]
unconstrained fn mint_private_failure_overflow_total_supply() {
// Setup without account contracts. We are not using authwits here, so dummy accounts are enough
let (env, token_contract_address, owner, recipient) =
utils::setup(/* with_account_contracts */ false);
let mint_amount = 10000;
// Mint some tokens
let secret_owner = random();
let secret_recipient = random();
let secret_hash_owner = compute_secret_hash(secret_owner);
let secret_hash_recipient = compute_secret_hash(secret_recipient);

Token::at(token_contract_address).mint_private(mint_amount, secret_hash_owner).call(
&mut env.public(),
);
Token::at(token_contract_address).mint_private(mint_amount, secret_hash_recipient).call(
&mut env.public(),
);

// Time travel so we can read keys from the registry
env.advance_block_by(6);

// Store 2 notes in the cache so we can redeem it for owner and recipient
env.add_note(
&mut TransparentNote::new(mint_amount, secret_hash_owner),
Token::storage_layout().pending_shields.slot,
token_contract_address,
);
env.add_note(
&mut TransparentNote::new(mint_amount, secret_hash_recipient),
Token::storage_layout().pending_shields.slot,
token_contract_address,
);

// Redeem owner's shielded tokens
env.impersonate(owner);
Token::at(token_contract_address).redeem_shield(owner, mint_amount, secret_owner).call(
&mut env.private(),
);

// Redeem recipient's shielded tokens
env.impersonate(recipient);
Token::at(token_contract_address).redeem_shield(recipient, mint_amount, secret_recipient).call(
&mut env.private(),
);

utils::check_private_balance(token_contract_address, owner, mint_amount);
utils::check_private_balance(token_contract_address, recipient, mint_amount);

env.impersonate(owner);
let mint_amount = 2.pow_32(128) - 2 * mint_amount;
// Try to mint some tokens
let secret = random();
let secret_hash = compute_secret_hash(secret);
Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public());
}
22 changes: 10 additions & 12 deletions yarn-project/end-to-end/src/e2e_prover/e2e_prover_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
Note,
type PXE,
type TxHash,
computeSecretHash,
createDebugLogger,
deployL1Contract,
} from '@aztec/aztec.js';
Expand Down Expand Up @@ -346,24 +345,23 @@ export class FullProverTest {
'mint',
async () => {
const { fakeProofsAsset: asset, accounts } = this;
const amount = 10000n;
const privateAmount = 10000n;
const publicAmount = 10000n;

const waitOpts = { proven: false };

this.logger.verbose(`Minting ${amount} publicly...`);
await asset.methods.mint_public(accounts[0].address, amount).send().wait(waitOpts);
this.logger.verbose(`Minting ${privateAmount + publicAmount} publicly...`);
await asset.methods
.mint_public(accounts[0].address, privateAmount + publicAmount)
.send()
.wait(waitOpts);

this.logger.verbose(`Minting ${amount} privately...`);
const secret = Fr.random();
const secretHash = computeSecretHash(secret);
const receipt = await asset.methods.mint_private(amount, secretHash).send().wait(waitOpts);
this.logger.verbose(`Transferring ${privateAmount} to private...`);
await asset.methods.transfer_to_private(accounts[0].address, privateAmount).send().wait(waitOpts);

await this.addPendingShieldNoteToPXE(0, amount, secretHash, receipt.txHash);
const txClaim = asset.methods.redeem_shield(accounts[0].address, amount, secret).send();
await txClaim.wait({ ...waitOpts, debug: true });
this.logger.verbose(`Minting complete.`);

return { amount };
return { amount: publicAmount };
},
async ({ amount }) => {
const {
Expand Down
69 changes: 9 additions & 60 deletions yarn-project/end-to-end/src/guides/dapp_testing.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
// docs:start:imports
import { createAccount, getDeployedTestAccountsWallets } from '@aztec/accounts/testing';
import {
type AccountWallet,
CheatCodes,
ExtendedNote,
Fr,
Note,
type PXE,
TxStatus,
computeSecretHash,
createPXEClient,
waitForPXE,
} from '@aztec/aztec.js';
import { type AccountWallet, CheatCodes, Fr, type PXE, TxStatus, createPXEClient, waitForPXE } from '@aztec/aztec.js';
// docs:end:imports
// docs:start:import_contract
import { TestContract } from '@aztec/noir-contracts.js/Test';
// docs:end:import_contract
import { TokenContract } from '@aztec/noir-contracts.js/Token';

import { U128_UNDERFLOW_ERROR } from '../fixtures/fixtures.js';
import { mintTokensToPrivate } from '../fixtures/token_utils.js';

const { PXE_URL = 'http://localhost:8080', ETHEREUM_HOST = 'http://localhost:8545' } = process.env;

Expand Down Expand Up @@ -51,22 +41,8 @@ describe('guides/dapp/testing', () => {
expect(await token.methods.balance_of_private(recipientAddress).simulate()).toEqual(0n);

const mintAmount = 20n;
const secret = Fr.random();
const secretHash = computeSecretHash(secret);
const receipt = await token.methods.mint_private(mintAmount, secretHash).send().wait();

const note = new Note([new Fr(mintAmount), secretHash]);
const extendedNote = new ExtendedNote(
note,
recipientAddress,
token.address,
TokenContract.storage.pending_shields.slot,
TokenContract.notes.TransparentNote.id,
receipt.txHash,
);
await owner.addNote(extendedNote);

await token.methods.redeem_shield(recipientAddress, mintAmount, secret).send().wait();
await mintTokensToPrivate(token, owner, recipientAddress, mintAmount);

expect(await token.withWallet(recipient).methods.balance_of_private(recipientAddress).simulate()).toEqual(20n);
});
});
Expand All @@ -91,22 +67,9 @@ describe('guides/dapp/testing', () => {
expect(await token.methods.balance_of_private(recipient.getAddress()).simulate()).toEqual(0n);
const recipientAddress = recipient.getAddress();
const mintAmount = 20n;
const secret = Fr.random();
const secretHash = computeSecretHash(secret);
const receipt = await token.methods.mint_private(mintAmount, secretHash).send().wait();

const note = new Note([new Fr(mintAmount), secretHash]);
const extendedNote = new ExtendedNote(
note,
recipientAddress,
token.address,
TokenContract.storage.pending_shields.slot,
TokenContract.notes.TransparentNote.id,
receipt.txHash,
);
await owner.addNote(extendedNote);

await token.methods.redeem_shield(recipientAddress, mintAmount, secret).send().wait();

await mintTokensToPrivate(token, owner, recipientAddress, mintAmount);

expect(await token.withWallet(recipient).methods.balance_of_private(recipientAddress).simulate()).toEqual(20n);
});
});
Expand All @@ -131,22 +94,8 @@ describe('guides/dapp/testing', () => {

const ownerAddress = owner.getAddress();
const mintAmount = 100n;
const secret = Fr.random();
const secretHash = computeSecretHash(secret);
const receipt = await token.methods.mint_private(100n, secretHash).send().wait();

const note = new Note([new Fr(mintAmount), secretHash]);
const extendedNote = new ExtendedNote(
note,
ownerAddress,
token.address,
TokenContract.storage.pending_shields.slot,
TokenContract.notes.TransparentNote.id,
receipt.txHash,
);
await owner.addNote(extendedNote);

await token.methods.redeem_shield(ownerAddress, 100n, secret).send().wait();

await mintTokensToPrivate(token, owner, ownerAddress, mintAmount);

// docs:start:calc-slot
cheats = await CheatCodes.create(ETHEREUM_HOST, pxe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {
AuthWitness,
type AuthWitnessProvider,
BatchCall,
type CompleteAddress, Fr,
GrumpkinScalar, Schnorr
type CompleteAddress,
Fr,
GrumpkinScalar,
Schnorr,
} from '@aztec/aztec.js';
import { SchnorrHardcodedAccountContractArtifact } from '@aztec/noir-contracts.js/SchnorrHardcodedAccount';
import { TokenContract } from '@aztec/noir-contracts.js/Token';
Expand Down

0 comments on commit 30c0f62

Please sign in to comment.