Skip to content

Commit

Permalink
fix(relayer): Relayer reorg (#14033)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhorsey authored Jun 22, 2023
1 parent 537dd5a commit 4794f45
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/relayer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,10 @@ type EventRepository interface {
ctx context.Context,
msgHash string,
) (*Event, error)
FirstByEventAndMsgHash(
ctx context.Context,
event string,
msgHash string,
) (*Event, error)
Delete(ctx context.Context, id int) error
}
9 changes: 7 additions & 2 deletions packages/relayer/indexer/detect_and_handle_reorg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ import (
"context"

"github.com/pkg/errors"

log "github.com/sirupsen/logrus"
)

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

if e == nil || e.MsgHash == "" {
if e == nil || e.MsgHash == "" || e.Event != eventType {
return nil
}

// reorg detected

log.Infof("reorg detected for msgHash %v and eventType %v", msgHash, eventType)

err = svc.eventRepo.Delete(ctx, e.ID)
if err != nil {
return errors.Wrap(err, "svc.eventRepo.Delete")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE `events` ADD INDEX `msg_hash_event_index` (`msg_hash`, `event`);

-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP INDEX msg_hash_event_index on events;
-- +goose StatementEnd
14 changes: 14 additions & 0 deletions packages/relayer/mock/event_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ func (r *EventRepository) FirstByMsgHash(
return nil, nil
}

func (r *EventRepository) FirstByEventAndMsgHash(
ctx context.Context,
event string,
msgHash string,
) (*relayer.Event, error) {
for _, e := range r.events {
if e.MsgHash == msgHash && e.Event == event {
return e, nil
}
}

return nil, nil
}

func (r *EventRepository) Delete(
ctx context.Context,
id int,
Expand Down
20 changes: 20 additions & 0 deletions packages/relayer/repo/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ func (r *EventRepository) FirstByMsgHash(
return e, nil
}

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

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

return e, nil
}

func (r *EventRepository) FindAllByAddress(
ctx context.Context,
req *http.Request,
Expand Down

0 comments on commit 4794f45

Please sign in to comment.