Skip to content

Commit

Permalink
fix: remove stamp data on expiration, not on node start up and API ch…
Browse files Browse the repository at this point in the history
…anges (#4439)
  • Loading branch information
istae authored Nov 1, 2023
1 parent f32ff23 commit 9afd792
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 318 deletions.
14 changes: 7 additions & 7 deletions openapi/Swarm.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.3

info:
version: 5.0.0
version: 5.1.0
title: Bee API
description: "A list of the currently provided Interfaces to interact with the swarm, implementing file operations and sending messages"

Expand Down Expand Up @@ -1651,12 +1651,6 @@ paths:
description: Default response

"/stamps":
parameters:
- in: query
name: all
schema:
$ref: "SwarmCommon.yaml#/components/schemas/isAll"
description: Gets all stamps. Default is null.
get:
summary: Get stamps for this node
description: This endpoint is available on the main API only if the node is spawned with the `--restricted` flag along with a bearer authentication token.
Expand All @@ -1671,6 +1665,8 @@ paths:
application/json:
schema:
$ref: "SwarmCommon.yaml#/components/schemas/DebugPostageBatchesResponse"
"404":
$ref: "SwarmCommon.yaml#/components/responses/404"

default:
description: Default response
Expand All @@ -1697,6 +1693,8 @@ paths:
application/json:
schema:
$ref: "SwarmCommon.yaml#/components/schemas/DebugPostageBatch"
"404":
$ref: "SwarmCommon.yaml#/components/responses/404"
"400":
$ref: "SwarmCommon.yaml#/components/responses/400"
default:
Expand Down Expand Up @@ -1724,6 +1722,8 @@ paths:
application/json:
schema:
$ref: "SwarmCommon.yaml#/components/schemas/PostageStampBuckets"
"404":
$ref: "SwarmCommon.yaml#/components/responses/404"
"400":
$ref: "SwarmCommon.yaml#/components/responses/400"
default:
Expand Down
4 changes: 1 addition & 3 deletions openapi/SwarmCommon.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
openapi: 3.0.3
info:
version: 3.2.5
version: 3.2.6
title: Common Data Types
description: |
\*****bzzz*****
Expand Down Expand Up @@ -470,8 +470,6 @@ components:
type: boolean
batchTTL:
type: integer
expired:
type: boolean

PostageBatchNoIssuer:
type: object
Expand Down
15 changes: 7 additions & 8 deletions openapi/SwarmDebug.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
openapi: 3.0.3
info:
version: 4.0.2
version: 4.1.0
title: Bee Debug API
description: "A list of the currently provided debug interfaces to interact with the bee node"

Expand Down Expand Up @@ -783,13 +783,6 @@ paths:
description: Default response

"/stamps":
parameters:
- in: query
name: all
schema:
type: string
required: false
description: Gets all stamps. Default is null.
get:
summary: Get stamps for this node
tags:
Expand All @@ -801,6 +794,8 @@ paths:
application/json:
schema:
$ref: "SwarmCommon.yaml#/components/schemas/DebugPostageBatchesResponse"
"404":
$ref: "SwarmCommon.yaml#/components/responses/404"

default:
description: Default response
Expand All @@ -824,6 +819,8 @@ paths:
application/json:
schema:
$ref: "SwarmCommon.yaml#/components/schemas/DebugPostageBatch"
"404":
$ref: "SwarmCommon.yaml#/components/responses/404"
"400":
$ref: "SwarmCommon.yaml#/components/responses/400"
default:
Expand All @@ -848,6 +845,8 @@ paths:
application/json:
schema:
$ref: "SwarmCommon.yaml#/components/schemas/PostageStampBuckets"
"404":
$ref: "SwarmCommon.yaml#/components/responses/404"
"400":
$ref: "SwarmCommon.yaml#/components/responses/400"
default:
Expand Down
81 changes: 26 additions & 55 deletions pkg/api/postage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package api

import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
Expand Down Expand Up @@ -145,7 +144,6 @@ type postageStampResponse struct {
ImmutableFlag bool `json:"immutableFlag"`
Exists bool `json:"exists"`
BatchTTL int64 `json:"batchTTL"`
Expired bool `json:"expired"`
}

type postageStampsResponse struct {
Expand Down Expand Up @@ -178,49 +176,30 @@ 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
}
if queries.All || 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,
Expired: v.Expired(),
})
}
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)
Expand Down Expand Up @@ -318,28 +297,21 @@ func (s *Service) postageGetStampHandler(w http.ResponseWriter, r *http.Request)
}
hexBatchID := hex.EncodeToString(paths.BatchID)

var issuer *postage.StampIssuer
stampIssuers := s.post.StampIssuers()
for _, stampIssuer := range stampIssuers {
if bytes.Equal(paths.BatchID, stampIssuer.ID()) {
issuer = stampIssuer
break
issuer, _, err := s.post.GetStampIssuer(paths.BatchID)
if err != nil {
logger.Debug("get stamp issuer: get issuer failed", "batch_id", hexBatchID, "error", err)
logger.Error(nil, "get stamp issuer: get issuer failed")
switch {
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")
default:
jsonhttp.InternalServerError(w, "get issuer failed")
}
}
if issuer == nil {
logger.Debug("get issuer failed", "batch_id", hexBatchID)
logger.Error(nil, "get issuer failed")
jsonhttp.BadRequest(w, "cannot find batch")
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 @@ -353,14 +325,13 @@ 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(),
Expired: issuer.Expired(),
})
}

Expand Down
Loading

0 comments on commit 9afd792

Please sign in to comment.