-
Notifications
You must be signed in to change notification settings - Fork 1
/
launchmevcommit
executable file
·369 lines (331 loc) · 10.5 KB
/
launchmevcommit
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
#!/usr/bin/env bash
# Default binary path and root path for mev-commit.
ROOT_PATH="${HOME}/.mev-commit"
BINARY_PATH="${ROOT_PATH}/mev-commit"
# Default mev-commit configuration values.
NODE_TYPE="bidder"
DOMAIN="testnet.mev-commit.xyz"
RPC_URL="wss://chainrpc-wss.${DOMAIN}"
FAUCET_URL="https://faucet.${DOMAIN}"
BOOTNODE="/dnsaddr/bootnode.${DOMAIN}"
CONTRACTS_URL="https://contracts.${DOMAIN}"
OTEL_COLLECTOR_ENDPOINT_URL=""
SERVICE_NAME=""
NAT_ADDR=""
NAT_PORT=""
AUTO_DEPOSIT_AMOUNT="300000000000"
# Default mev-commit binary download values.
ARTIFACTS_URL="https://github.com/primev/mev-commit/releases/latest/download/"
VERSION="__VERSION__"
if [ "${VERSION}" == "__VERSION__" ]; then
TAG="$(curl -sIL -o /dev/null -w %{url_effective} https://github.com/primev/mev-commit/releases/latest | sed 's:.*/::')"
VERSION="${TAG:1}"
FAUCET_URL=""
fi
# Rewrites the default values if the target machine IP is provided.
# This is useful when running the script in dev environments.
TARGET_MACHINE_IP="__TARGET_MACHINE_IP__"
if [ "${TARGET_MACHINE_IP}" != "__TARGET_MACHINE_IP__" ]; then
RPC_URL="ws://${TARGET_MACHINE_IP}:8546"
FAUCET_URL="http://${TARGET_MACHINE_IP}"
CONTRACTS_URL="http://${TARGET_MACHINE_IP}:1010/contracts.json"
TOPOLOGY="$(curl -skL https://${TARGET_MACHINE_IP}:13523/v1/debug/topology)"
if [ -z "${TOPOLOGY}" ]; then
echo "Failed to fetch topology from ${TARGET_MACHINE_IP}"
exit 1
fi
UNDERLAY="$(echo "${TOPOLOGY}" | jq -r '.topology.self.Underlay')"
if [ "${UNDERLAY}" == "null" ]; then
echo "Failed to fetch underlay from ${TARGET_MACHINE_IP}"
exit 1
fi
BOOTNODE="/ip4/${TARGET_MACHINE_IP}/tcp/13522/p2p/${UNDERLAY}"
ARTIFACTS_URL="http://${TARGET_MACHINE_IP}:1111"
fi
# Prints usage information for the script.
usage() {
echo "Usage: $0 --node-type [bidder|provider] [--nat-ip ip_address[:port]] [--otel-collector-endpoint-url url] [--nickname nickname] [--auto-deposit-amount amount]"
exit 1
}
# Logs an informational message in blue.
log_info() {
local message="${1}"
echo -e "\033[34m[\033[32m*\033[34m]\033[34m ${message} \033[0m"
}
# Logs a warning message in yellow.
log_warn() {
local message="${1}"
echo -e "\033[31m[\033[34m#\033[31m]\033[33m ${message} \033[0m"
}
# Logs an error message in red.
log_error() {
local message="${1}"
echo -e "\033[31m[\033[34m!\033[31m]\033[31m ${message} \033[0m"
}
# Checks if required utilities are installed.
check_utils() {
local missing_utils=()
local required_utilities=(
jq
sed
curl
)
for util in "${required_utilities[@]}"; do
if [ ! -x "$(command -v ${util})" ]; then
missing_utils+=("${util}")
fi
done
if [ ${#missing_utils[@]} -ne 0 ]; then
log_error "The following utilities are required and not installed: ${missing_utils[*]}"
exit 1
fi
}
# Downloads and installs the mev-commit binary.
install_mev_commit() {
local file="mev-commit_${VERSION}_"
case "$(uname -s):$(uname -m)" in
Linux:x86_64)
file+="Linux_x86_64.tar.gz"
;;
Linux:arm64)
file+="Linux_arm64.tar.gz"
;;
Darwin:x86_64)
file+="Darwin_x86_64.tar.gz"
;;
Darwin:arm64)
file+="Darwin_arm64.tar.gz"
;;
WindowsNT:x86_64)
file+="Windows_x86_64.zip"
;;
*)
log_error "Unsupported system ($(uname -a))"
exit 1
;;
esac
if [ ! -f "${ROOT_PATH}/${file}" ]; then
log_info "Installing mev-commit..."
http_status=$(curl -sL -w "%{http_code}" "${ARTIFACTS_URL}/${file}" -o "${ROOT_PATH}/${file}")
if [ "${http_status}" -ne 200 ]; then
log_error "Failed to download file from ${ARTIFACTS_URL}/${file}, status code: ${http_status}"
rm -f "${ROOT_PATH}/${file}"
exit 1
fi
tar -xzf "${ROOT_PATH}/${file}" -C "${ROOT_PATH}"
if [ $? -ne 0 ]; then
log_error "Failed to extract mev-commit binary"
exit 1
fi
else
log_info "Binary is already installed"
fi
}
# Waits until the specified URL is reachable or times out.
wait_until_reachable() {
local url="${1}"
local timeout=60
local start_time="$(date +%s)"
while true; do
if [ "$(curl -s -o /dev/null -w "%{http_code}" ${url})" -eq 200 ]; then
return 0
fi
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
if [ "${elapsed_time}" -ge "${timeout}" ]; then
log_error "Timeout reached, ${url} did not return status 200."
return 1
fi
sleep 1
done
}
# Parses command-line arguments.
parse_args() {
while [[ "$#" -gt 0 ]]; do
case ${1} in
--node-type)
NODE_TYPE="${2}"
case ${NODE_TYPE} in
bidder|provider)
# Valid node type, do nothing.
;;
*)
log_error "Invalid node type: ${NODE_TYPE}, must be 'bidder' or 'provider'"
usage
;;
esac
shift
;;
--nat-ip)
NAT_IP="${2}"
if [[ ! ${NAT_IP} =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}(:[0-9]{1,5})?$ ]]; then
log_error "Invalid NAT IP address: ${NAT_IP}"
usage
fi
if [[ "${NAT_IP}" == *":"* ]]; then
NAT_ADDR="${NAT_IP%%:*}"
NAT_PORT="${NAT_IP##*:}"
else
NAT_ADDR="${NAT_IP}"
NAT_PORT=""
fi
IFS='.' read -r -a octets <<< "${NAT_ADDR}"
for octet in "${octets[@]}"; do
if ! [[ "${octet}" =~ ^[0-9]+$ ]] || (( octet < 0 || octet > 255 )); then
log_error "Invalid NAT IP address: ${NAT_IP}"
usage
fi
done
if [ -n "${NAT_PORT}" ]; then
if ! [[ "${NAT_PORT}" =~ ^[0-9]+$ ]] || (( NAT_PORT < 1 || NAT_PORT > 65535 )); then
log_error "Invalid NAT port: ${NAT_PORT}, must be a number between 1 and 65535"
usage
fi
fi
shift
;;
--otel-collector-endpoint-url)
OTEL_COLLECTOR_ENDPOINT_URL="${2}"
shift
;;
--nickname)
SERVICE_NAME="${2}"
shift
;;
--auto-deposit-amount)
AUTO_DEPOSIT_AMOUNT="${2}"
if ! [[ "${AUTO_DEPOSIT_AMOUNT}" =~ ^[0-9]+$ ]]; then
log_error "Invalid auto deposit amount: ${AUTO_DEPOSIT_AMOUNT}, must be a positive number"
usage
fi
shift
;;
*)
log_error "Unknown parameter passed: ${1}"
usage
;;
esac
shift
done
}
# Cleans up by killing the specified process ID.
# The function is idempotent and only kills the process once.
cleanup() {
local pid="${1}"
local grace_period=5 # Set grace period in seconds
if [ -z "${cleaned_up}" ]; then
cleaned_up=true
log_info "Killing mev-commit node with PID ${pid}"
log_info "Waiting for ${grace_period} seconds before force killing..."
sleep ${grace_period}
if kill -0 ${pid} 2>/dev/null; then
log_warn "Process ${pid} still running, force killing now."
kill -9 ${pid}
fi
if wait ${pid}; then
log_info "mev-commit node with PID ${pid} has been killed"
else
log_error "Failed to kill mev-commit node with PID ${pid}"
fi
fi
exit 0
}
main() {
check_utils
parse_args "$@"
log_info "Starting mev-commit setup script..."
log_info "Version of mev-commit: ${VERSION}"
log_info "Type of node: ${NODE_TYPE}"
mkdir -p "${ROOT_PATH}"
if [ ! -O ${ROOT_PATH} ] && [ -z "${MEV_COMMIT_INSECURE_EXEC}" ]; then
log_error "Temp path ${ROOT_PATH} is not owned by current user"
log_error "Run again with MEV_COMMIT_INSECURE_EXEC=1 to ignore this warning"
exit 1
fi
install_mev_commit
if [ ! -x "$(command -v forge)" ] && [ ! -d "${HOME}/.foundry/bin" ]; then
log_info "Installing foundry..."
curl -s -L https://foundry.paradigm.xyz | bash
exec "${HOME}/.foundry/bin/foundryup"
fi
local contracts_json=$(curl -sL ${CONTRACTS_URL})
if ! echo "${contracts_json}" | jq . > /dev/null 2>&1; then
log_error "Failed to fetch contracts from ${CONTRACTS_URL}"
exit 1
fi
export MEV_COMMIT_BLOCK_TRACKER_ADDR="$(echo ${contracts_json} | jq -r '.BlockTracker')"
export MEV_COMMIT_BIDDER_REGISTRY_ADDR="$(echo ${contracts_json} | jq -r '.BidderRegistry')"
export MEV_COMMIT_PROVIDER_REGISTRY_ADDR="$(echo ${contracts_json} | jq -r '.ProviderRegistry')"
export MEV_COMMIT_PRECONF_ADDR="$(echo ${contracts_json} | jq -r '.PreconfManager')"
chmod +x ${BINARY_PATH}
log_info "Initializing mev-commit..."
local flags=(
--settlement-ws-rpc-endpoint "${RPC_URL}"
--peer-type "${NODE_TYPE}"
--bootnodes "${BOOTNODE}"
)
if [ -n "${NAT_ADDR}" ]; then
flags+=(--nat-addr "${NAT_ADDR}")
if [ -n "${NAT_PORT}" ]; then
flags+=(--nat-port "${NAT_PORT}")
fi
fi
if [ -n "${OTEL_COLLECTOR_ENDPOINT_URL}" ]; then
flags+=(--otel-collector-endpoint-url "${OTEL_COLLECTOR_ENDPOINT_URL}")
fi
if [ -n "${SERVICE_NAME}" ]; then
flags+=(--log-tags "service:${SERVICE_NAME}")
else
flags+=(--log-tags "service:launchmevcommit-${NODE_TYPE}-${VERSION}-$(hostname)")
fi
${BINARY_PATH} "${flags[@]}" &
local pid=$!
sleep 1
if ! ps -p ${pid} > /dev/null; then
log_error "Failed to start mev-commit"
exit 1
fi
trap "cleanup ${pid}" EXIT SIGINT SIGTERM
log_info "To kill mev-commit, exit the script with Ctrl+C"
local address=$(cast wallet address --private-key "0x$(cat ${ROOT_PATH}/key)")
log_info "Your wallet address is ${address}"
while true ; do
local balance=$(echo "scale=18; $(cast balance ${address} --rpc-url ${RPC_URL}) / 1000000000000000000" | bc)
if (( $(echo "${balance} > 0" | bc -l) )); then
log_info "Account ${address} has been funded"
break
fi
if [ -n "${FAUCET_URL}" ]; then
log_info "Waiting for account ${address} to be funded, visit ${FAUCET_URL}"
else
log_info "Waiting for account ${address} to be funded..."
fi
sleep 5
done
wait_until_reachable "http://127.0.0.1:13523/health"
case ${NODE_TYPE} in
bidder)
log_info "Sending auto deposit request..."
local response=$(
curl \
--silent \
--show-error \
--output /dev/null \
--write-out "%{http_code}" \
--request POST "http://127.0.0.1:13523/v1/bidder/auto_deposit/${AUTO_DEPOSIT_AMOUNT}"
)
if [ "${response}" -ne 200 ]; then
log_error "Failed to send auto deposit request, status code: ${response}"
exit 1
fi
log_info "Auto deposit request sent successfully"
;;
provider)
log_info "Next, to register and stake as a provider, visit https://docs.primev.xyz/get-started/providers/registering-a-provider"
;;
esac
wait ${pid}
}
# Global flag for cleanup
cleaned_up=false
main "$@" || exit 1