-
Notifications
You must be signed in to change notification settings - Fork 9
/
run.sh
executable file
·224 lines (190 loc) · 7.16 KB
/
run.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
#!/bin/bash
set -uex
# assume already install: libgmp-dev nasm nlohmann-json3-dev snarkit plonkit
source ./common.sh
source ./envs/small
export VERBOSE=false
export RUST_BACKTRACE=full
if [[ -v DIRTY ]] && [[ ! -v FORCE ]] ; then
echo -e "\033[31mDirty workspace, run stop.sh or set env FORCE to continue.\033[0m"
exit 1
fi
export DIRTY=true
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)"
STATE_MNGR_DIR=$DIR/rollup-state-manager
CIRCUITS_DIR=$DIR/circuits
BLOCKSCOUT_DIR=$DIR/blockscout
TARGET_CIRCUIT_DIR=$CIRCUITS_DIR/testdata/Block_$NTXS"_"$BALANCELEVELS"_"$ORDERLEVELS"_"$ACCOUNTLEVELS
PROVER_DIR=$DIR/prover-cluster
EXCHANGE_DIR=$DIR/dingir-exchange
FAUCET_DIR=$DIR/regnbue-bridge
CONTRACTS_DIR=$DIR/contracts
ORCHESTRA_DIR=$DIR/orchestra
ROLLUP_DB="postgres://rollup:[email protected]:5433/rollup"
CURRENTDATE=$(date +"%Y-%m-%d")
MNEMONIC="anxiety else floor soap tent sight belt leave top velvet meadow walk intact spice polar"
[[ -v ENVSUB ]] || ENVSUB=envsub
function handle_submodule() {
git submodule update --init --recursive
if [ -z ${CI+x} ]; then git pull --recurse-submodules; fi
}
function prepare_circuit() {
rm -rf $TARGET_CIRCUIT_DIR
#cd $STATE_MNGR_DIR
#cargo run --bin gen_export_circuit_testcase
mkdir -p $TARGET_CIRCUIT_DIR
CIRCUITS_DIR=$CIRCUITS_DIR $ENVSUB > $TARGET_CIRCUIT_DIR/circuit.circom << EOF
include "${CIRCUITS_DIR}/src/block.circom";
component main { public [oldRoot, newRoot, txDataHashHi, txDataHashLo]}= Block(${NTXS}, ${BALANCELEVELS}, ${ORDERLEVELS}, ${ACCOUNTLEVELS});
EOF
echo 'circuit source:'
cat $TARGET_CIRCUIT_DIR/circuit.circom
cd $CIRCUITS_DIR
npm i
# TODO: detect and install snarkit
# if you encounter issues on compile, try using following cmd line instead:
# see https://github.com/fluidex/snarkit/issues/14
# snarkit compile $TARGET_CIRCUIT_DIR --backend=auto 2>&1 | tee /tmp/snarkit.log
snarkit2 compile $TARGET_CIRCUIT_DIR --verbose --backend=auto 2>&1 | tee /tmp/snarkit.log
plonkit setup --power 20 --srs_monomial_form $TARGET_CIRCUIT_DIR/mon.key
# plonkit dump-lagrange -c $TARGET_CIRCUIT_DIR/circuit.r1cs --srs_monomial_form $TARGET_CIRCUIT_DIR/mon.key --srs_lagrange_form $TARGET_CIRCUIT_DIR/lag.key
plonkit export-verification-key -c $TARGET_CIRCUIT_DIR/circuit.r1cs --srs_monomial_form $TARGET_CIRCUIT_DIR/mon.key -v $TARGET_CIRCUIT_DIR/vk.bin
}
function prepare_contracts() {
rm -f $CONTRACTS_DIR/contracts/Verifier.sol
plonkit generate-verifier -v $TARGET_CIRCUIT_DIR/vk.bin -s $CONTRACTS_DIR/contracts/Verifier.sol
cd $CONTRACTS_DIR/
# git update-index --assume-unchanged $CONTRACTS_DIR/contracts/Verifier.sol
yarn install
npx hardhat compile
}
function config_prover_cluster() {
cd $PROVER_DIR
PORT=50055 DB=$ROLLUP_DB WITGEN_INTERVAL=2500 N_WORKERS=10 TARGET_CIRCUIT_DIR=$TARGET_CIRCUIT_DIR N_TXS=$NTXS $ENVSUB < $PROVER_DIR/config/coordinator.yaml.template > $PROVER_DIR/config/coordinator.yaml
TARGET_CIRCUIT_DIR=$TARGET_CIRCUIT_DIR N_TXS=$NTXS $ENVSUB < $PROVER_DIR/config/client.yaml.template > $PROVER_DIR/config/client.yaml
}
# TODO: send different tasks to different tmux windows
function start_docker_compose() {
dir=$1
name=$2
docker-compose --file $dir/docker/docker-compose.yaml --project-name $name up --force-recreate --detach
}
function run_core_docker_compose() {
start_docker_compose $ORCHESTRA_DIR orchestra
start_docker_compose $FAUCET_DIR faucet
sleep 10
}
function run_matchengine() {
cd $EXCHANGE_DIR
make startall
#cargo build --bin matchengine
#nohup $EXCHANGE_DIR/target/debug/matchengine >> $EXCHANGE_DIR/matchengine.$CURRENTDATE.log 2>&1 &
}
function run_ticker() {
cd $EXCHANGE_DIR/examples/js/
npm i
nohup npx ts-node tick.ts >> $EXCHANGE_DIR/tick.$CURRENTDATE.log 2>&1 &
}
function run_rollup() {
cd $STATE_MNGR_DIR
mkdir -p circuits/testdata/persist
cargo build --release --bin rollup_state_manager
nohup $STATE_MNGR_DIR/target/release/rollup_state_manager >> $STATE_MNGR_DIR/rollup_state_manager.$CURRENTDATE.log 2>&1 &
}
function run_prove_master() {
# run coordinator because we need to init db
cd $PROVER_DIR
cargo build --release
nohup $PROVER_DIR/target/release/coordinator >> $PROVER_DIR/coordinator.$CURRENTDATE.log 2>&1 &
}
function run_prove_workers() {
cd $PROVER_DIR # need to switch into PROVER_DIR to use .env
if [ ! -f $PROVER_DIR/target/release/client ]; then
cargo build --release
fi
if [[ ! -z ${NO_LOCAL_WORKER+x} ]]; then
return
fi
if [ $OS = "Darwin" ]; then
(nice -n 20 nohup $PROVER_DIR/target/release/client >> $PROVER_DIR/client.$CURRENTDATE.log 2>&1 &)
else
nohup $PROVER_DIR/target/release/client >> $PROVER_DIR/client.$CURRENTDATE.log 2>&1 &
sleep 1
cpulimit -P $PROVER_DIR/target/release/client -l $((50 * $(nproc))) -b -z # -q
fi
}
function run_eth_node() {
# a mainnet like 50 Gwei gas price
# base on 21,000 units limit from mainnet (21,000 units * 50 Gwei)
cd $CONTRACTS_DIR
yarn install
GANACHE_CLI_ARG="--host 0.0.0.0 \
--networkId 53371 \
--chainId 53371 \
--blockTime 15 \
--db $CONTRACTS_DIR/ganache \
--gasPrice 50000000000 \
--gasLimit 1050000000000000 \
--allowUnlimitedContractSize \
--accounts 20 \
--defaultBalanceEther 1000 \
--deterministic \
--mnemonic=$MNEMONIC"
if [ $VERBOSE_GANACHE == 'TRUE' ]; then
GANACHE_CLI_ARG=$GANACHE_CLI_ARG" --verbose"
fi
nohup npx ganache-cli $GANACHE_CLI_ARG >> $CONTRACTS_DIR/ganache.$CURRENTDATE.log 2>&1 &
sleep 1
}
function deploy_contracts() {
cd $CONTRACTS_DIR
export GENESIS_ROOT=$(cat $STATE_MNGR_DIR/rollup_state_manager.$CURRENTDATE.log | grep "genesis root" | tail -n1 | awk '{print $9}' | sed 's/Fr(//' | sed 's/)//')
export CONTRACT_ADDR=$(retry_cmd_until_ok npx hardhat run scripts/deploy.js --network localhost | grep "FluiDex deployed to:" | awk '{print $4}')
echo "export CONTRACT_ADDR=$CONTRACT_ADDR" > $CONTRACTS_DIR/contract-deployed.env
}
function restore_contracts() {
source $CONTRACTS_DIR/contract-deployed.env
}
function run_faucet() {
cd $FAUCET_DIR
cargo build --release --bin faucet
nohup "$FAUCET_DIR/target/release/faucet" >> $FAUCET_DIR/faucet.$CURRENTDATE.log 2>&1 &
}
# TODO: need to fix task_fetcher, gitignore, comfig template & example, contracts...
function run_block_submitter() {
cd $FAUCET_DIR
cargo build --release --bin block_submitter
DB=$ROLLUP_DB CONTRACTS_DIR=$CONTRACTS_DIR CONTRACT_ADDR=$CONTRACT_ADDR $ENVSUB < $FAUCET_DIR/config/block_submitter.yaml.template > $FAUCET_DIR/config/block_submitter.yaml
nohup "$FAUCET_DIR/target/release/block_submitter" >> $FAUCET_DIR/block_submitter.$CURRENTDATE.log 2>&1 &
}
function run_bin() {
run_matchengine
run_prove_master
run_prove_workers
run_rollup
sleep 10
run_eth_node
if [ $DX_CLEAN == 'TRUE' ]; then
deploy_contracts
else
restore_contracts
fi
run_faucet
run_block_submitter
}
function setup() {
handle_submodule
prepare_circuit
prepare_contracts
}
function run_all() {
config_prover_cluster
run_core_docker_compose
run_bin
run_ticker
start_docker_compose $BLOCKSCOUT_DIR blockscout # blockscout depends on eth_node, so we run it later
}
if [[ -z ${AS_RESOURCE+x} ]]; then
setup
run_all
fi