generated from kurtosis-tech/package-template-repo
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: kusama integration for local, test and mainnet
- Loading branch information
riyasng12
committed
Dec 18, 2023
1 parent
c06ca16
commit 0ba7026
Showing
9 changed files
with
306 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(¶Chain, "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(¶Nodes, "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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.