Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Used dumped state instead of fork #1399

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -822,18 +822,18 @@ jobs:
command: ./scripts/cond_run_script end-to-end $JOB_NAME ./scripts/run_tests_local e2e_pending_commitments_contract.test.ts
working_directory: yarn-project/end-to-end

# TODO(AD): Reenable after #1367
# uniswap-trade-on-l1-from-l2:
# machine:
# image: ubuntu-2004:202010-01
# resource_class: large
# steps:
# - *checkout
# - *setup_env
# - run:
# name: "Test"
# command: ./scripts/cond_run_script end-to-end $JOB_NAME ./scripts/run_tests_local uniswap_trade_on_l1_from_l2.test.ts
# working_directory: yarn-project/end-to-end

uniswap-trade-on-l1-from-l2:
machine:
image: ubuntu-2004:202010-01
resource_class: large
steps:
- *checkout
- *setup_env
- run:
name: "Test"
command: ./scripts/cond_run_script end-to-end $JOB_NAME ./scripts/run_tests_local uniswap_trade_on_l1_from_l2.test.ts
working_directory: yarn-project/end-to-end

integration-archiver-l1-to-l2:
machine:
Expand Down Expand Up @@ -1185,8 +1185,7 @@ workflows:
- e2e-account-contracts: *e2e_test
- e2e-escrow-contract: *e2e_test
- e2e-pending-commitments-contract: *e2e_test
# TODO(AD): Reenable after #1367
# - uniswap-trade-on-l1-from-l2: *e2e_test
- uniswap-trade-on-l1-from-l2: *e2e_test
- integration-l1-publisher: *e2e_test
- integration-archiver-l1-to-l2: *e2e_test
- e2e-p2p: *e2e_test
Expand All @@ -1211,8 +1210,7 @@ workflows:
- e2e-account-contracts
- e2e-escrow-contract
- e2e-pending-commitments-contract
# TODO(AD): Reenable after #1367
# - uniswap-trade-on-l1-from-l2
- uniswap-trade-on-l1-from-l2
- integration-l1-publisher
- integration-archiver-l1-to-l2
- e2e-p2p
Expand Down
5 changes: 0 additions & 5 deletions yarn-project/end-to-end/scripts/run_tests
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ set -e
export TEST=$1
export COMPOSE_FILE=${2:-docker-compose.yml}

if [ "$TEST" = "uniswap_trade_on_l1_from_l2.test.ts" ]; then
export FORK_URL=https://mainnet.infura.io/v3/9928b52099854248b3a096be07a6b23c
export FORK_BLOCK_NUMBER=17514288
fi

if [ -n "$COMMIT_HASH" ]; then
$(aws ecr get-login --region us-east-2 --no-include-email) 2> /dev/null

Expand Down
5 changes: 0 additions & 5 deletions yarn-project/end-to-end/scripts/run_tests_local
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ set -e
export TEST=$1
export COMPOSE_FILE=${2:-./scripts/docker-compose.yml}

if [ "$TEST" = "uniswap_trade_on_l1_from_l2.test.ts" ]; then
export FORK_URL=https://mainnet.infura.io/v3/9928b52099854248b3a096be07a6b23c
export FORK_BLOCK_NUMBER=17514288
fi

if [ -n "$COMMIT_HASH" ]; then
aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin 278380418400.dkr.ecr.us-east-2.amazonaws.com

Expand Down
29 changes: 27 additions & 2 deletions yarn-project/end-to-end/src/cheat_codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { pedersenPlookupCommitInputs } from '@aztec/circuits.js/barretenberg';
import { createDebugLogger } from '@aztec/foundation/log';
import { AztecRPC } from '@aztec/types';

import fs from 'fs';

const toFr = (value: Fr | bigint): Fr => {
return typeof value === 'bigint' ? new Fr(value) : value;
};
Expand Down Expand Up @@ -32,7 +34,7 @@ export class CheatCodes {
/**
* A class that provides utility functions for interacting with the L1 chain.
*/
class L1CheatCodes {
export class L1CheatCodes {
constructor(
/**
* The RPC client to use for interacting with the chain
Expand Down Expand Up @@ -102,14 +104,37 @@ class L1CheatCodes {
this.logger(`Set next block timestamp to ${timestamp}`);
}

/**
* Dumps the current chain state to a file.
* @param fileName - The file name to dump state into
*/
public async dumpChainState(fileName: string): Promise<void> {
const res = await this.rpcCall('anvil_dumpState', []);
if (res.error) throw new Error(`Error dumping state: ${res.error.message}`);
const jsonContent = JSON.stringify(res.result);
fs.writeFileSync(`${fileName}.json`, jsonContent, 'utf8');
this.logger(`Dumped state to ${fileName}`);
}

/**
* Loads the chain state from a file.
* @param fileName - The file name to load state from
*/
public async loadChainState(fileName: string): Promise<void> {
const data = JSON.parse(fs.readFileSync(`${fileName}.json`, 'utf8'));
const res = await this.rpcCall('anvil_loadState', [data]);
if (res.error) throw new Error(`Error loading state: ${res.error.message}`);
this.logger(`Loaded state from ${fileName}`);
}

// Good basis for the remaining functions:
// https://github.com/foundry-rs/foundry/blob/master/anvil/core/src/eth/mod.rs
}

/**
* A class that provides utility functions for interacting with the L2 chain.
*/
class L2CheatCodes {
export class L2CheatCodes {
constructor(
/**
* The RPC client to use for interacting with the chain
Expand Down
1 change: 1 addition & 0 deletions yarn-project/end-to-end/src/dumps/uniswap_state.json

Large diffs are not rendered by default.

19 changes: 13 additions & 6 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 @@ -14,16 +14,20 @@ import { CheatCodes } from './cheat_codes.js';
import { CrossChainTestHarness } from './cross_chain/test_harness.js';
import { delay, deployAndInitializeNonNativeL2TokenContracts, setup } from './utils.js';

// PSA: this works on a fork of mainnet but with the default anvil chain id. Start it with the command:
// PSA: This tests works on forked mainnet. There is a dump of the data in `dumpedState` such that we
// don't need to burn through RPC requests.
// To generate a new dump, use the `dumpChainState` cheatcode.
// To start an actual fork, use the command:
// anvil --fork-url https://mainnet.infura.io/v3/9928b52099854248b3a096be07a6b23c --fork-block-number 17514288 --chain-id 31337
// For CI, this is configured in `run_tests.sh` and `docker-compose.yml`

const dumpedState = 'src/dumps/uniswap_state';
const EXPECTED_FORKED_BLOCK = 0; //17514288;
Copy link
Contributor

@rahul-kothari rahul-kothari Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - add a comment on how dumped state doesn't change block number but it works nevertheless

// We tell the archiver to only sync from this block.
process.env.SEARCH_START_BLOCK = EXPECTED_FORKED_BLOCK.toString();

// Should mint WETH on L2, swap to DAI using L1 Uniswap and mint this DAI back on L2
describe('uniswap_trade_on_l1_from_l2', () => {
// test runs on a forked version of mainnet at this block.
const EXPECTED_FORKED_BLOCK = 17514288;
// We tell the archiver to only sync from this block.
process.env.SEARCH_START_BLOCK = EXPECTED_FORKED_BLOCK.toString();
const WETH9_ADDRESS: EthAddress = EthAddress.fromString('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2');
const DAI_ADDRESS: EthAddress = EthAddress.fromString('0x6B175474E89094C44Da98b954EedeAC495271d0F');

Expand All @@ -49,7 +53,10 @@ describe('uniswap_trade_on_l1_from_l2', () => {

beforeEach(async () => {
let deployL1ContractsValues: DeployL1Contracts;
({ aztecNode, aztecRpcServer, deployL1ContractsValues, accounts, logger, wallet, cheatCodes } = await setup(2));
({ aztecNode, aztecRpcServer, deployL1ContractsValues, accounts, logger, wallet, cheatCodes } = await setup(
2,
dumpedState,
));

const walletClient = deployL1ContractsValues.walletClient;
const publicClient = deployL1ContractsValues.publicClient;
Expand Down
13 changes: 11 additions & 2 deletions yarn-project/end-to-end/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
} from 'viem';
import { mnemonicToAccount } from 'viem/accounts';

import { CheatCodes } from './cheat_codes.js';
import { CheatCodes, L1CheatCodes } from './cheat_codes.js';
import { MNEMONIC, localAnvil } from './fixtures.js';

const { SANDBOX_URL = '' } = process.env;
Expand Down Expand Up @@ -275,7 +275,10 @@ export async function setupAztecRPCServer(
* Sets up the environment for the end-to-end tests.
* @param numberOfAccounts - The number of new accounts to be created once the RPC server is initiated.
*/
export async function setup(numberOfAccounts = 1): Promise<{
export async function setup(
numberOfAccounts = 1,
stateLoad: string | undefined = undefined,
): Promise<{
/**
* The Aztec Node service.
*/
Expand Down Expand Up @@ -310,6 +313,12 @@ export async function setup(numberOfAccounts = 1): Promise<{
cheatCodes: CheatCodes;
}> {
const config = getConfigEnvVars();

if (stateLoad) {
const l1CheatCodes = new L1CheatCodes(config.rpcUrl);
await l1CheatCodes.loadChainState(stateLoad);
}

const logger = getLogger();
const hdAccount = mnemonicToAccount(MNEMONIC);

Expand Down