Skip to content

Commit

Permalink
Add genesis transformation command to interchain-security-cd
Browse files Browse the repository at this point in the history
  • Loading branch information
bermuell committed Oct 2, 2023
1 parent 3a84cbc commit 13d3f61
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
99 changes: 99 additions & 0 deletions app/consumer/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ package app

import (
"encoding/json"
"fmt"
"os"

cmtjson "github.com/cometbft/cometbft/libs/json"

Check failure on line 8 in app/consumer/genesis.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) -s prefix(github.com/cometbft/cometbft) -s prefix(github.com/cosmos/interchain-security) --custom-order (gci)
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"

Check failure on line 12 in app/consumer/genesis.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) -s prefix(github.com/cometbft/cometbft) -s prefix(github.com/cosmos/interchain-security) --custom-order (gci)
consumerTypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types"
"github.com/cosmos/interchain-security/v3/x/ccv/types"
"github.com/spf13/cobra"

Check failure on line 15 in app/consumer/genesis.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) -s prefix(github.com/cometbft/cometbft) -s prefix(github.com/cosmos/interchain-security) --custom-order (gci)
)

// The genesis state of the blockchain is represented here as a map of raw json
Expand All @@ -15,7 +24,97 @@ import (
// object provided to it during init.
type GenesisState map[string]json.RawMessage

type (
// Transformation callback for a consumer genesis 'section' exported from a
// specific version of a provider
TransformationCallback func([]byte, client.Context) (json.RawMessage, error)

// TransformationMap defines the mapping from a version to a transformation callback
TransformationMap map[string]TransformationCallback
)

func migrateFromV2(jsonRaw []byte, ctx client.Context) (json.RawMessage, error) {
oldConsumerGenesis := consumerTypes.GenesisState{}
oldConsumerGenesis.Validate()
err := cmtjson.Unmarshal(jsonRaw, &oldConsumerGenesis)
if err != nil {
return nil, err
}

newGenesis := types.ConsumerGenesisState{
Params: oldConsumerGenesis.Params,
Provider: types.ProviderInfo{
ClientState: oldConsumerGenesis.ProviderClientState,
ConsensusState: oldConsumerGenesis.ConsensusState,
InitialValSet: oldConsumerGenesis.InitialValSet,
},
}

newJson := ctx.Codec.MustMarshalJSON(&newGenesis)
return newJson, nil
}

var transformationMap = TransformationMap{
"v2": migrateFromV2,
}

// Transform a consumer genesis json file exported from a given ccv provider version
// to a consumer genesis json format supported by current ccv consumer version.
// Result will be writen to defined output.

Check failure on line 63 in app/consumer/genesis.go

View workflow job for this annotation

GitHub Actions / lint

`writen` is a misspelling of `written` (misspell)
func TransformConsumerGenesis(cmd *cobra.Command, args []string, transformationMap TransformationMap) error {
sourceVersion := args[0]
sourceFile := args[1]

jsonRaw, err := os.ReadFile(sourceFile)
if err != nil {
return err
}

// TODO: content validation would be good
transform, exists := transformationMap[sourceVersion]
if !exists {
return fmt.Errorf("error transforming consumer genesis content: Unsupported versions %s", sourceVersion)
}

clientCtx := client.GetClientContextFromCmd(cmd)
newConsumerGenesis, err := transform(jsonRaw, clientCtx)
if err != nil {
return err
}

bz, err := cmtjson.Marshal(newConsumerGenesis)
if err != nil {
return err
}

sortedBz, err := sdk.SortJSON(bz)
if err != nil {
return err
}

cmd.Println(string(sortedBz))
return nil
}

// NewDefaultGenesisState generates the default state for the application.
func NewDefaultGenesisState(cdc codec.JSONCodec) GenesisState {
return ModuleBasics.DefaultGenesis(cdc)
}

func GetConsumerGenesisTransformCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "transform [source-version] [genesis-file]",
Short: "Transform consumer genesis section from a specified provider version",
Long: fmt.Sprintf(`Transform the consumer genesis into the target version and print to STDOUT.
Example:
$ %s transform v2.0.0 /path/to/consumer_genesis.json `, version.AppName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
//return ModuleBasics.ValidateGenesis(cmd, args, migrationMap)

Check failure on line 114 in app/consumer/genesis.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
return TransformConsumerGenesis(cmd, args, transformationMap)
},
}

return cmd
}
2 changes: 1 addition & 1 deletion cmd/interchain-security-cd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
// add keybase, auxiliary RPC, query, genesis, and tx child commands
rootCmd.AddCommand(
rpc.StatusCommand(),
genesisCommand(encodingConfig),
genesisCommand(encodingConfig, consumer.GetConsumerGenesisTransformCmd()),
queryCommand(),
txCommand(),
keys.Commands(consumer.DefaultNodeHome),
Expand Down

0 comments on commit 13d3f61

Please sign in to comment.