Skip to content

Commit 930110d

Browse files
fix(api): resolve panic when storage incentives disabled (#5150)
1 parent c7bb73e commit 930110d

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

pkg/api/router.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (s *Service) checkRouteAvailability(handler http.Handler) http.Handler {
191191
func (s *Service) checkSwapAvailability(handler http.Handler) http.Handler {
192192
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
193193
if !s.swapEnabled {
194-
jsonhttp.NotImplemented(w, "Swap is disabled. This endpoint is unavailable.")
194+
jsonhttp.Forbidden(w, "Swap is disabled. This endpoint is unavailable.")
195195
return
196196
}
197197
handler.ServeHTTP(w, r)
@@ -201,7 +201,17 @@ func (s *Service) checkSwapAvailability(handler http.Handler) http.Handler {
201201
func (s *Service) checkChequebookAvailability(handler http.Handler) http.Handler {
202202
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
203203
if !s.chequebookEnabled {
204-
jsonhttp.NotImplemented(w, "Chequebook is disabled. This endpoint is unavailable.")
204+
jsonhttp.Forbidden(w, "Chequebook is disabled. This endpoint is unavailable.")
205+
return
206+
}
207+
handler.ServeHTTP(w, r)
208+
})
209+
}
210+
211+
func (s *Service) checkStorageIncentivesAvailability(handler http.Handler) http.Handler {
212+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
213+
if s.redistributionAgent == nil {
214+
jsonhttp.Forbidden(w, "Storage incentives are disabled. This endpoint is unavailable.")
205215
return
206216
}
207217
handler.ServeHTTP(w, r)
@@ -635,9 +645,12 @@ func (s *Service) mountBusinessDebug() {
635645
})),
636646
)
637647

638-
handle("/redistributionstate", jsonhttp.MethodHandler{
639-
"GET": http.HandlerFunc(s.redistributionStatusHandler),
640-
})
648+
handle("/redistributionstate", web.ChainHandlers(
649+
s.checkStorageIncentivesAvailability,
650+
web.FinalHandler(jsonhttp.MethodHandler{
651+
"GET": http.HandlerFunc(s.redistributionStatusHandler),
652+
})),
653+
)
641654

642655
handle("/status", jsonhttp.MethodHandler{
643656
"GET": web.ChainHandlers(

pkg/api/router_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,17 @@ func TestEndpointOptions(t *testing.T) {
281281
{"/consumed", []string{"GET"}, http.StatusNoContent},
282282
{"/consumed/{peer}", []string{"GET"}, http.StatusNoContent},
283283
{"/timesettlements", []string{"GET"}, http.StatusNoContent},
284-
{"/settlements", nil, http.StatusNotImplemented},
285-
{"/settlements/{peer}", nil, http.StatusNotImplemented},
286-
{"/chequebook/cheque/{peer}", nil, http.StatusNotImplemented},
287-
{"/chequebook/cheque", nil, http.StatusNotImplemented},
288-
{"/chequebook/cashout/{peer}", nil, http.StatusNotImplemented},
284+
{"/settlements", nil, http.StatusForbidden},
285+
{"/settlements/{peer}", nil, http.StatusForbidden},
286+
{"/chequebook/cheque/{peer}", nil, http.StatusForbidden},
287+
{"/chequebook/cheque", nil, http.StatusForbidden},
288+
{"/chequebook/cashout/{peer}", nil, http.StatusForbidden},
289289
{"/chequebook/balance", []string{"GET"}, http.StatusNoContent},
290290
{"/chequebook/address", []string{"GET"}, http.StatusNoContent},
291291
{"/chequebook/deposit", []string{"POST"}, http.StatusNoContent},
292292
{"/chequebook/withdraw", []string{"POST"}, http.StatusNoContent},
293-
{"/wallet", nil, http.StatusNotImplemented},
294-
{"/wallet/withdraw/{coin}", nil, http.StatusNotImplemented},
293+
{"/wallet", nil, http.StatusForbidden},
294+
{"/wallet/withdraw/{coin}", nil, http.StatusForbidden},
295295
{"/stamps", []string{"GET"}, http.StatusNoContent},
296296
{"/stamps/{batch_id}", []string{"GET"}, http.StatusNoContent},
297297
{"/stamps/{batch_id}/buckets", []string{"GET"}, http.StatusNoContent},
@@ -381,12 +381,12 @@ func TestEndpointOptions(t *testing.T) {
381381
{"/chequebook/cheque/{peer}", []string{"GET"}, http.StatusNoContent},
382382
{"/chequebook/cheque", []string{"GET"}, http.StatusNoContent},
383383
{"/chequebook/cashout/{peer}", []string{"GET", "POST"}, http.StatusNoContent},
384-
{"/chequebook/balance", nil, http.StatusNotImplemented},
385-
{"/chequebook/address", nil, http.StatusNotImplemented},
386-
{"/chequebook/deposit", nil, http.StatusNotImplemented},
387-
{"/chequebook/withdraw", nil, http.StatusNotImplemented},
388-
{"/wallet", nil, http.StatusNotImplemented},
389-
{"/wallet/withdraw/{coin}", nil, http.StatusNotImplemented},
384+
{"/chequebook/balance", nil, http.StatusForbidden},
385+
{"/chequebook/address", nil, http.StatusForbidden},
386+
{"/chequebook/deposit", nil, http.StatusForbidden},
387+
{"/chequebook/withdraw", nil, http.StatusForbidden},
388+
{"/wallet", nil, http.StatusForbidden},
389+
{"/wallet/withdraw/{coin}", nil, http.StatusForbidden},
390390
{"/stamps", []string{"GET"}, http.StatusNoContent},
391391
{"/stamps/{batch_id}", []string{"GET"}, http.StatusNoContent},
392392
{"/stamps/{batch_id}/buckets", []string{"GET"}, http.StatusNoContent},

0 commit comments

Comments
 (0)