Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: Split out consumer genesis state #1324

Merged
merged 20 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions app/consumer-democracy/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ import (
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
// add mint
mint "github.com/cosmos/cosmos-sdk/x/mint"
mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
Expand Down Expand Up @@ -112,7 +111,6 @@ import (
ccvdistr "github.com/cosmos/interchain-security/v3/x/ccv/democracy/distribution"
ccvgov "github.com/cosmos/interchain-security/v3/x/ccv/democracy/governance"
ccvstaking "github.com/cosmos/interchain-security/v3/x/ccv/democracy/staking"
ccvtypes "github.com/cosmos/interchain-security/v3/x/ccv/types"
)

const (
Expand Down Expand Up @@ -713,7 +711,7 @@ func New(
return fromVM, fmt.Errorf("failed to unmarshal genesis state: %w", err)
}

consumerGenesis := ccvtypes.ConsumerGenesisState{}
consumerGenesis := consumertypes.GenesisState{}
appCodec.MustUnmarshalJSON(appState[consumertypes.ModuleName], &consumerGenesis)

consumerGenesis.PreCCV = true
Expand Down
136 changes: 136 additions & 0 deletions app/consumer/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@ package app

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strings"

"github.com/spf13/cobra"

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

consumerTypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types"
"github.com/cosmos/interchain-security/v3/x/ccv/types"
)

// The genesis state of the blockchain is represented here as a map of raw json
Expand All @@ -15,7 +28,130 @@ 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
)

// Migration of consumer genesis content as it is exported from a provider version v2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prob need some clarification on the use of "v2" and "v3" here, it seems like the term is applied to both consumer and provider. Are we referring to ICS major versions?

Copy link
Contributor Author

@bermuell bermuell Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is the major version of ICS modules we refer here.

// to a version readable by current consumer (version v3).
func migrateFromV2(jsonRaw []byte, ctx client.Context) (json.RawMessage, error) {
MSalopek marked this conversation as resolved.
Show resolved Hide resolved
// v2 uses deprecated fields of GenesisState type
oldConsumerGenesis := consumerTypes.GenesisState{}
err := ctx.Codec.UnmarshalJSON(jsonRaw, &oldConsumerGenesis)
if err != nil {
return nil, fmt.Errorf("reading consumer genesis data failed: %s", err)
}

// some sanity checks for v2 transformation
if len(oldConsumerGenesis.Provider.InitialValSet) > 0 {
return nil, fmt.Errorf("invalid source version. Unexpected element 'provider.initial_val_set'")
}

if oldConsumerGenesis.Provider.ClientState != nil {
return nil, fmt.Errorf("invalid source version. Unexpected element 'provider.client_state'")
}

if oldConsumerGenesis.Provider.ConsensusState != nil {
return nil, fmt.Errorf("invalid source version. Unexpected element 'provider.consensus_state'")
}

// Version 2 of provider genesis data fills up deprecated fields
// ProviderClientState, ConsensusState and InitialValSet
newGenesis := types.ConsumerGenesisState{
Params: oldConsumerGenesis.Params,
Provider: types.ProviderInfo{
ClientState: oldConsumerGenesis.ProviderClientState,
ConsensusState: oldConsumerGenesis.ProviderConsensusState,
InitialValSet: oldConsumerGenesis.InitialValSet,
},
}

newJson, err := ctx.Codec.MarshalJSON(&newGenesis)
if err != nil {
return nil, fmt.Errorf("failed marshalling data to new type: %s", err)
}
return newJson, nil
}

// Mapping of provider module version to related transformation function
var transformationMap = TransformationMap{
MSalopek marked this conversation as resolved.
Show resolved Hide resolved
"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 written to defined output.
func TransformConsumerGenesis(cmd *cobra.Command, args []string, transformationMap TransformationMap) error {
sourceVersion := args[0]
sourceFile := args[1]

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

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 := newConsumerGenesis.MarshalJSON()
if err != nil {
return fmt.Errorf("failed exporting new consumer genesis to JSON: %s", err)
}

sortedBz, err := sdk.SortJSON(bz)
if err != nil {
return fmt.Errorf("failed sorting transformed consumer genesis JSON: %s", err)
}

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

// List of provider versions supported by consumer genesis transformations
func getSupportedTransformationVersions() []string {
var keys []string
for k := range transformationMap {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}

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

// GetConsumerGenesisTransformCmd transforms Consumer Genesis JSON content exported from a specific
// provider version to a JSON format supported by this consumer version.
// Note: Provider module version can be received by querying 'module_versions' on the 'upgrade' module.
MSalopek marked this conversation as resolved.
Show resolved Hide resolved
func GetConsumerGenesisTransformCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "transform [source-version] [genesis-file]",
Short: "Transform CCV consumer genesis from a specified provider version",
Long: fmt.Sprintf(`Transform the consumer genesis file from a specified provider version to a version supported by this consumer. Result is printed to STDOUT.
Supported provider versions: %s

Example:
$ %s transform v2 /path/to/consumer_genesis.json `, strings.Join(getSupportedTransformationVersions(), ", "), version.AppName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return TransformConsumerGenesis(cmd, args, transformationMap)
},
}

return cmd
}
213 changes: 213 additions & 0 deletions app/consumer/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
package app_test

import (
"bytes"
"context"
"io/fs"
"os"
"path/filepath"
"testing"
"time"

"github.com/spf13/cobra"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/x/auth/types"

app "github.com/cosmos/interchain-security/v3/app/consumer"
consumerTypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types"
)

// Testdata mapping consumer genesis exports to a provider module version as
// used by transformation function for consumer genesis content.
var consumerGenesisStates map[string]string = map[string]string{
"v2": `
{
"params": {
"enabled": true,
"blocks_per_distribution_transmission": "1500",
"distribution_transmission_channel": "",
"provider_fee_pool_addr_str": "",
"ccv_timeout_period": "2419200s",
"transfer_timeout_period": "3600s",
"consumer_redistribution_fraction": "0.75",
"historical_entries": "10000",
"unbonding_period": "1728000s",
"soft_opt_out_threshold": "",
"reward_denoms": [],
"provider_reward_denoms": []
},
"provider_client_id": "",
"provider_channel_id": "",
"new_chain": true,
"provider_client_state": {
"chain_id": "cosmoshub-4",
"trust_level": {
"numerator": "1",
"denominator": "3"
},
"trusting_period": "1197504s",
"unbonding_period": "1814400s",
"max_clock_drift": "10s",
"frozen_height": {
"revision_number": "0",
"revision_height": "0"
},
"latest_height": {
"revision_number": "4",
"revision_height": "15211521"
},
"proof_specs": [
{
"leaf_spec": {
"hash": "SHA256",
"prehash_key": "NO_HASH",
"prehash_value": "SHA256",
"length": "VAR_PROTO",
"prefix": "AA=="
},
"inner_spec": {
"child_order": [
0,
1
],
"child_size": 33,
"min_prefix_length": 4,
"max_prefix_length": 12,
"empty_child": null,
"hash": "SHA256"
},
"max_depth": 0,
"min_depth": 0
},
{
"leaf_spec": {
"hash": "SHA256",
"prehash_key": "NO_HASH",
"prehash_value": "SHA256",
"length": "VAR_PROTO",
"prefix": "AA=="
},
"inner_spec": {
"child_order": [
0,
1
],
"child_size": 32,
"min_prefix_length": 1,
"max_prefix_length": 1,
"empty_child": null,
"hash": "SHA256"
},
"max_depth": 0,
"min_depth": 0
}
],
"upgrade_path": [
"upgrade",
"upgradedIBCState"
],
"allow_update_after_expiry": true,
"allow_update_after_misbehaviour": true
},
"provider_consensus_state": {
"timestamp": "2023-05-08T11:00:01.563901871Z",
"root": {
"hash": "qKVnVSXlsjDHC8ekKcy/0zSjzr3YekCurld9R4W07EI="
},
"next_validators_hash": "E08978F493101A3C5D459FB3087B8CFBA9E82D7A1FE1441E7D77E11AC0586BAC"
},
"maturing_packets": [],
"initial_val_set": [
{
"pub_key": {
"ed25519": "cOQZvh/h9ZioSeUMZB/1Vy1Xo5x2sjrVjlE/qHnYifM="
},
"power": "2345194"
},
{
"pub_key": {
"ed25519": "vGSKfbQyKApvBhinpOOA0XQAdjxceihYNwtGskfZGyQ="
},
"power": "463811"
}
],
"height_to_valset_update_id": [],
"outstanding_downtime_slashing": [],
"pending_consumer_packets": {
"list": []
},
"last_transmission_block_height": {
"height": "0"
},
"preCCV": false
}

`,
}

func getClientCtx() client.Context {
encodingConfig := app.MakeTestEncodingConfig()
return client.Context{}.
WithCodec(encodingConfig.Codec).
WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
WithTxConfig(encodingConfig.TxConfig).
WithLegacyAmino(encodingConfig.Amino).
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{})
}

// Setup client context
func getGenesisTransformCmd() (*cobra.Command, error) {
cmd := app.GetConsumerGenesisTransformCmd()
clientCtx := getClientCtx()
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
cmd.SetContext(ctx)
err := client.SetCmdClientContext(cmd, clientCtx)
return cmd, err
}

// Check transformation of a version 2 ConsumerGenesis export to
// consumer genesis json format used by current consumer implementation.
func TestConsumerGenesisTransformationV2(t *testing.T) {
version := "v2"
filePath := filepath.Join(t.TempDir(), "oldConsumerGenesis.json")

err := os.WriteFile(
filePath,
[]byte(consumerGenesisStates[version]),
fs.FileMode(0o644))
require.NoError(t, err)
defer os.Remove(filePath)

cmd, err := getGenesisTransformCmd()
require.NoError(t, err, "Error setting up transformation command: %s", err)
cmd.SetArgs([]string{version, filePath})

output := new(bytes.Buffer)
cmd.SetOutput(output)
require.NoError(t, err)
_, err = cmd.ExecuteC()
require.NoError(t, err)

consumerGenesis := consumerTypes.GenesisState{}

bz := output.Bytes()
ctx := client.GetClientContextFromCmd(cmd)
err = ctx.Codec.UnmarshalJSON(bz, &consumerGenesis)
require.NoError(t, err, "Error unmarshalling transformed genesis state :%s", err)

// Some basic sanity checks on the content.
require.Nil(t, consumerGenesis.ProviderClientState)
require.NotNil(t, consumerGenesis.Provider.ClientState)
require.Equal(t, "cosmoshub-4", consumerGenesis.Provider.ClientState.ChainId)

require.Nil(t, consumerGenesis.ProviderConsensusState)
require.NotNil(t, consumerGenesis.Provider.ConsensusState)
require.Equal(t, time.Date(2023, time.May, 8, 11, 0, 1, 563901871, time.UTC),
consumerGenesis.Provider.ConsensusState.Timestamp)

require.Empty(t, consumerGenesis.InitialValSet)
require.NotEmpty(t, consumerGenesis.Provider.InitialValSet)
}
Loading