Skip to content

Commit

Permalink
Refactoring: change rollup id to base64url (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored May 17, 2024
1 parent 8345052 commit 15a6c1d
Show file tree
Hide file tree
Showing 31 changed files with 112 additions and 97 deletions.
21 changes: 9 additions & 12 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: 6 additions & 12 deletions cmd/api/docs/swagger.json

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

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

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

2 changes: 1 addition & 1 deletion cmd/api/handler/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,5 +348,5 @@ func (s *AddressTestSuite) TestRollups() {
s.Require().EqualValues(1, rollup.ActionsCount)
s.Require().EqualValues(100, rollup.FirstHeight)
s.Require().EqualValues(10, rollup.Size)
s.Require().Equal(testRollupHash, rollup.AstriaId)
s.Require().Equal(testRollup.AstriaId, rollup.AstriaId)
}
4 changes: 3 additions & 1 deletion cmd/api/handler/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package handler
import (
"context"
"database/sql"
"encoding/base64"
"encoding/hex"
"encoding/json"
"net/http"
Expand Down Expand Up @@ -80,7 +81,8 @@ var (
ActionsCount: 1,
Size: 10,
}
testRollupHash = hex.EncodeToString(testRollup.AstriaId)
testRollupURLHash = base64.URLEncoding.EncodeToString(testRollup.AstriaId)
testRollupHash = base64.StdEncoding.EncodeToString(testRollup.AstriaId)

testRollupAction = storage.RollupAction{
Action: &storage.Action{
Expand Down
16 changes: 7 additions & 9 deletions cmd/api/handler/responses/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@
package responses

import (
"encoding/hex"

"github.com/celenium-io/astria-indexer/internal/storage"
"github.com/celenium-io/astria-indexer/pkg/types"
)

type Rollup struct {
Id uint64 `example:"321" json:"id" swaggertype:"integer"`
FirstHeight types.Level `example:"100" json:"first_height" swaggertype:"integer"`
AstriaId string `example:"19ba8abb3e4b56a309df6756c47b97e298e3a72d88449d36a0fadb1ca7366539" json:"hash" swaggertype:"string"`
ActionsCount int64 `example:"100" json:"actions_count" swaggertype:"integer"`
Size int64 `example:"100" json:"size" swaggertype:"integer"`
BridgeAddress string `example:"115F94D8C98FFD73FE65182611140F0EDC7C3C94" json:"bridge_address,omitempty" swaggertype:"string"`
Id uint64 `example:"321" json:"id" swaggertype:"integer"`
FirstHeight types.Level `example:"100" json:"first_height" swaggertype:"integer"`
AstriaId []byte `example:"O0Ia+lPYYMf3iFfxBaWXCSdlhphc6d4ZoBXINov6Tjc=" json:"hash" swaggertype:"string"`
ActionsCount int64 `example:"101" json:"actions_count" swaggertype:"integer"`
Size int64 `example:"100" json:"size" swaggertype:"integer"`
BridgeAddress string `example:"115F94D8C98FFD73FE65182611140F0EDC7C3C94" json:"bridge_address,omitempty" swaggertype:"string"`
}

func NewRollup(rollup *storage.Rollup) Rollup {
r := Rollup{
Id: rollup.Id,
AstriaId: hex.EncodeToString(rollup.AstriaId),
AstriaId: rollup.AstriaId,
FirstHeight: rollup.FirstHeight,
ActionsCount: rollup.ActionsCount,
Size: rollup.Size,
Expand Down
18 changes: 9 additions & 9 deletions cmd/api/handler/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package handler

import (
"encoding/hex"
"encoding/base64"
"net/http"

"github.com/celenium-io/astria-indexer/cmd/api/handler/responses"
Expand Down Expand Up @@ -34,7 +34,7 @@ func NewRollupHandler(
}

type getRollupRequest struct {
Hash string `param:"hash" validate:"required,rollup_id"`
Hash string `param:"hash" validate:"required,base64url"`
}

// Get godoc
Expand All @@ -43,7 +43,7 @@ type getRollupRequest struct {
// @Description Get rollup info
// @Tags rollup
// @ID get-rollup
// @Param hash path string true "Hash" minlength(64) maxlength(64)
// @Param hash path string true "Base64Url encoded rollup id"
// @Produce json
// @Success 200 {object} responses.Rollup
// @Success 204
Expand All @@ -56,9 +56,9 @@ func (handler *RollupHandler) Get(c echo.Context) error {
return badRequestError(c, err)
}

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

rollup, err := handler.rollups.ByHash(c.Request().Context(), hash)
Expand Down Expand Up @@ -127,7 +127,7 @@ func (handler *RollupHandler) List(c echo.Context) error {
}

type getRollupList struct {
Hash string `param:"hash" validate:"required,rollup_id"`
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"`
Expand All @@ -148,7 +148,7 @@ func (p *getRollupList) SetDefault() {
// @Description Get rollup actions
// @Tags rollup
// @ID rollup-actions
// @Param hash path string true "Hash" minlength(48) maxlength(48)
// @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)
Expand All @@ -164,7 +164,7 @@ func (handler *RollupHandler) Actions(c echo.Context) error {
}
req.SetDefault()

hash, err := hex.DecodeString(req.Hash)
hash, err := base64.URLEncoding.DecodeString(req.Hash)
if err != nil {
return badRequestError(c, err)
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func (handler *RollupHandler) Addresses(c echo.Context) error {
}
req.SetDefault()

hash, err := hex.DecodeString(req.Hash)
hash, err := base64.URLEncoding.DecodeString(req.Hash)
if err != nil {
return badRequestError(c, err)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/api/handler/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *RollupTestSuite) TestGet() {
c := s.echo.NewContext(req, rec)
c.SetPath("/rollup/:hash")
c.SetParamNames("hash")
c.SetParamValues(testRollupHash)
c.SetParamValues(testRollupURLHash)

s.rollups.EXPECT().
ByHash(gomock.Any(), testRollup.AstriaId).
Expand All @@ -76,7 +76,7 @@ func (s *RollupTestSuite) TestGet() {
s.Require().EqualValues(1, rollup.ActionsCount)
s.Require().EqualValues(100, rollup.FirstHeight)
s.Require().EqualValues(10, rollup.Size)
s.Require().Equal(testRollupHash, rollup.AstriaId)
s.Require().Equal(testRollup.AstriaId, rollup.AstriaId)
}

func (s *RollupTestSuite) TestGetInvalidAddress() {
Expand Down Expand Up @@ -126,7 +126,7 @@ func (s *RollupTestSuite) TestList() {
s.Require().EqualValues(1, rollup.ActionsCount)
s.Require().EqualValues(100, rollup.FirstHeight)
s.Require().EqualValues(10, rollup.Size)
s.Require().Equal(testRollupHash, rollup.AstriaId)
s.Require().Equal(testRollup.AstriaId, rollup.AstriaId)
}

func (s *RollupTestSuite) TestActions() {
Expand All @@ -140,7 +140,7 @@ func (s *RollupTestSuite) TestActions() {
c := s.echo.NewContext(req, rec)
c.SetPath("/rollup/:hash/actions")
c.SetParamNames("hash")
c.SetParamValues(testRollupHash)
c.SetParamValues(testRollupURLHash)

s.rollups.EXPECT().
ByHash(gomock.Any(), testRollup.AstriaId).
Expand Down Expand Up @@ -215,7 +215,7 @@ func (s *RollupTestSuite) TestAddresses() {
c := s.echo.NewContext(req, rec)
c.SetPath("/rollup/:hash/addresses")
c.SetParamNames("hash")
c.SetParamValues(testRollupHash)
c.SetParamValues(testRollupURLHash)

s.rollups.EXPECT().
ByHash(gomock.Any(), testRollup.AstriaId).
Expand Down
Loading

0 comments on commit 15a6c1d

Please sign in to comment.