|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# diff-reward-weights.sh |
| 4 | +# |
| 5 | +# Compare the reward weights in the approved-sv-id-values.yaml file with the weights from the DSO API |
| 6 | + |
| 7 | +set -euo pipefail |
| 8 | + |
| 9 | +CURL_TIMEOUT=5 |
| 10 | +CURL_CMD=(curl -fsS -m "$CURL_TIMEOUT") |
| 11 | + |
| 12 | +ENVS_AND_SCAN_URLS=( |
| 13 | + DevNet https://scan.sv-1.dev.global.canton.network.sync.global |
| 14 | + TestNet https://scan.sv-1.test.global.canton.network.sync.global |
| 15 | + MainNet https://scan.sv-1.global.canton.network.sync.global |
| 16 | +) |
| 17 | + |
| 18 | +SCRIPTS_DIR=$(dirname "$0") |
| 19 | + |
| 20 | +[[ -t 1 ]] && OUTPUT_IS_TERMINAL=true || OUTPUT_IS_TERMINAL=false |
| 21 | + |
| 22 | +usage() { |
| 23 | + echo "Usage: $(basename "$0") [-q]" |
| 24 | + echo |
| 25 | + echo "Options:" |
| 26 | + echo " -q Quiet mode. Don't show the diff." |
| 27 | +} |
| 28 | + |
| 29 | +config_diff() { |
| 30 | + local config_file=$1 |
| 31 | + local scan_url=$2 |
| 32 | + |
| 33 | + local configs_dir="$SCRIPTS_DIR/../configs" |
| 34 | + local dso_url="$scan_url/api/scan/v0/dso" |
| 35 | + |
| 36 | + echo "$config_file -> $dso_url" |
| 37 | + |
| 38 | + diff_errors=() |
| 39 | + |
| 40 | + local dso_data; dso_data=$( |
| 41 | + "${CURL_CMD[@]}" "$dso_url" |
| 42 | + ) || diff_errors+=("ERROR: Unable to fetch DSO from $dso_url") |
| 43 | + |
| 44 | + local weights_from_file; weights_from_file=$( |
| 45 | + yq -eo json . "$configs_dir/$config_file" | jq -eS '[.approvedSvIdentities[] | {(.name): .rewardWeightBps}] | add' |
| 46 | + ) || diff_errors+=("ERROR: Unable to read and parse weights from $config_file") |
| 47 | + |
| 48 | + local weights_from_url; weights_from_url=$( |
| 49 | + "${CURL_CMD[@]}" "$dso_url" | jq -eS '[.dso_rules.contract.payload.svs[][1] | {(.name): .svRewardWeight | tonumber}] | add' |
| 50 | + ) || diff_errors+=("ERROR: Unable to fetch and parse weights from $dso_url") |
| 51 | + |
| 52 | + if [[ ${#diff_errors[@]} -eq 0 ]]; then |
| 53 | + local diff_options=() |
| 54 | + |
| 55 | + if [[ $OUTPUT_IS_TERMINAL == true ]]; then |
| 56 | + diff_options+=("--color=always") |
| 57 | + fi |
| 58 | + |
| 59 | + if [[ ${QUIET-} == true ]]; then |
| 60 | + diff_options+=("--brief") |
| 61 | + fi |
| 62 | + |
| 63 | + local return_code |
| 64 | + diff_result=$(diff -su "${diff_options[@]}" --label "$config_file" --label "$dso_url" <(echo "$weights_from_file") <(echo "$weights_from_url")) && return_code=$? || return_code=$? |
| 65 | + echo "$diff_result" |
| 66 | + |
| 67 | + return "$return_code" |
| 68 | + else |
| 69 | + for error in "${diff_errors[@]}"; do |
| 70 | + echo "$error" >&2 |
| 71 | + done |
| 72 | + |
| 73 | + return 1 |
| 74 | + fi |
| 75 | +} |
| 76 | + |
| 77 | +compare() { |
| 78 | + local envs_and_scan_urls=("${ENVS_AND_SCAN_URLS[@]}") |
| 79 | + local return_code=0 |
| 80 | + |
| 81 | + for ((i = 0; i < ${#envs_and_scan_urls[@]}; i += 2)); do |
| 82 | + local env=${envs_and_scan_urls[i]} |
| 83 | + local scan_url=${envs_and_scan_urls[i + 1]} |
| 84 | + |
| 85 | + local exit_code |
| 86 | + config_diff "$env/approved-sv-id-values.yaml" "$scan_url" && exit_code=$? || exit_code=$? |
| 87 | + return_code=$((return_code | exit_code)) |
| 88 | + echo |
| 89 | + done |
| 90 | + |
| 91 | + return "$return_code" |
| 92 | +} |
| 93 | + |
| 94 | +main() { |
| 95 | + case ${1-} in |
| 96 | + -q) |
| 97 | + QUIET=true |
| 98 | + ;; |
| 99 | + -h|--help) |
| 100 | + usage |
| 101 | + exit 0 |
| 102 | + ;; |
| 103 | + "") |
| 104 | + ;; |
| 105 | + *) |
| 106 | + echo "Unknown option: $1" |
| 107 | + echo |
| 108 | + usage |
| 109 | + exit 1 |
| 110 | + ;; |
| 111 | + esac |
| 112 | + |
| 113 | + local result exit_code |
| 114 | + result=$(compare) && exit_code=$? || exit_code=$? |
| 115 | + echo "$result" | less --quit-if-one-screen --no-init --RAW-CONTROL-CHARS |
| 116 | + exit "$exit_code" |
| 117 | +} |
| 118 | + |
| 119 | +main "$@" |
0 commit comments