forked from ethersphere/bee
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2024 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 api | ||
|
||
import ( | ||
"encoding/hex" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/ethersphere/bee/v2/pkg/jsonhttp" | ||
"github.com/ethersphere/bee/v2/pkg/postage" | ||
"github.com/ethersphere/bee/v2/pkg/swarm" | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
type postEnvelopeResponse struct { | ||
Issuer string `json:"issuer"` // Ethereum address of the postage batch owner | ||
Index string `json:"index"` // used index of the Postage Batch | ||
Timestamp string `json:"timestamp"` // timestamp of the postage stamp | ||
Signature string `json:"signature"` // postage stamp signature | ||
} | ||
|
||
// envelopePostHandler generates new postage stamp for requested chunk address | ||
func (s *Service) envelopePostHandler(w http.ResponseWriter, r *http.Request) { | ||
logger := s.logger.WithName("post_envelope").Build() | ||
|
||
headers := struct { | ||
BatchID []byte `map:"Swarm-Postage-Batch-Id" validate:"required"` | ||
}{} | ||
if response := s.mapStructure(r.Header, &headers); response != nil { | ||
response("invalid header params", logger, w) | ||
return | ||
} | ||
|
||
paths := struct { | ||
Address swarm.Address `map:"address" validate:"required"` | ||
}{} | ||
if response := s.mapStructure(mux.Vars(r), &paths); response != nil { | ||
response("invalid path params", logger, w) | ||
return | ||
} | ||
|
||
stamper, save, err := s.getStamper(headers.BatchID) | ||
if err != nil { | ||
logger.Debug("get stamper failed", "error", err) | ||
logger.Error(err, "get stamper failed") | ||
switch { | ||
case errors.Is(err, errBatchUnusable) || errors.Is(err, postage.ErrNotUsable): | ||
jsonhttp.UnprocessableEntity(w, "batch not usable yet or does not exist") | ||
case errors.Is(err, postage.ErrNotFound): | ||
jsonhttp.NotFound(w, "batch with id not found") | ||
case errors.Is(err, errInvalidPostageBatch): | ||
jsonhttp.BadRequest(w, "invalid batch id") | ||
default: | ||
jsonhttp.InternalServerError(w, nil) | ||
} | ||
return | ||
} | ||
|
||
stamp, err := stamper.Stamp(paths.Address) | ||
if err != nil { | ||
logger.Debug("split write all failed", "error", err) | ||
logger.Error(nil, "split write all failed") | ||
switch { | ||
case errors.Is(err, postage.ErrBucketFull): | ||
jsonhttp.PaymentRequired(w, "batch is overissued") | ||
default: | ||
jsonhttp.InternalServerError(w, "stamping failed") | ||
} | ||
return | ||
} | ||
err = save() | ||
if err != nil { | ||
jsonhttp.InternalServerError(w, "failed to save stamp issuer") | ||
return | ||
} | ||
|
||
issuer, err := s.signer.EthereumAddress() | ||
if err != nil { | ||
jsonhttp.InternalServerError(w, "signer ethereum address") | ||
return | ||
} | ||
jsonhttp.Created(w, postEnvelopeResponse{ | ||
Issuer: issuer.Hex(), | ||
Index: hex.EncodeToString(stamp.Index()), | ||
Timestamp: hex.EncodeToString(stamp.Timestamp()), | ||
Signature: hex.EncodeToString(stamp.Sig()), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2024 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 api_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/ethersphere/bee/v2/pkg/api" | ||
"github.com/ethersphere/bee/v2/pkg/jsonhttp" | ||
"github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest" | ||
mockbatchstore "github.com/ethersphere/bee/v2/pkg/postage/batchstore/mock" | ||
mockpost "github.com/ethersphere/bee/v2/pkg/postage/mock" | ||
) | ||
|
||
func TestPostEnvelope(t *testing.T) { | ||
t.Parallel() | ||
|
||
zeroHex := "0000000000000000000000000000000000000000000000000000000000000000" | ||
envelopeEndpoint := func(chunkAddress string) string { return fmt.Sprintf("/envelope/%s", chunkAddress) } | ||
client, _, _, _ := newTestServer(t, testServerOptions{ | ||
Post: mockpost.New(mockpost.WithAcceptAll()), | ||
}) | ||
|
||
t.Run("ok", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
jsonhttptest.Request(t, client, http.MethodPost, envelopeEndpoint(zeroHex), http.StatusCreated, | ||
jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), | ||
) | ||
}) | ||
|
||
t.Run("wrong chunk address", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
jsonhttptest.Request(t, client, http.MethodPost, envelopeEndpoint("invalid"), http.StatusBadRequest, | ||
jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), | ||
) | ||
}) | ||
|
||
t.Run("postage does not exist", func(t *testing.T) { | ||
t.Parallel() | ||
client, _, _, _ := newTestServer(t, testServerOptions{}) | ||
|
||
jsonhttptest.Request(t, client, http.MethodPost, envelopeEndpoint(zeroHex), http.StatusNotFound, | ||
jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, zeroHex), | ||
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{Message: "batch with id not found", Code: http.StatusNotFound}), | ||
) | ||
}) | ||
|
||
t.Run("batch unusable", func(t *testing.T) { | ||
t.Parallel() | ||
client, _, _, _ := newTestServer(t, testServerOptions{ | ||
Post: mockpost.New(mockpost.WithAcceptAll()), | ||
BatchStore: mockbatchstore.New(), | ||
}) | ||
|
||
jsonhttptest.Request(t, client, http.MethodPost, envelopeEndpoint(zeroHex), http.StatusUnprocessableEntity, | ||
jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), | ||
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{Message: "batch not usable yet or does not exist", Code: http.StatusUnprocessableEntity}), | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters