Skip to content

Commit 7deec2d

Browse files
committed
update genesis transformation
1 parent 4d59eb2 commit 7deec2d

File tree

2 files changed

+88
-43
lines changed

2 files changed

+88
-43
lines changed

app/consumer/genesis.go

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ import (
1010
"github.com/spf13/cobra"
1111
"golang.org/x/exp/maps"
1212

13-
"github.com/cosmos/cosmos-sdk/client"
1413
"github.com/cosmos/cosmos-sdk/codec"
1514
sdk "github.com/cosmos/cosmos-sdk/types"
1615
"github.com/cosmos/cosmos-sdk/version"
17-
18-
"github.com/cosmos/interchain-security/v6/x/ccv/types"
1916
)
2017

2118
// The genesis state of the blockchain is represented here as a map of raw json
@@ -60,52 +57,44 @@ func removeParameterFromParams(params json.RawMessage, param string) (json.RawMe
6057
// Transformation of consumer genesis content as it is exported by provider version >= v6.2.x
6158
// to a format supported by consumer chains version with either SDK v0.47 and ICS < v4.5.0 or SDK v0.50 and ICS < v6.2.0
6259
// This transformation removes the 'consumer_id' parameter from the 'params' field introduced in ICS v6.2.x
63-
func removeConsumerID(jsonRaw []byte, ctx client.Context) (json.RawMessage, error) {
64-
srcConGen := types.ConsumerGenesisState{}
65-
err := ctx.Codec.UnmarshalJSON(jsonRaw, &srcConGen)
66-
if err != nil {
67-
return nil, fmt.Errorf("reading consumer genesis data failed: %s", err)
68-
}
69-
60+
func removeConsumerID(genState map[string]json.RawMessage) (map[string]json.RawMessage, error) {
7061
// Remove 'consumer_id' from 'params'
71-
params, err := ctx.Codec.MarshalJSON(&srcConGen.Params)
62+
params, err := removeParameterFromParams(genState["params"], "consumer_id")
7263
if err != nil {
7364
return nil, err
7465
}
7566

76-
params, err = removeParameterFromParams(params, "consumer_id")
77-
if err != nil {
78-
return nil, err
79-
}
80-
81-
// Marshal GenesisState and patch 'params' value
82-
result, err := ctx.Codec.MarshalJSON(&srcConGen)
83-
if err != nil {
84-
return nil, err
85-
}
86-
genState := map[string]json.RawMessage{}
87-
if err := json.Unmarshal(result, &genState); err != nil {
88-
return nil, fmt.Errorf("unmarshalling 'GenesisState' failed: %v", err)
89-
}
9067
genState["params"] = params
9168

92-
result, err = json.Marshal(genState)
93-
if err != nil {
94-
return nil, fmt.Errorf("marshalling transformation result failed: %v", err)
69+
return genState, nil
70+
}
71+
72+
func removeFieldsFromGenesisState(genState map[string]json.RawMessage, keysToRemove []string) (map[string]json.RawMessage, error) {
73+
for _, key := range keysToRemove {
74+
delete(genState, key) // Remove the key from the map if it exists
9575
}
96-
return result, nil
76+
return genState, nil
9777
}
9878

9979
// transformGenesis transforms ccv consumer genesis data to the specified target version
10080
// Returns the transformed data or an error in case the transformation failed or the format is not supported by current implementation
101-
func transformGenesis(ctx client.Context, targetVersion IcsVersion, jsonRaw []byte) (json.RawMessage, error) {
102-
var newConsumerGenesis json.RawMessage = nil
81+
func transformGenesis(targetVersion IcsVersion, jsonRaw []byte) (json.RawMessage, error) {
10382
var err error
83+
// Unmarshal genesis state from raw msg
84+
genState := map[string]json.RawMessage{}
85+
if err := json.Unmarshal(jsonRaw, &genState); err != nil {
86+
return nil, fmt.Errorf("unmarshalling 'GenesisState' failed: %v", err)
87+
}
10488

10589
switch targetVersion {
10690
case v4_x_x, v5_x_x:
107-
newConsumerGenesis, err = removeConsumerID(jsonRaw, ctx)
108-
// TODO: in addition, remove both `connection_id` and `preCCV` fields if target is v4_5_x or v6_x_x
91+
genState, err = removeConsumerID(genState)
92+
if err != nil {
93+
break
94+
}
95+
genState, err = removeFieldsFromGenesisState(genState, []string{"preCCV", "connection_id"})
96+
case v4_5_x, v6_x_x:
97+
genState, err = removeFieldsFromGenesisState(genState, []string{"preCCV", "connection_id"})
10998
default:
11099
err = fmt.Errorf("unsupported target version '%s'. Run %s --help",
111100
targetVersion, version.AppName)
@@ -114,6 +103,13 @@ func transformGenesis(ctx client.Context, targetVersion IcsVersion, jsonRaw []by
114103
if err != nil {
115104
return nil, fmt.Errorf("transformation failed: %v", err)
116105
}
106+
107+
// Marshal genesis state to raw msg
108+
newConsumerGenesis, err := json.Marshal(genState)
109+
if err != nil {
110+
return nil, fmt.Errorf("marshalling transformation result failed: %v", err)
111+
}
112+
117113
return newConsumerGenesis, err
118114
}
119115

@@ -130,7 +126,6 @@ func TransformConsumerGenesis(cmd *cobra.Command, args []string) error {
130126
return err
131127
}
132128

133-
clientCtx := client.GetClientContextFromCmd(cmd)
134129
version, err := cmd.Flags().GetString("to")
135130
if err != nil {
136131
return fmt.Errorf("error getting targetVersion %v", err)
@@ -141,7 +136,7 @@ func TransformConsumerGenesis(cmd *cobra.Command, args []string) error {
141136
}
142137

143138
// try to transform data to target format
144-
newConsumerGenesis, err := transformGenesis(clientCtx, targetVersion, jsonRaw)
139+
newConsumerGenesis, err := transformGenesis(targetVersion, jsonRaw)
145140
if err != nil {
146141
return err
147142
}

app/consumer/genesis_test.go

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ var consumerGenesisStates map[string]string = map[string]string{
275275
]
276276
},
277277
"new_chain": true,
278-
"preCCV": false,
279-
"connection_id": ""
278+
"preCCV": true,
279+
"connection_id": "connection-0"
280280
}
281281
`,
282282
}
@@ -353,9 +353,25 @@ func TestConsumerGenesisTransformationV63xToV4x(t *testing.T) {
353353
CheckGenesisTransform(t, Provider_v6_3_x, Consumer_v4_x_x)
354354
}
355355

356-
// TODO: check the following transformations
357-
// - Provider_v6_4_x to Consumer_v4_5_x
358-
// - Provider_v6_4_x to Consumer_v6_x_x
356+
// Check transformation of provider v6.4.x implementation to consumer v4.x.x
357+
func TestConsumerGenesisTransformationV64xToV4xx(t *testing.T) {
358+
CheckGenesisTransform(t, Provider_v6_4_x, Consumer_v4_x_x)
359+
}
360+
361+
// Check transformation of provider v6.4.x implementation to consumer v5.x.x
362+
func TestConsumerGenesisTransformationV64xToV5xx(t *testing.T) {
363+
CheckGenesisTransform(t, Provider_v6_4_x, Consumer_v5_x_x)
364+
}
365+
366+
// Check transformation of provider v6.4.x implementation to consumer v4.5.x
367+
func TestConsumerGenesisTransformationV64xToV45x(t *testing.T) {
368+
CheckGenesisTransform(t, Provider_v6_4_x, Consumer_v4_5_x)
369+
}
370+
371+
// Check transformation of provider v6.4.x implementation to consumer v6.x.x
372+
func TestConsumerGenesisTransformationV64xToV6xx(t *testing.T) {
373+
CheckGenesisTransform(t, Provider_v6_4_x, Consumer_v6_x_x)
374+
}
359375

360376
// CheckGenesisTransform checks that the transformation of consumer genesis data
361377
// from a given source version to a target version is successful
@@ -388,7 +404,14 @@ func CheckGenesisTransform(t *testing.T, sourceVersion string, targetVersion str
388404
require.NoError(t, err)
389405

390406
_, consumerIdFound := params["consumer_id"]
391-
require.False(t, consumerIdFound, "consumer_id field should not be present in params")
407+
require.Equal(t, shouldContainConsumerId(targetVersion), consumerIdFound)
408+
409+
// Check for preCCV
410+
_, found = resultRaw["preCCV"]
411+
require.Equal(t, shouldContainPreCCVAndConnectionId(targetVersion), found)
412+
// Check for no connection_id
413+
_, found = resultRaw["connection_id"]
414+
require.Equal(t, shouldContainPreCCVAndConnectionId(targetVersion), found)
392415

393416
// Iterate over all fields of ConsumerParams and check:
394417
// - that they match between source and result genesis
@@ -401,9 +424,15 @@ func CheckGenesisTransform(t *testing.T, sourceVersion string, targetVersion str
401424
srcField := srcParams.Field(i).Interface()
402425
// srcType and resultType are the same so we can use index 'i' to get the field from resultParams
403426
resultField := resultParams.Field(i).Interface()
404-
if fieldName == "ConsumerId" {
427+
if fieldName == "ConsumerId" && !shouldContainConsumerId(targetVersion) {
405428
// ConsumerId is not present in v5.x => expect empty string when unmarshalled to v6
406429
require.EqualValues(t, "", resultField, "Field %s does not match", fieldName)
430+
} else if fieldName == "PreCCV" && !shouldContainPreCCVAndConnectionId(targetVersion) {
431+
// PreCCV is not present in <v6.4.x => expect false when unmarshalled it
432+
require.EqualValues(t, false, resultField, "Field %s does not match", fieldName)
433+
} else if fieldName == "ConnectionId" && !shouldContainPreCCVAndConnectionId(targetVersion) {
434+
// ConnectionId is not present in <v6.4.x => expect empty string when unmarshalled it
435+
require.EqualValues(t, "", resultField, "Field %s does not match", fieldName)
407436
} else {
408437
require.EqualValues(t, srcField, resultField, "Field %s does not match", fieldName)
409438
}
@@ -419,7 +448,7 @@ func CheckGenesisTransform(t *testing.T, sourceVersion string, targetVersion str
419448
for i := 0; i < srcParams.NumField(); i++ {
420449
fieldName := srcType.Field(i).Name
421450
// Skip Params field as it was checked above
422-
if fieldName == "Params" {
451+
if fieldName == "Params" || fieldName == "PreCCV" || fieldName == "ConnectionId" {
423452
continue
424453
}
425454
srcField := srcParams.Field(i).Interface()
@@ -428,3 +457,24 @@ func CheckGenesisTransform(t *testing.T, sourceVersion string, targetVersion str
428457
require.EqualValues(t, srcField, resultField, "Field %s does not match", fieldName)
429458
}
430459
}
460+
461+
func shouldContainConsumerId(version string) bool {
462+
switch version {
463+
case Consumer_v6_x_x, Consumer_v4_5_x:
464+
return true
465+
case Consumer_v5_x_x, Consumer_v4_x_x:
466+
return false
467+
}
468+
469+
return false
470+
}
471+
472+
func shouldContainPreCCVAndConnectionId(version string) bool {
473+
switch version {
474+
case Consumer_v4_x_x, Consumer_v4_5_x, Consumer_v5_x_x, Consumer_v6_x_x:
475+
return false
476+
// future greater versions will contain this fields and should return true
477+
}
478+
479+
return false
480+
}

0 commit comments

Comments
 (0)