Commit b90e92cd authored by mrekucci's avatar mrekucci Committed by GitHub

feat: add expired field to the postage stamp response (#2328)

parent 4909a7f5
......@@ -333,6 +333,8 @@ components:
type: integer
immutableFlag:
type: boolean
exists:
type: boolean
Settlement:
type: object
......
......@@ -101,6 +101,7 @@ type postageStampResponse struct {
BucketDepth uint8 `json:"bucketDepth"`
BlockNumber uint64 `json:"blockNumber"`
ImmutableFlag bool `json:"immutableFlag"`
Exists bool `json:"exists"`
}
type postageStampsResponse struct {
......@@ -110,6 +111,13 @@ type postageStampsResponse struct {
func (s *Service) postageGetStampsHandler(w http.ResponseWriter, _ *http.Request) {
resp := postageStampsResponse{}
for _, v := range s.post.StampIssuers() {
exists, err := s.post.BatchExists(v.ID())
if err != nil {
s.logger.Errorf("get stamp issuer: check batch: %v", err)
s.logger.Error("get stamp issuer: check batch")
jsonhttp.InternalServerError(w, "unable to check batch")
return
}
resp.Stamps = append(resp.Stamps, postageStampResponse{
BatchID: v.ID(),
Utilization: v.Utilization(),
......@@ -120,6 +128,7 @@ func (s *Service) postageGetStampsHandler(w http.ResponseWriter, _ *http.Request
BucketDepth: v.BucketDepth(),
BlockNumber: v.BlockNumber(),
ImmutableFlag: v.ImmutableFlag(),
Exists: exists,
})
}
jsonhttp.OK(w, resp)
......@@ -127,14 +136,14 @@ func (s *Service) postageGetStampsHandler(w http.ResponseWriter, _ *http.Request
func (s *Service) postageGetStampHandler(w http.ResponseWriter, r *http.Request) {
idStr := mux.Vars(r)["id"]
if idStr == "" || len(idStr) != 64 {
if len(idStr) != 64 {
s.logger.Error("get stamp issuer: invalid batchID")
jsonhttp.BadRequest(w, "invalid batchID")
return
}
id, err := hex.DecodeString(idStr)
if err != nil {
s.logger.Error("get stamp issuer: invalid batchID: %v", err)
s.logger.Errorf("get stamp issuer: invalid batchID: %v", err)
s.logger.Error("get stamp issuer: invalid batchID")
jsonhttp.BadRequest(w, "invalid batchID")
return
......@@ -142,11 +151,18 @@ func (s *Service) postageGetStampHandler(w http.ResponseWriter, r *http.Request)
issuer, err := s.post.GetStampIssuer(id)
if err != nil {
s.logger.Error("get stamp issuer: get issuer: %v", err)
s.logger.Errorf("get stamp issuer: get issuer: %v", err)
s.logger.Error("get stamp issuer: get issuer")
jsonhttp.BadRequest(w, "cannot get issuer")
return
}
exists, err := s.post.BatchExists(id)
if err != nil {
s.logger.Errorf("get stamp issuer: check batch: %v", err)
s.logger.Error("get stamp issuer: check batch")
jsonhttp.InternalServerError(w, "unable to check batch")
return
}
resp := postageStampResponse{
BatchID: id,
Utilization: issuer.Utilization(),
......@@ -157,6 +173,7 @@ func (s *Service) postageGetStampHandler(w http.ResponseWriter, r *http.Request)
BucketDepth: issuer.BucketDepth(),
BlockNumber: issuer.BlockNumber(),
ImmutableFlag: issuer.ImmutableFlag(),
Exists: exists,
}
jsonhttp.OK(w, &resp)
}
......
......@@ -211,6 +211,7 @@ func TestPostageGetStamps(t *testing.T) {
BucketDepth: si.BucketDepth(),
BlockNumber: si.BlockNumber(),
ImmutableFlag: si.ImmutableFlag(),
Exists: true,
},
},
}),
......@@ -234,6 +235,7 @@ func TestPostageGetStamp(t *testing.T) {
BucketDepth: si.BucketDepth(),
BlockNumber: si.BlockNumber(),
ImmutableFlag: si.ImmutableFlag(),
Exists: true,
}),
)
})
......
......@@ -92,7 +92,11 @@ func (bs *BatchStore) Get(id []byte) (*postage.Batch, error) {
}
bs.getErrDelayCnt--
}
if !bytes.Equal(bs.id, id) {
exists, err := bs.Exists(id)
if err != nil {
return nil, err
}
if !exists {
return nil, errors.New("no such id")
}
return bs.batch, nil
......@@ -147,6 +151,11 @@ func (bs *BatchStore) SetRadiusSetter(r postage.RadiusSetter) {
panic("not implemented")
}
// Exists reports whether batch referenced by the give id exists.
func (bs *BatchStore) Exists(id []byte) (bool, error) {
return bytes.Equal(bs.id, id), nil
}
func (bs *BatchStore) Reset() error {
bs.resetCallCount++
return nil
......
......@@ -198,6 +198,18 @@ func (s *store) SetRadiusSetter(r postage.RadiusSetter) {
s.radiusSetter = r
}
// Exists reports whether batch referenced by the give id exists.
func (s *store) Exists(id []byte) (bool, error) {
switch err := s.store.Get(batchKey(id), new(postage.Batch)); {
case err == nil:
return true, nil
case errors.Is(err, storage.ErrNotFound):
return false, nil
default:
return false, err
}
}
func (s *store) Reset() error {
prefix := "batchstore_"
if err := s.store.Iterate(prefix, func(k, _ []byte) (bool, error) {
......
......@@ -30,11 +30,12 @@ type UnreserveIteratorFn func(id []byte, radius uint8) (bool, error)
type Storer interface {
Get(id []byte) (*Batch, error)
Put(*Batch, *big.Int, uint8) error
PutChainState(*ChainState) error
GetChainState() *ChainState
PutChainState(*ChainState) error
GetReserveState() *ReserveState
SetRadiusSetter(RadiusSetter)
Unreserve(UnreserveIteratorFn) error
Exists(id []byte) (bool, error)
Reset() error
}
......
......@@ -69,6 +69,11 @@ func (m *mockPostage) IssuerUsable(_ *postage.StampIssuer) bool {
return true
}
// BatchExists returns always true.
func (m *mockPostage) BatchExists(_ []byte) (bool, error) {
return true, nil
}
func (m *mockPostage) Handle(_ *postage.Batch) {}
func (m *mockPostage) Close() error {
......
......@@ -34,6 +34,7 @@ type Service interface {
StampIssuers() []*StampIssuer
GetStampIssuer([]byte) (*StampIssuer, error)
IssuerUsable(*StampIssuer) bool
BatchExists([]byte) (bool, error)
BatchCreationListener
io.Closer
}
......@@ -123,6 +124,11 @@ func (ps *service) IssuerUsable(st *StampIssuer) bool {
return true
}
// BatchExists returns true if the batch referenced by the given id exists.
func (ps *service) BatchExists(id []byte) (bool, error) {
return ps.postageStore.Exists(id)
}
// GetStampIssuer finds a stamp issuer by batch ID.
func (ps *service) GetStampIssuer(batchID []byte) (*StampIssuer, error) {
ps.lock.Lock()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment