Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend information returned by GET /stamps #2051

Merged
merged 1 commit into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca
github.com/uber/jaeger-client-go v2.24.0+incompatible
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.3.4
github.com/wealdtech/go-ens/v3 v3.4.4
gitlab.com/nolash/go-mockbytes v0.0.7
go.opencensus.io v0.22.5 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,10 @@ github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljT
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
github.com/vmihailenco/msgpack/v5 v5.3.4 h1:qMKAwOV+meBw2Y8k9cVwAy7qErtYCwBzZ2ellBfvnqc=
github.com/vmihailenco/msgpack/v5 v5.3.4/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/wealdtech/go-ens/v3 v3.4.4 h1:rgfjBqKj7L9ipVJOo/9XTQTKMcVERvxJpQBNUcIOnhs=
github.com/wealdtech/go-ens/v3 v3.4.4/go.mod h1:X1ORiTz78XpHIhDATM1yZR9jxBPnV83mdX5Ty53IRb8=
github.com/wealdtech/go-multicodec v1.2.0 h1:9AHSxcSE9F9r6ZvQLAO0EXCdM08QfYohaXmW3k6sSh4=
Expand Down
12 changes: 12 additions & 0 deletions openapi/SwarmCommon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ components:
type: integer
usable:
type: boolean
label:
type: string
depth:
type: integer
amount:
$ref: "#/components/schemas/BigInt"
bucketDepth:
type: integer
blockNumber:
type: integer
immutableFlag:
type: boolean

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, 1000)))
mp = mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, big.NewInt(3), 11, 10, 1000, true)))
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, 1000)))
mp = mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, big.NewInt(3), 11, 10, 1000, true)))
mockStorer = mock.NewStorer()
client, _, _ = newTestServer(t, testServerOptions{
Storer: mockStorer,
Expand Down
47 changes: 32 additions & 15 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,25 +98,35 @@ 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"`
BatchID batchID `json:"batchID"`
Utilization uint32 `json:"utilization"`
Usable bool `json:"usable"`
Label string `json:"label"`
Depth uint8 `json:"depth"`
Amount *bigint.BigInt `json:"amount"`
BucketDepth uint8 `json:"bucketDepth"`
BlockNumber uint64 `json:"blockNumber"`
ImmutableFlag bool `json:"immutableFlag"`
}

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

func (s *server) postageGetStampsHandler(w http.ResponseWriter, r *http.Request) {
issuers := s.post.StampIssuers()
func (s *server) postageGetStampsHandler(w http.ResponseWriter, _ *http.Request) {
resp := postageStampsResponse{}
for _, v := range issuers {
issuer := postageStampResponse{
BatchID: v.ID(),
Utilization: v.Utilization(),
Usable: s.post.IssuerUsable(v),
}
resp.Stamps = append(resp.Stamps, issuer)
for _, v := range s.post.StampIssuers() {
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(),
})
}
jsonhttp.OK(w, resp)
}
Expand Down Expand Up @@ -143,9 +154,15 @@ func (s *server) postageGetStampHandler(w http.ResponseWriter, r *http.Request)
return
}
resp := postageStampResponse{
BatchID: id,
Utilization: issuer.Utilization(),
Usable: s.post.IssuerUsable(issuer),
BatchID: id,
Utilization: issuer.Utilization(),
Usable: s.post.IssuerUsable(issuer),
Label: issuer.Label(),
Depth: issuer.Depth(),
Amount: bigint.Wrap(issuer.Amount()),
BucketDepth: issuer.BucketDepth(),
BlockNumber: issuer.BlockNumber(),
ImmutableFlag: issuer.ImmutableFlag(),
}
jsonhttp.OK(w, &resp)
}
31 changes: 23 additions & 8 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,32 +193,46 @@ func TestPostageCreateStamp(t *testing.T) {
}

func TestPostageGetStamps(t *testing.T) {
mp := mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, 11, 10, 1000)))
si := postage.NewStampIssuer("", "", batchOk, big.NewInt(3), 11, 10, 1000, true)
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,
Usable: true,
BatchID: batchOk,
Utilization: si.Utilization(),
Usable: true,
Label: si.Label(),
Depth: si.Depth(),
Amount: bigint.Wrap(si.Amount()),
BucketDepth: si.BucketDepth(),
BlockNumber: si.BlockNumber(),
ImmutableFlag: si.ImmutableFlag(),
},
},
}),
)
}

func TestPostageGetStamp(t *testing.T) {
mp := mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, 11, 10, 1000)))
si := postage.NewStampIssuer("", "", batchOk, big.NewInt(3), 11, 10, 1000, true)
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,
Usable: true,
BatchID: batchOk,
Utilization: si.Utilization(),
Usable: true,
Label: si.Label(),
Depth: si.Depth(),
Amount: bigint.Wrap(si.Amount()),
BucketDepth: si.BucketDepth(),
BlockNumber: si.BlockNumber(),
ImmutableFlag: si.ImmutableFlag(),
}),
)
})
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, 1000)))
mp = mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, big.NewInt(3), 11, 10, 1000, true)))
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, 1000)))
mp = mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, big.NewInt(3), 11, 10, 1000, true)))
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, 1000), nil
return postage.NewStampIssuer("test fallback", "test identity", id, big.NewInt(3), 24, 6, 1000, true), nil
}

if m.i != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/postage/postagecontract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ func (c *postageContract) CreateBatch(ctx context.Context, initialBalance *big.I
label,
c.owner.Hex(),
batchID,
depth,
initialBalance,
createdEvent.Depth,
createdEvent.BucketDepth,
ev.BlockNumber,
createdEvent.ImmutableFlag,
))

return createdEvent.BatchId[:], nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/postage/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (ps *service) IssuerUsable(st *StampIssuer) bool {
// 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 cs.Block < st.data.BlockNumber || (cs.Block-st.data.BlockNumber) < blockThreshold {
return false
}
return true
Expand All @@ -105,7 +105,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.data.BatchID) {
if !ps.IssuerUsable(st) {
return nil, ErrNotUsable
}
Expand Down
10 changes: 6 additions & 4 deletions 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 @@ -75,11 +76,12 @@ func TestGetStampIssuer(t *testing.T) {
if i == 0 {
continue
}
if i < 4 {
ps.Add(postage.NewStampIssuer(string(id), "", id, 16, 8, validBlockNumber))
} else {
ps.Add(postage.NewStampIssuer(string(id), "", id, 16, 8, validBlockNumber+uint64(i)))

var shift uint64 = 0
if i > 3 {
shift = uint64(i)
}
ps.Add(postage.NewStampIssuer(string(id), "", id, big.NewInt(3), 16, 8, validBlockNumber+shift, true))
}
t.Run("found", func(t *testing.T) {
for _, id := range ids[1:4] {
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, 1000)
issuer := postage.NewStampIssuer("label", "keyID", b.ID, big.NewInt(3), b.Depth, b.BucketDepth, 1000, true)
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.data.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.data.BatchID, index, ts, sig), nil
}

func timestamp() []byte {
Expand Down
4 changes: 2 additions & 2 deletions 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 @@ -90,8 +91,7 @@ func TestStamperStamping(t *testing.T) {
// tests that Stamps returns with postage.ErrBucketFull iff
// issuer has the corresponding collision bucket filled]
t.Run("bucket full", func(t *testing.T) {
st := newTestStampIssuer(t, 1000)
st = postage.NewStampIssuer("", "", st.ID(), 12, 8, 1000)
st := postage.NewStampIssuer("", "", newTestStampIssuer(t, 1000).ID(), big.NewInt(3), 12, 8, 1000, true)
stamper := postage.NewStamper(st, signer)
// issue 1 stamp
chunkAddr, _ := createStamp(t, stamper)
Expand Down
Loading