From 1cc1397b6c536ddfc74471dd5e5867000759eace Mon Sep 17 00:00:00 2001 From: Vanshdeep Singh Date: Mon, 20 Aug 2018 14:38:41 +0200 Subject: [PATCH] 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 --- bigchaindb/backend/localmongodb/query.py | 6 ++---- bigchaindb/backend/query.py | 8 ++++---- bigchaindb/upsert_validator/validator_election.py | 11 +++++------ 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/bigchaindb/backend/localmongodb/query.py b/bigchaindb/backend/localmongodb/query.py index ed17485a..b4508477 100644 --- a/bigchaindb/backend/localmongodb/query.py +++ b/bigchaindb/backend/localmongodb/query.py @@ -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([ diff --git a/bigchaindb/backend/query.py b/bigchaindb/backend/query.py index 62b3fb09..7527c863 100644 --- a/bigchaindb/backend/query.py +++ b/bigchaindb/backend/query.py @@ -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. diff --git a/bigchaindb/upsert_validator/validator_election.py b/bigchaindb/upsert_validator/validator_election.py index 1e19ec1f..6a8db205 100644 --- a/bigchaindb/upsert_validator/validator_election.py +++ b/bigchaindb/upsert_validator/validator_election.py @@ -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