From d2d467a56d067365ac7b9f8982016a7bcd5dd01f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Mon, 28 Nov 2022 19:15:34 -0500 Subject: [PATCH] CI: automatically update the fastcrypto pointer This is a sibling PR to https://github.com/MystenLabs/sui/pull/4393 which allowed logic that would automatically update the NW pointer. --- .github/workflows/fastcrypto_pull.yml | 41 +++++++++++++ scripts/update_fastcrypto.sh | 85 +++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 .github/workflows/fastcrypto_pull.yml create mode 100755 scripts/update_fastcrypto.sh diff --git a/.github/workflows/fastcrypto_pull.yml b/.github/workflows/fastcrypto_pull.yml new file mode 100644 index 0000000000000..0205949472683 --- /dev/null +++ b/.github/workflows/fastcrypto_pull.yml @@ -0,0 +1,41 @@ +name: Update the Fastcrypto pointer Sui +on: + ## Allow triggering this workflow manually via GitHub CLI/web + workflow_dispatch: + schedule: + # Update on every hour at 10 past the hour + - cron: '10 * * * *' + +jobs: + update-dep: + runs-on: ubuntu-latest + # Important settings as we don't want to open a PR when the update fails + continue-on-error: false + strategy: + fail-fast: true + steps: + - uses: actions/checkout@v3 + - uses: actions-rs/toolchain@v1 + - uses: bmwill/rust-cache@v1 # Fork of 'Swatinem/rust-cache' which allows caching additional paths + - name: Install cargo-hakari, and cache the binary + uses: baptiste0928/cargo-install@v1 + with: + crate: cargo-hakari + locked: true + - name: Update the Narwhal pointer + run: | + scripts/update_fastcrypto.sh + - name: Create Pull Request + uses: peter-evans/create-pull-request@v4 + with: + # TODO: change the token to something FC-specific? + token: ${{ secrets.NW_AUTO_UPDATE }} + commit-message: chore(deps) Update the Fastcrypto pointer + title: chore(deps) Update the Fastcrypto pointer + body: | + - Update Fastcrypto + + Auto-generated by [create-pull-request][1] and the scripts/update_fastcrypto.sh script + + [1]: https://github.com/peter-evans/create-pull-request + branch: update-fastcrypto diff --git a/scripts/update_fastcrypto.sh b/scripts/update_fastcrypto.sh new file mode 100755 index 0000000000000..265e0dc3eceda --- /dev/null +++ b/scripts/update_fastcrypto.sh @@ -0,0 +1,85 @@ +#!/bin/bash +# Copyright (c) Mysten Labs, Inc. +# SPDX-License-Identifier: Apache-2.0 + +# shellcheck disable=SC2181 +# This script attempts to update the Fastcrypto pointer in Sui +# It is expected to fail in some cases, notably when those updates require code changes +set -e +set -eo pipefail + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +TOPLEVEL="${DIR}/../" +GREP=${GREP:=grep} + +# Crutch for old bash versions +# Very minimal readarray implementation using read. Does NOT work with lines that contain double-quotes due to eval() +readarray() { + while IFS= read -r var; do + MAPFILE+=("$var") + done +} + + +# check for the presence of needed executables: +# - we use GNU grep in perl re mode +# - we use cargo-hakari +function check_gnu_grep() { + GNUSTRING=$($GREP --version|head -n1| grep 'GNU grep') + if [[ -z $GNUSTRING ]]; + then + echo "Could not find GNU grep. This requires GNU grep 3.7 with PCRE expressions"; exit 1 + else + return 0 + fi +} + +function check_cargo_hakari() { + cargo hakari --version > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + echo "Could not find cargo hakari. Please install"; exit 1 + else + return 0 + fi +} + + +function latest_fc_revision() { + FC_CHECKOUT=$(mktemp -d) + cd "$FC_CHECKOUT" + git clone --depth 1 https://github.com/mystenlabs/fastcrypto + cd fastcrypto + git rev-parse HEAD +} + +function current_fc_revision() { + cd "$TOPLEVEL" + readarray -t <<< "$(find ./ -iname '*.toml' -exec $GREP -oPe 'git = "https://github.com/MystenLabs/fastcrypto", *rev *= *\"\K[0-9a-fA-F]+' '{}' \;)" + watermark=${MAPFILE[0]} + for i in "${MAPFILE[@]}"; do + if [[ "$watermark" != "$i" ]]; then + not_equal=true + break + fi + done + + [[ -n "$not_equal" ]] && echo "Different values found for the current Fastcrypto revision in Sui, aborting" && exit 1 + echo "$watermark" +} + +# Check for tooling +check_gnu_grep +check_cargo_hakari + +# Debug prints +CURRENT_FC=$(current_fc_revision) +LATEST_FC=$(latest_fc_revision) +if [[ "$CURRENT_FC" != "$LATEST_FC" ]]; then + echo "About to replace $CURRENT_FC with $LATEST_FC as the Narwhal pointer in Sui" +else + exit 0 +fi + +# Edit the source & run hakari +find ./ -iname "*.toml" -execdir sed -i '' -re "s/$CURRENT_FC/$LATEST_FC/" '{}' \; +cargo hakari generate