5
5
"fmt"
6
6
"os"
7
7
"path/filepath"
8
- "regexp"
9
8
"strings"
10
9
11
10
"github.com/spf13/cobra"
@@ -16,7 +15,6 @@ import (
16
15
sdk "github.com/cosmos/cosmos-sdk/types"
17
16
"github.com/cosmos/cosmos-sdk/version"
18
17
19
- consumerTypes "github.com/cosmos/interchain-security/v6/x/ccv/consumer/types"
20
18
"github.com/cosmos/interchain-security/v6/x/ccv/types"
21
19
)
22
20
@@ -33,152 +31,50 @@ type GenesisState map[string]json.RawMessage
33
31
type IcsVersion string
34
32
35
33
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"
41
34
v4_x_x IcsVersion = "v4.x"
35
+ v5_x_x IcsVersion = "v5.x"
42
36
)
43
37
44
38
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 ,
51
41
}
52
42
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 {
123
47
return nil , fmt .Errorf ("unmarshalling 'params' failed: %v" , err )
124
48
}
125
- _ , exists := tmp [ "retry_delay_period" ]
49
+ _ , exists := paramsMap [ param ]
126
50
if exists {
127
- delete (tmp , "retry_delay_period" )
51
+ delete (paramsMap , param )
128
52
}
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 )
150
54
}
151
55
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 {}
158
61
err := ctx .Codec .UnmarshalJSON (jsonRaw , & srcConGen )
159
62
if err != nil {
160
63
return nil , fmt .Errorf ("reading consumer genesis data failed: %s" , err )
161
64
}
162
65
163
- // remove retry_delay_period from 'params' if present (introduced in v4.x)
66
+ // Remove 'consumer_id' from 'params'
164
67
params , err := ctx .Codec .MarshalJSON (& srcConGen .Params )
165
68
if err != nil {
166
69
return nil , err
167
70
}
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" )
177
73
if err != nil {
178
74
return nil , err
179
75
}
180
76
181
- // marshal GenesisState and patch 'params' value
77
+ // Marshal GenesisState and patch 'params' value
182
78
result , err := ctx .Codec .MarshalJSON (& srcConGen )
183
79
if err != nil {
184
80
return nil , err
@@ -189,65 +85,10 @@ func transformToV2(jsonRaw []byte, ctx client.Context, removePreHashKey bool) (j
189
85
}
190
86
genState ["params" ] = params
191
87
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
241
88
result , err = json .Marshal (genState )
242
89
if err != nil {
243
90
return nil , fmt .Errorf ("marshalling transformation result failed: %v" , err )
244
91
}
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
- }
251
92
return result , nil
252
93
}
253
94
@@ -258,16 +99,8 @@ func transformGenesis(ctx client.Context, targetVersion IcsVersion, jsonRaw []by
258
99
var err error
259
100
260
101
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 )
271
104
default :
272
105
err = fmt .Errorf ("unsupported target version '%s'. Run %s --help" ,
273
106
targetVersion , version .AppName )
@@ -280,10 +113,9 @@ func transformGenesis(ctx client.Context, targetVersion IcsVersion, jsonRaw []by
280
113
}
281
114
282
115
// 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
284
117
// 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
287
119
//
288
120
// Result will be written to defined output.
289
121
func TransformConsumerGenesis (cmd * cobra.Command , args []string ) error {
@@ -336,7 +168,7 @@ func GetConsumerGenesisTransformCmd() *cobra.Command {
336
168
Short : "Transform CCV consumer genesis data exported to a specific target format" ,
337
169
Long : strings .TrimSpace (
338
170
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.
340
172
The result is printed to STDOUT.
341
173
342
174
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
349
181
Args : cobra .RangeArgs (1 , 2 ),
350
182
RunE : TransformConsumerGenesis ,
351
183
}
352
- cmd .Flags ().String ("to" , string (v4_x_x ),
184
+ cmd .Flags ().String ("to" , string (v5_x_x ),
353
185
fmt .Sprintf ("target version for consumer genesis. Supported versions %s" ,
354
186
maps .Keys (TransformationVersions )))
355
187
return cmd
0 commit comments