-
Notifications
You must be signed in to change notification settings - Fork 176
/
tx.go
314 lines (259 loc) · 7.63 KB
/
tx.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//nolint
package rest
import (
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gorilla/mux"
"github.com/maticnetwork/heimdall/slashing/types"
hmTypes "github.com/maticnetwork/heimdall/types"
"github.com/maticnetwork/heimdall/types/rest"
restClient "github.com/maticnetwork/heimdall/client/rest"
)
//It represents unjail msg.
//swagger:response slashingUnjailResponse
type slashingUnjailResponse struct {
//in:body
Output slashingUnjailOutput `json:"output"`
}
type slashingUnjailOutput struct {
Type string `json:"type"`
Value slashingUnjailValue `json:"value"`
}
type slashingUnjailValue struct {
Msg slashingUnjailMsg `json:"msg"`
Signature string `json:"signature"`
Memo string `json:"memo"`
}
type slashingUnjailMsg struct {
Type string `json:"type"`
Value slashingUnjailVal `json:"value"`
}
type slashingUnjailVal struct {
From string `json:"from"`
ID uint64 `json:"id"`
TxHash string `json:"tx_hash"`
LogIndex uint64 `json:"log_index"`
BlockNumber uint64 `json:"block_number"`
}
//It represents Propose Span msg.
//swagger:response slashingNewTickResponse
type slashingNewTickResponse struct {
//in:body
Output slashingNewTickOutput `json:"output"`
}
type slashingNewTickOutput struct {
Type string `json:"type"`
Value slashingNewTickValue `json:"value"`
}
type slashingNewTickValue struct {
Msg slashingNewTickMsg `json:"msg"`
Signature string `json:"signature"`
Memo string `json:"memo"`
}
type slashingNewTickMsg struct {
Type string `json:"type"`
Value slashingNewTickVal `json:"value"`
}
type slashingNewTickVal struct {
ID uint64 `json:"id"`
Proposer string `json:"proposer"`
SlashingInfoBytes string `json:"slashinginfobytes"`
}
//It represents Propose Span msg.
//swagger:response slashingTickAckResponse
type slashingTickAckResponse struct {
//in:body
Output slashingTickAckOutput `json:"output"`
}
type slashingTickAckOutput struct {
Type string `json:"type"`
Value slashingTickAckValue `json:"value"`
}
type slashingTickAckValue struct {
Msg slashingTickAckMsg `json:"msg"`
Signature string `json:"signature"`
Memo string `json:"memo"`
}
type slashingTickAckMsg struct {
Type string `json:"type"`
Value slashingTickAckVal `json:"value"`
}
type slashingTickAckVal struct {
From string `json:"from"`
ID uint64 `json:"tick_id"`
Amount uint64 `json:"slashed_amount"`
TxHash string `json:"tx_hash"`
LogIndex uint64 `json:"log_index"`
BlockNumber uint64 `json:"block_number"`
}
func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc(
"/slashing/validators/{validatorAddr}/unjail",
newUnjailRequestHandlerFn(cliCtx),
).Methods("POST")
r.HandleFunc(
"/slashing/tick",
newTickRequestHandlerFn(cliCtx),
).Methods("POST")
r.HandleFunc(
"/slashing/tick-ack",
newTickAckHandler(cliCtx),
).Methods("POST")
}
// Unjail TX body
type UnjailReq struct {
BaseReq rest.BaseReq `json:"base_req"`
ID uint64 `json:"ID"`
TxHash string `json:"tx_hash"`
LogIndex uint64 `json:"log_index"`
BlockNumber uint64 `json:"block_number" yaml:"block_number"`
}
type TickReq struct {
BaseReq rest.BaseReq `json:"base_req"`
ID uint64 `json:"ID"`
Proposer string `json:"proposer"`
SlashingInfoBytes string `json:"slashing_info_bytes"`
}
type TickAckReq struct {
BaseReq rest.BaseReq `json:"base_req"`
ID uint64 `json:"ID"`
Amount uint64 `json:"amount"`
TxHash string `json:"tx_hash"`
LogIndex uint64 `json:"log_index"`
BlockNumber uint64 `json:"block_number" yaml:"block_number"`
}
//swagger:parameters slashingUnjail
type slashingUnjailParam struct {
//Body
//required:true
//in:body
Input slashingUnjailInput `json:"input"`
//Validator Address
//required:true
//in:path
ValidatorAddr string `json:"validatorAddr"`
}
type slashingUnjailInput struct {
BaseReq BaseReq `json:"base_req"`
ID uint64 `json:"ID"`
TxHash string `json:"tx_hash"`
LogIndex uint64 `json:"log_index"`
BlockNumber uint64 `json:"block_number" yaml:"block_number"`
}
type BaseReq struct {
//Address of the sender
//required:true
//in:body
From string `json:"address"`
//Chain ID of Heimdall
//required:true
//in:body
ChainID string `json:"chain_id"`
}
// swagger:route POST /slashing/validators/{validatorAddr}/unjail slashing slashingUnjail
// It returns the prepared msg for unjail
// responses:
// 200: slashingUnjailResponse
func newUnjailRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// read req from Request
var req UnjailReq
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgUnjail(
hmTypes.HexToHeimdallAddress(req.BaseReq.From),
req.ID,
hmTypes.HexToHeimdallHash(req.TxHash),
req.LogIndex,
req.BlockNumber,
)
err := msg.ValidateBasic()
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
restClient.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}
//swagger:parameters slashingNewTick
type slashingNewTickParam struct {
//Body
//required:true
//in:body
Input slashingNewTickInput `json:"input"`
}
type slashingNewTickInput struct {
BaseReq BaseReq `json:"base_req"`
ID uint64 `json:"ID"`
Proposer string `json:"proposer"`
SlashingInfoBytes string `json:"slashing_info_bytes"`
}
// swagger:route POST /slashing/tick slashing slashingNewTick
// It returns the prepared msg for new tick
// responses:
// 200: slashingNewTickResponse
func newTickRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// read req from Request
var req TickReq
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgTick(
req.ID,
hmTypes.HexToHeimdallAddress(req.Proposer),
hmTypes.HexToHexBytes(req.SlashingInfoBytes),
)
restClient.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}
//swagger:parameters slashingTickAck
type slashingTickAckParam struct {
//Body
//required:true
//in:body
Input slashingTickAckInput `json:"input"`
}
type slashingTickAckInput struct {
BaseReq BaseReq `json:"base_req"`
ID uint64 `json:"ID"`
Amount uint64 `json:"amount"`
TxHash string `json:"tx_hash"`
LogIndex uint64 `json:"log_index"`
BlockNumber uint64 `json:"block_number" yaml:"block_number"`
}
// swagger:route POST slashing/tick-ack slashing slashingTickAck
// It returns the prepared msg for tick-ack
// responses:
// 200: stakingTickAckResponse
func newTickAckHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req TickAckReq
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgTickAck(
hmTypes.HexToHeimdallAddress(req.BaseReq.From),
req.ID,
req.Amount,
hmTypes.HexToHeimdallHash(req.TxHash),
req.LogIndex,
req.BlockNumber,
)
restClient.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}