Skip to content

Commit

Permalink
Feature: devrunner config refactor (#146)
Browse files Browse the repository at this point in the history
* networks config refactor

* services versions are now controlled by the networks config

* dev version command now reads network config

* init now downloads all services for all networks

* wip refactor of process runner creation

* implement generic service start based on network config

* Errorf typo

* dynamic base config

* add panic on unknown network name, comment updates

* file renames

* update binaries config to structs

* move service version config values

* comment updates

* created GetUserHomeDirOrPanic helper function

* NewBaseConfig to DefaultBaseConfig

* update var names, remove unneeded func
  • Loading branch information
sambukowski committed Aug 6, 2024
1 parent 58a9817 commit 0fd7d93
Show file tree
Hide file tree
Showing 16 changed files with 639 additions and 529 deletions.
145 changes: 145 additions & 0 deletions modules/cli/cmd/devrunner/config/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package config

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/astriaorg/astria-cli-go/modules/cli/cmd"
"github.com/pelletier/go-toml/v2"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

// BaseConfig is a map of string key-value pairs that represent the base
// configuration for all services in the Astria stack. The key-values pairs are
// also parsed into environment variables for the services to consume. A map was
// used here to allow for dynamically adding new configuration options to the
// config toml file.
type BaseConfig map[string]string

// DefaultBaseConfig returns a BaseConfig with default values.
func DefaultBaseConfig(instanceName string) BaseConfig {
homeDir := cmd.GetUserHomeDirOrPanic()
return map[string]string{
// conductor
"astria_conductor_celestia_block_time_ms": "1200",
"astria_conductor_celestia_bearer_token": "<JWT Bearer token>",
"astria_conductor_celestia_node_http_url": "http://127.0.0.1:26658",
"astria_conductor_execution_rpc_url": "http://127.0.0.1:50051",
"astria_conductor_execution_commit_level": "SoftOnly",
"astria_conductor_log": "astria_conductor=info",
"astria_conductor_no_otel": "true",
"astria_conductor_force_stdout": "true",
"astria_conductor_pretty_print": "true",
"astria_conductor_sequencer_grpc_url": "http://127.0.0.1:8080",
"astria_conductor_sequencer_cometbft_url": "http://127.0.0.1:26657",
"astria_conductor_sequencer_block_time_ms": "2000",
"astria_conductor_sequencer_requests_per_second": "500",
"astria_conductor_no_metrics": "true",
"astria_conductor_metrics_http_listener_addr": "127.0.0.1:9000",

// sequencer
"astria_sequencer_listen_addr": "127.0.0.1:26658",
"astria_sequencer_db_filepath": filepath.Join(homeDir, ".astria", instanceName, DataDirName, "astria_sequencer_db"),
"astria_sequencer_enable_mint": "false",
"astria_sequencer_grpc_addr": "127.0.0.1:8080",
"astria_sequencer_log": "astria_sequencer=info",
"astria_sequencer_no_otel": "true",
"astria_sequencer_force_stdout": "true",
"astria_sequencer_no_metrics": "true",
"astria_sequencer_metrics_http_listener_addr": "127.0.0.1:9000",
"astria_sequencer_pretty_print": "true",

// composer
"astria_composer_log": "astria_composer=info",
"astria_composer_no_otel": "true",
"astria_composer_force_stdout": "true",
"astria_composer_pretty_print": "true",
"astria_composer_api_listen_addr": "0.0.0.0:0",
"astria_composer_sequencer_url": "http://127.0.0.1:26657",
"astria_composer_sequencer_chain_id": "astria-dusk-" + duskNum,
"astria_composer_rollups": "astriachain::ws://127.0.0.1:8546",
"astria_composer_private_key_file": filepath.Join(homeDir, ".astria", instanceName, DefaultConfigDirName, "composer_dev_priv_key"),
"astria_composer_sequencer_address_prefix": "astria",
"astria_composer_max_submit_interval_ms": "2000",
"astria_composer_max_bytes_per_bundle": "200000",
"astria_composer_bundle_queue_capacity": "40000",
"astria_composer_no_metrics": "true",
"astria_composer_metrics_http_listener_addr": "127.0.0.1:9000",
"astria_composer_grpc_addr": "0.0.0.0:0",
"astria_composer_fee_asset": "nria",

// ANSI
"no_color": "",

// otel
"otel_exporter_otlp_endpoint": "http://localhost:4317",
"otel_exporter_otlp_traces_endpoint": "http://localhost:4317/v1/traces",
"otel_exporter_otlp_traces_timeout": "10",
"otel_exporter_otlp_traces_compression": "gzip",
"otel_exporter_otlp_headers": "key1=value1,key2=value2",
"otel_exporter_otlp_trace_headers": "key1=value1,key2=value2",
}
}

// CreateBaseConfig creates a base configuration file at
// the given path, populating the file with the service defaults.
// It will skip initialization if the file already exists. It will panic if the
// file cannot be created or written to.
func CreateBaseConfig(path, instance string) {
_, err := os.Stat(path)
if err == nil {
log.Infof("%s already exists. Skipping initialization.\n", path)
return
}
// create an instance of the Config struct with some data
config := DefaultBaseConfig(instance)

// open a file for writing
file, err := os.Create(path)
if err != nil {
panic(err)
}
defer file.Close()

// encode the struct to TOML and write to the file
if err := toml.NewEncoder(file).Encode(config); err != nil {
panic(err)
}
log.Infof("New network config file created successfully: %s\n", path)
}

// LoadBaseConfigOrPanic loads the BaseConfig from the given path. If the file
// cannot be loaded or parsed, the function will panic.
func LoadBaseConfigOrPanic(path string) BaseConfig {
viper.SetConfigFile(path)

if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file, %s", err)
panic(err)
}

// var config BaseConfig
config := make(map[string]string)
if err := viper.Unmarshal(&config); err != nil {
log.Fatalf("Unable to decode into struct, %v", err)
panic(err)
}

return config
}

// ToSlice creates a []string of "key=value" pairs out of a BaseConfig.
// The variable name will become the env var key and that variable's value will
// be the value.
func (b BaseConfig) ToSlice() []string {
var output []string

for key, value := range b {
output = append(output, fmt.Sprintf("%s=%s", strings.ToUpper(key), value))
}

return output
}
38 changes: 31 additions & 7 deletions modules/cli/cmd/devrunner/config/binaries_config_darwin_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,37 @@
package config

type Binary struct {
Name string
Url string
Name string
Version string
Url string
}

var Binaries = []Binary{
{"cometbft", "https://github.com/cometbft/cometbft/releases/download/v" + CometbftVersion + "/cometbft_" + CometbftVersion + "_darwin_amd64.tar.gz"},
{"astria-sequencer", "https://github.com/astriaorg/astria/releases/download/sequencer-v" + AstriaSequencerVersion + "/astria-sequencer-x86_64-apple-darwin.tar.gz"},
{"astria-composer", "https://github.com/astriaorg/astria/releases/download/composer-v" + AstriaComposerVersion + "/astria-composer-x86_64-apple-darwin.tar.gz"},
{"astria-conductor", "https://github.com/astriaorg/astria/releases/download/conductor-v" + AstriaConductorVersion + "/astria-conductor-x86_64-apple-darwin.tar.gz"},
type Binaries struct {
CometBFT Binary
AstriaSequencer Binary
AstriaComposer Binary
AstriaConductor Binary
}

var KnownBinaries = Binaries{
CometBFT: Binary{
Name: "cometbft",
Version: "v" + CometbftVersion,
Url: "https://github.com/cometbft/cometbft/releases/download/v" + CometbftVersion + "/cometbft_" + CometbftVersion + "_darwin_amd64.tar.gz",
},
AstriaSequencer: Binary{
Name: "astria-sequencer",
Version: "v" + AstriaSequencerVersion,
Url: "https://github.com/astriaorg/astria/releases/download/sequencer-v" + AstriaSequencerVersion + "/astria-sequencer-x86_64-apple-darwin.tar.gz",
},
AstriaComposer: Binary{
Name: "astria-composer",
Version: "v" + AstriaComposerVersion,
Url: "https://github.com/astriaorg/astria/releases/download/composer-v" + AstriaComposerVersion + "/astria-composer-x86_64-apple-darwin.tar.gz",
},
AstriaConductor: Binary{
Name: "astria-conductor",
Version: "v" + AstriaConductorVersion,
Url: "https://github.com/astriaorg/astria/releases/download/conductor-v" + AstriaConductorVersion + "/astria-conductor-x86_64-apple-darwin.tar.gz",
},
}
38 changes: 31 additions & 7 deletions modules/cli/cmd/devrunner/config/binaries_config_darwin_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,37 @@
package config

type Binary struct {
Name string
Url string
Name string
Version string
Url string
}

var Binaries = []Binary{
{"cometbft", "https://github.com/cometbft/cometbft/releases/download/v" + CometbftVersion + "/cometbft_" + CometbftVersion + "_darwin_arm64.tar.gz"},
{"astria-sequencer", "https://github.com/astriaorg/astria/releases/download/sequencer-v" + AstriaSequencerVersion + "/astria-sequencer-aarch64-apple-darwin.tar.gz"},
{"astria-composer", "https://github.com/astriaorg/astria/releases/download/composer-v" + AstriaComposerVersion + "/astria-composer-aarch64-apple-darwin.tar.gz"},
{"astria-conductor", "https://github.com/astriaorg/astria/releases/download/conductor-v" + AstriaConductorVersion + "/astria-conductor-aarch64-apple-darwin.tar.gz"},
type Binaries struct {
CometBFT Binary
AstriaSequencer Binary
AstriaComposer Binary
AstriaConductor Binary
}

var KnownBinaries = Binaries{
CometBFT: Binary{
Name: "cometbft",
Version: "v" + CometbftVersion,
Url: "https://github.com/cometbft/cometbft/releases/download/v" + CometbftVersion + "/cometbft_" + CometbftVersion + "_darwin_arm64.tar.gz",
},
AstriaSequencer: Binary{
Name: "astria-sequencer",
Version: "v" + AstriaSequencerVersion,
Url: "https://github.com/astriaorg/astria/releases/download/sequencer-v" + AstriaSequencerVersion + "/astria-sequencer-aarch64-apple-darwin.tar.gz",
},
AstriaComposer: Binary{
Name: "astria-composer",
Version: "v" + AstriaComposerVersion,
Url: "https://github.com/astriaorg/astria/releases/download/composer-v" + AstriaComposerVersion + "/astria-composer-aarch64-apple-darwin.tar.gz",
},
AstriaConductor: Binary{
Name: "astria-conductor",
Version: "v" + AstriaConductorVersion,
Url: "https://github.com/astriaorg/astria/releases/download/conductor-v" + AstriaConductorVersion + "/astria-conductor-aarch64-apple-darwin.tar.gz",
},
}
38 changes: 31 additions & 7 deletions modules/cli/cmd/devrunner/config/binaries_config_linux_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,37 @@
package config

type Binary struct {
Name string
Url string
Name string
Version string
Url string
}

var Binaries = []Binary{
{"cometbft", "https://github.com/cometbft/cometbft/releases/download/v" + CometbftVersion + "/cometbft_" + CometbftVersion + "_linux_amd64.tar.gz"},
{"astria-sequencer", "https://github.com/astriaorg/astria/releases/download/sequencer-v" + AstriaSequencerVersion + "/astria-sequencer-x86_64-unknown-linux-gnu.tar.gz"},
{"astria-composer", "https://github.com/astriaorg/astria/releases/download/composer-v" + AstriaComposerVersion + "/astria-composer-x86_64-unknown-linux-gnu.tar.gz"},
{"astria-conductor", "https://github.com/astriaorg/astria/releases/download/conductor-v" + AstriaConductorVersion + "/astria-conductor-x86_64-unknown-linux-gnu.tar.gz"},
type Binaries struct {
CometBFT Binary
AstriaSequencer Binary
AstriaComposer Binary
AstriaConductor Binary
}

var KnownBinaries = Binaries{
CometBFT: Binary{
Name: "cometbft",
Version: "v" + CometbftVersion,
Url: "https://github.com/cometbft/cometbft/releases/download/v" + CometbftVersion + "/cometbft_" + CometbftVersion + "_linux_amd64.tar.gz",
},
AstriaSequencer: Binary{
Name: "astria-sequencer",
Version: "v" + AstriaSequencerVersion,
Url: "https://github.com/astriaorg/astria/releases/download/sequencer-v" + AstriaSequencerVersion + "/astria-sequencer-x86_64-unknown-linux-gnu.tar.gz",
},
AstriaComposer: Binary{
Name: "astria-composer",
Version: "v" + AstriaComposerVersion,
Url: "https://github.com/astriaorg/astria/releases/download/composer-v" + AstriaComposerVersion + "/astria-composer-x86_64-unknown-linux-gnu.tar.gz",
},
AstriaConductor: Binary{
Name: "astria-conductor",
Version: "v" + AstriaConductorVersion,
Url: "https://github.com/astriaorg/astria/releases/download/conductor-v" + AstriaConductorVersion + "/astria-conductor-x86_64-unknown-linux-gnu.tar.gz",
},
}
8 changes: 8 additions & 0 deletions modules/cli/cmd/devrunner/config/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

const (
duskNum = "9"
dawnNum = "0"
BinariesDirName = "bin"
DataDirName = "data"
DefaultBaseConfigName = "base-config.toml"
Expand All @@ -13,4 +15,10 @@ const (
DefaultServiceLogLevel = "info"
DefaultTargetNetwork = "local"
LocalNativeDenom = "nria"

// NOTE - do not include the 'v' at the beginning of the version number
CometbftVersion = "0.38.8"
AstriaSequencerVersion = "0.15.0"
AstriaComposerVersion = "0.8.1"
AstriaConductorVersion = "0.19.0"
)
4 changes: 2 additions & 2 deletions modules/cli/cmd/devrunner/config/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ func RecreateCometbftAndSequencerGenesisData(path, localNetworkName, localNative
}

// InitCometbft initializes CometBFT for running a local sequencer.
func InitCometbft(defaultDir string, dataDirName string, binDirName string, configDirName string) {
func InitCometbft(defaultDir string, dataDirName string, binDirName string, binVersion string, configDirName string) {
log.Info("Initializing CometBFT for running local sequencer:")
cometbftDataPath := filepath.Join(defaultDir, dataDirName, ".cometbft")

// verify that cometbft was downloaded and extracted to the correct location
cometbftCmdPath := filepath.Join(defaultDir, binDirName, "cometbft")
cometbftCmdPath := filepath.Join(defaultDir, binDirName, "cometbft-v"+binVersion)
if !util.PathExists(cometbftCmdPath) {
log.Error("Error: cometbft binary not found here", cometbftCmdPath)
log.Error("\tCannot continue with initialization.")
Expand Down
Loading

0 comments on commit 0fd7d93

Please sign in to comment.