Skip to content

Commit

Permalink
feat: added feature to pass public ports from cli to package
Browse files Browse the repository at this point in the history
  • Loading branch information
riyasng12 committed Jan 11, 2024
1 parent 123e881 commit 66eed34
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 362 deletions.
4 changes: 4 additions & 0 deletions cli/cmd/chains/kusama/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const (
runKusamaGrafana = "launch_grafana"
)

var (
kusamaParachains = []string{"altair", "bajun", "bifrost", "calamari", "encointer", "khala", "kintsugi-btc", "litmus", "mangata", "moonriver", "robonomics", "subzero", "turing"}
)

var KusamaCmd = common.NewDiveCommandBuilder().
SetUse("kusama").
SetShort("Build, initialize and start a Kusama node").
Expand Down
248 changes: 79 additions & 169 deletions cli/cmd/chains/kusama/run.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions cli/cmd/chains/polkadot/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const (
runPolkadotGrafana = "launch_grafana"
)

var (
polkadotParachains = []string{"acala", "ajuna", "bifrost", "centrifuge", "clover", "frequency", "integritee", "interlay", "karura", "kilt", "kylin", "litentry", "manta", "moonbeam", "moonsama", "nodle", "parallel", "pendulum", "phala", "polkadex", "subsocial", "zeitgeist"}
)

var PolkadotCmd = common.NewDiveCommandBuilder().
SetUse("polkadot").
SetShort("Build, initialize and start a Polkadot node").
Expand Down
250 changes: 79 additions & 171 deletions cli/cmd/chains/polkadot/run.go

Large diffs are not rendered by default.

177 changes: 171 additions & 6 deletions cli/cmd/chains/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package utils

import (
"encoding/json"
"fmt"
"slices"

"github.com/hugobyte/dive-core/cli/common"
)
Expand Down Expand Up @@ -160,8 +162,15 @@ func (sc *HardhatServiceConfig) EncodeToString() (string, error) {

type NodeConfig struct {
Name string `json:"name"`
NodeType string `json:"node-type"`
NodeType string `json:"node_type"`
Prometheus bool `json:"prometheus"`
Ports Ports `json:"ports"`
}

type Ports struct {
RPCPort int `json:"rpc_port"`
Lib2LibPort int `json:"lib2lib_port"`
PrometheusPort int `json:"prometheus_port,omitempty"`
}

type RelayChainConfig struct {
Expand All @@ -175,14 +184,23 @@ type ParaNodeConfig struct {
}

type PolkadotServiceConfig struct {
ChainType string `json:"chain-type"`
ChainType string `json:"chain_type"`
RelayChain RelayChainConfig `json:"relaychain"`
Para []ParaNodeConfig `json:"para"`
Para []ParaNodeConfig `json:"parachains"`
Explorer bool `json:"explorer"`
}

func (sc *ParaNodeConfig) EncodeToString() (string, error) {
encodedBytes, err := json.Marshal(sc)
func (pc *ParaNodeConfig) EncodeToString() (string, error) {
encodedBytes, err := json.Marshal(pc)
if err != nil {
return "", common.WrapMessageToError(common.ErrDataMarshall, err.Error())
}

return string(encodedBytes), nil
}

func (rc *RelayChainConfig) EncodeToString() (string, error) {
encodedBytes, err := json.Marshal(rc)
if err != nil {
return "", common.WrapMessageToError(common.ErrDataMarshall, err.Error())
}
Expand All @@ -209,6 +227,17 @@ func (sc *PolkadotServiceConfig) LoadConfigFromFile(cliContext *common.Cli, file
if err != nil {
return common.WrapMessageToError(err, "Failed To Load Configuration")
}

for i := range sc.RelayChain.Nodes {
sc.RelayChain.Nodes[i].AssignPorts(sc.RelayChain.Nodes[i].Prometheus)
}

for _, parachain := range sc.Para {
for i := range parachain.Nodes {
sc.RelayChain.Nodes[i].AssignPorts(sc.RelayChain.Nodes[i].Prometheus)
}
}

return nil
}

Expand All @@ -217,12 +246,37 @@ func (sc *PolkadotServiceConfig) LoadDefaultConfig() error {
sc.Explorer = false
sc.RelayChain.Name = "rococo-local"
sc.RelayChain.Nodes = []NodeConfig{
{Name: "bob", NodeType: "full", Prometheus: false},
{Name: "bob", NodeType: "validator", Prometheus: false},
{Name: "alice", NodeType: "validator", Prometheus: false},
}

for i := range sc.RelayChain.Nodes {
sc.RelayChain.Nodes[i].AssignPorts(sc.RelayChain.Nodes[i].Prometheus)
}

sc.Para = []ParaNodeConfig{}
return nil
}

func (nc *NodeConfig) AssignPorts(prometheus bool) error {
var rpcPort, lib2libPort, prometheusPort int
var err error
rpcPort, err = common.GetAvailablePort()
if err != nil {
return err
}

lib2libPort, err = common.GetAvailablePort()
if err != nil {
return err
}
if prometheus {
prometheusPort, err = common.GetAvailablePort()
if err != nil {
return err
}
}
nc.Ports = Ports{RPCPort: rpcPort, Lib2LibPort: lib2libPort, PrometheusPort: prometheusPort}
return nil
}

Expand Down Expand Up @@ -291,3 +345,114 @@ func (nc *NodeConfig) IsEmpty() error {

return nil
}

func (sc *PolkadotServiceConfig) HasPrometheus() bool {
// Check relay chain nodes
if sc.RelayChain.Name != "" {
for _, node := range sc.RelayChain.Nodes {
if node.Prometheus {
return true
}
}
}

// Check para nodes
for _, paraNode := range sc.Para {
for _, node := range paraNode.Nodes {
if node.Prometheus {
return true
}
}
}

return false
}

func (sc *PolkadotServiceConfig) ValidateConfig() error {
var validChainTypes = []string{"local", "testnet", "mainnet"}
var validRelayNodeType = []string{"validator", "full"}
var validParaNodeType = []string{"collator", "full"}

if !slices.Contains(validChainTypes, sc.ChainType) {
return fmt.Errorf("invalid Chain Type: %s", sc.ChainType)
}

if sc.ChainType == "local" && sc.RelayChain.Name != "rococo-local" {
return fmt.Errorf("invalid Chain Name for local: %s", sc.RelayChain.Name)
}

if sc.RelayChain.Name != "" {
if sc.ChainType == "testnet" && !(sc.RelayChain.Name == "rococo" || sc.RelayChain.Name == "westend") {
return fmt.Errorf("invalid Chain Name for testnet: %s", sc.RelayChain.Name)
}

if sc.ChainType == "mainnet" && !(sc.RelayChain.Name == "kusama" || sc.RelayChain.Name == "polkadot") {
return fmt.Errorf("invalid Chain Name for mainnet: %s", sc.RelayChain.Name)
}
}

if sc.ChainType == "local" {
for _, node := range sc.RelayChain.Nodes {
if node.NodeType != "validator" {
return fmt.Errorf("invalid Node Type for Relay Chain Local: %s", node.NodeType)
}
}
} else {
for _, node := range sc.RelayChain.Nodes {
if !slices.Contains(validRelayNodeType, node.NodeType) {
return fmt.Errorf("invalid Node Type for Relay Chain: %s", node.NodeType)
}
}
}

for _, paraChain := range sc.Para {
for _, node := range paraChain.Nodes {
if !slices.Contains(validParaNodeType, node.NodeType) {
return fmt.Errorf("invalid Node Type for Para Chain: %s", node.NodeType)
}
}
}

return nil
}

func (sc *PolkadotServiceConfig) GetParamsForRelay() (string, error) {
relay_nodes, err := sc.RelayChain.EncodeToString()
if err != nil {
return "", common.WrapMessageToError(common.ErrDataMarshall, err.Error())
}

if sc.ChainType == "local" {
return fmt.Sprintf(`{"relaychain": %s}`, relay_nodes), nil
} else {
return fmt.Sprintf(`{"chain_type": "%s", "relaychain": %s}`, sc.ChainType, relay_nodes), nil
}
}

func (sc *PolkadotServiceConfig) ConfigureMetrics() {
for i := range sc.RelayChain.Nodes {
sc.RelayChain.Nodes[i].Prometheus = true
}
if len(sc.Para) != 0 {
for i := range sc.Para[0].Nodes {
sc.Para[0].Nodes[i].Prometheus = true
}
}
}

func (sc *PolkadotServiceConfig) ConfigureFullNodes(network string) {

if network == "testnet" {
sc.RelayChain.Name = "rococo"
} else if network == "mainnet" {
sc.RelayChain.Name = "kusama"
}

sc.RelayChain.Nodes = []NodeConfig{}

sc.RelayChain.Nodes = append(sc.RelayChain.Nodes, NodeConfig{
Name: "alice",
NodeType: "full",
Prometheus: false,
})
}
1 change: 1 addition & 0 deletions cli/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const (
EmptyFieldsError
MissingFlagsError
InvalidFlagError
InvalidConfigError
)

var DiveLogs bool
Expand Down
1 change: 1 addition & 0 deletions cli/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
ErrEmptyFields = NewBase(EmptyFieldsError, "Missing Fields In The Config File")
ErrMissingFlags = NewBase(MissingFlagsError, "Missing Flags")
ErrInvalidFlag = NewBase(InvalidFlagError, "Invalid Flag Usage")
ErrInvalidConfig = NewBase(InvalidConfigError, "Invalid Config")
)

func (c ErrorCode) New(msg string) error {
Expand Down
54 changes: 39 additions & 15 deletions cli/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import (
)

type DiveServiceResponse struct {
ServiceName string `json:"service_name,omitempty"`
PublicEndpoint string `json:"endpoint_public,omitempty"`
PrivateEndpoint string `json:"endpoint,omitempty"`
KeyPassword string `json:"keypassword,omitempty"`
KeystorePath string `json:"keystore_path,omitempty"`
Network string `json:"network,omitempty"`
NetworkName string `json:"network_name,omitempty"`
NetworkId string `json:"nid,omitempty"`
ChainId string `json:"chain_id,omitempty"`
ChainKey string `json:"chain_key,omitempty"`
PrometheusEndpoint string `json:"endpoint_prometheus,omitempty"`
Prometheus bool `json:"prometheus,omitempty"`
IpAddress string `json:"ip_address,omitempty"`
Node string `json:"node-type,omitempty"`
PrometheusPort int `json:"prometheus_port,omitempty"`
ServiceName string `json:"service_name,omitempty"`
PublicEndpoint string `json:"endpoint_public,omitempty"`
PrivateEndpoint string `json:"endpoint,omitempty"`
KeyPassword string `json:"keypassword,omitempty"`
KeystorePath string `json:"keystore_path,omitempty"`
Network string `json:"network,omitempty"`
NetworkName string `json:"network_name,omitempty"`
NetworkId string `json:"nid,omitempty"`
ChainId string `json:"chain_id,omitempty"`
ChainKey string `json:"chain_key,omitempty"`
PrometheusEndpoint string `json:"endpoint_prometheus,omitempty"`
Prometheus bool `json:"prometheus,omitempty"`
IpAddress string `json:"ip_address,omitempty"`
Node string `json:"node-type,omitempty"`
PrometheusPort int `json:"prometheus_port,omitempty"`
PrometheusPublicPort int `json:"prometheus_public_port,omitempty"`
}

type DiveMultipleServiceResponse struct {
Expand Down Expand Up @@ -68,6 +69,29 @@ func (dive *DiveMultipleServiceResponse) EncodeToString() (string, error) {
return string(encodedBytes), nil
}

func (dive *DiveMultipleServiceResponse) ConcatenateDiveResults(result2 *DiveMultipleServiceResponse) *DiveMultipleServiceResponse {
result1 := dive
if result1 == nil {
return result2
} else if result2 == nil {
return result1
}

concatenatedResult := &DiveMultipleServiceResponse{
Dive: make(map[string]*DiveServiceResponse),
}

for key, value := range result1.Dive {
concatenatedResult.Dive[key] = value
}

for key, value := range result2.Dive {
concatenatedResult.Dive[key] = value
}

return concatenatedResult
}

type Services map[string]*DiveServiceResponse

type DiveBridgeResponse map[string]interface{}
Expand Down
2 changes: 1 addition & 1 deletion cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/briandowns/spinner v1.23.0
github.com/fatih/color v1.16.0
github.com/google/go-github v17.0.0+incompatible
github.com/kurtosis-tech/kurtosis/api/golang v0.85.42
github.com/kurtosis-tech/kurtosis/api/golang v0.86.2
github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/sirupsen/logrus v1.9.3
Expand Down

0 comments on commit 66eed34

Please sign in to comment.