This repository was archived by the owner on Jun 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconfigure-environment.sh
More file actions
executable file
Β·742 lines (653 loc) Β· 35.4 KB
/
Copy pathconfigure-environment.sh
File metadata and controls
executable file
Β·742 lines (653 loc) Β· 35.4 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
#!/bin/bash
# Initial variable declarations
DOCKER_NETWORK_PRUNE=false
SETUP_COMPLETE=true
SKIP_CREDENTIAL_UPDATE=false
NO_COLLECTOR=false
OVERRIDE_DEFAULTS_SCRIPT_FLAG=false
DEVNET_MODE=false
BDS_DSV_DEVNET=false
BDS_DSV_MAINNET=false
DOCKER_MODE=false
RESULT_FILE=""
# GitHub configuration URL
MARKETS_CONFIG_URL="https://raw.githubusercontent.com/powerloom/curated-datamarkets/master/sources.json"
# Dynamic defaults (will be populated from GitHub API)
DEFAULT_POWERLOOM_CHAIN=""
DEFAULT_SOURCE_CHAIN=""
DEFAULT_NAMESPACE=""
DEFAULT_POWERLOOM_RPC_URL=""
DEFAULT_PROTOCOL_STATE_CONTRACT=""
DEFAULT_DATA_MARKET_CONTRACT=""
DEFAULT_SNAPSHOT_CONFIG_REPO=""
DEFAULT_SNAPSHOT_CONFIG_REPO_BRANCH=""
DEFAULT_SNAPSHOT_CONFIG_REPO_COMMIT=""
DEFAULT_SNAPSHOTTER_COMPUTE_REPO=""
DEFAULT_SNAPSHOTTER_COMPUTE_REPO_BRANCH=""
DEFAULT_SNAPSHOTTER_COMPUTE_REPO_COMMIT=""
DEFAULT_CONNECTION_REFRESH_INTERVAL_SEC=""
DEFAULT_TELEGRAM_NOTIFICATION_COOLDOWN=""
DEFAULT_DEVNET_POWERLOOM_CHAIN=""
DEFAULT_DEVNET_SOURCE_CHAIN=""
DEFAULT_DEVNET_NAMESPACE=""
DEFAULT_DEVNET_POWERLOOM_RPC_URL=""
DEFAULT_DEVNET_PROTOCOL_STATE_CONTRACT=""
DEFAULT_DEVNET_SNAPSHOT_CONFIG_REPO=""
DEFAULT_DEVNET_SNAPSHOT_CONFIG_REPO_BRANCH=""
DEFAULT_DEVNET_SNAPSHOT_CONFIG_REPO_COMMIT=""
DEFAULT_DEVNET_SNAPSHOTTER_COMPUTE_REPO=""
DEFAULT_DEVNET_SNAPSHOTTER_COMPUTE_REPO_BRANCH=""
DEFAULT_DEVNET_SNAPSHOTTER_COMPUTE_REPO_COMMIT=""
DEFAULT_DEVNET_CONNECTION_REFRESH_INTERVAL_SEC=""
DEFAULT_DEVNET_TELEGRAM_NOTIFICATION_COOLDOWN=""
# Global variables for storing fetched configuration
MARKETS_CONFIG_JSON=""
# --- Global Variables ---
ENV_FILE_PATH=""
FILE_WAS_NEWLY_CREATED=false
CLEANUP_ENV_FILE_ON_ABORT=false
TARGET_ENV_FILE_FOR_CLEANUP=""
# Error handling and cleanup functions
handle_error() {
local exit_code=$?
if [ $exit_code -lt 100 ]; then
echo "Error on line $1: Command exited with status $exit_code"
exit $exit_code
fi
return $exit_code
}
final_cleanup_handler() {
find . -name "*.backup" -type f -delete
if [ "$CLEANUP_ENV_FILE_ON_ABORT" = true ] && [ -n "$TARGET_ENV_FILE_FOR_CLEANUP" ] && [ -f "$TARGET_ENV_FILE_FOR_CLEANUP" ] && [ "$SETUP_COMPLETE" = false ]; then
rm -f "$TARGET_ENV_FILE_FOR_CLEANUP"
echo "ποΈ Setup was interrupted or incomplete. Deleted partially created $TARGET_ENV_FILE_FOR_CLEANUP file."
elif [ "$SETUP_COMPLETE" = false ] && [ -n "$ENV_FILE_PATH" ] && [ "$ENV_FILE_PATH" != "$TARGET_ENV_FILE_FOR_CLEANUP" ]; then
echo "β οΈ Setup incomplete or aborted. Please review $ENV_FILE_PATH as it might be in an inconsistent state."
elif [ "$SETUP_COMPLETE" = false ] && [ -z "$TARGET_ENV_FILE_FOR_CLEANUP" ]; then
echo "β οΈ Setup incomplete or aborted."
fi
}
trap 'handle_error $LINENO' ERR
trap final_cleanup_handler EXIT
# Function to parse command line arguments
parse_arguments() {
while [[ $# -gt 0 ]]; do
case $1 in
--docker-network-prune) DOCKER_NETWORK_PRUNE=true; shift ;;
--skip-credential-update) SKIP_CREDENTIAL_UPDATE=true; shift ;;
--no-collector) NO_COLLECTOR=true; shift ;;
--override) OVERRIDE_DEFAULTS_SCRIPT_FLAG=true; shift ;;
--devnet) DEVNET_MODE=true; shift ;;
--bds-dsv-devnet) DEVNET_MODE=true; BDS_DSV_DEVNET=true; shift ;;
--bds-dsv-mainnet) BDS_DSV_MAINNET=true; shift ;;
--docker-mode) DOCKER_MODE=true; shift ;;
--result-file) RESULT_FILE=$2; shift 2 ;;
*)
shift
;;
esac
done
}
# Function to safely parse configuration output into variables
parse_config_vars() {
local config_output="$1"
CHAIN_RPC_URL=""
DATA_MARKET_CONTRACT=""
PROTOCOL_STATE_CONTRACT=""
SNAPSHOT_CONFIG_REPO=""
SNAPSHOT_CONFIG_REPO_BRANCH=""
SNAPSHOT_CONFIG_REPO_COMMIT=""
SNAPSHOTTER_COMPUTE_REPO=""
SNAPSHOTTER_COMPUTE_REPO_BRANCH=""
SNAPSHOTTER_COMPUTE_REPO_COMMIT=""
SOURCE_CHAIN=""
BOOTSTRAP_NODE_ADDRS=""
while IFS= read -r line; do
if [ -n "$line" ] && [[ "$line" == *"="* ]]; then
case "$line" in
CHAIN_RPC_URL=*) CHAIN_RPC_URL="${line#CHAIN_RPC_URL=}" ;;
DATA_MARKET_CONTRACT=*) DATA_MARKET_CONTRACT="${line#DATA_MARKET_CONTRACT=}" ;;
PROTOCOL_STATE_CONTRACT=*) PROTOCOL_STATE_CONTRACT="${line#PROTOCOL_STATE_CONTRACT=}" ;;
SNAPSHOT_CONFIG_REPO=*) SNAPSHOT_CONFIG_REPO="${line#SNAPSHOT_CONFIG_REPO=}" ;;
SNAPSHOT_CONFIG_REPO_BRANCH=*) SNAPSHOT_CONFIG_REPO_BRANCH="${line#SNAPSHOT_CONFIG_REPO_BRANCH=}" ;;
SNAPSHOT_CONFIG_REPO_COMMIT=*) SNAPSHOT_CONFIG_REPO_COMMIT="${line#SNAPSHOT_CONFIG_REPO_COMMIT=}" ;;
SNAPSHOTTER_COMPUTE_REPO=*) SNAPSHOTTER_COMPUTE_REPO="${line#SNAPSHOTTER_COMPUTE_REPO=}" ;;
SNAPSHOTTER_COMPUTE_REPO_BRANCH=*) SNAPSHOTTER_COMPUTE_REPO_BRANCH="${line#SNAPSHOTTER_COMPUTE_REPO_BRANCH=}" ;;
SNAPSHOTTER_COMPUTE_REPO_COMMIT=*) SNAPSHOTTER_COMPUTE_REPO_COMMIT="${line#SNAPSHOTTER_COMPUTE_REPO_COMMIT=}" ;;
SOURCE_CHAIN=*) SOURCE_CHAIN="${line#SOURCE_CHAIN=}" ;;
BOOTSTRAP_NODE_ADDRS=*) BOOTSTRAP_NODE_ADDRS="${line#BOOTSTRAP_NODE_ADDRS=}" ;;
esac
fi
done <<< "$config_output"
}
# Function to check if jq is available
has_jq() {
command -v jq >/dev/null 2>&1
}
# Function to extract chain configuration using jq
extract_chain_config_jq() {
local chain_name="$1"
local market_name="$2"
if [ -z "$MARKETS_CONFIG_JSON" ]; then return 1; fi
local jq_filter=".[] | select(.powerloomChain.name == \"$chain_name\") | { rpcURL: .powerloomChain.rpcURL, market: (.dataMarkets[]? | select(.name == \"$market_name\")) }"
local result
if ! result=$(echo "$MARKETS_CONFIG_JSON" | jq -c "$jq_filter" 2>&1); then
echo "β οΈ jq parsing failed for chain: $chain_name, market: $market_name" >&2
echo "π jq error: $result" >&2
return 1
fi
if [ "$result" = "null" ] || [ -z "$result" ] || [ "$result" = "{}" ]; then return 1; fi
local rpc_url=$(echo "$result" | jq -r '.rpcURL // empty')
local contract_addr=$(echo "$result" | jq -r '.market.contractAddress // empty')
local protocol_state=$(echo "$result" | jq -r '.market.powerloomProtocolStateContractAddress // empty')
local config_repo=$(echo "$result" | jq -r '.market.config.repo // empty')
local config_branch=$(echo "$result" | jq -r '.market.config.branch // empty')
local config_commit=$(echo "$result" | jq -r '.market.config.commit // empty')
local compute_repo=$(echo "$result" | jq -r '.market.compute.repo // empty')
local compute_branch=$(echo "$result" | jq -r '.market.compute.branch // empty')
local compute_commit=$(echo "$result" | jq -r '.market.compute.commit // empty')
local source_chain=$(echo "$result" | jq -r '.market.sourceChain // empty')
local bootstrap_nodes_json=$(echo "$result" | jq -c '.market.bootstrapNodes // []')
local sequencer_addr=$(echo "$result" | jq -r '.market.sequencer // empty')
if [[ "$source_chain" == *"-"* ]]; then source_chain="${source_chain%%-*}"; fi
[ -n "$rpc_url" ] && echo "CHAIN_RPC_URL=$rpc_url"
[ -n "$contract_addr" ] && echo "DATA_MARKET_CONTRACT=$contract_addr"
[ -n "$protocol_state" ] && echo "PROTOCOL_STATE_CONTRACT=$protocol_state"
[ -n "$config_repo" ] && echo "SNAPSHOT_CONFIG_REPO=$config_repo"
[ -n "$config_branch" ] && echo "SNAPSHOT_CONFIG_REPO_BRANCH=$config_branch"
[ -n "$config_commit" ] && echo "SNAPSHOT_CONFIG_REPO_COMMIT=$config_commit"
[ -n "$compute_repo" ] && echo "SNAPSHOTTER_COMPUTE_REPO=$compute_repo"
[ -n "$compute_branch" ] && echo "SNAPSHOTTER_COMPUTE_REPO_BRANCH=$compute_branch"
[ -n "$compute_commit" ] && echo "SNAPSHOTTER_COMPUTE_REPO_COMMIT=$compute_commit"
[ -n "$source_chain" ] && echo "SOURCE_CHAIN=$source_chain"
# Handle multiple bootstrap nodes from sources.json
local bootstrap_count=$(echo "$bootstrap_nodes_json" | jq 'length')
if [ "$bootstrap_count" -gt 0 ]; then
# Create comma-separated list of bootstrap nodes
local bootstrap_addrs=$(echo "$bootstrap_nodes_json" | jq -r '.[]' | tr '\n' ',' | sed 's/,$//')
echo "BOOTSTRAP_NODE_ADDRS=$bootstrap_addrs"
echo "π Configured $bootstrap_count bootstrap nodes from sources.json"
elif [ -n "$sequencer_addr" ]; then
# Fallback to legacy sequencer field if no bootstrapNodes array
echo "BOOTSTRAP_NODE_ADDRS=$sequencer_addr"
echo "β οΈ Using legacy sequencer configuration (consider updating sources.json)"
fi
}
# Function to extract chain configuration
extract_chain_config() {
if ! has_jq; then
echo "β Error: jq is required but not installed." >&2
return 1
fi
extract_chain_config_jq "$1" "$2"
}
# Function to fetch markets configuration from GitHub
fetch_markets_config() {
echo "π Fetching latest protocol state and data market configuration from GitHub..."
if command -v curl >/dev/null 2>&1; then
MARKETS_CONFIG_JSON=$(curl -s --connect-timeout 10 --max-time 30 "$MARKETS_CONFIG_URL")
elif command -v wget >/dev/null 2>&1;
then
MARKETS_CONFIG_JSON=$(wget -qO- --timeout=30 --connect-timeout=10 "$MARKETS_CONFIG_URL")
else
echo "β οΈ Neither curl nor wget found. Exiting..."
exit 1
fi
if [ -z "$MARKETS_CONFIG_JSON" ] || ! echo "$MARKETS_CONFIG_JSON" | jq -e . >/dev/null 2>&1;
then
echo "β οΈ Failed to fetch or parse configuration from GitHub. Exiting..."
exit 1
fi
echo "β
Successfully fetched protocol state and data market configurations."
}
# Function to initialize default configuration values
initialize_default_config() {
if ! fetch_markets_config; then exit 1; fi
echo "π§ Initializing default configuration from GitHub data..."
# For mainnet - find the first available chain and its first data market
local first_mainnet_chain=$(echo "$MARKETS_CONFIG_JSON" | jq -r '.[].powerloomChain.name | select(startswith("mainnet")) | select(. != null)' | head -1)
if [ -n "$first_mainnet_chain" ]; then
local first_market_on_chain=$(echo "$MARKETS_CONFIG_JSON" | jq -r ".[] | select(.powerloomChain.name == \"$first_mainnet_chain\") | .dataMarkets[0].name // \"UNISWAPV2\"" | head -1)
local mainnet_config=$(extract_chain_config "$first_mainnet_chain" "$first_market_on_chain")
if [ -n "$mainnet_config" ]; then
parse_config_vars "$mainnet_config"
DEFAULT_POWERLOOM_CHAIN="$first_mainnet_chain"
DEFAULT_SOURCE_CHAIN="$SOURCE_CHAIN"
DEFAULT_NAMESPACE="$first_market_on_chain"
DEFAULT_POWERLOOM_RPC_URL="$CHAIN_RPC_URL"
DEFAULT_PROTOCOL_STATE_CONTRACT="$PROTOCOL_STATE_CONTRACT"
DEFAULT_DATA_MARKET_CONTRACT="$DATA_MARKET_CONTRACT"
DEFAULT_SNAPSHOT_CONFIG_REPO="$SNAPSHOT_CONFIG_REPO"
DEFAULT_SNAPSHOT_CONFIG_REPO_BRANCH="$SNAPSHOT_CONFIG_REPO_BRANCH"
DEFAULT_SNAPSHOT_CONFIG_REPO_COMMIT="$SNAPSHOT_CONFIG_REPO_COMMIT"
DEFAULT_SNAPSHOTTER_COMPUTE_REPO="$SNAPSHOTTER_COMPUTE_REPO"
DEFAULT_SNAPSHOTTER_COMPUTE_REPO_BRANCH="$SNAPSHOTTER_COMPUTE_REPO_BRANCH"
DEFAULT_SNAPSHOTTER_COMPUTE_REPO_COMMIT="$SNAPSHOTTER_COMPUTE_REPO_COMMIT"
DEFAULT_CONNECTION_REFRESH_INTERVAL_SEC="60"
DEFAULT_TELEGRAM_NOTIFICATION_COOLDOWN="300"
fi
fi
# For devnet - find the first available chain and its first data market
local first_devnet_chain=$(echo "$MARKETS_CONFIG_JSON" | jq -r '.[].powerloomChain.name | select(startswith("devnet")) | select(. != null)' | head -1)
if [ -n "$first_devnet_chain" ]; then
local first_market_on_devnet=$(echo "$MARKETS_CONFIG_JSON" | jq -r ".[] | select(.powerloomChain.name == \"$first_devnet_chain\") | .dataMarkets[0].name // \"UNISWAPV2\"" | head -1)
local devnet_config=$(extract_chain_config "$first_devnet_chain" "$first_market_on_devnet")
if [ -n "$devnet_config" ]; then
parse_config_vars "$devnet_config"
DEFAULT_DEVNET_POWERLOOM_CHAIN="$first_devnet_chain"
DEFAULT_DEVNET_SOURCE_CHAIN="$SOURCE_CHAIN"
DEFAULT_DEVNET_NAMESPACE="$first_market_on_devnet"
DEFAULT_DEVNET_POWERLOOM_RPC_URL="$CHAIN_RPC_URL"
DEFAULT_DEVNET_PROTOCOL_STATE_CONTRACT="$PROTOCOL_STATE_CONTRACT"
DEFAULT_DEVNET_SNAPSHOT_CONFIG_REPO="$SNAPSHOT_CONFIG_REPO"
DEFAULT_DEVNET_SNAPSHOT_CONFIG_REPO_BRANCH="$SNAPSHOT_CONFIG_REPO_BRANCH"
DEFAULT_DEVNET_SNAPSHOT_CONFIG_REPO_COMMIT="$SNAPSHOT_CONFIG_REPO_COMMIT"
DEFAULT_DEVNET_SNAPSHOTTER_COMPUTE_REPO="$SNAPSHOTTER_COMPUTE_REPO"
DEFAULT_DEVNET_SNAPSHOTTER_COMPUTE_REPO_BRANCH="$SNAPSHOTTER_COMPUTE_REPO_BRANCH"
DEFAULT_DEVNET_SNAPSHOTTER_COMPUTE_REPO_COMMIT="$SNAPSHOTTER_COMPUTE_REPO_COMMIT"
DEFAULT_DEVNET_CONNECTION_REFRESH_INTERVAL_SEC="60"
DEFAULT_DEVNET_TELEGRAM_NOTIFICATION_COOLDOWN="300"
fi
fi
}
# Helper function to mask sensitive values for logging
mask_sensitive_value() {
local name="$1"
local value="$2"
if [[ "$name" == "SIGNER_ACCOUNT_PRIVATE_KEY" ]] || [[ "$name" == "TELEGRAM_CHAT_ID" ]]; then
echo "<HIDDEN>"
else
echo "$value"
fi
}
# Helper function to update a variable in a file or append it if it doesn't exist
update_or_append_var() {
local var_name="$1"
local var_value="$2"
local target_file="$3"
# Validate input parameters to prevent corruption
if [ -z "$var_name" ]; then
echo "β ERROR: Variable name cannot be empty"
return 1
fi
# Check for malformed assignments involving this variable
if grep -q "^[A-Z_][A-Z0-9_]*=.*${var_name}=" "$target_file"; then
echo "β οΈ WARNING: Found malformed assignment involving ${var_name}, fixing..."
sed -i.backup "/^[A-Z_][A-Z0-9_]*=.*${var_name}=/d" "$target_file"
rm -f "$target_file.backup"
fi
if grep -q "^${var_name}=" "$target_file"; then
local existing_var_value
existing_var_value=$(grep "^${var_name}=" "$target_file" | head -1 | cut -d'=' -f2-)
if [ "$var_value" != "$existing_var_value" ]; then
local masked_new=$(mask_sensitive_value "$var_name" "$var_value")
echo "π Updating $var_name in $target_file to: $masked_new"
# Use exact pattern match to avoid corruption
sed -i.backup "/^${var_name}=/d" "$target_file"
echo "${var_name}=${var_value}" >> "$target_file"
rm -f "$target_file.backup"
fi
else
local masked_value=$(mask_sensitive_value "$var_name" "$var_value")
echo "π Adding new variable $var_name to $target_file with value: $masked_value"
echo "${var_name}=${var_value}" >> "$target_file"
fi
}
# Function to dynamically select a data market and configure it
select_market_and_configure() {
local is_devnet_mode="${1:-false}"
local selected_chain_name
local selected_market_name
# Auto-select BDS_DEVNET_ALPHA_UNISWAPV3 when BDS_DSV_DEVNET is enabled
if [ "$BDS_DSV_DEVNET" = "true" ]; then
echo "π BDS DSV Devnet mode - auto-selecting BDS_DEVNET_ALPHA_UNISWAPV3 market"
# Use same logic as manual devnet selection
local first_devnet_chain=$(echo "$MARKETS_CONFIG_JSON" | jq -r '.[].powerloomChain.name | select(startswith("devnet")) | select(. != null)' | head -1)
selected_chain_name="$first_devnet_chain"
selected_market_name="BDS_DEVNET_ALPHA_UNISWAPV3"
echo "β
Selected chain: $selected_chain_name"
echo "β
Selected market: $selected_market_name"
# Auto-select BDS_MAINNET_UNISWAPV3 when BDS_DSV_MAINNET is enabled
elif [ "$BDS_DSV_MAINNET" = "true" ]; then
echo "π BDS DSV Mainnet mode - auto-selecting BDS_MAINNET_UNISWAPV3 market"
# Use same logic as manual mainnet selection
local first_mainnet_chain=$(echo "$MARKETS_CONFIG_JSON" | jq -r '.[].powerloomChain.name | select(startswith("mainnet")) | select(. != null)' | head -1)
selected_chain_name="$first_mainnet_chain"
selected_market_name="BDS_MAINNET_UNISWAPV3"
echo "β
Selected chain: $selected_chain_name"
echo "β
Selected market: $selected_market_name"
elif [ "$is_devnet_mode" = "true" ]; then
local devnet_chains
mapfile -t devnet_chains < <(echo "$MARKETS_CONFIG_JSON" | jq -r '.[].powerloomChain.name | select(startswith("devnet"))' | sort -u)
if [ "${#devnet_chains[@]}" -eq 0 ]; then
echo "β No devnet chains found in the configuration."
exit 1
elif [ "${#devnet_chains[@]}" -eq 1 ]; then
selected_chain_name="${devnet_chains[0]}"
else
echo "π Select a Powerloom devnet chain:"
select chain_choice in "${devnet_chains[@]}"; do
if [ -n "$chain_choice" ]; then
selected_chain_name="$chain_choice"
break
else
echo "β Invalid selection. Please try again."
fi
done
fi
else
local chains
mapfile -t chains < <(echo "$MARKETS_CONFIG_JSON" | jq -r '.[].powerloomChain.name' | sort -u)
echo "π Select a Powerloom chain:"
select chain_choice in "${chains[@]}"; do
if [ -n "$chain_choice" ]; then
selected_chain_name="$chain_choice"
break
else
echo "β Invalid selection. Please try again."
fi
done
fi
# Skip market selection if BDS_DSV_DEVNET or BDS_DSV_MAINNET is true (already set above)
if [ "$BDS_DSV_DEVNET" != "true" ] && [ "$BDS_DSV_MAINNET" != "true" ]; then
local markets
mapfile -t markets < <(echo "$MARKETS_CONFIG_JSON" | jq -r ".[] | select(.powerloomChain.name == \"$selected_chain_name\") | .dataMarkets[].name" | sort)
echo "π Select a data market on '$selected_chain_name':"
select market_choice in "${markets[@]}"; do
if [ -n "$market_choice" ]; then
selected_market_name="$market_choice"
break
else
echo "β Invalid selection. Please try again."
fi
done
fi
echo "β
You selected: $selected_market_name on $selected_chain_name"
local config_result=$(extract_chain_config "$selected_chain_name" "$selected_market_name")
if [ -n "$config_result" ]; then
parse_config_vars "$config_result"
export NAMESPACE="$selected_market_name"
export POWERLOOM_CHAIN="$selected_chain_name"
export POWERLOOM_RPC_URL="$CHAIN_RPC_URL"
else
echo "β οΈ Could not fetch $selected_market_name config from GitHub, exiting..."
exit 1
fi
}
# Function to prompt for user credentials
prompt_for_credentials() {
local env_file="$1"
read -p "Enter SOURCE_RPC_URL: " source_rpc_url_val
update_or_append_var "SOURCE_RPC_URL" "$source_rpc_url_val" "$env_file"
read -p "Enter SIGNER_ACCOUNT_ADDRESS: " signer_account_address_val
update_or_append_var "SIGNER_ACCOUNT_ADDRESS" "$signer_account_address_val" "$env_file"
read -s -p "Enter SIGNER_ACCOUNT_PRIVATE_KEY: " signer_account_private_key_val; echo
update_or_append_var "SIGNER_ACCOUNT_PRIVATE_KEY" "$signer_account_private_key_val" "$env_file"
read -p "Enter Your SLOT_ID (NFT_ID): " slot_id_val
update_or_append_var "SLOT_ID" "$slot_id_val" "$env_file"
read -p "Enter Your TELEGRAM_CHAT_ID (Optional, leave blank to skip.): " telegram_chat_id_val
update_or_append_var "TELEGRAM_CHAT_ID" "$telegram_chat_id_val" "$env_file"
}
# Function to update all configuration variables in a file
update_common_config() {
local env_file="$1"
export FULL_NAMESPACE="${POWERLOOM_CHAIN}-${NAMESPACE}-${SOURCE_CHAIN}"
export DOCKER_NETWORK_NAME="snapshotter-lite-v2-${FULL_NAMESPACE}"
update_or_append_var "POWERLOOM_CHAIN" "$POWERLOOM_CHAIN" "$env_file"
update_or_append_var "NAMESPACE" "$NAMESPACE" "$env_file"
update_or_append_var "SOURCE_CHAIN" "$SOURCE_CHAIN" "$env_file"
update_or_append_var "POWERLOOM_RPC_URL" "$POWERLOOM_RPC_URL" "$env_file"
update_or_append_var "PROTOCOL_STATE_CONTRACT" "$PROTOCOL_STATE_CONTRACT" "$env_file"
update_or_append_var "DATA_MARKET_CONTRACT" "$DATA_MARKET_CONTRACT" "$env_file"
update_or_append_var "SNAPSHOT_CONFIG_REPO" "$SNAPSHOT_CONFIG_REPO" "$env_file"
update_or_append_var "SNAPSHOT_CONFIG_REPO_BRANCH" "$SNAPSHOT_CONFIG_REPO_BRANCH" "$env_file"
update_or_append_var "SNAPSHOT_CONFIG_REPO_COMMIT" "$SNAPSHOT_CONFIG_REPO_COMMIT" "$env_file"
update_or_append_var "SNAPSHOTTER_COMPUTE_REPO" "$SNAPSHOTTER_COMPUTE_REPO" "$env_file"
update_or_append_var "SNAPSHOTTER_COMPUTE_REPO_BRANCH" "$SNAPSHOTTER_COMPUTE_REPO_BRANCH" "$env_file"
update_or_append_var "SNAPSHOTTER_COMPUTE_REPO_COMMIT" "$SNAPSHOTTER_COMPUTE_REPO_COMMIT" "$env_file"
update_or_append_var "FULL_NAMESPACE" "$FULL_NAMESPACE" "$env_file"
update_or_append_var "DOCKER_NETWORK_NAME" "$DOCKER_NETWORK_NAME" "$env_file"
update_or_append_var "CONNECTION_REFRESH_INTERVAL_SEC" "$CONNECTION_REFRESH_INTERVAL_SEC" "$env_file"
update_or_append_var "TELEGRAM_NOTIFICATION_COOLDOWN" "$TELEGRAM_NOTIFICATION_COOLDOWN" "$env_file"
# Write NO_COLLECTOR flag to env file so deploy-services.sh can read it
update_or_append_var "NO_COLLECTOR" "${NO_COLLECTOR:-false}" "$env_file"
# Set bootstrap node configuration if available
if [ -n "$BOOTSTRAP_NODE_ADDRS" ]; then
update_or_append_var "BOOTSTRAP_NODE_ADDRS" "$BOOTSTRAP_NODE_ADDRS" "$env_file"
fi
# Preserve CENTRALIZED_SEQUENCER_ENABLED from sourced env file if it exists
# This ensures values set by deployment.py (e.g., from profile env files) are preserved
if [ -n "${CENTRALIZED_SEQUENCER_ENABLED:-}" ]; then
update_or_append_var "CENTRALIZED_SEQUENCER_ENABLED" "$CENTRALIZED_SEQUENCER_ENABLED" "$env_file"
fi
}
# Function to handle credential updates
handle_credential_updates() {
if [ "$SKIP_CREDENTIAL_UPDATE" = "true" ]; then return; fi
if [ "$FILE_WAS_NEWLY_CREATED" = "true" ]; then return; fi
local current_signer_address=$(grep "^SIGNER_ACCOUNT_ADDRESS=" "$ENV_FILE_PATH" | cut -d'=' -f2- || echo "")
local current_slot_id=$(grep "^SLOT_ID=" "$ENV_FILE_PATH" | cut -d'=' -f2- || echo "")
local current_source_rpc_url=$(grep "^SOURCE_RPC_URL=" "$ENV_FILE_PATH" | cut -d'=' -f2- || echo "")
read -p "π«Έ βΆοΈ Would you like to update any credentials in $ENV_FILE_PATH? (y/n): " update_env_vars
if [ "$update_env_vars" = "y" ]; then
SETUP_COMPLETE=false
read -p "Enter new SIGNER_ACCOUNT_ADDRESS (current: ${current_signer_address:-\<not set\>}, press enter to skip): " new_signer_account_address
if [ -n "$new_signer_account_address" ]; then
read -s -p "Enter new SIGNER_ACCOUNT_PRIVATE_KEY: " new_signer_account_private_key; echo
update_or_append_var "SIGNER_ACCOUNT_ADDRESS" "$new_signer_account_address" "$ENV_FILE_PATH"
update_or_append_var "SIGNER_ACCOUNT_PRIVATE_KEY" "$new_signer_account_private_key" "$ENV_FILE_PATH"
fi
read -p "Enter new SLOT_ID (current: ${current_slot_id:-\<not set\>}, press enter to skip): " new_slot_id
if [ -n "$new_slot_id" ]; then update_or_append_var "SLOT_ID" "$new_slot_id" "$ENV_FILE_PATH"; fi
read -p "Enter new SOURCE_RPC_URL (current: ${current_source_rpc_url:-\<not set\>}, press enter to skip): " new_source_rpc_url
if [ -n "$new_source_rpc_url" ]; then update_or_append_var "SOURCE_RPC_URL" "$new_source_rpc_url" "$ENV_FILE_PATH"; fi
read -p "Enter new TELEGRAM_CHAT_ID (press enter to skip): " new_telegram_chat_id
if [ -n "$new_telegram_chat_id" ]; then update_or_append_var "TELEGRAM_CHAT_ID" "$new_telegram_chat_id" "$ENV_FILE_PATH"; fi
fi
}
# Function to set default optional variables
set_default_optional_variables() {
local env_file="$1"
local optional_vars=(
"LOCAL_COLLECTOR_PORT:50051"
"MAX_STREAM_POOL_SIZE:2"
"STREAM_HEALTH_CHECK_TIMEOUT_MS:5000"
"STREAM_WRITE_TIMEOUT_MS:5000"
"MAX_WRITE_RETRIES:3"
"MAX_CONCURRENT_WRITES:4"
"TELEGRAM_MESSAGE_THREAD_ID:"
"GOSSIPSUB_SNAPSHOT_SUBMISSION_PREFIX:/powerloom/snapshot-submissions"
)
for var_def in "${optional_vars[@]}"; do
local var_name="${var_def%:*}"
local default_value="${var_def#*:}"
if ! grep -q "^${var_name}=" "$env_file"; then
echo "π $var_name not found in $env_file, setting to default value ${default_value} and adding to file."
update_or_append_var "$var_name" "$default_value" "$env_file"
fi
done
# Add DSV-specific configuration for devnet mode
if [ "$DEVNET_MODE" = "true" ]; then
echo "π§ Applying DSV devnet specific configuration..."
# Set DSV-specific gossipsub and rendezvous point for devnet
if ! grep -q "^GOSSIPSUB_SNAPSHOT_SUBMISSION_PREFIX=" "$env_file"; then
update_or_append_var "GOSSIPSUB_SNAPSHOT_SUBMISSION_PREFIX" "/powerloom/dsv-devnet-alpha/snapshot-submissions" "$env_file"
echo "π GOSSIPSUB_SNAPSHOT_SUBMISSION_PREFIX not found in $env_file, setting to DSV devnet default"
else
# Update existing GOSSIPSUB to DSV version for devnet
update_or_append_var "GOSSIPSUB_SNAPSHOT_SUBMISSION_PREFIX" "/powerloom/dsv-devnet-alpha/snapshot-submissions" "$env_file"
rm -f "$env_file.bak"
echo "π§ Updated GOSSIPSUB_SNAPSHOT_SUBMISSION_PREFIX for DSV devnet mode"
fi
if ! grep -q "^RENDEZVOUS_POINT=" "$env_file"; then
update_or_append_var "RENDEZVOUS_POINT" "powerloom-dsv-devnet-alpha" "$env_file"
echo "π RENDEZVOUS_POINT not found in $env_file, setting to DSV devnet default"
else
# Update existing RENDEZVOUS_POINT to DSV version for devnet
update_or_append_var "RENDEZVOUS_POINT" "powerloom-dsv-devnet-alpha" "$env_file"
rm -f "$env_file.bak"
echo "π§ Updated RENDEZVOUS_POINT for DSV devnet mode"
fi
# Configure P2P port for DSV devnet
if ! grep -q "^LOCAL_COLLECTOR_P2P_PORT=" "$env_file"; then
update_or_append_var "LOCAL_COLLECTOR_P2P_PORT" "8001" "$env_file"
echo "π LOCAL_COLLECTOR_P2P_PORT not found in $env_file, setting to DSV devnet default (8001)"
else
echo "β
LOCAL_COLLECTOR_P2P_PORT already configured in $env_file"
fi
# Handle P2P private key
if ! grep -q "^LOCAL_COLLECTOR_PRIVATE_KEY=" "$env_file"; then
echo "π LOCAL_COLLECTOR_PRIVATE_KEY will be generated by setup_operator_keys.sh"
echo " Run './scripts/setup_operator_keys.sh --env-file $env_file' to generate a key"
else
echo "β
LOCAL_COLLECTOR_PRIVATE_KEY already configured in $env_file"
fi
# Validate bootstrap node configuration for DSV devnet
if grep -q "^BOOTSTRAP_NODE_ADDRS=" "$env_file"; then
BOOTSTRAP_ADDRS=$(grep "^BOOTSTRAP_NODE_ADDRS=" "$env_file" | cut -d'=' -f2)
BOOTSTRAP_COUNT=$(echo "$BOOTSTRAP_ADDRS" | tr ',' '\n' | wc -l | tr -d ' ')
echo "β
BOOTSTRAP_NODE_ADDRS configured with $BOOTSTRAP_COUNT nodes: $BOOTSTRAP_ADDRS"
else
echo "β οΈ No bootstrap node configuration found in $env_file"
echo " This should have been auto-fetched from curated-datamarkets sources.json"
echo " Check that the selected data market has bootstrapNodes configured"
fi
# Remove backup files if they exist
rm -f "$env_file.bak"
echo "π DSV devnet P2P configuration completed!"
fi
# Add DSV-specific configuration for mainnet BDS mode
if [ "$BDS_DSV_MAINNET" = "true" ]; then
echo "π§ Applying DSV mainnet BDS specific configuration..."
# Set DSV-specific gossipsub and rendezvous point for mainnet
if ! grep -q "^GOSSIPSUB_SNAPSHOT_SUBMISSION_PREFIX=" "$env_file"; then
update_or_append_var "GOSSIPSUB_SNAPSHOT_SUBMISSION_PREFIX" "/powerloom/dsv-mainnet-bds/snapshot-submissions" "$env_file"
echo "π GOSSIPSUB_SNAPSHOT_SUBMISSION_PREFIX not found in $env_file, setting to DSV mainnet default"
else
update_or_append_var "GOSSIPSUB_SNAPSHOT_SUBMISSION_PREFIX" "/powerloom/dsv-mainnet-bds/snapshot-submissions" "$env_file"
rm -f "$env_file.bak"
echo "π§ Updated GOSSIPSUB_SNAPSHOT_SUBMISSION_PREFIX for DSV mainnet mode"
fi
if ! grep -q "^RENDEZVOUS_POINT=" "$env_file"; then
update_or_append_var "RENDEZVOUS_POINT" "powerloom-dsv-mainnet-bds" "$env_file"
echo "π RENDEZVOUS_POINT not found in $env_file, setting to DSV mainnet default"
else
update_or_append_var "RENDEZVOUS_POINT" "powerloom-dsv-mainnet-bds" "$env_file"
rm -f "$env_file.bak"
echo "π§ Updated RENDEZVOUS_POINT for DSV mainnet mode"
fi
# Configure P2P port for DSV mainnet
if ! grep -q "^LOCAL_COLLECTOR_P2P_PORT=" "$env_file"; then
update_or_append_var "LOCAL_COLLECTOR_P2P_PORT" "8001" "$env_file"
echo "π LOCAL_COLLECTOR_P2P_PORT not found in $env_file, setting to DSV mainnet default (8001)"
else
echo "β
LOCAL_COLLECTOR_P2P_PORT already configured in $env_file"
fi
# Handle P2P private key
if ! grep -q "^LOCAL_COLLECTOR_PRIVATE_KEY=" "$env_file"; then
echo "π LOCAL_COLLECTOR_PRIVATE_KEY will be generated by setup_operator_keys.sh"
echo " Run './scripts/setup_operator_keys.sh --env-file $env_file' to generate a key"
else
echo "β
LOCAL_COLLECTOR_PRIVATE_KEY already configured in $env_file"
fi
# Validate bootstrap node configuration for DSV mainnet
if grep -q "^BOOTSTRAP_NODE_ADDRS=" "$env_file"; then
BOOTSTRAP_ADDRS=$(grep "^BOOTSTRAP_NODE_ADDRS=" "$env_file" | cut -d'=' -f2)
BOOTSTRAP_COUNT=$(echo "$BOOTSTRAP_ADDRS" | tr ',' '\n' | wc -l | tr -d ' ')
echo "β
BOOTSTRAP_NODE_ADDRS configured with $BOOTSTRAP_COUNT nodes: $BOOTSTRAP_ADDRS"
else
echo "β οΈ No bootstrap node configuration found in $env_file"
echo " This should have been auto-fetched from curated-datamarkets sources.json"
echo " Check that the selected data market has bootstrapNodes configured"
fi
# Set CENTRALIZED_SEQUENCER_ENABLED to false for BDS mainnet (mesh-only mode)
# This preserves any value from deployment.py if set, otherwise defaults to false
if ! grep -q "^CENTRALIZED_SEQUENCER_ENABLED=" "$env_file"; then
update_or_append_var "CENTRALIZED_SEQUENCER_ENABLED" "false" "$env_file"
echo "π CENTRALIZED_SEQUENCER_ENABLED not found in $env_file, setting to false for mesh-only mode"
else
# Preserve existing value (from deployment.py/profile env)
echo "β
CENTRALIZED_SEQUENCER_ENABLED already configured in $env_file"
fi
# Remove backup files if they exist
rm -f "$env_file.bak"
echo "π DSV mainnet P2P configuration completed!"
fi
}
# Main execution flow
main() {
parse_arguments "$@"
if [ "$DOCKER_MODE" != "true" ] && ! docker info >/dev/null 2>&1; then
echo "β Docker daemon is not running"; exit 1; fi
initialize_default_config
if [ "$OVERRIDE_DEFAULTS_SCRIPT_FLAG" = "true" ]; then
echo "OVERRIDE"
# This path is not fully implemented for interactive override based on new structure
else
select_market_and_configure "$DEVNET_MODE"
export FULL_NAMESPACE="${POWERLOOM_CHAIN}-${NAMESPACE}-${SOURCE_CHAIN}"
ENV_FILE_PATH=".env-${FULL_NAMESPACE}"
if [ -f "$ENV_FILE_PATH" ]; then
echo "π’ Found existing environment file: $ENV_FILE_PATH"
echo "π§ Checking for and fixing known formatting issues in $ENV_FILE_PATH..."
# Removed problematic sed commands that may cause corruption in Alpine environment
echo "π Updating file with latest configuration from selected market."
# Source existing file to not lose credentials
source "$ENV_FILE_PATH"
update_common_config "$ENV_FILE_PATH"
FILE_WAS_NEWLY_CREATED=false
else
echo "π‘ $ENV_FILE_PATH file not found, creating one..."
cp env.example "$ENV_FILE_PATH"
CLEANUP_ENV_FILE_ON_ABORT=true
TARGET_ENV_FILE_FOR_CLEANUP="$ENV_FILE_PATH"
export CONNECTION_REFRESH_INTERVAL_SEC="${DEFAULT_CONNECTION_REFRESH_INTERVAL_SEC}"
export TELEGRAM_NOTIFICATION_COOLDOWN="${DEFAULT_TELEGRAM_NOTIFICATION_COOLDOWN}"
update_common_config "$ENV_FILE_PATH"
prompt_for_credentials "$ENV_FILE_PATH"
FILE_WAS_NEWLY_CREATED=true
# Set OVERRIDE_DEFAULTS=true for BDS DSV Devnet mode
if [ "$BDS_DSV_DEVNET" = "true" ]; then
update_or_append_var "OVERRIDE_DEFAULTS" "true" "$ENV_FILE_PATH"
update_or_append_var "DEV_MODE" "true" "$ENV_FILE_PATH"
update_or_append_var "LOCAL_COLLECTOR_P2P_PORT" "8001" "$ENV_FILE_PATH"
elif [ "$BDS_DSV_MAINNET" = "true" ]; then
update_or_append_var "OVERRIDE_DEFAULTS" "true" "$ENV_FILE_PATH"
update_or_append_var "DEV_MODE" "true" "$ENV_FILE_PATH"
update_or_append_var "LOCAL_COLLECTOR_P2P_PORT" "8001" "$ENV_FILE_PATH"
else
update_or_append_var "OVERRIDE_DEFAULTS" "false" "$ENV_FILE_PATH"
fi
echo "π’ $ENV_FILE_PATH file created successfully."
fi
fi
handle_credential_updates
if [ ! -f "$ENV_FILE_PATH" ]; then echo "β Env file not found."; exit 1; fi
source "$ENV_FILE_PATH"
set_default_optional_variables "$ENV_FILE_PATH"
SETUP_COMPLETE=true
# Commit pins are optional: init_docker.sh uses branch tip when *_COMMIT is unset
local required_vars=("POWERLOOM_RPC_URL" "SOURCE_RPC_URL" "SIGNER_ACCOUNT_ADDRESS" "SIGNER_ACCOUNT_PRIVATE_KEY" "SLOT_ID" "DATA_MARKET_CONTRACT" "PROTOCOL_STATE_CONTRACT" "SNAPSHOT_CONFIG_REPO" "SNAPSHOT_CONFIG_REPO_BRANCH" "SNAPSHOTTER_COMPUTE_REPO" "SNAPSHOTTER_COMPUTE_REPO_BRANCH" "FULL_NAMESPACE")
for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then
echo "β $var not found after configuration, please set this in your $ENV_FILE_PATH!"
SETUP_COMPLETE=false
fi
done
if [ "$SETUP_COMPLETE" = true ]; then
# Clean up any remaining backup files
find "$(dirname "$ENV_FILE_PATH")" -name "*.backup" -type f -delete 2>/dev/null || true
echo "β
Configuration complete. Environment file ready at $ENV_FILE_PATH"
# Note: P2P private key will be generated after setup container completes
if ! grep -q "^[[:space:]]*LOCAL_COLLECTOR_PRIVATE_KEY=" "$ENV_FILE_PATH"; then
echo "βΉοΈ LOCAL_COLLECTOR_PRIVATE_KEY not set - will be generated after setup completes"
echo " Key generation will run on host system with Docker access"
else
echo "β
P2P private key already exists in environment file"
fi
if [ -n "$RESULT_FILE" ]; then echo "$ENV_FILE_PATH" > "$RESULT_FILE"; fi
else
echo "β Configuration incomplete."; exit 1;
fi
}
main "$@"