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

Commit

Permalink
feat: support using config files for orchestrator and relayer (#583)
Browse files Browse the repository at this point in the history
* chore: refactor the flags to base package and allow usage of reference config

* feat: trying to parse the config file

* feat: support config files for relayer

* chore: error handling

* chore: fix flags in e2e tests

* chore: fix config for deploy command

* chore: fix config for deploy command
  • Loading branch information
rach-id authored Nov 10, 2023
1 parent 29598b1 commit 4f6aa67
Show file tree
Hide file tree
Showing 12 changed files with 737 additions and 262 deletions.
214 changes: 214 additions & 0 deletions cmd/blobstream/base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"os"
"strings"

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

"github.com/celestiaorg/orchestrator-relayer/evm"

"github.com/spf13/cobra"

"github.com/pkg/errors"
Expand All @@ -24,6 +28,19 @@ type Config struct {
EVMPassphrase string
}

func AddEVMPassphraseFlag(cmd *cobra.Command) {
cmd.Flags().String(FlagEVMPassphrase, "", "the evm account passphrase (if not specified as a flag, it will be asked interactively)")
}

func GetEVMPassphraseFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagEVMPassphrase)
val, err := cmd.Flags().GetString(FlagEVMPassphrase)
if err != nil {
return "", changed, err
}
return val, changed, nil
}

// DefaultServicePath constructs the default Blobstream store path for
// the provided service.
// It tries to get the home directory from an environment variable
Expand All @@ -47,20 +64,217 @@ const (
FlagP2PListenAddress = "p2p.listen-addr"
FlagP2PNickname = "p2p.nickname"
FlagGRPCInsecure = "grpc.insecure"

FlagEVMAccAddress = "evm.account"
FlagEVMChainID = "evm.chain-id"
FlagEVMRPC = "evm.rpc"
FlagEVMGasLimit = "evm.gas-limit"
FlagEVMContractAddress = "evm.contract-address"

FlagCoreGRPC = "core.grpc"
FlagCoreRPC = "core.rpc"

FlagStartingNonce = "starting-nonce"
)

func AddStartingNonceFlag(cmd *cobra.Command) {
cmd.Flags().String(
FlagStartingNonce,
"latest",
"Specify the nonce to start the Blobstream contract from. "+
"\"earliest\": for genesis, "+
"\"latest\": for latest nonce, "+
"\"nonce\": for a specific nonce.",
)
}

func GetStartingNonceFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagStartingNonce)
val, err := cmd.Flags().GetString(FlagStartingNonce)
if err != nil {
return "", changed, err
}
return val, changed, nil
}

func AddP2PNicknameFlag(cmd *cobra.Command) {
cmd.Flags().String(FlagP2PNickname, "", "Nickname of the p2p private key to use (if not provided, an existing one from the p2p store or a newly generated one will be used)")
}

func GetP2PNicknameFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagP2PNickname)
val, err := cmd.Flags().GetString(FlagP2PNickname)
if err != nil {
return "", changed, err
}
return val, changed, nil
}

func AddP2PListenAddressFlag(cmd *cobra.Command) {
cmd.Flags().String(FlagP2PListenAddress, "/ip4/0.0.0.0/tcp/30000", "MultiAddr for the p2p peer to listen on")
}

func GetP2PListenAddressFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagP2PListenAddress)
val, err := cmd.Flags().GetString(FlagP2PListenAddress)
if err != nil {
return "", changed, err
}
return val, changed, nil
}

func AddBootstrappersFlag(cmd *cobra.Command) {
cmd.Flags().String(FlagBootstrappers, "", "Comma-separated multiaddresses of p2p peers to connect to")
}

func GetBootstrappersFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagBootstrappers)
val, err := cmd.Flags().GetString(FlagBootstrappers)
if err != nil {
return "", changed, err
}
return val, changed, nil
}

func AddGRPCInsecureFlag(cmd *cobra.Command) {
cmd.Flags().Bool(FlagGRPCInsecure, false, "allow gRPC over insecure channels, if not TLS the server must use TLS")
}

func GetGRPCInsecureFlag(cmd *cobra.Command) (bool, bool, error) {
changed := cmd.Flags().Changed(FlagGRPCInsecure)
val, err := cmd.Flags().GetBool(FlagGRPCInsecure)
if err != nil {
return false, changed, err
}
return val, changed, nil
}

func AddCoreGRPCFlag(cmd *cobra.Command) {
cmd.Flags().String(FlagCoreGRPC, "localhost:9090", "Specify the celestia app grpc address")
}

func GetCoreGRPCFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagCoreGRPC)
val, err := cmd.Flags().GetString(FlagCoreGRPC)
if err != nil {
return "", changed, err
}
return val, changed, nil
}

func AddEVMChainIDFlag(cmd *cobra.Command) {
cmd.Flags().Uint64(FlagEVMChainID, 5, "Specify the evm chain id")
}

func GetEVMChainIDFlag(cmd *cobra.Command) (uint64, bool, error) {
changed := cmd.Flags().Changed(FlagEVMChainID)
val, err := cmd.Flags().GetUint64(FlagEVMChainID)
if err != nil {
return 0, changed, err
}
return val, changed, nil
}

func AddCoreRPCFlag(cmd *cobra.Command) {
cmd.Flags().String(FlagCoreRPC, "tcp://localhost:26657", "Specify the celestia app rest rpc address")
}

func GetCoreRPCFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagCoreRPC)
val, err := cmd.Flags().GetString(FlagCoreRPC)
if err != nil {
return "", changed, err
}
return val, changed, nil
}

func AddEVMRPCFlag(cmd *cobra.Command) {
cmd.Flags().String(FlagEVMRPC, "http://localhost:8545", "Specify the ethereum rpc address")
}

func GetEVMRPCFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagEVMRPC)
val, err := cmd.Flags().GetString(FlagEVMRPC)
if err != nil {
return "", changed, err
}
return val, changed, nil
}

func AddEVMContractAddressFlag(cmd *cobra.Command) {
cmd.Flags().String(FlagEVMContractAddress, "", "Specify the contract at which the Blobstream is deployed")
}

func GetEVMContractAddressFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagEVMContractAddress)
val, err := cmd.Flags().GetString(FlagEVMContractAddress)
if err != nil {
return "", changed, err
}
return val, changed, nil
}

func AddEVMGasLimitFlag(cmd *cobra.Command) {
cmd.Flags().Uint64(FlagEVMGasLimit, evm.DefaultEVMGasLimit, "Specify the evm gas limit")
}

func GetEVMGasLimitFlag(cmd *cobra.Command) (uint64, bool, error) {
changed := cmd.Flags().Changed(FlagEVMGasLimit)
val, err := cmd.Flags().GetUint64(FlagEVMGasLimit)
if err != nil {
return 0, changed, err
}
return val, changed, nil
}

func AddHomeFlag(cmd *cobra.Command, serviceName string, defaultHomeDir string) {
cmd.Flags().String(FlagHome, defaultHomeDir, fmt.Sprintf("The Blobstream %s home directory", serviceName))
}

func GetHomeFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagHome)
val, err := cmd.Flags().GetString(FlagHome)
if err != nil {
return "", changed, err
}
return val, changed, nil
}

func GetHomeDirectory(cmd *cobra.Command, service string) (string, error) {
homeDir, changed, err := GetHomeFlag(cmd)
if err != nil {
return "", err
}
if changed && homeDir != "" {
return homeDir, nil
}
return DefaultServicePath(service)
}

func AddEVMAccAddressFlag(cmd *cobra.Command) {
cmd.Flags().String(FlagEVMAccAddress, "", "Specify the EVM account address to use for signing (Note: the private key should be in the keystore)")
}

func GetEVMAccAddressFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagEVMAccAddress)
val, err := cmd.Flags().GetString(FlagEVMAccAddress)
if err != nil {
return "", changed, err
}
return val, changed, nil
}

func ValidateEVMAddress(addr string) error {
if addr == "" {
return fmt.Errorf("the EVM address cannot be empty")
}
if !ethcmn.IsHexAddress(addr) {
return errors.New("valid EVM address is required")
}
return nil
}

// EnsureConfigPath creates a directory configPath if it does not exist
func EnsureConfigPath(configPath string) error {
return os.MkdirAll(configPath, os.ModePerm)
}
Loading

0 comments on commit 4f6aa67

Please sign in to comment.