From 87c6e20340757d3bdd6075afb8b5cd264cc511a3 Mon Sep 17 00:00:00 2001 From: jeff <113397187+cyberhorsey@users.noreply.github.com> Date: Mon, 12 Jun 2023 01:13:55 -0700 Subject: [PATCH] fix(relayer): only need to find first msg hash (#13966) --- packages/relayer/event.go | 4 +- .../indexer/detect_and_handle_reorg.go | 26 ++++-------- .../save_message_status_changed_events.go | 10 ++--- packages/relayer/mock/event_repository.go | 10 ++--- packages/relayer/repo/event.go | 10 ++--- packages/relayer/repo/event_test.go | 40 +++++++++---------- 6 files changed, 42 insertions(+), 58 deletions(-) diff --git a/packages/relayer/event.go b/packages/relayer/event.go index f50e7380bd7..f61c9f8af31 100644 --- a/packages/relayer/event.go +++ b/packages/relayer/event.go @@ -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 } diff --git a/packages/relayer/indexer/detect_and_handle_reorg.go b/packages/relayer/indexer/detect_and_handle_reorg.go index b354c20f6c8..fd1beb8a78b 100644 --- a/packages/relayer/indexer/detect_and_handle_reorg.go +++ b/packages/relayer/indexer/detect_and_handle_reorg.go @@ -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 diff --git a/packages/relayer/indexer/save_message_status_changed_events.go b/packages/relayer/indexer/save_message_status_changed_events.go index ba74f596913..d50900a4b44 100644 --- a/packages/relayer/indexer/save_message_status_changed_events.go +++ b/packages/relayer/indexer/save_message_status_changed_events.go @@ -58,13 +58,13 @@ 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{ @@ -72,7 +72,7 @@ func (svc *Service) saveMessageStatusChangedEvent( 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, }) diff --git a/packages/relayer/mock/event_repository.go b/packages/relayer/mock/event_repository.go index 53208a11ad8..039509661e1 100644 --- a/packages/relayer/mock/event_repository.go +++ b/packages/relayer/mock/event_repository.go @@ -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( diff --git a/packages/relayer/repo/event.go b/packages/relayer/repo/event.go index 9d413aa673e..eec79c7e8a6 100644 --- a/packages/relayer/repo/event.go +++ b/packages/relayer/repo/event.go @@ -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 diff --git a/packages/relayer/repo/event_test.go b/packages/relayer/repo/event_test.go index 183bd29ebe2..59af84ec478 100644 --- a/packages/relayer/repo/event_test.go +++ b/packages/relayer/repo/event_test.go @@ -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) @@ -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) })