mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
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:
parent
4b46d3d87c
commit
1cc1397b6c
@ -310,11 +310,9 @@ def get_validator_set(conn, height=None):
|
|||||||
|
|
||||||
|
|
||||||
@register_query(LocalMongoDBConnection)
|
@register_query(LocalMongoDBConnection)
|
||||||
def get_asset_tokens_for_public_keys(conn, asset_id, public_keys, operation=None):
|
def get_asset_tokens_for_public_key(conn, asset_id, public_key):
|
||||||
query = {'outputs.public_keys': public_keys,
|
query = {'outputs.public_keys': [public_key],
|
||||||
'asset.id': asset_id}
|
'asset.id': asset_id}
|
||||||
if operation:
|
|
||||||
query['operation'] = operation
|
|
||||||
|
|
||||||
cursor = conn.run(
|
cursor = conn.run(
|
||||||
conn.collection('transactions').aggregate([
|
conn.collection('transactions').aggregate([
|
||||||
|
|||||||
@ -375,12 +375,12 @@ def get_validator_set(conn, height):
|
|||||||
|
|
||||||
|
|
||||||
@singledispatch
|
@singledispatch
|
||||||
def get_asset_tokens_for_public_keys(connection, asset_id,
|
def get_asset_tokens_for_public_key(connection, asset_id,
|
||||||
public_keys, operation):
|
public_key, operation):
|
||||||
"""Retrieve a list of tokens of type `asset_id` that are owned by `public_keys`.
|
"""Retrieve a list of tokens of type `asset_id` that are owned by the `public_key`.
|
||||||
Args:
|
Args:
|
||||||
asset_id (str): Id of the token.
|
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`
|
operation: filter transaction based on `operation`
|
||||||
Returns:
|
Returns:
|
||||||
Iterator of transaction that list given owner in conditions.
|
Iterator of transaction that list given owner in conditions.
|
||||||
|
|||||||
@ -156,9 +156,9 @@ class ValidatorElection(Transaction):
|
|||||||
return base58.b58encode(bytes.fromhex(election_id))
|
return base58.b58encode(bytes.fromhex(election_id))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def count_votes(cls, election_pk, txns, getter=getattr):
|
def count_votes(cls, election_pk, transactions, getter=getattr):
|
||||||
votes = 0
|
votes = 0
|
||||||
for txn in txns:
|
for txn in transactions:
|
||||||
if getter(txn, 'operation') == 'VALIDATOR_ELECTION_VOTE':
|
if getter(txn, 'operation') == 'VALIDATOR_ELECTION_VOTE':
|
||||||
for output in getter(txn, 'outputs'):
|
for output in getter(txn, 'outputs'):
|
||||||
# NOTE: We enforce that a valid vote to election id will have only
|
# 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):
|
def get_commited_votes(self, bigchain, election_pk=None):
|
||||||
if election_pk is None:
|
if election_pk is None:
|
||||||
election_pk = self.to_public_key(self.id)
|
election_pk = self.to_public_key(self.id)
|
||||||
txns = list(backend.query.get_asset_tokens_for_public_keys(bigchain.connection,
|
txns = list(backend.query.get_asset_tokens_for_public_key(bigchain.connection,
|
||||||
self.id,
|
self.id,
|
||||||
[election_pk],
|
election_pk))
|
||||||
'VALIDATOR_ELECTION_VOTE'))
|
|
||||||
return self.count_votes(election_pk, txns, dict.get)
|
return self.count_votes(election_pk, txns, dict.get)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user