Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(relayer): add msgHash and event type lookups to findallbyaddress #13310

Merged
merged 5 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/relayer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,21 @@ type SaveEventOpts struct {
MessageOwner string
}

type FindAllByAddressOpts struct {
Address common.Address
EventType *EventType
MsgHash *string
ChainID *big.Int
}

// EventRepository is used to interact with events in the store
type EventRepository interface {
Save(ctx context.Context, opts SaveEventOpts) (*Event, error)
UpdateStatus(ctx context.Context, id int, status EventStatus) error
FindAllByAddressAndChainID(
ctx context.Context,
chainID *big.Int,
address common.Address,
) ([]*Event, error)
FindAllByAddress(
ctx context.Context,
req *http.Request,
address common.Address,
opts FindAllByAddressOpts,
) (paginate.Page, error)
FindAllByMsgHash(
ctx context.Context,
Expand Down
29 changes: 28 additions & 1 deletion packages/relayer/http/get_events_by_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,47 @@ package http

import (
"html"
"math/big"
"net/http"
"strconv"

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

func (srv *Server) GetEventsByAddress(c echo.Context) error {
chainID, _ := new(big.Int).SetString(c.QueryParam("chainID"), 10)

address := html.EscapeString(c.QueryParam("address"))

msgHash := html.EscapeString(c.QueryParam("msgHash"))

eventTypeParam := html.EscapeString(c.QueryParam("eventType"))

var eventType *relayer.EventType

if eventTypeParam != "" {
i, err := strconv.Atoi(eventTypeParam)
if err != nil {
return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err)
}

et := relayer.EventType(i)

eventType = &et
}

page, err := srv.eventRepo.FindAllByAddress(
c.Request().Context(),
c.Request(),
common.HexToAddress(address),
relayer.FindAllByAddressOpts{
Address: common.HexToAddress(address),
MsgHash: &msgHash,
EventType: eventType,
ChainID: chainID,
},
)
if err != nil {
return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err)
Expand Down
41 changes: 2 additions & 39 deletions packages/relayer/mock/event_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package mock
import (
"context"
"encoding/json"
"math/big"
"math/rand"
"net/http"

"github.com/ethereum/go-ethereum/common"
"github.com/morkid/paginate"
"github.com/taikoxyz/taiko-mono/packages/relayer"
"gorm.io/datatypes"
Expand Down Expand Up @@ -61,45 +59,10 @@ func (r *EventRepository) UpdateStatus(ctx context.Context, id int, status relay
return nil
}

func (r *EventRepository) FindAllByAddressAndChainID(
ctx context.Context,
chainID *big.Int,
address common.Address,
) ([]*relayer.Event, error) {
type d struct {
Owner string `json:"Owner"`
}

events := make([]*relayer.Event, 0)

for _, e := range r.events {
if e.ChainID != int64(chainID.Uint64()) {
continue
}

m, err := e.Data.MarshalJSON()
if err != nil {
return nil, err
}

data := &d{}
if err := json.Unmarshal(m, data); err != nil {
return nil, err
}

if data.Owner == address.Hex() {
events = append(events, e)
break
}
}

return events, nil
}

func (r *EventRepository) FindAllByAddress(
ctx context.Context,
req *http.Request,
address common.Address,
opts relayer.FindAllByAddressOpts,
) (paginate.Page, error) {
type d struct {
Owner string `json:"Owner"`
Expand All @@ -118,7 +81,7 @@ func (r *EventRepository) FindAllByAddress(
return paginate.Page{}, err
}

if data.Owner == address.Hex() {
if data.Owner == opts.Address.Hex() {
events = append(events, e)
break
}
Expand Down
40 changes: 17 additions & 23 deletions packages/relayer/repo/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package repo

import (
"context"
"math/big"
"net/http"
"strings"

"github.com/ethereum/go-ethereum/common"
"net/http"

"github.com/morkid/paginate"
"github.com/pkg/errors"
"github.com/taikoxyz/taiko-mono/packages/relayer"
Expand Down Expand Up @@ -42,6 +41,7 @@ func (r *EventRepository) Save(ctx context.Context, opts relayer.SaveEventOpts)
MsgHash: opts.MsgHash,
MessageOwner: opts.MessageOwner,
}

if err := r.db.GormDB().Create(e).Error; err != nil {
return nil, errors.Wrap(err, "r.db.Create")
}
Expand Down Expand Up @@ -79,35 +79,29 @@ func (r *EventRepository) FindAllByMsgHash(
return e, nil
}

func (r *EventRepository) FindAllByAddressAndChainID(
ctx context.Context,
chainID *big.Int,
address common.Address,
) ([]*relayer.Event, error) {
e := make([]*relayer.Event, 0)
// find all message sent events
if err := r.db.GormDB().Where("chain_id = ?", chainID.Int64()).
Where("message_owner = ?", strings.ToLower(address.Hex())).
Find(&e).Error; err != nil {
return nil, errors.Wrap(err, "r.db.Find")
}

// find all message status changed events

return e, nil
}

func (r *EventRepository) FindAllByAddress(
ctx context.Context,
req *http.Request,
address common.Address,
opts relayer.FindAllByAddressOpts,
) (paginate.Page, error) {
pg := paginate.New(&paginate.Config{
DefaultSize: 100,
})

q := r.db.GormDB().
Model(&relayer.Event{}).Where("message_owner = ?", strings.ToLower(address.Hex()))
Model(&relayer.Event{}).Where("message_owner = ?", strings.ToLower(opts.Address.Hex()))

if opts.EventType != nil {
q = q.Where("event_type = ?", *opts.EventType)
}

if opts.MsgHash != nil && *opts.MsgHash != "" {
q = q.Where("msg_hash = ?", *opts.MsgHash)
}

if opts.ChainID != nil {
q = q.Where("chain_id = ?", opts.ChainID.Int64())
}

reqCtx := pg.With(q)

Expand Down
Loading