Skip to content

Commit

Permalink
indexer: GetEnvelopes now returns a TotalCount
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Jul 19, 2024
1 parent c1970ee commit cdc086b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
41 changes: 24 additions & 17 deletions vochain/indexer/db/votes.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 13 additions & 9 deletions vochain/indexer/queries/votes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ LIMIT 1;
SELECT COUNT(*) FROM votes;

-- name: SearchVotes :many
SELECT v.*, t.hash FROM votes AS v
LEFT JOIN transactions AS t
ON v.block_height = t.block_height
AND v.block_index = t.block_index
WHERE (sqlc.arg(process_id) = '' OR process_id = sqlc.arg(process_id))
AND (sqlc.arg(nullifier_substr) = '' OR (INSTR(LOWER(HEX(nullifier)), sqlc.arg(nullifier_substr)) > 0))
ORDER BY v.block_height DESC, v.nullifier ASC
WITH results AS (
SELECT v.*, t.hash
FROM votes AS v
LEFT JOIN transactions AS t
ON v.block_height = t.block_height
AND v.block_index = t.block_index
WHERE (sqlc.arg(process_id) = '' OR process_id = sqlc.arg(process_id))
AND (sqlc.arg(nullifier_substr) = '' OR (INSTR(LOWER(HEX(nullifier)), sqlc.arg(nullifier_substr)) > 0))
)
SELECT *, COUNT(*) OVER() AS total_count
FROM results
ORDER BY block_height DESC, nullifier ASC
LIMIT sqlc.arg(limit)
OFFSET sqlc.arg(offset)
;
OFFSET sqlc.arg(offset);
23 changes: 12 additions & 11 deletions vochain/indexer/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,27 @@ func (idx *Indexer) GetEnvelope(nullifier []byte) (*indexertypes.EnvelopePackage
// Returns ErrVoteNotFound if the envelope reference is not found.
func (idx *Indexer) GetEnvelopes(processId []byte, max, from int,
searchTerm string,
) ([]*indexertypes.EnvelopeMetadata, error) {
) ([]*indexertypes.EnvelopeMetadata, uint64, error) {
if from < 0 {
return nil, fmt.Errorf("GetEnvelopes: invalid value: from is invalid value %d", from)
return nil, 0, fmt.Errorf("GetEnvelopes: invalid value: from is invalid value %d", from)
}
if max <= 0 {
return nil, fmt.Errorf("GetEnvelopes: invalid value: max is invalid value %d", max)
return nil, 0, fmt.Errorf("GetEnvelopes: invalid value: max is invalid value %d", max)
}
envelopes := []*indexertypes.EnvelopeMetadata{}
txRefs, err := idx.readOnlyQuery.SearchVotes(context.TODO(), indexerdb.SearchVotesParams{
results, err := idx.readOnlyQuery.SearchVotes(context.TODO(), indexerdb.SearchVotesParams{
ProcessID: processId,
NullifierSubstr: strings.ToLower(searchTerm), // we search in lowercase
Limit: int64(max),
Offset: int64(from),
})
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrVoteNotFound
return nil, 0, ErrVoteNotFound
}
return nil, err
return nil, 0, err
}
for _, txRef := range txRefs {
list := []*indexertypes.EnvelopeMetadata{}
for _, txRef := range results {
envelopeMetadata := &indexertypes.EnvelopeMetadata{
ProcessId: txRef.ProcessID,
Nullifier: txRef.Nullifier,
Expand All @@ -92,11 +92,12 @@ func (idx *Indexer) GetEnvelopes(processId []byte, max, from int,
TxHash: txRef.Hash,
}
if len(txRef.VoterID) > 0 {
envelopeMetadata.VoterID = txRef.VoterID.Address()
envelopeMetadata.VoterID = state.VoterID(txRef.VoterID).Address()
}
envelopes = append(envelopes, envelopeMetadata)
list = append(list, envelopeMetadata)

}
return envelopes, nil
return list, uint64(results[0].TotalCount), nil
}

// CountTotalVotes returns the total number of envelopes.
Expand Down

0 comments on commit cdc086b

Please sign in to comment.