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
This commit is contained in:
agodnic 2023-07-20 16:37:45 -03:00 committed by GitHub
parent d8bb9c46f6
commit 80280da757
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 42 deletions

View File

@ -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()}
}

View File

@ -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
}

View File

@ -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
}