Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

feat: support getting config from homes for query commands #619

Merged
merged 6 commits into from
Nov 28, 2023
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
6 changes: 3 additions & 3 deletions cmd/blobstream/orchestrator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func LoadFileConfiguration(homeDir string) (*StartConfig, error) {
}
}

conf, err := getStartConfig(v, configPath)
conf, err := GetStartConfig(v, configPath)
if err != nil {
return nil, fmt.Errorf("couldn't get client config: %v", err)
}
Expand Down Expand Up @@ -279,8 +279,8 @@ func writeConfigToFile(configFilePath string, config *StartConfig) error {
return os.WriteFile(configFilePath, buffer.Bytes(), 0o600)
}

// getStartConfig reads values from config.toml file and unmarshalls them into StartConfig
func getStartConfig(v *viper.Viper, configPath string) (*StartConfig, error) {
// GetStartConfig reads values from config.toml file and unmarshalls them into StartConfig
func GetStartConfig(v *viper.Viper, configPath string) (*StartConfig, error) {
v.AddConfigPath(configPath)
v.SetConfigName("config")
v.SetConfigType("toml")
Expand Down
114 changes: 106 additions & 8 deletions cmd/blobstream/query/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import (
"fmt"
"math/big"
"os"
"path/filepath"
"strconv"
"time"

"github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/base"
"github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/orchestrator"
"github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/relayer"
"github.com/spf13/viper"

common2 "github.com/ethereum/go-ethereum/common"

celestiatypes "github.com/celestiaorg/celestia-app/x/qgb/types"
Expand Down Expand Up @@ -52,13 +58,17 @@ func Signers() *cobra.Command {
" will query signatures for. It should be either a specific nonce starting from 2 and on." +
" Or, use 'latest' as argument to check the latest attestation nonce",
RunE: func(cmd *cobra.Command, args []string) error {
config, err := parseFlags(cmd)
// creating the logger
logger := tmlog.NewTMLogger(os.Stdout)
fileConfig, err := tryToGetExistingConfig(cmd, logger)
if err != nil {
return err
}
config, err := parseFlags(cmd, &fileConfig)
if err != nil {
return err
}

// creating the logger
logger := tmlog.NewTMLogger(os.Stdout)
logger.Debug("initializing queriers")

ctx, cancel := context.WithCancel(cmd.Context())
Expand All @@ -75,7 +85,12 @@ func Signers() *cobra.Command {
}()

// create tm querier and app querier
tmQuerier, appQuerier, stops, err := common.NewTmAndAppQuerier(logger, config.coreRPC, config.coreGRPC, config.grpcInsecure)
tmQuerier, appQuerier, stops, err := common.NewTmAndAppQuerier(
logger,
config.coreRPC,
config.coreGRPC,
config.grpcInsecure,
)
stopFuncs = append(stopFuncs, stops...)
if err != nil {
return err
Expand Down Expand Up @@ -335,13 +350,17 @@ func Signature() *cobra.Command {
" nonce that the command will query signatures for. The EVM address is the address registered by the validator " +
"in the staking module.",
RunE: func(cmd *cobra.Command, args []string) error {
config, err := parseFlags(cmd)
// creating the logger
logger := tmlog.NewTMLogger(os.Stdout)
fileConfig, err := tryToGetExistingConfig(cmd, logger)
if err != nil {
return err
}
config, err := parseFlags(cmd, &fileConfig)
if err != nil {
return err
}

// creating the logger
logger := tmlog.NewTMLogger(os.Stdout)
logger.Debug("initializing queriers")

ctx, cancel := context.WithCancel(cmd.Context())
Expand All @@ -358,7 +377,7 @@ func Signature() *cobra.Command {
}()

// create tm querier and app querier
tmQuerier, appQuerier, stops, err := common.NewTmAndAppQuerier(logger, config.coreRPC, config.coreGRPC, config.grpcInsecure)
tmQuerier, appQuerier, stops, err := common.NewTmAndAppQuerier(logger, config.coreRPC, config.coreRPC, config.grpcInsecure)
stopFuncs = append(stopFuncs, stops...)
if err != nil {
return err
Expand Down Expand Up @@ -478,3 +497,82 @@ func getSignatureAndPrintIt(
}
return nil
}

// tryToGetExistingConfig tries to get the query config from existing
// orchestrator/relayer homes. It first checks whether the `--home` flag was
// changed. If so, it gets the config from there. If not, then it tries the
// orchestrator default home directory, then the relayer default home directory.
func tryToGetExistingConfig(cmd *cobra.Command, logger tmlog.Logger) (Config, error) {
v := viper.New()
v.SetEnvPrefix("")
v.AutomaticEnv()
homeDir, changed, err := base.GetHomeFlag(cmd)
if err != nil {
return Config{}, err
}
// the --home flag was set to some directory
if changed && homeDir != "" {
logger.Debug("using home", "home", homeDir)
configPath := filepath.Join(homeDir, "config")

// assume this home is an orchestrator home directory
orchConf, err := orchestrator.GetStartConfig(v, configPath)
if err == nil {
// it is an orchestrator, so we get the config from it
return *NewPartialConfig(
orchConf.CoreGRPC,
orchConf.CoreRPC,
orchConf.Bootstrappers,
orchConf.GRPCInsecure,
), nil
}

// assume this home is a relayer home directory
relConf, err := relayer.GetStartConfig(v, configPath)
if err == nil {
// it is a relayer, so we get the config from it
return *NewPartialConfig(
relConf.CoreGRPC,
relConf.CoreRPC,
relConf.Bootstrappers,
relConf.GrpcInsecure,
), nil
}
return Config{}, fmt.Errorf("the provided home directory is neither an orchestrator nor a relayer home directory")
}
// try to get the config from the orchestrator home directory
orchHome, err := base.GetHomeDirectory(cmd, orchestrator.ServiceNameOrchestrator)
if err != nil {
return Config{}, err
}
orchConf, err := orchestrator.GetStartConfig(v, filepath.Join(orchHome, "config"))
if err == nil {
// found orchestrator home, get the config from it
logger.Debug("using home", "home", orchHome)
return *NewPartialConfig(
orchConf.CoreGRPC,
orchConf.CoreRPC,
orchConf.Bootstrappers,
orchConf.GRPCInsecure,
), nil
}

// try to get the config from the relayer home directory
relHome, err := base.GetHomeDirectory(cmd, relayer.ServiceNameRelayer)
if err != nil {
return Config{}, err
}
relConf, err := relayer.GetStartConfig(v, filepath.Join(relHome, "config"))
if err == nil {
// found relayer home, so we get the config from it
logger.Debug("using home", "home", relHome)
return *NewPartialConfig(
relConf.CoreGRPC,
relConf.CoreRPC,
relConf.Bootstrappers,
relConf.GrpcInsecure,
), nil
}

return *DefaultConfig(), nil
}
70 changes: 55 additions & 15 deletions cmd/blobstream/query/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func addFlags(cmd *cobra.Command) *cobra.Command {
cmd.Flags().String(FlagP2PNode, "", "P2P target node multiaddress (eg. /ip4/127.0.0.1/tcp/30000/p2p/12D3KooWBSMasWzRSRKXREhediFUwABNZwzJbkZcYz5rYr9Zdmfn)")
cmd.Flags().String(FlagOutputFile, "", "Path to an output file path if the results need to be written to a json file. Leaving it as empty will result in printing the result to stdout")
base.AddGRPCInsecureFlag(cmd)

cmd.Flags().String(base.FlagHome, "", "The Blobstream orchestrator|relayer home directory. If this flag is not set, it will try the orchestrator's default home directory, then the relayer's default home directory to get the necessary configuration")
return cmd
}

Expand All @@ -31,35 +31,75 @@ type Config struct {
grpcInsecure bool
}

func parseFlags(cmd *cobra.Command) (Config, error) {
coreRPC, err := cmd.Flags().GetString(base.FlagCoreRPC)
func NewPartialConfig(coreGRPC, coreRPC, targetNode string, grpcInsecure bool) *Config {
return &Config{
coreGRPC: coreGRPC,
coreRPC: coreRPC,
targetNode: targetNode,
grpcInsecure: grpcInsecure,
}
}

func DefaultConfig() *Config {
return &Config{
coreGRPC: "localhost:9090",
coreRPC: "tcp://localhost:26657",
targetNode: "",
outputFile: "",
grpcInsecure: true,
}
}

func parseFlags(cmd *cobra.Command, startConf *Config) (Config, error) {
coreRPC, changed, err := base.GetCoreRPCFlag(cmd)
if err != nil {
return Config{}, err
}
if !strings.HasPrefix(coreRPC, "tcp://") {
coreRPC = fmt.Sprintf("tcp://%s", coreRPC)
if changed {
if !strings.HasPrefix(coreRPC, "tcp://") {
coreRPC = fmt.Sprintf("tcp://%s", coreRPC)
}
startConf.coreRPC = coreRPC
}
coreGRPC, err := cmd.Flags().GetString(base.FlagCoreGRPC)

coreGRPC, changed, err := base.GetCoreGRPCFlag(cmd)
if err != nil {
return Config{}, err
}
targetNode, err := cmd.Flags().GetString(FlagP2PNode)
if changed {
startConf.coreGRPC = coreGRPC
}

targetNode, changed, err := getP2PNodeFlag(cmd)
if err != nil {
return Config{}, err
}
if changed {
startConf.targetNode = targetNode
}

outputFile, err := cmd.Flags().GetString(FlagOutputFile)
if err != nil {
return Config{}, err
}
grpcInsecure, err := cmd.Flags().GetBool(base.FlagGRPCInsecure)
startConf.outputFile = outputFile

grpcInsecure, changed, err := base.GetGRPCInsecureFlag(cmd)
if err != nil {
return Config{}, err
}
return Config{
coreGRPC: coreGRPC,
coreRPC: coreRPC,
targetNode: targetNode,
outputFile: outputFile,
grpcInsecure: grpcInsecure,
}, nil
if changed {
startConf.grpcInsecure = grpcInsecure
}

return *startConf, nil
}

func getP2PNodeFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagP2PNode)
val, err := cmd.Flags().GetString(FlagP2PNode)
if err != nil {
return "", changed, err
}
return val, changed, nil
}
6 changes: 3 additions & 3 deletions cmd/blobstream/relayer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func LoadFileConfiguration(homeDir string) (*StartConfig, error) {
}
}

conf, err := getStartConfig(v, configPath)
conf, err := GetStartConfig(v, configPath)
if err != nil {
return nil, fmt.Errorf("couldn't get client config: %v", err)
}
Expand Down Expand Up @@ -357,8 +357,8 @@ func writeConfigToFile(configFilePath string, config *StartConfig) error {
return os.WriteFile(configFilePath, buffer.Bytes(), 0o600)
}

// getStartConfig reads values from config.toml file and unmarshalls them into StartConfig
func getStartConfig(v *viper.Viper, configPath string) (*StartConfig, error) {
// GetStartConfig reads values from config.toml file and unmarshalls them into StartConfig
func GetStartConfig(v *viper.Viper, configPath string) (*StartConfig, error) {
v.AddConfigPath(configPath)
v.SetConfigName("config")
v.SetConfigType("toml")
Expand Down