Skip to content

Commit

Permalink
fix(postage_api): check if batch exists for stamp issuer
Browse files Browse the repository at this point in the history
  • Loading branch information
istae committed Nov 2, 2023
1 parent 9afd792 commit 5225470
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
61 changes: 43 additions & 18 deletions pkg/api/postage.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,36 +176,54 @@ type bucketData struct {
func (s *Service) postageGetStampsHandler(w http.ResponseWriter, r *http.Request) {
logger := s.logger.WithName("get_stamps").Build()

queries := struct {
All bool `map:"all"`
}{}
if response := s.mapStructure(r.URL.Query(), &queries); response != nil {
response("invalid query params", logger, w)
return
}

resp := postageStampsResponse{}
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.Debug("get stamp issuer: check batch failed", "batch_id", hex.EncodeToString(v.ID()), "error", err)
logger.Error(nil, "get stamp issuer: check batch failed")
jsonhttp.InternalServerError(w, "unable to check batch")
return
}

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")
jsonhttp.InternalServerError(w, "unable to estimate batch expiration")
return
}
resp.Stamps = append(resp.Stamps, postageStampResponse{
BatchID: v.ID(),
Utilization: v.Utilization(),
Usable: s.post.IssuerUsable(v),
Label: v.Label(),
Depth: v.Depth(),
Amount: bigint.Wrap(v.Amount()),
BucketDepth: v.BucketDepth(),
BlockNumber: v.BlockNumber(),
ImmutableFlag: v.ImmutableFlag(),
Exists: true,
BatchTTL: batchTTL,
})
if exists {
resp.Stamps = append(resp.Stamps, postageStampResponse{
BatchID: v.ID(),
Utilization: v.Utilization(),
Usable: exists && s.post.IssuerUsable(v),
Label: v.Label(),
Depth: v.Depth(),
Amount: bigint.Wrap(v.Amount()),
BucketDepth: v.BucketDepth(),
BlockNumber: v.BlockNumber(),
ImmutableFlag: v.ImmutableFlag(),
Exists: exists,
BatchTTL: batchTTL,
})
}
}

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 +281,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 +323,20 @@ 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
}
batchTTL, err := s.estimateBatchTTLFromID(paths.BatchID)
if err != nil {
logger.Debug("estimate batch expiration failed", "batch_id", hexBatchID, "error", err)
Expand All @@ -325,10 +350,10 @@ func (s *Service) postageGetStampHandler(w http.ResponseWriter, r *http.Request)
Depth: issuer.Depth(),
BucketDepth: issuer.BucketDepth(),
ImmutableFlag: issuer.ImmutableFlag(),
Exists: true,
Exists: exists,
BatchTTL: batchTTL,
Utilization: issuer.Utilization(),
Usable: s.post.IssuerUsable(issuer),
Usable: exists && s.post.IssuerUsable(issuer),
Label: issuer.Label(),
Amount: bigint.Wrap(issuer.Amount()),
BlockNumber: issuer.BlockNumber(),
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

0 comments on commit 5225470

Please sign in to comment.