1111#
1212# Usage: check-overlay-min-version.sh [--apply] <network-protocol>
1313#
14- # Finds the commit in HEAD's history that raised CURRENT_LEDGER_PROTOCOL_VERSION
15- # to <network-protocol>, and compares OVERLAY_PROTOCOL_MIN_VERSION in the
14+ # Finds the commit in HEAD's history that raised the ledger protocol version
15+ # (MAX_SUPPORTED_PROTOCOL_VERSION in src/util/ProtocolVersion.h, or
16+ # Config::CURRENT_LEDGER_PROTOCOL_VERSION in src/main/Config.cpp in older
17+ # revisions) to <network-protocol>, and compares OVERLAY_PROTOCOL_MIN_VERSION in the
1618# working tree against OVERLAY_PROTOCOL_VERSION at that commit. Exits with a
1719# non-zero status if the minimum is too high. If the minimum can be raised,
1820# says so, and with --apply rewrites it in the working tree.
2426
2527SRCDIR=$( realpath " $( dirname " $0 " ) /.." )
2628CONFIG_CPP=src/main/Config.cpp
29+ PROTOCOL_H=src/util/ProtocolVersion.h
2730
2831APPLY=
2932if [ " $1 " = " --apply" ]
4851
4952cd " $SRCDIR "
5053
51- # Print the contents of src/main/Config.cpp at revision $1 , or the working
52- # tree copy if $1 is empty.
53- config_at ()
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 ()
5457{
5558 if [ -n " $1 " ]
5659 then
57- git show " $1 :$CONFIG_CPP "
60+ git show " $1 :$2 "
5861 else
59- cat " $CONFIG_CPP "
62+ cat " $2 "
6063 fi
6164}
6265
63- # extract <rev> <name> <sed-expr>: print the numeric value assigned to
64- # <name> in Config.cpp at <rev>, failing unless exactly one assignment is
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
6568# found.
6669extract ()
6770{
68- if ! CONTENTS=$( config_at " $1 " )
71+ if ! CONTENTS=$( file_at " $1 " " $2 " )
6972 then
70- echo " error: failed to read $CONFIG_CPP at ${1:- working tree} " >&2
73+ echo " error: failed to read $2 at ${1:- working tree} " >&2
7174 exit 2
7275 fi
73- VAL=$( printf ' %s\n' " $CONTENTS " | sed -n " $3 " )
76+ VAL=$( printf ' %s\n' " $CONTENTS " | sed -n " $4 " )
7477 case " $VAL " in
7578 ' ' |* [!0-9]* )
76- echo " error: expected exactly one numeric assignment to $2 in $CONFIG_CPP at ${1:- working tree} , got '$VAL '" >&2
77- echo " (if the definition of $2 changed shape, update $0 to match)" >&2
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
7881 exit 2
7982 ;;
8083 esac
8184 echo " $VAL "
8285}
8386
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+
84101LEDGER_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+ # Matches diffs that change either form of the ledger protocol version.
104+ LEDGER_LOG_REGEX=' (CURRENT_LEDGER|MAX_SUPPORTED)_PROTOCOL_VERSION[[:space:]]*=[[:space:]]*[0-9]'
85105OVERLAY_EXPR=' s/^[[:space:]]*OVERLAY_PROTOCOL_VERSION[[:space:]]*=[[:space:]]*\([0-9][0-9]*\).*/\1/p'
86106MIN_EXPR=' s/^[[:space:]]*OVERLAY_PROTOCOL_MIN_VERSION[[:space:]]*=[[:space:]]*\([0-9][0-9]*\).*/\1/p'
87107
88- # Print the oldest commit in HEAD's history from which
89- # CURRENT_LEDGER_PROTOCOL_VERSION has been at least $NETWORK_LEDGER.
108+ # Print the oldest commit in HEAD's history from which the ledger protocol
109+ # version has been at least $NETWORK_LEDGER.
90110ledger_bump_commit ()
91111{
92112 FOUND=
93- for C in $( git log --format=%H -G ' CURRENT_LEDGER_PROTOCOL_VERSION[[:space:]]*= ' HEAD -- " $CONFIG_CPP " )
113+ for C in $( git log --format=%H -G " $LEDGER_LOG_REGEX " HEAD -- " $CONFIG_CPP " " $PROTOCOL_H " )
94114 do
95- LEDGER=$( extract " $C " CURRENT_LEDGER_PROTOCOL_VERSION " $LEDGER_EXPR " )
115+ LEDGER=$( ledger_at " $C " )
96116 if [ " $LEDGER " -lt " $NETWORK_LEDGER " ]
97117 then
98118 break
@@ -103,23 +123,23 @@ ledger_bump_commit()
103123 # necessarily the commit that introduced the protocol version.
104124 if [ -z " $FOUND " ] || ! git rev-parse -q --verify " $FOUND ^" > /dev/null
105125 then
106- echo " error: could not find the commit that set CURRENT_LEDGER_PROTOCOL_VERSION to $NETWORK_LEDGER in the history of HEAD (shallow clone?)" >&2
126+ echo " error: could not find the commit that set the ledger protocol version to $NETWORK_LEDGER in the history of HEAD (shallow clone?)" >&2
107127 exit 2
108128 fi
109129 echo " $FOUND "
110130}
111131
112- CODE_LEDGER=$( extract " " CURRENT_LEDGER_PROTOCOL_VERSION " $LEDGER_EXPR " )
113- CODE_MIN=$( extract " " OVERLAY_PROTOCOL_MIN_VERSION " $MIN_EXPR " )
132+ CODE_LEDGER=$( ledger_at " " )
133+ CODE_MIN=$( extract " " " $CONFIG_CPP " OVERLAY_PROTOCOL_MIN_VERSION " $MIN_EXPR " )
114134
115135if [ " $NETWORK_LEDGER " -gt " $CODE_LEDGER " ]
116136then
117- echo " error: network is on protocol $NETWORK_LEDGER , but CURRENT_LEDGER_PROTOCOL_VERSION is $CODE_LEDGER " >&2
137+ echo " error: network is on protocol $NETWORK_LEDGER , but the code's ledger protocol version is $CODE_LEDGER " >&2
118138 exit 2
119139fi
120140
121141BUMP_COMMIT=$( ledger_bump_commit)
122- TARGET_MIN=$( extract " $BUMP_COMMIT " OVERLAY_PROTOCOL_VERSION " $OVERLAY_EXPR " )
142+ TARGET_MIN=$( extract " $BUMP_COMMIT " " $CONFIG_CPP " OVERLAY_PROTOCOL_VERSION " $OVERLAY_EXPR " )
123143
124144echo " Network protocol $NETWORK_LEDGER was introduced in $( git log -1 --format=' %h (%s)' " $BUMP_COMMIT " ) "
125145echo " with OVERLAY_PROTOCOL_VERSION $TARGET_MIN "
@@ -144,7 +164,7 @@ if [ -n "$APPLY" ]
144164then
145165 sed " s/^\([[:space:]]*OVERLAY_PROTOCOL_MIN_VERSION[[:space:]]*=[[:space:]]*\)$CODE_MIN ;/\1$TARGET_MIN ;/" " $CONFIG_CPP " > " $CONFIG_CPP .tmp"
146166 mv " $CONFIG_CPP .tmp" " $CONFIG_CPP "
147- NEW_MIN=$( extract " " OVERLAY_PROTOCOL_MIN_VERSION " $MIN_EXPR " )
167+ NEW_MIN=$( extract " " " $CONFIG_CPP " OVERLAY_PROTOCOL_MIN_VERSION " $MIN_EXPR " )
148168 if [ " $NEW_MIN " != " $TARGET_MIN " ]
149169 then
150170 echo " error: failed to update OVERLAY_PROTOCOL_MIN_VERSION in $CONFIG_CPP " >&2
0 commit comments