Skip to content

Commit 6290088

Browse files
committed
Add tests for sorting and filtering
* Rename method (it is not only returning transfers). * Remove commented code (moved and tested).
1 parent f60f3af commit 6290088

File tree

8 files changed

+180
-87
lines changed

8 files changed

+180
-87
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/cloudflare/circl v1.5.0
88
github.com/cockroachdb/pebble v1.1.2
99
github.com/google/go-cmp v0.6.0
10+
github.com/google/martian v2.1.0+incompatible
1011
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0
1112
github.com/pkg/errors v0.9.1
1213
github.com/qubic/go-node-connector v0.10.2

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
148148
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
149149
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
150150
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
151+
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
151152
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
152153
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
153154
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=

rpc/rpc_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ func (s *Server) GetLatestTick(ctx context.Context, _ *emptypb.Empty) (*protobuf
456456
}
457457

458458
func (s *Server) GetTransferTransactionsPerTick(ctx context.Context, req *protobuff.GetTransferTransactionsPerTickRequest) (*protobuff.GetTransferTransactionsPerTickResponse, error) {
459-
txs, err := s.store.GetTransferTransactions(ctx, req.Identity, uint64(req.GetStartTick()), uint64(req.GetEndTick()))
459+
txs, err := s.store.GetTransactionsForEntity(ctx, req.Identity, uint64(req.GetStartTick()), uint64(req.GetEndTick()))
460460
if err != nil {
461461
return nil, status.Errorf(codes.Internal, "getting transfer transactions: %v", err)
462462
}

rpc/v2_endpoints.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func (s *Server) GetIdentityTransfersInTickRangeV2(ctx context.Context, req *pro
328328
pageSize = req.GetPageSize()
329329
}
330330
pageNumber := max(0, int(req.Page)-1) // API index starts with '1', implementation index starts with '0'.
331-
txs, totalCount, err := s.store.GetTransferTransactionsPaged(ctx, req.Identity,
331+
txs, totalCount, err := s.store.GetTransactionsForEntityPaged(ctx, req.Identity,
332332
uint64(req.GetStartTick()), uint64(req.GetEndTick()),
333333
store.Pageable{Page: uint32(pageNumber), Size: pageSize},
334334
store.Sortable{Descending: req.Desc},
@@ -349,11 +349,6 @@ func (s *Server) GetIdentityTransfersInTickRangeV2(ctx context.Context, req *pro
349349
return nil, status.Errorf(codes.Internal, "Got Err: %s when getting transaction info for tx id: %s", err.Error(), transaction.TxId)
350350
}
351351

352-
// TODO move into store and remove me later
353-
//if req.ScOnly == true && transaction.GetInputType() == 0 {
354-
// continue
355-
//}
356-
357352
transactionData := &protobuff.TransactionData{
358353
Transaction: transaction,
359354
Timestamp: transactionInfo.timestamp,
@@ -372,15 +367,6 @@ func (s *Server) GetIdentityTransfersInTickRangeV2(ctx context.Context, req *pro
372367
totalTransactions = append(totalTransactions, transfers)
373368
}
374369

375-
// FIXME move into store and remove me later
376-
//if req.Desc == true {
377-
//
378-
// slices.SortFunc(totalTransactions, func(a, b *protobuff.PerTickIdentityTransfers) int {
379-
// return -cmp.Compare(a.TickNumber, b.TickNumber)
380-
// })
381-
//
382-
//}
383-
384370
return &protobuff.GetIdentityTransfersInTickRangeResponseV2{
385371
Pagination: getPaginationInformation(totalCount, pageNumber+1, int(pageSize)),
386372
Transactions: totalTransactions,

store/store.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/binary"
66
"github.com/cockroachdb/pebble"
7+
"github.com/google/martian/log"
78
"github.com/pkg/errors"
89
"github.com/qubic/go-archiver/protobuff"
910
"go.uber.org/zap"
@@ -411,16 +412,16 @@ type Filterable struct {
411412
ScOnly bool
412413
}
413414

414-
func (s *PebbleStore) GetTransferTransactions(ctx context.Context, identity string, startTick, endTick uint64) ([]*protobuff.TransferTransactionsPerTick, error) {
415-
transfers, _, err := s.GetTransferTransactionsPaged(ctx, identity, startTick, endTick,
416-
Pageable{0, 1000},
417-
Sortable{false},
418-
Filterable{false},
415+
func (s *PebbleStore) GetTransactionsForEntity(ctx context.Context, identity string, startTick, endTick uint64) ([]*protobuff.TransferTransactionsPerTick, error) {
416+
transfers, _, err := s.GetTransactionsForEntityPaged(ctx, identity, startTick, endTick,
417+
Pageable{Size: 1000},
418+
Sortable{},
419+
Filterable{},
419420
)
420421
return transfers, err
421422
}
422423

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

425426
var index, start, end int
426427
start = int(page.Page) * int(page.Size)
@@ -437,9 +438,14 @@ func (s *PebbleStore) GetTransferTransactionsPaged(_ context.Context, identity s
437438
if err != nil {
438439
return nil, -1, errors.Wrap(err, "creating iterator")
439440
}
440-
defer iter.Close()
441+
defer func(iter *pebble.Iterator) {
442+
err := iter.Close()
443+
if err != nil {
444+
log.Errorf("closing iterator: %v", err)
445+
}
446+
}(iter)
441447

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

472-
transactions := filterTransactions(filter, &perTick) // TODO TEST
478+
transactions := filterTransactions(filter, &perTick)
473479

474480
count := len(transactions)
475481
if count > 0 && index+count >= pageStart && index < pageEnd {

0 commit comments

Comments
 (0)