|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# This script checks OVERLAY_PROTOCOL_MIN_VERSION (in src/main/Config.cpp) |
| 4 | +# against the ledger protocol version a network is running. |
| 5 | +# |
| 6 | +# Once a network is on ledger protocol P, nodes need software supporting P to |
| 7 | +# stay in sync, so the minimum overlay version can be raised to the lowest |
| 8 | +# maximum overlay version (OVERLAY_PROTOCOL_VERSION) of any release that |
| 9 | +# supports P. It should never be higher than that, or it would disconnect |
| 10 | +# nodes running a release that can still follow the network. |
| 11 | +# |
| 12 | +# Usage: check-overlay-min-version.sh [--apply] <network-protocol> |
| 13 | +# |
| 14 | +# Looks at the release tags (v<number>...) from the newest version down, |
| 15 | +# stopping at the first whose ledger protocol version (MAX_SUPPORTED_PROTOCOL_VERSION |
| 16 | +# in src/util/ProtocolVersion.h, or Config::CURRENT_LEDGER_PROTOCOL_VERSION in |
| 17 | +# src/main/Config.cpp in older releases) is below <network-protocol>, and |
| 18 | +# compares OVERLAY_PROTOCOL_MIN_VERSION in the working tree against the lowest |
| 19 | +# OVERLAY_PROTOCOL_VERSION among the releases that support it. Exits with a |
| 20 | +# non-zero status if the minimum is too high. If the minimum can be raised, |
| 21 | +# says so, and with --apply rewrites it in the working tree. |
| 22 | +# |
| 23 | +# Requires the release tags to have been fetched. |
| 24 | + |
| 25 | +set -e |
| 26 | + |
| 27 | +SRCDIR=$(realpath "$(dirname "$0")/..") |
| 28 | +CONFIG_CPP=src/main/Config.cpp |
| 29 | +PROTOCOL_H=src/util/ProtocolVersion.h |
| 30 | + |
| 31 | +APPLY= |
| 32 | +if [ "$1" = "--apply" ] |
| 33 | +then |
| 34 | + APPLY=1 |
| 35 | + shift |
| 36 | +fi |
| 37 | + |
| 38 | +if [ $# -ne 1 ] |
| 39 | +then |
| 40 | + echo "usage: $0 [--apply] <network-protocol>" >&2 |
| 41 | + exit 2 |
| 42 | +fi |
| 43 | + |
| 44 | +NETWORK_LEDGER=$1 |
| 45 | +case "$NETWORK_LEDGER" in |
| 46 | +''|*[!0-9]*) |
| 47 | + echo "error: network protocol must be a number, got '$NETWORK_LEDGER'" >&2 |
| 48 | + exit 2 |
| 49 | + ;; |
| 50 | +esac |
| 51 | + |
| 52 | +cd "$SRCDIR" |
| 53 | + |
| 54 | +# file_at <rev> <file>: print the contents of <file> at <rev>, or the |
| 55 | +# working tree copy if <rev> is empty. |
| 56 | +file_at() |
| 57 | +{ |
| 58 | + if [ -n "$1" ] |
| 59 | + then |
| 60 | + git show "$1:$2" |
| 61 | + else |
| 62 | + cat "$2" |
| 63 | + fi |
| 64 | +} |
| 65 | + |
| 66 | +# extract <rev> <file> <name> <sed-expr>: print the numeric value assigned |
| 67 | +# to <name> in <file> at <rev>, failing unless exactly one assignment is |
| 68 | +# found. |
| 69 | +extract() |
| 70 | +{ |
| 71 | + if ! CONTENTS=$(file_at "$1" "$2") |
| 72 | + then |
| 73 | + echo "error: failed to read $2 at ${1:-working tree}" >&2 |
| 74 | + exit 2 |
| 75 | + fi |
| 76 | + VAL=$(printf '%s\n' "$CONTENTS" | sed -n "$4") |
| 77 | + case "$VAL" in |
| 78 | + ''|*[!0-9]*) |
| 79 | + echo "error: expected exactly one numeric assignment to $3 in $2 at ${1:-working tree}, got '$VAL'" >&2 |
| 80 | + echo "(if the definition of $3 changed shape, update $0 to match)" >&2 |
| 81 | + exit 2 |
| 82 | + ;; |
| 83 | + esac |
| 84 | + echo "$VAL" |
| 85 | +} |
| 86 | + |
| 87 | +# ledger_at <rev>: print the ledger protocol version at <rev> (without the |
| 88 | +# vnext +1). It is MAX_SUPPORTED_PROTOCOL_VERSION in ProtocolVersion.h, or |
| 89 | +# was assigned directly to Config::CURRENT_LEDGER_PROTOCOL_VERSION in |
| 90 | +# Config.cpp before that. |
| 91 | +ledger_at() |
| 92 | +{ |
| 93 | + if file_at "$1" "$PROTOCOL_H" 2>/dev/null | grep -q 'MAX_SUPPORTED_PROTOCOL_VERSION[[:space:]]*=' |
| 94 | + then |
| 95 | + extract "$1" "$PROTOCOL_H" MAX_SUPPORTED_PROTOCOL_VERSION "$MAX_SUPPORTED_EXPR" |
| 96 | + else |
| 97 | + extract "$1" "$CONFIG_CPP" CURRENT_LEDGER_PROTOCOL_VERSION "$LEDGER_EXPR" |
| 98 | + fi |
| 99 | +} |
| 100 | + |
| 101 | +LEDGER_EXPR='s/.*Config::CURRENT_LEDGER_PROTOCOL_VERSION[[:space:]]*=[[:space:]]*\([0-9][0-9]*\).*/\1/p' |
| 102 | +MAX_SUPPORTED_EXPR='s/.*MAX_SUPPORTED_PROTOCOL_VERSION[[:space:]]*=[[:space:]]*\([0-9][0-9]*\).*/\1/p' |
| 103 | +OVERLAY_EXPR='s/^[[:space:]]*OVERLAY_PROTOCOL_VERSION[[:space:]]*=[[:space:]]*\([0-9][0-9]*\).*/\1/p' |
| 104 | +MIN_EXPR='s/^[[:space:]]*OVERLAY_PROTOCOL_MIN_VERSION[[:space:]]*=[[:space:]]*\([0-9][0-9]*\).*/\1/p' |
| 105 | + |
| 106 | +# List the releases supporting ledger protocol $NETWORK_LEDGER, and set |
| 107 | +# TARGET_MIN to the lowest OVERLAY_PROTOCOL_VERSION among them and TARGET_TAG |
| 108 | +# to a release that has it. Assumes the ledger protocol version doesn't |
| 109 | +# decrease from one release version to the next. |
| 110 | +release_target() |
| 111 | +{ |
| 112 | + TARGET_MIN= |
| 113 | + TARGET_TAG= |
| 114 | + for T in $(git tag -l 'v[0-9]*' --sort=-v:refname) |
| 115 | + do |
| 116 | + LEDGER=$(ledger_at "$T") |
| 117 | + if [ "$LEDGER" -lt "$NETWORK_LEDGER" ] |
| 118 | + then |
| 119 | + break |
| 120 | + fi |
| 121 | + OVERLAY=$(extract "$T" "$CONFIG_CPP" OVERLAY_PROTOCOL_VERSION "$OVERLAY_EXPR") |
| 122 | + echo " $T: ledger protocol $LEDGER, OVERLAY_PROTOCOL_VERSION $OVERLAY" |
| 123 | + if [ -z "$TARGET_MIN" ] || [ "$OVERLAY" -lt "$TARGET_MIN" ] |
| 124 | + then |
| 125 | + TARGET_MIN=$OVERLAY |
| 126 | + TARGET_TAG=$T |
| 127 | + fi |
| 128 | + done |
| 129 | + if [ -z "$TARGET_MIN" ] |
| 130 | + then |
| 131 | + echo "error: no release tag supports ledger protocol $NETWORK_LEDGER (tags not fetched?)" >&2 |
| 132 | + exit 2 |
| 133 | + fi |
| 134 | +} |
| 135 | + |
| 136 | +CODE_LEDGER=$(ledger_at "") |
| 137 | +CODE_MIN=$(extract "" "$CONFIG_CPP" OVERLAY_PROTOCOL_MIN_VERSION "$MIN_EXPR") |
| 138 | + |
| 139 | +if [ "$NETWORK_LEDGER" -gt "$CODE_LEDGER" ] |
| 140 | +then |
| 141 | + echo "error: network is on protocol $NETWORK_LEDGER, but the code's ledger protocol version is $CODE_LEDGER" >&2 |
| 142 | + exit 2 |
| 143 | +fi |
| 144 | + |
| 145 | +echo "Releases supporting ledger protocol $NETWORK_LEDGER:" |
| 146 | +release_target |
| 147 | +echo "Lowest OVERLAY_PROTOCOL_VERSION among them: $TARGET_MIN ($TARGET_TAG)" |
| 148 | +echo "OVERLAY_PROTOCOL_MIN_VERSION: $CODE_MIN" |
| 149 | + |
| 150 | +if [ "$CODE_MIN" -gt "$TARGET_MIN" ] |
| 151 | +then |
| 152 | + echo "error: OVERLAY_PROTOCOL_MIN_VERSION ($CODE_MIN) is higher than the overlay version" >&2 |
| 153 | + echo "of $TARGET_TAG ($TARGET_MIN), which supports protocol $NETWORK_LEDGER, so it would" >&2 |
| 154 | + echo "disconnect nodes that can still follow the network." >&2 |
| 155 | + exit 1 |
| 156 | +fi |
| 157 | + |
| 158 | +if [ "$CODE_MIN" -eq "$TARGET_MIN" ] |
| 159 | +then |
| 160 | + echo "OK" |
| 161 | + exit 0 |
| 162 | +fi |
| 163 | + |
| 164 | +echo "OVERLAY_PROTOCOL_MIN_VERSION can be raised from $CODE_MIN to $TARGET_MIN" |
| 165 | +if [ -n "$APPLY" ] |
| 166 | +then |
| 167 | + sed "s/^\([[:space:]]*OVERLAY_PROTOCOL_MIN_VERSION[[:space:]]*=[[:space:]]*\)$CODE_MIN;/\1$TARGET_MIN;/" "$CONFIG_CPP" > "$CONFIG_CPP.tmp" |
| 168 | + mv "$CONFIG_CPP.tmp" "$CONFIG_CPP" |
| 169 | + NEW_MIN=$(extract "" "$CONFIG_CPP" OVERLAY_PROTOCOL_MIN_VERSION "$MIN_EXPR") |
| 170 | + if [ "$NEW_MIN" != "$TARGET_MIN" ] |
| 171 | + then |
| 172 | + echo "error: failed to update OVERLAY_PROTOCOL_MIN_VERSION in $CONFIG_CPP" >&2 |
| 173 | + exit 2 |
| 174 | + fi |
| 175 | + echo "Updated $CONFIG_CPP" |
| 176 | +fi |
0 commit comments