-
Notifications
You must be signed in to change notification settings - Fork 1
/
machine_initialization.sh
executable file
·391 lines (317 loc) · 13.1 KB
/
machine_initialization.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/env bash
# This script sets up a new Debian machine to be able to run the bash scripts
# in this repo. Inspiration was taken from here:
# https://hackmd.io/@yisun/BkT0RS87q
# Get the path to the directory this script is in.
INIT_SCRIPT_PATH="$(realpath "${BASH_SOURCE[-1]}")"
INIT_SCRIPT_DIRECTORY="$(dirname "$INIT_SCRIPT_PATH")"
############################################
########### ERROR HANDLING #################
############################################
# Inspiration taken from
# https://unix.stackexchange.com/questions/462156/how-do-i-find-the-line-number-in-bash-when-an-error-occured
# https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html
# Change this in sourcing script to get error messages.
ERR_MSG="UNKNOWN"
set -eE -o functrace
failure() {
local lineno=$2
local fn=$3
local exitstatus=$4
local cmd=$5
local msg=$6
local lineno_fns=${1% 0}
if [[ "$lineno_fns" != "0" ]]; then
lineno="${lineno} ${lineno_fns}"
fi
printf "\n####### ERROR #######\n\n"
echo " Failure in file: ${BASH_SOURCE[1]}"
echo " Function trace [line numbers]: ${fn} [${lineno}]"
echo " Exit status: ${exitstatus}"
echo " Command that failed: $cmd"
echo " Error message: $msg"
}
trap 'failure "${BASH_LINENO[*]}" "$LINENO" "${FUNCNAME[*]:-script}" "$?" "$BASH_COMMAND" "$ERR_MSG"' ERR
############################################
################ DEFAULTS ##################
############################################
DEFAULT_PTAU_SIZE=18
DEFAULT_SWAP_SIZE=400G
############################################
################ CLI FLAGS #################
############################################
# https://stackoverflow.com/questions/7069682/how-to-get-arguments-with-flags-in-bash#21128172
CLOUDWATCH=false
SWAP=false
PTAU=false
REPO=false
VERBOSE=false
print_usage() {
printf "
Setup various software required to used the zk-proof-of-asset repo.
USAGE:
./machine_initialization.sh [FLAGS] [OPTIONS]
DESCRIPTION:
This script does the following:
1. Installs a bunch of software using the APT package manager
2. Installs Rust via their install script
3. Clones the Circom repo, and builds from source
4. Clones the pyenv repo, and installs Python 3.10 & 3.6
5. Clones the node repo, selects a particular commit, and builds from source
6. Installs nvm via their install script, and installs latest npm
7. Clones rapidsnark repo, and builds from source
8. Installs pnpm via their install script
9. Print out the 'export' command that needs to be run manually before trying to run full_workflow.sh
The script can be run multiple times without repeating work e.g. if a command
fails then you can run the script again and it will pick up where it left off.
TROUBLESHOOTING:
If the npm install fails with this error:
> Installing latest LTS version.
> Downloading and installing node v20.11.0...
> Binary download failed, trying source.
Then run this command manually:
export NVM_DIR=\"$HOME/.nvm\" && [ -s \"$NVM_DIR/nvm.sh\" ] && \. \"$NVM_DIR/nvm.sh\" && nvm install --lts.
And restart the process.
If the pnpm install fails with this error:
> ==> Downloading pnpm binaries 8.15.1
> WARN using --force I sure hope you know what you are doing
> Copying pnpm CLI from /tmp/tmp.a13YBtCUZy/pnpm to /root/.local/share/pnpm/pnpm
> ERR_PNPM_UNKNOWN_SHELL Could not infer shell type.
Then run this command manually (with different tmp file):
SHELL=\"$SHELL\" /tmp/tmp.PZoYjFP8NI/pnpm setup --force
And restart the process.
FLAGS:
-c AWS CloudWatch memory metrics
See this for more info https://stackoverflow.com/questions/42317062/how-to-monitor-ec2-instances-by-memory
-P Download ptau file number $DEFAULT_PTAU_SIZE & put in $HOME/zk-proof-of-assets
-S Create a swap file with default size: $DEFAULT_SWAP_SIZE
-v Print commands that are run (set -x)
-h Help
OPTIONS:
-b <BRANCH> Checkout <BRANCH> in $HOME/zk-proof-of-assets
-p <NUM> Download ptau file <NUM> & put in $HOME/zk-proof-of-assets
See all ptau files here https://github.com/iden3/snarkjs?tab=readme-ov-file#7-prepare-phase-2
-r <DIR> Clone zk-proof-of-assets repo into <DIR>
Also install dependencies and apply patches to dependencies
-s <SIZE> Create swap file of size <SIZE> (recommended for large circuits)
"
}
while getopts 'cvhr:Ss:b:Pp:' flag; do
case "${flag}" in
c) CLOUDWATCH=true ;;
s)
SWAP=true
SWAP_SIZE_INPUT="${OPTARG}"
;;
S) SWAP=true ;;
r)
REPO=true
REPO_PARENT_DIR="${OPTARG}"
;;
b) BRANCH="${OPTARG}" ;;
p)
PTAU=true
PTAU_SIZE_INPUT="${OPTARG}"
;;
P) PTAU=true ;;
v) VERBOSE=true ;;
h)
print_usage
exit 1
;;
*)
print_usage
exit 1
;;
esac
done
SWAP_SIZE=${SWAP_SIZE_INPUT:-$DEFAULT_SWAP_SIZE}
PTAU_SIZE=${PTAU_SIZE_INPUT:-$DEFAULT_PTAU_SIZE}
if $VERBOSE; then
# print commands before executing
set -x
fi
############################################
####### APT SOFTWARE INSTALLATION ##########
############################################
ERR_MSG="Initial setup failed"
sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential gcc pkg-config libssl-dev libgmp-dev libsodium-dev nasm nlohmann-json3-dev cmake m4 curl wget git time patch parallel jq bc
# for pyenv
sudo apt install -y --no-install-recommends make zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
############################################
############### CLOUDWATCH #################
############################################
# Setup memory metrics for CloudWatch
# https://stackoverflow.com/questions/42317062/how-to-monitor-ec2-instances-by-memory
if $CLOUDWATCH; then
ERR_MSG="CloudWatch setup failed"
sudo wget https://amazoncloudwatch-agent.s3.amazonaws.com/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
sudo dpkg -i -E ./amazon-cloudwatch-agent.deb
echo '{"metrics":{"metrics_collected":{"mem":{"measurement":["mem_used_percent"],"metrics_collection_interval":30}}}}' | sudo tee -a /opt/aws/amazon-cloudwatch-agent/bin/config.json >/dev/null
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s
fi
############################################
############## SYSTEM CONFIG ###############
############################################
ERR_MSG="System config setup failed"
# Increase kernel memory map per process, and persist after reboot
# https://www.kernel.org/doc/Documentation/sysctl/vm.txt
# https://github.com/iden3/snarkjs/issues/397#issuecomment-1876700914
#
# NOTE this may not work if you are using a Docker container, and will give message (exit code 0)
# `sysctl: setting key "vm.max_map_count", ignoring: Read-only file system`
# https://stackoverflow.com/questions/23537560/docker-build-read-only-file-system
# 6553000 is enough for at least 256 signatures in layer 1 circuit
sudo sysctl -w vm.max_map_count=6553000
if ! grep "vm.max_map_count=6553000" /etc/sysctl.conf; then
sudo sh -c 'echo "vm.max_map_count=6553000" >>/etc/sysctl.conf'
fi
if $SWAP; then
# Increase swap memory, and persist after reboot
sudo fallocate -l $SWAP_SIZE /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# https://serverfault.com/questions/967852/swapfile-mount-etc-fstab-swap-swap-or-none-swap
echo '/swapfile whatever swap sw 0 0' | sudo tee -a /etc/fstab
fi
############################################
##### CUSTOM SOFTWARE INSTALLATION #########
############################################
# Rust
ERR_MSG="Rust setup failed"
cd "$HOME"
if [[ ! -f "$HOME/.cargo/env" ]]; then
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs >>./rustup-init.sh
chmod +x ./rustup-init.sh
./rustup-init.sh -y --no-modify-path
rm -f ./rustup-init.sh
fi
source "$HOME/.cargo/env"
# Circom
ERR_MSG="Circom setup failed"
cd "$HOME"
if [[ ! -d "$HOME/circom" ]]; then
git clone https://github.com/iden3/circom.git
cd circom
# this is circom 2.1.8 (there seems to be problems compiling circuits for later versions)
git checkout 207a712e7830774a66189ef453b77a04cec97995
cargo build --release
cargo install --path circom
fi
# Pyenv
ERR_MSG="Pyenv setup failed"
cd "$HOME"
if [[ ! -d "$HOME/pyenv" ]]; then git clone https://github.com/pyenv/pyenv.git; fi
export PATH="$HOME/pyenv/bin:$PATH"
eval "$(pyenv init -)"
if ! pyenv versions | grep "3\.10\."; then pyenv install 3.10; fi
pyenv global 3.10
if ! pyenv versions | grep "3\.6\."; then pyenv install 3.6; fi
# Patched node
ERR_MSG="Node setup failed"
cd "$HOME"
if [[ ! -f "$HOME/node/out/Release/node" ]]; then
git clone https://github.com/nodejs/node.git
cd node
pyenv local 3.6
git checkout 8beef5eeb82425b13d447b50beafb04ece7f91b1
python configure.py
make -j16
fi
export PATCHED_NODE_PATH=$HOME/node/out/Release/node
# NPM
ERR_MSG="NPM setup failed"
cd "$HOME"
if ! which npm; then
if [[ ! -d "$HOME/.nvm" ]]; then
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
fi
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
printf "
NOTE This sometimes fails with:
> Installing latest LTS version.
> Downloading and installing node v20.11.0...
> Binary download failed, trying source.
In this case run this command manually:
export NVM_DIR=\"\$HOME/.nvm\" && [ -s \"\$NVM_DIR/nvm.sh\" ] && \. \"\$NVM_DIR/nvm.sh\" && nvm install --lts.
\n"
nvm install --lts
fi
# Rapidsnark (x86_64 architecture only)
ERR_MSG="Rapidsnark setup failed"
cd "$HOME"
if [[ ! -f "$HOME/rapidsnark/package/bin/prover" ]]; then
git clone https://github.com/iden3/rapidsnark.git
cd rapidsnark
git submodule init
git submodule update
./build_gmp.sh host
mkdir build_prover && cd build_prover
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package
make -j4 && make install
fi
export RAPIDSNARK_PATH=$HOME/rapidsnark/package/bin/prover
# PNPM
ERR_MSG="PNPM setup failed"
cd "$HOME"
if [[ ! -f "$HOME/.local/share/pnpm/pnpm" ]]; then
printf "
NOTE This sometimes fails with:
> ==> Downloading pnpm binaries 8.15.1
> WARN using --force I sure hope you know what you are doing
> Copying pnpm CLI from /tmp/tmp.a13YBtCUZy/pnpm to /root/.local/share/pnpm/pnpm
> ERR_PNPM_UNKNOWN_SHELL Could not infer shell type.
In this case just run this manually (with different tmp file):
SHELL=\"\$SHELL\" /tmp/tmp.PZoYjFP8NI/pnpm setup --force
\n"
curl -fsSL https://get.pnpm.io/install.sh | sh -
fi
export PNPM_HOME="$HOME/.local/share/pnpm"
export PATH="$PNPM_HOME:$PATH"
pnpm add npx -g
pnpm add snarkjs -g
# Setup repo.
if $REPO; then
ERR_MSG="Repo setup failed"
REPO_DIR="$REPO_PARENT_DIR/zk-proof-of-assets"
if [[ ! -d "$REPO_DIR" ]]; then
cd "$REPO_PARENT_DIR"
git clone https://github.com/silversixpence-crypto/zk-proof-of-assets
fi
cd "$REPO_DIR"
if [[ ! -z "$BRANCH" ]]; then
git switch -c "$BRANCH" origin/"$BRANCH"
fi
pnpm i
git submodule init
git submodule update
# Workaround for a problem in the circom compiler
# https://github.com/iden3/circom/issues/230
BATCH_ECDSA_DIR="$REPO_DIR/git_modules/batch-ecdsa"
patch -u "$BATCH_ECDSA_DIR/circuits/batch_ecdsa.circom" -i ./batch-ecdsa.patch
# Naming conflict resolution between ed25519-circom & circom-pairing
ED25519_CIRCOM_DIR="$REPO_DIR/git_modules/ed25519-circom"
cd "$ED25519_CIRCOM_DIR"
git apply "$REPO_DIR"/ed25519-circom.patch
cd -
pip install -r requirements.txt
cargo build --release
fi
# TODO instead of checking if the file exists rather check its checksum,
# because the download might have only gotten partway
if $PTAU; then
if [[ $PTAU_SIZE -gt 27 ]]; then
ptau_url=https://storage.googleapis.com/zkevm/ptau/powersOfTau28_hez_final.ptau
else
ptau_url=https://storage.googleapis.com/zkevm/ptau/powersOfTau28_hez_final_"$PTAU_SIZE".ptau
fi
wget "$ptau_url"
fi
# This is so that the user can set these env vars in their shell.
# This is the 2nd method mentioned here:
# https://stackoverflow.com/questions/16618071/can-i-export-a-variable-to-the-environment-from-a-bash-script-without-sourcing-i/16619261#16619261
# The 1st method in the above tends to break things, so try to avoid that.
echo "export SNARKJS_PATH=\$HOME/snarkjs/cli.js && export RAPIDSNARK_PATH=\$HOME/rapidsnark/package/bin/prover && export PATCHED_NODE_PATH=\$HOME/node/out/Release/node && export PATH=\$PATH:\$HOME/pyenv/bin && eval \"\$(pyenv init -)\" && export PNPM_HOME=\$HOME/.local/share/pnpm && export PATH=\$PNPM_HOME:\$PATH && export NVM_DIR=\$HOME/.nvm && [ -s \$NVM_DIR/nvm.sh ] && \. \$NVM_DIR/nvm.sh && source $HOME/.cargo/env"