Skip to content

Commit

Permalink
Add some useful enpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Jan 13, 2025
1 parent b363287 commit 1b1b29d
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cmd/api/admin_middleware.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
49 changes: 49 additions & 0 deletions cmd/api/handler/rollup_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/base64"
"net/http"

"github.com/celenium-io/celestia-indexer/cmd/api/handler/responses"
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/celenium-io/celestia-indexer/internal/storage/postgres"
enums "github.com/celenium-io/celestia-indexer/internal/storage/types"
Expand Down Expand Up @@ -303,3 +304,51 @@ func (handler RollupAuthHandler) deleteRollup(ctx context.Context, id uint64) er

return tx.Flush(ctx)
}

func (handler RollupAuthHandler) Unverified(c echo.Context) error {
rollups, err := handler.rollups.Unverified(c.Request().Context())
if err != nil {
return handleError(c, err, handler.rollups)
}

response := make([]responses.Rollup, len(rollups))
for i := range rollups {
response[i] = responses.NewRollup(&rollups[i])
}

return returnArray(c, response)
}

type verifyRollupRequest struct {
Id uint64 `param:"id" validate:"required,min=1"`
}

func (handler RollupAuthHandler) Verify(c echo.Context) error {
req, err := bindAndValidate[verifyRollupRequest](c)
if err != nil {
return badRequestError(c, err)
}

if err := handler.verify(c.Request().Context(), req.Id); err != nil {
return handleError(c, err, handler.address)
}

return success(c)
}

func (handler RollupAuthHandler) verify(ctx context.Context, id uint64) error {
tx, err := postgres.BeginTransaction(ctx, handler.tx)
if err != nil {
return err
}

err = tx.UpdateRollup(ctx, &storage.Rollup{
Id: id,
Verified: true,
})
if err != nil {
return tx.HandleError(ctx, err)
}

return tx.Flush(ctx)
}
2 changes: 2 additions & 0 deletions cmd/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ func initHandlers(ctx context.Context, e *echo.Echo, cfg Config, db postgres.Sto
rollup.POST("/new", rollupAuthHandler.Create, keyMiddleware)
rollup.PATCH("/:id", rollupAuthHandler.Update, keyMiddleware)
rollup.DELETE("/:id", rollupAuthHandler.Delete, keyMiddleware, adminMiddleware)
rollup.PATCH("/:id/verify", rollupAuthHandler.Verify, keyMiddleware, adminMiddleware)
rollup.GET("/unverified", rollupAuthHandler.Unverified, keyMiddleware, adminMiddleware)
}
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/api/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func TestRoutes(t *testing.T) {
"/v1/stats/changes_24h GET": {},
"/v1/rollup/count GET": {},
"/v1/auth/rollup/:id PATCH": {},
"/v1/auth/rollup/:id/verify PATCH": {},
"/v1/auth/rollup/unverified GET": {},
"/v1/address/:hash/undelegations GET": {},
"/v1/block/:height/messages GET": {},
"/v1/namespace/active GET": {},
Expand Down
39 changes: 39 additions & 0 deletions internal/storage/mock/rollup.go

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

8 changes: 8 additions & 0 deletions internal/storage/postgres/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,11 @@ func (r *Rollup) Tags(ctx context.Context) (arr []string, err error) {
Scan(ctx, &arr)
return
}

func (r *Rollup) Unverified(ctx context.Context) (rollups []storage.Rollup, err error) {
err = r.DB().NewSelect().
Model(&rollups).
Where("verified = false").
Scan(ctx)
return
}
9 changes: 9 additions & 0 deletions internal/storage/postgres/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,12 @@ func (s *StorageTestSuite) TestCount() {
s.Require().NoError(err)
s.Require().EqualValues(3, count)
}

func (s *StorageTestSuite) TestUnverified() {
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer ctxCancel()

rollups, err := s.storage.Rollup.Unverified(ctx)
s.Require().NoError(err)
s.Require().EqualValues(1, len(rollups))
}
2 changes: 1 addition & 1 deletion internal/storage/postgres/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ func (tx Transaction) SaveRollup(ctx context.Context, rollup *models.Rollup) err
}

func (tx Transaction) UpdateRollup(ctx context.Context, rollup *models.Rollup) error {
if rollup == nil || rollup.IsEmpty() {
if rollup == nil || (rollup.IsEmpty() && !rollup.Verified) {
return nil
}

Expand Down
1 change: 1 addition & 0 deletions internal/storage/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type IRollup interface {
BySlug(ctx context.Context, slug string) (RollupWithStats, error)
RollupStatsGrouping(ctx context.Context, fltrs RollupGroupStatsFilters) ([]RollupGroupedStats, error)
Tags(ctx context.Context) ([]string, error)
Unverified(ctx context.Context) (rollups []Rollup, err error)
}

// Rollup -
Expand Down

0 comments on commit 1b1b29d

Please sign in to comment.