diff --git a/cmd/blobstream/orchestrator/config.go b/cmd/blobstream/orchestrator/config.go index a81675e7..22cd1afb 100644 --- a/cmd/blobstream/orchestrator/config.go +++ b/cmd/blobstream/orchestrator/config.go @@ -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) } @@ -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") diff --git a/cmd/blobstream/query/cmd.go b/cmd/blobstream/query/cmd.go index a35d8c58..da31f748 100644 --- a/cmd/blobstream/query/cmd.go +++ b/cmd/blobstream/query/cmd.go @@ -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" @@ -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()) @@ -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 @@ -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()) @@ -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 @@ -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 +} diff --git a/cmd/blobstream/query/config.go b/cmd/blobstream/query/config.go index 530bab99..4373ef69 100644 --- a/cmd/blobstream/query/config.go +++ b/cmd/blobstream/query/config.go @@ -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 } @@ -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 } diff --git a/cmd/blobstream/relayer/config.go b/cmd/blobstream/relayer/config.go index 86c7e733..4ae53b1a 100644 --- a/cmd/blobstream/relayer/config.go +++ b/cmd/blobstream/relayer/config.go @@ -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) } @@ -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")