Skip to content

Commit

Permalink
Refactoring: remove active namespaces endpoint (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Feb 12, 2025
1 parent e389e9c commit 70208f9
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 102 deletions.
43 changes: 3 additions & 40 deletions cmd/api/handler/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"context"
"encoding/base64"
"encoding/hex"
"net/http"
"time"

"github.com/celestiaorg/celestia-app/v3/pkg/appconsts"
"github.com/celestiaorg/celestia-app/v3/pkg/da"
"github.com/celestiaorg/go-square/v2/share"
"net/http"
"time"

"github.com/celenium-io/celestia-indexer/pkg/types"
"github.com/celestiaorg/go-square/v2"
sdk "github.com/dipdup-net/indexer-sdk/pkg/storage"

"github.com/celenium-io/celestia-indexer/cmd/api/handler/responses"
"github.com/celenium-io/celestia-indexer/internal/storage"
Expand Down Expand Up @@ -301,43 +301,6 @@ func (handler *NamespaceHandler) GetMessages(c echo.Context) error {
return returnArray(c, response)
}

type getActiveRequest struct {
Sort string `query:"sort" validate:"omitempty,oneof=time pfb_count size"`
}

// GetActive godoc
//
// @Summary Get last used namespace
// @Description Get last used namespace
// @Tags namespace
// @ID get-namespace-active
// @Param sort query string false "Sort field. Default: time" Enums(time,pfb_count,size)
// @Produce json
// @Success 200 {array} responses.Namespace
// @Failure 500 {object} Error
// @Router /namespace/active [get]
func (handler *NamespaceHandler) GetActive(c echo.Context) error {
req, err := bindAndValidate[getActiveRequest](c)
if err != nil {
return badRequestError(c, err)
}

if req.Sort == "" {
req.Sort = "time"
}

active, err := handler.namespace.ListWithSort(c.Request().Context(), req.Sort, sdk.SortOrderDesc, 5, 0)
if err != nil {
return handleError(c, err, handler.namespace)
}

response := make([]responses.Namespace, len(active))
for i := range response {
response[i] = responses.NewNamespace(active[i])
}
return returnArray(c, response)
}

// Count godoc
//
// @Summary Get count of namespaces in network
Expand Down
60 changes: 0 additions & 60 deletions cmd/api/handler/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,66 +442,6 @@ func (s *NamespaceTestSuite) TestCount() {
s.Require().EqualValues(123, count)
}

func (s *NamespaceTestSuite) TestGetActive() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := s.echo.NewContext(req, rec)
c.SetPath("/namespace/active")

s.namespaces.EXPECT().
ListWithSort(gomock.Any(), "time", sdk.SortOrderDesc, 5, 0).
Return([]storage.Namespace{
testNamespace,
}, nil)

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

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

namespace := ns[0]
s.Require().Equal("0000000000000000000000000000000000000000fc7443b155920156", namespace.NamespaceID)
s.Require().EqualValues(100, namespace.LastHeight)
s.Require().EqualValues(100, namespace.Size)
s.Require().Equal(testTime, namespace.LastMessageTime)
}

func (s *NamespaceTestSuite) TestGetActiveWithSort() {
for _, field := range []string{"pfb_count", "time", "size"} {
q := make(url.Values)
q.Set("sort", field)

req := httptest.NewRequest(http.MethodGet, "/?"+q.Encode(), nil)
rec := httptest.NewRecorder()
c := s.echo.NewContext(req, rec)
c.SetPath("/namespace/active")

s.namespaces.EXPECT().
ListWithSort(gomock.Any(), field, sdk.SortOrderDesc, 5, 0).
Return([]storage.Namespace{
testNamespace,
}, nil)

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

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

namespace := ns[0]
s.Require().Equal("0000000000000000000000000000000000000000fc7443b155920156", namespace.NamespaceID)
s.Require().EqualValues(100, namespace.LastHeight)
s.Require().EqualValues(100, namespace.Size)
s.Require().Equal(testTime, namespace.LastMessageTime)

}
}

func (s *NamespaceTestSuite) TestBlob() {
commitment := "ZeKGjIwsIkFsACD0wtEh/jbzzW+zIPP716VihNpm9T0="

Expand Down
1 change: 0 additions & 1 deletion cmd/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ func initHandlers(ctx context.Context, e *echo.Echo, cfg Config, db postgres.Sto
{
namespaceGroup.GET("", namespaceHandlers.List)
namespaceGroup.GET("/count", namespaceHandlers.Count)
namespaceGroup.GET("/active", namespaceHandlers.GetActive)
namespaceGroup.GET("/:id", namespaceHandlers.Get)
namespaceGroup.GET("/:id/:version", namespaceHandlers.GetWithVersion)
namespaceGroup.GET("/:id/:version/messages", namespaceHandlers.GetMessages)
Expand Down
1 change: 0 additions & 1 deletion cmd/api/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func TestRoutes(t *testing.T) {
"/v1/auth/rollup/unverified GET": {},
"/v1/address/:hash/undelegations GET": {},
"/v1/block/:height/messages GET": {},
"/v1/namespace/active GET": {},
"/v1/namespace_by_hash/:hash GET": {},
"/v1/vesting/:id/periods GET": {},
"/v1/constants GET": {},
Expand Down

0 comments on commit 70208f9

Please sign in to comment.