Skip to content

Commit

Permalink
Addressed comments from review
Browse files Browse the repository at this point in the history
  • Loading branch information
bermuell committed Dec 21, 2023
1 parent 6a38288 commit becfdca
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 26 deletions.
50 changes: 39 additions & 11 deletions app/consumer/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/spf13/cobra"
"golang.org/x/exp/maps"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -26,6 +27,27 @@ import (
// object provided to it during init.
type GenesisState map[string]json.RawMessage

// Map of supported versions for consumer genesis transformation
type IcsVersion string

const (
v2_x IcsVersion = "v2.x"
v3_0_x IcsVersion = "v3.0.x"
v3_1_x IcsVersion = "v3.1.x"
v3_2_x IcsVersion = "v3.2.x"
v3_3_x IcsVersion = "v3.3.x"
v4_x_x IcsVersion = "v4.x"
)

var TransformationVersions map[string]IcsVersion = map[string]IcsVersion{
"v2.x": v2_x,
"v3.0.x": v3_0_x,
"v3.1.x": v3_1_x,
"v3.2.x": v3_2_x,
"v3.3.x": v3_3_x,
"v4.x": v4_x_x,
}

// Transformation of consumer genesis content as it is exported from a provider version v1,2,3
// to a format readable by current consumer implementation.
func transformToNew(jsonRaw []byte, ctx client.Context) (json.RawMessage, error) {
Expand Down Expand Up @@ -218,19 +240,18 @@ func transformToV2(jsonRaw []byte, ctx client.Context) (json.RawMessage, error)
return result, nil
}

// transformGenesis tries to transform provided data to ccv consumer genesis data
// either to a format supported by current consumer impelementation or to the format
// supported by consumer of version v2.x
func transformGenesis(ctx client.Context, targetVersion string, jsonRaw []byte) (json.RawMessage, error) {
// transformGenesis transforms ccv consumer genesis data to the specified target version
// Returns the transformed data or an error in case the transformation failed or the format is not supported by current implementation
func transformGenesis(ctx client.Context, targetVersion IcsVersion, jsonRaw []byte) (json.RawMessage, error) {
var newConsumerGenesis json.RawMessage = nil
var err error = nil

switch targetVersion {
case "v2.x":
case v2_x, v3_0_x, v3_1_x:
newConsumerGenesis, err = transformToV2(jsonRaw, ctx)
case "v3.3.x":
case v3_3_x:
newConsumerGenesis, err = transformToV33(jsonRaw, ctx)
case "v4.x":
case v4_x_x:
newConsumerGenesis, err = transformToNew(jsonRaw, ctx)
default:
err = fmt.Errorf("unsupported target version '%s'. Run %s --help",
Expand Down Expand Up @@ -259,10 +280,15 @@ func TransformConsumerGenesis(cmd *cobra.Command, args []string) error {
}

clientCtx := client.GetClientContextFromCmd(cmd)
targetVersion, err := cmd.Flags().GetString("to")
version, err := cmd.Flags().GetString("to")
if err != nil {
cmd.PrintErrf("Error getting targetVersion %v", err)
return fmt.Errorf("error getting targetVersion %v", err)
}
targetVersion, exists := TransformationVersions[version]
if !exists {
return fmt.Errorf("unsupported target version '%s'", version)
}

// try to transform data to target format
newConsumerGenesis, err := transformGenesis(clientCtx, targetVersion, jsonRaw)
if err != nil {
Expand Down Expand Up @@ -293,7 +319,7 @@ func NewDefaultGenesisState(cdc codec.JSONCodec) GenesisState {
func GetConsumerGenesisTransformCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "transform [-to version] genesis-file",
Short: "Transform CCV consumer genesis data exported to a specific target format v2.x, v3.3.x, v4.x",
Short: "Transform CCV consumer genesis data exported to a specific target format",
Long: strings.TrimSpace(
fmt.Sprintf(`
Transform the consumer genesis data exported from a provider version v1,v2, v3, v4 to a specified consumer target version.
Expand All @@ -309,6 +335,8 @@ $ %s --to v2.x transform /path/to/ccv_consumer_genesis.json
Args: cobra.RangeArgs(1, 2),
RunE: TransformConsumerGenesis,
}
cmd.Flags().String("to", "v4.x", "target version for consumer genesis. Supported versions [v2.x, v3.3.x, v4.x] default=v4.x")
cmd.Flags().String("to", string(v4_x_x),
fmt.Sprintf("target version for consumer genesis. Supported versions %s",
maps.Keys(TransformationVersions)))
return cmd
}
2 changes: 1 addition & 1 deletion app/consumer/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func transformConsumerGenesis(filePath string, version *string) ([]byte, error)
cmd.SetOutput(result)
_, err = cmd.ExecuteC()
if err != nil {
return nil, fmt.Errorf("Error runnning transformation command: %v", err)
return nil, fmt.Errorf("Error running transformation command: %v", err)
}
return result.Bytes(), nil
}
Expand Down
37 changes: 24 additions & 13 deletions docs/docs/consumer-development/consumer-genesis-transformation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,36 @@ sidebar_position: 6
# Consumer Genesis Transformation

Preparing a consumer chain for onboarding requires some information explaining how to run your chain. This includes a genesis file with CCV data where the CCV data is exported from the provider chain and added to the consumers genesis file (for more details check the documentation on [Onboarding](./onboarding.md) and [Changeover](./changeover-procedure.md)).
In case that the provider chain is running an older version of the InterChainSecurity (ICS) module than the consumer chain the exported CCV data might need to be transformed to the format supported by the ICS implementation run on the consumer chain. This is the case if the consumer chain runs version 4 of ICS or later and the provider is running version 3 or older of the ICS module.
In case that the provider chain is running an older version of the InterChainSecurity (ICS) module than the consumer chain - or vice versa - the exported CCV data might need to be transformed to the format supported by the ICS implementation run on the consumer chain. This is the case if the consumer chain runs version 4 of ICS or later and the provider is running version 3 or older of the ICS module.

Check the [compatibility notes](../../../RELEASES.md#backwards-compatibility) for known incompatibilities between provider and consumer versions and indications if a consumer genesis transformation is required.

To transform such CCV data follow the instructions below

## 1. Prerequisite
- Provider chain is running version 3 or older of the ICS provider module
- Consumer is running version 4 or later of the ICS consumer module.
- interchain-security-cd application complies to the version run on the consumer chain
- used provider and consumer versions require transformation step as indicated in in the [compatibility notes](../../../RELEASES.md#backwards-compatibility)
- interchain-security-cd application supports the versions used by the consumer and provider

## 2. Export the CCV data
Export the CCV data from the provider chain as described in the [Onboarding](./onboarding.md) and [Changeover](./changeover-procedure.md)) your following.
Export the CCV data from the provider chain as described in the [Onboarding](./onboarding.md) and [Changeover](./changeover-procedure.md) your following.
As a result the CCV data will be stored in a file in JSON format.

## 3. Transform CCV data
To transform the CCV data to the newer format run the following command.
```
interchain-security-cd genesis transform [genesis-file]
```
where 'genesis-file' is the path to the file containing the CCV data exported in [step 2](#2-export-the-ccv-data).
As a result the CCV data in the new format will be written to standard output.

Use the new CCV data as described in the procedure you're following.
To transform the CCV data
- to the format supported by the current version of the consumer run the following command:
```
interchain-security-cd genesis transform [genesis-file]
```
where 'genesis-file' is the path to the file containing the CCV data exported in [step 2](#2-export-the-ccv-data).
As a result the CCV data in the new format will be written to standard output.
- a specific target version of a consumer run the following command:
```
interchain-security-cd genesis transform --to <target_version> [genesis-file]
```
where `<target_version` is the ICS version the consumer chain is running.
Use `interchain-security-cd genesis transform --help` to get more details about supported target versions and more.
Use the new CCV data as described in the procedure you're following.
6 changes: 5 additions & 1 deletion docs/docs/validators/changeover-procedure.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ gaiad q provider consumer-genesis stride-1 -o json > ccv-state.json
jq -s '.[0].app_state.ccvconsumer = .[1] | .[0]' genesis.json ccv-state.json > ccv.json
```

Transformation of the exported consumer genesis state to the target version of the consumer might be needed in case the provider and consumer formats are incompatible.
Refer to the compatibility notes [here](../../../RELEASES.md#backwards-compatibility) to check if data transformation is needed for your case.
Instructions on how to transform the exported CCV genesis state (`ccv-state.json` in the example above) to the required target version can be found [here](../consumer-development/consumer-genesis-transformation.md)

### 2. `SoftwareUpgradeProposal` on the standalone/consumer chain

This upgrade proposal will introduce ICS to the standalone chain, making it a consumer.
Expand All @@ -37,7 +41,7 @@ After `spawn_time`, make sure to assign a consumer key if you intend to use one.

Instructions are available [here](../features/key-assignment.md)

### 4. Perform the software ugprade on standalone chain
### 4. Perform the software upgrade on standalone chain

Please use instructions provided by the standalone chain team and make sure to reach out if you are facing issues.
The upgrade preparation depends on your setup, so please make sure you prepare ahead of time.
Expand Down

0 comments on commit becfdca

Please sign in to comment.