mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Problem: ValidatorElection and MigrationElection need to inherit from a common Election class
Solution: Factored the common logic out of `ValidatorElection` and moved it to `Election` parent class
This commit is contained in:
parent
7de4226666
commit
3d67565bda
@ -98,5 +98,5 @@ from bigchaindb.upsert_validator import ValidatorElectionVote # noqa
|
||||
|
||||
Transaction.register_type(Transaction.CREATE, models.Transaction)
|
||||
Transaction.register_type(Transaction.TRANSFER, models.Transaction)
|
||||
Transaction.register_type(ValidatorElection.VALIDATOR_ELECTION, ValidatorElection)
|
||||
Transaction.register_type(ValidatorElection.ELECTION_TYPE, ValidatorElection)
|
||||
Transaction.register_type(ValidatorElectionVote.VALIDATOR_ELECTION_VOTE, ValidatorElectionVote)
|
||||
|
||||
@ -300,11 +300,11 @@ def get_validator_set(conn, height=None):
|
||||
|
||||
|
||||
@register_query(LocalMongoDBConnection)
|
||||
def get_validator_set_by_election_id(conn, election_id):
|
||||
def get_result_by_election_id(conn, election_id, table):
|
||||
query = {'election_id': election_id}
|
||||
|
||||
cursor = conn.run(
|
||||
conn.collection('validators')
|
||||
conn.collection(table)
|
||||
.find(query, projection={'_id': False})
|
||||
)
|
||||
|
||||
|
||||
@ -361,7 +361,7 @@ def get_validator_set(conn, height):
|
||||
|
||||
|
||||
@singledispatch
|
||||
def get_validator_set_by_election_id(conn, election_id):
|
||||
def get_result_by_election_id(conn, election_id, table):
|
||||
"""Return a validator set change with the specified election_id
|
||||
"""
|
||||
|
||||
@ -369,13 +369,11 @@ def get_validator_set_by_election_id(conn, election_id):
|
||||
|
||||
|
||||
@singledispatch
|
||||
def get_asset_tokens_for_public_key(connection, asset_id,
|
||||
public_key, operation):
|
||||
def get_asset_tokens_for_public_key(connection, asset_id, public_key):
|
||||
"""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_key (str): base58 encoded public key
|
||||
operation: filter transaction based on `operation`
|
||||
Returns:
|
||||
Iterator of transaction that list given owner in conditions.
|
||||
"""
|
||||
|
||||
248
bigchaindb/common/election.py
Normal file
248
bigchaindb/common/election.py
Normal file
@ -0,0 +1,248 @@
|
||||
# Copyright BigchainDB GmbH and BigchainDB contributors
|
||||
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
||||
# Code is Apache-2.0 and docs are CC-BY-4.0
|
||||
|
||||
import base58
|
||||
|
||||
from bigchaindb import backend
|
||||
from bigchaindb.common.exceptions import (InvalidSignature,
|
||||
MultipleInputsError,
|
||||
InvalidProposer,
|
||||
UnequalValidatorSet,
|
||||
DuplicateTransaction)
|
||||
from bigchaindb.tendermint_utils import key_from_base64
|
||||
from bigchaindb.common.crypto import (public_key_from_ed25519_key)
|
||||
from bigchaindb.common.transaction import Transaction
|
||||
from bigchaindb.common.schema import (_validate_schema,
|
||||
TX_SCHEMA_COMMON,
|
||||
TX_SCHEMA_CREATE)
|
||||
|
||||
|
||||
class Election(Transaction):
|
||||
|
||||
# NOTE: this transaction class extends create so the operation inheritance is achieved
|
||||
# by setting an ELECTION_TYPE and renaming CREATE = ELECTION_TYPE and ALLOWED_OPERATIONS = (ELECTION_TYPE,)
|
||||
ELECTION_TYPE = None
|
||||
# the name of the mongodb table managed by the election
|
||||
DB_TABLE = None
|
||||
# the model for votes issued by the election
|
||||
VOTE_TYPE = None
|
||||
# Election Statuses:
|
||||
ONGOING = 'ongoing'
|
||||
CONCLUDED = 'concluded'
|
||||
INCONCLUSIVE = 'inconclusive'
|
||||
# Vote ratio to approve an election
|
||||
ELECTION_THRESHOLD = 2 / 3
|
||||
|
||||
@classmethod
|
||||
def get_validator_change(cls, bigchain, height=None):
|
||||
"""Return the latest change to the validator set
|
||||
|
||||
:return: {
|
||||
'height': <block_height>,
|
||||
'asset': {
|
||||
'height': <block_height>,
|
||||
'validators': <validator_set>,
|
||||
'election_id': <election_id_that_approved_the_change>
|
||||
}
|
||||
}
|
||||
"""
|
||||
return bigchain.get_validator_change(height)
|
||||
|
||||
@classmethod
|
||||
def get_validators(cls, bigchain, height=None):
|
||||
"""Return a dictionary of validators with key as `public_key` and
|
||||
value as the `voting_power`
|
||||
"""
|
||||
validators = {}
|
||||
for validator in bigchain.get_validators(height):
|
||||
# NOTE: we assume that Tendermint encodes public key in base64
|
||||
public_key = public_key_from_ed25519_key(key_from_base64(validator['pub_key']['data']))
|
||||
validators[public_key] = validator['voting_power']
|
||||
|
||||
return validators
|
||||
|
||||
@classmethod
|
||||
def recipients(cls, bigchain):
|
||||
"""Convert validator dictionary to a recipient list for `Transaction`"""
|
||||
|
||||
recipients = []
|
||||
for public_key, voting_power in cls.get_validators(bigchain).items():
|
||||
recipients.append(([public_key], voting_power))
|
||||
|
||||
return recipients
|
||||
|
||||
@classmethod
|
||||
def is_same_topology(cls, current_topology, election_topology):
|
||||
voters = {}
|
||||
for voter in election_topology:
|
||||
if len(voter.public_keys) > 1:
|
||||
return False
|
||||
|
||||
[public_key] = voter.public_keys
|
||||
voting_power = voter.amount
|
||||
voters[public_key] = voting_power
|
||||
|
||||
# Check whether the voters and their votes is same to that of the
|
||||
# validators and their voting power in the network
|
||||
return current_topology == voters
|
||||
|
||||
def validate(self, bigchain, current_transactions=[]):
|
||||
"""Validate election transaction
|
||||
|
||||
NOTE:
|
||||
* A valid election is initiated by an existing validator.
|
||||
|
||||
* A valid election is one where voters are validators and votes are
|
||||
allocated according to the voting power of each validator node.
|
||||
|
||||
Args:
|
||||
:param bigchain: (BigchainDB) an instantiated bigchaindb.lib.BigchainDB object.
|
||||
:param current_transactions: (list) A list of transactions to be validated along with the election
|
||||
|
||||
Returns:
|
||||
ValidatorElection object
|
||||
|
||||
Raises:
|
||||
ValidationError: If the election is invalid
|
||||
"""
|
||||
input_conditions = []
|
||||
|
||||
duplicates = any(txn for txn in current_transactions if txn.id == self.id)
|
||||
if bigchain.get_transaction(self.id) or duplicates:
|
||||
raise DuplicateTransaction('transaction `{}` already exists'
|
||||
.format(self.id))
|
||||
|
||||
if not self.inputs_valid(input_conditions):
|
||||
raise InvalidSignature('Transaction signature is invalid.')
|
||||
|
||||
current_validators = self.get_validators(bigchain)
|
||||
|
||||
# NOTE: Proposer should be a single node
|
||||
if len(self.inputs) != 1 or len(self.inputs[0].owners_before) != 1:
|
||||
raise MultipleInputsError('`tx_signers` must be a list instance of length one')
|
||||
|
||||
# NOTE: Check if the proposer is a validator.
|
||||
[election_initiator_node_pub_key] = self.inputs[0].owners_before
|
||||
if election_initiator_node_pub_key not in current_validators.keys():
|
||||
raise InvalidProposer('Public key is not a part of the validator set')
|
||||
|
||||
# NOTE: Check if all validators have been assigned votes equal to their voting power
|
||||
if not self.is_same_topology(current_validators, self.outputs):
|
||||
raise UnequalValidatorSet('Validator set much be exactly same to the outputs of election')
|
||||
|
||||
return self
|
||||
|
||||
@classmethod
|
||||
def generate(cls, initiator, voters, election_data, metadata=None):
|
||||
(inputs, outputs) = cls.validate_create(initiator, voters, election_data, metadata)
|
||||
election = cls(cls.ELECTION_TYPE, {'data': election_data}, inputs, outputs, metadata)
|
||||
cls.validate_schema(election.to_dict(), skip_id=True)
|
||||
return election
|
||||
|
||||
@classmethod
|
||||
def validate_schema(cls, tx, skip_id=False):
|
||||
"""Validate the election transaction. Since `ELECTION` extends `CREATE` transaction, all the validations for
|
||||
`CREATE` transaction should be inherited
|
||||
"""
|
||||
if not skip_id:
|
||||
cls.validate_id(tx)
|
||||
_validate_schema(TX_SCHEMA_COMMON, tx)
|
||||
_validate_schema(TX_SCHEMA_CREATE, tx)
|
||||
|
||||
@classmethod
|
||||
def create(cls, tx_signers, recipients, metadata=None, asset=None):
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def transfer(cls, tx_signers, recipients, metadata=None, asset=None):
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def to_public_key(cls, election_id):
|
||||
return base58.b58encode(bytes.fromhex(election_id))
|
||||
|
||||
@classmethod
|
||||
def count_votes(cls, election_pk, transactions, getter=getattr):
|
||||
votes = 0
|
||||
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
|
||||
# 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(getter(output, 'public_keys')) == 1 and [election_pk] == getter(output, 'public_keys'):
|
||||
votes = votes + int(getter(output, 'amount'))
|
||||
return votes
|
||||
|
||||
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_key(bigchain.connection,
|
||||
self.id,
|
||||
election_pk))
|
||||
return self.count_votes(election_pk, txns, dict.get)
|
||||
|
||||
@classmethod
|
||||
def has_concluded(cls, bigchain, election_id, current_votes=[], height=None):
|
||||
"""Check if the given `election_id` can be concluded or not
|
||||
NOTE:
|
||||
* Election is concluded iff the current validator set is exactly equal
|
||||
to the validator set encoded in election outputs
|
||||
* Election can concluded only if the current votes achieves a supermajority
|
||||
"""
|
||||
election = bigchain.get_transaction(election_id)
|
||||
|
||||
if election:
|
||||
election_pk = election.to_public_key(election.id)
|
||||
votes_committed = election.get_commited_votes(bigchain, election_pk)
|
||||
votes_current = election.count_votes(election_pk, current_votes)
|
||||
current_validators = election.get_validators(bigchain, height)
|
||||
|
||||
if election.is_same_topology(current_validators, election.outputs):
|
||||
total_votes = sum(current_validators.values())
|
||||
if (votes_committed < (2/3)*total_votes) and \
|
||||
(votes_committed + votes_current >= (2/3)*total_votes):
|
||||
return election
|
||||
return False
|
||||
|
||||
def get_status(self, bigchain):
|
||||
concluded = self.get_election_result(self.id, bigchain)
|
||||
if concluded:
|
||||
return self.CONCLUDED
|
||||
|
||||
latest_change = self.get_validator_change(bigchain)
|
||||
latest_change_height = latest_change['height']
|
||||
election_height = bigchain.get_block_containing_tx(self.id)[0]
|
||||
|
||||
if latest_change_height >= election_height:
|
||||
return self.INCONCLUSIVE
|
||||
else:
|
||||
return self.ONGOING
|
||||
|
||||
def get_election_result(self, election_id, bigchain):
|
||||
result = bigchain.get_result_by_election_id(election_id, self.DB_TABLE)
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def is_approved(cls, bigchain, new_height, txns):
|
||||
votes = {}
|
||||
for txn in txns:
|
||||
if not isinstance(txn, cls.VOTE_TYPE):
|
||||
continue
|
||||
|
||||
election_id = txn.asset['id']
|
||||
election_votes = votes.get(election_id, [])
|
||||
election_votes.append(txn)
|
||||
votes[election_id] = election_votes
|
||||
|
||||
election = cls.has_concluded(bigchain, election_id, election_votes, new_height)
|
||||
# Once an election concludes any other conclusion for the same
|
||||
# or any other election is invalidated
|
||||
if election:
|
||||
return cls.on_approval(bigchain, election, new_height)
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
def on_approval(cls, bigchain, election, new_height):
|
||||
raise NotImplementedError
|
||||
@ -142,9 +142,9 @@ class App(BaseApplication):
|
||||
|
||||
# Check if the current block concluded any validator elections and
|
||||
# update the locally tracked validator set
|
||||
validator_updates = ValidatorElection.get_validator_update(self.bigchaindb,
|
||||
self.new_height,
|
||||
self.block_transactions)
|
||||
validator_updates = ValidatorElection.is_approved(self.bigchaindb,
|
||||
self.new_height,
|
||||
self.block_transactions)
|
||||
|
||||
# Store pre-commit state to recover in case there is a crash
|
||||
# during `commit`
|
||||
|
||||
@ -429,13 +429,10 @@ class BigchainDB(object):
|
||||
validators = result['validators']
|
||||
return validators
|
||||
|
||||
def get_validators_by_election_id(self, election_id):
|
||||
result = backend.query.get_validator_set_by_election_id(self.connection, election_id)
|
||||
def get_result_by_election_id(self, election_id, table):
|
||||
result = backend.query.get_result_by_election_id(self.connection, election_id, table)
|
||||
return result
|
||||
|
||||
def delete_validator_update(self):
|
||||
return backend.query.delete_validator_update(self.connection)
|
||||
|
||||
def store_pre_commit_state(self, state):
|
||||
return backend.query.store_pre_commit_state(self.connection, state)
|
||||
|
||||
|
||||
@ -2,263 +2,56 @@
|
||||
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
||||
# Code is Apache-2.0 and docs are CC-BY-4.0
|
||||
|
||||
import base58
|
||||
|
||||
from bigchaindb import backend
|
||||
from bigchaindb.common.exceptions import (InvalidSignature,
|
||||
MultipleInputsError,
|
||||
InvalidProposer,
|
||||
UnequalValidatorSet,
|
||||
InvalidPowerChange,
|
||||
DuplicateTransaction)
|
||||
from bigchaindb.tendermint_utils import key_from_base64
|
||||
from bigchaindb.common.crypto import (public_key_from_ed25519_key)
|
||||
from bigchaindb.common.transaction import Transaction
|
||||
from bigchaindb.common.exceptions import InvalidPowerChange
|
||||
from bigchaindb.common.election import Election
|
||||
from bigchaindb.common.schema import (_validate_schema,
|
||||
TX_SCHEMA_VALIDATOR_ELECTION,
|
||||
TX_SCHEMA_COMMON,
|
||||
TX_SCHEMA_CREATE)
|
||||
TX_SCHEMA_VALIDATOR_ELECTION)
|
||||
from . import ValidatorElectionVote
|
||||
from .validator_utils import (new_validator_set, encode_validator)
|
||||
|
||||
|
||||
class ValidatorElection(Transaction):
|
||||
class ValidatorElection(Election):
|
||||
|
||||
VALIDATOR_ELECTION = 'VALIDATOR_ELECTION'
|
||||
ELECTION_TYPE = 'VALIDATOR_ELECTION'
|
||||
# NOTE: this transaction class extends create so the operation inheritence is achieved
|
||||
# by renaming CREATE to VALIDATOR_ELECTION
|
||||
CREATE = VALIDATOR_ELECTION
|
||||
ALLOWED_OPERATIONS = (VALIDATOR_ELECTION,)
|
||||
# Election Statuses:
|
||||
ONGOING = 'ongoing'
|
||||
CONCLUDED = 'concluded'
|
||||
INCONCLUSIVE = 'inconclusive'
|
||||
ELECTION_THRESHOLD = 2 / 3
|
||||
|
||||
def __init__(self, operation, asset, inputs, outputs,
|
||||
metadata=None, version=None, hash_id=None):
|
||||
# operation `CREATE` is being passed as argument as `VALIDATOR_ELECTION` is an extension
|
||||
# of `CREATE` and any validation on `CREATE` in the parent class should apply to it
|
||||
super().__init__(operation, asset, inputs, outputs, metadata, version, hash_id)
|
||||
|
||||
@classmethod
|
||||
def get_validator_change(cls, bigchain, height=None):
|
||||
"""Return the latest change to the validator set
|
||||
|
||||
:return: {
|
||||
'height': <block_height>,
|
||||
'asset': {
|
||||
'height': <block_height>,
|
||||
'validators': <validator_set>,
|
||||
'election_id': <election_id_that_approved_the_change>
|
||||
}
|
||||
}
|
||||
"""
|
||||
return bigchain.get_validator_change(height)
|
||||
|
||||
@classmethod
|
||||
def get_validators(cls, bigchain, height=None):
|
||||
"""Return a dictionary of validators with key as `public_key` and
|
||||
value as the `voting_power`
|
||||
"""
|
||||
validators = {}
|
||||
for validator in bigchain.get_validators(height):
|
||||
# NOTE: we assume that Tendermint encodes public key in base64
|
||||
public_key = public_key_from_ed25519_key(key_from_base64(validator['pub_key']['data']))
|
||||
validators[public_key] = validator['voting_power']
|
||||
|
||||
return validators
|
||||
|
||||
@classmethod
|
||||
def recipients(cls, bigchain):
|
||||
"""Convert validator dictionary to a recipient list for `Transaction`"""
|
||||
|
||||
recipients = []
|
||||
for public_key, voting_power in cls.get_validators(bigchain).items():
|
||||
recipients.append(([public_key], voting_power))
|
||||
|
||||
return recipients
|
||||
|
||||
@classmethod
|
||||
def is_same_topology(cls, current_topology, election_topology):
|
||||
voters = {}
|
||||
for voter in election_topology:
|
||||
if len(voter.public_keys) > 1:
|
||||
return False
|
||||
|
||||
[public_key] = voter.public_keys
|
||||
voting_power = voter.amount
|
||||
voters[public_key] = voting_power
|
||||
|
||||
# Check whether the voters and their votes is same to that of the
|
||||
# validators and their voting power in the network
|
||||
return (current_topology == voters)
|
||||
CREATE = ELECTION_TYPE
|
||||
ALLOWED_OPERATIONS = (ELECTION_TYPE,)
|
||||
DB_TABLE = 'validators'
|
||||
VOTE_TYPE = ValidatorElectionVote
|
||||
|
||||
def validate(self, bigchain, current_transactions=[]):
|
||||
"""Validate election transaction
|
||||
For more details refer BEP-21: https://github.com/bigchaindb/BEPs/tree/master/21
|
||||
|
||||
NOTE:
|
||||
* A valid election is initiated by an existing validator.
|
||||
|
||||
* A valid election is one where voters are validators and votes are
|
||||
alloacted according to the voting power of each validator node.
|
||||
|
||||
Args:
|
||||
bigchain (BigchainDB): an instantiated bigchaindb.lib.BigchainDB object.
|
||||
|
||||
Returns:
|
||||
ValidatorElection object
|
||||
|
||||
Raises:
|
||||
ValidationError: If the election is invalid
|
||||
"""For more details refer BEP-21: https://github.com/bigchaindb/BEPs/tree/master/21
|
||||
"""
|
||||
input_conditions = []
|
||||
|
||||
duplicates = any(txn for txn in current_transactions if txn.id == self.id)
|
||||
if bigchain.get_transaction(self.id) or duplicates:
|
||||
raise DuplicateTransaction('transaction `{}` already exists'
|
||||
.format(self.id))
|
||||
|
||||
if not self.inputs_valid(input_conditions):
|
||||
raise InvalidSignature('Transaction signature is invalid.')
|
||||
|
||||
current_validators = self.get_validators(bigchain)
|
||||
|
||||
# NOTE: Proposer should be a single node
|
||||
if len(self.inputs) != 1 or len(self.inputs[0].owners_before) != 1:
|
||||
raise MultipleInputsError('`tx_signers` must be a list instance of length one')
|
||||
super(ValidatorElection, self).validate(bigchain, current_transactions=current_transactions)
|
||||
|
||||
# NOTE: change more than 1/3 of the current power is not allowed
|
||||
if self.asset['data']['power'] >= (1/3)*sum(current_validators.values()):
|
||||
raise InvalidPowerChange('`power` change must be less than 1/3 of total power')
|
||||
|
||||
# NOTE: Check if the proposer is a validator.
|
||||
[election_initiator_node_pub_key] = self.inputs[0].owners_before
|
||||
if election_initiator_node_pub_key not in current_validators.keys():
|
||||
raise InvalidProposer('Public key is not a part of the validator set')
|
||||
|
||||
# NOTE: Check if all validators have been assigned votes equal to their voting power
|
||||
if not self.is_same_topology(current_validators, self.outputs):
|
||||
raise UnequalValidatorSet('Validator set much be exactly same to the outputs of election')
|
||||
|
||||
return self
|
||||
|
||||
@classmethod
|
||||
def generate(cls, initiator, voters, election_data, metadata=None):
|
||||
(inputs, outputs) = cls.validate_create(initiator, voters, election_data, metadata)
|
||||
election = cls(cls.VALIDATOR_ELECTION, {'data': election_data}, inputs, outputs, metadata)
|
||||
cls.validate_schema(election.to_dict(), skip_id=True)
|
||||
return election
|
||||
|
||||
@classmethod
|
||||
def validate_schema(cls, tx, skip_id=False):
|
||||
"""Validate the validator election transaction. Since `VALIDATOR_ELECTION` extends `CREATE`
|
||||
transaction, all the validations for `CREATE` transaction should be inherited
|
||||
"""Validate the validator election transaction. Since `VALIDATOR_ELECTION` extends `ELECTION`
|
||||
transaction, all the validations for `ELECTION` transaction are covered by `super`
|
||||
"""
|
||||
if not skip_id:
|
||||
cls.validate_id(tx)
|
||||
_validate_schema(TX_SCHEMA_COMMON, tx)
|
||||
_validate_schema(TX_SCHEMA_CREATE, tx)
|
||||
|
||||
super(ValidatorElection, cls).validate_schema(tx, skip_id=skip_id)
|
||||
|
||||
_validate_schema(TX_SCHEMA_VALIDATOR_ELECTION, tx)
|
||||
|
||||
@classmethod
|
||||
def create(cls, tx_signers, recipients, metadata=None, asset=None):
|
||||
raise NotImplementedError
|
||||
def on_approval(cls, bigchain, election, new_height):
|
||||
# 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,
|
||||
validator_updates)
|
||||
|
||||
@classmethod
|
||||
def transfer(cls, tx_signers, recipients, metadata=None, asset=None):
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def to_public_key(cls, election_id):
|
||||
return base58.b58encode(bytes.fromhex(election_id))
|
||||
|
||||
@classmethod
|
||||
def count_votes(cls, election_pk, transactions, getter=getattr):
|
||||
votes = 0
|
||||
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
|
||||
# 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(getter(output, 'public_keys')) == 1 and [election_pk] == getter(output, 'public_keys'):
|
||||
votes = votes + int(getter(output, 'amount'))
|
||||
return votes
|
||||
|
||||
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_key(bigchain.connection,
|
||||
self.id,
|
||||
election_pk))
|
||||
return self.count_votes(election_pk, txns, dict.get)
|
||||
|
||||
@classmethod
|
||||
def has_concluded(cls, bigchain, election_id, current_votes=[], height=None):
|
||||
"""Check if the given `election_id` can be concluded or not
|
||||
NOTE:
|
||||
* Election is concluded iff the current validator set is exactly equal
|
||||
to the validator set encoded in election outputs
|
||||
* Election can concluded only if the current votes achieves a supermajority
|
||||
"""
|
||||
election = bigchain.get_transaction(election_id)
|
||||
|
||||
if election:
|
||||
election_pk = election.to_public_key(election.id)
|
||||
votes_commited = election.get_commited_votes(bigchain, election_pk)
|
||||
votes_current = election.count_votes(election_pk, current_votes)
|
||||
current_validators = election.get_validators(bigchain, height)
|
||||
|
||||
if election.is_same_topology(current_validators, election.outputs):
|
||||
total_votes = sum(current_validators.values())
|
||||
if (votes_commited < (2/3)*total_votes) and \
|
||||
(votes_commited + votes_current >= (2/3)*total_votes):
|
||||
return election
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def get_validator_update(cls, bigchain, new_height, txns):
|
||||
votes = {}
|
||||
for txn in txns:
|
||||
if not isinstance(txn, ValidatorElectionVote):
|
||||
continue
|
||||
|
||||
election_id = txn.asset['id']
|
||||
election_votes = votes.get(election_id, [])
|
||||
election_votes.append(txn)
|
||||
votes[election_id] = election_votes
|
||||
|
||||
election = cls.has_concluded(bigchain, election_id, election_votes, new_height)
|
||||
# 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,
|
||||
validator_updates)
|
||||
|
||||
updated_validator_set = [v for v in updated_validator_set if v['voting_power'] > 0]
|
||||
bigchain.store_validator_set(new_height+1, updated_validator_set, election.id)
|
||||
return [encode_validator(election.asset['data'])]
|
||||
return []
|
||||
|
||||
def get_validator_update_by_election_id(self, election_id, bigchain):
|
||||
result = bigchain.get_validators_by_election_id(election_id)
|
||||
return result
|
||||
|
||||
def get_status(self, bigchain):
|
||||
concluded = self.get_validator_update_by_election_id(self.id, bigchain)
|
||||
if concluded:
|
||||
return self.CONCLUDED
|
||||
|
||||
latest_change = self.get_validator_change(bigchain)
|
||||
latest_change_height = latest_change['height']
|
||||
election_height = bigchain.get_block_containing_tx(self.id)[0]
|
||||
|
||||
if latest_change_height >= election_height:
|
||||
return self.INCONCLUSIVE
|
||||
else:
|
||||
return self.ONGOING
|
||||
updated_validator_set = [v for v in updated_validator_set if v['voting_power'] > 0]
|
||||
bigchain.store_validator_set(new_height+1, updated_validator_set, election.id)
|
||||
return [encode_validator(election.asset['data'])]
|
||||
|
||||
2
setup.py
2
setup.py
@ -91,7 +91,7 @@ install_requires = [
|
||||
'pyyaml~=3.12',
|
||||
'aiohttp~=3.0',
|
||||
'bigchaindb-abci==0.5.1',
|
||||
'setproctitle~=1.1.0',
|
||||
'setproctitle~=1.1.0', 'base58'
|
||||
]
|
||||
|
||||
setup(
|
||||
|
||||
@ -189,7 +189,7 @@ def test_end_block_return_validator_updates(b, init_chain_request):
|
||||
resp = app.end_block(RequestEndBlock(height=99))
|
||||
assert resp.validator_updates[0] == encode_validator(validator)
|
||||
|
||||
updates = b.get_validator_update()
|
||||
updates = b.is_approved()
|
||||
assert updates == []
|
||||
|
||||
|
||||
|
||||
@ -163,11 +163,11 @@ def test_validator_updates(b, validator_pub_key):
|
||||
'update_id': VALIDATOR_UPDATE_ID}
|
||||
query.store_validator_update(b.connection, validator_update)
|
||||
|
||||
updates = b.get_validator_update()
|
||||
updates = b.is_approved()
|
||||
assert updates == [validator_update['validator']]
|
||||
|
||||
b.delete_validator_update()
|
||||
assert b.get_validator_update() == []
|
||||
assert b.is_approved() == []
|
||||
|
||||
|
||||
@pytest.mark.bdb
|
||||
|
||||
@ -299,17 +299,17 @@ def test_get_validator_update(b, node_keys, node_key, ed25519_node_keys):
|
||||
assert not ValidatorElection.has_concluded(b, election.id, [tx_vote0, tx_vote1])
|
||||
assert ValidatorElection.has_concluded(b, election.id, [tx_vote0, tx_vote1, tx_vote2])
|
||||
|
||||
assert ValidatorElection.get_validator_update(b, 4, [tx_vote0]) == []
|
||||
assert ValidatorElection.get_validator_update(b, 4, [tx_vote0, tx_vote1]) == []
|
||||
assert ValidatorElection.is_approved(b, 4, [tx_vote0]) == []
|
||||
assert ValidatorElection.is_approved(b, 4, [tx_vote0, tx_vote1]) == []
|
||||
|
||||
update = ValidatorElection.get_validator_update(b, 4, [tx_vote0, tx_vote1, tx_vote2])
|
||||
update = ValidatorElection.is_approved(b, 4, [tx_vote0, tx_vote1, tx_vote2])
|
||||
update_public_key = codecs.encode(update[0].pub_key.data, 'base64').decode().rstrip('\n')
|
||||
assert len(update) == 1
|
||||
assert update_public_key == public_key64
|
||||
|
||||
b.store_bulk_transactions([tx_vote0, tx_vote1])
|
||||
|
||||
update = ValidatorElection.get_validator_update(b, 4, [tx_vote2])
|
||||
update = ValidatorElection.is_approved(b, 4, [tx_vote2])
|
||||
update_public_key = codecs.encode(update[0].pub_key.data, 'base64').decode().rstrip('\n')
|
||||
assert len(update) == 1
|
||||
assert update_public_key == public_key64
|
||||
@ -332,7 +332,7 @@ def test_get_validator_update(b, node_keys, node_key, ed25519_node_keys):
|
||||
|
||||
b.store_bulk_transactions([tx_vote0, tx_vote1])
|
||||
|
||||
update = ValidatorElection.get_validator_update(b, 9, [tx_vote2])
|
||||
update = ValidatorElection.is_approved(b, 9, [tx_vote2])
|
||||
update_public_key = codecs.encode(update[0].pub_key.data, 'base64').decode().rstrip('\n')
|
||||
assert len(update) == 1
|
||||
assert update_public_key == public_key64
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user