Skip to content

Commit

Permalink
fix: adding usability as function for api
Browse files Browse the repository at this point in the history
  • Loading branch information
aloknerurkar committed Jun 11, 2021
1 parent c50e0e3 commit 300d02d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
8 changes: 7 additions & 1 deletion pkg/api/postage.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (s *server) postageCreateHandler(w http.ResponseWriter, r *http.Request) {
type postageStampResponse struct {
BatchID batchID `json:"batchID"`
Utilization uint32 `json:"utilization"`
Usable bool `json:"usable"`
}

type postageStampsResponse struct {
Expand All @@ -109,7 +110,11 @@ func (s *server) postageGetStampsHandler(w http.ResponseWriter, r *http.Request)
issuers := s.post.StampIssuers()
resp := postageStampsResponse{}
for _, v := range issuers {
issuer := postageStampResponse{BatchID: v.ID(), Utilization: v.Utilization()}
issuer := postageStampResponse{
BatchID: v.ID(),
Utilization: v.Utilization(),
Usable: s.post.IssuerUsable(v),
}
resp.Stamps = append(resp.Stamps, issuer)
}
jsonhttp.OK(w, resp)
Expand Down Expand Up @@ -140,6 +145,7 @@ func (s *server) postageGetStampHandler(w http.ResponseWriter, r *http.Request)
resp := postageStampResponse{
BatchID: id,
Utilization: issuer.Utilization(),
Usable: s.post.IssuerUsable(issuer),
}
jsonhttp.OK(w, &resp)
}
2 changes: 2 additions & 0 deletions pkg/api/postage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func TestPostageGetStamps(t *testing.T) {
{
BatchID: batchOk,
Utilization: 0,
Usable: true,
},
},
}),
Expand All @@ -216,6 +217,7 @@ func TestPostageGetStamp(t *testing.T) {
jsonhttptest.WithExpectedJSONResponse(&api.PostageStampResponse{
BatchID: batchOk,
Utilization: 0,
Usable: true,
}),
)
})
Expand Down
4 changes: 4 additions & 0 deletions pkg/postage/mock/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func (m *mockPostage) GetStampIssuer(id []byte) (*postage.StampIssuer, error) {
return nil, errors.New("stampissuer not found")
}

func (m *mockPostage) IssuerUsable(_ *postage.StampIssuer) bool {
return true
}

func (m *mockPostage) Close() error {
return nil
}
22 changes: 15 additions & 7 deletions pkg/postage/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Service interface {
Add(*StampIssuer)
StampIssuers() []*StampIssuer
GetStampIssuer([]byte) (*StampIssuer, error)
IssuerUsable(*StampIssuer) bool
io.Closer
}

Expand Down Expand Up @@ -86,19 +87,26 @@ func (ps *service) StampIssuers() []*StampIssuer {
return ps.issuers
}

// GetStampIssuer finds a stamp issuer by batch ID.
func (ps *service) GetStampIssuer(batchID []byte) (*StampIssuer, error) {
func (ps *service) IssuerUsable(st *StampIssuer) bool {
cs := ps.postageStore.GetChainState()

// this checks atleast threshold blocks are seen on the blockchain after
// the batch creation, before we start using a stamp issuer. The threshold
// is meant to allow enough time for upstream peers to see the batch and
// hence validate the stamps issued
if cs.Block < st.blockNumber || (cs.Block-st.blockNumber) < blockThreshold {
return false
}
return true
}

// GetStampIssuer finds a stamp issuer by batch ID.
func (ps *service) GetStampIssuer(batchID []byte) (*StampIssuer, error) {
ps.lock.Lock()
defer ps.lock.Unlock()
for _, st := range ps.issuers {
if bytes.Equal(batchID, st.batchID) {
// this checks atleast threshold blocks are seen on the blockchain after
// the batch creation, before we start using a stamp issuer. The threshold
// is meant to allow enough time for upstream peers to see the batch and
// hence validate the stamps issued
if cs.Block < st.blockNumber || (cs.Block-st.blockNumber) < blockThreshold {
if !ps.IssuerUsable(st) {
return nil, ErrNotUsable
}
return st, nil
Expand Down

0 comments on commit 300d02d

Please sign in to comment.