-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eventindexer): ProposeEvents filtering, API exposing, and gettin…
…g count by address/event + tests (#13624) Co-authored-by: dave | d1onys1us <13951458+d1onys1us@users.noreply.github.com>
- Loading branch information
1 parent
43247d3
commit 839a0be
Showing
22 changed files
with
1,052 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/eventindexer/http/get_count_by_address_and_event.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
68
packages/eventindexer/http/get_count_by_address_and_event_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.