From 52254705c3e8d4821245a9fcbd25f5df11068a1e Mon Sep 17 00:00:00 2001 From: istae <14264581+istae@users.noreply.github.com> Date: Thu, 2 Nov 2023 15:32:51 +0300 Subject: [PATCH 1/3] fix(postage_api): check if batch exists for stamp issuer --- pkg/api/postage.go | 61 ++++++++++++++++++++++++++----------- pkg/api/postage_test.go | 2 +- pkg/api/router.go | 2 +- pkg/postage/mock/service.go | 8 +---- 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/pkg/api/postage.go b/pkg/api/postage.go index 68eec904130..384d7cb7cad 100644 --- a/pkg/api/postage.go +++ b/pkg/api/postage.go @@ -176,10 +176,26 @@ 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) @@ -187,25 +203,27 @@ func (s *Service) postageGetStampsHandler(w http.ResponseWriter, r *http.Request 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) @@ -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") } @@ -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) @@ -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(), diff --git a/pkg/api/postage_test.go b/pkg/api/postage_test.go index 504a9607c2c..a7055ed098a 100644 --- a/pkg/api/postage_test.go +++ b/pkg/api/postage_test.go @@ -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, }), ) diff --git a/pkg/api/router.go b/pkg/api/router.go index b7f83a6623c..aba674e2379 100644 --- a/pkg/api/router.go +++ b/pkg/api/router.go @@ -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), })), ) diff --git a/pkg/postage/mock/service.go b/pkg/postage/mock/service.go index d2945a48b85..84b2358dd27 100644 --- a/pkg/postage/mock/service.go +++ b/pkg/postage/mock/service.go @@ -5,7 +5,6 @@ package mock import ( - "bytes" "context" "math/big" "sync" @@ -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 } From 8c64604ea4ae5bc81eedb0da7a6a3c2399214a2b Mon Sep 17 00:00:00 2001 From: istae <14264581+istae@users.noreply.github.com> Date: Thu, 2 Nov 2023 15:39:49 +0300 Subject: [PATCH 2/3] fix: asd --- pkg/api/postage.go | 51 +++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/pkg/api/postage.go b/pkg/api/postage.go index 384d7cb7cad..23459cc5a21 100644 --- a/pkg/api/postage.go +++ b/pkg/api/postage.go @@ -176,18 +176,11 @@ 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) @@ -195,6 +188,9 @@ func (s *Service) postageGetStampsHandler(w http.ResponseWriter, r *http.Request jsonhttp.InternalServerError(w, "unable to check batch") return } + if !exists { + continue + } batchTTL, err := s.estimateBatchTTLFromID(v.ID()) if err != nil { @@ -203,21 +199,20 @@ func (s *Service) postageGetStampsHandler(w http.ResponseWriter, r *http.Request jsonhttp.InternalServerError(w, "unable to estimate batch expiration") return } - 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, - }) - } + + 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, + }) } jsonhttp.OK(w, resp) @@ -337,6 +332,12 @@ func (s *Service) postageGetStampHandler(w http.ResponseWriter, r *http.Request) 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) @@ -350,10 +351,10 @@ func (s *Service) postageGetStampHandler(w http.ResponseWriter, r *http.Request) Depth: issuer.Depth(), BucketDepth: issuer.BucketDepth(), ImmutableFlag: issuer.ImmutableFlag(), - Exists: exists, + Exists: true, BatchTTL: batchTTL, Utilization: issuer.Utilization(), - Usable: exists && s.post.IssuerUsable(issuer), + Usable: s.post.IssuerUsable(issuer), Label: issuer.Label(), Amount: bigint.Wrap(issuer.Amount()), BlockNumber: issuer.BlockNumber(), From 4746bf3cc34d35560d92394c581c4675df38f92d Mon Sep 17 00:00:00 2001 From: istae <14264581+istae@users.noreply.github.com> Date: Thu, 2 Nov 2023 15:41:53 +0300 Subject: [PATCH 3/3] fix: logs --- pkg/api/postage.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/api/postage.go b/pkg/api/postage.go index 23459cc5a21..457b0dc0f63 100644 --- a/pkg/api/postage.go +++ b/pkg/api/postage.go @@ -183,8 +183,7 @@ func (s *Service) postageGetStampsHandler(w http.ResponseWriter, r *http.Request 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") + logger.Error(err, "get stamp issuer: check batch failed", "batch_id", hex.EncodeToString(v.ID())) jsonhttp.InternalServerError(w, "unable to check batch") return } @@ -194,8 +193,7 @@ func (s *Service) postageGetStampsHandler(w http.ResponseWriter, r *http.Request 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 }