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: integrate polkadot package in dive-cli for local network setup
- Loading branch information
Showing
6 changed files
with
298 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package polkadot | ||
|
||
import ( | ||
"fmt" | ||
|
||
"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 ( | ||
runPolkadotFunctionName = "run" | ||
runPolkadotRelayLocal = "start_relay_chains_local" | ||
runPolkadotRelayTestnetMainet = "start_test_main_net_relay_nodes" | ||
) | ||
|
||
var PolkadotCmd = common.NewDiveCommandBuilder(). | ||
SetUse("polkadot"). | ||
SetShort("Build, initialize and start a polkadot node"). | ||
SetLong("The command starts the polkadot relay chain and polkadot parachain if -p flag is given"). | ||
SetRun(polkadot). | ||
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 stat polakdot relaychain and parachain nodes."). | ||
AddBoolFlag(&explorer, "explorer", false, "specify the bool flag if you wanna start polakdot js explorer service"). | ||
AddBoolFlag(&metrics, "metrics", false, "specify the bool flag if you wanna start prometheus metrics service"). | ||
Build() | ||
|
||
func polkadot(cmd *cobra.Command, args []string) { | ||
cliContext := common.GetCliWithKurtosisContext() | ||
|
||
err := common.ValidateArgs(args) | ||
|
||
if err != nil { | ||
cliContext.Fatalf("Error %s. %s", err, cmd.UsageString()) | ||
} | ||
|
||
cliContext.StartSpinnerIfNotVerbose("Starting Polkadot Node", common.DiveLogs) | ||
|
||
response, err := RunPolkadot(cliContext) | ||
|
||
if err != nil { | ||
cliContext.Fatal(err) | ||
} | ||
serviceFileName := fmt.Sprintf(common.ServiceFilePath, common.EnclaveName) | ||
|
||
fmt.Print(response.Dive) | ||
for serviceName := range response.Dive { | ||
err = common.WriteServiceResponseData(response.Dive[serviceName].ServiceName, *response.Dive[serviceName], cliContext, serviceFileName) | ||
|
||
if err != nil { | ||
cliContext.Fatal(err) | ||
} | ||
} | ||
|
||
cliContext.StartSpinnerIfNotVerbose("Polkadot Node Started. Please find the service details in current working directory(services.json)", common.DiveLogs) | ||
} |
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,126 @@ | ||
package polkadot | ||
|
||
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 = "/Users/abhishekharde/Desktop/hugobyte/dive-packages/services/polkadot/parachain/static_files/configs" | ||
) | ||
|
||
func RunPolkadot(cli *common.Cli) (*common.DiveMultipleServiceResponse, error) { | ||
enclaveContext, err := cli.Context().GetEnclaveContext(common.EnclaveName) | ||
|
||
if err != nil { | ||
return nil, common.WrapMessageToError(err, "Polkadot Run Failed") | ||
} | ||
var serviceConfig = &utils.PolkadotServiceConfig{} | ||
|
||
err = common.LoadConfig(cli, serviceConfig, configFilePath) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
configureService(serviceConfig) | ||
|
||
encodedServiceConfigDataString, err := serviceConfig.EncodeToString() | ||
|
||
para := fmt.Sprintf("{args: %s}", encodedServiceConfigDataString) | ||
|
||
if err != nil { | ||
return nil, common.WrapMessageToError(common.ErrDataMarshall, err.Error()) | ||
} | ||
|
||
runConfig := getPolkadotRunConfig(serviceConfig, enclaveContext, para) | ||
|
||
response, _, err := enclaveContext.RunStarlarkPackage(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.DiveEnclave) | ||
if errRemove != nil { | ||
return nil, common.WrapMessageToError(errRemove, "Polkadot Run Failed ") | ||
} | ||
|
||
return nil, common.WrapMessageToError(err, "Polkadot Run Failed ") | ||
} | ||
|
||
if cli.Context().CheckSkippedInstructions(skippedInstructions) { | ||
return nil, common.WrapMessageToError(common.ErrStarlarkResponse, "Polkadot Running") | ||
} | ||
|
||
polkadotResponseData := &common.DiveMultipleServiceResponse{} | ||
result, err := polkadotResponseData.Decode([]byte(responseData)) | ||
|
||
fmt.Print(result) | ||
|
||
if err != nil { | ||
|
||
errRemove := cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave) | ||
if errRemove != nil { | ||
return nil, common.WrapMessageToError(errRemove, "Polkadot Run Failed ") | ||
} | ||
|
||
return nil, common.WrapMessageToErrorf(common.ErrDataUnMarshall, "%s.%s", err, "Polkadot 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 = "polkadot" | ||
} | ||
} | ||
|
||
if explorer { | ||
serviceConfig.Explorer = true | ||
} | ||
|
||
if metrics { | ||
configureMetrics(serviceConfig) | ||
} | ||
} | ||
|
||
func configureMetrics(serviceConfig *utils.PolkadotServiceConfig) { | ||
for _, node := range append(serviceConfig.RelayChain.Nodes, serviceConfig.Para[0].Nodes...) { | ||
node.Prometheus = true | ||
} | ||
} | ||
|
||
func getPolkadotRunConfig(serviceConfig *utils.PolkadotServiceConfig, enclaveContext *enclaves.EnclaveContext, para string) *starlark_run_config.StarlarkRunConfig { | ||
if serviceConfig.Para[0].Name != "" { | ||
return common.GetStarlarkRunConfig(para, common.DivePolkadotDefaultNodeSetupScript, runPolkadotFunctionName) | ||
} else { | ||
if serviceConfig.ChainType == localChain { | ||
enclaveContext.UploadFiles(configsDirectory, "configs") | ||
return common.GetStarlarkRunConfig(para, common.DivePolkadotRelayNodeSetupScript, runPolkadotRelayLocal) | ||
} else { | ||
return common.GetStarlarkRunConfig(para, common.DivePolkadotRelayNodeSetupScript, runPolkadotRelayTestnetMainet) | ||
} | ||
|
||
} | ||
} |
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