mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Problem: Code comments not updated
Solution: Documents the current behavior of the function calls via code comments
This commit is contained in:
parent
d2e71eaebc
commit
8c6b7b37bd
@ -372,10 +372,10 @@ def get_validator_set(conn, height):
|
|||||||
@singledispatch
|
@singledispatch
|
||||||
def get_asset_tokens_for_public_keys(connection, asset_id,
|
def get_asset_tokens_for_public_keys(connection, asset_id,
|
||||||
public_keys, operation):
|
public_keys, operation):
|
||||||
"""Retrieve a list of `VALIDATOR_ELECTION_VOTE`s `txids` that are owned by `owner`.
|
"""Retrieve a list of tokens of type `asset_id` that are owned by `public_keys`.
|
||||||
Args:
|
Args:
|
||||||
election_id (str): Id of the election.
|
asset_id (str): Id of the token.
|
||||||
election_public_key (str): base58 encoded public key of the election
|
public_keys (str): list of public keys
|
||||||
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.
|
||||||
|
|||||||
@ -131,16 +131,11 @@ class App(BaseApplication):
|
|||||||
else:
|
else:
|
||||||
self.block_txn_hash = block['app_hash']
|
self.block_txn_hash = block['app_hash']
|
||||||
|
|
||||||
# TODO: calculate if an election has concluded
|
# Check if the current block concluded any validator elections and
|
||||||
# NOTE: ensure the local validator set is updated
|
# update the locally tracked validator set
|
||||||
validator_updates = ValidatorElection.get_validator_update(self.bigchaindb,
|
validator_updates = ValidatorElection.get_validator_update(self.bigchaindb,
|
||||||
self.new_height,
|
self.new_height,
|
||||||
self.block_transactions)
|
self.block_transactions)
|
||||||
# if validator_updates:
|
|
||||||
# validator_set = new_validator_set(self.bigchaindb, self.new_height, validator_updates)
|
|
||||||
# self.bigchaindb.store_validator_set(self.new_height+1, validator_set)
|
|
||||||
|
|
||||||
# validator_updates = [vutils.encode_validator(v) for v in validator_updates]
|
|
||||||
|
|
||||||
# Store pre-commit state to recover in case there is a crash
|
# Store pre-commit state to recover in case there is a crash
|
||||||
# during `commit`
|
# during `commit`
|
||||||
|
|||||||
@ -24,7 +24,6 @@ from bigchaindb.common.exceptions import (SchemaValidationError,
|
|||||||
from bigchaindb.tendermint_utils import encode_transaction, merkleroot
|
from bigchaindb.tendermint_utils import encode_transaction, merkleroot
|
||||||
from bigchaindb import exceptions as core_exceptions
|
from bigchaindb import exceptions as core_exceptions
|
||||||
from bigchaindb.consensus import BaseConsensusRules
|
from bigchaindb.consensus import BaseConsensusRules
|
||||||
from bigchaindb.upsert_validator import ValidatorElection, ValidatorElectionVote
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|||||||
@ -152,25 +152,16 @@ 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):
|
def count_votes(cls, election_pk, txns, getter=getattr):
|
||||||
votes = 0
|
votes = 0
|
||||||
for txn in txns:
|
for txn in txns:
|
||||||
if isinstance(txn, dict):
|
if getter(txn, 'operation') == 'VALIDATOR_ELECTION_VOTE':
|
||||||
if txn['operation'] == 'VALIDATOR_ELECTION_VOTE':
|
for output in getter(txn, 'outputs'):
|
||||||
for output in 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
|
# election_pk in the output public keys, including any other public key
|
||||||
# election_pk in the output public keys, including any other public key
|
# along with election_pk will lead to vote being not considered valid.
|
||||||
# along with election_pk will lead to vote being not considered valid.
|
if len(getter(output, 'public_keys')) == 1 and [election_pk] == getter(output, 'public_keys'):
|
||||||
if len(output['public_keys']) == 1 and [election_pk] == output['public_keys']:
|
votes = votes + int(getter(output, 'amount'))
|
||||||
votes = votes + int(output['amount'])
|
|
||||||
else:
|
|
||||||
if txn.operation == 'VALIDATOR_ELECTION_VOTE':
|
|
||||||
for output in txn.outputs:
|
|
||||||
# NOTE: We enforce that a valid vote to election id will have only
|
|
||||||
# election_pk in the output public keys, including any other public key
|
|
||||||
# along with election_pk will lead to vote being not considered valid.
|
|
||||||
if len(output.public_keys) == 1 and [election_pk] == output.public_keys:
|
|
||||||
votes = votes + int(output.amount)
|
|
||||||
return votes
|
return votes
|
||||||
|
|
||||||
def get_commited_votes(self, bigchain, election_pk=None):
|
def get_commited_votes(self, bigchain, election_pk=None):
|
||||||
@ -180,7 +171,7 @@ class ValidatorElection(Transaction):
|
|||||||
self.id,
|
self.id,
|
||||||
[election_pk],
|
[election_pk],
|
||||||
'VALIDATOR_ELECTION_VOTE'))
|
'VALIDATOR_ELECTION_VOTE'))
|
||||||
return self.count_votes(election_pk, txns)
|
return self.count_votes(election_pk, txns, dict.get)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def conclude(cls, bigchain, txn_id, current_votes=[], height=None):
|
def conclude(cls, bigchain, txn_id, current_votes=[], height=None):
|
||||||
@ -210,22 +201,24 @@ class ValidatorElection(Transaction):
|
|||||||
def get_validator_update(cls, bigchain, new_height, txns):
|
def get_validator_update(cls, bigchain, new_height, txns):
|
||||||
votes = {}
|
votes = {}
|
||||||
for txn in txns:
|
for txn in txns:
|
||||||
if isinstance(txn, ValidatorElectionVote):
|
if not isinstance(txn, ValidatorElectionVote):
|
||||||
election_id = txn.asset['id']
|
continue
|
||||||
election_votes = votes.get(election_id, [])
|
|
||||||
election_votes.append(txn)
|
|
||||||
votes[election_id] = election_votes
|
|
||||||
|
|
||||||
election = cls.conclude(bigchain, election_id, election_votes, new_height)
|
election_id = txn.asset['id']
|
||||||
# Once an election concludes any other conclusion for the same
|
election_votes = votes.get(election_id, [])
|
||||||
# or any other election is invalidated
|
election_votes.append(txn)
|
||||||
if election:
|
votes[election_id] = election_votes
|
||||||
# The new validator set comes into effect from height = new_height+1
|
|
||||||
validator_updates = [election.asset['data']]
|
|
||||||
curr_validator_set = bigchain.get_validators(new_height)
|
|
||||||
updated_validator_set = new_validator_set(curr_validator_set,
|
|
||||||
new_height, validator_updates)
|
|
||||||
|
|
||||||
bigchain.store_validator_set(new_height+1, updated_validator_set)
|
election = cls.conclude(bigchain, election_id, election_votes, new_height)
|
||||||
return [encode_validator(election.asset['data'])]
|
# Once an election concludes any other conclusion for the same
|
||||||
|
# or any other election is invalidated
|
||||||
|
if election:
|
||||||
|
# The new validator set comes into effect from height = new_height+1
|
||||||
|
validator_updates = [election.asset['data']]
|
||||||
|
curr_validator_set = bigchain.get_validators(new_height)
|
||||||
|
updated_validator_set = new_validator_set(curr_validator_set,
|
||||||
|
new_height, validator_updates)
|
||||||
|
|
||||||
|
bigchain.store_validator_set(new_height+1, updated_validator_set)
|
||||||
|
return [encode_validator(election.asset['data'])]
|
||||||
return []
|
return []
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user