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

Handle multiple VAAs with the same txHash #564

Merged
merged 2 commits into from
Jul 20, 2023
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
46 changes: 17 additions & 29 deletions api/handlers/vaa/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ func (r *Repository) FindVaasByTxHashWorkaround(

// Find VAAs that match the given VAA ID
q := *query // making a copy to avoid modifying the struct passed by the caller
q.SetID(globalTxs[0].ID)
var ids []string
for i := range globalTxs {
ids = append(ids, globalTxs[i].ID)
}
q.SetIDs(ids)
// Disable txHash filter, but keep all the other filters.
// We have to do this because the transaction hashes in the `globalTransactions` collection
// may be different that the transaction hash in the `vaas` collection. This is the case
Expand All @@ -128,10 +132,15 @@ func (r *Repository) FindVaas(
{"$sort", bson.D{q.getSortPredicate()}},
})

// filter by _id
if q.id != "" {
// filter by VAA ids (potentially more than one)
if len(q.ids) > 0 {
var array bson.A
for _, id := range q.ids {
predicate := bson.D{bson.E{"_id", id}}
array = append(array, predicate)
}
pipeline = append(pipeline, bson.D{
{"$match", bson.D{bson.E{"_id", q.id}}},
{"$match", bson.D{{"$or", array}}},
})
}

Expand Down Expand Up @@ -294,7 +303,7 @@ func (r *Repository) FindVaas(
// GetVaaCount get a count of vaa by chainID.
func (r *Repository) GetVaaCount(ctx context.Context, q *VaaQuery) ([]*VaaStats, error) {

cur, err := r.collections.vaaCount.Find(ctx, q.toBSON(), q.findOptions())
cur, err := r.collections.vaaCount.Find(ctx, bson.D{}, q.findOptions())
if err != nil {
requestID := fmt.Sprintf("%v", ctx.Value("requestid"))
r.logger.Error("failed execute Find command to get vaaCount",
Expand All @@ -315,7 +324,7 @@ func (r *Repository) GetVaaCount(ctx context.Context, q *VaaQuery) ([]*VaaStats,
// VaaQuery respresent a query for the vaa mongodb document.
type VaaQuery struct {
pagination.Pagination
id string
ids []string
chainId sdk.ChainID
emitter string
sequence string
Expand All @@ -330,9 +339,8 @@ func Query() *VaaQuery {
return &VaaQuery{Pagination: *p}
}

// SetChain sets the id field of the VaaQuery struct.
func (q *VaaQuery) SetID(id string) *VaaQuery {
q.id = id
func (q *VaaQuery) SetIDs(ids []string) *VaaQuery {
q.ids = ids
return q
}

Expand Down Expand Up @@ -376,26 +384,6 @@ func (q *VaaQuery) IncludeParsedPayload(val bool) *VaaQuery {
return q
}

func (q *VaaQuery) toBSON() *bson.D {
r := bson.D{}
if q.id != "" {
r = append(r, bson.E{"_id", q.id})
}
if q.chainId > 0 {
r = append(r, bson.E{"emitterChain", q.chainId})
}
if q.emitter != "" {
r = append(r, bson.E{"emitterAddr", q.emitter})
}
if q.sequence != "" {
r = append(r, bson.E{"sequence", q.sequence})
}
if q.txHash != "" {
r = append(r, bson.E{"txHash", q.txHash})
}
return &r
}

func (q *VaaQuery) getSortPredicate() bson.E {
return bson.E{"timestamp", q.GetSortInt()}
}
Expand Down
11 changes: 4 additions & 7 deletions api/handlers/vaa/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *Service) FindAll(
return nil, err
}

// Eeturn the matching documents
// Return the matching documents
res := response.Response[[]*VaaDoc]{Data: vaas}
return &res, nil
}
Expand Down Expand Up @@ -183,12 +183,9 @@ func (s *Service) findById(
}

// GetVaaCount get a list a list of vaa count grouped by chainID.
func (s *Service) GetVaaCount(ctx context.Context, p *pagination.Pagination) (*response.Response[[]*VaaStats], error) {
if p == nil {
p = pagination.Default()
}
query := Query().SetPagination(p)
stats, err := s.repo.GetVaaCount(ctx, query)
func (s *Service) GetVaaCount(ctx context.Context) (*response.Response[[]*VaaStats], error) {
q := Query()
stats, err := s.repo.GetVaaCount(ctx, q)
res := response.Response[[]*VaaStats]{Data: stats}
return &res, err
}
Expand Down
7 changes: 1 addition & 6 deletions api/routes/wormscan/vaa/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,7 @@ func (c *Controller) FindById(ctx *fiber.Ctx) error {
// @Router /api/v1/vaas/vaa-counts [get]
func (c *Controller) GetVaaCount(ctx *fiber.Ctx) error {

p, err := middleware.ExtractPagination(ctx)
if err != nil {
return err
}

vaas, err := c.srv.GetVaaCount(ctx.Context(), p)
vaas, err := c.srv.GetVaaCount(ctx.Context())
if err != nil {
return err
}
Expand Down
Loading