Skip to content

Commit

Permalink
Sort tx with correct order
Browse files Browse the repository at this point in the history
  • Loading branch information
kacpersaw committed Jan 11, 2024
1 parent 428877f commit 1163b29
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
5 changes: 4 additions & 1 deletion internal/service/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ func (e *Service) GetAccountTransactions(ctx context.Context, accountID string,
bson.D{{Key: "receiver", Value: addr.String()}},
}},
}
return e.getTransactions(ctx, filter, e.getFindOptions("counter", page, perPage))

return e.getTransactions(ctx, filter, e.getFindOptionsSort(bson.D{
{Key: "layer", Value: -1}, {Key: "blockIndex", Value: -1},
}, page, perPage))
}

// GetAccountRewards returns rewards by account id.
Expand Down
4 changes: 3 additions & 1 deletion internal/service/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func (e *Service) GetLayers(ctx context.Context, page, perPage int64) (layers []

// GetLayerTransactions returns transactions for layer.
func (e *Service) GetLayerTransactions(ctx context.Context, layerNum int, page, perPage int64) (txs []*model.Transaction, total int64, err error) {
return e.getTransactions(ctx, &bson.D{{Key: "layer", Value: layerNum}}, e.getFindOptions("blockIndex", page, perPage))
return e.getTransactions(ctx, &bson.D{{Key: "layer", Value: layerNum}}, e.getFindOptionsSort(bson.D{
{Key: "blockIndex", Value: 1},
}, page, perPage))
}

// GetLayerSmeshers returns smeshers for layer.
Expand Down
8 changes: 8 additions & 0 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ func (e *Service) getFindOptions(key string, page, perPage int64) *options.FindO
SetProjection(bson.D{{Key: "_id", Value: 0}})
}

func (e *Service) getFindOptionsSort(sort bson.D, page, perPage int64) *options.FindOptions {
return options.Find().
SetSort(sort).
SetLimit(perPage).
SetSkip((page - 1) * perPage).
SetProjection(bson.D{{Key: "_id", Value: 0}})
}

func (e *Service) getEpochLayers(epoch int) (uint32, uint32) {
e.networkInfoMU.RLock()
net := e.networkInfo
Expand Down
4 changes: 3 additions & 1 deletion internal/service/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ func (e *Service) GetTransaction(ctx context.Context, txID string) (*model.Trans

// GetTransactions returns txs by filter.
func (e *Service) GetTransactions(ctx context.Context, page, perPage int64) (txs []*model.Transaction, total int64, err error) {
return e.getTransactions(ctx, &bson.D{}, e.getFindOptions("layer", page, perPage))
return e.getTransactions(ctx, &bson.D{}, e.getFindOptionsSort(bson.D{
{Key: "layer", Value: -1}, {Key: "blockIndex", Value: -1},
}, page, perPage))
}

func (e *Service) getTransactions(ctx context.Context, filter *bson.D, options *options.FindOptions) (txs []*model.Transaction, total int64, err error) {
Expand Down

0 comments on commit 1163b29

Please sign in to comment.