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

feat: AWS mainnet fork #2986

Merged
merged 7 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
13 changes: 13 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,17 @@ jobs:
name: "Build and test"
command: build aztec-node | add_timestamps

mainnet-fork:
machine:
image: ubuntu-2204:2023.07.2
resource_class: large
steps:
- *checkout
- *setup_env
- run:
name: "Build"
command: build mainnet-fork | add_timestamps

aztec-faucet:
machine:
image: ubuntu-2204:2023.07.2
Expand Down Expand Up @@ -1222,6 +1233,8 @@ workflows:
- l1-contracts: *defaults
- noir-contracts-build: *defaults

- mainnet-fork: *defaults

# Yarn Project
- yarn-project-base:
requires:
Expand Down
4 changes: 4 additions & 0 deletions build_manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ p2p-bootstrap:
dependencies:
- yarn-project

mainnet-fork:
buildDir: iac/mainnet-fork
projectDir: iac/mainnet-fork

docs:
buildDir: .
dockerfile: docs/Dockerfile
Expand Down
18 changes: 18 additions & 0 deletions iac/mainnet-fork/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM ubuntu:focal

# Install nginx
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
RUN apt-get update && apt install -y git curl nginx

# Copy nginx config
COPY . .
COPY nginx/gateway.conf /etc/nginx/gateway.conf
COPY nginx/nginx.conf /etc/nginx/nginx.conf

# Install foundry
RUN ./scripts/install_foundry.sh
ENV PATH="./foundry/bin:${PATH}"

# Run anvil and nginx
EXPOSE 80
ENTRYPOINT ["sh", "-c", "./scripts/run_nginx_anvil.sh"]
14 changes: 14 additions & 0 deletions iac/mainnet-fork/nginx/gateway.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
server {
listen 80 default_server;
listen 8545;

location = /{{API_KEY}} {
proxy_pass http://0.0.0.0:8544;
rewrite ^/{{API_KEY}}(.*) /$1 break;
}

# Error responses
error_page 404 = @400; # Treat invalid paths as bad requests
proxy_intercept_errors on; # Do not send backend errors to client
default_type application/json; # If no content-type, assume JSON
}
53 changes: 53 additions & 0 deletions iac/mainnet-fork/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;


include /etc/nginx/gateway.conf;
include /etc/nginx/conf.d/*.conf;
}

21 changes: 21 additions & 0 deletions iac/mainnet-fork/scripts/install_foundry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh
set -eu

export FOUNDRY_DIR="$PWD/.foundry"
FOUNDRY_BIN_DIR="$FOUNDRY_DIR/bin"
BIN_URL="https://raw.githubusercontent.com/foundry-rs/foundry/master/foundryup/foundryup"
BIN_PATH="$FOUNDRY_BIN_DIR/foundryup"
FOUNDRY_MAN_DIR="$FOUNDRY_DIR/share/man/man1"

# Clean
rm -rf $FOUNDRY_DIR

# Install foundryup.
mkdir -p $FOUNDRY_BIN_DIR
mkdir -p $FOUNDRY_MAN_DIR
curl -# -L $BIN_URL -o $BIN_PATH
chmod +x $BIN_PATH
export PATH=$FOUNDRY_BIN_DIR:$PATH

# Use version.
foundryup
28 changes: 28 additions & 0 deletions iac/mainnet-fork/scripts/run_nginx_anvil.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

set -eum pipefail

# Replace API_KEY in nginx config
echo "Replacing api key with $API_KEY in nginx config..."
sed -i 's/{{API_KEY}}/'$API_KEY'/' /etc/nginx/gateway.conf

# Run nginx and anvil alongside each other
trap 'kill $(jobs -p)' SIGTERM

# Anvil defaults - Nginx assumes these values to be as they are
HOST="0.0.0.0"
PORT=8544
ETHEREUM_HOST=$HOST:$PORT

# Data directory for anvil state
mkdir -p /data

# Run anvil silently
.foundry/bin/anvil --silent --host $HOST -p $PORT -m "$MNEMONIC" -f=https://mainnet.infura.io/v3/$INFURA_API_KEY --chain-id=$CHAIN_ID --fork-block-number=15918000 --block-base-fee-per-gas=10 -s=$SNAPSHOT_FREQUENCY --state=./data/state --balance=1000000000000000000 >/dev/null &

echo "Waiting for ethereum host at $ETHEREUM_HOST..."
while ! curl -s $ETHEREUM_HOST >/dev/null; do sleep 1; done

echo "Starting nginx..."
nginx &
wait
15 changes: 15 additions & 0 deletions iac/mainnet-fork/scripts/wait_for_fork
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e

# When destroying and applying mainnet fork terraform, it may not be
# ready for a while, as it must register with DNS etc.
# This script waits on a healthy status from the fork - a valid response to the chainid request
# We retry every 20 seconds, and wait for a total of 5 minutes (15 times)

export ETHEREUM_HOST="https://aztec-mainnet-fork.aztec.network:8545/$FORK_API_KEY"

curl -H "Content-Type: application/json" -X POST --data '{"method":"eth_chainId","params":[],"id":33,"jsonrpc":"2.0"}' \
--connect-timeout 30 \
--retry 15 \
--retry-delay 20 \
$ETHEREUM_HOST
Loading