From 19f1438ff4dc726838c644eba981ecd82c215c70 Mon Sep 17 00:00:00 2001 From: tre Date: Thu, 11 Jul 2024 16:42:22 -0700 Subject: [PATCH] feat: check for minimum anvil version --- cmd/main.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/cmd/main.go b/cmd/main.go index 37843200..d9d23c8e 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,9 +1,14 @@ package main import ( + "bytes" "context" "fmt" "os" + "os/exec" + "regexp" + "strconv" + "strings" "github.com/ethereum-optimism/supersim" @@ -23,6 +28,10 @@ var ( EnvVarPrefix = "SUPERSIM" ) +const ( + minAnvilVersion = "0.2.0" +) + func main() { oplog.SetupDefaults() @@ -45,6 +54,11 @@ func main() { func SupersimMain(ctx *cli.Context, closeApp context.CancelCauseFunc) (cliapp.Lifecycle, error) { log := oplog.NewLogger(oplog.AppOut(ctx), oplog.ReadCLIConfig(ctx)) + ok, minAnvilErr := IsMinAnvilInstalled(log) + + if !ok || minAnvilErr != nil { + return nil, fmt.Errorf("anvil version %s or higher is required.", minAnvilVersion) + } _, err := supersim.ReadCLIConfig(ctx) if err != nil { @@ -59,3 +73,53 @@ func SupersimMain(ctx *cli.Context, closeApp context.CancelCauseFunc) (cliapp.Li return s, nil } + +func IsMinAnvilInstalled(log log.Logger) (bool, error) { + cmd := exec.Command("anvil", "--version") + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + return false, err + } + + output := out.String() + + versionRegex := regexp.MustCompile(`\d+\.\d+\.\d+`) + version := versionRegex.FindString(output) + + if version == "" { + return false, fmt.Errorf("failed to parse anvil version from output") + } + + log.Info(fmt.Sprintf("anvil version: %s installed", version)) + + if isVersionGreaterOrEqual(version, minAnvilVersion) { + return true, nil + } + + return false, nil +} + +// Compares two semantic versions. +func isVersionGreaterOrEqual(version, minVersion string) bool { + v1 := strings.Split(version, ".") + v2 := strings.Split(minVersion, ".") + + for i := 0; i < 3; i++ { + num1, err1 := strconv.Atoi(v1[i]) + num2, err2 := strconv.Atoi(v2[i]) + if err1 != nil || err2 != nil { + return false + } + + if num1 > num2 { + return true + } else if num1 < num2 { + return false + } + } + + // versions are equal + return true +}