From 09dad20754f72a26d0bcb9b394f6ccce3ce373f3 Mon Sep 17 00:00:00 2001 From: acud <12988138+acud@users.noreply.github.com> Date: Mon, 26 Apr 2021 21:21:09 +0200 Subject: [PATCH] storage-incentives: cleanups (#1602) * deprecate empty batches fallback * adjust bucketDepth and default radius * fix long standing rebase error in node.go some renaming improvments too --- pkg/api/api.go | 3 +- pkg/api/api_test.go | 24 +++++++++--- pkg/api/bytes_test.go | 4 ++ pkg/api/bzz_test.go | 13 +++++++ pkg/api/chunk_test.go | 7 ++++ pkg/api/dirs_test.go | 7 ++++ pkg/api/feed_test.go | 9 ++++- pkg/api/gatewaymode_test.go | 5 +++ pkg/api/pin_test.go | 6 +++ pkg/api/postage_test.go | 19 +++++----- pkg/api/pss_test.go | 12 ++++-- pkg/api/soc_test.go | 13 +++++-- pkg/api/tag_test.go | 9 +++++ pkg/node/node.go | 34 +++++++---------- pkg/postage/batchstore/reserve.go | 2 +- pkg/postage/batchstore/reserve_test.go | 39 +++++++++++-------- pkg/postage/batchstore/store.go | 4 +- pkg/postage/batchstore/store_test.go | 2 +- pkg/postage/listener/export_test.go | 4 ++ pkg/postage/listener/listener.go | 22 +++++++---- pkg/postage/listener/listener_test.go | 4 ++ pkg/postage/mock/service.go | 40 ++++++++++++-------- pkg/postage/postagecontract/contract.go | 7 ++-- pkg/postage/postagecontract/contract_test.go | 4 ++ 24 files changed, 203 insertions(+), 90 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index 0742fa1603c..3cfd32bf0aa 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -235,8 +235,7 @@ func requestPostageBatchId(r *http.Request) ([]byte, error) { return b, nil } - // fallback to a slice of 32 zeros - return make([]byte, 32), nil + return nil, errInvalidPostageBatch } func (s *server) newTracingHandler(spanName string) func(h http.Handler) http.Handler { diff --git a/pkg/api/api_test.go b/pkg/api/api_test.go index 3be62c9912e..6db478c52d4 100644 --- a/pkg/api/api_test.go +++ b/pkg/api/api_test.go @@ -6,6 +6,7 @@ package api_test import ( "bytes" + "crypto/rand" "encoding/hex" "errors" "io" @@ -22,6 +23,7 @@ import ( "github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" "github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/pinning" + "github.com/ethersphere/bee/pkg/postage" mockpost "github.com/ethersphere/bee/pkg/postage/mock" "github.com/ethersphere/bee/pkg/postage/postagecontract" "github.com/ethersphere/bee/pkg/pss" @@ -39,10 +41,17 @@ import ( var ( batchInvalid = []byte{0} - batchOk = []byte{31: 0} // 32 zeros + batchOk = make([]byte, 32) + batchOkStr string batchEmpty = []byte{} ) +func init() { + _, _ = rand.Read(batchOk) + + batchOkStr = hex.EncodeToString(batchOk) +} + type testServerOptions struct { Storer storage.Storer Resolver resolver.Interface @@ -58,13 +67,13 @@ type testServerOptions struct { Feeds feeds.Factory CORSAllowedOrigins []string PostageContract postagecontract.Interface + Post postage.Service } func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket.Conn, string) { t.Helper() pk, _ := crypto.GenerateSecp256k1Key() signer := crypto.NewDefaultSigner(pk) - mockPostage := mockpost.New() if o.Logger == nil { o.Logger = logging.New(ioutil.Discard, 0) @@ -75,7 +84,10 @@ func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket. if o.WsPingPeriod == 0 { o.WsPingPeriod = 60 * time.Second } - s := api.New(o.Tags, o.Storer, o.Resolver, o.Pss, o.Traversal, o.Pinning, o.Feeds, mockPostage, o.PostageContract, signer, o.Logger, nil, api.Options{ + if o.Post == nil { + o.Post = mockpost.New() + } + s := api.New(o.Tags, o.Storer, o.Resolver, o.Pss, o.Traversal, o.Pinning, o.Feeds, o.Post, o.PostageContract, signer, o.Logger, nil, api.Options{ CORSAllowedOrigins: o.CORSAllowedOrigins, GatewayMode: o.GatewayMode, WsPingPeriod: o.WsPingPeriod, @@ -253,10 +265,12 @@ 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))) client, _, _ = newTestServer(t, testServerOptions{ Storer: mockStorer, Tags: tags.NewTags(mockStatestore, logger), Logger: logger, + Post: mp, }) endpoints = []string{ @@ -267,14 +281,14 @@ func TestPostageHeaderError(t *testing.T) { for _, endpoint := range endpoints { t.Run(endpoint+": empty batch", func(t *testing.T) { hexbatch := hex.EncodeToString(batchEmpty) - expCode := http.StatusOK + expCode := http.StatusBadRequest jsonhttptest.Request(t, client, http.MethodPost, "/"+endpoint, expCode, jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, hexbatch), jsonhttptest.WithRequestHeader(api.ContentTypeHeader, "application/octet-stream"), jsonhttptest.WithRequestBody(bytes.NewReader(content)), ) }) - t.Run(endpoint+": all zeros - ok", func(t *testing.T) { + t.Run(endpoint+": ok batch", func(t *testing.T) { hexbatch := hex.EncodeToString(batchOk) expCode := http.StatusOK jsonhttptest.Request(t, client, http.MethodPost, "/"+endpoint, expCode, diff --git a/pkg/api/bytes_test.go b/pkg/api/bytes_test.go index 00abd457648..f828b7cf366 100644 --- a/pkg/api/bytes_test.go +++ b/pkg/api/bytes_test.go @@ -16,6 +16,7 @@ import ( "github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" "github.com/ethersphere/bee/pkg/logging" pinning "github.com/ethersphere/bee/pkg/pinning/mock" + mockpost "github.com/ethersphere/bee/pkg/postage/mock" statestore "github.com/ethersphere/bee/pkg/statestore/mock" "github.com/ethersphere/bee/pkg/storage/mock" "github.com/ethersphere/bee/pkg/swarm" @@ -41,6 +42,7 @@ func TestBytes(t *testing.T) { Tags: tags.NewTags(statestore.NewStateStore(), logging.New(ioutil.Discard, 0)), Pinning: pinningMock, Logger: logger, + Post: mockpost.New(mockpost.WithAcceptAll()), }) ) @@ -53,6 +55,7 @@ func TestBytes(t *testing.T) { t.Run("upload", func(t *testing.T) { chunkAddr := swarm.MustParseHexAddress(expHash) jsonhttptest.Request(t, client, http.MethodPost, resource, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(content)), jsonhttptest.WithExpectedJSONResponse(api.BytesPostResponse{ Reference: chunkAddr, @@ -75,6 +78,7 @@ func TestBytes(t *testing.T) { t.Run("upload-with-pinning", func(t *testing.T) { var res api.BytesPostResponse jsonhttptest.Request(t, client, http.MethodPost, resource, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(content)), jsonhttptest.WithRequestHeader(api.SwarmPinHeader, "true"), jsonhttptest.WithUnmarshalJSONResponse(&res), diff --git a/pkg/api/bzz_test.go b/pkg/api/bzz_test.go index 8a9bfc76bcd..0744aebb675 100644 --- a/pkg/api/bzz_test.go +++ b/pkg/api/bzz_test.go @@ -24,6 +24,7 @@ import ( "github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/manifest" pinning "github.com/ethersphere/bee/pkg/pinning/mock" + mockpost "github.com/ethersphere/bee/pkg/postage/mock" statestore "github.com/ethersphere/bee/pkg/statestore/mock" "github.com/ethersphere/bee/pkg/storage" smock "github.com/ethersphere/bee/pkg/storage/mock" @@ -46,12 +47,14 @@ func TestBzzFiles(t *testing.T) { Pinning: pinningMock, Tags: tags.NewTags(statestoreMock, logger), Logger: logger, + Post: mockpost.New(mockpost.WithAcceptAll()), }) ) t.Run("invalid-content-type", func(t *testing.T) { jsonhttptest.Request(t, client, http.MethodPost, fileUploadResource, http.StatusBadRequest, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(simpleData)), jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ Message: api.InvalidContentType.Error(), @@ -89,6 +92,7 @@ func TestBzzFiles(t *testing.T) { }) address := swarm.MustParseHexAddress("f30c0aa7e9e2a0ef4c9b1b750ebfeaeb7c7c24da700bb089da19a46e3677824b") jsonhttptest.Request(t, client, http.MethodPost, fileUploadResource, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(tr), jsonhttptest.WithRequestHeader("Content-Type", api.ContentTypeTar), jsonhttptest.WithExpectedJSONResponse(api.BzzUploadResponse{ @@ -138,6 +142,7 @@ func TestBzzFiles(t *testing.T) { }) address := swarm.MustParseHexAddress("f30c0aa7e9e2a0ef4c9b1b750ebfeaeb7c7c24da700bb089da19a46e3677824b") jsonhttptest.Request(t, client, http.MethodPost, fileUploadResource, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestHeader(api.SwarmPinHeader, "true"), jsonhttptest.WithRequestBody(tr), jsonhttptest.WithRequestHeader("Content-Type", api.ContentTypeTar), @@ -172,6 +177,7 @@ func TestBzzFiles(t *testing.T) { var resp api.BzzUploadResponse jsonhttptest.Request(t, client, http.MethodPost, fileUploadResource+"?name="+fileName, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(simpleData)), jsonhttptest.WithRequestHeader(api.SwarmEncryptHeader, "True"), jsonhttptest.WithRequestHeader("Content-Type", "image/jpeg; charset=utf-8"), @@ -202,6 +208,7 @@ func TestBzzFiles(t *testing.T) { jsonhttptest.Request(t, client, http.MethodPost, fileUploadResource+"?name="+fileName, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(simpleData)), jsonhttptest.WithExpectedJSONResponse(api.BzzUploadResponse{ Reference: swarm.MustParseHexAddress(rootHash), @@ -242,6 +249,7 @@ func TestBzzFiles(t *testing.T) { rcvdHeader := jsonhttptest.Request(t, client, http.MethodPost, fileUploadResource+"?name="+fileName, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(strings.NewReader(sampleHtml)), jsonhttptest.WithExpectedJSONResponse(api.BzzUploadResponse{ Reference: swarm.MustParseHexAddress(rootHash), @@ -280,6 +288,7 @@ func TestBzzFiles(t *testing.T) { jsonhttptest.Request(t, client, http.MethodPost, fileUploadResource+"?name="+fileName, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(simpleData)), jsonhttptest.WithExpectedJSONResponse(api.BzzUploadResponse{ Reference: swarm.MustParseHexAddress(rootHash), @@ -392,11 +401,13 @@ func TestBzzFilesRangeRequests(t *testing.T) { Storer: smock.NewStorer(), Tags: tags.NewTags(mockStatestore, logger), Logger: logger, + Post: mockpost.New(mockpost.WithAcceptAll()), }) var resp api.BzzUploadResponse testOpts := []jsonhttptest.Option{ + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(upload.reader), jsonhttptest.WithRequestHeader("Content-Type", upload.contentType), jsonhttptest.WithUnmarshalJSONResponse(&resp), @@ -505,6 +516,7 @@ func TestFeedIndirection(t *testing.T) { Storer: storer, Tags: tags.NewTags(mockStatestore, logger), Logger: logger, + Post: mockpost.New(mockpost.WithAcceptAll()), }) ) // tar all the test case files @@ -520,6 +532,7 @@ func TestFeedIndirection(t *testing.T) { var resp api.BzzUploadResponse options := []jsonhttptest.Option{ + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(tarReader), jsonhttptest.WithRequestHeader("Content-Type", api.ContentTypeTar), jsonhttptest.WithRequestHeader(api.SwarmCollectionHeader, "True"), diff --git a/pkg/api/chunk_test.go b/pkg/api/chunk_test.go index 1e3bcb4d5f1..a002c248fad 100644 --- a/pkg/api/chunk_test.go +++ b/pkg/api/chunk_test.go @@ -13,6 +13,7 @@ import ( "github.com/ethersphere/bee/pkg/logging" pinning "github.com/ethersphere/bee/pkg/pinning/mock" + mockpost "github.com/ethersphere/bee/pkg/postage/mock" statestore "github.com/ethersphere/bee/pkg/statestore/mock" "github.com/ethersphere/bee/pkg/tags" @@ -45,11 +46,13 @@ func TestChunkUploadDownload(t *testing.T) { Storer: storerMock, Pinning: pinningMock, Tags: tag, + Post: mockpost.New(mockpost.WithAcceptAll()), }) ) t.Run("empty chunk", func(t *testing.T) { jsonhttptest.Request(t, client, http.MethodPost, chunksEndpoint, http.StatusBadRequest, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ Message: "data length", Code: http.StatusBadRequest, @@ -59,6 +62,7 @@ func TestChunkUploadDownload(t *testing.T) { t.Run("ok", func(t *testing.T) { jsonhttptest.Request(t, client, http.MethodPost, chunksEndpoint, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(chunk.Data())), jsonhttptest.WithExpectedJSONResponse(api.ChunkAddressResponse{Reference: chunk.Address()}), ) @@ -77,6 +81,7 @@ func TestChunkUploadDownload(t *testing.T) { t.Run("pin-invalid-value", func(t *testing.T) { jsonhttptest.Request(t, client, http.MethodPost, chunksEndpoint, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(chunk.Data())), jsonhttptest.WithExpectedJSONResponse(api.ChunkAddressResponse{Reference: chunk.Address()}), jsonhttptest.WithRequestHeader(api.SwarmPinHeader, "invalid-pin"), @@ -89,6 +94,7 @@ func TestChunkUploadDownload(t *testing.T) { }) t.Run("pin-header-missing", func(t *testing.T) { jsonhttptest.Request(t, client, http.MethodPost, chunksEndpoint, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(chunk.Data())), jsonhttptest.WithExpectedJSONResponse(api.ChunkAddressResponse{Reference: chunk.Address()}), ) @@ -101,6 +107,7 @@ func TestChunkUploadDownload(t *testing.T) { t.Run("pin-ok", func(t *testing.T) { address := chunk.Address() jsonhttptest.Request(t, client, http.MethodPost, chunksEndpoint, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(chunk.Data())), jsonhttptest.WithExpectedJSONResponse(api.ChunkAddressResponse{Reference: address}), jsonhttptest.WithRequestHeader(api.SwarmPinHeader, "True"), diff --git a/pkg/api/dirs_test.go b/pkg/api/dirs_test.go index d27fc6b3c4a..a81c1b7fbe3 100644 --- a/pkg/api/dirs_test.go +++ b/pkg/api/dirs_test.go @@ -24,6 +24,7 @@ import ( "github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" "github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/manifest" + mockpost "github.com/ethersphere/bee/pkg/postage/mock" statestore "github.com/ethersphere/bee/pkg/statestore/mock" "github.com/ethersphere/bee/pkg/storage" "github.com/ethersphere/bee/pkg/storage/mock" @@ -44,12 +45,14 @@ func TestDirs(t *testing.T) { Tags: tags.NewTags(mockStatestore, logger), Logger: logger, PreventRedirect: true, + Post: mockpost.New(mockpost.WithAcceptAll()), }) ) t.Run("empty request body", func(t *testing.T) { jsonhttptest.Request(t, client, http.MethodPost, dirUploadResource, http.StatusBadRequest, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(nil)), jsonhttptest.WithRequestHeader(api.SwarmCollectionHeader, "True"), jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ @@ -65,6 +68,7 @@ func TestDirs(t *testing.T) { jsonhttptest.Request(t, client, http.MethodPost, dirUploadResource, http.StatusInternalServerError, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(file), jsonhttptest.WithRequestHeader(api.SwarmCollectionHeader, "True"), jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ @@ -84,6 +88,7 @@ func TestDirs(t *testing.T) { // submit valid tar, but with wrong content-type jsonhttptest.Request(t, client, http.MethodPost, dirUploadResource, http.StatusBadRequest, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(tarReader), jsonhttptest.WithRequestHeader(api.SwarmCollectionHeader, "True"), jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ @@ -382,6 +387,7 @@ func TestDirs(t *testing.T) { var resp api.BzzUploadResponse options := []jsonhttptest.Option{ + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(tarReader), jsonhttptest.WithRequestHeader(api.SwarmCollectionHeader, "True"), jsonhttptest.WithRequestHeader("Content-Type", api.ContentTypeTar), @@ -414,6 +420,7 @@ func TestDirs(t *testing.T) { var resp api.BzzUploadResponse options := []jsonhttptest.Option{ + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(mwReader), jsonhttptest.WithRequestHeader(api.SwarmCollectionHeader, "True"), jsonhttptest.WithRequestHeader("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%q", mwBoundary)), diff --git a/pkg/api/feed_test.go b/pkg/api/feed_test.go index 173e5155cb1..67cb99aad2b 100644 --- a/pkg/api/feed_test.go +++ b/pkg/api/feed_test.go @@ -22,6 +22,8 @@ import ( "github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" "github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/manifest" + "github.com/ethersphere/bee/pkg/postage" + mockpost "github.com/ethersphere/bee/pkg/postage/mock" testingsoc "github.com/ethersphere/bee/pkg/soc/testing" statestore "github.com/ethersphere/bee/pkg/statestore/mock" "github.com/ethersphere/bee/pkg/storage" @@ -152,17 +154,20 @@ 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))) mockStorer = mock.NewStorer() client, _, _ = newTestServer(t, testServerOptions{ Storer: mockStorer, Tags: tag, Logger: logger, + Post: mp, }) url = fmt.Sprintf("/feeds/%s/%s?type=%s", ownerString, topic, "sequence") ) t.Run("ok", func(t *testing.T) { jsonhttptest.Request(t, client, http.MethodPost, url, http.StatusCreated, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithExpectedJSONResponse(api.FeedReferenceResponse{ Reference: expReference, }), @@ -206,9 +211,9 @@ func TestFeed_Post(t *testing.T) { jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, hexbatch), ) }) - t.Run("ok - batch empty", func(t *testing.T) { + t.Run("bad request - batch empty", func(t *testing.T) { hexbatch := hex.EncodeToString(batchEmpty) - jsonhttptest.Request(t, client, http.MethodPost, url, http.StatusCreated, + jsonhttptest.Request(t, client, http.MethodPost, url, http.StatusBadRequest, jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, hexbatch), ) }) diff --git a/pkg/api/gatewaymode_test.go b/pkg/api/gatewaymode_test.go index 8eb426c957c..0dfd13e363e 100644 --- a/pkg/api/gatewaymode_test.go +++ b/pkg/api/gatewaymode_test.go @@ -14,6 +14,7 @@ import ( "github.com/ethersphere/bee/pkg/jsonhttp" "github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" "github.com/ethersphere/bee/pkg/logging" + mockpost "github.com/ethersphere/bee/pkg/postage/mock" statestore "github.com/ethersphere/bee/pkg/statestore/mock" "github.com/ethersphere/bee/pkg/storage/mock" testingc "github.com/ethersphere/bee/pkg/storage/testing" @@ -28,6 +29,7 @@ func TestGatewayMode(t *testing.T) { Tags: tags.NewTags(statestore.NewStateStore(), logger), Logger: logger, GatewayMode: true, + Post: mockpost.New(mockpost.WithAcceptAll()), }) forbiddenResponseOption := jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ @@ -66,12 +68,14 @@ func TestGatewayMode(t *testing.T) { // should work without pinning jsonhttptest.Request(t, client, http.MethodPost, "/chunks", http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(chunk.Data())), ) jsonhttptest.Request(t, client, http.MethodPost, "/chunks/0773a91efd6547c754fc1d95fb1c62c7d1b47f959c2caa685dfec8736da95c1c", http.StatusForbidden, forbiddenResponseOption, headerOption) jsonhttptest.Request(t, client, http.MethodPost, "/bytes", http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(chunk.Data())), ) // should work without pinning jsonhttptest.Request(t, client, http.MethodPost, "/bytes", http.StatusForbidden, forbiddenResponseOption, headerOption) @@ -88,6 +92,7 @@ func TestGatewayMode(t *testing.T) { }) jsonhttptest.Request(t, client, http.MethodPost, "/bytes", http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(chunk.Data())), ) // should work without pinning jsonhttptest.Request(t, client, http.MethodPost, "/bytes", http.StatusForbidden, forbiddenResponseOption, headerOption) diff --git a/pkg/api/pin_test.go b/pkg/api/pin_test.go index f8d45332f58..72866a5b160 100644 --- a/pkg/api/pin_test.go +++ b/pkg/api/pin_test.go @@ -16,6 +16,7 @@ import ( "github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" "github.com/ethersphere/bee/pkg/logging" pinning "github.com/ethersphere/bee/pkg/pinning/mock" + mockpost "github.com/ethersphere/bee/pkg/postage/mock" statestore "github.com/ethersphere/bee/pkg/statestore/mock" "github.com/ethersphere/bee/pkg/storage/mock" testingc "github.com/ethersphere/bee/pkg/storage/testing" @@ -86,12 +87,14 @@ func TestPinHandlers(t *testing.T) { Tags: tags.NewTags(statestore.NewStateStore(), logging.New(ioutil.Discard, 0)), Pinning: pinning.NewServiceMock(), Logger: logging.New(ioutil.Discard, 5), + Post: mockpost.New(mockpost.WithAcceptAll()), }) ) t.Run("bytes", func(t *testing.T) { const rootHash = "838d0a193ecd1152d1bb1432d5ecc02398533b2494889e23b8bd5ace30ac2aeb" jsonhttptest.Request(t, client, http.MethodPost, "/bytes", http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(strings.NewReader("this is a simple text")), jsonhttptest.WithExpectedJSONResponse(api.BzzUploadResponse{ Reference: swarm.MustParseHexAddress(rootHash), @@ -108,6 +111,7 @@ func TestPinHandlers(t *testing.T) { }}) rootHash := "9e178dbd1ed4b748379e25144e28dfb29c07a4b5114896ef454480115a56b237" jsonhttptest.Request(t, client, http.MethodPost, "/bzz", http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(tarReader), jsonhttptest.WithRequestHeader("Content-Type", api.ContentTypeTar), jsonhttptest.WithRequestHeader(api.SwarmCollectionHeader, "True"), @@ -119,6 +123,7 @@ func TestPinHandlers(t *testing.T) { rootHash = "dd13a5a6cc9db3ef514d645e6719178dbfb1a90b49b9262cafce35b0d27cf245" jsonhttptest.Request(t, client, http.MethodPost, "/bzz?name=somefile.txt", http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestHeader("Content-Type", "text/plain"), jsonhttptest.WithRequestBody(strings.NewReader("this is a simple text")), jsonhttptest.WithExpectedJSONResponse(api.BzzUploadResponse{ @@ -134,6 +139,7 @@ func TestPinHandlers(t *testing.T) { rootHash = chunk.Address().String() ) jsonhttptest.Request(t, client, http.MethodPost, "/chunks", http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(chunk.Data())), jsonhttptest.WithExpectedJSONResponse(api.ChunkAddressResponse{ Reference: chunk.Address(), diff --git a/pkg/api/postage_test.go b/pkg/api/postage_test.go index 635565affd6..6e4558a4a5a 100644 --- a/pkg/api/postage_test.go +++ b/pkg/api/postage_test.go @@ -16,6 +16,8 @@ import ( "github.com/ethersphere/bee/pkg/api" "github.com/ethersphere/bee/pkg/jsonhttp" "github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" + "github.com/ethersphere/bee/pkg/postage" + mockpost "github.com/ethersphere/bee/pkg/postage/mock" "github.com/ethersphere/bee/pkg/postage/postagecontract" contractMock "github.com/ethersphere/bee/pkg/postage/postagecontract/mock" ) @@ -133,14 +135,14 @@ func TestPostageCreateStamp(t *testing.T) { } func TestPostageGetStamps(t *testing.T) { - batchID := make([]byte, 32) // this will break once the fallback batchid is gone - client, _, _ := newTestServer(t, testServerOptions{}) + mp := mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, 11, 10))) + client, _, _ := newTestServer(t, testServerOptions{Post: mp}) jsonhttptest.Request(t, client, http.MethodGet, "/stamps", http.StatusOK, jsonhttptest.WithExpectedJSONResponse(&api.PostageStampsResponse{ Stamps: []api.PostageStampResponse{ { - BatchID: batchID, + BatchID: batchOk, Utilization: 0, }, }, @@ -149,20 +151,18 @@ func TestPostageGetStamps(t *testing.T) { } func TestPostageGetStamp(t *testing.T) { - batchID := make([]byte, 32) // this will break once the fallback batchid is gone + mp := mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, 11, 10))) + client, _, _ := newTestServer(t, testServerOptions{Post: mp}) t.Run("ok", func(t *testing.T) { - client, _, _ := newTestServer(t, testServerOptions{}) - - jsonhttptest.Request(t, client, http.MethodGet, "/stamps/"+hex.EncodeToString(batchID), http.StatusOK, + jsonhttptest.Request(t, client, http.MethodGet, "/stamps/"+batchOkStr, http.StatusOK, jsonhttptest.WithExpectedJSONResponse(&api.PostageStampResponse{ - BatchID: batchID, + BatchID: batchOk, Utilization: 0, }), ) }) t.Run("ok", func(t *testing.T) { - client, _, _ := newTestServer(t, testServerOptions{}) badBatch := []byte{0, 1, 2} jsonhttptest.Request(t, client, http.MethodGet, "/stamps/"+hex.EncodeToString(badBatch), http.StatusBadRequest, @@ -173,7 +173,6 @@ func TestPostageGetStamp(t *testing.T) { ) }) t.Run("ok", func(t *testing.T) { - client, _, _ := newTestServer(t, testServerOptions{}) badBatch := []byte{0, 1, 2, 4} jsonhttptest.Request(t, client, http.MethodGet, "/stamps/"+hex.EncodeToString(badBatch), http.StatusBadRequest, diff --git a/pkg/api/pss_test.go b/pkg/api/pss_test.go index 24462f12b76..3470a4e911f 100644 --- a/pkg/api/pss_test.go +++ b/pkg/api/pss_test.go @@ -24,6 +24,7 @@ import ( "github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" "github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/postage" + mockpost "github.com/ethersphere/bee/pkg/postage/mock" "github.com/ethersphere/bee/pkg/pss" "github.com/ethersphere/bee/pkg/pushsync" "github.com/ethersphere/bee/pkg/storage/mock" @@ -184,12 +185,13 @@ func TestPssSend(t *testing.T) { mtx.Unlock() return err } - + mp = mockpost.New(mockpost.WithIssuer(postage.NewStampIssuer("", "", batchOk, 11, 10))) p = newMockPss(sendFn) client, _, _ = newTestServer(t, testServerOptions{ Pss: p, Storer: mock.NewStorer(), Logger: logger, + Post: mp, }) recipient = hex.EncodeToString(publicKeyBytes) @@ -225,16 +227,16 @@ func TestPssSend(t *testing.T) { ) }) - t.Run("ok - batch zeros", func(t *testing.T) { + t.Run("ok batch", func(t *testing.T) { hexbatch := hex.EncodeToString(batchOk) jsonhttptest.Request(t, client, http.MethodPost, "/pss/send/to/12", http.StatusOK, jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, hexbatch), jsonhttptest.WithRequestBody(bytes.NewReader(payload)), ) }) - t.Run("ok - batch empty", func(t *testing.T) { + t.Run("bad request - batch empty", func(t *testing.T) { hexbatch := hex.EncodeToString(batchEmpty) - jsonhttptest.Request(t, client, http.MethodPost, "/pss/send/to/12", http.StatusOK, + jsonhttptest.Request(t, client, http.MethodPost, "/pss/send/to/12", http.StatusBadRequest, jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, hexbatch), jsonhttptest.WithRequestBody(bytes.NewReader(payload)), ) @@ -242,6 +244,7 @@ func TestPssSend(t *testing.T) { t.Run("ok", func(t *testing.T) { jsonhttptest.Request(t, client, http.MethodPost, "/pss/send/testtopic/12?recipient="+recipient, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(payload)), jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ Message: "OK", @@ -262,6 +265,7 @@ func TestPssSend(t *testing.T) { t.Run("without recipient", func(t *testing.T) { jsonhttptest.Request(t, client, http.MethodPost, "/pss/send/testtopic/12", http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(payload)), jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ Message: "OK", diff --git a/pkg/api/soc_test.go b/pkg/api/soc_test.go index bab54444c53..f2f0b084ab4 100644 --- a/pkg/api/soc_test.go +++ b/pkg/api/soc_test.go @@ -16,6 +16,8 @@ import ( "github.com/ethersphere/bee/pkg/jsonhttp" "github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" "github.com/ethersphere/bee/pkg/logging" + "github.com/ethersphere/bee/pkg/postage" + mockpost "github.com/ethersphere/bee/pkg/postage/mock" "github.com/ethersphere/bee/pkg/soc" testingsoc "github.com/ethersphere/bee/pkg/soc/testing" statestore "github.com/ethersphere/bee/pkg/statestore/mock" @@ -30,10 +32,12 @@ 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))) mockStorer = mock.NewStorer() client, _, _ = newTestServer(t, testServerOptions{ Storer: mockStorer, Tags: tag, + Post: mp, }) ) t.Run("cmpty data", func(t *testing.T) { @@ -94,6 +98,7 @@ func TestSOC(t *testing.T) { s := testingsoc.GenerateMockSOC(t, testData) jsonhttptest.Request(t, client, http.MethodPost, socResource(hex.EncodeToString(s.Owner), hex.EncodeToString(s.ID), hex.EncodeToString(s.Signature)), http.StatusCreated, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(s.WrappedChunk.Data())), jsonhttptest.WithExpectedJSONResponse(api.SocPostResponse{ Reference: s.Address(), @@ -117,12 +122,14 @@ func TestSOC(t *testing.T) { s := testingsoc.GenerateMockSOC(t, testData) jsonhttptest.Request(t, client, http.MethodPost, socResource(hex.EncodeToString(s.Owner), hex.EncodeToString(s.ID), hex.EncodeToString(s.Signature)), http.StatusCreated, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(s.WrappedChunk.Data())), jsonhttptest.WithExpectedJSONResponse(api.SocPostResponse{ Reference: s.Address(), }), ) jsonhttptest.Request(t, client, http.MethodPost, socResource(hex.EncodeToString(s.Owner), hex.EncodeToString(s.ID), hex.EncodeToString(s.Signature)), http.StatusConflict, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(s.WrappedChunk.Data())), jsonhttptest.WithExpectedJSONResponse( jsonhttp.StatusResponse{ @@ -145,7 +152,7 @@ func TestSOC(t *testing.T) { })) }) - t.Run("ok - batch zeros", func(t *testing.T) { + t.Run("ok batch", func(t *testing.T) { s := testingsoc.GenerateMockSOC(t, testData) hexbatch := hex.EncodeToString(batchOk) jsonhttptest.Request(t, client, http.MethodPost, socResource(hex.EncodeToString(s.Owner), hex.EncodeToString(s.ID), hex.EncodeToString(s.Signature)), http.StatusCreated, @@ -153,10 +160,10 @@ func TestSOC(t *testing.T) { jsonhttptest.WithRequestBody(bytes.NewReader(s.WrappedChunk.Data())), ) }) - t.Run("ok - batch empty", func(t *testing.T) { + t.Run("err - batch empty", func(t *testing.T) { s := testingsoc.GenerateMockSOC(t, testData) hexbatch := hex.EncodeToString(batchEmpty) - jsonhttptest.Request(t, client, http.MethodPost, socResource(hex.EncodeToString(s.Owner), hex.EncodeToString(s.ID), hex.EncodeToString(s.Signature)), http.StatusCreated, + jsonhttptest.Request(t, client, http.MethodPost, socResource(hex.EncodeToString(s.Owner), hex.EncodeToString(s.ID), hex.EncodeToString(s.Signature)), http.StatusBadRequest, jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, hexbatch), jsonhttptest.WithRequestBody(bytes.NewReader(s.WrappedChunk.Data())), ) diff --git a/pkg/api/tag_test.go b/pkg/api/tag_test.go index 0ad612e9e43..6d8b4202971 100644 --- a/pkg/api/tag_test.go +++ b/pkg/api/tag_test.go @@ -14,6 +14,7 @@ import ( "testing" "github.com/ethersphere/bee/pkg/logging" + mockpost "github.com/ethersphere/bee/pkg/postage/mock" statestore "github.com/ethersphere/bee/pkg/statestore/mock" "github.com/ethersphere/bee/pkg/api" @@ -47,6 +48,7 @@ func TestTags(t *testing.T) { Storer: mock.NewStorer(), Tags: tag, Logger: logger, + Post: mockpost.New(mockpost.WithAcceptAll()), }) ) @@ -105,11 +107,13 @@ func TestTags(t *testing.T) { ) _ = jsonhttptest.Request(t, client, http.MethodPost, chunksResource, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(chunk.Data())), jsonhttptest.WithExpectedJSONResponse(api.ChunkAddressResponse{Reference: chunk.Address()}), ) rcvdHeaders := jsonhttptest.Request(t, client, http.MethodPost, chunksResource, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(chunk.Data())), jsonhttptest.WithExpectedJSONResponse(api.ChunkAddressResponse{Reference: chunk.Address()}), jsonhttptest.WithRequestHeader(api.SwarmTagHeader, strconv.FormatUint(uint64(tr.Uid), 10)), @@ -229,6 +233,7 @@ func TestTags(t *testing.T) { // upload content with tag jsonhttptest.Request(t, client, http.MethodPost, chunksResource, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(chunk.Data())), jsonhttptest.WithRequestHeader(api.SwarmTagHeader, fmt.Sprint(tagId)), ) @@ -268,6 +273,7 @@ func TestTags(t *testing.T) { respHeaders := jsonhttptest.Request(t, client, http.MethodPost, bzzResource+"?name=somefile", http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader([]byte("some data"))), jsonhttptest.WithExpectedJSONResponse(expectedResponse), jsonhttptest.WithRequestHeader("Content-Type", "application/octet-stream"), @@ -290,6 +296,7 @@ func TestTags(t *testing.T) { expectedResponse := api.BzzUploadResponse{Reference: expectedHash} respHeaders := jsonhttptest.Request(t, client, http.MethodPost, bzzResource, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(tarReader), jsonhttptest.WithRequestHeader(api.SwarmCollectionHeader, "True"), jsonhttptest.WithExpectedJSONResponse(expectedResponse), @@ -307,6 +314,7 @@ func TestTags(t *testing.T) { // create a tag using the API tr := api.TagResponse{} jsonhttptest.Request(t, client, http.MethodPost, tagsResource, http.StatusCreated, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithJSONRequestBody(api.TagRequest{}), jsonhttptest.WithUnmarshalJSONResponse(&tr), ) @@ -327,6 +335,7 @@ func TestTags(t *testing.T) { copy(content[:swarm.ChunkSize], dataChunk) rcvdHeaders := jsonhttptest.Request(t, client, http.MethodPost, bytesResource, http.StatusOK, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), jsonhttptest.WithRequestBody(bytes.NewReader(content)), jsonhttptest.WithExpectedJSONResponse(fileUploadResponse{ Reference: rootAddress, diff --git a/pkg/node/node.go b/pkg/node/node.go index a658dd891f8..1de8b37bee6 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -21,7 +21,6 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/ethclient" "github.com/ethersphere/bee/pkg/accounting" "github.com/ethersphere/bee/pkg/addressbook" "github.com/ethersphere/bee/pkg/api" @@ -57,7 +56,6 @@ import ( "github.com/ethersphere/bee/pkg/settlement/pseudosettle" "github.com/ethersphere/bee/pkg/settlement/swap" "github.com/ethersphere/bee/pkg/settlement/swap/chequebook" - "github.com/ethersphere/bee/pkg/settlement/swap/transaction" "github.com/ethersphere/bee/pkg/storage" "github.com/ethersphere/bee/pkg/swarm" "github.com/ethersphere/bee/pkg/tags" @@ -202,30 +200,26 @@ func NewBee(addr string, swarmAddress swarm.Address, publicKey ecdsa.PublicKey, addressbook := addressbook.New(stateStore) - var swapBackend *ethclient.Client - var overlayEthAddress common.Address - var chainID int64 - var transactionService transaction.Service - var transactionMonitor transaction.Monitor var chequebookFactory chequebook.Factory var chequebookService chequebook.Service var chequeStore chequebook.ChequeStore var cashoutService chequebook.CashoutService - if o.SwapEnable { - swapBackend, overlayEthAddress, chainID, transactionMonitor, transactionService, err = InitChain( - p2pCtx, - logger, - stateStore, - o.SwapEndpoint, - signer, - ) - if err != nil { - return nil, err - } - b.ethClientCloser = swapBackend.Close - b.transactionMonitorCloser = transactionMonitor + swapBackend, overlayEthAddress, chainID, transactionMonitor, transactionService, err := InitChain( + p2pCtx, + logger, + stateStore, + o.SwapEndpoint, + signer, + ) + if err != nil { + return nil, err + } + b.ethClientCloser = swapBackend.Close + b.transactionMonitorCloser = transactionMonitor + + if o.SwapEnable { chequebookFactory, err = InitChequebookFactory( logger, swapBackend, diff --git a/pkg/postage/batchstore/reserve.go b/pkg/postage/batchstore/reserve.go index 66693c68332..0430163a45d 100644 --- a/pkg/postage/batchstore/reserve.go +++ b/pkg/postage/batchstore/reserve.go @@ -40,7 +40,7 @@ import ( var ErrBatchNotFound = errors.New("postage batch not found or expired") // DefaultDepth is the initial depth for the reserve -const DefaultDepth = 5 +var DefaultDepth = uint8(12) // 12 is the testnet depth at the time of merging to master // Capacity is the number of chunks in reserve. `2^23` (8388608) was chosen to remain // relatively near the current 5M chunks ~25GB. diff --git a/pkg/postage/batchstore/reserve_test.go b/pkg/postage/batchstore/reserve_test.go index 4b34da6fb65..f2711df24ea 100644 --- a/pkg/postage/batchstore/reserve_test.go +++ b/pkg/postage/batchstore/reserve_test.go @@ -50,10 +50,11 @@ func newValue(price, value *big.Int) *big.Int { // - reserve exceeds capacity // - value-consistency of unreserved POs func TestBatchStoreUnreserveEvents(t *testing.T) { - // temporarily reset reserve Capacity - defer func(i int64) { + defer func(i int64, d uint8) { batchstore.Capacity = i - }(batchstore.Capacity) + batchstore.DefaultDepth = d + }(batchstore.Capacity, batchstore.DefaultDepth) + batchstore.DefaultDepth = 5 batchstore.Capacity = batchstore.Exp2(16) bStore, unreserved := setupBatchStore(t) @@ -133,10 +134,11 @@ func TestBatchStoreUnreserveEvents(t *testing.T) { } func TestBatchStoreUnreserveAll(t *testing.T) { - // temporarily reset reserve Capacity - defer func(i int64) { + defer func(i int64, d uint8) { batchstore.Capacity = i - }(batchstore.Capacity) + batchstore.DefaultDepth = d + }(batchstore.Capacity, batchstore.DefaultDepth) + batchstore.DefaultDepth = 5 batchstore.Capacity = batchstore.Exp2(16) bStore, unreserved := setupBatchStore(t) @@ -324,9 +326,11 @@ func checkReserve(bStore postage.Storer, unreserved map[string]uint8) (uint8, er // └──┴──┴──┴──┴───────> time // func TestBatchStore_Unreserve(t *testing.T) { - defer func(i int64) { + defer func(i int64, d uint8) { batchstore.Capacity = i - }(batchstore.Capacity) + batchstore.DefaultDepth = d + }(batchstore.Capacity, batchstore.DefaultDepth) + batchstore.DefaultDepth = 5 batchstore.Capacity = batchstore.Exp2(5) // 32 chunks // 8 is the initial batch depth we add the initial state batches with. // the default radius is 5 (defined in reserve.go file), which means there @@ -573,9 +577,11 @@ func TestBatchStore_Unreserve(t *testing.T) { // └──┴──┴──┴──┴──┴─────> time // func TestBatchStore_Topup(t *testing.T) { - defer func(i int64) { + defer func(i int64, d uint8) { batchstore.Capacity = i - }(batchstore.Capacity) + batchstore.DefaultDepth = d + }(batchstore.Capacity, batchstore.DefaultDepth) + batchstore.DefaultDepth = 5 batchstore.Capacity = batchstore.Exp2(5) // 32 chunks initBatchDepth := uint8(8) @@ -688,9 +694,11 @@ func TestBatchStore_Topup(t *testing.T) { // └──┴──┴──┴──┴──┴─────> time // func TestBatchStore_Dilution(t *testing.T) { - defer func(i int64) { + defer func(i int64, d uint8) { batchstore.Capacity = i - }(batchstore.Capacity) + batchstore.DefaultDepth = d + }(batchstore.Capacity, batchstore.DefaultDepth) + batchstore.DefaultDepth = 5 batchstore.Capacity = batchstore.Exp2(5) // 32 chunks initBatchDepth := uint8(8) @@ -797,10 +805,11 @@ func TestBatchStore_Dilution(t *testing.T) { } func TestBatchStore_EvictExpired(t *testing.T) { - // temporarily reset reserve Capacity - defer func(i int64) { + defer func(i int64, d uint8) { batchstore.Capacity = i - }(batchstore.Capacity) + batchstore.DefaultDepth = d + }(batchstore.Capacity, batchstore.DefaultDepth) + batchstore.DefaultDepth = 5 batchstore.Capacity = batchstore.Exp2(5) // 32 chunks initBatchDepth := uint8(8) diff --git a/pkg/postage/batchstore/store.go b/pkg/postage/batchstore/store.go index 571a6dfe836..6d0033bbbca 100644 --- a/pkg/postage/batchstore/store.go +++ b/pkg/postage/batchstore/store.go @@ -15,8 +15,8 @@ import ( const ( batchKeyPrefix = "batchstore_batch_" valueKeyPrefix = "batchstore_value_" - chainStateKey = "batchstore_chain_" - reserveStateKey = "batchstore_reserve_" + chainStateKey = "batchstore_chainstate" + reserveStateKey = "batchstore_reservestate" ) type unreserveFn func(batchID []byte, radius uint8) error diff --git a/pkg/postage/batchstore/store_test.go b/pkg/postage/batchstore/store_test.go index 475176b0219..80d7f64dce2 100644 --- a/pkg/postage/batchstore/store_test.go +++ b/pkg/postage/batchstore/store_test.go @@ -106,4 +106,4 @@ func batchStorePutChainState(t *testing.T, st postage.Storer, cs *postage.ChainS type noopRadiusSetter struct{} -func (_ noopRadiusSetter) SetRadius(_ uint8) {} +func (n noopRadiusSetter) SetRadius(_ uint8) {} diff --git a/pkg/postage/listener/export_test.go b/pkg/postage/listener/export_test.go index 5c77e1f82b8..596255c4764 100644 --- a/pkg/postage/listener/export_test.go +++ b/pkg/postage/listener/export_test.go @@ -1,3 +1,7 @@ +// Copyright 2021 The Swarm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package listener var ( diff --git a/pkg/postage/listener/listener.go b/pkg/postage/listener/listener.go index 45ea5efa915..be0cdca6db3 100644 --- a/pkg/postage/listener/listener.go +++ b/pkg/postage/listener/listener.go @@ -1,3 +1,7 @@ +// Copyright 2021 The Swarm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package listener import ( @@ -26,13 +30,17 @@ const ( ) var ( - chainUpdateInterval = 5 * time.Second - postageStampABI = parseABI(postageabi.PostageStampABIv0_1_0) - priceOracleABI = parseABI(postageabi.PriceOracleABIv0_1_0) - batchCreatedTopic = postageStampABI.Events["BatchCreated"].ID - batchTopupTopic = postageStampABI.Events["BatchTopUp"].ID + chainUpdateInterval = 5 * time.Second + postageStampABI = parseABI(postageabi.PostageStampABIv0_1_0) + priceOracleABI = parseABI(postageabi.PriceOracleABIv0_1_0) + // batchCreatedTopic is the postage contract's batch created event topic + batchCreatedTopic = postageStampABI.Events["BatchCreated"].ID + // batchTopupTopic is the postage contract's batch topup event topic + batchTopupTopic = postageStampABI.Events["BatchTopUp"].ID + // batchDepthIncreaseTopic is the postage contract's batch dilution event topic batchDepthIncreaseTopic = postageStampABI.Events["BatchDepthIncrease"].ID - priceUpdateTopic = priceOracleABI.Events["PriceUpdate"].ID + // priceUpdateTopic is the price oracle's price update event topic + priceUpdateTopic = priceOracleABI.Events["PriceUpdate"].ID ) type BlockHeightContractFilterer interface { @@ -160,7 +168,7 @@ func (l *listener) sync(from uint64, updater postage.EventUpdater) error { case <-l.quit: return nil } - to, err := l.ev.BlockNumber(context.Background()) + to, err := l.ev.BlockNumber(ctx) if err != nil { return err } diff --git a/pkg/postage/listener/listener_test.go b/pkg/postage/listener/listener_test.go index e70eb0ee488..9f811fbce6b 100644 --- a/pkg/postage/listener/listener_test.go +++ b/pkg/postage/listener/listener_test.go @@ -1,3 +1,7 @@ +// Copyright 2021 The Swarm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package listener_test import ( diff --git a/pkg/postage/mock/service.go b/pkg/postage/mock/service.go index 1feade7a318..f7ca44a5297 100644 --- a/pkg/postage/mock/service.go +++ b/pkg/postage/mock/service.go @@ -4,7 +4,11 @@ package mock -import "github.com/ethersphere/bee/pkg/postage" +import ( + "errors" + + "github.com/ethersphere/bee/pkg/postage" +) type optionFunc func(*mockPostage) @@ -22,24 +26,22 @@ func New(o ...Option) postage.Service { v.apply(m) } - // Add the fallback value we have in the api right now. - // In the future this needs to go away once batch id becomes de facto - // mandatory in the package. - - id := make([]byte, 32) - st := postage.NewStampIssuer("test fallback", "test identity", id, 24, 6) - m.Add(st) - return m } -// WithMockBatch sets the mock batch on the mock postage service. -func WithMockBatch(id []byte) Option { - return optionFunc(func(m *mockPostage) {}) +// WithAcceptAll sets the mock to return a new BatchIssuer on every +// call to GetStampIssuer. +func WithAcceptAll() Option { + return optionFunc(func(m *mockPostage) { m.acceptAll = true }) +} + +func WithIssuer(s *postage.StampIssuer) Option { + return optionFunc(func(m *mockPostage) { m.i = s }) } type mockPostage struct { - i *postage.StampIssuer + i *postage.StampIssuer + acceptAll bool } func (m *mockPostage) Add(s *postage.StampIssuer) { @@ -50,8 +52,16 @@ func (m *mockPostage) StampIssuers() []*postage.StampIssuer { return []*postage.StampIssuer{m.i} } -func (m *mockPostage) GetStampIssuer(_ []byte) (*postage.StampIssuer, error) { - return m.i, nil +func (m *mockPostage) GetStampIssuer(id []byte) (*postage.StampIssuer, error) { + if m.acceptAll { + return postage.NewStampIssuer("test fallback", "test identity", id, 24, 6), nil + } + + if m.i != nil { + return m.i, nil + } + + return nil, errors.New("stampissuer not found") } func (m *mockPostage) Load() error { diff --git a/pkg/postage/postagecontract/contract.go b/pkg/postage/postagecontract/contract.go index 1f0c089b4bc..41cf1ee181f 100644 --- a/pkg/postage/postagecontract/contract.go +++ b/pkg/postage/postagecontract/contract.go @@ -22,6 +22,8 @@ import ( ) var ( + BucketDepth = uint8(16) + postageStampABI = parseABI(postageabi.PostageStampABIv0_1_0) erc20ABI = parseABI(sw3abi.ERC20ABIv0_3_1) batchCreatedTopic = postageStampABI.Events["BatchCreated"].ID @@ -141,9 +143,8 @@ func (c *postageContract) getBalance(ctx context.Context) (*big.Int, error) { } func (c *postageContract) CreateBatch(ctx context.Context, initialBalance *big.Int, depth uint8, label string) ([]byte, error) { - bucketDepth := uint8(10) - if depth < bucketDepth { + if depth < BucketDepth { return nil, ErrInvalidDepth } @@ -188,7 +189,7 @@ func (c *postageContract) CreateBatch(ctx context.Context, initialBalance *big.I c.owner.Hex(), batchID, depth, - bucketDepth, + BucketDepth, )) return createdEvent.BatchId[:], nil diff --git a/pkg/postage/postagecontract/contract_test.go b/pkg/postage/postagecontract/contract_test.go index 65c746bbfeb..5837101561c 100644 --- a/pkg/postage/postagecontract/contract_test.go +++ b/pkg/postage/postagecontract/contract_test.go @@ -21,6 +21,10 @@ import ( ) func TestCreateBatch(t *testing.T) { + defer func(b uint8) { + postagecontract.BucketDepth = b + }(postagecontract.BucketDepth) + postagecontract.BucketDepth = 10 owner := common.HexToAddress("abcd") label := "label" postageStampAddress := common.HexToAddress("ffff")