-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun
More file actions
executable file
Β·995 lines (882 loc) Β· 30.6 KB
/
run
File metadata and controls
executable file
Β·995 lines (882 loc) Β· 30.6 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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
#!/bin/bash
PG_PORT=5332
PEER_LIST_FILE="demos_peerlist.json"
VERBOSE=false
NO_TUI=false
EXTERNAL_DB=false
GIT_PULL=true
TLSNOTARY_DISABLED=false
MONITORING_DISABLED=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
}
# Patch falcon-sign module to properly log uncaught exceptions
patch_falcon_sign() {
local falcon_file="./node_modules/falcon-sign/kernel/n3_v1/wasmFile/falcon512.js"
if [ ! -f "$falcon_file" ]; then
log_verbose "falcon-sign file not found, skipping patch"
return 0
fi
# Check if already patched (look for console.error(ex) near the throw)
if grep -q "console.error(ex)" "$falcon_file"; then
log_verbose "falcon-sign already patched"
return 0
fi
log_verbose "Patching falcon-sign to log uncaught exceptions..."
if [ "$PLATFORM_NAME" = "macOS" ]; then
sed -i '' '/throw ex;/i\
console.error(ex);
' "$falcon_file"
else
sed -i '/throw ex;/i\ console.error(ex);' "$falcon_file"
fi
echo "β
Patched falcon-sign for proper error logging"
}
# 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
-t Disable TUI (use legacy scrolling logs)
-v Verbose logging
-e Use external database (skip local PostgreSQL setup)
-h Show this help message
-m Disable monitoring stack (Prometheus/Grafana)
--no-tui Disable TUI (same as -t)
--external-db Use external database (same as -e)
--no-monitoring Disable monitoring stack (same as -m)
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)
./run -t # Legacy mode (scrolling logs for developers)
./run --no-tui # Same as -t
./run -e # Use external database (DATABASE_URL from env)
./run --external-db # Same as -e
SYSTEM REQUIREMENTS:
- 4GB RAM minimum (8GB recommended)
- 4+ CPU cores
- Docker and Docker Compose (unless using --external-db)
- 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
}
# Git origin validation - ensures origin points to official repo
check_git_origin() {
log_verbose "Checking git origin configuration..."
# Get current origin URL
local origin_url=$(git remote get-url origin 2>/dev/null)
if [[ -z "${origin_url}" ]]; then
echo "β οΈ No git origin configured"
return 0
fi
# Define valid official origins (both HTTPS and SSH)
local valid_https="https://github.com/kynesyslabs/node"
local valid_https_git="https://github.com/kynesyslabs/node.git"
local valid_ssh="[email protected]:kynesyslabs/node.git"
local valid_ssh_alt="[email protected]:kynesyslabs/node"
# Check if origin is the official repo
if [[ "${origin_url}" = "${valid_https}" ]] || [[ "${origin_url}" = "${valid_https_git}" ]] ||
[[ "${origin_url}" = "${valid_ssh}" ]] || [[ "${origin_url}" = "${valid_ssh_alt}" ]]; then
log_verbose "Git origin is correctly set to official repository"
return 0
fi
# Origin is not official - likely a fork
echo ""
echo "β οΈ Git origin mismatch detected!"
echo " Current origin: ${origin_url}"
echo " Expected origin: ${valid_https}"
echo ""
echo " This can cause 'git pull' to fail if your fork doesn't have the 'testnet' branch."
echo ""
# Check if this is likely a fork (contains github.com and /node)
if echo "${origin_url}" | grep -qE "github\.com.*node"; then
echo " It looks like you're using a fork of the repository."
echo ""
read -p " Would you like to fix the origin to point to the official repo? [Y/n] " -n 1 -r
echo ""
if [[ ${REPLY} =~ ^[Nn]$ ]]; then
echo " Skipping git pull for this run (origin unchanged)."
echo " π‘ Tip: Use './run -n true' to always skip git pull on custom setups."
GIT_PULL=false
return 0
else
echo " π§ Updating origin to official repository..."
# Save the old origin as 'fork' remote if it doesn't exist
if ! git remote get-url fork &>/dev/null; then
git remote add fork "${origin_url}"
echo " πΎ Your fork saved as remote 'fork'"
fi
# Update origin to official repo
git remote set-url origin "${valid_https}"
echo " β
Origin updated to ${valid_https}"
# Fetch from new origin
echo " π Fetching from official repository..."
git fetch origin
return 0
fi
else
echo " Origin doesn't appear to be a GitHub repository."
echo " Skipping git pull for this run."
echo " π‘ Tip: Use './run -n true' to always skip git pull on custom setups."
GIT_PULL=false
return 0
fi
}
# System requirements validation
check_system_requirements() {
echo "π Checking system requirements..."
log_verbose "Platform detected: ${PLATFORM_NAME}"
local failed_requirements=0
local warnings=0
# Load requirements from .requirements file if it exists
local MIN_RAM=4
local SUGGESTED_RAM=8
if [[ -f ".requirements" ]]; then
log_verbose "Loading requirements from .requirements file"
# Source the file to get the values
while IFS='=' read -r key value; do
# Skip comments and empty lines
[[ ${key} =~ ^#.*$ ]] && continue
[[ -z ${key} ]] && continue
# Remove any whitespace
key=$(echo "${key}" | tr -d ' ')
value=$(echo "${value}" | tr -d ' ')
case "${key}" in
"MIN_RAM") MIN_RAM=${value} ;;
"SUGGESTED_RAM") SUGGESTED_RAM=${value} ;;
esac
done <.requirements
log_verbose "Loaded MIN_RAM=${MIN_RAM}, SUGGESTED_RAM=${SUGGESTED_RAM}"
fi
# 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 "$MIN_RAM" ]]; then
echo "β Insufficient RAM: ${ram_gb}GB (minimum: ${MIN_RAM}GB)"
failed_requirements=$((failed_requirements + 1))
elif [[ "$ram_gb" -lt "$SUGGESTED_RAM" ]]; then
echo "β οΈ RAM below recommended: ${ram_gb}GB (recommended: ${SUGGESTED_RAM}GB)"
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
# Helper function to check if a port is in use
is_port_in_use() {
local port=$1
if command -v lsof >/dev/null; then
lsof -i :"$port" >/dev/null 2>&1
return $?
elif command -v netstat >/dev/null; then
netstat -an | grep ":${port} " >/dev/null 2>&1
return $?
else
# Cannot check, assume not in use
return 1
fi
}
# Check port availability
log_verbose "Checking port availability"
if ! command -v lsof >/dev/null && ! command -v netstat >/dev/null; then
echo "β οΈ Cannot check port availability - lsof and netstat not available"
warnings=$((warnings + 1))
else
# Only check PostgreSQL port if not using external database
if [[ "${EXTERNAL_DB}" = false ]]; then
# Check PostgreSQL port with auto-recovery attempt
if is_port_in_use "$PG_PORT"; then
echo "β οΈ PostgreSQL port ${PG_PORT} is in use, attempting to stop leftover containers..."
log_verbose "Trying to stop postgres_${PG_PORT} container"
# Try to stop the docker container that might be using the port
PG_FOLDER="postgres_${PG_PORT}"
if [[ -d "${PG_FOLDER}" ]]; then
(cd "${PG_FOLDER}" && docker compose down 2>/dev/null) || true
sleep 2 # Give Docker time to release the port
fi
# Also try the base postgres folder in case it's using the port
if [[ -d "postgres" ]]; then
(cd "postgres" && docker compose down 2>/dev/null) || true
sleep 1
fi
# Recheck after cleanup attempt
if is_port_in_use "$PG_PORT"; then
echo "β PostgreSQL ${PG_PORT} is still in use after cleanup attempt"
echo " Another process is using this port. Check with: lsof -i :${PG_PORT}"
failed_requirements=$((failed_requirements + 1))
else
echo "β
PostgreSQL ${PG_PORT} is now available (stopped leftover container)"
fi
else
echo "β
PostgreSQL ${$PG_PORT} is available"
fi
fi
# Check Node port
if is_port_in_use "$PORT"; then
echo "β Node ${PORT} is already in use"
failed_requirements=$((failed_requirements + 1))
else
echo "β
Node ${$PORT} is available"
fi
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
if [[ "${EXTERNAL_DB}" = false ]]; then
cd postgres
docker compose down
cd ..
fi
# Stop TLSNotary container if running (enabled by default)
if [[ "${TLSNOTARY_DISABLED}" != "true" ]] && [[ -d "tlsnotary" ]]; then
(cd tlsnotary && docker compose down --timeout 5 2>/dev/null) || true
# Force kill if still running
docker rm -f "tlsn-notary-${TLSNOTARY_PORT:-7047}" 2>/dev/null || true
fi
# Stop monitoring stack if running (enabled by default)
if [[ "${MONITORING_DISABLED}" != "true" ]] && [[ -d "monitoring" ]]; then
(cd monitoring && docker compose down --timeout 5 2>/dev/null) || true
fi
}
# 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"
patch_falcon_sign
# We need docker and docker compose to be installed (unless using external DB)
if [[ "${EXTERNAL_DB}" = false ]]; then
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"
fi
# 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
# Handle long options (--no-tui, --external-db) before getopts
for arg in "$@"; do
case ${arg} in
--no-tui)
NO_TUI=true
# Remove --no-tui from arguments so getopts doesn't choke on it
set -- "${@/--no-tui/}"
;;
--external-db)
EXTERNAL_DB=true
# Remove --external-db from arguments so getopts doesn't choke on it
set -- "${@/--external-db/}"
;;
--no-monitoring)
MONITORING_DISABLED=true
# Remove --no-monitoring from arguments so getopts doesn't choke on it
set -- "${@/--no-monitoring/}"
;;
esac
done
# Getting arguments
while getopts "p:d:c:i:n:u:l:r:b:tvehm" 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} ;;
t) NO_TUI=true ;;
v) VERBOSE=true ;;
e) EXTERNAL_DB=true ;;
m) MONITORING_DISABLED=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
# Check git origin configuration (may disable GIT_PULL if fork detected)
if [[ "${GIT_PULL}" = true ]]; then
check_git_origin
fi
# 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"
# Attempt git pull, handle conflicts
PULL_OUTPUT=$(git pull 2>&1)
PULL_EXIT_CODE=$?
if [[ "$PULL_EXIT_CODE" -ne 0 ]]; then
# Check if the conflict is ONLY about package.json (stash issue)
if echo "${PULL_OUTPUT}" | grep -qE "package\.json" && ! echo "${PULL_OUTPUT}" | grep -vE "package\.json|error:|CONFLICT|stash|overwritten" | grep -qE "\.ts|\.js|\.md|\.json"; then
echo "β οΈ package.json conflict detected, stashing and retrying..."
log_verbose "Stashing local changes to package.json"
git stash
if ! git pull; then
echo "β Git pull failed even after stashing"
exit 1
fi
echo "β
Repository updated after stashing package.json"
else
# Hard exit on any other git pull failure
echo "β Git pull failed:"
echo "${PULL_OUTPUT}"
echo ""
# Check for specific "no such ref" error (common with forks)
if echo "${PULL_OUTPUT}" | grep -q "no such ref was fetched"; then
echo "π‘ This error typically occurs when:"
echo " - Your 'origin' remote points to a fork that doesn't have the 'testnet' branch"
echo " - Run 'git remote -v' to check your remotes"
echo ""
echo " Quick fixes:"
echo " 1. Skip git pull: ./run -n true"
echo " 2. Fix origin: git remote set-url origin https://github.com/kynesyslabs/node"
echo " 3. Or re-run ./run and choose 'Y' when prompted about the fork"
else
echo "π‘ Please resolve git conflicts manually and try again"
fi
exit 1
fi
else
echo "β
Repository updated successfully"
fi
# Always run bun install after successful git pull
echo "π¦ Installing dependencies..."
log_verbose "Running bun install after git pull"
if ! bun install; then
echo "β Failed to install dependencies"
exit 1
fi
echo "β
Dependencies installed successfully"
patch_falcon_sign
fi
echo ""
echo "π Welcome to Demos Network!"
echo "βοΈ Node Configuration:"
echo " π Node Port: $PORT"
if [[ "${EXTERNAL_DB}" = false ]]; then
echo " ποΈ Database Port: $PG_PORT"
fi
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 [[ "${EXTERNAL_DB}" = true ]]; then
echo " π Mode: External database (DATABASE_URL)"
elif [[ "${RESTORE}" = "true" ]]; then
echo " π¦ Mode: Restore from backup"
elif [[ "${CLEAN}" = "true" ]]; then
echo " π§Ή Mode: Clean start (fresh database)"
else
echo " βΆοΈ Mode: Normal start"
fi
if [[ "${NO_TUI}" = true ]]; then
echo " π Display: Legacy logs (TUI disabled)"
else
echo " π₯οΈ Display: TUI (use -t or --no-tui for legacy logs)"
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}
# Only manage PostgreSQL if not using external database
if [[ "${EXTERNAL_DB}" = false ]]; then
# 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 || exit
# 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} || exit
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
fi
# TLSNotary Docker container management (enabled by default)
# Set TLSNOTARY_DISABLED=true to disable
if [[ "${TLSNOTARY_DISABLED}" != "true" ]]; then
TLSNOTARY_PORT="${TLSNOTARY_PORT:-7047}"
echo "π Starting TLSNotary notary container..."
if [[ -d "tlsnotary" ]]; then
cd tlsnotary
# Stop any existing container
docker compose down >/dev/null 2>&1 || true
# Start the TLSNotary container
log_verbose "Starting TLSNotary container on port ${TLSNOTARY_PORT}"
if ! TLSNOTARY_PORT=${TLSNOTARY_PORT} docker compose up -d; then
echo "β οΈ Warning: Failed to start TLSNotary container"
echo "π‘ TLSNotary attestation features will not be available"
else
echo "β
TLSNotary container started on port ${TLSNOTARY_PORT}"
# Wait for TLSNotary to be healthy (max 15 seconds)
log_verbose "Waiting for TLSNotary to be healthy..."
TLSN_TIMEOUT=15
TLSN_COUNT=0
while ! curl -sf --connect-timeout 1 --max-time 2 "http://localhost:${TLSNOTARY_PORT}/info" >/dev/null 2>&1; do
TLSN_COUNT=$((TLSN_COUNT + 1))
if [ "$TLSN_COUNT" -gt "$TLSN_TIMEOUT" ]; then
echo "β οΈ Warning: TLSNotary health check timeout"
break
fi
sleep 1
done
if [[ "$TLSN_COUNT" -le "$TLSN_TIMEOUT" ]]; then
echo "β
TLSNotary is ready"
fi
fi
cd ..
else
echo "β οΈ Warning: tlsnotary folder not found, skipping TLSNotary setup"
fi
else
log_verbose "TLSNotary disabled (TLSNOTARY_DISABLED=true)"
fi
# Monitoring stack (Prometheus/Grafana) management (enabled by default)
# Set MONITORING_DISABLED=true or use -m/--no-monitoring to disable
if [[ "${MONITORING_DISABLED}" != "true" ]]; then
echo "π Starting monitoring stack (Prometheus/Grafana)..."
if [[ -d "monitoring" ]]; then
cd monitoring || exit
# Stop any existing containers
docker compose down >/dev/null 2>&1 || true
# Start the monitoring stack
log_verbose "Starting monitoring containers"
if ! docker compose up -d; then
echo "β οΈ Warning: Failed to start monitoring stack"
echo "π‘ Monitoring dashboards will not be available"
else
echo "β
Monitoring stack started"
echo " π Prometheus: http://localhost:${PROMETHEUS_PORT:-9091}"
echo " π Grafana: http://localhost:${GRAFANA_PORT:-3000} (admin/demos)"
# Wait for Grafana to be healthy (max 30 seconds)
log_verbose "Waiting for Grafana to be healthy..."
GRAFANA_TIMEOUT=30
GRAFANA_COUNT=0
GRAFANA_PORT="${GRAFANA_PORT:-3000}"
while ! curl -sf --connect-timeout 1 --max-time 2 "http://localhost:${GRAFANA_PORT}/api/health" >/dev/null 2>&1; do
GRAFANA_COUNT=$((GRAFANA_COUNT + 1))
if [[ "$GRAFANA_COUNT" -gt "$GRAFANA_TIMEOUT" ]]; then
echo "β οΈ Warning: Grafana health check timeout"
break
fi
sleep 1
done
if [[ "$GRAFANA_COUNT" -le "$GRAFANA_TIMEOUT" ]]; then
echo "β
Grafana is ready"
fi
fi
cd ..
else
echo "β οΈ Warning: monitoring folder not found, skipping monitoring setup"
fi
else
log_verbose "Monitoring disabled (MONITORING_DISABLED=true)"
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://localho${t:$P}ORT"
if [[ ! -z "${EXPOSED_URL}" ]]; then
echo "π External UR${: $EXPOSED_}URL"
fi
echo ""
echo "π‘ Press Ctrl+C to stop the node safely"
echo "ββββββββββββββββββββββββββββββββββββββββββββββ"
echo ""
# Build the final command with optional --no-tui flag
FINAL_COMMAND="${START_COMMAND}"
if [[ "${NO_TUI}" = true ]]; then
FINAL_COMMAND="${START_COMMAND} -- --no-tui"
fi
# Starting the node managing errors
log_verbose "Starting node with environment: RPC_PORT=${PORT} PG_PORT=${PG_PORT} IDENTITY_FILE=${IDENTITY_FILE}"
log_verbose "Command: ${FINAL_COMMAND}"
if ! RPC_PORT=$PORT PG_PORT=$PG_PORT IDENTITY_FILE=$IDENTITY_FILE $FINAL_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
# Only stop PostgreSQL if we started it
if [[ "${EXTERNAL_DB}" = false ]]; then
# 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 ..
fi
# Stop TLSNotary container if it was started (enabled by default)
if [[ "${TLSNOTARY_DISABLED}" != "true" ]] && [[ -d "tlsnotary" ]]; then
echo "π Stopping TLSNotary container..."
TLSN_CONTAINER="tlsn-notary-${TLSNOTARY_PORT:-7047}"
# Try graceful shutdown first with short timeout
cd tlsnotary || exit
docker compose down --timeout 5 2>/dev/null || true
cd ..
# Force kill if still running
if docker ps -q -f "name=${TLSN_CONTAINER}" 2>/dev/null | grep -q .; then
echo " Force stopping TLSNotary container..."
docker kill "${TLSN_CONTAINER}" 2>/dev/null || true
docker rm -f "${TLSN_CONTAINER}" 2>/dev/null || true
fi
echo "β
TLSNotary stopped"
fi
# Stop monitoring stack if it was started (enabled by default)
if [[ "${MONITORING_DISABLED}" != "true" ]] && [[ -d "monitoring" ]]; then
echo "π Stopping monitoring stack..."
# Try graceful shutdown first with short timeout (subshell to preserve working directory)
(
cd monitoring && docker compose down --timeout 5 2>/dev/null
) || echo "β οΈ Warning: Failed to stop monitoring stack cleanly."
echo "β
Monitoring stack stopped"
fi
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}"