forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_utils.go
289 lines (281 loc) · 7.01 KB
/
json_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"encoding/json"
"fmt"
"reflect"
)
// MarshalJSON marshals a step into JSON while including the type of the action.
func (step Step) MarshalJSON() ([]byte, error) {
actionType := reflect.TypeOf(step.Action)
result := struct {
ActionType string
Action interface{}
State State
}{
ActionType: actionType.String(),
Action: step.Action,
State: step.State,
}
return json.Marshal(result)
}
// UnmarshalJSON unmarshals a step from JSON while including the type of the action.
func (step *Step) UnmarshalJSON(data []byte) error {
var tmp struct {
ActionType string
Action json.RawMessage
State State
}
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
action, err := UnmarshalMapToActionType(tmp.Action, tmp.ActionType)
if err != nil {
return err
}
step.Action = action
step.State = tmp.State
return nil
}
// UnmarshalMapToActionType takes a JSON object and an action type and marshals into an object of the corresponding action.
func UnmarshalMapToActionType(rawAction json.RawMessage, actionTypeString string) (interface{}, error) {
var err error
switch actionTypeString {
case "main.SubmitConsumerAdditionProposalAction":
var a SubmitConsumerAdditionProposalAction
err = json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.SendTokensAction":
var a SendTokensAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.StartChainAction":
var a StartChainAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.SubmitTextProposalAction":
var a SubmitTextProposalAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.SubmitConsumerRemovalProposalAction":
var a SubmitConsumerRemovalProposalAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.SubmitEnableTransfersProposalAction":
var a SubmitEnableTransfersProposalAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.VoteGovProposalAction":
var a VoteGovProposalAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.StartConsumerChainAction":
var a StartConsumerChainAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.AddChainToRelayerAction":
var a AddChainToRelayerAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.AddIbcConnectionAction":
var a AddIbcConnectionAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.AddIbcChannelAction":
var a AddIbcChannelAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.TransferChannelCompleteAction":
var a TransferChannelCompleteAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.UnjailValidatorAction":
var a UnjailValidatorAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.AssignConsumerPubKeyAction":
var a AssignConsumerPubKeyAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.DelegateTokensAction":
var a DelegateTokensAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.RelayPacketsAction":
var a RelayPacketsAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.RegisterRepresentativeAction":
var a RegisterRepresentativeAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.RelayRewardPacketsToProviderAction":
var a RelayRewardPacketsToProviderAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.SubmitChangeRewardDenomsProposalAction":
var a SubmitChangeRewardDenomsProposalAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.DowntimeSlashAction":
var a DowntimeSlashAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.UnbondTokensAction":
var a UnbondTokensAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.CancelUnbondTokensAction":
var a CancelUnbondTokensAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.RedelegateTokensAction":
var a RedelegateTokensAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.DoublesignSlashAction":
var a DoublesignSlashAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.StartRelayerAction":
var a StartRelayerAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.SlashMeterReplenishmentAction":
var a SlashMeterReplenishmentAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.WaitTimeAction":
var a WaitTimeAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.CreateIbcClientsAction":
var a CreateIbcClientsAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.LegacyUpgradeProposalAction":
var a UpgradeProposalAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.WaitUntilBlockAction":
var a WaitUntilBlockAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.ChangeoverChainAction":
var a ChangeoverChainAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.StartSovereignChainAction":
var a StartSovereignChainAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.LightClientEquivocationAttackAction":
var a LightClientEquivocationAttackAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.LightClientAmnesiaAttackAction":
var a LightClientAmnesiaAttackAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.LightClientLunaticAttackAction":
var a LightClientLunaticAttackAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.ForkConsumerChainAction":
var a ForkConsumerChainAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.DetectConsumerEvidenceAction":
var a DetectConsumerEvidenceAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.SubmitConsumerMisbehaviourAction":
var a SubmitConsumerMisbehaviourAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
case "main.UpdateLightClientAction":
var a UpdateLightClientAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
}
default:
return nil, fmt.Errorf("unknown action type: %s", actionTypeString)
}
return nil, err
}