Skip to content

Commit

Permalink
Feature: add commitment filter to blob request (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Jun 17, 2024
1 parent c9ff961 commit cbcbd0e
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 28 deletions.
18 changes: 18 additions & 0 deletions cmd/api/docs/docs.go

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

18 changes: 18 additions & 0 deletions cmd/api/docs/swagger.json

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

12 changes: 12 additions & 0 deletions cmd/api/docs/swagger.yaml

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

16 changes: 14 additions & 2 deletions cmd/api/handler/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package handler

import (
"net/http"
"time"

"github.com/celenium-io/celestia-indexer/pkg/node"
"github.com/celenium-io/celestia-indexer/pkg/types"
Expand Down Expand Up @@ -404,6 +405,14 @@ func (handler *BlockHandler) Blobs(c echo.Context) error {
}
req.SetDefault()

blockTime, err := handler.block.Time(c.Request().Context(), req.Height)
if err != nil {
if handler.block.IsNoRows(err) {
return returnArray(c, []any{})
}
return handleError(c, err, handler.block)
}

blobs, err := handler.blobLogs.ByHeight(
c.Request().Context(),
req.Height,
Expand All @@ -412,6 +421,9 @@ func (handler *BlockHandler) Blobs(c echo.Context) error {
Offset: int(req.Offset),
Sort: pgSort(req.Sort),
SortBy: req.SortBy,
// using time filters to take certain partition
From: blockTime,
To: blockTime.Add(time.Minute),
},
)
if err != nil {
Expand Down Expand Up @@ -469,12 +481,12 @@ func (handler *BlockHandler) BlockODS(c echo.Context) error {
return badRequestError(c, err)
}

b, err := handler.block.ByHeightWithStats(c.Request().Context(), req.Height)
blockStats, err := handler.blockStats.ByHeight(c.Request().Context(), req.Height)
if err != nil {
return handleError(c, err, handler.block)
}

if b.Stats.TxCount == 0 {
if blockStats.TxCount == 0 {
return c.JSON(http.StatusOK, responses.ODS{
Width: 0,
Items: make([]responses.ODSItem, 0),
Expand Down
11 changes: 8 additions & 3 deletions cmd/api/handler/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,11 @@ func (s *BlockTestSuite) TestBlobs() {
c.SetParamNames("height")
c.SetParamValues("100")

s.blocks.EXPECT().
Time(gomock.Any(), pkgTypes.Level(100)).
Return(testBlock.Time, nil).
Times(1)

s.blobLogs.EXPECT().
ByHeight(gomock.Any(), pkgTypes.Level(100), gomock.Any()).
Return([]storage.BlobLog{
Expand Down Expand Up @@ -590,9 +595,9 @@ func (s *BlockTestSuite) TestBlockODS() {
c.SetParamNames("height")
c.SetParamValues("100")

s.blocks.EXPECT().
ByHeightWithStats(gomock.Any(), pkgTypes.Level(100)).
Return(testBlockWithStats, nil).
s.blockStats.EXPECT().
ByHeight(gomock.Any(), pkgTypes.Level(100)).
Return(testBlockStats, nil).
Times(1)

rawTxs := []string{
Expand Down
70 changes: 52 additions & 18 deletions cmd/api/handler/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/base64"
"encoding/hex"
"net/http"
"time"

"github.com/celenium-io/celestia-indexer/pkg/types"
sdk "github.com/dipdup-net/indexer-sdk/pkg/storage"
Expand Down Expand Up @@ -419,12 +420,27 @@ func (handler *NamespaceHandler) BlobMetadata(c echo.Context) error {
}

type getBlobLogsForNamespace struct {
Id string `param:"id" validate:"required,hexadecimal,len=56"`
Version byte `param:"version"`
Limit uint64 `query:"limit" validate:"omitempty,min=1,max=100"`
Offset uint64 `query:"offset" validate:"omitempty,min=0"`
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
SortBy string `query:"sort_by" validate:"omitempty,oneof=time size"`
Id string `param:"id" validate:"required,hexadecimal,len=56"`
Version byte `param:"version"`
Limit uint64 `query:"limit" validate:"omitempty,min=1,max=100"`
Offset uint64 `query:"offset" validate:"omitempty,min=0"`
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
SortBy string `query:"sort_by" validate:"omitempty,oneof=time size"`
Commitment string `query:"commitment" validate:"omitempty,base64url"`

From int64 `example:"1692892095" query:"from" swaggertype:"integer" validate:"omitempty,min=1"`
To int64 `example:"1692892095" query:"to" swaggertype:"integer" validate:"omitempty,min=1"`
}

func (req getBlobLogsForNamespace) getCommitment() (string, error) {
if req.Commitment == "" {
return "", nil
}
data, err := base64.URLEncoding.DecodeString(req.Commitment)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(data), nil
}

func (req *getBlobLogsForNamespace) SetDefault() {
Expand All @@ -442,12 +458,15 @@ func (req *getBlobLogsForNamespace) SetDefault() {
// @Description Returns blob changes for namespace
// @Tags namespace
// @ID get-blob-logs
// @Param id path string true "Namespace id in hexadecimal" minlength(56) maxlength(56)
// @Param version path integer true "Version of namespace"
// @Param limit query integer false "Count of requested entities" mininum(1) maximum(100)
// @Param offset query integer false "Offset" mininum(1)
// @Param sort query string false "Sort order. Default: desc" Enums(asc, desc)
// @Param sort_by query string false "Sort field. If it's empty internal id is used" Enums(time, size)
// @Param id path string true "Namespace id in hexadecimal" minlength(56) maxlength(56)
// @Param version path integer true "Version of namespace"
// @Param limit query integer false "Count of requested entities" mininum(1) maximum(100)
// @Param offset query integer false "Offset" mininum(1)
// @Param sort query string false "Sort order. Default: desc" Enums(asc, desc)
// @Param sort_by query string false "Sort field. If it's empty internal id is used" Enums(time, size)
// @Param commitment query string false "Commitment value in URLbase64 format"
// @Param from query integer false "Time from in unix timestamp" mininum(1)
// @Param to query integer false "Time to in unix timestamp" mininum(1)
// @Produce json
// @Success 200 {array} responses.BlobLog
// @Failure 400 {object} Error
Expand All @@ -460,6 +479,11 @@ func (handler *NamespaceHandler) GetBlobLogs(c echo.Context) error {
}
req.SetDefault()

cm, err := req.getCommitment()
if err != nil {
return badRequestError(c, err)
}

namespaceId, err := hex.DecodeString(req.Id)
if err != nil {
return badRequestError(c, err)
Expand All @@ -470,15 +494,25 @@ func (handler *NamespaceHandler) GetBlobLogs(c echo.Context) error {
return handleError(c, err, handler.namespace)
}

fltrs := storage.BlobLogFilters{
Limit: int(req.Limit),
Offset: int(req.Offset),
Sort: pgSort(req.Sort),
SortBy: req.SortBy,
Commitment: cm,
}

if req.From > 0 {
fltrs.From = time.Unix(req.From, 0).UTC()
}
if req.To > 0 {
fltrs.To = time.Unix(req.To, 0).UTC()
}

logs, err := handler.blobLogs.ByNamespace(
c.Request().Context(),
ns.Id,
storage.BlobLogFilters{
Limit: int(req.Limit),
Offset: int(req.Offset),
Sort: pgSort(req.Sort),
SortBy: req.SortBy,
},
fltrs,
)
if err != nil {
return handleError(c, err, handler.namespace)
Expand Down
55 changes: 55 additions & 0 deletions cmd/api/handler/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,61 @@ func (s *NamespaceTestSuite) TestGetLogs() {
s.Require().Nil(l.Namespace)
}

func (s *NamespaceTestSuite) TestGetLogsWithCommitment() {
cm := "T1EPYi3jq6hC3ueLOZRtWB7LUsAC4DcnAX_oSwDopps="
args := make(url.Values)
args.Set("commitment", cm)

req := httptest.NewRequest(http.MethodGet, "/?"+args.Encode(), nil)
rec := httptest.NewRecorder()
c := s.echo.NewContext(req, rec)
c.SetPath("/namespace/:id/:version/logs")
c.SetParamNames("id", "version")
c.SetParamValues(testNamespaceId, "0")

s.namespaces.EXPECT().
ByNamespaceIdAndVersion(gomock.Any(), testNamespace.NamespaceID, byte(0)).
Return(testNamespace, nil)

s.blobLogs.EXPECT().
ByNamespace(gomock.Any(), testNamespace.Id, storage.BlobLogFilters{
Limit: 10,
Sort: "desc",
Commitment: "T1EPYi3jq6hC3ueLOZRtWB7LUsAC4DcnAX/oSwDopps=",
}).
Return([]storage.BlobLog{
{
NamespaceId: testNamespace.Id,
MsgId: 1,
TxId: 1,
SignerId: 1,
Signer: &storage.Address{
Address: testAddress,
},
Commitment: "T1EPYi3jq6hC3ueLOZRtWB7LUsAC4DcnAX/oSwDopps=",
Size: 1000,
Height: 10000,
Time: testTime,
},
}, nil)

s.Require().NoError(s.handler.GetBlobLogs(c))
s.Require().Equal(http.StatusOK, rec.Code, rec.Body.String())

var logs []responses.BlobLog
err := json.NewDecoder(rec.Body).Decode(&logs)
s.Require().NoError(err)
s.Require().Len(logs, 1)

l := logs[0]
s.Require().EqualValues(10000, l.Height)
s.Require().Equal(testTime, l.Time)
s.Require().Equal(testAddress, l.Signer)
s.Require().Equal("T1EPYi3jq6hC3ueLOZRtWB7LUsAC4DcnAX/oSwDopps=", l.Commitment)
s.Require().EqualValues(1000, l.Size)
s.Require().Nil(l.Namespace)
}

func (s *NamespaceTestSuite) TestRollups() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
Expand Down
3 changes: 3 additions & 0 deletions cmd/api/routes_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: 2024 PK Lab AG <[email protected]>
// SPDX-License-Identifier: MIT

package main

import (
Expand Down
11 changes: 7 additions & 4 deletions internal/storage/blob_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ import (
)

type BlobLogFilters struct {
Limit int
Offset int
Sort sdk.SortOrder
SortBy string
Limit int
Offset int
Sort sdk.SortOrder
SortBy string
From time.Time
To time.Time
Commitment string
}

//go:generate mockgen -source=$GOFILE -destination=mock/$GOFILE -package=mock -typed
Expand Down
Loading

0 comments on commit cbcbd0e

Please sign in to comment.