Skip to content

Commit

Permalink
alter e2e crosschain harness to be the same as canary
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-kothari committed Oct 11, 2023
1 parent 7e705ed commit 9be2846
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 130 deletions.
20 changes: 4 additions & 16 deletions yarn-project/end-to-end/src/e2e_cross_chain_messaging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,14 @@ describe('e2e_cross_chain_messaging', () => {
let outbox: any;

beforeEach(async () => {
const {
aztecNode,
pxe,
deployL1ContractsValues,
accounts,
wallets,
logger: logger_,
cheatCodes,
teardown: teardown_,
} = await setup(2);
const { pxe, deployL1ContractsValues, wallets, logger: logger_, teardown: teardown_ } = await setup(2);

crossChainTestHarness = await CrossChainTestHarness.new(
aztecNode,
pxe,
deployL1ContractsValues,
accounts,
deployL1ContractsValues.publicClient,
deployL1ContractsValues.walletClient,
wallets[0],
logger_,
cheatCodes,
);

l2Token = crossChainTestHarness.l2Token;
Expand All @@ -58,7 +47,6 @@ describe('e2e_cross_chain_messaging', () => {

afterEach(async () => {
await teardown();
await crossChainTestHarness?.stop();
});

it('Milestone 2: Deposit funds from L1 -> L2 and withdraw back to L1', async () => {
Expand Down Expand Up @@ -230,7 +218,7 @@ describe('e2e_cross_chain_messaging', () => {
await delay(5000); /// waiting 5 seconds.

// Perform an unrelated transaction on L2 to progress the rollup. Here we mint public tokens.
await crossChainTestHarness.performL2Transfer(0n);
await crossChainTestHarness.mintTokensPublicOnL2(0n);

// 3. Consume L1-> L2 message and try to mint publicly on L2 - should fail
await expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,13 @@ describe('e2e_public_cross_chain_messaging', () => {
let outbox: any;

beforeEach(async () => {
const {
aztecNode: aztecNode_,
pxe,
deployL1ContractsValues,
accounts,
wallets,
logger: logger_,
teardown: teardown_,
cheatCodes,
} = await setup(2);
const { pxe, deployL1ContractsValues, wallets, logger: logger_, teardown: teardown_ } = await setup(2);
crossChainTestHarness = await CrossChainTestHarness.new(
aztecNode_,
pxe,
deployL1ContractsValues,
accounts,
deployL1ContractsValues.publicClient,
deployL1ContractsValues.walletClient,
wallets[0],
logger_,
cheatCodes,
);
l2Token = crossChainTestHarness.l2Token;
l2Bridge = crossChainTestHarness.l2Bridge;
Expand All @@ -57,7 +46,6 @@ describe('e2e_public_cross_chain_messaging', () => {

afterEach(async () => {
await teardown();
await crossChainTestHarness?.stop();
});

it('Milestone 2: Deposit funds from L1 -> L2 and withdraw back to L1', async () => {
Expand Down Expand Up @@ -184,7 +172,7 @@ describe('e2e_public_cross_chain_messaging', () => {
await delay(5000); /// waiting 5 seconds.

// Perform an unrelated transaction on L2 to progress the rollup. Here we mint public tokens.
await crossChainTestHarness.performL2Transfer(0n);
await crossChainTestHarness.mintTokensPublicOnL2(0n);

await expect(
l2Bridge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,13 @@ describe('e2e_public_to_private_messaging', () => {
let crossChainTestHarness: CrossChainTestHarness;

beforeEach(async () => {
const {
aztecNode,
pxe,
deployL1ContractsValues,
accounts,
wallet,
logger: logger_,
cheatCodes,
teardown: teardown_,
} = await setup(2);
const { pxe, deployL1ContractsValues, wallet, logger: logger_, teardown: teardown_ } = await setup(2);
crossChainTestHarness = await CrossChainTestHarness.new(
aztecNode,
pxe,
deployL1ContractsValues,
accounts,
deployL1ContractsValues.publicClient,
deployL1ContractsValues.walletClient,
wallet,
logger_,
cheatCodes,
);

ethAccount = crossChainTestHarness.ethAccount;
Expand All @@ -49,7 +38,6 @@ describe('e2e_public_to_private_messaging', () => {

afterEach(async () => {
await teardown();
await crossChainTestHarness?.stop();
});

it('Milestone 5.4: Should be able to create a commitment in a public function and spend in a private function', async () => {
Expand Down
93 changes: 26 additions & 67 deletions yarn-project/end-to-end/src/fixtures/cross_chain_test_harness.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { AztecNodeService } from '@aztec/aztec-node';
import { CheatCodes, TxHash, Wallet, computeMessageSecretHash } from '@aztec/aztec.js';
import { AztecAddress, CompleteAddress, EthAddress, Fr, PublicKey } from '@aztec/circuits.js';
import { DeployL1Contracts } from '@aztec/ethereum';
import { toBufferBE } from '@aztec/foundation/bigint-buffer';
import { TxHash, Wallet, computeMessageSecretHash } from '@aztec/aztec.js';
import { AztecAddress, EthAddress, Fr } from '@aztec/circuits.js';
import { sha256ToField } from '@aztec/foundation/crypto';
import { DebugLogger } from '@aztec/foundation/log';
import { OutboxAbi } from '@aztec/l1-artifacts';
import { TokenBridgeContract, TokenContract } from '@aztec/noir-contracts/types';
import { PXEService } from '@aztec/pxe';
import { AztecNode, NotePreimage, PXE, TxStatus } from '@aztec/types';
import { NotePreimage, PXE, TxStatus } from '@aztec/types';

import { Chain, HttpTransport, PublicClient, getContract } from 'viem';

Expand All @@ -20,61 +16,41 @@ import { deployAndInitializeTokenAndBridgeContracts } from './utils.js';
*/
export class CrossChainTestHarness {
static async new(
aztecNode: AztecNode | undefined,
pxeService: PXE,
deployL1ContractsValues: DeployL1Contracts,
accounts: CompleteAddress[],
publicClient: PublicClient<HttpTransport, Chain>,
walletClient: any,
wallet: Wallet,
logger: DebugLogger,
cheatCodes: CheatCodes,
underlyingERC20Address?: EthAddress,
initialBalance?: bigint,
): Promise<CrossChainTestHarness> {
const walletClient = deployL1ContractsValues.walletClient;
const publicClient = deployL1ContractsValues.publicClient;
const ethAccount = EthAddress.fromString((await walletClient.getAddresses())[0]);
const [owner, receiver] = accounts;
const owner = wallet.getCompleteAddress();
const l1ContractAddresses = (await pxeService.getNodeInfo()).l1ContractAddresses;

const outbox = getContract({
address: deployL1ContractsValues.l1ContractAddresses.outboxAddress!.toString(),
address: l1ContractAddresses.outboxAddress!.toString(),
abi: OutboxAbi,
publicClient,
});

// Deploy and initialize all required contracts
logger('Deploying and initializing token, portal and its bridge...');
const contracts = await deployAndInitializeTokenAndBridgeContracts(
wallet,
walletClient,
publicClient,
deployL1ContractsValues!.l1ContractAddresses.registryAddress!,
owner.address,
underlyingERC20Address,
);
const l2Token = contracts.token;
const l2Bridge = contracts.bridge;
const underlyingERC20 = contracts.underlyingERC20;
const tokenPortal = contracts.tokenPortal;
const tokenPortalAddress = contracts.tokenPortalAddress;
const { token, bridge, tokenPortalAddress, tokenPortal, underlyingERC20 } =
await deployAndInitializeTokenAndBridgeContracts(
wallet,
walletClient,
publicClient,
l1ContractAddresses.registryAddress,
owner.address,
underlyingERC20Address,
);
logger('Deployed and initialized token, portal and its bridge.');

if (initialBalance) {
logger(`Minting ${initialBalance} tokens to ${owner.address}...`);
const mintTx = l2Token.methods.mint_public(owner.address, initialBalance).send();
const mintReceipt = await mintTx.wait();
expect(mintReceipt.status).toBe(TxStatus.MINED);
expect(l2Token.methods.balance_of_public(owner.address).view()).toBe(initialBalance);
logger(`Minted ${initialBalance} tokens to ${owner.address}.`);
}

return new CrossChainTestHarness(
aztecNode,
pxeService,
cheatCodes,
accounts,
logger,
l2Token,
l2Bridge,
token,
bridge,
ethAccount,
tokenPortalAddress,
tokenPortal,
Expand All @@ -83,19 +59,12 @@ export class CrossChainTestHarness {
publicClient,
walletClient,
owner.address,
receiver.address,
owner.publicKey,
);
}

constructor(
/** AztecNode. */
public aztecNode: AztecNode | undefined,
/** Private eXecution Environment (PXE). */
public pxeService: PXE,
/** CheatCodes. */
public cc: CheatCodes,
/** Accounts. */
public accounts: CompleteAddress[],
/** Logger. */
public logger: DebugLogger,

Expand All @@ -122,10 +91,6 @@ export class CrossChainTestHarness {

/** Aztec address to use in tests. */
public ownerAddress: AztecAddress,
/** Another Aztec Address to use in tests. */
public receiver: AztecAddress,
/** The owners public key. */
public ownerPub: PublicKey,
) {}

async generateClaimSecret(): Promise<[Fr, Fr]> {
Expand Down Expand Up @@ -208,9 +173,11 @@ export class CrossChainTestHarness {
await this.addPendingShieldNoteToPXE(amount, secretHash, receipt.txHash);
}

async performL2Transfer(transferAmount: bigint) {
async performL2Transfer(transferAmount: bigint, receiverAddress: AztecAddress) {
// send a transfer tx to force through rollup with the message included
const transferTx = this.l2Token.methods.transfer_public(this.ownerAddress, this.receiver, transferAmount, 0).send();
const transferTx = this.l2Token.methods
.transfer_public(this.ownerAddress, receiverAddress, transferAmount, 0)
.send();
const receipt = await transferTx.wait();
expect(receipt.status).toBe(TxStatus.MINED);
}
Expand Down Expand Up @@ -285,12 +252,11 @@ export class CrossChainTestHarness {

async checkEntryIsNotInOutbox(withdrawAmount: bigint, callerOnL1: EthAddress = EthAddress.ZERO): Promise<Fr> {
this.logger('Ensure that the entry is not in outbox yet');
const contractData = await this.pxeService.getContractData(this.l2Bridge.address);
// 0xb460af94, selector for "withdraw(uint256,address,address)"
const content = sha256ToField(
Buffer.concat([
Buffer.from([0xb4, 0x60, 0xaf, 0x94]),
toBufferBE(withdrawAmount, 32),
new Fr(withdrawAmount).toBuffer(),
this.ethAccount.toBuffer32(),
callerOnL1.toBuffer32(),
]),
Expand All @@ -299,7 +265,7 @@ export class CrossChainTestHarness {
Buffer.concat([
this.l2Bridge.address.toBuffer(),
new Fr(1).toBuffer(), // aztec version
contractData?.portalContractAddress.toBuffer32() ?? Buffer.alloc(32, 0),
this.tokenPortalAddress.toBuffer32() ?? Buffer.alloc(32, 0),
new Fr(this.publicClient.chain.id).toBuffer(), // chain id
content.toBuffer(),
]),
Expand Down Expand Up @@ -356,11 +322,4 @@ export class CrossChainTestHarness {
const unshieldReceipt = await unshieldTx.wait();
expect(unshieldReceipt.status).toBe(TxStatus.MINED);
}

async stop() {
if (this.aztecNode instanceof AztecNodeService) await this.aztecNode?.stop();
if (this.pxeService instanceof PXEService) {
await this.pxeService?.stop();
}
}
}
24 changes: 8 additions & 16 deletions yarn-project/end-to-end/src/uniswap_trade_on_l1_from_l2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Fr } from '@aztec/foundation/fields';
import { DebugLogger } from '@aztec/foundation/log';
import { UniswapPortalAbi, UniswapPortalBytecode } from '@aztec/l1-artifacts';
import { UniswapContract } from '@aztec/noir-contracts/types';
import { AztecNode, PXE, TxStatus } from '@aztec/types';
import { PXE, TxStatus } from '@aztec/types';

import { jest } from '@jest/globals';
import { getContract, parseEther } from 'viem';
Expand Down Expand Up @@ -34,7 +34,6 @@ describe('uniswap_trade_on_l1_from_l2', () => {
const WETH9_ADDRESS: EthAddress = EthAddress.fromString('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2');
const DAI_ADDRESS: EthAddress = EthAddress.fromString('0x6B175474E89094C44Da98b954EedeAC495271d0F');

let aztecNode: AztecNode | undefined;
let pxe: PXE;
let logger: DebugLogger;
let teardown: () => Promise<void>;
Expand Down Expand Up @@ -63,13 +62,11 @@ describe('uniswap_trade_on_l1_from_l2', () => {
beforeAll(async () => {
const {
teardown: teardown_,
aztecNode: aztecNode_,
pxe: pxe_,
deployL1ContractsValues,
accounts,
wallets,
logger: logger_,
cheatCodes,
} = await setup(2, { stateLoad: dumpedState });
walletClient = deployL1ContractsValues.walletClient;
const publicClient = deployL1ContractsValues.publicClient;
Expand All @@ -78,7 +75,6 @@ describe('uniswap_trade_on_l1_from_l2', () => {
throw new Error('This test must be run on a fork of mainnet with the expected fork block');
}

aztecNode = aztecNode_;
pxe = pxe_;
logger = logger_;
teardown = teardown_;
Expand All @@ -90,25 +86,21 @@ describe('uniswap_trade_on_l1_from_l2', () => {

logger('Deploying DAI Portal, initializing and deploying l2 contract...');
daiCrossChainHarness = await CrossChainTestHarness.new(
aztecNode,
pxe,
deployL1ContractsValues,
accounts,
deployL1ContractsValues.publicClient,
deployL1ContractsValues.walletClient,
ownerWallet,
logger,
cheatCodes,
DAI_ADDRESS,
);

logger('Deploying WETH Portal, initializing and deploying l2 contract...');
wethCrossChainHarness = await CrossChainTestHarness.new(
aztecNode,
pxe,
deployL1ContractsValues,
accounts,
deployL1ContractsValues.publicClient,
deployL1ContractsValues.walletClient,
ownerWallet,
logger,
cheatCodes,
WETH9_ADDRESS,
);

Expand Down Expand Up @@ -139,11 +131,11 @@ describe('uniswap_trade_on_l1_from_l2', () => {

afterAll(async () => {
await teardown();
await wethCrossChainHarness.stop();
await daiCrossChainHarness.stop();
// await wethCrossChainHarness.stop();
// await daiCrossChainHarness.stop();
});

it('should uniswap trade on L1 from L2 funds privately (swaps WETH -> DAI)', async () => {
it.only('should uniswap trade on L1 from L2 funds privately (swaps WETH -> DAI)', async () => {
const wethL1BeforeBalance = await wethCrossChainHarness.getL1BalanceOf(ownerEthAddress);

// 1. Approve and deposit weth to the portal and move to L2
Expand Down

0 comments on commit 9be2846

Please sign in to comment.