forked from casper-network/casper-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
joiner-dev-tmux.sh
executable file
·226 lines (183 loc) · 5.53 KB
/
joiner-dev-tmux.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
#!/usr/bin/env bash
# -*- mode: sh; fill-column: 80; sh-basic-offset: 4; -*-
# This file is formatted with `shfmt -i 4 -ci`
# and linted with shellcheck
set -o errexit
set -o nounset
set -o pipefail
tmux_new_window() {
local SESSION=${1}
local ID=${2}
local CMD=${3}
local NAME="${SESSION}-${ID}"
tmux new-window -t "${SESSION}:${ID}" -n "${NAME}"
tmux send-keys -t "${NAME}" "${CMD}" C-m
}
build_system_contracts() {
local CMD=(
"make -s"
"build-contracts-rs"
"CARGO_FLAGS=--quiet"
)
echo "Building system contracts..."
${CMD[*]}
}
build_node() {
local CMD=(
"cargo build"
"--quiet"
"--manifest-path=node/Cargo.toml"
)
echo "Building node..."
${CMD[*]}
}
build_casper_client() {
local CMD=(
"cargo build"
"--quiet"
"-p casper-client"
)
echo "Building casper-client..."
${CMD[*]}
}
generate_timestamp() {
local DELAY=${1}
local SCRIPT=(
"from datetime import datetime, timedelta;"
"print((datetime.utcnow() + timedelta(seconds=${DELAY})).isoformat('T') + 'Z')"
)
python3 -c "${SCRIPT[*]}"
}
generate_chainspec() {
local BASEDIR=${1}
local TIMESTAMP=${2}
local SOURCE="${BASEDIR}/resources/joiner/chainspec.toml.in"
local TARGET="${BASEDIR}/resources/joiner/chainspec.toml"
export BASEDIR
export TIMESTAMP
echo "Generating chainspec..."
envsubst <"${SOURCE}" >"${TARGET}"
}
run_node() {
local EXECUTABLE=${1}
local SESSION=${2}
local ID=${3}
local CONFIG_DIR=${4}
local DATA_DIR=${5}
local BOOTSTRAP_RPC_PORT=${6}
local CONFIG_TOML_PATH="${CONFIG_DIR}/config.toml"
local SECRET_KEY_PATH="${CONFIG_DIR}/secret_keys/node-${ID}.pem"
local STORAGE_DIR="${DATA_DIR}/node-${ID}-storage"
local CMD=(
"${EXECUTABLE}"
"validator"
"${CONFIG_TOML_PATH}"
"-C consensus.secret_key_path=${SECRET_KEY_PATH}"
"-C storage.path=${STORAGE_DIR}"
"-C rpc_server.address='0.0.0.0:${BOOTSTRAP_RPC_PORT}'"
"1> >(tee ${DATA_DIR}/node-${ID}.log) 2> >(tee ${DATA_DIR}/node-${ID}.log.stderr)"
)
mkdir -p "${STORAGE_DIR}"
tmux_new_window "${SESSION}" "${ID}" "${CMD[*]}"
echo "Booting node ${ID}..."
}
run_joiner_node() {
local EXECUTABLE=${1}
local SESSION=${2}
local ID=${3}
local CONFIG_DIR=${4}
local DATA_DIR=${5}
local BOOTSTRAP_RPC_PORT=${6}
local RPC_URL="http://127.0.0.1:${BOOTSTRAP_RPC_PORT}"
local GET_LATEST_BLOCK_HASH_CMD=(
"cargo"
"run"
"--quiet"
"-p" "casper-client"
"--"
"get-block"
"--node-address=${RPC_URL}"
"|"
"jq"
".result.block.hash"
)
local GET_TRUSTED_HASH=(
"TRUSTED_HASH='null';"
"while [[ \${TRUSTED_HASH} == 'null' ]] ; do"
"TRUSTED_HASH=\$(${GET_LATEST_BLOCK_HASH_CMD[*]});"
"echo \${TRUSTED_HASH};"
"sleep 5;"
"done"
"1> >(tee ${DATA_DIR}/node-${ID}.log) 2> >(tee ${DATA_DIR}/node-${ID}.log.stderr)"
)
local CONFIG_TOML_PATH="${CONFIG_DIR}/config.toml"
local SECRET_KEY_PATH="${CONFIG_DIR}/secret_keys/node-${ID}.pem"
local STORAGE_DIR="${DATA_DIR}/node-${ID}-storage"
local RUN_JOINER=(
"${EXECUTABLE}"
"validator"
"${CONFIG_TOML_PATH}"
"-C consensus.secret_key_path=${SECRET_KEY_PATH}"
"-C storage.path=${STORAGE_DIR}"
"-C network.bind_address='0.0.0.0:0'"
"-C rpc_server.address='0.0.0.0:0'"
"-C rest_server.address='0.0.0.0:0'"
"-C event_stream_server.address='0.0.0.0:0'"
"-C node.trusted_hash=\"\${TRUSTED_HASH}\""
"1> >(tee -a ${DATA_DIR}/node-${ID}.log) 2> >(tee -a ${DATA_DIR}/node-${ID}.log.stderr)"
)
local CMD=(
"${GET_TRUSTED_HASH[*]}"
"&&"
"${RUN_JOINER[*]}"
)
tmux_new_window "${SESSION}" "${ID}" "${CMD[*]}"
echo "Launching joiner node ${ID}..."
}
check_for_bootstrap() {
local BOOTSTRAP_PORT=34553
while ! (: </dev/tcp/0.0.0.0/${BOOTSTRAP_PORT}) &>/dev/null; do
sleep 1
done
}
main() {
local BASEDIR
BASEDIR="$(readlink -f "$(dirname "${0}")")"
local CONFIG_DIR="${BASEDIR}/resources/joiner"
local EXECUTABLE="${BASEDIR}/target/debug/casper-node"
local DELAY=${1:-40}
local TIMESTAMP
TIMESTAMP="$(generate_timestamp "${DELAY}")"
local BOOTSTRAP_RPC_PORT="${BOOTSTRAP_RPC_PORT:-50101}"
export RUST_LOG="${RUST_LOG:-debug}"
export TMPDIR="${TMPDIR:-$(mktemp -d)}"
# create a new tmux session
# if one already exists this will fail and the program will exit
local SESSION="${SESSION:-local}"
tmux new-session -d -s "${SESSION}"
if [[ ! -x "${EXECUTABLE}" || ! -v QUICK_START ]]; then
build_system_contracts
build_node
build_casper_client
fi
generate_chainspec "${BASEDIR}" "${TIMESTAMP}"
local ID=1
run_node "${EXECUTABLE}" "${SESSION}" "${ID}" "${CONFIG_DIR}" "${TMPDIR}" "${BOOTSTRAP_RPC_PORT}"
check_for_bootstrap
local ID=2
run_joiner_node "${EXECUTABLE}" "${SESSION}" "${ID}" "${CONFIG_DIR}" "${TMPDIR}" "${BOOTSTRAP_RPC_PORT}"
echo
echo "DELAY : ${DELAY}"
echo "RPC : http://127.0.0.1:${BOOTSTRAP_RPC_PORT}"
echo "RUST_LOG : ${RUST_LOG}"
echo "TIMESTAMP : ${TIMESTAMP}"
echo "TMPDIR : ${TMPDIR}"
echo
echo "To view: "
echo " tmux attach-session -t ${SESSION}"
echo
echo "To kill: "
echo " tmux kill-session -t ${SESSION}"
echo
}
main "${@}"