-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdeploy_mainnet.sh
More file actions
executable file
·778 lines (678 loc) · 26.4 KB
/
Copy pathdeploy_mainnet.sh
File metadata and controls
executable file
·778 lines (678 loc) · 26.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
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
#!/bin/bash
#
# Boundless mainnet deploy script.
#
# This is a separate script from deploy_and_upgrade.sh because mainnet deploys
# need stronger guards (explicit confirmation, env var validation, paired
# deploy of profile + events, multi-sig rotation flow) than the testnet
# scripted path can carry safely.
#
# Companion: boundless-contract/docs/mainnet-deploy-runbook.md
#
# Usage:
# ./deploy_mainnet.sh deploy-profile
# ./deploy_mainnet.sh deploy-events
# ./deploy_mainnet.sh register-token <token-sac-address>
# ./deploy_mainnet.sh rotate-admin <new-multisig-address>
# ./deploy_mainnet.sh upload-events-wasm <wasm-path>
# ./deploy_mainnet.sh prepare-pause-events <xdr-output>
# ./deploy_mainnet.sh prepare-propose-upgrade-events <wasm-path> <new-version> <xdr-output>
# ./deploy_mainnet.sh verify-proposed-upgrade-events <wasm-path> <new-version>
# ./deploy_mainnet.sh prepare-apply-upgrade-events <wasm-path> <expected-version> <xdr-output>
# ./deploy_mainnet.sh prepare-migrate-upgrade-events <expected-version> <xdr-output>
# ./deploy_mainnet.sh prepare-cancel-upgrade-events <xdr-output>
# ./deploy_mainnet.sh prepare-unpause-events <expected-version> <xdr-output>
# ./deploy_mainnet.sh verify-upgrade-events <wasm-path> <expected-version>
# ./deploy_mainnet.sh upgrade-status
# ./deploy_mainnet.sh verify
set -euo pipefail
ACTION="${1:-}"
shift || true
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BOLD='\033[1m'
NC='\033[0m'
NETWORK="${STELLAR_NETWORK:-}"
NETWORK_PASSPHRASE="Public Global Stellar Network ; September 2015"
DEPLOYMENTS_DIR="$(cd "$(dirname "$0")" && pwd)/deployments"
DEPLOYMENT_FILE="$DEPLOYMENTS_DIR/mainnet.json"
err() {
echo -e "${RED}error: $*${NC}" >&2
exit 1
}
info() {
echo -e "${YELLOW}$*${NC}"
}
ok() {
echo -e "${GREEN}$*${NC}"
}
confirm_mainnet() {
echo -e "${BOLD}You are about to operate on Stellar MAINNET.${NC}"
echo "CLI network config: $NETWORK"
echo "Network passphrase: $NETWORK_PASSPHRASE"
echo ""
read -rp "Type 'mainnet' to continue: " ack
if [ "$ack" != "mainnet" ]; then
err "aborted"
fi
}
require_env() {
local var="$1"
if [ -z "${!var:-}" ]; then
err "missing required env var: $var"
fi
}
require_cli() {
[ -n "$NETWORK" ] || \
err "set STELLAR_NETWORK to the configured mainnet network name"
command -v stellar >/dev/null 2>&1 || err "stellar CLI not on PATH"
command -v jq >/dev/null 2>&1 || err "jq not on PATH"
command -v awk >/dev/null 2>&1 || err "awk not on PATH"
command -v sha256sum >/dev/null 2>&1 || command -v shasum >/dev/null 2>&1 || \
err "sha256sum or shasum not on PATH"
if [ -n "${STELLAR_RPC_URL:-}" ] || [ -n "${STELLAR_NETWORK_PASSPHRASE:-}" ]; then
err "unset STELLAR_RPC_URL and STELLAR_NETWORK_PASSPHRASE when using --network $NETWORK"
fi
local networks configured_rpc configured_passphrase
networks="$(stellar network ls --long)"
configured_rpc="$(printf '%s\n' "$networks" | awk -v target="$NETWORK" '
$0 == "Name: " target { found = 1; next }
found && /^RPC url: / { sub(/^RPC url: /, ""); print; exit }
found && /^Name: / { exit }
')"
configured_passphrase="$(printf '%s\n' "$networks" | awk -v target="$NETWORK" '
$0 == "Name: " target { found = 1; next }
found && /^Network passphrase: / {
sub(/^Network passphrase: /, ""); print; exit
}
found && /^Name: / { exit }
')"
[ "$configured_passphrase" = "$NETWORK_PASSPHRASE" ] || \
err "network $NETWORK is not configured with the Stellar public-network passphrase"
[ -n "$configured_rpc" ] && [[ "$configured_rpc" != "Bring Your Own:"* ]] || \
err "network $NETWORK does not have a usable RPC URL"
}
hash_wasm() {
local f="$1"
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$f" | cut -d' ' -f1
else
shasum -a 256 "$f" | cut -d' ' -f1
fi
}
ensure_deployments_dir() {
mkdir -p "$DEPLOYMENTS_DIR"
if [ ! -f "$DEPLOYMENT_FILE" ]; then
echo "{\"network\":\"$NETWORK\",\"passphrase\":\"$NETWORK_PASSPHRASE\"}" > "$DEPLOYMENT_FILE"
fi
}
deployment_get() {
local key="$1"
jq -r ".\"$key\" // empty" "$DEPLOYMENT_FILE"
}
deployment_set() {
local key="$1" val="$2"
local tmp
tmp="$(mktemp)"
jq ".\"$key\" = \"$val\"" "$DEPLOYMENT_FILE" > "$tmp"
mv "$tmp" "$DEPLOYMENT_FILE"
}
deployment_set_raw() {
local key="$1" val="$2"
local tmp
tmp="$(mktemp)"
jq ".\"$key\" = $val" "$DEPLOYMENT_FILE" > "$tmp"
mv "$tmp" "$DEPLOYMENT_FILE"
}
events_contract_id() {
local events_id="${EVENTS_ID:-}"
if [ -z "$events_id" ] && [ -f "$DEPLOYMENT_FILE" ]; then
events_id="$(deployment_get events_contract)"
fi
[ -n "$events_id" ] || \
err "events contract id missing; set EVENTS_ID or restore deployments/mainnet.json"
printf '%s\n' "$events_id"
}
events_read() {
local events_id="$1"
shift
stellar contract invoke \
--send no \
--network "$NETWORK" \
--source "$ADMIN_SOURCE" \
--id "$events_id" \
-- "$@"
}
read_json_string() {
jq -er 'if type == "string" then . else error("expected JSON string") end'
}
assert_events_version() {
local events_id="$1" expected="$2"
local actual
actual="$(events_read "$events_id" version | read_json_string)" || \
err "could not read the current events contract version"
[ "$actual" = "$expected" ] || \
err "events version mismatch: expected $expected, got $actual"
}
assert_admin_source_matches() {
local events_id="$1"
local actual_admin
actual_admin="$(events_read "$events_id" get_admin | read_json_string)" || \
err "could not read the current events admin"
[ "$actual_admin" = "$ADMIN_SOURCE" ] || \
err "ADMIN_SOURCE mismatch: contract admin is $actual_admin, got $ADMIN_SOURCE"
}
assert_events_paused() {
local events_id="$1"
local paused
paused="$(events_read "$events_id" is_paused)" || \
err "could not read events.is_paused"
[ "$paused" = "true" ] || \
err "events contract is not paused; refusing to rely on a zero-event snapshot"
}
assert_events_unpaused() {
local events_id="$1"
local paused
paused="$(events_read "$events_id" is_paused)" || \
err "could not read events.is_paused"
[ "$paused" = "false" ] || \
err "events contract is still paused after unpause"
}
assert_no_pending_events_upgrade() {
local events_id="$1"
local pending
pending="$(events_read "$events_id" get_pending_upgrade)" || \
err "could not read pending events upgrade"
[ "$pending" = "null" ] || \
err "an events upgrade is already pending; inspect it with upgrade-status"
}
assert_pending_events_upgrade() {
local events_id="$1" expected_version="$2" expected_hash="$3"
local pending actual_version actual_hash
pending="$(events_read "$events_id" get_pending_upgrade)" || \
err "could not read pending events upgrade"
[ "$pending" != "null" ] || err "no events upgrade is pending"
actual_version="$(printf '%s' "$pending" | jq -er '.new_version')" || \
err "pending events upgrade did not contain new_version"
actual_hash="$(printf '%s' "$pending" | jq -er '.wasm_hash')" || \
err "pending events upgrade did not contain wasm_hash"
[ "$actual_version" = "$expected_version" ] || \
err "pending version mismatch: expected $expected_version, got $actual_version"
[ "$actual_hash" = "$expected_hash" ] || \
err "pending wasm mismatch: expected $expected_hash, got $actual_hash"
}
assert_no_events_exist() {
local events_id="$1"
local base first_event_id output status
base="$(events_read "$events_id" id_base)" || \
err "could not read events.id_base"
[[ "$base" =~ ^[0-9]+$ ]] || \
err "events.id_base returned a non-numeric value: $base"
first_event_id=$((base + 1))
set +e
output="$(events_read "$events_id" get_event --event_id "$first_event_id" 2>&1)"
status=$?
set -e
if [ "$status" -eq 0 ]; then
err "event $first_event_id exists; abort 1.2.0 and implement a legacy-total migration"
fi
if [[ "$output" != *"Error(Contract, #30)"* ]]; then
err "could not prove event $first_event_id is absent; refusing to continue: $output"
fi
ok "Zero-event guard confirmed: $first_event_id returned EventNotFound."
}
assert_migrated_version() {
local events_id="$1" expected="$2"
local migrated_version
migrated_version="$(events_read "$events_id" get_migrated_to_version | read_json_string)" || \
err "could not read migrated-to version"
[ "$migrated_version" = "$expected" ] || \
err "migration marker mismatch: expected $expected, got $migrated_version"
}
prepare_events_invoke() {
local events_id="$1" output_file="$2"
shift 2
local output_dir
output_dir="$(dirname "$output_file")"
[ -d "$output_dir" ] || err "XDR output directory does not exist: $output_dir"
assert_admin_source_matches "$events_id"
local tmp
tmp="$(mktemp)"
if ! stellar contract invoke \
--network "$NETWORK" \
--source-account "$ADMIN_SOURCE" \
--id "$events_id" \
--build-only \
-- "$@" \
| stellar tx simulate \
--network "$NETWORK" \
--source-account "$ADMIN_SOURCE" \
> "$tmp"; then
rm -f "$tmp"
err "could not build and simulate the multisig transaction"
fi
[ -s "$tmp" ] || {
rm -f "$tmp"
err "prepared transaction was empty"
}
mv "$tmp" "$output_file"
ok "Prepared simulated XDR: $output_file"
echo "Sign it sequentially with the required multi-sig quorum, then submit with:"
echo " stellar tx send \"<signed-xdr>\" --network $NETWORK"
}
append_upgrade_log() {
local action="$1" version="$2" wasm_hash="${3:-}"
local upgrades_log="$DEPLOYMENTS_DIR/mainnet-upgrades.jsonl"
jq -nc \
--arg timestamp "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
--arg action "$action" \
--arg version "$version" \
--arg wasm_hash "$wasm_hash" \
'{timestamp: $timestamp, action: $action, version: $version, wasm_hash: $wasm_hash}' \
>> "$upgrades_log"
}
cmd_build_release() {
info "Building contracts in release mode..."
stellar contract build
ok "Build complete."
}
cmd_deploy_profile() {
require_env INITIAL_ADMIN_KEY
require_cli
confirm_mainnet
ensure_deployments_dir
if [ -n "$(deployment_get profile_contract)" ]; then
err "profile contract already deployed: $(deployment_get profile_contract). Refusing to re-deploy."
fi
cmd_build_release
local wasm="target/wasm32v1-none/release/boundless_profile.wasm"
[ -f "$wasm" ] || err "missing wasm: $wasm"
info "Deploying boundless-profile..."
local profile_id
profile_id=$(stellar contract deploy \
--network "$NETWORK" \
--source "$INITIAL_ADMIN_KEY" \
--wasm "$wasm" \
-- \
--admin "$(stellar keys address "$INITIAL_ADMIN_KEY")")
ok "profile_contract=$profile_id"
deployment_set profile_contract "$profile_id"
deployment_set profile_wasm_hash "$(hash_wasm "$wasm")"
deployment_set deployed_at "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
}
cmd_deploy_events() {
require_env INITIAL_ADMIN_KEY
require_env FEE_ACCOUNT
require_env INITIAL_GLOBAL_FEE_BPS
require_cli
confirm_mainnet
ensure_deployments_dir
local profile_id
profile_id="$(deployment_get profile_contract)"
[ -n "$profile_id" ] || err "deploy-profile first; profile_contract not set"
if [ -n "$(deployment_get events_contract)" ]; then
err "events contract already deployed: $(deployment_get events_contract). Refusing to re-deploy. Use the guarded upgrade flow in the mainnet runbook."
fi
cmd_build_release
local wasm="target/wasm32v1-none/release/boundless_events.wasm"
[ -f "$wasm" ] || err "missing wasm: $wasm"
info "Deploying boundless-events..."
local events_id
events_id=$(stellar contract deploy \
--network "$NETWORK" \
--source "$INITIAL_ADMIN_KEY" \
--wasm "$wasm" \
-- \
--admin "$(stellar keys address "$INITIAL_ADMIN_KEY")" \
--fee_account "$FEE_ACCOUNT" \
--fee_bps "$INITIAL_GLOBAL_FEE_BPS" \
--profile_contract "$profile_id")
ok "events_contract=$events_id"
deployment_set events_contract "$events_id"
deployment_set events_wasm_hash "$(hash_wasm "$wasm")"
deployment_set fee_account "$FEE_ACCOUNT"
deployment_set_raw initial_fee_bps "$INITIAL_GLOBAL_FEE_BPS"
info "Wiring profile -> events..."
stellar contract invoke \
--network "$NETWORK" \
--source "$INITIAL_ADMIN_KEY" \
--id "$profile_id" \
-- set_events_contract \
--new_addr "$events_id"
ok "profile.events_contract = $events_id"
}
cmd_register_token() {
local token="${1:-}"
[ -n "$token" ] || err "usage: register-token <token-sac-address>"
require_env INITIAL_ADMIN_KEY
require_cli
confirm_mainnet
local events_id
events_id="$(deployment_get events_contract)"
[ -n "$events_id" ] || err "events contract not deployed"
info "Registering token $token..."
stellar contract invoke \
--network "$NETWORK" \
--source "$INITIAL_ADMIN_KEY" \
--id "$events_id" \
-- register_supported_token \
--token "$token"
local supported
supported=$(stellar contract invoke \
--network "$NETWORK" \
--source "$INITIAL_ADMIN_KEY" \
--id "$events_id" \
-- is_supported_token \
--token "$token")
[ "$supported" = "true" ] || err "token registration not confirmed; got: $supported"
ok "Token registered: $token"
# Append to registered_tokens array.
local tmp
tmp="$(mktemp)"
jq ".registered_tokens = ((.registered_tokens // []) + [\"$token\"] | unique)" \
"$DEPLOYMENT_FILE" > "$tmp"
mv "$tmp" "$DEPLOYMENT_FILE"
}
cmd_rotate_admin() {
local new_admin="${1:-}"
[ -n "$new_admin" ] || err "usage: rotate-admin <new-multisig-address>"
require_env INITIAL_ADMIN_KEY
require_cli
confirm_mainnet
local events_id profile_id
events_id="$(deployment_get events_contract)"
profile_id="$(deployment_get profile_contract)"
[ -n "$events_id" ] || err "events contract not deployed"
[ -n "$profile_id" ] || err "profile contract not deployed"
info "Setting pending admin on events..."
stellar contract invoke \
--network "$NETWORK" --source "$INITIAL_ADMIN_KEY" --id "$events_id" \
-- set_admin --new_admin "$new_admin"
info "Setting pending admin on profile..."
stellar contract invoke \
--network "$NETWORK" --source "$INITIAL_ADMIN_KEY" --id "$profile_id" \
-- set_admin --new_admin "$new_admin"
echo ""
echo -e "${BOLD}Now the new admin ($new_admin) must call accept_admin on both contracts.${NC}"
echo "This step requires the multi-sig quorum to sign. After both accept ops"
echo "land, run 'verify' to confirm get_admin returns the new address."
}
cmd_upload_events_wasm() {
local wasm="${1:-}"
[ -n "$wasm" ] || err "usage: upload-events-wasm <path-to-new-wasm>"
[ -f "$wasm" ] || err "wasm file not found: $wasm"
require_env UPLOAD_SOURCE
require_env ADMIN_SOURCE
require_cli
confirm_mainnet
ensure_deployments_dir
local events_id expected_hash uploaded_hash
events_id="$(events_contract_id)"
assert_events_version "$events_id" "1.1.0"
assert_admin_source_matches "$events_id"
expected_hash="$(hash_wasm "$wasm")"
uploaded_hash="$(stellar contract upload \
--source-account "$UPLOAD_SOURCE" \
--network "$NETWORK" \
--optimize=false \
--wasm "$wasm")"
[ "$uploaded_hash" = "$expected_hash" ] || \
err "uploaded wasm hash mismatch: expected $expected_hash, got $uploaded_hash"
append_upgrade_log "uploaded" "" "$uploaded_hash"
ok "Uploaded exact events WASM: $uploaded_hash"
}
cmd_prepare_pause_events() {
local output_file="${1:-}"
[ -n "$output_file" ] || err "usage: prepare-pause-events <xdr-output>"
require_env ADMIN_SOURCE
require_cli
local events_id
events_id="$(events_contract_id)"
assert_events_version "$events_id" "1.1.0"
assert_events_unpaused "$events_id"
assert_no_pending_events_upgrade "$events_id"
assert_no_events_exist "$events_id"
prepare_events_invoke "$events_id" "$output_file" pause
}
cmd_prepare_propose_upgrade_events() {
local wasm="${1:-}" new_version="${2:-}" output_file="${3:-}"
[ -n "$wasm" ] || \
err "usage: prepare-propose-upgrade-events <path-to-new-wasm> <new-version> <xdr-output>"
[ -n "$new_version" ] || \
err "usage: prepare-propose-upgrade-events <path-to-new-wasm> <new-version> <xdr-output>"
[ -n "$output_file" ] || \
err "usage: prepare-propose-upgrade-events <path-to-new-wasm> <new-version> <xdr-output>"
[ -f "$wasm" ] || err "wasm file not found: $wasm"
require_env ADMIN_SOURCE
require_cli
local events_id wasm_hash
events_id="$(events_contract_id)"
wasm_hash="$(hash_wasm "$wasm")"
assert_no_pending_events_upgrade "$events_id"
if [ "$new_version" = "1.2.0" ]; then
assert_events_version "$events_id" "1.1.0"
assert_events_paused "$events_id"
assert_no_events_exist "$events_id"
fi
prepare_events_invoke "$events_id" "$output_file" propose_upgrade \
--new_wasm_hash "$wasm_hash" \
--new_version "$new_version"
echo "Expected version: $new_version"
echo "Expected WASM: $wasm_hash"
}
cmd_verify_proposed_upgrade_events() {
local wasm="${1:-}" expected_version="${2:-}"
[ -n "$wasm" ] || \
err "usage: verify-proposed-upgrade-events <path-to-new-wasm> <expected-version>"
[ -n "$expected_version" ] || \
err "usage: verify-proposed-upgrade-events <path-to-new-wasm> <expected-version>"
[ -f "$wasm" ] || err "wasm file not found: $wasm"
require_env ADMIN_SOURCE
require_cli
ensure_deployments_dir
local events_id expected_hash
events_id="$(events_contract_id)"
expected_hash="$(hash_wasm "$wasm")"
assert_pending_events_upgrade "$events_id" "$expected_version" "$expected_hash"
if [ "$expected_version" = "1.2.0" ]; then
assert_events_version "$events_id" "1.1.0"
assert_events_paused "$events_id"
assert_no_events_exist "$events_id"
fi
append_upgrade_log "proposal-verified" "$expected_version" "$expected_hash"
ok "Queued events upgrade matches the expected version and exact WASM."
}
cmd_prepare_apply_upgrade_events() {
local wasm="${1:-}" expected_version="${2:-}" output_file="${3:-}"
[ -n "$wasm" ] || \
err "usage: prepare-apply-upgrade-events <path-to-new-wasm> <expected-version> <xdr-output>"
[ -n "$expected_version" ] || \
err "usage: prepare-apply-upgrade-events <path-to-new-wasm> <expected-version> <xdr-output>"
[ -n "$output_file" ] || \
err "usage: prepare-apply-upgrade-events <path-to-new-wasm> <expected-version> <xdr-output>"
[ -f "$wasm" ] || err "wasm file not found: $wasm"
require_env ADMIN_SOURCE
require_cli
local events_id expected_hash
events_id="$(events_contract_id)"
expected_hash="$(hash_wasm "$wasm")"
assert_pending_events_upgrade "$events_id" "$expected_version" "$expected_hash"
if [ "$expected_version" = "1.2.0" ]; then
assert_events_version "$events_id" "1.1.0"
assert_events_paused "$events_id"
assert_no_events_exist "$events_id"
fi
prepare_events_invoke "$events_id" "$output_file" apply_upgrade
}
cmd_prepare_migrate_upgrade_events() {
local expected_version="${1:-}" output_file="${2:-}"
[ -n "$expected_version" ] || \
err "usage: prepare-migrate-upgrade-events <expected-version> <xdr-output>"
[ -n "$output_file" ] || \
err "usage: prepare-migrate-upgrade-events <expected-version> <xdr-output>"
require_env ADMIN_SOURCE
require_cli
local events_id migrated_version
events_id="$(events_contract_id)"
assert_events_version "$events_id" "$expected_version"
assert_no_pending_events_upgrade "$events_id"
if [ "$expected_version" = "1.2.0" ]; then
assert_events_paused "$events_id"
fi
migrated_version="$(events_read "$events_id" get_migrated_to_version | jq -r '. // empty')" || \
err "could not read migrated-to version"
[ "$migrated_version" != "$expected_version" ] || \
err "migration marker is already $expected_version; do not replay migrate"
prepare_events_invoke "$events_id" "$output_file" migrate
}
cmd_prepare_cancel_upgrade_events() {
local output_file="${1:-}"
[ -n "$output_file" ] || \
err "usage: prepare-cancel-upgrade-events <xdr-output>"
require_env ADMIN_SOURCE
require_cli
local events_id pending_version
events_id="$(events_contract_id)"
pending_version="$(events_read "$events_id" get_pending_upgrade | jq -r '.new_version // empty')" || \
err "could not read pending events upgrade"
[ -n "$pending_version" ] || err "no events upgrade is pending"
prepare_events_invoke "$events_id" "$output_file" cancel_pending_upgrade
echo "Cancelling queued version: $pending_version"
echo "This transaction does not unpause the contract."
}
cmd_prepare_unpause_events() {
local expected_version="${1:-}" output_file="${2:-}"
[ -n "$expected_version" ] || \
err "usage: prepare-unpause-events <expected-version> <xdr-output>"
[ -n "$output_file" ] || \
err "usage: prepare-unpause-events <expected-version> <xdr-output>"
require_env ADMIN_SOURCE
require_cli
local events_id
events_id="$(events_contract_id)"
assert_events_version "$events_id" "$expected_version"
assert_events_paused "$events_id"
assert_no_pending_events_upgrade "$events_id"
if [ "$expected_version" = "1.2.0" ]; then
assert_migrated_version "$events_id" "$expected_version"
fi
prepare_events_invoke "$events_id" "$output_file" unpause
}
cmd_verify_upgrade_events() {
local wasm="${1:-}" expected_version="${2:-}"
[ -n "$wasm" ] || \
err "usage: verify-upgrade-events <path-to-new-wasm> <expected-version>"
[ -n "$expected_version" ] || \
err "usage: verify-upgrade-events <path-to-new-wasm> <expected-version>"
[ -f "$wasm" ] || err "wasm file not found: $wasm"
require_env ADMIN_SOURCE
require_cli
ensure_deployments_dir
local events_id expected_hash
events_id="$(events_contract_id)"
expected_hash="$(hash_wasm "$wasm")"
assert_events_version "$events_id" "$expected_version"
assert_migrated_version "$events_id" "$expected_version"
assert_no_pending_events_upgrade "$events_id"
assert_events_unpaused "$events_id"
deployment_set events_contract "$events_id"
deployment_set events_wasm_hash "$expected_hash"
deployment_set events_version "$expected_version"
append_upgrade_log "upgrade-verified" "$expected_version" "$expected_hash"
ok "Events $expected_version is migrated, has no pending upgrade, and is unpaused."
}
cmd_upgrade_status() {
require_env ADMIN_SOURCE
require_cli
local events_id
events_id="$(events_contract_id)"
echo "events.version:"
events_read "$events_id" version
echo "events.is_paused:"
events_read "$events_id" is_paused
echo "events.get_pending_upgrade:"
events_read "$events_id" get_pending_upgrade
echo "events.get_migrated_to_version:"
events_read "$events_id" get_migrated_to_version
}
cmd_verify() {
require_env INITIAL_ADMIN_KEY # for the read-source; any key works for invoke-as-read
require_cli
local events_id profile_id
events_id="$(deployment_get events_contract)"
profile_id="$(deployment_get profile_contract)"
[ -n "$events_id" ] || err "events contract not in deployment file"
[ -n "$profile_id" ] || err "profile contract not in deployment file"
info "Verifying mainnet state..."
echo "events.get_admin:"
stellar contract invoke --network "$NETWORK" --source "$INITIAL_ADMIN_KEY" --id "$events_id" -- get_admin
echo "events.get_fee_bps:"
stellar contract invoke --network "$NETWORK" --source "$INITIAL_ADMIN_KEY" --id "$events_id" -- get_fee_bps
echo "events.get_fee_account:"
stellar contract invoke --network "$NETWORK" --source "$INITIAL_ADMIN_KEY" --id "$events_id" -- get_fee_account
echo "events.is_paused:"
stellar contract invoke --network "$NETWORK" --source "$INITIAL_ADMIN_KEY" --id "$events_id" -- is_paused
echo "profile.get_admin:"
stellar contract invoke --network "$NETWORK" --source "$INITIAL_ADMIN_KEY" --id "$profile_id" -- get_admin
ok "Verification reads complete. Compare against the runbook expectations."
}
case "$ACTION" in
"deploy-profile")
cmd_deploy_profile
;;
"deploy-events")
cmd_deploy_events
;;
"register-token")
cmd_register_token "$@"
;;
"rotate-admin")
cmd_rotate_admin "$@"
;;
"upload-events-wasm")
cmd_upload_events_wasm "$@"
;;
"prepare-pause-events")
cmd_prepare_pause_events "$@"
;;
"prepare-propose-upgrade-events")
cmd_prepare_propose_upgrade_events "$@"
;;
"verify-proposed-upgrade-events")
cmd_verify_proposed_upgrade_events "$@"
;;
"prepare-apply-upgrade-events")
cmd_prepare_apply_upgrade_events "$@"
;;
"prepare-migrate-upgrade-events")
cmd_prepare_migrate_upgrade_events "$@"
;;
"prepare-cancel-upgrade-events")
cmd_prepare_cancel_upgrade_events "$@"
;;
"prepare-unpause-events")
cmd_prepare_unpause_events "$@"
;;
"verify-upgrade-events")
cmd_verify_upgrade_events "$@"
;;
"upgrade-status")
cmd_upgrade_status
;;
"upgrade-events")
err "upgrade-events is removed; use the guarded prepare/sign/send flow in mainnet-deploy-runbook.md"
;;
"propose-upgrade-events"|"apply-upgrade-events"|"migrate-upgrade-events"|"cancel-upgrade-events"|"unpause-events")
err "$ACTION cannot safely submit as the 2-of-3 admin; use the matching prepare-* action, sign sequentially, and send"
;;
"verify")
cmd_verify
;;
"")
err "no action specified. See mainnet-deploy-runbook.md for usage."
;;
*)
err "unknown action: $ACTION. See mainnet-deploy-runbook.md for usage."
;;
esac