forked from kynesyslabs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun
More file actions
executable file
Β·593 lines (526 loc) Β· 18.7 KB
/
Copy pathrun
File metadata and controls
executable file
Β·593 lines (526 loc) Β· 18.7 KB
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#!/bin/bash
PG_PORT=5332
GIT_PULL=true
PEER_LIST_FILE="demos_peerlist.json"
VERBOSE=false
# Detect platform for cross-platform compatibility
PLATFORM=$(uname -s)
case $PLATFORM in
"Darwin")
PLATFORM_NAME="macOS"
;;
"Linux")
PLATFORM_NAME="Linux"
;;
*)
PLATFORM_NAME="Unknown"
;;
esac
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
HAS_BEEN_INTERRUPTED=false
# Verbose logging function
log_verbose() {
if [ "$VERBOSE" = true ]; then
echo "[VERBOSE] $1"
fi
}
# Help documentation function
show_help() {
cat << EOF
π Demos Network Node Runner
USAGE:
./run [OPTIONS]
Welcome to Demos Network! This script helps you run your blockchain node easily.
OPTIONS:
-p <port> Node port (default: 53550)
-d <port> PostgreSQL port (default: 5332)
-i <path> Identity file path (default: .demos_identity)
-c <true/false> Clean database on startup
-n <true/false> Skip git pull (useful for custom branches)
-u <url> Override EXPOSED_URL
-l <path> Peer list file (default: demos_peerlist.json)
-r <runtime> Force runtime (bun only - node deprecated)
-b <true/false> Restore from backup
-v Verbose logging
-h Show this help message
EXAMPLES:
./run # Start with default settings
./run -p 53551 -d 5333 # Run on custom ports
./run -c # Clean start (fresh database)
./run -v # Verbose output for troubleshooting
./run -n # Skip git update (for development)
SYSTEM REQUIREMENTS:
- 8GB RAM minimum (12GB recommended)
- 4+ CPU cores
- Docker and Docker Compose
- Bun runtime
- Network: <200ms ping to 1.1.1.1 (<100ms recommended)
- Free ports: 5332 (PostgreSQL) and 53550 (Node)
For support and documentation: https://demos.network
EOF
}
# System requirements validation
check_system_requirements() {
echo "π Checking system requirements..."
log_verbose "Platform detected: $PLATFORM_NAME"
local failed_requirements=0
local warnings=0
# Check RAM
log_verbose "Checking RAM requirements"
if [ "$PLATFORM_NAME" = "macOS" ]; then
# macOS: Get RAM in bytes, convert to GB
ram_bytes=$(sysctl -n hw.memsize)
ram_gb=$((ram_bytes / 1024 / 1024 / 1024))
elif [ "$PLATFORM_NAME" = "Linux" ]; then
# Linux: Get RAM from /proc/meminfo in kB, convert to GB
ram_kb=$(grep MemTotal /proc/meminfo | awk '{print $2}')
ram_gb=$((ram_kb / 1024 / 1024))
else
echo "β οΈ Unknown platform - cannot check RAM"
ram_gb=0
warnings=$((warnings + 1))
fi
if [ $ram_gb -lt 8 ]; then
echo "β Insufficient RAM: ${ram_gb}GB (minimum: 8GB)"
failed_requirements=$((failed_requirements + 1))
elif [ $ram_gb -lt 12 ]; then
echo "β οΈ RAM below recommended: ${ram_gb}GB (recommended: 12GB)"
warnings=$((warnings + 1))
else
echo "β
RAM: ${ram_gb}GB"
fi
# Check CPU cores
log_verbose "Checking CPU requirements"
if [ "$PLATFORM_NAME" = "macOS" ]; then
cpu_cores=$(sysctl -n hw.ncpu)
elif [ "$PLATFORM_NAME" = "Linux" ]; then
cpu_cores=$(nproc)
else
echo "β οΈ Unknown platform - cannot check CPU"
cpu_cores=0
warnings=$((warnings + 1))
fi
if [ $cpu_cores -lt 4 ]; then
echo "β Insufficient CPU cores: ${cpu_cores} (minimum: 4)"
failed_requirements=$((failed_requirements + 1))
else
echo "β
CPU cores: ${cpu_cores}"
fi
# Check network connectivity (ping 1.1.1.1)
log_verbose "Checking network connectivity"
if command -v ping > /dev/null; then
if [ "$PLATFORM_NAME" = "macOS" ]; then
# macOS ping syntax
ping_result=$(ping -c 3 -t 5 1.1.1.1 2>/dev/null | tail -1 | awk -F'/' '{print $5}' | cut -d'.' -f1)
elif [ "$PLATFORM_NAME" = "Linux" ]; then
# Linux ping syntax
ping_result=$(ping -c 3 -W 5 1.1.1.1 2>/dev/null | tail -1 | awk -F'/' '{print $5}' | cut -d'.' -f1)
fi
if [ -z "$ping_result" ]; then
echo "β Network connectivity failed - cannot reach 1.1.1.1"
failed_requirements=$((failed_requirements + 1))
elif [ "$ping_result" -gt 200 ]; then
echo "β Network latency too high: ${ping_result}ms (maximum: 200ms)"
failed_requirements=$((failed_requirements + 1))
elif [ "$ping_result" -gt 100 ]; then
echo "β οΈ Network latency above recommended: ${ping_result}ms (recommended: <100ms)"
warnings=$((warnings + 1))
else
echo "β
Network latency: ${ping_result}ms"
fi
else
echo "β οΈ Cannot test network - ping command not available"
warnings=$((warnings + 1))
fi
# Check port availability
log_verbose "Checking port availability"
if command -v lsof > /dev/null; then
if lsof -i :$PG_PORT > /dev/null 2>&1; then
echo "β PostgreSQL port $PG_PORT is already in use"
failed_requirements=$((failed_requirements + 1))
else
echo "β
PostgreSQL port $PG_PORT is available"
fi
if lsof -i :$PORT > /dev/null 2>&1; then
echo "β Node port $PORT is already in use"
failed_requirements=$((failed_requirements + 1))
else
echo "β
Node port $PORT is available"
fi
elif command -v netstat > /dev/null; then
# Fallback to netstat if lsof is not available
if netstat -an | grep ":$PG_PORT " > /dev/null 2>&1; then
echo "β PostgreSQL port $PG_PORT is already in use"
failed_requirements=$((failed_requirements + 1))
else
echo "β
PostgreSQL port $PG_PORT is available"
fi
if netstat -an | grep ":$PORT " > /dev/null 2>&1; then
echo "β Node port $PORT is already in use"
failed_requirements=$((failed_requirements + 1))
else
echo "β
Node port $PORT is available"
fi
else
echo "β οΈ Cannot check port availability - lsof and netstat not available"
warnings=$((warnings + 1))
fi
# Summary
if [ $failed_requirements -gt 0 ]; then
echo ""
echo "β System requirements check FAILED!"
echo " Failed requirements: $failed_requirements"
echo " Please resolve the above issues before running the node."
echo ""
echo "π‘ Need help? Visit: https://demos.network/support"
exit 1
elif [ $warnings -gt 0 ]; then
echo ""
echo "β οΈ System requirements check passed with $warnings warning(s)"
echo " The node will run but performance may be limited."
echo ""
else
echo ""
echo "β
All system requirements met!"
echo ""
fi
}
function ctrl_c() {
HAS_BEEN_INTERRUPTED=true
cd postgres
docker compose down
cd ..
}
# Function to check if we are on the first run with the .RUN file
function is_first_run() {
if [ ! -f ".RUN" ]; then
return 0
else
return 1
fi
}
# If we are on the first run, we need to install the dependencies
if is_first_run; then
echo "Installing node dependencies"
if ! bun install; then
echo "Error: Dependencies failed to install"
exit 1
fi
echo "Ok, dependencies installed"
# We need docker and docker compose to be installed
echo "π Checking Docker..."
if ! command -v docker &> /dev/null; then
echo "β Docker is not installed"
echo "π‘ Install Docker from: https://docs.docker.com/get-docker/"
if [ "$PLATFORM_NAME" = "macOS" ]; then
echo "π On macOS: Download Docker Desktop from https://docker.com/products/docker-desktop"
elif [ "$PLATFORM_NAME" = "Linux" ]; then
echo "π§ On Linux: Use your package manager or install script"
fi
exit 1
fi
if ! docker compose version &> /dev/null; then
echo "β Docker Compose is not available"
echo "π‘ Make sure Docker Desktop is running or install docker-compose-plugin"
exit 1
fi
# Check if Docker daemon is running
if ! docker info &> /dev/null; then
echo "β Docker daemon is not running"
echo "π‘ Start Docker Desktop or run: sudo systemctl start docker"
exit 1
fi
echo "β
Docker and Docker Compose are ready"
# Check if Bun is installed
if ! command -v bun &> /dev/null; then
echo "Error: Bun is not installed and is required to run the node (since 0.9.5)"
echo "Install bun following: https://bun.sh/docs/installation."
exit 1
else
echo "Ok, Bun is installed"
fi
# This file is used to check if we are on the first run
# This file is used to check if we are on the first run
touch .RUN
fi
CLEAN="false"
PORT=53550
# Getting arguments
while getopts "p:d:c:i:n:u:l:r:b:vh" opt; do
case $opt in
p) PORT=$OPTARG;;
d) PG_PORT=$OPTARG;;
c) CLEAN=$OPTARG;;
i) IDENTITY_FILE=$OPTARG;;
l) PEER_LIST_FILE=$OPTARG;;
n) GIT_PULL=false;;
u) EXPOSED_URL=$OPTARG;;
r) RUNTIME=$OPTARG;;
b) RESTORE=$OPTARG;;
v) VERBOSE=true;;
h) show_help; exit 0;;
*) echo "Invalid option. Use -h for help."; exit 1;;
esac
done
shift $((OPTIND -1))
# if RESTORE is true, make clean false
if [ "$RESTORE" == "true" ]; then
CLEAN="false"
fi
# Run system requirements check
check_system_requirements
# Perform git pull if GIT_PULL is true
if [ "$GIT_PULL" = true ]; then
echo "π Updating repository..."
log_verbose "Running git pull to get latest changes"
if ! git pull; then
echo "β οΈ Warning: Git pull failed, continuing with current version"
log_verbose "Git pull failed but continuing - might be in development mode"
else
echo "β
Repository updated successfully"
fi
fi
echo ""
echo "π Welcome to Demos Network!"
echo "βοΈ Node Configuration:"
echo " π Node Port: $PORT"
echo " ποΈ Database Port: $PG_PORT"
if [ ! -z "$IDENTITY_FILE" ]; then
echo " π Identity File: $IDENTITY_FILE"
fi
if [ ! -z "$EXPOSED_URL" ]; then
echo " π‘ Exposed URL: $EXPOSED_URL"
fi
echo " π₯ Peer List: $PEER_LIST_FILE"
if [ "$RESTORE" = "true" ]; then
echo " π¦ Mode: Restore from backup"
elif [ "$CLEAN" = "true" ]; then
echo " π§Ή Mode: Clean start (fresh database)"
else
echo " βΆοΈ Mode: Normal start"
fi
log_verbose "Platform: $PLATFORM_NAME"
log_verbose "Verbose logging enabled"
echo ""
# Check if runtime is forced via CLI argument
if [ ! -z "$RUNTIME" ]; then
case $RUNTIME in
"node")
echo "β Error: Node runtime requested but Bun is required to run the node (since 0.9.5)"
exit 1
;;
"bun")
if ! command -v bun &> /dev/null; then
echo "β Error: Bun runtime requested but Bun is not installed"
exit 1
fi
START_COMMAND="bun run start:bun"
;;
*)
echo "β Error: Invalid runtime specified. Use 'bun' only (node deprecated)"
exit 1
;;
esac
else
# Default behavior: Check if Bun is available and set the start command
if command -v bun &> /dev/null; then
START_COMMAND="bun run start:bun"
log_verbose "Using Bun runtime with command: $START_COMMAND"
else
echo "β Error: Bun is not installed and is required to run the node (since 0.9.5)"
echo "π‘ Install Bun from: https://bun.sh/docs/installation"
if [ "$PLATFORM_NAME" = "macOS" ]; then
echo "π On macOS: brew install oven-sh/bun/bun"
elif [ "$PLATFORM_NAME" = "Linux" ]; then
echo "π§ On Linux: curl -fsSL https://bun.sh/install | bash"
fi
exit 1
fi
# Temporary overriding
START_COMMAND="bun start:bun" # REVIEW Consider using bun directly
fi
echo "π Starting your Demos Network node..."
log_verbose "Using command: $START_COMMAND"
echo "π‘ This may take a moment to initialize all services..."
sleep 1
# PG_PORT and IDENTITY_FILE are exported for both docker and bun
export PG_PORT=$PG_PORT
export IDENTITY_FILE=$IDENTITY_FILE
export EXPOSED_URL=$EXPOSED_URL
export PEER_LIST_FILE=$PEER_LIST_FILE
export RESTORE=$RESTORE
# Database management with proper folder based on the port
# Create a unique postgres folder for this instance based on the port number
PG_FOLDER="postgres_${PG_PORT}"
# If the folder doesn't exist yet, create it by copying the base postgres folder
# This allows multiple instances to run simultaneously with different ports
if [ ! -d "$PG_FOLDER" ]; then
cp -r postgres $PG_FOLDER
fi
cd $PG_FOLDER
# If we are cleaning, we need to remove the database
if [ "$CLEAN" == "true" ]; then
echo "π§Ή Cleaning the database..."
log_verbose "Removing existing database data for clean start"
sleep 1
rm -rf data_*
mkdir data_${PG_PORT}
echo "β
Database cleaned"
fi
# Suppressing errors if the database is not running
docker compose down > /dev/null 2>&1
if [ "$CLEAN" == "true" ]; then
rm -rf data_${PG_PORT} || rm -rf data_${PG_PORT}
mkdir data_${PG_PORT}
fi
# Finally starting the database
echo "ποΈ Starting PostgreSQL database..."
log_verbose "Running docker compose up -d in $PG_FOLDER"
if ! docker compose up -d; then
echo "β Failed to start PostgreSQL database"
echo "π‘ Check Docker Desktop is running and try again"
exit 1
fi
echo "β
PostgreSQL container started"
cd ..
function is_db_ready() {
docker exec postgres_${PG_PORT} pg_isready -U demosuser -d demos > /dev/null 2>&1
return $?
}
# Function to wait for database availability
function wait_for_database() {
local port=$1
local timeout=${2:-30} # Increased timeout to 30 seconds
echo "β³ Waiting for PostgreSQL to be available on port $port..."
log_verbose "Checking database connectivity with timeout of ${timeout}s"
local count=0
while ! nc -z localhost $port; do
if [ $((count % 5)) -eq 0 ]; then
echo " Still waiting... (${count}s elapsed)"
fi
count=$((count+1))
if [ $count -gt $timeout ]; then
echo "β Timeout waiting for PostgreSQL to be available after ${timeout}s"
echo "π‘ Try increasing resources or check Docker logs"
return 1
fi
sleep 1
done
echo "β
PostgreSQL is accepting connections"
return 0
}
function wait_for_database_ready() {
local port=$1
local timeout=${2:-20} # Increased timeout to 20 seconds
echo "β³ Waiting for PostgreSQL to be ready for connections..."
log_verbose "Checking database readiness with pg_isready, timeout ${timeout}s"
local count=0
while ! is_db_ready; do
if [ $((count % 3)) -eq 0 ] && [ $count -gt 0 ]; then
echo " Database initializing... (${count}s elapsed)"
fi
count=$((count+1))
if [ $count -gt $timeout ]; then
echo "β Timeout waiting for PostgreSQL to be ready after ${timeout}s"
echo "π‘ Database may be still initializing - check Docker logs"
return 1
fi
sleep 1
done
echo "β
PostgreSQL is ready for operations"
return 0
}
# Replace the original wait code with function call
if ! wait_for_database $PG_PORT; then
echo "β Failed to connect to PostgreSQL database"
echo "π‘ Try restarting Docker or check system resources"
exit 1
fi
if [ "$RESTORE" == "true" ]; then
if ! wait_for_database_ready $PG_PORT; then
echo "β Failed to connect to PostgreSQL database"
echo "π‘ Database may need more time to initialize"
exit 1
fi
echo "π Restoring the node"
if ! bun run restore; then
echo "β Error: Failed to restore the node"
exit 1
fi
# sleep 20
# exit 0
# Stop the database
echo "Stopping the database"
cd postgres_${PG_PORT}
docker compose down
# Remove the database folder
echo "Removing the database folder"
rm -rf data_* || sudo rm -rf data_*
mkdir data_${PG_PORT}
# Start the database
echo "Starting the database"
docker compose up -d
cd ..
# Wait for the database to be available
echo "Restarting database"
wait_for_database $PG_PORT
# else
# echo "Cleaning the output/ folder"
# rm -rf output/*
fi
# Ensuring the logs folder exists
mkdir -p logs
echo ""
echo "π All systems ready! Starting your Demos Network node..."
echo "π Logs will be saved to: logs/"
echo "π Your node will be available on: http://localhost:$PORT"
if [ ! -z "$EXPOSED_URL" ]; then
echo "π External URL: $EXPOSED_URL"
fi
echo ""
echo "π‘ Press Ctrl+C to stop the node safely"
echo "ββββββββββββββββββββββββββββββββββββββββββββββ"
echo ""
# Starting the node managing errors
log_verbose "Starting node with environment: RPC_PORT=$PORT PG_PORT=$PG_PORT IDENTITY_FILE=$IDENTITY_FILE"
if ! RPC_PORT=$PORT PG_PORT=$PG_PORT IDENTITY_FILE=$IDENTITY_FILE $START_COMMAND; then
if [ "$HAS_BEEN_INTERRUPTED" == "true" ]; then
echo ""
echo "β
Demos Network node stopped successfully"
else
echo "β Error: RPC failed to start or crashed"
exit_code=$?
# Logging memory usage (cross-platform)
echo "πΎ Memory usage at crash:"
if [ "$PLATFORM_NAME" = "macOS" ]; then
vm_stat | head -10
vm_stat | head -10 > last_crash_memory_usage.txt
elif [ "$PLATFORM_NAME" = "Linux" ]; then
free -m
free -m > last_crash_memory_usage.txt
else
echo "Unknown platform - cannot collect memory info"
echo "Platform: $PLATFORM_NAME" > last_crash_memory_usage.txt
fi
fi
else
echo ""
echo "β
Demos Network node exited successfully"
exit_code=0
fi
# Once exiting, stopping the database
echo "π Stopping PostgreSQL database..."
cd postgres_${PG_PORT}
if docker compose down; then
echo "β
PostgreSQL stopped successfully"
else
echo "β οΈ Warning: Failed to stop PostgreSQL gracefully"
fi
cd ..
echo ""
echo "π Demos Network node session completed"
echo "π‘ Thank you for running a Demos Network node!"
echo "π For support: https://demos.network/support"
echo ""
log_verbose "Final exit code: $exit_code"