Skip to content

Commit

Permalink
Handle multiple VAAs with the same txHash (#564)
Browse files Browse the repository at this point in the history
### 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: #563
  • Loading branch information
agodnic committed Jul 20, 2023
1 parent d8bb9c4 commit 80280da
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 42 deletions.
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

0 comments on commit 80280da

Please sign in to comment.