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

chore(relayer): avoid copy big struct #17686

Merged
merged 8 commits into from
Jun 27, 2024
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
4 changes: 2 additions & 2 deletions packages/relayer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ type FindAllByAddressOpts struct {

// EventRepository is used to interact with events in the store
type EventRepository interface {
Save(ctx context.Context, opts SaveEventOpts) (*Event, error)
Save(ctx context.Context, opts *SaveEventOpts) (*Event, error)
UpdateStatus(ctx context.Context, id int, status EventStatus) error
UpdateFeesAndProfitability(ctx context.Context, id int, opts UpdateFeesAndProfitabilityOpts) error
UpdateFeesAndProfitability(ctx context.Context, id int, opts *UpdateFeesAndProfitabilityOpts) error
FindAllByAddress(
ctx context.Context,
req *http.Request,
Expand Down
6 changes: 2 additions & 4 deletions packages/relayer/indexer/handle_chain_data_synced_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (i *Indexer) handleChainDataSyncedEvent(
return errors.Wrap(err, "json.Marshal(event)")
}

opts := relayer.SaveEventOpts{
_, err = i.eventRepo.Save(ctx, &relayer.SaveEventOpts{
Name: relayer.EventNameChainDataSynced,
Event: relayer.EventNameChainDataSynced,
Data: string(marshaled),
Expand All @@ -65,9 +65,7 @@ func (i *Indexer) handleChainDataSyncedEvent(
SyncData: common.BytesToHash(event.Data[:]).Hex(),
Kind: common.BytesToHash(event.Kind[:]).Hex(),
SyncedInBlockID: event.Raw.BlockNumber,
}

_, err = i.eventRepo.Save(ctx, opts)
})
if err != nil {
return errors.Wrap(err, "i.eventRepo.Save")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (i *Indexer) handleMessageStatusChangedEvent(
return nil
}

_, err = i.eventRepo.Save(ctx, relayer.SaveEventOpts{
_, err = i.eventRepo.Save(ctx, &relayer.SaveEventOpts{
Name: relayer.EventNameMessageStatusChanged,
Data: string(marshaled),
ChainID: chainID,
Expand Down
2 changes: 1 addition & 1 deletion packages/relayer/indexer/save_event_to_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (i *Indexer) saveEventToDB(
opts.CanonicalTokenDecimals = canonicalToken.TokenDecimals()
}

e, err := i.eventRepo.Save(ctx, opts)
e, err := i.eventRepo.Save(ctx, &opts)
if err != nil {
return 0, errors.Wrap(err, "svc.eventRepo.Save")
}
Expand Down
2 changes: 1 addition & 1 deletion packages/relayer/pkg/http/get_events_by_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func Test_GetEventsByAddress(t *testing.T) {
srv := newTestServer("")

_, err := srv.eventRepo.Save(context.Background(), relayer.SaveEventOpts{
_, err := srv.eventRepo.Save(context.Background(), &relayer.SaveEventOpts{
Name: "name",
Data: `{"Owner": "0x0000000000000000000000000000000000000123"}`,
ChainID: big.NewInt(167001),
Expand Down
4 changes: 2 additions & 2 deletions packages/relayer/pkg/mock/event_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewEventRepository() *EventRepository {
events: make([]*relayer.Event, 0),
}
}
func (r *EventRepository) Save(ctx context.Context, opts relayer.SaveEventOpts) (*relayer.Event, error) {
func (r *EventRepository) Save(ctx context.Context, opts *relayer.SaveEventOpts) (*relayer.Event, error) {
r.events = append(r.events, &relayer.Event{
ID: rand.Int(), // nolint: gosec
Data: datatypes.JSON(opts.Data),
Expand Down Expand Up @@ -65,7 +65,7 @@ func (r *EventRepository) UpdateStatus(ctx context.Context, id int, status relay

func (r *EventRepository) UpdateFeesAndProfitability(
ctx context.Context,
id int, opts relayer.UpdateFeesAndProfitabilityOpts,
id int, opts *relayer.UpdateFeesAndProfitabilityOpts,
) error {
var event *relayer.Event

Expand Down
4 changes: 2 additions & 2 deletions packages/relayer/pkg/repo/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewEventRepository(db DB) (*EventRepository, error) {
}, nil
}

func (r *EventRepository) Save(ctx context.Context, opts relayer.SaveEventOpts) (*relayer.Event, error) {
func (r *EventRepository) Save(ctx context.Context, opts *relayer.SaveEventOpts) (*relayer.Event, error) {
e := &relayer.Event{
Data: datatypes.JSON(opts.Data),
Status: opts.Status,
Expand Down Expand Up @@ -62,7 +62,7 @@ func (r *EventRepository) Save(ctx context.Context, opts relayer.SaveEventOpts)
func (r *EventRepository) UpdateFeesAndProfitability(
ctx context.Context,
id int,
opts relayer.UpdateFeesAndProfitabilityOpts,
opts *relayer.UpdateFeesAndProfitabilityOpts,
) error {
e := &relayer.Event{}
if err := r.db.GormDB().Where("id = ?", id).First(e).Error; err != nil {
Expand Down
14 changes: 7 additions & 7 deletions packages/relayer/pkg/repo/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ func TestIntegration_Event_Save(t *testing.T) {
assert.Equal(t, nil, err)
tests := []struct {
name string
opts relayer.SaveEventOpts
opts *relayer.SaveEventOpts
wantErr error
}{
{
"success",
relayer.SaveEventOpts{
&relayer.SaveEventOpts{
Name: "test",
ChainID: big.NewInt(1),
DestChainID: big.NewInt(2),
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestIntegration_Event_UpdateStatus(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "success" {
_, err := eventRepo.Save(context.Background(),
relayer.SaveEventOpts{
&relayer.SaveEventOpts{
Name: "test",
ChainID: big.NewInt(1),
DestChainID: big.NewInt(2),
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestIntegration_Event_FindAllByAddress(t *testing.T) {

addr := common.HexToAddress("0x71C7656EC7ab88b098defB751B7401B5f6d8976F")

_, err = eventRepo.Save(context.Background(), relayer.SaveEventOpts{
_, err = eventRepo.Save(context.Background(), &relayer.SaveEventOpts{
Name: "name",
Data: fmt.Sprintf(`{"Message": {"Owner": "%s"}}`, strings.ToLower(addr.Hex())),
ChainID: big.NewInt(1),
Expand All @@ -243,7 +243,7 @@ func TestIntegration_Event_FindAllByAddress(t *testing.T) {

assert.Equal(t, nil, err)

_, err = eventRepo.Save(context.Background(), relayer.SaveEventOpts{
_, err = eventRepo.Save(context.Background(), &relayer.SaveEventOpts{
Name: "name",
Data: fmt.Sprintf(`{"Message": {"Owner": "%s"}}`, strings.ToLower(addr.Hex())),
ChainID: big.NewInt(1),
Expand All @@ -262,7 +262,7 @@ func TestIntegration_Event_FindAllByAddress(t *testing.T) {
})
assert.Equal(t, nil, err)

_, err = eventRepo.Save(context.Background(), relayer.SaveEventOpts{
_, err = eventRepo.Save(context.Background(), &relayer.SaveEventOpts{
Name: "name",
Data: fmt.Sprintf(`{"Message": {"Owner": "%s"}}`, strings.ToLower(addr.Hex())),
ChainID: big.NewInt(1),
Expand Down Expand Up @@ -443,7 +443,7 @@ func TestIntegration_Event_FirstByMsgHash(t *testing.T) {
eventRepo, err := NewEventRepository(db)
assert.Equal(t, nil, err)

_, err = eventRepo.Save(context.Background(), relayer.SaveEventOpts{
_, err = eventRepo.Save(context.Background(), &relayer.SaveEventOpts{
Name: "name",
Data: fmt.Sprintf(`{"Message": {"Owner": "%s"}}`, strings.ToLower(addr.Hex())),
ChainID: big.NewInt(1),
Expand Down
2 changes: 1 addition & 1 deletion packages/relayer/processor/is_profitable.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p *Processor) isProfitable(
EstimatedOnchainFee: estimatedOnchainFee,
}

if err := p.eventRepo.UpdateFeesAndProfitability(ctx, id, opts); err != nil {
if err := p.eventRepo.UpdateFeesAndProfitability(ctx, id, &opts); err != nil {
slog.Error("failed to update event", "error", err)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/relayer/processor/process_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func (p *Processor) saveMessageStatusChangedEvent(
// keep same format as other raw events
data := fmt.Sprintf(`{"Raw":{"transactionHash": "%v"}}`, receipt.TxHash.Hex())

_, err = p.eventRepo.Save(ctx, relayer.SaveEventOpts{
_, err = p.eventRepo.Save(ctx, &relayer.SaveEventOpts{
Name: relayer.EventNameMessageStatusChanged,
Data: data,
EmittedBlockID: event.Raw.BlockNumber,
Expand Down
Loading