Skip to content

Commit

Permalink
clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
just-mitch authored and alexghr committed Feb 23, 2024
1 parent c66c141 commit 7c05bfb
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 98 deletions.
96 changes: 52 additions & 44 deletions yarn-project/end-to-end/src/e2e_dapp_subscription.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
AccountWalletWithPrivateKey,
AztecAddress,
Contract,
FeePaymentMethod,
Fr,
PrivateFeePaymentMethod,
Expand All @@ -20,7 +19,13 @@ import {

import { jest } from '@jest/globals';

import { EndToEndContext, setup } from './fixtures/utils.js';
import {
EndToEndContext,
PublicBalancesFn,
assertPublicBalances,
getPublicBalancesFn,
setup,
} from './fixtures/utils.js';
import { GasBridgingTestHarness } from './shared/gas_portal_test_harness.js';

jest.setTimeout(1_000_000);
Expand All @@ -43,6 +48,12 @@ describe('e2e_fees', () => {
let bananaFPC: FPCContract;
let e2eContext: EndToEndContext;
let gasBridgeTestHarness: GasBridgingTestHarness;
let gasBalances: PublicBalancesFn;

const FEE_AMOUNT = 1n;
const SUBSCRIPTION_AMOUNT = 100n;
const BRIDGED_GAS_BALANCE = 1000n;
const MINTED_BANANAS = 1000n;

beforeAll(async () => {
process.env.PXE_URL = '';
Expand Down Expand Up @@ -83,7 +94,7 @@ describe('e2e_fees', () => {
bobAddress,
// anyone can purchase a subscription for 100 test tokens
bananaCoin.address,
100n,
SUBSCRIPTION_AMOUNT,
// I had to pass this in because the address kept changing
gasTokenContract.address,
)
Expand All @@ -92,49 +103,56 @@ describe('e2e_fees', () => {

// mint some test tokens for Alice
// she'll pay for the subscription with these
await bananaCoin.methods.privately_mint_private_note(1000n).send().wait();
await bananaCoin.methods.mint_public(aliceAddress, 1000n).send().wait();
await bananaCoin.methods.privately_mint_private_note(MINTED_BANANAS).send().wait();
await bananaCoin.methods.mint_public(aliceAddress, MINTED_BANANAS).send().wait();
await gasBridgeTestHarness.bridgeFromL1ToL2(BRIDGED_GAS_BALANCE, BRIDGED_GAS_BALANCE, subscriptionContract.address);
await gasBridgeTestHarness.bridgeFromL1ToL2(BRIDGED_GAS_BALANCE, BRIDGED_GAS_BALANCE, bananaFPC.address);

await gasBridgeTestHarness.bridgeFromL1ToL2(1000n, 1000n, subscriptionContract.address);
await gasBridgeTestHarness.bridgeFromL1ToL2(1000n, 1000n, bananaFPC.address);
gasBalances = getPublicBalancesFn('⛽', gasTokenContract, e2eContext.logger);

{
const { sequencerBalance, subscriptionBalance, fpcBalance } = await balances('⛽', gasTokenContract);
expect(sequencerBalance).toEqual(0n);
expect(subscriptionBalance).toEqual(1000n);
expect(fpcBalance).toEqual(1000n);
}
await assertPublicBalances(
gasBalances,
[sequencerAddress, subscriptionContract.address, bananaFPC.address],
[0n, BRIDGED_GAS_BALANCE, BRIDGED_GAS_BALANCE],
);
});

it('should allow Alice to subscribe by paying privately with bananas', async () => {
// Authorize the subscription contract to transfer the subscription amount from the subscriber.
await subscribe(new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet));
expect(await bananaCoin.methods.balance_of_private(aliceAddress).view()).toBe(899n);
expect(await bananaCoin.methods.balance_of_private(bobAddress).view()).toBe(100n);
expect(await bananaCoin.methods.balance_of_public(bananaFPC).view()).toBe(1n);
expect(await bananaCoin.methods.balance_of_private(aliceAddress).view()).toBe(
BRIDGED_GAS_BALANCE - SUBSCRIPTION_AMOUNT - FEE_AMOUNT,
);
expect(await bananaCoin.methods.balance_of_private(bobAddress).view()).toBe(SUBSCRIPTION_AMOUNT);
expect(await bananaCoin.methods.balance_of_public(bananaFPC).view()).toBe(FEE_AMOUNT);

// remains unchanged
expect(await gasTokenContract.methods.balance_of_public(subscriptionContract).view()).toBe(1000n);
expect(await gasTokenContract.methods.balance_of_public(bananaFPC).view()).toBe(999n);
expect(await gasTokenContract.methods.balance_of_public(sequencerAddress).view()).toBe(1n);
await assertPublicBalances(
gasBalances,
[subscriptionContract.address, bananaFPC.address, sequencerAddress],
[BRIDGED_GAS_BALANCE, BRIDGED_GAS_BALANCE - FEE_AMOUNT, FEE_AMOUNT],
);
});

it('should allow Alice to subscribe by paying with bananas in public', async () => {
// Authorize the subscription contract to transfer the subscription amount from the subscriber.
await subscribe(new PublicFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet));

// assert that Alice paid 100n for the subscription
expect(await bananaCoin.methods.balance_of_private(aliceAddress).view()).toBe(799n);
expect(await bananaCoin.methods.balance_of_private(bobAddress).view()).toBe(200n);
expect(await bananaCoin.methods.balance_of_private(aliceAddress).view()).toBe(
BRIDGED_GAS_BALANCE - 2n * SUBSCRIPTION_AMOUNT - FEE_AMOUNT,
);
expect(await bananaCoin.methods.balance_of_private(bobAddress).view()).toBe(2n * SUBSCRIPTION_AMOUNT);

// assert that Alice has paid one banana publicly for the tx above
expect(await bananaCoin.methods.balance_of_public(aliceAddress).view()).toBe(999n);
expect(await bananaCoin.methods.balance_of_public(bananaFPC).view()).toBe(2n);
expect(await gasTokenContract.methods.balance_of_public(bananaFPC).view()).toBe(998n);
expect(await bananaCoin.methods.balance_of_public(aliceAddress).view()).toBe(MINTED_BANANAS - FEE_AMOUNT);
expect(await bananaCoin.methods.balance_of_public(bananaFPC).view()).toBe(2n * FEE_AMOUNT);

// remains unchanged
expect(await gasTokenContract.methods.balance_of_public(subscriptionContract).view()).toBe(1000n);
expect(await gasTokenContract.methods.balance_of_public(sequencerAddress).view()).toBe(2n);
await assertPublicBalances(
gasBalances,
[subscriptionContract.address, bananaFPC.address, sequencerAddress],
[BRIDGED_GAS_BALANCE, BRIDGED_GAS_BALANCE - 2n * FEE_AMOUNT, 2n * FEE_AMOUNT],
);
});

it('should call dapp subscription entrypoint', async () => {
Expand All @@ -147,8 +165,12 @@ describe('e2e_fees', () => {
await sentTx.wait();

expect(await counterContract.methods.get_counter(bobAddress).view()).toBe(1n);
expect(await gasTokenContract.methods.balance_of_public(subscriptionContract).view()).toBe(999n);
expect(await gasTokenContract.methods.balance_of_public(sequencerAddress).view()).toBe(3n);

await assertPublicBalances(
gasBalances,
[subscriptionContract.address, sequencerAddress],
[BRIDGED_GAS_BALANCE - FEE_AMOUNT, FEE_AMOUNT * 3n],
);
});

it('should reject after the sub runs out', async () => {
Expand All @@ -169,7 +191,7 @@ describe('e2e_fees', () => {
async function subscribe(paymentMethod: FeePaymentMethod, blockDelta: number = 5, txCount: number = 4) {
{
const nonce = Fr.random();
const action = bananaCoin.methods.transfer(aliceAddress, bobAddress, 100n, nonce);
const action = bananaCoin.methods.transfer(aliceAddress, bobAddress, SUBSCRIPTION_AMOUNT, nonce);
const messageHash = computeAuthWitMessageHash(subscriptionContract.address, action.request());
await aliceWallet.createAuthWitness(messageHash);

Expand All @@ -195,18 +217,4 @@ describe('e2e_fees', () => {
const sentTx = new SentTx(pxe, pxe.sendTx(tx));
return sentTx.wait();
}

async function balances(symbol: string, contract: Contract) {
const [sequencerBalance, subscriptionBalance, fpcBalance] = await Promise.all([
contract.methods.balance_of_public(sequencerAddress).view(),
contract.methods.balance_of_public(subscriptionContract.address).view(),
contract.methods.balance_of_public(bananaFPC.address).view(),
]);

e2eContext.logger(
`${symbol} balances: Alice ${subscriptionBalance}, bananaPay: ${fpcBalance}, sequencer: ${sequencerBalance}`,
);

return { sequencerBalance, subscriptionBalance, fpcBalance };
}
});
74 changes: 30 additions & 44 deletions yarn-project/end-to-end/src/e2e_fees.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
AztecAddress,
Contract,
ExtendedNote,
Fr,
FunctionSelector,
Expand All @@ -15,7 +14,13 @@ import { TokenContract as BananaCoin, FPCContract, GasTokenContract } from '@azt

import { jest } from '@jest/globals';

import { EndToEndContext, setup } from './fixtures/utils.js';
import {
EndToEndContext,
PublicBalancesFn,
assertPublicBalances,
getPublicBalancesFn,
setup,
} from './fixtures/utils.js';
import { GasBridgingTestHarness } from './shared/gas_portal_test_harness.js';

const TOKEN_NAME = 'BananaCoin';
Expand All @@ -34,6 +39,9 @@ describe('e2e_fees', () => {
let gasBridgeTestHarness: GasBridgingTestHarness;
let e2eContext: EndToEndContext;

let gasBalances: PublicBalancesFn;
let bananaBalances: PublicBalancesFn;

beforeAll(async () => {
process.env.PXE_URL = '';
e2eContext = await setup(3);
Expand Down Expand Up @@ -90,20 +98,15 @@ describe('e2e_fees', () => {
.deployed();
e2eContext.logger(`bananaPay deployed at ${bananaFPC.address}`);
await gasBridgeTestHarness.bridgeFromL1ToL2(InitialFPCGas + 1n, InitialFPCGas, bananaFPC.address);
{
const { sequencerBalance, aliceBalance, fpcBalance } = await balances('⛽', gasTokenContract);
expect(sequencerBalance).toEqual(0n);
expect(aliceBalance).toEqual(0n);
expect(fpcBalance).toEqual(InitialFPCGas);
}

{
// Sanity check. No public bananas yet.
const { sequencerBalance, aliceBalance, fpcBalance } = await balances('🍌', bananaCoin);
expect(sequencerBalance).toEqual(0n);
expect(aliceBalance).toEqual(0n);
expect(fpcBalance).toEqual(0n);
}

gasBalances = getPublicBalancesFn('⛽', gasTokenContract, e2eContext.logger);
bananaBalances = getPublicBalancesFn('🍌', bananaCoin, e2eContext.logger);
await assertPublicBalances(
gasBalances,
[sequencerAddress, aliceAddress, bananaFPC.address],
[0n, 0n, InitialFPCGas],
);
await assertPublicBalances(bananaBalances, [sequencerAddress, aliceAddress, bananaFPC.address], [0n, 0n, 0n]);
}, 100_000);

it('mint banana privately, pay privately with banana via FPC', async () => {
Expand Down Expand Up @@ -142,20 +145,17 @@ describe('e2e_fees', () => {
})
.wait();

// Fee asset
{
const { sequencerBalance, aliceBalance, fpcBalance } = await balances('⛽', gasTokenContract);
expect(sequencerBalance).toEqual(FeeAmount);
expect(aliceBalance).toEqual(0n);
expect(fpcBalance).toEqual(InitialFPCGas - FeeAmount);
}
// Bananas asset
{
const { sequencerBalance, aliceBalance, fpcBalance } = await balances('🍌', bananaCoin);
expect(sequencerBalance).toEqual(0n);
expect(aliceBalance).toEqual(MintedBananasAmount);
expect(fpcBalance).toEqual(FeeAmount);
}
await assertPublicBalances(
gasBalances,
[sequencerAddress, aliceAddress, bananaFPC.address],
[FeeAmount, 0n, InitialFPCGas - FeeAmount],
);

await assertPublicBalances(
bananaBalances,
[sequencerAddress, aliceAddress, bananaFPC.address],
[0n, MintedBananasAmount, FeeAmount],
);
}, 100_000);

const addPendingShieldNoteToPXE = async (accountIndex: number, amount: bigint, secretHash: Fr, txHash: TxHash) => {
Expand All @@ -173,18 +173,4 @@ describe('e2e_fees', () => {
);
await e2eContext.wallets[accountIndex].addNote(extendedNote);
};

const balances = async (symbol: string, contract: Contract) => {
const [sequencerBalance, aliceBalance, fpcBalance] = await Promise.all([
contract.methods.balance_of_public(sequencerAddress).view(),
contract.methods.balance_of_public(aliceAddress).view(),
contract.methods.balance_of_public(bananaFPC.address).view(),
]);

e2eContext.logger(
`${symbol} balances: Alice ${aliceBalance}, bananaPay: ${fpcBalance}, sequencer: ${sequencerBalance}`,
);

return { sequencerBalance, aliceBalance, fpcBalance };
};
});
32 changes: 22 additions & 10 deletions yarn-project/end-to-end/src/fixtures/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,17 +410,29 @@ export const expectUnencryptedLogsFromLastBlockToBe = async (pxe: PXE, logMessag
expect(asciiLogs).toStrictEqual(logMessages);
};

export function getBalancesFn(
addresses: AztecAddress[],
): (symbol: string, contract: Contract) => Record<AztecAddress, bigint> {
const balances = async (symbol: string, contract: Contract, logger?: any) => {
export type PublicBalancesFn = ReturnType<typeof getPublicBalancesFn>;
export function getPublicBalancesFn(
symbol: string,
contract: Contract,
logger: any,
): (...addresses: AztecAddress[]) => Promise<bigint[]> {
const balances = async (...addresses: AztecAddress[]) => {
const b = await Promise.all(addresses.map(address => contract.methods.balance_of_public(address).view()));
const debugString = `${symbol} balances: ${addresses.map((address, i) => `${address}: ${b[i]}`).join(', ')}`;
logger(debugString);
return b;
};

const debugString = ``;

logger();
// `${symbol} balances: Alice ${aliceBalance}, bananaPay: ${fpcBalance}, sequencer: ${sequencerBalance}`,
return balances;
}

return { sequencerBalance, aliceBalance, fpcBalance };
};
export async function assertPublicBalances(
balances: PublicBalancesFn,
addresses: AztecAddress[],
expectedBalances: bigint[],
) {
const actualBalances = await balances(...addresses);
for (let i = 0; i < addresses.length; i++) {
expect(actualBalances[i]).toBe(expectedBalances[i]);
}
}

0 comments on commit 7c05bfb

Please sign in to comment.