Skip to content

Commit

Permalink
expose block info endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhorsey committed Feb 10, 2023
1 parent 06986ea commit aae35ee
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 22 deletions.
30 changes: 17 additions & 13 deletions packages/relayer/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,17 @@ func Run(
log.Fatal(err)
}

srv, err := newHTTPServer(db)
l1EthClient, err := ethclient.Dial(os.Getenv("L1_RPC_URL"))
if err != nil {
log.Fatal(err)
}

l2EthClient, err := ethclient.Dial(os.Getenv("L2_RPC_URL"))
if err != nil {
log.Fatal(err)
}

srv, err := newHTTPServer(db, l1EthClient, l2EthClient)
if err != nil {
log.Fatal(err)
}
Expand All @@ -102,7 +112,7 @@ func Run(
}()

if !httpOnly {
indexers, closeFunc, err := makeIndexers(layer, db, profitableOnly)
indexers, closeFunc, err := makeIndexers(layer, db, profitableOnly, l1EthClient, l2EthClient)
if err != nil {
sqlDB.Close()
log.Fatal(err)
Expand All @@ -127,6 +137,8 @@ func makeIndexers(
layer relayer.Layer,
db relayer.DB,
profitableOnly relayer.ProfitableOnly,
l1EthClient *ethclient.Client,
l2EthClient *ethclient.Client,
) ([]*indexer.Service, func(), error) {
eventRepository, err := repo.NewEventRepository(db)
if err != nil {
Expand Down Expand Up @@ -167,16 +179,6 @@ func makeIndexers(
confirmations = defaultConfirmations
}

l1EthClient, err := ethclient.Dial(os.Getenv("L1_RPC_URL"))
if err != nil {
return nil, nil, err
}

l2EthClient, err := ethclient.Dial(os.Getenv("L2_RPC_URL"))
if err != nil {
return nil, nil, err
}

l1RpcClient, err := rpc.DialContext(context.Background(), os.Getenv("L1_RPC_URL"))
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -341,7 +343,7 @@ func loadAndValidateEnv() error {
return errors.Errorf("Missing env vars: %v", missing)
}

func newHTTPServer(db relayer.DB) (*http.Server, error) {
func newHTTPServer(db relayer.DB, l1EthClient relayer.EthClient, l2EthClient relayer.EthClient) (*http.Server, error) {
eventRepo, err := repo.NewEventRepository(db)
if err != nil {
return nil, err
Expand All @@ -351,6 +353,8 @@ func newHTTPServer(db relayer.DB) (*http.Server, error) {
EventRepo: eventRepo,
Echo: echo.New(),
CorsOrigins: strings.Split(os.Getenv("CORS_ORIGINS"), ","),
L1EthClient: l1EthClient,
L2EthClient: l2EthClient,
})
if err != nil {
return nil, err
Expand Down
7 changes: 4 additions & 3 deletions packages/relayer/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/taikoxyz/taiko-mono/packages/relayer"
"github.com/taikoxyz/taiko-mono/packages/relayer/mock"
)

var dummyEcdsaKey = "8da4ef21b864d2cc526dbdb2a120bd2874c36c9d0a1fb7f8c63d7f7a8b41de8f"
Expand Down Expand Up @@ -203,7 +204,7 @@ func Test_makeIndexers(t *testing.T) {
defer reset()
}

indexers, cancel, err := makeIndexers(tt.layer, tt.dbFunc(t), relayer.ProfitableOnly(true))
indexers, cancel, err := makeIndexers(tt.layer, tt.dbFunc(t), relayer.ProfitableOnly(true), nil, nil)
if cancel != nil {
defer cancel()
}
Expand All @@ -226,12 +227,12 @@ func Test_newHTTPServer(t *testing.T) {

defer cancel()

srv, err := newHTTPServer(db)
srv, err := newHTTPServer(db, &mock.EthClient{}, &mock.EthClient{})
assert.Nil(t, err)
assert.NotNil(t, srv)
}

func Test_newHTTPServer_nilDB(t *testing.T) {
_, err := newHTTPServer(nil)
_, err := newHTTPServer(nil, &mock.EthClient{}, &mock.EthClient{})
assert.NotNil(t, err)
}
68 changes: 68 additions & 0 deletions packages/relayer/http/get_block_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package http

import (
"net/http"

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

type blockInfo struct {
ChainID int64 `json:"chainID"`
LatestProcessedBlock int64 `json:"latestProcessedBlock"`
LatestBlock int64 `json:"latestBlock"`
}

type getBlockInfoResponse struct {
Data []blockInfo `json:"data"`
}

func (srv *Server) GetBlockInfo(c echo.Context) error {
l1ChainID, err := srv.l1EthClient.ChainID(c.Request().Context())
if err != nil {
return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err)
}

l2ChainID, err := srv.l2EthClient.ChainID(c.Request().Context())
if err != nil {
return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err)
}

latestL1Block, err := srv.l1EthClient.BlockNumber(c.Request().Context())
if err != nil {
return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err)
}

latestL2Block, err := srv.l2EthClient.BlockNumber(c.Request().Context())
if err != nil {
return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err)
}

latestProcessedL1Block, err := srv.blockRepo.GetLatestBlockProcessedForEvent(relayer.EventNameMessageSent, l1ChainID)
if err != nil {
return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err)
}

latestProcessedL2Block, err := srv.blockRepo.GetLatestBlockProcessedForEvent(relayer.EventNameMessageSent, l2ChainID)
if err != nil {
return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err)
}

resp := getBlockInfoResponse{
Data: []blockInfo{
{
ChainID: l1ChainID.Int64(),
LatestProcessedBlock: int64(latestProcessedL1Block.Height),
LatestBlock: int64(latestL1Block),
},
{
ChainID: l2ChainID.Int64(),
LatestProcessedBlock: int64(latestProcessedL2Block.Height),
LatestBlock: int64(latestL2Block),
},
},
}

return c.JSON(http.StatusOK, resp)
}
1 change: 1 addition & 0 deletions packages/relayer/http/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ func (srv *Server) configureRoutes() {
srv.echo.GET("/", srv.Health)

srv.echo.GET("/events", srv.GetEventsByAddress)
srv.echo.GET("/blockInfo", srv.GetBlockInfo)
}
29 changes: 25 additions & 4 deletions packages/relayer/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ import (
)

type Server struct {
echo *echo.Echo
eventRepo relayer.EventRepository
echo *echo.Echo
eventRepo relayer.EventRepository
blockRepo relayer.BlockRepository
l1EthClient relayer.EthClient
l2EthClient relayer.EthClient
}

type NewServerOpts struct {
Echo *echo.Echo
EventRepo relayer.EventRepository
BlockRepo relayer.BlockRepository
CorsOrigins []string
L1EthClient relayer.EthClient
L2EthClient relayer.EthClient
}

func (opts NewServerOpts) Validate() error {
Expand All @@ -37,6 +43,18 @@ func (opts NewServerOpts) Validate() error {
return relayer.ErrNoCORSOrigins
}

if opts.L1EthClient == nil {
return relayer.ErrNoEthClient
}

if opts.L2EthClient == nil {
return relayer.ErrNoEthClient
}

if opts.BlockRepo == nil {
return relayer.ErrNoBlockRepository
}

return nil
}

Expand All @@ -46,8 +64,11 @@ func NewServer(opts NewServerOpts) (*Server, error) {
}

srv := &Server{
echo: opts.Echo,
eventRepo: opts.EventRepo,
blockRepo: opts.BlockRepo,
echo: opts.Echo,
eventRepo: opts.EventRepo,
l1EthClient: opts.L1EthClient,
l2EthClient: opts.L2EthClient,
}

corsOrigins := opts.CorsOrigins
Expand Down
49 changes: 47 additions & 2 deletions packages/relayer/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,64 @@ func Test_NewServer(t *testing.T) {
Echo: echo.New(),
EventRepo: &repo.EventRepository{},
CorsOrigins: make([]string, 0),
L1EthClient: &mock.EthClient{},
L2EthClient: &mock.EthClient{},
BlockRepo: &mock.BlockRepository{},
},
nil,
},
{
"noL1EthClient",
NewServerOpts{
Echo: echo.New(),
EventRepo: &repo.EventRepository{},
CorsOrigins: make([]string, 0),
L2EthClient: &mock.EthClient{},
BlockRepo: &mock.BlockRepository{},
},
relayer.ErrNoEthClient,
},
{
"noL2EthClient",
NewServerOpts{
Echo: echo.New(),
EventRepo: &repo.EventRepository{},
CorsOrigins: make([]string, 0),
L1EthClient: &mock.EthClient{},
BlockRepo: &mock.BlockRepository{},
},
relayer.ErrNoEthClient,
},
{
"noBlockRepo",
NewServerOpts{
Echo: echo.New(),
EventRepo: &repo.EventRepository{},
CorsOrigins: make([]string, 0),
L1EthClient: &mock.EthClient{},
L2EthClient: &mock.EthClient{},
},
relayer.ErrNoBlockRepository,
},
{
"noEventRepo",
NewServerOpts{
Echo: echo.New(),
CorsOrigins: make([]string, 0),
L1EthClient: &mock.EthClient{},
L2EthClient: &mock.EthClient{},
BlockRepo: &mock.BlockRepository{},
},
relayer.ErrNoEventRepository,
},
{
"noCorsOrigins",
NewServerOpts{
Echo: echo.New(),
EventRepo: &repo.EventRepository{},
Echo: echo.New(),
EventRepo: &repo.EventRepository{},
L1EthClient: &mock.EthClient{},
L2EthClient: &mock.EthClient{},
BlockRepo: &mock.BlockRepository{},
},
relayer.ErrNoCORSOrigins,
},
Expand All @@ -65,6 +107,9 @@ func Test_NewServer(t *testing.T) {
NewServerOpts{
EventRepo: &repo.EventRepository{},
CorsOrigins: make([]string, 0),
L1EthClient: &mock.EthClient{},
L2EthClient: &mock.EthClient{},
BlockRepo: &mock.BlockRepository{},
},
ErrNoHTTPFramework,
},
Expand Down
5 changes: 5 additions & 0 deletions packages/relayer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,8 @@ type CanonicalToken struct {
Symbol string `json:"symbol"`
Name string `json:"name"`
}

type EthClient interface {
BlockNumber(ctx context.Context) (uint64, error)
ChainID(ctx context.Context) (*big.Int, error)
}

0 comments on commit aae35ee

Please sign in to comment.