11#! /usr/bin/env bash
22
33# This scripts builds compatible and current branch with nix
4- # It handles two cases differently:
5- # - When given an $1 argument, it treats itself as being run in
6- # Buildkite CI and $1 to be "fork" branch that needs to be built
7- # - When it isn't given any arguments, it asusmes it is being
8- # executed locally and builds code in $PWD as the fork branch
9- #
10- # When run locally, `compatible` branch is built in a temporary folder
11- # (and fetched clean from Mina's repository). When run in CI,
12- # `compatible` branch of git repo in $PWD is used to being the
13- # compatible executable.
14- #
15- # In either case at the end of its execution this script leaves
16- # current dir at the fork branch (in case of local run, it never
17- # switches the branch with git) and nix builds put to `compatible-devnet`
18- # and `fork-devnet` symlinks (located in $PWD).
19-
20- set -exo pipefail
4+ # 0. Prepare environment if needed
5+ # 1. Build compatible as a prefork build;
6+ # 2. Build current branch(not compatible) as a postfork build;
7+ # 3. Build hardfork_test on current branch;
8+ # 4. Upload to nix cache
9+ # 5. Execute hardfork_test on them.
10+
11+ # Step 0. Prepare environment if needed
12+ set -eux -o pipefail
2113
2214NIX_OPTS=( --accept-flake-config --experimental-features ' nix-command flakes' )
2315
24- if [[ " $ NIX_CACHE_NAR_SECRET" != " " ]]; then
16+ if [[ -n " ${ NIX_CACHE_NAR_SECRET:- } " ]]; then
2517 echo " $NIX_CACHE_NAR_SECRET " > /tmp/nix-cache-secret
2618 echo " Configuring the NAR signing secret"
2719 NIX_SECRET_KEY=/tmp/nix-cache-secret
2820fi
2921
30- if [[ " $ NIX_CACHE_GCP_ID" != " " ]] && [[ " $ NIX_CACHE_GCP_SECRET" != " " ]]; then
22+ if [[ -n " ${ NIX_CACHE_GCP_ID:- } " ]] && [[ -n " ${ NIX_CACHE_GCP_SECRET:- } " ]]; then
3123 echo " GCP uploading configured (for nix binaries)"
3224 cat << 'EOF '> /tmp/nix-post-build
3325#!/bin/sh
4234 NIX_POST_BUILD_HOOK=/tmp/nix-post-build
4335fi
4436
45- if [[ " $ NIX_POST_BUILD_HOOK" != " " ]]; then
37+ if [[ -n " ${ NIX_POST_BUILD_HOOK:- } " ]]; then
4638 NIX_OPTS+=( --post-build-hook " $NIX_POST_BUILD_HOOK " )
4739fi
48- if [[ " $ NIX_SECRET_KEY" != " " ]]; then
40+ if [[ -n " ${ NIX_SECRET_KEY:- } " ]]; then
4941 NIX_OPTS+=( --secret-key-files " $NIX_SECRET_KEY " )
5042fi
5143
52- INIT_DIR= " $PWD "
44+ pushd " $( git rev-parse --show-toplevel ) "
5345SCRIPT_DIR=$( cd -- " $( dirname -- " ${BASH_SOURCE[0]} " ) " & > /dev/null && pwd )
5446
55- if [[ $# -gt 0 ]]; then
56- # Branch is specified, this is a CI run
57- chown -R " ${USER} " /workdir
58- git config --global --add safe.directory /workdir
59- git fetch
47+ if [ -n " ${BUILDKITE:- } " ]; then
48+ # This is a CI run, ensure nix docker has everything what we want.
6049 nix-env -iA unstable.{curl,git-lfs,gnused,jq,python311}
6150
6251 python -m venv .venv
6352 source .venv/bin/activate
64- pip install -r $INIT_DIR / scripts/mina-local-network/requirements.txt
53+ pip install -r scripts/mina-local-network/requirements.txt
6554
6655 # Manually patch zone infos, nix doesn't provide stdenv breaking janestreet's core
6756 zone_info=(/nix/store/* tzdata* /share/zoneinfo)
@@ -78,60 +67,51 @@ if [[ $# -gt 0 ]]; then
7867 fi
7968fi
8069
81- if [[ ! -L compatible-devnet ]]; then
82- if [[ $# == 0 ]]; then
83- compatible_build=$( mktemp --tmpdir -d mina-compatible-worktree.XXXXXXXXXX)
84- git fetch origin compatible
85- git worktree add " $compatible_build " origin/compatible
86- cd " $compatible_build "
87- else
88- git checkout -f $1
89- git checkout -f compatible
90- git checkout -f $1 -- scripts/hardfork
91- compatible_build=" $INIT_DIR "
92- fi
93- git submodule sync --recursive
94- git submodule update --init --recursive --depth 1
95- nix " ${NIX_OPTS[@]} " build " $compatible_build ?submodules=1#devnet" --out-link " $INIT_DIR /compatible-devnet"
96- if [[ $# == 0 ]]; then
97- cd -
98- rm -Rf " $compatible_build "
99- fi
100- fi
70+ # 1. Build compatible as a prefork build;
71+ git fetch origin compatible
72+ git checkout origin/compatible
73+ git submodule sync --recursive
74+ git submodule update --init --recursive --depth 1
75+ nix " ${NIX_OPTS[@]} " build " $PWD ?submodules=1#devnet" --out-link " prefork-devnet"
76+
77+ # 2. Build current branch(not compatible) as a postfork build;
78+ git checkout -
79+ git submodule sync --recursive
80+ git submodule update --init --recursive --depth 1
81+ nix " ${NIX_OPTS[@]} " build " $PWD ?submodules=1#devnet" --out-link " postfork-devnet"
10182
102- if [[ $# -gt 0 ]]; then
103- # Branch is specified, this is a CI run
104- git checkout -f $1
105- git submodule sync --recursive
106- git submodule update --init --recursive --depth 1
107- fi
108- nix " ${NIX_OPTS[@]} " build " $INIT_DIR ?submodules=1#devnet" --out-link " $INIT_DIR /fork-devnet"
10983
110- if [[ " $NIX_CACHE_GCP_ID " != " " ]] && [[ " $NIX_CACHE_GCP_SECRET " != " " ]]; then
84+ # 3. Build hardfork_test on current branch;
85+ nix " ${NIX_OPTS[@]} " build " $PWD ?submodules=1#hardfork_test" --out-link " hardfork_test"
86+
87+ # 4. Upload to nix cache
88+
89+ if [[ -n " ${NIX_CACHE_GCP_ID:- } " ]] && [[ -n " ${NIX_CACHE_GCP_SECRET:- } " ]]; then
11190 mkdir -p $HOME /.aws
11291 cat << EOF> $HOME /.aws/credentials
11392[default]
11493aws_access_key_id=$NIX_CACHE_GCP_ID
11594aws_secret_access_key=$NIX_CACHE_GCP_SECRET
11695EOF
11796
118- nix --experimental-features nix-command copy --to "s3://mina-nix-cache?endpoint=https://storage.googleapis.com" --stdin </tmp/nix-paths
97+ nix "${NIX_OPTS[@]} " copy \
98+ --to "s3://mina-nix-cache?endpoint=https://storage.googleapis.com" \
99+ --stdin </tmp/nix-paths
119100fi
120101
121-
122- nix "${NIX_OPTS[@]} " build "$INIT_DIR ?submodules=1#hardfork_test" --out-link "$INIT_DIR /hardfork_test"
102+ # 5. Execute hardfork_test on them.
123103
124104SLOT_TX_END=${SLOT_TX_END:- $((40))}
125105SLOT_CHAIN_END=${SLOT_CHAIN_END:- $((SLOT_TX_END+5))}
126106
127- echo "Running HF test with SLOT_TX_END=$SLOT_TX_END "
128-
129- "$INIT_DIR /hardfork_test/bin/hardfork_test" \
130- --main-mina-exe "$INIT_DIR /compatible-devnet/bin/mina" \
131- --main-runtime-genesis-ledger "$INIT_DIR /compatible-devnet/bin/runtime_genesis_ledger" \
132- --fork-mina-exe "$INIT_DIR /fork-devnet/bin/mina" \
133- --fork-runtime-genesis-ledger "$INIT_DIR /fork-devnet/bin/runtime_genesis_ledger" \
107+ hardfork_test/bin/hardfork_test \
108+ --main-mina-exe prefork-devnet/bin/mina \
109+ --main-runtime-genesis-ledger prefork-devnet/bin/runtime_genesis_ledger \
110+ --fork-mina-exe postfork-devnet/bin/mina \
111+ --fork-runtime-genesis-ledger postfork-devnet/bin/runtime_genesis_ledger \
134112 --slot-tx-end "$SLOT_TX_END " \
135113 --slot-chain-end "$SLOT_CHAIN_END " \
136114 --script-dir "$SCRIPT_DIR " \
137115&& echo "HF test completed successfully"
116+
117+ popd
0 commit comments