Skip to content
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
2 changes: 1 addition & 1 deletion ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
DefaultPackageRepository = "github.com/ethpandaops/ethereum-package"
// DefaultPackageVersion is the pinned version of ethereum-package
// See https://github.com/ethpandaops/ethereum-package/pull/1013 (v6.0.0).
DefaultPackageVersion = "release-please--branches--main--components--ethereum-package"
DefaultPackageVersion = "main"
)

// RunOption configures how the Ethereum network is started
Expand Down
6 changes: 6 additions & 0 deletions examples/extra_files/configs/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Sample validator configuration
graffiti: "ethereum-package-go extra_files example"
metrics:
enabled: true
address: 0.0.0.0
port: 8008
105 changes: 105 additions & 0 deletions examples/extra_files/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package main

import (
"context"
"fmt"
"log"

"github.com/ethpandaops/ethereum-package-go"
"github.com/ethpandaops/ethereum-package-go/pkg/client"
"github.com/ethpandaops/ethereum-package-go/pkg/config"
)

func main() {
ctx := context.Background()

// Method 1: Using ExtraFilesHelper for convenience
helper := config.NewExtraFilesHelper()

// Add inline content
helper.AddFile("beacon-config.yaml", `
# Beacon node configuration
metrics:
enabled: true
port: 8080
logging:
level: info
`)

// Add from file (if it exists)
if err := helper.AddFileFromPath("validator-config.yaml", "./configs/sample.yaml"); err != nil {
log.Printf("Note: sample.yaml not found, using inline example: %v", err)
helper.AddFile("validator-config.yaml", "graffiti: 'ethereum-package-go example'")
}

// Add JSON config
jsonConfig := map[string]interface{}{
"version": "1.0",
"features": map[string]bool{
"metrics": true,
"tracing": false,
},
}
helper.AddJSON("features.json", jsonConfig)

// Method 2: Direct inline definition
participant := config.NewParticipantBuilder().
WithEL(client.Geth).
WithCL(client.Lighthouse).
WithCLExtraMounts(map[string]string{
"/configs/beacon.yaml": "beacon-config.yaml",
"/configs/features.json": "features.json",
}).
WithCLExtraParams([]string{
"--config-file=/configs/beacon.yaml",
}).
WithVCExtraMounts(map[string]string{
"/configs/validator.yaml": "validator-config.yaml",
}).
Build()

// Build network configuration with extra files at root level
networkConfig, err := config.NewConfigBuilder().
WithParticipant(participant).
WithNetworkParams(&config.NetworkParams{
Network: "kurtosis",
SecondsPerSlot: 12,
NumValidatorKeysPerNode: 32,
}).
WithExtraFiles(helper.Build()).
Build()

if err != nil {
log.Fatalf("Failed to build config: %v", err)
}

fmt.Printf("Starting network with %d extra files...\n", helper.Count())

// Start the network
network, err := ethereum.Run(ctx, ethereum.WithConfig(networkConfig))
if err != nil {
log.Fatalf("Failed to start network: %v", err)
}

fmt.Println("Network started successfully!")
fmt.Println("Extra files have been mounted into the containers.")

// Display network info
execClients := network.ExecutionClients()
consClients := network.ConsensusClients()
fmt.Printf("\nExecution clients: %d\n", len(execClients.All()))
fmt.Printf("Consensus clients: %d\n", len(consClients.All()))

// Wait for user input
fmt.Println("\nPress Enter to stop the network...")
var input string
fmt.Scanln(&input)

// Cleanup
fmt.Println("Stopping network...")
if err := network.Cleanup(ctx); err != nil {
log.Fatalf("Failed to cleanup: %v", err)
}

fmt.Println("Network stopped successfully!")
}
15 changes: 15 additions & 0 deletions pkg/config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ func (b *ConfigBuilder) WithDockerCacheParams(dockerCache *DockerCacheParams) *C
return b
}

// WithExtraFile adds a single extra file to the configuration
func (b *ConfigBuilder) WithExtraFile(name, content string) *ConfigBuilder {
if b.config.ExtraFiles == nil {
b.config.ExtraFiles = make(map[string]string)
}
b.config.ExtraFiles[name] = content
return b
}

// WithExtraFiles sets all extra files at once
func (b *ConfigBuilder) WithExtraFiles(files map[string]string) *ConfigBuilder {
b.config.ExtraFiles = files
return b
}

// WithEthereumMetricsExporterEnabled sets the ethereum metrics exporter enabled
func (b *ConfigBuilder) WithEthereumMetricsExporterEnabled(enabled bool) *ConfigBuilder {
b.config.EthereumMetricsExporterEnabled = &enabled
Expand Down
87 changes: 87 additions & 0 deletions pkg/config/extra_files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package config

import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
)

// ExtraFilesHelper provides utilities for working with extra files
type ExtraFilesHelper struct {
files map[string]string
}

// NewExtraFilesHelper creates a new helper for managing extra files
func NewExtraFilesHelper() *ExtraFilesHelper {
return &ExtraFilesHelper{
files: make(map[string]string),
}
}

// AddFile adds a file with inline content
func (h *ExtraFilesHelper) AddFile(name, content string) *ExtraFilesHelper {
h.files[name] = content
return h
}

// AddFileFromPath reads a file from disk and adds it
func (h *ExtraFilesHelper) AddFileFromPath(name, path string) error {
content, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file %s: %w", path, err)
}
h.files[name] = string(content)
return nil
}

// AddFileFromReader reads content from an io.Reader
func (h *ExtraFilesHelper) AddFileFromReader(name string, r io.Reader) error {
content, err := io.ReadAll(r)
if err != nil {
return fmt.Errorf("failed to read content: %w", err)
}
h.files[name] = string(content)
return nil
}

// AddJSON marshals an object to JSON and adds it as a file
func (h *ExtraFilesHelper) AddJSON(name string, v interface{}) error {
content, err := json.MarshalIndent(v, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal JSON: %w", err)
}
h.files[name] = string(content)
return nil
}

// AddFilesFromDirectory adds all files from a directory
func (h *ExtraFilesHelper) AddFilesFromDirectory(dir string) error {
entries, err := os.ReadDir(dir)
if err != nil {
return fmt.Errorf("failed to read directory %s: %w", dir, err)
}

for _, entry := range entries {
if !entry.IsDir() {
path := filepath.Join(dir, entry.Name())
content, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file %s: %w", path, err)
}
h.files[entry.Name()] = string(content)
}
}
return nil
}

// Build returns the files map for use in NetworkParams
func (h *ExtraFilesHelper) Build() map[string]string {
return h.files
}

// Count returns the number of files
func (h *ExtraFilesHelper) Count() int {
return len(h.files)
}
Loading
Loading