Skip to content

Commit

Permalink
Refactoring: separate blob metadata endpoint (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Jun 9, 2024
1 parent a7c8e69 commit 25dca60
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 51 deletions.
63 changes: 60 additions & 3 deletions cmd/api/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 60 additions & 3 deletions cmd/api/docs/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 40 additions & 2 deletions cmd/api/docs/swagger.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 33 additions & 17 deletions cmd/api/handler/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ type postBlobRequest struct {
Hash string `json:"hash" validate:"required,namespace"`
Height types.Level `json:"height" validate:"required,min=1"`
Commitment string `json:"commitment" validate:"required,base64"`
Metadata bool `json:"metadata" validate:"omitempty"`
}

// Blob godoc
Expand Down Expand Up @@ -379,27 +378,44 @@ func (handler *NamespaceHandler) Blob(c echo.Context) error {
return handleError(c, err, handler.blobLogs)
}

if req.Metadata {
namespaceId, err := base64.StdEncoding.DecodeString(req.Hash)
if err != nil {
return handleError(c, err, handler.namespace)
}
return c.JSON(http.StatusOK, response)
}

ns, err := handler.namespace.ByNamespaceIdAndVersion(c.Request().Context(), namespaceId[1:], namespaceId[0])
if err != nil {
return handleError(c, err, handler.namespace)
}
// BlobMetadata godoc
//
// @Summary Get blob metadata by commitment on height
// @Description Returns blob metadata
// @Tags namespace
// @ID get-blob-metadata
// @Param hash body string true "Base64-encoded namespace id and version"
// @Param height body integer true "Block heigth" minimum(1)
// @Param commitment body string true "Blob commitment"
// @Accept json
// @Produce json
// @Success 200 {object} responses.BlobLog
// @Failure 400 {object} Error
// @Router /v1/blob/metadata [post]
func (handler *NamespaceHandler) BlobMetadata(c echo.Context) error {
req, err := bindAndValidate[postBlobRequest](c)
if err != nil {
return badRequestError(c, err)
}
namespaceId, err := base64.StdEncoding.DecodeString(req.Hash)
if err != nil {
return handleError(c, err, handler.namespace)
}

blobMetadata, err := handler.blobLogs.Blob(c.Request().Context(), req.Height, ns.Id, req.Commitment)
if err != nil {
return handleError(c, err, handler.namespace)
}
ns, err := handler.namespace.ByNamespaceIdAndVersion(c.Request().Context(), namespaceId[1:], namespaceId[0])
if err != nil {
return handleError(c, err, handler.namespace)
}

metadata := responses.NewBlobLog(blobMetadata)
response.Metadata = &metadata
blobMetadata, err := handler.blobLogs.Blob(c.Request().Context(), req.Height, ns.Id, req.Commitment)
if err != nil {
return handleError(c, err, handler.namespace)
}

return c.JSON(http.StatusOK, response)
return c.JSON(http.StatusOK, responses.NewBlobLog(blobMetadata))
}

type getBlobLogsForNamespace struct {
Expand Down
32 changes: 9 additions & 23 deletions cmd/api/handler/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,14 +562,13 @@ func (s *NamespaceTestSuite) TestRollups() {
s.Require().EqualValues("test-rollup", rollup.Slug)
}

func (s *NamespaceTestSuite) TestBlobWithMetadata() {
func (s *NamespaceTestSuite) TestBlobMetadata() {
commitment := "ZeKGjIwsIkFsACD0wtEh/jbzzW+zIPP716VihNpm9T1="

blobReq := map[string]any{
"hash": testNamespaceBase64,
"height": 1000,
"commitment": commitment,
"metadata": true,
}
stream := new(bytes.Buffer)
err := json.NewEncoder(stream).Encode(blobReq)
Expand All @@ -578,21 +577,14 @@ func (s *NamespaceTestSuite) TestBlobWithMetadata() {
req := httptest.NewRequest(http.MethodPost, "/", stream)
rec := httptest.NewRecorder()
c := s.echo.NewContext(req, rec)
c.SetPath("/blob")
c.SetPath("/blob/metadata")

req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)

data := make([]byte, 88)
_, err = rand.Read(data)
s.Require().NoError(err)

result := nodeTypes.Blob{
Namespace: testNamespaceBase64,
Data: base64.StdEncoding.EncodeToString(data),
Commitment: commitment,
ShareVersion: 0,
}

s.blobLogs.EXPECT().
Blob(gomock.Any(), pkgTypes.Level(1000), uint64(1), "ZeKGjIwsIkFsACD0wtEh/jbzzW+zIPP716VihNpm9T1=").
Return(storage.BlobLog{
Expand All @@ -607,6 +599,8 @@ func (s *NamespaceTestSuite) TestBlobWithMetadata() {
Size: 1000,
Height: 1000,
Time: testTime,
Tx: &testTx,
Namespace: &testNamespace,
}, nil).
Times(1)

Expand All @@ -615,21 +609,13 @@ func (s *NamespaceTestSuite) TestBlobWithMetadata() {
Return(testNamespace, nil).
Times(1)

s.blobReceiver.EXPECT().
Blob(gomock.Any(), pkgTypes.Level(1000), testNamespaceBase64, commitment).
Return(result, nil).
Times(1)

s.Require().NoError(s.handler.Blob(c))
s.Require().NoError(s.handler.BlobMetadata(c))
s.Require().Equal(http.StatusOK, rec.Code)

var blob responses.Blob
var blob responses.BlobLog
err = json.NewDecoder(rec.Body).Decode(&blob)
s.Require().NoError(err)

s.Require().EqualValues(0, blob.ShareVersion)
s.Require().Equal(testNamespaceBase64, blob.Namespace)
s.Require().Equal(result.Data, blob.Data)
s.Require().Equal(commitment, blob.Commitment)
s.Require().NotNil(blob.Metadata)
s.Require().NotNil(blob.Namespace)
s.Require().NotNil(blob.Tx)
s.Require().NotNil(blob.Signer)
}
2 changes: 0 additions & 2 deletions cmd/api/handler/responses/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type Blob struct {
ShareVersion int `example:"0" format:"integer" json:"share_version" swaggertype:"integer"`
Commitment string `example:"vbGakK59+Non81TE3ULg5Ve5ufT9SFm/bCyY+WLR3gg=" format:"base64" json:"commitment" swaggertype:"string"`
ContentType string `example:"image/png" format:"string" json:"content_type" swaggertype:"string"`

Metadata *BlobLog `json:"metadata,omitempty"`
}

func NewBlob(blob types.Blob) (Blob, error) {
Expand Down
7 changes: 6 additions & 1 deletion cmd/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,12 @@ func initHandlers(ctx context.Context, e *echo.Echo, cfg Config, db postgres.Sto
}

namespaceHandlers := handler.NewNamespaceHandler(db.Namespace, db.BlobLogs, db.Rollup, db.State, cfg.Indexer.Name, blobReceiver)
v1.POST("/blob", namespaceHandlers.Blob)

blobGroup := v1.Group("blob")
{
blobGroup.POST("", namespaceHandlers.Blob)
blobGroup.POST("/metadata", namespaceHandlers.BlobMetadata)
}

namespaceGroup := v1.Group("/namespace")
{
Expand Down

0 comments on commit 25dca60

Please sign in to comment.