/
tx.go
179 lines (145 loc) · 3.94 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
// nolint
package rest
import (
"errors"
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gorilla/mux"
jsoniter "github.com/json-iterator/go"
"github.com/ethereum/go-ethereum/common"
"github.com/maticnetwork/heimdall/bor/types"
restClient "github.com/maticnetwork/heimdall/client/rest"
hmTypes "github.com/maticnetwork/heimdall/types"
"github.com/maticnetwork/heimdall/types/rest"
)
// It represents Propose Span msg.
//
//swagger:response borProposeSpanResponse
type borProposeSpanResponse struct {
//in:body
Output output `json:"output"`
}
type output struct {
Type string `json:"type"`
Value value `json:"value"`
}
type value struct {
Msg msg `json:"msg"`
Signature string `json:"signature"`
Memo string `json:"memo"`
}
type msg struct {
Type string `json:"type"`
Value val `json:"value"`
}
type val struct {
SpanID string `json:"span_id"`
Proposer string `json:"proposer"`
StartBlock string `json:"start_block"`
EndBlock string `json:"end_block"`
BorChainId string `json:"bor_chain_id"`
Seed string `json:"seed"`
}
func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc(
"/bor/propose-span",
postProposeSpanHandlerFn(cliCtx),
).Methods("POST")
}
// ProposeSpanReq struct for proposing new span
type ProposeSpanReq struct {
BaseReq rest.BaseReq `json:"base_req"`
ID uint64 `json:"span_id"`
StartBlock uint64 `json:"start_block"`
BorChainID string `json:"bor_chain_id"`
}
//swagger:parameters borProposeSpan
type borProposeSpan struct {
//Body
//required:true
//in:body
Input SendReqInput `json:"input"`
}
type SendReqInput struct {
//required:true
//in:body
BaseReq BaseReq `json:"base_req"`
//required:true
//in:body
ID uint64 `json:"span_id"`
//required:true
//in:body
StartBlock uint64 `json:"start_block"`
//required:true
//in:body
BorChainID string `json:"bor_chain_id"`
}
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 /bor/propose-span bor borProposeSpan
// It returns the prepared msg for proposing the span
// responses:
// 200: borProposeSpanResponse
func postProposeSpanHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// read req from request
var req ProposeSpanReq
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
//
// Get span duration
//
// fetch duration
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryParams, types.ParamSpan), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if len(res) == 0 {
rest.WriteErrorResponse(w, http.StatusBadRequest, errors.New("Span duration not found ").Error())
return
}
var spanDuration uint64
if err = jsoniter.ConfigFastest.Unmarshal(res, &spanDuration); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
// fetch seed
res, _, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNextSpanSeed), nil)
if err != nil {
RestLogger.Error("Error while fetching next span seed ", "Error", err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
var seed common.Hash
if err = jsoniter.ConfigFastest.Unmarshal(res, &seed); err != nil {
return
}
// draft a propose span message
msg := types.NewMsgProposeSpan(
req.ID,
hmTypes.HexToHeimdallAddress(req.BaseReq.From),
req.StartBlock,
req.StartBlock+spanDuration-1,
req.BorChainID,
seed,
)
// send response
restClient.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}