From f7d735e72de1806c631a2934b9822b2e64147de3 Mon Sep 17 00:00:00 2001 From: Yarik Bratashchuk Date: Fri, 21 Jun 2024 17:00:44 +0200 Subject: [PATCH] feat: rollkit rebuild command to update entrypoint (#1707) ## Summary by CodeRabbit - **New Features** - Introduced a `rebuild` command to the CLI for rebuilding the rollup entrypoint based on configuration in `rollkit.toml`. - **Refactor** - Improved the logic for building and running entrypoint binaries by introducing a dedicated function to handle the build process. --------- Co-authored-by: Javed Khan --- cmd/rollkit/commands/intercept.go | 43 +++++++++++++++++------------ cmd/rollkit/commands/rebuild.go | 27 ++++++++++++++++++ cmd/rollkit/docs/rollkit.md | 1 + cmd/rollkit/docs/rollkit_rebuild.md | 29 +++++++++++++++++++ cmd/rollkit/main.go | 1 + 5 files changed, 84 insertions(+), 17 deletions(-) create mode 100644 cmd/rollkit/commands/rebuild.go create mode 100644 cmd/rollkit/docs/rollkit_rebuild.md diff --git a/cmd/rollkit/commands/intercept.go b/cmd/rollkit/commands/intercept.go index 2981fc6c2..363db8918 100644 --- a/cmd/rollkit/commands/intercept.go +++ b/cmd/rollkit/commands/intercept.go @@ -65,6 +65,28 @@ readTOML: return true, runEntrypoint(&rollkitConfig, flags) } +func buildEntrypoint(rootDir, entrypointSourceFile string, forceRebuild bool) (string, error) { + // The entrypoint binary file is always in the same directory as the rollkit.toml file. + entrypointBinaryFile := filepath.Join(rootDir, rollupBinEntrypoint) + + if !cometos.FileExists(entrypointBinaryFile) || forceRebuild { + if !cometos.FileExists(entrypointSourceFile) { + return "", fmt.Errorf("no entrypoint source file: %s", entrypointSourceFile) + } + + // try to build the entrypoint as a go binary + buildArgs := []string{"build", "-o", entrypointBinaryFile, entrypointSourceFile} + buildCmd := exec.Command("go", buildArgs...) //nolint:gosec + buildCmd.Stdout = os.Stdout + buildCmd.Stderr = os.Stderr + if err := buildCmd.Run(); err != nil { + return "", fmt.Errorf("failed to build entrypoint: %w", err) + } + } + + return entrypointBinaryFile, nil +} + // RunRollupEntrypoint runs the entrypoint specified in the rollkit.toml configuration file. // If the entrypoint is not built, it will build it first. The entrypoint is built // in the same directory as the rollkit.toml file. The entrypoint is run with the @@ -79,22 +101,9 @@ func RunRollupEntrypoint(rollkitConfig *rollconf.TomlConfig, args []string) erro entrypointSourceFile = rollkitConfig.Entrypoint } - // The entrypoint binary file is always in the same directory as the rollkit.toml file. - entrypointBinaryFile := filepath.Join(rollkitConfig.RootDir, rollupBinEntrypoint) - - if !cometos.FileExists(entrypointBinaryFile) { - if !cometos.FileExists(entrypointSourceFile) { - return fmt.Errorf("no entrypoint file: %s", entrypointSourceFile) - } - - // try to build the entrypoint as a go binary - buildArgs := []string{"build", "-o", entrypointBinaryFile, entrypointSourceFile} - buildCmd := exec.Command("go", buildArgs...) //nolint:gosec - buildCmd.Stdout = os.Stdout - buildCmd.Stderr = os.Stderr - if err := buildCmd.Run(); err != nil { - return fmt.Errorf("failed to build entrypoint: %w", err) - } + entrypointBinaryFilePath, err := buildEntrypoint(rollkitConfig.RootDir, entrypointSourceFile, false) + if err != nil { + return err } var runArgs []string @@ -105,7 +114,7 @@ func RunRollupEntrypoint(rollkitConfig *rollconf.TomlConfig, args []string) erro runArgs = append(runArgs, "--home", rollkitConfig.Chain.ConfigDir) } - entrypointCmd := exec.Command(entrypointBinaryFile, runArgs...) //nolint:gosec + entrypointCmd := exec.Command(entrypointBinaryFilePath, runArgs...) //nolint:gosec entrypointCmd.Stdout = os.Stdout entrypointCmd.Stderr = os.Stderr diff --git a/cmd/rollkit/commands/rebuild.go b/cmd/rollkit/commands/rebuild.go new file mode 100644 index 000000000..369e0a61b --- /dev/null +++ b/cmd/rollkit/commands/rebuild.go @@ -0,0 +1,27 @@ +package commands + +import ( + "log" + + rollconf "github.com/rollkit/rollkit/config" + + "github.com/spf13/cobra" +) + +// RebuildCmd is a command to rebuild rollup entrypoint +var RebuildCmd = &cobra.Command{ + Use: "rebuild", + Short: "Rebuild rollup entrypoint", + Long: "Rebuild rollup entrypoint specified in the rollkit.toml", + Run: func(cmd *cobra.Command, args []string) { + var err error + rollkitConfig, err = rollconf.ReadToml() + if err != nil { + log.Fatalf("Could not read rollkit.toml file: %s", err) + } + + if _, err := buildEntrypoint(rollkitConfig.RootDir, rollkitConfig.Entrypoint, true); err != nil { + log.Fatalf("Could not rebuild rollup entrypoint: %s", err) + } + }, +} diff --git a/cmd/rollkit/docs/rollkit.md b/cmd/rollkit/docs/rollkit.md index 18d3ff2dd..5a4256f7e 100644 --- a/cmd/rollkit/docs/rollkit.md +++ b/cmd/rollkit/docs/rollkit.md @@ -23,6 +23,7 @@ If a path is not specified for RKHOME, the rollkit command will create a folder * [rollkit completion](rollkit_completion.md) - Generate the autocompletion script for the specified shell * [rollkit docs-gen](rollkit_docs-gen.md) - Generate documentation for rollkit CLI +* [rollkit rebuild](rollkit_rebuild.md) - Rebuild rollup entrypoint * [rollkit start](rollkit_start.md) - Run the rollkit node * [rollkit toml](rollkit_toml.md) - TOML file operations * [rollkit version](rollkit_version.md) - Show version info diff --git a/cmd/rollkit/docs/rollkit_rebuild.md b/cmd/rollkit/docs/rollkit_rebuild.md new file mode 100644 index 000000000..99ef5118c --- /dev/null +++ b/cmd/rollkit/docs/rollkit_rebuild.md @@ -0,0 +1,29 @@ +## rollkit rebuild + +Rebuild rollup entrypoint + +### Synopsis + +Rebuild rollup entrypoint specified in the rollkit.toml + +``` +rollkit rebuild [flags] +``` + +### Options + +``` + -h, --help help for rebuild +``` + +### Options inherited from parent commands + +``` + --home string directory for config and data (default "HOME/.rollkit") + --log_level string set the log level; default is info. other options include debug, info, error, none (default "info") + --trace print out full stack trace on errors +``` + +### SEE ALSO + +* [rollkit](rollkit.md) - The first sovereign rollup framework that allows you to launch a sovereign, customizable blockchain as easily as a smart contract. diff --git a/cmd/rollkit/main.go b/cmd/rollkit/main.go index 8ae6c8057..8c72265ce 100644 --- a/cmd/rollkit/main.go +++ b/cmd/rollkit/main.go @@ -21,6 +21,7 @@ func main() { cmd.NewRunNodeCmd(), cmd.VersionCmd, cmd.NewTomlCmd(), + cmd.RebuildCmd, ) // In case there is a rollkit.toml file in the current dir or somewhere up the