Skip to content

Commit

Permalink
feat: extend informations returned by GET /stamps
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Mrekaj committed Jun 14, 2021
1 parent d73dfe3 commit 67c939a
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 100 deletions.
8 changes: 8 additions & 0 deletions openapi/SwarmCommon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,14 @@ components:
$ref: "#/components/schemas/BatchID"
utilization:
type: integer
label:
type: string
depth:
type: integer
amount:
$ref: "#/components/schemas/BigInt"
createdAt:
type: integer

Settlement:
type: object
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"io"
"io/ioutil"
"math/big"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -267,7 +268,7 @@ func TestPostageHeaderError(t *testing.T) {
mockStorer = mock.NewStorer()
mockStatestore = statestore.NewStateStore()
logger = logging.New(ioutil.Discard, 5)
mp = mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, 11, 10)))
mp = mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, big.NewInt(3), 11, 10)))
client, _, _ = newTestServer(t, testServerOptions{
Storer: mockStorer,
Tags: tags.NewTags(mockStatestore, logger),
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"testing"

Expand Down Expand Up @@ -154,7 +155,7 @@ func TestFeed_Post(t *testing.T) {
logger = logging.New(ioutil.Discard, 0)
tag = tags.NewTags(mockStatestore, logger)
topic = "aabbcc"
mp = mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, 11, 10)))
mp = mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, big.NewInt(3), 11, 10)))
mockStorer = mock.NewStorer()
client, _, _ = newTestServer(t, testServerOptions{
Storer: mockStorer,
Expand Down
26 changes: 20 additions & 6 deletions pkg/api/postage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"strconv"

"github.com/ethersphere/bee/pkg/bigint"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/postage/postagecontract"
"github.com/ethersphere/bee/pkg/sctx"
Expand Down Expand Up @@ -97,20 +98,29 @@ func (s *server) postageCreateHandler(w http.ResponseWriter, r *http.Request) {
}

type postageStampResponse struct {
BatchID batchID `json:"batchID"`
Utilization uint32 `json:"utilization"`
BatchID batchID `json:"batchID"`
Utilization uint32 `json:"utilization"`
Label string `json:"label"`
Depth uint8 `json:"depth"`
Amount *bigint.BigInt `json:"amount"`
CreatedAt int64 `json:"createdAt"`
}

type postageStampsResponse struct {
Stamps []postageStampResponse `json:"stamps"`
}

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()}
resp.Stamps = append(resp.Stamps, issuer)
for _, v := range s.post.StampIssuers() {
resp.Stamps = append(resp.Stamps, postageStampResponse{
BatchID: v.ID(),
Utilization: v.Utilization(),
Label: v.Label(),
Depth: v.Depth(),
Amount: bigint.Wrap(v.Amount()),
CreatedAt: v.CreatedAt(),
})
}
jsonhttp.OK(w, resp)
}
Expand Down Expand Up @@ -140,6 +150,10 @@ func (s *server) postageGetStampHandler(w http.ResponseWriter, r *http.Request)
resp := postageStampResponse{
BatchID: id,
Utilization: issuer.Utilization(),
Label: issuer.Label(),
Depth: issuer.Depth(),
Amount: bigint.Wrap(issuer.Amount()),
CreatedAt: issuer.CreatedAt(),
}
jsonhttp.OK(w, &resp)
}
19 changes: 15 additions & 4 deletions pkg/api/postage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"

"github.com/ethersphere/bee/pkg/api"
"github.com/ethersphere/bee/pkg/bigint"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/pkg/postage"
Expand Down Expand Up @@ -192,30 +193,40 @@ func TestPostageCreateStamp(t *testing.T) {
}

func TestPostageGetStamps(t *testing.T) {
mp := mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, 11, 10)))
si := postage.NewStampIssuer("", "", batchOk, big.NewInt(3), 11, 10)
mp := mockpost.New(mockpost.WithIssuer(si))
client, _, _ := newTestServer(t, testServerOptions{Post: mp})

jsonhttptest.Request(t, client, http.MethodGet, "/stamps", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(&api.PostageStampsResponse{
Stamps: []api.PostageStampResponse{
{
BatchID: batchOk,
Utilization: 0,
Utilization: si.Utilization(),
Label: si.Label(),
Depth: si.Depth(),
Amount: bigint.Wrap(si.Amount()),
CreatedAt: si.CreatedAt(),
},
},
}),
)
}

func TestPostageGetStamp(t *testing.T) {
mp := mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, 11, 10)))
si := postage.NewStampIssuer("", "", batchOk, big.NewInt(3), 11, 10)
mp := mockpost.New(mockpost.WithIssuer(si))
client, _, _ := newTestServer(t, testServerOptions{Post: mp})

t.Run("ok", func(t *testing.T) {
jsonhttptest.Request(t, client, http.MethodGet, "/stamps/"+batchOkStr, http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(&api.PostageStampResponse{
BatchID: batchOk,
Utilization: 0,
Utilization: si.Utilization(),
Label: si.Label(),
Depth: si.Depth(),
Amount: bigint.Wrap(si.Amount()),
CreatedAt: si.CreatedAt(),
}),
)
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/pss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/hex"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"net/url"
"sync"
Expand Down Expand Up @@ -185,7 +186,7 @@ func TestPssSend(t *testing.T) {
mtx.Unlock()
return err
}
mp = mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, 11, 10)))
mp = mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, big.NewInt(3), 11, 10)))
p = newMockPss(sendFn)
client, _, _ = newTestServer(t, testServerOptions{
Pss: p,
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/soc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/hex"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"testing"

Expand All @@ -32,7 +33,7 @@ func TestSOC(t *testing.T) {
mockStatestore = statestore.NewStateStore()
logger = logging.New(ioutil.Discard, 0)
tag = tags.NewTags(mockStatestore, logger)
mp = mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, 11, 10)))
mp = mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, big.NewInt(3), 11, 10)))
mockStorer = mock.NewStorer()
client, _, _ = newTestServer(t, testServerOptions{
Storer: mockStorer,
Expand Down
2 changes: 1 addition & 1 deletion pkg/postage/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Batch struct {

// MarshalBinary implements BinaryMarshaller. It will attempt to serialize the
// postage batch to a byte slice.
// serialised as ID(32)|big endian value(32)|start block(8)|owner addr(20)|bucketDepth(1)|depth(1)|immutable(1)
// serialised as ID(32)|big endian value(32)|start block(8)|owner addr(20)|BucketDepth(1)|depth(1)|immutable(1)
func (b *Batch) MarshalBinary() ([]byte, error) {
out := make([]byte, 95)
copy(out, b.ID)
Expand Down
3 changes: 2 additions & 1 deletion pkg/postage/mock/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package mock

import (
"errors"
"math/big"

"github.com/ethersphere/bee/pkg/postage"
)
Expand Down Expand Up @@ -54,7 +55,7 @@ func (m *mockPostage) StampIssuers() []*postage.StampIssuer {

func (m *mockPostage) GetStampIssuer(id []byte) (*postage.StampIssuer, error) {
if m.acceptAll {
return postage.NewStampIssuer("test fallback", "test identity", id, 24, 6), nil
return postage.NewStampIssuer("test fallback", "test identity", id, big.NewInt(3), 24, 6), nil
}

if m.i != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/postage/postagecontract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (c *postageContract) CreateBatch(ctx context.Context, initialBalance *big.I
label,
c.owner.Hex(),
batchID,
initialBalance,
depth,
createdEvent.BucketDepth,
))
Expand Down
2 changes: 1 addition & 1 deletion pkg/postage/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ 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) {
if bytes.Equal(batchID, st.BatchID) {
return st, nil
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/postage/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package postage_test
import (
crand "crypto/rand"
"io"
"math/big"
"reflect"
"testing"

Expand Down Expand Up @@ -66,7 +67,7 @@ func TestGetStampIssuer(t *testing.T) {
if i == 0 {
continue
}
ps.Add(postage.NewStampIssuer(string(id), "", id, 16, 8))
ps.Add(postage.NewStampIssuer(string(id), "", id, big.NewInt(3), 16, 8))
}
t.Run("found", func(t *testing.T) {
for _, id := range ids[1:] {
Expand Down
3 changes: 2 additions & 1 deletion pkg/postage/stamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package postage_test

import (
"bytes"
"math/big"
"testing"

"github.com/ethersphere/bee/pkg/crypto"
Expand Down Expand Up @@ -74,7 +75,7 @@ func TestValidStamp(t *testing.T) {
b := postagetesting.MustNewBatch(postagetesting.WithOwner(owner))
bs := mock.New(mock.WithBatch(b))
signer := crypto.NewDefaultSigner(privKey)
issuer := postage.NewStampIssuer("label", "keyID", b.ID, b.Depth, b.BucketDepth)
issuer := postage.NewStampIssuer("label", "keyID", b.ID, big.NewInt(3), b.Depth, b.BucketDepth)
stamper := postage.NewStamper(issuer, signer)

// this creates a chunk with a mocked stamp. ValidStamp will override this
Expand Down
4 changes: 2 additions & 2 deletions pkg/postage/stamper.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ func (st *stamper) Stamp(addr swarm.Address) (*Stamp, error) {
return nil, err
}
ts := timestamp()
toSign, err := toSignDigest(addr.Bytes(), st.issuer.batchID, index, ts)
toSign, err := toSignDigest(addr.Bytes(), st.issuer.BatchID, index, ts)
if err != nil {
return nil, err
}
sig, err := st.signer.Sign(toSign)
if err != nil {
return nil, err
}
return NewStamp(st.issuer.batchID, index, ts, sig), nil
return NewStamp(st.issuer.BatchID, index, ts, sig), nil
}

func timestamp() []byte {
Expand Down
3 changes: 2 additions & 1 deletion pkg/postage/stamper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
crand "crypto/rand"
"errors"
"io"
"math/big"
"testing"

"github.com/ethersphere/bee/pkg/crypto"
Expand Down Expand Up @@ -91,7 +92,7 @@ func TestStamperStamping(t *testing.T) {
// issuer has the corresponding collision bucket filled]
t.Run("bucket full", func(t *testing.T) {
st := newTestStampIssuer(t)
st = postage.NewStampIssuer("", "", st.ID(), 12, 8)
st = postage.NewStampIssuer("", "", st.ID(), big.NewInt(3), 12, 8)
stamper := postage.NewStamper(st, signer)
// issue 1 stamp
chunkAddr, _ := createStamp(t, stamper)
Expand Down
Loading

0 comments on commit 67c939a

Please sign in to comment.