-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
185 lines (161 loc) · 5.12 KB
/
api.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"github.com/samber/lo"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/go-playground/validator/v10"
"github.com/gorilla/mux"
)
func NewRouter(srv *Service, secret []byte) http.Handler {
r := mux.NewRouter()
r.Use(authMiddleware(secret))
impl := newImplAPI(srv)
r.HandleFunc("/requests", impl.create).Methods("POST")
r.HandleFunc("/requests/{id:[0-9]+}", impl.get).Methods("GET")
r.HandleFunc("/requests/{id:[0-9]+}/speedup", impl.speedup).Methods("POST")
return r
}
type implAPI struct {
validate *validator.Validate
srv *Service
}
func newImplAPI(srv *Service) *implAPI {
return &implAPI{
validator.New(validator.WithRequiredStructEnabled()),
srv,
}
}
type createRequestArgs struct {
Miner address.Address `json:"miner"` // miner address
From time.Time `json:"from"` // expiration from
To time.Time `json:"to"` // expiration to
Extension *abi.ChainEpoch `json:"extension"` // extension to set
NewExpiration *abi.ChainEpoch `json:"new_expiration"` // new expiration to set
Tolerance *abi.ChainEpoch `json:"tolerance"` // tolerance for expiration
MaxSectors *int `json:"max_sectors"` // max sectors to include in a single message
MaxInitialPledges *float64 `json:"max_initial_pledges"` // max initial pledges to extend
DryRun bool `json:"dry_run"`
}
func (a *implAPI) create(w http.ResponseWriter, r *http.Request) {
var args createRequestArgs
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
warpResponse(w, http.StatusBadRequest, nil, err)
return
}
if err := a.validate.Struct(args); err != nil {
warpResponse(w, http.StatusBadRequest, nil, err)
return
}
if args.Miner.Empty() {
warpResponse(w, http.StatusBadRequest, nil, fmt.Errorf("miner address is empty"))
return
}
if args.Extension == nil && args.NewExpiration == nil {
warpResponse(w, http.StatusBadRequest, nil, fmt.Errorf("either extension or new_expiration must be set"))
return
}
fromEpoch := TimestampToEpoch(args.From)
toEpoch := TimestampToEpoch(args.To)
if toEpoch < fromEpoch {
warpResponse(w, http.StatusBadRequest, nil, fmt.Errorf("to must be greater than from"))
return
}
var maxSectors int
if args.MaxSectors == nil {
maxSectors = 500 // default value
} else {
maxSectors = *args.MaxSectors
}
if maxSectors < 0 {
warpResponse(w, http.StatusBadRequest, nil, fmt.Errorf("max_sectors must be greater than 0"))
return
}
req, err := a.srv.createRequest(r.Context(), args.Miner, args.From, args.To,
args.Extension, args.NewExpiration, args.Tolerance, maxSectors, lo.FromPtr(args.MaxInitialPledges), args.DryRun)
if err != nil {
warpResponse(w, http.StatusBadRequest, nil, err)
return
}
if req.Messages == nil {
req.Messages = make([]*Message, 0)
}
warpResponse(w, http.StatusOK, req, nil)
}
func (a *implAPI) get(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
warpResponse(w, http.StatusBadRequest, nil, fmt.Errorf("invalid id: %s", vars["id"]))
return
}
req, err := a.srv.getRequest(r.Context(), uint(id))
if err != nil {
warpResponse(w, http.StatusBadRequest, nil, err)
return
}
warpResponse(w, http.StatusOK, req, nil)
}
type speedupRequestArgs struct {
FeeLimit *string `json:"fee_limit"`
}
func (a *implAPI) speedup(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
warpResponse(w, http.StatusBadRequest, nil, fmt.Errorf("invalid id: %s", vars["id"]))
return
}
var args speedupRequestArgs
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
warpResponse(w, http.StatusBadRequest, nil, err)
return
}
if err := a.validate.Struct(args); err != nil {
warpResponse(w, http.StatusBadRequest, nil, err)
return
}
var mss *api.MessageSendSpec
if args.FeeLimit != nil {
maxFee, err := types.ParseFIL(*args.FeeLimit)
if err != nil {
warpResponse(w, http.StatusBadRequest, nil,
fmt.Errorf("failed to parse fee limit: %s", err))
return
}
mss = &api.MessageSendSpec{
MaxFee: abi.TokenAmount(maxFee),
}
}
err = a.srv.speedupRequest(uint(id), mss)
if err != nil {
warpResponse(w, http.StatusBadRequest, nil, err)
return
}
warpResponse(w, http.StatusOK, "success", nil)
}
type response struct {
Data interface{} `json:"data,omitempty"`
Error *string `json:"error,omitempty"`
}
func warpResponse(w http.ResponseWriter, code int, data interface{}, err error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
resp := response{Data: data}
if err != nil {
msg := err.Error()
resp.Error = &msg
}
payload, err := json.Marshal(resp) // nolint: errcheck
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, _ = w.Write(payload) // nolint: errcheck
}