Skip to content

Feat/two chains support #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions examples/running-two-chains.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Running two chains

This example shows how to run two chains on the same machine.

First, we need to deploy the first chain:

```bash
$ go run main.go cook opstack
```

This chain is going to run under the default `ethplayground` Docker network. Playground uses DNS resolution to discover services in the same network.

In order to run a second chain, we can use the same command and specify a different network name:

```bash
$ go run main.go cook opstack --network eth2
```

This will deploy the second chain under the `eth2` Docker network.
18 changes: 13 additions & 5 deletions internal/local_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"gopkg.in/yaml.v2"
)

const networkName = "ethplayground"
const defaultNetworkName = "ethplayground"

// LocalRunner is a component that runs the services from the manifest on the local host machine.
// By default, it uses docker and docker compose to run all the services.
Expand Down Expand Up @@ -67,6 +67,9 @@ type LocalRunner struct {
// sessionID is a random sequence that is used to identify the session
// it is used to identify the containers in the cleanup process
sessionID string

// networkName is the name of the network to use for the services
networkName string
}

type task struct {
Expand Down Expand Up @@ -96,7 +99,8 @@ func newDockerClient() (*client.Client, error) {
return client, nil
}

func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool) (*LocalRunner, error) {
// TODO: add a runner config struct
func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool, networkName string) (*LocalRunner, error) {
client, err := newDockerClient()
if err != nil {
return nil, fmt.Errorf("failed to create docker client: %w", err)
Expand Down Expand Up @@ -141,6 +145,9 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
}
}

if networkName == "" {
networkName = defaultNetworkName
}
d := &LocalRunner{
out: out,
manifest: manifest,
Expand All @@ -153,6 +160,7 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
exitErr: make(chan error, 2),
bindHostPortsLocally: bindHostPortsLocally,
sessionID: uuid.New().String(),
networkName: networkName,
}

if interactive {
Expand Down Expand Up @@ -563,7 +571,7 @@ func (d *LocalRunner) toDockerComposeService(s *service) (map[string]interface{}
fmt.Sprintf("%s:/artifacts", outputFolder),
},
// Add the ethereum network
"networks": []string{networkName},
"networks": []string{d.networkName},
"labels": labels,
}

Expand Down Expand Up @@ -648,8 +656,8 @@ func (d *LocalRunner) generateDockerCompose() ([]byte, error) {
// We create a new network to be used by all the services so that
// we can do DNS discovery between them.
"networks": map[string]interface{}{
networkName: map[string]interface{}{
"name": networkName,
d.networkName: map[string]interface{}{
"name": d.networkName,
},
},
}
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var timeout time.Duration
var logLevelFlag string
var bindExternal bool
var withPrometheus bool
var networkName string

var rootCmd = &cobra.Command{
Use: "playground",
Expand Down Expand Up @@ -171,6 +172,7 @@ func main() {
recipeCmd.Flags().StringVar(&logLevelFlag, "log-level", "info", "log level")
recipeCmd.Flags().BoolVar(&bindExternal, "bind-external", false, "bind host ports to external interface")
recipeCmd.Flags().BoolVar(&withPrometheus, "with-prometheus", false, "whether to gather the Prometheus metrics")
recipeCmd.Flags().StringVar(&networkName, "network", "", "network name")

cookCmd.AddCommand(recipeCmd)
}
Expand Down Expand Up @@ -244,7 +246,7 @@ func runIt(recipe internal.Recipe) error {
}
}

dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal)
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal, networkName)
if err != nil {
return fmt.Errorf("failed to create docker runner: %w", err)
}
Expand Down