Skip to content

Commit

Permalink
Feature: endpoint with merged rollup and bridge actions
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Sep 1, 2024
1 parent 9969cc2 commit 4e83d3b
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 0 deletions.
82 changes: 82 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.

82 changes: 82 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.

56 changes: 56 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.

82 changes: 82 additions & 0 deletions cmd/api/handler/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/celenium-io/astria-indexer/cmd/api/handler/responses"
"github.com/celenium-io/astria-indexer/internal/storage"
testsuite "github.com/celenium-io/astria-indexer/internal/test_suite"
"github.com/labstack/echo/v4"
)

Expand Down Expand Up @@ -298,3 +299,84 @@ func (handler *RollupHandler) Bridges(c echo.Context) error {

return returnArray(c, response)
}

type allRollupActionsRequest struct {
Hash string `param:"hash" validate:"required,base64url"`
Limit int `query:"limit" validate:"omitempty,min=1,max=100"`
Offset int `query:"offset" validate:"omitempty,min=0"`
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
RollupActions *bool `query:"rollup_actions" validate:"omitempty"`
BridgeActions *bool `query:"bridge_actions" validate:"omitempty"`
}

func (p *allRollupActionsRequest) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
if p.Sort == "" {
p.Sort = asc
}
if p.BridgeActions == nil {
p.BridgeActions = testsuite.Ptr(true)
}
if p.RollupActions == nil {
p.RollupActions = testsuite.Ptr(true)
}
}

func (p *allRollupActionsRequest) toDbRequest() storage.RollupAndBridgeActionsFilter {
return storage.RollupAndBridgeActionsFilter{
Limit: p.Limit,
Offset: p.Offset,
Sort: pgSort(p.Sort),
RollupActions: *p.RollupActions,
BridgeActions: *p.BridgeActions,
}
}

// AllActions godoc
//
// @Summary Get rollup actions with actions of all connected bridges
// @Description Get rollup actions with actions of all connected bridges
// @Tags rollup
// @ID rollup-all-actions
// @Param hash path string true "Base64Url encoded rollup id"
// @Param limit query integer false "Count of requested entities" minimum(1) maximum(100)
// @Param offset query integer false "Offset" minimum(1)
// @Param sort query string false "Sort order" Enums(asc, desc)
// @Param rollup_actions query boolean false "If true join rollup actions. Default: true"
// @Param bridge_actions query boolean false "If true join brigde actions. Default: true"
// @Produce json
// @Success 200 {array} responses.Action
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/rollup/{hash}/all_actions [get]
func (handler *RollupHandler) AllActions(c echo.Context) error {
req, err := bindAndValidate[allRollupActionsRequest](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()

hash, err := base64.URLEncoding.DecodeString(req.Hash)
if err != nil {
return badRequestError(c, err)
}

rollup, err := handler.rollups.ByHash(c.Request().Context(), hash)
if err != nil {
return handleError(c, err, handler.rollups)
}

actions, err := handler.actions.ByRollupAndBridge(c.Request().Context(), rollup.Id, req.toDbRequest())
if err != nil {
return handleError(c, err, handler.rollups)
}

response := make([]responses.Action, len(actions))
for i := range actions {
response[i] = responses.NewActionWithTx(actions[i])
}

return returnArray(c, response)
}
56 changes: 56 additions & 0 deletions cmd/api/handler/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,59 @@ func (s *RollupTestSuite) TestBridges() {
s.Require().Equal("nria", bridge.Asset)
s.Require().Equal("nria", bridge.FeeAsset)
}

func (s *RollupTestSuite) TestAllActions() {
q := make(url.Values)
q.Set("limit", "10")
q.Set("offset", "0")
q.Set("sort", "desc")
q.Set("rollup_actions", "false")

req := httptest.NewRequest(http.MethodGet, "/?"+q.Encode(), nil)
rec := httptest.NewRecorder()
c := s.echo.NewContext(req, rec)
c.SetPath("/rollup/:hash/all_actions")
c.SetParamNames("hash")
c.SetParamValues(testRollupURLHash)

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

s.actions.EXPECT().
ByRollupAndBridge(gomock.Any(), uint64(1), storage.RollupAndBridgeActionsFilter{
Limit: 10,
Offset: 0,
Sort: sdk.SortOrderDesc,
RollupActions: false,
BridgeActions: true,
}).
Return([]storage.ActionWithTx{
{
Action: storage.Action{
Data: map[string]any{},
Position: 1,
Type: types.ActionTypeSequence,
Id: 1,
Height: 100,
},
Tx: &testTx,
},
}, nil).
Times(1)

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

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

action := actions[0]
s.Require().EqualValues(1, action.Id)
s.Require().EqualValues(100, action.Height)
s.Require().EqualValues(1, action.Position)
s.Require().EqualValues(types.ActionTypeSequence, action.Type)
}
Loading

0 comments on commit 4e83d3b

Please sign in to comment.