Skip to content

Commit

Permalink
Add tests for sorting and filtering
Browse files Browse the repository at this point in the history
* Rename method (it is not only returning transfers).
* Remove commented code (moved and tested).
  • Loading branch information
qubicmio committed Jan 21, 2025
1 parent f60f3af commit 6290088
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 87 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/cloudflare/circl v1.5.0
github.com/cockroachdb/pebble v1.1.2
github.com/google/go-cmp v0.6.0
github.com/google/martian v2.1.0+incompatible
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0
github.com/pkg/errors v0.9.1
github.com/qubic/go-node-connector v0.10.2
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down
2 changes: 1 addition & 1 deletion rpc/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func (s *Server) GetLatestTick(ctx context.Context, _ *emptypb.Empty) (*protobuf
}

func (s *Server) GetTransferTransactionsPerTick(ctx context.Context, req *protobuff.GetTransferTransactionsPerTickRequest) (*protobuff.GetTransferTransactionsPerTickResponse, error) {
txs, err := s.store.GetTransferTransactions(ctx, req.Identity, uint64(req.GetStartTick()), uint64(req.GetEndTick()))
txs, err := s.store.GetTransactionsForEntity(ctx, req.Identity, uint64(req.GetStartTick()), uint64(req.GetEndTick()))
if err != nil {
return nil, status.Errorf(codes.Internal, "getting transfer transactions: %v", err)
}
Expand Down
16 changes: 1 addition & 15 deletions rpc/v2_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func (s *Server) GetIdentityTransfersInTickRangeV2(ctx context.Context, req *pro
pageSize = req.GetPageSize()
}
pageNumber := max(0, int(req.Page)-1) // API index starts with '1', implementation index starts with '0'.
txs, totalCount, err := s.store.GetTransferTransactionsPaged(ctx, req.Identity,
txs, totalCount, err := s.store.GetTransactionsForEntityPaged(ctx, req.Identity,
uint64(req.GetStartTick()), uint64(req.GetEndTick()),
store.Pageable{Page: uint32(pageNumber), Size: pageSize},
store.Sortable{Descending: req.Desc},
Expand All @@ -349,11 +349,6 @@ func (s *Server) GetIdentityTransfersInTickRangeV2(ctx context.Context, req *pro
return nil, status.Errorf(codes.Internal, "Got Err: %s when getting transaction info for tx id: %s", err.Error(), transaction.TxId)
}

// TODO move into store and remove me later
//if req.ScOnly == true && transaction.GetInputType() == 0 {
// continue
//}

transactionData := &protobuff.TransactionData{
Transaction: transaction,
Timestamp: transactionInfo.timestamp,
Expand All @@ -372,15 +367,6 @@ func (s *Server) GetIdentityTransfersInTickRangeV2(ctx context.Context, req *pro
totalTransactions = append(totalTransactions, transfers)
}

// FIXME move into store and remove me later
//if req.Desc == true {
//
// slices.SortFunc(totalTransactions, func(a, b *protobuff.PerTickIdentityTransfers) int {
// return -cmp.Compare(a.TickNumber, b.TickNumber)
// })
//
//}

return &protobuff.GetIdentityTransfersInTickRangeResponseV2{
Pagination: getPaginationInformation(totalCount, pageNumber+1, int(pageSize)),
Transactions: totalTransactions,
Expand Down
24 changes: 15 additions & 9 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/binary"
"github.com/cockroachdb/pebble"
"github.com/google/martian/log"
"github.com/pkg/errors"
"github.com/qubic/go-archiver/protobuff"
"go.uber.org/zap"
Expand Down Expand Up @@ -411,16 +412,16 @@ type Filterable struct {
ScOnly bool
}

func (s *PebbleStore) GetTransferTransactions(ctx context.Context, identity string, startTick, endTick uint64) ([]*protobuff.TransferTransactionsPerTick, error) {
transfers, _, err := s.GetTransferTransactionsPaged(ctx, identity, startTick, endTick,
Pageable{0, 1000},
Sortable{false},
Filterable{false},
func (s *PebbleStore) GetTransactionsForEntity(ctx context.Context, identity string, startTick, endTick uint64) ([]*protobuff.TransferTransactionsPerTick, error) {
transfers, _, err := s.GetTransactionsForEntityPaged(ctx, identity, startTick, endTick,
Pageable{Size: 1000},
Sortable{},
Filterable{},
)
return transfers, err
}

func (s *PebbleStore) GetTransferTransactionsPaged(_ context.Context, identity string, startTick, endTick uint64, page Pageable, sort Sortable, filter Filterable) ([]*protobuff.TransferTransactionsPerTick, int, error) {
func (s *PebbleStore) GetTransactionsForEntityPaged(_ context.Context, identity string, startTick, endTick uint64, page Pageable, sort Sortable, filter Filterable) ([]*protobuff.TransferTransactionsPerTick, int, error) {

var index, start, end int
start = int(page.Page) * int(page.Size)
Expand All @@ -437,9 +438,14 @@ func (s *PebbleStore) GetTransferTransactionsPaged(_ context.Context, identity s
if err != nil {
return nil, -1, errors.Wrap(err, "creating iterator")
}
defer iter.Close()
defer func(iter *pebble.Iterator) {
err := iter.Close()
if err != nil {
log.Errorf("closing iterator: %v", err)
}
}(iter)

if sort.Descending { // TODO test
if sort.Descending {
for iter.Last(); iter.Valid(); iter.Prev() {
index, transferTxs, err = getTransfersPage(iter, index, transferTxs, start, end, filter)
}
Expand Down Expand Up @@ -469,7 +475,7 @@ func getTransfersPage(iter *pebble.Iterator, index int, transferTxs []*protobuff
return -1, nil, errors.Wrap(err, "unmarshalling transfer tx per tick to protobuff type")
}

transactions := filterTransactions(filter, &perTick) // TODO TEST
transactions := filterTransactions(filter, &perTick)

count := len(transactions)
if count > 0 && index+count >= pageStart && index < pageEnd {
Expand Down
Loading

0 comments on commit 6290088

Please sign in to comment.