Skip to content

Commit

Permalink
feat(ops): consolidate version control
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Jan 9, 2024
1 parent d8f8877 commit 222c856
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 41 deletions.
1 change: 0 additions & 1 deletion .abigenrc

This file was deleted.

1 change: 0 additions & 1 deletion .foundryrc

This file was deleted.

1 change: 0 additions & 1 deletion .gethrc

This file was deleted.

1 change: 0 additions & 1 deletion .kontrolrc

This file was deleted.

1 change: 0 additions & 1 deletion .slitherrc

This file was deleted.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,6 @@ install-geth:
./ops/scripts/geth-version-checker.sh && \
(echo "Geth versions match, not installing geth..."; true) || \
(echo "Versions do not match, installing geth!"; \
go install -v github.com/ethereum/go-ethereum/cmd/geth@$(shell cat .gethrc); \
go install -v github.com/ethereum/go-ethereum/cmd/geth@$(shell cat versions.json | jq -r '.geth'); \
echo "Installed geth!"; true)
.PHONY: install-geth
47 changes: 43 additions & 4 deletions op-bindings/bindgen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -151,13 +152,12 @@ func genContractBindings(logger log.Logger, monorepoRootPath, abiFilePath, bytec
abigenVersion := bytes.Trim(versionBuf.Bytes(), "\n")

// Fetch expected abigen version (format: vX.Y.Z)
expectedAbigenVersion, err := os.ReadFile(path.Join(monorepoRootPath, ".abigenrc"))
expectedAbigenVersion, err := readExpectedAbigenVersion(monorepoRootPath)
if err != nil {
return fmt.Errorf("error reading .abigenrc file: %w", err)
return fmt.Errorf("error fetching the expected abigen version: %w", err)
}
expectedAbigenVersion = bytes.Trim(expectedAbigenVersion, "\n")[1:]

if !bytes.Contains(abigenVersion, expectedAbigenVersion) {
if !bytes.Contains(abigenVersion, []byte(expectedAbigenVersion)) {
return fmt.Errorf("abigen version mismatch, expected %s, got %s. Please run `pnpm install:abigen` in the monorepo root", expectedAbigenVersion, abigenVersion)
}
} else {
Expand Down Expand Up @@ -188,3 +188,42 @@ func genContractBindings(logger log.Logger, monorepoRootPath, abiFilePath, bytec

return nil
}

// Versions is a struct for holding the versions of the tools used in the monorepo
type Versions struct {
Abigen string `json:"abigen"`
Foundry string `json:"foundry"`
Geth string `json:"geth"`
Nvm string `json:"nvm"`
Slither string `json:"slither"`
Kontrol string `json:"kontrol"`
}

// readExpectedAbigenVersion reads the expected abigen version from the monorepo's
// versions.json file. This function will remove the 'v' prefix from the version
// string.
//
// Parameters:
// - monorepoRootPath: The path to the monorepo's root directory.
//
// Returns:
// - The expected abigen version.
// - An error if the versions.json file cannot be read or parsed, nil otherwise.
func readExpectedAbigenVersion(monorepoRootPath string) (string, error) {
// Open the version control file
jsonFile, err := os.Open(path.Join(monorepoRootPath, "versions.json"))
if err != nil {
return "", fmt.Errorf("error reading versions.json file: %w", err)
}
defer jsonFile.Close()

// Parse the version control file
byteValue, _ := io.ReadAll(jsonFile)
var versions Versions
if err := json.Unmarshal(byteValue, &versions); err != nil {
return "", fmt.Errorf("error parsing versions.json file: %w", err)
}

// Trim the 'v' prefix from the version string
return strings.Trim(versions.Abigen, "v"), nil
}
34 changes: 34 additions & 0 deletions op-bindings/bindgen/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package bindgen

import (
"encoding/json"
"os"
"path"
"testing"

"github.com/stretchr/testify/require"
)

func TestReadExpectedAbigenVersion(t *testing.T) {
// Create a temporary directory for the version control file.
tmpDir := path.Join(os.TempDir(), "version-tests")
defer os.RemoveAll(tmpDir)
require.NoError(t, os.MkdirAll(tmpDir, 0755))

// Create a temporary version control file.
versionFile := path.Join(tmpDir, "versions.json")
versions := Versions{Abigen: "v1.2.3"}

// Marshal the versions to JSON.
versionsJSON, err := json.Marshal(versions)
require.NoError(t, err)

// Write the JSON to the version control file.
require.NoError(t, os.WriteFile(versionFile, versionsJSON, 0644))

// Read the expected version from the version control file.
// The read version should not have a "v" prefix.
expectedVersion, err := readExpectedAbigenVersion(tmpDir)
require.NoError(t, err)
require.Equal(t, expectedVersion, "1.2.3")
}
16 changes: 7 additions & 9 deletions ops/docker/ci-builder/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RUN source $HOME/.profile && cargo install svm-rs

# Only diff from upstream docker image is this clone instead
# of COPY. We select a specific commit to use.
COPY ./.foundryrc ./.foundryrc
COPY ./versions.json ./versions.json
COPY ./ops/scripts/install-foundry.sh ./install-foundry.sh

RUN curl -L https://foundry.paradigm.xyz | bash
Expand All @@ -43,12 +43,10 @@ RUN curl -sL https://go.dev/dl/go$GO_VERSION.linux-amd64.tar.gz -o go$GO_VERSION
ENV GOPATH=/go
ENV PATH=/usr/local/go/bin:$GOPATH/bin:$PATH

# Install the specific version of abigen from .abigenrc
COPY ./.abigenrc ./.abigenrc
RUN go install github.com/ethereum/go-ethereum/cmd/abigen@$(cat .abigenrc)

COPY ./.gethrc ./.gethrc
RUN go install github.com/ethereum/go-ethereum/cmd/geth@$(cat .gethrc)
# Install the specific version of abigen and geth from version control
COPY ./versions.json ./versions.json
RUN go install github.com/ethereum/go-ethereum/cmd/abigen@$(cat versions.json | jq -r .abigen)
RUN go install github.com/ethereum/go-ethereum/cmd/geth@$(cat versions.json | jq -r .geth)

RUN go install gotest.tools/gotestsum@latest
RUN go install github.com/vektra/mockery/v2@v2.28.1
Expand Down Expand Up @@ -84,9 +82,9 @@ COPY --from=rust-build /root/.foundry/bin/anvil /usr/local/bin/anvil
COPY --from=echidna-test /usr/local/bin/echidna-test /usr/local/bin/echidna-test

COPY .nvmrc .nvmrc
COPY ./versions.json ./versions.json

ENV NODE_MAJOR=20
ENV SLITHER_VERSION=0.10.0

# note: python3 package in apt is python 3.9, while base image already has python 3.11
RUN /bin/sh -c set -eux; \
Expand All @@ -102,7 +100,7 @@ RUN /bin/sh -c set -eux; \
apt-get install -y nodejs docker-ce-cli; \
ln -s /usr/local/go/bin/gofmt /usr/local/bin/gofmt; \
npm i -g depcheck; \
pip install slither-analyzer==$SLITHER_VERSION capstone pyelftools; \
pip install slither-analyzer==$(cat versions.json | jq -r '.slither') capstone pyelftools; \
curl -fLSs https://raw.githubusercontent.com/CircleCI-Public/circleci-cli/master/install.sh | bash; \
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
rm -rf /var/lib/apt/lists/*; \
Expand Down
4 changes: 1 addition & 3 deletions ops/docker/ci-builder/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
*
!/.foundryrc
!/.abigenrc
!/.gethrc
!/.nvmrc
!/versions.json
!/ops/scripts/install-foundry.sh
16 changes: 8 additions & 8 deletions ops/scripts/geth-version-checker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
# Extract the version from the geth command output
GETH_VERSION="v$(geth version | grep '^Version:' | awk '{print $2}')"

# Read the version from the .gethrc file
GETHRC_VERSION=$(cat .gethrc)
# Read the version from the versions file
EXPECTED_GETH_VERSION=$(cat versions.json | jq -r '.geth')

# Check if GETHRC_VERSION contains a '-'. If not, append '-stable'.
if [[ $GETHRC_VERSION != *-* ]]; then
GETHRC_VERSION="${GETHRC_VERSION}-stable"
# Check if EXPECTED_GETH_VERSION contains a '-'. If not, append '-stable'.
if [[ $EXPECTED_GETH_VERSION != *-* ]]; then
EXPECTED_GETH_VERSION="${EXPECTED_GETH_VERSION}-stable"
fi

# Compare the versions
if [[ "$GETH_VERSION" == "$GETHRC_VERSION" ]]; then
if [[ "$GETH_VERSION" == "$EXPECTED_GETH_VERSION" ]]; then
echo "Geth version $GETH_VERSION is correct!"
exit 0
else
echo "Geth version does not match!"
echo "geth version: $GETH_VERSION"
echo ".gethrc version: $GETHRC_VERSION"
echo "Local geth version: $GETH_VERSION"
echo "Expected geth version: $EXPECTED_GETH_VERSION"
exit 1
fi
6 changes: 3 additions & 3 deletions ops/scripts/install-foundry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

set -e

# Grab the `.foundryrc` commit hash.
SHA=$(cat ./.foundryrc)
# Grab the foundry commit hash.
SHA=$(cat versions.json | jq -r '.foundry')

# Check if there is a nightly tag corresponding to the `.foundryrc` commit hash
# Check if there is a nightly tag corresponding to the commit hash
TAG="nightly-$SHA"

# If the foundry repository exists and a branch is checked out, we need to abort
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
"release:version": "changeset version && pnpm install --lockfile-only",
"install:foundry": "curl -L https://foundry.paradigm.xyz | bash && pnpm update:foundry",
"update:foundry": "bash ./ops/scripts/install-foundry.sh",
"install:abigen": "go install github.com/ethereum/go-ethereum/cmd/abigen@$(cat .abigenrc)",
"install:abigen": "go install github.com/ethereum/go-ethereum/cmd/abigen@$(cat versions.json | jq -r '.abigen')",
"print:abigen": "abigen --version | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' -e 's/ /./g' -e 's/^/v/'",
"check:abigen": "[[ $(abigen --version | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' -e 's/ /./g' -e 's/^/v/') = $(cat .abigenrc) ]] && echo '✓ abigen versions match' || (echo '✗ abigen version mismatch. Run `pnpm upgrade:abigen` to upgrade.' && exit 1)",
"upgrade:abigen": "abigen --version | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' -e 's/ /./g' -e 's/^/v/' > .abigenrc",
"install:slither": "pip3 install slither-analyzer==$(cat .slitherrc)",
"check:abigen": "[[ $(pnpm -s print:abigen) = $(cat versions.json | jq -r '.abigen') ]] && echo '✓ abigen versions match' || (echo '✗ abigen version mismatch. Run `pnpm upgrade:abigen` to upgrade.' && exit 1)",
"upgrade:abigen": "jq '.abigen = $v' --arg v $(pnpm -s print:abigen) <<<$(cat versions.json) > versions.json",
"install:slither": "pip3 install slither-analyzer==$(cat versions.json | jq -r '.slither')",
"print:slither": "slither --version",
"check:slither": "[[ $(slither --version) = $(cat .slitherrc) ]] && echo '✓ slither versions match' || (echo '✗ slither version mismatch. Run `pnpm upgrade:slither` to upgrade.' && exit 1)",
"upgrade:slither": "slither --version > .slitherrc"
"check:slither": "[[ $(pnpm -s print:slither) = $(cat versions.json | jq -r '.slither') ]] && echo '✓ slither versions match' || (echo '✗ slither version mismatch. Run `pnpm upgrade:slither` to upgrade.' && exit 1)",
"upgrade:slither": "jq '.slither = $v' --arg v $(pnpm -s print:slither) <<<$(cat versions.json) > versions.json"
},
"devDependencies": {
"@babel/eslint-parser": "^7.23.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts-bedrock/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Optimism's smart contracts are written in Solidity and we use [foundry](https://
1. Make sure to `pnpm install`
1. [foundry](https://getfoundry.sh)
1. Foundry is built with [rust](https://www.rust-lang.org/tools/install), and this project uses a pinned version of foundry. Install the rust toolchain with `rustup`.
1. Make sure to install the version of foundry used by `ci-builder`, defined in the `.foundryrc` file in the root of this repo. Once you have `foundryup` installed, there is a helper to do this: `pnpm install:foundry`
1. Make sure to install the version of foundry used by `ci-builder`, defined in the `versions.json` file in the root of this repo under the `foundry` key. Once you have `foundryup` installed, there is a helper to do this: `pnpm install:foundry`
1. [golang](https://golang.org/doc/install)
1. [python](https://www.python.org/downloads/)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ blank_line

export FOUNDRY_PROFILE=kontrol
export CONTAINER_NAME=kontrol-tests
KONTROLRC=$(cat "${WORKSPACE_DIR}/../../.kontrolrc")
KONTROLRC=$(cat "${WORKSPACE_DIR}/../../versions.json" | jq -r '.kontrol')
export KONTROL_RELEASE=${KONTROLRC}


Expand Down
8 changes: 8 additions & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"abigen": "v1.10.25",
"foundry": "71d8ea5923571f33c7aab9ee6e0d1f9a348bd6be",
"geth": "v1.13.4",
"nvm": "v20.9.0",
"slither": "0.10.0",
"kontrol": "0.1.74"
}

0 comments on commit 222c856

Please sign in to comment.