Skip to content

Commit

Permalink
Feature: add application to native rollup (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Dec 4, 2024
1 parent e0003da commit 021877c
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cmd/api/handler/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var (
Slug: "test-app",
Category: types.AppCategoryNft,
}
testRollupWithStats = storage.AppWithStats{
testAppWithStats = storage.AppWithStats{
App: testApplication,
AppStats: storage.AppStats{
ActionsCount: 100,
Expand Down Expand Up @@ -98,7 +98,7 @@ func (s *AppTestSuite) TestLeaderboard() {
types.AppCategoryGaming,
},
}).
Return([]storage.AppWithStats{testRollupWithStats}, nil).
Return([]storage.AppWithStats{testAppWithStats}, nil).
Times(1)

s.Require().NoError(s.handler.Leaderboard(c))
Expand Down Expand Up @@ -131,7 +131,7 @@ func (s *AppTestSuite) TestGet() {

s.apps.EXPECT().
BySlug(gomock.Any(), "test-app").
Return(testRollupWithStats, nil).
Return(testAppWithStats, nil).
Times(1)

s.Require().NoError(s.handler.Get(c))
Expand Down
2 changes: 2 additions & 0 deletions cmd/api/handler/responses/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Rollup struct {
ActionsCount int64 `example:"101" json:"actions_count" swaggertype:"integer"`
BridgeCount int64 `example:"2" json:"bridge_count" swaggertype:"integer"`
Size int64 `example:"100" json:"size" swaggertype:"integer"`

App *AppWithStats `json:"app,omitempty"`
}

func NewRollup(rollup *storage.Rollup) Rollup {
Expand Down
16 changes: 15 additions & 1 deletion cmd/api/handler/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type RollupHandler struct {
actions storage.IAction
bridge storage.IBridge
deposits storage.IDeposit
app storage.IApp
state storage.IState
indexerName string
}
Expand All @@ -32,6 +33,7 @@ func NewRollupHandler(
actions storage.IAction,
bridge storage.IBridge,
deposits storage.IDeposit,
app storage.IApp,
state storage.IState,
indexerName string,
) *RollupHandler {
Expand All @@ -41,6 +43,7 @@ func NewRollupHandler(
actions: actions,
bridge: bridge,
deposits: deposits,
app: app,
state: state,
indexerName: indexerName,
}
Expand Down Expand Up @@ -79,7 +82,18 @@ func (handler *RollupHandler) Get(c echo.Context) error {
return handleError(c, err, handler.rollups)
}

return c.JSON(http.StatusOK, responses.NewRollup(&rollup))
response := responses.NewRollup(&rollup)

if app, err := handler.app.ByRollupId(c.Request().Context(), rollup.Id); err != nil {
if !handler.app.IsNoRows(err) {
return handleError(c, err, handler.rollups)
}
} else {
appResp := responses.NewAppWithStats(app)
response.App = &appResp
}

return c.JSON(http.StatusOK, response)
}

type listRollupsRequest struct {
Expand Down
49 changes: 48 additions & 1 deletion cmd/api/handler/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package handler

import (
"context"
"database/sql"
"encoding/json"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -32,6 +33,7 @@ type RollupTestSuite struct {
bridge *mock.MockIBridge
deposits *mock.MockIDeposit
state *mock.MockIState
app *mock.MockIApp
echo *echo.Echo
handler *RollupHandler
ctrl *gomock.Controller
Expand All @@ -46,9 +48,10 @@ func (s *RollupTestSuite) SetupSuite() {
s.actions = mock.NewMockIAction(s.ctrl)
s.bridge = mock.NewMockIBridge(s.ctrl)
s.deposits = mock.NewMockIDeposit(s.ctrl)
s.app = mock.NewMockIApp(s.ctrl)
s.state = mock.NewMockIState(s.ctrl)
cc := cache.NewConstantsCache(nil)
s.handler = NewRollupHandler(cc, s.rollups, s.actions, s.bridge, s.deposits, s.state, testIndexerName)
s.handler = NewRollupHandler(cc, s.rollups, s.actions, s.bridge, s.deposits, s.app, s.state, testIndexerName)
}

// TearDownSuite -
Expand All @@ -74,6 +77,49 @@ func (s *RollupTestSuite) TestGet() {
Return(testRollup, nil).
Times(1)

s.app.EXPECT().
ByRollupId(gomock.Any(), testRollup.Id).
Return(storage.AppWithStats{}, sql.ErrNoRows).
Times(1)

s.app.EXPECT().
IsNoRows(sql.ErrNoRows).
Return(true).
Times(1)

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

var rollup responses.Rollup
err := json.NewDecoder(rec.Body).Decode(&rollup)
s.Require().NoError(err)
s.Require().EqualValues(1, rollup.Id)
s.Require().EqualValues(1, rollup.ActionsCount)
s.Require().EqualValues(1, rollup.BridgeCount)
s.Require().EqualValues(100, rollup.FirstHeight)
s.Require().EqualValues(10, rollup.Size)
s.Require().Equal(testRollup.AstriaId, rollup.AstriaId)
s.Require().Nil(rollup.App)
}

func (s *RollupTestSuite) TestGetWithApp() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := s.echo.NewContext(req, rec)
c.SetPath("/rollup/:hash")
c.SetParamNames("hash")
c.SetParamValues(testRollupURLHash)

s.rollups.EXPECT().
ByHash(gomock.Any(), testRollup.AstriaId).
Return(testRollup, nil).
Times(1)

s.app.EXPECT().
ByRollupId(gomock.Any(), testRollup.Id).
Return(testAppWithStats, nil).
Times(1)

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

Expand All @@ -86,6 +132,7 @@ func (s *RollupTestSuite) TestGet() {
s.Require().EqualValues(100, rollup.FirstHeight)
s.Require().EqualValues(10, rollup.Size)
s.Require().Equal(testRollup.AstriaId, rollup.AstriaId)
s.Require().NotNil(rollup.App)
}

func (s *RollupTestSuite) TestGetInvalidAddress() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func initHandlers(ctx context.Context, e *echo.Echo, cfg Config, db postgres.Sto
}
}

rollupsHandler := handler.NewRollupHandler(constantCache, db.Rollup, db.Action, db.Bridges, db.Deposit, db.State, cfg.Indexer.Name)
rollupsHandler := handler.NewRollupHandler(constantCache, db.Rollup, db.Action, db.Bridges, db.Deposit, db.App, db.State, cfg.Indexer.Name)
rollupsGroup := v1.Group("/rollup")
{
rollupsGroup.GET("", rollupsHandler.List)
Expand Down
1 change: 1 addition & 0 deletions internal/storage/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type IApp interface {

Leaderboard(ctx context.Context, fltrs LeaderboardFilters) ([]AppWithStats, error)
BySlug(ctx context.Context, slug string) (AppWithStats, error)
ByRollupId(ctx context.Context, rollupId uint64) (AppWithStats, error)
}

type App struct {
Expand Down
39 changes: 39 additions & 0 deletions internal/storage/mock/app.go

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

12 changes: 12 additions & 0 deletions internal/storage/postgres/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,15 @@ func (app *App) BySlug(ctx context.Context, slug string) (result storage.AppWith
Scan(ctx, &result)
return
}

func (app *App) ByRollupId(ctx context.Context, rollupId uint64) (result storage.AppWithStats, err error) {
err = app.DB().NewSelect().
Table(storage.ViewLeaderboard).
ColumnExpr("leaderboard.*").
ColumnExpr("address.hash as bridge__hash").
Where("rollup_id = ?", rollupId).
Join("left join address on native_bridge_id = address.id").
Limit(1).
Scan(ctx, &result)
return
}
20 changes: 20 additions & 0 deletions internal/storage/postgres/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,23 @@ func (s *StorageTestSuite) TestAppBySlug() {
s.Require().NotNil(app.Rollup)
s.Require().EqualValues("19ba8abb3e4b56a309df6756c47b97e298e3a72d88449d36a0fadb1ca7366539", hex.EncodeToString(app.Rollup.AstriaId))
}

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

_, err := s.storage.Connection().Exec(ctx, "REFRESH MATERIALIZED VIEW leaderboard;")
s.Require().NoError(err)

app, err := s.storage.App.ByRollupId(ctx, 1)
s.Require().NoError(err)

s.Require().EqualValues("App 1", app.Name)
s.Require().EqualValues(34, app.Size)
s.Require().EqualValues(1, app.ActionsCount)
s.Require().False(app.LastActionTime.IsZero())
s.Require().False(app.FirstActionTime.IsZero())
s.Require().NotNil(app.Bridge)
s.Require().EqualValues("astria1lm45urgugesyhaymn68xww0m6g49zreqa32w7p", app.Bridge.Hash)
s.Require().Nil(app.Rollup)
}

0 comments on commit 021877c

Please sign in to comment.