Skip to content

Commit

Permalink
feat: rollkit rebuild command to update entrypoint (rollkit#1707)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## 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.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Javed Khan <tuxcanfly@gmail.com>
  • Loading branch information
yarikbratashchuk and tuxcanfly authored Jun 21, 2024
1 parent d674e80 commit f7d735e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 17 deletions.
43 changes: 26 additions & 17 deletions cmd/rollkit/commands/intercept.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
27 changes: 27 additions & 0 deletions cmd/rollkit/commands/rebuild.go
Original file line number Diff line number Diff line change
@@ -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)
}
},
}
1 change: 1 addition & 0 deletions cmd/rollkit/docs/rollkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions cmd/rollkit/docs/rollkit_rebuild.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions cmd/rollkit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f7d735e

Please sign in to comment.