Skip to content

Commit b9ecf44

Browse files
authored
fix: update consumer genesis transform (#2373)
* add genesis transform v5 * addressed comments * addressed more comments
1 parent af5d7a4 commit b9ecf44

File tree

3 files changed

+212
-759
lines changed

3 files changed

+212
-759
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `[x/consumer]` Updated `genesis transform` CLI to transform `consumer-genesis` content exported by v6.2 providers for consumer chains at version v5. Removed transformation for older consumer versions.
2+
([\#2373](https://github.com/cosmos/interchain-security/pull/2373))

app/consumer/genesis.go

Lines changed: 25 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8-
"regexp"
98
"strings"
109

1110
"github.com/spf13/cobra"
@@ -16,7 +15,6 @@ import (
1615
sdk "github.com/cosmos/cosmos-sdk/types"
1716
"github.com/cosmos/cosmos-sdk/version"
1817

19-
consumerTypes "github.com/cosmos/interchain-security/v6/x/ccv/consumer/types"
2018
"github.com/cosmos/interchain-security/v6/x/ccv/types"
2119
)
2220

@@ -33,152 +31,50 @@ type GenesisState map[string]json.RawMessage
3331
type IcsVersion string
3432

3533
const (
36-
v2_x IcsVersion = "v2.x"
37-
v3_0_x IcsVersion = "v3.0.x"
38-
v3_1_x IcsVersion = "v3.1.x"
39-
v3_2_x IcsVersion = "v3.2.x"
40-
v3_3_x IcsVersion = "v3.3.x"
4134
v4_x_x IcsVersion = "v4.x"
35+
v5_x_x IcsVersion = "v5.x"
4236
)
4337

4438
var TransformationVersions map[string]IcsVersion = map[string]IcsVersion{
45-
"v2.x": v2_x,
46-
"v3.0.x": v3_0_x,
47-
"v3.1.x": v3_1_x,
48-
"v3.2.x": v3_2_x,
49-
"v3.3.x": v3_3_x,
50-
"v4.x": v4_x_x,
39+
"v4.x": v4_x_x,
40+
"v5.x": v5_x_x,
5141
}
5242

53-
// Transformation of consumer genesis content as it is exported from a provider version v1,2,3
54-
// to a format readable by current consumer implementation.
55-
func transformToNew(jsonRaw []byte, ctx client.Context) (json.RawMessage, error) {
56-
// v1,2,3 uses deprecated fields of GenesisState type
57-
oldConsumerGenesis := consumerTypes.GenesisState{}
58-
err := ctx.Codec.UnmarshalJSON(jsonRaw, &oldConsumerGenesis)
59-
if err != nil {
60-
return nil, fmt.Errorf("reading consumer genesis data failed: %s", err)
61-
}
62-
63-
initialValSet := oldConsumerGenesis.InitialValSet
64-
// transformation from >= v3.3.x
65-
if len(initialValSet) == 0 {
66-
initialValSet = oldConsumerGenesis.Provider.InitialValSet
67-
}
68-
69-
clientState := oldConsumerGenesis.ProviderClientState
70-
if clientState == nil {
71-
clientState = oldConsumerGenesis.Provider.ClientState
72-
}
73-
74-
consensusState := oldConsumerGenesis.ProviderConsensusState
75-
if consensusState == nil {
76-
consensusState = oldConsumerGenesis.Provider.ConsensusState
77-
}
78-
79-
// Use DefaultRetryDelayPeriod if not set
80-
if oldConsumerGenesis.Params.RetryDelayPeriod == 0 {
81-
oldConsumerGenesis.Params.RetryDelayPeriod = types.DefaultRetryDelayPeriod
82-
}
83-
84-
// `SoftOptOutThreshold` is deprecated in the current consumer implementation, so set to zero
85-
oldConsumerGenesis.Params.SoftOptOutThreshold = "0"
86-
87-
// Versions before v3.3.x of provider genesis data fills up deprecated fields
88-
// ProviderClientState, ConsensusState and InitialValSet in type GenesisState
89-
newGenesis := types.ConsumerGenesisState{
90-
Params: oldConsumerGenesis.Params,
91-
Provider: types.ProviderInfo{
92-
ClientState: clientState,
93-
ConsensusState: consensusState,
94-
InitialValSet: initialValSet,
95-
},
96-
NewChain: oldConsumerGenesis.NewChain,
97-
}
98-
99-
newJson, err := ctx.Codec.MarshalJSON(&newGenesis)
100-
if err != nil {
101-
return nil, fmt.Errorf("failed marshalling data to new type: %s", err)
102-
}
103-
return newJson, nil
104-
}
105-
106-
// Transformation of consumer genesis content as it is exported by current provider version
107-
// to a format supported by consumer version v3.3.x
108-
func transformToV33(jsonRaw []byte, ctx client.Context) ([]byte, error) {
109-
// v1,2,3 uses deprecated fields of GenesisState type
110-
srcConGen := consumerTypes.GenesisState{}
111-
err := ctx.Codec.UnmarshalJSON(jsonRaw, &srcConGen)
112-
if err != nil {
113-
return nil, fmt.Errorf("reading consumer genesis data failed: %s", err)
114-
}
115-
116-
// Remove retry_delay_period from 'params'
117-
params, err := ctx.Codec.MarshalJSON(&srcConGen.Params)
118-
if err != nil {
119-
return nil, err
120-
}
121-
tmp := map[string]json.RawMessage{}
122-
if err := json.Unmarshal(params, &tmp); err != nil {
43+
// Remove a parameter from a JSON object
44+
func removeParameterFromParams(params json.RawMessage, param string) (json.RawMessage, error) {
45+
paramsMap := map[string]json.RawMessage{}
46+
if err := json.Unmarshal(params, &paramsMap); err != nil {
12347
return nil, fmt.Errorf("unmarshalling 'params' failed: %v", err)
12448
}
125-
_, exists := tmp["retry_delay_period"]
49+
_, exists := paramsMap[param]
12650
if exists {
127-
delete(tmp, "retry_delay_period")
51+
delete(paramsMap, param)
12852
}
129-
params, err = json.Marshal(tmp)
130-
if err != nil {
131-
return nil, err
132-
}
133-
134-
// Marshal GenesisState and patch 'params' value
135-
result, err := ctx.Codec.MarshalJSON(&srcConGen)
136-
if err != nil {
137-
return nil, err
138-
}
139-
genState := map[string]json.RawMessage{}
140-
if err := json.Unmarshal(result, &genState); err != nil {
141-
return nil, fmt.Errorf("unmarshalling 'GenesisState' failed: %v", err)
142-
}
143-
genState["params"] = params
144-
145-
result, err = json.Marshal(genState)
146-
if err != nil {
147-
return nil, fmt.Errorf("marshalling transformation result failed: %v", err)
148-
}
149-
return result, nil
53+
return json.Marshal(paramsMap)
15054
}
15155

152-
// Transformation of consumer genesis content as it is exported from current provider version
153-
// to a format readable by consumer implementation of version v2.x
154-
// Use removePreHashKey to remove prehash_key_before_comparison from result.
155-
func transformToV2(jsonRaw []byte, ctx client.Context, removePreHashKey bool) (json.RawMessage, error) {
156-
// populate deprecated fields of GenesisState used by version v2.x
157-
srcConGen := consumerTypes.GenesisState{}
56+
// Transformation of consumer genesis content as it is exported by provider version >= v6.2.x
57+
// 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
58+
// This transformation removes the 'consumer_id' parameter from the 'params' field introduced in ICS v6.2.x
59+
func transformToV5(jsonRaw []byte, ctx client.Context) (json.RawMessage, error) {
60+
srcConGen := types.ConsumerGenesisState{}
15861
err := ctx.Codec.UnmarshalJSON(jsonRaw, &srcConGen)
15962
if err != nil {
16063
return nil, fmt.Errorf("reading consumer genesis data failed: %s", err)
16164
}
16265

163-
// remove retry_delay_period from 'params' if present (introduced in v4.x)
66+
// Remove 'consumer_id' from 'params'
16467
params, err := ctx.Codec.MarshalJSON(&srcConGen.Params)
16568
if err != nil {
16669
return nil, err
16770
}
168-
paramsMap := map[string]json.RawMessage{}
169-
if err := json.Unmarshal(params, &paramsMap); err != nil {
170-
return nil, fmt.Errorf("unmarshalling 'params' failed: %v", err)
171-
}
172-
_, exists := paramsMap["retry_delay_period"]
173-
if exists {
174-
delete(paramsMap, "retry_delay_period")
175-
}
176-
params, err = json.Marshal(paramsMap)
71+
72+
params, err = removeParameterFromParams(params, "consumer_id")
17773
if err != nil {
17874
return nil, err
17975
}
18076

181-
// marshal GenesisState and patch 'params' value
77+
// Marshal GenesisState and patch 'params' value
18278
result, err := ctx.Codec.MarshalJSON(&srcConGen)
18379
if err != nil {
18480
return nil, err
@@ -189,65 +85,10 @@ func transformToV2(jsonRaw []byte, ctx client.Context, removePreHashKey bool) (j
18985
}
19086
genState["params"] = params
19187

192-
provider, err := ctx.Codec.MarshalJSON(&srcConGen.Provider)
193-
if err != nil {
194-
return nil, fmt.Errorf("marshalling 'Provider' failed: %v", err)
195-
}
196-
providerMap := map[string]json.RawMessage{}
197-
if err := json.Unmarshal(provider, &providerMap); err != nil {
198-
return nil, fmt.Errorf("unmarshalling 'provider' failed: %v", err)
199-
}
200-
201-
// patch .initial_val_set form .provider.initial_val_set if needed
202-
if len(srcConGen.Provider.InitialValSet) > 0 {
203-
valSet, exists := providerMap["initial_val_set"]
204-
if !exists {
205-
return nil, fmt.Errorf("'initial_val_set' not found in provider data")
206-
}
207-
_, exists = genState["initial_val_set"]
208-
if exists {
209-
genState["initial_val_set"] = valSet
210-
}
211-
}
212-
213-
// patch .provider_consensus_state from provider.consensus_state if needed
214-
if srcConGen.Provider.ConsensusState != nil {
215-
valSet, exists := providerMap["consensus_state"]
216-
if !exists {
217-
return nil, fmt.Errorf("'consensus_state' not found in provider data")
218-
}
219-
_, exists = genState["provider_consensus_state"]
220-
if exists {
221-
genState["provider_consensus_state"] = valSet
222-
}
223-
}
224-
225-
// patch .provider_client_state from provider.client_state if needed
226-
if srcConGen.Provider.ClientState != nil {
227-
clientState, exists := providerMap["client_state"]
228-
if !exists {
229-
return nil, fmt.Errorf("'client_state' not found in provider data")
230-
}
231-
_, exists = genState["provider_client_state"]
232-
if exists {
233-
genState["provider_client_state"] = clientState
234-
}
235-
}
236-
237-
// delete .provider entry (introduced in v3.3.x)
238-
delete(genState, "provider")
239-
240-
// Marshall final result
24188
result, err = json.Marshal(genState)
24289
if err != nil {
24390
return nil, fmt.Errorf("marshalling transformation result failed: %v", err)
24491
}
245-
246-
if removePreHashKey {
247-
// remove all `prehash_key_before_comparison` entries not supported in v2.x (see ics23)
248-
re := regexp.MustCompile(`,\s*"prehash_key_before_comparison"\s*:\s*(false|true)`)
249-
result = re.ReplaceAll(result, []byte{})
250-
}
25192
return result, nil
25293
}
25394

@@ -258,16 +99,8 @@ func transformGenesis(ctx client.Context, targetVersion IcsVersion, jsonRaw []by
25899
var err error
259100

260101
switch targetVersion {
261-
// v2.x, v3.0-v3.2 share same consumer genesis type
262-
case v2_x:
263-
newConsumerGenesis, err = transformToV2(jsonRaw, ctx, true)
264-
case v3_0_x, v3_1_x, v3_2_x:
265-
// same as v2 replacement without need of `prehash_key_before_comparison` removal
266-
newConsumerGenesis, err = transformToV2(jsonRaw, ctx, false)
267-
case v3_3_x:
268-
newConsumerGenesis, err = transformToV33(jsonRaw, ctx)
269-
case v4_x_x:
270-
newConsumerGenesis, err = transformToNew(jsonRaw, ctx)
102+
case v4_x_x, v5_x_x:
103+
newConsumerGenesis, err = transformToV5(jsonRaw, ctx)
271104
default:
272105
err = fmt.Errorf("unsupported target version '%s'. Run %s --help",
273106
targetVersion, version.AppName)
@@ -280,10 +113,9 @@ func transformGenesis(ctx client.Context, targetVersion IcsVersion, jsonRaw []by
280113
}
281114

282115
// Transform a consumer genesis json file exported from a given ccv provider version
283-
// to a consumer genesis json format supported by current ccv consumer version or v2.x
116+
// to a consumer genesis json format supported by current ccv consumer version
284117
// This allows user to patch consumer genesis of
285-
// - current implementation from exports of provider of < v3.3.x
286-
// - v2.x from exports of provider >= v3.2.x
118+
// - v4.x, v5.x, v6.1.x from exports of provider >= v6.2.x
287119
//
288120
// Result will be written to defined output.
289121
func TransformConsumerGenesis(cmd *cobra.Command, args []string) error {
@@ -336,7 +168,7 @@ func GetConsumerGenesisTransformCmd() *cobra.Command {
336168
Short: "Transform CCV consumer genesis data exported to a specific target format",
337169
Long: strings.TrimSpace(
338170
fmt.Sprintf(`
339-
Transform the consumer genesis data exported from a provider version v1,v2, v3, v4 to a specified consumer target version.
171+
Transform the consumer genesis data exported from a provider version v5.x v6.x to a specified consumer target version.
340172
The result is printed to STDOUT.
341173
342174
Note: Content to be transformed is not the consumer genesis file itself but the exported content from provider chain which is used to patch the consumer genesis file!
@@ -349,7 +181,7 @@ $ %s --to v2.x transform /path/to/ccv_consumer_genesis.json
349181
Args: cobra.RangeArgs(1, 2),
350182
RunE: TransformConsumerGenesis,
351183
}
352-
cmd.Flags().String("to", string(v4_x_x),
184+
cmd.Flags().String("to", string(v5_x_x),
353185
fmt.Sprintf("target version for consumer genesis. Supported versions %s",
354186
maps.Keys(TransformationVersions)))
355187
return cmd

0 commit comments

Comments
 (0)