Skip to content

Commit

Permalink
feat: multiple kusama parachain for local with same relay
Browse files Browse the repository at this point in the history
  • Loading branch information
riyasng12 committed Dec 21, 2023
1 parent 6ef793f commit 1f9da5a
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 26 deletions.
10 changes: 6 additions & 4 deletions cli/cmd/chains/kusama/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ var (
)

const (
runKusamaFunctionName = "run_polkadot_setup"
runKusamaRelayLocal = "start_relay_chains_local"
runKusamaRelayTestnetMainet = "start_test_main_net_relay_nodes"
runUploadFiles = "upload_files"
runKusamaFunctionName = "run_polkadot_setup"
runKusamaRelayLocal = "start_relay_chains_local"
runKusamaRelayTestnetMainet = "start_test_main_net_relay_nodes"
runUploadFiles = "upload_files"
runKusamaParaLocalFunctionName = "start_nodes"
runKusamaParaTestMainFunctionName = "run_testnet_mainnet"
)

var KusamaCmd = common.NewDiveCommandBuilder().
Expand Down
323 changes: 303 additions & 20 deletions cli/cmd/chains/kusama/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,44 +48,154 @@ func RunKusama(cli *common.Cli) (*common.DiveMultipleServiceResponse, error) {
return nil, err
}

para := fmt.Sprintf(`{"args": %s}`, encodedServiceConfigDataString)
runConfig := getKusamaRunConfig(serviceConfig, enclaveContext, para)
result, err := startRelayAndParaChain(cli, enclaveContext, serviceConfig, encodedServiceConfigDataString)
if err != nil {
return nil, err
}

return result, nil

}

func startRelayAndParaChain(cli *common.Cli, enclaveContext *enclaves.EnclaveContext, serviceConfig *utils.PolkadotServiceConfig, para string) (*common.DiveMultipleServiceResponse, error) {

param := fmt.Sprintf(`{"args": %s}`, para)

KusamaResponseData := &common.DiveMultipleServiceResponse{}
paraResult := &common.DiveMultipleServiceResponse{}

runConfig := getKusamaRunConfig(serviceConfig, enclaveContext, param)
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 ")
}

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 ")
}

if cli.Context().CheckSkippedInstructions(skippedInstructions) {
return nil, common.WrapMessageToError(common.ErrStarlarkResponse, "Kusama already Running")
if len(serviceConfig.Para) != 0 && serviceConfig.Para[0].Name != "" {
ipAddress, err := GetIPAddress(cli, serviceConfig, true, result)
if err != nil {
return nil, err
}
paraResult, err = startParaChains(cli, enclaveContext, serviceConfig, para, ipAddress)
if err != nil {
return nil, err
}
} else {
return nil, common.WrapMessageToError(common.ErrStarlarkResponse, "Kusama Already Running")
}
} else {
if len(serviceConfig.Para) != 0 && serviceConfig.Para[0].Name != "" {
ipAddress, err := GetIPAddress(cli, serviceConfig, false, result)
if err != nil {
return nil, err
}
paraResult, err = startParaChains(cli, enclaveContext, serviceConfig, para, ipAddress)
if err != nil {
return nil, err
}
}
}

KusamaResponseData := &common.DiveMultipleServiceResponse{}
finalResult := ConcatenateDiveResults(result, paraResult)

result, err := KusamaResponseData.Decode([]byte(responseData))
return finalResult, nil
}

func startParaChains(cli *common.Cli, enclaveContext *enclaves.EnclaveContext, serviceConfig *utils.PolkadotServiceConfig, para string, ipAddress string) (*common.DiveMultipleServiceResponse, error) {
paraResult := &common.DiveMultipleServiceResponse{}
var err error

if serviceConfig.ChainType == localChain {
param := fmt.Sprintf(`{"args": %s, "relay_chain_ip":"%s"}`, para, ipAddress)
paraResult, err = runParaChain(cli, enclaveContext, serviceConfig, param)
if err != nil {
return nil, err
}
} else {
for _, paraNode := range serviceConfig.Para {
paraChainConfig, err := paraNode.EncodeToString()
if err != nil {
return nil, common.WrapMessageToError(common.ErrDataMarshall, err.Error())
}
param := fmt.Sprintf(`{"parachain":%s, "args":%s}`, paraChainConfig, para)
paraResult, err = runParaChain(cli, enclaveContext, serviceConfig, param)
if err != nil {
return nil, err
}
}

}

return paraResult, nil
}

func runParaChain(cli *common.Cli, enclaveContext *enclaves.EnclaveContext, serviceConfig *utils.PolkadotServiceConfig, para string) (*common.DiveMultipleServiceResponse, error) {

runParaConfig := getParaRunConfig(serviceConfig, enclaveContext, para)
paraResponse, _, err := enclaveContext.RunStarlarkRemotePackage(cli.Context().GetContext(), common.PolkadotRemotePackagePath, runParaConfig)
if err != nil {
return nil, common.WrapMessageToError(common.ErrStarlarkRunFailed, err.Error())
}

paraResponseData, paraServices, skippedParaInstructions, err := common.GetSerializedData(cli, paraResponse)
if err != nil {
errRemove := cli.Context().RemoveServicesByServiceNames(paraServices, common.EnclaveName)
if errRemove != nil {
return nil, common.WrapMessageToError(errRemove, "ParaChain Run Failed ")
}
return nil, common.WrapMessageToError(err, "ParaChain Run Failed ")
}

errRemove := cli.Context().RemoveServicesByServiceNames(services, common.EnclaveName)
if cli.Context().CheckSkippedInstructions(skippedParaInstructions) {
return nil, common.WrapMessageToError(common.ErrStarlarkResponse, "ParaChain Already Running")
}

KusamaParaResponseData := &common.DiveMultipleServiceResponse{}
resultPara, err := KusamaParaResponseData.Decode([]byte(paraResponseData))
if err != nil {
errRemove := cli.Context().RemoveServicesByServiceNames(paraServices, common.EnclaveName)
if errRemove != nil {
return nil, common.WrapMessageToError(errRemove, "Kusama Run Failed ")
return nil, common.WrapMessageToError(errRemove, "ParaChain Run Failed ")
}
return nil, common.WrapMessageToErrorf(common.ErrDataUnMarshall, "%s.%s", err, "ParaChain Run Failed ")

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

return resultPara, nil
}

func ConcatenateDiveResults(result, paraResult *common.DiveMultipleServiceResponse) *common.DiveMultipleServiceResponse {
concatenatedResult := &common.DiveMultipleServiceResponse{
Dive: make(map[string]*common.DiveServiceResponse),
}

return result, nil
for key, value := range result.Dive {
concatenatedResult.Dive[key] = value
}

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

return concatenatedResult
}

func configureService(serviceConfig *utils.PolkadotServiceConfig) error {
Expand Down Expand Up @@ -163,24 +273,197 @@ func flagCheck() error {
}

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

return common.GetStarlarkRunConfig(para, common.DivePolkadotRelayNodeSetupScript, runKusamaRelayTestnetMainet)
}
}

func uploadFiles(cli *common.Cli, enclaveCtx *enclaves.EnclaveContext) error {
runConfig := common.GetStarlarkRunConfig("{}", common.DivePolkaDotUtilsPath, runUploadFiles)
_, _, err := enclaveCtx.RunStarlarkRemotePackage(cli.Context().GetContext(), common.PolkadotRemotePackagePath, runConfig)
_, _, err := enclaveCtx.RunStarlarkPackage(cli.Context().GetContext(), "/home/riya/polakadot-kurtosis-package", runConfig)
if err != nil {
return common.WrapMessageToError(common.ErrStarlarkRunFailed, err.Error())
}

return nil
}

func getParaRunConfig(serviceConfig *utils.PolkadotServiceConfig, enclaveContext *enclaves.EnclaveContext, para string) *starlark_run_config.StarlarkRunConfig {
if len(serviceConfig.Para) != 0 && serviceConfig.Para[0].Name != "" {
if serviceConfig.ChainType == localChain {
return common.GetStarlarkRunConfig(para, common.DivePolkadotParachainNodeSetup, runKusamaParaLocalFunctionName)

} else {
return common.GetStarlarkRunConfig(para, common.DivePolkadotParachainNodeSetup, runKusamaParaTestMainFunctionName)

}
}
return nil
}

func GetIPAddress(cli *common.Cli, serviceConfig *utils.PolkadotServiceConfig, relayReRun bool, result *common.DiveMultipleServiceResponse) (string, error) {
var nodename string
if serviceConfig.ChainType == localChain {
if relayReRun {
nodename = serviceConfig.RelayChain.Nodes[0].Name
var services = common.Services{}
serviceFileName := fmt.Sprintf(common.ServiceFilePath, common.EnclaveName)

err := cli.FileHandler().ReadJson(serviceFileName, &serviceConfig)
if err != nil {
return "", err
}

ChainServiceName := fmt.Sprintf("rococo-local-%s", nodename)
chainServiceResponse, OK := services[ChainServiceName]
if !OK {
return "", fmt.Errorf("service name not found")
}

ipAddress := chainServiceResponse.IpAddress
return ipAddress, nil
} else {
servicename := fmt.Sprintf("rococo-local-%s", serviceConfig.RelayChain.Nodes[0].Name)
ipAddress := result.Dive[servicename].IpAddress
return ipAddress, nil
}
}
return "", nil
}

// func startParaChains(cli *common.Cli, serviceConfig *utils.PolkadotServiceConfig, enclaveContext *enclaves.EnclaveContext, params string, ipAddress string) (*common.DiveMultipleServiceResponse, error) {
// para := fmt.Sprintf(`{"args": %s, "relay_chain_ip":"%s"}`, params, ipAddress)

// runParaConfig := getParaRunConfig(serviceConfig, enclaveContext, para)
// paraResponse, _, err := enclaveContext.RunStarlarkRemotePackage(cli.Context().GetContext(), common.PolkadotRemotePackagePath, runParaConfig)
// if err != nil {
// return nil, common.WrapMessageToError(common.ErrStarlarkRunFailed, err.Error())
// }

// paraResponseData, paraServices, skippedParaInstructions, err := common.GetSerializedData(cli, paraResponse)
// if err != nil {

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

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

// if cli.Context().CheckSkippedInstructions(skippedParaInstructions) {
// return nil, common.WrapMessageToError(common.ErrStarlarkResponse, "ParaChain already Running")
// }

// KusamaParaResponseData := &common.DiveMultipleServiceResponse{}

// resultPara, err := KusamaParaResponseData.Decode([]byte(paraResponseData))
// if err != nil {

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

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

// }

// return resultPara, nil
// }

// func startParaChainsLocal(cli *common.Cli, enclaveContext *enclaves.EnclaveContext, serviceConfig *utils.PolkadotServiceConfig, result *common.DiveMultipleServiceResponse, skippedInstructions map[string]bool, encodedServiceConfigDataString string) (*common.DiveMultipleServiceResponse, error) {
// paraResult := &common.DiveMultipleServiceResponse{}
// var err error

// if cli.Context().CheckSkippedInstructions(skippedInstructions) {
// if len(serviceConfig.Para) != 0 && serviceConfig.Para[0].Name != "" {
// nodename := serviceConfig.RelayChain.Nodes[0].Name
// ipAddress, err := GetIPAddress(cli, nodename)
// if err != nil {
// return nil, err
// }
// paraResult, err = startParaChains(cli, serviceConfig, enclaveContext, encodedServiceConfigDataString, ipAddress)
// if err != nil {
// return nil, err
// }
// }
// return nil, common.WrapMessageToError(common.ErrStarlarkResponse, "Kusama already Running")
// } else {
// if len(serviceConfig.Para) != 0 && serviceConfig.Para[0].Name != "" {
// servicename := fmt.Sprintf("rococo-local-%s", serviceConfig.RelayChain.Nodes[0].Name)
// ipAddress := result.Dive[servicename].IpAddress
// paraResult, err = startParaChains(cli, serviceConfig, enclaveContext, encodedServiceConfigDataString, ipAddress)
// if err != nil {
// return nil, err
// }
// }
// }

// return paraResult, nil
// }

// func startParaChainsTestAndMain(cli *common.Cli, enclaveContext *enclaves.EnclaveContext, serviceConfig *utils.PolkadotServiceConfig, result *common.DiveMultipleServiceResponse, skippedInstructions map[string]bool, encodedServiceConfigDataString string) {
// if cli.Context().CheckSkippedInstructions(skippedInstructions) {
// if len(serviceConfig.Para) != 0 && serviceConfig.Para[0].Name != "" {

// paraResult, err = runParaTestAndMain(cli, enclaveContext, serviceConfig, encodedServiceConfigDataString)
// if err != nil {
// return nil, err
// }
// }
// return nil, common.WrapMessageToError(common.ErrStarlarkResponse, "Kusama already Running")
// } else {
// if len(serviceConfig.Para) != 0 && serviceConfig.Para[0].Name != "" {
// servicename := fmt.Sprintf("rococo-local-%s", serviceConfig.RelayChain.Nodes[0].Name)
// ipAddress := result.Dive[servicename].IpAddress
// paraResult, err = runParaTestAndMain(cli, enclaveContext, serviceConfig, encodedServiceConfigDataString)
// if err != nil {
// return nil, err
// }
// }
// }
// }

// func runParaTestAndMain(cli *common.Cli, enclaveContext *enclaves.EnclaveContext, serviceConfig *utils.PolkadotServiceConfig, params string) (*common.DiveMultipleServiceResponse, error) {
// para := fmt.Sprintf(`{"parachain":"%s", "args":%s}`, paraChain, params)

// runParaConfig := getParaRunConfig(serviceConfig, enclaveContext, para)
// paraResponse, _, err := enclaveContext.RunStarlarkRemotePackage(cli.Context().GetContext(), common.PolkadotRemotePackagePath, runParaConfig)
// if err != nil {
// return nil, common.WrapMessageToError(common.ErrStarlarkRunFailed, err.Error())
// }

// paraResponseData, paraServices, skippedParaInstructions, err := common.GetSerializedData(cli, paraResponse)
// if err != nil {

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

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

// if cli.Context().CheckSkippedInstructions(skippedParaInstructions) {
// return nil, common.WrapMessageToError(common.ErrStarlarkResponse, "ParaChain already Running")
// }

// KusamaParaResponseData := &common.DiveMultipleServiceResponse{}

// resultPara, err := KusamaParaResponseData.Decode([]byte(paraResponseData))
// if err != nil {

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

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

// }

// return resultPara, nil
// }
Loading

0 comments on commit 1f9da5a

Please sign in to comment.