forked from Lilypad-Tech/lilypad-modicum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack
executable file
·461 lines (409 loc) · 11.7 KB
/
stack
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
declare -ga GLOBAL_ARGS
GLOBAL_ARGS=( "$@" )
export DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export DOCKER_REGISTRY=${DOCKER_REGISTRY:="quay.io/lilypad"}
export GCP_ZONE=${GCP_ZONE:="us-central1-a"}
export GCP_NODE_NAME=${GCP_NODE_NAME:="bravo-testnet-vm-0"}
# this means -d or -i --rm
# bash sucks sometimes because we can't just pass these as a single string
# server or cli
export DEFAULT_DATA_DIR="/tmp/geth"
export CONTAINER_MODE=${CONTAINER_MODE:="server"}
export DATA_DIRECTORY=${DATA_DIRECTORY:="$DEFAULT_DATA_DIR"}
export IMAGE_BASE=${IMAGE_BASE:="lilypad"}
export IMAGE_HARDHAT=${IMAGE_HARDHAT:="${IMAGE_BASE}-hardhat"}
export DEPLOYMENT=${DEPLOYMENT:="localgeth"}
export RPC_URL=${RPC_URL:="http://geth:8545"}
export VERSION=${VERSION:="$(cd $DIR; git rev-parse --short HEAD)-$(uname -m |sed 's/arm64/aarch64/')"}
export IMAGE_MODICUM_NAME=${IMAGE_MODICUM_NAME:="${IMAGE_BASE}-modicum"}
export IMAGE_RESOURCE_PROVIDER_NAME=${IMAGE_RESOURCE_PROVIDER_NAME:="${IMAGE_BASE}-resource-provider"}
export IMAGE_MEDIATOR_NAME=${IMAGE_MEDIATOR_NAME:="${IMAGE_BASE}-mediator"}
export IMAGE_MODICUM="${DOCKER_REGISTRY}/${IMAGE_MODICUM_NAME}:${VERSION}"
export IMAGE_RESOURCE_PROVIDER="${DOCKER_REGISTRY}/${IMAGE_RESOURCE_PROVIDER_NAME}:${VERSION}"
export IMAGE_MEDIATOR="${DOCKER_REGISTRY}/${IMAGE_MEDIATOR_NAME}:${VERSION}"
export ADDRESS_ADMIN=${ADDRESS_ADMIN:=""}
export QUICK_RESET=${QUICK_RESET:=""}
export LILYPAD_NODE_FLAGS=""
function gSSH() {
gcloud compute ssh --quiet --zone=$GCP_ZONE $GCP_NODE_NAME -- sudo $*
}
function gSCP() {
gcloud compute scp --quiet --zone=$GCP_ZONE $1 $GCP_NODE_NAME:$2
}
function upload-stack() {
gSCP ./stack ./stack
}
function build-modicum() {
compile-contract
docker build -t $IMAGE_MODICUM --target modicum .
}
function build-resource-provider() {
compile-contract
docker build -t $IMAGE_RESOURCE_PROVIDER --target resource-provider .
}
function build-mediator() {
compile-contract
docker build -t $IMAGE_MEDIATOR --target mediator .
}
function build-hardhat() {
compile-contract
docker build -t $IMAGE_HARDHAT src/js
}
function version() {
echo $VERSION
}
function build() {
build-modicum
build-resource-provider
build-mediator
build-hardhat
}
function push-images() {
docker push $IMAGE_MODICUM
docker push $IMAGE_RESOURCE_PROVIDER
docker push $IMAGE_MEDIATOR
}
function contract-address() {
cat "src/js/deployments/${DEPLOYMENT}/Modicum.json" | jq -r .address
}
function compile-contract() {
source ${DIR}/.env
(
set -euo pipefail
cd src/js
npm install
npx hardhat compile
)
}
# TODO: should be called contract-tests?
function unit-tests() {
source ${DIR}/.env
(
set -euo pipefail
cd src/js
npx hardhat test --network hardhat
)
}
function integration-tests() {
source ${DIR}/.env
( cd test ; go test -timeout 120m )
}
function deploy-contract() {
source ${DIR}/.env
(cd src/js && npx hardhat deploy --network ${DEPLOYMENT})
}
function geth-proxy() {
source ${DIR}/.env
docker run -d --restart always \
--name geth-proxy \
--net host \
-e "NOXY_PORT=8545" \
-e "NOXY_GETH_FRONT=/" \
-e "NOXY_GETH_HOST=localhost " \
-e "NOXY_GETH_PORT=8546" \
-e "NOXY_GETH_BACK=/" \
-e "NOXY_GETH_WS=1" \
quay.io/lilypad/noxy:v0.0.1
}
function geth() {
source ${DIR}/.env
docker network ls | grep modicum || docker network create modicum
docker run -d --restart always \
--name geth \
--network modicum \
-p 8546:8545 \
-v ${DATA_DIRECTORY}:/data/geth \
ethereum/client-go \
--datadir /data/geth \
--dev \
--http \
--http.api web3,eth,net \
--http.addr 0.0.0.0 \
--http.corsdomain '*' \
--http.port 8545 \
--http.vhosts '*'
}
function faucet-proxy() {
source ${DIR}/.env
docker run -d --restart always \
--name faucet-proxy \
--net host \
-e "NOXY_PORT=80" \
-e "NOXY_FAUCET_FRONT=/" \
-e "NOXY_FAUCET_HOST=localhost " \
-e "NOXY_FAUCET_PORT=81" \
-e "NOXY_FAUCET_BACK=/" \
-e "NOXY_FAUCET_WS=1" \
quay.io/lilypad/noxy:v0.0.1
}
function faucet() {
source ${DIR}/.env
local privateKey=$(echo -n "$PRIVATE_KEY_FAUCET" | sed 's/^0x//')
docker run -d \
--name faucet \
-p 81:8080 \
--network modicum \
-e WEB3_PROVIDER=http://geth:8545 \
-e PRIVATE_KEY=$privateKey \
chainflag/eth-faucet:1.1.0 \
--faucet.amount=100 \
--faucet.minutes=1
}
function hardhat() {
source ${DIR}/.env
docker network ls | grep modicum || docker network create modicum
docker run -d --restart always \
--name geth \
--network modicum \
-p 8545:8545 \
-v $DIR/src/js/deployments:/app/deployments \
-v $DIR/src/js/contracts:/app/contracts \
-v $DIR/src/js/scripts:/app/scripts \
$IMAGE_HARDHAT npx hardhat node --hostname 0.0.0.0 --port 8545
}
function geth-command() {
docker exec -i geth geth --exec "$@" attach /data/geth/geth.ipc
}
function geth-cli() {
docker exec -i geth geth attach /data/geth/geth.ipc
}
function geth-script() {
source ${DIR}/.env
(
set -euo pipefail
cd src/js
npx hardhat --network $DEPLOYMENT run "$@"
)
}
function balances() {
source ${DIR}/.env
geth-script scripts/show-balances.js
}
# move ALL the money apart from 1 eth to the given account
function fund-admin() {
source ${DIR}/.env
if [[ -z "$ADDRESS_ADMIN" ]]; then
echo >&2 "ADDRESS_ADMIN must be set (source ${DIR}/.env)"
exit 1
fi
geth-command "eth.sendTransaction({from: eth.coinbase, to: \"${ADDRESS_ADMIN}\", value: new web3.BigNumber(eth.getBalance(eth.coinbase)).minus(web3.toWei(1, \"ether\")) })"
}
function fund-faucet() {
source ${DIR}/.env
sleep 5
geth-script scripts/fund-faucet.js
}
function fund-services() {
source ${DIR}/.env
geth-script scripts/fund-services.js
}
function fund-throwaway() {
source ${DIR}/.env
geth-script scripts/fund-throwaway.js
}
# this means run ipfs, bacalhau and the modicum resource provider
# in the same service - it is what nodes will run on their hardware
function lilypad-node() {
source ${DIR}/.env
if [[ -z "$LILYPAD_NODE_IMAGE" ]]; then
echo >&2 "LILYPAD_NODE_IMAGE must be set"
exit 1
fi
if [[ -z "$LILYPAD_NODE_NAME" ]]; then
echo >&2 "LILYPAD_NODE_NAME must be set"
exit 1
fi
if [[ -z "$RPC_URL" ]]; then
echo >&2 "RPC_URL must be set"
exit 1
fi
local LILYPAD_NODE_FLAGS="$LILYPAD_NODE_FLAGS --network modicum"
# this is for dev trim
if [[ "$DATA_DIRECTORY" == "$DEFAULT_DATA_DIR" ]]; then
LILYPAD_NODE_FLAGS="$LILYPAD_NODE_FLAGS -v $DIR/src/python:/app"
# Workaround https://github.com/bacalhau-project/lilypad/issues/39
docker run -i --entrypoint /bin/bash -v $DIR/src/python:/app \
$LILYPAD_NODE_IMAGE -c "pip install -e ."
else
echo >&2 "running in production mode..."
fi
if [[ "$CONTAINER_MODE" == "server" ]]; then
LILYPAD_NODE_FLAGS="$LILYPAD_NODE_FLAGS -d --restart always"
elif [[ "$CONTAINER_MODE" == "cli" ]]; then
LILYPAD_NODE_FLAGS="$LILYPAD_NODE_FLAGS -i --rm"
fi
GPU_ARG=""
GPU_VALUE=""
if [ -f "/usr/bin/nvidia-smi" ]; then
GPU_ARG="--gpus"
GPU_VALUE="all"
fi
eval docker run $LILYPAD_NODE_FLAGS --name $LILYPAD_NODE_NAME \
-v /tmp:/tmp \
-v /var/run/docker.sock:/var/run/docker.sock \
$GPU_ARG $GPU_VALUE \
-e DEBUG \
-e PRIVATE_KEY \
-e BAD_ACTOR \
-e MEDIATION_CHANCE_PERCENT \
-e MEDIATOR_ADDRESSES \
-e RPC_URL \
-e RPC_TOKEN \
-e CONTRACT_ADDRESS=$(contract-address) \
-e CONTRACT_ABI_FILE=/Modicum.json \
$LILYPAD_NODE_IMAGE \
${1:-""} \
$(for arg in ${GLOBAL_ARGS[@]:1}; do echo ${arg@Q}; done)
}
function logs() {
# trap "sleep 1; reset" EXIT
service="$1"
if [[ "$service" == "geth" ]]; then
docker logs -f geth
fi
if [[ "$service" == "solver" ]]; then
docker logs -f solver
fi
# docker exec -i $service supervisorctl tail -f bacalhau-serve stdout &
# docker exec -i $service supervisorctl tail -f bacalhau-serve stderr &
# docker exec -i $service supervisorctl tail -f ipfs stdout &
# docker exec -i $service supervisorctl tail -f ipfs stderr &
docker exec -i $service supervisorctl tail -f $service stdout &
docker exec -i $service supervisorctl tail -f $service stderr
}
function resource-provider() {
source ${DIR}/.env
LILYPAD_NODE_IMAGE=$IMAGE_RESOURCE_PROVIDER \
LILYPAD_NODE_NAME=resource-provider \
CONTAINER_MODE=server \
MEDIATOR_ADDRESSES=$ADDRESS_MEDIATOR \
PRIVATE_KEY=$PRIVATE_KEY_RESOURCE_PROVIDER \
lilypad-node "$@"
}
function mediator() {
source ${DIR}/.env
LILYPAD_NODE_IMAGE=$IMAGE_MEDIATOR \
LILYPAD_NODE_NAME=mediator \
CONTAINER_MODE=server \
PRIVATE_KEY=$PRIVATE_KEY_MEDIATOR \
lilypad-node "$@"
}
function solver() {
source ${DIR}/.env
LILYPAD_NODE_IMAGE=$IMAGE_MODICUM \
LILYPAD_NODE_NAME=solver \
CONTAINER_MODE=server \
PRIVATE_KEY=$PRIVATE_KEY_SOLVER \
lilypad-node "runAsSolver"
}
function submitjob() {
source ${DIR}/.env
LILYPAD_NODE_IMAGE=$IMAGE_MODICUM \
LILYPAD_NODE_NAME=submitjob \
CONTAINER_MODE=cli \
MEDIATOR_ADDRESSES=$ADDRESS_MEDIATOR \
PRIVATE_KEY=$PRIVATE_KEY_JOB_CREATOR \
lilypad-node "runLilypadCLI"
}
function reset() {
set -x
docker rm -f faucet-proxy faucet resource-provider mediator solver geth-proxy geth ||true
sudo rm -rf $DATA_DIRECTORY
local sleepSeconds="1"
if [[ -z "$QUICK_RESET" ]]; then
(cd src/js && npm install)
sleepSeconds="5"
fi
node src/js/scripts/create-new-accounts.js > .env
source ${DIR}/.env
rm -rf src/js/deployments/localgeth
(
set -euo pipefail
cd src/js
npx hardhat compile
)
build
geth
geth-proxy
sleep $sleepSeconds
fund-admin
fund-faucet
fund-services
balances
(cd src/js && npx hardhat deploy --network localgeth)
}
# it's IMPORTANT to stop geth gracefully so it commit's it's state to disk
# https://github.com/ethereum/go-ethereum/issues/20383
function stop() {
docker stop geth || true
sleep 5
# Don't kill other containers that someone else started in production, e.g.
# datadog logging agent
docker rm -f faucet-proxy faucet resource-provider mediator solver geth-proxy geth
}
function start() {
source ${DIR}/.env
local sleepSeconds="1"
if [[ -z "$QUICK_RESET" ]]; then
build
sleepSeconds="5"
fi
geth || true
geth-proxy ||true
echo "Waiting for geth to start"
sleep $sleepSeconds
solver
sleep $sleepSeconds
mediator
resource-provider
faucet
faucet-proxy
echo "Now run (in separate terminals):"
echo "./stack logs geth"
echo "./stack logs solver"
echo "./stack logs mediator"
echo "./stack logs resource-provider"
}
function clean() {
sudo rm -rf "$DATA_DIRECTORY"
rm -rf "$DIR/src/js/deployments/$DEPLOYMENT"
}
function stop-modicum() {
docker rm -f solver || true
docker rm -f mediator || true
docker rm -f resource-provider || true
docker rm -f submitjob || true
}
function generate-golang-bindings() {
local package="$1"
local outputDir="$2"
# check if package is set and exit if not
if [[ -z "$package" ]]; then
echo "package is not set"
exit 1
fi
# check if outputDir exists and exit if not
if [[ ! -d "$outputDir" ]]; then
echo "outputDir $outputDir does not exist"
exit 1
fi
# check if the jq command exists and exit if not
if ! command -v jq &> /dev/null; then
echo "jq command could not be found"
exit 1
fi
(
set -euo pipefail
cd src/js
# a quick workaround for not actually needing the env
# because all we are doing is re-compiling
export ADDRESS_ADMIN=xxx
npx hardhat compile
cat ./artifacts/contracts/Modicum.sol/Modicum.json | jq -r '.abi' > /tmp/Modicum.abi
abigen --abi /tmp/Modicum.abi --pkg "$package" --type Modicum --out "$outputDir/Modicum.go"
)
}
eval "$@"