Skip to content

Commit

Permalink
feat: kusama integration for local, test and mainnet
Browse files Browse the repository at this point in the history
  • Loading branch information
riyasng12 committed Dec 18, 2023
1 parent c06ca16 commit 0ba7026
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cli/cmd/bridge/btp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func runBtpSetupByRunningNodes(cli *common.Cli, enclaveCtx *enclaves.EnclaveCont
}
executionSerializedData, services, skippedInstructions, err := common.GetSerializedData(cli, executionData)
if err != nil {
errRemove := cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave)
errRemove := cli.Context().RemoveServicesByServiceNames(services, common.EnclaveName)
if errRemove != nil {
return "", common.WrapMessageToError(errRemove, "BTP Setup Run Failed")
}
Expand Down Expand Up @@ -289,7 +289,7 @@ func runBtpSetupForAlreadyRunningNodes(cli *common.Cli, enclaveCtx *enclaves.Enc
}
executionSerializedData, services, skippedInstructions, err := common.GetSerializedData(cli, executionData)
if err != nil {
errRemove := cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave)
errRemove := cli.Context().RemoveServicesByServiceNames(services, common.EnclaveName)
if errRemove != nil {
return "", common.WrapMessageToError(errRemove, "BTP Setup Run Failed")
}
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/bridge/ibc/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func startIbcRelayIconToCosmos(cli *common.Cli, enclaveContext *enclaves.Enclave
executionSerializedData, services, _, err := common.GetSerializedData(cli, executionData)

if err != nil {
errRemove := cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave)
errRemove := cli.Context().RemoveServicesByServiceNames(services, common.EnclaveName)
if errRemove != nil {
return "", common.WrapMessageToError(errRemove, "IBC Setup Run Failed")
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func runStarlarkPackage(cli *common.Cli, enclaveContext *enclaves.EnclaveContext
executionSerializedData, services, skippedInstructions, err := common.GetSerializedData(cli, executionData)

if err != nil {
errRemove := cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave)
errRemove := cli.Context().RemoveServicesByServiceNames(services, common.EnclaveName)
if errRemove != nil {
return "", common.WrapMessageToError(errRemove, "IBC Setup Run Failed")
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/chains/archway/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func RunArchway(cli *common.Cli) (*common.DiveServiceResponse, error) {
if err != nil {
return nil, err
}

encodedServiceConfigDataString, err := serviceConfig.EncodeToString()

if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cli/cmd/chains/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hugobyte/dive-core/cli/cmd/chains/eth"
"github.com/hugobyte/dive-core/cli/cmd/chains/hardhat"
"github.com/hugobyte/dive-core/cli/cmd/chains/icon"
"github.com/hugobyte/dive-core/cli/cmd/chains/kusama"
"github.com/hugobyte/dive-core/cli/cmd/chains/neutron"
"github.com/hugobyte/dive-core/cli/cmd/chains/polkadot"
"github.com/hugobyte/dive-core/cli/common"
Expand All @@ -28,6 +29,7 @@ maintenance within the specified blockchain ecosystem.`,
AddCommand(archway.ArchwayCmd).
AddCommand(neutron.NeutronCmd).
AddCommand(polkadot.PolkadotCmd).
AddCommand(kusama.KusamaCmd).
SetRun(chains).
Build()

Expand Down
71 changes: 71 additions & 0 deletions cli/cmd/chains/kusama/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package kusama

import (
"fmt"
"strings"

"github.com/hugobyte/dive-core/cli/common"
"github.com/spf13/cobra"
)

var (
configFilePath string
paraChain string
network string
paraNodes string
relayNodes string
explorer bool
metrics bool
)

const (
runKusamaFunctionName = "run_polkadot_setup"
runKusamaRelayLocal = "start_relay_chains_local"
runKusamaRelayTestnetMainet = "start_test_main_net_relay_nodes"
)

var KusamaCmd = common.NewDiveCommandBuilder().
SetUse("kusama").
SetShort("Build, initialize and start a Kusama node").
SetLong("The command starts the kusama relay chain and kusama parachain if -p flag is given").
SetRun(kusama).
AddStringFlagWithShortHand(&paraChain, "parachain", "p", "", "specify the parachain to spwan parachain node").
AddStringFlagWithShortHand(&network, "network", "n", "", "specify the which network to run. local/testnet/mainnet. default will be local.").
AddStringFlag(&paraNodes, "para-nodes", "", "specify the nodes for parachain, default will be '[full, collator]'").
AddStringFlag(&relayNodes, "relay-nodes", "", "specify the nodes for relaychain, default will be '[full, validator]'").
AddStringFlagWithShortHand(&configFilePath, "config", "c", "", "path to custom config json file to start kusama relaychain and parachain nodes.").
AddBoolFlag(&explorer, "explorer", false, "specify the bool flag if you want to start polkadot js explorer service").
AddBoolFlag(&metrics, "metrics", false, "specify the bool flag if you want to start prometheus metrics service").
Build()

func kusama(cmd *cobra.Command, args []string) {
cliContext := common.GetCliWithKurtosisContext()

err := common.ValidateArgs(args)
if err != nil {
cliContext.Fatalf("Error %s. %s", err, cmd.UsageString())
}

cliContext.Spinner().StartWithMessage("Starting Kusama Node", "green")

response, err := RunKusama(cliContext)
if err != nil {
if strings.Contains(err.Error(), "already running") {
cliContext.Error(err)
cliContext.Context().Exit(0)
} else {
cliContext.Fatal(err)
}
}

serviceFileName := fmt.Sprintf(common.ServiceFilePath, common.EnclaveName)

for serviceName := range response.Dive {
err = common.WriteServiceResponseData(response.Dive[serviceName].ServiceName, *response.Dive[serviceName], cliContext, serviceFileName)
if err != nil {
cliContext.Fatal(err)
}
}
stopMessage := fmt.Sprintf("Kusama Node Started. Please find service details in current working directory(%s)", serviceFileName)
cliContext.Spinner().StopWithMessage(stopMessage)
}
123 changes: 123 additions & 0 deletions cli/cmd/chains/kusama/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package kusama

import (
"fmt"

"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/enclaves"

"github.com/hugobyte/dive-core/cli/cmd/chains/utils"
"github.com/hugobyte/dive-core/cli/common"
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/starlark_run_config"
)

const (
localChain = "local"
configsDirectory = "/home/riya/polakadot-kurtosis-package/parachain/static_files/configs"
)

func RunKusama(cli *common.Cli) (*common.DiveMultipleServiceResponse, error) {

enclaveContext, err := cli.Context().GetEnclaveContext(common.EnclaveName)
if err != nil {
return nil, common.WrapMessageToError(err, "Kusama Run Failed")
}

var serviceConfig = &utils.PolkadotServiceConfig{}

err = common.LoadConfig(cli, serviceConfig, configFilePath)
if err != nil {
return nil, err
}

configureService(serviceConfig)

encodedServiceConfigDataString, err := serviceConfig.EncodeToString()
if err != nil {
return nil, common.WrapMessageToError(common.ErrDataMarshall, err.Error())
}

para := fmt.Sprintf(`{"args": %s}`, encodedServiceConfigDataString)
runConfig := getKusamaRunConfig(serviceConfig, enclaveContext, para)

response, _, err := enclaveContext.RunStarlarkRemotePackage(cli.Context().GetContext(), common.PolkadotRemotePackagePath, runConfig)
if err != nil {
return nil, common.WrapMessageToError(common.ErrStarlarkRunFailed, err.Error())
}

responseData, services, skippedInstructions, err := common.GetSerializedData(cli, response)
if err != nil {

errRemove := cli.Context().RemoveServicesByServiceNames(services, common.EnclaveName)
if errRemove != nil {
return nil, common.WrapMessageToError(errRemove, "Kusama Run Failed ")
}

return nil, common.WrapMessageToError(err, "Kusama Run Failed ")
}

if cli.Context().CheckSkippedInstructions(skippedInstructions) {
return nil, common.WrapMessageToError(common.ErrStarlarkResponse, "Kusama already Running")
}

KusamaResponseData := &common.DiveMultipleServiceResponse{}

result, err := KusamaResponseData.Decode([]byte(responseData))
if err != nil {

errRemove := cli.Context().RemoveServicesByServiceNames(services, common.EnclaveName)
if errRemove != nil {
return nil, common.WrapMessageToError(errRemove, "Kusama Run Failed ")
}

return nil, common.WrapMessageToErrorf(common.ErrDataUnMarshall, "%s.%s", err, "Kusama Run Failed ")

}

return result, nil
}

func configureService(serviceConfig *utils.PolkadotServiceConfig) {
if paraChain != "" {
serviceConfig.Para[0].Name = paraChain
}

if network != "" {
serviceConfig.ChainType = network
if network == "testnet" {
serviceConfig.RelayChain.Name = "rococo"
} else if network == "mainnet" {
serviceConfig.RelayChain.Name = "kusama"
}
}

if explorer {
serviceConfig.Explorer = true
}

if metrics {
configureMetrics(serviceConfig)
}
}

func configureMetrics(serviceConfig *utils.PolkadotServiceConfig) {
for i := range serviceConfig.RelayChain.Nodes {
serviceConfig.RelayChain.Nodes[i].Prometheus = true
}
for i := range serviceConfig.Para[0].Nodes {
serviceConfig.Para[0].Nodes[i].Prometheus = true
}
}

func getKusamaRunConfig(serviceConfig *utils.PolkadotServiceConfig, enclaveContext *enclaves.EnclaveContext, para string) *starlark_run_config.StarlarkRunConfig {
if serviceConfig.Para[0].Name != "" {
return common.GetStarlarkRunConfig(para, common.DivePolkadotDefaultNodeSetupScript, runKusamaFunctionName)
} else {
if serviceConfig.ChainType == localChain {
enclaveContext.UploadFiles(configsDirectory, "configs")
return common.GetStarlarkRunConfig(para, common.DivePolkadotRelayNodeSetupScript, runKusamaRelayLocal)
} else {
return common.GetStarlarkRunConfig(para, common.DivePolkadotRelayNodeSetupScript, runKusamaRelayTestnetMainet)
}

}
}
94 changes: 91 additions & 3 deletions cli/cmd/chains/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func (cs *CosmosServiceConfig) LoadDefaultConfig() error {
cs.ChainID = nil
cs.Key = nil
cs.Password = nil
cs.ChainName = nil
publicGrpc, err := common.GetAvailablePort()
if err != nil {
return common.WrapMessageToError(err, "error getting available gRPC port")
Expand Down Expand Up @@ -65,6 +64,20 @@ func (cs *CosmosServiceConfig) LoadConfigFromFile(cliContext *common.Cli, filePa
if err != nil {
return common.WrapMessageToError(err, "Failed To Load Configuration")
}

err = cs.IsEmpty()
if err != nil {
return common.WrapMessageToError(err, "Failed To Load Configuration")
}
return nil
}

func (cc *CosmosServiceConfig) IsEmpty() error {
if cc.ChainID == nil || cc.Key == nil || cc.Password == nil ||
cc.PublicGrpc == nil || cc.PublicHTTP == nil || cc.PublicTCP == nil || cc.PublicRPC == nil {
return common.WrapMessageToErrorf(common.ErrEmptyFields, "Missing Fields In The Config File")
}

return nil
}

Expand Down Expand Up @@ -106,6 +119,18 @@ func (sc *IconServiceConfig) LoadConfigFromFile(cliContext *common.Cli, filePath
if err != nil {
return common.WrapMessageToError(err, "Failed To Load Configuration")
}
err = sc.IsEmpty()
if err != nil {
return common.WrapMessageToError(err, "Failed To Load Configuration")
}

return nil
}

func (sc *IconServiceConfig) IsEmpty() error {
if sc.Port == 0 || sc.PublicPort == 0 || sc.P2PListenAddress == "" || sc.P2PAddress == "" || sc.Cid == "" {
return common.WrapMessageToErrorf(common.ErrEmptyFields, "Missing Fields In The Config File")
}
return nil
}

Expand Down Expand Up @@ -171,16 +196,26 @@ func (sc *PolkadotServiceConfig) LoadConfigFromFile(cliContext *common.Cli, file
if err != nil {
return common.WrapMessageToError(err, "Failed To Load Configuration")
}

err = sc.IsEmpty()
if err != nil {
return common.WrapMessageToError(err, "Failed To Load Configuration")
}
return nil
}

func (sc *PolkadotServiceConfig) LoadDefaultConfig() error {
sc.ChainType = "local"
sc.Explorer = false
sc.RelayChain.Name = "rococo-local"
Port1, err := common.GetAvailablePort()
if err != nil {
return err
}
Port2 := Port1 + 1
sc.RelayChain.Nodes = []NodeConfig{
{Name: "bob", NodeType: "full", Port: 9944, Prometheus: false},
{Name: "alice", NodeType: "validator", Port: 9955, Prometheus: false},
{Name: "bob", NodeType: "full", Port: Port1, Prometheus: false},
{Name: "alice", NodeType: "validator", Port: Port2, Prometheus: false},
}
sc.Para = []ParaNodeConfig{
{
Expand All @@ -194,3 +229,56 @@ func (sc *PolkadotServiceConfig) LoadDefaultConfig() error {

return nil
}

func (psc *PolkadotServiceConfig) IsEmpty() error {
if psc == nil || psc.ChainType == "" || psc.Explorer {
return common.WrapMessageToErrorf(common.ErrEmptyFields, "Missing Fields In PolkadotServiceConfig")
}

if err := psc.RelayChain.IsEmpty(); err != nil {
return err
}

for _, para := range psc.Para {
if err := para.IsEmpty(); err != nil {
return err
}
}

return nil
}

func (rcc *RelayChainConfig) IsEmpty() error {
if rcc == nil || rcc.Name == "" || len(rcc.Nodes) == 0 {
return common.WrapMessageToErrorf(common.ErrEmptyFields, "Missing Fields In RelayChainConfig")
}

for _, node := range rcc.Nodes {
if err := node.IsEmpty(); err != nil {
return err
}
}

return nil
}

func (pnc *ParaNodeConfig) IsEmpty() error {
if pnc == nil || pnc.Name == "" || len(pnc.Nodes) == 0 {
return common.WrapMessageToErrorf(common.ErrEmptyFields, "Missing Fields In ParaNodeConfig")
}

for _, node := range pnc.Nodes {
if err := node.IsEmpty(); err != nil {
return err
}
}

return nil
}

func (nc *NodeConfig) IsEmpty() error {
if nc == nil || nc.Name == "" || nc.NodeType == "" || nc.Port == 0 {
return common.WrapMessageToErrorf(common.ErrEmptyFields, "Missing Fields In NodeConfig")
}
return nil
}
Loading

0 comments on commit 0ba7026

Please sign in to comment.