Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Nov 11, 2024
1 parent 602bb28 commit b72f38b
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,7 @@ contract Token {
// Transfers token `amount` from public balance of message sender to a private balance of `to`.
#[private]
fn transfer_to_private(to: AztecAddress, amount: Field) {
// We check the minter permissions in the enqueued call as that allows us to avoid the need for `SharedMutable`
// which is less efficient.
// `from` is the owner of the public balance from which we'll subtract the `amount`.
let from = context.msg_sender();
let token = Token::at(context.this_address());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ export class BlacklistTokenContractTest {
this.logger.verbose(`Public balance of wallet 0: ${publicBalance}`);
expect(publicBalance).toEqual(this.tokenSim.balanceOfPublic(address));

tokenSim.mintPrivate(amount);
tokenSim.redeemShield(address, amount);
tokenSim.mintPrivate(address, amount);
const privateBalance = await asset.methods.balance_of_private(address).simulate();
this.logger.verbose(`Private balance of wallet 0: ${privateBalance}`);
expect(privateBalance).toEqual(tokenSim.balanceOfPrivate(address));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('e2e_blacklist_token_contract mint', () => {
describe('Mint flow', () => {
it('mint_private as minter', async () => {
const receipt = await asset.methods.mint_private(amount, secretHash).send().wait();
tokenSim.mintPrivate(amount);
tokenSim.mintPrivate(wallets[0].getAddress(), amount);
txHash = receipt.txHash;
});

Expand All @@ -97,7 +97,7 @@ describe('e2e_blacklist_token_contract mint', () => {
.send()
.wait({ debug: true });

tokenSim.redeemShield(wallets[0].getAddress(), amount);
tokenSim.mintPrivate(wallets[0].getAddress(), amount);
// 1 note should be created containing `amount` of tokens
const { visibleIncomingNotes } = receiptClaim.debugInfo!;
expect(visibleIncomingNotes.length).toBe(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ describe('e2e_blacklist_token_contract shield + redeem_shield', () => {

const receipt = await asset.methods.shield(wallets[0].getAddress(), amount, secretHash, 0).send().wait();

tokenSim.shield(wallets[0].getAddress(), amount);
await t.tokenSim.check();

// Redeem it
await t.addPendingShieldNoteToPXE(0, amount, secretHash, receipt.txHash);
await asset.methods.redeem_shield(wallets[0].getAddress(), amount, secret).send().wait();

tokenSim.redeemShield(wallets[0].getAddress(), amount);
// Check that the result matches token sim
tokenSim.transferToPrivate(wallets[0].getAddress(), wallets[0].getAddress(), amount);
await t.tokenSim.check();
});

it('on behalf of other', async () => {
Expand All @@ -59,9 +58,6 @@ describe('e2e_blacklist_token_contract shield + redeem_shield', () => {

const receipt = await action.send().wait();

tokenSim.shield(wallets[0].getAddress(), amount);
await t.tokenSim.check();

// Check that replaying the shield should fail!
await expect(
asset.withWallet(wallets[1]).methods.shield(wallets[0].getAddress(), amount, secretHash, nonce).simulate(),
Expand All @@ -71,7 +67,9 @@ describe('e2e_blacklist_token_contract shield + redeem_shield', () => {
await t.addPendingShieldNoteToPXE(0, amount, secretHash, receipt.txHash);
await asset.methods.redeem_shield(wallets[0].getAddress(), amount, secret).send().wait();

tokenSim.redeemShield(wallets[0].getAddress(), amount);
// Check that the result matches token sim
tokenSim.transferToPrivate(wallets[0].getAddress(), wallets[0].getAddress(), amount);
await t.tokenSim.check();
});

describe('failure cases', () => {
Expand Down
4 changes: 1 addition & 3 deletions yarn-project/end-to-end/src/e2e_lending_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,9 @@ describe('e2e_lending_contract', () => {
}

lendingSim.mintStableCoinOutsideLoan(lendingAccount.address, 10000n, true);
lendingSim.stableCoin.redeemShield(lendingAccount.address, 10000n);
lendingSim.mintStableCoinOutsideLoan(lendingAccount.address, 10000n, false);

lendingSim.collateralAsset.mintPrivate(10000n);
lendingSim.collateralAsset.redeemShield(lendingAccount.address, 10000n);
lendingSim.collateralAsset.mintPrivate(lendingAccount.address, 10000n);
lendingSim.collateralAsset.mintPublic(lendingAccount.address, 10000n);
});

Expand Down
3 changes: 1 addition & 2 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 @@ -375,8 +375,7 @@ export class FullProverTest {
this.logger.verbose(`Public balance of wallet 0: ${publicBalance}`);
expect(publicBalance).toEqual(this.tokenSim.balanceOfPublic(address));

tokenSim.mintPrivate(amount);
tokenSim.redeemShield(address, amount);
tokenSim.mintPrivate(address, amount);
const privateBalance = await asset.methods.balance_of_private(address).simulate();
this.logger.verbose(`Private balance of wallet 0: ${privateBalance}`);
expect(privateBalance).toEqual(tokenSim.balanceOfPrivate(address));
Expand Down
131 changes: 0 additions & 131 deletions yarn-project/end-to-end/src/e2e_token_contract/shielding.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ export class TokenContractTest {
this.logger.verbose(`Public balance of wallet 0: ${publicBalance}`);
expect(publicBalance).toEqual(this.tokenSim.balanceOfPublic(address));

tokenSim.mintPrivate(amount);
tokenSim.redeemShield(address, amount);
tokenSim.mintPrivate(address, amount);
const privateBalance = await asset.methods.balance_of_private(address).simulate();
this.logger.verbose(`Private balance of wallet 0: ${privateBalance}`);
expect(privateBalance).toEqual(tokenSim.balanceOfPrivate(address));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { U128_UNDERFLOW_ERROR } from '../fixtures/fixtures.js';
import { TokenContractTest } from './token_contract_test.js';

describe('e2e_token_contract transfer_to_private', () => {
const t = new TokenContractTest('transfer_to_private');
let { asset, accounts, tokenSim } = t;

beforeAll(async () => {
await t.applyBaseSnapshots();
await t.applyMintSnapshot();
await t.setup();
// Have to destructure again to ensure we have latest refs.
({ asset, accounts, tokenSim } = t);
});

afterAll(async () => {
await t.teardown();
});

afterEach(async () => {
await t.tokenSim.check();
});

it('to self', async () => {
const balancePub = await asset.methods.balance_of_public(accounts[0].address).simulate();
const amount = balancePub / 2n;
expect(amount).toBeGreaterThan(0n);

await asset.methods.transfer_to_private(accounts[0].address, amount).send().wait();

// Check that the result matches token sim
tokenSim.transferToPrivate(accounts[0].address, accounts[0].address, amount);
await tokenSim.check();
});

it('to someone else', async () => {
const balancePub = await asset.methods.balance_of_public(accounts[0].address).simulate();
const amount = balancePub / 2n;
expect(amount).toBeGreaterThan(0n);

await asset.methods.transfer_to_private(accounts[1].address, amount).send().wait();

// Check that the result matches token sim
tokenSim.transferToPrivate(accounts[0].address, accounts[1].address, amount);
await tokenSim.check();
});

describe('failure cases', () => {
it('to self (more than balance)', async () => {
const balancePub = await asset.methods.balance_of_public(accounts[0].address).simulate();
const amount = balancePub + 1n;
expect(amount).toBeGreaterThan(0n);

await expect(asset.methods.transfer_to_private(accounts[0].address, amount).simulate()).rejects.toThrow(
U128_UNDERFLOW_ERROR,
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class LendingSimulator {

mintStableCoinOutsideLoan(recipient: AztecAddress, amount: bigint, priv = false) {
if (priv) {
this.stableCoin.mintPrivate(amount);
this.stableCoin.mintPrivate(recipient, amount);
} else {
this.stableCoin.mintPublic(recipient, amount);
}
Expand Down
8 changes: 3 additions & 5 deletions yarn-project/end-to-end/src/simulators/token_simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export class TokenSimulator {
this.lookupProvider.set(account.toString(), wallet);
}

public mintPrivate(amount: bigint) {
public mintPrivate(to: AztecAddress, amount: bigint) {
this.totalSupply += amount;
this.balancesPrivate.set(to.toString(), (this.balancesPrivate.get(to.toString()) || 0n) + amount);
}

public mintPublic(to: AztecAddress, amount: bigint) {
Expand All @@ -54,13 +55,10 @@ export class TokenSimulator {
this.balancesPrivate.set(to.toString(), toBalance + amount);
}

public shield(from: AztecAddress, amount: bigint) {
public transferToPrivate(from: AztecAddress, to: AztecAddress, amount: bigint) {
const fromBalance = this.balancePublic.get(from.toString()) || 0n;
expect(fromBalance).toBeGreaterThanOrEqual(amount);
this.balancePublic.set(from.toString(), fromBalance - amount);
}

public redeemShield(to: AztecAddress, amount: bigint) {
const toBalance = this.balancesPrivate.get(to.toString()) || 0n;
this.balancesPrivate.set(to.toString(), toBalance + amount);
}
Expand Down
14 changes: 7 additions & 7 deletions yarn-project/protocol-contracts/src/protocol_contract_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ export const ProtocolContractAddress: Record<ProtocolContractName, AztecAddress>
};

export const ProtocolContractLeaf = {
AuthRegistry: Fr.fromString('0x0931f3bf89563f3898ae9650851083cd560ad800c2e3c561c3853eec4dd7ea8b'),
ContractInstanceDeployer: Fr.fromString('0x266ea4c9917455daa905c1dd1a10753714c6d0369b6f2fe23feeca6de556d164'),
ContractClassRegisterer: Fr.fromString('0x1ccb7a219f72a851089e956d527997b01068d5a28c9ae96b35ebeb45f068af23'),
MultiCallEntrypoint: Fr.fromString('0x1d060217817cf472a579638db722903fd1bbc4c3bdb0ecefa5694c0d4eed851a'),
FeeJuice: Fr.fromString('0x1e47caab3dd90f26b91e14e003a5ceab8d069b654174f6d698cdec9b1a6d19d5'),
Router: Fr.fromString('0x00827d5a8aedb9627d9e5de04735600a4dbb817d4a2f51281aab991699f5de99'),
AuthRegistry: Fr.fromString('0x2a41677b735548d2f24177ca3b9d610b38942de22fe15309946e4634ff8973d5'),
ContractInstanceDeployer: Fr.fromString('0x1cdb435ec8d3175011aff31f8dbd745a4247e1553222e56ad86d2f2af55398fa'),
ContractClassRegisterer: Fr.fromString('0x27cc2acbbbe18867a5491e1de928128e3399124e93ceef26057616d84510fab7'),
MultiCallEntrypoint: Fr.fromString('0x1684450a11b38a1c5a66e8b95339163ea729fdee9c4ffc914e22630abb55d3a5'),
FeeJuice: Fr.fromString('0x1d96812114fa939d893e40d8b89ff9391f9c6ddd4301d9fa0a73a12941c64670'),
Router: Fr.fromString('0x13c6f3df2f4f62572cba9abc3ac08d2eb64f6088c5a57b20b67eaa3eb3ee080b'),
};

export const protocolContractTreeRoot = Fr.fromString(
'0x24df09a860e983bd4e2e63538c62419d9640ae4d2ed77153b104cb8f1a9fd27e',
'0x0079d7feac3738bd39ab167ce31f537b56247be4a87cc35d3a7383657a5abf8c',
);

0 comments on commit b72f38b

Please sign in to comment.