Skip to content

Commit

Permalink
Merge branch 'master' into feat/tvl
Browse files Browse the repository at this point in the history
  • Loading branch information
mismirnov committed Jan 17, 2025
2 parents 809eb21 + 53da19b commit 8026bb7
Show file tree
Hide file tree
Showing 54 changed files with 950 additions and 461 deletions.
2 changes: 1 addition & 1 deletion build/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ---------------------------------------------------------------------
# The first stage container, for building the application
# ---------------------------------------------------------------------
FROM golang:1.23.2-alpine as builder
FROM golang:1.23.2-alpine AS builder

ENV CGO_ENABLED=0
ENV GO111MODULE=on
Expand Down
2 changes: 1 addition & 1 deletion build/indexer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ---------------------------------------------------------------------
# The first stage container, for building the application
# ---------------------------------------------------------------------
FROM golang:1.23.2-alpine as builder
FROM golang:1.23.2-alpine AS builder

ENV CGO_ENABLED=0
ENV GO111MODULE=on
Expand Down
2 changes: 1 addition & 1 deletion build/quotes/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ---------------------------------------------------------------------
# The first stage container, for building the application
# ---------------------------------------------------------------------
FROM golang:1.23.2-alpine as builder
FROM golang:1.23.2-alpine AS builder

ENV CGO_ENABLED=0
ENV GO111MODULE=on
Expand Down
34 changes: 34 additions & 0 deletions cmd/api/admin_middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: 2024 PK Lab AG <[email protected]>
// SPDX-License-Identifier: MIT

package main

import (
"net/http"

"github.com/celenium-io/celestia-indexer/cmd/api/handler"
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/labstack/echo/v4"
)

var accessDeniedErr = echo.Map{
"error": "access denied",
}

func AdminMiddleware() echo.MiddlewareFunc {
return checkOnAdminPermission
}

func checkOnAdminPermission(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
val := ctx.Get(handler.ApiKeyName)
apiKey, ok := val.(storage.ApiKey)
if !ok {
return ctx.JSON(http.StatusForbidden, accessDeniedErr)
}
if !apiKey.Admin {
return ctx.JSON(http.StatusForbidden, accessDeniedErr)
}
return next(ctx)
}
}
22 changes: 11 additions & 11 deletions cmd/api/bus/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package bus

import (
"context"
"strconv"
"sync"

"github.com/celenium-io/celestia-indexer/internal/storage"
Expand All @@ -18,7 +17,6 @@ import (

type Dispatcher struct {
listener storage.Listener
blocks storage.IBlock
validators storage.IValidator

mx *sync.RWMutex
Expand All @@ -29,7 +27,6 @@ type Dispatcher struct {

func NewDispatcher(
factory storage.ListenerFactory,
blocks storage.IBlock,
validators storage.IValidator,
) (*Dispatcher, error) {
if factory == nil {
Expand All @@ -38,7 +35,6 @@ func NewDispatcher(
listener := factory.CreateListener()
return &Dispatcher{
listener: listener,
blocks: blocks,
validators: validators,
observers: make([]*Observer, 0),
mx: new(sync.RWMutex),
Expand Down Expand Up @@ -111,18 +107,22 @@ func (d *Dispatcher) handleNotification(ctx context.Context, notification *pq.No
}

func (d *Dispatcher) handleBlock(ctx context.Context, payload string) error {
id, err := strconv.ParseUint(payload, 10, 64)
if err != nil {
return errors.Wrapf(err, "parse block id: %s", payload)
block := new(storage.Block)
if err := jsoniter.UnmarshalFromString(payload, block); err != nil {
return err
}

block, err := d.blocks.ByIdWithRelations(ctx, id)
if err != nil {
return err
if block.ProposerId > 0 {
validator, err := d.validators.GetByID(ctx, block.ProposerId)
if err != nil {
return err
}
block.Proposer = *validator
}

d.mx.RLock()
for i := range d.observers {
d.observers[i].notifyBlocks(&block)
d.observers[i].notifyBlocks(block)
}
d.mx.RUnlock()
return nil
Expand Down
30 changes: 30 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.

16 changes: 10 additions & 6 deletions cmd/api/handler/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ import (
type ConstantHandler struct {
constants storage.IConstant
denomMetadata storage.IDenomMetadata
address storage.IAddress
rollup storage.IRollup
}

func NewConstantHandler(
constants storage.IConstant,
denomMetadata storage.IDenomMetadata,
address storage.IAddress,
rollup storage.IRollup,
) *ConstantHandler {
return &ConstantHandler{
constants: constants,
denomMetadata: denomMetadata,
address: address,
rollup: rollup,
}
}

Expand All @@ -43,11 +43,11 @@ func NewConstantHandler(
func (handler *ConstantHandler) Get(c echo.Context) error {
consts, err := handler.constants.All(c.Request().Context())
if err != nil {
return handleError(c, err, handler.address)
return handleError(c, err, handler.rollup)
}
dm, err := handler.denomMetadata.All(c.Request().Context())
if err != nil {
return handleError(c, err, handler.address)
return handleError(c, err, handler.rollup)
}
return c.JSON(http.StatusOK, responses.NewConstants(consts, dm))
}
Expand All @@ -62,5 +62,9 @@ func (handler *ConstantHandler) Get(c echo.Context) error {
// @Success 200 {object} responses.Enums
// @Router /enums [get]
func (handler *ConstantHandler) Enums(c echo.Context) error {
return c.JSON(http.StatusOK, responses.NewEnums())
tags, err := handler.rollup.Tags(c.Request().Context())
if err != nil {
return handleError(c, err, handler.rollup)
}
return c.JSON(http.StatusOK, responses.NewEnums(tags))
}
11 changes: 9 additions & 2 deletions cmd/api/handler/constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ConstantTestSuite struct {
suite.Suite
constants *mock.MockIConstant
denomMetadata *mock.MockIDenomMetadata
address *mock.MockIAddress
rollup *mock.MockIRollup
echo *echo.Echo
handler *ConstantHandler
ctrl *gomock.Controller
Expand All @@ -35,7 +35,8 @@ func (s *ConstantTestSuite) SetupSuite() {
s.ctrl = gomock.NewController(s.T())
s.constants = mock.NewMockIConstant(s.ctrl)
s.denomMetadata = mock.NewMockIDenomMetadata(s.ctrl)
s.address = mock.NewMockIAddress(s.ctrl)
s.rollup = mock.NewMockIRollup(s.ctrl)
s.handler = NewConstantHandler(s.constants, s.denomMetadata, s.rollup)
}

// TearDownSuite -
Expand All @@ -54,6 +55,11 @@ func (s *ConstantTestSuite) TestEnums() {
c := s.echo.NewContext(req, rec)
c.SetPath("/enums")

s.rollup.EXPECT().
Tags(gomock.Any()).
Return([]string{"ai", "zk"}, nil).
Times(1)

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

Expand All @@ -64,4 +70,5 @@ func (s *ConstantTestSuite) TestEnums() {
s.Require().Len(enums.MessageType, 76)
s.Require().Len(enums.Status, 2)
s.Require().Len(enums.Categories, 5)
s.Require().Len(enums.Tags, 2)
}
1 change: 1 addition & 0 deletions cmd/api/handler/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
errInvalidHashLength = errors.New("invalid hash: should be 32 bytes length")
errInvalidAddress = errors.New("invalid address")
errUnknownAddress = errors.New("unknown address")
errInvalidApiKey = errors.New("invalid api key")
errCancelRequest = "pq: canceling statement due to user request"
)

Expand Down
4 changes: 3 additions & 1 deletion cmd/api/handler/responses/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ type Enums struct {
EventType []string `json:"event_type"`
Categories []string `json:"categories"`
RollupTypes []string `json:"rollup_type"`
Tags []string `json:"tags"`
}

func NewEnums() Enums {
func NewEnums(tags []string) Enums {
return Enums{
Status: types.StatusNames(),
MessageType: types.MsgTypeNames(),
EventType: types.EventTypeNames(),
Categories: types.RollupCategoryNames(),
RollupTypes: types.RollupTypeNames(),
Tags: tags,
}
}
4 changes: 4 additions & 0 deletions cmd/api/handler/responses/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type RollupWithStats struct {
FeePct float64 `example:"0.9876" format:"float" json:"fee_pct" swaggertype:"number"`
BlobsCountPct float64 `example:"0.9876" format:"float" json:"blobs_count_pct" swaggertype:"number"`

Tags []string `json:"tags,omitempty"`
Links []string `json:"links,omitempty"`
}

Expand Down Expand Up @@ -71,6 +72,7 @@ func NewRollupWithStats(r storage.RollupWithStats) RollupWithStats {
Provider: r.Provider,
VM: r.VM,
Fee: r.Fee.StringFixed(0),
Tags: r.Tags,
}
}

Expand All @@ -94,6 +96,7 @@ type Rollup struct {
Compression string `example:"zip" format:"string" json:"compression,omitempty" swaggertype:"string"`
VM string `example:"evm" format:"string" json:"vm,omitempty" swaggertype:"string"`

Tags []string `json:"tags,omitempty"`
Links []string `json:"links,omitempty"`
}

Expand All @@ -118,6 +121,7 @@ func NewRollup(r *storage.Rollup) Rollup {
Type: r.Type.String(),
Provider: r.Provider,
VM: r.VM,
Tags: r.Tags,
}
}

Expand Down
Loading

0 comments on commit 8026bb7

Please sign in to comment.