Skip to content

Commit

Permalink
Fixes for compatibility checker (#19549)
Browse files Browse the repository at this point in the history
- Revert workaround that is no longer necessary
- Support running without a metrics api key
- Show diffs if the script fails
  • Loading branch information
mystenmark authored Sep 25, 2024
1 parent c28a213 commit b816c49
Showing 1 changed file with 41 additions and 51 deletions.
92 changes: 41 additions & 51 deletions scripts/compatibility/check-protocol-compatibility.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,47 @@
# Copyright (c) Mysten Labs, Inc.
# SPDX-License-Identifier: Apache-2.0

# check if API_USER and API_KEY env vars are set
if [ -z "$API_USER" ] || [ -z "$API_KEY" ]; then
echo "Error: API_USER and API_KEY environment variables must be set"
exit 1
fi

NETWORK="$1"

REPO_ROOT=$(git rev-parse --show-toplevel)
cd $REPO_ROOT
if [ -z "$RELEASED_COMMIT" ]; then
# check if API_USER and API_KEY env vars are set
if [ -z "$API_USER" ] || [ -z "$API_KEY" ]; then
echo "Error: API_USER and API_KEY environment variables must be set"
exit 1
fi

if [ "$NETWORK" != "devnet" ] && [ "$NETWORK" != "testnet" ] && [ "$NETWORK" != "mainnet" ]; then
echo "Invalid network: $NETWORK"
echo "Usage: check-protocol-compatibility.sh <devnet|testnet|mainnet> <git|prometheus>"
exit 1
fi
REPO_ROOT=$(git rev-parse --show-toplevel)
cd $REPO_ROOT

case "$NETWORK" in
devnet)
URL="https://$API_USER:$API_KEY@gateway.mimir.sui.io/prometheus/api/v1/query"
;;
testnet)
URL="http://$API_USER:$API_KEY@metrics-gw-2.testnet.sui.io/prometheus/api/v1/query"
;;
mainnet)
URL="https://$API_USER:$API_KEY@metrics-gw-2.mainnet.sui.io/prometheus/api/v1/query"
;;
esac

VERSIONS=$(curl -s -G -k "$URL" --data-urlencode "query=uptime{network=\"$NETWORK\"}" | jq -r '.data.result[].metric.version' | sort | uniq -c | sort -rn)
TOP_VERSION=$(echo "$VERSIONS" | head -n 1 | awk '{print $2}')

echo "Found following versions on $NETWORK:"
echo "$VERSIONS"
echo ""
echo "Using most frequent version $TOP_VERSION for compatibility check"

# TOP_VERSION looks like "1.0.0-ae1212baf8", split out the commit hash
ORIGIN_COMMIT=$(echo "$TOP_VERSION" | cut -d- -f2)
if [ "$NETWORK" != "devnet" ] && [ "$NETWORK" != "testnet" ] && [ "$NETWORK" != "mainnet" ]; then
echo "Invalid network: $NETWORK"
echo "Usage: check-protocol-compatibility.sh <devnet|testnet|mainnet> <git|prometheus>"
exit 1
fi

case "$NETWORK" in
devnet)
URL="https://$API_USER:$API_KEY@gateway.mimir.sui.io/prometheus/api/v1/query"
;;
testnet)
URL="http://$API_USER:$API_KEY@metrics-gw-2.testnet.sui.io/prometheus/api/v1/query"
;;
mainnet)
URL="https://$API_USER:$API_KEY@metrics-gw-2.mainnet.sui.io/prometheus/api/v1/query"
;;
esac

VERSIONS=$(curl -s -G -k "$URL" --data-urlencode "query=uptime{network=\"$NETWORK\"}" | jq -r '.data.result[].metric.version' | sort | uniq -c | sort -rn)
TOP_VERSION=$(echo "$VERSIONS" | head -n 1 | awk '{print $2}')

echo "Found following versions on $NETWORK:"
echo "$VERSIONS"
echo ""
echo "Using most frequent version $TOP_VERSION for compatibility check"

# TOP_VERSION looks like "1.0.0-ae1212baf8", split out the commit hash
RELEASED_COMMIT=$(echo "$TOP_VERSION" | cut -d- -f2)
fi

git fetch -q || exit 1
SOURCE_COMMIT=$(git rev-parse HEAD)
Expand All @@ -49,7 +51,7 @@ SOURCE_BRANCH=$(git branch -a --contains "$SOURCE_COMMIT" | head -n 1 | cut -d'
echo "Source commit: $SOURCE_COMMIT"
echo "Source branch: $SOURCE_BRANCH"

echo "Checking protocol compatibility with $NETWORK ($ORIGIN_COMMIT)"
echo "Checking protocol compatibility with $NETWORK ($RELEASED_COMMIT)"

# put code to check if git client is clean into function
function check_git_clean {
Expand All @@ -58,6 +60,7 @@ function check_git_clean {
# if any files are edited or staged, exit with error
if ! git diff --quiet --exit-code -- $path || ! git diff --cached --quiet --exit-code -- $path; then
echo "Error: $message"
git diff HEAD
exit 1
fi
}
Expand All @@ -66,7 +69,7 @@ check_git_clean "Please commit or stash your changes before running this script"

# check out all files in crates/sui-protocol-config/src/snapshots at origin commit
echo "Checking out $NETWORK snapshot files"
git checkout $ORIGIN_COMMIT -- crates/sui-protocol-config/src/snapshots || exit 1
git checkout $RELEASED_COMMIT -- crates/sui-protocol-config/src/snapshots || exit 1

if [ "$NETWORK" != "testnet" ] && [ "$NETWORK" != "mainnet" ]; then
NETWORK_PATTERN="*__version_*"
Expand All @@ -75,20 +78,7 @@ else
fi

echo "Checking for changes to snapshot files matching $NETWORK_PATTERN"

# The fields `scoring_decision_mad_divisor`, `scoring_decision_cutoff_value` were removed from the protocol config,
# but they are still present in older snapshot files. We need to delete them from the snapshot files before
# checking if the git repo is clean.
# TODO: Remove this workaround once commit 3959d9af51172824b0e4f20802c71e416596c7df has been release to all networks.
SED=$(which gsed)
if [ -z "$SED" ]; then
SED=$(which sed)
fi

grep -lE 'scoring_decision_mad_divisor|scoring_decision_cutoff_value' crates/sui-protocol-config/src/snapshots/$NETWORK_PATTERN | xargs $SED -Ei '/(scoring_decision_mad_divisor|scoring_decision_cutoff_value)/d'
git add .

check_git_clean "Detected changes to snapshot files since $ORIGIN_COMMIT - not safe to release" "$NETWORK_PATTERN"
check_git_clean "Detected changes to snapshot files since $RELEASED_COMMIT - not safe to release" "$NETWORK_PATTERN"

# remove any snapshot file changes that were ignored
git reset --hard HEAD
Expand Down

0 comments on commit b816c49

Please sign in to comment.