Skip to content

Commit

Permalink
feat(eventindexer): ProposeEvents filtering, API exposing, and gettin…
Browse files Browse the repository at this point in the history
…g count by address/event + tests (#13624)

Co-authored-by: dave | d1onys1us <13951458+d1onys1us@users.noreply.github.com>
  • Loading branch information
cyberhorsey and d1onys1us committed Apr 20, 2023
1 parent 43247d3 commit 839a0be
Show file tree
Hide file tree
Showing 22 changed files with 1,052 additions and 14 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
gorm.io/datatypes v1.0.7
gorm.io/driver/mysql v1.4.3
gorm.io/gorm v1.24.6
gotest.tools v2.2.0+incompatible
)

require (
Expand Down Expand Up @@ -57,6 +58,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.4.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-containerregistry v0.5.1/go.mod h1:Ct15B4yir3PLOP5jsy0GNeYVaIZs/MK/Jz5any1wFW0=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down
12 changes: 11 additions & 1 deletion packages/eventindexer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

var (
EventNameBlockProven = "BlockProven"
EventNameBlockProven = "BlockProven"
EventNameBlockProposed = "BlockProposed"
)

// Event represents a stored EVM event. The fields will be serialized
Expand Down Expand Up @@ -37,10 +38,19 @@ type UniqueProversResponse struct {
Count int `json:"count"`
}

type UniqueProposersResponse struct {
Address string `json:"address"`
Count int `json:"count"`
}

// EventRepository is used to interact with events in the store
type EventRepository interface {
Save(ctx context.Context, opts SaveEventOpts) (*Event, error)
FindUniqueProvers(
ctx context.Context,
) ([]UniqueProversResponse, error)
FindUniqueProposers(
ctx context.Context,
) ([]UniqueProposersResponse, error)
GetCountByAddressAndEventName(ctx context.Context, address string, event string) (int, error)
}
27 changes: 27 additions & 0 deletions packages/eventindexer/http/get_count_by_address_and_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package http

import (
"net/http"

"github.com/cyberhorsey/webutils"
"github.com/labstack/echo/v4"
)

type GetCountByAddressAndEventNameResp struct {
Count int `json:"count"`
}

func (srv *Server) GetCountByAddressAndEventName(c echo.Context) error {
count, err := srv.eventRepo.GetCountByAddressAndEventName(
c.Request().Context(),
c.QueryParam("address"),
c.QueryParam("event"),
)
if err != nil {
return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err)
}

return c.JSON(http.StatusOK, &GetCountByAddressAndEventNameResp{
Count: count,
})
}
68 changes: 68 additions & 0 deletions packages/eventindexer/http/get_count_by_address_and_event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package http

import (
"context"
"fmt"
"math/big"
"net/http"
"net/http/httptest"
"testing"

"github.com/cyberhorsey/webutils/testutils"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/taikoxyz/taiko-mono/packages/eventindexer"
)

func Test_GetCountByAddressAndEvent(t *testing.T) {
srv := newTestServer("")

_, err := srv.eventRepo.Save(context.Background(), eventindexer.SaveEventOpts{
Name: "name",
Data: `{"Owner": "0x0000000000000000000000000000000000000123"}`,
ChainID: big.NewInt(167001),
Address: "0x123",
Event: eventindexer.EventNameBlockProposed,
})

assert.Equal(t, nil, err)

tests := []struct {
name string
address string
event string
wantStatus int
wantBodyRegexpMatches []string
}{
{
"successZeroCount",
"0xhasntProposedAnything",
eventindexer.EventNameBlockProposed,
http.StatusOK,
[]string{`{"count":0`},
},
{
"success",
"0x123",
eventindexer.EventNameBlockProposed,
http.StatusOK,
[]string{`{"count":1`},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := testutils.NewUnauthenticatedRequest(
echo.GET,
fmt.Sprintf("/eventByAddress?address=%v&event=%v", tt.address, tt.event),
nil,
)

rec := httptest.NewRecorder()

srv.ServeHTTP(rec, req)

testutils.AssertStatusAndBody(t, rec, tt.wantStatus, tt.wantBodyRegexpMatches)
})
}
}
28 changes: 28 additions & 0 deletions packages/eventindexer/http/get_unique_proposers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package http

import (
"net/http"

"github.com/cyberhorsey/webutils"
"github.com/labstack/echo/v4"
"github.com/taikoxyz/taiko-mono/packages/eventindexer"
)

type uniqueProposersResp struct {
Proposers []eventindexer.UniqueProposersResponse `json:"proposers"`
UniqueProposers int `json:"uniqueProposers"`
}

func (srv *Server) GetUniqueProposers(c echo.Context) error {
proposers, err := srv.eventRepo.FindUniqueProposers(
c.Request().Context(),
)
if err != nil {
return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err)
}

return c.JSON(http.StatusOK, &uniqueProposersResp{
Proposers: proposers,
UniqueProposers: len(proposers),
})
}
56 changes: 56 additions & 0 deletions packages/eventindexer/http/get_unique_proposers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package http

import (
"context"
"math/big"
"net/http"
"net/http/httptest"
"testing"

"github.com/cyberhorsey/webutils/testutils"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/taikoxyz/taiko-mono/packages/eventindexer"
)

func Test_GetUniqueProposers(t *testing.T) {
srv := newTestServer("")

_, err := srv.eventRepo.Save(context.Background(), eventindexer.SaveEventOpts{
Name: "name",
Data: `{"Owner": "0x0000000000000000000000000000000000000123"}`,
ChainID: big.NewInt(167001),
Address: "0x123",
Event: eventindexer.EventNameBlockProposed,
})

assert.Equal(t, nil, err)

tests := []struct {
name string
wantStatus int
wantBodyRegexpMatches []string
}{
{
"successEmptyList",
http.StatusOK,
[]string{`\[\]`},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := testutils.NewUnauthenticatedRequest(
echo.GET,
"/uniqueProposers",
nil,
)

rec := httptest.NewRecorder()

srv.ServeHTTP(rec, req)

testutils.AssertStatusAndBody(t, rec, tt.wantStatus, tt.wantBodyRegexpMatches)
})
}
}
56 changes: 56 additions & 0 deletions packages/eventindexer/http/get_unique_provers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package http

import (
"context"
"math/big"
"net/http"
"net/http/httptest"
"testing"

"github.com/cyberhorsey/webutils/testutils"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/taikoxyz/taiko-mono/packages/eventindexer"
)

func Test_GetUniqueProvers(t *testing.T) {
srv := newTestServer("")

_, err := srv.eventRepo.Save(context.Background(), eventindexer.SaveEventOpts{
Name: "name",
Data: `{"Owner": "0x0000000000000000000000000000000000000123"}`,
ChainID: big.NewInt(167001),
Address: "0x123",
Event: eventindexer.EventNameBlockProven,
})

assert.Equal(t, nil, err)

tests := []struct {
name string
wantStatus int
wantBodyRegexpMatches []string
}{
{
"successEmptyList",
http.StatusOK,
[]string{`\[\]`},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := testutils.NewUnauthenticatedRequest(
echo.GET,
"/uniqueProvers",
nil,
)

rec := httptest.NewRecorder()

srv.ServeHTTP(rec, req)

testutils.AssertStatusAndBody(t, rec, tt.wantStatus, tt.wantBodyRegexpMatches)
})
}
}
2 changes: 2 additions & 0 deletions packages/eventindexer/http/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ func (srv *Server) configureRoutes() {
srv.echo.GET("/", srv.Health)

srv.echo.GET("/uniqueProvers", srv.GetUniqueProvers)
srv.echo.GET("/uniqueProposers", srv.GetUniqueProposers)
srv.echo.GET("/eventByAddress", srv.GetCountByAddressAndEventName)
}
Loading

0 comments on commit 839a0be

Please sign in to comment.