diff --git a/.github/workflows/soroban-settings-sync.yml b/.github/workflows/soroban-settings-sync.yml new file mode 100644 index 000000000..93f7fba3e --- /dev/null +++ b/.github/workflows/soroban-settings-sync.yml @@ -0,0 +1,200 @@ +name: Soroban settings sync + +# Opens a PR rewriting soroban-settings/testnet_settings_upgrade.json with +# testnet's live Soroban settings (see scripts/sync-soroban-settings.sh), so +# that after a protocol upgrade or settings change on testnet, applying the +# file after a network reset reproduces testnet's current settings. Closes +# that PR again if testnet goes back to matching the file before it's merged. + +on: + schedule: + # 09:10 UTC: about 1 am Pacific (1:10 PST, 2:10 PDT), a few minutes past the + # hour when GitHub's scheduled-run queue is busiest. + - cron: '10 9 * * *' + workflow_dispatch: + +permissions: + contents: write + pull-requests: write + +concurrency: + group: ${{ github.workflow }} + cancel-in-progress: false + +jobs: + sync-settings: + # Scheduled runs only upstream; forks can still dispatch it by hand. + if: github.repository == 'stellar/stellar-core' || github.event_name == 'workflow_dispatch' + runs-on: ubuntu-24.04 + env: + SETTINGS_FILE: soroban-settings/testnet_settings_upgrade.json + steps: + # No credentials are left in .git/config: the only step that writes to the + # repo sets them up for itself, so the downloaded stellar-xdr never runs + # where it could use them. + - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + with: + fetch-depth: 0 + persist-credentials: false + + # The latest release rather than a pinned one; the sync script checks it + # against testnet and fails if it's too old to know testnet's settings. + # The token is only for gh's API calls; the binary isn't run here. + - name: Install the latest stellar-xdr + env: + GH_TOKEN: ${{ github.token }} + run: | + fail() { echo "error: $*" | tee -a "$RUNNER_TEMP/errors.txt" >&2; exit 1; } + TAG=$(gh release view -R stellar/rs-stellar-xdr --json tagName --jq .tagName) \ + || fail "could not find the latest stellar/rs-stellar-xdr release" + mkdir -p "$HOME/.local/bin" + gh release download "$TAG" -R stellar/rs-stellar-xdr \ + -p "stellar-xdr-${TAG#v}-x86_64-unknown-linux-gnu" -O "$HOME/.local/bin/stellar-xdr" \ + || fail "could not download stellar-xdr $TAG for x86_64 Linux" + chmod +x "$HOME/.local/bin/stellar-xdr" + echo "$HOME/.local/bin" >> "$GITHUB_PATH" + + - name: Copy live settings into the settings file + run: | + set -o pipefail + stellar-xdr version + # stderr also goes to a file so a failure notice can quote the error. + ./scripts/sync-soroban-settings.sh \ + --file "$SETTINGS_FILE" 2> >(tee "$RUNNER_TEMP/errors.txt" >&2) | tee "$RUNNER_TEMP/sync.txt" + + - name: Open, update or close the PR + env: + GH_TOKEN: ${{ github.token }} + BASE: ${{ github.ref_name }} + DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} + WORKFLOW: ${{ github.workflow }} + run: | + # One bot branch per base, so a run started from another branch can't + # force-push over the open PR against the default branch. A hash of the + # base keeps distinct bases (release/x vs release-x) apart. + BRANCH="bot/sync-soroban-settings-testnet" + if [ "$BASE" != "$DEFAULT_BRANCH" ] + then + BRANCH="$BRANCH-$(printf '%s' "$BASE" | sha256sum | cut -c1-12)" + fi + + # The bot's own open PR, not a fork's PR that happens to use the same + # branch name. + OPEN_PR=$(gh pr list --head "$BRANCH" --base "$BASE" --state open \ + --json number,isCrossRepository \ + --jq '[.[] | select(.isCrossRepository | not) | .number] | first // empty') + + if git diff --quiet -- "$SETTINGS_FILE" + then + echo "$SETTINGS_FILE is up to date" + # Testnet went back to matching the file, so an open PR would now + # propose settings testnet no longer has. + if [ -n "$OPEN_PR" ] + then + gh pr close "$OPEN_PR" --delete-branch --comment \ + "Testnet's settings match \`$SETTINGS_FILE\` on \`$BASE\` again, so this is no longer needed. Closed by the \`$WORKFLOW\` workflow." + fi + exit 0 + fi + + PROTOCOL=$(sed -n 's/^.* is on protocol \([0-9][0-9]*\) .*$/\1/p' "$RUNNER_TEMP/sync.txt") + TITLE="Sync $(basename "$SETTINGS_FILE") with testnet (protocol $PROTOCOL)" + BODY="$RUNNER_TEMP/body.md" + { + echo "Testnet is on protocol $PROTOCOL, and its live Soroban settings differ from \`$SETTINGS_FILE\`, so this copies them over. Opened by the \`$WORKFLOW\` workflow." + echo + echo "**Reviewer:** check that every change below is intended on testnet (a protocol upgrade or an approved settings upgrade), not a one-off experiment. The file covers upgradeable settings only; frozen-ledger-keys and freeze-bypass lists aren't in it." + echo + echo '```' + cat "$RUNNER_TEMP/sync.txt" + echo '```' + } > "$BODY" + + # Skip the push only if the bot branch already has this file and is + # still one commit on top of the current base. If the base has moved + # (say, someone edited the file on it), merging the old bot commit + # could combine both edits, so the commit is rebuilt on the new base. + gh auth setup-git + if [ -n "$OPEN_PR" ] && + git fetch origin "$BRANCH" 2>/dev/null && + [ "$(git rev-parse FETCH_HEAD^)" = "$(git rev-parse HEAD)" ] && + git diff --quiet FETCH_HEAD -- "$SETTINGS_FILE" + then + echo "$BRANCH already has this change on top of $BASE" + else + git config user.name 'github-actions[bot]' + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' + git checkout -B "$BRANCH" + git add -- "$SETTINGS_FILE" + git commit -q -m "$TITLE" \ + -m "Copies testnet's live upgradeable Soroban settings into the file." + git push -f origin "$BRANCH" + fi + + # Updated even when nothing was pushed, so a description left stale by + # an earlier failed edit gets fixed. + if [ -z "$OPEN_PR" ] + then + gh pr create --base "$BASE" --head "$BRANCH" --title "$TITLE" --body-file "$BODY" + else + gh pr edit "$OPEN_PR" --title "$TITLE" --body-file "$BODY" + fi + + # Covers anything that stops a scheduled sync: the script refusing to guess + # (e.g. isNonUpgradeableConfigSettingEntry moved, or testnet having a setting + # the latest stellar-xdr doesn't know), RPC or download failures, or the PR + # step. Posts with the org's Slack bot the way other stellar repos do + # (e.g. stellar/dashboard), so the bot must be a member of SLACK_CHANNEL. + # Like stellar/quickstart's notify job, hand-started runs are watched by + # whoever started them, and a cache entry keyed on the error posts each + # distinct failure once rather than every day until it's fixed. + - name: Key the failure notice by its error + id: notify_key + if: failure() && github.event_name == 'schedule' + continue-on-error: true + env: + SLACK_CHANNEL: alerts-core-team + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: | + ERROR=$({ grep '^error: ' "$RUNNER_TEMP/errors.txt" 2>/dev/null || true; } | tail -1 | sed 's/^error: //') + # Built with jq so the error text can't break the message's JSON. + PAYLOAD=$(jq -nc --arg channel "$SLACK_CHANNEL" --arg error "$ERROR" --arg run_url "$RUN_URL" \ + '{channel: $channel, + text: (":red_circle: Soroban settings sync failed; testnet_settings_upgrade.json may be out of date with testnet. <\($run_url)|run>" + + (if $error != "" then "\n> \($error)" else "" end))}') + { + echo "payload=$PAYLOAD" + echo "key=soroban-settings-sync-notify-$(printf '%s' "${ERROR:-unknown}" | sha256sum | cut -c1-16)" + } >> "$GITHUB_OUTPUT" + mkdir -p .notify_cache + date -u +%Y-%m-%dT%H:%M:%SZ > .notify_cache/timestamp + + - name: Check whether this failure was already reported + id: notify_cache + if: failure() && github.event_name == 'schedule' + continue-on-error: true + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: .notify_cache + key: ${{ steps.notify_key.outputs.key }} + + # errors: true makes a refused post (e.g. not_in_channel) fail this step, so + # it isn't recorded as reported below. + - name: Notify Slack on failure + id: notify_slack + if: failure() && github.event_name == 'schedule' && steps.notify_cache.outputs.cache-hit != 'true' + continue-on-error: true + uses: slackapi/slack-github-action@45a88b9581bfab2566dc881e2cd66d334e621e2c # v3.0.3 + with: + method: chat.postMessage + token: ${{ secrets.SLACK_BOT_TOKEN }} + errors: true + payload: ${{ steps.notify_key.outputs.payload }} + + - name: Remember that this failure was reported + if: failure() && github.event_name == 'schedule' && steps.notify_slack.outcome == 'success' + continue-on-error: true + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: .notify_cache + key: ${{ steps.notify_key.outputs.key }} diff --git a/scripts/README.md b/scripts/README.md index bbde3d3b4..ffaf83f96 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -85,6 +85,12 @@ then parse the path of log files, bucket directory, and SQL DB. All these fields in case the script has some issues, so please read through that doc before attempting to use this script. This script should be run from the directory that contains your stellar-core binary. This script also queries the SDF Horizon for the sequence number of the account, so if the SDF Horizon instance is unavailable, then you'll need to provide the account's sequence number manually. - Usage - Ex. `sh ../scripts/settings-helper.sh SCSQHJIUGUGTH2P4K6AOFTEW4HUMI2BRTUBBDDXMQ4FLHXCX25W3PGBJ testnet ../soroban-settings/testnet_settings_phase2.json 100`. The first argument is the secret key of the source account that will be used for the transactions to set up the upgrade, the second argument is the network (one of pubnet|testnet|futurenet), the third is the path to the JSON file with the proposed settings, and the fourth is any additional resource fee you want to add to your transactions. +### Sync Soroban Settings +- Name - `sync-soroban-settings.sh` +- Prequisites - `stellar-xdr`, `curl`, `jq`, `xxd` +- Description - Rewrites a settings file with testnet's live upgradeable Soroban settings, read through its RPC, so that applying the file after a network reset reproduces testnet's current settings, including cost types and settings added by protocol upgrades. The setting types come from `stellar-xdr`, and the non-upgradeable ones are read from `SorobanNetworkConfig::isNonUpgradeableConfigSettingEntry` in `src/ledger/NetworkConfig.cpp`, so it fails rather than guessing if either is out of date. The `Soroban settings sync` GitHub workflow runs it daily against testnet and opens a PR when the file changes. +- Usage - `./scripts/sync-soroban-settings.sh --file soroban-settings/testnet_settings_upgrade.json`. It only ever reads from testnet. + ### Histogram Generator - Name - `HistogramGenerator.py` - Description - A Python script that takes Hubble transaction data as input and outputs histograms containing information about the resource usage of those transactions. This script enables easy updating of transaction resource distributions in [Supercluster](https://github.com/stellar/supercluster), but some may find it more broadly helpful for understanding real-world usage of the Stellar network. diff --git a/scripts/sync-soroban-settings.sh b/scripts/sync-soroban-settings.sh new file mode 100755 index 000000000..e903cadc1 --- /dev/null +++ b/scripts/sync-soroban-settings.sh @@ -0,0 +1,236 @@ +#!/usr/bin/env bash +# +# Rewrites a settings file with testnet's live Soroban settings. +# +# After a protocol upgrade, testnet's settings can include new cost types, +# recalibrated cost params, and new upgradeable settings, and validators reject +# a settings upgrade that doesn't match the protocol. This reads every +# upgradeable ConfigSetting ledger entry through testnet's RPC and writes them +# to the file in ConfigSettingID order, so applying the file after a network +# reset reproduces testnet's current settings. It only ever reads from testnet, +# and refuses to run if the RPC reports a different network. +# +# Only settings a ConfigUpgradeSet can set are covered. Network-maintained +# entries, and the frozen-ledger-keys and freeze-bypass lists (changed through +# their own delta upgrades), are not, so applying the file doesn't restore them. +# +# The setting types come from stellar-xdr's ConfigSettingID enum, and the +# non-upgradeable ones are read from stellar-core's own +# SorobanNetworkConfig::isNonUpgradeableConfigSettingEntry, so neither list is +# kept here. +# +# Usage: +# scripts/sync-soroban-settings.sh [options] +# +# Options: +# --file PATH Settings file (default: soroban-settings/testnet_settings_upgrade.json) +# --core-src PATH stellar-core source defining isNonUpgradeableConfigSettingEntry +# (default: src/ledger/NetworkConfig.cpp) +# +# Prints a summary of what changed; the file is left untouched if nothing did. +# +# Requires: stellar-xdr (any release; the script checks it against testnet), curl, jq, xxd + +set -euo pipefail + +RPC_URL="https://soroban-testnet.stellar.org" +FILE="soroban-settings/testnet_settings_upgrade.json" +CORE_SRC="src/ledger/NetworkConfig.cpp" + +die() { printf 'error: %s\n' "$*" >&2; exit 1; } + +while [ $# -gt 0 ]; do + case "$1" in + --file) FILE="$2"; shift 2 ;; + --core-src) CORE_SRC="$2"; shift 2 ;; + -h|--help) awk 'NR < 3 { next } /^#/ { sub(/^# ?/, ""); print; next } { exit }' "$0"; exit 0 ;; + *) die "unknown argument: $1" ;; + esac +done + +for tool in curl jq stellar-xdr xxd; do + command -v "$tool" >/dev/null 2>&1 || die "$tool not found in PATH" +done +[ -r "$FILE" ] || die "cannot read $FILE" +[ -r "$CORE_SRC" ] || die "cannot read $CORE_SRC" + +rpc() +{ + local method="$1" params="${2:-null}" resp + resp="$(curl -sS --retry 3 -m 30 -X POST "$RPC_URL" -H 'Content-Type: application/json' \ + -d "$(jq -nc --arg m "$method" --argjson p "$params" '{jsonrpc:"2.0", id:1, method:$m, params:$p}')")" \ + || die "RPC request failed: $method" + printf '%s' "$resp" | jq -e 'has("result")' >/dev/null \ + || die "RPC error for $method: $(printf '%s' "$resp" | jq -c '.error // .')" + printf '%s' "$resp" | jq -c '.result' +} + +NETWORK_INFO="$(rpc getNetwork)" +[ "$(printf '%s' "$NETWORK_INFO" | jq -r '.passphrase')" = "Test SDF Network ; September 2015" ] \ + || die "$RPC_URL is not testnet" +PROTOCOL="$(printf '%s' "$NETWORK_INFO" | jq -r '.protocolVersion')" + +# stellar-xdr release numbers don't track protocols one-to-one (there is no +# 29.x), so instead of comparing versions, this checks the setting types it +# knows against what testnet's RPC accepts and has. +XDR_VERSION="$(stellar-xdr version | awk 'NR == 1 { print $2 }')" + +# Every ConfigSettingID, in id order. +ALL_IDS="$(stellar-xdr types schema --type ConfigSettingId | jq -c '.enum')" +[ "$(printf '%s' "$ALL_IDS" | jq 'length')" -gt 0 ] \ + || die "could not list ConfigSettingIDs from stellar-xdr" + +# getLedgerEntries, but returns the raw response instead of dying on an error. +get_entries_raw() +{ + curl -sS --retry 3 -m 30 -X POST "$RPC_URL" -H 'Content-Type: application/json' \ + -d "$(jq -nc --argjson k "$1" '{jsonrpc:"2.0", id:1, method:"getLedgerEntries", params:{keys:$k}}')" \ + || die "RPC request failed: getLedgerEntries" +} + +# The index of the key the RPC rejected because its XDR doesn't know that +# ConfigSettingID, or nothing. +rejected_index() +{ + printf '%s' "$1" | jq -r '.error.message // "" + | capture("cannot unmarshal key value .* at index (?[0-9]+)").i // empty' +} + +# If testnet has a setting newer than this stellar-xdr, it would be silently +# left out of the file, so look at the ids just past the last one stellar-xdr +# knows and fail if testnet has an entry for any. Several are checked because +# some ids (the *_delta ones) never have an entry, so a newer stored setting +# can sit behind one. The first id the RPC rejects ends the search: the RPC +# doesn't know it, so testnet can't have it or anything after it. +# +# A ConfigSetting LedgerKey is the CONFIG_SETTING (8) discriminant followed by +# the ConfigSettingID, both big-endian 32-bit. +config_key_for_id() +{ + printf '%08x%08x' 8 "$1" | xxd -r -p | base64 +} +NEXT_ID="$(printf '%s' "$ALL_IDS" | jq 'length')" +for id in $(seq "$NEXT_ID" $((NEXT_ID + 3))); do + resp="$(get_entries_raw "$(jq -nc --arg k "$(config_key_for_id "$id")" '[$k]')")" + [ -z "$(rejected_index "$resp")" ] || break + printf '%s' "$resp" | jq -e 'has("result")' >/dev/null \ + || die "RPC error for getLedgerEntries: $(printf '%s' "$resp" | jq -c '.error // .')" + [ "$(printf '%s' "$resp" | jq '.result.entries // [] | length')" -eq 0 ] \ + || die "testnet has ConfigSettingID $id, which stellar-xdr $XDR_VERSION doesn't know; use a newer stellar-xdr" +done + +# The ids core never sets by upgrade, read from the body of +# isNonUpgradeableConfigSettingEntry(ConfigSettingID const&), ignoring // +# comments. CONFIG_SETTING_FOO_BAR in core is foo_bar in stellar-xdr's JSON. +SKIP="$(awk ' + /SorobanNetworkConfig::isNonUpgradeableConfigSettingEntry\(/ { sig = 1; next } + sig && /ConfigSettingID const/ { body = 1; sig = 0; next } + sig { sig = 0 } + body && /^}/ { exit } + body' "$CORE_SRC" \ + | sed 's://.*$::' \ + | { grep -o 'CONFIG_SETTING_[A-Z0-9_]*' || true; } \ + | sed 's/^CONFIG_SETTING_//' | tr 'A-Z' 'a-z' \ + | jq -Rsc 'split("\n") | map(select(length > 0))')" +[ "$(printf '%s' "$SKIP" | jq 'length')" -gt 0 ] \ + || die "found no non-upgradeable settings in isNonUpgradeableConfigSettingEntry in $CORE_SRC; has it moved?" +unknown="$(jq -nr --argjson s "$SKIP" --argjson a "$ALL_IDS" '$s - $a | join(", ")')" +[ -z "$unknown" ] \ + || die "$CORE_SRC lists settings stellar-xdr $XDR_VERSION doesn't know: $unknown" + +WANT="$(jq -nc --argjson s "$SKIP" --argjson a "$ALL_IDS" '$a - $s')" + +# Built in a plain loop rather than a pipeline so that a failed encode stops the +# script instead of silently dropping that setting. +keys='[]' +for id in $(printf '%s' "$WANT" | jq -r '.[]'); do + key="$(jq -nc --arg id "$id" '{config_setting:{config_setting_id:$id}}' \ + | stellar-xdr encode --type LedgerKey)" \ + || die "stellar-xdr could not encode the LedgerKey for $id" + keys="$(printf '%s' "$keys" | jq -c --arg k "$key" '. + [$k]')" +done + +# Settings this stellar-xdr knows but testnet's RPC doesn't can't exist on +# testnet yet, so drop each one the RPC rejects and ask again. +while :; do + resp="$(get_entries_raw "$keys")" + bad="$(rejected_index "$resp")" + [ -n "$bad" ] || break + [ "$bad" -lt "$(printf '%s' "$keys" | jq 'length')" ] \ + || die "RPC rejected key $bad of $(printf '%s' "$keys" | jq 'length')" + echo "testnet's RPC doesn't know $(printf '%s' "$WANT" | jq -r --argjson i "$bad" '.[$i]'); skipping it" >&2 + keys="$(printf '%s' "$keys" | jq -c --argjson i "$bad" 'del(.[$i])')" + WANT="$(printf '%s' "$WANT" | jq -c --argjson i "$bad" 'del(.[$i])')" +done +printf '%s' "$resp" | jq -e 'has("result")' >/dev/null \ + || die "RPC error for getLedgerEntries: $(printf '%s' "$resp" | jq -c '.error // .')" + +# Upgradeable settings with a ledger entry (the *_delta ids only exist inside +# upgrades and have none). Decoded in a plain loop, like the keys above, so a +# failed decode stops the script; each must be a setting that was asked for, +# at most once. +entries="$(printf '%s' "$resp" | jq -c '.result.entries // []')" +live='[]' +count="$(printf '%s' "$entries" | jq 'length')" +i=0 +while [ "$i" -lt "$count" ]; do + xdr="$(printf '%s' "$entries" | jq -r --argjson i "$i" '.[$i].xdr')" + setting="$(printf '%s' "$xdr" | stellar-xdr decode --type LedgerEntryData --output json \ + | jq -c '.config_setting // empty')" \ + || die "stellar-xdr could not decode ledger entry $i returned by $RPC_URL" + [ -n "$setting" ] || die "$RPC_URL returned a ledger entry that isn't a config setting" + id="$(printf '%s' "$setting" | jq -r 'keys[0]')" + printf '%s' "$WANT" | jq -e --arg id "$id" 'index($id) != null' >/dev/null \ + || die "$RPC_URL returned $id, which wasn't requested" + printf '%s' "$live" | jq -e --arg id "$id" 'map(keys[0]) | index($id) == null' >/dev/null \ + || die "$RPC_URL returned $id more than once" + live="$(printf '%s' "$live" | jq -c --argjson s "$setting" '. + [$s]')" + i=$((i + 1)) +done +live="$(printf '%s' "$live" | jq -c --argjson a "$ALL_IDS" 'sort_by(keys[0] as $k | $a | index($k))')" + +# A setting never disappears from a network, so one the file has but testnet +# didn't return means the lookup went wrong (a partial response, or an RPC that +# doesn't know that setting). Fail rather than propose deleting it. +missing="$(jq -r --argjson live "$live" \ + '[.updated_entry[] | keys[0]] - [$live[] | keys[0]] | join(", ")' "$FILE")" +[ -z "$missing" ] || die "testnet returned no $missing, which $FILE has; refusing to remove it" + +updated="$(jq --argjson live "$live" '.updated_entry = $live' "$FILE")" +# Encoding the result checks it is still a valid ConfigUpgradeSet. +printf '%s' "$updated" | stellar-xdr encode --type ConfigUpgradeSet >/dev/null \ + || die "the updated file is not a valid ConfigUpgradeSet" + +echo "testnet is on protocol $PROTOCOL ($RPC_URL)" +if [ "$(jq -c . "$FILE")" = "$(printf '%s' "$updated" | jq -c .)" ]; then + echo "$FILE already matches testnet" + exit 0 +fi + +# One line per setting that was added, removed or changed. Array settings (the +# cost params) list the changed and added indices; object settings list the +# changed fields. +jq -rn --slurpfile old "$FILE" --argjson new "$updated" ' + def bykey: map(to_entries[0]) | from_entries; + ($old[0].updated_entry | bykey) as $o | ($new.updated_entry | bykey) as $n + | (($o | keys) + ($n | keys) | unique)[] as $k + | if $o[$k] == null then "\($k): added" + elif $n[$k] == null then "\($k): removed" + elif $o[$k] == $n[$k] then empty + elif ($n[$k] | type) == "array" then + [range(0; [($o[$k] | length), ($n[$k] | length)] | min) | select($o[$k][.] != $n[$k][.])] as $c + | "\($k): \($o[$k] | length) -> \($n[$k] | length) entries" + + (if ($c | length) > 0 then ", changed \($c | map(tostring) | join(", "))" else "" end) + + (if ($n[$k] | length) > ($o[$k] | length) then ", added \($o[$k] | length)-\(($n[$k] | length) - 1)" else "" end) + elif ($n[$k] | type) == "object" then + "\($k): changed \([($n[$k] | keys[]) as $f | select($o[$k][$f] != $n[$k][$f]) | $f] | join(", "))" + else "\($k): \($o[$k]) -> \($n[$k])" + end' + +# Keep the file's existing trailing-newline convention so the diff is only the values. +if [ -n "$(tail -c 1 "$FILE")" ]; then + printf '%s' "$updated" > "$FILE" +else + printf '%s\n' "$updated" > "$FILE" +fi +echo "updated $FILE"