github.com/celestiaorg/celestia-node@v0.15.0-beta.1/nodebuilder/core/config.go (about)

     1  package core
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/celestiaorg/celestia-node/libs/utils"
     8  )
     9  
    10  var MetricsEnabled bool
    11  
    12  // Config combines all configuration fields for managing the relationship with a Core node.
    13  type Config struct {
    14  	IP       string
    15  	RPCPort  string
    16  	GRPCPort string
    17  }
    18  
    19  // DefaultConfig returns default configuration for managing the
    20  // node's connection to a Celestia-Core endpoint.
    21  func DefaultConfig() Config {
    22  	return Config{
    23  		IP:       "",
    24  		RPCPort:  "26657",
    25  		GRPCPort: "9090",
    26  	}
    27  }
    28  
    29  // Validate performs basic validation of the config.
    30  func (cfg *Config) Validate() error {
    31  	if !cfg.IsEndpointConfigured() {
    32  		return nil
    33  	}
    34  
    35  	ip, err := utils.ValidateAddr(cfg.IP)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	cfg.IP = ip
    40  	_, err = strconv.Atoi(cfg.RPCPort)
    41  	if err != nil {
    42  		return fmt.Errorf("nodebuilder/core: invalid rpc port: %s", err.Error())
    43  	}
    44  	_, err = strconv.Atoi(cfg.GRPCPort)
    45  	if err != nil {
    46  		return fmt.Errorf("nodebuilder/core: invalid grpc port: %s", err.Error())
    47  	}
    48  	return nil
    49  }
    50  
    51  // IsEndpointConfigured returns whether a core endpoint has been set
    52  // on the config (true if set).
    53  func (cfg *Config) IsEndpointConfigured() bool {
    54  	return cfg.IP != ""
    55  }