Skip to content

Commit

Permalink
fix(relayer): only need to find first msg hash (#13966)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhorsey authored and dantaik committed Jun 12, 2023
1 parent 87eed87 commit 0980e57
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 58 deletions.
4 changes: 2 additions & 2 deletions packages/relayer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ type EventRepository interface {
req *http.Request,
opts FindAllByAddressOpts,
) (paginate.Page, error)
FindAllByMsgHash(
FirstByMsgHash(
ctx context.Context,
msgHash string,
) ([]*Event, error)
) (*Event, error)
Delete(ctx context.Context, id int) error
}
26 changes: 7 additions & 19 deletions packages/relayer/indexer/detect_and_handle_reorg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,22 @@ import (
"context"

"github.com/pkg/errors"
"github.com/taikoxyz/taiko-mono/packages/relayer"
)

func (svc *Service) detectAndHandleReorg(ctx context.Context, eventType string, msgHash string) error {
events, err := svc.eventRepo.FindAllByMsgHash(ctx, msgHash)
e, err := svc.eventRepo.FirstByMsgHash(ctx, msgHash)
if err != nil {
return errors.Wrap(err, "svc.eventRepo.FindAllByMsgHash")
return errors.Wrap(err, "svc.eventRepo.FirstByMsgHash")
}

if events == nil {
if e == nil {
return nil
}

var existingEvent *relayer.Event

for _, e := range events {
if e.Event == eventType && e.MsgHash == msgHash {
existingEvent = e
break
}
}

if existingEvent != nil {
// reorg detected
err := svc.eventRepo.Delete(ctx, existingEvent.ID)
if err != nil {
return errors.Wrap(err, "svc.eventRepo.Delete")
}
// reorg detected
err = svc.eventRepo.Delete(ctx, e.ID)
if err != nil {
return errors.Wrap(err, "svc.eventRepo.Delete")
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions packages/relayer/indexer/save_message_status_changed_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ func (svc *Service) saveMessageStatusChangedEvent(
// get the previous MessageSent event or other message status changed events,
// so we can find out the previous owner of this msg hash,
// to save to the db.
previousEvents, err := svc.eventRepo.FindAllByMsgHash(ctx, common.Hash(event.MsgHash).Hex())
e, err := svc.eventRepo.FirstByMsgHash(ctx, common.Hash(event.MsgHash).Hex())
if err != nil {
return errors.Wrap(err, "svc.eventRepo.FindAllByMsgHash")
return errors.Wrap(err, "svc.eventRepo.FirstByMsgHash")
}

if len(previousEvents) == 0 {
return errors.Wrap(err, "svc.eventRepo.FindAllByMsgHash")
if e == nil {
return errors.Wrap(err, "svc.eventRepo.FirstByMsgHash")
}

_, err = svc.eventRepo.Save(ctx, relayer.SaveEventOpts{
Name: relayer.EventNameMessageStatusChanged,
Data: string(marshaled),
ChainID: chainID,
Status: relayer.EventStatus(event.Status),
MessageOwner: previousEvents[0].MessageOwner,
MessageOwner: e.MessageOwner,
MsgHash: common.Hash(event.MsgHash).Hex(),
Event: relayer.EventNameMessageStatusChanged,
})
Expand Down
10 changes: 4 additions & 6 deletions packages/relayer/mock/event_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,17 @@ func (r *EventRepository) FindAllByAddress(
}, nil
}

func (r *EventRepository) FindAllByMsgHash(
func (r *EventRepository) FirstByMsgHash(
ctx context.Context,
msgHash string,
) ([]*relayer.Event, error) {
events := make([]*relayer.Event, 0)

) (*relayer.Event, error) {
for _, e := range r.events {
if e.MsgHash == msgHash {
events = append(events, e)
return e, nil
}
}

return events, nil
return nil, nil
}

func (r *EventRepository) Delete(
Expand Down
10 changes: 5 additions & 5 deletions packages/relayer/repo/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ func (r *EventRepository) UpdateStatus(ctx context.Context, id int, status relay
return nil
}

func (r *EventRepository) FindAllByMsgHash(
func (r *EventRepository) FirstByMsgHash(
ctx context.Context,
msgHash string,
) ([]*relayer.Event, error) {
e := make([]*relayer.Event, 0)
) (*relayer.Event, error) {
e := &relayer.Event{}
// find all message sent events
if err := r.db.GormDB().Where("msg_hash = ?", msgHash).
Find(&e).Error; err != nil {
First(&e).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return e, nil
}

return nil, errors.Wrap(err, "r.db.Find")
return nil, errors.Wrap(err, "r.db.First")
}

return e, nil
Expand Down
40 changes: 19 additions & 21 deletions packages/relayer/repo/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func TestIntegration_Event_FindAllByAddress(t *testing.T) {
}
}

func TestIntegration_Event_FindAllByMsgHash(t *testing.T) {
func TestIntegration_Event_FirstByMsgHash(t *testing.T) {
db, close, err := testMysql(t)
assert.Equal(t, nil, err)

Expand All @@ -441,43 +441,41 @@ func TestIntegration_Event_FindAllByMsgHash(t *testing.T) {
tests := []struct {
name string
msgHash string
wantResp []*relayer.Event
wantResp *relayer.Event
wantErr error
}{
{
"success",
"0x1",
[]*relayer.Event{
{
ID: 1,
Name: "name",
// nolint lll
Data: datatypes.JSON([]byte(fmt.Sprintf(`{"Message": {"Owner": "%s"}}`, strings.ToLower(addr.Hex())))),
ChainID: 1,
Status: relayer.EventStatusDone,
EventType: relayer.EventTypeSendETH,
CanonicalTokenAddress: "0x1",
CanonicalTokenSymbol: "ETH",
CanonicalTokenName: "Ethereum",
CanonicalTokenDecimals: 18,
Amount: "1",
MsgHash: "0x1",
MessageOwner: addr.Hex(),
},
&relayer.Event{
ID: 1,
Name: "name",
// nolint lll
Data: datatypes.JSON([]byte(fmt.Sprintf(`{"Message": {"Owner": "%s"}}`, strings.ToLower(addr.Hex())))),
ChainID: 1,
Status: relayer.EventStatusDone,
EventType: relayer.EventTypeSendETH,
CanonicalTokenAddress: "0x1",
CanonicalTokenSymbol: "ETH",
CanonicalTokenName: "Ethereum",
CanonicalTokenDecimals: 18,
Amount: "1",
MsgHash: "0x1",
MessageOwner: addr.Hex(),
},
nil,
},
{
"noneByMgHash",
"0xfake",
[]*relayer.Event{},
nil,
nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := eventRepo.FindAllByMsgHash(context.Background(), tt.msgHash)
resp, err := eventRepo.FirstByMsgHash(context.Background(), tt.msgHash)
assert.Equal(t, tt.wantResp, resp)
assert.Equal(t, tt.wantErr, err)
})
Expand Down

0 comments on commit 0980e57

Please sign in to comment.