Skip to content

Commit

Permalink
Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
bermuell committed Oct 5, 2023
1 parent 0948a6e commit 9d7c31f
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 78 deletions.
20 changes: 12 additions & 8 deletions app/consumer/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"

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

consumerTypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types"
"github.com/cosmos/interchain-security/v3/x/ccv/types"
)
Expand All @@ -42,8 +40,10 @@ type (
// Migration of consumer genesis content as it is exported from a provider version v2
// to a version readable by current consumer (version v3).
func migrateFromV2(jsonRaw []byte, ctx client.Context) (json.RawMessage, error) {

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

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
// v2 uses deprecated fields of GenesisState type
oldConsumerGenesis := consumerTypes.GenesisState{}
err := cmtjson.Unmarshal(jsonRaw, &oldConsumerGenesis)
err := ctx.Codec.UnmarshalJSON(jsonRaw, &oldConsumerGenesis)
if err != nil {
return nil, fmt.Errorf("reading consumer genesis data failed: %s", err)
}
Expand All @@ -61,17 +61,21 @@ func migrateFromV2(jsonRaw []byte, ctx client.Context) (json.RawMessage, error)
return nil, fmt.Errorf("invalid source version. Unexpected element 'provider.consensus_state'")
}

// Note: version 2 of provider genesis data fills up deprecated fields
// 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.ConsensusState,
ConsensusState: oldConsumerGenesis.ProviderConsensusState,
InitialValSet: oldConsumerGenesis.InitialValSet,
},
}

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

Expand Down Expand Up @@ -103,14 +107,14 @@ func TransformConsumerGenesis(cmd *cobra.Command, args []string, transformationM
return err
}

bz, err := cmtjson.Marshal(newConsumerGenesis)
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 err
return fmt.Errorf("failed sorting transformed consumer genesis JSON: %s", err)
}

cmd.Println(string(sortedBz))
Expand Down
211 changes: 211 additions & 0 deletions app/consumer/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package app_test

import (
"bytes"
"context"
"fmt"
"io/fs"
"os"
"testing"
"time"

Check failure on line 11 in app/consumer/genesis_test.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/x/auth/types"

Check failure on line 13 in app/consumer/genesis_test.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)
app "github.com/cosmos/interchain-security/v3/app/consumer"
consumerTypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types"
"github.com/spf13/cobra"

Check failure on line 16 in app/consumer/genesis_test.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/stretchr/testify/require"
)

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(t *testing.T) *cobra.Command {

Check failure on line 158 in app/consumer/genesis_test.go

View workflow job for this annotation

GitHub Actions / lint

test helper function should start from t.Helper() (thelper)
cmd := app.GetConsumerGenesisTransformCmd()
clientCtx := getClientCtx()
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
cmd.SetContext(ctx)
err := client.SetCmdClientContext(cmd, clientCtx)
require.NoError(t, err, "Error setting up transformation command: %s", err)
return cmd
}

// 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 := os.TempDir() + "testdata.json"
err := os.WriteFile(
filePath,
[]byte(consumerGenesisStates[version]),
fs.FileMode(0644))

Check failure on line 176 in app/consumer/genesis_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
require.NoError(t, err)
defer os.Remove(filePath)

cmd := getGenesisTransformCmd(t)
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()
fmt.Println(string(bz))
// err = consumerGenesis.Unmarshal(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.Time(time.Date(2023, time.May, 8, 11, 0, 1, 563901871, time.UTC)),

Check failure on line 206 in app/consumer/genesis_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
consumerGenesis.Provider.ConsensusState.Timestamp)

require.Empty(t, consumerGenesis.InitialValSet)
require.NotEmpty(t, consumerGenesis.Provider.InitialValSet)
}
2 changes: 1 addition & 1 deletion proto/interchain_security/ccv/consumer/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ message GenesisState {
// !!! DEPRECATED !!! ProviderClientState is deprecated. Use provider.client_state instead
ibc.lightclients.tendermint.v1.ClientState provider_client_state = 5 [ deprecated = true];
// !!! DEPRECATED !!! ProviderConsensusState is deprecated. Use provider.consensus_state instead
ibc.lightclients.tendermint.v1.ConsensusState consensus_state = 6 [ deprecated = true];
ibc.lightclients.tendermint.v1.ConsensusState provider_consensus_state = 6 [ deprecated = true];
// MaturingPackets nil on new chain, filled in on restart.
repeated MaturingVSCPacket maturing_packets = 7
[ (gogoproto.nullable) = false ];
Expand Down
Loading

0 comments on commit 9d7c31f

Please sign in to comment.