From 9be2846b8e31d5fa35e34af7103aca48891d21f1 Mon Sep 17 00:00:00 2001
From: Rahul Kothari <rahul.kothari.201@gmail.com>
Date: Wed, 11 Oct 2023 11:21:56 +0000
Subject: [PATCH] alter e2e crosschain harness to be the same as canary

---
 .../src/e2e_cross_chain_messaging.test.ts     | 20 +---
 .../e2e_public_cross_chain_messaging.test.ts  | 20 +---
 .../e2e_public_to_private_messaging.test.ts   | 18 +---
 .../src/fixtures/cross_chain_test_harness.ts  | 93 ++++++-------------
 .../src/uniswap_trade_on_l1_from_l2.test.ts   | 24 ++---
 5 files changed, 45 insertions(+), 130 deletions(-)

diff --git a/yarn-project/end-to-end/src/e2e_cross_chain_messaging.test.ts b/yarn-project/end-to-end/src/e2e_cross_chain_messaging.test.ts
index 1f2b8209d661..55a2e988ef01 100644
--- a/yarn-project/end-to-end/src/e2e_cross_chain_messaging.test.ts
+++ b/yarn-project/end-to-end/src/e2e_cross_chain_messaging.test.ts
@@ -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;
@@ -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 () => {
@@ -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(
diff --git a/yarn-project/end-to-end/src/e2e_public_cross_chain_messaging.test.ts b/yarn-project/end-to-end/src/e2e_public_cross_chain_messaging.test.ts
index 6d77f9826e71..329dc5b026d9 100644
--- a/yarn-project/end-to-end/src/e2e_public_cross_chain_messaging.test.ts
+++ b/yarn-project/end-to-end/src/e2e_public_cross_chain_messaging.test.ts
@@ -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;
@@ -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 () => {
@@ -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
diff --git a/yarn-project/end-to-end/src/e2e_public_to_private_messaging.test.ts b/yarn-project/end-to-end/src/e2e_public_to_private_messaging.test.ts
index 3514744c0a7f..df236df5ec82 100644
--- a/yarn-project/end-to-end/src/e2e_public_to_private_messaging.test.ts
+++ b/yarn-project/end-to-end/src/e2e_public_to_private_messaging.test.ts
@@ -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;
@@ -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 () => {
diff --git a/yarn-project/end-to-end/src/fixtures/cross_chain_test_harness.ts b/yarn-project/end-to-end/src/fixtures/cross_chain_test_harness.ts
index 6e9c6077b419..b546c42196d3 100644
--- a/yarn-project/end-to-end/src/fixtures/cross_chain_test_harness.ts
+++ b/yarn-project/end-to-end/src/fixtures/cross_chain_test_harness.ts
@@ -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';
 
@@ -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,
@@ -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,
 
@@ -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]> {
@@ -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);
   }
@@ -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(),
       ]),
@@ -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(),
       ]),
@@ -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();
-    }
-  }
 }
diff --git a/yarn-project/end-to-end/src/uniswap_trade_on_l1_from_l2.test.ts b/yarn-project/end-to-end/src/uniswap_trade_on_l1_from_l2.test.ts
index bc084357caf1..ec49e257ad06 100644
--- a/yarn-project/end-to-end/src/uniswap_trade_on_l1_from_l2.test.ts
+++ b/yarn-project/end-to-end/src/uniswap_trade_on_l1_from_l2.test.ts
@@ -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';
@@ -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>;
@@ -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;
@@ -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_;
@@ -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,
     );
 
@@ -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