From 80280da7571e226ad7ac373551cc22d210212a83 Mon Sep 17 00:00:00 2001 From: agodnic Date: Thu, 20 Jul 2023 16:37:45 -0300 Subject: [PATCH] Handle multiple VAAs with the same `txHash` (#564) ### Description Before this pull request, when calling `GET /api/v1/vaas?txHash={h}` for a hash that has multiple VAAs associated with it, we were just returning one. After this pull request, the API should return all of the VAAs associated with that transaction hash. Tracking issue: https://github.com/wormhole-foundation/wormhole-explorer/issues/563 --- api/handlers/vaa/repository.go | 46 ++++++++++----------------- api/handlers/vaa/service.go | 11 +++---- api/routes/wormscan/vaa/controller.go | 7 +--- 3 files changed, 22 insertions(+), 42 deletions(-) diff --git a/api/handlers/vaa/repository.go b/api/handlers/vaa/repository.go index 1899101fd..cc3892bfe 100644 --- a/api/handlers/vaa/repository.go +++ b/api/handlers/vaa/repository.go @@ -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 @@ -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}}}, }) } @@ -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", @@ -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 @@ -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 } @@ -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()} } diff --git a/api/handlers/vaa/service.go b/api/handlers/vaa/service.go index 2b26e05ca..3db434d7b 100644 --- a/api/handlers/vaa/service.go +++ b/api/handlers/vaa/service.go @@ -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 } @@ -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 } diff --git a/api/routes/wormscan/vaa/controller.go b/api/routes/wormscan/vaa/controller.go index 29c558b72..56743f7fd 100644 --- a/api/routes/wormscan/vaa/controller.go +++ b/api/routes/wormscan/vaa/controller.go @@ -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 }