Problem: get_asset_tokens_for_public_key query complicated

Solution: Simply the query by removing the operation and allowing only one
public key as argument
This commit is contained in:
Vanshdeep Singh 2018-08-20 14:38:41 +02:00
parent 4b46d3d87c
commit 1cc1397b6c
3 changed files with 11 additions and 14 deletions

View File

@ -310,11 +310,9 @@ def get_validator_set(conn, height=None):
@register_query(LocalMongoDBConnection)
def get_asset_tokens_for_public_keys(conn, asset_id, public_keys, operation=None):
query = {'outputs.public_keys': public_keys,
def get_asset_tokens_for_public_key(conn, asset_id, public_key):
query = {'outputs.public_keys': [public_key],
'asset.id': asset_id}
if operation:
query['operation'] = operation
cursor = conn.run(
conn.collection('transactions').aggregate([

View File

@ -375,12 +375,12 @@ def get_validator_set(conn, height):
@singledispatch
def get_asset_tokens_for_public_keys(connection, asset_id,
public_keys, operation):
"""Retrieve a list of tokens of type `asset_id` that are owned by `public_keys`.
def get_asset_tokens_for_public_key(connection, asset_id,
public_key, operation):
"""Retrieve a list of tokens of type `asset_id` that are owned by the `public_key`.
Args:
asset_id (str): Id of the token.
public_keys (str): list of public keys
public_key (str): base58 encoded public key
operation: filter transaction based on `operation`
Returns:
Iterator of transaction that list given owner in conditions.

View File

@ -156,9 +156,9 @@ class ValidatorElection(Transaction):
return base58.b58encode(bytes.fromhex(election_id))
@classmethod
def count_votes(cls, election_pk, txns, getter=getattr):
def count_votes(cls, election_pk, transactions, getter=getattr):
votes = 0
for txn in txns:
for txn in transactions:
if getter(txn, 'operation') == 'VALIDATOR_ELECTION_VOTE':
for output in getter(txn, 'outputs'):
# NOTE: We enforce that a valid vote to election id will have only
@ -171,10 +171,9 @@ class ValidatorElection(Transaction):
def get_commited_votes(self, bigchain, election_pk=None):
if election_pk is None:
election_pk = self.to_public_key(self.id)
txns = list(backend.query.get_asset_tokens_for_public_keys(bigchain.connection,
self.id,
[election_pk],
'VALIDATOR_ELECTION_VOTE'))
txns = list(backend.query.get_asset_tokens_for_public_key(bigchain.connection,
self.id,
election_pk))
return self.count_votes(election_pk, txns, dict.get)
@classmethod