Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(postage_api): check if batch exists for stamp issuer #4440

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions pkg/api/postage.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,24 @@ func (s *Service) postageGetStampsHandler(w http.ResponseWriter, r *http.Request
stampIssuers := s.post.StampIssuers()
resp.Stamps = make([]postageStampResponse, 0, len(stampIssuers))
for _, v := range stampIssuers {

exists, err := s.batchStore.Exists(v.ID())
if err != nil {
logger.Error(err, "get stamp issuer: check batch failed", "batch_id", hex.EncodeToString(v.ID()))
jsonhttp.InternalServerError(w, "unable to check batch")
return
}
if !exists {
continue
}

batchTTL, err := s.estimateBatchTTLFromID(v.ID())
if err != nil {
logger.Debug("get stamp issuer: estimate batch expiration failed", "batch_id", hex.EncodeToString(v.ID()), "error", err)
logger.Error(nil, "get stamp issuer: estimate batch expiration failed")
logger.Error(err, "get stamp issuer: estimate batch expiration failed", "batch_id", hex.EncodeToString(v.ID()))
jsonhttp.InternalServerError(w, "unable to estimate batch expiration")
return
}

resp.Stamps = append(resp.Stamps, postageStampResponse{
BatchID: v.ID(),
Utilization: v.Utilization(),
Expand All @@ -205,7 +216,7 @@ func (s *Service) postageGetStampsHandler(w http.ResponseWriter, r *http.Request
jsonhttp.OK(w, resp)
}

func (s *Service) postageGetAllStampsHandler(w http.ResponseWriter, _ *http.Request) {
func (s *Service) postageGetAllBatchesHandler(w http.ResponseWriter, _ *http.Request) {
logger := s.logger.WithName("get_batches").Build()

batches := make([]postageBatchResponse, 0)
Expand Down Expand Up @@ -263,7 +274,7 @@ func (s *Service) postageGetStampBucketsHandler(w http.ResponseWriter, r *http.R
case errors.Is(err, postage.ErrNotUsable):
jsonhttp.BadRequest(w, "batch not usable")
case errors.Is(err, postage.ErrNotFound):
jsonhttp.NotFound(w, "cannot get batch")
jsonhttp.NotFound(w, "issuer does not exist")
default:
jsonhttp.InternalServerError(w, "get issuer failed")
}
Expand Down Expand Up @@ -305,13 +316,26 @@ func (s *Service) postageGetStampHandler(w http.ResponseWriter, r *http.Request)
case errors.Is(err, postage.ErrNotUsable):
jsonhttp.BadRequest(w, "batch not usable")
case errors.Is(err, postage.ErrNotFound):
jsonhttp.NotFound(w, "unable to get stamp issuer")
jsonhttp.NotFound(w, "issuer does not exist")
default:
jsonhttp.InternalServerError(w, "get issuer failed")
}
return
}

exists, err := s.batchStore.Exists(paths.BatchID)
if err != nil {
logger.Debug("exist check failed", "batch_id", hexBatchID, "error", err)
logger.Error(nil, "exist check failed")
jsonhttp.InternalServerError(w, "unable to check batch")
return
}
if !exists {
logger.Debug("batch does not exists", "batch_id", hexBatchID)
jsonhttp.NotFound(w, "issuer does not exist")
return
}

batchTTL, err := s.estimateBatchTTLFromID(paths.BatchID)
if err != nil {
logger.Debug("estimate batch expiration failed", "batch_id", hexBatchID, "error", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/postage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func TestPostageGetStamps(t *testing.T) {

jsonhttptest.Request(t, ts, http.MethodGet, "/stamps/"+hex.EncodeToString(eb.ID), http.StatusNotFound,
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{
Message: "unable to get stamp issuer",
Message: "issuer does not exist",
Code: 404,
}),
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ func (s *Service) mountBusinessDebug(restricted bool) {

handle("/batches", web.ChainHandlers(
web.FinalHandler(jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.postageGetAllStampsHandler),
"GET": http.HandlerFunc(s.postageGetAllBatchesHandler),
})),
)

Expand Down
8 changes: 1 addition & 7 deletions pkg/postage/mock/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package mock

import (
"bytes"
"context"
"math/big"
"sync"
Expand Down Expand Up @@ -55,12 +54,7 @@ type mockPostage struct {
func (m *mockPostage) HandleStampExpiry(ctx context.Context, id []byte) error {
m.issuerLock.Lock()
defer m.issuerLock.Unlock()

for k, v := range m.issuersMap {
if bytes.Equal(id, v.ID()) {
delete(m.issuersMap, k)
}
}
delete(m.issuersMap, string(id))
return nil
}

Expand Down
Loading