github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/scripts/generate-l2-genesis.sh (about) 1 #!/usr/bin/env bash 2 3 set -euo pipefail 4 5 # Create a L2 genesis.json suitable for the solidity tests to 6 # ingest using `vm.loadAllocs(string)`. 7 # This script depends on the relative path to the op-node from 8 # contracts-bedrock 9 10 SCRIPTS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null && pwd)" 11 CONTRACTS_DIR="$(realpath "$SCRIPTS_DIR/..")" 12 MONOREPO_BASE="$(realpath "$CONTRACTS_DIR/../..")" 13 14 DEPLOY_ARTIFACT="$CONTRACTS_DIR/deployments/hardhat/.deploy" 15 OP_NODE="$MONOREPO_BASE/op-node/cmd/main.go" 16 L1_STARTING_BLOCK_PATH="$CONTRACTS_DIR/test/mocks/block.json" 17 TESTDATA_DIR="$CONTRACTS_DIR/.testdata" 18 19 OUTFILE_L2="$TESTDATA_DIR/genesis.json" 20 OUTFILE_ROLLUP="$TESTDATA_DIR/rollup.json" 21 22 LOCKDIR="/tmp/lock-generate-l2-genesis" 23 24 cleanup() { 25 rm -rf -- "$LOCKDIR" 26 } 27 28 # Wait for the L2 outfile to be over 8M for up to $2 iterations 29 # of $1 seconds. This is a hack to ensure that the outfile is fully 30 # written before the solidity tests try to read it 31 wait_l2_outfile() { 32 i=1 33 while [ $i -le "$2" ]; do 34 i=$((i + 1)) 35 36 if [ ! -f "$OUTFILE_L2" ]; then 37 sleep "$1" 38 continue 39 fi 40 41 if [ "$(du -m "$OUTFILE_L2" | cut -f1)" -lt 8 ]; then 42 sleep "$1" 43 continue 44 fi 45 46 exit 0 47 done 48 49 echo "L2 genesis file not generated in time. Exiting." 50 exit 1 51 } 52 53 # Directory creations are atomic, so we can use mkdir to 54 # create a lockfile that prevents subsequent invocations 55 # of the script from running concurrently. 56 if mkdir -- "$LOCKDIR" > /dev/null 2>&1; then 57 trap 'cleanup' EXIT 58 59 mkdir -p "$TESTDATA_DIR" 60 61 if [ ! -f "$DEPLOY_ARTIFACT" ]; then 62 forge script "$CONTRACTS_DIR"/scripts/Deploy.s.sol:Deploy > /dev/null 2>&1 63 fi 64 65 if [ ! -f "$OUTFILE_L2" ]; then 66 tempfile=$(mktemp) 67 if ! go run "$OP_NODE" genesis l2 \ 68 --deploy-config "$CONTRACTS_DIR/deploy-config/hardhat.json" \ 69 --l1-deployments "$DEPLOY_ARTIFACT" \ 70 --l1-starting-block "$L1_STARTING_BLOCK_PATH" \ 71 --outfile.l2 "$OUTFILE_L2" \ 72 --outfile.rollup "$OUTFILE_ROLLUP" 2>"$tempfile"; then 73 cat "$tempfile" >&2 74 fi 75 rm "$tempfile" 76 fi 77 else 78 # Wait up to 5 minutes for the lock to be released 79 wait_l2_outfile 0.25 1200 80 fi