From 17f0d6751f94cc761f6effc94b4cbdfee7bdc1d2 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Thu, 22 Jun 2023 02:12:25 -0700 Subject: [PATCH 1/2] relayer reorg needs to check event type --- packages/relayer/event.go | 5 +++++ .../indexer/detect_and_handle_reorg.go | 9 +++++++-- packages/relayer/mock/event_repository.go | 14 +++++++++++++ packages/relayer/repo/event.go | 20 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/packages/relayer/event.go b/packages/relayer/event.go index f61c9f8af31..4c0a701fabc 100644 --- a/packages/relayer/event.go +++ b/packages/relayer/event.go @@ -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 } diff --git a/packages/relayer/indexer/detect_and_handle_reorg.go b/packages/relayer/indexer/detect_and_handle_reorg.go index 3ff30a81073..55cfac8b7e9 100644 --- a/packages/relayer/indexer/detect_and_handle_reorg.go +++ b/packages/relayer/indexer/detect_and_handle_reorg.go @@ -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") diff --git a/packages/relayer/mock/event_repository.go b/packages/relayer/mock/event_repository.go index 039509661e1..7e52dcca689 100644 --- a/packages/relayer/mock/event_repository.go +++ b/packages/relayer/mock/event_repository.go @@ -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, diff --git a/packages/relayer/repo/event.go b/packages/relayer/repo/event.go index 655b76f18c2..60ee33b27ff 100644 --- a/packages/relayer/repo/event.go +++ b/packages/relayer/repo/event.go @@ -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, From 23c4695320ac8035e3877b92a9bf392df2ed1c3d Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Thu, 22 Jun 2023 02:13:42 -0700 Subject: [PATCH 2/2] add an index for the new query --- ...50708_alter_events_table_add_msg_hash_event_index.sql | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 packages/relayer/migrations/1666650708_alter_events_table_add_msg_hash_event_index.sql diff --git a/packages/relayer/migrations/1666650708_alter_events_table_add_msg_hash_event_index.sql b/packages/relayer/migrations/1666650708_alter_events_table_add_msg_hash_event_index.sql new file mode 100644 index 00000000000..a384f311ae6 --- /dev/null +++ b/packages/relayer/migrations/1666650708_alter_events_table_add_msg_hash_event_index.sql @@ -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 \ No newline at end of file